brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
361 / 396
91.2%
Branches:
386 / 826
46.7%
Functions:
17 / 19
89.5%

src/kernels/mir_fa_fused_mma.cpp
Line Branch Exec Source
1 #include "mir_fa_fused_mma.h"
2
3 #include "mir_common.h"
4
5 #include <algorithm>
6 #include <array>
7 #include <stdexcept>
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 namespace brotensor::kernels {
13
14 namespace {
15
16 62 Value* ptr_idx_scaled(Ctx& c, Value* base, Value* elem_idx, int esz) {
17 62 return ptr_at(c, base, elem_idx, esz);
18 }
19
20 16 Value* pack_pair_i32(Ctx& c, Dtype dt, Value* v0, Value* v1) {
21
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (dt == Dtype::FP16) return c.kb.pack_f16x2(v0, v1);
22 ✗ Value* b0 = c.b.build_and(c.kb.f32_to_bf16(v0), c.i32(0xffff));
23 ✗ Value* b1 = c.b.build_shl(c.kb.f32_to_bf16(v1), c.i32(16));
24 ✗ return c.b.build_or(b0, b1);
25 16 }
26
27 64 Value* mma_sync_f32(Ctx& c, Dtype dt, Value* a4, Value* b0, Value* b1, Value* c4) {
28 64 const char* sym = (dt == Dtype::FP16) ? "ptx_mma_m16n8k16_f16_f32" : "ptx_mma_m16n8k16_bf16_f32";
29 64 return c.b.build_call(sym, Type::f32x4(), {a4, b0, b1, c4});
30 }
31
32 5 void emit_load_tile_cp_async(Ctx& c, Value* smem_base_i32, Value* g_base_ptr,
33 Value* row0, Value* row_limit, Value* ld, Value* col0,
34 int rows, int hd_pad, int lds, int segs_real, Value* safe_ptr) {
35 5 Value* tid = c.kb.tid_x();
36 5 const int nthreads = static_cast<int>(kFaFusedThreads);
37 5 const int segs = hd_pad / 8;
38 5 const int total = rows * segs;
39 5 const int per = (total + nthreads - 1) / nthreads;
40 5 const bool need_guard = (per * nthreads != total);
41
42
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 5 times.
25 for (int i = 0; i < per; ++i) {
43
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5 times.
20 Value* idx = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * nthreads));
44 40 auto body = [&] {
45 20 Value* r = c.b.build_udiv(idx, c.i32(segs));
46 20 Value* seg = c.b.build_umod(idx, c.i32(segs));
47 20 Value* gr = c.b.build_add(row0, r);
48 20 Value* r_ok = c.b.build_slt(gr, row_limit);
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 Value* s_ok = (segs_real < segs)
50 ✗ ? c.b.build_and(r_ok, c.b.build_slt(seg, c.i32(segs_real)))
51 20 : r_ok;
52 40 Value* dst = c.b.build_add(
53 20 smem_base_i32,
54 40 c.b.build_shl(c.b.build_add(c.b.build_mul(r, c.i32(lds)), c.b.build_shl(seg, c.i32(3))),
55 20 c.i32(1)));
56 40 Value* g_elem = c.b.build_add(c.b.build_add(c.b.build_mul(gr, ld), col0),
57 20 c.b.build_shl(seg, c.i32(3)));
58 20 Value* g_ptr = ptr_idx_scaled(c, g_base_ptr, g_elem, 2);
59 20 Value* safe_g = c.b.build_select(s_ok, g_ptr, safe_ptr);
60 20 Value* sz = c.b.build_select(s_ok, c.i32(16), c.i32(0));
61 20 c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, safe_g, sz});
62 20 };
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (need_guard) {
64
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ c.kb.if_then(c.b.build_slt(idx, c.i32(total)), body);
65 ✗ } else {
66 20 body();
67 }
68 20 }
69 5 }
70
71 } // namespace
72
73 1 brass::Function* build_fa_fused_mma_mir(brass::Module& mod, const FaFusedMmaSpec& spec) {
74
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (spec.dt != Dtype::FP16 && spec.dt != Dtype::BF16) {
75
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_fa_fused_mma_mir: only FP16/BF16 supported");
76 }
77
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (spec.hd == 0u || (spec.hd % 8u) != 0u || spec.hd > 160u) {
78
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_fa_fused_mma_mir: unsupported head_dim");
79 }
80 1 const int HD = static_cast<int>(spec.hd);
81 1 const int PAD = static_cast<int>(fa_fused_pad_hd(spec.hd));
82 1 const int BC = static_cast<int>(fa_fused_bc(spec.hd));
83 1 const int BR = static_cast<int>(kFaFusedBr);
84 1 const int LDS = PAD + 8;
85 1 const int KT = PAD / 16;
86 1 const int NT = BC / 8;
87 1 const int KC_PV = BC / 16;
88 1 const int SEGS_REAL = HD / 8;
89 1 const int stage_elems = BC * LDS;
90 1 const int stage_bytes = stage_elems * 2;
91 1 const int kv_total_elems = 4 * stage_elems; // sK[2] + sV[2]
92
93
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
3 const std::string name = std::string("mir_fa_fused_") +
94
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 (spec.dt == Dtype::FP16 ? "f16" : "bf16") +
95
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 "_hd" + std::to_string(HD) +
96
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 (spec.causal ? "_c" : "") +
97
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 (spec.has_mask ? "_m" : "") +
98 1 (spec.is_split ? "_sp" : "");
99
100
16/32
✓ 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.
✓ Branch 12 taken 1 time.
✗ Branch 13 not taken.
✓ Branch 14 taken 1 time.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 time.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 time.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 time.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 time.
✗ Branch 23 not taken.
✓ Branch 24 taken 1 time.
✗ Branch 25 not taken.
✓ Branch 26 taken 1 time.
✗ Branch 27 not taken.
✓ Branch 28 taken 1 time.
✗ Branch 29 not taken.
✓ Branch 30 taken 1 time.
✗ Branch 31 not taken.
16 std::vector<Type> params = {
101 7 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
102 8 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
103 1 Type::f32(),
104 };
105
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params));
106
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::codegen::KernelBuilder kb(mod, fn);
107
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Ctx c(kb);
108
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto p = entry_params(c, fn);
109 // p: 0:Q 1:K 2:V 3:mask 4:O 5:part_o 6:part_ml
110 // 7:Lq 8:Lk 9:D 10:Dkv 11:group 12:splits 13:qtiles 14:heads | 15:scale_log2
111 1 Value* Q_in = p[0];
112 1 Value* K_in = p[1];
113 1 Value* V_in = p[2];
114 1 Value* mask_in = p[3];
115 1 Value* O_in = p[4];
116 1 Value* part_o = p[5];
117 1 Value* part_ml = p[6];
118 1 Value* Lq = p[7];
119 1 Value* Lk = p[8];
120 1 Value* D = p[9];
121 1 Value* Dkv = p[10];
122 1 Value* group = p[11];
123 1 Value* splits = p[12];
124 1 Value* qtiles = p[13];
125 1 Value* heads = p[14];
126 1 Value* scale_log2 = p[15];
127
128
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* smem = kb.shared_alloc_u16(static_cast<std::uint32_t>(kv_total_elems));
129
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* sK_base = c.b.build_trunc_i32(smem);
130
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* sV_base = c.b.build_add(sK_base, c.i32(2 * stage_bytes));
131
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* sMask = spec.has_mask ? kb.shared_alloc_f32(static_cast<std::uint32_t>(BC)) : nullptr;
132
133
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* bz = kb.ctaid_z();
134
2/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 Value* split = spec.is_split ? c.b.build_umod(bz, splits) : c.i32(0);
135
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* b_idx = spec.is_split ? c.b.build_udiv(bz, splits) : bz;
136
137
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* Q = ptr_idx_scaled(c, Q_in, c.b.build_mul(b_idx, c.b.build_mul(Lq, D)), 2);
138
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* K = ptr_idx_scaled(c, K_in, c.b.build_mul(b_idx, c.b.build_mul(Lk, Dkv)), 2);
139
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* V = ptr_idx_scaled(c, V_in, c.b.build_mul(b_idx, c.b.build_mul(Lk, Dkv)), 2);
140
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* O = ptr_idx_scaled(c, O_in, c.b.build_mul(b_idx, c.b.build_mul(Lq, D)), 2);
141
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* mask_b = spec.has_mask ? ptr_idx_scaled(c, mask_in, c.b.build_mul(b_idx, Lk), 4) : mask_in;
142
143
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* head = kb.ctaid_y();
144
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* head_off = c.b.build_mul(head, c.i32(HD));
145
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* kv_off = c.b.build_mul(c.b.build_udiv(head, group), c.i32(HD));
146
147
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 Value* qtile = spec.causal
148
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_sub(c.b.build_sub(qtiles, c.i32(1)), kb.ctaid_x())
149
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ : kb.ctaid_x();
150
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* q0 = c.b.build_mul(qtile, c.i32(BR));
151
152
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* tid = kb.tid_x();
153
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* warp = c.b.build_lshr(tid, c.i32(5));
154
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* lane = c.b.build_and(tid, c.i32(31));
155
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(lane, c.i32(2));
156
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* t = c.b.build_and(lane, c.i32(3));
157
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* r0 = c.b.build_add(c.b.build_add(q0, c.b.build_shl(warp, c.i32(4))), g);
158
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* r1 = c.b.build_add(r0, c.i32(8));
159
160 // Stage Q into sK_base (aliased as sQ) once, load Q_frag via ldmatrix.x4, then sync before KV.
161
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 emit_load_tile_cp_async(c, sK_base, Q, q0, Lq, D, head_off, BR, PAD, LDS, SEGS_REAL, Q);
162
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c.b.build_call("ptx_cp_async_commit", Type::void_type());
163
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c.b.build_call("ptx_cp_async_wait_all", Type::void_type());
164
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
165
166
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> q_frag(static_cast<std::size_t>(KT));
167
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* q_lrow = c.b.build_add(c.b.build_shl(warp, c.i32(4)), c.b.build_and(lane, c.i32(15)));
168
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* q_lcol0 = c.b.build_shl(c.b.build_lshr(lane, c.i32(4)), c.i32(3));
169
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 for (int kc = 0; kc < KT; ++kc) {
170
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Value* col = c.b.build_add(c.i32(kc * 16), q_lcol0);
171
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 Value* q_addr = c.b.build_add(
172
5/10
✓ 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.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
4 sK_base, c.b.build_shl(c.b.build_add(c.b.build_mul(q_lrow, c.i32(LDS)), col), c.i32(1)));
173
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 q_frag[static_cast<std::size_t>(kc)] = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {q_addr});
174 4 }
175
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
176
177 // Key range & tile count for this CTA.
178
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* kv_end = spec.causal ? smin_i32(c, Lk, c.b.build_add(q0, c.i32(BR))) : Lk;
179
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* kv_begin = c.i32(0);
180
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (spec.is_split) {
181
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* total_tiles = c.b.build_udiv(c.b.build_add(kv_end, c.i32(BC - 1)), c.i32(BC));
182
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* tps = c.b.build_udiv(c.b.build_sub(c.b.build_add(total_tiles, splits), c.i32(1)), splits);
183
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 kv_begin = c.b.build_mul(c.b.build_mul(split, tps), c.i32(BC));
184
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 kv_end = smin_i32(c, kv_end, c.b.build_add(kv_begin, c.b.build_mul(tps, c.i32(BC))));
185 1 }
186
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* span_k = smax_i32(c, c.b.build_sub(kv_end, kv_begin), c.i32(0));
187
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 Value* n_tiles = c.b.build_select(
188
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_slt(kv_begin, kv_end),
189
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_udiv(c.b.build_add(span_k, c.i32(BC - 1)), c.i32(BC)),
190
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.i32(0));
191
192 // Precompute ldmatrix offsets for K and V relative to stage 0 (stage 1 = +stage_bytes).
193
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* mi = c.b.build_lshr(lane, c.i32(3));
194
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* k_lrow0 = c.b.build_add(c.b.build_shl(c.b.build_lshr(mi, c.i32(1)), c.i32(3)),
195
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c.b.build_and(lane, c.i32(7)));
196
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* k_lcol0 = c.b.build_shl(c.b.build_and(mi, c.i32(1)), c.i32(3));
197
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* v_lrow0 = c.b.build_add(c.b.build_shl(c.b.build_and(mi, c.i32(1)), c.i32(3)),
198
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c.b.build_and(lane, c.i32(7)));
199
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* v_lcol0 = c.b.build_shl(c.b.build_lshr(mi, c.i32(1)), c.i32(3));
200
201
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Type> loop_types(static_cast<std::size_t>(4 + 2 * KT), Type::f32());
202
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 time.
9 for (int dt_i = 0; dt_i < 2 * KT; ++dt_i) loop_types[static_cast<std::size_t>(4 + dt_i)] = Type::f32x4();
203
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> loop_inits(loop_types.size());
204
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 loop_inits[0] = c.f32(-1e30f);
205
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 loop_inits[1] = c.f32(-1e30f);
206
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 loop_inits[2] = c.f32(0.0f);
207
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 loop_inits[3] = c.f32(0.0f);
208
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
9 for (int dt_i = 0; dt_i < 2 * KT; ++dt_i) loop_inits[static_cast<std::size_t>(4 + dt_i)] = c.b.build_vzero(Type::f32x4());
209
210
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto res_vec = emit_if_else_values(
211
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c, c.b.build_sgt(n_tiles, c.i32(0)), loop_types,
212 2 [&]() -> std::vector<Value*> {
213 1 emit_load_tile_cp_async(c, sK_base, K, kv_begin, kv_end, Dkv, kv_off, BC, PAD, LDS, SEGS_REAL, K);
214 1 emit_load_tile_cp_async(c, sV_base, V, kv_begin, kv_end, Dkv, kv_off, BC, PAD, LDS, SEGS_REAL, V);
215 1 c.b.build_call("ptx_cp_async_commit", Type::void_type());
216
217
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 return kb.for_range_reduce_n(
218 1 c.i32(0), n_tiles, c.i32(1), loop_inits,
219 2 [&](Value* j, const std::vector<Value*>& st) -> std::vector<Value*> {
220 1 Value* buf = c.b.build_and(j, c.i32(1));
221 1 Value* cur_k = c.b.build_add(sK_base, c.b.build_mul(buf, c.i32(stage_bytes)));
222 1 Value* cur_v = c.b.build_add(sV_base, c.b.build_mul(buf, c.i32(stage_bytes)));
223
224 1 Value* has_next = c.b.build_slt(c.b.build_add(j, c.i32(1)), n_tiles);
225
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 emit_if_else_values(
226 1 c, has_next, {},
227 2 [&]() -> std::vector<Value*> {
228 1 Value* nxt_buf = c.b.build_xor(buf, c.i32(1));
229 1 Value* nxt_k = c.b.build_add(sK_base, c.b.build_mul(nxt_buf, c.i32(stage_bytes)));
230 1 Value* nxt_v = c.b.build_add(sV_base, c.b.build_mul(nxt_buf, c.i32(stage_bytes)));
231 1 Value* nk = c.b.build_add(kv_begin, c.b.build_mul(c.b.build_add(j, c.i32(1)), c.i32(BC)));
232 1 emit_load_tile_cp_async(c, nxt_k, K, nk, kv_end, Dkv, kv_off, BC, PAD, LDS, SEGS_REAL, K);
233 1 emit_load_tile_cp_async(c, nxt_v, V, nk, kv_end, Dkv, kv_off, BC, PAD, LDS, SEGS_REAL, V);
234 1 c.b.build_call("ptx_cp_async_commit", Type::void_type());
235 1 c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(1)});
236 1 return {};
237 },
238 2 [&]() -> std::vector<Value*> {
239 1 c.b.build_call("ptx_cp_async_wait_all", Type::void_type());
240 1 return {};
241 });
242
243 1 Value* kb_row = c.b.build_add(kv_begin, c.b.build_mul(j, c.i32(BC)));
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (spec.has_mask) {
245
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 kb.if_then(c.b.build_slt(tid, c.i32(BC)), [&] {
246 1 Value* gk = c.b.build_add(kb_row, tid);
247 1 Value* ok = c.b.build_slt(gk, kv_end);
248 1 Value* mptr = c.b.build_select(ok, ptr_idx_scaled(c, mask_b, gk, 4), mask_b);
249 1 Value* mv = c.b.build_select(ok, kb.load_f32(mptr), c.f32(0.0f));
250 1 kb.shared_store_f32_indexed(sMask, tid, mv);
251 1 });
252 1 }
253 1 kb.sync();
254
255 // S = Q * K^T (NT f32x4 accumulators).
256 1 std::vector<Value*> s_acc(static_cast<std::size_t>(NT), c.b.build_vzero(Type::f32x4()));
257
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 for (int kc = 0; kc < KT; ++kc) {
258 4 Value* qf = q_frag[static_cast<std::size_t>(kc)];
259
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Value* col_k = c.b.build_add(c.i32(kc * 16), k_lcol0);
260
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 for (int np = 0; np < BC / 16; ++np) {
261
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 Value* row_k = c.b.build_add(c.i32(np * 16), k_lrow0);
262
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
32 Value* k_addr = c.b.build_add(
263
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 cur_k, c.b.build_shl(c.b.build_add(c.b.build_mul(row_k, c.i32(LDS)), col_k), c.i32(1)));
264
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 Value* b4 = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {k_addr});
265
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 Value* b0 = c.b.build_vextract_lane(b4, 0);
266
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 Value* b1 = c.b.build_vextract_lane(b4, 1);
267
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 Value* b2 = c.b.build_vextract_lane(b4, 2);
268
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 Value* b3 = c.b.build_vextract_lane(b4, 3);
269 16 const auto n0 = static_cast<std::size_t>(2 * np);
270 16 const auto n1 = n0 + 1u;
271
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 s_acc[n0] = mma_sync_f32(c, spec.dt, qf, b0, b1, s_acc[n0]);
272
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 s_acc[n1] = mma_sync_f32(c, spec.dt, qf, b2, b3, s_acc[n1]);
273 16 }
274 4 }
275
276 // Scale + mask + online softmax in registers.
277
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* mx0 = c.f32(-1e30f);
278
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* mx1 = c.f32(-1e30f);
279
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<std::array<Value*, 4>> s_elem(static_cast<std::size_t>(NT));
280
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<std::array<Value*, 4>> s_valid(static_cast<std::size_t>(NT));
281
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* key_t2 = c.b.build_shl(t, c.i32(1));
282
283
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
9 for (int nt = 0; nt < NT; ++nt) {
284 8 const auto ntu = static_cast<std::size_t>(nt);
285
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 Value* key0 = c.b.build_add(kb_row, c.b.build_add(c.i32(nt * 8), key_t2));
286
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (int e = 0; e < 2; ++e) {
287
4/6
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
16 Value* kk = (e == 0) ? key0 : c.b.build_add(key0, c.i32(1));
288
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 Value* in_k = c.b.build_slt(kk, kv_end);
289
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (spec.has_mask) {
290
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 Value* mcol = c.b.build_add(c.i32(nt * 8 + e), key_t2);
291
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 Value* mv = kb.shared_load_f32_indexed(sMask, mcol);
292
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 in_k = c.b.build_and(in_k, c.b.build_sgt(mv, c.f32(0.5f)));
293 16 }
294
3/6
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
16 Value* ok0 = spec.causal ? c.b.build_and(in_k, c.b.build_sle(kk, r0)) : in_k;
295
3/6
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
16 Value* ok1 = spec.causal ? c.b.build_and(in_k, c.b.build_sle(kk, r1)) : in_k;
296
297
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 Value* v0 = c.b.build_mul(c.b.build_vextract_lane(s_acc[ntu], static_cast<std::uint32_t>(e)), scale_log2);
298
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 Value* v1 = c.b.build_mul(c.b.build_vextract_lane(s_acc[ntu], static_cast<std::uint32_t>(2 + e)), scale_log2);
299
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 Value* m0v = c.b.build_select(ok0, v0, c.f32(-1e30f));
300
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 Value* m1v = c.b.build_select(ok1, v1, c.f32(-1e30f));
301 16 s_elem[ntu][static_cast<std::size_t>(e)] = m0v;
302 16 s_elem[ntu][static_cast<std::size_t>(2 + e)] = m1v;
303 16 s_valid[ntu][static_cast<std::size_t>(e)] = ok0;
304 16 s_valid[ntu][static_cast<std::size_t>(2 + e)] = ok1;
305
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 mx0 = kb.fmax(mx0, m0v);
306
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 mx1 = kb.fmax(mx1, m1v);
307 16 }
308 8 }
309
310
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (int off = 1; off <= 2; off <<= 1) {
311
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 mx0 = kb.fmax(mx0, kb.shfl_bfly_f32(mx0, static_cast<std::uint32_t>(off)));
312
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 mx1 = kb.fmax(mx1, kb.shfl_bfly_f32(mx1, static_cast<std::uint32_t>(off)));
313 2 }
314
315 1 Value* m0_old = st[0];
316 1 Value* m1_old = st[1];
317 1 Value* l0_old = st[2];
318 1 Value* l1_old = st[3];
319
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* has0 = c.b.build_sgt(mx0, c.f32(-1e29f));
320
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* has1 = c.b.build_sgt(mx1, c.f32(-1e29f));
321
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* mn0 = c.b.build_select(has0, kb.fmax(m0_old, mx0), m0_old);
322
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* mn1 = c.b.build_select(has1, kb.fmax(m1_old, mx1), m1_old);
323
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* prev0 = c.b.build_sgt(m0_old, c.f32(-1e29f));
324
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* prev1 = c.b.build_sgt(m1_old, c.f32(-1e29f));
325
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 Value* a0 = c.b.build_select(
326
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_and(prev0, has0), kb.ex2_approx(c.b.build_sub(m0_old, mn0)), c.f32(1.0f));
327
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 Value* a1 = c.b.build_select(
328
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_and(prev1, has1), kb.ex2_approx(c.b.build_sub(m1_old, mn1)), c.f32(1.0f));
329
330
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* ps0 = c.f32(0.0f);
331
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* ps1 = c.f32(0.0f);
332
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
9 for (int nt = 0; nt < NT; ++nt) {
333 8 const auto ntu = static_cast<std::size_t>(nt);
334
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (int e = 0; e < 2; ++e) {
335 16 const auto eu = static_cast<std::size_t>(e);
336
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
32 Value* p0 = c.b.build_select(
337
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 c.b.build_and(has0, s_valid[ntu][eu]),
338
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 kb.ex2_approx(c.b.build_sub(s_elem[ntu][eu], mn0)), c.f32(0.0f));
339
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
32 Value* p1 = c.b.build_select(
340
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 c.b.build_and(has1, s_valid[ntu][2u + eu]),
341
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 kb.ex2_approx(c.b.build_sub(s_elem[ntu][2u + eu], mn1)), c.f32(0.0f));
342 16 s_elem[ntu][eu] = p0;
343 16 s_elem[ntu][2u + eu] = p1;
344
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 ps0 = c.b.build_add(ps0, p0);
345
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 ps1 = c.b.build_add(ps1, p1);
346 16 }
347 8 }
348
349
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> nxt(st.size());
350 1 nxt[0] = mn0;
351 1 nxt[1] = mn1;
352
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 nxt[2] = c.b.build_fma_f32(l0_old, a0, ps0);
353
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 nxt[3] = c.b.build_fma_f32(l1_old, a1, ps1);
354
355 // Rescale O accumulators in place.
356
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* rescale01 = c.b.build_vzero(Type::f32x4());
357
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 rescale01 = c.b.build_vinsert_lane(rescale01, a0, 0);
358
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 rescale01 = c.b.build_vinsert_lane(rescale01, a0, 1);
359
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 rescale01 = c.b.build_vinsert_lane(rescale01, a1, 2);
360
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 rescale01 = c.b.build_vinsert_lane(rescale01, a1, 3);
361
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
9 for (int dt_i = 0; dt_i < 2 * KT; ++dt_i) {
362 8 const auto idx = static_cast<std::size_t>(4 + dt_i);
363
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 nxt[idx] = kb.vmul(st[idx], rescale01);
364 8 }
365
366 // O += P * V directly from register-packed P.
367
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 for (int kc = 0; kc < KC_PV; ++kc) {
368 4 const auto k2 = static_cast<std::size_t>(2 * kc);
369
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* a_p = c.b.build_vzero(Type::i32x4());
370
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 a_p = c.b.build_vinsert_lane(a_p, pack_pair_i32(c, spec.dt, s_elem[k2][0], s_elem[k2][1]), 0);
371
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 a_p = c.b.build_vinsert_lane(a_p, pack_pair_i32(c, spec.dt, s_elem[k2][2], s_elem[k2][3]), 1);
372
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 a_p = c.b.build_vinsert_lane(a_p, pack_pair_i32(c, spec.dt, s_elem[k2 + 1u][0], s_elem[k2 + 1u][1]), 2);
373
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 a_p = c.b.build_vinsert_lane(a_p, pack_pair_i32(c, spec.dt, s_elem[k2 + 1u][2], s_elem[k2 + 1u][3]), 3);
374
375
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Value* row_v = c.b.build_add(c.i32(kc * 16), v_lrow0);
376
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 for (int dp = 0; dp < KT; ++dp) {
377
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 Value* col_v = c.b.build_add(c.i32(dp * 16), v_lcol0);
378
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
32 Value* v_addr = c.b.build_add(
379
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 cur_v, c.b.build_shl(c.b.build_add(c.b.build_mul(row_v, c.i32(LDS)), col_v), c.i32(1)));
380
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 Value* b4 = c.b.build_call("ptx_ldmatrix_x4_trans", Type::i32x4(), {v_addr});
381 16 const auto o0 = static_cast<std::size_t>(4 + 2 * dp);
382 16 const auto o1 = o0 + 1u;
383
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
32 nxt[o0] = mma_sync_f32(c, spec.dt, a_p,
384
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 c.b.build_vextract_lane(b4, 0),
385
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 c.b.build_vextract_lane(b4, 1), nxt[o0]);
386
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
32 nxt[o1] = mma_sync_f32(c, spec.dt, a_p,
387
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 c.b.build_vextract_lane(b4, 2),
388
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 c.b.build_vextract_lane(b4, 3), nxt[o1]);
389 16 }
390 4 }
391
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
392 1 return nxt;
393
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 });
394 ✗ },
395 2 [&]() -> std::vector<Value*> { return loop_inits; });
396
397 1 Value* m0_f = res_vec[0];
398 1 Value* m1_f = res_vec[1];
399 1 Value* l0_f = res_vec[2];
400 1 Value* l1_f = res_vec[3];
401
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (int off = 1; off <= 2; off <<= 1) {
402
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 l0_f = c.b.build_add(l0_f, kb.shfl_bfly_f32(l0_f, static_cast<std::uint32_t>(off)));
403
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 l1_f = c.b.build_add(l1_f, kb.shfl_bfly_f32(l1_f, static_cast<std::uint32_t>(off)));
404 2 }
405
406
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* t2 = c.b.build_shl(t, c.i32(1));
407
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (spec.is_split) {
408
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* prow0 = c.b.build_add(c.b.build_mul(bz, Lq), r0);
409
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* prow1 = c.b.build_add(c.b.build_mul(bz, Lq), r1);
410
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* out0_base = c.b.build_add(c.b.build_mul(prow0, D), head_off);
411
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* out1_base = c.b.build_add(c.b.build_mul(prow1, D), head_off);
412
413
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
9 for (int dt_i = 0; dt_i < 2 * KT; ++dt_i) {
414
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 Value* col = c.b.build_add(c.i32(dt_i * 8), t2);
415
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 Value* c_ok = c.b.build_slt(col, c.i32(HD));
416 8 Value* ov = res_vec[static_cast<std::size_t>(4 + dt_i)];
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.
16 kb.if_then(c.b.build_and(c_ok, c.b.build_slt(r0, Lq)), [&] {
418 8 Value* o_idx = c.b.build_add(out0_base, col);
419 8 kb.store_f32(ptr_idx_scaled(c, part_o, o_idx, 4), c.b.build_vextract_lane(ov, 0));
420 8 kb.store_f32(ptr_idx_scaled(c, part_o, c.b.build_add(o_idx, c.i32(1)), 4), c.b.build_vextract_lane(ov, 1));
421 8 });
422
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 not taken.
✓ Branch 7 taken 8 times.
16 kb.if_then(c.b.build_and(c_ok, c.b.build_slt(r1, Lq)), [&] {
423 8 Value* o_idx = c.b.build_add(out1_base, col);
424 8 kb.store_f32(ptr_idx_scaled(c, part_o, o_idx, 4), c.b.build_vextract_lane(ov, 2));
425 8 kb.store_f32(ptr_idx_scaled(c, part_o, c.b.build_add(o_idx, c.i32(1)), 4), c.b.build_vextract_lane(ov, 3));
426 8 });
427 8 }
428
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 kb.if_then(c.b.build_eq(t, c.i32(0)), [&] {
429
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 kb.if_then(c.b.build_slt(r0, Lq), [&] {
430 1 Value* ml0 = c.b.build_shl(c.b.build_add(c.b.build_mul(prow0, heads), head), c.i32(1));
431 1 kb.store_f32(ptr_idx_scaled(c, part_ml, ml0, 4), m0_f);
432 1 kb.store_f32(ptr_idx_scaled(c, part_ml, c.b.build_add(ml0, c.i32(1)), 4), l0_f);
433 1 });
434
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 kb.if_then(c.b.build_slt(r1, Lq), [&] {
435 1 Value* ml1 = c.b.build_shl(c.b.build_add(c.b.build_mul(prow1, heads), head), c.i32(1));
436 1 kb.store_f32(ptr_idx_scaled(c, part_ml, ml1, 4), m1_f);
437 1 kb.store_f32(ptr_idx_scaled(c, part_ml, c.b.build_add(ml1, c.i32(1)), 4), l1_f);
438 1 });
439 1 });
440 1 } else {
441
0/12
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Value* inv0 = c.b.build_select(c.b.build_sgt(l0_f, c.f32(0.0f)), kb.div_approx(c.f32(1.0f), l0_f), c.f32(0.0f));
442
0/12
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Value* inv1 = c.b.build_select(c.b.build_sgt(l1_f, c.f32(0.0f)), kb.div_approx(c.f32(1.0f), l1_f), c.f32(0.0f));
443
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* r0_ok = c.b.build_slt(r0, Lq);
444
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* r1_ok = c.b.build_slt(r1, Lq);
445
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* o0_base = c.b.build_add(c.b.build_mul(r0, D), head_off);
446
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* o1_base = c.b.build_add(c.b.build_mul(r1, D), head_off);
447
448
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int dt_i = 0; dt_i < 2 * KT; ++dt_i) {
449
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* col = c.b.build_add(c.i32(dt_i * 8), t2);
450
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* c_ok = c.b.build_slt(col, c.i32(HD));
451 ✗ Value* ov = res_vec[static_cast<std::size_t>(4 + dt_i)];
452
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* p0 = pack_pair_i32(
453 ✗ c, spec.dt,
454
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ c.b.build_mul(c.b.build_vextract_lane(ov, 0), inv0),
455
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ c.b.build_mul(c.b.build_vextract_lane(ov, 1), inv0));
456
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* p1 = pack_pair_i32(
457 ✗ c, spec.dt,
458
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ c.b.build_mul(c.b.build_vextract_lane(ov, 2), inv1),
459
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ c.b.build_mul(c.b.build_vextract_lane(ov, 3), inv1));
460
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_and(c_ok, r0_ok), [&] {
461 ✗ kb.store_i32(ptr_idx_scaled(c, O, c.b.build_add(o0_base, col), 2), p0);
462 ✗ });
463
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_and(c_ok, r1_ok), [&] {
464 ✗ kb.store_i32(ptr_idx_scaled(c, O, c.b.build_add(o1_base, col), 2), p1);
465 ✗ });
466 ✗ }
467 }
468
469
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_ret_void();
470
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return finalize_fn(fn);
471 1 }
472
473 } // namespace brotensor::kernels
474