Value ValueMap::find_insert(Value x) { const intx hash = x->hash(); if (hash != 0) { // 0 hash means: exclude from value numbering NOT_PRODUCT(_number_of_finds++); for (ValueMapEntry* entry = entry_at(entry_index(hash, size())); entry != NULL; entry = entry->next()) { if (entry->hash() == hash) { Value f = entry->value(); if (!is_killed(f) && f->is_equal(x)) { NOT_PRODUCT(_number_of_hits++); TRACE_VALUE_NUMBERING(tty->print_cr("Value Numbering: %s %c%d equal to %c%d (size %d, entries %d, nesting-diff %d)", x->name(), x->type()->tchar(), x->id(), f->type()->tchar(), f->id(), size(), entry_count(), nesting() - entry->nesting())); if (entry->nesting() != nesting() && f->as_Constant() == NULL) { // non-constant values of of another block must be pinned, // otherwise it is possible that they are not evaluated f->pin(Instruction::PinGlobalValueNumbering); } assert(x->type()->tag() == f->type()->tag(), "should have same type"); return f; } } } // x not found, so insert it if (entry_count() >= size_threshold()) { increase_table_size(); } int idx = entry_index(hash, size()); _entries.at_put(idx, new ValueMapEntry(hash, x, nesting(), entry_at(idx))); _entry_count++; TRACE_VALUE_NUMBERING(tty->print_cr("Value Numbering: insert %s %c%d (size %d, entries %d, nesting %d)", x->name(), x->type()->tchar(), x->id(), size(), entry_count(), nesting())); } return x; }
bool Run(Node * node) { const Tensor * input_tensor=node->GetInputTensor(0); Tensor * output_tensor=node->GetOutputTensor(0); const std::vector<int>& dims=input_tensor->GetShape().GetDim(); float *input = (float *)get_tensor_mem(input_tensor); float *output = (float *)get_tensor_mem(output_tensor); Region *reorg_op = dynamic_cast<Region *>(node->GetOp()); RegionParam *param = reorg_op->GetParam(); int hw = dims[2] * dims[3]; int chw = dims[1] * hw; int nchw = dims[0] * chw; int num_box = param->num_box; int num_class = param->num_classes; int coords = param->coords; memcpy(output, input, nchw * sizeof(float)); for (int b = 0; b < dims[0]; b++) { for (int n = 0; n < num_box; n++) { int index = entry_index(b, n * hw, 0, hw, chw, num_class); logit_activate_array(output + index, 2 * hw); index = entry_index(b, n * hw, coords, hw, chw, num_class); logit_activate_array(output + index, hw); index = entry_index(b, n * hw, coords + 1, hw, chw, num_class); } } int index = entry_index(0, 0, coords + 1, hw, chw, num_class); softmax_cpu(input + index, num_class, dims[0] * num_box, chw / num_box, hw, hw, output + index); return true; }
void ValueMap::increase_table_size() { int old_size = size(); int new_size = old_size * 2 + 1; ValueMapEntryList worklist(8); ValueMapEntryArray new_entries(new_size, NULL); int new_entry_count = 0; TRACE_VALUE_NUMBERING(tty->print_cr("increasing table size from %d to %d", old_size, new_size)); for (int i = old_size - 1; i >= 0; i--) { ValueMapEntry* entry; for (entry = entry_at(i); entry != NULL; entry = entry->next()) { if (!is_killed(entry->value())) { worklist.push(entry); } } while (!worklist.is_empty()) { entry = worklist.pop(); int new_index = entry_index(entry->hash(), new_size); if (entry->nesting() != nesting() && new_entries.at(new_index) != entry->next()) { // changing entries with a lower nesting than the current nesting of the table // is not allowed because then the same entry is contained in multiple value maps. // clone entry when next-pointer must be changed entry = new ValueMapEntry(entry->hash(), entry->value(), entry->nesting(), NULL); } entry->set_next(new_entries.at(new_index)); new_entries.at_put(new_index, entry); new_entry_count++; } } _entries = new_entries; _entry_count = new_entry_count; }
void get_region_detections(layer l, int w, int h, int netw, int neth, float thresh, int *map, float tree_thresh, int relative, detection *dets) { int i, j, n, z; float *predictions = l.output; if (l.batch == 2) { float *flip = l.output + l.outputs; for (j = 0; j < l.h; ++j) { for (i = 0; i < l.w / 2; ++i) { for (n = 0; n < l.n; ++n) { for (z = 0; z < l.classes + l.coords + 1; ++z) { int i1 = z*l.w*l.h*l.n + n*l.w*l.h + j*l.w + i; int i2 = z*l.w*l.h*l.n + n*l.w*l.h + j*l.w + (l.w - i - 1); float swap = flip[i1]; flip[i1] = flip[i2]; flip[i2] = swap; if (z == 0) { flip[i1] = -flip[i1]; flip[i2] = -flip[i2]; } } } } } for (i = 0; i < l.outputs; ++i) { l.output[i] = (l.output[i] + flip[i]) / 2.; } } for (i = 0; i < l.w*l.h; ++i) { int row = i / l.w; int col = i % l.w; for (n = 0; n < l.n; ++n) { int index = n*l.w*l.h + i; for (j = 0; j < l.classes; ++j) { dets[index].prob[j] = 0; } int obj_index = entry_index(l, 0, n*l.w*l.h + i, l.coords); int box_index = entry_index(l, 0, n*l.w*l.h + i, 0); int mask_index = entry_index(l, 0, n*l.w*l.h + i, 4); float scale = l.background ? 1 : predictions[obj_index]; dets[index].bbox = get_region_box(predictions, l.biases, n, box_index, col, row, l.w, l.h);// , l.w*l.h); dets[index].objectness = scale > thresh ? scale : 0; if (dets[index].mask) { for (j = 0; j < l.coords - 4; ++j) { dets[index].mask[j] = l.output[mask_index + j*l.w*l.h]; } } int class_index = entry_index(l, 0, n*l.w*l.h + i, l.coords + !l.background); if (l.softmax_tree) { hierarchy_predictions(predictions + class_index, l.classes, l.softmax_tree, 0);// , l.w*l.h); if (map) { for (j = 0; j < 200; ++j) { int class_index = entry_index(l, 0, n*l.w*l.h + i, l.coords + 1 + map[j]); float prob = scale*predictions[class_index]; dets[index].prob[j] = (prob > thresh) ? prob : 0; } } else { int j = hierarchy_top_prediction(predictions + class_index, l.softmax_tree, tree_thresh, l.w*l.h); dets[index].prob[j] = (scale > thresh) ? scale : 0; } } else { if (dets[index].objectness) { for (j = 0; j < l.classes; ++j) { int class_index = entry_index(l, 0, n*l.w*l.h + i, l.coords + 1 + j); float prob = scale*predictions[class_index]; dets[index].prob[j] = (prob > thresh) ? prob : 0; } } } } } correct_region_boxes(dets, l.w*l.h*l.n, w, h, netw, neth, relative); }