src/kernels/mir_gemm_mma_nn.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_gemm_mma_nn.h" | ||
| 2 | #include "mir_gemm_mma.h" | ||
| 3 | #include "mir_common.h" | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <stdexcept> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | namespace brotensor::kernels { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | constexpr std::uint32_t kNnBk = 32u; | ||
| 15 | constexpr std::uint32_t kNnStages = 3u; | ||
| 16 | constexpr std::uint32_t kNnL2GroupM = 8u; | ||
| 17 | |||
| 18 | // A tile has 32 FP16/BF16 cols -> 64 bytes/row -> 4 x 16B chunks per row. | ||
| 19 | 6 | Value* swz_a_bytes(Ctx& c, Value* row, Value* ch) { | |
| 20 | 6 | Value* swz = c.b.build_and(c.b.build_ashr(row, c.i32(1)), c.i32(3)); | |
| 21 | 12 | return c.b.build_or(c.b.build_shl(row, c.i32(6)), | |
| 22 | 6 | c.b.build_shl(c.b.build_xor(ch, swz), c.i32(4))); | |
| 23 | } | ||
| 24 | |||
| 25 | // B tile has BN cols: bn==128 -> 256 bytes/row (16 chunks), bn==64 -> 128 bytes/row (8 chunks). | ||
| 26 | 4 | Value* swz_b_bytes(Ctx& c, Value* k_row, Value* n_ch, int bn_shift) { | |
| 27 | 4 | Value* swz = c.b.build_and(k_row, c.i32(7)); | |
| 28 | 8 | return c.b.build_or(c.b.build_shl(k_row, c.i32(bn_shift)), | |
| 29 | 4 | c.b.build_shl(c.b.build_xor(n_ch, swz), c.i32(4))); | |
| 30 | } | ||
| 31 | |||
| 32 | 36 | Value* ptr_2d_i64(Ctx& c, Value* base, Value* row, Value* ld, Value* col) { | |
| 33 | 72 | Value* elem = c.b.build_add( | |
| 34 | 36 | c.b.build_mul(c.b.build_zext_i64(row), c.b.build_zext_i64(ld)), | |
| 35 | 36 | c.b.build_zext_i64(col)); | |
| 36 | 36 | return c.b.build_add(base, c.b.build_shl(elem, c.kb.const_i64(1))); | |
| 37 | } | ||
| 38 | |||
| 39 | 32 | Value* pack_pair_i32(Ctx& c, Dtype dt, Value* v0, Value* v1) { | |
| 40 | 32 | if (dt == Dtype::FP16) return c.kb.pack_f16x2(v0, v1); | |
| 41 | ✗ | Value* b0 = c.b.build_and(c.kb.f32_to_bf16(v0), c.i32(0xffff)); | |
| 42 | ✗ | Value* b1 = c.b.build_shl(c.kb.f32_to_bf16(v1), c.i32(16)); | |
| 43 | ✗ | return c.b.build_or(b0, b1); | |
| 44 | 32 | } | |
| 45 | |||
| 46 | } // namespace | ||
| 47 | |||
| 48 | 1 | Function* build_gemm_mma_nn_mir(Module& mod, const GemmMmaNnSpec& spec) { | |
| 49 | 1 | if (spec.dt != Dtype::FP16 && spec.dt != Dtype::BF16) { | |
| 50 | ✗ | throw std::runtime_error("build_gemm_mma_nn_mir: only FP16/BF16 supported"); | |
| 51 | } | ||
| 52 | 1 | const GemmMmaCfg& cfg = kGemmMmaCfgs[std::min<std::uint8_t>(spec.cfg, 3u)]; | |
| 53 | 1 | const int bm = static_cast<int>(cfg.bm); | |
| 54 | 1 | const int bn = static_cast<int>(cfg.bn); | |
| 55 | 1 | const int warps_n = static_cast<int>(cfg.warps_n); | |
| 56 | 1 | const int threads = static_cast<int>(cfg.threads); | |
| 57 | 1 | const int wm = bm / static_cast<int>(cfg.warps_m); | |
| 58 | 1 | const int wn = bn / warps_n; | |
| 59 | 1 | const int mt = wm / 16; | |
| 60 | 1 | const int nt = wn / 8; | |
| 61 | 1 | const int a_chunks = (bm * 4) / threads; | |
| 62 | 1 | const int bn_ch = bn / 8; | |
| 63 | 1 | const int bn_ch_shift = (bn == 128) ? 4 : 3; | |
| 64 | 1 | const int bn_row_shift = (bn == 128) ? 8 : 7; // log2(bn * 2 bytes) | |
| 65 | 1 | const int b_chunks = (static_cast<int>(kNnBk) * bn_ch) / threads; | |
| 66 | 1 | const int num_accs = mt * nt; | |
| 67 | 1 | const int a_stage_bytes = bm * static_cast<int>(kNnBk) * 2; | |
| 68 | 1 | const int b_stage_bytes = static_cast<int>(kNnBk) * bn * 2; | |
| 69 | |||
| 70 | 5 | std::vector<Type> param_types = {Type::ptr(), Type::ptr(), Type::ptr(), | |
| 71 | 4 | Type::i32(), Type::i32(), Type::i32(), Type::i32()}; | |
| 72 | 1 | const int extra = (spec.batched ? 3 : 0) + (spec.strided ? 3 : 0); | |
| 73 | 1 | for (int i = 0; i < extra; ++i) param_types.push_back(Type::i32()); | |
| 74 | 1 | Function* fn = mod.create_function(spec.f32_out ? "mir_gemm_mma_nn_f32" : "mir_gemm_mma_nn", | |
| 75 | 1 | Type::void_type(), param_types); | |
| 76 | |||
| 77 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 78 | 1 | Ctx c(kb); | |
| 79 | 1 | auto p = entry_params(c, fn); | |
| 80 | // p: A(0) B(1) C(2) M(3) N(4) K(5) grid_m(6) [sA sB sC] [lda ldb ldc] | ||
| 81 | 1 | Value* A = p[0]; | |
| 82 | 1 | Value* B = p[1]; | |
| 83 | 1 | Value* C = p[2]; | |
| 84 | 1 | if (spec.batched) { | |
| 85 | ✗ | Value* bz64 = c.b.build_zext_i64(kb.ctaid_z()); | |
| 86 | ✗ | auto shift_batch = [&](Value* base, Value* stride32, int shift) { | |
| 87 | ✗ | Value* elems = c.b.build_mul(bz64, c.b.build_zext_i64(stride32)); | |
| 88 | ✗ | return c.b.build_add(base, c.b.build_shl(elems, kb.const_i64(shift))); | |
| 89 | }; | ||
| 90 | ✗ | A = shift_batch(A, p[7], 1); | |
| 91 | ✗ | B = shift_batch(B, p[8], 1); | |
| 92 | ✗ | C = shift_batch(C, p[9], spec.f32_out ? 2 : 1); | |
| 93 | ✗ | } | |
| 94 | 1 | Value* M = p[3]; | |
| 95 | 1 | Value* N = p[4]; | |
| 96 | 1 | Value* K = p[5]; | |
| 97 | 1 | Value* grid_m = p[6]; | |
| 98 | 1 | const std::size_t ld0 = spec.batched ? 10u : 7u; | |
| 99 | 1 | Value* lda = spec.strided ? p[ld0] : K; | |
| 100 | 1 | Value* ldb = spec.strided ? p[ld0 + 1] : N; | |
| 101 | 1 | Value* ldc = spec.strided ? p[ld0 + 2] : N; | |
| 102 | |||
| 103 | 1 | Value* smem = kb.shared_alloc_u16(kNnStages * (cfg.bm + cfg.bn) * kNnBk); | |
| 104 | 1 | Value* sA_base = c.b.build_trunc_i32(smem); | |
| 105 | 1 | Value* sB_base = c.b.build_add(sA_base, c.i32(static_cast<int>(kNnStages) * a_stage_bytes)); | |
| 106 | |||
| 107 | 1 | Value* tid = kb.tid_x(); | |
| 108 | 1 | Value* lane = c.b.build_and(tid, c.i32(31)); | |
| 109 | 1 | Value* warp = c.b.build_lshr(tid, c.i32(5)); | |
| 110 | 1 | Value* w_m = c.b.build_udiv(warp, c.i32(warps_n)); | |
| 111 | 1 | Value* w_n = c.b.build_umod(warp, c.i32(warps_n)); | |
| 112 | |||
| 113 | 1 | Value* bx = kb.ctaid_x(); | |
| 114 | 1 | Value* by = kb.ctaid_y(); | |
| 115 | 1 | Value* tile_m = by; | |
| 116 | 1 | Value* tile_n = bx; | |
| 117 | 1 | if (spec.swizzle_l2) { | |
| 118 | ✗ | Value* gn = kb.nctaid_x(); | |
| 119 | ✗ | Value* linear = c.b.build_add(c.b.build_mul(by, gn), bx); | |
| 120 | ✗ | Value* per_slab = c.b.build_mul(c.i32(static_cast<int>(kNnL2GroupM)), gn); | |
| 121 | ✗ | Value* slab = c.b.build_udiv(linear, per_slab); | |
| 122 | ✗ | Value* in_slab = c.b.build_umod(linear, per_slab); | |
| 123 | ✗ | Value* m_start = c.b.build_mul(slab, c.i32(static_cast<int>(kNnL2GroupM))); | |
| 124 | ✗ | Value* slab_h = smin_i32(c, c.i32(static_cast<int>(kNnL2GroupM)), c.b.build_sub(grid_m, m_start)); | |
| 125 | ✗ | tile_n = c.b.build_udiv(in_slab, slab_h); | |
| 126 | ✗ | tile_m = c.b.build_add(m_start, c.b.build_umod(in_slab, slab_h)); | |
| 127 | ✗ | } | |
| 128 | 1 | Value* m0 = c.b.build_mul(tile_m, c.i32(bm)); | |
| 129 | 1 | Value* n0 = c.b.build_mul(tile_n, c.i32(bn)); | |
| 130 | 1 | Value* KT = c.b.build_lshr(c.b.build_add(K, c.i32(31)), c.i32(5)); | |
| 131 | |||
| 132 | // Precompute cp.async source/dest addresses + single-i32 guard bitmask. | ||
| 133 | 1 | Value* a_lane_k = c.b.build_shl(c.b.build_and(tid, c.i32(3)), c.i32(3)); | |
| 134 | 1 | Value* guard_mask = spec.aligned_mnk ? nullptr : c.i32(0); | |
| 135 | 1 | std::vector<Value*> a_s_off(static_cast<std::size_t>(a_chunks)); | |
| 136 | 1 | std::vector<Value*> a_g_base(static_cast<std::size_t>(a_chunks)); | |
| 137 | 3 | for (int i = 0; i < a_chunks; ++i) { | |
| 138 | 2 | const auto iu = static_cast<std::size_t>(i); | |
| 139 | 2 | Value* lin = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * threads)); | |
| 140 | 2 | Value* row = c.b.build_lshr(lin, c.i32(2)); | |
| 141 | 2 | Value* ch = c.b.build_and(lin, c.i32(3)); | |
| 142 | 2 | a_s_off[iu] = c.b.build_add(sA_base, swz_a_bytes(c, row, ch)); | |
| 143 | 2 | Value* gr = c.b.build_add(m0, row); | |
| 144 | 2 | a_g_base[iu] = ptr_2d_i64(c, A, gr, lda, c.b.build_shl(ch, c.i32(3))); | |
| 145 | 2 | if (!spec.aligned_mnk) { | |
| 146 | ✗ | Value* bit = c.b.build_select(c.b.build_slt(gr, M), c.i32(1 << i), c.i32(0)); | |
| 147 | ✗ | guard_mask = c.b.build_or(guard_mask, bit); | |
| 148 | ✗ | } | |
| 149 | 2 | } | |
| 150 | |||
| 151 | 1 | std::vector<Value*> b_s_off(static_cast<std::size_t>(b_chunks)); | |
| 152 | 1 | std::vector<Value*> b_g_base(static_cast<std::size_t>(b_chunks)); | |
| 153 | 1 | std::vector<Value*> b_k_row(static_cast<std::size_t>(b_chunks)); | |
| 154 | 3 | for (int i = 0; i < b_chunks; ++i) { | |
| 155 | 2 | const auto iu = static_cast<std::size_t>(i); | |
| 156 | 2 | Value* lin = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * threads)); | |
| 157 | 2 | Value* k_row = c.b.build_lshr(lin, c.i32(bn_ch_shift)); | |
| 158 | 2 | Value* n_ch = c.b.build_and(lin, c.i32(bn_ch - 1)); | |
| 159 | 2 | b_s_off[iu] = c.b.build_add(sB_base, swz_b_bytes(c, k_row, n_ch, bn_row_shift)); | |
| 160 | 2 | b_k_row[iu] = k_row; | |
| 161 | 2 | Value* gcol = c.b.build_add(n0, c.b.build_shl(n_ch, c.i32(3))); | |
| 162 | 2 | b_g_base[iu] = ptr_2d_i64(c, B, k_row, ldb, gcol); | |
| 163 | 2 | if (!spec.aligned_mnk) { | |
| 164 | ✗ | Value* bit = c.b.build_select(c.b.build_slt(gcol, N), c.i32(1 << (a_chunks + i)), c.i32(0)); | |
| 165 | ✗ | guard_mask = c.b.build_or(guard_mask, bit); | |
| 166 | ✗ | } | |
| 167 | 2 | } | |
| 168 | |||
| 169 | // Precompute ldmatrix addresses for kk=0; kk=1 is xor 32 for A and +(16*bn*2) = xor (16<<bn_row_shift) for B. | ||
| 170 | 1 | Value* a_lane_row = c.b.build_and(lane, c.i32(15)); | |
| 171 | 1 | Value* a_lane_ch = c.b.build_lshr(lane, c.i32(4)); | |
| 172 | 1 | Value* wm_base = c.b.build_mul(w_m, c.i32(wm)); | |
| 173 | 1 | std::vector<Value*> a_ld_off(static_cast<std::size_t>(mt)); | |
| 174 | 5 | for (int i = 0; i < mt; ++i) { | |
| 175 | 4 | Value* row = c.b.build_add(c.b.build_add(wm_base, c.i32(i * 16)), a_lane_row); | |
| 176 | 4 | a_ld_off[static_cast<std::size_t>(i)] = c.b.build_add(sA_base, swz_a_bytes(c, row, a_lane_ch)); | |
| 177 | 4 | } | |
| 178 | |||
| 179 | 1 | Value* mi = c.b.build_lshr(lane, c.i32(3)); | |
| 180 | 2 | Value* bk_row0 = c.b.build_add(c.b.build_shl(c.b.build_and(mi, c.i32(1)), c.i32(3)), | |
| 181 | 1 | c.b.build_and(lane, c.i32(7))); | |
| 182 | 1 | Value* b_lane_nch = c.b.build_lshr(mi, c.i32(1)); | |
| 183 | 1 | Value* wn_base_ch = c.b.build_lshr(c.b.build_mul(w_n, c.i32(wn)), c.i32(3)); | |
| 184 | 1 | std::vector<Value*> b_ld_off(static_cast<std::size_t>(nt / 2)); | |
| 185 | 3 | for (int j = 0; j < nt / 2; ++j) { | |
| 186 | 2 | Value* n_ch = c.b.build_add(c.b.build_add(wn_base_ch, c.i32(j * 2)), b_lane_nch); | |
| 187 | 2 | b_ld_off[static_cast<std::size_t>(j)] = c.b.build_add( | |
| 188 | 2 | sB_base, swz_b_bytes(c, bk_row0, n_ch, bn_row_shift)); | |
| 189 | 2 | } | |
| 190 | 1 | const int b_kk1_off = 16 << bn_row_shift; // +16 K-rows in byte offset (swz unchanged) | |
| 191 | |||
| 192 | 1 | Value* N64 = c.b.build_zext_i64(ldb); | |
| 193 | 8 | auto emit_load_stage = [&](int stage, Value* kt_idx) { | |
| 194 | 7 | Value* k0 = c.b.build_shl(kt_idx, c.i32(5)); | |
| 195 | 7 | Value* a_k_bytes = c.b.build_shl(c.b.build_zext_i64(kt_idx), kb.const_i64(6)); // 32 elems * 2B | |
| 196 | 7 | Value* b_k_bytes = c.b.build_shl(c.b.build_mul(c.b.build_zext_i64(k0), N64), kb.const_i64(1)); | |
| 197 | 7 | Value* a_k_ok_mask = spec.aligned_mnk | |
| 198 | ? nullptr | ||
| 199 | ✗ | : c.b.build_select(c.b.build_slt(c.b.build_add(k0, a_lane_k), K), guard_mask, c.i32(0)); | |
| 200 | 7 | Value* a_st_off = c.i32(stage * a_stage_bytes); | |
| 201 | 7 | Value* b_st_off = c.i32(stage * b_stage_bytes); | |
| 202 | |||
| 203 | 21 | for (int i = 0; i < a_chunks; ++i) { | |
| 204 | 14 | const auto iu = static_cast<std::size_t>(i); | |
| 205 | 14 | Value* dst = (stage == 0) ? a_s_off[iu] : c.b.build_add(a_s_off[iu], a_st_off); | |
| 206 | 14 | Value* src = c.b.build_add(a_g_base[iu], a_k_bytes); | |
| 207 | 14 | if (spec.aligned_mnk) { | |
| 208 | 14 | c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, src}); | |
| 209 | 14 | } else { | |
| 210 | ✗ | Value* ok = c.b.build_ne(c.b.build_and(a_k_ok_mask, c.i32(1 << i)), c.i32(0)); | |
| 211 | ✗ | c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), | |
| 212 | ✗ | {dst, c.b.build_select(ok, src, A), c.b.build_select(ok, c.i32(16), c.i32(0))}); | |
| 213 | } | ||
| 214 | 14 | } | |
| 215 | 21 | for (int i = 0; i < b_chunks; ++i) { | |
| 216 | 14 | const auto iu = static_cast<std::size_t>(i); | |
| 217 | 14 | Value* dst = (stage == 0) ? b_s_off[iu] : c.b.build_add(b_s_off[iu], b_st_off); | |
| 218 | 14 | Value* src = c.b.build_add(b_g_base[iu], b_k_bytes); | |
| 219 | 14 | if (spec.aligned_mnk) { | |
| 220 | 14 | c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), {dst, src}); | |
| 221 | 14 | } else { | |
| 222 | ✗ | Value* k_ok = c.b.build_slt(c.b.build_add(k0, b_k_row[iu]), K); | |
| 223 | ✗ | Value* n_ok = c.b.build_ne(c.b.build_and(guard_mask, c.i32(1 << (a_chunks + i))), c.i32(0)); | |
| 224 | ✗ | Value* ok = c.b.build_and(k_ok, n_ok); | |
| 225 | ✗ | c.b.build_call("ptx_cp_async_cg_16", Type::void_type(), | |
| 226 | ✗ | {dst, c.b.build_select(ok, src, B), c.b.build_select(ok, c.i32(16), c.i32(0))}); | |
| 227 | } | ||
| 228 | 14 | } | |
| 229 | 7 | }; | |
| 230 | |||
| 231 | 1 | const char* mma_sym = (spec.dt == Dtype::FP16) ? "ptx_mma_m16n8k16_f16_f32" | |
| 232 | : "ptx_mma_m16n8k16_bf16_f32"; | ||
| 233 | |||
| 234 | 8 | auto emit_compute_stage = [&](int stage, std::vector<Value*>& accs) { | |
| 235 | 7 | Value* a_st_off = c.i32(stage * a_stage_bytes); | |
| 236 | 7 | Value* b_st_off = c.i32(stage * b_stage_bytes); | |
| 237 | 21 | for (int kk = 0; kk < 2; ++kk) { | |
| 238 | 70 | auto a_addr = [&](int i) -> Value* { | |
| 239 | 56 | Value* base = a_ld_off[static_cast<std::size_t>(i)]; | |
| 240 | 56 | Value* p0 = (stage == 0) ? base : c.b.build_add(base, a_st_off); | |
| 241 | 56 | return (kk == 0) ? p0 : c.b.build_xor(p0, c.i32(32)); | |
| 242 | }; | ||
| 243 | 42 | auto b_addr = [&](int j) -> Value* { | |
| 244 | 28 | Value* base = b_ld_off[static_cast<std::size_t>(j)]; | |
| 245 | 28 | Value* p0 = (stage == 0) ? base : c.b.build_add(base, b_st_off); | |
| 246 | 28 | return (kk == 0) ? p0 : c.b.build_add(p0, c.i32(b_kk1_off)); | |
| 247 | }; | ||
| 248 | |||
| 249 | 14 | std::vector<Value*> b0(static_cast<std::size_t>(nt)); | |
| 250 | 14 | std::vector<Value*> b1(static_cast<std::size_t>(nt)); | |
| 251 | 42 | for (int j = 0; j < nt / 2; ++j) { | |
| 252 | 28 | Value* r = c.b.build_call("ptx_ldmatrix_x4_trans", Type::i32x4(), {b_addr(j)}); | |
| 253 | 28 | const auto j2 = static_cast<std::size_t>(2 * j); | |
| 254 | 28 | b0[j2] = c.b.build_vextract_lane(r, 0); | |
| 255 | 28 | b1[j2] = c.b.build_vextract_lane(r, 1); | |
| 256 | 28 | b0[j2 + 1u] = c.b.build_vextract_lane(r, 2); | |
| 257 | 28 | b1[j2 + 1u] = c.b.build_vextract_lane(r, 3); | |
| 258 | 28 | } | |
| 259 | |||
| 260 | 14 | Value* af_cur = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {a_addr(0)}); | |
| 261 | 70 | for (int i = 0; i < mt; ++i) { | |
| 262 | 98 | Value* af_next = (i + 1 < mt) | |
| 263 | 42 | ? c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {a_addr(i + 1)}) | |
| 264 | : nullptr; | ||
| 265 | 280 | for (int j = 0; j < nt; ++j) { | |
| 266 | 224 | const auto idx = static_cast<std::size_t>(i * nt + j); | |
| 267 | 224 | accs[idx] = c.b.build_call( | |
| 268 | 224 | mma_sym, Type::f32x4(), | |
| 269 | 224 | {af_cur, b0[static_cast<std::size_t>(j)], b1[static_cast<std::size_t>(j)], accs[idx]}); | |
| 270 | 224 | } | |
| 271 | 56 | af_cur = af_next; | |
| 272 | 56 | } | |
| 273 | 14 | } | |
| 274 | 7 | }; | |
| 275 | |||
| 276 | 2 | kb.if_then(c.b.build_slt(c.i32(0), KT), [&] { emit_load_stage(0, c.i32(0)); }); | |
| 277 | 1 | c.b.build_call("ptx_cp_async_commit", Type::void_type()); | |
| 278 | 2 | kb.if_then(c.b.build_slt(c.i32(1), KT), [&] { emit_load_stage(1, c.i32(1)); }); | |
| 279 | 1 | c.b.build_call("ptx_cp_async_commit", Type::void_type()); | |
| 280 | |||
| 281 | 1 | std::vector<Value*> state_init; | |
| 282 | 1 | state_init.reserve(static_cast<std::size_t>(num_accs + 1)); | |
| 283 | 17 | for (int i = 0; i < num_accs; ++i) state_init.push_back(c.b.build_vzero(Type::f32x4())); | |
| 284 | 1 | state_init.push_back(c.i32(0)); | |
| 285 | |||
| 286 | 1 | Value* kt_limit = c.b.build_sub(KT, c.i32(4)); | |
| 287 | 1 | std::vector<Value*> state = kb.for_range_reduce_n( | |
| 288 | 1 | c.i32(0), kt_limit, c.i32(3), state_init, | |
| 289 | 2 | [&](Value* kt, const std::vector<Value*>& cur) -> std::vector<Value*> { | |
| 290 | 1 | std::vector<Value*> accs(cur.begin(), cur.begin() + num_accs); | |
| 291 | 4 | for (int s = 0; s < 3; ++s) { | |
| 292 | 3 | c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(1)}); | |
| 293 | 3 | kb.sync(); | |
| 294 | 3 | emit_load_stage((s + 2) % 3, c.b.build_add(kt, c.i32(s + 2))); | |
| 295 | 3 | c.b.build_call("ptx_cp_async_commit", Type::void_type()); | |
| 296 | 3 | emit_compute_stage(s, accs); | |
| 297 | 3 | } | |
| 298 | 1 | accs.push_back(c.b.build_add(kt, c.i32(3))); | |
| 299 | 1 | return accs; | |
| 300 | 1 | }); | |
| 301 | |||
| 302 | 1 | Value* kt_rem = state.back(); | |
| 303 | 1 | std::vector<Value*> accs(state.begin(), state.begin() + num_accs); | |
| 304 | 1 | const std::vector<Type> acc_types(static_cast<std::size_t>(num_accs), Type::f32x4()); | |
| 305 | 5 | for (int d = 0; d < 4; ++d) { | |
| 306 | 4 | const int st_read = d % 3; | |
| 307 | 4 | const int st_write = (d + 2) % 3; | |
| 308 | 4 | Value* kcur = (d == 0) ? kt_rem : c.b.build_add(kt_rem, c.i32(d)); | |
| 309 | 4 | accs = emit_if_else_values( | |
| 310 | 4 | c, c.b.build_slt(kcur, KT), acc_types, | |
| 311 | 12 | [&]() -> std::vector<Value*> { | |
| 312 | 8 | std::vector<Value*> cur_accs = accs; | |
| 313 | 8 | c.b.build_call("ptx_cp_async_wait_group", Type::void_type(), {c.i32(1)}); | |
| 314 | 4 | kb.sync(); | |
| 315 | 4 | if (d == 0 || d == 1) { | |
| 316 | 4 | Value* knext = c.b.build_add(kcur, c.i32(2)); | |
| 317 | 4 | kb.if_then(c.b.build_slt(knext, KT), [&] { emit_load_stage(st_write, knext); }); | |
| 318 | 2 | } | |
| 319 | 2 | c.b.build_call("ptx_cp_async_commit", Type::void_type()); | |
| 320 | 4 | emit_compute_stage(st_read, cur_accs); | |
| 321 | 4 | return cur_accs; | |
| 322 | 12 | }, | |
| 323 | 8 | [&]() -> std::vector<Value*> { return accs; }); | |
| 324 | 4 | } | |
| 325 | 1 | c.b.build_call("ptx_cp_async_wait_all", Type::void_type()); | |
| 326 | |||
| 327 | 1 | Value* g = c.b.build_lshr(lane, c.i32(2)); | |
| 328 | 1 | Value* t2 = c.b.build_shl(c.b.build_and(lane, c.i32(3)), c.i32(1)); | |
| 329 | 1 | Value* wn_base = c.b.build_mul(w_n, c.i32(wn)); | |
| 330 | 5 | for (int j = 0; j < nt; ++j) { | |
| 331 | 4 | Value* col = c.b.build_add(c.b.build_add(n0, wn_base), c.b.build_add(c.i32(j * 8), t2)); | |
| 332 | 8 | auto emit_col = [&] { | |
| 333 | 20 | for (int i = 0; i < mt; ++i) { | |
| 334 | 16 | Value* frag = accs[static_cast<std::size_t>(i * nt + j)]; | |
| 335 | 48 | for (int h = 0; h < 2; ++h) { | |
| 336 | 64 | Value* row = c.b.build_add(c.b.build_add(m0, wm_base), | |
| 337 | 32 | c.b.build_add(c.i32(i * 16 + h * 8), g)); | |
| 338 | 32 | Value* v0 = c.b.build_vextract_lane(frag, 2 * h); | |
| 339 | 32 | Value* v1 = c.b.build_vextract_lane(frag, 2 * h + 1); | |
| 340 | 64 | auto emit_st = [&] { | |
| 341 | 32 | if (spec.f32_out) { | |
| 342 | ✗ | Value* elem = c.b.build_add( | |
| 343 | ✗ | c.b.build_mul(c.b.build_zext_i64(row), c.b.build_zext_i64(ldc)), | |
| 344 | ✗ | c.b.build_zext_i64(col)); | |
| 345 | ✗ | Value* dst = c.b.build_add(C, c.b.build_shl(elem, kb.const_i64(2))); | |
| 346 | ✗ | kb.store_f32(dst, v0, 0); | |
| 347 | ✗ | kb.store_f32(dst, v1, 4); | |
| 348 | ✗ | return; | |
| 349 | } | ||
| 350 | 32 | kb.store_i32(ptr_2d_i64(c, C, row, ldc, col), pack_pair_i32(c, spec.dt, v0, v1)); | |
| 351 | 32 | }; | |
| 352 | 32 | if (spec.aligned_mnk) emit_st(); | |
| 353 | ✗ | else kb.if_then(c.b.build_slt(row, M), emit_st); | |
| 354 | 32 | } | |
| 355 | 16 | } | |
| 356 | 4 | }; | |
| 357 | 4 | if (spec.aligned_mnk) emit_col(); | |
| 358 | ✗ | else kb.if_then(c.b.build_slt(col, N), emit_col); | |
| 359 | 4 | } | |
| 360 | |||
| 361 | 1 | c.b.build_ret_void(); | |
| 362 | 1 | fn->rebuild_cfg_predecessors(); | |
| 363 | 1 | return fn; | |
| 364 | 1 | } | |
| 365 | |||
| 366 | } // namespace brotensor::kernels | ||
| 367 |