brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
391 / 498
78.5%
Branches:
301 / 680
44.3%
Functions:
30 / 38
78.9%

src/kernels/mir_gemm_quant_mma.cpp
Line Branch Exec Source
1 #include "mir_gemm_quant_mma.h"
2
3 #include "mir_common.h"
4 #include "mir_quant_helpers.h"
5 #include "../vulkan/shaders/op_codes.h"
6
7 #include <algorithm>
8 #include <array>
9 #include <cstdint>
10 #include <stdexcept>
11 #include <utility>
12 #include <vector>
13
14 namespace brotensor::kernels {
15
16 namespace {
17
18 constexpr std::uint32_t kQuantBk = 32u;
19 constexpr std::uint32_t kQuantStages = 2u;
20 constexpr std::uint32_t kL2PanelN = 16u;
21
22 12 Value* swz_bytes(Ctx& c, Value* row, Value* chunk) {
23 12 Value* row_off = c.b.build_shl(row, c.i32(6));
24 12 Value* swz = c.b.build_and(c.b.build_ashr(row, c.i32(1)), c.i32(3));
25 12 Value* ch_off = c.b.build_shl(c.b.build_xor(chunk, swz), c.i32(4));
26 12 return c.b.build_or(row_off, ch_off);
27 }
28
29 4 Value* swz_chunk(Ctx& c, Value* row, Value* chunk) {
30 4 Value* row_off = c.b.build_shl(row, c.i32(2));
31 4 Value* swz = c.b.build_and(c.b.build_ashr(row, c.i32(1)), c.i32(3));
32 4 return c.b.build_or(row_off, c.b.build_xor(chunk, swz));
33 }
34
35 36 Value* ptr_2d_i64(Ctx& c, Value* base, Value* row, Value* ld, Value* col, int esz_shift) {
36 72 Value* elem = c.b.build_add(
37 36 c.b.build_mul(c.b.build_zext_i64(row), c.b.build_zext_i64(ld)),
38 36 c.b.build_zext_i64(col));
39 36 return c.b.build_add(base, c.b.build_shl(elem, c.kb.const_i64(esz_shift)));
40 }
41
42 32 Value* pack_pair_i32(Ctx& c, Dtype dt, Value* v0, Value* v1) {
43
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 if (dt == Dtype::FP16) return c.kb.pack_f16x2(v0, v1);
44 16 Value* b0 = c.b.build_and(c.kb.f32_to_bf16(v0), c.i32(0xffff));
45 16 Value* b1 = c.b.build_shl(c.kb.f32_to_bf16(v1), c.i32(16));
46 16 return c.b.build_or(b0, b1);
47 32 }
48
49 ✗ std::pair<Value*, Value*> unpack_pair_f32(Ctx& c, Dtype dt, Value* packed) {
50 ✗ Value* lo = c.b.build_and(packed, c.i32(0xffff));
51 ✗ Value* hi = c.b.build_lshr(packed, c.i32(16));
52
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (dt == Dtype::FP16) return {c.kb.f16_to_f32(lo), c.kb.f16_to_f32(hi)};
53 ✗ return {c.kb.bf16_to_f32(lo), c.kb.bf16_to_f32(hi)};
54 ✗ }
55
56 // Assemble 32-bit word from two 2-byte aligned u16 loads (safe for Q8_0 34B & Q6_K 210B blocks).
57 ✗ Value* load_u32_align2(Ctx& c, Value* ptr) {
58 ✗ Value* lo = c.kb.load_u16(ptr);
59 ✗ Value* hi = c.kb.load_u16(c.b.build_add(ptr, c.kb.const_i64(2)));
60 ✗ return c.b.build_or(lo, c.b.build_shl(hi, c.i32(16)));
61 }
62
63 8 Value* make_i32x4(Ctx& c, Value* w0, Value* w1, Value* w2, Value* w3) {
64 8 Value* v = c.b.build_vzero(Type::i32x4());
65 8 v = c.b.build_vinsert_lane(v, w0, 0);
66 8 v = c.b.build_vinsert_lane(v, w1, 1);
67 8 v = c.b.build_vinsert_lane(v, w2, 2);
68 8 return c.b.build_vinsert_lane(v, w3, 3);
69 }
70
71 // Split B load and dequantize so global loads can overlap with Tensor Core MMA.
72 16 struct RawBChunk {
73 16 Value* w0 = nullptr; // INT8: i64 q64; Q8_0: d_u16; Q4_K: hdr i32x4; Q6_K: qlo i32
74 16 Value* w1 = nullptr; // Q8_0: qlo i32; Q4_K: nib64 i64; Q6_K: qhi i32
75 16 Value* w2 = nullptr; // Q8_0: qhi i32; Q6_K: hlo i32
76 16 Value* w3 = nullptr; // Q6_K: hhi i32
77 16 Value* w4 = nullptr; // Q6_K: bx_u16
78 16 Value* w5 = nullptr; // Q6_K: d_u16
79 16 Value* in_range = nullptr;
80 };
81
82 8 RawBChunk fetch_b_raw(Ctx& c, const GemmQuantMmaSpec& spec, Value* row_ptr,
83 Value* kt_idx, Value* ch, Value* in_range) {
84 8 RawBChunk r;
85 8 r.in_range = in_range;
86 16 auto do_fetch = [&](std::vector<Type> types, auto&& fn) -> std::vector<Value*> {
87
2/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
8 if (!in_range) return fn();
88 8 std::vector<Value*> zeros;
89
2/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
8 zeros.reserve(types.size());
90
4/8
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
20 for (Type t : types) {
91
7/24
✓ 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 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 4 times.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 4 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
12 if (t == Type::i64()) zeros.push_back(c.kb.const_i64(0));
92
3/24
✗ 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.
✓ Branch 12 taken 4 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 4 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
4 else if (t == Type::i32x4()) zeros.push_back(c.b.build_vzero(Type::i32x4()));
93
0/16
✗ 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.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ else zeros.push_back(c.i32(0));
94 }
95
2/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
16 return emit_if_else_values(c, in_range, types, fn, [&] { return zeros; });
96 8 };
97
98
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (spec.fmt == QF_INT8) {
99
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 auto v = do_fetch({Type::i64()}, [&]() -> std::vector<Value*> {
100 4 Value* gk = c.b.build_add(c.b.build_shl(kt_idx, c.i32(5)), c.b.build_shl(ch, c.i32(3)));
101 4 return {c.kb.load_i64(c.b.build_add(row_ptr, c.b.build_zext_i64(gk)))};
102 });
103 4 r.w0 = v[0];
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
8 } else if (spec.fmt == QF_Q8_0) {
105
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ auto v = do_fetch({Type::i32(), Type::i32(), Type::i32()}, [&]() -> std::vector<Value*> {
106 ✗ Value* blk = c.b.build_add(row_ptr, c.b.build_mul(c.b.build_zext_i64(kt_idx), c.kb.const_i64(34)));
107 ✗ Value* d_u16 = c.kb.load_u16(blk);
108 ✗ Value* qp = c.b.build_add(blk, c.b.build_zext_i64(c.b.build_add(c.i32(2), c.b.build_shl(ch, c.i32(3)))));
109 ✗ return {d_u16, load_u32_align2(c, qp), load_u32_align2(c, c.b.build_add(qp, c.kb.const_i64(4)))};
110 });
111 ✗ r.w0 = v[0];
112 ✗ r.w1 = v[1];
113 ✗ r.w2 = v[2];
114
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 } else if (spec.fmt == QF_Q4K) {
115
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 auto v = do_fetch({Type::i32x4(), Type::i64()}, [&]() -> std::vector<Value*> {
116 8 Value* blk = c.b.build_add(
117 4 row_ptr, c.b.build_mul(c.b.build_zext_i64(c.b.build_lshr(kt_idx, c.i32(3))), c.kb.const_i64(144)));
118 4 Value* hdr = c.kb.vload(Type::i32x4(), blk);
119 4 Value* j = c.b.build_and(kt_idx, c.i32(7));
120 8 Value* b_off = c.b.build_add(
121 8 c.i32(16), c.b.build_add(c.b.build_shl(c.b.build_lshr(j, c.i32(1)), c.i32(5)),
122 4 c.b.build_shl(ch, c.i32(3))));
123 4 Value* nib64 = c.kb.load_i64(c.b.build_add(blk, c.b.build_zext_i64(b_off)));
124 4 return {hdr, nib64};
125 });
126 4 r.w0 = v[0];
127 4 r.w1 = v[1];
128
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
4 } else if (spec.fmt == QF_Q6K) {
129
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ auto v = do_fetch(std::vector<Type>(6, Type::i32()), [&]() -> std::vector<Value*> {
130 ✗ Value* blk = c.b.build_add(
131 ✗ row_ptr, c.b.build_mul(c.b.build_zext_i64(c.b.build_lshr(kt_idx, c.i32(3))), c.kb.const_i64(210)));
132 ✗ Value* e0 = c.b.build_add(c.b.build_shl(c.b.build_and(kt_idx, c.i32(7)), c.i32(5)),
133 ✗ c.b.build_shl(ch, c.i32(3)));
134 ✗ Value* g = c.b.build_lshr(e0, c.i32(7));
135 ✗ Value* quad = c.b.build_lshr(c.b.build_and(e0, c.i32(127)), c.i32(5));
136 ✗ Value* l0 = c.b.build_and(e0, c.i32(31));
137 ✗ Value* ql_off = c.b.build_add(
138 ✗ c.b.build_add(c.b.build_shl(g, c.i32(6)), c.b.build_shl(c.b.build_and(quad, c.i32(1)), c.i32(5))),
139 ✗ l0);
140 ✗ Value* qh_off = c.b.build_add(c.i32(128), c.b.build_add(c.b.build_shl(g, c.i32(5)), l0));
141 ✗ Value* sc_off = c.b.build_add(c.i32(192), c.b.build_add(c.b.build_shl(g, c.i32(3)),
142 ✗ c.b.build_shl(quad, c.i32(1))));
143 ✗ Value* qlp = c.b.build_add(blk, c.b.build_zext_i64(ql_off));
144 ✗ Value* qhp = c.b.build_add(blk, c.b.build_zext_i64(qh_off));
145 ✗ return {
146 ✗ load_u32_align2(c, qlp),
147 ✗ load_u32_align2(c, c.b.build_add(qlp, c.kb.const_i64(4))),
148 ✗ load_u32_align2(c, qhp),
149 ✗ load_u32_align2(c, c.b.build_add(qhp, c.kb.const_i64(4))),
150 ✗ c.kb.load_u16(c.b.build_add(blk, c.b.build_zext_i64(sc_off))),
151 ✗ c.kb.load_u16(c.b.build_add(blk, c.kb.const_i64(208))),
152 };
153 });
154 ✗ r.w0 = v[0]; r.w1 = v[1]; r.w2 = v[2]; r.w3 = v[3]; r.w4 = v[4]; r.w5 = v[5];
155 ✗ } else {
156
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_gemm_quant_mma_mir: unsupported quant fmt");
157 }
158 8 return r;
159 ✗ }
160
161 8 Value* decode_raw_b(Ctx& c, const GemmQuantMmaSpec& spec, const RawBChunk& r,
162 Value* row_scale, Value* kt_idx, Value* ch) {
163 8 std::array<Value*, 8> w{};
164
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (spec.fmt == QF_INT8) {
165 4 Value* qlo = c.b.build_trunc_i32(r.w0);
166 4 Value* qhi = c.b.build_trunc_i32(c.b.build_lshr(r.w0, c.kb.const_i64(32)));
167
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 4 times.
36 for (int e = 0; e < 8; ++e) {
168
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 Value* bw = (e < 4) ? qlo : qhi;
169 32 Value* b = c.b.build_and(c.b.build_lshr(bw, c.i32((e & 3) * 8)), c.i32(0xff));
170 32 w[static_cast<std::size_t>(e)] = c.b.build_mul(emit_s8_byte_to_f32(c, b), row_scale);
171 32 }
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
8 } else if (spec.fmt == QF_Q8_0) {
173 ✗ Value* d = c.kb.f16_to_f32(r.w0);
174
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int e = 0; e < 8; ++e) {
175
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* bw = (e < 4) ? r.w1 : r.w2;
176 ✗ Value* b = c.b.build_and(c.b.build_lshr(bw, c.i32((e & 3) * 8)), c.i32(0xff));
177 ✗ w[static_cast<std::size_t>(e)] = c.b.build_mul(emit_s8_byte_to_f32(c, b), d);
178 ✗ }
179
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 } else if (spec.fmt == QF_Q4K) {
180 4 Value* a0 = c.b.build_vextract_lane(r.w0, 0);
181 4 Value* s0 = c.b.build_vextract_lane(r.w0, 1);
182 4 Value* s1 = c.b.build_vextract_lane(r.w0, 2);
183 4 Value* s2 = c.b.build_vextract_lane(r.w0, 3);
184 4 Value* j = c.b.build_and(kt_idx, c.i32(7));
185 4 Value* blo = c.b.build_trunc_i32(r.w1);
186 4 Value* bhi = c.b.build_trunc_i32(c.b.build_lshr(r.w1, c.kb.const_i64(32)));
187 4 Value* low_j = c.b.build_ult(j, c.i32(4));
188 4 Value* j4 = c.b.build_add(j, c.i32(4));
189 4 Value* jm4 = c.b.build_sub(j, c.i32(4));
190 4 Value* bs_j = emit_extract_byte(c, emit_sel_word3(c, c.b.build_lshr(j, c.i32(2)), s0, s1, s2), j);
191 4 Value* bs_j4 = emit_extract_byte(c, emit_sel_word3(c, c.b.build_lshr(j4, c.i32(2)), s0, s1, s2), j4);
192 4 Value* bs_jm4 = emit_extract_byte(c, emit_sel_word3(c, c.b.build_lshr(jm4, c.i32(2)), s0, s1, s2), jm4);
193 8 Value* sc = c.b.build_select(
194 4 low_j, c.b.build_and(bs_j, c.i32(63)),
195 8 c.b.build_or(c.b.build_and(bs_j4, c.i32(15)),
196 4 c.b.build_shl(c.b.build_lshr(bs_jm4, c.i32(6)), c.i32(4))));
197 8 Value* mn = c.b.build_select(
198 4 low_j, c.b.build_and(bs_j4, c.i32(63)),
199 8 c.b.build_or(c.b.build_lshr(bs_j4, c.i32(4)),
200 4 c.b.build_shl(c.b.build_lshr(bs_j, c.i32(6)), c.i32(4))));
201 4 Value* d = c.kb.f16_to_f32(c.b.build_and(a0, c.i32(0xffff)));
202 4 Value* dmin = c.kb.f16_to_f32(c.b.build_lshr(a0, c.i32(16)));
203 4 Value* ws = c.b.build_mul(c.kb.u32_to_f32(sc), d);
204 4 Value* wm = c.b.build_mul(c.kb.u32_to_f32(mn), dmin);
205 4 Value* sh = c.b.build_shl(c.b.build_and(j, c.i32(1)), c.i32(2));
206
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 4 times.
36 for (int e = 0; e < 8; ++e) {
207
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 Value* bw = (e < 4) ? blo : bhi;
208 32 Value* q4 = c.b.build_and(c.b.build_lshr(bw, c.b.build_add(sh, c.i32((e & 3) * 8))), c.i32(0x0f));
209 32 w[static_cast<std::size_t>(e)] =
210 32 c.b.build_fma_f32(ws, c.kb.u32_to_f32(q4), c.b.build_neg(wm));
211 32 }
212 4 } else {
213 ✗ Value* e0 = c.b.build_add(c.b.build_shl(c.b.build_and(kt_idx, c.i32(7)), c.i32(5)),
214 ✗ c.b.build_shl(ch, c.i32(3)));
215 ✗ Value* quad = c.b.build_lshr(c.b.build_and(e0, c.i32(127)), c.i32(5));
216 ✗ Value* l0 = c.b.build_and(e0, c.i32(31));
217 ✗ Value* d = c.kb.f16_to_f32(r.w5);
218 ✗ Value* sbyte = c.b.build_and(
219 ✗ c.b.build_lshr(r.w4, c.b.build_shl(c.b.build_lshr(l0, c.i32(4)), c.i32(3))),
220 ✗ c.i32(0xff));
221 ✗ Value* s = c.b.build_mul(d, emit_s8_byte_to_f32(c, sbyte));
222 ✗ Value* ql_sh = c.b.build_select(c.b.build_uge(quad, c.i32(2)), c.i32(4), c.i32(0));
223 ✗ Value* qh_sh = c.b.build_shl(quad, c.i32(1));
224
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int e = 0; e < 8; ++e) {
225
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* bw = (e < 4) ? r.w0 : r.w1;
226
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* hw = (e < 4) ? r.w2 : r.w3;
227 ✗ Value* bsh = c.i32((e & 3) * 8);
228 ✗ Value* nib = c.b.build_and(c.b.build_lshr(c.b.build_lshr(bw, bsh), ql_sh), c.i32(0x0f));
229 ✗ Value* hb = c.b.build_shl(
230 ✗ c.b.build_and(c.b.build_lshr(c.b.build_lshr(hw, bsh), qh_sh), c.i32(0x03)), c.i32(4));
231 ✗ Value* q6 = c.b.build_sub(c.kb.u32_to_f32(c.b.build_or(nib, hb)), c.f32(32.0f));
232 ✗ w[static_cast<std::size_t>(e)] = c.b.build_mul(s, q6);
233 ✗ }
234 }
235 8 Value* p0 = pack_pair_i32(c, spec.dt, w[0], w[1]);
236 8 Value* p1 = pack_pair_i32(c, spec.dt, w[2], w[3]);
237 8 Value* p2 = pack_pair_i32(c, spec.dt, w[4], w[5]);
238 8 Value* p3 = pack_pair_i32(c, spec.dt, w[6], w[7]);
239
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (!r.in_range) return make_i32x4(c, p0, p1, p2, p3);
240 8 Value* zero = c.i32(0);
241 8 return make_i32x4(
242 8 c,
243 8 c.b.build_select(r.in_range, p0, zero),
244 8 c.b.build_select(r.in_range, p1, zero),
245 8 c.b.build_select(r.in_range, p2, zero),
246 8 c.b.build_select(r.in_range, p3, zero));
247 8 }
248
249 } // namespace
250
251 2 Function* build_gemm_quant_mma_mir(Module& mod, const GemmQuantMmaSpec& spec) {
252
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 if (spec.dt != Dtype::FP16 && spec.dt != Dtype::BF16) {
253
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_gemm_quant_mma_mir: only FP16/BF16 activations supported");
254 }
255
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 if (spec.dt == Dtype::BF16 && spec.fmt != QF_INT8) {
256
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_gemm_quant_mma_mir: BF16 only supported for QF_INT8");
257 }
258 2 const GemmQuantMmaCfg& cfg = kGemmQuantMmaCfgs[std::min<std::uint8_t>(spec.cfg, 1u)];
259 2 const int bm = static_cast<int>(cfg.bm);
260 2 const int bn = static_cast<int>(cfg.bn);
261 2 const int warps_n = static_cast<int>(cfg.warps_n);
262 2 const int threads = static_cast<int>(cfg.threads);
263 2 const int wm = bm / static_cast<int>(cfg.warps_m);
264 2 const int wn = bn / warps_n;
265 2 const int mt = wm / 16;
266 2 const int nt = wn / 8;
267 2 const int a_chunks = (bm * 4) / threads;
268 2 const int b_chunks = (bn * 4) / threads;
269 2 const int num_accs = mt * nt;
270 2 const int a_stage_bytes = bm * static_cast<int>(kQuantBk) * 2;
271 2 const int sB_chunk_base = static_cast<int>(kQuantStages) * bm * 4;
272 2 const int b_stage_chunks = bn * 4;
273 2 const int b_stage_bytes = bn * static_cast<int>(kQuantBk) * 2;
274
275 20 std::vector<Type> param_types = {
276 10 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
277 10 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32()
278 };
279
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Function* fn = mod.create_function("mir_gemm_quant_mma", Type::void_type(), param_types);
280
281
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 brass::codegen::KernelBuilder kb(mod, fn);
282
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Ctx c(kb);
283
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto p = entry_params(c, fn);
284 2 Value* X = p[0];
285 2 Value* W = p[1];
286 2 Value* scales = p[2];
287 2 Value* bias = p[3];
288 2 Value* Y = p[4];
289 2 Value* B = p[5];
290 2 Value* M = p[6];
291 2 Value* K = p[7];
292 2 Value* row_bytes = p[8];
293 2 Value* grid_m = p[9];
294
295
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* smem = kb.shared_alloc_u16(kQuantStages * (cfg.bm + cfg.bn) * kQuantBk);
296
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* sA_base = c.b.build_trunc_i32(smem);
297
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* sB_base = c.b.build_add(sA_base, c.i32(static_cast<int>(kQuantStages) * a_stage_bytes));
298
299
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* tid = kb.tid_x();
300
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* lane = c.b.build_and(tid, c.i32(31));
301
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* warp = c.b.build_lshr(tid, c.i32(5));
302
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* w_m = c.b.build_udiv(warp, c.i32(warps_n));
303
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* w_n = c.b.build_umod(warp, c.i32(warps_n));
304
305
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* bx = kb.ctaid_x();
306
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* by = kb.ctaid_y();
307 2 Value* tile_m = by;
308 2 Value* tile_n = bx;
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (spec.swizzle_l2) {
310
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* gn = kb.nctaid_x();
311
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* lid = c.b.build_add(c.b.build_mul(by, gn), bx);
312
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* panel_n = c.i32(static_cast<int>(kL2PanelN));
313
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* per_panel = c.b.build_mul(panel_n, grid_m);
314
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* pan = c.b.build_udiv(lid, per_panel);
315
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* rem = c.b.build_umod(lid, per_panel);
316
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* n_start = c.b.build_mul(pan, panel_n);
317
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* pw = smin_i32(c, panel_n, c.b.build_sub(gn, n_start));
318
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ tile_m = c.b.build_udiv(rem, pw);
319
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ tile_n = c.b.build_add(n_start, c.b.build_umod(rem, pw));
320 ✗ }
321
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* m0 = c.b.build_mul(tile_m, c.i32(bm));
322
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* n0 = c.b.build_mul(tile_n, c.i32(bn));
323
324
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
2 Value* KT = c.b.build_lshr(c.b.build_add(K, c.i32(31)), c.i32(5));
325
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
2 Value* lane_ch_k = c.b.build_shl(c.b.build_and(tid, c.i32(3)), c.i32(3));
326
327
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* a_row_mask = spec.aligned_mnk ? nullptr : c.i32(0);
328
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Value*> a_s_off(static_cast<std::size_t>(a_chunks));
329
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Value*> a_g_base(static_cast<std::size_t>(a_chunks));
330
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 for (int i = 0; i < a_chunks; ++i) {
331 4 const auto iu = static_cast<std::size_t>(i);
332
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
4 Value* lin = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * threads));
333
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Value* row = c.b.build_lshr(lin, c.i32(2));
334
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Value* ch = c.b.build_and(lin, c.i32(3));
335
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 a_s_off[iu] = c.b.build_add(sA_base, swz_bytes(c, row, ch));
336
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* gr = c.b.build_add(m0, row);
337
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 a_g_base[iu] = ptr_2d_i64(c, X, gr, K, c.b.build_shl(ch, c.i32(3)), 1);
338
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!spec.aligned_mnk) {
339
4/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 Value* bit = c.b.build_select(c.b.build_slt(gr, B), c.i32(1 << i), c.i32(0));
340
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 a_row_mask = c.b.build_or(a_row_mask, bit);
341 4 }
342 4 }
343
344
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Value*> b_chunk_off(static_cast<std::size_t>(b_chunks));
345
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Value*> b_row_ptr(static_cast<std::size_t>(b_chunks));
346
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Value*> b_row_ok(static_cast<std::size_t>(b_chunks));
347
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Value*> b_ch(static_cast<std::size_t>(b_chunks));
348
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Value*> b_scale(static_cast<std::size_t>(b_chunks), nullptr);
349
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 for (int i = 0; i < b_chunks; ++i) {
350 4 const auto iu = static_cast<std::size_t>(i);
351
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
4 Value* lin = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * threads));
352
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Value* row = c.b.build_lshr(lin, c.i32(2));
353
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Value* ch = c.b.build_and(lin, c.i32(3));
354 4 b_ch[iu] = ch;
355
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 b_chunk_off[iu] = c.b.build_add(c.i32(sB_chunk_base), swz_chunk(c, row, ch));
356
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* grow = c.b.build_add(n0, row);
357
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Value* ok = spec.aligned_mnk ? nullptr : c.b.build_slt(grow, M);
358 4 b_row_ok[iu] = ok;
359
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 Value* safe_row = spec.aligned_mnk ? grow : c.b.build_select(ok, grow, c.i32(0));
360
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 b_row_ptr[iu] = c.b.build_add(
361
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 W, c.b.build_mul(c.b.build_zext_i64(safe_row), c.b.build_zext_i64(row_bytes)));
362
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (spec.fmt == QF_INT8) {
363
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 b_scale[iu] = kb.load_f32(ptr_at(c, scales, safe_row, 4));
364 2 }
365 4 }
366
367
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* a_lane_row = c.b.build_and(lane, c.i32(15));
368
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* a_lane_ch = c.b.build_lshr(lane, c.i32(4));
369
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* wm_base = c.b.build_mul(w_m, c.i32(wm));
370
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Value*> a_ld_off(static_cast<std::size_t>(mt));
371
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 for (int i = 0; i < mt; ++i) {
372
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 Value* row = c.b.build_add(c.b.build_add(wm_base, c.i32(i * 16)), a_lane_row);
373
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 a_ld_off[static_cast<std::size_t>(i)] = c.b.build_add(sA_base, swz_bytes(c, row, a_lane_ch));
374 4 }
375
376
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* mi = c.b.build_lshr(lane, c.i32(3));
377
5/10
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
4 Value* b_lane_row = c.b.build_add(c.b.build_shl(c.b.build_lshr(mi, c.i32(1)), c.i32(3)),
378
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 c.b.build_and(lane, c.i32(7)));
379
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* b_lane_ch = c.b.build_and(mi, c.i32(1));
380
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* wn_base = c.b.build_mul(w_n, c.i32(wn));
381
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Value*> b_ld_off(static_cast<std::size_t>(nt / 2));
382
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 for (int j = 0; j < nt / 2; ++j) {
383
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 Value* row = c.b.build_add(c.b.build_add(wn_base, c.i32(j * 16)), b_lane_row);
384
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 b_ld_off[static_cast<std::size_t>(j)] = c.b.build_add(sB_base, swz_bytes(c, row, b_lane_ch));
385 4 }
386
387 6 auto emit_load_A = [&](Value* st_idx, Value* kt_idx) {
388 4 Value* a_st_off = c.b.build_mul(st_idx, c.i32(a_stage_bytes));
389 4 Value* kt_bytes = c.b.build_shl(c.b.build_zext_i64(kt_idx), kb.const_i64(6));
390 4 Value* k0 = c.b.build_shl(kt_idx, c.i32(5));
391 4 Value* gk = c.b.build_add(k0, lane_ch_k);
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 Value* k_ok_mask = spec.aligned_mnk
393 ? nullptr
394 4 : c.b.build_select(c.b.build_slt(gk, K), a_row_mask, c.i32(0));
395
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (int i = 0; i < a_chunks; ++i) {
396 8 const auto iu = static_cast<std::size_t>(i);
397 8 Value* dst = c.b.build_add(a_s_off[iu], a_st_off);
398 8 Value* src = c.b.build_add(a_g_base[iu], kt_bytes);
399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (spec.aligned_mnk) {
400 ✗ c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, src});
401 ✗ } else {
402 8 Value* ok = c.b.build_ne(c.b.build_and(k_ok_mask, c.i32(1 << i)), c.i32(0));
403 8 Value* safe_src = c.b.build_select(ok, src, X);
404 8 Value* sz = c.b.build_select(ok, c.i32(16), c.i32(0));
405 8 c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, safe_src, sz});
406 }
407 8 }
408 4 };
409
410 6 auto emit_fetch_B = [&](Value* kt_idx) -> std::vector<RawBChunk> {
411 4 Value* k0 = c.b.build_shl(kt_idx, c.i32(5));
412
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
4 Value* gk_ok = (spec.aligned_mnk || spec.fmt != QF_INT8)
413 ? nullptr
414 2 : c.b.build_slt(c.b.build_add(k0, lane_ch_k), K);
415 4 std::vector<RawBChunk> raw(static_cast<std::size_t>(b_chunks));
416
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (int i = 0; i < b_chunks; ++i) {
417 8 const auto iu = static_cast<std::size_t>(i);
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 Value* in_range = spec.aligned_mnk ? nullptr : b_row_ok[iu];
419
4/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
8 if (in_range && gk_ok) in_range = c.b.build_and(in_range, gk_ok);
420
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 raw[iu] = fetch_b_raw(c, spec, b_row_ptr[iu], kt_idx, b_ch[iu], in_range);
421 8 }
422 4 return raw;
423
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 };
424
425 6 auto emit_store_B = [&](Value* st_idx, Value* kt_idx, const std::vector<RawBChunk>& raw) {
426 4 Value* b_ch_off = c.b.build_mul(st_idx, c.i32(b_stage_chunks));
427
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (int i = 0; i < b_chunks; ++i) {
428 8 const auto iu = static_cast<std::size_t>(i);
429 8 Value* v4 = decode_raw_b(c, spec, raw[iu], b_scale[iu], kt_idx, b_ch[iu]);
430 8 kb.shared_store_i32x4_indexed(smem, c.b.build_add(b_chunk_off[iu], b_ch_off), v4);
431 8 }
432 4 };
433
434 2 const char* mma_sym = (spec.dt == Dtype::FP16) ? "ptx_mma_m16n8k16_f16_f32"
435 : "ptx_mma_m16n8k16_bf16_f32";
436 6 auto emit_mma = [&](Value* st_idx, std::vector<Value*>& accs) {
437 4 Value* a_st_off = c.b.build_mul(st_idx, c.i32(a_stage_bytes));
438 4 Value* b_st_off = c.b.build_mul(st_idx, c.i32(b_stage_bytes));
439
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (int kk = 0; kk < 2; ++kk) {
440 24 auto a_addr = [&](int i) -> Value* {
441 16 Value* p0 = c.b.build_add(a_ld_off[static_cast<std::size_t>(i)], a_st_off);
442
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 return (kk == 0) ? p0 : c.b.build_xor(p0, c.i32(32));
443 };
444 24 auto b_addr = [&](int j) -> Value* {
445 16 Value* p0 = c.b.build_add(b_ld_off[static_cast<std::size_t>(j)], b_st_off);
446
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 return (kk == 0) ? p0 : c.b.build_xor(p0, c.i32(32));
447 };
448 8 std::vector<Value*> b0(static_cast<std::size_t>(nt));
449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 std::vector<Value*> b1(static_cast<std::size_t>(nt));
450
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 16 times.
24 for (int j = 0; j < nt / 2; ++j) {
451
3/6
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
16 Value* r = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {b_addr(j)});
452 16 const auto j2 = static_cast<std::size_t>(2 * j);
453
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 b0[j2] = c.b.build_vextract_lane(r, 0);
454
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 b1[j2] = c.b.build_vextract_lane(r, 1);
455
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 b0[j2 + 1u] = c.b.build_vextract_lane(r, 2);
456
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 b1[j2 + 1u] = c.b.build_vextract_lane(r, 3);
457 16 }
458
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 Value* af_cur = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {a_addr(0)});
459
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 16 times.
24 for (int i = 0; i < mt; ++i) {
460
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
24 Value* af_next = (i + 1 < mt)
461
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 ? c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {a_addr(i + 1)})
462 : nullptr;
463
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16 times.
80 for (int j = 0; j < nt; ++j) {
464 64 const auto idx = static_cast<std::size_t>(i * nt + j);
465 64 const auto ju = static_cast<std::size_t>(j);
466
2/4
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
64 accs[idx] = c.b.build_call(mma_sym, Type::f32x4(), {af_cur, b0[ju], b1[ju], accs[idx]});
467 64 }
468 16 af_cur = af_next;
469 16 }
470 8 }
471 4 };
472
473 // Prefill stage 0 before entering the software-pipelined loop.
474
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
4 kb.if_then(c.b.build_slt(c.i32(0), KT), [&] {
475 2 emit_load_A(c.i32(0), c.i32(0));
476 2 c.b.build_call("ptx_cp_async_commit", Type::void_type());
477 2 auto raw0 = emit_fetch_B(c.i32(0));
478
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 emit_store_B(c.i32(0), c.i32(0), raw0);
479
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(0)});
480
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 kb.sync();
481 2 });
482
483 2 std::vector<Value*> init;
484
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 init.reserve(static_cast<std::size_t>(num_accs + 1));
485
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
18 for (int i = 0; i < num_accs; ++i) init.push_back(c.b.build_vzero(Type::f32x4()));
486
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 init.push_back(c.i32(0)); // cur_stage
487
488
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* kt_lim = c.b.build_sub(KT, c.i32(1));
489
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Value*> state = kb.for_range_reduce_n(
490
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 c.i32(0), kt_lim, c.i32(1), init,
491
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 [&](Value* kt, const std::vector<Value*>& cur) -> std::vector<Value*> {
492 2 std::vector<Value*> cur_accs(cur.begin(), cur.begin() + num_accs);
493 2 Value* cur_st = cur.back();
494
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* nxt_st = c.b.build_xor(cur_st, c.i32(1));
495
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* knext = c.b.build_add(kt, c.i32(1));
496
497 // Overlap next tile's global reads (A cp.async + B raw loads) with cur tile's MMA.
498
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 emit_load_A(nxt_st, knext);
499
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 c.b.build_call("ptx_cp_async_commit", Type::void_type());
500
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto raw_next = emit_fetch_B(knext);
501
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 emit_mma(cur_st, cur_accs);
502
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 emit_store_B(nxt_st, knext, raw_next);
503
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(0)});
504
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 kb.sync();
505
506
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 cur_accs.push_back(nxt_st);
507 2 return cur_accs;
508
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 });
509
510 // Epilogue MMA for the last K tile (KT - 1).
511
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Type> tail_types(static_cast<std::size_t>(num_accs), Type::f32x4());
512
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Value*> init_accs(state.begin(), state.begin() + num_accs);
513 2 Value* tail_st = state.back();
514
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Value*> accs = emit_if_else_values(
515
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 c, c.b.build_sgt(KT, c.i32(0)), tail_types,
516 4 [&]() -> std::vector<Value*> {
517 2 std::vector<Value*> a = init_accs;
518
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 emit_mma(tail_st, a);
519 2 return a;
520
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 },
521 4 [&]() -> std::vector<Value*> { return init_accs; });
522
523 // Direct register epilogue straight from m16n8k16 accumulators.
524
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* g = c.b.build_lshr(lane, c.i32(2));
525
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
2 Value* t2 = c.b.build_shl(c.b.build_and(lane, c.i32(3)), c.i32(1));
526
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 for (int j = 0; j < nt; ++j) {
527
4/8
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
8 Value* col0 = c.b.build_add(c.b.build_add(n0, wn_base), c.b.build_add(c.i32(j * 8), t2));
528
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 Value* col1 = c.b.build_add(col0, c.i32(1));
529
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (spec.aligned_mnk) {
530 ✗ Value* b0 = nullptr;
531 ✗ Value* b1 = nullptr;
532
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (spec.has_bias) {
533
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.
✗ std::tie(b0, b1) = unpack_pair_f32(c, spec.dt, kb.load_i32(ptr_at(c, bias, col0, 2)));
534 ✗ }
535
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < mt; ++i) {
536 ✗ Value* frag = accs[static_cast<std::size_t>(i * nt + j)];
537
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int h = 0; h < 2; ++h) {
538
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* row = c.b.build_add(c.b.build_add(m0, wm_base), c.b.build_add(c.i32(i * 16 + h * 8), g));
539
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* v0 = c.b.build_vextract_lane(frag, 2 * h);
540
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* v1 = c.b.build_vextract_lane(frag, 2 * h + 1);
541
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (spec.has_bias) {
542
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ v0 = c.b.build_add(v0, b0);
543
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ v1 = c.b.build_add(v1, b1);
544 ✗ }
545
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.
✗ kb.store_i32(ptr_2d_i64(c, Y, row, M, col0, 1), pack_pair_i32(c, spec.dt, v0, v1));
546 ✗ }
547 ✗ }
548 ✗ } else {
549
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* c0_ok = c.b.build_slt(col0, M);
550
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* c1_ok = c.b.build_slt(col1, M);
551
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
16 kb.if_then(c0_ok, [&] {
552
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* b0 = spec.has_bias ? load_elem_dt(c, spec.dt, bias, col0) : nullptr;
553 8 Value* b1 = nullptr;
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (spec.has_bias) {
555
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 b1 = emit_if_else_values(
556 8 c, c1_ok, {Type::f32()},
557 16 [&]() -> std::vector<Value*> { return {load_elem_dt(c, spec.dt, bias, col1)}; },
558 16 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
559 8 }
560
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (int i = 0; i < mt; ++i) {
561 16 Value* frag = accs[static_cast<std::size_t>(i * nt + j)];
562
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 32 times.
48 for (int h = 0; h < 2; ++h) {
563 64 Value* row = c.b.build_add(c.b.build_add(m0, wm_base),
564 32 c.b.build_add(c.i32(i * 16 + h * 8), g));
565
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
64 kb.if_then(c.b.build_slt(row, B), [&] {
566 32 Value* v0 = c.b.build_vextract_lane(frag, 2 * h);
567 32 Value* v1 = c.b.build_vextract_lane(frag, 2 * h + 1);
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (spec.has_bias) {
569 32 v0 = c.b.build_add(v0, b0);
570 32 v1 = c.b.build_add(v1, b1);
571 32 }
572 32 Value* dst0 = ptr_2d_i64(c, Y, row, M, col0, 1);
573
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 store_lanes(c, spec.dt, 1, dst0, {v0});
574
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
64 kb.if_then(c1_ok, [&] {
575
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 store_lanes(c, spec.dt, 1, c.b.build_add(dst0, kb.const_i64(2)), {v1});
576 32 });
577 32 });
578 32 }
579 16 }
580 8 });
581 }
582 8 }
583
584
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 c.b.build_ret_void();
585
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 fn->rebuild_cfg_predecessors();
586 2 return fn;
587 2 }
588
589 } // namespace brotensor::kernels
590