brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
524 / 526
99.6%
Branches:
353 / 654
54.0%
Functions:
52 / 52
100.0%

src/kernels/mir_fa_bwd.cpp
Line Branch Exec Source
1 #include "mir_fa_bwd.h"
2
3 #include "mir_common.h"
4 #include "mir_math.h"
5 #include "../vulkan/shaders/op_codes.h"
6
7 #include <stdexcept>
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 namespace brotensor::kernels {
13
14 namespace {
15
16 constexpr float kLog2e = 1.44269504088896340736f;
17
18 432 Value* alloc_stage(Ctx& c, Dtype dt, std::uint32_t count) {
19
2/2
✓ Branch 0 taken 340 times.
✓ Branch 1 taken 92 times.
432 return (dt == Dtype::FP32) ? c.kb.shared_alloc_f32(count) : c.kb.shared_alloc_u16(count);
20 }
21
22 18460 Value* load_stage_f32(Ctx& c, Dtype dt, Value* buf, Value* idx) {
23
2/2
✓ Branch 0 taken 15104 times.
✓ Branch 1 taken 3356 times.
18460 if (dt == Dtype::FP32) return c.kb.shared_load_f32_indexed(buf, idx);
24 15104 Value* raw = c.kb.shared_load_u16_indexed(buf, idx);
25
2/2
✓ Branch 0 taken 8228 times.
✓ Branch 1 taken 6876 times.
15104 return (dt == Dtype::FP16) ? c.kb.f16_to_f32(raw) : c.kb.bf16_to_f32(raw);
26 18460 }
27
28 538 void stage_tile(Ctx& c, Dtype dt, Value* s_buf, Value* g_ptr, Value* g_ld,
29 Value* r0, Value* r_limit, Value* head_col,
30 std::uint32_t rows, std::uint32_t hd, std::uint32_t hdp, std::uint32_t nt) {
31 538 Value* tid = c.kb.tid_x();
32 538 const std::uint32_t per = (rows * hdp) / nt;
33 538 const int esz = elem_bytes(dt);
34
2/2
✓ Branch 0 taken 6472 times.
✓ Branch 1 taken 538 times.
7010 for (std::uint32_t e = 0; e < per; ++e) {
35 6472 Value* idx = c.b.build_add(c.i32(static_cast<int>(e * nt)), tid);
36 6472 Value* r = c.b.build_udiv(idx, c.i32(static_cast<int>(hdp)));
37 6472 Value* d = c.b.build_umod(idx, c.i32(static_cast<int>(hdp)));
38 6472 Value* gr = c.b.build_add(r0, r);
39 6472 Value* ok = c.b.build_and(c.b.build_ult(gr, r_limit), c.b.build_ult(d, c.i32(static_cast<int>(hd))));
40 6472 Value* elem_off = c.b.build_add(c.b.build_add(c.b.build_mul(gr, g_ld), head_col), d);
41
2/2
✓ Branch 0 taken 5408 times.
✓ Branch 1 taken 1064 times.
6472 if (dt == Dtype::FP32) {
42
1/2
✓ Branch 0 taken 1064 times.
✗ Branch 1 not taken.
1064 Value* val = emit_if_else_values(
43 1064 c, ok, {Type::f32()},
44 2128 [&]() -> std::vector<Value*> { return {c.kb.load_f32(ptr_at(c, g_ptr, elem_off, esz))}; },
45 2128 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
46 1064 c.kb.shared_store_f32_indexed(s_buf, idx, val);
47 1064 } else {
48
1/2
✓ Branch 0 taken 5408 times.
✗ Branch 1 not taken.
5408 Value* raw = emit_if_else_values(
49 5408 c, ok, {Type::i32()},
50 10816 [&]() -> std::vector<Value*> { return {c.kb.load_u16(ptr_at(c, g_ptr, elem_off, esz))}; },
51 10816 [&]() -> std::vector<Value*> { return {c.i32(0)}; })[0];
52 5408 c.kb.shared_store_u16_indexed(s_buf, idx, raw);
53 }
54 6472 }
55 538 }
56
57 161 void stage_kval(Ctx& c, bool has_mask, Value* s_kval, Value* mask_ptr, Value* k0, Value* lk,
58 std::uint32_t bc, std::uint32_t nt) {
59 161 Value* tid = c.kb.tid_x();
60
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 161 times.
322 for (std::uint32_t e = 0; e < bc; e += nt) {
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161 times.
161 Value* col = (e == 0) ? tid : c.b.build_add(c.i32(static_cast<int>(e)), tid);
62 322 auto work = [&] {
63 161 Value* gk = c.b.build_add(k0, col);
64 161 Value* in_k = c.b.build_ult(gk, lk);
65 161 Value* ok = in_k;
66
2/2
✓ Branch 0 taken 135 times.
✓ Branch 1 taken 26 times.
161 if (has_mask) {
67
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 Value* mv = emit_if_else_values(
68 26 c, in_k, {Type::f32()},
69 52 [&]() -> std::vector<Value*> { return {c.kb.load_f32(ptr_at(c, mask_ptr, gk, 4))}; },
70 52 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
71 26 ok = c.b.build_and(in_k, c.b.build_slt(c.f32(0.5f), mv));
72 26 }
73 161 c.kb.shared_store_i32_indexed(s_kval, col, c.b.build_select(ok, c.i32(1), c.i32(0)));
74 161 };
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161 times.
161 if (e + nt <= bc) work();
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161 times.
161 else c.kb.if_then(c.b.build_ult(col, c.i32(static_cast<int>(bc))), work);
77 161 }
78 161 }
79
80 108 std::pair<Value*, Value*> emit_bw_interval(Ctx& c, std::uint32_t mode, Value* in_q,
81 Value* qi, Value* lk, Value* causal, Value* window,
82 Value* nseq, Value* aux, Value* aux2) {
83
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 auto res = emit_if_else_values(
84 108 c, in_q, {Type::i32(), Type::i32()},
85 216 [&]() -> std::vector<Value*> {
86
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 28 times.
108 if (mode == FA_MODE_VARLEN) {
87 28 Value* has_seq = c.b.build_sgt(nseq, c.i32(0));
88
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 return emit_if_else_values(
89 28 c, has_seq, {Type::i32(), Type::i32()},
90 56 [&]() -> std::vector<Value*> {
91
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 auto st = c.kb.for_range_reduce_n(
92 28 c.i32(0), c.i32(24), c.i32(1), {c.i32(0), nseq},
93
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
56 [&](Value* /*step*/, const std::vector<Value*>& cur) -> std::vector<Value*> {
94 28 Value* slo = cur[0];
95 28 Value* shi = cur[1];
96 28 Value* active = c.b.build_slt(slo, shi);
97 28 Value* mid = c.b.build_add(slo, c.b.build_ashr(c.b.build_sub(shi, slo), c.i32(1)));
98 28 Value* safe_mid = c.b.build_select(active, mid, c.i32(0));
99 28 Value* end_q = c.kb.load_i32(ptr_at(c, aux, c.b.build_add(safe_mid, c.i32(1)), 4));
100 28 Value* go_right = c.b.build_sle(end_q, qi);
101 28 Value* nlo = c.b.build_select(c.b.build_and(active, go_right), c.b.build_add(safe_mid, c.i32(1)), slo);
102 28 Value* nhi = c.b.build_select(c.b.build_and(active, c.b.build_not(go_right)), safe_mid, shi);
103 28 return {nlo, nhi};
104 });
105
4/8
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
28 Value* b = clamp_i32(c, st[0], c.i32(0), c.b.build_sub(nseq, c.i32(1)));
106
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 Value* qs = c.kb.load_i32(ptr_at(c, aux, b, 4));
107
4/8
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
28 Value* qe = c.kb.load_i32(ptr_at(c, aux, c.b.build_add(b, c.i32(1)), 4));
108
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 Value* ks = c.kb.load_i32(ptr_at(c, aux2, b, 4));
109
4/8
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
28 Value* ke = c.kb.load_i32(ptr_at(c, aux2, c.b.build_add(b, c.i32(1)), 4));
110
3/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
28 Value* in_seq = c.b.build_and(c.b.build_sle(qs, qi), c.b.build_slt(qi, qe));
111
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 Value* lo = smax_i32(c, c.i32(0), ks);
112
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 Value* hi_seq = smin_i32(c, lk, ke);
113
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 Value* is_c = c.b.build_ne(causal, c.i32(0));
114
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 Value* aq = c.b.build_add(ks, c.b.build_sub(qi, qs));
115
4/8
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
28 Value* hi = c.b.build_select(is_c, smin_i32(c, hi_seq, c.b.build_add(aq, c.i32(1))), hi_seq);
116
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 Value* has_win = c.b.build_sgt(window, c.i32(0));
117
4/8
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
28 Value* wlo_c = smax_i32(c, lo, c.b.build_add(c.b.build_sub(aq, window), c.i32(1)));
118
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 Value* hw = c.b.build_ashr(window, c.i32(1));
119
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 Value* wlo_b = smax_i32(c, lo, c.b.build_sub(aq, hw));
120
4/8
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
28 Value* whi_b = smin_i32(c, hi, c.b.build_add(c.b.build_add(aq, hw), c.i32(1)));
121
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 lo = c.b.build_select(has_win, c.b.build_select(is_c, wlo_c, wlo_b), lo);
122
3/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
28 hi = c.b.build_select(c.b.build_and(has_win, c.b.build_not(is_c)), whi_b, hi);
123
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 lo = c.b.build_select(in_seq, lo, c.i32(0));
124
3/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
28 hi = c.b.build_select(in_seq, smax_i32(c, lo, hi), c.i32(0));
125
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 return {lo, hi};
126 28 },
127 56 [&]() -> std::vector<Value*> { return {c.i32(0), c.i32(0)}; });
128 }
129
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 59 times.
80 if (mode == FA_MODE_PACKED) {
130 21 Value* s0 = c.kb.load_i32(ptr_at(c, aux, c.b.build_shl(qi, c.i32(1)), 4));
131 21 Value* s1 = c.kb.load_i32(ptr_at(c, aux, c.b.build_add(c.b.build_shl(qi, c.i32(1)), c.i32(1)), 4));
132 21 Value* lo = smax_i32(c, c.i32(0), s0);
133 21 Value* hi = smin_i32(c, lk, s1);
134 21 Value* has_win = c.b.build_sgt(window, c.i32(0));
135 21 Value* hw = c.b.build_ashr(window, c.i32(1));
136 21 lo = c.b.build_select(has_win, smax_i32(c, lo, c.b.build_sub(qi, hw)), lo);
137 21 hi = c.b.build_select(has_win, smin_i32(c, hi, c.b.build_add(c.b.build_add(qi, hw), c.i32(1))), hi);
138 21 return {lo, smax_i32(c, lo, hi)};
139 }
140 // FA_MODE_ROWS
141 59 Value* is_c = c.b.build_ne(causal, c.i32(0));
142 59 Value* has_win = c.b.build_sgt(window, c.i32(0));
143 59 Value* hw = c.b.build_ashr(window, c.i32(1));
144 59 Value* hi_c = smin_i32(c, lk, c.b.build_add(qi, c.i32(1)));
145 59 Value* hi_nc = c.b.build_select(has_win, smin_i32(c, lk, c.b.build_add(c.b.build_add(qi, hw), c.i32(1))), lk);
146 59 Value* hi = c.b.build_select(is_c, hi_c, hi_nc);
147 59 Value* lo_c = c.b.build_select(has_win, smax_i32(c, c.i32(0), c.b.build_add(c.b.build_sub(qi, window), c.i32(1))), c.i32(0));
148 59 Value* lo_nc = c.b.build_select(has_win, smax_i32(c, c.i32(0), c.b.build_sub(qi, hw)), c.i32(0));
149 59 Value* lo = c.b.build_select(is_c, lo_c, lo_nc);
150 59 return {lo, smax_i32(c, lo, hi)};
151 108 },
152 216 [&]() -> std::vector<Value*> { return {c.i32(0), c.i32(0)}; });
153 108 return {res[0], res[1]};
154 108 }
155
156 55 std::pair<Value*, Value*> emit_bw_query_range(Ctx& c, std::uint32_t mode,
157 Value* k0, Value* kend, Value* lq,
158 Value* causal, Value* window, Value* nseq,
159 Value* aux, Value* aux2) {
160
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 14 times.
55 if (mode == FA_MODE_VARLEN) {
161 14 Value* has_seq = c.b.build_sgt(nseq, c.i32(0));
162
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 auto res = emit_if_else_values(
163 14 c, has_seq, {Type::i32(), Type::i32()},
164 28 [&]() -> std::vector<Value*> {
165
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 auto st0 = c.kb.for_range_reduce_n(
166 14 c.i32(0), c.i32(24), c.i32(1), {c.i32(0), nseq},
167
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
28 [&](Value* /*step*/, const std::vector<Value*>& cur) -> std::vector<Value*> {
168 14 Value* slo = cur[0];
169 14 Value* shi = cur[1];
170 14 Value* active = c.b.build_slt(slo, shi);
171 14 Value* mid = c.b.build_add(slo, c.b.build_ashr(c.b.build_sub(shi, slo), c.i32(1)));
172 14 Value* safe_mid = c.b.build_select(active, mid, c.i32(0));
173 14 Value* end_k = c.kb.load_i32(ptr_at(c, aux2, c.b.build_add(safe_mid, c.i32(1)), 4));
174 14 Value* go_right = c.b.build_sle(end_k, k0);
175 14 Value* nlo = c.b.build_select(c.b.build_and(active, go_right), c.b.build_add(safe_mid, c.i32(1)), slo);
176 14 Value* nhi = c.b.build_select(c.b.build_and(active, c.b.build_not(go_right)), safe_mid, shi);
177 14 return {nlo, nhi};
178 });
179
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
28 auto st1 = c.kb.for_range_reduce_n(
180
5/10
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
14 c.i32(0), c.i32(24), c.i32(1), {c.i32(0), nseq},
181
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
28 [&](Value* /*step*/, const std::vector<Value*>& cur) -> std::vector<Value*> {
182 14 Value* slo = cur[0];
183 14 Value* shi = cur[1];
184 14 Value* active = c.b.build_slt(slo, shi);
185 14 Value* mid = c.b.build_add(slo, c.b.build_ashr(c.b.build_sub(shi, slo), c.i32(1)));
186 14 Value* safe_mid = c.b.build_select(active, mid, c.i32(0));
187 14 Value* beg_k = c.kb.load_i32(ptr_at(c, aux2, safe_mid, 4));
188 14 Value* go_right = c.b.build_slt(beg_k, kend);
189 14 Value* nlo = c.b.build_select(c.b.build_and(active, go_right), c.b.build_add(safe_mid, c.i32(1)), slo);
190 14 Value* nhi = c.b.build_select(c.b.build_and(active, c.b.build_not(go_right)), safe_mid, shi);
191 14 return {nlo, nhi};
192 });
193 14 Value* b0 = st0[0];
194
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 Value* b1 = c.b.build_sub(st1[0], c.i32(1));
195
3/6
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
14 Value* valid = c.b.build_and(c.b.build_slt(b0, nseq), c.b.build_sle(b0, b1));
196
4/8
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
14 Value* sb0 = clamp_i32(c, b0, c.i32(0), c.b.build_sub(nseq, c.i32(1)));
197
4/8
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
14 Value* sb1 = clamp_i32(c, b1, c.i32(0), c.b.build_sub(nseq, c.i32(1)));
198
4/8
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
14 Value* qlo = clamp_i32(c, c.kb.load_i32(ptr_at(c, aux, sb0, 4)), c.i32(0), lq);
199
5/10
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
14 Value* qhi = clamp_i32(c, c.kb.load_i32(ptr_at(c, aux, c.b.build_add(sb1, c.i32(1)), 4)), qlo, lq);
200
5/10
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
14 return {c.b.build_select(valid, qlo, c.i32(0)), c.b.build_select(valid, qhi, c.i32(0))};
201 14 },
202 28 [&]() -> std::vector<Value*> { return {c.i32(0), c.i32(0)}; });
203 14 return {res[0], res[1]};
204 14 }
205
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 31 times.
41 if (mode == FA_MODE_PACKED) {
206 10 return {c.i32(0), lq};
207 }
208 // FA_MODE_ROWS
209 31 Value* is_c = c.b.build_ne(causal, c.i32(0));
210 31 Value* has_win = c.b.build_sgt(window, c.i32(0));
211 31 Value* hw = c.b.build_ashr(window, c.i32(1));
212 31 Value* qlo_c = smax_i32(c, c.i32(0), k0);
213 31 Value* qlo_nc = c.b.build_select(has_win, smax_i32(c, c.i32(0), c.b.build_sub(k0, hw)), c.i32(0));
214 31 Value* qlo = c.b.build_select(is_c, qlo_c, qlo_nc);
215 31 Value* qhi_c = c.b.build_select(has_win, smin_i32(c, lq, c.b.build_add(kend, c.b.build_sub(window, c.i32(1)))), lq);
216 31 Value* qhi_nc = c.b.build_select(has_win, smin_i32(c, lq, c.b.build_add(kend, hw)), lq);
217 31 Value* qhi = c.b.build_select(is_c, qhi_c, qhi_nc);
218 31 return {qlo, smax_i32(c, qlo, qhi)};
219 55 }
220
221 161 void compute_tile_sdp(Ctx& c, Dtype dt, Value* Qs, Value* Ks, Value* Gs, Value* Vs,
222 Value* Ps, Value* Ds, Value* scale,
223 std::uint32_t br, std::uint32_t bc, std::uint32_t hd, std::uint32_t hdp, std::uint32_t nt) {
224 161 Value* tid = c.kb.tid_x();
225 161 const std::uint32_t per = (br * bc) / nt;
226 161 const std::uint32_t hd4 = hd & ~3u;
227
2/2
✓ Branch 0 taken 1070 times.
✓ Branch 1 taken 161 times.
1231 for (std::uint32_t m = 0; m < per; ++m) {
228 1070 Value* cell = c.b.build_add(c.i32(static_cast<int>(m * nt)), tid);
229 1070 Value* r = c.b.build_udiv(cell, c.i32(static_cast<int>(bc)));
230 1070 Value* col = c.b.build_umod(cell, c.i32(static_cast<int>(bc)));
231 1070 Value* rb = c.b.build_mul(r, c.i32(static_cast<int>(hdp)));
232 1070 Value* cb = c.b.build_mul(col, c.i32(static_cast<int>(hdp)));
233
234 1070 std::vector<Value*> acc = {c.f32(0.0f), c.f32(0.0f)};
235
2/2
✓ Branch 0 taken 998 times.
✓ Branch 1 taken 72 times.
1070 if (hd4 > 0u) {
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 998 times.
1996 acc = c.kb.for_range_reduce_n(
237
3/6
✓ Branch 0 taken 998 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 998 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 998 times.
✗ Branch 5 not taken.
998 c.i32(0), c.i32(static_cast<int>(hd4)), c.i32(4), acc,
238
1/2
✓ Branch 0 taken 998 times.
✗ Branch 1 not taken.
1996 [&](Value* d, const std::vector<Value*>& cur) -> std::vector<Value*> {
239 998 Value* s = cur[0];
240 998 Value* dp = cur[1];
241
2/2
✓ Branch 0 taken 3992 times.
✓ Branch 1 taken 998 times.
4990 for (int e = 0; e < 4; ++e) {
242 3992 Value* qi = c.b.build_add(rb, c.b.build_add(d, c.i32(e)));
243 3992 Value* ki = c.b.build_add(cb, c.b.build_add(d, c.i32(e)));
244 3992 Value* qv = load_stage_f32(c, dt, Qs, qi);
245 3992 Value* kv = load_stage_f32(c, dt, Ks, ki);
246 3992 Value* gv = load_stage_f32(c, dt, Gs, qi);
247 3992 Value* vv = load_stage_f32(c, dt, Vs, ki);
248 3992 s = c.b.build_fma_f32(qv, kv, s);
249 3992 dp = c.b.build_fma_f32(gv, vv, dp);
250 3992 }
251 998 return {s, dp};
252 });
253 998 }
254
2/2
✓ Branch 0 taken 1070 times.
✓ Branch 1 taken 144 times.
1214 for (std::uint32_t d = hd4; d < hd; ++d) {
255
2/4
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
144 Value* qi = c.b.build_add(rb, c.i32(static_cast<int>(d)));
256
2/4
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
144 Value* ki = c.b.build_add(cb, c.i32(static_cast<int>(d)));
257
3/6
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 144 times.
✗ Branch 5 not taken.
144 acc[0] = c.b.build_fma_f32(load_stage_f32(c, dt, Qs, qi), load_stage_f32(c, dt, Ks, ki), acc[0]);
258
3/6
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 144 times.
✗ Branch 5 not taken.
144 acc[1] = c.b.build_fma_f32(load_stage_f32(c, dt, Gs, qi), load_stage_f32(c, dt, Vs, ki), acc[1]);
259 144 }
260
2/4
✓ Branch 0 taken 1070 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1070 times.
✗ Branch 3 not taken.
1070 c.kb.shared_store_f32_indexed(Ps, cell, c.b.build_mul(acc[0], scale));
261
1/2
✓ Branch 0 taken 1070 times.
✗ Branch 1 not taken.
1070 c.kb.shared_store_f32_indexed(Ds, cell, acc[1]);
262 1070 }
263 161 }
264
265 53 void tree_reduce_min_max(Ctx& c, Value* s_ctrl, std::uint32_t nt) {
266 53 Value* tid = c.kb.tid_x();
267
2/2
✓ Branch 0 taken 371 times.
✓ Branch 1 taken 53 times.
424 for (std::uint32_t step = nt / 2u; step >= 1u; step >>= 1) {
268 371 c.kb.sync();
269
1/2
✓ Branch 0 taken 371 times.
✗ Branch 1 not taken.
742 c.kb.if_then(c.b.build_ult(tid, c.i32(static_cast<int>(step))), [&] {
270 371 Value* o = c.b.build_add(tid, c.i32(static_cast<int>(step)));
271 742 c.kb.shared_store_i32_indexed(
272 371 s_ctrl, tid,
273 371 smin_i32(c, c.kb.shared_load_i32_indexed(s_ctrl, tid), c.kb.shared_load_i32_indexed(s_ctrl, o)));
274 371 Value* t2 = c.b.build_add(tid, c.i32(static_cast<int>(nt)));
275 371 Value* o2 = c.b.build_add(o, c.i32(static_cast<int>(nt)));
276 742 c.kb.shared_store_i32_indexed(
277 371 s_ctrl, t2,
278 371 smax_i32(c, c.kb.shared_load_i32_indexed(s_ctrl, t2), c.kb.shared_load_i32_indexed(s_ctrl, o2)));
279 371 });
280 371 }
281 53 c.kb.sync();
282 53 }
283
284 108 void compute_ps_ds_weights(Ctx& c, Value* Ps, Value* Ds, Value* kval, Value* s_qlo, Value* s_qhi,
285 Value* s_lse, Value* s_D, Value* k0, std::uint32_t br, std::uint32_t bc,
286 std::uint32_t nt) {
287 108 Value* tid = c.kb.tid_x();
288 108 const std::uint32_t per = (br * bc) / nt;
289
2/2
✓ Branch 0 taken 716 times.
✓ Branch 1 taken 108 times.
824 for (std::uint32_t m = 0; m < per; ++m) {
290 716 Value* cell = c.b.build_add(c.i32(static_cast<int>(m * nt)), tid);
291 716 Value* r = c.b.build_udiv(cell, c.i32(static_cast<int>(bc)));
292 716 Value* col = c.b.build_umod(cell, c.i32(static_cast<int>(bc)));
293 716 Value* gk = c.b.build_add(k0, col);
294 716 Value* kv_ok = c.b.build_ne(c.kb.shared_load_i32_indexed(kval, col), c.i32(0));
295 716 Value* lo_r = c.kb.shared_load_i32_indexed(s_qlo, r);
296 716 Value* hi_r = c.kb.shared_load_i32_indexed(s_qhi, r);
297 716 Value* in_r = c.b.build_and(c.b.build_sge(gk, lo_r), c.b.build_slt(gk, hi_r));
298 716 Value* lse = c.kb.shared_load_f32_indexed(s_lse, r);
299 716 Value* D_r = c.kb.shared_load_f32_indexed(s_D, r);
300 716 Value* valid = c.b.build_and(c.b.build_and(kv_ok, in_r), c.b.build_slt(lse, c.f32(1e29f)));
301 716 Value* sc = c.kb.shared_load_f32_indexed(Ps, cell);
302 716 Value* dp = c.kb.shared_load_f32_indexed(Ds, cell);
303 716 Value* pv = c.b.build_select(valid, c.kb.ex2_approx(c.b.build_sub(sc, lse)), c.f32(0.0f));
304 716 Value* dsv = c.b.build_mul(pv, c.b.build_sub(dp, D_r));
305 716 c.kb.shared_store_f32_indexed(Ps, cell, pv);
306 716 c.kb.shared_store_f32_indexed(Ds, cell, dsv);
307 716 }
308 108 }
309
310 } // namespace
311
312 108 brass::Function* build_fa_bwd_mir(brass::Module& mod, Dtype dt,
313 std::uint32_t nt, std::uint32_t hd, std::uint32_t hdp,
314 std::uint32_t br, std::uint32_t bc,
315 std::uint32_t mode, bool has_mask, std::uint32_t pass) {
316
5/6
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 46 times.
✓ Branch 4 taken 39 times.
✗ Branch 5 not taken.
108 if (dt != Dtype::FP32 && dt != Dtype::FP16 && dt != Dtype::BF16) {
317
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: mir_fa_bwd: unsupported dtype");
318 }
319
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
216 if (nt == 0 || (nt & (nt - 1u)) != 0u || br == 0 || bc == 0 || hd == 0 || hdp < hd ||
320 108 (br * hdp) % nt != 0u || (bc * hdp) % nt != 0u || (br * bc) % nt != 0u || nt < br) {
321
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: mir_fa_bwd: invalid tile geometry");
322 }
323
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
324 const std::string name = std::string("mir_fa_bwd_") +
324
3/4
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 (dt == Dtype::FP32 ? "f32" : dt == Dtype::FP16 ? "f16" : "bf16") +
325
6/12
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 108 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 108 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 108 times.
✗ Branch 11 not taken.
108 "_p" + std::to_string(pass) + "_nt" + std::to_string(nt) +
326
6/12
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 108 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 108 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 108 times.
✗ Branch 11 not taken.
108 "_hd" + std::to_string(hd) + "_br" + std::to_string(br) +
327
6/12
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 108 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 108 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 108 times.
✗ Branch 11 not taken.
108 "_bc" + std::to_string(bc) + "_m" + std::to_string(mode) +
328 108 (has_mask ? "_msk" : "");
329
330
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 std::vector<Type> params = {
331 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
332 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
333 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
334 Type::i32(), Type::i32(), Type::i32(), Type::i32(),
335 Type::f32(), Type::f32(),
336 };
337
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params));
338
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 brass::codegen::KernelBuilder kb(mod, fn);
339
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Ctx c(kb);
340
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 auto p = entry_params(c, fn);
341 // ptrs: 0:q 1:k 2:v 3:g 4:mask 5:aux 6:aux2 7:stats 8:o0 9:o1
342 // ints: 10:lq 11:lk 12:ldq 13:ldk 14:ldg 15:ldo 16:window 17:causal 18:nseq
343 // flts: 19:scale 20:oscale
344
345
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* Qs = alloc_stage(c, dt, br * hdp);
346
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* Gs = alloc_stage(c, dt, br * hdp);
347
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* Ks = alloc_stage(c, dt, bc * hdp);
348
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* Vs = alloc_stage(c, dt, bc * hdp);
349
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* Ps = kb.shared_alloc_f32(br * bc);
350
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* Ds = kb.shared_alloc_f32(br * bc);
351
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* kval = kb.shared_alloc_i32(bc);
352
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* s_ctrl = kb.shared_alloc_i32(nt * 2u);
353
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* s_qlo = kb.shared_alloc_i32(br);
354
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* s_qhi = kb.shared_alloc_i32(br);
355
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* s_lse = kb.shared_alloc_f32(br);
356
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* s_D = kb.shared_alloc_f32(br);
357
358
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* tid = kb.tid_x();
359
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 Value* h = kb.ctaid_y();
360
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 Value* head_col = c.b.build_mul(h, c.i32(static_cast<int>(hd)));
361
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 const int esz = elem_bytes(dt);
362
363
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 55 times.
108 if (pass == 0u) {
364
3/6
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
53 Value* q0 = c.b.build_mul(kb.ctaid_x(), c.i32(static_cast<int>(br)));
365
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 stage_tile(c, dt, Qs, p[0], p[12], q0, p[10], head_col, br, hd, hdp, nt);
366
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 stage_tile(c, dt, Gs, p[3], p[14], q0, p[10], head_col, br, hd, hdp, nt);
367
368 53 Value* def_lo = p[11];
369
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 Value* def_hi = c.i32(0);
370
4/8
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 53 times.
✗ Branch 7 not taken.
106 kb.if_then(c.b.build_ult(tid, c.i32(static_cast<int>(br))), [&] {
371 53 Value* qi = c.b.build_add(q0, tid);
372 53 Value* in_q = c.b.build_ult(qi, p[10]);
373 371 auto [lo_r, hi_r] = emit_bw_interval(c, mode, in_q, qi, p[11], p[17], p[16], p[18], p[5], p[6]);
374 106 kb.shared_store_i32_indexed(s_qlo, tid, lo_r);
375 106 kb.shared_store_i32_indexed(s_qhi, tid, hi_r);
376 159 Value* has_r = c.b.build_slt(lo_r, hi_r);
377 106 kb.shared_store_i32_indexed(s_ctrl, tid, c.b.build_select(has_r, lo_r, def_lo));
378 106 kb.shared_store_i32_indexed(
379 53 s_ctrl, c.b.build_add(tid, c.i32(static_cast<int>(nt))),
380 106 c.b.build_select(has_r, hi_r, def_hi));
381 53 });
382
4/8
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 53 times.
✗ Branch 7 not taken.
106 kb.if_then(c.b.build_uge(tid, c.i32(static_cast<int>(br))), [&] {
383 53 kb.shared_store_i32_indexed(s_ctrl, tid, def_lo);
384 53 kb.shared_store_i32_indexed(s_ctrl, c.b.build_add(tid, c.i32(static_cast<int>(nt))), def_hi);
385 53 });
386
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 tree_reduce_min_max(c, s_ctrl, nt);
387
388
2/4
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
53 Value* tile_lo = kb.shared_load_i32_indexed(s_ctrl, c.i32(0));
389
2/4
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
53 Value* tile_hi = kb.shared_load_i32_indexed(s_ctrl, c.i32(static_cast<int>(nt)));
390
391 // Sweep 1: online softmax statistics (m, l, D_num) per query row.
392
4/8
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 53 times.
✗ Branch 7 not taken.
53 std::vector<Value*> st_init = {c.f32(-1e30f), c.f32(0.0f), c.f32(0.0f)};
393
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 std::vector<Value*> st = kb.for_range_reduce_n(
394
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 tile_lo, tile_hi, c.i32(static_cast<int>(bc)), st_init,
395
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
106 [&](Value* k0, const std::vector<Value*>& cur) -> std::vector<Value*> {
396 53 stage_tile(c, dt, Ks, p[1], p[13], k0, p[11], head_col, bc, hd, hdp, nt);
397 53 stage_tile(c, dt, Vs, p[2], p[13], k0, p[11], head_col, bc, hd, hdp, nt);
398 53 stage_kval(c, has_mask, kval, p[4], k0, p[11], bc, nt);
399 53 kb.sync();
400 53 compute_tile_sdp(c, dt, Qs, Ks, Gs, Vs, Ps, Ds, p[19], br, bc, hd, hdp, nt);
401 53 kb.sync();
402
403 106 Value* is_row = c.b.build_and(c.b.build_ult(tid, c.i32(static_cast<int>(br))),
404 53 c.b.build_ult(c.b.build_add(q0, tid), p[10]));
405
2/4
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
106 std::vector<Value*> next = emit_if_else_values(
406 53 c, is_row, {Type::f32(), Type::f32(), Type::f32()},
407 106 [&]() -> std::vector<Value*> {
408 53 Value* lo_r = kb.shared_load_i32_indexed(s_qlo, tid);
409 53 Value* hi_r = kb.shared_load_i32_indexed(s_qhi, tid);
410 53 Value* r_base = c.b.build_mul(tid, c.i32(static_cast<int>(bc)));
411 53 Value* m_tile = c.f32(-1e30f);
412
2/2
✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 53 times.
1493 for (std::uint32_t col = 0; col < bc; ++col) {
413 1440 Value* gk = c.b.build_add(k0, c.i32(static_cast<int>(col)));
414 2880 Value* ok = c.b.build_and(
415 1440 c.b.build_ne(kb.shared_load_i32_indexed(kval, c.i32(static_cast<int>(col))), c.i32(0)),
416 1440 c.b.build_and(c.b.build_sge(gk, lo_r), c.b.build_slt(gk, hi_r)));
417 1440 Value* sv = kb.shared_load_f32_indexed(Ps, c.b.build_add(r_base, c.i32(static_cast<int>(col))));
418 1440 m_tile = c.b.build_select(ok, kb.fmax(m_tile, sv), m_tile);
419 1440 }
420 53 Value* m_new = kb.fmax(cur[0], m_tile);
421 53 Value* has_any = c.b.build_sgt(m_new, c.f32(-1e29f));
422 53 Value* had_prev = c.b.build_sgt(cur[0], c.f32(-1e29f));
423 106 Value* alpha = c.b.build_select(
424 53 c.b.build_and(has_any, had_prev), kb.ex2_approx(c.b.build_sub(cur[0], m_new)), c.f32(0.0f));
425 53 Value* l_sum = c.f32(0.0f);
426 53 Value* d_sum = c.f32(0.0f);
427
2/2
✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 53 times.
1493 for (std::uint32_t col = 0; col < bc; ++col) {
428 1440 Value* gk = c.b.build_add(k0, c.i32(static_cast<int>(col)));
429 2880 Value* ok = c.b.build_and(
430 1440 c.b.build_ne(kb.shared_load_i32_indexed(kval, c.i32(static_cast<int>(col))), c.i32(0)),
431 1440 c.b.build_and(c.b.build_sge(gk, lo_r), c.b.build_slt(gk, hi_r)));
432 1440 Value* cidx = c.b.build_add(r_base, c.i32(static_cast<int>(col)));
433 1440 Value* sv = kb.shared_load_f32_indexed(Ps, cidx);
434 1440 Value* dpv = kb.shared_load_f32_indexed(Ds, cidx);
435 2880 Value* pv = c.b.build_select(c.b.build_and(ok, has_any),
436 1440 kb.ex2_approx(c.b.build_sub(sv, m_new)), c.f32(0.0f));
437 1440 l_sum = c.b.build_add(l_sum, pv);
438 1440 d_sum = c.b.build_fma_f32(pv, dpv, d_sum);
439 1440 }
440 53 return {m_new, c.b.build_fma_f32(cur[1], alpha, l_sum), c.b.build_fma_f32(cur[2], alpha, d_sum)};
441 },
442 106 [&]() -> std::vector<Value*> { return cur; });
443
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 kb.sync();
444 53 return next;
445
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 });
446
447
4/8
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 53 times.
✗ Branch 7 not taken.
106 kb.if_then(c.b.build_ult(tid, c.i32(static_cast<int>(br))), [&] {
448 53 Value* l_pos = c.b.build_sgt(st[1], c.f32(0.0f));
449 106 Value* lse = c.b.build_select(
450 53 l_pos, c.b.build_fma_f32(emit_log(c, c.b.build_select(l_pos, st[1], c.f32(1.0f))), c.f32(kLog2e), st[0]),
451 53 c.f32(1e30f));
452 53 Value* D = c.b.build_select(l_pos, emit_div_rn(c, st[2], c.b.build_select(l_pos, st[1], c.f32(1.0f))), c.f32(0.0f));
453 53 kb.shared_store_f32_indexed(s_lse, tid, lse);
454 53 kb.shared_store_f32_indexed(s_D, tid, D);
455 53 Value* qi = c.b.build_add(q0, tid);
456
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
106 kb.if_then(c.b.build_ult(qi, p[10]), [&] {
457 53 Value* stat_idx = c.b.build_shl(c.b.build_add(c.b.build_mul(h, p[10]), qi), c.i32(1));
458 53 kb.store_f32(ptr_at(c, p[7], stat_idx, 4), lse);
459 53 kb.store_f32(ptr_at(c, p[7], c.b.build_add(stat_idx, c.i32(1)), 4), D);
460 53 });
461 53 });
462
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 kb.sync();
463
464 // Sweep 2: recompute S & dP and accumulate dQ.
465 53 const std::uint32_t nq_e = (br * hdp) / nt;
466
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 std::vector<Value*> dq_acc = kb.for_range_reduce_n(
467
3/6
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
53 tile_lo, tile_hi, c.i32(static_cast<int>(bc)), std::vector<Value*>(nq_e, c.f32(0.0f)),
468
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
106 [&](Value* k0, const std::vector<Value*>& cur) -> std::vector<Value*> {
469 53 stage_tile(c, dt, Ks, p[1], p[13], k0, p[11], head_col, bc, hd, hdp, nt);
470 53 stage_tile(c, dt, Vs, p[2], p[13], k0, p[11], head_col, bc, hd, hdp, nt);
471 53 stage_kval(c, has_mask, kval, p[4], k0, p[11], bc, nt);
472 53 kb.sync();
473 53 compute_tile_sdp(c, dt, Qs, Ks, Gs, Vs, Ps, Ds, p[19], br, bc, hd, hdp, nt);
474 53 kb.sync();
475 53 compute_ps_ds_weights(c, Ps, Ds, kval, s_qlo, s_qhi, s_lse, s_D, k0, br, bc, nt);
476 53 kb.sync();
477
478 53 std::vector<Value*> next = cur;
479
3/6
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
53 Value* tile_c = smin_i32(c, c.i32(static_cast<int>(bc)), c.b.build_sub(tile_hi, k0));
480
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 724 times.
777 for (std::uint32_t e = 0; e < nq_e; ++e) {
481
2/4
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 724 times.
✗ Branch 3 not taken.
724 Value* idx = c.b.build_add(c.i32(static_cast<int>(e * nt)), tid);
482
2/4
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 724 times.
✗ Branch 3 not taken.
724 Value* r = c.b.build_udiv(idx, c.i32(static_cast<int>(hdp)));
483
2/4
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 724 times.
✗ Branch 3 not taken.
724 Value* d = c.b.build_umod(idx, c.i32(static_cast<int>(hdp)));
484
2/4
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 724 times.
✗ Branch 3 not taken.
724 Value* rb = c.b.build_mul(r, c.i32(static_cast<int>(bc)));
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 724 times.
1448 next[e] = kb.for_range_reduce(
486
2/4
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 724 times.
✗ Branch 3 not taken.
724 c.i32(0), tile_c, c.i32(1), cur[e],
487
1/2
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
1448 [&](Value* col, Value* acc) -> Value* {
488 724 Value* dsv = kb.shared_load_f32_indexed(Ds, c.b.build_add(rb, col));
489 724 Value* kv = load_stage_f32(
490 724 c, dt, Ks, c.b.build_add(c.b.build_mul(col, c.i32(static_cast<int>(hdp))), d));
491 724 return c.b.build_fma_f32(dsv, kv, acc);
492 });
493 724 }
494
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 kb.sync();
495 53 return next;
496
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 });
497
498
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 724 times.
777 for (std::uint32_t e = 0; e < nq_e; ++e) {
499
2/4
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 724 times.
✗ Branch 3 not taken.
724 Value* idx = c.b.build_add(c.i32(static_cast<int>(e * nt)), tid);
500
2/4
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 724 times.
✗ Branch 3 not taken.
724 Value* r = c.b.build_udiv(idx, c.i32(static_cast<int>(hdp)));
501
2/4
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 724 times.
✗ Branch 3 not taken.
724 Value* d = c.b.build_umod(idx, c.i32(static_cast<int>(hdp)));
502
1/2
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
724 Value* qr = c.b.build_add(q0, r);
503
4/8
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 724 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 724 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 724 times.
✗ Branch 7 not taken.
724 Value* ok = c.b.build_and(c.b.build_ult(qr, p[10]), c.b.build_ult(d, c.i32(static_cast<int>(hd))));
504
2/4
✓ Branch 0 taken 724 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 724 times.
✗ Branch 3 not taken.
1448 kb.if_then(ok, [&] {
505 724 Value* out_idx = c.b.build_add(c.b.build_add(c.b.build_mul(qr, p[15]), head_col), d);
506 724 store_elem_dt(c, dt, p[8], out_idx, c.b.build_mul(dq_acc[e], p[20]));
507 724 });
508 724 }
509
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 c.b.build_ret_void();
510
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 return finalize_fn(fn);
511 53 }
512
513 // PASS 1: dK (o0), dV (o1)
514
3/6
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
55 Value* k0 = c.b.build_mul(kb.ctaid_x(), c.i32(static_cast<int>(bc)));
515
3/6
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
55 Value* kend = smin_i32(c, p[11], c.b.build_add(k0, c.i32(static_cast<int>(bc))));
516
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 stage_tile(c, dt, Ks, p[1], p[13], k0, p[11], head_col, bc, hd, hdp, nt);
517
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 stage_tile(c, dt, Vs, p[2], p[13], k0, p[11], head_col, bc, hd, hdp, nt);
518
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 stage_kval(c, has_mask, kval, p[4], k0, p[11], bc, nt);
519
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 kb.sync();
520
521
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
110 auto [q_lo, q_hi] = emit_bw_query_range(c, mode, k0, kend, p[10], p[17], p[16], p[18], p[5], p[6]);
522 55 const std::uint32_t nk_e = (bc * hdp) / nt;
523
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 std::vector<Value*> kv_acc = kb.for_range_reduce_n(
524
4/8
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 55 times.
✗ Branch 7 not taken.
110 q_lo, q_hi, c.i32(static_cast<int>(br)), std::vector<Value*>(2u * nk_e, c.f32(0.0f)),
525
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
165 [&](Value* q0, const std::vector<Value*>& cur) -> std::vector<Value*> {
526 55 kb.shared_store_i32_indexed(s_ctrl, tid, c.i32(0));
527
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
110 kb.if_then(c.b.build_ult(tid, c.i32(static_cast<int>(br))), [&] {
528 55 Value* qi = c.b.build_add(q0, tid);
529 55 Value* in_q = c.b.build_ult(qi, p[10]);
530 275 auto [lo_r, hi_r] = emit_bw_interval(c, mode, in_q, qi, p[11], p[17], p[16], p[18], p[5], p[6]);
531 110 kb.shared_store_i32_indexed(s_qlo, tid, lo_r);
532 110 kb.shared_store_i32_indexed(s_qhi, tid, hi_r);
533 165 Value* ov = c.b.build_and(in_q, c.b.build_slt(smax_i32(c, k0, lo_r), smin_i32(c, kend, hi_r)));
534 55 kb.shared_store_i32_indexed(s_ctrl, tid, c.b.build_select(ov, c.i32(1), c.i32(0)));
535
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 Value* lse = emit_if_else_values(
536 55 c, in_q, {Type::f32()},
537 110 [&]() -> std::vector<Value*> {
538 55 Value* si = c.b.build_shl(c.b.build_add(c.b.build_mul(h, p[10]), qi), c.i32(1));
539 55 return {kb.load_f32(ptr_at(c, p[7], si, 4))};
540 },
541 110 [&]() -> std::vector<Value*> { return {c.f32(1e30f)}; })[0];
542
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 Value* Dv = emit_if_else_values(
543 55 c, in_q, {Type::f32()},
544 110 [&]() -> std::vector<Value*> {
545 55 Value* si = c.b.build_shl(c.b.build_add(c.b.build_mul(h, p[10]), qi), c.i32(1));
546 55 return {kb.load_f32(ptr_at(c, p[7], c.b.build_add(si, c.i32(1)), 4))};
547 },
548 110 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
549 55 kb.shared_store_f32_indexed(s_lse, tid, lse);
550 55 kb.shared_store_f32_indexed(s_D, tid, Dv);
551 55 });
552
2/2
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 55 times.
440 for (std::uint32_t step = nt / 2u; step >= 1u; step >>= 1) {
553 385 kb.sync();
554
1/2
✓ Branch 0 taken 385 times.
✗ Branch 1 not taken.
770 kb.if_then(c.b.build_ult(tid, c.i32(static_cast<int>(step))), [&] {
555 385 Value* a = kb.shared_load_i32_indexed(s_ctrl, tid);
556 385 Value* b = kb.shared_load_i32_indexed(s_ctrl, c.b.build_add(tid, c.i32(static_cast<int>(step))));
557 385 kb.shared_store_i32_indexed(s_ctrl, tid, c.b.build_or(a, b));
558 385 });
559 385 }
560 55 kb.sync();
561
562 55 Value* any_q = c.b.build_ne(kb.shared_load_i32_indexed(s_ctrl, c.i32(0)), c.i32(0));
563
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
110 kb.if_then(any_q, [&] {
564 55 stage_tile(c, dt, Qs, p[0], p[12], q0, p[10], head_col, br, hd, hdp, nt);
565 55 stage_tile(c, dt, Gs, p[3], p[14], q0, p[10], head_col, br, hd, hdp, nt);
566 55 });
567 55 kb.sync();
568
569
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
110 kb.if_then(any_q, [&] { compute_tile_sdp(c, dt, Qs, Ks, Gs, Vs, Ps, Ds, p[19], br, bc, hd, hdp, nt); });
570 55 kb.sync();
571
572
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
110 kb.if_then(any_q, [&] { compute_ps_ds_weights(c, Ps, Ds, kval, s_qlo, s_qhi, s_lse, s_D, k0, br, bc, nt); });
573 55 kb.sync();
574
575
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
110 std::vector<Value*> next = emit_if_else_values(
576 55 c, any_q, std::vector<Type>(2u * nk_e, Type::f32()),
577 110 [&]() -> std::vector<Value*> {
578 55 std::vector<Value*> out = cur;
579
3/6
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
55 Value* tile_r = smin_i32(c, c.i32(static_cast<int>(br)), c.b.build_sub(q_hi, q0));
580
2/2
✓ Branch 0 taken 596 times.
✓ Branch 1 taken 55 times.
651 for (std::uint32_t e = 0; e < nk_e; ++e) {
581
2/4
✓ Branch 0 taken 596 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 596 times.
✗ Branch 3 not taken.
596 Value* idx = c.b.build_add(c.i32(static_cast<int>(e * nt)), tid);
582
2/4
✓ Branch 0 taken 596 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 596 times.
✗ Branch 3 not taken.
596 Value* col = c.b.build_udiv(idx, c.i32(static_cast<int>(hdp)));
583
2/4
✓ Branch 0 taken 596 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 596 times.
✗ Branch 3 not taken.
596 Value* d = c.b.build_umod(idx, c.i32(static_cast<int>(hdp)));
584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 596 times.
1192 auto res = kb.for_range_reduce_n(
585
3/6
✓ Branch 0 taken 596 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 596 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 596 times.
✗ Branch 5 not taken.
596 c.i32(0), tile_r, c.i32(1), {cur[e], cur[nk_e + e]},
586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 596 times.
1192 [&](Value* r, const std::vector<Value*>& acc) -> std::vector<Value*> {
587 596 Value* cell = c.b.build_add(c.b.build_mul(r, c.i32(static_cast<int>(bc))), col);
588 596 Value* pv = kb.shared_load_f32_indexed(Ps, cell);
589 596 Value* dsv = kb.shared_load_f32_indexed(Ds, cell);
590 596 Value* sidx = c.b.build_add(c.b.build_mul(r, c.i32(static_cast<int>(hdp))), d);
591 596 Value* qv = load_stage_f32(c, dt, Qs, sidx);
592 596 Value* gv = load_stage_f32(c, dt, Gs, sidx);
593 596 return {c.b.build_fma_f32(dsv, qv, acc[0]), c.b.build_fma_f32(pv, gv, acc[1])};
594 });
595 596 out[e] = res[0];
596 596 out[nk_e + e] = res[1];
597 596 }
598 55 return out;
599
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 },
600 110 [&]() -> std::vector<Value*> { return cur; });
601
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 kb.sync();
602 55 return next;
603
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 });
604
605
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 596 times.
651 for (std::uint32_t e = 0; e < nk_e; ++e) {
606
2/4
✓ Branch 0 taken 596 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 596 times.
✗ Branch 3 not taken.
596 Value* idx = c.b.build_add(c.i32(static_cast<int>(e * nt)), tid);
607
2/4
✓ Branch 0 taken 596 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 596 times.
✗ Branch 3 not taken.
596 Value* col = c.b.build_udiv(idx, c.i32(static_cast<int>(hdp)));
608
2/4
✓ Branch 0 taken 596 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 596 times.
✗ Branch 3 not taken.
596 Value* d = c.b.build_umod(idx, c.i32(static_cast<int>(hdp)));
609
1/2
✓ Branch 0 taken 596 times.
✗ Branch 1 not taken.
596 Value* kr = c.b.build_add(k0, col);
610
4/8
✓ Branch 0 taken 596 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 596 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 596 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 596 times.
✗ Branch 7 not taken.
596 Value* ok = c.b.build_and(c.b.build_ult(kr, p[11]), c.b.build_ult(d, c.i32(static_cast<int>(hd))));
611
2/4
✓ Branch 0 taken 596 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 596 times.
1192 kb.if_then(ok, [&] {
612 596 Value* out_idx = c.b.build_add(c.b.build_add(c.b.build_mul(kr, p[15]), head_col), d);
613 596 store_elem_dt(c, dt, p[8], out_idx, c.b.build_mul(kv_acc[e], p[20]));
614 596 store_elem_dt(c, dt, p[9], out_idx, kv_acc[nk_e + e]);
615 596 });
616 596 }
617 (void)esz;
618
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 c.b.build_ret_void();
619
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 return finalize_fn(fn);
620 108 }
621
622 } // namespace brotensor::kernels
623