brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
264 / 272
97.1%
Branches:
157 / 286
54.9%
Functions:
44 / 44
100.0%

src/kernels/mir_attn_small.cpp
Line Branch Exec Source
1 #include "mir_attn_small.h"
2
3 #include "mir_common.h"
4 #include "mir_math.h"
5
6 #include <string>
7
8 namespace brotensor::kernels {
9
10 namespace {
11
12 constexpr std::uint32_t kAuxRowGate = 0;
13 constexpr std::uint32_t kAuxHeadMean = 1;
14
15 39 Value* valid_mask(Ctx& c, Value* mask_ptr, Value* idx, bool mask_ge) {
16 39 Value* v = c.kb.load_f32(ptr_at(c, mask_ptr, idx, 4));
17
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 21 times.
39 return mask_ge ? c.b.build_sge(v, c.f32(0.5f)) : c.b.build_slt(c.f32(0.5f), v);
18 }
19
20 63 Value* eval_softmax_score(Ctx& c, std::uint32_t bias_mode, Value* s_ptr, Value* bias_ptr, Value* mask_ptr,
21 Value* so, Value* bo, Value* h, Value* q, Value* k, Value* bhs, Value* scale) {
22 63 Value* sv = c.kb.load_f32(ptr_at(c, s_ptr, c.b.build_add(so, k), 4));
23 63 Value* x = c.b.build_mul(sv, scale);
24
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 39 times.
63 if (bias_mode == 1u) {
25 24 return c.b.build_add(x, c.kb.load_f32(ptr_at(c, bias_ptr, c.b.build_add(bo, k), 4)));
26 }
27
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 24 times.
39 if (bias_mode == 2u) {
28 15 Value* gw = c.b.build_and(bhs, c.i32(0xffff));
29 15 Value* gh = c.b.build_lshr(bhs, c.i32(16));
30 15 Value* rq = c.b.build_add(c.b.build_mul(h, c.b.build_mul(gh, gw)), q);
31 15 Value* qh = c.b.build_udiv(q, gw);
32 15 Value* qw = c.b.build_umod(q, gw);
33 15 Value* kh = c.b.build_udiv(k, gw);
34 15 Value* kw = c.b.build_umod(k, gw);
35 15 Value* dh_idx = c.b.build_add(c.b.build_sub(qh, kh), c.b.build_sub(gh, c.i32(1)));
36 15 Value* dw_idx = c.b.build_add(c.b.build_sub(qw, kw), c.b.build_sub(gw, c.i32(1)));
37 15 Value* h_stride = c.b.build_sub(c.b.build_shl(gh, c.i32(1)), c.i32(1));
38 15 Value* w_stride = c.b.build_sub(c.b.build_shl(gw, c.i32(1)), c.i32(1));
39 15 Value* bh = c.kb.load_f32(ptr_at(c, bias_ptr, c.b.build_add(c.b.build_mul(rq, h_stride), dh_idx), 4));
40 15 Value* bw = c.kb.load_f32(ptr_at(c, mask_ptr, c.b.build_add(c.b.build_mul(rq, w_stride), dw_idx), 4));
41 15 return c.b.build_add(x, c.b.build_add(bh, bw));
42 }
43 24 return x;
44 63 }
45
46 } // namespace
47
48 10 brass::Function* build_attn_aux_mir(brass::Module& mod, std::uint32_t mode, Dtype out_dt, bool has_mask) {
49
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
30 const std::string name = std::string("mir_attn_aux_") +
50
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
20 (mode == kAuxRowGate ? "gate_" : "mean_") +
51
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 (out_dt == Dtype::FP32 ? "f32" : out_dt == Dtype::FP16 ? "f16" : "bf16") +
52 10 (has_mask ? "_m" : "");
53
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 std::vector<Type> params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32()};
54
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params));
55
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 brass::codegen::KernelBuilder kb(mod, fn);
56
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 Ctx c(kb);
57
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 auto p = entry_params(c, fn);
58 // p[0]=x, p[1]=y, p[2]=mask, p[3]=rows, p[4]=n
59
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 Value* gid = kb.global_tid_x();
60
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 Value* stride = grid_stride_ew(c);
61
62
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 time.
10 if (mode == kAuxRowGate) {
63
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 Value* total = c.b.build_mul(p[3], p[4]);
64
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
18 kb.for_range(gid, total, stride, [&](Value* i) {
65 9 Value* xv = kb.load_f32(ptr_at(c, p[0], i, 4));
66 9 Value* out = xv;
67
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (has_mask) {
68 7 Value* row = c.b.build_udiv(i, p[4]);
69 7 Value* mv = kb.load_f32(ptr_at(c, p[2], row, 4));
70 7 out = c.b.build_select(c.b.build_sge(mv, c.f32(0.5f)), xv, c.f32(0.0f));
71 7 }
72 9 store_elem_dt(c, out_dt, p[1], i, out);
73 9 });
74
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
10 } else if (mode == kAuxHeadMean) {
75
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 Value* inv = emit_div_rn(c, c.f32(1.0f), kb.u32_to_f32(p[3]));
76
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 kb.for_range(gid, p[4], stride, [&](Value* i) {
77
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 Value* sum = kb.for_range_reduce(
78 1 c.i32(0), p[3], c.i32(1), c.f32(0.0f),
79 2 [&](Value* h, Value* acc) -> Value* {
80 1 Value* v = kb.load_f32(ptr_at(c, p[0], c.b.build_add(c.b.build_mul(h, p[4]), i), 4));
81 1 return c.b.build_add(acc, v);
82 });
83 1 store_elem_dt(c, out_dt, p[1], i, c.b.build_mul(sum, inv));
84 1 });
85 1 } else {
86
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ throw std::runtime_error("brotensor: mir_attn_aux: unknown mode");
87 }
88
89
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 c.b.build_ret_void();
90
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 return finalize_fn(fn);
91 10 }
92
93 5 brass::Function* build_attn_bwd_mir(brass::Module& mod, bool has_kmask, bool has_qmask) {
94
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 const std::string name = std::string("mir_attn_bwd") + (has_kmask ? "_km" : "") + (has_qmask ? "_qm" : "");
95
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.
25 std::vector<Type> params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
96 20 Type::i32(), Type::i32(), Type::i32(), Type::f32()};
97
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));
98
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 brass::codegen::KernelBuilder kb(mod, fn);
99
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Ctx c(kb);
100
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 auto p = entry_params(c, fn);
101 // p[0]=p, p[1]=dp, p[2]=kmask, p[3]=qmask, p[4]=rows, p[5]=lq, p[6]=lk, p[7]=scale
102
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* s8 = kb.shared_alloc_f32(8);
103
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* tid = kb.tid_x();
104
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* wg = c.i32(static_cast<int>(jit::gpu_mir::kEwBlock));
105
106
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.for_range(kb.ctaid_x(), p[4], kb.nctaid_x(), [&](Value* row) {
107 5 Value* base = c.b.build_mul(row, p[6]);
108 10 auto reduce_row = [&]() -> Value* {
109
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
10 return kb.for_range_reduce(
110 5 tid, p[6], wg, c.f32(0.0f),
111 10 [&](Value* j, Value* cur) -> Value* {
112 5 Value* idx = c.b.build_add(base, j);
113 10 return c.b.build_fma_f32(kb.load_f32(ptr_at(c, p[1], idx, 4)),
114 5 kb.load_f32(ptr_at(c, p[0], idx, 4)), cur);
115 });
116 ✗ };
117 10 auto write_row = [&](Value* dot) {
118
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 kb.for_range(tid, p[6], wg, [&](Value* j) {
119 5 Value* idx = c.b.build_add(base, j);
120 5 Value* pv = kb.load_f32(ptr_at(c, p[0], idx, 4));
121 5 Value* dpv = kb.load_f32(ptr_at(c, p[1], idx, 4));
122 5 Value* ds = c.b.build_mul(c.b.build_mul(pv, c.b.build_sub(dpv, dot)), p[7]);
123
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 if (has_kmask) {
124 4 Value* masked = c.b.build_slt(kb.load_f32(ptr_at(c, p[2], j, 4)), c.f32(0.5f));
125 4 ds = c.b.build_select(masked, c.f32(0.0f), ds);
126 4 }
127 5 kb.store_f32(ptr_at(c, p[1], idx, 4), ds);
128 5 });
129 5 };
130
131
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 if (!has_qmask) {
132 2 Value* dot = emit_block_sum_f32(c, s8, reduce_row());
133 2 write_row(dot);
134 2 return;
135 }
136 3 Value* gated = c.b.build_slt(kb.load_f32(ptr_at(c, p[3], c.b.build_umod(row, p[5]), 4)), c.f32(0.5f));
137
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* acc = emit_if_else_values(
138 3 c, gated, {Type::f32()},
139 6 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; },
140 6 [&]() -> std::vector<Value*> { return {reduce_row()}; })[0];
141 3 Value* dot = emit_block_sum_f32(c, s8, acc);
142
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 emit_if_else_values(
143 3 c, gated, {},
144 6 [&]() -> std::vector<Value*> {
145
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 kb.for_range(tid, p[6], wg, [&](Value* j) {
146 3 kb.store_f32(ptr_at(c, p[1], c.b.build_add(base, j), 4), c.f32(0.0f));
147 3 });
148 3 return {};
149 ✗ },
150 6 [&]() -> std::vector<Value*> {
151 3 write_row(dot);
152 3 return {};
153 });
154 5 });
155
156
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 c.b.build_ret_void();
157
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 return finalize_fn(fn);
158 5 }
159
160 7 brass::Function* build_fa_combine_mir(brass::Module& mod, Dtype out_dt) {
161
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
14 const std::string name = std::string("mir_fa_combine_") +
162
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
7 (out_dt == Dtype::FP32 ? "f32" : out_dt == Dtype::FP16 ? "f16" : "bf16");
163
4/8
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✗ Branch 7 not taken.
28 std::vector<Type> params = {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(),
164 21 Type::i32(), Type::i32(), Type::i32()};
165
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params));
166
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 brass::codegen::KernelBuilder kb(mod, fn);
167
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 Ctx c(kb);
168
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 auto p = entry_params(c, fn);
169 // p[0]=part, p[1]=o, p[2]=entries, p[3]=nsplit, p[4]=hd, p[5]=hq, p[6]=ldo
170
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 Value* w = c.b.build_add(p[4], c.i32(2));
171
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 Value* tid = kb.tid_x();
172
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 Value* wg = c.i32(static_cast<int>(jit::gpu_mir::kEwBlock));
173
174
4/8
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✗ Branch 7 not taken.
14 kb.for_range(kb.ctaid_x(), p[2], kb.nctaid_x(), [&](Value* e) {
175 7 Value* base = c.b.build_mul(c.b.build_mul(e, p[3]), w);
176
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
14 Value* M = kb.for_range_reduce(
177 7 c.i32(0), p[3], c.i32(1), c.f32(-1e30f),
178 14 [&](Value* s, Value* cur) -> Value* {
179 7 Value* ms = kb.load_f32(ptr_at(c, p[0], c.b.build_add(c.b.build_add(base, c.b.build_mul(s, w)), p[4]), 4));
180 7 return c.b.build_fmax_f32(cur, ms);
181 });
182
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
14 Value* L = kb.for_range_reduce(
183 7 c.i32(0), p[3], c.i32(1), c.f32(0.0f),
184 14 [&](Value* s, Value* cur) -> Value* {
185 7 Value* off = c.b.build_add(base, c.b.build_mul(s, w));
186 7 Value* ls = kb.load_f32(ptr_at(c, p[0], c.b.build_add(c.b.build_add(off, p[4]), c.i32(1)), 4));
187 7 Value* ms = kb.load_f32(ptr_at(c, p[0], c.b.build_add(off, p[4]), 4));
188 7 Value* term = c.b.build_mul(ls, emit_exp(c, c.b.build_sub(ms, M)));
189 7 return c.b.build_add(cur, c.b.build_select(c.b.build_sgt(ls, c.f32(0.0f)), term, c.f32(0.0f)));
190 });
191 7 Value* inv = c.b.build_select(c.b.build_sgt(L, c.f32(0.0f)), emit_div_rn(c, c.f32(1.0f), L), c.f32(0.0f));
192 7 Value* row = c.b.build_udiv(e, p[5]);
193 7 Value* h = c.b.build_umod(e, p[5]);
194 7 Value* dst_row = c.b.build_add(c.b.build_mul(row, p[6]), c.b.build_mul(h, p[4]));
195
196
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
14 kb.for_range(tid, p[4], wg, [&](Value* d) {
197
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
14 Value* acc = kb.for_range_reduce(
198 7 c.i32(0), p[3], c.i32(1), c.f32(0.0f),
199 14 [&](Value* s, Value* cur) -> Value* {
200 7 Value* off = c.b.build_add(base, c.b.build_mul(s, w));
201 7 Value* ls = kb.load_f32(ptr_at(c, p[0], c.b.build_add(c.b.build_add(off, p[4]), c.i32(1)), 4));
202 7 Value* ok = c.b.build_sgt(ls, c.f32(0.0f));
203
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 Value* inc = emit_if_else_values(
204 7 c, ok, {Type::f32()},
205 14 [&]() -> std::vector<Value*> {
206 7 Value* vs = kb.load_f32(ptr_at(c, p[0], c.b.build_add(off, d), 4));
207 7 Value* ms = kb.load_f32(ptr_at(c, p[0], c.b.build_add(off, p[4]), 4));
208 7 return {c.b.build_mul(vs, emit_exp(c, c.b.build_sub(ms, M)))};
209 },
210 14 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
211 7 return c.b.build_add(cur, inc);
212 ✗ });
213 7 store_elem_dt(c, out_dt, p[1], c.b.build_add(dst_row, d), c.b.build_mul(acc, inv));
214 7 });
215 7 });
216
217
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 c.b.build_ret_void();
218
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 return finalize_fn(fn);
219 7 }
220
221 21 brass::Function* build_attn_softmax_mir(brass::Module& mod, Dtype out_dt, std::uint32_t bias_mode,
222 bool has_mask, bool has_qmask, bool mask_ge) {
223
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
63 const std::string name = std::string("mir_attn_softmax_") +
224
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
21 (out_dt == Dtype::FP32 ? "f32" : out_dt == Dtype::FP16 ? "f16" : "bf16") +
225
3/6
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21 times.
✗ Branch 5 not taken.
21 "_b" + std::to_string(bias_mode) +
226
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
21 (has_mask ? "_km" : "") + (has_qmask ? "_qm" : "") + (mask_ge ? "_ge" : "");
227
14/28
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 21 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 21 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 21 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 21 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 21 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 21 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 21 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 21 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 21 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 21 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 21 times.
✗ Branch 27 not taken.
294 std::vector<Type> params = {
228 105 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
229 105 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
230 84 Type::i32(), Type::i32(), Type::i32(), Type::f32(),
231 };
232
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params));
233
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 brass::codegen::KernelBuilder kb(mod, fn);
234
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 Ctx c(kb);
235
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 auto p = entry_params(c, fn);
236 // p[0..4] = s, p, bias, mask, qmask
237 // p[5..12] = rows, lk, lq, h0, lds, ldp, q0, bhs; p[13] = scale
238
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 Value* s8 = kb.shared_alloc_f32(8);
239
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 Value* tid = kb.tid_x();
240
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 Value* wg = c.i32(static_cast<int>(jit::gpu_mir::kEwBlock));
241
242
4/8
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 21 times.
✗ Branch 7 not taken.
42 kb.for_range(kb.ctaid_x(), p[5], kb.nctaid_x(), [&](Value* r) {
243 21 Value* q = c.b.build_add(p[11], c.b.build_umod(r, p[7]));
244 21 Value* h = c.b.build_add(p[8], c.b.build_udiv(r, p[7]));
245 21 Value* so = c.b.build_mul(r, p[9]);
246 21 Value* po = c.b.build_mul(r, p[10]);
247 21 Value* bo = c.b.build_add(c.b.build_mul(h, p[12]), c.b.build_mul(q, p[6]));
248
249 42 auto reduce_max = [&]() -> Value* {
250
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
42 return kb.for_range_reduce(
251 21 tid, p[6], wg, c.f32(-1e30f),
252 42 [&](Value* k, Value* cur) -> Value* {
253
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 10 times.
21 if (!has_mask) {
254 11 Value* sc = eval_softmax_score(c, bias_mode, p[0], p[2], p[3], so, bo, h, q, k, p[12], p[13]);
255 11 return c.b.build_fmax_f32(cur, sc);
256 }
257
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 return emit_if_else_values(
258 10 c, valid_mask(c, p[3], k, mask_ge), {Type::f32()},
259 20 [&]() -> std::vector<Value*> {
260 10 Value* sc = eval_softmax_score(c, bias_mode, p[0], p[2], p[3], so, bo, h, q, k, p[12], p[13]);
261 10 return {c.b.build_fmax_f32(cur, sc)};
262 },
263 20 [&]() -> std::vector<Value*> { return {cur}; })[0];
264 21 });
265 ✗ };
266
267
6/8
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
30 Value* mx_local = !has_qmask
268 12 ? reduce_max()
269
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 : emit_if_else_values(
270 9 c, valid_mask(c, p[4], q, mask_ge), {Type::f32()},
271 18 [&]() -> std::vector<Value*> { return {reduce_max()}; },
272 18 [&]() -> std::vector<Value*> { return {c.f32(-1e30f)}; })[0];
273
274 21 Value* mx = emit_block_max_f32(c, s8, mx_local);
275 21 Value* empty = c.b.build_sle(mx, c.f32(-1e29f));
276
277
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 Value* sum_local = emit_if_else_values(
278 21 c, empty, {Type::f32()},
279 42 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; },
280 42 [&]() -> std::vector<Value*> {
281
3/6
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21 times.
✗ Branch 5 not taken.
42 return {kb.for_range_reduce(
282 21 tid, p[6], wg, c.f32(0.0f),
283 42 [&](Value* k, Value* cur) -> Value* {
284
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 10 times.
21 if (!has_mask) {
285 11 Value* sc = eval_softmax_score(c, bias_mode, p[0], p[2], p[3], so, bo, h, q, k, p[12], p[13]);
286 11 return c.b.build_add(cur, emit_exp(c, c.b.build_sub(sc, mx)));
287 }
288
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 return emit_if_else_values(
289 10 c, valid_mask(c, p[3], k, mask_ge), {Type::f32()},
290 20 [&]() -> std::vector<Value*> {
291 10 Value* sc = eval_softmax_score(c, bias_mode, p[0], p[2], p[3], so, bo, h, q, k, p[12], p[13]);
292 10 return {c.b.build_add(cur, emit_exp(c, c.b.build_sub(sc, mx)))};
293 },
294 20 [&]() -> std::vector<Value*> { return {cur}; })[0];
295 21 })};
296 ✗ })[0];
297
298 21 Value* sum = emit_block_sum_f32(c, s8, sum_local);
299 42 Value* inv = c.b.build_select(
300 21 c.b.build_and(c.b.build_not(empty), c.b.build_sgt(sum, c.f32(0.0f))),
301 21 emit_div_rn(c, c.f32(1.0f), sum),
302 21 c.f32(0.0f));
303
304
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 emit_if_else_values(
305 21 c, empty, {},
306 42 [&]() -> std::vector<Value*> {
307
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
42 kb.for_range(tid, p[6], wg, [&](Value* k) {
308 21 store_elem_dt(c, out_dt, p[1], c.b.build_add(po, k), c.f32(0.0f));
309 21 });
310 21 return {};
311 ✗ },
312 42 [&]() -> std::vector<Value*> {
313
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
42 kb.for_range(tid, p[6], wg, [&](Value* k) {
314 Value* pv;
315
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 10 times.
21 if (!has_mask) {
316 11 Value* sc = eval_softmax_score(c, bias_mode, p[0], p[2], p[3], so, bo, h, q, k, p[12], p[13]);
317 11 pv = c.b.build_mul(emit_exp(c, c.b.build_sub(sc, mx)), inv);
318 11 } else {
319
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 pv = emit_if_else_values(
320 10 c, valid_mask(c, p[3], k, mask_ge), {Type::f32()},
321 20 [&]() -> std::vector<Value*> {
322 10 Value* sc = eval_softmax_score(c, bias_mode, p[0], p[2], p[3], so, bo, h, q, k, p[12], p[13]);
323 10 return {c.b.build_mul(emit_exp(c, c.b.build_sub(sc, mx)), inv)};
324 },
325 20 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
326 }
327 21 store_elem_dt(c, out_dt, p[1], c.b.build_add(po, k), pv);
328 21 });
329 21 return {};
330 ✗ });
331 21 });
332
333
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 c.b.build_ret_void();
334
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 return finalize_fn(fn);
335 21 }
336
337 } // namespace brotensor::kernels
338