brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
313 / 425
73.6%
Branches:
310 / 758
40.9%
Functions:
19 / 32
59.4%

src/kernels/mir_fa_cm.cpp
Line Branch Exec Source
1 #include "mir_fa_cm.h"
2
3 #include "mir_common.h"
4 #include "mir_coop_common.h"
5 #include "mir_math.h"
6 #include "../vulkan/shaders/op_codes.h"
7
8 #include <array>
9 #include <stdexcept>
10 #include <string>
11 #include <utility>
12 #include <vector>
13
14 namespace brotensor::kernels {
15
16 namespace {
17
18 5 std::pair<Value*, Value*> emit_fa_cm_interval(Ctx& c, std::uint32_t mode, Value* qi,
19 Value* lq, Value* lk, Value* window, Value* causal,
20 Value* nseq, Value* q_offset, Value* aux, Value* aux2) {
21
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 auto res = emit_if_else_values(
22 5 c, c.b.build_ult(qi, lq), {Type::i32(), Type::i32()},
23 10 [&]() -> std::vector<Value*> {
24 5 Value* a = c.i32(0);
25 5 Value* b = lk;
26
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (mode == FA_MODE_ROWS) {
27 5 Value* aq = c.b.build_add(qi, q_offset);
28 5 Value* is_c = c.b.build_ne(causal, c.i32(0));
29 5 Value* has_w = c.b.build_ugt(window, c.i32(0));
30 5 Value* b_c = smin_i32(c, lk, c.b.build_add(aq, c.i32(1)));
31 5 Value* a_cw = smax_i32(c, c.i32(0), c.b.build_add(c.b.build_sub(aq, window), c.i32(1)));
32 5 Value* a_c = c.b.build_select(has_w, a_cw, c.i32(0));
33 5 Value* w2 = c.b.build_lshr(window, c.i32(1));
34 5 Value* a_nc = c.b.build_select(has_w, smax_i32(c, c.i32(0), c.b.build_sub(aq, w2)), c.i32(0));
35 5 Value* b_nc = c.b.build_select(has_w, smin_i32(c, lk, c.b.build_add(c.b.build_add(aq, w2), c.i32(1))), lk);
36 5 a = c.b.build_select(is_c, a_c, a_nc);
37 5 b = c.b.build_select(is_c, b_c, b_nc);
38
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
5 } else if (mode == FA_MODE_VARLEN) {
39
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* valid = emit_if_else_values(
40 ✗ c, c.b.build_ugt(nseq, c.i32(0)), {Type::i32()},
41 ✗ [&]() -> std::vector<Value*> {
42 ✗ return {c.b.build_slt(qi, c.kb.load_i32(ptr_at(c, aux, nseq, 4)))};
43 },
44 ✗ [&]() -> std::vector<Value*> { return {c.i32(0)}; })[0];
45
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ auto ab = emit_if_else_values(
46 ✗ c, valid, {Type::i32(), Type::i32()},
47 ✗ [&]() -> std::vector<Value*> {
48
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ auto bs = c.kb.for_range_reduce_n(
49 ✗ c.i32(0), c.i32(24), c.i32(1), {c.i32(0), nseq},
50
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ [&](Value*, const std::vector<Value*>& st) -> std::vector<Value*> {
51 ✗ Value* lo = st[0];
52 ✗ Value* hi = st[1];
53 ✗ Value* gt1 = c.b.build_ugt(c.b.build_sub(hi, lo), c.i32(1));
54 ✗ Value* mid = c.b.build_lshr(c.b.build_add(lo, hi), c.i32(1));
55 ✗ Value* le = c.b.build_sle(c.kb.load_i32(ptr_at(c, aux, mid, 4)), qi);
56 ✗ Value* nlo = c.b.build_select(c.b.build_and(gt1, le), mid, lo);
57 ✗ Value* nhi = c.b.build_select(c.b.build_and(gt1, c.b.build_xor(le, c.i32(1))), mid, hi);
58 ✗ return {nlo, nhi};
59 });
60 ✗ Value* s0 = bs[0];
61
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* ka = c.kb.load_i32(ptr_at(c, aux2, s0, 4));
62
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Value* kb_v = c.kb.load_i32(ptr_at(c, aux2, c.b.build_add(s0, c.i32(1)), 4));
63
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* qstart = c.kb.load_i32(ptr_at(c, aux, s0, 4));
64
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Value* kb_c = smin_i32(c, kb_v, c.b.build_add(c.b.build_add(ka, c.b.build_sub(qi, qstart)), c.i32(1)));
65
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ return {ka, c.b.build_select(c.b.build_ne(causal, c.i32(0)), kb_c, kb_v)};
66 ✗ },
67 ✗ [&]() -> std::vector<Value*> { return {c.i32(0), c.i32(0)}; });
68 ✗ a = ab[0];
69 ✗ b = ab[1];
70
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (mode == FA_MODE_LEN) {
71 ✗ Value* flen = clamp_i32(c, c.kb.load_i32(aux), c.i32(0), lk);
72 ✗ b = c.b.build_add(c.b.build_add(c.b.build_sub(flen, lq), c.i32(1)), qi);
73
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (mode == FA_MODE_PACKED) {
74 ✗ Value* q2 = c.b.build_shl(qi, c.i32(1));
75 ✗ a = c.kb.load_i32(ptr_at(c, aux, q2, 4));
76 ✗ b = c.kb.load_i32(ptr_at(c, aux, c.b.build_add(q2, c.i32(1)), 4));
77 ✗ Value* has_w = c.b.build_ugt(window, c.i32(0));
78 ✗ Value* w2 = c.b.build_lshr(window, c.i32(1));
79 ✗ a = c.b.build_select(has_w, smax_i32(c, a, c.b.build_sub(qi, w2)), a);
80 ✗ b = c.b.build_select(has_w, smin_i32(c, b, c.b.build_add(c.b.build_add(qi, w2), c.i32(1))), b);
81 ✗ }
82 5 a = clamp_i32(c, a, c.i32(0), lk);
83 5 b = clamp_i32(c, b, a, lk);
84 5 return {a, b};
85 ✗ },
86 10 [&]() -> std::vector<Value*> { return {c.i32(0), c.i32(0)}; });
87 5 return {res[0], res[1]};
88 5 }
89
90 ✗ Value* load8_fa_scalar(Ctx& c, Dtype dt, std::uint32_t hd, Value* base, Value* off, Value* ok, Value* c0) {
91
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return emit_if_else_values(
92 ✗ c, ok, {Type::i32x4()},
93 ✗ [&]() -> std::vector<Value*> {
94 ✗ std::array<Value*, 8> h{};
95
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int e = 0; e < 8; ++e) {
96 ✗ Value* col = c.b.build_add(c0, c.i32(e));
97
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ h[static_cast<std::size_t>(e)] = emit_if_else_values(
98 ✗ c, c.b.build_ult(col, c.i32(static_cast<int>(hd))), {Type::f32()},
99 ✗ [&]() -> std::vector<Value*> { return {load_elem_dt(c, dt, base, c.b.build_add(off, col))}; },
100 ✗ [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
101 ✗ }
102 ✗ return {emit_pack_f32x8_to_f16_v4(c, h)};
103 ✗ },
104 ✗ [&]() -> std::vector<Value*> { return {emit_v4_zero(c)}; })[0];
105 ✗ }
106
107 } // namespace
108
109 5 brass::Function* build_fa_cm_mir(brass::Module& mod, const FaCmSpec& s) {
110
3/10
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 5 times.
5 if ((s.bc != 16u && s.bc != 32u && s.bc != 64u) || (s.nsg != 1u && s.nsg != 2u && s.nsg != 4u)) {
111
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: mir_fa_cm: invalid bc/nsg");
112 }
113 5 const std::uint32_t HD = s.hd;
114 5 const std::uint32_t DP = ((HD + 15u) / 16u) * 16u;
115 5 const std::uint32_t ND = DP / 16u;
116 5 const std::uint32_t DV = DP / 8u;
117 5 const std::uint32_t DVR = (HD + 7u) / 8u;
118 5 const std::uint32_t SD = DV + 1u;
119 5 const std::uint32_t BR = s.nsg * 16u;
120 5 const std::uint32_t WG = s.nsg * 32u;
121 5 const std::uint32_t NC = s.bc / 16u;
122 5 const std::uint32_t SPH = s.bc + 8u;
123 5 const std::uint32_t SPH_V4 = SPH / 8u;
124 5 const std::uint32_t KVSZ = s.bc * SD;
125 5 const std::uint32_t NKV = s.bc * DV;
126 5 const std::uint32_t LKV = (NKV + WG - 1u) / WG;
127 5 const std::uint32_t Q_KV_SLOTS = std::max(2u * KVSZ, BR * SD);
128
129
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
10 const std::string name = std::string("mir_fa_cm_") + (s.dt == Dtype::FP16 ? "f16" : "bf16") +
130
6/12
✓ 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.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 5 times.
✗ Branch 11 not taken.
5 "_hd" + std::to_string(HD) + "_bc" + std::to_string(s.bc) +
131
6/12
✓ 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.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 5 times.
✗ Branch 11 not taken.
5 "_g" + std::to_string(s.nsg) + "_m" + std::to_string(s.mode) +
132
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.
5 (s.has_mask ? "_mk" : "") + (s.vec ? "_v" : "_s") +
133
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 (s.direct ? "_d" : "") + (s.split ? "_sp" : "") +
134
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 (s.has_softcap ? "_sc" : "") + (s.head_swizzle ? "_hs" : "");
135
136
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 std::vector<Type> params = {
137 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
138 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
139 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
140 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
141 Type::i32(), Type::i32(), Type::i32(),
142 Type::f32(), Type::f32(),
143 };
144
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params));
145
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 brass::codegen::KernelBuilder kb(mod, fn);
146
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Ctx c(kb);
147
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 auto p = entry_params(c, fn);
148 // ptrs: 0:q 1:k 2:v 3:o 4:mask 5:aux 6:aux2 7:part
149 // ints: 8:lq 9:lk 10:ldq 11:ldk 12:ldo 13:group 14:window 15:causal 16:nseq 17:chunk 18:hq 19:qb0 20:q_offset
150 // flts: 21:scale, 22:softcap
151
152
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* KV = kb.shared_alloc_i32x4(Q_KV_SLOTS);
153
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* Ss = kb.shared_alloc_f32(std::max(256u, BR * s.bc));
154
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* Ps = kb.shared_alloc_i32x4(BR * SPH_V4);
155
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* rowfac = kb.shared_alloc_f32(BR);
156
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* maskS = s.has_mask ? kb.shared_alloc_f32(s.bc) : nullptr;
157
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* s_lo = kb.shared_alloc_i32(WG);
158
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* s_hi = kb.shared_alloc_i32(WG);
159
160
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.
5 Value* tid = c.b.build_and(kb.tid_x(), c.i32(static_cast<int>(WG - 1u)));
161
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.
5 Value* sg = c.b.build_and(kb.subgroup_id(), c.i32(static_cast<int>(s.nsg - 1u)));
162
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.
5 Value* lane = c.b.build_and(kb.lane_id(), c.i32(31));
163
164
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
10 Value* qb = s.head_swizzle
165
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 ? c.b.build_add(p[19], c.b.build_sub(c.b.build_sub(kb.nctaid_y(), c.i32(1)), kb.ctaid_y()))
166
6/12
✓ 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.
4 : c.b.build_add(p[19], c.b.build_sub(c.b.build_sub(kb.nctaid_x(), c.i32(1)), kb.ctaid_x()));
167
4/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
5 Value* h = s.head_swizzle ? kb.ctaid_x() : kb.ctaid_y();
168
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* hk = c.b.build_udiv(h, p[13]);
169
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* qoff = c.b.build_mul(h, c.i32(static_cast<int>(HD)));
170
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* koff = c.b.build_mul(hk, c.i32(static_cast<int>(HD)));
171
172
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* r = c.b.build_lshr(lane, c.i32(1));
173
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* half_ = c.b.build_and(lane, c.i32(1));
174
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.
5 Value* row_local = c.b.build_add(c.b.build_shl(sg, c.i32(4)), r);
175
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.
5 Value* qi = c.b.build_add(c.b.build_mul(qb, c.i32(static_cast<int>(BR))), row_local);
176
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
30 auto [lo, hi] = emit_fa_cm_interval(c, s.mode, qi, p[8], p[9], p[14], p[15], p[16], p[20], p[5], p[6]);
177
178 // Populate RI (row indices) in Ss[0..255] and stage Q into KV[0..BR*SD).
179
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 20 times.
25 for (std::uint32_t e0 = 0; e0 < 256u; e0 += WG) {
180
4/6
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
20 Value* e = (e0 == 0) ? tid : c.b.build_add(tid, c.i32(static_cast<int>(e0)));
181
4/8
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 20 times.
✗ Branch 7 not taken.
20 kb.shared_store_f32_indexed(Ss, e, kb.u32_to_f32(c.b.build_lshr(e, c.i32(4))));
182 20 }
183
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* z4 = emit_v4_zero(c);
184
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 20 times.
25 for (std::uint32_t i0 = 0; i0 < BR * DV; i0 += WG) {
185
4/6
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
20 Value* i = (i0 == 0) ? tid : c.b.build_add(tid, c.i32(static_cast<int>(i0)));
186 40 auto stage_q = [&] {
187 20 Value* rr = c.b.build_udiv(i, c.i32(static_cast<int>(DV)));
188 20 Value* col = c.b.build_umod(i, c.i32(static_cast<int>(DV)));
189 20 Value* row = c.b.build_add(c.b.build_mul(qb, c.i32(static_cast<int>(BR))), rr);
190 20 Value* ok = c.b.build_and(c.b.build_ult(row, p[8]), c.b.build_ult(col, c.i32(static_cast<int>(DVR))));
191 20 Value* w = nullptr;
192
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (s.vec) {
193 20 Value* off = c.b.build_add(c.b.build_add(c.b.build_mul(row, p[10]), qoff), c.b.build_shl(col, c.i32(3)));
194 20 Value* raw = kb.vload(Type::i32x4(), ptr_at(c, p[0], c.b.build_select(ok, off, c.i32(0)), 2));
195
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 8 times.
20 if (s.dt == Dtype::BF16) raw = emit_bf16x8_to_f16x8(c, raw);
196 20 w = emit_v4_select(c, ok, raw, z4);
197 20 } else {
198 ✗ w = load8_fa_scalar(c, s.dt, HD, p[0], c.b.build_add(c.b.build_mul(row, p[10]), qoff), ok,
199 ✗ c.b.build_shl(col, c.i32(3)));
200 }
201 20 kb.shared_store_i32x4_indexed(KV, c.b.build_add(c.b.build_mul(rr, c.i32(static_cast<int>(SD))), col), w);
202 20 };
203
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 if (i0 + WG <= BR * DV) stage_q();
204
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ else kb.if_then(c.b.build_ult(i, c.i32(static_cast<int>(BR * DV))), stage_q);
205 20 }
206
207 // Uniform tree reduction for workgroup [kbeg, kend) across all rows.
208
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.
15 Value* has_keys = c.b.build_ult(lo, hi);
209
4/8
✓ 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.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
10 kb.shared_store_i32_indexed(s_lo, tid, c.b.build_select(has_keys, lo, c.i32(static_cast<int>(0x7fffffff))));
210
4/8
✓ 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.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
10 kb.shared_store_i32_indexed(s_hi, tid, c.b.build_select(has_keys, hi, c.i32(0)));
211
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 kb.sync();
212
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 30 times.
35 for (std::uint32_t st = WG / 2u; st >= 1u; st >>= 1) {
213
4/8
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
60 kb.if_then(c.b.build_ult(tid, c.i32(static_cast<int>(st))), [&] {
214 30 Value* t2 = c.b.build_add(tid, c.i32(static_cast<int>(st)));
215 30 Value* a0 = kb.shared_load_i32_indexed(s_lo, tid);
216 30 Value* a1 = kb.shared_load_i32_indexed(s_lo, t2);
217 30 kb.shared_store_i32_indexed(s_lo, tid, smin_i32(c, a0, a1));
218 30 Value* b0 = kb.shared_load_i32_indexed(s_hi, tid);
219 30 Value* b1 = kb.shared_load_i32_indexed(s_hi, t2);
220 30 kb.shared_store_i32_indexed(s_hi, tid, smax_i32(c, b0, b1));
221 30 });
222
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 kb.sync();
223 30 }
224
225
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 std::vector<Value*> fq(ND), acc(ND);
226
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* fk = kb.coop_alloc_b();
227
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* fv = kb.coop_alloc_b();
228
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* fp = kb.coop_alloc_a();
229
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* s_frag = kb.coop_alloc_acc();
230
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* RI = kb.coop_alloc_acc();
231
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* stride_sd = c.i32(static_cast<int>(SD * 8u));
232
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* stride_bc = c.i32(static_cast<int>(s.bc));
233
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* stride_sph = c.i32(static_cast<int>(SPH));
234
235
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 20 times.
25 for (std::uint32_t dc = 0; dc < ND; ++dc) {
236
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 fq[dc] = kb.coop_alloc_a();
237
5/10
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 20 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 20 times.
✗ Branch 9 not taken.
40 Value* q_off = c.b.build_add(c.b.build_mul(c.b.build_shl(sg, c.i32(4)), c.i32(static_cast<int>(SD))),
238
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 c.i32(static_cast<int>(dc * 2u)));
239
4/8
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 20 times.
✗ Branch 7 not taken.
20 kb.coop_load_a(fq[dc], c.b.build_add(KV, c.b.build_shl(q_off, c.i32(4))), stride_sd, false);
240
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 acc[dc] = kb.coop_alloc_acc();
241
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 kb.coop_fill_acc(acc[dc], c.f32(0.0f));
242 20 }
243
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 kb.coop_load_acc(RI, Ss, c.i32(16), false, false);
244
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 kb.sync();
245
246
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* kbeg = kb.shared_load_i32_indexed(s_lo, c.i32(0));
247
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* kend = kb.shared_load_i32_indexed(s_hi, c.i32(0));
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (s.split) {
249
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.
✗ kbeg = c.b.build_add(kbeg, c.b.build_mul(kb.ctaid_z(), p[17]));
250
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ kend = smin_i32(c, kend, c.b.build_add(kbeg, p[17]));
251 ✗ }
252
253 5 struct KvLoaded {
254 std::vector<Value*> rk, rv, vk;
255 5 Value* mreg = nullptr;
256 };
257 10 auto load_kv = [&](Value* kb_v) -> KvLoaded {
258 5 KvLoaded t;
259
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 t.rk.resize(LKV);
260
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 t.rv.resize(LKV);
261
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 t.vk.resize(LKV);
262
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
15 for (std::uint32_t j = 0; j < LKV; ++j) {
263
4/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
10 Value* i = (j == 0) ? tid : c.b.build_add(tid, c.i32(static_cast<int>(j * WG)));
264
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 Value* rr = c.b.build_udiv(i, c.i32(static_cast<int>(DV)));
265
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 Value* col = c.b.build_umod(i, c.i32(static_cast<int>(DV)));
266
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 Value* key = c.b.build_add(kb_v, rr);
267
3/6
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
20 Value* ok = c.b.build_and(c.b.build_ult(i, c.i32(static_cast<int>(NKV))),
268
4/8
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
10 c.b.build_and(c.b.build_ult(key, kend), c.b.build_ult(col, c.i32(static_cast<int>(DVR)))));
269
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (s.vec) {
270
5/10
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 10 times.
✗ Branch 9 not taken.
10 Value* elem_off = c.b.build_add(c.b.build_add(c.b.build_mul(key, p[11]), koff), c.b.build_shl(col, c.i32(3)));
271
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 Value* safe_off = c.b.build_select(ok, elem_off, c.i32(0));
272
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 t.rk[j] = kb.vload(Type::i32x4(), ptr_at(c, p[1], safe_off, 2));
273
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 t.rv[j] = kb.vload(Type::i32x4(), ptr_at(c, p[2], safe_off, 2));
274 10 t.vk[j] = ok;
275 10 } else {
276
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* row_off = c.b.build_add(c.b.build_mul(key, p[11]), koff);
277
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.
✗ t.rk[j] = load8_fa_scalar(c, s.dt, HD, p[1], row_off, ok, c.b.build_shl(col, c.i32(3)));
278
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.
✗ t.rv[j] = load8_fa_scalar(c, s.dt, HD, p[2], row_off, ok, c.b.build_shl(col, c.i32(3)));
279
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ t.vk[j] = c.i32(1);
280 }
281 10 }
282
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (s.has_mask) {
283
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* key = c.b.build_add(kb_v, tid);
284
4/8
✓ 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.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
5 Value* mok = c.b.build_and(c.b.build_ult(tid, c.i32(static_cast<int>(s.bc))), c.b.build_ult(key, kend));
285
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 t.mreg = emit_if_else_values(
286
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 c, mok, {Type::f32()},
287 10 [&]() -> std::vector<Value*> { return {kb.load_f32(ptr_at(c, p[4], key, 4))}; },
288 10 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
289 5 }
290 5 return t;
291
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 };
292
293 10 auto store_kv = [&](const KvLoaded& t) {
294
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
15 for (std::uint32_t j = 0; j < LKV; ++j) {
295
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 Value* i = (j == 0) ? tid : c.b.build_add(tid, c.i32(static_cast<int>(j * WG)));
296 20 auto put = [&] {
297 10 Value* rr = c.b.build_udiv(i, c.i32(static_cast<int>(DV)));
298 10 Value* col = c.b.build_umod(i, c.i32(static_cast<int>(DV)));
299 10 Value* at = c.b.build_add(c.b.build_mul(rr, c.i32(static_cast<int>(SD))), col);
300 10 Value* rk = t.rk[j];
301 10 Value* rv = t.rv[j];
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (s.vec) {
303
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
10 if (s.dt == Dtype::BF16) {
304 4 rk = emit_bf16x8_to_f16x8(c, rk);
305 4 rv = emit_bf16x8_to_f16x8(c, rv);
306 4 }
307 10 rk = emit_v4_select(c, t.vk[j], rk, z4);
308 10 rv = emit_v4_select(c, t.vk[j], rv, z4);
309 10 }
310 10 kb.shared_store_i32x4_indexed(KV, at, rk);
311 10 kb.shared_store_i32x4_indexed(KV, c.b.build_add(at, c.i32(static_cast<int>(KVSZ))), rv);
312 10 };
313
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (j * WG + WG <= NKV) put();
314
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ else kb.if_then(c.b.build_ult(i, c.i32(static_cast<int>(NKV))), put);
315 10 }
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (s.has_mask) {
317
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 kb.if_then(c.b.build_ult(tid, c.i32(static_cast<int>(s.bc))), [&] {
318 5 kb.shared_store_f32_indexed(maskS, tid, t.mreg);
319 5 });
320 5 }
321 5 };
322
323
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* srow = c.b.build_mul(row_local, c.i32(static_cast<int>(s.bc)));
324
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* prow_v4 = c.b.build_mul(row_local, c.i32(static_cast<int>(SPH_V4)));
325
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* c0 = c.b.build_mul(half_, c.i32(static_cast<int>(s.bc / 2u)));
326
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* c0_v4 = c.b.build_lshr(c0, c.i32(3));
327
4/8
✓ 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.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
5 Value* sg_sbase = c.b.build_mul(c.b.build_shl(sg, c.i32(4)), c.i32(static_cast<int>(s.bc)));
328
4/8
✓ 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.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
5 Value* sg_pbase_v4 = c.b.build_mul(c.b.build_shl(sg, c.i32(4)), c.i32(static_cast<int>(SPH_V4)));
329
4/6
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
5 Value* sc_nat_inv = s.has_softcap ? emit_div_rn(c, c.f32(0.69314718055994531f), p[22]) : nullptr;
330
4/6
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
5 Value* sc_log2_cap = s.has_softcap ? c.b.build_mul(p[22], c.f32(1.4426950408889634f)) : nullptr;
331
332
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 auto ml = kb.for_range_reduce_n(
333
4/8
✓ 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.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
5 kbeg, kend, c.i32(static_cast<int>(s.bc)), {c.f32(-1e30f), c.f32(0.0f)},
334
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.
20 [&](Value* kb_v, const std::vector<Value*>& st) -> std::vector<Value*> {
335 5 Value* m = st[0];
336 5 Value* l = st[1];
337
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 store_kv(load_kv(kb_v));
338 5 kb.sync();
339
340
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 for (std::uint32_t j = 0; j < NC; ++j) {
341 5 kb.coop_fill_acc(s_frag, c.f32(0.0f));
342
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 5 times.
25 for (std::uint32_t dc = 0; dc < ND; ++dc) {
343 20 Value* k_off = c.i32(static_cast<int>(j * 16u * SD + dc * 2u));
344 20 kb.coop_load_b(fk, c.b.build_add(KV, c.b.build_shl(k_off, c.i32(4))), stride_sd, true);
345 20 kb.coop_mma(s_frag, fq[dc], fk);
346 20 }
347 5 Value* s_dst = c.b.build_add(sg_sbase, c.i32(static_cast<int>(j * 16u)));
348 5 kb.coop_store_acc(s_frag, c.b.build_add(Ss, c.b.build_shl(s_dst, c.i32(2))), stride_bc, false, false);
349 5 }
350 5 kb.subgroup_barrier();
351
352 5 Value* mx = c.f32(-1e30f);
353
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 5 times.
45 for (std::uint32_t ci = 0; ci < s.bc / 2u; ++ci) {
354 40 Value* col = c.b.build_add(c0, c.i32(static_cast<int>(ci)));
355 40 Value* key = c.b.build_add(kb_v, col);
356 40 Value* ok = c.b.build_and(c.b.build_uge(key, lo), c.b.build_ult(key, hi));
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (s.has_mask) ok = c.b.build_and(ok, c.b.build_sgt(kb.shared_load_f32_indexed(maskS, col), c.f32(0.5f)));
358 40 Value* sc = c.b.build_mul(kb.shared_load_f32_indexed(Ss, c.b.build_add(srow, col)), p[21]);
359
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 8 times.
40 if (s.has_softcap) {
360 8 sc = c.b.build_mul(sc_log2_cap, emit_tanh(c, c.b.build_mul(sc, sc_nat_inv)));
361 8 }
362 40 mx = c.b.build_select(ok, kb.fmax(mx, sc), mx);
363 40 }
364 5 mx = kb.fmax(mx, kb.shfl_bfly_f32(mx, 1u));
365 5 Value* mnew = kb.fmax(m, mx);
366 5 Value* alpha = kb.ex2_approx(c.b.build_sub(m, mnew));
367
368 5 Value* sum = c.f32(0.0f);
369
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 for (std::uint32_t cg = 0; cg < (s.bc / 2u) / 8u; ++cg) {
370 5 std::array<Value*, 8> pv{};
371
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 5 times.
45 for (int e = 0; e < 8; ++e) {
372 40 Value* col = c.b.build_add(c0, c.i32(static_cast<int>(cg * 8u + static_cast<std::uint32_t>(e))));
373 40 Value* key = c.b.build_add(kb_v, col);
374 40 Value* ok = c.b.build_and(c.b.build_uge(key, lo), c.b.build_ult(key, hi));
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (s.has_mask) ok = c.b.build_and(ok, c.b.build_sgt(kb.shared_load_f32_indexed(maskS, col), c.f32(0.5f)));
376 40 Value* sc = c.b.build_mul(kb.shared_load_f32_indexed(Ss, c.b.build_add(srow, col)), p[21]);
377
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 8 times.
40 if (s.has_softcap) {
378 8 sc = c.b.build_mul(sc_log2_cap, emit_tanh(c, c.b.build_mul(sc, sc_nat_inv)));
379 8 }
380 40 Value* pv_e = c.b.build_select(ok, kb.ex2_approx(c.b.build_sub(sc, mnew)), c.f32(0.0f));
381 40 pv[static_cast<std::size_t>(e)] = pv_e;
382 40 sum = c.b.build_add(sum, pv_e);
383 40 }
384 10 kb.shared_store_i32x4_indexed(
385 5 Ps, c.b.build_add(prow_v4, c.b.build_add(c0_v4, c.i32(static_cast<int>(cg)))),
386 5 emit_pack_f32x8_to_f16_v4(c, pv));
387 5 }
388 5 sum = c.b.build_add(sum, kb.shfl_bfly_f32(sum, 1u));
389 5 Value* lnew = c.b.build_fma_f32(l, alpha, sum);
390
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 kb.if_then(c.b.build_eq(half_, c.i32(0)), [&] {
391 5 kb.shared_store_f32_indexed(rowfac, row_local, alpha);
392 5 });
393 5 kb.subgroup_barrier();
394
395 5 Value* sg_rbase = c.b.build_shl(sg, c.i32(4));
396
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 20 times.
25 for (std::uint32_t dc = 0; dc < ND; ++dc) {
397
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
40 emit_coop_acc_map(c, acc[dc], [&](Value* idx, Value* av) {
398 20 Value* ri = c.b.build_and(kb.f32_to_u32(kb.coop_get_acc(RI, idx)), c.i32(15));
399 20 return c.b.build_mul(av, kb.shared_load_f32_indexed(rowfac, c.b.build_add(sg_rbase, ri)));
400 });
401 20 }
402
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 for (std::uint32_t j = 0; j < NC; ++j) {
403 5 Value* p_off = c.b.build_add(sg_pbase_v4, c.i32(static_cast<int>(j * 2u)));
404 5 kb.coop_load_a(fp, c.b.build_add(Ps, c.b.build_shl(p_off, c.i32(4))), stride_sph, false);
405
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 5 times.
25 for (std::uint32_t dc = 0; dc < ND; ++dc) {
406 20 Value* v_off = c.i32(static_cast<int>(KVSZ + j * 16u * SD + dc * 2u));
407 20 kb.coop_load_b(fv, c.b.build_add(KV, c.b.build_shl(v_off, c.i32(4))), stride_sd, false);
408 20 kb.coop_mma(acc[dc], fp, fv);
409 20 }
410 5 }
411 5 kb.sync();
412 5 return {mnew, lnew};
413 ✗ });
414
415 5 Value* m_fin = ml[0];
416 5 Value* l_fin = ml[1];
417
5/10
✓ 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.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5 times.
✗ Branch 9 not taken.
5 Value* row0 = c.b.build_add(c.b.build_mul(qb, c.i32(static_cast<int>(BR))), c.b.build_shl(sg, c.i32(4)));
418
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (s.split) {
420
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* nsplit = kb.nctaid_z();
421
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* sp = kb.ctaid_z();
422
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* stride_p = c.i32(static_cast<int>(HD + 2u));
423
0/12
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ kb.if_then(c.b.build_and(c.b.build_eq(half_, c.i32(0)), c.b.build_ult(qi, p[8])), [&] {
424 ✗ Value* e = c.b.build_mul(
425 ✗ c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(qi, p[18]), h), nsplit), sp), stride_p);
426 ✗ kb.store_f32(ptr_at(c, p[7], c.b.build_add(e, c.i32(static_cast<int>(HD))), 4),
427 ✗ c.b.build_mul(m_fin, c.f32(0.69314718055994531f)));
428 ✗ kb.store_f32(ptr_at(c, p[7], c.b.build_add(e, c.i32(static_cast<int>(HD + 1u))), 4), l_fin);
429 ✗ });
430
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (std::uint32_t dc = 0; dc < ND; ++dc) {
431
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ kb.coop_store_acc(acc[dc], c.b.build_add(Ss, c.b.build_shl(sg_sbase, c.i32(2))), c.i32(16), false, false);
432
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ kb.subgroup_barrier();
433
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int e0 = 0; e0 < 256; e0 += 32) {
434
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.
✗ Value* e = (e0 == 0) ? lane : c.b.build_add(lane, c.i32(e0));
435
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* rr = c.b.build_lshr(e, c.i32(4));
436
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Value* col = c.b.build_add(c.i32(static_cast<int>(dc * 16u)), c.b.build_and(e, c.i32(15)));
437
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* row = c.b.build_add(row0, rr);
438
0/12
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ kb.if_then(c.b.build_and(c.b.build_ult(row, p[8]), c.b.build_ult(col, c.i32(static_cast<int>(HD)))), [&] {
439 ✗ Value* dst_e = c.b.build_add(
440 ✗ c.b.build_mul(c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(row, p[18]), h), nsplit), sp),
441 ✗ stride_p),
442 ✗ col);
443 ✗ kb.store_f32(ptr_at(c, p[7], dst_e, 4), kb.shared_load_f32_indexed(Ss, c.b.build_add(sg_sbase, e)));
444 ✗ });
445 ✗ }
446
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ kb.subgroup_barrier();
447 ✗ }
448
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ c.b.build_ret_void();
449
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return finalize_fn(fn);
450 }
451
452
4/8
✓ 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.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
10 kb.if_then(c.b.build_eq(half_, c.i32(0)), [&] {
453 10 Value* inv_l = c.b.build_select(c.b.build_sgt(l_fin, c.f32(0.0f)),
454 5 c.b.build_sdiv(c.f32(1.0f), c.b.build_select(c.b.build_sgt(l_fin, c.f32(0.0f)), l_fin, c.f32(1.0f))),
455 5 c.f32(0.0f));
456 5 kb.shared_store_f32_indexed(rowfac, row_local, inv_l);
457 5 });
458
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 kb.subgroup_barrier();
459
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (s.direct) {
461
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* sg_rbase = c.b.build_shl(sg, c.i32(4));
462
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* o_row = c.b.build_add(c.b.build_mul(row0, p[12]), qoff);
463
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (std::uint32_t dc = 0; dc < ND; ++dc) {
464
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ emit_coop_acc_map(c, acc[dc], [&](Value* idx, Value* av) {
465 ✗ Value* ri = c.b.build_and(kb.f32_to_u32(kb.coop_get_acc(RI, idx)), c.i32(15));
466 ✗ return c.b.build_mul(av, kb.shared_load_f32_indexed(rowfac, c.b.build_add(sg_rbase, ri)));
467 });
468
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ kb.coop_store_acc(acc[dc], ptr_at(c, p[3], c.b.build_add(o_row, c.i32(static_cast<int>(dc * 16u))), 2),
469 ✗ p[12], false, true);
470 ✗ }
471
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ c.b.build_ret_void();
472
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return finalize_fn(fn);
473 }
474
475
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* sg_rbase = c.b.build_shl(sg, c.i32(4));
476
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 20 times.
25 for (std::uint32_t dc = 0; dc < ND; ++dc) {
477
5/10
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 20 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 20 times.
✗ Branch 9 not taken.
20 kb.coop_store_acc(acc[dc], c.b.build_add(Ss, c.b.build_shl(sg_sbase, c.i32(2))), c.i32(16), false, false);
478
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 kb.subgroup_barrier();
479
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 160 times.
180 for (int e0 = 0; e0 < 256; e0 += 32) {
480
4/6
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 140 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 140 times.
✗ Branch 5 not taken.
160 Value* e = (e0 == 0) ? lane : c.b.build_add(lane, c.i32(e0));
481
2/4
✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 160 times.
✗ Branch 3 not taken.
160 Value* rr = c.b.build_lshr(e, c.i32(4));
482
4/8
✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 160 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 160 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 160 times.
✗ Branch 7 not taken.
160 Value* col = c.b.build_add(c.i32(static_cast<int>(dc * 16u)), c.b.build_and(e, c.i32(15)));
483
1/2
✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
160 Value* row = c.b.build_add(row0, rr);
484
6/12
✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 160 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 160 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 160 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 160 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 160 times.
320 kb.if_then(c.b.build_and(c.b.build_ult(row, p[8]), c.b.build_ult(col, c.i32(static_cast<int>(HD)))), [&] {
485 320 Value* val = c.b.build_mul(kb.shared_load_f32_indexed(Ss, c.b.build_add(sg_sbase, e)),
486 160 kb.shared_load_f32_indexed(rowfac, c.b.build_add(sg_rbase, rr)));
487 160 Value* dst = c.b.build_add(c.b.build_add(c.b.build_mul(row, p[12]), qoff), col);
488 160 store_elem_dt(c, s.dt, p[3], dst, val);
489 160 });
490 160 }
491
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 kb.subgroup_barrier();
492 20 }
493
494
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 c.b.build_ret_void();
495
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 return finalize_fn(fn);
496 5 }
497
498 } // namespace brotensor::kernels
499