brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
426 / 436
97.7%
Branches:
172 / 324
53.1%
Functions:
69 / 69
100.0%

src/kernels/mir_select.cpp
Line Branch Exec Source
1 #include "mir_select.h"
2 #include "mir_common.h"
3 #include "mir_diffusion.h"
4 #include "mir_math.h"
5
6 #include <functional>
7 #include <vector>
8
9 namespace brotensor::kernels {
10
11 namespace {
12
13 constexpr float kFixScale = 72057594037927936.0f; // 2^56
14 constexpr float kExpScale = 1099511627776.0f; // 2^40
15 constexpr float kInvExpScale = 1.0f / 1099511627776.0f;
16
17 676 Value* pack_u64(Ctx& c, Value* lo32, Value* hi32) {
18 676 Value* hi64 = c.b.build_shl(c.b.build_zext_i64(hi32), c.kb.const_i64(32));
19 676 return c.b.build_or(hi64, c.b.build_zext_i64(lo32));
20 }
21
22 14 Value* neg_inf(Ctx& c) {
23 14 return jit::gpu_mir::bits_to_f32(c, c.i32(static_cast<int>(0xff800000u)));
24 }
25
26 4 Value* min_u32(Ctx& c, Value* a, Value* b) {
27 4 return c.b.build_select(c.b.build_ult(a, b), a, b);
28 }
29
30 // Structured while loop carrying arbitrary loop values in block parameters.
31 14 std::vector<Value*> emit_while_n(Ctx& c, const std::vector<Value*>& inits,
32 const std::function<Value*(const std::vector<Value*>&)>& cond_fn,
33 const std::function<std::vector<Value*>(const std::vector<Value*>&)>& body_fn) {
34 14 BasicBlock* from = c.b.current_block();
35 14 BasicBlock* head = c.b.append_block("while_head");
36 14 BasicBlock* body = c.b.append_block("while_body");
37 14 BasicBlock* exit_bb = c.b.append_block("while_exit");
38
39 14 std::vector<Value*> accs;
40
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 accs.reserve(inits.size());
41
4/6
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
42 for (Value* init : inits) accs.push_back(c.b.add_block_param(head, init->type()));
42
43 14 c.b.position_at_end(from);
44
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 c.b.build_br(head, inits);
45
46 14 c.b.position_at_end(head);
47
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 c.b.build_br_if(cond_fn(accs), body, exit_bb);
48
49 14 c.b.position_at_end(body);
50
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 std::vector<Value*> next = body_fn(accs);
51
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 c.b.build_br(head, next);
52
53 14 c.b.position_at_end(exit_bb);
54 14 return accs;
55 14 }
56
57 struct SelectSmem {
58 Value* sf;
59 Value* su;
60 Value* sq_hi;
61 Value* sq_lo;
62 };
63
64 7 SelectSmem alloc_select_smem(Ctx& c, bool need_sq, bool need_su) {
65 7 SelectSmem s{};
66 7 s.sf = c.kb.shared_alloc_f32(kSelectBlock);
67
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 s.su = need_su ? c.kb.shared_alloc_i32(kSelectBlock) : nullptr;
68
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 s.sq_hi = need_sq ? c.kb.shared_alloc_i32(kSelectBlock) : nullptr;
69
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 s.sq_lo = need_sq ? c.kb.shared_alloc_i32(kSelectBlock) : nullptr;
70 7 return s;
71 }
72
73 17 Value* wg_max(Ctx& c, Value* s_f, Value* v) {
74 17 Value* tid = c.kb.tid_x();
75 17 c.kb.shared_store_f32_indexed(s_f, tid, v);
76 17 c.kb.sync();
77
2/2
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 17 times.
153 for (int s = 128; s >= 1; s >>= 1) {
78
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
272 c.kb.if_then(c.b.build_ult(tid, c.i32(s)), [&] {
79 136 Value* a = c.kb.shared_load_f32_indexed(s_f, tid);
80 136 Value* b = c.kb.shared_load_f32_indexed(s_f, c.b.build_add(tid, c.i32(s)));
81 136 c.kb.shared_store_f32_indexed(s_f, tid, c.kb.fmax(a, b));
82 136 });
83 136 c.kb.sync();
84 136 }
85 17 Value* res = c.kb.shared_load_f32_indexed(s_f, c.i32(0));
86 17 c.kb.sync();
87 17 return res;
88 ✗ }
89
90 38 Value* wg_sum64(Ctx& c, Value* s_hi, Value* s_lo, Value* v64) {
91 38 Value* tid = c.kb.tid_x();
92 38 Value* hi = c.b.build_trunc_i32(c.b.build_lshr(v64, c.kb.const_i64(32)));
93 38 Value* lo = c.b.build_trunc_i32(v64);
94 38 c.kb.shared_store_i32_indexed(s_hi, tid, hi);
95 38 c.kb.shared_store_i32_indexed(s_lo, tid, lo);
96 38 c.kb.sync();
97
2/2
✓ Branch 0 taken 304 times.
✓ Branch 1 taken 38 times.
342 for (int s = 128; s >= 1; s >>= 1) {
98
1/2
✓ Branch 0 taken 304 times.
✗ Branch 1 not taken.
608 c.kb.if_then(c.b.build_ult(tid, c.i32(s)), [&] {
99 304 Value* p = c.b.build_add(tid, c.i32(s));
100 304 Value* a = pack_u64(c, c.kb.shared_load_i32_indexed(s_lo, tid), c.kb.shared_load_i32_indexed(s_hi, tid));
101 304 Value* b = pack_u64(c, c.kb.shared_load_i32_indexed(s_lo, p), c.kb.shared_load_i32_indexed(s_hi, p));
102 304 Value* sum = c.b.build_add(a, b);
103 304 c.kb.shared_store_i32_indexed(s_hi, tid, c.b.build_trunc_i32(c.b.build_lshr(sum, c.kb.const_i64(32))));
104 304 c.kb.shared_store_i32_indexed(s_lo, tid, c.b.build_trunc_i32(sum));
105 304 });
106 304 c.kb.sync();
107 304 }
108 38 Value* res = pack_u64(c, c.kb.shared_load_i32_indexed(s_lo, c.i32(0)), c.kb.shared_load_i32_indexed(s_hi, c.i32(0)));
109 38 c.kb.sync();
110 38 return res;
111 ✗ }
112
113 7 Value* wg_argmax(Ctx& c, Value* s_f, Value* s_u, Value* val, Value* idx) {
114 7 Value* tid = c.kb.tid_x();
115 7 c.kb.shared_store_f32_indexed(s_f, tid, val);
116 7 c.kb.shared_store_i32_indexed(s_u, tid, idx);
117 7 c.kb.sync();
118
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 7 times.
63 for (int s = 128; s >= 1; s >>= 1) {
119
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
112 c.kb.if_then(c.b.build_ult(tid, c.i32(s)), [&] {
120 56 Value* p = c.b.build_add(tid, c.i32(s));
121 56 Value* va = c.kb.shared_load_f32_indexed(s_f, tid);
122 56 Value* ia = c.kb.shared_load_i32_indexed(s_u, tid);
123 56 Value* vb = c.kb.shared_load_f32_indexed(s_f, p);
124 56 Value* ib = c.kb.shared_load_i32_indexed(s_u, p);
125 112 Value* better = c.b.build_or(c.b.build_sgt(vb, va),
126 56 c.b.build_and(c.b.build_eq(vb, va), c.b.build_ult(ib, ia)));
127 56 c.kb.shared_store_f32_indexed(s_f, tid, c.b.build_select(better, vb, va));
128 56 c.kb.shared_store_i32_indexed(s_u, tid, c.b.build_select(better, ib, ia));
129 56 });
130 56 c.kb.sync();
131 56 }
132 7 Value* res = c.kb.shared_load_i32_indexed(s_u, c.i32(0));
133 7 c.kb.sync();
134 7 return res;
135 ✗ }
136
137 18 Value* fixp_prob(Ctx& c, Value* p) {
138 18 return c.b.build_fptosi_i64_f32(c.b.build_mul(p, c.f32(kFixScale)));
139 }
140
141 21 Value* sort_key(Ctx& c, Value* p, Value* v) {
142 21 Value* pbits = jit::gpu_mir::f32_to_bits(c, p);
143 21 Value* hi = c.b.build_sub(c.i32(0x7fffffff), pbits);
144 21 return pack_u64(c, v, hi);
145 }
146
147 21 Value* emit_below(Ctx& c, const SelectSmem& sm, Value* prow, Value* V, Value* key64, bool mass_mode) {
148 21 Value* tid = c.kb.tid_x();
149
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
42 Value* acc = c.kb.for_range_reduce(tid, V, c.i32(kSelectBlock), c.kb.const_i64(0), [&](Value* v, Value* cur) {
150 21 Value* p = c.kb.load_f32(ptr_at(c, prow, v, 4));
151 21 Value* keep = c.b.build_ule(sort_key(c, p, v), key64);
152
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 6 times.
21 Value* add_v = mass_mode ? fixp_prob(c, p) : c.kb.const_i64(1);
153 21 return c.b.build_select(keep, c.b.build_add(cur, add_v), cur);
154 });
155 21 return wg_sum64(c, sm.sq_hi, sm.sq_lo, acc);
156 ✗ }
157
158 12 Value* emit_search(Ctx& c, const SelectSmem& sm, Value* prow, Value* V, Value* hi, bool mass_mode, Value* target,
159 bool strict) {
160
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 auto res = emit_while_n(
161 12 c, {c.kb.const_i64(0), hi},
162
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
24 [&](const std::vector<Value*>& st) { return c.b.build_ult(st[0], st[1]); },
163
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
24 [&](const std::vector<Value*>& st) -> std::vector<Value*> {
164 12 Value* lo = st[0];
165 12 Value* top = st[1];
166 12 Value* mid = c.b.build_add(lo, c.b.build_lshr(c.b.build_sub(top, lo), c.kb.const_i64(1)));
167 12 Value* b = emit_below(c, sm, prow, V, mid, mass_mode);
168
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
12 Value* ok = strict ? c.b.build_ugt(b, target) : c.b.build_uge(b, target);
169 12 Value* n_lo = c.b.build_select(ok, lo, c.b.build_add(mid, c.kb.const_i64(1)));
170 12 Value* n_top = c.b.build_select(ok, mid, top);
171 12 return {n_lo, n_top};
172 });
173 12 return res[0];
174 12 }
175
176 10 Value* emit_row_lse(Ctx& c, const SelectSmem& sm, Value* row_ptr, Value* V) {
177 10 Value* tid = c.kb.tid_x();
178 10 Value* block = c.i32(static_cast<int>(kSelectBlock));
179
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
20 Value* m0 = c.kb.for_range_reduce(tid, V, block, neg_inf(c), [&](Value* v, Value* cur) {
180 10 return c.kb.fmax(cur, c.kb.load_f32(ptr_at(c, row_ptr, v, 4)));
181 });
182 10 Value* m = wg_max(c, sm.sf, m0);
183
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
20 Value* s0 = c.kb.for_range_reduce(tid, V, block, c.kb.const_i64(0), [&](Value* v, Value* cur) {
184 10 Value* ex = emit_exp_acc(c, c.b.build_sub(c.kb.load_f32(ptr_at(c, row_ptr, v, 4)), m));
185 10 return c.b.build_add(cur, c.b.build_fptosi_i64_f32(c.b.build_mul(ex, c.f32(kExpScale))));
186 });
187 10 Value* s = wg_sum64(c, sm.sq_hi, sm.sq_lo, s0);
188 10 return c.b.build_add(m, emit_log_acc(c, c.b.build_mul(c.b.build_sitofp_f32_i64(s), c.f32(kInvExpScale))));
189 ✗ }
190
191 8 Value* okey_f32(Ctx& c, Value* x) {
192 8 Value* bits = jit::gpu_mir::f32_to_bits(c, x);
193 8 Value* neg = c.b.build_ne(c.b.build_lshr(bits, c.i32(31)), c.i32(0));
194 8 return c.b.build_select(neg, c.b.build_not(bits), c.b.build_or(bits, c.i32(static_cast<int>(0x80000000u))));
195 }
196
197 5 Value* hash_uniform_u01(Ctx& c, Value* seed64, std::uint64_t domain, Value* index64) {
198 5 Value* dom = c.kb.const_i64(static_cast<std::int64_t>(domain));
199 5 Value* x = c.b.build_xor(seed64, c.b.build_add(dom, index64));
200 5 Value* z = c.b.build_add(x, c.kb.const_i64(static_cast<std::int64_t>(0x9E3779B97F4A7C15ULL)));
201 5 return emit_splitmix64_mix_u01(c, z);
202 }
203
204 5 Value* emit_gumbel(Ctx& c, Value* u) {
205 5 Value* eps = c.f32(1e-10f);
206 5 Value* a = c.b.build_add(u, eps);
207 5 Value* b = c.b.build_add(c.b.build_neg(emit_log_acc(c, a)), eps);
208 5 return c.b.build_neg(emit_log_acc(c, b));
209 }
210
211 } // namespace
212
213 3 brass::Function* build_select_sample_mir(brass::Module& mod, bool has_counter) {
214 6 Function* fn = mod.create_function(
215 3 has_counter ? "mir_select_sample_ctr" : "mir_select_sample", Type::void_type(),
216 30 {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
217 21 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
218 6 Type::f32(), Type::f32()});
219 3 brass::codegen::KernelBuilder kb(mod, fn);
220
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Ctx c(kb);
221
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 auto p = entry_params(c, fn);
222 // p: a(0), b(1), d(2), e(3), key_lo(4), key_hi(5), V(6), u1(7), u2(8), top_k(9), rows(10), T(11), top_p(12)
223
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 SelectSmem sm = alloc_select_smem(c, true, true);
224
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* tid = kb.tid_x();
225
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* block = c.i32(static_cast<int>(kSelectBlock));
226
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* key64 = pack_u64(c, p[4], p[5]);
227 3 Value* V = p[6];
228 3 Value* top_k = p[9];
229 3 Value* T = p[11];
230 3 Value* top_p = p[12];
231
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* kmax = c.kb.const_i64(0x7fffffffffffffffLL);
232
233
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 kb.for_range(kb.ctaid_x(), p[10], kb.nctaid_x(), [&](Value* row) {
234 3 Value* row_off = c.b.build_mul(c.b.build_zext_i64(row), c.b.build_zext_i64(V));
235 3 Value* lrow = c.b.build_add(p[0], c.b.build_shl(row_off, c.kb.const_i64(2)));
236 3 Value* is_greedy = c.b.build_eq(T, c.f32(0.0f));
237
238
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 emit_if_else_values(
239 3 c, is_greedy, {},
240 6 [&]() -> std::vector<Value*> {
241 3 Value* inv_bi = c.i32(-1);
242
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 auto best = kb.for_range_reduce_n(
243
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 tid, V, block, {c.f32(-3.4028235e38f), inv_bi}, [&](Value* v, const std::vector<Value*>& cur) {
244 3 Value* x = kb.load_f32(ptr_at(c, lrow, v, 4));
245 3 Value* gt = c.b.build_sgt(x, cur[0]);
246 3 return std::vector<Value*>{c.b.build_select(gt, x, cur[0]), c.b.build_select(gt, v, cur[1])};
247 });
248
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.
3 Value* bi_safe = c.b.build_select(c.b.build_eq(best[1], inv_bi), c.i32(-2), best[1]);
249
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* r = wg_argmax(c, sm.sf, sm.su, best[0], bi_safe);
250
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 kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
251 3 Value* out = c.b.build_select(c.b.build_uge(r, V), c.i32(0), r);
252 3 kb.store_i32(ptr_at(c, p[2], row, 4), out);
253 3 });
254 3 return {};
255 3 },
256 6 [&]() -> std::vector<Value*> {
257 3 Value* prow = c.b.build_add(p[1], c.b.build_shl(row_off, c.kb.const_i64(2)));
258
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 Value* m0 = kb.for_range_reduce(tid, V, block, c.f32(-3.4028235e38f), [&](Value* v, Value* cur) {
259 3 return kb.fmax(cur, c.b.build_sdiv(kb.load_f32(ptr_at(c, lrow, v, 4)), T));
260 });
261 3 Value* m = wg_max(c, sm.sf, m0);
262
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 Value* es0 = kb.for_range_reduce(tid, V, block, c.kb.const_i64(0), [&](Value* v, Value* cur) {
263 3 Value* x = c.b.build_sdiv(kb.load_f32(ptr_at(c, lrow, v, 4)), T);
264 3 Value* ex = emit_exp_acc(c, c.b.build_sub(x, m));
265 3 kb.store_f32(ptr_at(c, prow, v, 4), ex);
266 3 return c.b.build_add(cur, c.b.build_fptosi_i64_f32(c.b.build_mul(ex, c.f32(kExpScale))));
267 });
268 3 Value* es = wg_sum64(c, sm.sq_hi, sm.sq_lo, es0);
269 3 Value* sum = c.b.build_mul(c.b.build_sitofp_f32_i64(es), c.f32(kInvExpScale));
270 6 Value* inv = c.b.build_select(c.b.build_sgt(sum, c.f32(0.0f)),
271 3 c.b.build_sdiv(c.f32(1.0f), sum), c.f32(0.0f));
272
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 kb.for_range(tid, V, block, [&](Value* v) {
273 3 Value* vp = ptr_at(c, prow, v, 4);
274 3 kb.store_f32(vp, c.b.build_mul(kb.load_f32(vp), inv));
275 3 });
276 3 kb.sync();
277
278 3 Value* has_k = c.b.build_and(c.b.build_ugt(top_k, c.i32(0)), c.b.build_ult(top_k, V));
279 3 Value* bound = c.b.build_select(has_k, top_k, V);
280
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* kk0 = emit_if_else_values(
281 3 c, c.b.build_ult(bound, V), {Type::i64()},
282 6 [&]() -> std::vector<Value*> {
283 3 return {emit_search(c, sm, prow, V, kmax, false, c.b.build_zext_i64(bound), false)};
284 },
285 6 [&]() -> std::vector<Value*> { return {kmax}; })[0];
286
287
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* kk = emit_if_else_values(
288 3 c, c.b.build_slt(top_p, c.f32(1.0f)), {Type::i64()},
289 6 [&]() -> std::vector<Value*> {
290 3 Value* tp = fixp_prob(c, top_p);
291 3 Value* b0 = emit_below(c, sm, prow, V, kk0, true);
292
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 return emit_if_else_values(
293 3 c, c.b.build_uge(b0, tp), {Type::i64()},
294 6 [&]() -> std::vector<Value*> { return {emit_search(c, sm, prow, V, kk0, true, tp, false)}; },
295 6 [&]() -> std::vector<Value*> { return {kk0}; });
296 ✗ },
297 6 [&]() -> std::vector<Value*> { return {kk0}; })[0];
298
299 3 Value* kept = emit_below(c, sm, prow, V, kk, true);
300
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 Value* base = has_counter ? c.b.build_zext_i64(kb.load_i32(p[3])) : pack_u64(c, p[7], p[8]);
301 3 Value* k0 = c.b.build_trunc_i32(key64);
302 3 Value* k1 = c.b.build_trunc_i32(c.b.build_lshr(key64, c.kb.const_i64(32)));
303 6 Value* u24 = c.b.build_lshr(philox_words(c, k0, k1, c.b.build_add(base, c.b.build_zext_i64(row))).first,
304 3 c.i32(8));
305 3 Value* u24_64 = c.b.build_zext_i64(u24);
306
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* chosen = emit_if_else_values(
307 3 c, c.b.build_ugt(kept, c.kb.const_i64(0)), {Type::i64()},
308 6 [&]() -> std::vector<Value*> {
309 3 Value* hi_part = c.b.build_mul(c.b.build_lshr(kept, c.kb.const_i64(24)), u24_64);
310 6 Value* lo_part = c.b.build_lshr(
311 3 c.b.build_mul(c.b.build_and(kept, c.kb.const_i64(0xffffff)), u24_64), c.kb.const_i64(24));
312 3 Value* target = c.b.build_add(hi_part, lo_part);
313 3 Value* b_kk = emit_below(c, sm, prow, V, kk, true);
314
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 return emit_if_else_values(
315 3 c, c.b.build_ugt(b_kk, target), {Type::i64()},
316 6 [&]() -> std::vector<Value*> {
317 3 return {emit_search(c, sm, prow, V, kk, true, target, true)};
318 },
319 6 [&]() -> std::vector<Value*> { return {kk}; });
320 ✗ },
321 6 [&]() -> std::vector<Value*> {
322 3 return {emit_search(c, sm, prow, V, kmax, false, c.kb.const_i64(1), false)};
323 })[0];
324
325
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
326 3 kb.store_i32(ptr_at(c, p[2], row, 4), c.b.build_trunc_i32(chosen));
327 3 });
328 3 return {};
329 ✗ });
330 3 });
331
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 c.b.build_ret_void();
332
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 return finalize_fn(fn);
333 3 }
334
335 1 brass::Function* build_select_counter_mir(brass::Module& mod) {
336 1 Function* fn = mod.create_function("mir_select_counter", Type::void_type(), {Type::ptr(), Type::i32()});
337 1 brass::codegen::KernelBuilder kb(mod, fn);
338
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Ctx c(kb);
339
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto p = entry_params(c, fn);
340
5/10
✓ 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.
2 kb.if_then(c.b.build_eq(kb.global_tid_x(), c.i32(0)), [&] {
341 1 kb.store_i32(p[0], c.b.build_add(kb.load_i32(p[0]), p[1]));
342 1 });
343
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_ret_void();
344
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return finalize_fn(fn);
345 1 }
346
347 2 brass::Function* build_select_commit_mir(brass::Module& mod) {
348 4 Function* fn = mod.create_function(
349 2 "mir_select_commit", Type::void_type(),
350 2 {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32()});
351 2 brass::codegen::KernelBuilder kb(mod, fn);
352
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Ctx c(kb);
353
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto p = entry_params(c, fn);
354
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[4], 1, kSelectBlock, [&](Value* i) {
355 2 Value* idx = kb.load_i32(ptr_at(c, p[1], i, 4));
356 2 Value* inb = c.b.build_and(c.b.build_sge(idx, c.i32(0)), c.b.build_slt(idx, p[5]));
357
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 kb.if_then(inb, [&] {
358 2 kb.store_i32(ptr_at(c, p[2], idx, 4), kb.load_i32(ptr_at(c, p[0], idx, 4)));
359 2 kb.store_i32(ptr_at(c, p[3], idx, 4), p[6]);
360 2 });
361 2 });
362
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 c.b.build_ret_void();
363
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return finalize_fn(fn);
364 2 }
365
366 4 brass::Function* build_select_md_mir(brass::Module& mod, bool guided, bool has_class_temp, bool has_pos_temp) {
367
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.
12 std::string name = "mir_select_md_" + std::to_string(guided ? 1 : 0) + "_" +
368
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 std::to_string(has_class_temp ? 1 : 0) + "_" + std::to_string(has_pos_temp ? 1 : 0);
369
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 Function* fn = mod.create_function(
370 4 name, Type::void_type(),
371 4 {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
372 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
373 Type::f32(), Type::f32(), Type::f32(), Type::f32()});
374
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 brass::codegen::KernelBuilder kb(mod, fn);
375
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Ctx c(kb);
376
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 auto p = entry_params(c, fn);
377 // p: a(0) b(1) c(2) d(3) e(4) f(5) key_lo(6) key_hi(7) T(8) C(9) V(10) mask_id(11) k_keep(12)
378 // guidance(13) layer_penalty(14) pos_t(15) class_t(16)
379
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 SelectSmem sm = alloc_select_smem(c, true, true);
380
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* tid = kb.tid_x();
381
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* block = c.i32(static_cast<int>(kSelectBlock));
382
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* key64 = pack_u64(c, p[6], p[7]);
383 4 Value* T = p[8];
384 4 Value* C = p[9];
385 4 Value* V = p[10];
386 4 Value* mask_id = p[11];
387 4 Value* k_keep = p[12];
388
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* ncells = c.b.build_mul(C, T);
389
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.
4 Value* stride = c.b.build_mul(c.b.build_zext_i64(C), c.b.build_zext_i64(V));
390
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* ninf = neg_inf(c);
391
392
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.for_range(kb.ctaid_x(), ncells, kb.nctaid_x(), [&](Value* cell) {
393 4 Value* ch = c.b.build_udiv(cell, T);
394 4 Value* fr = c.b.build_sub(cell, c.b.build_mul(ch, T));
395 4 Value* ch_v = c.b.build_mul(c.b.build_zext_i64(ch), c.b.build_zext_i64(V));
396 4 Value* cond_off = c.b.build_add(c.b.build_mul(c.b.build_zext_i64(fr), stride), ch_v);
397 4 Value* cond = c.b.build_add(p[0], c.b.build_shl(cond_off, c.kb.const_i64(2)));
398 4 Value* lp_off = c.b.build_mul(c.b.build_zext_i64(cell), c.b.build_zext_i64(V));
399 4 Value* lp = c.b.build_add(p[2], c.b.build_shl(lp_off, c.kb.const_i64(2)));
400
401
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
4 if (guided) {
402 3 Value* unc_off = c.b.build_add(c.b.build_mul(c.b.build_zext_i64(c.b.build_add(T, fr)), stride), ch_v);
403 3 Value* unc = c.b.build_add(p[0], c.b.build_shl(unc_off, c.kb.const_i64(2)));
404 3 Value* lc = emit_row_lse(c, sm, cond, V);
405 3 Value* lu = emit_row_lse(c, sm, unc, V);
406
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 kb.for_range(tid, V, block, [&](Value* v) {
407 3 Value* av = c.b.build_sub(kb.load_f32(ptr_at(c, cond, v, 4)), lc);
408 3 Value* bv = c.b.build_sub(kb.load_f32(ptr_at(c, unc, v, 4)), lu);
409 3 Value* g = c.b.build_add(av, c.b.build_mul(p[13], c.b.build_sub(av, bv)));
410 3 kb.store_f32(ptr_at(c, lp, v, 4), g);
411 3 });
412 3 kb.sync();
413 3 Value* lg = emit_row_lse(c, sm, lp, V);
414
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 kb.for_range(tid, V, block, [&](Value* v) {
415 3 Value* ptr = ptr_at(c, lp, v, 4);
416 3 kb.store_f32(ptr, c.b.build_sub(kb.load_f32(ptr), lg));
417 3 });
418 3 } else {
419 1 Value* l = emit_row_lse(c, sm, cond, V);
420
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 kb.for_range(tid, V, block, [&](Value* v) {
421 1 kb.store_f32(ptr_at(c, lp, v, 4), c.b.build_sub(kb.load_f32(ptr_at(c, cond, v, 4)), l));
422 1 });
423 }
424 4 kb.sync();
425
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] { kb.store_f32(ptr_at(c, lp, mask_id, 4), ninf); });
426 4 kb.sync();
427
428
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 Value* cmax = kb.for_range_reduce(tid, V, block, ninf, [&](Value* v, Value* cur) {
429 4 return kb.fmax(cur, kb.load_f32(ptr_at(c, lp, v, 4)));
430 });
431 4 Value* conf = wg_max(c, sm.sf, cmax);
432
433 4 Value* best = nullptr;
434
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (has_class_temp) {
435
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto rng = emit_while_n(
436 2 c, {c.kb.const_i64(0), c.kb.const_i64(0xffffffffLL)},
437
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 [&](const std::vector<Value*>& st) { return c.b.build_ult(st[0], st[1]); },
438
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 [&](const std::vector<Value*>& st) -> std::vector<Value*> {
439 2 Value* lo = st[0];
440 2 Value* hi = st[1];
441 4 Value* mid = c.b.build_add(
442 2 lo, c.b.build_lshr(c.b.build_add(c.b.build_sub(hi, lo), c.kb.const_i64(1)), c.kb.const_i64(1)));
443
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 Value* n0 = kb.for_range_reduce(tid, V, block, c.kb.const_i64(0), [&](Value* v, Value* cur) {
444 2 Value* kx = c.b.build_zext_i64(okey_f32(c, kb.load_f32(ptr_at(c, lp, v, 4))));
445 4 return c.b.build_add(cur, c.b.build_select(c.b.build_uge(kx, mid),
446 2 c.kb.const_i64(1), c.kb.const_i64(0)));
447 });
448 2 Value* n = wg_sum64(c, sm.sq_hi, sm.sq_lo, n0);
449 2 Value* ok = c.b.build_uge(n, c.b.build_zext_i64(k_keep));
450 2 return {c.b.build_select(ok, mid, lo), c.b.build_select(ok, hi, c.b.build_sub(mid, c.kb.const_i64(1)))};
451 ✗ });
452
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* thr = c.b.build_trunc_i32(rng[0]);
453
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 Value* ng0 = kb.for_range_reduce(tid, V, block, c.kb.const_i64(0), [&](Value* v, Value* cur) {
454 2 Value* kx = okey_f32(c, kb.load_f32(ptr_at(c, lp, v, 4)));
455 4 return c.b.build_add(cur, c.b.build_select(c.b.build_ugt(kx, thr),
456 2 c.kb.const_i64(1), c.kb.const_i64(0)));
457 });
458
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* ng = wg_sum64(c, sm.sq_hi, sm.sq_lo, ng0);
459
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* m_rem = c.b.build_sub(k_keep, c.b.build_trunc_i32(ng));
460
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.
2 Value* chunk = c.b.build_udiv(c.b.build_add(V, c.i32(255)), block);
461
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* v0 = min_u32(c, c.b.build_mul(tid, chunk), V);
462
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* v1 = min_u32(c, c.b.build_add(v0, chunk), V);
463
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 Value* eq = kb.for_range_reduce(v0, v1, c.i32(1), c.i32(0), [&](Value* v, Value* cur) {
464 2 Value* kx = okey_f32(c, kb.load_f32(ptr_at(c, lp, v, 4)));
465 2 return c.b.build_add(cur, c.b.build_select(c.b.build_eq(kx, thr), c.i32(1), c.i32(0)));
466 });
467
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 kb.shared_store_i32_indexed(sm.su, tid, eq);
468
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 kb.sync();
469
5/10
✓ 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.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
4 Value* before0 = kb.for_range_reduce(c.i32(0), tid, c.i32(1), c.i32(0), [&](Value* j, Value* cur) {
470 2 return c.b.build_add(cur, kb.shared_load_i32_indexed(sm.su, j));
471 });
472
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 kb.sync();
473
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.
2 Value* base_idx = c.b.build_mul(c.b.build_zext_i64(cell), c.b.build_zext_i64(V));
474
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* inv_bi = c.i32(-1);
475
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 auto arg = kb.for_range_reduce_n(
476
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 v0, v1, c.i32(1), {ninf, inv_bi, before0}, [&](Value* v, const std::vector<Value*>& st) {
477 2 Value* bs = st[0];
478 2 Value* bi = st[1];
479 2 Value* bef = st[2];
480 2 Value* x = kb.load_f32(ptr_at(c, lp, v, 4));
481 2 Value* kx = okey_f32(c, x);
482 2 Value* gt_thr = c.b.build_ugt(kx, thr);
483 2 Value* eq_thr = c.b.build_eq(kx, thr);
484 2 Value* kept_eq = c.b.build_and(eq_thr, c.b.build_ult(bef, m_rem));
485 2 Value* kept = c.b.build_or(gt_thr, kept_eq);
486 2 Value* next_bef = c.b.build_select(eq_thr, c.b.build_add(bef, c.i32(1)), bef);
487
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* score = emit_if_else_values(
488 2 c, kept, {Type::f32()},
489 4 [&]() -> std::vector<Value*> {
490 4 Value* u = hash_uniform_u01(c, key64, 0x14057B7EF767814FULL,
491 2 c.b.build_add(base_idx, c.b.build_zext_i64(v)));
492 2 Value* q = c.b.build_sdiv(x, p[16]);
493 2 return {c.b.build_add(q, emit_gumbel(c, u))};
494 },
495 4 [&]() -> std::vector<Value*> { return {ninf}; })[0];
496 2 Value* win = c.b.build_or(c.b.build_eq(bi, inv_bi), c.b.build_sgt(score, bs));
497 2 return std::vector<Value*>{c.b.build_select(win, score, bs), c.b.build_select(win, v, bi), next_bef};
498 ✗ });
499
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.
2 Value* bi_safe = c.b.build_select(c.b.build_eq(arg[1], inv_bi), c.i32(-2), arg[1]);
500
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 best = wg_argmax(c, sm.sf, sm.su, arg[0], bi_safe);
501 2 } else {
502 2 Value* inv_bi = c.i32(-1);
503
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto arg = kb.for_range_reduce_n(
504
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 tid, V, block, {ninf, inv_bi}, [&](Value* v, const std::vector<Value*>& st) {
505 2 Value* x = kb.load_f32(ptr_at(c, lp, v, 4));
506 2 Value* win = c.b.build_or(c.b.build_eq(st[1], inv_bi), c.b.build_sgt(x, st[0]));
507 2 return std::vector<Value*>{c.b.build_select(win, x, st[0]), c.b.build_select(win, v, st[1])};
508 });
509
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.
2 Value* bi_safe = c.b.build_select(c.b.build_eq(arg[1], inv_bi), c.i32(-2), arg[1]);
510
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 best = wg_argmax(c, sm.sf, sm.su, arg[0], bi_safe);
511 2 }
512
513
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
514 4 kb.store_i32(ptr_at(c, p[3], cell, 4), c.b.build_select(c.b.build_uge(best, V), c.i32(0), best));
515 4 kb.store_f32(ptr_at(c, p[5], cell, 4), conf);
516 4 Value* is_mask = c.b.build_eq(kb.load_i32(ptr_at(c, p[1], cell, 4)), mask_id);
517
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* s = emit_if_else_values(
518 4 c, is_mask, {Type::f32()},
519 8 [&]() -> std::vector<Value*> {
520 4 Value* sv = c.b.build_sub(conf, c.b.build_mul(kb.u32_to_f32(ch), p[14]));
521
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 if (has_pos_temp) {
522 3 Value* u = hash_uniform_u01(c, key64, 0x5851F42D4C957F2DULL, c.b.build_zext_i64(cell));
523 3 sv = c.b.build_add(c.b.build_sdiv(sv, p[15]), emit_gumbel(c, u));
524 3 }
525 4 return {sv};
526 },
527 8 [&]() -> std::vector<Value*> { return {ninf}; })[0];
528 4 kb.store_f32(ptr_at(c, p[4], cell, 4), s);
529 4 });
530 4 });
531
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 c.b.build_ret_void();
532
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 return finalize_fn(fn);
533 4 }
534
535 } // namespace brotensor::kernels
536