brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
274 / 292
93.8%
Branches:
161 / 281
57.3%
Functions:
36 / 37
97.3%

src/kernels/mir_misc.cpp
Line Branch Exec Source
1 #include "mir_misc.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 9 Value* count_valid_rows(Ctx& c, Value* mask, Value* K, bool has_mask) {
15
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 if (!has_mask) return K;
16
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 return c.kb.for_range_reduce(c.i32(0), K, c.i32(1), c.i32(0), [&](Value* k, Value* cur) {
17 5 Value* ok = c.b.build_sge(c.kb.load_f32(ptr_at(c, mask, k, 4)), c.f32(0.5f));
18 5 return c.b.build_add(cur, c.b.build_select(ok, c.i32(1), c.i32(0)));
19 });
20 9 }
21
22 5 Value* row_valid(Ctx& c, Value* mask, Value* k, bool has_mask) {
23
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!has_mask) return c.i32(1);
24 5 return c.b.build_sge(c.kb.load_f32(ptr_at(c, mask, k, 4)), c.f32(0.5f));
25 5 }
26
27 14 Value* act_eval(Ctx& c, Value* t, Value* is_lrelu, Value* alpha) {
28 14 Value* neg_or_zero = c.b.build_eq(c.b.build_sgt(t, c.f32(0.0f)), c.i32(0));
29 14 return c.b.build_select(c.b.build_and(is_lrelu, neg_or_zero), c.b.build_mul(alpha, t), t);
30 }
31
32 10 Value* bias_act_grad_eval(Ctx& c, Value* dy, Value* t, Value* is_lrelu, Value* alpha, Value* gain, Value* clamp_v) {
33 10 Value* neg_or_zero = c.b.build_eq(c.b.build_sgt(t, c.f32(0.0f)), c.i32(0));
34 10 Value* gr = c.b.build_select(c.b.build_and(is_lrelu, neg_or_zero), alpha, c.f32(1.0f));
35 10 Value* dt = c.b.build_mul(c.b.build_mul(dy, gain), gr);
36 10 Value* has_clamp = c.b.build_sge(clamp_v, c.f32(0.0f));
37 10 Value* yp = c.b.build_mul(gain, act_eval(c, t, is_lrelu, alpha));
38 10 Value* sat = c.b.build_or(c.b.build_slt(yp, c.b.build_neg(clamp_v)), c.b.build_sgt(yp, clamp_v));
39 10 return c.b.build_select(c.b.build_and(has_clamp, sat), c.f32(0.0f), dt);
40 }
41
42 9 void build_mean_pool(Ctx& c, const std::vector<Value*>& p, Dtype dt, bool has_mask, bool bwd) {
43 9 Value* K = p[3];
44 9 Value* D = p[4];
45 9 Value* nv = count_valid_rows(c, p[1], K, has_mask);
46 18 Value* inv = c.b.build_select(c.b.build_ugt(nv, c.i32(0)),
47 9 c.b.build_sdiv(c.f32(1.0f), c.kb.u32_to_f32(nv)), c.f32(0.0f));
48
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
9 if (!bwd) {
49
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 emit_vec_grid_stride(c, D, 1, kMiscBlock, [&](Value* j) {
50
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 Value* acc = c.kb.for_range_reduce(c.i32(0), K, c.i32(1), c.f32(0.0f), [&](Value* k, Value* cur) {
51 5 Value* idx = c.b.build_add(c.b.build_mul(k, D), j);
52
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 if (!has_mask) return c.b.build_add(cur, load_elem_dt(c, dt, p[0], idx));
53
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 return emit_if_else_values(
54 3 c, row_valid(c, p[1], k, true), {Type::f32()},
55 6 [&]() -> std::vector<Value*> { return {c.b.build_add(cur, load_elem_dt(c, dt, p[0], idx))}; },
56 6 [&]() -> std::vector<Value*> { return {cur}; })[0];
57 5 });
58 5 store_elem_dt(c, dt, p[2], j, c.b.build_mul(acc, inv));
59 5 });
60 5 } else {
61 4 Value* total = c.b.build_mul(K, D);
62 4 Value* has_nv = c.b.build_ugt(nv, c.i32(0));
63
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 emit_vec_grid_stride(c, total, 1, kMiscBlock, [&](Value* i) {
64 4 Value* k = c.b.build_udiv(i, D);
65 4 Value* j = c.b.build_sub(i, c.b.build_mul(k, D));
66
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 Value* ok = has_mask ? c.b.build_and(has_nv, row_valid(c, p[1], k, true)) : has_nv;
67
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* val = emit_if_else_values(
68 4 c, ok, {Type::f32()},
69 8 [&]() -> std::vector<Value*> { return {c.b.build_mul(load_elem_dt(c, dt, p[0], j), inv)}; },
70 8 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
71 4 store_elem_dt(c, dt, p[2], i, val);
72 4 });
73 }
74 9 }
75
76 3 void build_count_above(Ctx& c, const std::vector<Value*>& p, Dtype dt) {
77 3 Value* R = p[2];
78 3 Value* C = p[3];
79 3 Value* t_lo = p[4];
80 3 Value* t_hi = p[5];
81 3 Value* tid = c.kb.tid_x();
82 3 Value* block = c.i32(static_cast<int>(kMiscBlock));
83 3 Value* s_red = c.kb.shared_alloc_f32(8);
84
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 c.kb.for_range(c.kb.ctaid_x(), R, c.kb.nctaid_x(), [&](Value* r) {
85 3 Value* base = c.b.build_mul(r, C);
86
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 auto cnts = c.kb.for_range_reduce_n(
87
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 tid, C, block, {c.i32(0), c.i32(0)}, [&](Value* j, const std::vector<Value*>& cur) {
88 3 Value* v = load_elem_dt(c, dt, p[0], c.b.build_add(base, j));
89 3 Value* nlo = c.b.build_add(cur[0], c.b.build_select(c.b.build_sgt(v, t_lo), c.i32(1), c.i32(0)));
90 3 Value* nhi = c.b.build_add(cur[1], c.b.build_select(c.b.build_sgt(v, t_hi), c.i32(1), c.i32(0)));
91 3 return std::vector<Value*>{nlo, nhi};
92 });
93
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* slo = emit_block_sum_f32(c, s_red, c.kb.u32_to_f32(cnts[0]));
94
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 Value* shi = emit_block_sum_f32(c, s_red, c.kb.u32_to_f32(cnts[1]));
95
4/8
✓ 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.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
6 c.kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
96 3 Value* o = c.b.build_shl(r, c.i32(1));
97 3 c.kb.store_i32(ptr_at(c, p[1], o, 4), c.b.build_fptosi_i32_f32(slo));
98 3 c.kb.store_i32(ptr_at(c, p[1], c.b.build_add(o, c.i32(1)), 4), c.b.build_fptosi_i32_f32(shi));
99 3 });
100 3 });
101 3 }
102
103 2 void build_moments(Ctx& c, const std::vector<Value*>& p, Dtype dt) {
104 2 Value* Lq = p[3];
105 2 Value* Lk = p[4];
106 2 Value* w_lat = p[5];
107 2 Value* tid = c.kb.tid_x();
108 2 Value* block = c.i32(static_cast<int>(kMiscBlock));
109 2 Value* s_red = c.kb.shared_alloc_f32(8);
110
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 c.kb.for_range(c.kb.ctaid_x(), Lk, c.kb.nctaid_x(), [&](Value* k) {
111
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto acc = c.kb.for_range_reduce_n(
112
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 tid, Lq, block, {c.f32(0.0f), c.f32(0.0f), c.f32(0.0f)}, [&](Value* q, const std::vector<Value*>& cur) {
113 2 Value* v = load_elem_dt(c, dt, p[0], c.b.build_add(c.b.build_mul(q, Lk), k));
114 2 Value* row = c.b.build_udiv(q, w_lat);
115 2 Value* col = c.b.build_sub(q, c.b.build_mul(row, w_lat));
116 6 return std::vector<Value*>{
117 2 c.b.build_add(cur[0], v),
118 2 c.b.build_add(cur[1], c.b.build_mul(c.kb.u32_to_f32(row), v)),
119 2 c.b.build_add(cur[2], c.b.build_mul(c.kb.u32_to_f32(col), v)),
120 };
121 });
122
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* am = emit_block_sum_f32(c, s_red, acc[0]);
123
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* ay = emit_block_sum_f32(c, s_red, acc[1]);
124
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* ax = emit_block_sum_f32(c, s_red, acc[2]);
125
4/8
✓ 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.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
4 c.kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
126 2 c.kb.store_f32(ptr_at(c, p[1], k, 4), am);
127 2 Value* ok = c.b.build_sgt(am, c.f32(1e-8f));
128 2 Value* inv = c.b.build_select(ok, c.b.build_sdiv(c.f32(1.0f), am), c.f32(0.0f));
129 2 Value* cy = c.b.build_select(ok, c.b.build_mul(ay, inv), c.f32(0.0f));
130 2 Value* cx = c.b.build_select(ok, c.b.build_mul(ax, inv), c.f32(0.0f));
131 2 Value* o = c.b.build_shl(k, c.i32(1));
132 2 c.kb.store_f32(ptr_at(c, p[2], o, 4), cy);
133 2 c.kb.store_f32(ptr_at(c, p[2], c.b.build_add(o, c.i32(1)), 4), cx);
134 2 });
135 2 });
136 2 }
137
138 14 void build_bias_act(Ctx& c, const std::vector<Value*>& p, std::uint32_t op, Dtype dt, bool has_bias) {
139
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
14 if (op == MI_BIAS_ACT_DB) {
140 // p: dY(0), X(1), bias(2), dB(3), N(4), C(5), HW(6), act(7), alpha(8), gain(9), clamp(10)
141 6 Value* N = p[4];
142 6 Value* C = p[5];
143 6 Value* HW = p[6];
144 6 Value* is_lrelu = c.b.build_eq(p[7], c.i32(1));
145 6 Value* per = c.b.build_mul(N, HW);
146 6 Value* tid = c.kb.tid_x();
147 6 Value* block = c.i32(static_cast<int>(kMiscBlock));
148 6 Value* s_red = c.kb.shared_alloc_f32(8);
149
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 c.kb.for_range(c.kb.ctaid_x(), C, c.kb.nctaid_x(), [&](Value* ch) {
150
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 Value* bias = has_bias ? load_elem_dt(c, dt, p[2], ch) : c.f32(0.0f);
151
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 Value* acc = c.kb.for_range_reduce(tid, per, block, c.f32(0.0f), [&](Value* j, Value* cur) {
152 6 Value* n = c.b.build_udiv(j, HW);
153 6 Value* rem = c.b.build_sub(j, c.b.build_mul(n, HW));
154 6 Value* idx = c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(n, C), ch), HW), rem);
155 6 Value* t = c.b.build_add(load_elem_dt(c, dt, p[1], idx), bias);
156 6 Value* g = bias_act_grad_eval(c, load_elem_dt(c, dt, p[0], idx), t, is_lrelu, p[8], p[9], p[10]);
157 6 return c.b.build_add(cur, g);
158 });
159 6 Value* sum = emit_block_sum_f32(c, s_red, acc);
160
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 c.kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
161 6 Value* old_db = load_elem_dt(c, dt, p[3], ch);
162 6 store_elem_dt(c, dt, p[3], ch, c.b.build_add(old_db, sum));
163 6 });
164 6 });
165 6 return;
166 }
167 8 const bool fwd = (op == MI_BIAS_ACT);
168 // fwd: a X(0), b bias(1), c Y(2), total(3), C(4), HW(5), act(6), alpha(7), gain(8), clamp(9)
169 // bwd: a dY(0), b X(1), c bias(2), d dX(3), total(4), C(5), HW(6), act(7), alpha(8), gain(9), clamp(10)
170 8 const std::size_t off = fwd ? 0 : 1;
171
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 Value* xb = fwd ? p[0] : p[1];
172
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 Value* bb = fwd ? p[1] : p[2];
173 8 Value* out_ptr = p[2 + off];
174 8 Value* total = p[3 + off];
175 8 Value* C = p[4 + off];
176 8 Value* HW = p[5 + off];
177 8 Value* is_lrelu = c.b.build_eq(p[6 + off], c.i32(1));
178 8 Value* alpha = p[7 + off];
179 8 Value* gain = p[8 + off];
180 8 Value* clamp_v = p[9 + off];
181 8 Value* has_clamp = c.b.build_sge(clamp_v, c.f32(0.0f));
182
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
16 emit_vec_grid_stride(c, total, 1, kMiscBlock, [&](Value* i) {
183 8 Value* ch = c.b.build_umod(c.b.build_udiv(i, HW), C);
184
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 Value* bval = has_bias ? load_elem_dt(c, dt, bb, ch) : c.f32(0.0f);
185 8 Value* t = c.b.build_add(load_elem_dt(c, dt, xb, i), bval);
186
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (fwd) {
187 4 Value* y = c.b.build_mul(act_eval(c, t, is_lrelu, alpha), gain);
188 4 Value* yc = c.kb.fmin(c.kb.fmax(y, c.b.build_neg(clamp_v)), clamp_v);
189 4 store_elem_dt(c, dt, out_ptr, i, c.b.build_select(has_clamp, yc, y));
190 4 } else {
191 4 Value* dy = load_elem_dt(c, dt, p[0], i);
192 4 store_elem_dt(c, dt, out_ptr, i, bias_act_grad_eval(c, dy, t, is_lrelu, alpha, gain, clamp_v));
193 }
194 8 });
195 14 }
196
197 } // namespace
198
199 37 brass::Function* build_misc_mir(brass::Module& mod, std::uint32_t op, Dtype dt, bool has_aux) {
200 37 std::vector<Type> params;
201
12/14
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 4 times.
✓ Branch 12 taken 19 times.
✗ Branch 13 not taken.
37 switch (op) {
202 case MI_MEAN_POOL:
203 case MI_MEAN_POOL_BWD:
204
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 10 times.
19 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32()};
205 9 break;
206 case MI_SLOT_MASK:
207
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 params = {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32()};
208 1 break;
209 case MI_CAUSAL_ROW:
210
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 params = {Type::ptr(), Type::i32(), Type::i32()};
211 1 break;
212 case MI_THRESHOLD_U8:
213
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 params = {Type::ptr(), Type::ptr(), Type::i32(), Type::f32()};
214 2 break;
215 case MI_COUNT_ABOVE:
216
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 params = {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::f32(), Type::f32()};
217 3 break;
218 case MI_XAVIER:
219
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ params = {Type::ptr(), Type::i32(), Type::i32(), Type::i32(), Type::f32()};
220 ✗ break;
221 case MI_SGD:
222
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::f32(), Type::f32()};
223 1 break;
224 case MI_ADAM:
225
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.
7 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(),
226 6 Type::f32(), Type::f32(), Type::f32(), Type::f32(), Type::f32(), Type::f32()};
227 1 break;
228 case MI_MSE_SAMPLE:
229
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i32()};
230 1 break;
231 case MI_MSE_BWD:
232
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::f32()};
233 1 break;
234 case MI_MSE_SUM:
235
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32()};
236 1 break;
237 case MI_MOMENTS:
238
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32()};
239 2 break;
240 case MI_BIAS_ACT:
241
8/16
✓ 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.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 4 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
32 params = {Type::ptr(), Type::ptr(), Type::ptr(),
242 16 Type::i32(), Type::i32(), Type::i32(), Type::i32(),
243 12 Type::f32(), Type::f32(), Type::f32()};
244 4 break;
245 case MI_BIAS_ACT_BWD:
246 case MI_BIAS_ACT_DB:
247
16/16
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 10 times.
✓ Branch 6 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 8 taken 10 times.
✓ Branch 9 taken 10 times.
✓ Branch 10 taken 10 times.
✓ Branch 11 taken 10 times.
✓ Branch 12 taken 10 times.
✓ Branch 13 taken 10 times.
✓ Branch 14 taken 10 times.
✓ Branch 15 taken 10 times.
✗ params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
248 ✗ Type::i32(), Type::i32(), Type::i32(), Type::i32(),
249 ✗ Type::f32(), Type::f32(), Type::f32()};
250 10 break;
251 default:
252 ✗ return nullptr;
253 }
254
255
7/14
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 37 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 37 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 37 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 37 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 37 times.
✗ Branch 13 not taken.
74 std::string name = "mir_misc_" + std::to_string(op) + "_" + std::to_string(static_cast<int>(dt)) + "_" +
256
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 std::to_string(has_aux ? 1 : 0);
257
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 Function* fn = mod.create_function(name, Type::void_type(), params);
258
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 brass::codegen::KernelBuilder kb(mod, fn);
259
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 Ctx c(kb);
260
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 auto p = entry_params(c, fn);
261
262
11/13
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 9 taken 1 time.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 23 times.
✗ Branch 12 not taken.
37 switch (op) {
263 case MI_MEAN_POOL:
264 case MI_MEAN_POOL_BWD:
265
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
✗ build_mean_pool(c, p, dt, has_aux, op == MI_MEAN_POOL_BWD);
266 9 break;
267 case MI_SLOT_MASK:
268
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 emit_vec_grid_stride(c, p[2], 1, kMiscBlock, [&](Value* k) {
269 1 Value* idx = c.b.build_add(p[3], c.b.build_mul(k, p[4]));
270 1 Value* ok = c.b.build_sgt(kb.load_f32(ptr_at(c, p[0], idx, 4)), c.f32(0.5f));
271 1 kb.store_f32(ptr_at(c, p[1], k, 4), c.b.build_select(ok, c.f32(1.0f), c.f32(0.0f)));
272 1 });
273 1 break;
274 case MI_CAUSAL_ROW:
275
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 emit_vec_grid_stride(c, p[1], 1, kMiscBlock, [&](Value* k) {
276 1 Value* ok = c.b.build_sle(k, p[2]);
277 1 kb.store_f32(ptr_at(c, p[0], k, 4), c.b.build_select(ok, c.f32(1.0f), c.f32(0.0f)));
278 1 });
279 1 break;
280 case MI_THRESHOLD_U8:
281
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 emit_vec_grid_stride(c, p[2], 1, kMiscBlock, [&](Value* i) {
282 2 Value* ok = c.b.build_sgt(load_elem_dt(c, dt, p[0], i), p[3]);
283 2 kb.store_u8(ptr_at(c, p[1], i, 1), c.b.build_select(ok, c.i32(1), c.i32(0)));
284 2 });
285 2 break;
286 case MI_COUNT_ABOVE:
287
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 build_count_above(c, p, dt);
288 3 break;
289 case MI_XAVIER: {
290
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* base = c.b.build_or(c.b.build_zext_i64(p[2]),
291
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ c.b.build_shl(c.b.build_zext_i64(p[3]), kb.const_i64(32)));
292
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* mul_k = kb.const_i64(static_cast<std::int64_t>(0x9E3779B97F4A7C15ULL));
293
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ emit_vec_grid_stride(c, p[1], 1, kMiscBlock, [&](Value* i) {
294 ✗ Value* z = c.b.build_add(base, c.b.build_mul(c.b.build_zext_i64(c.b.build_add(i, c.i32(1))), mul_k));
295 ✗ Value* u = emit_splitmix64_mix_u01(c, z);
296 ✗ Value* val = c.b.build_mul(c.b.build_sub(c.b.build_mul(u, c.f32(2.0f)), c.f32(1.0f)), p[4]);
297 ✗ kb.store_f32(ptr_at(c, p[0], i, 4), val);
298 ✗ });
299 ✗ break;
300 }
301 case MI_SGD:
302
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 emit_vec_grid_stride(c, p[3], 1, kMiscBlock, [&](Value* i) {
303 1 Value* pp = ptr_at(c, p[0], i, 4);
304 1 Value* vp = ptr_at(c, p[2], i, 4);
305 1 Value* nv = c.b.build_add(c.b.build_mul(p[5], kb.load_f32(vp)), kb.load_f32(ptr_at(c, p[1], i, 4)));
306 1 kb.store_f32(vp, nv);
307 1 kb.store_f32(pp, c.b.build_sub(kb.load_f32(pp), c.b.build_mul(p[4], nv)));
308 1 });
309 1 break;
310 case MI_ADAM:
311
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 emit_vec_grid_stride(c, p[4], 1, kMiscBlock, [&](Value* i) {
312 1 Value* pp = ptr_at(c, p[0], i, 4);
313 1 Value* mp = ptr_at(c, p[2], i, 4);
314 1 Value* vp = ptr_at(c, p[3], i, 4);
315 1 Value* g = kb.load_f32(ptr_at(c, p[1], i, 4));
316 1 Value* b1 = p[8];
317 1 Value* b2 = p[9];
318 1 Value* omb1 = c.b.build_sub(c.f32(1.0f), b1);
319 1 Value* omb2 = c.b.build_sub(c.f32(1.0f), b2);
320 1 Value* m = c.b.build_add(c.b.build_mul(b1, kb.load_f32(mp)), c.b.build_mul(omb1, g));
321 2 Value* v = c.b.build_add(c.b.build_mul(b2, kb.load_f32(vp)),
322 1 c.b.build_mul(c.b.build_mul(omb2, g), g));
323 1 kb.store_f32(mp, m);
324 1 kb.store_f32(vp, v);
325 1 Value* num = c.b.build_mul(p[7], c.b.build_mul(m, p[5]));
326 1 Value* den = c.b.build_add(c.b.build_sqrt_f32(c.b.build_mul(v, p[6])), p[10]);
327 1 kb.store_f32(pp, c.b.build_sub(kb.load_f32(pp), c.b.build_sdiv(num, den)));
328 1 });
329 1 break;
330 case MI_MSE_SAMPLE:
331
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 emit_vec_grid_stride(c, p[4], 1, kMiscBlock, [&](Value* i) {
332 1 Value* dd = c.b.build_sub(kb.load_f32(ptr_at(c, p[0], i, 4)), kb.load_f32(ptr_at(c, p[1], i, 4)));
333 1 kb.store_f32(ptr_at(c, p[2], i, 4), dd);
334 1 kb.store_f32(ptr_at(c, p[3], i, 4), c.b.build_mul(c.f32(0.5f), c.b.build_mul(dd, dd)));
335 1 });
336 1 break;
337 case MI_MSE_BWD:
338
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 emit_vec_grid_stride(c, p[3], 1, kMiscBlock, [&](Value* i) {
339 1 Value* dd = c.b.build_sub(kb.load_f32(ptr_at(c, p[0], i, 4)), kb.load_f32(ptr_at(c, p[1], i, 4)));
340 1 kb.store_f32(ptr_at(c, p[2], i, 4), c.b.build_mul(p[4], dd));
341 1 });
342 1 break;
343 case MI_MSE_SUM: {
344
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* tid = kb.tid_x();
345
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* s_red = kb.shared_alloc_f32(8);
346
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 Value* acc = kb.for_range_reduce(tid, p[3], c.i32(static_cast<int>(kMiscBlock)), c.f32(0.0f),
347
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* i, Value* cur) {
348 2 Value* dd = c.b.build_sub(kb.load_f32(ptr_at(c, p[0], i, 4)),
349 1 kb.load_f32(ptr_at(c, p[1], i, 4)));
350 1 return c.b.build_add(cur, c.b.build_mul(dd, dd));
351 });
352
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* sum = emit_block_sum_f32(c, s_red, acc);
353
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)), [&] { kb.store_f32(p[2], sum); });
354 1 break;
355 }
356 case MI_MOMENTS:
357
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 build_moments(c, p, dt);
358 2 break;
359 case MI_BIAS_ACT:
360 case MI_BIAS_ACT_BWD:
361 case MI_BIAS_ACT_DB:
362
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 9 times.
23 build_bias_act(c, p, op, dt, has_aux);
363 14 break;
364 default:
365 ✗ break;
366 }
367
368
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 c.b.build_ret_void();
369
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 return finalize_fn(fn);
370 75 }
371
372 } // namespace brotensor::kernels
373