src/kernels/mir_conv_igemm_mma.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_conv_igemm_mma.h" | ||
| 2 | |||
| 3 | #include "mir_common.h" | ||
| 4 | #include "mir_conv_mma.h" | ||
| 5 | |||
| 6 | #include <array> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | namespace brotensor::kernels { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | constexpr int kBk = 32; | ||
| 15 | constexpr int kStages = 2; | ||
| 16 | |||
| 17 | // Shared tiles are rows of 32 halves (64 bytes, four 16-byte chunks), the | ||
| 18 | // chunk XOR-swizzled by row so ldmatrix reads are conflict-free. | ||
| 19 | 8 | Value* swz_bytes(Ctx& c, Value* row, Value* chunk) { | |
| 20 | 8 | Value* row_off = c.b.build_shl(row, c.i32(6)); | |
| 21 | 8 | Value* swz = c.b.build_and(c.b.build_ashr(row, c.i32(1)), c.i32(3)); | |
| 22 | 8 | return c.b.build_or(row_off, c.b.build_shl(c.b.build_xor(chunk, swz), c.i32(4))); | |
| 23 | } | ||
| 24 | |||
| 25 | 10 | Value* swz_chunk(Ctx& c, Value* row, Value* chunk) { | |
| 26 | 10 | Value* row_off = c.b.build_shl(row, c.i32(2)); | |
| 27 | 10 | Value* swz = c.b.build_and(c.b.build_ashr(row, c.i32(1)), c.i32(3)); | |
| 28 | 10 | return c.b.build_or(row_off, c.b.build_xor(chunk, swz)); | |
| 29 | } | ||
| 30 | |||
| 31 | 24 | Value* make_i32x4(Ctx& c, const std::vector<Value*>& w, std::size_t o) { | |
| 32 | 24 | Value* v = c.b.build_vzero(Type::i32x4()); | |
| 33 | 120 | for (std::uint32_t j = 0; j < 4; ++j) v = c.b.build_vinsert_lane(v, w[o + j], j); | |
| 34 | 24 | return v; | |
| 35 | } | ||
| 36 | |||
| 37 | } // namespace | ||
| 38 | |||
| 39 | 2 | Function* build_conv2d_igemm_mma_mir(Module& mod, bool has_bias, bool aligned_k4, int cfg_id, bool gemm, | |
| 40 | bool split) { | ||
| 41 | 2 | const Conv2dMmaCfg& cfg = kConv2dMmaCfgs[cfg_id == 0 ? 0 : 1]; | |
| 42 | 2 | const int bm = static_cast<int>(cfg.bm); | |
| 43 | 2 | const int bn = static_cast<int>(cfg.bn); | |
| 44 | 2 | const int warps_n = static_cast<int>(cfg.warps_n); | |
| 45 | 2 | const int threads = static_cast<int>(cfg.threads); | |
| 46 | 2 | const int wm = bm / static_cast<int>(cfg.warps_m); | |
| 47 | 2 | const int wn = bn / warps_n; | |
| 48 | 2 | const int mt = wm / 16; | |
| 49 | 2 | const int nt = wn / 8; | |
| 50 | 2 | const int num_accs = mt * nt; | |
| 51 | 2 | const int b_chunks = (bn * 4) / threads; | |
| 52 | 2 | const int a_stage_bytes = bm * kBk * 2; | |
| 53 | 2 | const int b_stage_bytes = bn * kBk * 2; | |
| 54 | 2 | const int a_stage_chunks = bm * 4; | |
| 55 | 2 | const int b_stage_chunks = bn * 4; | |
| 56 | 2 | const int sB_chunk_base = kStages * a_stage_chunks; | |
| 57 | // split: a second copy of both tiles, after the first, holds the lo halves. | ||
| 58 | 2 | const int lo_chunk_base = kStages * (a_stage_chunks + b_stage_chunks); | |
| 59 | 2 | const int lo_byte_base = lo_chunk_base * 16; | |
| 60 | 2 | const int words_per_set = 8 + 4 * b_chunks; | |
| 61 | |||
| 62 | 2 | std::vector<Type> params(5, Type::ptr()); | |
| 63 | 2 | params.insert(params.end(), 20, Type::i32()); | |
| 64 | 2 | Function* fn = mod.create_function(split ? "mir_conv2d_igemm_mma_x3" : "mir_conv2d_igemm_mma", | |
| 65 | 2 | Type::void_type(), params); | |
| 66 | 2 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 67 | 2 | Ctx c(kb); | |
| 68 | 2 | auto p = entry_params(c, fn); | |
| 69 | 2 | Value* X = p[0]; | |
| 70 | 2 | Value* Wt = p[1]; | |
| 71 | 2 | Value* bias = p[2]; | |
| 72 | 2 | Value* Cin = p[5]; | |
| 73 | 2 | Value* H = p[6]; | |
| 74 | 2 | Value* W = p[7]; | |
| 75 | 2 | Value* Cout = p[8]; | |
| 76 | 2 | Value* kH = p[9]; | |
| 77 | 2 | Value* kW = p[10]; | |
| 78 | 2 | Value* sh = p[11]; | |
| 79 | 2 | Value* sw = p[12]; | |
| 80 | 2 | Value* ph = p[13]; | |
| 81 | 2 | Value* pw = p[14]; | |
| 82 | 2 | Value* dh = p[15]; | |
| 83 | 2 | Value* dw = p[16]; | |
| 84 | 2 | Value* Wo = p[18]; | |
| 85 | 2 | Value* M = p[19]; | |
| 86 | 2 | Value* Ktot = p[20]; | |
| 87 | 2 | Value* KHW = p[21]; | |
| 88 | 2 | Value* HWo = p[22]; | |
| 89 | |||
| 90 | // Split-K: block z sums K in [z kz, min(K, (z+1) kz)). | ||
| 91 | 2 | Value* kz_id = kb.ctaid_z(); | |
| 92 | 2 | Value* split0 = c.b.build_eq(kz_id, c.i32(0)); | |
| 93 | 2 | Value* k_begin = c.b.build_mul(kz_id, p[23]); | |
| 94 | 2 | Value* k_end = smin_i32(c, c.b.build_add(k_begin, p[23]), Ktot); | |
| 95 | 4 | Value* part_off = c.b.build_mul( | |
| 96 | 2 | c.b.build_zext_i64(c.b.build_select(split0, c.i32(0), c.b.build_sub(kz_id, c.i32(1)))), | |
| 97 | 2 | c.b.build_zext_i64(p[24])); | |
| 98 | 2 | Value* Y = c.b.build_select(split0, p[3], c.b.build_add(p[4], c.b.build_shl(part_off, kb.const_i64(2)))); | |
| 99 | 2 | Value* KT = c.b.build_lshr(c.b.build_add(c.b.build_sub(k_end, k_begin), c.i32(kBk - 1)), c.i32(5)); | |
| 100 | |||
| 101 | 2 | Value* smem = kb.shared_alloc_u16(static_cast<std::uint32_t>(kStages * (bm + bn) * kBk * (split ? 2 : 1))); | |
| 102 | 2 | Value* sA_base = c.b.build_trunc_i32(smem); | |
| 103 | 2 | Value* sB_base = c.b.build_add(sA_base, c.i32(kStages * a_stage_bytes)); | |
| 104 | |||
| 105 | 2 | Value* tid = kb.tid_x(); | |
| 106 | 2 | Value* lane = c.b.build_and(tid, c.i32(31)); | |
| 107 | 2 | Value* warp = c.b.build_lshr(tid, c.i32(5)); | |
| 108 | 2 | Value* w_m = c.b.build_udiv(warp, c.i32(warps_n)); | |
| 109 | 2 | Value* w_n = c.b.build_umod(warp, c.i32(warps_n)); | |
| 110 | 2 | Value* m0 = c.b.build_mul(kb.ctaid_x(), c.i32(bm)); | |
| 111 | 2 | Value* n0 = c.b.build_mul(kb.ctaid_y(), c.i32(bn)); | |
| 112 | |||
| 113 | 2 | Value* hw_in = c.b.build_mul(H, W); | |
| 114 | 2 | Value* chw_in = c.b.build_mul(Cin, hw_in); | |
| 115 | |||
| 116 | // A loader: a warp owns k chunk a_ch (8 consecutive k, warp-uniform) of | ||
| 117 | // 32 consecutive pixels a row pass, so a load reads 32 adjacent pixels. | ||
| 118 | // Per pixel, fixed for the whole K loop: the image base plus the | ||
| 119 | // window's top-left offset, and the window's (ih0, iw0); a pixel past M | ||
| 120 | // gets ih0 = -2^30, which no tap brings into [0, H). | ||
| 121 | 2 | Value* a_ch = c.b.build_and(warp, c.i32(3)); | |
| 122 | 2 | Value* a_base_row = (cfg_id == 0) | |
| 123 | 2 | ? lane | |
| 124 | ✗ | : c.b.build_add(c.b.build_shl(c.b.build_lshr(warp, c.i32(2)), c.i32(6)), lane); | |
| 125 | 2 | std::array<Value*, 2> a_chunk_off{}, pix_base{}, pix_ih0{}, pix_iw0{}; | |
| 126 | 6 | for (int r = 0; r < 2; ++r) { | |
| 127 | 4 | Value* row = (r == 0) ? a_base_row : c.b.build_add(a_base_row, c.i32(32)); | |
| 128 | 4 | Value* m_g = c.b.build_add(m0, row); | |
| 129 | 4 | Value* m_ok = c.b.build_slt(m_g, M); | |
| 130 | 4 | Value* safe_m = c.b.build_select(m_ok, m_g, c.i32(0)); | |
| 131 | 4 | Value* n_img = c.b.build_udiv(safe_m, HWo); | |
| 132 | 4 | Value* rem = c.b.build_umod(safe_m, HWo); | |
| 133 | 4 | Value* oh = c.b.build_udiv(rem, Wo); | |
| 134 | 4 | Value* ow = c.b.build_umod(rem, Wo); | |
| 135 | 4 | Value* ih0 = c.b.build_sub(c.b.build_mul(oh, sh), ph); | |
| 136 | 4 | Value* iw0 = c.b.build_sub(c.b.build_mul(ow, sw), pw); | |
| 137 | 4 | const auto ru = static_cast<std::size_t>(r); | |
| 138 | 4 | a_chunk_off[ru] = swz_chunk(c, row, a_ch); | |
| 139 | 4 | pix_ih0[ru] = c.b.build_select(m_ok, ih0, c.i32(-(1 << 30))); | |
| 140 | 4 | pix_iw0[ru] = iw0; | |
| 141 | 8 | pix_base[ru] = c.b.build_add(c.b.build_mul(n_img, chw_in), | |
| 142 | 4 | c.b.build_add(c.b.build_mul(ih0, W), iw0)); | |
| 143 | 4 | } | |
| 144 | |||
| 145 | // Row-major chunk loader (Wt, and X in GEMM mode): chunk i is row r0 + | ||
| 146 | // row of a K-contiguous matrix, 8 consecutive k at ch * 8, so 4 threads | ||
| 147 | // read a row's 128 bytes. | ||
| 148 | struct RowChunks { | ||
| 149 | std::vector<Value*> smem_chunk, row_ptr, row_ok, k_lane; | ||
| 150 | }; | ||
| 151 | 5 | auto row_chunks = [&](int chunks, Value* base, Value* r0, Value* rows, int chunk_base) { | |
| 152 | 3 | RowChunks rc; | |
| 153 | 9 | for (int i = 0; i < chunks; ++i) { | |
| 154 | 6 | Value* lin = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * threads)); | |
| 155 | 6 | Value* row = c.b.build_lshr(lin, c.i32(2)); | |
| 156 | 6 | Value* ch = c.b.build_and(lin, c.i32(3)); | |
| 157 | 6 | rc.smem_chunk.push_back(c.b.build_add(c.i32(chunk_base), swz_chunk(c, row, ch))); | |
| 158 | 6 | Value* g_row = c.b.build_add(r0, row); | |
| 159 | 6 | Value* ok = c.b.build_slt(g_row, rows); | |
| 160 | 6 | Value* safe = c.b.build_select(ok, g_row, c.i32(0)); | |
| 161 | 6 | rc.row_ok.push_back(ok); | |
| 162 | 12 | rc.row_ptr.push_back(c.b.build_add( | |
| 163 | 12 | base, c.b.build_shl(c.b.build_mul(c.b.build_zext_i64(safe), c.b.build_zext_i64(Ktot)), | |
| 164 | 6 | kb.const_i64(2)))); | |
| 165 | 6 | rc.k_lane.push_back(c.b.build_shl(ch, c.i32(3))); | |
| 166 | 6 | } | |
| 167 | 3 | return rc; | |
| 168 | 3 | }; | |
| 169 | 2 | const RowChunks bw_rows = row_chunks(b_chunks, Wt, n0, Cout, sB_chunk_base); | |
| 170 | 2 | const int a_chunks = (bm * 4) / threads; | |
| 171 | 2 | const RowChunks ax_rows = gemm ? row_chunks(a_chunks, X, m0, M, 0) : RowChunks{}; | |
| 172 | |||
| 173 | // ldmatrix lane addresses (kk = 0; kk = 1 is addr ^ 32). | ||
| 174 | 2 | Value* a_lane_row = c.b.build_and(lane, c.i32(15)); | |
| 175 | 2 | Value* a_lane_ch = c.b.build_lshr(lane, c.i32(4)); | |
| 176 | 2 | Value* wm_base = c.b.build_mul(w_m, c.i32(wm)); | |
| 177 | 2 | std::vector<Value*> a_ld_off(static_cast<std::size_t>(mt)); | |
| 178 | 6 | for (int i = 0; i < mt; ++i) { | |
| 179 | 4 | Value* row = c.b.build_add(c.b.build_add(wm_base, c.i32(i * 16)), a_lane_row); | |
| 180 | 4 | a_ld_off[static_cast<std::size_t>(i)] = c.b.build_add(sA_base, swz_bytes(c, row, a_lane_ch)); | |
| 181 | 4 | } | |
| 182 | 2 | Value* mi = c.b.build_lshr(lane, c.i32(3)); | |
| 183 | 4 | Value* b_lane_row = c.b.build_add(c.b.build_shl(c.b.build_lshr(mi, c.i32(1)), c.i32(3)), | |
| 184 | 2 | c.b.build_and(lane, c.i32(7))); | |
| 185 | 2 | Value* b_lane_ch = c.b.build_and(mi, c.i32(1)); | |
| 186 | 2 | Value* wn_base = c.b.build_mul(w_n, c.i32(wn)); | |
| 187 | 2 | std::vector<Value*> b_ld_off(static_cast<std::size_t>(nt / 2)); | |
| 188 | 6 | for (int j = 0; j < nt / 2; ++j) { | |
| 189 | 4 | Value* row = c.b.build_add(c.b.build_add(wn_base, c.i32(j * 16)), b_lane_row); | |
| 190 | 4 | b_ld_off[static_cast<std::size_t>(j)] = c.b.build_add(sB_base, swz_bytes(c, row, b_lane_ch)); | |
| 191 | 4 | } | |
| 192 | |||
| 193 | // Pack 8 FP32 into the tile words: 4 FP16 pairs, or (split) 4 BF16 hi | ||
| 194 | // pairs then 4 BF16 lo pairs, x = hi + lo to ~17 bits. | ||
| 195 | 30 | auto pack8 = [&](const std::array<Value*, 8>& v) -> std::vector<Value*> { | |
| 196 | 28 | std::vector<Value*> out; | |
| 197 | 28 | if (!split) { | |
| 198 | 60 | for (int pr = 0; pr < 4; ++pr) out.push_back(kb.pack_f16x2(v[2 * pr], v[2 * pr + 1])); | |
| 199 | 12 | return out; | |
| 200 | } | ||
| 201 | 16 | std::array<Value*, 8> hb{}, lb{}; | |
| 202 | 144 | for (std::size_t e = 0; e < 8; ++e) { | |
| 203 | 128 | hb[e] = kb.f32_to_bf16(v[e]); | |
| 204 | 128 | lb[e] = kb.f32_to_bf16(c.b.build_sub(v[e], kb.bf16_to_f32(hb[e]))); | |
| 205 | 128 | } | |
| 206 | 144 | auto pair = [&](Value* lo16, Value* hi16) { | |
| 207 | 128 | return c.b.build_or(c.b.build_and(lo16, c.i32(0xffff)), c.b.build_shl(hi16, c.i32(16))); | |
| 208 | }; | ||
| 209 | 80 | for (std::size_t pr = 0; pr < 4; ++pr) out.push_back(pair(hb[2 * pr], hb[2 * pr + 1])); | |
| 210 | 80 | for (std::size_t pr = 0; pr < 4; ++pr) out.push_back(pair(lb[2 * pr], lb[2 * pr + 1])); | |
| 211 | 16 | return out; | |
| 212 | 28 | }; | |
| 213 | 2 | const std::size_t chunk_words = split ? 8u : 4u; | |
| 214 | |||
| 215 | // One row chunk: 8 consecutive FP32 of a row, packed. | ||
| 216 | 14 | auto load_row_chunk = [&](const RowChunks& rc, int i, Value* k0) -> std::vector<Value*> { | |
| 217 | 12 | const auto iu = static_cast<std::size_t>(i); | |
| 218 | 12 | Value* kb0 = c.b.build_add(k0, rc.k_lane[iu]); | |
| 219 | 24 | auto scalar = [&]() -> std::vector<Value*> { | |
| 220 | 12 | std::array<Value*, 8> v{}; | |
| 221 | 108 | for (int e = 0; e < 8; ++e) { | |
| 222 | 96 | Value* kk = c.b.build_add(kb0, c.i32(e)); | |
| 223 | 96 | Value* ok = c.b.build_and(rc.row_ok[iu], c.b.build_slt(kk, k_end)); | |
| 224 | 96 | Value* skk = c.b.build_select(ok, kk, c.i32(0)); | |
| 225 | 96 | v[static_cast<std::size_t>(e)] = | |
| 226 | 96 | c.b.build_select(ok, kb.load_f32(ptr_at(c, rc.row_ptr[iu], skk, 4)), c.f32(0.0f)); | |
| 227 | 96 | } | |
| 228 | 12 | return pack8(v); | |
| 229 | }; | ||
| 230 | 12 | if (!aligned_k4) return scalar(); | |
| 231 | 12 | Value* full = c.b.build_and(rc.row_ok[iu], c.b.build_sle(c.b.build_add(kb0, c.i32(8)), k_end)); | |
| 232 | 12 | return emit_if_else_values( | |
| 233 | 12 | c, full, std::vector<Type>(chunk_words, Type::i32()), | |
| 234 | 24 | [&]() -> std::vector<Value*> { | |
| 235 | 12 | Value* src = ptr_at(c, rc.row_ptr[iu], kb0, 4); | |
| 236 | 12 | Value* lo = kb.vload_f32x4(src); | |
| 237 | 12 | Value* hi = kb.vload_f32x4(src, 16); | |
| 238 | 12 | std::array<Value*, 8> v{}; | |
| 239 | 60 | for (std::uint32_t e = 0; e < 4; ++e) { | |
| 240 | 48 | v[e] = c.b.build_vextract_lane(lo, e); | |
| 241 | 48 | v[4 + e] = c.b.build_vextract_lane(hi, e); | |
| 242 | 48 | } | |
| 243 | 12 | return pack8(v); | |
| 244 | }, | ||
| 245 | scalar); | ||
| 246 | 12 | }; | |
| 247 | |||
| 248 | // Fetch the next tile into registers: 2 x 4 packed words of A then | ||
| 249 | // b_chunks x 4 words of B (words_per_set), and with split a second set, | ||
| 250 | // the lo halves, after the first. | ||
| 251 | 6 | auto fetch = [&](Value* kt) -> std::vector<Value*> { | |
| 252 | 4 | Value* k0 = c.b.build_add(k_begin, c.b.build_shl(kt, c.i32(5))); | |
| 253 | 4 | std::vector<Value*> hi_words, lo_words; | |
| 254 | 20 | auto add = [&](const std::vector<Value*>& w) { | |
| 255 | 16 | hi_words.insert(hi_words.end(), w.begin(), w.begin() + 4); | |
| 256 | 16 | if (split) lo_words.insert(lo_words.end(), w.begin() + 4, w.end()); | |
| 257 | 16 | }; | |
| 258 | 8 | auto finish = [&] { | |
| 259 | 12 | for (int i = 0; i < b_chunks; ++i) add(load_row_chunk(bw_rows, i, k0)); | |
| 260 | 4 | hi_words.insert(hi_words.end(), lo_words.begin(), lo_words.end()); | |
| 261 | 4 | return hi_words; | |
| 262 | ✗ | }; | |
| 263 | 4 | if (gemm) { | |
| 264 | 6 | for (int i = 0; i < a_chunks; ++i) add(load_row_chunk(ax_rows, i, k0)); | |
| 265 | 2 | return finish(); | |
| 266 | } | ||
| 267 | |||
| 268 | // The thread's 8 k decode once (two divisions), then step (ic, kh, kw). | ||
| 269 | 2 | Value* gk = c.b.build_add(k0, c.b.build_shl(a_ch, c.i32(3))); | |
| 270 | 2 | Value* sk = c.b.build_select(c.b.build_slt(gk, k_end), gk, c.i32(0)); | |
| 271 | 2 | Value* ic = c.b.build_udiv(sk, KHW); | |
| 272 | 2 | Value* rem = c.b.build_umod(sk, KHW); | |
| 273 | 2 | Value* kh = c.b.build_udiv(rem, kW); | |
| 274 | 2 | Value* kw = c.b.build_umod(rem, kW); | |
| 275 | 2 | std::array<Value*, 8> k_ok{}, dkh{}, dkw{}, koff{}; | |
| 276 | 18 | for (int q = 0; q < 8; ++q) { | |
| 277 | 16 | const auto qu = static_cast<std::size_t>(q); | |
| 278 | 16 | if (q > 0) { | |
| 279 | 14 | Value* kw1 = c.b.build_add(kw, c.i32(1)); | |
| 280 | 14 | Value* wrap_w = c.b.build_eq(kw1, kW); | |
| 281 | 14 | kw = c.b.build_select(wrap_w, c.i32(0), kw1); | |
| 282 | 14 | Value* kh1 = c.b.build_select(wrap_w, c.b.build_add(kh, c.i32(1)), kh); | |
| 283 | 14 | Value* wrap_h = c.b.build_eq(kh1, kH); | |
| 284 | 14 | kh = c.b.build_select(wrap_h, c.i32(0), kh1); | |
| 285 | 14 | ic = c.b.build_select(wrap_h, c.b.build_add(ic, c.i32(1)), ic); | |
| 286 | 14 | } | |
| 287 | 16 | k_ok[qu] = c.b.build_slt(c.b.build_add(gk, c.i32(q)), k_end); | |
| 288 | 16 | dkh[qu] = c.b.build_mul(kh, dh); | |
| 289 | 16 | dkw[qu] = c.b.build_mul(kw, dw); | |
| 290 | 32 | koff[qu] = c.b.build_add(c.b.build_mul(ic, hw_in), | |
| 291 | 16 | c.b.build_add(c.b.build_mul(dkh[qu], W), dkw[qu])); | |
| 292 | 16 | } | |
| 293 | 6 | for (int r = 0; r < 2; ++r) { | |
| 294 | 4 | const auto ru = static_cast<std::size_t>(r); | |
| 295 | 4 | std::array<Value*, 8> v{}; | |
| 296 | 36 | for (int q = 0; q < 8; ++q) { | |
| 297 | 32 | const auto qu = static_cast<std::size_t>(q); | |
| 298 | // Unsigned compares fold ih >= 0 into ih < H. | ||
| 299 | 32 | Value* ih = c.b.build_add(pix_ih0[ru], dkh[qu]); | |
| 300 | 32 | Value* iw = c.b.build_add(pix_iw0[ru], dkw[qu]); | |
| 301 | 32 | Value* ok = c.b.build_and(k_ok[qu], c.b.build_and(c.b.build_ult(ih, H), c.b.build_ult(iw, W))); | |
| 302 | 32 | Value* off = c.b.build_select(ok, c.b.build_add(pix_base[ru], koff[qu]), c.i32(0)); | |
| 303 | 32 | Value* x = kb.load_f32(c.b.build_add(X, c.b.build_shl(c.b.build_zext_i64(off), kb.const_i64(2)))); | |
| 304 | 32 | v[qu] = c.b.build_select(ok, x, c.f32(0.0f)); | |
| 305 | 32 | } | |
| 306 | 4 | add(pack8(v)); | |
| 307 | 4 | } | |
| 308 | 2 | return finish(); | |
| 309 | 4 | }; | |
| 310 | |||
| 311 | 6 | auto store = [&](Value* st, const std::vector<Value*>& words) { | |
| 312 | 10 | for (int set = 0; set < (split ? 2 : 1); ++set) { | |
| 313 | 6 | const std::size_t w0 = static_cast<std::size_t>(set * words_per_set); | |
| 314 | 6 | Value* base = c.i32(set * lo_chunk_base); | |
| 315 | 6 | Value* a_off = c.b.build_add(base, c.b.build_mul(st, c.i32(a_stage_chunks))); | |
| 316 | 18 | for (int r = 0; r < 2; ++r) { | |
| 317 | 12 | const auto ru = static_cast<std::size_t>(r); | |
| 318 | 12 | Value* chunk = gemm ? ax_rows.smem_chunk[ru] : a_chunk_off[ru]; | |
| 319 | 24 | kb.shared_store_i32x4_indexed(smem, c.b.build_add(chunk, a_off), | |
| 320 | 12 | make_i32x4(c, words, w0 + 4 * ru)); | |
| 321 | 12 | } | |
| 322 | 6 | Value* b_off = c.b.build_add(base, c.b.build_mul(st, c.i32(b_stage_chunks))); | |
| 323 | 18 | for (int i = 0; i < b_chunks; ++i) { | |
| 324 | 24 | kb.shared_store_i32x4_indexed( | |
| 325 | 12 | smem, c.b.build_add(bw_rows.smem_chunk[static_cast<std::size_t>(i)], b_off), | |
| 326 | 12 | make_i32x4(c, words, w0 + static_cast<std::size_t>(8 + 4 * i))); | |
| 327 | 12 | } | |
| 328 | 6 | } | |
| 329 | 4 | }; | |
| 330 | |||
| 331 | // split: acc += A_hi B_hi + A_hi B_lo + A_lo B_hi (the lo lo term is | ||
| 332 | // below the sum's rounding), all BF16 products into FP32. | ||
| 333 | 2 | const char* mma_sym = split ? "ptx_mma_m16n8k16_bf16_f32" : "ptx_mma_m16n8k16_f16_f32"; | |
| 334 | 6 | auto emit_mma = [&](Value* st, std::vector<Value*>& accs) { | |
| 335 | 4 | Value* a_st_off = c.b.build_mul(st, c.i32(a_stage_bytes)); | |
| 336 | 4 | Value* b_st_off = c.b.build_mul(st, c.i32(b_stage_bytes)); | |
| 337 | 12 | for (int kk = 0; kk < 2; ++kk) { | |
| 338 | 32 | auto a_addr = [&](int i, int set) -> Value* { | |
| 339 | 24 | Value* p0 = c.b.build_add(a_ld_off[static_cast<std::size_t>(i)], a_st_off); | |
| 340 | 24 | if (set) p0 = c.b.build_add(p0, c.i32(lo_byte_base)); | |
| 341 | 24 | return (kk == 0) ? p0 : c.b.build_xor(p0, c.i32(32)); | |
| 342 | }; | ||
| 343 | 32 | auto b_addr = [&](int j, int set) -> Value* { | |
| 344 | 24 | Value* p0 = c.b.build_add(b_ld_off[static_cast<std::size_t>(j)], b_st_off); | |
| 345 | 24 | if (set) p0 = c.b.build_add(p0, c.i32(lo_byte_base)); | |
| 346 | 24 | return (kk == 0) ? p0 : c.b.build_xor(p0, c.i32(32)); | |
| 347 | }; | ||
| 348 | 20 | auto load_b = [&](int set, std::vector<Value*>& b0, std::vector<Value*>& b1) { | |
| 349 | 12 | b0.assign(static_cast<std::size_t>(nt), nullptr); | |
| 350 | 12 | b1.assign(static_cast<std::size_t>(nt), nullptr); | |
| 351 | 36 | for (int j = 0; j < nt / 2; ++j) { | |
| 352 | 24 | Value* r = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {b_addr(j, set)}); | |
| 353 | 24 | const auto j2 = static_cast<std::size_t>(2 * j); | |
| 354 | 24 | b0[j2] = c.b.build_vextract_lane(r, 0); | |
| 355 | 24 | b1[j2] = c.b.build_vextract_lane(r, 1); | |
| 356 | 24 | b0[j2 + 1u] = c.b.build_vextract_lane(r, 2); | |
| 357 | 24 | b1[j2 + 1u] = c.b.build_vextract_lane(r, 3); | |
| 358 | 24 | } | |
| 359 | 12 | }; | |
| 360 | 8 | std::vector<Value*> bh0, bh1, bl0, bl1; | |
| 361 | 8 | load_b(0, bh0, bh1); | |
| 362 | 8 | if (split) load_b(1, bl0, bl1); | |
| 363 | 24 | for (int i = 0; i < mt; ++i) { | |
| 364 | 16 | Value* ah = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {a_addr(i, 0)}); | |
| 365 | 16 | Value* al = split ? c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {a_addr(i, 1)}) : nullptr; | |
| 366 | 80 | for (int j = 0; j < nt; ++j) { | |
| 367 | 64 | const auto idx = static_cast<std::size_t>(i * nt + j); | |
| 368 | 64 | const auto ju = static_cast<std::size_t>(j); | |
| 369 | 64 | Value* acc = accs[idx]; | |
| 370 | 64 | if (split) { | |
| 371 | 32 | acc = c.b.build_call(mma_sym, Type::f32x4(), {al, bh0[ju], bh1[ju], acc}); | |
| 372 | 32 | acc = c.b.build_call(mma_sym, Type::f32x4(), {ah, bl0[ju], bl1[ju], acc}); | |
| 373 | 32 | } | |
| 374 | 64 | accs[idx] = c.b.build_call(mma_sym, Type::f32x4(), {ah, bh0[ju], bh1[ju], acc}); | |
| 375 | 64 | } | |
| 376 | 16 | } | |
| 377 | 8 | } | |
| 378 | 4 | }; | |
| 379 | |||
| 380 | 4 | kb.if_then(c.b.build_slt(c.i32(0), KT), [&] { | |
| 381 | 2 | store(c.i32(0), fetch(c.i32(0))); | |
| 382 | 2 | kb.sync(); | |
| 383 | 2 | }); | |
| 384 | |||
| 385 | 2 | std::vector<Value*> init; | |
| 386 | 2 | init.reserve(static_cast<std::size_t>(num_accs + 1)); | |
| 387 | 18 | for (int i = 0; i < num_accs; ++i) init.push_back(c.b.build_vzero(Type::f32x4())); | |
| 388 | 2 | init.push_back(c.i32(0)); | |
| 389 | 2 | std::vector<Value*> state = kb.for_range_reduce_n( | |
| 390 | 2 | c.i32(0), c.b.build_sub(KT, c.i32(1)), c.i32(1), init, | |
| 391 | 4 | [&](Value* kt, const std::vector<Value*>& cur) -> std::vector<Value*> { | |
| 392 | 2 | std::vector<Value*> accs(cur.begin(), cur.begin() + num_accs); | |
| 393 | 2 | Value* st = cur.back(); | |
| 394 | 2 | Value* nxt = c.b.build_xor(st, c.i32(1)); | |
| 395 | 2 | auto words = fetch(c.b.build_add(kt, c.i32(1))); | |
| 396 | 2 | emit_mma(st, accs); | |
| 397 | 2 | store(nxt, words); | |
| 398 | 2 | kb.sync(); | |
| 399 | 2 | accs.push_back(nxt); | |
| 400 | 2 | return accs; | |
| 401 | 2 | }); | |
| 402 | |||
| 403 | 2 | std::vector<Value*> loop_accs(state.begin(), state.begin() + num_accs); | |
| 404 | 2 | Value* tail_st = state.back(); | |
| 405 | 2 | std::vector<Value*> accs = emit_if_else_values( | |
| 406 | 2 | c, c.b.build_sgt(KT, c.i32(0)), std::vector<Type>(static_cast<std::size_t>(num_accs), Type::f32x4()), | |
| 407 | 4 | [&]() -> std::vector<Value*> { | |
| 408 | 2 | std::vector<Value*> a = loop_accs; | |
| 409 | 2 | emit_mma(tail_st, a); | |
| 410 | 2 | return a; | |
| 411 | 2 | }, | |
| 412 | 4 | [&]() -> std::vector<Value*> { return loop_accs; }); | |
| 413 | |||
| 414 | // Epilogue: Y[n, oc, sp] (+ bias[oc] on split 0). | ||
| 415 | 2 | Value* g = c.b.build_lshr(lane, c.i32(2)); | |
| 416 | 2 | Value* t2 = c.b.build_shl(c.b.build_and(lane, c.i32(3)), c.i32(1)); | |
| 417 | 2 | Value* cout_hwo = c.b.build_mul(c.b.build_zext_i64(Cout), c.b.build_zext_i64(HWo)); | |
| 418 | 2 | Value* hwo64 = c.b.build_zext_i64(HWo); | |
| 419 | 10 | for (int j = 0; j < nt; ++j) { | |
| 420 | 24 | for (int c_off = 0; c_off < 2; ++c_off) { | |
| 421 | 16 | Value* oc = c.b.build_add(c.b.build_add(n0, wn_base), c.b.build_add(c.i32(j * 8 + c_off), t2)); | |
| 422 | 32 | kb.if_then(c.b.build_slt(oc, Cout), [&] { | |
| 423 | 16 | Value* bv = has_bias | |
| 424 | 16 | ? c.b.build_select(split0, kb.load_f32(ptr_at(c, bias, oc, 4)), c.f32(0.0f)) | |
| 425 | : nullptr; | ||
| 426 | 16 | Value* oc_off = c.b.build_mul(c.b.build_zext_i64(oc), hwo64); | |
| 427 | 48 | for (int i = 0; i < mt; ++i) { | |
| 428 | 32 | Value* frag = accs[static_cast<std::size_t>(i * nt + j)]; | |
| 429 | 96 | for (int h = 0; h < 2; ++h) { | |
| 430 | 128 | Value* m_g = c.b.build_add(c.b.build_add(m0, wm_base), | |
| 431 | 64 | c.b.build_add(c.i32(i * 16 + h * 8), g)); | |
| 432 | 128 | kb.if_then(c.b.build_slt(m_g, M), [&] { | |
| 433 | 64 | Value* n_b = c.b.build_udiv(m_g, HWo); | |
| 434 | 64 | Value* sp = c.b.build_umod(m_g, HWo); | |
| 435 | 128 | Value* idx = c.b.build_add( | |
| 436 | 64 | c.b.build_add(c.b.build_mul(c.b.build_zext_i64(n_b), cout_hwo), oc_off), | |
| 437 | 64 | c.b.build_zext_i64(sp)); | |
| 438 | 64 | Value* v = c.b.build_vextract_lane(frag, static_cast<std::uint32_t>(2 * h + c_off)); | |
| 439 | 64 | if (bv) v = c.b.build_add(v, bv); | |
| 440 | 64 | kb.store_f32(c.b.build_add(Y, c.b.build_shl(idx, kb.const_i64(2))), v); | |
| 441 | 64 | }); | |
| 442 | 64 | } | |
| 443 | 32 | } | |
| 444 | 16 | }); | |
| 445 | 16 | } | |
| 446 | 8 | } | |
| 447 | |||
| 448 | 2 | c.b.build_ret_void(); | |
| 449 | 2 | fn->rebuild_cfg_predecessors(); | |
| 450 | 2 | return fn; | |
| 451 | 2 | } | |
| 452 | |||
| 453 | } // namespace brotensor::kernels | ||
| 454 |