src/kernels/mir_conv_mma.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | // 2-stage Tensor Core implicit-GEMM Conv2D MIR kernel builder (FP16 / BF16): | ||
| 4 | // Y[n, oc, oh, ow] = sum_{ic, kh, kw} X[n, ic, in_h, in_w] * Wt[oc, ic, kh, kw] [+ bias[oc]] | ||
| 5 | // Covers all 7 SD1.5 + annotator geometries (shape_id 0..6). | ||
| 6 | |||
| 7 | #include <brotensor/tensor.h> | ||
| 8 | |||
| 9 | #include <brass/mir/module.hpp> | ||
| 10 | |||
| 11 | #include <cstdint> | ||
| 12 | |||
| 13 | namespace brotensor::kernels { | ||
| 14 | |||
| 15 | struct Conv2dMmaCfg { | ||
| 16 | std::uint32_t bm = 64; | ||
| 17 | std::uint32_t bn = 64; | ||
| 18 | std::uint32_t warps_m = 2; | ||
| 19 | std::uint32_t warps_n = 2; | ||
| 20 | std::uint32_t threads = 128; | ||
| 21 | std::uint32_t min_ctas_per_sm = 3; | ||
| 22 | }; | ||
| 23 | |||
| 24 | inline constexpr Conv2dMmaCfg kConv2dMmaCfgs[2] = { | ||
| 25 | {64u, 64u, 2u, 2u, 128u, 3u}, // cfg=0: 4 warps, 16 KB smem | ||
| 26 | {128u, 64u, 4u, 2u, 256u, 2u}, // cfg=1: 8 warps, 24 KB smem (larger M) | ||
| 27 | }; | ||
| 28 | |||
| 29 | 1 | struct Conv2dMmaSpec { | |
| 30 | 1 | Dtype dt = Dtype::FP16; // FP16 or BF16 | |
| 31 | 1 | std::uint8_t shape_id = 0; // 0..6: 3x3p1s1, 1x1p0s1, 3x3p1s2, 3x3p0s1, 7x7p3s1, 7x7p0s1, 5x5p2s1 | |
| 32 | 1 | std::uint8_t cfg = 0; // 0 or 1 | |
| 33 | 1 | bool has_bias = false; | |
| 34 | 1 | bool aligned_k8 = false; // (K_tot & 7) == 0 -> cp.async.cg.16 on Wt | |
| 35 | 1 | bool split_k = false; // grid.z splits K by kts tiles of 32; see Params | |
| 36 | }; | ||
| 37 | |||
| 38 | // Params: X(0) Wt(1) bias(2) Y(3) | ||
| 39 | // | N(4) C_in(5) H(6) W(7) C_out(8) H_out(9) W_out(10) M_tot(11) HW_out(12) K_tot(13) | ||
| 40 | // split_k: X Wt bias Y part | N .. K_tot kts. Split 0 stores Y (+ bias); split | ||
| 41 | // z > 0 stores FP32 partial sums at part + (z - 1) N C_out HW_out in Y's layout, | ||
| 42 | // for ConvAux op 7 to add into Y. | ||
| 43 | brass::Function* build_conv2d_mma_mir(brass::Module& mod, const Conv2dMmaSpec& spec); | ||
| 44 | |||
| 45 | } // namespace brotensor::kernels | ||
| 46 |