MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / forward_impl

Function forward_impl

dnn/src/naive/roi_align/opr_impl.cpp:15–66  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13
14template <typename T, typename Pooler>
15void forward_impl(
16 _megdnn_tensor_in src, _megdnn_tensor_in rois, _megdnn_tensor_in dst,
17 _megdnn_tensor_out index, float spatial_scale, float offset,
18 const int sample_height, const int sample_width) {
19 size_t channels = src.layout[1], hi = src.layout[2], wi = src.layout[3];
20 size_t pooled_height = dst.layout[2], pooled_width = dst.layout[3];
21
22 size_t total_nr_elems = dst.layout.total_nr_elems();
23 int height = hi, width = wi;
24 for (size_t idx = 0; idx < total_nr_elems; ++idx) {
25 int pw = idx % pooled_width;
26 int ph = (idx / pooled_width) % pooled_height;
27 int c = (idx / pooled_width / pooled_height) % channels;
28 int n = idx / pooled_width / pooled_height / channels;
29
30 auto rois_ptr = rois.ptr<T>() + n * 5;
31 int roi_batch_ind = rois_ptr[0];
32 float roi_start_w = rois_ptr[1] * spatial_scale - offset;
33 float roi_start_h = rois_ptr[2] * spatial_scale - offset;
34 float roi_end_w = rois_ptr[3] * spatial_scale - offset;
35 float roi_end_h = rois_ptr[4] * spatial_scale - offset;
36
37 float roi_width = std::max(roi_end_w - roi_start_w, ((float)(0.0)));
38 float roi_height = std::max(roi_end_h - roi_start_h, ((float)(0.0)));
39 float bin_size_h =
40 static_cast<float>(roi_height) / static_cast<float>(pooled_height);
41 float bin_size_w =
42 static_cast<float>(roi_width) / static_cast<float>(pooled_width);
43
44 auto feat_map_ptr =
45 src.ptr<T>() + (roi_batch_ind * channels + c) * height * width;
46 float sample_h_rate = 1.0f / float(sample_height);
47 float sample_w_rate = 1.0f / float(sample_width);
48 float hcenter;
49 float wcenter;
50
51 Pooler pooler;
52 for (int h_iter = 0; h_iter < sample_height; ++h_iter) {
53 for (int w_iter = 0; w_iter < sample_width; ++w_iter) {
54 hcenter = roi_start_h +
55 bin_size_h * (ph + sample_h_rate * (h_iter + 0.5f));
56 wcenter = roi_start_w +
57 bin_size_w * (pw + sample_w_rate * (w_iter + 0.5f));
58 T val = bilinear_interp(feat_map_ptr, hcenter, wcenter, height, width);
59 int idx = h_iter * sample_width + w_iter;
60 pooler.feed(val, idx);
61 }
62 }
63 pooler.writeback_val(dst.ptr<T>()[idx]);
64 pooler.writeback_idx(index.ptr<dt_int32>()[idx]);
65 }
66}
67
68template <typename T>
69void forward(

Callers

nothing calls this directly

Calls 6

maxFunction · 0.85
bilinear_interpFunction · 0.50
total_nr_elemsMethod · 0.45
feedMethod · 0.45
writeback_valMethod · 0.45
writeback_idxMethod · 0.45

Tested by

no test coverage detected