brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
262 / 263
99.6%
Branches:
166 / 310
53.5%
Functions:
32 / 32
100.0%

src/kernels/mir_topk.cpp
Line Branch Exec Source
1 #include "mir_topk.h"
2
3 #include "mir_math.h"
4
5 #include <limits>
6 #include <string>
7 #include <utility>
8 #include <vector>
9
10 namespace brotensor::kernels {
11
12 namespace {
13
14 14 Value* beats(Ctx& c, Value* a, Value* ia, Value* b, Value* ib) {
15 14 return c.b.build_select(c.b.build_ne(a, b), c.b.build_sgt(a, b), c.b.build_ult(ia, ib));
16 }
17
18 18 std::pair<Value*, Value*> merge2(Ctx& c, Value* a1, Value* a2, Value* b1, Value* b2) {
19 18 Value* b1_wins = c.b.build_sgt(b1, a1);
20 36 return {c.b.build_select(b1_wins, b1, a1),
21 18 c.b.build_select(b1_wins, c.kb.fmax(a1, b2), c.kb.fmax(a2, b1))};
22 }
23
24 } // namespace
25
26 11 brass::Function* build_topk_mir(brass::Module& mod, std::uint32_t mode, Dtype dt) {
27
4/8
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
11 const std::string name = "mir_topk_" + std::to_string(mode) + "_dt" + std::to_string(static_cast<int>(dt));
28
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
22 auto* fn = mod.create_function(
29 11 name, Type::void_type(),
30 11 {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32(), Type::i32()});
31
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 brass::codegen::KernelBuilder kb(mod, fn);
32
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 Ctx c(kb);
33
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 auto p = entry_params(c, fn);
34 11 Value *x = p[0], *vals = p[1], *idx = p[2], *off = p[3];
35 11 Value *cols = p[5], *k = p[6], *nseg = p[7];
36
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 Value* tid = c.kb.tid_x();
37
38 // Ranks element i of the `ncols` starting at x[base] by counting the
39 // elements that beat it; the k best land in output row `out_row` in rank
40 // order. `pad`: slots past a range shorter than k get -inf (index 0).
41 16 auto rank_select = [&](Value* base, Value* ncols, Value* out_row, bool pad) {
42 5 Value* sv = c.kb.shared_alloc_f32(256);
43 5 Value* i = c.kb.global_tid_x();
44 5 Value* live = c.b.build_ult(i, ncols);
45
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* v = emit_if_else_values(
46 5 c, live, {Type::f32()},
47 10 [&]() -> std::vector<Value*> { return {load_elem_dt(c, dt, x, c.b.build_add(base, i))}; },
48 10 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
49
50 5 Value* block_sz = c.i32(256);
51
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 Value* rank = c.kb.for_range_reduce(c.i32(0), ncols, block_sz, c.i32(0), [&](Value* j0, Value* r_acc) {
52 5 c.kb.sync();
53 5 Value* j = c.b.build_add(j0, tid);
54
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 c.kb.if_then(c.b.build_ult(j, ncols), [&] {
55 5 c.kb.shared_store_f32_indexed(sv, tid, load_elem_dt(c, dt, x, c.b.build_add(base, j)));
56 5 });
57 5 c.kb.sync();
58 5 Value* rem = c.b.build_sub(ncols, j0);
59 5 Value* n = c.b.build_select(c.b.build_ult(rem, block_sz), rem, block_sz);
60
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 return c.kb.for_range_reduce(c.i32(0), n, c.i32(1), r_acc, [&](Value* t, Value* cur_r) {
61 5 Value* svt = c.kb.shared_load_f32_indexed(sv, t);
62 5 Value* win = beats(c, svt, c.b.build_add(j0, t), v, i);
63 5 return c.b.build_add(cur_r, c.b.build_select(win, c.i32(1), c.i32(0)));
64 });
65 ✗ });
66
67 5 Value* row_off = c.b.build_mul(out_row, k);
68
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 c.kb.if_then(c.b.build_and(live, c.b.build_ult(rank, k)), [&] {
69 5 Value* out_idx = c.b.build_add(row_off, rank);
70 5 c.kb.store_f32(ptr_at(c, vals, out_idx, 4), v);
71 5 c.kb.store_i32(ptr_at(c, idx, out_idx, 4), i);
72 5 });
73
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 if (pad) {
74
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 c.kb.if_then(c.b.build_and(c.b.build_not(live), c.b.build_ult(i, k)), [&] {
75 1 Value* out_idx = c.b.build_add(row_off, i);
76 1 c.kb.store_f32(ptr_at(c, vals, out_idx, 4), c.f32(-std::numeric_limits<float>::infinity()));
77 1 c.kb.store_i32(ptr_at(c, idx, out_idx, 4), c.i32(0));
78 1 });
79 1 }
80 5 };
81
82
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
11 if (mode == 0u) {
83
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* r = c.kb.ctaid_y();
84
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 rank_select(c.b.build_mul(r, cols), cols, r, false);
85
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6 times.
11 } else if (mode == 3u) {
86 // Segmented: row r' = r * nseg + s ranks columns [s * L, s * L + L) of
87 // row r (L = the `rows` parameter), the last segment possibly short.
88 1 Value* seg_len = p[4];
89
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* rp = c.kb.ctaid_y();
90
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* r = c.b.build_udiv(rp, nseg);
91
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* s = c.b.build_umod(rp, nseg);
92
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* start = c.b.build_mul(s, seg_len);
93
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* rem = c.b.build_sub(cols, start);
94
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* ncols = c.b.build_select(c.b.build_ult(rem, seg_len), rem, seg_len);
95
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 rank_select(c.b.build_add(c.b.build_mul(r, cols), start), ncols, rp, true);
96
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 5 times.
7 } else if (mode == 4u) {
97 // Segmented, small k: one 256-thread block per row r' = r * nseg + s
98 // takes the k best of columns [s * L, s * L + L) of row r (L = the
99 // `rows` parameter, <= kTopkSelectSeg) by k rounds of a block argmax
100 // over the segment held in shared memory, each round's winner marked
101 // taken: ~k * L work where rank_select's is L^2. Ties go to the lower
102 // column; slots past a segment shorter than k get -inf (index 0).
103 1 Value* seg_len = p[4];
104
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* rp = c.kb.ctaid_y();
105
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* r = c.b.build_udiv(rp, nseg);
106
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* s = c.b.build_umod(rp, nseg);
107
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* start = c.b.build_mul(s, seg_len);
108
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* rem = c.b.build_sub(cols, start);
109
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* ncols = c.b.build_select(c.b.build_ult(rem, seg_len), rem, seg_len);
110
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* base = c.b.build_add(c.b.build_mul(r, cols), start);
111
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* block_sz = c.i32(256);
112
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* sv = c.kb.shared_alloc_f32(kTopkSelectSeg);
113
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* staken = c.kb.shared_alloc_i32(kTopkSelectSeg);
114
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* rv = c.kb.shared_alloc_f32(256);
115
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* ri = c.kb.shared_alloc_i32(256);
116
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 c.kb.for_range(tid, ncols, block_sz, [&](Value* j) {
117 1 c.kb.shared_store_f32_indexed(sv, j, load_elem_dt(c, dt, x, c.b.build_add(base, j)));
118 1 c.kb.shared_store_i32_indexed(staken, j, c.i32(0));
119 1 });
120
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.kb.sync();
121
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* row_off = c.b.build_mul(rp, k);
122
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* none = c.i32(0x7fffffff);
123
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 c.kb.for_range(c.i32(0), k, c.i32(1), [&](Value* t) {
124
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto best = c.kb.for_range_reduce_n(
125 1 tid, ncols, block_sz, {c.f32(-std::numeric_limits<float>::infinity()), none},
126
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* j, const std::vector<Value*>& cur) -> std::vector<Value*> {
127 1 Value* v = c.kb.shared_load_f32_indexed(sv, j);
128 1 Value* free = c.b.build_eq(c.kb.shared_load_i32_indexed(staken, j), c.i32(0));
129 1 Value* win = c.b.build_and(free, beats(c, v, j, cur[0], cur[1]));
130 1 return {c.b.build_select(win, v, cur[0]), c.b.build_select(win, j, cur[1])};
131 });
132
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.kb.shared_store_f32_indexed(rv, tid, best[0]);
133
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.kb.shared_store_i32_indexed(ri, tid, best[1]);
134
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.kb.sync();
135
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
9 for (int w = 128; w > 0; w >>= 1) {
136
4/8
✓ 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.
16 c.kb.if_then(c.b.build_ult(tid, c.i32(w)), [&] {
137 8 Value* mate = c.b.build_add(tid, c.i32(w));
138 8 Value* bv = c.kb.shared_load_f32_indexed(rv, mate);
139 8 Value* bi = c.kb.shared_load_i32_indexed(ri, mate);
140 8 Value* av = c.kb.shared_load_f32_indexed(rv, tid);
141 8 Value* ai = c.kb.shared_load_i32_indexed(ri, tid);
142
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
16 c.kb.if_then(beats(c, bv, bi, av, ai), [&] {
143 8 c.kb.shared_store_f32_indexed(rv, tid, bv);
144 8 c.kb.shared_store_i32_indexed(ri, tid, bi);
145 8 });
146 8 });
147
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c.kb.sync();
148 8 }
149
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 c.kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
150 1 Value* bv = c.kb.shared_load_f32_indexed(rv, c.i32(0));
151 1 Value* bi = c.kb.shared_load_i32_indexed(ri, c.i32(0));
152 1 Value* found = c.b.build_ult(bi, ncols);
153 1 Value* out_idx = c.b.build_add(row_off, t);
154 2 c.kb.store_f32(ptr_at(c, vals, out_idx, 4),
155 1 c.b.build_select(found, bv, c.f32(-std::numeric_limits<float>::infinity())));
156 1 c.kb.store_i32(ptr_at(c, idx, out_idx, 4), c.b.build_select(found, bi, c.i32(0)));
157
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 c.kb.if_then(found, [&] { c.kb.shared_store_i32_indexed(staken, bi, c.i32(1)); });
158 1 });
159
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.kb.sync();
160 1 });
161
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 3 times.
6 } else if (mode == 5u || mode == 6u) {
162 // Softmax of long rows, a block per segment (as mode 4's layout).
163 // Mode 5: segment r' = r * nseg + s's online (max, sum of exp) into
164 // vals[2 r'], vals[2 r' + 1]. Mode 6: folds row r's nseg partials
165 // (idx, FP32) into its (max, sum) and writes y = exp(x - max) / sum
166 // over the segment into vals (dtype `dt`).
167 2 Value* seg_len = p[4];
168
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* rp = c.kb.ctaid_y();
169
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* r = c.b.build_udiv(rp, nseg);
170
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* s = c.b.build_umod(rp, nseg);
171
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* start = c.b.build_mul(s, seg_len);
172
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* rem = c.b.build_sub(cols, start);
173
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* ncols = c.b.build_select(c.b.build_ult(rem, seg_len), rem, seg_len);
174
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* base = c.b.build_add(c.b.build_mul(r, cols), start);
175
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* block_sz = c.i32(256);
176
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* lowest = c.f32(-3.4028234663852886e38f);
177
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* rm = c.kb.shared_alloc_f32(256);
178
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* rs = c.kb.shared_alloc_f32(256);
179 // (m, s) += (m2, s2), in the log-sum-exp sense.
180 20 auto fold = [&](Value* m, Value* sm, Value* m2, Value* s2) -> std::pair<Value*, Value*> {
181 18 Value* mx = c.kb.fmax(m, m2);
182 36 Value* sum = c.b.build_add(c.b.build_mul(sm, emit_exp(c, c.b.build_sub(m, mx))),
183 18 c.b.build_mul(s2, emit_exp(c, c.b.build_sub(m2, mx))));
184 18 return {mx, sum};
185 };
186 // The block's (m, s) from each thread's, left in rm[0], rs[0].
187 4 auto block_fold = [&](Value* m, Value* sm) {
188 2 c.kb.shared_store_f32_indexed(rm, tid, m);
189 2 c.kb.shared_store_f32_indexed(rs, tid, sm);
190 2 c.kb.sync();
191
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (int w = 128; w > 0; w >>= 1) {
192
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
32 c.kb.if_then(c.b.build_ult(tid, c.i32(w)), [&] {
193 16 Value* mate = c.b.build_add(tid, c.i32(w));
194 64 auto [fm, fs] = fold(c.kb.shared_load_f32_indexed(rm, tid), c.kb.shared_load_f32_indexed(rs, tid),
195 16 c.kb.shared_load_f32_indexed(rm, mate), c.kb.shared_load_f32_indexed(rs, mate));
196 32 c.kb.shared_store_f32_indexed(rm, tid, fm);
197 32 c.kb.shared_store_f32_indexed(rs, tid, fs);
198 16 });
199 16 c.kb.sync();
200 16 }
201 2 };
202
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (mode == 5u) {
203
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 auto acc = c.kb.for_range_reduce_n(
204
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 tid, ncols, block_sz, {lowest, c.f32(0.0f)},
205
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* j, const std::vector<Value*>& cur) -> std::vector<Value*> {
206 1 Value* v = load_elem_dt(c, dt, x, c.b.build_add(base, j));
207 2 auto [fm, fs] = fold(cur[0], cur[1], c.kb.fmax(v, lowest), c.f32(1.0f));
208 2 return {fm, fs};
209 });
210
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 block_fold(acc[0], acc[1]);
211
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 c.kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
212 1 Value* o = c.b.build_mul(rp, c.i32(2));
213 1 c.kb.store_f32(ptr_at(c, vals, o, 4), c.kb.shared_load_f32_indexed(rm, c.i32(0)));
214 1 c.kb.store_f32(ptr_at(c, vals, c.b.build_add(o, c.i32(1)), 4), c.kb.shared_load_f32_indexed(rs, c.i32(0)));
215 1 });
216 1 } else {
217
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* first = c.b.build_mul(r, nseg);
218
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 auto acc = c.kb.for_range_reduce_n(
219
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 tid, nseg, block_sz, {lowest, c.f32(0.0f)},
220
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* q, const std::vector<Value*>& cur) -> std::vector<Value*> {
221 1 Value* o = c.b.build_mul(c.b.build_add(first, q), c.i32(2));
222 3 auto [fm, fs] = fold(cur[0], cur[1], c.kb.load_f32(ptr_at(c, idx, o, 4)),
223 1 c.kb.load_f32(ptr_at(c, idx, c.b.build_add(o, c.i32(1)), 4)));
224 2 return {fm, fs};
225 });
226
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 block_fold(acc[0], acc[1]);
227
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* mx = c.kb.shared_load_f32_indexed(rm, c.i32(0));
228
6/12
✓ 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.
1 Value* inv = c.b.build_sdiv(c.f32(1.0f), c.kb.fmax(c.kb.shared_load_f32_indexed(rs, c.i32(0)), c.f32(1e-30f)));
229
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 c.kb.for_range(tid, ncols, block_sz, [&](Value* j) {
230 1 Value* at = c.b.build_add(base, j);
231 1 Value* v = load_elem_dt(c, dt, x, at);
232 1 store_elem_dt(c, dt, vals, at, c.b.build_mul(emit_exp(c, c.b.build_sub(v, mx)), inv));
233 1 });
234 1 }
235
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
5 } else if (mode == 2u) {
236 // Remap after the segmented pass: idx[t] := local index of the
237 // winner at position off[t] among row r's nseg * k segment winners
238 // (x, INT32), plus its segment's start (segment = position / k,
239 // segment length = cols).
240
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* t = c.kb.global_tid_x();
241 1 Value* rows = p[4];
242
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 c.kb.if_then(c.b.build_ult(t, c.b.build_mul(rows, k)), [&] {
243 1 Value* r = c.b.build_udiv(t, k);
244 1 Value* pos = c.kb.load_i32(ptr_at(c, off, t, 4));
245 1 Value* seg = c.b.build_udiv(pos, k);
246 1 Value* src = c.b.build_add(c.b.build_mul(c.b.build_mul(r, nseg), k), pos);
247 1 Value* local = c.kb.load_i32(ptr_at(c, x, src, 4));
248 1 c.kb.store_i32(ptr_at(c, idx, t, 4), c.b.build_add(local, c.b.build_mul(seg, cols)));
249 1 });
250 1 } else {
251
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* scratch8 = c.kb.shared_alloc_f32(8);
252
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* s1 = c.kb.shared_alloc_f32(256);
253
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* s2 = c.kb.shared_alloc_f32(256);
254
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* block_sz = c.i32(256);
255
256
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.for_range(c.kb.ctaid_x(), nseg, c.kb.nctaid_x(), [&](Value* s) {
257 2 Value* raw_a = c.kb.load_i32(ptr_at(c, off, s, 4));
258 2 Value* raw_b = c.kb.load_i32(ptr_at(c, off, c.b.build_add(s, c.i32(1)), 4));
259 2 Value* a = clamp_i32(c, raw_a, c.i32(0), cols);
260 2 Value* b = clamp_i32(c, raw_b, a, cols);
261 2 Value* non_empty = c.b.build_sgt(b, a);
262 2 Value* start_i = c.b.build_add(a, tid);
263
264
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 Value* mx_loc = c.kb.for_range_reduce(
265 2 start_i, b, block_sz, c.f32(-3.4028234663852886e38f),
266 4 [&](Value* i, Value* mx) { return c.kb.fmax(mx, load_elem_dt(c, dt, x, i)); });
267 2 Value* mx = emit_block_max_f32(c, scratch8, mx_loc);
268
269
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 Value* sum_loc = c.kb.for_range_reduce(start_i, b, block_sz, c.f32(0.0f), [&](Value* i, Value* sm) {
270 2 return c.b.build_add(sm, emit_exp(c, c.b.build_sub(load_elem_dt(c, dt, x, i), mx)));
271 });
272 2 Value* sum = emit_block_sum_f32(c, scratch8, sum_loc);
273 2 Value* safe_sum = c.b.build_select(non_empty, sum, c.f32(1.0f));
274
275
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto acc = c.kb.for_range_reduce_n(
276 2 start_i, b, block_sz, {c.f32(0.0f), c.f32(0.0f), c.f32(0.0f)},
277
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 [&](Value* i, const std::vector<Value*>& cur) -> std::vector<Value*> {
278 4 Value* pr = c.b.build_sdiv(
279 2 emit_exp(c, c.b.build_sub(load_elem_dt(c, dt, x, i), mx)), safe_sum);
280 4 Value* ent = c.b.build_sub(
281 2 cur[2], c.b.build_mul(pr, emit_log(c, c.kb.fmax(pr, c.f32(1e-9f)))));
282 4 auto [nt1, nt2] = merge2(c, cur[0], cur[1], pr, c.f32(0.0f));
283 4 return {nt1, nt2, ent};
284 });
285
286
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* ent_tot = emit_block_sum_f32(c, scratch8, acc[2]);
287
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 c.kb.shared_store_f32_indexed(s1, tid, acc[0]);
288
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 c.kb.shared_store_f32_indexed(s2, tid, acc[1]);
289
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 c.kb.sync();
290
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
18 for (int w = 128; w > 0; w >>= 1) {
291
4/8
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 16 times.
✗ Branch 7 not taken.
32 c.kb.if_then(c.b.build_ult(tid, c.i32(w)), [&] {
292 16 Value* mate = c.b.build_add(tid, c.i32(w));
293 48 auto [na1, na2] = merge2(
294 16 c, c.kb.shared_load_f32_indexed(s1, tid), c.kb.shared_load_f32_indexed(s2, tid),
295 16 c.kb.shared_load_f32_indexed(s1, mate), c.kb.shared_load_f32_indexed(s2, mate));
296 32 c.kb.shared_store_f32_indexed(s1, tid, na1);
297 32 c.kb.shared_store_f32_indexed(s2, tid, na2);
298 16 });
299
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 c.kb.sync();
300 16 }
301
302
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* o = c.b.build_mul(s, c.i32(4));
303
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)), [&] {
304 2 Value* top1 = c.kb.shared_load_f32_indexed(s1, c.i32(0));
305 2 Value* top2 = c.kb.shared_load_f32_indexed(s2, c.i32(0));
306 2 Value* len_f = c.kb.i32_to_f32(c.b.build_sub(b, a));
307 2 Value* kk = c.kb.fmax(c.f32(2.0f), len_f);
308 2 Value* out0 = c.b.build_select(non_empty, top1, c.f32(0.0f));
309 2 Value* out1 = c.b.build_select(non_empty, c.b.build_sub(top1, top2), c.f32(0.0f));
310 2 Value* out2 = c.b.build_select(non_empty, c.b.build_sdiv(ent_tot, emit_log(c, kk)), c.f32(0.0f));
311 2 Value* out3 = c.b.build_select(non_empty, c.b.build_sdiv(kk, c.f32(255.0f)), c.f32(0.0f));
312 2 store_elem_dt(c, dt, vals, o, out0);
313 2 store_elem_dt(c, dt, vals, c.b.build_add(o, c.i32(1)), out1);
314 2 store_elem_dt(c, dt, vals, c.b.build_add(o, c.i32(2)), out2);
315 2 store_elem_dt(c, dt, vals, c.b.build_add(o, c.i32(3)), out3);
316 2 });
317
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 c.kb.sync();
318 2 });
319 }
320
321
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 c.b.build_ret_void();
322
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 return finalize_fn(fn);
323 11 }
324
325 } // namespace brotensor::kernels
326