| Line |
Branch |
Exec |
Source |
| 1 |
|
|
#include "mir_quant_helpers.h" |
| 2 |
|
|
|
| 3 |
|
|
#include "mir_common.h" |
| 4 |
|
|
#include "mir_math.h" |
| 5 |
|
|
#include "../vulkan/shaders/op_codes.h" |
| 6 |
|
|
|
| 7 |
|
|
#include <array> |
| 8 |
|
|
#include <stdexcept> |
| 9 |
|
|
#include <string> |
| 10 |
|
|
|
| 11 |
|
|
namespace brotensor::kernels { |
| 12 |
|
|
|
| 13 |
|
|
namespace { |
| 14 |
|
|
|
| 15 |
|
|
using jit::gpu_mir::bits_to_f32; |
| 16 |
|
|
using jit::gpu_mir::f32_to_bits; |
| 17 |
|
|
|
| 18 |
|
24 |
Value* umax_i32(Ctx& c, Value* a, Value* b) { |
| 19 |
|
24 |
return c.b.build_select(c.b.build_ugt(a, b), a, b); |
| 20 |
|
|
} |
| 21 |
|
|
|
| 22 |
|
12 |
Value* finite_or_0(Ctx& c, Value* u) { |
| 23 |
|
12 |
return c.b.build_select(c.b.build_ult(u, c.i32(0x7f800000)), u, c.i32(0)); |
| 24 |
|
|
} |
| 25 |
|
|
|
| 26 |
|
✗ |
Value* abs_bits_elem(Ctx& c, Dtype dt, Value* base, Value* idx) { |
| 27 |
|
✗ |
Value* u = nullptr; |
| 28 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
if (dt == Dtype::BF16) { |
| 29 |
|
✗ |
Value* raw = c.kb.load_u16(ptr_at(c, base, idx, 2)); |
| 30 |
|
✗ |
u = c.b.build_and(c.b.build_shl(raw, c.i32(16)), c.i32(0x7fffffff)); |
| 31 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
} else if (dt == Dtype::FP32) { |
| 32 |
|
✗ |
u = c.b.build_and(c.kb.load_i32(ptr_at(c, base, idx, 4)), c.i32(0x7fffffff)); |
| 33 |
|
✗ |
} else { |
| 34 |
|
✗ |
Value* fv = c.kb.fabs(load_elem_dt(c, dt, base, idx)); |
| 35 |
|
✗ |
u = f32_to_bits(c, fv); |
| 36 |
|
|
} |
| 37 |
|
✗ |
return finite_or_0(c, u); |
| 38 |
|
|
} |
| 39 |
|
|
|
| 40 |
|
2 |
Value* exponent_for(Ctx& c, Value* u) { |
| 41 |
|
2 |
Value* raw = c.b.build_sub(c.b.build_lshr(u, c.i32(23)), c.i32(141)); |
| 42 |
|
2 |
Value* clamped = clamp_i32(c, raw, c.i32(-100), c.i32(100)); |
| 43 |
|
2 |
return c.b.build_select(c.b.build_eq(u, c.i32(0)), c.i32(0), clamped); |
| 44 |
|
|
} |
| 45 |
|
|
|
| 46 |
|
1934 |
Value* s8_byte_to_f32(Ctx& c, Value* b) { |
| 47 |
|
1934 |
return c.kb.i32_to_f32(c.b.build_ashr(c.b.build_shl(b, c.i32(24)), c.i32(24))); |
| 48 |
|
|
} |
| 49 |
|
|
|
| 50 |
|
|
// Decode OCP FP8 E4M3 (low 7-bit magnitude + bit 7 sign) to FP32. |
| 51 |
|
68 |
Value* fp8_decode_u32(Ctx& c, Value* b) { |
| 52 |
|
68 |
Value* v = c.b.build_and(b, c.i32(0x7f)); |
| 53 |
|
68 |
Value* norm = bits_to_f32(c, c.b.build_add(c.b.build_shl(v, c.i32(20)), c.i32(0x3c000000))); |
| 54 |
|
68 |
Value* sub = c.b.build_mul(c.kb.u32_to_f32(v), c.f32(0.001953125f)); |
| 55 |
|
68 |
Value* mag = c.b.build_select(c.b.build_ult(v, c.i32(8)), sub, norm); |
| 56 |
|
68 |
Value* is_nan = c.b.build_eq(v, c.i32(0x7f)); |
| 57 |
|
68 |
mag = c.b.build_select(is_nan, bits_to_f32(c, c.i32(0x7fc00000)), mag); |
| 58 |
|
68 |
Value* sgn = c.b.build_shl(c.b.build_and(b, c.i32(0x80)), c.i32(24)); |
| 59 |
|
68 |
return bits_to_f32(c, c.b.build_or(f32_to_bits(c, mag), sgn)); |
| 60 |
|
|
} |
| 61 |
|
|
|
| 62 |
|
5 |
Value* fp8_encode_f32(Ctx& c, Value* v) { |
| 63 |
|
5 |
Value* bits = f32_to_bits(c, v); |
| 64 |
|
5 |
Value* mag_bits = c.b.build_and(bits, c.i32(0x7fffffff)); |
| 65 |
|
5 |
Value* is_nan = c.b.build_ugt(mag_bits, c.i32(0x7f800000)); |
| 66 |
|
5 |
Value* sign = c.b.build_shl(c.b.build_lshr(bits, c.i32(31)), c.i32(7)); |
| 67 |
|
5 |
Value* a = c.kb.fabs(v); |
| 68 |
|
|
|
| 69 |
|
5 |
Value* lo = c.i32(0); |
| 70 |
|
5 |
Value* hi = c.i32(126); |
| 71 |
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 5 times.
|
40 |
for (int step = 0; step < 7; ++step) { |
| 72 |
|
35 |
Value* mid = c.b.build_lshr(c.b.build_add(lo, hi), c.i32(1)); |
| 73 |
|
35 |
Value* le = c.b.build_sle(fp8_decode_u32(c, mid), a); |
| 74 |
|
35 |
lo = c.b.build_select(le, mid, lo); |
| 75 |
|
35 |
hi = c.b.build_select(le, hi, mid); |
| 76 |
|
35 |
} |
| 77 |
|
5 |
Value* dl = c.b.build_sub(a, fp8_decode_u32(c, lo)); |
| 78 |
|
5 |
Value* dh = c.b.build_sub(fp8_decode_u32(c, hi), a); |
| 79 |
|
5 |
Value* even_pick = c.b.build_select(c.b.build_ne(c.b.build_and(lo, c.i32(1)), c.i32(0)), hi, lo); |
| 80 |
|
10 |
Value* code = c.b.build_select(c.b.build_slt(dl, dh), lo, |
| 81 |
|
5 |
c.b.build_select(c.b.build_slt(dh, dl), hi, even_pick)); |
| 82 |
|
5 |
Value* sat = c.b.build_select(c.b.build_sge(a, c.f32(448.0f)), c.i32(0x7e), code); |
| 83 |
|
5 |
return c.b.build_select(is_nan, c.i32(0x7f), c.b.build_or(sign, sat)); |
| 84 |
|
|
} |
| 85 |
|
|
|
| 86 |
|
|
// Correctly rounded a / b using pinned NR + FMA exact remainder on candidate +-1 ULP. |
| 87 |
|
14 |
Value* div_cr_f32(Ctx& c, Value* a, Value* b, Value* zero_mask) { |
| 88 |
|
98 |
auto pin_f32 = [&](Value* v) { |
| 89 |
|
84 |
return bits_to_f32(c, c.b.build_xor(f32_to_bits(c, v), zero_mask)); |
| 90 |
|
|
}; |
| 91 |
|
14 |
Value* r = pin_f32(c.b.build_sdiv(c.f32(1.0f), b)); |
| 92 |
|
14 |
Value* q0 = pin_f32(c.b.build_mul(a, r)); |
| 93 |
|
14 |
Value* q1 = pin_f32(c.b.build_fma_f32(c.b.build_fma_f32(c.b.build_neg(q0), b, a), r, q0)); |
| 94 |
|
14 |
Value* q = pin_f32(c.b.build_fma_f32(c.b.build_fma_f32(c.b.build_neg(q1), b, a), r, q1)); |
| 95 |
|
14 |
Value* qb = f32_to_bits(c, q); |
| 96 |
|
14 |
Value* dn = pin_f32(bits_to_f32(c, c.b.build_sub(qb, c.i32(1)))); |
| 97 |
|
14 |
Value* up = pin_f32(bits_to_f32(c, c.b.build_add(qb, c.i32(1)))); |
| 98 |
|
56 |
auto rem_abs = [&](Value* cand) -> Value* { |
| 99 |
|
42 |
return c.kb.fabs(c.b.build_fma_f32(c.b.build_neg(cand), b, a)); |
| 100 |
|
|
}; |
| 101 |
|
14 |
Value* e = rem_abs(q); |
| 102 |
|
14 |
Value* ed = rem_abs(dn); |
| 103 |
|
14 |
Value* eu = rem_abs(up); |
| 104 |
|
14 |
Value* best = c.b.build_select(c.b.build_slt(ed, e), dn, q); |
| 105 |
|
14 |
Value* me = c.kb.fmin(e, ed); |
| 106 |
|
14 |
best = c.b.build_select(c.b.build_slt(eu, me), up, best); |
| 107 |
|
28 |
Value* zero_in = c.b.build_or( |
| 108 |
|
14 |
c.b.build_eq(a, c.f32(0.0f)), |
| 109 |
|
14 |
c.b.build_eq(c.b.build_and(qb, c.i32(0x7fffffff)), c.i32(0))); |
| 110 |
|
14 |
return c.b.build_select(zero_in, a, best); |
| 111 |
|
|
} |
| 112 |
|
|
|
| 113 |
|
126 |
Value* sel_word3(Ctx& c, Value* widx, Value* w0, Value* w1, Value* w2) { |
| 114 |
|
252 |
return c.b.build_select(c.b.build_eq(widx, c.i32(0)), w0, |
| 115 |
|
126 |
c.b.build_select(c.b.build_eq(widx, c.i32(1)), w1, w2)); |
| 116 |
|
|
} |
| 117 |
|
|
|
| 118 |
|
126 |
Value* extract_byte(Ctx& c, Value* word, Value* bidx) { |
| 119 |
|
126 |
return c.b.build_and(c.b.build_lshr(word, c.b.build_shl(c.b.build_and(bidx, c.i32(3)), c.i32(3))), c.i32(0xff)); |
| 120 |
|
|
} |
| 121 |
|
|
|
| 122 |
|
|
} // namespace |
| 123 |
|
|
|
| 124 |
|
1922 |
Value* emit_s8_byte_to_f32(Ctx& c, Value* byte_u32) { return s8_byte_to_f32(c, byte_u32); } |
| 125 |
|
20 |
Value* emit_fp8_e4m3_to_f32(Ctx& c, Value* byte_u32) { return fp8_decode_u32(c, byte_u32); } |
| 126 |
|
120 |
Value* emit_sel_word3(Ctx& c, Value* widx, Value* w0, Value* w1, Value* w2) { |
| 127 |
|
120 |
return sel_word3(c, widx, w0, w1, w2); |
| 128 |
|
|
} |
| 129 |
|
120 |
Value* emit_extract_byte(Ctx& c, Value* word, Value* bidx) { return extract_byte(c, word, bidx); } |
| 130 |
|
|
|
| 131 |
|
2 |
brass::Function* build_gemm_rowscale_mir(brass::Module& mod, Dtype dt, bool ta, bool vec) { |
| 132 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
6 |
const std::string name = std::string("mir_gemm_rowscale_") + |
| 133 |
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 |
(dt == Dtype::FP32 ? "f32" : dt == Dtype::FP16 ? "f16" : "bf16") + |
| 134 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
(ta ? "_ta" : "") + (vec ? "_v" : ""); |
| 135 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
std::vector<Type> params = {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32(), Type::i32()}; |
| 136 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); |
| 137 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
brass::codegen::KernelBuilder kb(mod, fn); |
| 138 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
Ctx c(kb); |
| 139 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
auto p = entry_params(c, fn); |
| 140 |
|
|
// p[0]=a, p[1]=out, p[2]=m, p[3]=k, p[4]=lda, p[5]=sa |
| 141 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
const int esz = elem_bytes(dt); |
| 142 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
Value* z = kb.ctaid_z(); |
| 143 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 |
Value* A = ptr_at(c, p[0], c.b.build_mul(z, p[5]), esz); |
| 144 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
Value* t = kb.tid_x(); |
| 145 |
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.
|
2 |
Value* g = c.b.build_add(kb.ctaid_x(), c.b.build_mul(kb.ctaid_y(), kb.nctaid_x())); |
| 146 |
|
|
|
| 147 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 |
if (ta) { |
| 148 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
Value* m = c.b.build_add(c.b.build_mul(g, c.i32(256)), t); |
| 149 |
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.if_then(c.b.build_ult(m, p[2]), [&] { |
| 150 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
Value* mx = kb.for_range_reduce( |
| 151 |
|
✗ |
c.i32(0), p[3], c.i32(1), c.i32(0), |
| 152 |
|
✗ |
[&](Value* k, Value* cur) -> Value* { |
| 153 |
|
✗ |
return umax_i32(c, cur, abs_bits_elem(c, dt, A, c.b.build_add(c.b.build_mul(k, p[4]), m))); |
| 154 |
|
|
}); |
| 155 |
|
✗ |
kb.store_i32(ptr_at(c, p[1], c.b.build_add(c.b.build_mul(z, p[2]), m), 4), exponent_for(c, mx)); |
| 156 |
|
✗ |
}); |
| 157 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
c.b.build_ret_void(); |
| 158 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
return finalize_fn(fn); |
| 159 |
|
|
} |
| 160 |
|
|
|
| 161 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
Value* s_red = kb.shared_alloc_i32(256); |
| 162 |
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.
|
2 |
Value* row = c.b.build_add(c.b.build_mul(g, c.i32(4)), c.b.build_lshr(t, c.i32(6))); |
| 163 |
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(t, c.i32(63)); |
| 164 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
Value* mx = emit_if_else_values( |
| 165 |
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_ult(row, p[2]), {Type::i32()}, |
| 166 |
|
4 |
[&]() -> std::vector<Value*> { |
| 167 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 |
if (vec && dt != Dtype::FP16) { |
| 168 |
|
2 |
Value* per = c.i32(dt == Dtype::BF16 ? 8 : 4); |
| 169 |
|
2 |
Value* row_base = c.b.build_udiv(c.b.build_mul(row, p[4]), per); |
| 170 |
|
2 |
Value* nchunks = c.b.build_udiv(p[3], per); |
| 171 |
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.
|
4 |
return {kb.for_range_reduce( |
| 172 |
|
2 |
lane, nchunks, c.i32(64), c.i32(0), |
| 173 |
|
4 |
[&](Value* ch, Value* cur) -> Value* { |
| 174 |
|
2 |
Value* v4 = kb.vload(Type::i32x4(), ptr_at(c, A, c.b.build_add(row_base, ch), 16)); |
| 175 |
|
2 |
Value* res = cur; |
| 176 |
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
|
10 |
for (std::uint32_t j = 0; j < 4; ++j) { |
| 177 |
|
8 |
Value* w = c.b.build_vextract_lane(v4, j); |
| 178 |
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 |
if (dt == Dtype::BF16) { |
| 179 |
|
4 |
Value* lo = finite_or_0(c, c.b.build_and(c.b.build_shl(w, c.i32(16)), c.i32(0x7fffffff))); |
| 180 |
|
4 |
Value* hi = finite_or_0(c, c.b.build_and(w, c.i32(0x7fff0000))); |
| 181 |
|
4 |
res = umax_i32(c, res, umax_i32(c, lo, hi)); |
| 182 |
|
4 |
} else { |
| 183 |
|
4 |
res = umax_i32(c, res, finite_or_0(c, c.b.build_and(w, c.i32(0x7fffffff)))); |
| 184 |
|
|
} |
| 185 |
|
8 |
} |
| 186 |
|
2 |
return res; |
| 187 |
|
|
})}; |
| 188 |
|
|
} |
| 189 |
|
✗ |
Value* rbase = c.b.build_mul(row, p[4]); |
| 190 |
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.
|
✗ |
return {kb.for_range_reduce( |
| 191 |
|
✗ |
lane, p[3], c.i32(64), c.i32(0), |
| 192 |
|
✗ |
[&](Value* k, Value* cur) -> Value* { |
| 193 |
|
✗ |
return umax_i32(c, cur, abs_bits_elem(c, dt, A, c.b.build_add(rbase, k))); |
| 194 |
|
|
})}; |
| 195 |
|
2 |
}, |
| 196 |
|
4 |
[&]() -> std::vector<Value*> { return {c.i32(0)}; })[0]; |
| 197 |
|
|
|
| 198 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
kb.shared_store_i32_indexed(s_red, t, mx); |
| 199 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
kb.sync(); |
| 200 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
|
14 |
for (int s = 32; s > 0; s >>= 1) { |
| 201 |
4/8
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
|
24 |
kb.if_then(c.b.build_ult(lane, c.i32(s)), [&] { |
| 202 |
|
24 |
Value* v = umax_i32(c, kb.shared_load_i32_indexed(s_red, t), |
| 203 |
|
12 |
kb.shared_load_i32_indexed(s_red, c.b.build_add(t, c.i32(s)))); |
| 204 |
|
12 |
kb.shared_store_i32_indexed(s_red, t, v); |
| 205 |
|
12 |
}); |
| 206 |
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 |
kb.sync(); |
| 207 |
|
12 |
} |
| 208 |
6/12
✓ 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.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
|
4 |
kb.if_then(c.b.build_and(c.b.build_eq(lane, c.i32(0)), c.b.build_ult(row, p[2])), [&] { |
| 209 |
|
2 |
Value* best = kb.shared_load_i32_indexed(s_red, t); |
| 210 |
|
2 |
kb.store_i32(ptr_at(c, p[1], c.b.build_add(c.b.build_mul(z, p[2]), row), 4), exponent_for(c, best)); |
| 211 |
|
2 |
}); |
| 212 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
c.b.build_ret_void(); |
| 213 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
return finalize_fn(fn); |
| 214 |
|
2 |
} |
| 215 |
|
|
|
| 216 |
|
13 |
brass::Function* build_wq_rows_mir(brass::Module& mod, std::uint32_t mode, Dtype dt, bool fp8) { |
| 217 |
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
39 |
const std::string name = std::string("mir_wq_rows_") + (mode == 0u ? "q_" : "dq_") + |
| 218 |
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 |
(dt == Dtype::FP32 ? "f32" : dt == Dtype::FP16 ? "f16" : "bf16") + |
| 219 |
|
13 |
(fp8 ? "_fp8" : "_i8"); |
| 220 |
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 |
std::vector<Type> params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32()}; |
| 221 |
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 |
Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); |
| 222 |
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 |
brass::codegen::KernelBuilder kb(mod, fn); |
| 223 |
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 |
Ctx c(kb); |
| 224 |
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 |
auto p = entry_params(c, fn); |
| 225 |
|
|
// p[0]=w, p[1]=q, p[2]=scale, p[3]=rows, p[4]=k |
| 226 |
|
|
|
| 227 |
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6 times.
|
13 |
if (mode == 1u) { |
| 228 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
Value* total = c.b.build_mul(p[3], p[4]); |
| 229 |
4/8
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
|
12 |
kb.for_range(kb.global_tid_x(), total, grid_stride_ew(c), [&](Value* i) { |
| 230 |
|
6 |
Value* b = kb.load_u8(ptr_at(c, p[1], i, 1)); |
| 231 |
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 |
Value* code = fp8 ? fp8_decode_u32(c, b) : s8_byte_to_f32(c, b); |
| 232 |
|
6 |
Value* sc = kb.load_f32(ptr_at(c, p[2], c.b.build_udiv(i, p[4]), 4)); |
| 233 |
|
6 |
store_elem_dt(c, dt, p[0], i, c.b.build_mul(code, sc)); |
| 234 |
|
6 |
}); |
| 235 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
c.b.build_ret_void(); |
| 236 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
return finalize_fn(fn); |
| 237 |
|
|
} |
| 238 |
|
|
|
| 239 |
5/10
✓ 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.
✓ Branch 8 taken 7 times.
✗ Branch 9 not taken.
|
7 |
Value* row = c.b.build_add(kb.ctaid_x(), c.b.build_mul(kb.ctaid_y(), kb.nctaid_x())); |
| 240 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
Value* tid = kb.tid_x(); |
| 241 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
Value* s8 = kb.shared_alloc_f32(8); |
| 242 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
Value* wg = c.i32(static_cast<int>(jit::gpu_mir::kEwBlock)); |
| 243 |
|
|
|
| 244 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
Value* mx_local = emit_if_else_values( |
| 245 |
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 |
c, c.b.build_ult(row, p[3]), {Type::f32()}, |
| 246 |
|
14 |
[&]() -> std::vector<Value*> { |
| 247 |
|
7 |
Value* base = c.b.build_mul(row, p[4]); |
| 248 |
3/6
✓ 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.
|
14 |
return {kb.for_range_reduce( |
| 249 |
|
7 |
tid, p[4], wg, c.f32(0.0f), |
| 250 |
|
14 |
[&](Value* col, Value* cur) -> Value* { |
| 251 |
|
7 |
return c.kb.fmax(cur, c.kb.fabs(load_elem_dt(c, dt, p[0], c.b.build_add(base, col)))); |
| 252 |
|
|
})}; |
| 253 |
|
✗ |
}, |
| 254 |
|
14 |
[&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; |
| 255 |
|
|
|
| 256 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
Value* mx = emit_block_max_f32(c, s8, mx_local); |
| 257 |
5/10
✓ 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.
✓ Branch 8 taken 7 times.
✗ Branch 9 not taken.
|
7 |
Value* zero_mask = c.b.build_select(c.b.build_eq(p[4], c.i32(0)), c.i32(1), c.i32(0)); |
| 258 |
3/6
✓ 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.
|
14 |
kb.if_then(c.b.build_ult(row, p[3]), [&] { |
| 259 |
|
7 |
Value* base = c.b.build_mul(row, p[4]); |
| 260 |
|
7 |
Value* scale = div_cr_f32(c, mx, c.f32(fp8 ? 448.0f : 127.0f), zero_mask); |
| 261 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
14 |
kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] { |
| 262 |
|
7 |
kb.store_f32(ptr_at(c, p[2], row, 4), scale); |
| 263 |
|
7 |
}); |
| 264 |
|
7 |
Value* pos_sc = c.b.build_sgt(scale, c.f32(0.0f)); |
| 265 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
14 |
kb.for_range(tid, p[4], wg, [&](Value* col) { |
| 266 |
|
7 |
Value* idx = c.b.build_add(base, col); |
| 267 |
|
7 |
Value* wv = load_elem_dt(c, dt, p[0], idx); |
| 268 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
Value* qv = emit_if_else_values( |
| 269 |
|
7 |
c, pos_sc, {Type::f32()}, |
| 270 |
|
14 |
[&]() -> std::vector<Value*> { return {div_cr_f32(c, wv, scale, zero_mask)}; }, |
| 271 |
|
14 |
[&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; |
| 272 |
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
|
7 |
Value* byte = fp8 ? fp8_encode_f32(c, qv) |
| 273 |
|
6 |
: c.b.build_and(kb.f32_to_i32(c.kb.clamp_f32(emit_round_even(c, qv), |
| 274 |
|
2 |
c.f32(-127.0f), c.f32(127.0f))), |
| 275 |
|
2 |
c.i32(0xff)); |
| 276 |
|
7 |
kb.store_u8(ptr_at(c, p[1], idx, 1), byte); |
| 277 |
|
7 |
}); |
| 278 |
|
7 |
}); |
| 279 |
|
|
|
| 280 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
c.b.build_ret_void(); |
| 281 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
return finalize_fn(fn); |
| 282 |
|
13 |
} |
| 283 |
|
|
|
| 284 |
|
4 |
brass::Function* build_dequant_mir(brass::Module& mod, std::uint32_t fmt, bool vec) { |
| 285 |
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 |
const std::string name = std::string("mir_dequant_f") + std::to_string(fmt) + (vec ? "_v" : ""); |
| 286 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
std::vector<Type> params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32()}; |
| 287 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); |
| 288 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
brass::codegen::KernelBuilder kb(mod, fn); |
| 289 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
Ctx c(kb); |
| 290 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
auto p = entry_params(c, fn); |
| 291 |
|
|
// p[0]=w, p[1]=y, p[2]=scale, p[3]=rows, p[4]=k, p[5]=rowbytes |
| 292 |
|
|
|
| 293 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 |
if (!vec) { |
| 294 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
Value* total = c.b.build_mul(p[3], p[4]); |
| 295 |
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
✗ |
kb.for_range(kb.global_tid_x(), total, grid_stride_ew(c), [&](Value* i) { |
| 296 |
|
✗ |
Value* r = c.b.build_udiv(i, p[4]); |
| 297 |
|
✗ |
Value* col = c.b.build_umod(i, p[4]); |
| 298 |
|
✗ |
Value* s = kb.load_f32(ptr_at(c, p[2], r, 4)); |
| 299 |
|
✗ |
Value* b = kb.load_u8(c.b.build_add(p[0], c.b.build_add(c.b.build_mul(c.b.build_zext_i64(r), |
| 300 |
|
✗ |
c.b.build_zext_i64(p[5])), |
| 301 |
|
✗ |
c.b.build_zext_i64(col)))); |
| 302 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
Value* dv = (fmt == QF_FP8) ? fp8_decode_u32(c, b) : s8_byte_to_f32(c, b); |
| 303 |
|
✗ |
store_elem_dt(c, Dtype::FP16, p[1], i, c.b.build_mul(dv, s)); |
| 304 |
|
✗ |
}); |
| 305 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
c.b.build_ret_void(); |
| 306 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
return finalize_fn(fn); |
| 307 |
|
|
} |
| 308 |
|
|
|
| 309 |
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 |
Value* per = c.b.build_lshr(p[4], c.i32(3)); |
| 310 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
Value* total = c.b.build_mul(p[3], per); |
| 311 |
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.
|
8 |
kb.for_range(kb.global_tid_x(), total, grid_stride_ew(c), [&](Value* i) { |
| 312 |
|
4 |
Value* r = c.b.build_udiv(i, per); |
| 313 |
|
4 |
Value* k = c.b.build_shl(c.b.build_umod(i, per), c.i32(3)); |
| 314 |
|
4 |
Value* row_ptr = c.b.build_add(p[0], c.b.build_mul(c.b.build_zext_i64(r), c.b.build_zext_i64(p[5]))); |
| 315 |
|
4 |
std::vector<Value*> w(8, c.f32(0.0f)); |
| 316 |
|
|
|
| 317 |
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 |
if (fmt == QF_INT8 || fmt == QF_FP8) { |
| 318 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
Value* q64 = kb.load_i64(c.b.build_add(row_ptr, c.b.build_zext_i64(k))); |
| 319 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
Value* qlo = c.b.build_trunc_i32(q64); |
| 320 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
Value* qhi = c.b.build_trunc_i32(c.b.build_lshr(q64, kb.const_i64(32))); |
| 321 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
Value* s = kb.load_f32(ptr_at(c, p[2], r, 4)); |
| 322 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
for (int e = 0; e < 8; ++e) { |
| 323 |
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
✗ |
Value* b = c.b.build_and(c.b.build_lshr(e < 4 ? qlo : qhi, c.i32((e & 3) * 8)), c.i32(0xff)); |
| 324 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
Value* dv = (fmt == QF_FP8) ? fp8_decode_u32(c, b) : s8_byte_to_f32(c, b); |
| 325 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
w[static_cast<std::size_t>(e)] = c.b.build_mul(dv, s); |
| 326 |
|
✗ |
} |
| 327 |
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
|
4 |
} else if (fmt == QF_Q8_0) { |
| 328 |
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
2 |
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))), |
| 329 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
kb.const_i64(34))); |
| 330 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
Value* d = kb.f16_to_f32(kb.load_u16(blk)); |
| 331 |
|
|
// 34-byte blocks are 2-byte aligned; load four u16s instead of an unaligned i64. |
| 332 |
6/12
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
|
1 |
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))))); |
| 333 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
2 |
Value* qlo = c.b.build_or(kb.load_u16(qp), |
| 334 |
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
1 |
c.b.build_shl(kb.load_u16(c.b.build_add(qp, kb.const_i64(2))), c.i32(16))); |
| 335 |
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
|
2 |
Value* qhi = c.b.build_or(kb.load_u16(c.b.build_add(qp, kb.const_i64(4))), |
| 336 |
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
1 |
c.b.build_shl(kb.load_u16(c.b.build_add(qp, kb.const_i64(6))), c.i32(16))); |
| 337 |
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 time.
|
9 |
for (int e = 0; e < 8; ++e) { |
| 338 |
6/10
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ 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.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
|
8 |
Value* b = c.b.build_and(c.b.build_lshr(e < 4 ? qlo : qhi, c.i32((e & 3) * 8)), c.i32(0xff)); |
| 339 |
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 |
w[static_cast<std::size_t>(e)] = c.b.build_mul(s8_byte_to_f32(c, b), d); |
| 340 |
|
8 |
} |
| 341 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
|
4 |
} else if (fmt == QF_Q4K) { |
| 342 |
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* blk = c.b.build_add(row_ptr, c.b.build_mul(c.b.build_zext_i64(c.b.build_lshr(k, c.i32(8))), |
| 343 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
kb.const_i64(144))); |
| 344 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
Value* hdr = kb.vload(Type::i32x4(), blk); |
| 345 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
Value* a0 = c.b.build_vextract_lane(hdr, 0); |
| 346 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
Value* s0 = c.b.build_vextract_lane(hdr, 1); |
| 347 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
Value* s1 = c.b.build_vextract_lane(hdr, 2); |
| 348 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
Value* s2 = c.b.build_vextract_lane(hdr, 3); |
| 349 |
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* j = c.b.build_lshr(c.b.build_and(k, c.i32(255)), c.i32(5)); |
| 350 |
7/14
✓ 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.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
|
4 |
Value* b_off = 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)), |
| 351 |
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(k, c.i32(31)))); |
| 352 |
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 |
Value* nib64 = kb.load_i64(c.b.build_add(blk, c.b.build_zext_i64(b_off))); |
| 353 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 |
Value* blo = c.b.build_trunc_i32(nib64); |
| 354 |
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 |
Value* bhi = c.b.build_trunc_i32(c.b.build_lshr(nib64, kb.const_i64(32))); |
| 355 |
|
|
|
| 356 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 |
Value* low_j = c.b.build_ult(j, c.i32(4)); |
| 357 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 |
Value* j4 = c.b.build_add(j, c.i32(4)); |
| 358 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 |
Value* jm4 = c.b.build_sub(j, c.i32(4)); |
| 359 |
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* bs_j = extract_byte(c, sel_word3(c, c.b.build_lshr(j, c.i32(2)), s0, s1, s2), j); |
| 360 |
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* bs_j4 = extract_byte(c, sel_word3(c, c.b.build_lshr(j4, c.i32(2)), s0, s1, s2), j4); |
| 361 |
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* bs_jm4 = extract_byte(c, sel_word3(c, c.b.build_lshr(jm4, c.i32(2)), s0, s1, s2), jm4); |
| 362 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
4 |
Value* sc = c.b.build_select( |
| 363 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 |
low_j, c.b.build_and(bs_j, c.i32(63)), |
| 364 |
7/14
✓ 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.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
|
2 |
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)))); |
| 365 |
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
4 |
Value* mn = c.b.build_select( |
| 366 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 |
low_j, c.b.build_and(bs_j4, c.i32(63)), |
| 367 |
7/14
✓ 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.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
|
2 |
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)))); |
| 368 |
|
|
|
| 369 |
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 |
Value* d = kb.f16_to_f32(c.b.build_and(a0, c.i32(0xffff))); |
| 370 |
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 |
Value* dmin = kb.f16_to_f32(c.b.build_lshr(a0, c.i32(16))); |
| 371 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 |
Value* ws = c.b.build_mul(kb.u32_to_f32(sc), d); |
| 372 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 |
Value* wm = c.b.build_mul(kb.u32_to_f32(mn), dmin); |
| 373 |
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* sh = c.b.build_shl(c.b.build_and(j, c.i32(1)), c.i32(2)); |
| 374 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
|
18 |
for (int e = 0; e < 8; ++e) { |
| 375 |
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 |
Value* bw = e < 4 ? blo : bhi; |
| 376 |
5/10
✓ 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.
✓ Branch 6 taken 16 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 16 times.
✗ Branch 9 not taken.
|
16 |
Value* q4 = c.b.build_and(c.b.build_lshr(bw, c.b.build_add(sh, c.i32((e & 3) * 8))), c.i32(0x0f)); |
| 377 |
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 |
w[static_cast<std::size_t>(e)] = c.b.build_fma_f32(ws, kb.u32_to_f32(q4), c.b.build_neg(wm)); |
| 378 |
|
16 |
} |
| 379 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
3 |
} else if (fmt == QF_Q6K) { |
| 380 |
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
2 |
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))), |
| 381 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
kb.const_i64(210))); |
| 382 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
Value* e0 = c.b.build_and(k, c.i32(255)); |
| 383 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
Value* g = c.b.build_lshr(e0, c.i32(7)); |
| 384 |
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
|
1 |
Value* quad = c.b.build_lshr(c.b.build_and(e0, c.i32(127)), c.i32(5)); |
| 385 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
Value* l0 = c.b.build_and(e0, c.i32(31)); |
| 386 |
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
|
2 |
Value* ql_off = c.b.build_add(c.b.build_add(c.b.build_shl(g, c.i32(6)), |
| 387 |
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
|
1 |
c.b.build_shl(c.b.build_and(quad, c.i32(1)), c.i32(5))), |
| 388 |
|
1 |
l0); |
| 389 |
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
1 |
Value* qh_off = c.b.build_add(c.i32(128), c.b.build_add(c.b.build_shl(g, c.i32(5)), l0)); |
| 390 |
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
2 |
Value* sc_off = c.b.build_add(c.i32(192), c.b.build_add(c.b.build_shl(g, c.i32(3)), |
| 391 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
c.b.build_shl(quad, c.i32(1)))); |
| 392 |
|
|
// 210-byte blocks are 2-byte aligned; load four u16s per 8-byte slice. |
| 393 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
Value* qlp = c.b.build_add(blk, c.b.build_zext_i64(ql_off)); |
| 394 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
Value* qhp = c.b.build_add(blk, c.b.build_zext_i64(qh_off)); |
| 395 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
2 |
Value* qlo = c.b.build_or(kb.load_u16(qlp), |
| 396 |
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
1 |
c.b.build_shl(kb.load_u16(c.b.build_add(qlp, kb.const_i64(2))), c.i32(16))); |
| 397 |
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
|
2 |
Value* qhi = c.b.build_or(kb.load_u16(c.b.build_add(qlp, kb.const_i64(4))), |
| 398 |
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
1 |
c.b.build_shl(kb.load_u16(c.b.build_add(qlp, kb.const_i64(6))), c.i32(16))); |
| 399 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
2 |
Value* hlo = c.b.build_or(kb.load_u16(qhp), |
| 400 |
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
1 |
c.b.build_shl(kb.load_u16(c.b.build_add(qhp, kb.const_i64(2))), c.i32(16))); |
| 401 |
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
|
2 |
Value* hhi = c.b.build_or(kb.load_u16(c.b.build_add(qhp, kb.const_i64(4))), |
| 402 |
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
1 |
c.b.build_shl(kb.load_u16(c.b.build_add(qhp, kb.const_i64(6))), c.i32(16))); |
| 403 |
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* bx = kb.load_u16(c.b.build_add(blk, c.b.build_zext_i64(sc_off))); |
| 404 |
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
|
1 |
Value* d = kb.f16_to_f32(kb.load_u16(c.b.build_add(blk, kb.const_i64(208)))); |
| 405 |
6/12
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
|
2 |
Value* sbyte = c.b.build_and(c.b.build_lshr(bx, c.b.build_shl(c.b.build_lshr(l0, c.i32(4)), c.i32(3))), |
| 406 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
c.i32(0xff)); |
| 407 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
Value* s = c.b.build_mul(d, s8_byte_to_f32(c, sbyte)); |
| 408 |
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
1 |
Value* ql_sh = c.b.build_select(c.b.build_uge(quad, c.i32(2)), c.i32(4), c.i32(0)); |
| 409 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
Value* qh_sh = c.b.build_shl(quad, c.i32(1)); |
| 410 |
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
|
9 |
for (int e = 0; e < 8; ++e) { |
| 411 |
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 |
Value* bw = e < 4 ? qlo : qhi; |
| 412 |
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 |
Value* hw = e < 4 ? hlo : hhi; |
| 413 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
Value* bsh = c.i32((e & 3) * 8); |
| 414 |
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* nib = c.b.build_and(c.b.build_lshr(c.b.build_lshr(bw, bsh), ql_sh), c.i32(0x0f)); |
| 415 |
5/10
✓ 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.
✓ Branch 8 taken 8 times.
✗ Branch 9 not taken.
|
16 |
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)), |
| 416 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
c.i32(4)); |
| 417 |
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* q6 = c.b.build_sub(c.kb.u32_to_f32(c.b.build_or(nib, hb)), c.f32(32.0f)); |
| 418 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
w[static_cast<std::size_t>(e)] = c.b.build_mul(s, q6); |
| 419 |
|
8 |
} |
| 420 |
|
1 |
} else { |
| 421 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
throw std::runtime_error("brotensor: mir_dequant: unknown fmt"); |
| 422 |
|
|
} |
| 423 |
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 |
store_lanes(c, Dtype::FP16, 8, ptr_at(c, p[1], i, 16), w); |
| 424 |
|
4 |
}); |
| 425 |
|
|
|
| 426 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
c.b.build_ret_void(); |
| 427 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
return finalize_fn(fn); |
| 428 |
|
4 |
} |
| 429 |
|
|
|
| 430 |
|
|
} // namespace brotensor::kernels |
| 431 |
|
|
|