brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
202 / 210
96.2%
Branches:
69 / 74
93.2%
Functions:
18 / 19
94.7%

src/kernels/mir_coop_common.cpp
Line Branch Exec Source
1 #include "mir_coop_common.h"
2
3 #include "mir_quant_helpers.h"
4 #include "../vulkan/shaders/op_codes.h"
5
6 namespace brotensor::kernels {
7
8 using jit::gpu_mir::bits_to_f32;
9
10 709 Value* emit_v4_zero(Ctx& c) {
11 709 return c.b.build_vzero(Type::i32x4());
12 }
13
14 1629 Value* emit_v4_pack(Ctx& c, Value* w0, Value* w1, Value* w2, Value* w3) {
15 1629 Value* v = c.b.build_vzero(Type::i32x4());
16 1629 v = c.b.build_vinsert_lane(v, w0, 0);
17 1629 v = c.b.build_vinsert_lane(v, w1, 1);
18 1629 v = c.b.build_vinsert_lane(v, w2, 2);
19 1629 v = c.b.build_vinsert_lane(v, w3, 3);
20 1629 return v;
21 }
22
23 968 Value* emit_v4_select(Ctx& c, Value* cond, Value* a, Value* b) {
24 Value* w[4];
25
2/2
✓ Branch 0 taken 3872 times.
✓ Branch 1 taken 968 times.
4840 for (std::uint32_t i = 0; i < 4; ++i) {
26 3872 w[i] = c.b.build_select(cond, c.b.build_vextract_lane(a, i), c.b.build_vextract_lane(b, i));
27 3872 }
28 968 return emit_v4_pack(c, w[0], w[1], w[2], w[3]);
29 }
30
31 453 Value* emit_pack_f32x8_to_f16_v4(Ctx& c, const std::array<Value*, 8>& v) {
32 453 Value* w0 = c.kb.pack_f16x2(v[0], v[1]);
33 453 Value* w1 = c.kb.pack_f16x2(v[2], v[3]);
34 453 Value* w2 = c.kb.pack_f16x2(v[4], v[5]);
35 453 Value* w3 = c.kb.pack_f16x2(v[6], v[7]);
36 453 return emit_v4_pack(c, w0, w1, w2, w3);
37 }
38
39 84 Value* emit_bf16x8_to_f16x8(Ctx& c, Value* v4) {
40 Value* out[4];
41
2/2
✓ Branch 0 taken 336 times.
✓ Branch 1 taken 84 times.
420 for (std::uint32_t i = 0; i < 4; ++i) {
42 336 Value* w = c.b.build_vextract_lane(v4, i);
43 336 Value* lo = bits_to_f32(c, c.b.build_shl(w, c.i32(16)));
44 336 Value* hi = bits_to_f32(c, c.b.build_and(w, c.i32(static_cast<int>(0xffff0000u))));
45 336 out[i] = c.kb.pack_f16x2(lo, hi);
46 336 }
47 84 return emit_v4_pack(c, out[0], out[1], out[2], out[3]);
48 }
49
50 16 Value* emit_bf16x8_to_f16x8_scaled(Ctx& c, Value* v4, const std::array<Value*, 8>& mul) {
51 Value* out[4];
52
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16 times.
80 for (std::uint32_t i = 0; i < 4; ++i) {
53 64 Value* w = c.b.build_vextract_lane(v4, i);
54 64 Value* lo = c.b.build_mul(bits_to_f32(c, c.b.build_shl(w, c.i32(16))), mul[2u * i]);
55 64 Value* hi = c.b.build_mul(bits_to_f32(c, c.b.build_and(w, c.i32(static_cast<int>(0xffff0000u)))), mul[2u * i + 1u]);
56 64 out[i] = c.kb.pack_f16x2(lo, hi);
57 64 }
58 16 return emit_v4_pack(c, out[0], out[1], out[2], out[3]);
59 }
60
61 ✗ Value* emit_f32x8_to_f16x8_scaled(Ctx& c, Value* v0, Value* v1, const std::array<Value*, 8>& mul) {
62 ✗ std::array<Value*, 8> f{};
63
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (std::uint32_t i = 0; i < 4; ++i) {
64 ✗ f[i] = c.b.build_mul(bits_to_f32(c, c.b.build_vextract_lane(v0, i)), mul[i]);
65 ✗ f[4u + i] = c.b.build_mul(bits_to_f32(c, c.b.build_vextract_lane(v1, i)), mul[4u + i]);
66 ✗ }
67 ✗ return emit_pack_f32x8_to_f16_v4(c, f);
68 }
69
70 104 QRawMir emit_qload8_mir(Ctx& c, std::uint32_t fmt, bool vec, Value* row_ptr,
71 Value* scale_ptr, Value* ridx, Value* k, Value* kmax) {
72 104 QRawMir o;
73
4/4
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 44 times.
104 if (fmt == QF_INT8 || fmt == QF_FP8) {
74
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 4 times.
60 if (vec) {
75 56 Value* q64 = c.kb.load_i64(c.b.build_add(row_ptr, c.b.build_zext_i64(k)));
76 56 o.w0 = c.b.build_trunc_i32(q64);
77 56 o.w1 = c.b.build_trunc_i32(c.b.build_lshr(q64, c.kb.const_i64(32)));
78 56 } else {
79 4 Value* w[2] = {c.i32(0), c.i32(0)};
80
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 32 times.
36 for (int e = 0; e < 8; ++e) {
81 32 Value* ke = c.b.build_add(k, c.i32(e));
82 32 Value* ok = c.b.build_ult(ke, kmax);
83
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 Value* b = emit_if_else_values(
84 32 c, ok, {Type::i32()},
85 64 [&]() -> std::vector<Value*> {
86 32 return {c.kb.load_u8(c.b.build_add(row_ptr, c.b.build_zext_i64(ke)))};
87 },
88 64 [&]() -> std::vector<Value*> { return {c.i32(0)}; })[0];
89 32 w[e >> 2] = c.b.build_or(w[e >> 2], c.b.build_shl(b, c.i32((e & 3) * 8)));
90 32 }
91 4 o.w0 = w[0];
92 4 o.w1 = w[1];
93 }
94 60 o.c = c.kb.load_i32(ptr_at(c, scale_ptr, ridx, 4));
95
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 32 times.
104 } else if (fmt == QF_Q8_0) {
96 24 Value* blk = c.b.build_add(row_ptr, c.b.build_mul(c.b.build_zext_i64(c.b.build_lshr(k, c.i32(5))),
97 12 c.kb.const_i64(34)));
98 12 Value* qp = c.b.build_add(blk, c.b.build_zext_i64(c.b.build_add(c.i32(2), c.b.build_and(k, c.i32(31)))));
99
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 12 times.
60 for (int i = 0; i < 4; ++i) {
100 48 o.s0[static_cast<std::size_t>(i)] = c.kb.load_u16(c.b.build_add(qp, c.kb.const_i64(i * 2)));
101 48 }
102 12 o.c = c.kb.load_u16(blk);
103
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 12 times.
44 } else if (fmt == QF_Q4K) {
104 40 Value* blk = c.b.build_add(row_ptr, c.b.build_mul(c.b.build_zext_i64(c.b.build_lshr(k, c.i32(8))),
105 20 c.kb.const_i64(144)));
106 20 Value* j = c.b.build_lshr(c.b.build_and(k, c.i32(255)), c.i32(5));
107 40 Value* boff = c.b.build_add(c.i32(16), c.b.build_add(c.b.build_shl(c.b.build_lshr(j, c.i32(1)), c.i32(5)),
108 20 c.b.build_and(k, c.i32(31))));
109 20 o.a = c.kb.vload(Type::i32x4(), blk);
110 20 Value* n64 = c.kb.load_i64(c.b.build_add(blk, c.b.build_zext_i64(boff)));
111 20 o.w0 = c.b.build_trunc_i32(n64);
112 20 o.w1 = c.b.build_trunc_i32(c.b.build_lshr(n64, c.kb.const_i64(32)));
113 20 } else {
114 // QF_Q6K: 210-byte super-blocks (2-byte aligned)
115 24 Value* blk = c.b.build_add(row_ptr, c.b.build_mul(c.b.build_zext_i64(c.b.build_lshr(k, c.i32(8))),
116 12 c.kb.const_i64(210)));
117 12 Value* e0 = c.b.build_and(k, c.i32(255));
118 12 Value* g = c.b.build_lshr(e0, c.i32(7));
119 12 Value* quad = c.b.build_lshr(c.b.build_and(e0, c.i32(127)), c.i32(5));
120 12 Value* l0 = c.b.build_and(e0, c.i32(31));
121 36 Value* ql_off = c.b.build_add(c.b.build_add(c.b.build_shl(g, c.i32(6)),
122 12 c.b.build_shl(c.b.build_and(quad, c.i32(1)), c.i32(5))),
123 12 l0);
124 12 Value* qh_off = c.b.build_add(c.i32(128), c.b.build_add(c.b.build_shl(g, c.i32(5)), l0));
125 24 Value* sc_off = c.b.build_add(c.i32(192), c.b.build_add(c.b.build_shl(g, c.i32(3)),
126 12 c.b.build_shl(quad, c.i32(1))));
127 12 Value* qlp = c.b.build_add(blk, c.b.build_zext_i64(ql_off));
128 12 Value* qhp = c.b.build_add(blk, c.b.build_zext_i64(qh_off));
129
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 12 times.
60 for (int i = 0; i < 4; ++i) {
130 48 o.s0[static_cast<std::size_t>(i)] = c.kb.load_u16(c.b.build_add(qlp, c.kb.const_i64(i * 2)));
131 48 o.s1[static_cast<std::size_t>(i)] = c.kb.load_u16(c.b.build_add(qhp, c.kb.const_i64(i * 2)));
132 48 }
133 12 o.c = c.kb.load_u16(c.b.build_add(blk, c.b.build_zext_i64(sc_off)));
134 12 o.d = c.kb.load_u16(c.b.build_add(blk, c.kb.const_i64(208)));
135 }
136 104 return o;
137 ✗ }
138
139 104 Value* emit_qdecode8_f16_mir(Ctx& c, std::uint32_t fmt, const QRawMir& q, Value* k, Value* s_fp8) {
140 104 std::array<Value*, 8> w{};
141
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 100 times.
104 if (fmt == QF_FP8) {
142 4 Value* sc = bits_to_f32(c, q.c);
143
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 4 times.
36 for (int e = 0; e < 8; ++e) {
144
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 Value* word = (e < 4) ? q.w0 : q.w1;
145 32 Value* b = c.b.build_and(c.b.build_lshr(word, c.i32((e & 3) * 8)), c.i32(0xff));
146
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 Value* dv = s_fp8 ? c.kb.shared_load_f32_indexed(s_fp8, b) : emit_fp8_e4m3_to_f32(c, b);
147 32 w[static_cast<std::size_t>(e)] = c.b.build_mul(dv, sc);
148 32 }
149
4/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 32 times.
104 } else if (fmt == QF_INT8 || fmt == QF_Q8_0) {
150
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 12 times.
68 Value* sc = (fmt == QF_INT8) ? bits_to_f32(c, q.c) : c.kb.f16_to_f32(q.c);
151
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 12 times.
68 Value* qlo = (fmt == QF_INT8) ? q.w0 : c.b.build_or(q.s0[0], c.b.build_shl(q.s0[1], c.i32(16)));
152
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 12 times.
68 Value* qhi = (fmt == QF_INT8) ? q.w1 : c.b.build_or(q.s0[2], c.b.build_shl(q.s0[3], c.i32(16)));
153
2/2
✓ Branch 0 taken 544 times.
✓ Branch 1 taken 68 times.
612 for (int e = 0; e < 8; ++e) {
154
2/2
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 272 times.
544 Value* word = (e < 4) ? qlo : qhi;
155 544 Value* b = c.b.build_and(c.b.build_lshr(word, c.i32((e & 3) * 8)), c.i32(0xff));
156 544 w[static_cast<std::size_t>(e)] = c.b.build_mul(emit_s8_byte_to_f32(c, b), sc);
157 544 }
158
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 12 times.
100 } else if (fmt == QF_Q4K) {
159 20 Value* a0 = c.b.build_vextract_lane(q.a, 0);
160 20 Value* s0 = c.b.build_vextract_lane(q.a, 1);
161 20 Value* s1 = c.b.build_vextract_lane(q.a, 2);
162 20 Value* s2 = c.b.build_vextract_lane(q.a, 3);
163 20 Value* j = c.b.build_lshr(c.b.build_and(k, c.i32(255)), c.i32(5));
164 20 Value* low_j = c.b.build_ult(j, c.i32(4));
165 20 Value* j4 = c.b.build_add(j, c.i32(4));
166 20 Value* jm4 = c.b.build_sub(j, c.i32(4));
167 20 Value* bs_j = emit_extract_byte(c, emit_sel_word3(c, c.b.build_lshr(j, c.i32(2)), s0, s1, s2), j);
168 20 Value* bs_j4 = emit_extract_byte(c, emit_sel_word3(c, c.b.build_lshr(j4, c.i32(2)), s0, s1, s2), j4);
169 20 Value* bs_jm4 = emit_extract_byte(c, emit_sel_word3(c, c.b.build_lshr(jm4, c.i32(2)), s0, s1, s2), jm4);
170 40 Value* sc = c.b.build_select(
171 20 low_j, c.b.build_and(bs_j, c.i32(63)),
172 20 c.b.build_or(c.b.build_and(bs_j4, c.i32(15)), c.b.build_shl(c.b.build_lshr(bs_jm4, c.i32(6)), c.i32(4))));
173 40 Value* mn = c.b.build_select(
174 20 low_j, c.b.build_and(bs_j4, c.i32(63)),
175 20 c.b.build_or(c.b.build_lshr(bs_j4, c.i32(4)), c.b.build_shl(c.b.build_lshr(bs_j, c.i32(6)), c.i32(4))));
176 20 Value* d = c.kb.f16_to_f32(c.b.build_and(a0, c.i32(0xffff)));
177 20 Value* dmin = c.kb.f16_to_f32(c.b.build_lshr(a0, c.i32(16)));
178 20 Value* ws = c.b.build_mul(c.kb.u32_to_f32(sc), d);
179 20 Value* wm = c.b.build_mul(c.kb.u32_to_f32(mn), dmin);
180 20 Value* sh = c.b.build_shl(c.b.build_and(j, c.i32(1)), c.i32(2));
181
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 20 times.
180 for (int e = 0; e < 8; ++e) {
182
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 80 times.
160 Value* word = (e < 4) ? q.w0 : q.w1;
183 160 Value* q4 = c.b.build_and(c.b.build_lshr(word, c.b.build_add(sh, c.i32((e & 3) * 8))), c.i32(0x0f));
184 160 w[static_cast<std::size_t>(e)] = c.b.build_fma_f32(ws, c.kb.u32_to_f32(q4), c.b.build_neg(wm));
185 160 }
186 20 } else {
187 12 Value* e0 = c.b.build_and(k, c.i32(255));
188 12 Value* quad = c.b.build_lshr(c.b.build_and(e0, c.i32(127)), c.i32(5));
189 12 Value* l0 = c.b.build_and(e0, c.i32(31));
190 24 Value* sbyte = c.b.build_and(c.b.build_lshr(q.c, c.b.build_shl(c.b.build_lshr(l0, c.i32(4)), c.i32(3))),
191 12 c.i32(0xff));
192 12 Value* sc = c.b.build_mul(c.kb.f16_to_f32(q.d), emit_s8_byte_to_f32(c, sbyte));
193 12 Value* qlo = c.b.build_or(q.s0[0], c.b.build_shl(q.s0[1], c.i32(16)));
194 12 Value* qhi = c.b.build_or(q.s0[2], c.b.build_shl(q.s0[3], c.i32(16)));
195 12 Value* hlo = c.b.build_or(q.s1[0], c.b.build_shl(q.s1[1], c.i32(16)));
196 12 Value* hhi = c.b.build_or(q.s1[2], c.b.build_shl(q.s1[3], c.i32(16)));
197 12 Value* ql_sh = c.b.build_select(c.b.build_uge(quad, c.i32(2)), c.i32(4), c.i32(0));
198 12 Value* qh_sh = c.b.build_shl(quad, c.i32(1));
199
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 12 times.
108 for (int e = 0; e < 8; ++e) {
200
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 48 times.
96 Value* bw = (e < 4) ? qlo : qhi;
201
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 48 times.
96 Value* hw = (e < 4) ? hlo : hhi;
202 96 Value* bsh = c.i32((e & 3) * 8);
203 96 Value* nib = c.b.build_and(c.b.build_lshr(c.b.build_lshr(bw, bsh), ql_sh), c.i32(0x0f));
204 192 Value* hb = c.b.build_shl(c.b.build_and(c.b.build_lshr(c.b.build_lshr(hw, bsh), qh_sh), c.i32(0x03)),
205 96 c.i32(4));
206 96 Value* q6 = c.b.build_sub(c.kb.u32_to_f32(c.b.build_or(nib, hb)), c.f32(32.0f));
207 96 w[static_cast<std::size_t>(e)] = c.b.build_mul(sc, q6);
208 96 }
209 }
210 104 return emit_pack_f32x8_to_f16_v4(c, w);
211 }
212
213 87 void emit_coop_acc_map(Ctx& c, Value* acc, const std::function<Value*(Value* idx, Value* val)>& fn) {
214 87 Value* len = c.kb.coop_acc_len(acc);
215
1/2
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
174 c.kb.for_range(c.i32(0), len, c.i32(1), [&](Value* e) {
216 87 Value* cur = c.kb.coop_get_acc(acc, e);
217 87 c.kb.coop_set_acc(acc, e, fn(e, cur));
218 87 });
219 87 }
220
221 24 Value* emit_cluster16_max_f32(Ctx& c, Value* v) {
222
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 24 times.
120 for (std::uint32_t m = 8; m >= 1; m >>= 1) v = c.kb.fmax(v, c.kb.shfl_bfly_f32(v, m));
223 24 return v;
224 }
225
226 48 Value* emit_cluster16_add_f32(Ctx& c, Value* v) {
227
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 48 times.
240 for (std::uint32_t m = 8; m >= 1; m >>= 1) v = c.b.build_add(v, c.kb.shfl_bfly_f32(v, m));
228 48 return v;
229 }
230
231 72 Value* emit_cluster16_min_i32(Ctx& c, Value* v) {
232
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 72 times.
360 for (std::uint32_t m = 8; m >= 1; m >>= 1) {
233 288 Value* o = c.kb.shfl_bfly_i32(v, m);
234 288 v = c.b.build_select(c.b.build_ult(o, v), o, v);
235 288 }
236 72 return v;
237 }
238
239 72 Value* emit_cluster16_max_i32(Ctx& c, Value* v) {
240
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 72 times.
360 for (std::uint32_t m = 8; m >= 1; m >>= 1) {
241 288 Value* o = c.kb.shfl_bfly_i32(v, m);
242 288 v = c.b.build_select(c.b.build_ugt(o, v), o, v);
243 288 }
244 72 return v;
245 }
246
247 3 Value* emit_subgroup32_any_i32(Ctx& c, Value* cond) {
248 3 Value* v = c.b.build_select(cond, c.i32(1), c.i32(0));
249
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
18 for (std::uint32_t m = 16; m >= 1; m >>= 1) v = c.b.build_or(v, c.kb.shfl_bfly_i32(v, m));
250 3 return c.b.build_ne(v, c.i32(0));
251 }
252
253 9 Value* emit_subgroup32_all_i32(Ctx& c, Value* cond) {
254 9 Value* v = c.b.build_select(cond, c.i32(1), c.i32(0));
255
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 9 times.
54 for (std::uint32_t m = 16; m >= 1; m >>= 1) v = c.b.build_and(v, c.kb.shfl_bfly_i32(v, m));
256 9 return c.b.build_ne(v, c.i32(0));
257 }
258
259 } // namespace brotensor::kernels
260