brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
214 / 273
78.4%
Branches:
213 / 468
45.5%
Functions:
14 / 19
73.7%

src/kernels/mir_gemm_skinny.cpp
Line Branch Exec Source
1 #include "mir_gemm_skinny.h"
2 #include "mir_common.h"
3 #include "mir_math.h"
4
5 #include <algorithm>
6 #include <cstdint>
7 #include <stdexcept>
8 #include <utility>
9 #include <vector>
10
11 namespace brotensor::kernels {
12
13 namespace {
14
15 constexpr int kWarps = 8;
16 constexpr int kThreads = kWarps * 32; // 256
17 constexpr int kCtaCols = kWarps * 16; // 128
18 constexpr int kChunk = 64; // K elements per stage (128B)
19 constexpr int kStages = 4;
20 constexpr int kTileStride = kCtaCols + 4; // 132 floats per tile row
21
22 6 Value* ptr_2d_i64(Ctx& c, Value* base, Value* row, Value* ld, Value* col, int esz_shift) {
23 12 Value* elem = c.b.build_add(
24 6 c.b.build_mul(c.b.build_zext_i64(row), c.b.build_zext_i64(ld)),
25 6 c.b.build_zext_i64(col));
26 6 return c.b.build_add(base, c.b.build_shl(elem, c.kb.const_i64(esz_shift)));
27 }
28
29 4 Value* pack_i32x4(Ctx& c, Value* x0, Value* x1, Value* x2, Value* x3) {
30 4 Value* v = c.b.build_vzero(Type::i32x4());
31 4 v = c.b.build_vinsert_lane(v, x0, 0);
32 4 v = c.b.build_vinsert_lane(v, x1, 1);
33 4 v = c.b.build_vinsert_lane(v, x2, 2);
34 4 v = c.b.build_vinsert_lane(v, x3, 3);
35 4 return v;
36 }
37
38 1 Value* load_bias_f32(Ctx& c, Dtype dt, Value* bias_ptr, Value* n) {
39 1 Value* raw = c.kb.load_u16(ptr_at(c, bias_ptr, n, 2));
40
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return (dt == Dtype::FP16) ? c.kb.f16_to_f32(raw) : c.kb.bf16_to_f32(raw);
41 }
42
43 5 Value* build_w_row(Ctx& c, Value* cb, Value* lr, Value* N, int epi) {
44
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (epi == 3) {
45 ✗ Value* half = c.b.build_lshr(N, c.i32(1));
46 ✗ Value* j = c.b.build_add(c.b.build_mul(cb, c.i32(64)), c.b.build_and(lr, c.i32(63)));
47 ✗ Value* cand = c.b.build_select(c.b.build_slt(lr, c.i32(64)), j, c.b.build_add(half, j));
48 ✗ return c.b.build_select(c.b.build_slt(j, half), cand, c.i32(-1));
49 }
50 5 Value* n = c.b.build_add(c.b.build_mul(cb, c.i32(kCtaCols)), lr);
51 5 return c.b.build_select(c.b.build_slt(n, N), n, c.i32(-1));
52 5 }
53
54 1 void emit_out_elem(Ctx& c, Dtype dt, Value* Y, Value* m, Value* col, Value* out_cols,
55 Value* a, Value* b, int act, int epi) {
56 1 Value* v = nullptr;
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (epi == 2) {
58 ✗ v = c.b.build_mul(a, emit_gelu_exact(c, b));
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 } else if (epi == 3) {
60 ✗ v = c.b.build_mul(emit_gemm_act(c, 4, a), b);
61 ✗ } else {
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 v = (act != 0) ? emit_gemm_act(c, act, a) : a;
63 }
64 1 Value* dst = ptr_2d_i64(c, Y, m, out_cols, col, 1);
65
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (epi == 1) {
66 ✗ Value* old_raw = c.kb.load_u16(dst);
67
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* old_v = (dt == Dtype::FP16) ? c.kb.f16_to_f32(old_raw) : c.kb.bf16_to_f32(old_raw);
68 ✗ v = c.b.build_add(v, old_v);
69 ✗ }
70
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 store_lanes(c, dt, 1, dst, {v});
71 1 }
72
73 } // namespace
74
75 1 Function* build_gemm_skinny_mir(Module& mod, const GemmSkinnySpec& spec) {
76
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) {
77
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_gemm_skinny_mir: only FP16/BF16 supported");
78 }
79 1 const int nt = std::clamp(static_cast<int>(spec.nt), 2, 8);
80 1 const int rows = nt * 8;
81 1 const int stage_bytes = (kCtaCols + rows) * 128;
82 1 const int w_tile_bytes = kCtaCols * 128;
83
84 2 Function* fn = mod.create_function(
85 1 "mir_gemm_skinny", Type::void_type(),
86 5 {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
87 4 Type::i32(), Type::i32(), Type::i32(), Type::i32()});
88
89 1 brass::codegen::KernelBuilder kb(mod, fn);
90
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Ctx c(kb);
91
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto p = entry_params(c, fn);
92 // p: X(0) W(1) Y(2) bias(3) partial(4) M(5) N(6) K(7) cps(8)
93 1 Value* X = p[0];
94 1 Value* W = p[1];
95 1 Value* Y = p[2];
96 1 Value* bias = p[3];
97 1 Value* partial = p[4];
98 1 Value* M = p[5];
99 1 Value* N = p[6];
100 1 Value* K = p[7];
101 1 Value* cps = p[8];
102
103
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* smem_dyn = c.b.build_call("ptx_shared_dynamic", Type::ptr());
104
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* s_base_u32 = c.b.build_trunc_i32(smem_dyn);
105
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* evict_first = c.b.build_call("ptx_l2_evict_first_policy", Type::i64());
106
107
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* tid = kb.tid_x();
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* warp = c.b.build_lshr(tid, c.i32(5));
110
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));
111
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));
112
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* cb = kb.ctaid_x();
113
114
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* n_chunks = c.b.build_lshr(c.b.build_add(K, c.i32(kChunk - 1)), c.i32(6));
115
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* c0 = c.b.build_mul(kb.ctaid_y(), cps);
116
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* KT = c.b.build_sub(smin_i32(c, n_chunks, c.b.build_add(c0, cps)), c0);
117
118 6 auto piece_off = [&](Value* row, Value* u) -> Value* {
119 5 Value* swz = c.b.build_shl(c.b.build_and(row, c.i32(1)), c.i32(2));
120 5 Value* col_bytes = c.b.build_shl(c.b.build_xor(u, swz), c.i32(4));
121 5 return c.b.build_add(c.b.build_shl(row, c.i32(7)), col_bytes);
122 };
123
124 // Precompute W cp.async addresses (4 pieces/thread) and X cp.async addresses.
125 1 constexpr int kWIters = (kCtaCols * 8) / kThreads; // 4
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 std::vector<Value*> w_s0(kWIters), w_g0(kWIters), w_uk(kWIters), w_ok(kWIters);
127
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 for (int i = 0; i < kWIters; ++i) {
128
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
4 Value* q = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * kThreads));
129
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Value* row = c.b.build_lshr(q, c.i32(3));
130
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Value* u = c.b.build_and(q, c.i32(7));
131
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* n = build_w_row(c, cb, row, N, spec.epi);
132
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Value* uk = c.b.build_shl(u, c.i32(3));
133
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 w_s0[static_cast<std::size_t>(i)] = c.b.build_add(s_base_u32, piece_off(row, u));
134 4 w_uk[static_cast<std::size_t>(i)] = uk;
135
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 w_ok[static_cast<std::size_t>(i)] = c.b.build_sge(n, c.i32(0));
136
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Value* safe_n = c.b.build_select(w_ok[static_cast<std::size_t>(i)], n, c.i32(0));
137
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 w_g0[static_cast<std::size_t>(i)] = ptr_2d_i64(c, W, safe_n, K, uk, 1);
138 4 }
139
140 1 const int x_iters = (rows * 8 + kThreads - 1) / kThreads; // 1 or 2
141
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> x_s0(static_cast<std::size_t>(x_iters));
142
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> x_g0(static_cast<std::size_t>(x_iters));
143
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> x_uk(static_cast<std::size_t>(x_iters));
144
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> x_ok(static_cast<std::size_t>(x_iters));
145
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 for (int i = 0; i < x_iters; ++i) {
146 1 const auto iu = static_cast<std::size_t>(i);
147
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 Value* q = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * kThreads));
148
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* row = c.b.build_lshr(q, c.i32(3));
149
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* u = c.b.build_and(q, c.i32(7));
150
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* uk = c.b.build_shl(u, c.i32(3));
151
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 x_s0[iu] = c.b.build_add(c.b.build_add(s_base_u32, c.i32(w_tile_bytes)), piece_off(row, u));
152 1 x_uk[iu] = uk;
153
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 x_ok[iu] = c.b.build_slt(row, M);
154
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* safe_r = c.b.build_select(x_ok[iu], row, c.i32(0));
155
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 x_g0[iu] = ptr_2d_i64(c, X, safe_r, K, uk, 1);
156 1 }
157
158 5 auto emit_load_stage = [&](Value* stage_u32, Value* chunk_idx) {
159 4 Value* st_off = c.b.build_mul(stage_u32, c.i32(stage_bytes));
160 4 Value* kc = c.b.build_shl(chunk_idx, c.i32(6));
161 4 Value* kc_bytes = c.b.build_shl(c.b.build_zext_i64(chunk_idx), kb.const_i64(7));
162
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (int i = 0; i < kWIters; ++i) {
163 16 const auto iu = static_cast<std::size_t>(i);
164 16 Value* dst = c.b.build_add(w_s0[iu], st_off);
165 16 Value* ok = c.b.build_and(w_ok[iu], c.b.build_slt(c.b.build_add(kc, w_uk[iu]), K));
166 16 Value* src = c.b.build_select(ok, c.b.build_add(w_g0[iu], kc_bytes), W);
167 16 Value* sz = c.b.build_select(ok, c.i32(16), c.i32(0));
168 16 c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, src, sz, evict_first});
169 16 }
170
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (int i = 0; i < x_iters; ++i) {
171 4 const auto iu = static_cast<std::size_t>(i);
172 4 const int rem = rows * 8 - i * kThreads;
173 8 auto emit_x = [&] {
174 4 Value* dst = c.b.build_add(x_s0[iu], st_off);
175 4 Value* ok = c.b.build_and(x_ok[iu], c.b.build_slt(c.b.build_add(kc, x_uk[iu]), K));
176 4 Value* src = c.b.build_select(ok, c.b.build_add(x_g0[iu], kc_bytes), X);
177 4 Value* sz = c.b.build_select(ok, c.i32(16), c.i32(0));
178 4 c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, src, sz});
179 4 };
180
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (rem < kThreads) {
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 kb.if_then(c.b.build_slt(tid, c.i32(rem)), emit_x);
182 4 } else {
183 ✗ emit_x();
184 }
185 4 }
186 4 };
187
188
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 for (int s = 0; s < kStages - 1; ++s) {
189
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
6 kb.if_then(c.b.build_slt(c.i32(s), KT), [&] {
190 3 emit_load_stage(c.i32(s), c.b.build_add(c0, c.i32(s)));
191 3 });
192
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 c.b.build_call("ptx_cp_async_commit", Type::void_type());
193 3 }
194
195
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* ra = c.b.build_add(c.b.build_shl(warp, c.i32(4)), g);
196
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* rb = c.b.build_add(ra, c.i32(8));
197
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* ra8 = c.b.build_shl(ra, 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 Value* rb8 = c.b.build_shl(rb, c.i32(3));
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* g_swz = c.b.build_shl(c.b.build_and(g, c.i32(1)), c.i32(2));
200
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* t_q0 = c.b.build_xor(t, g_swz);
201
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* t_q1 = c.b.build_xor(c.b.build_add(t, c.i32(4)), g_swz);
202
203
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> acc_init(static_cast<std::size_t>(nt));
204
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
3 for (int j = 0; j < nt; ++j) acc_init[static_cast<std::size_t>(j)] = c.b.build_vzero(Type::f32x4());
205
206 1 const char* mma_sym = (spec.dt == Dtype::FP16) ? "ptx_mma_m16n8k16_f16_f32"
207 : "ptx_mma_m16n8k16_bf16_f32";
208
209
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<Value*> acc = kb.for_range_reduce_n(
210
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c.i32(0), KT, c.i32(1), acc_init,
211
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* kt, const std::vector<Value*>& cur) -> std::vector<Value*> {
212 1 std::vector<Value*> next = cur;
213
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 c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(kStages - 2)});
214
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
215
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* nk = c.b.build_add(kt, c.i32(kStages - 1));
216
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.
2 kb.if_then(c.b.build_slt(nk, KT), [&] {
217 1 emit_load_stage(c.b.build_and(nk, c.i32(3)), c.b.build_add(c0, nk));
218 1 });
219
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());
220
221
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* st = c.b.build_and(kt, c.i32(3));
222
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* s_ptr = c.b.build_add(smem_dyn, c.b.build_zext_i64(c.b.build_mul(st, c.i32(stage_bytes))));
223
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* xs_ptr = c.b.build_add(s_ptr, kb.const_i64(w_tile_bytes));
224
225
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (int q = 0; q < 2; ++q) {
226
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 Value* u_swz = (q == 0) ? t_q0 : t_q1;
227
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* wa = c.b.build_call("ptx_shared_load_i32x4_indexed", Type::i32x4(),
228
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 {s_ptr, c.b.build_or(ra8, u_swz)});
229
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* wb = c.b.build_call("ptx_shared_load_i32x4_indexed", Type::i32x4(),
230
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 {s_ptr, c.b.build_or(rb8, u_swz)});
231
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
4 Value* a0 = pack_i32x4(c, c.b.build_vextract_lane(wa, 0), c.b.build_vextract_lane(wb, 0),
232
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 c.b.build_vextract_lane(wa, 1), c.b.build_vextract_lane(wb, 1));
233
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
4 Value* a1 = pack_i32x4(c, c.b.build_vextract_lane(wa, 2), c.b.build_vextract_lane(wb, 2),
234
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 c.b.build_vextract_lane(wa, 3), c.b.build_vextract_lane(wb, 3));
235
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int j = 0; j < nt; ++j) {
236 4 const auto ju = static_cast<std::size_t>(j);
237
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 Value* x_idx = c.b.build_or(
238
4/8
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 c.b.build_shl(c.b.build_add(g, c.i32(j * 8)), c.i32(3)), u_swz);
239
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 Value* xv = c.b.build_call("ptx_shared_load_i32x4_indexed", Type::i32x4(),
240 4 {xs_ptr, x_idx});
241
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 next[ju] = c.b.build_call(
242
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 mma_sym, Type::f32x4(),
243
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 {a0, c.b.build_vextract_lane(xv, 0), c.b.build_vextract_lane(xv, 1), next[ju]});
244
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 next[ju] = c.b.build_call(
245
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 mma_sym, Type::f32x4(),
246
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 {a1, c.b.build_vextract_lane(xv, 2), c.b.build_vextract_lane(xv, 3), next[ju]});
247 4 }
248 2 }
249 1 return next;
250
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 });
251
252
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());
253
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
254
255 // Spill-free accumulator -> shared [m][lr] FP32 tile (stride 132).
256
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));
257
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (int j = 0; j < nt; ++j) {
258 2 Value* frag = acc[static_cast<std::size_t>(j)];
259
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* m0_r = c.b.build_add(c.i32(j * 8), t2);
260
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* r0_off = c.b.build_mul(m0_r, c.i32(kTileStride));
261
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* r1_off = c.b.build_add(r0_off, c.i32(kTileStride));
262
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 kb.shared_store_f32_indexed(smem_dyn, c.b.build_add(r0_off, ra), c.b.build_vextract_lane(frag, 0));
263
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 kb.shared_store_f32_indexed(smem_dyn, c.b.build_add(r1_off, ra), c.b.build_vextract_lane(frag, 1));
264
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 kb.shared_store_f32_indexed(smem_dyn, c.b.build_add(r0_off, rb), c.b.build_vextract_lane(frag, 2));
265
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 kb.shared_store_f32_indexed(smem_dyn, c.b.build_add(r1_off, rb), c.b.build_vextract_lane(frag, 3));
266 2 }
267
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
268
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (spec.has_partial) {
270
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Value* mn64 = c.b.build_mul(c.b.build_zext_i64(M), c.b.build_zext_i64(N));
271
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* p_base = c.b.build_add(
272
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ partial, c.b.build_shl(c.b.build_mul(c.b.build_zext_i64(kb.ctaid_y()), mn64), kb.const_i64(2)));
273
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* total = c.b.build_mul(M, c.i32(kCtaCols));
274
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.for_range(tid, total, c.i32(kThreads), [&](Value* idx) {
275 ✗ Value* m = c.b.build_lshr(idx, c.i32(7));
276 ✗ Value* lr = c.b.build_and(idx, c.i32(127));
277 ✗ Value* n = build_w_row(c, cb, lr, N, spec.epi);
278
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ kb.if_then(c.b.build_sge(n, c.i32(0)), [&] {
279 ✗ Value* v = kb.shared_load_f32_indexed(
280 ✗ smem_dyn, c.b.build_add(c.b.build_mul(m, c.i32(kTileStride)), lr));
281 ✗ kb.store_f32(ptr_2d_i64(c, p_base, m, N, n, 2), v, 0);
282 ✗ });
283 ✗ });
284
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 } else if (spec.epi <= 1u) {
285
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* total = c.b.build_mul(M, c.i32(kCtaCols));
286
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.
2 kb.for_range(tid, total, c.i32(kThreads), [&](Value* idx) {
287 1 Value* m = c.b.build_lshr(idx, c.i32(7));
288 1 Value* lr = c.b.build_and(idx, c.i32(127));
289 1 Value* n = build_w_row(c, cb, lr, N, spec.epi);
290
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 kb.if_then(c.b.build_sge(n, c.i32(0)), [&] {
291 2 Value* v = kb.shared_load_f32_indexed(
292 1 smem_dyn, c.b.build_add(c.b.build_mul(m, c.i32(kTileStride)), lr));
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (spec.has_bias) v = c.b.build_add(v, load_bias_f32(c, spec.dt, bias, n));
294 1 emit_out_elem(c, spec.dt, Y, m, n, N, v, nullptr, spec.act, spec.epi);
295 1 });
296 1 });
297 1 } else {
298 ✗ constexpr int H = kCtaCols / 2; // 64
299
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* total = c.b.build_mul(M, c.i32(H));
300
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* out_cols = c.b.build_lshr(N, c.i32(1));
301
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.for_range(tid, total, c.i32(kThreads), [&](Value* idx) {
302 ✗ Value* m = c.b.build_lshr(idx, c.i32(6));
303 ✗ Value* pp = c.b.build_and(idx, c.i32(63));
304 ✗ Value* row_off = c.b.build_mul(m, c.i32(kTileStride));
305
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (spec.epi == 2u) {
306 ✗ Value* n = c.b.build_add(c.b.build_mul(cb, c.i32(kCtaCols)), c.b.build_shl(pp, c.i32(1)));
307
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ kb.if_then(c.b.build_slt(c.b.build_add(n, c.i32(1)), N), [&] {
308 ✗ Value* p2 = c.b.build_add(row_off, c.b.build_shl(pp, c.i32(1)));
309 ✗ Value* va = kb.shared_load_f32_indexed(smem_dyn, p2);
310 ✗ Value* vb = kb.shared_load_f32_indexed(smem_dyn, c.b.build_add(p2, c.i32(1)));
311
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (spec.has_bias) {
312 ✗ va = c.b.build_add(va, load_bias_f32(c, spec.dt, bias, n));
313 ✗ vb = c.b.build_add(vb, load_bias_f32(c, spec.dt, bias, c.b.build_add(n, c.i32(1))));
314 ✗ }
315 ✗ emit_out_elem(c, spec.dt, Y, m, c.b.build_lshr(n, c.i32(1)), out_cols, va, vb, 0, 2);
316 ✗ });
317 ✗ } else {
318 ✗ Value* j = build_w_row(c, cb, pp, N, 3);
319
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ kb.if_then(c.b.build_sge(j, c.i32(0)), [&] {
320 ✗ Value* rp = c.b.build_add(row_off, pp);
321 ✗ Value* va = kb.shared_load_f32_indexed(smem_dyn, rp);
322 ✗ Value* vb = kb.shared_load_f32_indexed(smem_dyn, c.b.build_add(rp, c.i32(H)));
323
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (spec.has_bias) {
324 ✗ va = c.b.build_add(va, load_bias_f32(c, spec.dt, bias, j));
325 ✗ vb = c.b.build_add(vb, load_bias_f32(c, spec.dt, bias, c.b.build_add(out_cols, j)));
326 ✗ }
327 ✗ emit_out_elem(c, spec.dt, Y, m, j, out_cols, va, vb, 0, 3);
328 ✗ });
329 }
330 ✗ });
331 }
332
333
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_ret_void();
334
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 fn->rebuild_cfg_predecessors();
335 1 return fn;
336 1 }
337
338 } // namespace brotensor::kernels
339