brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
363 / 374
97.1%
Branches:
107 / 174
61.5%
Functions:
44 / 44
100.0%

src/kernels/mir_vision.cpp
Line Branch Exec Source
1 #include "mir_vision.h"
2 #include "mir_common.h"
3 #include "mir_math.h"
4
5 #include "../vulkan/shaders/op_codes.h"
6
7 #include <string>
8 #include <vector>
9
10 namespace brotensor::kernels {
11
12 namespace {
13
14 26 Value* min_i32(Ctx& c, Value* a, Value* b) {
15 26 return c.b.build_select(c.b.build_slt(a, b), a, b);
16 }
17
18 26 Value* max_i32(Ctx& c, Value* a, Value* b) {
19 26 return c.b.build_select(c.b.build_sgt(a, b), a, b);
20 }
21
22 13 void build_upfirdn(Ctx& c, const std::vector<Value*>& p, Dtype dt, bool flip) {
23 // p: X(0) f(1) Y(2) NC(3) Hin(4) Win(5) Hout(6) Wout(7) fH(8) fW(9)
24 // upx(10) upy(11) downx(12) downy(13) px0(14) py0(15) gain(16)
25 13 Value* NC = p[3];
26 13 Value* Hin = p[4];
27 13 Value* Win = p[5];
28 13 Value* Hout = p[6];
29 13 Value* Wout = p[7];
30 13 Value* fH = p[8];
31 13 Value* fW = p[9];
32 13 Value* upx = p[10];
33 13 Value* upy = p[11];
34 13 Value* downx = p[12];
35 13 Value* downy = p[13];
36 13 Value* px0 = p[14];
37 13 Value* py0 = p[15];
38 13 Value* gain = p[16];
39 13 Value* Hu = c.b.build_mul(Hin, upy);
40 13 Value* Wu = c.b.build_mul(Win, upx);
41 13 Value* plane = c.b.build_mul(Hout, Wout);
42 13 Value* in_plane = c.b.build_mul(Hin, Win);
43 13 Value* total = c.b.build_mul(NC, plane);
44
45
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
26 emit_vec_grid_stride(c, total, 1, kVisionBlock, [&](Value* i) {
46 13 Value* nc = c.b.build_udiv(i, plane);
47 13 Value* rem = c.b.build_sub(i, c.b.build_mul(nc, plane));
48 13 Value* oh = c.b.build_udiv(rem, Wout);
49 13 Value* ow = c.b.build_sub(rem, c.b.build_mul(oh, Wout));
50 13 Value* by = c.b.build_sub(c.b.build_mul(oh, downy), py0);
51 13 Value* bx = c.b.build_sub(c.b.build_mul(ow, downx), px0);
52
53 13 Value* kh0a = max_i32(c, c.i32(0), c.b.build_neg(by));
54 13 Value* rh = c.b.build_umod(c.b.build_add(by, kh0a), upy);
55 26 Value* kh0 = c.b.build_select(c.b.build_ne(rh, c.i32(0)),
56 13 c.b.build_add(kh0a, c.b.build_sub(upy, rh)), kh0a);
57 13 Value* kh1 = min_i32(c, fH, c.b.build_sub(Hu, by));
58
59 13 Value* kw0a = max_i32(c, c.i32(0), c.b.build_neg(bx));
60 13 Value* rw = c.b.build_umod(c.b.build_add(bx, kw0a), upx);
61 26 Value* kw0 = c.b.build_select(c.b.build_ne(rw, c.i32(0)),
62 13 c.b.build_add(kw0a, c.b.build_sub(upx, rw)), kw0a);
63 13 Value* kw1 = min_i32(c, fW, c.b.build_sub(Wu, bx));
64
65 13 Value* ib = c.b.build_mul(nc, in_plane);
66
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
26 Value* acc = c.kb.for_range_reduce(kh0, kh1, upy, c.f32(0.0f), [&](Value* kh, Value* row_acc) {
67 13 Value* iy = c.b.build_udiv(c.b.build_add(by, kh), upy);
68
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
13 Value* fy = flip ? kh : c.b.build_sub(c.b.build_sub(fH, c.i32(1)), kh);
69 13 Value* xr = c.b.build_add(ib, c.b.build_mul(iy, Win));
70 13 Value* fr = c.b.build_mul(fy, fW);
71
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
26 return c.kb.for_range_reduce(kw0, kw1, upx, row_acc, [&](Value* kw, Value* cur) {
72 13 Value* ix = c.b.build_udiv(c.b.build_add(bx, kw), upx);
73
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
13 Value* fx = flip ? kw : c.b.build_sub(c.b.build_sub(fW, c.i32(1)), kw);
74 13 Value* xv = load_elem_dt(c, dt, p[0], c.b.build_add(xr, ix));
75 13 Value* fv = load_elem_dt(c, dt, p[1], c.b.build_add(fr, fx));
76 13 return c.b.build_add(cur, c.b.build_mul(xv, fv));
77 });
78 ✗ });
79 13 store_elem_dt(c, dt, p[2], i, c.b.build_mul(acc, gain));
80 13 });
81 13 }
82
83 8 void build_modw(Ctx& c, const std::vector<Value*>& p, Dtype dt, bool demodulate, bool from_dcoef) {
84 // p: W(0) s(1) Wn(2) dcoef(3) N(4) Cout(5) Cin(6) kk(7) eps(8)
85 8 Value* N = p[4];
86 8 Value* Cout = p[5];
87 8 Value* Cin = p[6];
88 8 Value* kk = p[7];
89 8 Value* eps = p[8];
90 8 Value* wk = c.b.build_mul(Cin, kk);
91 8 Value* total = c.b.build_mul(N, Cout);
92 8 Value* tid = c.kb.tid_x();
93 8 Value* block = c.i32(static_cast<int>(kVisionBlock));
94
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 3 times.
8 Value* s_red = (!from_dcoef && demodulate) ? c.kb.shared_alloc_f32(8) : nullptr;
95
96
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
16 c.kb.for_range(c.kb.ctaid_x(), total, c.kb.nctaid_x(), [&](Value* no) {
97 8 Value* n = c.b.build_udiv(no, Cout);
98 8 Value* o = c.b.build_sub(no, c.b.build_mul(n, Cout));
99 8 Value* w_base = c.b.build_mul(o, wk);
100 8 Value* s_base = c.b.build_mul(n, Cin);
101 8 Value* wn_base = c.b.build_mul(no, wk);
102
103 8 Value* dc = c.f32(1.0f);
104
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (demodulate) {
105
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (from_dcoef) {
106 3 dc = c.kb.load_f32(ptr_at(c, p[3], no, 4));
107 3 } else {
108
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 Value* ss = c.kb.for_range_reduce(tid, wk, block, c.f32(0.0f), [&](Value* j, Value* cur) {
109 3 Value* ic = c.b.build_udiv(j, kk);
110 6 Value* w = c.b.build_mul(load_elem_dt(c, dt, p[0], c.b.build_add(w_base, j)),
111 3 load_elem_dt(c, dt, p[1], c.b.build_add(s_base, ic)));
112 3 return c.b.build_add(cur, c.b.build_mul(w, w));
113 });
114 3 Value* sum_sq = emit_block_sum_f32(c, s_red, ss);
115 3 dc = c.b.build_sdiv(c.f32(1.0f), c.b.build_sqrt_f32(c.b.build_add(sum_sq, eps)));
116 }
117 6 }
118
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
16 c.kb.for_range(tid, wk, block, [&](Value* j) {
119 8 Value* ic = c.b.build_udiv(j, kk);
120 16 Value* w = c.b.build_mul(load_elem_dt(c, dt, p[0], c.b.build_add(w_base, j)),
121 8 load_elem_dt(c, dt, p[1], c.b.build_add(s_base, ic)));
122
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 Value* out = demodulate ? c.b.build_mul(w, dc) : w;
123 8 store_elem_dt(c, dt, p[2], c.b.build_add(wn_base, j), out);
124 8 });
125
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (!from_dcoef) {
126
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 c.kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] { c.kb.store_f32(ptr_at(c, p[3], no, 4), dc); });
127 4 c.kb.sync();
128 4 }
129 8 });
130 8 }
131
132 4 void build_modw_bwd(Ctx& c, const std::vector<Value*>& p, Dtype dt) {
133 // p: W(0) s(1) dw(2) dcoef(3) N(4) Cout(5) Cin(6) kk(7)
134 4 Value* Cout = p[5];
135 4 Value* Cin = p[6];
136 4 Value* kk = p[7];
137 4 Value* wk = c.b.build_mul(Cin, kk);
138 4 Value* total = c.b.build_mul(p[4], Cout);
139 4 Value* tid = c.kb.tid_x();
140 4 Value* block = c.i32(static_cast<int>(kVisionBlock));
141 4 Value* s_red = c.kb.shared_alloc_f32(8);
142
143
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 c.kb.for_range(c.kb.ctaid_x(), total, c.kb.nctaid_x(), [&](Value* no) {
144 4 Value* n = c.b.build_udiv(no, Cout);
145 4 Value* o = c.b.build_sub(no, c.b.build_mul(n, Cout));
146 4 Value* w_base = c.b.build_mul(o, wk);
147 4 Value* s_base = c.b.build_mul(n, Cin);
148 4 Value* dw_base = c.b.build_mul(no, wk);
149
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 Value* g0 = c.kb.for_range_reduce(tid, wk, block, c.f32(0.0f), [&](Value* j, Value* cur) {
150 4 Value* ic = c.b.build_udiv(j, kk);
151 8 Value* wp = c.b.build_mul(load_elem_dt(c, dt, p[0], c.b.build_add(w_base, j)),
152 4 load_elem_dt(c, dt, p[1], c.b.build_add(s_base, ic)));
153 4 return c.b.build_add(cur, c.b.build_mul(c.kb.load_f32(ptr_at(c, p[2], c.b.build_add(dw_base, j), 4)), wp));
154 });
155 4 Value* g = emit_block_sum_f32(c, s_red, g0);
156 4 Value* dc = c.kb.load_f32(ptr_at(c, p[3], no, 4));
157 4 Value* gd3 = c.b.build_mul(c.b.build_mul(c.b.build_mul(g, dc), dc), dc);
158
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 c.kb.for_range(tid, wk, block, [&](Value* j) {
159 4 Value* ic = c.b.build_udiv(j, kk);
160 8 Value* wp = c.b.build_mul(load_elem_dt(c, dt, p[0], c.b.build_add(w_base, j)),
161 4 load_elem_dt(c, dt, p[1], c.b.build_add(s_base, ic)));
162 4 Value* ptr = ptr_at(c, p[2], c.b.build_add(dw_base, j), 4);
163 4 Value* dwn = c.b.build_sub(c.b.build_mul(c.kb.load_f32(ptr), dc), c.b.build_mul(gd3, wp));
164 4 c.kb.store_f32(ptr, dwn);
165 4 });
166 4 });
167 4 }
168
169 6 void build_modw_ds_dw(Ctx& c, const std::vector<Value*>& p, Dtype dt, bool is_dw) {
170 // ds: W(0) dw(1) ds(2) N(3) Cout(4) Cin(5) kk(6)
171 // dw: s(0) dw(1) dW(2) N(3) Cout(4) Cin(5) kk(6)
172 6 Value* N = p[3];
173 6 Value* Cout = p[4];
174 6 Value* Cin = p[5];
175 6 Value* kk = p[6];
176 6 Value* wk = c.b.build_mul(Cin, kk);
177
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (!is_dw) {
178
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 emit_vec_grid_stride(c, c.b.build_mul(N, Cin), 1, kVisionBlock, [&](Value* ni) {
179 3 Value* n = c.b.build_udiv(ni, Cin);
180 3 Value* ic = c.b.build_sub(ni, c.b.build_mul(n, Cin));
181 3 Value* nc_base = c.b.build_mul(n, Cout);
182 3 Value* ik = c.b.build_mul(ic, kk);
183
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 Value* acc = c.kb.for_range_reduce(c.i32(0), Cout, c.i32(1), c.f32(0.0f), [&](Value* o, Value* r_acc) {
184 3 Value* dw_b = c.b.build_mul(c.b.build_add(nc_base, o), wk);
185 3 Value* w_b = c.b.build_mul(o, wk);
186
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 return c.kb.for_range_reduce(c.i32(0), kk, c.i32(1), r_acc, [&](Value* t, Value* cur) {
187 3 Value* j = c.b.build_add(ik, t);
188 3 Value* dwp = c.kb.load_f32(ptr_at(c, p[1], c.b.build_add(dw_b, j), 4));
189 3 Value* w = load_elem_dt(c, dt, p[0], c.b.build_add(w_b, j));
190 3 return c.b.build_add(cur, c.b.build_mul(dwp, w));
191 });
192 ✗ });
193 3 store_elem_dt(c, dt, p[2], ni, acc);
194 3 });
195 3 } else {
196
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 emit_vec_grid_stride(c, c.b.build_mul(Cout, wk), 1, kVisionBlock, [&](Value* oj) {
197 3 Value* o = c.b.build_udiv(oj, wk);
198 3 Value* j = c.b.build_sub(oj, c.b.build_mul(o, wk));
199 3 Value* ic = c.b.build_udiv(j, kk);
200
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 Value* acc = c.kb.for_range_reduce(c.i32(0), N, c.i32(1), c.f32(0.0f), [&](Value* n, Value* cur) {
201 3 Value* idx_dw = c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(n, Cout), o), wk), j);
202 3 Value* idx_s = c.b.build_add(c.b.build_mul(n, Cin), ic);
203 6 return c.b.build_add(cur, c.b.build_mul(c.kb.load_f32(ptr_at(c, p[1], idx_dw, 4)),
204 3 load_elem_dt(c, dt, p[0], idx_s)));
205 });
206 3 store_elem_dt(c, dt, p[2], oj, c.b.build_add(load_elem_dt(c, dt, p[2], oj), acc));
207 3 });
208 }
209 6 }
210
211 6 void build_col2im2d(Ctx& c, const std::vector<Value*>& p, Dtype dt, bool has_bias, bool cols_f32) {
212 // p: cols(0) bias(1) Y(2) N(3) Cout(4) H(5) W(6) kH(7) kW(8) sh(9) sw(10) ph(11) pw(12) dh(13) dw(14) Ho(15) Wo(16)
213 6 Value* Cout = p[4];
214 6 Value* H = p[5];
215 6 Value* W = p[6];
216 6 Value* kH = p[7];
217 6 Value* kW = p[8];
218 6 Value* sh = p[9];
219 6 Value* sw = p[10];
220 6 Value* ph = p[11];
221 6 Value* pw = p[12];
222 6 Value* dh = p[13];
223 6 Value* dw = p[14];
224 6 Value* Ho = p[15];
225 6 Value* Wo = p[16];
226 6 Value* plane = c.b.build_mul(Ho, Wo);
227 6 Value* kk = c.b.build_mul(kH, kW);
228 6 Value* hw = c.b.build_mul(H, W);
229 6 Value* total = c.b.build_mul(c.b.build_mul(p[3], Cout), plane);
230
231
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 emit_vec_grid_stride(c, total, 1, kVisionBlock, [&](Value* i) {
232 6 Value* noc = c.b.build_udiv(i, plane);
233 6 Value* px = c.b.build_sub(i, c.b.build_mul(noc, plane));
234 6 Value* n = c.b.build_udiv(noc, Cout);
235 6 Value* oc = c.b.build_sub(noc, c.b.build_mul(n, Cout));
236 6 Value* oh = c.b.build_udiv(px, Wo);
237 6 Value* ow = c.b.build_sub(px, c.b.build_mul(oh, Wo));
238 6 Value* rowb = c.b.build_mul(noc, kk);
239
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 Value* init = has_bias ? load_elem_dt(c, dt, p[1], oc) : c.f32(0.0f);
240
241
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 Value* acc = c.kb.for_range_reduce(c.i32(0), kH, c.i32(1), init, [&](Value* kh, Value* r_acc) {
242 6 Value* th = c.b.build_sub(c.b.build_add(oh, ph), c.b.build_mul(kh, dh));
243 6 Value* th_nn = c.b.build_sge(th, c.i32(0));
244
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 return emit_if_else_values(
245 6 c, th_nn, {Type::f32()},
246 12 [&]() -> std::vector<Value*> {
247 6 Value* ih = c.b.build_udiv(th, sh);
248 6 Value* th_div = c.b.build_eq(c.b.build_sub(th, c.b.build_mul(ih, sh)), c.i32(0));
249 6 Value* h_ok = c.b.build_and(th_div, c.b.build_slt(ih, H));
250
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 return emit_if_else_values(
251 6 c, h_ok, {Type::f32()},
252 12 [&]() -> std::vector<Value*> {
253 6 Value* row_k = c.b.build_add(rowb, c.b.build_mul(kh, kW));
254 6 Value* ih_w = c.b.build_mul(ih, W);
255
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
12 return {c.kb.for_range_reduce(c.i32(0), kW, c.i32(1), r_acc, [&](Value* kw, Value* cur) {
256 6 Value* tw = c.b.build_sub(c.b.build_add(ow, pw), c.b.build_mul(kw, dw));
257 6 Value* tw_nn = c.b.build_sge(tw, c.i32(0));
258
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 return emit_if_else_values(
259 6 c, tw_nn, {Type::f32()},
260 12 [&]() -> std::vector<Value*> {
261 6 Value* iw = c.b.build_udiv(tw, sw);
262 6 Value* tw_div = c.b.build_eq(c.b.build_sub(tw, c.b.build_mul(iw, sw)), c.i32(0));
263 6 Value* w_ok = c.b.build_and(tw_div, c.b.build_slt(iw, W));
264
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 return emit_if_else_values(
265 6 c, w_ok, {Type::f32()},
266 12 [&]() -> std::vector<Value*> {
267 12 Value* ci = c.b.build_add(
268 6 c.b.build_mul(c.b.build_add(row_k, kw), hw),
269 6 c.b.build_add(ih_w, iw));
270
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 5 times.
6 Value* v = cols_f32 ? c.kb.load_f32(ptr_at(c, p[0], ci, 4))
271 5 : load_elem_dt(c, dt, p[0], ci);
272 6 return {c.b.build_add(cur, v)};
273 },
274 12 [&]() -> std::vector<Value*> { return {cur}; });
275 ✗ },
276 12 [&]() -> std::vector<Value*> { return {cur}; })[0];
277 ✗ })};
278 ✗ },
279 12 [&]() -> std::vector<Value*> { return {r_acc}; });
280 ✗ },
281 12 [&]() -> std::vector<Value*> { return {r_acc}; })[0];
282 ✗ });
283 6 store_elem_dt(c, dt, p[2], i, acc);
284 6 });
285 6 }
286
287 3 Value* bilinear_at(Ctx& c, Dtype dt, Value* x_ptr, Value* base, Value* H, Value* W, Value* fH, Value* fW,
288 Value* h, Value* w) {
289 6 Value* oob = c.b.build_or(
290 3 c.b.build_or(c.b.build_sle(h, c.f32(-1.0f)), c.b.build_sle(fH, h)),
291 3 c.b.build_or(c.b.build_sle(w, c.f32(-1.0f)), c.b.build_sle(fW, w)));
292
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 return emit_if_else_values(
293 3 c, oob, {Type::f32()},
294 6 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; },
295 6 [&]() -> std::vector<Value*> {
296 3 Value* hf = c.b.build_floor_f32(h);
297 3 Value* wf = c.b.build_floor_f32(w);
298 3 Value* hl = c.b.build_fptosi_i32_f32(hf);
299 3 Value* wl = c.b.build_fptosi_i32_f32(wf);
300 3 Value* hh = c.b.build_add(hl, c.i32(1));
301 3 Value* wh = c.b.build_add(wl, c.i32(1));
302 3 Value* lh = c.b.build_sub(h, hf);
303 3 Value* lw = c.b.build_sub(w, wf);
304 3 Value* ih = c.b.build_sub(c.f32(1.0f), lh);
305 3 Value* iw = c.b.build_sub(c.f32(1.0f), lw);
306
307 15 auto corner = [&](Value* cy, Value* cx, Value* in_y, Value* in_x) {
308
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 return emit_if_else_values(
309 12 c, c.b.build_and(in_y, in_x), {Type::f32()},
310 24 [&]() -> std::vector<Value*> {
311 12 Value* idx = c.b.build_add(base, c.b.build_add(c.b.build_mul(cy, W), cx));
312 12 return {load_elem_dt(c, dt, x_ptr, idx)};
313 },
314 24 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
315 ✗ };
316 3 Value* hl_ok = c.b.build_sge(hl, c.i32(0));
317 3 Value* wl_ok = c.b.build_sge(wl, c.i32(0));
318 3 Value* hh_ok = c.b.build_slt(hh, H);
319 3 Value* wh_ok = c.b.build_slt(wh, W);
320 3 Value* v1 = corner(hl, wl, hl_ok, wl_ok);
321 3 Value* v2 = corner(hl, wh, hl_ok, wh_ok);
322 3 Value* v3 = corner(hh, wl, hh_ok, wl_ok);
323 3 Value* v4 = corner(hh, wh, hh_ok, wh_ok);
324 3 Value* t1 = c.b.build_mul(c.b.build_mul(ih, iw), v1);
325 3 Value* t2 = c.b.build_mul(c.b.build_mul(ih, lw), v2);
326 3 Value* t3 = c.b.build_mul(c.b.build_mul(lh, iw), v3);
327 3 Value* t4 = c.b.build_mul(c.b.build_mul(lh, lw), v4);
328 3 return {c.b.build_add(c.b.build_add(t1, t2), c.b.build_add(t3, t4))};
329 })[0];
330 ✗ }
331
332 6 void build_deform_col(Ctx& c, const std::vector<Value*>& p, Dtype dt, bool has_offset, bool has_mask) {
333 // p: X(0) off(1) mask(2) col(3) Cgi(4) H(5) W(6) Wo(7) HoWo(8) kH(9) kW(10)
334 // sh_sw(11) ph_pw(12) dh_dw(13) Cin_dg(14) ic_first(15) p0(16) np(17)
335 6 Value* H = p[5];
336 6 Value* W = p[6];
337 6 Value* Wo = p[7];
338 6 Value* HoWo = p[8];
339 6 Value* kW = p[10];
340 6 Value* kk = c.b.build_mul(p[9], kW);
341 6 Value* sh = c.b.build_and(p[11], c.i32(0xffff));
342 6 Value* sw = c.b.build_lshr(p[11], c.i32(16));
343 6 Value* ph = c.b.build_and(p[12], c.i32(0xffff));
344 6 Value* pw = c.b.build_lshr(p[12], c.i32(16));
345 6 Value* dh = c.b.build_and(p[13], c.i32(0xffff));
346 6 Value* dw = c.b.build_lshr(p[13], c.i32(16));
347 6 Value* Cin_dg = p[14];
348 6 Value* ic_first = p[15];
349 6 Value* p0 = p[16];
350 6 Value* np = p[17];
351 6 Value* hw = c.b.build_mul(H, W);
352
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 Value* fH = has_offset ? c.kb.i32_to_f32(H) : nullptr;
353
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 Value* fW = has_offset ? c.kb.i32_to_f32(W) : nullptr;
354 6 Value* total = c.b.build_mul(c.b.build_mul(p[4], kk), np);
355
356
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 emit_vec_grid_stride(c, total, 1, kVisionBlock, [&](Value* i) {
357 6 Value* r = c.b.build_udiv(i, np);
358 6 Value* sp = c.b.build_add(p0, c.b.build_sub(i, c.b.build_mul(r, np)));
359 6 Value* icl = c.b.build_udiv(r, kk);
360 6 Value* tap = c.b.build_sub(r, c.b.build_mul(icl, kk));
361 6 Value* kh = c.b.build_udiv(tap, kW);
362 6 Value* kw = c.b.build_sub(tap, c.b.build_mul(kh, kW));
363 6 Value* oh = c.b.build_udiv(sp, Wo);
364 6 Value* ow = c.b.build_sub(sp, c.b.build_mul(oh, Wo));
365 6 Value* ic = c.b.build_add(ic_first, icl);
366 6 Value* yi = c.b.build_add(c.b.build_sub(c.b.build_mul(oh, sh), ph), c.b.build_mul(kh, dh));
367 6 Value* xi = c.b.build_add(c.b.build_sub(c.b.build_mul(ow, sw), pw), c.b.build_mul(kw, dw));
368 6 Value* x_base = c.b.build_mul(ic, hw);
369
370
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (!has_offset) {
371 6 Value* inb = c.b.build_and(c.b.build_and(c.b.build_sge(yi, c.i32(0)), c.b.build_slt(yi, H)),
372 3 c.b.build_and(c.b.build_sge(xi, c.i32(0)), c.b.build_slt(xi, W)));
373
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* val = emit_if_else_values(
374 3 c, inb, {Type::f32()},
375 6 [&]() -> std::vector<Value*> {
376 3 Value* idx = c.b.build_add(x_base, c.b.build_add(c.b.build_mul(yi, W), xi));
377 3 return {load_elem_dt(c, dt, p[0], idx)};
378 },
379 6 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
380 3 store_elem_dt(c, dt, p[3], i, val);
381 3 } else {
382 3 Value* og = c.b.build_udiv(ic, Cin_dg);
383 3 Value* og_tap = c.b.build_add(c.b.build_mul(og, kk), tap);
384 3 Value* oy_idx = c.b.build_add(c.b.build_mul(c.b.build_shl(og_tap, c.i32(1)), HoWo), sp);
385 3 Value* ox_idx = c.b.build_add(oy_idx, HoWo);
386 3 Value* oy = load_elem_dt(c, dt, p[1], oy_idx);
387 3 Value* ox = load_elem_dt(c, dt, p[1], ox_idx);
388
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 Value* m = has_mask ? load_elem_dt(c, dt, p[2], c.b.build_add(c.b.build_mul(og_tap, HoWo), sp))
389 1 : c.f32(1.0f);
390 3 Value* yf = c.b.build_add(c.kb.i32_to_f32(yi), oy);
391 3 Value* xf = c.b.build_add(c.kb.i32_to_f32(xi), ox);
392 3 Value* bv = bilinear_at(c, dt, p[0], x_base, H, W, fH, fW, yf, xf);
393 3 store_elem_dt(c, dt, p[3], i, c.b.build_mul(m, bv));
394 }
395 6 });
396 6 }
397
398 } // namespace
399
400 43 brass::Function* build_vision_mir(brass::Module& mod, std::uint32_t op, Dtype dt, bool aux0, bool aux1) {
401 43 std::vector<Type> params;
402
6/7
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
43 switch (op) {
403 case MI_UPFIRDN:
404
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 params = {Type::ptr(), Type::ptr(), Type::ptr(),
405 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
406 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::f32()};
407 13 break;
408 case MI_MODW:
409
6/12
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 8 times.
✗ Branch 11 not taken.
48 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
410 40 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::f32()};
411 8 break;
412 case MI_MODW_BWD:
413
5/10
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
20 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
414 16 Type::i32(), Type::i32(), Type::i32(), Type::i32()};
415 4 break;
416 case MI_MODW_DS:
417 case MI_MODW_DW:
418
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32(), Type::i32()};
419 6 break;
420 case MI_COL2IM2D:
421
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 params = {Type::ptr(), Type::ptr(), Type::ptr(),
422 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
423 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
424 Type::i32(), Type::i32()};
425 6 break;
426 case MI_DEFORM_COL:
427
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
428 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
429 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32()};
430 6 break;
431 default:
432 ✗ return nullptr;
433 }
434
435
7/14
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 43 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 43 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 43 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 43 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 43 times.
✗ Branch 13 not taken.
129 std::string name = "mir_vision_" + std::to_string(op) + "_" + std::to_string(static_cast<int>(dt)) + "_" +
436
4/8
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 43 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 43 times.
✗ Branch 7 not taken.
86 std::to_string(aux0 ? 1 : 0) + "_" + std::to_string(aux1 ? 1 : 0);
437
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 Function* fn = mod.create_function(name, Type::void_type(), params);
438
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 brass::codegen::KernelBuilder kb(mod, fn);
439
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 Ctx c(kb);
440
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 auto p = entry_params(c, fn);
441
442
6/7
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
43 switch (op) {
443
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 case MI_UPFIRDN: build_upfirdn(c, p, dt, aux0); break;
444
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 case MI_MODW: build_modw(c, p, dt, aux0, aux1); break;
445
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 case MI_MODW_BWD: build_modw_bwd(c, p, dt); break;
446 case MI_MODW_DS:
447
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 case MI_MODW_DW: build_modw_ds_dw(c, p, dt, op == MI_MODW_DW); break;
448
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 case MI_COL2IM2D: build_col2im2d(c, p, dt, aux0, aux1); break;
449
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 case MI_DEFORM_COL: build_deform_col(c, p, dt, aux0, aux1); break;
450 ✗ default: break;
451 }
452
453
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 c.b.build_ret_void();
454
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 return finalize_fn(fn);
455 43 }
456
457 } // namespace brotensor::kernels
458