src/kernels/mir_conv_int8w.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_conv_int8w.h" | ||
| 2 | |||
| 3 | #include "mir_common.h" | ||
| 4 | #include "mir_quant_helpers.h" | ||
| 5 | |||
| 6 | #include <array> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <stdexcept> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | namespace brotensor::kernels { | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | |||
| 15 | constexpr int kConvBm = 64; | ||
| 16 | constexpr int kConvBn = 64; | ||
| 17 | constexpr int kConvBk = 32; | ||
| 18 | constexpr int kConvThreads = 128; | ||
| 19 | |||
| 20 | 4 | Value* swz_bytes(Ctx& c, Value* row, Value* ch) { | |
| 21 | 4 | Value* row_off = c.b.build_shl(row, c.i32(6)); | |
| 22 | 4 | Value* swz = c.b.build_and(c.b.build_ashr(row, c.i32(1)), c.i32(3)); | |
| 23 | 4 | return c.b.build_or(row_off, c.b.build_shl(c.b.build_xor(ch, swz), c.i32(4))); | |
| 24 | } | ||
| 25 | |||
| 26 | 4 | Value* swz_chunk(Ctx& c, Value* row, Value* ch) { | |
| 27 | 4 | Value* row_off = c.b.build_shl(row, c.i32(2)); | |
| 28 | 4 | Value* swz = c.b.build_and(c.b.build_ashr(row, c.i32(1)), c.i32(3)); | |
| 29 | 4 | return c.b.build_or(row_off, c.b.build_xor(ch, swz)); | |
| 30 | } | ||
| 31 | |||
| 32 | 8 | Value* make_i32x4(Ctx& c, Value* w0, Value* w1, Value* w2, Value* w3) { | |
| 33 | 8 | Value* v = c.b.build_vzero(Type::i32x4()); | |
| 34 | 8 | v = c.b.build_vinsert_lane(v, w0, 0); | |
| 35 | 8 | v = c.b.build_vinsert_lane(v, w1, 1); | |
| 36 | 8 | v = c.b.build_vinsert_lane(v, w2, 2); | |
| 37 | 8 | return c.b.build_vinsert_lane(v, w3, 3); | |
| 38 | } | ||
| 39 | |||
| 40 | struct ConvCfg { int kh, kw, ph, pw, sh, sw; }; | ||
| 41 | constexpr ConvCfg kConvCfgs[3] = { | ||
| 42 | {3, 3, 1, 1, 1, 1}, | ||
| 43 | {1, 1, 0, 0, 1, 1}, | ||
| 44 | {3, 3, 1, 1, 2, 2}, | ||
| 45 | }; | ||
| 46 | |||
| 47 | } // namespace | ||
| 48 | |||
| 49 | 1 | Function* build_conv2d_int8w_mma_mir(Module& mod, std::uint8_t cfg_id, | |
| 50 | bool has_bias, bool aligned_k8) { | ||
| 51 | 1 | if (cfg_id > 2u) { | |
| 52 | ✗ | throw std::runtime_error("build_conv2d_int8w_mma_mir: bad cfg_id"); | |
| 53 | } | ||
| 54 | 1 | const ConvCfg& cc = kConvCfgs[cfg_id]; | |
| 55 | 1 | const int khw_sz = cc.kh * cc.kw; | |
| 56 | static constexpr int mt = 2; // WM = 32 -> 2 * 16 | ||
| 57 | static constexpr int nt = 4; // WN = 32 -> 4 * 8 | ||
| 58 | static constexpr int num_accs = mt * nt; | ||
| 59 | static constexpr int a_chunks = (kConvBm * 4) / kConvThreads; // 2 | ||
| 60 | static constexpr int b_chunks = (kConvBn * 4) / kConvThreads; // 2 | ||
| 61 | static constexpr int a_stage_bytes = kConvBm * kConvBk * 2; // 4096 | ||
| 62 | static constexpr int b_stage_bytes = kConvBn * kConvBk * 2; // 4096 | ||
| 63 | static constexpr int a_stage_chunks = kConvBm * 4; // 256 | ||
| 64 | static constexpr int sB_chunk_base = 2 * a_stage_chunks; // 512 | ||
| 65 | static constexpr int b_stage_chunks = kConvBn * 4; // 256 | ||
| 66 | |||
| 67 | 15 | std::vector<Type> param_types = { | |
| 68 | 5 | Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), | |
| 69 | 5 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | |
| 70 | 5 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | |
| 71 | }; | ||
| 72 | 1 | Function* fn = mod.create_function("mir_conv2d_int8w_mma", Type::void_type(), param_types); | |
| 73 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 74 | 1 | Ctx c(kb); | |
| 75 | 1 | auto p = entry_params(c, fn); | |
| 76 | 1 | Value* X = p[0]; | |
| 77 | 1 | Value* W_i8 = p[1]; | |
| 78 | 1 | Value* scales = p[2]; | |
| 79 | 1 | Value* bias = p[3]; | |
| 80 | 1 | Value* Y = p[4]; | |
| 81 | 1 | Value* C_in = p[6]; | |
| 82 | 1 | Value* H = p[7]; | |
| 83 | 1 | Value* W = p[8]; | |
| 84 | 1 | Value* C_out = p[9]; | |
| 85 | 1 | Value* W_out = p[11]; | |
| 86 | 1 | Value* M_tot = p[12]; | |
| 87 | 1 | Value* HW_out = p[13]; | |
| 88 | 1 | Value* K_tot = p[14]; | |
| 89 | |||
| 90 | 1 | Value* smem = kb.shared_alloc_u16(2u * (kConvBm + kConvBn) * kConvBk); | |
| 91 | 1 | Value* sA_base = c.b.build_trunc_i32(smem); | |
| 92 | 1 | Value* sB_base = c.b.build_add(sA_base, c.i32(2 * a_stage_bytes)); | |
| 93 | |||
| 94 | 1 | Value* tid = kb.tid_x(); | |
| 95 | 1 | Value* lane = c.b.build_and(tid, c.i32(31)); | |
| 96 | 1 | Value* warp = c.b.build_lshr(tid, c.i32(5)); | |
| 97 | 1 | Value* w_m = c.b.build_lshr(warp, c.i32(1)); | |
| 98 | 1 | Value* w_n = c.b.build_and(warp, c.i32(1)); | |
| 99 | 1 | Value* m0 = c.b.build_mul(kb.ctaid_y(), c.i32(kConvBm)); | |
| 100 | 1 | Value* n0 = c.b.build_mul(kb.ctaid_x(), c.i32(kConvBn)); | |
| 101 | |||
| 102 | 1 | Value* HW_in = c.b.build_mul(H, W); | |
| 103 | 1 | Value* CHW_in = c.b.build_mul(C_in, HW_in); | |
| 104 | 1 | Value* KT = c.b.build_lshr(c.b.build_add(K_tot, c.i32(31)), c.i32(5)); | |
| 105 | |||
| 106 | // Precompute A spatial coordinates and B row pointers/scales outside the K-loop. | ||
| 107 | struct AGather { | ||
| 108 | Value* chunk_off; | ||
| 109 | Value* ch; | ||
| 110 | Value* m_ok; | ||
| 111 | Value* x_base; | ||
| 112 | Value* in_h0; | ||
| 113 | Value* in_w0; | ||
| 114 | }; | ||
| 115 | 1 | std::array<AGather, a_chunks> ag{}; | |
| 116 | 3 | for (int i = 0; i < a_chunks; ++i) { | |
| 117 | 2 | Value* lin = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * kConvThreads)); | |
| 118 | 2 | Value* row = c.b.build_lshr(lin, c.i32(2)); | |
| 119 | 2 | Value* ch = c.b.build_and(lin, c.i32(3)); | |
| 120 | 2 | Value* m_g = c.b.build_add(m0, row); | |
| 121 | 2 | Value* m_ok = c.b.build_slt(m_g, M_tot); | |
| 122 | 2 | Value* safe_m = c.b.build_select(m_ok, m_g, c.i32(0)); | |
| 123 | 2 | Value* n_b = c.b.build_udiv(safe_m, HW_out); | |
| 124 | 2 | Value* sp = c.b.build_umod(safe_m, HW_out); | |
| 125 | 2 | Value* oh = c.b.build_udiv(sp, W_out); | |
| 126 | 2 | Value* ow = c.b.build_umod(sp, W_out); | |
| 127 | 4 | Value* in_h0 = (cc.kh == 1 && cc.sh == 1 && cc.ph == 0) | |
| 128 | ✗ | ? oh | |
| 129 | 2 | : c.b.build_sub(c.b.build_mul(oh, c.i32(cc.sh)), c.i32(cc.ph)); | |
| 130 | 4 | Value* in_w0 = (cc.kw == 1 && cc.sw == 1 && cc.pw == 0) | |
| 131 | ✗ | ? ow | |
| 132 | 2 | : c.b.build_sub(c.b.build_mul(ow, c.i32(cc.sw)), c.i32(cc.pw)); | |
| 133 | 4 | Value* x_n = c.b.build_add(X, c.b.build_shl( | |
| 134 | 2 | c.b.build_mul(c.b.build_zext_i64(n_b), c.b.build_zext_i64(CHW_in)), kb.const_i64(1))); | |
| 135 | 2 | if (cc.kh == 1 && cc.kw == 1 && cc.ph == 0 && cc.pw == 0) { | |
| 136 | ✗ | Value* hw_off = c.b.build_add(c.b.build_mul(oh, W), ow); | |
| 137 | ✗ | x_n = c.b.build_add(x_n, c.b.build_shl(c.b.build_zext_i64(hw_off), kb.const_i64(1))); | |
| 138 | ✗ | } | |
| 139 | 2 | ag[static_cast<std::size_t>(i)] = {swz_chunk(c, row, ch), ch, m_ok, x_n, in_h0, in_w0}; | |
| 140 | 2 | } | |
| 141 | |||
| 142 | struct BLoad { | ||
| 143 | Value* chunk_off; | ||
| 144 | Value* ch; | ||
| 145 | Value* oc_ok; | ||
| 146 | Value* w_row_ptr; | ||
| 147 | Value* scale; | ||
| 148 | }; | ||
| 149 | 1 | std::array<BLoad, b_chunks> bl{}; | |
| 150 | 3 | for (int i = 0; i < b_chunks; ++i) { | |
| 151 | 2 | Value* lin = (i == 0) ? tid : c.b.build_add(tid, c.i32(i * kConvThreads)); | |
| 152 | 2 | Value* row = c.b.build_lshr(lin, c.i32(2)); | |
| 153 | 2 | Value* ch = c.b.build_and(lin, c.i32(3)); | |
| 154 | 2 | Value* oc = c.b.build_add(n0, row); | |
| 155 | 2 | Value* oc_ok = c.b.build_slt(oc, C_out); | |
| 156 | 2 | Value* safe_oc = c.b.build_select(oc_ok, oc, c.i32(0)); | |
| 157 | 4 | Value* w_ptr = c.b.build_add( | |
| 158 | 2 | W_i8, c.b.build_mul(c.b.build_zext_i64(safe_oc), c.b.build_zext_i64(K_tot))); | |
| 159 | 2 | Value* sc = c.b.build_select(oc_ok, kb.load_f32(ptr_at(c, scales, safe_oc, 4)), c.f32(0.0f)); | |
| 160 | 12 | bl[static_cast<std::size_t>(i)] = { | |
| 161 | 2 | c.b.build_add(c.i32(sB_chunk_base), swz_chunk(c, row, ch)), ch, oc_ok, w_ptr, sc}; | |
| 162 | 2 | } | |
| 163 | |||
| 164 | // Precompute ldmatrix addresses (kk = 0; kk = 1 is addr ^ 32). | ||
| 165 | 1 | Value* a_lane_row = c.b.build_and(lane, c.i32(15)); | |
| 166 | 1 | Value* a_lane_ch = c.b.build_lshr(lane, c.i32(4)); | |
| 167 | 1 | Value* wm_base = c.b.build_mul(w_m, c.i32(32)); | |
| 168 | 1 | std::array<Value*, mt> a_ld_off{}; | |
| 169 | 3 | for (int i = 0; i < mt; ++i) { | |
| 170 | 2 | Value* row = c.b.build_add(c.b.build_add(wm_base, c.i32(i * 16)), a_lane_row); | |
| 171 | 2 | a_ld_off[static_cast<std::size_t>(i)] = c.b.build_add(sA_base, swz_bytes(c, row, a_lane_ch)); | |
| 172 | 2 | } | |
| 173 | 1 | Value* mi = c.b.build_lshr(lane, c.i32(3)); | |
| 174 | 2 | Value* b_lane_row = c.b.build_add(c.b.build_shl(c.b.build_lshr(mi, c.i32(1)), c.i32(3)), | |
| 175 | 1 | c.b.build_and(lane, c.i32(7))); | |
| 176 | 1 | Value* b_lane_ch = c.b.build_and(mi, c.i32(1)); | |
| 177 | 1 | Value* wn_base = c.b.build_mul(w_n, c.i32(32)); | |
| 178 | 1 | std::array<Value*, nt / 2> b_ld_off{}; | |
| 179 | 3 | for (int j = 0; j < nt / 2; ++j) { | |
| 180 | 2 | Value* row = c.b.build_add(c.b.build_add(wn_base, c.i32(j * 16)), b_lane_row); | |
| 181 | 2 | b_ld_off[static_cast<std::size_t>(j)] = c.b.build_add(sB_base, swz_bytes(c, row, b_lane_ch)); | |
| 182 | 2 | } | |
| 183 | |||
| 184 | 3 | auto emit_load_tiles = [&](Value* st_idx, Value* kt_idx) { | |
| 185 | 2 | Value* k0 = c.b.build_shl(kt_idx, c.i32(5)); | |
| 186 | 2 | Value* a_ch_off = c.b.build_mul(st_idx, c.i32(a_stage_chunks)); | |
| 187 | 2 | Value* b_ch_off = c.b.build_mul(st_idx, c.i32(b_stage_chunks)); | |
| 188 | |||
| 189 | // Gather A tile directly as packed u16x2 pairs (no f16->f32 round-trip). | ||
| 190 | 6 | for (int i = 0; i < a_chunks; ++i) { | |
| 191 | 4 | const AGather& a = ag[static_cast<std::size_t>(i)]; | |
| 192 | 4 | Value* gk0 = c.b.build_add(k0, c.b.build_shl(a.ch, c.i32(3))); | |
| 193 | 4 | std::array<Value*, 4> pairs{}; | |
| 194 | 20 | for (int pr = 0; pr < 4; ++pr) { | |
| 195 | Value* p_u16[2]; | ||
| 196 | 48 | for (int q = 0; q < 2; ++q) { | |
| 197 | 32 | const int col = 2 * pr + q; | |
| 198 | 32 | Value* gk = c.b.build_add(gk0, c.i32(col)); | |
| 199 | 32 | if (cfg_id == 1u) { | |
| 200 | ✗ | Value* ok = c.b.build_and(a.m_ok, c.b.build_slt(gk, C_in)); | |
| 201 | ✗ | Value* xp = c.b.build_add( | |
| 202 | ✗ | a.x_base, | |
| 203 | ✗ | c.b.build_shl(c.b.build_mul(c.b.build_zext_i64(gk), c.b.build_zext_i64(HW_in)), | |
| 204 | ✗ | kb.const_i64(1))); | |
| 205 | ✗ | p_u16[q] = emit_if_else_values( | |
| 206 | ✗ | c, ok, {Type::i32()}, | |
| 207 | ✗ | [&]() -> std::vector<Value*> { return {kb.load_u16(xp)}; }, | |
| 208 | ✗ | [&]() -> std::vector<Value*> { return {c.i32(0)}; })[0]; | |
| 209 | ✗ | } else { | |
| 210 | 32 | Value* k_ok = c.b.build_and(a.m_ok, c.b.build_slt(gk, K_tot)); | |
| 211 | 32 | Value* safe_k = c.b.build_select(k_ok, gk, c.i32(0)); | |
| 212 | 32 | Value* ic = c.b.build_udiv(safe_k, c.i32(khw_sz)); | |
| 213 | 32 | Value* rem = c.b.build_umod(safe_k, c.i32(khw_sz)); | |
| 214 | 32 | Value* kh = c.b.build_udiv(rem, c.i32(cc.kw)); | |
| 215 | 32 | Value* kw = c.b.build_umod(rem, c.i32(cc.kw)); | |
| 216 | 32 | Value* ih = c.b.build_add(a.in_h0, kh); | |
| 217 | 32 | Value* iw = c.b.build_add(a.in_w0, kw); | |
| 218 | 64 | Value* in_ok = c.b.build_and( | |
| 219 | 32 | k_ok, | |
| 220 | 64 | c.b.build_and( | |
| 221 | 32 | c.b.build_and(c.b.build_sge(ih, c.i32(0)), c.b.build_slt(ih, H)), | |
| 222 | 32 | c.b.build_and(c.b.build_sge(iw, c.i32(0)), c.b.build_slt(iw, W)))); | |
| 223 | 64 | Value* xoff = c.b.build_add( | |
| 224 | 32 | c.b.build_mul(ic, HW_in), | |
| 225 | 32 | c.b.build_add(c.b.build_mul(ih, W), iw)); | |
| 226 | 32 | Value* xp = ptr_at(c, a.x_base, xoff, 2); | |
| 227 | 32 | p_u16[q] = emit_if_else_values( | |
| 228 | 32 | c, in_ok, {Type::i32()}, | |
| 229 | 64 | [&]() -> std::vector<Value*> { return {kb.load_u16(xp)}; }, | |
| 230 | 64 | [&]() -> std::vector<Value*> { return {c.i32(0)}; })[0]; | |
| 231 | } | ||
| 232 | 32 | } | |
| 233 | 32 | pairs[static_cast<std::size_t>(pr)] = c.b.build_or( | |
| 234 | 16 | c.b.build_and(p_u16[0], c.i32(0xffff)), | |
| 235 | 16 | c.b.build_shl(p_u16[1], c.i32(16))); | |
| 236 | 16 | } | |
| 237 | 8 | kb.shared_store_i32x4_indexed( | |
| 238 | 4 | smem, c.b.build_add(a.chunk_off, a_ch_off), | |
| 239 | 4 | make_i32x4(c, pairs[0], pairs[1], pairs[2], pairs[3])); | |
| 240 | 4 | } | |
| 241 | |||
| 242 | // Dequant B tile (INT8 -> FP16). | ||
| 243 | 6 | for (int i = 0; i < b_chunks; ++i) { | |
| 244 | 4 | const BLoad& b = bl[static_cast<std::size_t>(i)]; | |
| 245 | 4 | Value* gk0 = c.b.build_add(k0, c.b.build_shl(b.ch, c.i32(3))); | |
| 246 | 4 | std::array<Value*, 8> fv{}; | |
| 247 | 4 | if (aligned_k8) { | |
| 248 | 4 | Value* ok = c.b.build_and(b.oc_ok, c.b.build_slt(gk0, K_tot)); | |
| 249 | 4 | Value* q64 = emit_if_else_values( | |
| 250 | 4 | c, ok, {Type::i64()}, | |
| 251 | 8 | [&]() -> std::vector<Value*> { | |
| 252 | 4 | return {kb.load_i64(c.b.build_add(b.w_row_ptr, c.b.build_zext_i64(gk0)))}; | |
| 253 | }, | ||
| 254 | 8 | [&]() -> std::vector<Value*> { return {kb.const_i64(0)}; })[0]; | |
| 255 | 4 | Value* qlo = c.b.build_trunc_i32(q64); | |
| 256 | 4 | Value* qhi = c.b.build_trunc_i32(c.b.build_lshr(q64, kb.const_i64(32))); | |
| 257 | 36 | for (int q = 0; q < 8; ++q) { | |
| 258 | 32 | Value* bw = (q < 4) ? qlo : qhi; | |
| 259 | 32 | Value* sb = c.b.build_and(c.b.build_lshr(bw, c.i32((q & 3) * 8)), c.i32(0xff)); | |
| 260 | 32 | fv[static_cast<std::size_t>(q)] = c.b.build_mul(emit_s8_byte_to_f32(c, sb), b.scale); | |
| 261 | 32 | } | |
| 262 | 4 | } else { | |
| 263 | ✗ | for (int q = 0; q < 8; ++q) { | |
| 264 | ✗ | Value* gk = c.b.build_add(gk0, c.i32(q)); | |
| 265 | ✗ | Value* ok = c.b.build_and(b.oc_ok, c.b.build_slt(gk, K_tot)); | |
| 266 | ✗ | fv[static_cast<std::size_t>(q)] = emit_if_else_values( | |
| 267 | ✗ | c, ok, {Type::f32()}, | |
| 268 | ✗ | [&]() -> std::vector<Value*> { | |
| 269 | ✗ | Value* ub = kb.load_u8(c.b.build_add(b.w_row_ptr, c.b.build_zext_i64(gk))); | |
| 270 | ✗ | return {c.b.build_mul(emit_s8_byte_to_f32(c, ub), b.scale)}; | |
| 271 | }, | ||
| 272 | ✗ | [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; | |
| 273 | ✗ | } | |
| 274 | } | ||
| 275 | 4 | Value* v4 = make_i32x4( | |
| 276 | 4 | c, | |
| 277 | 4 | kb.pack_f16x2(fv[0], fv[1]), | |
| 278 | 4 | kb.pack_f16x2(fv[2], fv[3]), | |
| 279 | 4 | kb.pack_f16x2(fv[4], fv[5]), | |
| 280 | 4 | kb.pack_f16x2(fv[6], fv[7])); | |
| 281 | 4 | kb.shared_store_i32x4_indexed(smem, c.b.build_add(b.chunk_off, b_ch_off), v4); | |
| 282 | 4 | } | |
| 283 | 2 | }; | |
| 284 | |||
| 285 | 2 | auto emit_mma = [&](Value* st_idx, std::vector<Value*>& accs) { | |
| 286 | 1 | Value* a_st_off = c.b.build_mul(st_idx, c.i32(a_stage_bytes)); | |
| 287 | 1 | Value* b_st_off = c.b.build_mul(st_idx, c.i32(b_stage_bytes)); | |
| 288 | 3 | for (int kk = 0; kk < 2; ++kk) { | |
| 289 | 6 | auto a_addr = [&](int i) -> Value* { | |
| 290 | 4 | Value* p0 = c.b.build_add(a_ld_off[static_cast<std::size_t>(i)], a_st_off); | |
| 291 | 4 | return (kk == 0) ? p0 : c.b.build_xor(p0, c.i32(32)); | |
| 292 | }; | ||
| 293 | 6 | auto b_addr = [&](int j) -> Value* { | |
| 294 | 4 | Value* p0 = c.b.build_add(b_ld_off[static_cast<std::size_t>(j)], b_st_off); | |
| 295 | 4 | return (kk == 0) ? p0 : c.b.build_xor(p0, c.i32(32)); | |
| 296 | }; | ||
| 297 | 2 | std::array<Value*, nt> b0{}; | |
| 298 | 2 | std::array<Value*, nt> b1{}; | |
| 299 | 6 | for (int j = 0; j < nt / 2; ++j) { | |
| 300 | 4 | Value* r = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {b_addr(j)}); | |
| 301 | 4 | const auto j2 = static_cast<std::size_t>(2 * j); | |
| 302 | 4 | b0[j2] = c.b.build_vextract_lane(r, 0); | |
| 303 | 4 | b1[j2] = c.b.build_vextract_lane(r, 1); | |
| 304 | 4 | b0[j2 + 1u] = c.b.build_vextract_lane(r, 2); | |
| 305 | 4 | b1[j2 + 1u] = c.b.build_vextract_lane(r, 3); | |
| 306 | 4 | } | |
| 307 | 6 | for (int i = 0; i < mt; ++i) { | |
| 308 | 4 | Value* af = c.b.build_call("ptx_ldmatrix_x4", Type::i32x4(), {a_addr(i)}); | |
| 309 | 20 | for (int j = 0; j < nt; ++j) { | |
| 310 | 16 | const auto idx = static_cast<std::size_t>(i * nt + j); | |
| 311 | 16 | const auto ju = static_cast<std::size_t>(j); | |
| 312 | 32 | accs[idx] = c.b.build_call( | |
| 313 | 16 | "ptx_mma_m16n8k16_f16_f32", Type::f32x4(), {af, b0[ju], b1[ju], accs[idx]}); | |
| 314 | 16 | } | |
| 315 | 4 | } | |
| 316 | 2 | } | |
| 317 | 1 | }; | |
| 318 | |||
| 319 | 2 | kb.if_then(c.b.build_slt(c.i32(0), KT), [&] { | |
| 320 | 1 | emit_load_tiles(c.i32(0), c.i32(0)); | |
| 321 | 1 | kb.sync(); | |
| 322 | 1 | }); | |
| 323 | |||
| 324 | 1 | std::vector<Value*> init; | |
| 325 | 1 | init.reserve(static_cast<std::size_t>(num_accs + 1)); | |
| 326 | 9 | for (int i = 0; i < num_accs; ++i) init.push_back(c.b.build_vzero(Type::f32x4())); | |
| 327 | 1 | init.push_back(c.i32(0)); | |
| 328 | |||
| 329 | 1 | std::vector<Value*> state = kb.for_range_reduce_n( | |
| 330 | 1 | c.i32(0), KT, c.i32(1), init, | |
| 331 | 2 | [&](Value* kt, const std::vector<Value*>& cur) -> std::vector<Value*> { | |
| 332 | 1 | std::vector<Value*> cur_accs(cur.begin(), cur.begin() + num_accs); | |
| 333 | 1 | Value* cur_st = cur.back(); | |
| 334 | 1 | Value* nxt_st = c.b.build_xor(cur_st, c.i32(1)); | |
| 335 | 1 | Value* knext = c.b.build_add(kt, c.i32(1)); | |
| 336 | 1 | emit_mma(cur_st, cur_accs); | |
| 337 | 2 | kb.if_then(c.b.build_slt(knext, KT), [&] { | |
| 338 | 1 | emit_load_tiles(nxt_st, knext); | |
| 339 | 1 | kb.sync(); | |
| 340 | 1 | }); | |
| 341 | 1 | cur_accs.push_back(nxt_st); | |
| 342 | 1 | return cur_accs; | |
| 343 | 1 | }); | |
| 344 | |||
| 345 | 1 | std::vector<Value*> accs(state.begin(), state.begin() + num_accs); | |
| 346 | |||
| 347 | // Direct register epilogue: Y[(n * C_out + oc) * HW_out + sp]. | ||
| 348 | 1 | Value* g = c.b.build_lshr(lane, c.i32(2)); | |
| 349 | 1 | Value* t2 = c.b.build_shl(c.b.build_and(lane, c.i32(3)), c.i32(1)); | |
| 350 | 1 | Value* CHW_out = c.b.build_mul(C_out, HW_out); | |
| 351 | |||
| 352 | 3 | for (int i = 0; i < mt; ++i) { | |
| 353 | 6 | for (int h = 0; h < 2; ++h) { | |
| 354 | 4 | Value* m_g = c.b.build_add(c.b.build_add(m0, wm_base), c.b.build_add(c.i32(i * 16 + h * 8), g)); | |
| 355 | 8 | kb.if_then(c.b.build_slt(m_g, M_tot), [&] { | |
| 356 | 4 | Value* n_b = c.b.build_udiv(m_g, HW_out); | |
| 357 | 4 | Value* sp = c.b.build_umod(m_g, HW_out); | |
| 358 | 4 | Value* y_base_idx = c.b.build_add(c.b.build_mul(n_b, CHW_out), sp); | |
| 359 | 20 | for (int j = 0; j < nt; ++j) { | |
| 360 | 16 | Value* frag = accs[static_cast<std::size_t>(i * nt + j)]; | |
| 361 | 48 | for (int c_off = 0; c_off < 2; ++c_off) { | |
| 362 | 64 | Value* oc = c.b.build_add( | |
| 363 | 32 | c.b.build_add(n0, wn_base), c.b.build_add(c.i32(j * 8 + c_off), t2)); | |
| 364 | 64 | kb.if_then(c.b.build_slt(oc, C_out), [&] { | |
| 365 | 32 | Value* v = c.b.build_vextract_lane(frag, 2 * h + c_off); | |
| 366 | 32 | if (has_bias) { | |
| 367 | 32 | v = c.b.build_add(v, load_elem_dt(c, Dtype::FP16, bias, oc)); | |
| 368 | 32 | } | |
| 369 | 32 | Value* dst_idx = c.b.build_add(y_base_idx, c.b.build_mul(oc, HW_out)); | |
| 370 | 32 | store_elem_dt(c, Dtype::FP16, Y, dst_idx, v); | |
| 371 | 32 | }); | |
| 372 | 32 | } | |
| 373 | 16 | } | |
| 374 | 4 | }); | |
| 375 | 4 | } | |
| 376 | 2 | } | |
| 377 | |||
| 378 | 1 | c.b.build_ret_void(); | |
| 379 | 1 | fn->rebuild_cfg_predecessors(); | |
| 380 | 1 | return fn; | |
| 381 | 1 | } | |
| 382 | |||
| 383 | 1 | Function* build_matmul_int8w_fp16_mir(Module& mod) { | |
| 384 | 1 | constexpr int kTile = 16; | |
| 385 | 7 | std::vector<Type> params = { | |
| 386 | 7 | Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32() | |
| 387 | }; | ||
| 388 | 1 | Function* fn = mod.create_function("mir_matmul_int8w_fp16", Type::void_type(), params); | |
| 389 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 390 | 1 | Ctx c(kb); | |
| 391 | 1 | auto p = entry_params(c, fn); | |
| 392 | // W(0) scales(1) X(2) Y(3) | M(4) N(5) K(6) | ||
| 393 | 1 | Value* sW = kb.shared_alloc_f32(kTile * kTile); | |
| 394 | 1 | Value* sX = kb.shared_alloc_f32(kTile * kTile); | |
| 395 | |||
| 396 | 1 | Value* tid = kb.tid_x(); | |
| 397 | 1 | Value* tx = c.b.build_and(tid, c.i32(15)); | |
| 398 | 1 | Value* ty = c.b.build_lshr(tid, c.i32(4)); | |
| 399 | 1 | Value* row = c.b.build_add(c.b.build_mul(kb.ctaid_y(), c.i32(kTile)), ty); | |
| 400 | 1 | Value* col = c.b.build_add(c.b.build_mul(kb.ctaid_x(), c.i32(kTile)), tx); | |
| 401 | 1 | Value* row_ok = c.b.build_slt(row, p[4]); | |
| 402 | 1 | Value* col_ok = c.b.build_slt(col, p[5]); | |
| 403 | 1 | Value* row_scale = emit_if_else_values( | |
| 404 | 1 | c, row_ok, {Type::f32()}, | |
| 405 | 2 | [&]() -> std::vector<Value*> { return {kb.load_f32(ptr_at(c, p[1], row, 4))}; }, | |
| 406 | 2 | [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; | |
| 407 | |||
| 408 | 1 | Value* n_tiles = c.b.build_lshr(c.b.build_add(p[6], c.i32(15)), c.i32(4)); | |
| 409 | 1 | Value* ty16 = c.b.build_shl(ty, c.i32(4)); | |
| 410 | 1 | Value* s_tid = c.b.build_add(ty16, tx); | |
| 411 | |||
| 412 | 1 | Value* acc = kb.for_range_reduce( | |
| 413 | 1 | c.i32(0), n_tiles, c.i32(1), c.f32(0.0f), | |
| 414 | 2 | [&](Value* t, Value* cur) -> Value* { | |
| 415 | 1 | Value* t0 = c.b.build_shl(t, c.i32(4)); | |
| 416 | 1 | Value* w_col = c.b.build_add(t0, tx); | |
| 417 | 1 | Value* x_row = c.b.build_add(t0, ty); | |
| 418 | 1 | Value* w_val = emit_if_else_values( | |
| 419 | 1 | c, c.b.build_and(row_ok, c.b.build_slt(w_col, p[6])), {Type::f32()}, | |
| 420 | 2 | [&]() -> std::vector<Value*> { | |
| 421 | 1 | Value* wi = c.b.build_add(c.b.build_mul(row, p[6]), w_col); | |
| 422 | 1 | Value* ub = kb.load_u8(ptr_at(c, p[0], wi, 1)); | |
| 423 | 1 | return {c.b.build_mul(emit_s8_byte_to_f32(c, ub), row_scale)}; | |
| 424 | }, | ||
| 425 | 2 | [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; | |
| 426 | 1 | Value* x_val = emit_if_else_values( | |
| 427 | 1 | c, c.b.build_and(c.b.build_slt(x_row, p[6]), col_ok), {Type::f32()}, | |
| 428 | 2 | [&]() -> std::vector<Value*> { | |
| 429 | 1 | Value* xi = c.b.build_add(c.b.build_mul(x_row, p[5]), col); | |
| 430 | 1 | return {load_elem_dt(c, Dtype::FP16, p[2], xi)}; | |
| 431 | }, | ||
| 432 | 2 | [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; | |
| 433 | 1 | kb.shared_store_f32_indexed(sW, s_tid, w_val); | |
| 434 | 1 | kb.shared_store_f32_indexed(sX, s_tid, x_val); | |
| 435 | 1 | kb.sync(); | |
| 436 | 2 | Value* next_acc = kb.for_range_reduce( | |
| 437 | 1 | c.i32(0), c.i32(kTile), c.i32(1), cur, | |
| 438 | 2 | [&](Value* k, Value* a) -> Value* { | |
| 439 | 1 | Value* wv = kb.shared_load_f32_indexed(sW, c.b.build_add(ty16, k)); | |
| 440 | 1 | Value* xv = kb.shared_load_f32_indexed(sX, c.b.build_add(c.b.build_shl(k, c.i32(4)), tx)); | |
| 441 | 1 | return c.b.build_fma_f32(wv, xv, a); | |
| 442 | }); | ||
| 443 | 1 | kb.sync(); | |
| 444 | 1 | return next_acc; | |
| 445 | ✗ | }); | |
| 446 | |||
| 447 | 2 | kb.if_then(c.b.build_and(row_ok, col_ok), [&] { | |
| 448 | 1 | store_elem_dt(c, Dtype::FP16, p[3], c.b.build_add(c.b.build_mul(row, p[5]), col), acc); | |
| 449 | 1 | }); | |
| 450 | 1 | c.b.build_ret_void(); | |
| 451 | 1 | return finalize_fn(fn); | |
| 452 | 1 | } | |
| 453 | |||
| 454 | 1 | Function* build_conv2d_int8w_fallback_mir(Module& mod, bool has_bias) { | |
| 455 | 1 | std::vector<Type> params(5, Type::ptr()); | |
| 456 | 1 | params.insert(params.end(), 18, Type::i32()); | |
| 457 | 1 | Function* fn = mod.create_function("mir_conv2d_int8w_fallback", Type::void_type(), params); | |
| 458 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 459 | 1 | Ctx c(kb); | |
| 460 | 1 | auto p = entry_params(c, fn); | |
| 461 | // 0:X 1:W 2:scales 3:bias 4:Y | ||
| 462 | // 5:N 6:Ci 7:H 8:Wi 9:Co 10:kH 11:kW 12:Ho 13:Wo 14:sh 15:sw 16:ph 17:pw 18:dh 19:dw 20:Cgi 21:Cgo 22:total | ||
| 463 | 1 | Value* idx = kb.global_tid_x(); | |
| 464 | 2 | kb.if_then(c.b.build_slt(idx, p[22]), [&] { | |
| 465 | 1 | Value* ow = c.b.build_umod(idx, p[13]); | |
| 466 | 1 | Value* t = c.b.build_udiv(idx, p[13]); | |
| 467 | 1 | Value* oh = c.b.build_umod(t, p[12]); | |
| 468 | 1 | Value* t2 = c.b.build_udiv(t, p[12]); | |
| 469 | 1 | Value* oc = c.b.build_umod(t2, p[9]); | |
| 470 | 1 | Value* n = c.b.build_udiv(t2, p[9]); | |
| 471 | |||
| 472 | 1 | Value* ih0 = c.b.build_sub(c.b.build_mul(oh, p[14]), p[16]); | |
| 473 | 1 | Value* iw0 = c.b.build_sub(c.b.build_mul(ow, p[15]), p[17]); | |
| 474 | 1 | Value* scale = kb.load_f32(ptr_at(c, p[2], oc, 4)); | |
| 475 | 1 | Value* ic_abs_base = c.b.build_mul(c.b.build_udiv(oc, p[21]), p[20]); | |
| 476 | 1 | Value* ksz = c.b.build_mul(p[10], p[11]); | |
| 477 | 1 | Value* w_oc_base = c.b.build_mul(oc, c.b.build_mul(p[20], ksz)); | |
| 478 | 1 | Value* hw_in = c.b.build_mul(p[7], p[8]); | |
| 479 | 1 | Value* x_n_base = c.b.build_mul(n, c.b.build_mul(p[6], hw_in)); | |
| 480 | |||
| 481 | 2 | Value* acc = kb.for_range_reduce( | |
| 482 | 1 | c.i32(0), p[20], c.i32(1), c.f32(0.0f), | |
| 483 | 2 | [&](Value* ic_loc, Value* a0) -> Value* { | |
| 484 | 1 | Value* ic = c.b.build_add(ic_abs_base, ic_loc); | |
| 485 | 1 | Value* w_ic = c.b.build_add(w_oc_base, c.b.build_mul(ic_loc, ksz)); | |
| 486 | 1 | Value* x_ic = c.b.build_add(x_n_base, c.b.build_mul(ic, hw_in)); | |
| 487 | 2 | return kb.for_range_reduce( | |
| 488 | 1 | c.i32(0), p[10], c.i32(1), a0, | |
| 489 | 2 | [&](Value* kh, Value* a1) -> Value* { | |
| 490 | 1 | Value* ih = c.b.build_add(ih0, c.b.build_mul(kh, p[18])); | |
| 491 | 1 | Value* h_ok = c.b.build_and(c.b.build_sge(ih, c.i32(0)), c.b.build_slt(ih, p[7])); | |
| 492 | 1 | return emit_if_else_values( | |
| 493 | 1 | c, h_ok, {Type::f32()}, | |
| 494 | 2 | [&]() -> std::vector<Value*> { | |
| 495 | 1 | Value* w_kh = c.b.build_add(w_ic, c.b.build_mul(kh, p[11])); | |
| 496 | 1 | Value* x_row = c.b.build_add(x_ic, c.b.build_mul(ih, p[8])); | |
| 497 | 2 | return {kb.for_range_reduce( | |
| 498 | 1 | c.i32(0), p[11], c.i32(1), a1, | |
| 499 | 2 | [&](Value* kw, Value* a2) -> Value* { | |
| 500 | 1 | Value* iw = c.b.build_add(iw0, c.b.build_mul(kw, p[19])); | |
| 501 | 2 | Value* w_ok = c.b.build_and( | |
| 502 | 1 | c.b.build_sge(iw, c.i32(0)), c.b.build_slt(iw, p[8])); | |
| 503 | 1 | return emit_if_else_values( | |
| 504 | 1 | c, w_ok, {Type::f32()}, | |
| 505 | 2 | [&]() -> std::vector<Value*> { | |
| 506 | 1 | Value* xv = load_elem_dt( | |
| 507 | 1 | c, Dtype::FP16, p[0], c.b.build_add(x_row, iw)); | |
| 508 | 2 | Value* ub = kb.load_u8( | |
| 509 | 1 | ptr_at(c, p[1], c.b.build_add(w_kh, kw), 1)); | |
| 510 | 1 | Value* wv = c.b.build_mul(emit_s8_byte_to_f32(c, ub), scale); | |
| 511 | 1 | return {c.b.build_fma_f32(xv, wv, a2)}; | |
| 512 | }, | ||
| 513 | 2 | [&]() -> std::vector<Value*> { return {a2}; })[0]; | |
| 514 | ✗ | })}; | |
| 515 | ✗ | }, | |
| 516 | 2 | [&]() -> std::vector<Value*> { return {a1}; })[0]; | |
| 517 | ✗ | }); | |
| 518 | ✗ | }); | |
| 519 | |||
| 520 | 1 | if (has_bias) { | |
| 521 | 1 | acc = c.b.build_add(acc, load_elem_dt(c, Dtype::FP16, p[3], oc)); | |
| 522 | 1 | } | |
| 523 | 1 | store_elem_dt(c, Dtype::FP16, p[4], idx, acc); | |
| 524 | 1 | }); | |
| 525 | 1 | c.b.build_ret_void(); | |
| 526 | 1 | return finalize_fn(fn); | |
| 527 | 1 | } | |
| 528 | |||
| 529 | } // namespace brotensor::kernels | ||
| 530 |