brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
392 / 411
95.4%
Branches:
193 / 363
53.2%
Functions:
56 / 56
100.0%

src/kernels/mir_conv_aux.cpp
Line Branch Exec Source
1 #include "mir_conv_aux.h"
2
3 #include "mir_common.h"
4 #include "mir_math.h"
5 #include "mir_quant_helpers.h"
6
7 #include <cstdint>
8 #include <stdexcept>
9 #include <string>
10 #include <vector>
11
12 namespace brotensor::kernels {
13
14 namespace {
15
16 constexpr int kAuxBlock = 256;
17
18 // Accumulates `delta` into `base[idx]` for FP32, FP16, or BF16 using 64-bit addressing.
19 15 void accum_elem_dt_i64(Ctx& c, Dtype dt, Value* base, Value* idx64, Value* delta) {
20 15 Value* p = c.b.build_add(base, c.b.build_shl(idx64, c.kb.const_i64(dt == Dtype::FP32 ? 2 : 1)));
21 15 Value* prev = load_lanes(c, dt, 1, p)[0];
22
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 store_lanes(c, dt, 1, p, {c.b.build_add(prev, delta)});
23 15 }
24
25 10 Value* block_sum_256(Ctx& c, Value* s_buf, Value* tid, Value* local_acc) {
26 10 c.kb.shared_store_f32_indexed(s_buf, tid, local_acc);
27 10 c.kb.sync();
28
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 10 times.
90 for (int s = 128; s > 0; s >>= 1) {
29
1/2
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
160 c.kb.if_then(c.b.build_slt(tid, c.i32(s)), [&] {
30 160 Value* v = c.b.build_add(
31 80 c.kb.shared_load_f32_indexed(s_buf, tid),
32 80 c.kb.shared_load_f32_indexed(s_buf, c.b.build_add(tid, c.i32(s))));
33 80 c.kb.shared_store_f32_indexed(s_buf, tid, v);
34 80 });
35 80 c.kb.sync();
36 80 }
37 10 return c.kb.shared_load_f32_indexed(s_buf, c.i32(0));
38 ✗ }
39
40 8 Function* build_bwd_weight(Module& mod, Dtype dt, bool block_per_elem) {
41 8 std::vector<Type> params(3, Type::ptr());
42
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 params.insert(params.end(), 17, Type::i32());
43 // 0:X 1:dY 2:dWt | 3:N 4:Cin 5:H 6:W 7:Cout 8:kH 9:kW 10:Ho 11:Wo
44 // 12:sh 13:sw 14:ph 15:pw 16:dh 17:dw 18:Cgi 19:Cgo (total=block grid or 19:total)
45
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
8 if (!block_per_elem) params.push_back(Type::i32()); // 20:total
46
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
16 Function* fn = mod.create_function(
47
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 block_per_elem ? "mir_conv2d_bwd_w_blk" : "mir_conv2d_bwd_w_thr",
48 8 Type::void_type(), params);
49
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 brass::codegen::KernelBuilder kb(mod, fn);
50
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Ctx c(kb);
51
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 auto p = entry_params(c, fn);
52
53
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* tid = kb.tid_x();
54
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
8 Value* s_acc = block_per_elem ? kb.shared_alloc_f32(kAuxBlock) : nullptr;
55
4/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
8 Value* idx = block_per_elem ? kb.ctaid_x() : kb.global_tid_x();
56
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
16 int go = [&] {
57 // Compute (c_out, c_in_local, kh, kw) -> shared between modes.
58 16 auto body = [&] {
59 8 Value* kw = c.b.build_umod(idx, p[9]);
60 8 Value* t0 = c.b.build_udiv(idx, p[9]);
61 8 Value* kh = c.b.build_umod(t0, p[8]);
62 8 Value* t1 = c.b.build_udiv(t0, p[8]);
63 8 Value* c_in_loc = c.b.build_umod(t1, p[18]);
64 8 Value* c_out = c.b.build_udiv(t1, p[18]);
65 8 Value* c_in = c.b.build_add(c.b.build_mul(c.b.build_udiv(c_out, p[19]), p[18]), c_in_loc);
66 8 Value* spatial = c.b.build_mul(p[10], p[11]);
67 8 Value* hw_in = c.b.build_mul(p[5], p[6]);
68 8 Value* cin_hw = c.b.build_mul(p[4], hw_in);
69 8 Value* cout_sp = c.b.build_mul(p[7], spatial);
70 8 Value* kh_dil = c.b.build_sub(c.b.build_mul(kh, p[16]), p[14]);
71 8 Value* kw_dil = c.b.build_sub(c.b.build_mul(kw, p[17]), p[15]);
72 8 Value* sum = c.f32(0.0f);
73
74
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
8 if (block_per_elem) {
75 5 Value* M = c.b.build_mul(p[3], spatial);
76
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
10 Value* local = kb.for_range_reduce(
77 5 tid, M, c.i32(kAuxBlock), c.f32(0.0f),
78 10 [&](Value* m, Value* cur) -> Value* {
79 5 Value* n = c.b.build_udiv(m, spatial);
80 5 Value* sp = c.b.build_umod(m, spatial);
81 5 Value* io = c.b.build_udiv(sp, p[11]);
82 5 Value* jo = c.b.build_umod(sp, p[11]);
83 5 Value* ih = c.b.build_add(c.b.build_mul(io, p[12]), kh_dil);
84 5 Value* iw = c.b.build_add(c.b.build_mul(jo, p[13]), kw_dil);
85 10 Value* ok = c.b.build_and(
86 5 c.b.build_and(c.b.build_sge(ih, c.i32(0)), c.b.build_slt(ih, p[5])),
87 5 c.b.build_and(c.b.build_sge(iw, c.i32(0)), c.b.build_slt(iw, p[6])));
88
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 return emit_if_else_values(
89 5 c, ok, {Type::f32()},
90 10 [&]() -> std::vector<Value*> {
91 10 Value* xi = c.b.build_add(
92 10 c.b.build_add(c.b.build_mul(c.b.build_zext_i64(n), c.b.build_zext_i64(cin_hw)),
93 5 c.b.build_mul(c.b.build_zext_i64(c_in), c.b.build_zext_i64(hw_in))),
94 5 c.b.build_zext_i64(c.b.build_add(c.b.build_mul(ih, p[6]), iw)));
95 10 Value* dyi = c.b.build_add(
96 10 c.b.build_add(c.b.build_mul(c.b.build_zext_i64(n), c.b.build_zext_i64(cout_sp)),
97 5 c.b.build_mul(c.b.build_zext_i64(c_out), c.b.build_zext_i64(spatial))),
98 5 c.b.build_zext_i64(sp));
99 5 const int esz_sh = (dt == Dtype::FP32) ? 2 : 1;
100 5 Value* xv = load_lanes(c, dt, 1, c.b.build_add(p[0], c.b.build_shl(xi, kb.const_i64(esz_sh))))[0];
101 5 Value* dv = load_lanes(c, dt, 1, c.b.build_add(p[1], c.b.build_shl(dyi, kb.const_i64(esz_sh))))[0];
102 5 return {c.b.build_fma_f32(dv, xv, cur)};
103 },
104 10 [&]() -> std::vector<Value*> { return {cur}; })[0];
105 ✗ });
106 5 sum = block_sum_256(c, s_acc, tid, local);
107
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
108 5 accum_elem_dt_i64(c, dt, p[2], c.b.build_zext_i64(idx), sum);
109 5 });
110 5 } else {
111 3 const int esz_sh = (dt == Dtype::FP32) ? 2 : 1;
112
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 sum = kb.for_range_reduce(
113 3 c.i32(0), p[3], c.i32(1), c.f32(0.0f),
114 6 [&](Value* n, Value* a0) -> Value* {
115 6 Value* xn = c.b.build_add(
116 3 c.b.build_mul(c.b.build_zext_i64(n), c.b.build_zext_i64(cin_hw)),
117 3 c.b.build_mul(c.b.build_zext_i64(c_in), c.b.build_zext_i64(hw_in)));
118 6 Value* dyn = c.b.build_add(
119 3 c.b.build_mul(c.b.build_zext_i64(n), c.b.build_zext_i64(cout_sp)),
120 3 c.b.build_mul(c.b.build_zext_i64(c_out), c.b.build_zext_i64(spatial)));
121
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 return kb.for_range_reduce(
122 3 c.i32(0), p[10], c.i32(1), a0,
123 6 [&](Value* io, Value* a1) -> Value* {
124 3 Value* ih = c.b.build_add(c.b.build_mul(io, p[12]), kh_dil);
125 3 Value* h_ok = c.b.build_and(c.b.build_sge(ih, c.i32(0)), c.b.build_slt(ih, p[5]));
126
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 return emit_if_else_values(
127 3 c, h_ok, {Type::f32()},
128 6 [&]() -> std::vector<Value*> {
129 3 Value* xrow = c.b.build_add(xn, c.b.build_zext_i64(c.b.build_mul(ih, p[6])));
130 3 Value* dyrow = c.b.build_add(dyn, c.b.build_zext_i64(c.b.build_mul(io, p[11])));
131
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
6 return {kb.for_range_reduce(
132 3 c.i32(0), p[11], c.i32(1), a1,
133 6 [&](Value* jo, Value* a2) -> Value* {
134 3 Value* iw = c.b.build_add(c.b.build_mul(jo, p[13]), kw_dil);
135 6 Value* w_ok = c.b.build_and(
136 3 c.b.build_sge(iw, c.i32(0)), c.b.build_slt(iw, p[6]));
137
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 return emit_if_else_values(
138 3 c, w_ok, {Type::f32()},
139 6 [&]() -> std::vector<Value*> {
140 3 Value* xi = c.b.build_add(xrow, c.b.build_zext_i64(iw));
141 3 Value* dyi = c.b.build_add(dyrow, c.b.build_zext_i64(jo));
142 3 Value* xv = load_lanes(c, dt, 1, c.b.build_add(p[0], c.b.build_shl(xi, kb.const_i64(esz_sh))))[0];
143 3 Value* dv = load_lanes(c, dt, 1, c.b.build_add(p[1], c.b.build_shl(dyi, kb.const_i64(esz_sh))))[0];
144 3 return {c.b.build_fma_f32(dv, xv, a2)};
145 },
146 6 [&]() -> std::vector<Value*> { return {a2}; })[0];
147 ✗ })};
148 ✗ },
149 6 [&]() -> std::vector<Value*> { return {a1}; })[0];
150 ✗ });
151 ✗ });
152 3 accum_elem_dt_i64(c, dt, p[2], c.b.build_zext_i64(idx), sum);
153 }
154 8 };
155
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
8 if (block_per_elem) body();
156
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 else kb.if_then(c.b.build_slt(idx, p[20]), body);
157 8 return 0;
158 ✗ }();
159 (void)go;
160
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c.b.build_ret_void();
161
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 return finalize_fn(fn);
162 8 }
163
164 1 Function* build_bwd_bias(Module& mod, Dtype dt) {
165 1 std::vector<Type> params = {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32(), Type::i32()};
166
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Function* fn = mod.create_function("mir_conv2d_bwd_bias", Type::void_type(), params);
167
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::codegen::KernelBuilder kb(mod, fn);
168
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Ctx c(kb);
169
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto p = entry_params(c, fn);
170 // 0:dY 1:dB | 2:N 3:C_out 4:Ho 5:Wo
171
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* s_acc = kb.shared_alloc_f32(kAuxBlock);
172
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* oc = kb.ctaid_x();
173
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* tid = kb.tid_x();
174
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* spatial = c.b.build_mul(p[4], p[5]);
175
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* total = c.b.build_mul(p[2], spatial);
176
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* cout_sp = c.b.build_mul(p[3], spatial);
177 1 const int esz_sh = (dt == Dtype::FP32) ? 2 : 1;
178
179
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* local = kb.for_range_reduce(
180
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 tid, total, c.i32(kAuxBlock), c.f32(0.0f),
181
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* idx, Value* cur) -> Value* {
182 1 Value* n = c.b.build_udiv(idx, spatial);
183 1 Value* sp = c.b.build_umod(idx, spatial);
184 2 Value* off = c.b.build_add(
185 2 c.b.build_add(c.b.build_mul(c.b.build_zext_i64(n), c.b.build_zext_i64(cout_sp)),
186 1 c.b.build_mul(c.b.build_zext_i64(oc), c.b.build_zext_i64(spatial))),
187 1 c.b.build_zext_i64(sp));
188 1 Value* v = load_lanes(c, dt, 1, c.b.build_add(p[0], c.b.build_shl(off, kb.const_i64(esz_sh))))[0];
189 1 return c.b.build_add(cur, v);
190 });
191
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* sum = block_sum_256(c, s_acc, tid, local);
192
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
2 kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
193 1 accum_elem_dt_i64(c, dt, p[1], c.b.build_zext_i64(oc), sum);
194 1 });
195
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_ret_void();
196
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return finalize_fn(fn);
197 1 }
198
199 4 Function* build_conv3d_int8w(Module& mod, bool has_bias) {
200 4 std::vector<Type> params(5, Type::ptr());
201
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 params.insert(params.end(), 24, Type::i32());
202 // 0:X 1:W 2:scales 3:bias 4:Y
203 // 5:N 6:Cin 7:Tin 8:H 9:Win 10:Cout 11:kT 12:kH 13:kW 14:To 15:Ho 16:Wo
204 // 17:st 18:sh 19:sw 20:pt 21:ph 22:pw 23:dt 24:dh 25:dw 26:Cgi 27:Cgo 28:total
205
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Function* fn = mod.create_function("mir_conv3d_int8w_fp16", Type::void_type(), params);
206
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 brass::codegen::KernelBuilder kb(mod, fn);
207
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Ctx c(kb);
208
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 auto p = entry_params(c, fn);
209
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* idx = kb.global_tid_x();
210
3/6
✓ 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.
8 kb.if_then(c.b.build_slt(idx, p[28]), [&] {
211 4 Value* ow = c.b.build_umod(idx, p[16]);
212 4 Value* t0 = c.b.build_udiv(idx, p[16]);
213 4 Value* oh = c.b.build_umod(t0, p[15]);
214 4 Value* t1 = c.b.build_udiv(t0, p[15]);
215 4 Value* ot = c.b.build_umod(t1, p[14]);
216 4 Value* t2 = c.b.build_udiv(t1, p[14]);
217 4 Value* oc = c.b.build_umod(t2, p[10]);
218 4 Value* n = c.b.build_udiv(t2, p[10]);
219
220 4 Value* it0 = c.b.build_sub(c.b.build_mul(ot, p[17]), p[20]);
221 4 Value* ih0 = c.b.build_sub(c.b.build_mul(oh, p[18]), p[21]);
222 4 Value* iw0 = c.b.build_sub(c.b.build_mul(ow, p[19]), p[22]);
223 4 Value* scale = kb.load_f32(ptr_at(c, p[2], oc, 4));
224 4 Value* ic_abs_base = c.b.build_mul(c.b.build_udiv(oc, p[27]), p[26]);
225 4 Value* khw = c.b.build_mul(p[12], p[13]);
226 4 Value* kthw = c.b.build_mul(p[11], khw);
227 4 Value* w_oc_base = c.b.build_mul(oc, c.b.build_mul(p[26], kthw));
228 4 Value* hwi = c.b.build_mul(p[8], p[9]);
229 4 Value* thwi = c.b.build_mul(p[7], hwi);
230 4 Value* x_n_base = c.b.build_mul(n, c.b.build_mul(p[6], thwi));
231
232
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 Value* acc = kb.for_range_reduce(
233 4 c.i32(0), p[26], c.i32(1), c.f32(0.0f),
234 8 [&](Value* ic_loc, Value* a0) -> Value* {
235 4 Value* ic = c.b.build_add(ic_abs_base, ic_loc);
236 4 Value* w_ic = c.b.build_add(w_oc_base, c.b.build_mul(ic_loc, kthw));
237 4 Value* x_ic = c.b.build_add(x_n_base, c.b.build_mul(ic, thwi));
238
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 return kb.for_range_reduce(
239 4 c.i32(0), p[11], c.i32(1), a0,
240 8 [&](Value* kt, Value* a1) -> Value* {
241 4 Value* it = c.b.build_add(it0, c.b.build_mul(kt, p[23]));
242 4 Value* t_ok = c.b.build_and(c.b.build_sge(it, c.i32(0)), c.b.build_slt(it, p[7]));
243
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 return emit_if_else_values(
244 4 c, t_ok, {Type::f32()},
245 8 [&]() -> std::vector<Value*> {
246 4 Value* w_kt = c.b.build_add(w_ic, c.b.build_mul(kt, khw));
247 4 Value* x_t = c.b.build_add(x_ic, c.b.build_mul(it, hwi));
248
3/6
✓ 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.
8 return {kb.for_range_reduce(
249 4 c.i32(0), p[12], c.i32(1), a1,
250 8 [&](Value* kh, Value* a2) -> Value* {
251 4 Value* ih = c.b.build_add(ih0, c.b.build_mul(kh, p[24]));
252 8 Value* h_ok = c.b.build_and(
253 4 c.b.build_sge(ih, c.i32(0)), c.b.build_slt(ih, p[8]));
254
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 return emit_if_else_values(
255 4 c, h_ok, {Type::f32()},
256 8 [&]() -> std::vector<Value*> {
257 4 Value* w_kh = c.b.build_add(w_kt, c.b.build_mul(kh, p[13]));
258 4 Value* x_h = c.b.build_add(x_t, c.b.build_mul(ih, p[9]));
259
3/6
✓ 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.
8 return {kb.for_range_reduce(
260 4 c.i32(0), p[13], c.i32(1), a2,
261 8 [&](Value* kw, Value* a3) -> Value* {
262 4 Value* iw = c.b.build_add(iw0, c.b.build_mul(kw, p[25]));
263 8 Value* w_ok = c.b.build_and(
264 4 c.b.build_sge(iw, c.i32(0)), c.b.build_slt(iw, p[9]));
265
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 return emit_if_else_values(
266 4 c, w_ok, {Type::f32()},
267 8 [&]() -> std::vector<Value*> {
268 4 Value* xv = load_elem_dt(
269 4 c, Dtype::FP16, p[0], c.b.build_add(x_h, iw));
270 8 Value* ub = kb.load_u8(
271 4 ptr_at(c, p[1], c.b.build_add(w_kh, kw), 1));
272 8 Value* wv = c.b.build_mul(
273 4 emit_s8_byte_to_f32(c, ub), scale);
274 4 return {c.b.build_fma_f32(xv, wv, a3)};
275 },
276 8 [&]() -> std::vector<Value*> { return {a3}; })[0];
277 ✗ })};
278 ✗ },
279 8 [&]() -> std::vector<Value*> { return {a2}; })[0];
280 ✗ })};
281 ✗ },
282 8 [&]() -> std::vector<Value*> { return {a1}; })[0];
283 ✗ });
284 ✗ });
285
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 if (has_bias) acc = c.b.build_add(acc, load_elem_dt(c, Dtype::FP16, p[3], oc));
286 4 store_elem_dt(c, Dtype::FP16, p[4], idx, acc);
287 4 });
288
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 c.b.build_ret_void();
289
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 return finalize_fn(fn);
290 4 }
291
292 // idx32: Y and cols each hold fewer than 2^31 elements, so the output index
293 // decomposes in 32-bit (a 64-bit divide is a long software sequence on GPUs).
294 1 Function* build_col2im(Module& mod, bool has_bias, Dtype dt, bool idx32) {
295 1 std::vector<Type> params(3, Type::ptr());
296
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 params.insert(params.end(), 13, Type::i32());
297 // 0:cols (f32) 1:bias (dt) 2:Y (dt) | 3:N 4:Cout 5:H 6:W 7:kH 8:kW 9:sh 10:sw 11:ph 12:pw
298 // 13:dh 14:dw 15:Ho (16:Wo added below)
299
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 params.push_back(Type::i32()); // 16:Wo
300
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 Function* fn = mod.create_function(
301
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 std::string(dt == Dtype::FP32 ? "mir_col2im_f32" : "mir_col2im") + (idx32 ? "_i32" : ""),
302 1 Type::void_type(), params);
303
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::codegen::KernelBuilder kb(mod, fn);
304
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Ctx c(kb);
305
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto p = entry_params(c, fn);
306
307 // Index arithmetic in i32 (idx32) or i64.
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
10 auto w = [&](Value* v) { return idx32 ? v : c.b.build_zext_i64(v); };
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
5 auto narrow = [&](Value* v) { return idx32 ? v : c.b.build_trunc_i32(v); };
310
7/14
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 time.
✗ Branch 13 not taken.
1 Value* total = c.b.build_mul(c.b.build_mul(w(p[3]), w(p[4])), c.b.build_mul(w(p[15]), w(p[16])));
311
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* gid = w(kb.global_tid_x());
312
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* stride = w(grid_stride_ew(c));
313
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* Wo_w = w(p[16]);
314
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* Ho_w = w(p[15]);
315
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* Co_w = w(p[4]);
316
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* Co64 = c.b.build_zext_i64(p[4]);
317
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 Value* khw_hw = c.b.build_mul(
318
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
1 c.b.build_mul(c.b.build_zext_i64(p[7]), c.b.build_zext_i64(p[8])),
319
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
1 c.b.build_mul(c.b.build_zext_i64(p[5]), c.b.build_zext_i64(p[6])));
320
321
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 kb.for_range(gid, total, stride, [&](Value* idx_w) {
322 1 Value* ow = narrow(c.b.build_umod(idx_w, Wo_w));
323 1 Value* t = c.b.build_udiv(idx_w, Wo_w);
324 1 Value* oh = narrow(c.b.build_umod(t, Ho_w));
325 1 Value* t2 = c.b.build_udiv(t, Ho_w);
326 1 Value* oc = narrow(c.b.build_umod(t2, Co_w));
327 1 Value* n = narrow(c.b.build_udiv(t2, Co_w));
328 1 Value* idx = c.b.build_zext_i64(idx_w);
329
330
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* init_acc = has_bias ? load_elem_dt(c, dt, p[1], oc) : c.f32(0.0f);
331 2 Value* cbase_idx = c.b.build_mul(
332 1 c.b.build_add(c.b.build_mul(c.b.build_zext_i64(n), Co64), c.b.build_zext_i64(oc)), khw_hw);
333 1 Value* cbase = c.b.build_add(p[0], c.b.build_shl(cbase_idx, kb.const_i64(2)));
334 1 Value* hw_in = c.b.build_mul(p[5], p[6]);
335
336
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 Value* acc = kb.for_range_reduce(
337 1 c.i32(0), p[7], c.i32(1), init_acc,
338 2 [&](Value* kh, Value* a0) -> Value* {
339 1 Value* th = c.b.build_sub(c.b.build_add(oh, p[11]), c.b.build_mul(kh, p[13]));
340 1 Value* th_nn = c.b.build_sge(th, c.i32(0));
341
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return emit_if_else_values(
342 1 c, th_nn, {Type::f32()},
343 2 [&]() -> std::vector<Value*> {
344 1 Value* ih = c.b.build_udiv(th, p[9]);
345 2 Value* th_ok = c.b.build_and(
346 1 c.b.build_eq(c.b.build_umod(th, p[9]), c.i32(0)),
347 1 c.b.build_slt(ih, p[5]));
348
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return emit_if_else_values(
349 1 c, th_ok, {Type::f32()},
350 2 [&]() -> std::vector<Value*> {
351
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 return {kb.for_range_reduce(
352 1 c.i32(0), p[8], c.i32(1), a0,
353 2 [&](Value* kw, Value* a1) -> Value* {
354 1 Value* tw = c.b.build_sub(c.b.build_add(ow, p[12]), c.b.build_mul(kw, p[14]));
355 1 Value* tw_nn = c.b.build_sge(tw, c.i32(0));
356
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return emit_if_else_values(
357 1 c, tw_nn, {Type::f32()},
358 2 [&]() -> std::vector<Value*> {
359 1 Value* iw = c.b.build_udiv(tw, p[10]);
360 2 Value* tw_ok = c.b.build_and(
361 1 c.b.build_eq(c.b.build_umod(tw, p[10]), c.i32(0)),
362 1 c.b.build_slt(iw, p[6]));
363
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return emit_if_else_values(
364 1 c, tw_ok, {Type::f32()},
365 2 [&]() -> std::vector<Value*> {
366 1 Value* tap = c.b.build_add(c.b.build_mul(kh, p[8]), kw);
367 2 Value* off = c.b.build_add(
368 1 c.b.build_mul(c.b.build_add(c.b.build_mul(tap, p[5]), ih), p[6]), iw);
369 1 return {c.b.build_add(a1, kb.load_f32(ptr_at(c, cbase, off, 4)))};
370 },
371 2 [&]() -> std::vector<Value*> { return {a1}; });
372 ✗ },
373 2 [&]() -> std::vector<Value*> { return {a1}; })[0];
374 ✗ })};
375 ✗ },
376 2 [&]() -> std::vector<Value*> { return {a0}; });
377 ✗ },
378 2 [&]() -> std::vector<Value*> { return {a0}; })[0];
379 ✗ });
380 1 const int shift = dt == Dtype::FP32 ? 2 : 1;
381
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 store_lanes(c, dt, 1, c.b.build_add(p[2], c.b.build_shl(idx, kb.const_i64(shift))), {acc});
382 1 });
383
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_ret_void();
384
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return finalize_fn(fn);
385 1 }
386
387 5 Function* build_resblock_shift(Module& mod, Dtype dt, bool has_N) {
388 5 std::vector<Type> params = {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32()};
389
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Function* fn = mod.create_function("mir_resblock_add_shift", Type::void_type(), params);
390
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 brass::codegen::KernelBuilder kb(mod, fn);
391
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Ctx c(kb);
392
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 auto p = entry_params(c, fn);
393 // 0:Y 1:shift | 2:N 3:C 4:spatial
394
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* total = c.b.build_mul(p[2], c.b.build_mul(p[3], p[4]));
395
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* idx = kb.global_tid_x();
396
3/6
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
10 kb.if_then(c.b.build_slt(idx, total), [&] {
397 5 Value* t = c.b.build_udiv(idx, p[4]);
398 5 Value* ch = c.b.build_umod(t, p[3]);
399 5 Value* n = c.b.build_udiv(t, p[3]);
400
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 Value* sidx = has_N ? c.b.build_add(c.b.build_mul(n, p[3]), ch) : ch;
401 5 Value* yv = load_elem_dt(c, dt, p[0], idx);
402 5 Value* sv = load_elem_dt(c, dt, p[1], sidx);
403 5 store_elem_dt(c, dt, p[0], idx, c.b.build_add(yv, sv));
404 5 });
405
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 c.b.build_ret_void();
406
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 return finalize_fn(fn);
407 5 }
408
409 4 Function* build_resblock_sum_hw(Module& mod, Dtype dt) {
410 4 std::vector<Type> params = {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32()};
411
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Function* fn = mod.create_function("mir_resblock_sum_hw", Type::void_type(), params);
412
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 brass::codegen::KernelBuilder kb(mod, fn);
413
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Ctx c(kb);
414
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 auto p = entry_params(c, fn);
415 // 0:dh2 1:d_shift | 2:N 3:C 4:spatial
416
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* s_buf = kb.shared_alloc_f32(kAuxBlock);
417
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* nc = kb.ctaid_x();
418
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* tid = kb.tid_x();
419
4/8
✓ 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.
8 kb.if_then(c.b.build_slt(nc, c.b.build_mul(p[2], p[3])), [&] {
420 4 Value* rbase = c.b.build_mul(c.b.build_zext_i64(nc), c.b.build_zext_i64(p[4]));
421 4 Value* row = c.b.build_add(p[0], c.b.build_shl(rbase, kb.const_i64(1)));
422
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 Value* local = kb.for_range_reduce(
423 4 tid, p[4], c.i32(kAuxBlock), c.f32(0.0f),
424 8 [&](Value* i, Value* cur) -> Value* {
425 4 return c.b.build_add(cur, load_elem_dt(c, dt, row, i));
426 });
427 4 Value* sum = block_sum_256(c, s_buf, tid, local);
428
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
429 4 accum_elem_dt_i64(c, dt, p[1], c.b.build_zext_i64(nc), sum);
430 4 });
431 4 });
432
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 c.b.build_ret_void();
433
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 return finalize_fn(fn);
434 4 }
435
436 2 Function* build_splitk_fold(Module& mod, Dtype dt) {
437 2 std::vector<Type> params = {Type::ptr(), Type::ptr(), Type::i32(), Type::i32()};
438
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Function* fn = mod.create_function("mir_conv2d_splitk_fold", Type::void_type(), params);
439
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 brass::codegen::KernelBuilder kb(mod, fn);
440
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Ctx c(kb);
441
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto p = entry_params(c, fn);
442 // 0:part(f32) 1:dst(dt) | 2:n 3:S
443
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* idx = kb.global_tid_x();
444
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
4 kb.if_then(c.b.build_slt(idx, p[2]), [&] {
445 2 Value* n64 = c.b.build_zext_i64(p[2]);
446 2 Value* idx64 = c.b.build_zext_i64(idx);
447
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 Value* acc = kb.for_range_reduce(
448 2 c.i32(0), p[3], c.i32(1), c.f32(0.0f),
449 4 [&](Value* s, Value* cur) -> Value* {
450 2 Value* off = c.b.build_add(c.b.build_mul(c.b.build_zext_i64(s), n64), idx64);
451 2 Value* v = kb.load_f32(c.b.build_add(p[0], c.b.build_shl(off, kb.const_i64(2))));
452 2 return c.b.build_add(cur, v);
453 });
454 2 accum_elem_dt_i64(c, dt, p[1], idx64, acc);
455 2 });
456
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 c.b.build_ret_void();
457
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return finalize_fn(fn);
458 2 }
459
460 } // namespace
461
462 25 Function* build_conv_aux_mir(Module& mod, std::uint32_t op, Dtype dt, std::uint8_t flags) {
463
8/9
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
25 switch (op) {
464 5 case 0u: return build_bwd_weight(mod, dt, true);
465 3 case 1u: return build_bwd_weight(mod, dt, false);
466 1 case 2u: return build_bwd_bias(mod, dt);
467 4 case 3u: return build_conv3d_int8w(mod, (flags & 1u) != 0);
468 1 case 4u: return build_col2im(mod, (flags & 1u) != 0, dt, (flags & 2u) != 0);
469 5 case 5u: return build_resblock_shift(mod, dt, (flags & 1u) != 0);
470 4 case 6u: return build_resblock_sum_hw(mod, dt);
471 2 case 7u: return build_splitk_fold(mod, dt);
472
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ default: throw std::runtime_error("build_conv_aux_mir: unknown op");
473 }
474 25 }
475
476 } // namespace brotensor::kernels
477