brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
313 / 317
98.7%
Branches:
232 / 430
54.0%
Functions:
18 / 18
100.0%

src/kernels/mir_fa_packed_mma.cpp
Line Branch Exec Source
1 #include "mir_fa_packed_mma.h"
2
3 #include "mir_common.h"
4
5 #include <array>
6 #include <climits>
7 #include <stdexcept>
8 #include <string>
9 #include <vector>
10
11 namespace brotensor::kernels {
12
13 namespace {
14
15 constexpr int kHd = 64;
16 constexpr int kBq = 64;
17 constexpr int kBk = 64;
18 constexpr int kLds = kHd + 8;
19 constexpr int kThreads = 128;
20
21 32 Value* pack_pair_i32(Ctx& c, Dtype dt, Value* v0, Value* v1) {
22
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (dt == Dtype::FP16) return c.kb.pack_f16x2(v0, v1);
23 ✗ Value* b0 = c.b.build_and(c.kb.f32_to_bf16(v0), c.i32(0xffff));
24 ✗ Value* b1 = c.b.build_shl(c.kb.f32_to_bf16(v1), c.i32(16));
25 ✗ return c.b.build_or(b0, b1);
26 32 }
27
28 64 Value* mma_sync_f32(Ctx& c, Dtype dt, Value* a4, Value* b0, Value* b1, Value* c4) {
29 64 const char* sym = (dt == Dtype::FP16) ? "ptx_mma_m16n8k16_f16_f32" : "ptx_mma_m16n8k16_bf16_f32";
30 64 return c.b.build_call(sym, Type::f32x4(), {a4, b0, b1, c4});
31 }
32
33 3 std::pair<Value*, Value*> row_range(Ctx& c, Value* bounds, Value* r, Value* L, Value* hw) {
34
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 auto res = emit_if_else_values(
35 3 c, c.b.build_slt(r, L), {Type::i32(), Type::i32()},
36 6 [&]() -> std::vector<Value*> {
37 3 Value* r2 = c.b.build_shl(r, c.i32(1));
38 3 Value* s = c.kb.load_i32(ptr_at(c, bounds, r2, 4));
39 3 Value* e = c.kb.load_i32(ptr_at(c, bounds, c.b.build_add(r2, c.i32(1)), 4));
40 3 Value* has_w = c.b.build_sge(hw, c.i32(0));
41 3 Value* lo = c.b.build_select(has_w, smax_i32(c, s, c.b.build_sub(r, hw)), s);
42 3 Value* hi = c.b.build_select(has_w, smin_i32(c, e, c.b.build_add(c.b.build_add(r, hw), c.i32(1))), e);
43 3 return {lo, hi};
44 },
45 6 [&]() -> std::vector<Value*> { return {c.i32(0), c.i32(0)}; });
46 3 return {res[0], res[1]};
47 3 }
48
49 5 void load_tile_cp_async(Ctx& c, Value* dst_smem_i32, Value* src, Value* ld, Value* col0,
50 Value* row0, Value* row_end) {
51 5 Value* tid = c.kb.tid_x();
52
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 5 times.
25 for (int i = 0; i < 4; ++i) {
53
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5 times.
20 Value* chunk = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * kThreads));
54 20 Value* r = c.b.build_lshr(chunk, c.i32(3));
55 20 Value* ch = c.b.build_and(chunk, c.i32(7));
56 20 Value* ch8 = c.b.build_shl(ch, c.i32(3));
57 20 Value* gr = c.b.build_add(row0, r);
58 20 Value* ok = c.b.build_slt(gr, row_end);
59 20 Value* g_idx = c.b.build_add(c.b.build_add(c.b.build_mul(gr, ld), col0), ch8);
60 20 Value* g_ptr = c.b.build_select(ok, ptr_at(c, src, g_idx, 2), src);
61 20 Value* s_off = c.b.build_shl(c.b.build_add(c.b.build_mul(r, c.i32(kLds)), ch8), c.i32(1));
62 20 Value* dst = c.b.build_add(dst_smem_i32, s_off);
63 20 Value* sz = c.b.build_select(ok, c.i32(16), c.i32(0));
64 20 c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, g_ptr, sz});
65 20 }
66 5 }
67
68 } // namespace
69
70 1 brass::Function* build_fa_packed_mma_mir(brass::Module& mod, Dtype dt) {
71
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (dt != Dtype::FP16 && dt != Dtype::BF16) {
72
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_fa_packed_mma_mir: only FP16/BF16 supported");
73 }
74
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 const std::string name = std::string("mir_fa_packed_hd64_") + (dt == Dtype::FP16 ? "f16" : "bf16");
75
7/14
✓ 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.
7 std::vector<Type> params = {
76 3 Type::ptr(), Type::ptr(), Type::ptr(),
77 3 Type::i32(), Type::i32(), Type::i32(),
78 1 Type::f32(),
79 };
80
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));
81
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::codegen::KernelBuilder kb(mod, fn);
82
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Ctx c(kb);
83
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto p = entry_params(c, fn);
84 // p: 0:qkv 1:bounds 2:out | 3:L 4:H 5:hw | 6:scale_log2
85 1 Value* qkv = p[0];
86 1 Value* bounds = p[1];
87 1 Value* out = p[2];
88 1 Value* L = p[3];
89 1 Value* H = p[4];
90 1 Value* hw = p[5];
91 1 Value* scale_log2 = p[6];
92
93 1 const int stage_elems = kBk * kLds;
94 1 const int stage_bytes = stage_elems * 2;
95
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* smem = kb.shared_alloc_u16(static_cast<std::uint32_t>(4 * stage_elems));
96
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* sK_base = c.b.build_trunc_i32(smem);
97
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));
98
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* s_lo = kb.shared_alloc_i32(1);
99
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* s_hi = kb.shared_alloc_i32(1);
100
101
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* D = c.b.build_mul(H, c.i32(kHd));
102
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* ld = c.b.build_mul(D, c.i32(3));
103
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* h = kb.ctaid_y();
104
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(h, c.i32(kHd));
105
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* q0 = c.b.build_mul(kb.ctaid_x(), c.i32(kBq));
106
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* tid = kb.tid_x();
107
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));
108
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));
109
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));
110
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));
111
112
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(tid, c.i32(0)), [&] {
113 1 kb.shared_store_i32(s_lo, c.i32(INT_MAX));
114 1 kb.shared_store_i32(s_hi, c.i32(INT_MIN));
115 1 });
116
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
117
118
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_slt(tid, c.i32(kBq)), [&] {
119 5 auto [lo, hi] = row_range(c, bounds, c.b.build_add(q0, tid), L, hw);
120
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.
6 kb.if_then(c.b.build_slt(lo, hi), [&] {
121 1 kb.atom_shared_min_i32(s_lo, lo);
122 1 kb.atom_shared_max_i32(s_hi, hi);
123 1 });
124 1 });
125
126
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);
127
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));
128
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
5 auto [lo0, hi0] = row_range(c, bounds, r0, L, hw);
129
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
7 auto [lo1, hi1] = row_range(c, bounds, r1, L, hw);
130
131
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.
3 Value* wlo0 = c.b.build_select(c.b.build_slt(lo0, hi0), lo0, c.i32(INT_MAX));
132
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.
3 Value* wlo1 = c.b.build_select(c.b.build_slt(lo1, hi1), lo1, c.i32(INT_MAX));
133
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.
3 Value* whi0 = c.b.build_select(c.b.build_slt(lo0, hi0), hi0, c.i32(INT_MIN));
134
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.
3 Value* whi1 = c.b.build_select(c.b.build_slt(lo1, hi1), hi1, c.i32(INT_MIN));
135
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* wlo = smin_i32(c, wlo0, wlo1);
136
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* whi = smax_i32(c, whi0, whi1);
137
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 5 times.
6 for (int off = 16; off > 0; off >>= 1) {
138
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 wlo = smin_i32(c, wlo, kb.shfl_bfly_i32(wlo, static_cast<std::uint32_t>(off)));
139
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 whi = smax_i32(c, whi, kb.shfl_bfly_i32(whi, static_cast<std::uint32_t>(off)));
140 5 }
141
142 // Stage Q into sK_base alias and read Q_frag via ldmatrix.
143
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 load_tile_cp_async(c, sK_base, qkv, ld, head_off, q0, L);
144
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());
145
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());
146
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
147
148
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* kmin = kb.shared_load_i32(s_lo);
149
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* kmax = kb.shared_load_i32(s_hi);
150
7/14
✓ 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.
2 kb.if_then(c.b.build_slt(kmin, kmax), [&] {
151 1 std::array<Value*, 4> qf{};
152 1 Value* q_lrow = c.b.build_add(c.b.build_shl(warp, c.i32(4)), c.b.build_and(lane, c.i32(15)));
153 1 Value* q_lcol0 = c.b.build_shl(c.b.build_lshr(lane, c.i32(4)), c.i32(3));
154
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 for (int kc = 0; kc < 4; ++kc) {
155 4 Value* col = c.b.build_add(c.i32(kc * 16), q_lcol0);
156 8 Value* q_addr = c.b.build_add(
157 4 sK_base, c.b.build_shl(c.b.build_add(c.b.build_mul(q_lrow, c.i32(kLds)), col), c.i32(1)));
158 4 qf[static_cast<std::size_t>(kc)] = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {q_addr});
159 4 }
160 1 kb.sync();
161
162 1 Value* k_col0 = c.b.build_add(D, head_off);
163 1 Value* v_col0 = c.b.build_add(c.b.build_shl(D, c.i32(1)), head_off);
164 3 Value* ntiles = c.b.build_udiv(c.b.build_sub(c.b.build_add(c.b.build_sub(kmax, kmin), c.i32(kBk - 1)),
165 1 c.i32(0)),
166 1 c.i32(kBk));
167
168 1 load_tile_cp_async(c, sK_base, qkv, ld, k_col0, kmin, kmax);
169 1 load_tile_cp_async(c, sV_base, qkv, ld, v_col0, kmin, kmax);
170 1 c.b.build_call("ptx_cp_async_commit", Type::void_type());
171
172 1 Value* mi = c.b.build_lshr(lane, c.i32(3));
173 2 Value* k_lrow0 = c.b.build_add(c.b.build_shl(c.b.build_lshr(mi, c.i32(1)), c.i32(3)),
174 1 c.b.build_and(lane, c.i32(7)));
175 1 Value* k_lcol0 = c.b.build_shl(c.b.build_and(mi, c.i32(1)), c.i32(3));
176 2 Value* v_lrow0 = c.b.build_add(c.b.build_shl(c.b.build_and(mi, c.i32(1)), c.i32(3)),
177 1 c.b.build_and(lane, c.i32(7)));
178 1 Value* v_lcol0 = c.b.build_shl(c.b.build_lshr(mi, c.i32(1)), c.i32(3));
179
180 1 std::vector<Type> loop_types(12, Type::f32());
181
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 time.
9 for (int i = 0; i < 8; ++i) loop_types[static_cast<std::size_t>(4 + i)] = Type::f32x4();
182
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> inits(12);
183
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 inits[0] = c.f32(-1e30f);
184
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 inits[1] = c.f32(-1e30f);
185
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 inits[2] = c.f32(0.0f);
186
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 inits[3] = c.f32(0.0f);
187
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
9 for (int i = 0; i < 8; ++i) inits[static_cast<std::size_t>(4 + i)] = c.b.build_vzero(Type::f32x4());
188
189
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 auto out_st = kb.for_range_reduce_n(
190
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c.i32(0), ntiles, c.i32(1), inits,
191
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* j, const std::vector<Value*>& st) -> std::vector<Value*> {
192 1 Value* buf = c.b.build_and(j, c.i32(1));
193 1 Value* cur_k = c.b.build_add(sK_base, c.b.build_mul(buf, c.i32(stage_bytes)));
194 1 Value* cur_v = c.b.build_add(sV_base, c.b.build_mul(buf, c.i32(stage_bytes)));
195
196 1 Value* has_next = c.b.build_slt(c.b.build_add(j, c.i32(1)), ntiles);
197
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 emit_if_else_values(
198 1 c, has_next, {},
199 2 [&]() -> std::vector<Value*> {
200 1 Value* nb = c.b.build_add(kmin, c.b.build_mul(c.b.build_add(j, c.i32(1)), c.i32(kBk)));
201 1 Value* nxt_buf = c.b.build_xor(buf, c.i32(1));
202 1 Value* nxt_k = c.b.build_add(sK_base, c.b.build_mul(nxt_buf, c.i32(stage_bytes)));
203 1 Value* nxt_v = c.b.build_add(sV_base, c.b.build_mul(nxt_buf, c.i32(stage_bytes)));
204 1 load_tile_cp_async(c, nxt_k, qkv, ld, k_col0, nb, kmax);
205 1 load_tile_cp_async(c, nxt_v, qkv, ld, v_col0, nb, kmax);
206 1 c.b.build_call("ptx_cp_async_commit", Type::void_type());
207 1 c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(1)});
208 1 return {};
209 },
210 2 [&]() -> std::vector<Value*> {
211 1 c.b.build_call("ptx_cp_async_wait_all", Type::void_type());
212 1 return {};
213 });
214 1 kb.sync();
215
216 1 Value* kb_row = c.b.build_add(kmin, c.b.build_mul(j, c.i32(kBk)));
217 2 Value* warp_active = c.b.build_and(
218 1 c.b.build_slt(kb_row, whi),
219 1 c.b.build_sgt(c.b.build_add(kb_row, c.i32(kBk)), wlo));
220
221 2 auto nxt = emit_if_else_values(
222 1 c, warp_active, loop_types,
223 2 [&]() -> std::vector<Value*> {
224 1 std::array<Value*, 8> s_acc{};
225 1 s_acc.fill(c.b.build_vzero(Type::f32x4()));
226
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 for (int kc = 0; kc < 4; ++kc) {
227 4 Value* qv = qf[static_cast<std::size_t>(kc)];
228 4 Value* col_k = c.b.build_add(c.i32(kc * 16), k_lcol0);
229
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int np = 0; np < 4; ++np) {
230 16 Value* row_k = c.b.build_add(c.i32(np * 16), k_lrow0);
231 32 Value* k_addr = c.b.build_add(
232 16 cur_k, c.b.build_shl(c.b.build_add(c.b.build_mul(row_k, c.i32(kLds)), col_k), c.i32(1)));
233 16 Value* b4 = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {k_addr});
234 16 const auto s0 = static_cast<std::size_t>(2 * np);
235 16 const auto s1 = s0 + 1u;
236 32 s_acc[s0] = mma_sync_f32(c, dt, qv,
237 16 c.b.build_vextract_lane(b4, 0),
238 16 c.b.build_vextract_lane(b4, 1), s_acc[s0]);
239 32 s_acc[s1] = mma_sync_f32(c, dt, qv,
240 16 c.b.build_vextract_lane(b4, 2),
241 16 c.b.build_vextract_lane(b4, 3), s_acc[s1]);
242 16 }
243 4 }
244
245 1 Value* mx0 = c.f32(-1e30f);
246 1 Value* mx1 = c.f32(-1e30f);
247 1 std::array<std::array<Value*, 4>, 8> sv{};
248 1 std::array<std::array<Value*, 4>, 8> sok{};
249 1 Value* t2 = c.b.build_shl(t, c.i32(1));
250
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 time.
9 for (int nt = 0; nt < 8; ++nt) {
251 8 const auto ntu = static_cast<std::size_t>(nt);
252 8 Value* key0 = c.b.build_add(kb_row, c.b.build_add(c.i32(nt * 8), t2));
253
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (int e = 0; e < 2; ++e) {
254
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 Value* kk = (e == 0) ? key0 : c.b.build_add(key0, c.i32(1));
255 16 Value* ok0 = c.b.build_and(c.b.build_sge(kk, lo0), c.b.build_slt(kk, hi0));
256 16 Value* ok1 = c.b.build_and(c.b.build_sge(kk, lo1), c.b.build_slt(kk, hi1));
257 16 Value* v0 = c.b.build_mul(c.b.build_vextract_lane(s_acc[ntu], static_cast<std::uint32_t>(e)), scale_log2);
258 16 Value* v1 = c.b.build_mul(c.b.build_vextract_lane(s_acc[ntu], static_cast<std::uint32_t>(2 + e)), scale_log2);
259 16 sv[ntu][static_cast<std::size_t>(e)] = c.b.build_select(ok0, v0, c.f32(-1e30f));
260 16 sv[ntu][static_cast<std::size_t>(2 + e)] = c.b.build_select(ok1, v1, c.f32(-1e30f));
261 16 sok[ntu][static_cast<std::size_t>(e)] = ok0;
262 16 sok[ntu][static_cast<std::size_t>(2 + e)] = ok1;
263 16 mx0 = kb.fmax(mx0, sv[ntu][static_cast<std::size_t>(e)]);
264 16 mx1 = kb.fmax(mx1, sv[ntu][static_cast<std::size_t>(2 + e)]);
265 16 }
266 8 }
267
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 for (int off = 1; off <= 2; off <<= 1) {
268 2 mx0 = kb.fmax(mx0, kb.shfl_bfly_f32(mx0, static_cast<std::uint32_t>(off)));
269 2 mx1 = kb.fmax(mx1, kb.shfl_bfly_f32(mx1, static_cast<std::uint32_t>(off)));
270 2 }
271 1 Value* has0 = c.b.build_sgt(mx0, c.f32(-1e29f));
272 1 Value* has1 = c.b.build_sgt(mx1, c.f32(-1e29f));
273 1 Value* mn0 = c.b.build_select(has0, kb.fmax(st[0], mx0), st[0]);
274 1 Value* mn1 = c.b.build_select(has1, kb.fmax(st[1], mx1), st[1]);
275 1 Value* prev0 = c.b.build_sgt(st[0], c.f32(-1e29f));
276 1 Value* prev1 = c.b.build_sgt(st[1], c.f32(-1e29f));
277 2 Value* a0 = c.b.build_select(
278 1 c.b.build_and(prev0, has0), kb.ex2_approx(c.b.build_sub(st[0], mn0)), c.f32(1.0f));
279 2 Value* a1 = c.b.build_select(
280 1 c.b.build_and(prev1, has1), kb.ex2_approx(c.b.build_sub(st[1], mn1)), c.f32(1.0f));
281
282 1 Value* ps0 = c.f32(0.0f);
283 1 Value* ps1 = c.f32(0.0f);
284
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 time.
9 for (int nt = 0; nt < 8; ++nt) {
285 8 const auto ntu = static_cast<std::size_t>(nt);
286
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (int e = 0; e < 2; ++e) {
287 16 const auto eu = static_cast<std::size_t>(e);
288 32 Value* p0 = c.b.build_select(
289 16 c.b.build_and(has0, sok[ntu][eu]),
290 16 kb.ex2_approx(c.b.build_sub(sv[ntu][eu], mn0)), c.f32(0.0f));
291 32 Value* p1 = c.b.build_select(
292 16 c.b.build_and(has1, sok[ntu][2u + eu]),
293 16 kb.ex2_approx(c.b.build_sub(sv[ntu][2u + eu], mn1)), c.f32(0.0f));
294 16 sv[ntu][eu] = p0;
295 16 sv[ntu][2u + eu] = p1;
296 16 ps0 = c.b.build_add(ps0, p0);
297 16 ps1 = c.b.build_add(ps1, p1);
298 16 }
299 8 }
300
301 1 std::vector<Value*> nst(12);
302 1 nst[0] = mn0;
303 1 nst[1] = mn1;
304
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 nst[2] = c.b.build_fma_f32(st[2], a0, ps0);
305
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 nst[3] = c.b.build_fma_f32(st[3], a1, ps1);
306
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* rscale = c.b.build_vzero(Type::f32x4());
307
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 rscale = c.b.build_vinsert_lane(rscale, a0, 0);
308
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 rscale = c.b.build_vinsert_lane(rscale, a0, 1);
309
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 rscale = c.b.build_vinsert_lane(rscale, a1, 2);
310
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 rscale = c.b.build_vinsert_lane(rscale, a1, 3);
311
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
9 for (int i = 0; i < 8; ++i) nst[static_cast<std::size_t>(4 + i)] = kb.vmul(st[static_cast<std::size_t>(4 + i)], rscale);
312
313
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 for (int kc = 0; kc < 4; ++kc) {
314 4 const auto k2 = static_cast<std::size_t>(2 * kc);
315
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* ap = c.b.build_vzero(Type::i32x4());
316
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 ap = c.b.build_vinsert_lane(ap, pack_pair_i32(c, dt, sv[k2][0], sv[k2][1]), 0);
317
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 ap = c.b.build_vinsert_lane(ap, pack_pair_i32(c, dt, sv[k2][2], sv[k2][3]), 1);
318
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 ap = c.b.build_vinsert_lane(ap, pack_pair_i32(c, dt, sv[k2 + 1u][0], sv[k2 + 1u][1]), 2);
319
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 ap = c.b.build_vinsert_lane(ap, pack_pair_i32(c, dt, sv[k2 + 1u][2], sv[k2 + 1u][3]), 3);
320
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);
321
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 for (int dp = 0; dp < 4; ++dp) {
322
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);
323
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
32 Value* v_addr = c.b.build_add(
324
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(kLds)), col_v), c.i32(1)));
325
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});
326 16 const auto o0 = static_cast<std::size_t>(4 + 2 * dp);
327 16 const auto o1 = o0 + 1u;
328
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
32 nst[o0] = mma_sync_f32(c, dt, ap,
329
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 c.b.build_vextract_lane(b4, 0),
330
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 c.b.build_vextract_lane(b4, 1), nst[o0]);
331
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
32 nst[o1] = mma_sync_f32(c, dt, ap,
332
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 c.b.build_vextract_lane(b4, 2),
333
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 c.b.build_vextract_lane(b4, 3), nst[o1]);
334 16 }
335 4 }
336 1 return nst;
337
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 },
338 2 [&]() -> std::vector<Value*> { return st; });
339
340
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
341 1 return nxt;
342
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 });
343
344 1 Value* l0 = out_st[2];
345 1 Value* l1 = out_st[3];
346
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (int off = 1; off <= 2; off <<= 1) {
347
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 l0 = c.b.build_add(l0, kb.shfl_bfly_f32(l0, static_cast<std::uint32_t>(off)));
348
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 l1 = c.b.build_add(l1, kb.shfl_bfly_f32(l1, static_cast<std::uint32_t>(off)));
349 2 }
350
6/12
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
1 Value* inv0 = c.b.build_select(c.b.build_sgt(l0, c.f32(0.0f)), kb.div_approx(c.f32(1.0f), l0), c.f32(0.0f));
351
6/12
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
1 Value* inv1 = c.b.build_select(c.b.build_sgt(l1, c.f32(0.0f)), kb.div_approx(c.f32(1.0f), l1), c.f32(0.0f));
352
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* r0_ok = c.b.build_slt(r0, L);
353
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* r1_ok = c.b.build_slt(r1, L);
354
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));
355
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* o0_base = c.b.build_add(c.b.build_mul(r0, D), head_off);
356
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* o1_base = c.b.build_add(c.b.build_mul(r1, D), head_off);
357
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 time.
9 for (int dt_i = 0; dt_i < 8; ++dt_i) {
358
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);
359 8 Value* ov = out_st[static_cast<std::size_t>(4 + dt_i)];
360
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* p0 = pack_pair_i32(
361 8 c, dt,
362
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 c.b.build_mul(c.b.build_vextract_lane(ov, 0), inv0),
363
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 c.b.build_mul(c.b.build_vextract_lane(ov, 1), inv0));
364
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* p1 = pack_pair_i32(
365 8 c, dt,
366
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 c.b.build_mul(c.b.build_vextract_lane(ov, 2), inv1),
367
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 c.b.build_mul(c.b.build_vextract_lane(ov, 3), inv1));
368
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
16 kb.if_then(r0_ok, [&] { kb.store_i32(ptr_at(c, out, c.b.build_add(o0_base, col), 2), p0); });
369
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
16 kb.if_then(r1_ok, [&] { kb.store_i32(ptr_at(c, out, c.b.build_add(o1_base, col), 2), p1); });
370 8 }
371 1 });
372
373
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_ret_void();
374
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return finalize_fn(fn);
375 1 }
376
377 } // namespace brotensor::kernels
378