| Line |
Branch |
Exec |
Source |
| 1 |
|
|
#include "mir_fa_rows.h" |
| 2 |
|
|
|
| 3 |
|
|
#include "mir_common.h" |
| 4 |
|
|
#include "mir_math.h" |
| 5 |
|
|
#include "../vulkan/shaders/op_codes.h" |
| 6 |
|
|
|
| 7 |
|
|
#include <stdexcept> |
| 8 |
|
|
#include <string> |
| 9 |
|
|
#include <utility> |
| 10 |
|
|
#include <vector> |
| 11 |
|
|
|
| 12 |
|
|
namespace brotensor::kernels { |
| 13 |
|
|
|
| 14 |
|
|
namespace { |
| 15 |
|
|
|
| 16 |
|
|
constexpr float kLog2e = 1.44269504088896340736f; |
| 17 |
|
|
|
| 18 |
|
76 |
std::pair<Value*, Value*> emit_fa_interval(Ctx& c, std::uint32_t mode, |
| 19 |
|
|
Value* qi, Value* lk, Value* causal, Value* window, |
| 20 |
|
|
Value* q_offset, Value* lq, Value* nseq, Value* aux, Value* aux2) { |
| 21 |
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 13 times.
|
76 |
if (mode == FA_MODE_VARLEN) { |
| 22 |
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 |
auto st = c.kb.for_range_reduce_n( |
| 23 |
|
13 |
c.i32(0), c.i32(24), c.i32(1), {c.i32(0), nseq}, |
| 24 |
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
26 |
[&](Value* /*step*/, const std::vector<Value*>& cur) -> std::vector<Value*> { |
| 25 |
|
13 |
Value* slo = cur[0]; |
| 26 |
|
13 |
Value* shi = cur[1]; |
| 27 |
|
13 |
Value* active = c.b.build_slt(slo, shi); |
| 28 |
|
13 |
Value* diff = c.b.build_sub(shi, slo); |
| 29 |
|
13 |
Value* mid = c.b.build_add(slo, c.b.build_ashr(diff, c.i32(1))); |
| 30 |
|
13 |
Value* safe_mid = c.b.build_select(active, mid, c.i32(0)); |
| 31 |
|
13 |
Value* end_q = c.kb.load_i32(ptr_at(c, aux, c.b.build_add(safe_mid, c.i32(1)), 4)); |
| 32 |
|
13 |
Value* go_right = c.b.build_sle(end_q, qi); |
| 33 |
|
13 |
Value* nlo = c.b.build_select(c.b.build_and(active, go_right), c.b.build_add(safe_mid, c.i32(1)), slo); |
| 34 |
|
13 |
Value* nhi = c.b.build_select(c.b.build_and(active, c.b.build_not(go_right)), safe_mid, shi); |
| 35 |
|
13 |
return {nlo, nhi}; |
| 36 |
|
|
}); |
| 37 |
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 |
Value* has_seq = c.b.build_sgt(nseq, c.i32(0)); |
| 38 |
6/12
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 13 times.
✗ Branch 11 not taken.
|
13 |
Value* b = c.b.build_select(has_seq, clamp_i32(c, st[0], c.i32(0), c.b.build_sub(nseq, c.i32(1))), c.i32(0)); |
| 39 |
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 |
Value* qs = c.kb.load_i32(ptr_at(c, aux, b, 4)); |
| 40 |
4/8
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
|
13 |
Value* qe = c.kb.load_i32(ptr_at(c, aux, c.b.build_add(b, c.i32(1)), 4)); |
| 41 |
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 |
Value* ks = c.kb.load_i32(ptr_at(c, aux2, b, 4)); |
| 42 |
4/8
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
|
13 |
Value* ke = c.kb.load_i32(ptr_at(c, aux2, c.b.build_add(b, c.i32(1)), 4)); |
| 43 |
4/8
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
|
13 |
Value* in_seq = c.b.build_and(has_seq, c.b.build_and(c.b.build_sle(qs, qi), c.b.build_slt(qi, qe))); |
| 44 |
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 |
Value* lo = smax_i32(c, c.i32(0), ks); |
| 45 |
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 |
Value* hi_seq = smin_i32(c, lk, ke); |
| 46 |
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 |
Value* is_c = c.b.build_ne(causal, c.i32(0)); |
| 47 |
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 |
Value* aq = c.b.build_add(ks, c.b.build_sub(qi, qs)); |
| 48 |
4/8
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
|
13 |
Value* hi = c.b.build_select(is_c, smin_i32(c, hi_seq, c.b.build_add(aq, c.i32(1))), hi_seq); |
| 49 |
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 |
Value* has_win = c.b.build_sgt(window, c.i32(0)); |
| 50 |
4/8
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
|
13 |
Value* wlo_c = smax_i32(c, lo, c.b.build_add(c.b.build_sub(aq, window), c.i32(1))); |
| 51 |
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 |
Value* half_w = c.b.build_ashr(window, c.i32(1)); |
| 52 |
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 |
Value* wlo_b = smax_i32(c, lo, c.b.build_sub(aq, half_w)); |
| 53 |
4/8
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
|
13 |
Value* whi_b = smin_i32(c, hi, c.b.build_add(c.b.build_add(aq, half_w), c.i32(1))); |
| 54 |
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 |
lo = c.b.build_select(has_win, c.b.build_select(is_c, wlo_c, wlo_b), lo); |
| 55 |
3/6
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 |
hi = c.b.build_select(c.b.build_and(has_win, c.b.build_not(is_c)), whi_b, hi); |
| 56 |
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13 |
lo = c.b.build_select(in_seq, lo, c.i32(0)); |
| 57 |
3/6
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
13 |
hi = c.b.build_select(in_seq, smax_i32(c, lo, hi), c.i32(0)); |
| 58 |
|
13 |
return {lo, hi}; |
| 59 |
|
13 |
} |
| 60 |
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 54 times.
|
63 |
if (mode == FA_MODE_PACKED) { |
| 61 |
|
9 |
Value* s0 = c.kb.load_i32(ptr_at(c, aux, c.b.build_shl(qi, c.i32(1)), 4)); |
| 62 |
|
9 |
Value* s1 = c.kb.load_i32(ptr_at(c, aux, c.b.build_add(c.b.build_shl(qi, c.i32(1)), c.i32(1)), 4)); |
| 63 |
|
9 |
Value* lo = smax_i32(c, c.i32(0), s0); |
| 64 |
|
9 |
Value* hi = smin_i32(c, lk, s1); |
| 65 |
|
9 |
Value* has_win = c.b.build_sgt(window, c.i32(0)); |
| 66 |
|
9 |
Value* hw = c.b.build_ashr(window, c.i32(1)); |
| 67 |
|
9 |
lo = c.b.build_select(has_win, smax_i32(c, lo, c.b.build_sub(qi, hw)), lo); |
| 68 |
|
9 |
hi = c.b.build_select(has_win, smin_i32(c, hi, c.b.build_add(c.b.build_add(qi, hw), c.i32(1))), hi); |
| 69 |
|
9 |
return {lo, smax_i32(c, lo, hi)}; |
| 70 |
|
|
} |
| 71 |
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 48 times.
|
54 |
if (mode == FA_MODE_LEN) { |
| 72 |
|
6 |
Value* dlen = clamp_i32(c, c.kb.load_i32(aux), c.i32(0), lk); |
| 73 |
|
6 |
Value* pq = c.b.build_add(c.b.build_sub(dlen, lq), qi); |
| 74 |
|
6 |
Value* hi = clamp_i32(c, c.b.build_add(pq, c.i32(1)), c.i32(0), lk); |
| 75 |
|
12 |
Value* lo = c.b.build_select( |
| 76 |
|
6 |
c.b.build_sgt(window, c.i32(0)), |
| 77 |
|
6 |
smax_i32(c, c.i32(0), c.b.build_add(c.b.build_sub(pq, window), c.i32(1))), c.i32(0)); |
| 78 |
|
6 |
return {lo, smax_i32(c, lo, hi)}; |
| 79 |
|
|
} |
| 80 |
|
|
// FA_MODE_ROWS |
| 81 |
|
48 |
Value* pq = c.b.build_add(q_offset, qi); |
| 82 |
|
48 |
Value* is_c = c.b.build_ne(causal, c.i32(0)); |
| 83 |
|
48 |
Value* has_win = c.b.build_sgt(window, c.i32(0)); |
| 84 |
|
48 |
Value* hw = c.b.build_ashr(window, c.i32(1)); |
| 85 |
|
48 |
Value* hi_c = clamp_i32(c, c.b.build_add(pq, c.i32(1)), c.i32(0), lk); |
| 86 |
|
48 |
Value* hi_nc = c.b.build_select(has_win, clamp_i32(c, c.b.build_add(c.b.build_add(pq, hw), c.i32(1)), c.i32(0), lk), lk); |
| 87 |
|
48 |
Value* hi = c.b.build_select(is_c, hi_c, hi_nc); |
| 88 |
|
48 |
Value* lo_c = c.b.build_select(has_win, smax_i32(c, c.i32(0), c.b.build_add(c.b.build_sub(pq, window), c.i32(1))), c.i32(0)); |
| 89 |
|
48 |
Value* lo_nc = c.b.build_select(has_win, smax_i32(c, c.i32(0), c.b.build_sub(pq, hw)), c.i32(0)); |
| 90 |
|
48 |
Value* lo = c.b.build_select(is_c, lo_c, lo_nc); |
| 91 |
|
48 |
return {lo, smax_i32(c, lo, hi)}; |
| 92 |
|
76 |
} |
| 93 |
|
|
|
| 94 |
|
|
} // namespace |
| 95 |
|
|
|
| 96 |
|
83 |
brass::Function* build_fa_rows_mir(brass::Module& mod, Dtype dt, std::uint32_t hd, std::uint32_t gqa_g, |
| 97 |
|
|
std::uint32_t mode, bool has_mask, bool split, |
| 98 |
|
|
bool has_softcap, bool vec, bool has_maskpos) { |
| 99 |
5/6
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
|
83 |
if (dt != Dtype::FP32 && dt != Dtype::FP16 && dt != Dtype::BF16) { |
| 100 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
throw std::runtime_error("brotensor: mir_fa_rows: unsupported dtype"); |
| 101 |
|
|
} |
| 102 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
if (hd == 0 || hd > 8192u || gqa_g == 0 || gqa_g > 16u) { |
| 103 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
throw std::runtime_error("brotensor: mir_fa_rows: bad head_dim or G"); |
| 104 |
|
|
} |
| 105 |
|
83 |
const std::uint32_t hds = (hd + 127u) / 128u; |
| 106 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
if (gqa_g * hds > 16u || gqa_g * hd > 8192u) { |
| 107 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
throw std::runtime_error("brotensor: mir_fa_rows: G * HDS / shared limit exceeded"); |
| 108 |
|
|
} |
| 109 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
249 |
const std::string name = std::string("mir_fa_rows_") + |
| 110 |
3/4
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
|
83 |
(dt == Dtype::FP32 ? "f32" : dt == Dtype::FP16 ? "f16" : "bf16") + |
| 111 |
6/12
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 83 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 83 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 83 times.
✗ Branch 11 not taken.
|
83 |
"_hd" + std::to_string(hd) + "_g" + std::to_string(gqa_g) + |
| 112 |
4/8
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 83 times.
✗ Branch 7 not taken.
|
83 |
"_m" + std::to_string(mode) + (has_mask ? "_msk" : "") + |
| 113 |
2/4
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
|
83 |
(split ? "_sp" : "") + (has_softcap ? "_sc" : "") + |
| 114 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
(vec ? "_v" : "") + (has_maskpos ? "_mp" : ""); |
| 115 |
|
|
|
| 116 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
std::vector<Type> params = { |
| 117 |
|
|
Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), |
| 118 |
|
|
Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), |
| 119 |
|
|
Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), |
| 120 |
|
|
Type::f32(), Type::f32(), |
| 121 |
|
|
}; |
| 122 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); |
| 123 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
brass::codegen::KernelBuilder kb(mod, fn); |
| 124 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
Ctx c(kb); |
| 125 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
auto p = entry_params(c, fn); |
| 126 |
|
|
// ptrs: 0:q 1:k 2:v 3:o 4:mask 5:aux 6:aux2 7:part |
| 127 |
|
|
// ints: 8:lq 9:lk 10:ldq 11:ldk 12:ldo 13:group 14:window 15:causal 16:nseq 17:chunk 18:hq 19:qb0 20:q_offset |
| 128 |
|
|
// flts: 21:scale 22:softcap |
| 129 |
|
|
|
| 130 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
const int esz = elem_bytes(dt); |
| 131 |
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 75 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 50 times.
|
158 |
const bool vec_qk_multi = vec && (hd == 64u || hd == 128u); |
| 132 |
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 49 times.
|
83 |
const std::uint32_t qk_kpw = !vec_qk_multi ? 1u : (hd == 64u ? 4u : 2u); |
| 133 |
|
83 |
const std::uint32_t qk_lanes_per_k = 32u / qk_kpw; // 8 (hd=64), 16 (hd=128), 32 (fallback) |
| 134 |
6/6
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 25 times.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 41 times.
|
83 |
const bool vec_pv = vec && (hd == 64u || hd == 128u) && gqa_g <= 4u; |
| 135 |
|
83 |
const std::uint32_t pv_cols_v4 = hd / 8u; // 8 (hd=64) or 16 (hd=128) |
| 136 |
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 51 times.
|
83 |
const std::uint32_t pv_k_step = vec_pv ? (128u / pv_cols_v4) : 1u; // 16 or 8 |
| 137 |
|
|
|
| 138 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
Value* sQ = kb.shared_alloc_f32(gqa_g * hd); |
| 139 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
Value* sS = kb.shared_alloc_f32(gqa_g * 128u); |
| 140 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
Value* s_valid = kb.shared_alloc_i32(128u); |
| 141 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
Value* s_red = kb.shared_alloc_f32(2u * gqa_g * 4u); |
| 142 |
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
|
83 |
Value* sO = vec_pv ? kb.shared_alloc_f32(pv_k_step * hd) : nullptr; |
| 143 |
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
83 |
Value* s_mp = has_maskpos ? kb.shared_alloc_i32(4u) : nullptr; |
| 144 |
|
|
|
| 145 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
Value* tid = kb.tid_x(); |
| 146 |
2/4
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
|
83 |
Value* warp = c.b.build_lshr(tid, c.i32(5)); |
| 147 |
2/4
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
|
83 |
Value* lane = c.b.build_and(tid, c.i32(31)); |
| 148 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
Value* sp = kb.ctaid_x(); |
| 149 |
2/4
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
|
83 |
Value* qi = c.b.build_add(p[19], kb.ctaid_y()); |
| 150 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
Value* hg = kb.ctaid_z(); |
| 151 |
2/4
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
|
83 |
Value* qh0 = c.b.build_mul(hg, c.i32(static_cast<int>(gqa_g))); |
| 152 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
Value* hkv = c.b.build_udiv(qh0, p[13]); |
| 153 |
|
|
|
| 154 |
|
|
// Load Q * scale -> sQ[g, d] |
| 155 |
4/8
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 83 times.
✗ Branch 7 not taken.
|
83 |
Value* q_row_base = c.b.build_add(c.b.build_mul(qi, p[10]), c.b.build_mul(qh0, c.i32(static_cast<int>(hd)))); |
| 156 |
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 86 times.
|
169 |
for (std::uint32_t sl = 0; sl < hds; ++sl) { |
| 157 |
2/4
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
|
86 |
Value* d = c.b.build_add(c.i32(static_cast<int>(sl * 128u)), tid); |
| 158 |
|
172 |
auto load_q_slot = [&] { |
| 159 |
2/2
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 86 times.
|
238 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 160 |
|
152 |
Value* qidx = c.b.build_add(c.b.build_add(q_row_base, c.i32(static_cast<int>(g * hd))), d); |
| 161 |
|
152 |
Value* qv = c.b.build_mul(load_elem_dt(c, dt, p[0], qidx), p[21]); |
| 162 |
|
152 |
kb.shared_store_f32_indexed(sQ, c.b.build_add(c.i32(static_cast<int>(g * hd)), d), qv); |
| 163 |
|
152 |
} |
| 164 |
|
86 |
}; |
| 165 |
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
|
86 |
if ((sl + 1u) * 128u <= hd) load_q_slot(); |
| 166 |
4/8
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 72 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 72 times.
✗ Branch 7 not taken.
|
72 |
else kb.if_then(c.b.build_ult(d, c.i32(static_cast<int>(hd))), load_q_slot); |
| 167 |
|
86 |
} |
| 168 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
kb.sync(); |
| 169 |
|
|
|
| 170 |
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 76 times.
|
166 |
auto [iv_lo, iv_hi] = has_maskpos |
| 171 |
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 |
? std::pair<Value*, Value*>{c.i32(0), p[9]} |
| 172 |
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
|
76 |
: emit_fa_interval(c, mode, qi, p[9], p[15], p[14], p[20], p[8], p[16], p[5], p[6]); |
| 173 |
|
83 |
Value* lo = iv_lo; |
| 174 |
|
83 |
Value* hi = iv_hi; |
| 175 |
|
|
|
| 176 |
4/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 time.
|
83 |
if (has_maskpos && has_mask) { |
| 177 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
Value* best = kb.for_range_reduce( |
| 178 |
3/6
✓ 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.
|
6 |
c.b.build_add(lo, tid), hi, c.i32(128), c.i32(-1), |
| 179 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
12 |
[&](Value* kk, Value* cur) -> Value* { |
| 180 |
|
6 |
Value* mv = kb.load_f32(ptr_at(c, p[4], kk, 4)); |
| 181 |
|
6 |
return c.b.build_select(c.b.build_slt(c.f32(0.5f), mv), smax_i32(c, cur, kk), cur); |
| 182 |
|
|
}); |
| 183 |
4/6
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
|
36 |
for (std::uint32_t m = 16; m >= 1; m >>= 1) best = smax_i32(c, best, kb.shfl_bfly_i32(best, m)); |
| 184 |
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.if_then(c.b.build_eq(lane, c.i32(0)), [&] { kb.shared_store_i32_indexed(s_mp, warp, best); }); |
| 185 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
kb.sync(); |
| 186 |
5/10
✓ 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.
✓ Branch 8 taken 6 times.
✗ Branch 9 not taken.
|
6 |
Value* b01 = smax_i32(c, kb.shared_load_i32_indexed(s_mp, c.i32(0)), kb.shared_load_i32_indexed(s_mp, c.i32(1))); |
| 187 |
5/10
✓ 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.
✓ Branch 8 taken 6 times.
✗ Branch 9 not taken.
|
6 |
Value* b23 = smax_i32(c, kb.shared_load_i32_indexed(s_mp, c.i32(2)), kb.shared_load_i32_indexed(s_mp, c.i32(3))); |
| 188 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
Value* last_k = smax_i32(c, b01, b23); |
| 189 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
kb.sync(); |
| 190 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 |
Value* has_pos = c.b.build_sge(last_k, c.i32(0)); |
| 191 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
12 |
lo = c.b.build_select( |
| 192 |
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.
|
6 |
has_pos, smax_i32(c, lo, c.b.build_add(c.b.build_sub(last_k, p[14]), c.i32(1))), lo); |
| 193 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
hi = c.b.build_select(has_pos, hi, lo); |
| 194 |
|
6 |
} |
| 195 |
|
|
|
| 196 |
|
83 |
Value* tile_lo = lo; |
| 197 |
|
83 |
Value* tile_hi = hi; |
| 198 |
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 64 times.
|
83 |
if (split) { |
| 199 |
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 |
Value* sp_lo = c.b.build_mul(sp, p[17]); |
| 200 |
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 |
Value* sp_hi = c.b.build_add(sp_lo, p[17]); |
| 201 |
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 |
tile_lo = smax_i32(c, lo, sp_lo); |
| 202 |
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
|
19 |
tile_hi = smax_i32(c, tile_lo, smin_i32(c, hi, sp_hi)); |
| 203 |
|
19 |
} |
| 204 |
|
|
|
| 205 |
|
|
// State vector: |
| 206 |
|
|
// When vec_pv: [0 .. 8*G-1] = o_vec8, else [0 .. G*HDS-1] = o_reg |
| 207 |
|
|
// then [n_o .. n_o+G-1] = m, [n_o+G .. n_o+2G-1] = l |
| 208 |
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 51 times.
|
83 |
const std::size_t n_o = vec_pv ? (gqa_g * 8u) : (gqa_g * hds); |
| 209 |
|
83 |
const std::size_t m_off = n_o; |
| 210 |
|
83 |
const std::size_t l_off = n_o + gqa_g; |
| 211 |
2/4
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
|
83 |
std::vector<Value*> init(n_o + 2u * gqa_g, c.f32(0.0f)); |
| 212 |
3/4
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 148 times.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
|
231 |
for (std::uint32_t g = 0; g < gqa_g; ++g) init[m_off + g] = c.f32(-1e30f); |
| 213 |
|
|
|
| 214 |
2/4
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
|
83 |
Value* kv_head_off = c.b.build_mul(hkv, c.i32(static_cast<int>(hd))); |
| 215 |
4/6
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
|
83 |
Value* softcap_inv = has_softcap ? emit_div_rn(c, c.f32(1.0f), p[22]) : nullptr; |
| 216 |
4/6
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
|
83 |
Value* pv_kslot = vec_pv ? c.b.build_udiv(tid, c.i32(static_cast<int>(pv_cols_v4))) : nullptr; |
| 217 |
4/6
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
|
83 |
Value* pv_dv4 = vec_pv ? c.b.build_umod(tid, c.i32(static_cast<int>(pv_cols_v4))) : nullptr; |
| 218 |
|
|
|
| 219 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
std::vector<Value*> state = kb.for_range_reduce_n( |
| 220 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
tile_lo, tile_hi, c.i32(128), init, |
| 221 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
166 |
[&](Value* t0, const std::vector<Value*>& cur) -> std::vector<Value*> { |
| 222 |
|
83 |
Value* tile_n = smin_i32(c, c.i32(128), c.b.build_sub(tile_hi, t0)); |
| 223 |
|
83 |
Value* in_t = c.b.build_ult(tid, tile_n); |
| 224 |
|
83 |
Value* valid_k = in_t; |
| 225 |
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 21 times.
|
83 |
if (has_mask) { |
| 226 |
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21 |
Value* mv = emit_if_else_values( |
| 227 |
|
21 |
c, in_t, {Type::f32()}, |
| 228 |
|
42 |
[&]() -> std::vector<Value*> { return {kb.load_f32(ptr_at(c, p[4], c.b.build_add(t0, tid), 4))}; }, |
| 229 |
|
42 |
[&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; |
| 230 |
|
21 |
valid_k = c.b.build_and(in_t, c.b.build_slt(c.f32(0.5f), mv)); |
| 231 |
|
21 |
} |
| 232 |
|
83 |
kb.shared_store_i32_indexed(s_valid, tid, c.b.build_select(valid_k, c.i32(1), c.i32(0))); |
| 233 |
|
83 |
kb.sync(); |
| 234 |
|
|
|
| 235 |
|
|
// 1. Q * K^T scores: 4 warps x qk_kpw keys per warp (full 32-lane utilization when vec_qk_multi). |
| 236 |
|
83 |
const std::uint32_t qk_step = 4u * qk_kpw; |
| 237 |
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 49 times.
|
83 |
Value* qk_sub = vec_qk_multi |
| 238 |
|
34 |
? c.b.build_udiv(lane, c.i32(static_cast<int>(qk_lanes_per_k))) |
| 239 |
|
49 |
: c.i32(0); |
| 240 |
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 49 times.
|
83 |
Value* qk_lane = vec_qk_multi |
| 241 |
|
34 |
? c.b.build_and(lane, c.i32(static_cast<int>(qk_lanes_per_k - 1u))) |
| 242 |
|
49 |
: lane; |
| 243 |
|
83 |
Value* qk_warp_k = c.b.build_add(c.b.build_mul(warp, c.i32(static_cast<int>(qk_kpw))), qk_sub); |
| 244 |
|
|
|
| 245 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
166 |
kb.for_range(c.i32(0), tile_n, c.i32(static_cast<int>(qk_step)), [&](Value* ks_base) { |
| 246 |
|
83 |
Value* ks = c.b.build_add(ks_base, qk_warp_k); |
| 247 |
|
83 |
Value* in_ks = c.b.build_ult(ks, tile_n); |
| 248 |
|
83 |
Value* safe_ks = c.b.build_select(in_ks, c.b.build_and(ks, c.i32(127)), c.i32(0)); |
| 249 |
|
166 |
Value* k_ok = c.b.build_and( |
| 250 |
|
83 |
in_ks, c.b.build_ne(kb.shared_load_i32_indexed(s_valid, safe_ks), c.i32(0))); |
| 251 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
std::vector<Value*> dots = emit_if_else_values( |
| 252 |
|
83 |
c, k_ok, std::vector<Type>(gqa_g, Type::f32()), |
| 253 |
|
166 |
[&]() -> std::vector<Value*> { |
| 254 |
|
83 |
Value* kg = c.b.build_add(t0, safe_ks); |
| 255 |
|
83 |
Value* k_row = c.b.build_add(c.b.build_mul(kg, p[11]), kv_head_off); |
| 256 |
|
83 |
std::vector<Value*> d_init(gqa_g, c.f32(0.0f)); |
| 257 |
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 34 times.
|
83 |
if (vec_qk_multi) { |
| 258 |
2/4
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
|
34 |
Value* db = c.b.build_shl(qk_lane, c.i32(3)); |
| 259 |
2/4
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 34 times.
✗ Branch 3 not taken.
|
34 |
Value* kp = ptr_at(c, p[1], c.b.build_add(k_row, db), esz); |
| 260 |
|
34 |
std::vector<Value*> kv; |
| 261 |
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 30 times.
|
34 |
if (dt == Dtype::FP32) { |
| 262 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
kv = load_lanes(c, Dtype::FP32, 4, kp); |
| 263 |
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 |
auto kv2 = load_lanes(c, Dtype::FP32, 4, c.b.build_add(kp, kb.const_i64(16))); |
| 264 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
kv.insert(kv.end(), kv2.begin(), kv2.end()); |
| 265 |
|
4 |
} else { |
| 266 |
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 |
kv = load_lanes(c, dt, 8, kp); |
| 267 |
|
|
} |
| 268 |
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 34 times.
|
106 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 269 |
2/4
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
|
72 |
Value* s_off = c.b.build_add(c.i32(static_cast<int>(g * hd)), db); |
| 270 |
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 72 times.
|
648 |
for (int e = 0; e < 8; ++e) { |
| 271 |
3/6
✓ Branch 0 taken 576 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 576 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 576 times.
✗ Branch 5 not taken.
|
576 |
Value* qv = kb.shared_load_f32_indexed(sQ, c.b.build_add(s_off, c.i32(e))); |
| 272 |
1/2
✓ Branch 0 taken 576 times.
✗ Branch 1 not taken.
|
576 |
d_init[g] = c.b.build_fma_f32(qv, kv[e], d_init[g]); |
| 273 |
|
576 |
} |
| 274 |
|
72 |
} |
| 275 |
|
34 |
return d_init; |
| 276 |
|
34 |
} |
| 277 |
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 8 times.
|
49 |
if (vec) { |
| 278 |
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
82 |
return kb.for_range_reduce_n( |
| 279 |
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
|
41 |
qk_lane, c.i32(static_cast<int>(hd / 8u)), c.i32(32), d_init, |
| 280 |
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
82 |
[&](Value* dc_chunk, const std::vector<Value*>& dc) -> std::vector<Value*> { |
| 281 |
|
41 |
Value* db = c.b.build_shl(dc_chunk, c.i32(3)); |
| 282 |
|
41 |
std::vector<Value*> dn = dc; |
| 283 |
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
|
41 |
Value* kp = ptr_at(c, p[1], c.b.build_add(k_row, db), esz); |
| 284 |
|
41 |
std::vector<Value*> kv; |
| 285 |
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 29 times.
|
41 |
if (dt == Dtype::FP32) { |
| 286 |
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 |
kv = load_lanes(c, Dtype::FP32, 4, kp); |
| 287 |
3/6
✓ 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.
|
12 |
auto kv2 = load_lanes(c, Dtype::FP32, 4, c.b.build_add(kp, kb.const_i64(16))); |
| 288 |
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 |
kv.insert(kv.end(), kv2.begin(), kv2.end()); |
| 289 |
|
12 |
} else { |
| 290 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
kv = load_lanes(c, dt, 8, kp); |
| 291 |
|
|
} |
| 292 |
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 41 times.
|
107 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 293 |
2/4
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
|
66 |
Value* s_off = c.b.build_add(c.i32(static_cast<int>(g * hd)), db); |
| 294 |
2/2
✓ Branch 0 taken 528 times.
✓ Branch 1 taken 66 times.
|
594 |
for (int e = 0; e < 8; ++e) { |
| 295 |
3/6
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 528 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 528 times.
✗ Branch 5 not taken.
|
528 |
Value* qv = kb.shared_load_f32_indexed(sQ, c.b.build_add(s_off, c.i32(e))); |
| 296 |
1/2
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
|
528 |
dn[g] = c.b.build_fma_f32(qv, kv[e], dn[g]); |
| 297 |
|
528 |
} |
| 298 |
|
66 |
} |
| 299 |
|
41 |
return dn; |
| 300 |
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
|
41 |
}); |
| 301 |
|
|
} |
| 302 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
16 |
return kb.for_range_reduce_n( |
| 303 |
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 |
qk_lane, c.i32(static_cast<int>(hd)), c.i32(32), d_init, |
| 304 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
16 |
[&](Value* d, const std::vector<Value*>& dc) -> std::vector<Value*> { |
| 305 |
|
8 |
std::vector<Value*> dn = dc; |
| 306 |
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 |
Value* kv = load_elem_dt(c, dt, p[1], c.b.build_add(k_row, d)); |
| 307 |
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 8 times.
|
18 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 308 |
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
20 |
Value* qv = kb.shared_load_f32_indexed( |
| 309 |
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 |
sQ, c.b.build_add(c.i32(static_cast<int>(g * hd)), d)); |
| 310 |
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 |
dn[g] = c.b.build_fma_f32(qv, kv, dn[g]); |
| 311 |
|
10 |
} |
| 312 |
|
8 |
return dn; |
| 313 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
}); |
| 314 |
|
83 |
}, |
| 315 |
|
166 |
[&]() -> std::vector<Value*> { return std::vector<Value*>(gqa_g, c.f32(0.0f)); }); |
| 316 |
|
|
|
| 317 |
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 83 times.
|
231 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 318 |
|
148 |
Value* s = dots[g]; |
| 319 |
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 611 times.
|
759 |
for (std::uint32_t m = qk_lanes_per_k >> 1; m >= 1; m >>= 1) { |
| 320 |
2/4
✓ Branch 0 taken 611 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 611 times.
✗ Branch 3 not taken.
|
611 |
s = c.b.build_add(s, kb.shfl_bfly_f32(s, m)); |
| 321 |
|
611 |
} |
| 322 |
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 130 times.
|
148 |
if (has_softcap) { |
| 323 |
3/6
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
|
18 |
s = c.b.build_mul(p[22], emit_tanh(c, c.b.build_mul(s, softcap_inv))); |
| 324 |
|
18 |
} |
| 325 |
5/10
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 148 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 148 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 148 times.
✗ Branch 9 not taken.
|
296 |
kb.if_then(c.b.build_and(c.b.build_eq(qk_lane, c.i32(0)), in_ks), [&] { |
| 326 |
|
148 |
Value* sval = c.b.build_select(k_ok, s, c.f32(-1e30f)); |
| 327 |
|
148 |
kb.shared_store_f32_indexed(sS, c.b.build_add(c.i32(static_cast<int>(g * 128u)), safe_ks), sval); |
| 328 |
|
148 |
}); |
| 329 |
|
148 |
} |
| 330 |
|
83 |
}); |
| 331 |
|
83 |
kb.sync(); |
| 332 |
|
|
|
| 333 |
|
|
// 2. Batched per-head online softmax & store probabilities in sS (only 2 barriers for all G heads). |
| 334 |
|
83 |
Value* self_valid = c.b.build_ne(kb.shared_load_i32_indexed(s_valid, tid), c.i32(0)); |
| 335 |
|
83 |
std::vector<Value*> sc_v(gqa_g); |
| 336 |
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 148 times.
|
231 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 337 |
2/4
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
|
148 |
Value* sidx = c.b.build_add(c.i32(static_cast<int>(g * 128u)), tid); |
| 338 |
3/6
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 148 times.
✗ Branch 5 not taken.
|
148 |
Value* sc = c.b.build_select(self_valid, kb.shared_load_f32_indexed(sS, sidx), c.f32(-1e30f)); |
| 339 |
|
148 |
sc_v[g] = sc; |
| 340 |
|
148 |
Value* mx = sc; |
| 341 |
4/6
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 740 times.
✓ Branch 2 taken 740 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 740 times.
✗ Branch 5 not taken.
|
888 |
for (std::uint32_t m = 16; m >= 1; m >>= 1) mx = c.kb.fmax(mx, kb.shfl_bfly_f32(mx, m)); |
| 342 |
4/8
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 148 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 148 times.
✗ Branch 7 not taken.
|
296 |
kb.if_then(c.b.build_eq(lane, c.i32(0)), [&] { |
| 343 |
|
148 |
kb.shared_store_f32_indexed(s_red, c.b.build_add(c.i32(static_cast<int>(g * 4u)), warp), mx); |
| 344 |
|
148 |
}); |
| 345 |
|
148 |
} |
| 346 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
kb.sync(); |
| 347 |
|
|
|
| 348 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
std::vector<Value*> next = cur; |
| 349 |
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 148 times.
|
231 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 350 |
|
148 |
const int rb = static_cast<int>(g * 4u); |
| 351 |
3/6
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 148 times.
✗ Branch 5 not taken.
|
296 |
Value* m01 = kb.fmax(kb.shared_load_f32_indexed(s_red, c.i32(rb + 0)), |
| 352 |
2/4
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
|
148 |
kb.shared_load_f32_indexed(s_red, c.i32(rb + 1))); |
| 353 |
3/6
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 148 times.
✗ Branch 5 not taken.
|
296 |
Value* m23 = kb.fmax(kb.shared_load_f32_indexed(s_red, c.i32(rb + 2)), |
| 354 |
2/4
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
|
148 |
kb.shared_load_f32_indexed(s_red, c.i32(rb + 3))); |
| 355 |
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
|
148 |
Value* m_tile = kb.fmax(m01, m23); |
| 356 |
|
148 |
Value* m_prev = cur[m_off + g]; |
| 357 |
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
|
148 |
Value* m_new = c.kb.fmax(m_prev, m_tile); |
| 358 |
2/4
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
|
148 |
Value* has_any = c.b.build_sgt(m_new, c.f32(-1e29f)); |
| 359 |
2/4
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
|
148 |
Value* had_prev = c.b.build_sgt(m_prev, c.f32(-1e29f)); |
| 360 |
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
|
296 |
Value* alpha = c.b.build_select( |
| 361 |
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
|
148 |
c.b.build_and(has_any, had_prev), |
| 362 |
4/8
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 148 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 148 times.
✗ Branch 7 not taken.
|
148 |
kb.ex2_approx(c.b.build_mul(c.b.build_sub(m_prev, m_new), c.f32(kLog2e))), |
| 363 |
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
|
148 |
c.f32(0.0f)); |
| 364 |
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
|
296 |
Value* pv = c.b.build_select( |
| 365 |
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
|
148 |
c.b.build_and(has_any, self_valid), |
| 366 |
4/8
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 148 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 148 times.
✗ Branch 7 not taken.
|
148 |
kb.ex2_approx(c.b.build_mul(c.b.build_sub(sc_v[g], m_new), c.f32(kLog2e))), |
| 367 |
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
|
148 |
c.f32(0.0f)); |
| 368 |
2/4
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
|
148 |
Value* sidx = c.b.build_add(c.i32(static_cast<int>(g * 128u)), tid); |
| 369 |
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
|
148 |
kb.shared_store_f32_indexed(sS, sidx, pv); |
| 370 |
|
148 |
Value* wsum = pv; |
| 371 |
4/6
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 740 times.
✓ Branch 2 taken 740 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 740 times.
✗ Branch 5 not taken.
|
888 |
for (std::uint32_t m = 16; m >= 1; m >>= 1) wsum = c.b.build_add(wsum, kb.shfl_bfly_f32(wsum, m)); |
| 372 |
4/8
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 148 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 148 times.
✗ Branch 7 not taken.
|
296 |
kb.if_then(c.b.build_eq(lane, c.i32(0)), [&] { |
| 373 |
|
296 |
kb.shared_store_f32_indexed( |
| 374 |
|
148 |
s_red, c.b.build_add(c.i32(static_cast<int>((gqa_g + g) * 4u)), warp), wsum); |
| 375 |
|
148 |
}); |
| 376 |
|
148 |
next[m_off + g] = m_new; |
| 377 |
|
148 |
next[l_off + g] = alpha; // stash alpha temporarily until l_tile sync finishes |
| 378 |
|
148 |
} |
| 379 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
kb.sync(); |
| 380 |
|
|
|
| 381 |
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 148 times.
|
231 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 382 |
|
148 |
const int sb = static_cast<int>((gqa_g + g) * 4u); |
| 383 |
3/6
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 148 times.
✗ Branch 5 not taken.
|
296 |
Value* s01 = c.b.build_add(kb.shared_load_f32_indexed(s_red, c.i32(sb + 0)), |
| 384 |
2/4
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
|
148 |
kb.shared_load_f32_indexed(s_red, c.i32(sb + 1))); |
| 385 |
3/6
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 148 times.
✗ Branch 5 not taken.
|
296 |
Value* s23 = c.b.build_add(kb.shared_load_f32_indexed(s_red, c.i32(sb + 2)), |
| 386 |
2/4
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 148 times.
✗ Branch 3 not taken.
|
148 |
kb.shared_load_f32_indexed(s_red, c.i32(sb + 3))); |
| 387 |
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
|
148 |
Value* l_tile = c.b.build_add(s01, s23); |
| 388 |
|
148 |
Value* alpha = next[l_off + g]; |
| 389 |
1/2
✓ Branch 0 taken 148 times.
✗ Branch 1 not taken.
|
148 |
next[l_off + g] = c.b.build_fma_f32(cur[l_off + g], alpha, l_tile); |
| 390 |
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 92 times.
|
148 |
if (vec_pv) { |
| 391 |
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 448 times.
|
504 |
for (int e = 0; e < 8; ++e) { |
| 392 |
|
448 |
next[g * 8u + static_cast<std::size_t>(e)] = |
| 393 |
1/2
✓ Branch 0 taken 448 times.
✗ Branch 1 not taken.
|
448 |
c.b.build_mul(cur[g * 8u + static_cast<std::size_t>(e)], alpha); |
| 394 |
|
448 |
} |
| 395 |
|
56 |
} else { |
| 396 |
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 96 times.
|
188 |
for (std::uint32_t sl = 0; sl < hds; ++sl) { |
| 397 |
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
|
96 |
next[g * hds + sl] = c.b.build_mul(cur[g * hds + sl], alpha); |
| 398 |
|
96 |
} |
| 399 |
|
|
} |
| 400 |
|
148 |
} |
| 401 |
|
|
|
| 402 |
|
|
// 3. P * V accumulation: read V[t0 + ks, d] across all G query heads. |
| 403 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
std::vector<Value*> o_init(next.begin(), next.begin() + static_cast<std::ptrdiff_t>(n_o)); |
| 404 |
6/10
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 51 times.
✓ Branch 4 taken 51 times.
✓ Branch 5 taken 32 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
166 |
std::vector<Value*> o_updated = vec_pv |
| 405 |
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
64 |
? kb.for_range_reduce_n( |
| 406 |
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
|
32 |
c.i32(0), tile_n, c.i32(static_cast<int>(pv_k_step)), o_init, |
| 407 |
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
64 |
[&](Value* ks_base, const std::vector<Value*>& o_cur) -> std::vector<Value*> { |
| 408 |
|
32 |
Value* ks = c.b.build_add(ks_base, pv_kslot); |
| 409 |
|
32 |
Value* in_k = c.b.build_ult(ks, tile_n); |
| 410 |
|
32 |
Value* safe_ks = c.b.build_select(in_k, c.b.build_and(ks, c.i32(127)), c.i32(0)); |
| 411 |
|
64 |
Value* v_ok = c.b.build_and( |
| 412 |
|
32 |
in_k, c.b.build_ne(kb.shared_load_i32_indexed(s_valid, safe_ks), c.i32(0))); |
| 413 |
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 |
return emit_if_else_values( |
| 414 |
|
32 |
c, v_ok, std::vector<Type>(n_o, Type::f32()), |
| 415 |
|
64 |
[&]() -> std::vector<Value*> { |
| 416 |
|
64 |
Value* v_row = c.b.build_add( |
| 417 |
|
32 |
c.b.build_mul(c.b.build_add(t0, safe_ks), p[11]), kv_head_off); |
| 418 |
|
32 |
Value* vp = ptr_at(c, p[2], c.b.build_add(v_row, c.b.build_shl(pv_dv4, c.i32(3))), esz); |
| 419 |
|
32 |
std::vector<Value*> vv; |
| 420 |
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 28 times.
|
32 |
if (dt == Dtype::FP32) { |
| 421 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
vv = load_lanes(c, Dtype::FP32, 4, vp); |
| 422 |
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 |
auto vv2 = load_lanes(c, Dtype::FP32, 4, c.b.build_add(vp, kb.const_i64(16))); |
| 423 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
vv.insert(vv.end(), vv2.begin(), vv2.end()); |
| 424 |
|
4 |
} else { |
| 425 |
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 |
vv = load_lanes(c, dt, 8, vp); |
| 426 |
|
|
} |
| 427 |
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 |
std::vector<Value*> o_next = o_cur; |
| 428 |
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 32 times.
|
88 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 429 |
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
112 |
Value* pv = kb.shared_load_f32_indexed( |
| 430 |
2/4
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
|
56 |
sS, c.b.build_add(c.i32(static_cast<int>(g * 128u)), safe_ks)); |
| 431 |
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 448 times.
|
504 |
for (int e = 0; e < 8; ++e) { |
| 432 |
|
448 |
const std::size_t idx = g * 8u + static_cast<std::size_t>(e); |
| 433 |
1/2
✓ Branch 0 taken 448 times.
✗ Branch 1 not taken.
|
448 |
o_next[idx] = c.b.build_fma_f32(pv, vv[static_cast<std::size_t>(e)], o_next[idx]); |
| 434 |
|
448 |
} |
| 435 |
|
56 |
} |
| 436 |
|
32 |
return o_next; |
| 437 |
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 |
}, |
| 438 |
|
64 |
[&]() -> std::vector<Value*> { return o_cur; }); |
| 439 |
|
✗ |
}) |
| 440 |
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
|
102 |
: kb.for_range_reduce_n( |
| 441 |
2/4
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
✗ Branch 3 not taken.
|
51 |
c.i32(0), tile_n, c.i32(1), o_init, |
| 442 |
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
|
102 |
[&](Value* ks, const std::vector<Value*>& o_cur) -> std::vector<Value*> { |
| 443 |
|
51 |
Value* v_ok = c.b.build_ne(kb.shared_load_i32_indexed(s_valid, ks), c.i32(0)); |
| 444 |
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
|
51 |
return emit_if_else_values( |
| 445 |
|
51 |
c, v_ok, std::vector<Type>(n_o, Type::f32()), |
| 446 |
|
102 |
[&]() -> std::vector<Value*> { |
| 447 |
|
51 |
Value* v_row = c.b.build_add(c.b.build_mul(c.b.build_add(t0, ks), p[11]), kv_head_off); |
| 448 |
|
51 |
std::vector<Value*> pv(gqa_g); |
| 449 |
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 92 times.
|
143 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 450 |
1/2
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
|
184 |
pv[g] = kb.shared_load_f32_indexed( |
| 451 |
2/4
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
✗ Branch 3 not taken.
|
92 |
sS, c.b.build_add(c.i32(static_cast<int>(g * 128u)), ks)); |
| 452 |
|
92 |
} |
| 453 |
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
|
51 |
std::vector<Value*> o_next = o_cur; |
| 454 |
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 54 times.
|
105 |
for (std::uint32_t sl = 0; sl < hds; ++sl) { |
| 455 |
2/4
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
✗ Branch 3 not taken.
|
54 |
Value* d = c.b.build_add(c.i32(static_cast<int>(sl * 128u)), tid); |
| 456 |
6/8
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 49 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
108 |
Value* vv = ((sl + 1u) * 128u <= hd) |
| 457 |
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 |
? load_elem_dt(c, dt, p[2], c.b.build_add(v_row, d)) |
| 458 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
|
49 |
: emit_if_else_values( |
| 459 |
3/6
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 49 times.
✗ Branch 5 not taken.
|
49 |
c, c.b.build_ult(d, c.i32(static_cast<int>(hd))), {Type::f32()}, |
| 460 |
|
98 |
[&]() -> std::vector<Value*> { |
| 461 |
|
49 |
return {load_elem_dt(c, dt, p[2], c.b.build_add(v_row, d))}; |
| 462 |
|
|
}, |
| 463 |
|
98 |
[&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; |
| 464 |
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 96 times.
|
150 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 465 |
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
|
96 |
o_next[g * hds + sl] = c.b.build_fma_f32(pv[g], vv, o_next[g * hds + sl]); |
| 466 |
|
96 |
} |
| 467 |
|
54 |
} |
| 468 |
|
51 |
return o_next; |
| 469 |
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
|
51 |
}, |
| 470 |
|
102 |
[&]() -> std::vector<Value*> { return o_cur; }); |
| 471 |
|
✗ |
}); |
| 472 |
2/2
✓ Branch 0 taken 544 times.
✓ Branch 1 taken 83 times.
|
627 |
for (std::size_t i = 0; i < n_o; ++i) next[i] = o_updated[i]; |
| 473 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
kb.sync(); |
| 474 |
|
83 |
return next; |
| 475 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
}); |
| 476 |
|
|
|
| 477 |
2/4
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
✗ Branch 3 not taken.
|
83 |
std::vector<Value*> final_o(gqa_g * hds, c.f32(0.0f)); |
| 478 |
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 51 times.
|
83 |
if (vec_pv) { |
| 479 |
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
64 |
Value* slot_base = c.b.build_add( |
| 480 |
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
|
32 |
c.b.build_mul(pv_kslot, c.i32(static_cast<int>(hd))), |
| 481 |
2/4
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
|
32 |
c.b.build_shl(pv_dv4, c.i32(3))); |
| 482 |
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 32 times.
|
88 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 483 |
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 448 times.
|
504 |
for (int e = 0; e < 8; ++e) { |
| 484 |
1/2
✓ Branch 0 taken 448 times.
✗ Branch 1 not taken.
|
448 |
kb.shared_store_f32_indexed( |
| 485 |
2/4
✓ Branch 0 taken 448 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 448 times.
✗ Branch 3 not taken.
|
448 |
sO, c.b.build_add(slot_base, c.i32(e)), state[g * 8u + static_cast<std::size_t>(e)]); |
| 486 |
|
448 |
} |
| 487 |
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 |
kb.sync(); |
| 488 |
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 |
Value* acc_d = c.f32(0.0f); |
| 489 |
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 15 times.
|
56 |
if (hd == 128u) { |
| 490 |
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 15 times.
|
135 |
for (std::uint32_t k = 0; k < pv_k_step; ++k) { |
| 491 |
1/2
✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
|
240 |
acc_d = c.b.build_add( |
| 492 |
3/6
✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 120 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 120 times.
✗ Branch 5 not taken.
|
120 |
acc_d, kb.shared_load_f32_indexed(sO, c.b.build_add(c.i32(static_cast<int>(k * hd)), tid))); |
| 493 |
|
120 |
} |
| 494 |
|
15 |
} else { |
| 495 |
4/8
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 41 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 41 times.
✗ Branch 7 not taken.
|
41 |
Value* d_safe = c.b.build_select(c.b.build_ult(tid, c.i32(static_cast<int>(hd))), tid, c.i32(0)); |
| 496 |
2/2
✓ Branch 0 taken 656 times.
✓ Branch 1 taken 41 times.
|
697 |
for (std::uint32_t k = 0; k < pv_k_step; ++k) { |
| 497 |
1/2
✓ Branch 0 taken 656 times.
✗ Branch 1 not taken.
|
1312 |
acc_d = c.b.build_add( |
| 498 |
3/6
✓ Branch 0 taken 656 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 656 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 656 times.
✗ Branch 5 not taken.
|
656 |
acc_d, kb.shared_load_f32_indexed(sO, c.b.build_add(c.i32(static_cast<int>(k * hd)), d_safe))); |
| 499 |
|
656 |
} |
| 500 |
|
|
} |
| 501 |
|
56 |
final_o[g * hds] = acc_d; |
| 502 |
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
56 |
if (g + 1u < gqa_g) kb.sync(); |
| 503 |
|
56 |
} |
| 504 |
|
32 |
} else { |
| 505 |
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 51 times.
|
147 |
for (std::size_t i = 0; i < gqa_g * hds; ++i) final_o[i] = state[i]; |
| 506 |
|
|
} |
| 507 |
|
|
|
| 508 |
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 64 times.
|
83 |
if (split) { |
| 509 |
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 |
Value* nsplit = kb.nctaid_x(); |
| 510 |
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
|
19 |
Value* pw = c.i32(static_cast<int>(hd + 2u)); |
| 511 |
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 40 times.
|
59 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 512 |
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 |
Value* qh = c.b.build_add(qh0, c.i32(static_cast<int>(g))); |
| 513 |
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 |
Value* entry = c.b.build_add(c.b.build_mul(qi, p[18]), qh); |
| 514 |
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 |
Value* row_idx = c.b.build_add(c.b.build_mul(entry, nsplit), sp); |
| 515 |
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 |
Value* row_base = c.b.build_mul(row_idx, pw); |
| 516 |
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 42 times.
|
82 |
for (std::uint32_t sl = 0; sl < hds; ++sl) { |
| 517 |
2/4
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
|
42 |
Value* d = c.b.build_add(c.i32(static_cast<int>(sl * 128u)), tid); |
| 518 |
|
84 |
auto store_p = [&] { |
| 519 |
|
42 |
kb.store_f32(ptr_at(c, p[7], c.b.build_add(row_base, d), 4), final_o[g * hds + sl]); |
| 520 |
|
42 |
}; |
| 521 |
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
42 |
if ((sl + 1u) * 128u <= hd) store_p(); |
| 522 |
4/8
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 32 times.
✗ Branch 7 not taken.
|
32 |
else kb.if_then(c.b.build_ult(d, c.i32(static_cast<int>(hd))), store_p); |
| 523 |
|
42 |
} |
| 524 |
4/8
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 40 times.
✗ Branch 7 not taken.
|
80 |
kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] { |
| 525 |
|
80 |
kb.store_f32(ptr_at(c, p[7], c.b.build_add(row_base, c.i32(static_cast<int>(hd))), 4), |
| 526 |
|
40 |
state[m_off + g]); |
| 527 |
|
80 |
kb.store_f32(ptr_at(c, p[7], c.b.build_add(row_base, c.i32(static_cast<int>(hd + 1u))), 4), |
| 528 |
|
40 |
state[l_off + g]); |
| 529 |
|
40 |
}); |
| 530 |
|
40 |
} |
| 531 |
|
19 |
} else { |
| 532 |
4/8
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 64 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 64 times.
✗ Branch 7 not taken.
|
64 |
Value* o_row_base = c.b.build_add(c.b.build_mul(qi, p[12]), c.b.build_mul(qh0, c.i32(static_cast<int>(hd)))); |
| 533 |
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 108 times.
|
172 |
for (std::uint32_t g = 0; g < gqa_g; ++g) { |
| 534 |
|
108 |
Value* lv = state[l_off + g]; |
| 535 |
6/12
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 108 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 108 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 108 times.
✗ Branch 11 not taken.
|
108 |
Value* inv = c.b.build_select(c.b.build_sgt(lv, c.f32(0.0f)), emit_div_rn(c, c.f32(1.0f), lv), c.f32(0.0f)); |
| 536 |
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 108 times.
|
218 |
for (std::uint32_t sl = 0; sl < hds; ++sl) { |
| 537 |
2/4
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 110 times.
✗ Branch 3 not taken.
|
110 |
Value* d = c.b.build_add(c.i32(static_cast<int>(sl * 128u)), tid); |
| 538 |
|
220 |
auto store_o = [&] { |
| 539 |
|
110 |
Value* oidx = c.b.build_add(c.b.build_add(o_row_base, c.i32(static_cast<int>(g * hd))), d); |
| 540 |
|
110 |
store_elem_dt(c, dt, p[3], oidx, c.b.build_mul(final_o[g * hds + sl], inv)); |
| 541 |
|
110 |
}; |
| 542 |
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
110 |
if ((sl + 1u) * 128u <= hd) store_o(); |
| 543 |
4/8
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 98 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 98 times.
|
98 |
else kb.if_then(c.b.build_ult(d, c.i32(static_cast<int>(hd))), store_o); |
| 544 |
|
110 |
} |
| 545 |
|
108 |
} |
| 546 |
|
|
} |
| 547 |
|
|
|
| 548 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
c.b.build_ret_void(); |
| 549 |
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
|
83 |
return finalize_fn(fn); |
| 550 |
|
83 |
} |
| 551 |
|
|
|
| 552 |
|
|
} // namespace brotensor::kernels |
| 553 |
|
|
|