From 0dd159c6a2357c1e4570980eda7e07bac9eba2e0 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 04:51:20 +0900 Subject: [PATCH 1/4] poly: Inkling 2's int8 prompt tiles in stages of 128 on Turing (same bits) On sm_75 the int8 prompt kernel's tile of 128 activation rows takes 59-63 KB of the multiprocessor's 64 KB, so one block decodes a stage and multiplies it by turns. For Inkling 2 Mini's types (IQ4_NL, Q8_0, NVFP4, IQ2_XXS, IQ2_XS, IQ2_S) in tiles of 32 and 128 rows, the Turing pass now takes stages of 128 values with 256 threads (two blocks a multiprocessor), half of the int32 sums made floats by an exact add in place of the conversion, and for the i-quants shared rows without padding (pieces swizzled by row). The passes for Ampere and newer compile these shapes empty. Each output's terms are added in the same order: the same bits. Other cards, and STRATA_I8M_TURING=0, take the tile as before. RTX 2070 Super, poly_i8m_shapes_test --time: IQ4_NL experts gate|up 5,489 -> 5,002 us, down 3,075 -> 2,551; Q8_0 dense [2304 -> 3072] 615 -> 543; NVFP4 down 3,391 -> 2,681; IQ2_XXS 5,876 -> 5,095. poly_i8m_shapes_test: each shape against the old one, bit for bit (--time: their times), and the launcher's own pick; also rows of 704 and 1,088 values, 2,280 outputs, rows read and written by index with experts without rows; it fails where a Turing shape fits fewer than two blocks a multiprocessor. The kernel is VecSzn's (i8_mma.cuh); this changes only its tile shape on Turing. Co-Authored-By: Claude Opus 5.5 --- engine/cmake/strata_poly.cmake | 10 + engine/src/kernels/cuda/i8_mma.cuh | 255 +++++++++++------ engine/src/poly/i8m_shapes_test.cu | 430 +++++++++++++++++++++++++++++ 3 files changed, 615 insertions(+), 80 deletions(-) create mode 100644 engine/src/poly/i8m_shapes_test.cu diff --git a/engine/cmake/strata_poly.cmake b/engine/cmake/strata_poly.cmake index 11a4ab043..32b3cb01d 100644 --- a/engine/cmake/strata_poly.cmake +++ b/engine/cmake/strata_poly.cmake @@ -370,6 +370,16 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA target_link_libraries(poly_mmq_tiles_test PRIVATE strata_poly_mmq ${_strata_gpu_blas_target} ${_strata_gpu_runtime_target}) add_test(NAME poly_mmq_tiles_test COMMAND poly_mmq_tiles_test) set_tests_properties(poly_mmq_tiles_test PROPERTIES SKIP_RETURN_CODE 77) + # the int8 prompt kernel in each tile shape a card may be given against the shape it had before, bit for bit, on + # Inkling 2 Mini's products (--time: each shape's time); needs a card + if(NOT STRATA_POLY_MMQ AND NOT STRATA_ENABLE_HIP) + add_executable(poly_i8m_shapes_test src/poly/i8m_shapes_test.cu) + target_include_directories(poly_i8m_shapes_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/cuda + ${CMAKE_CURRENT_SOURCE_DIR}/third_party/ggml ${_strata_gpu_include_directories}) + target_link_libraries(poly_i8m_shapes_test PRIVATE strata_poly_mmq ${_strata_gpu_blas_target} ${_strata_gpu_runtime_target}) + add_test(NAME poly_i8m_shapes_test COMMAND poly_i8m_shapes_test) + set_tests_properties(poly_i8m_shapes_test PROPERTIES SKIP_RETURN_CODE 77) + endif() # the int8 product of a matrix whose rows end inside a stage (32-value blocks) against the same rows padded with # zero blocks, bit for bit; needs a card if(NOT STRATA_POLY_MMQ) diff --git a/engine/src/kernels/cuda/i8_mma.cuh b/engine/src/kernels/cuda/i8_mma.cuh index e3f18b060..2b447e2c1 100644 --- a/engine/src/kernels/cuda/i8_mma.cuh +++ b/engine/src/kernels/cuda/i8_mma.cuh @@ -19,6 +19,9 @@ // scale times the sum of its codes). A value's sum is its own, stage after stage, so it does not depend on the // launch its row is in. // +// On Turing some types take another shape of the same arithmetic (I8M_CF_* below, i8m_cf75()): stages of 128 values +// and 256 threads, so that two blocks share a multiprocessor's 64 KB. +// // The activations (gemm_iq_i8_quantize): [rows][K] int8 codes, then [rows][K / 32] float scales, then [rows][K / 32] // floats scale * (sum of the block's codes). A block of 32 values: scale = max|x| / 127, code = round(x / scale). constexpr int I8M_NT = 256; @@ -59,12 +62,42 @@ __host__ __device__ constexpr int i8m_rows(int t, int nj) { return nj == 8 && t // (Q3_K experts 578 -> 642, IQ3_XXS 473 -> 498, Q6_K 345 -> 363 with two). __host__ __device__ constexpr int i8m_tiles_a(int t, int nj, int wr = 64) { return wr == 128 || (nj == 8 && (t == 8 || t == 12 || t == 13)) ? 2 : 1; } -template +// A tile's shape chosen per card by the launcher (CF, bits; 0: the shape above, i8m_threads() / i8m_tiles_a()): +// I8M_CF_T256 256 threads a block, I8M_CF_NA1 one tile of 16 weight rows a warp, +// I8M_CF_CVT the MMAs' int32 sums made floats by an add (i8m_float()) in place of the conversion instruction (I8M_CF_CVT2: +// half of them, so that the conversions and the adds run side by side on their two units), +// I8M_CF_K128 stages of 128 values in place of 256 (half the shared memory, so that two blocks fit a multiprocessor +// that has 64 KB: one decodes its stage while the other multiplies), I8M_CF_SWZ shared rows of codes without the 16 +// bytes between them, their 16-byte pieces in an order that changes from row to row (piece c of row r at c ^ (r & 7)), +// so that 8 rows still lie in 8 banks (3 KB less: the i-quants' tables then leave room for two blocks too). None of +// them changes a value's sum: each is the same terms in the same order, so the same bits. +constexpr int I8M_CF_T256 = 1, I8M_CF_NA1 = 4, I8M_CF_CVT = 16, I8M_CF_CVT2 = 32, I8M_CF_K128 = 64, I8M_CF_SWZ = 128; +__host__ __device__ constexpr int i8m_nt(int t, int nj, int wr, int cf) { return (cf & I8M_CF_T256) ? 256 : i8m_threads(t, nj, wr); } +__host__ __device__ constexpr int i8m_na(int t, int nj, int wr, int cf) { return (cf & I8M_CF_NA1) ? 1 : i8m_tiles_a(t, nj, wr); } +__host__ __device__ constexpr int i8m_ks(int cf) { return (cf & I8M_CF_K128) ? 128 : 256; } +// the threads cover the tile: warps by RG down the weight rows and CG across the activation rows, whole tiles of 8 +// activation rows each, whole 16-byte pieces of the stage's activations, NB / RT sub-blocks of a weight row a thread +__host__ __device__ constexpr bool i8m_shape_ok(int t, int nj, int wr, int cf) { + const int nt = i8m_nt(t, nj, wr, cf), rg = wr / (16 * i8m_na(t, nj, wr, cf)), cg = rg > 0 ? nt / 32 / rg : 0; + const int nb = i8m_ks(cf) / 32, tpr = i8m_ks(cf) / 16; // sub-blocks of a stage, threads of an activation row's stage + // (stages of 128: not the types whose threads decode two sub-blocks that share their reads, i8m_sub()) + return rg >= 1 && cg >= 1 && (2 * nj) % cg == 0 && 2 * nj >= cg && (16 * nj * tpr) % nt == 0 && 16 * nj * tpr >= nt && + nt <= nb * wr && nt >= wr && nb % (nt / wr) == 0 && nt >= 16 * nj && nt >= 256 && + (nb == 8 || !(t == 11 || t == 14 || t == 23)); +} +// An MMA's int32 sum as a float, exactly: |c| < 2^22 (32 products of two int8 codes are at most 2^19), so c + 1.5 2^23 +// is a float whose bits are c + 0x4B400000, and less 1.5 2^23 it is c: the same float as (float) c. An integer add +// and a float add, where the conversion goes to a unit that takes 16 values a clock on a multiprocessor of compute +// capability 7.5 (the CUDA guide's throughput table), a quarter of its float adds. +__device__ __forceinline__ float i8m_float(int c) { return __int_as_float(c + 0x4B400000) - 12582912.0f; } + +template struct I8mSmem { static constexpr int MT = 16 * NJ; // activation rows of a tile + static constexpr int LD = KS / 4 + (SWZ ? 0 : 4); // I8M_LD for a stage of KS values static constexpr int GW = !i8_gg_type(T) && i8_grid_words(T) > 4 ? (i8_grid_words(T) + 3) / 4 * 4 : 0; static constexpr int SG = i8_sign_type(T) ? 512 : 0; - static constexpr int NS = i8m_two(T) ? 16 : 8; // scales of a weight row a stage + static constexpr int NS = i8m_two(T) ? KS / 16 : KS / 32; // scales of a weight row a stage static constexpr int MN = i8_min_type(T) ? 1 : 0; // A type with a minimum per 32 values keeps {scale, minimum} of a sub-block together (PK): those of two sub-blocks // are one 16-byte read @@ -77,9 +110,9 @@ struct I8mSmem { static constexpr int XQ = MN ? 4 : 2; static constexpr int XSTR = MT / 2 * XQ + 4; // floats a block: +4 spreads the blocks over the banks static constexpr int S16 = MT + 4; - static constexpr int o_sg = GW, o_w = o_sg + SG, o_x = o_w + WR * I8M_LD, o_ws = o_x + MT * I8M_LD, o_wm = o_ws + WR * LDS, - o_xq = (o_wm + (MN && !PK) * WR * LDB + M16 * WR * LDM + 3) / 4 * 4, o_x16 = o_xq + 8 * XSTR, - o_id = o_x16 + M16 * 16 * S16, o_raw = (o_id + MT + 3) / 4 * 4, + static constexpr int o_sg = GW, o_w = o_sg + SG, o_x = o_w + WR * LD, o_ws = o_x + MT * LD, o_wm = o_ws + WR * LDS, + o_xq = (o_wm + (MN && !PK) * WR * LDB + M16 * WR * LDM + 3) / 4 * 4, o_x16 = o_xq + KS / 32 * XSTR, + o_id = o_x16 + M16 * KS / 16 * S16, o_raw = (o_id + MT + 3) / 4 * 4, RW = !RAW ? 0 : i8m_raw_bytes(T) % 16 == 0 ? i8m_raw_bytes(T) / 4 : ((i8m_raw_bytes(T) + 15) / 16 + 1) * 4, total = o_raw + WR * RW; static constexpr size_t bytes = (size_t) total * 4; @@ -338,9 +371,9 @@ template constexpr int I8M_RAW = T == 11 ? I8M_Q3K_RAW : T == 10 ? I8M_Q // what they read (Q3_K's code and high-bit words and scales, Q6_K's high bits and low bits: 0 and 2, 1 and 3, ...; // IQ4_XS's scales), which is then read once: on an RTX 5090 their experts' products took 7-28% less time. (Q4_K, Q5_K, // Q8_0 and the i-quants were 2-6% slower so.) -template +template __device__ __forceinline__ int i8m_sub(int r0, int part) { - constexpr int PER = 8 / RT; + constexpr int PER = NB / RT; if constexpr (T == 14 && PER == 2) return (r0 >> 1) * 4 + (r0 & 1) + 2 * part; if constexpr (T == 11 || T == 14 || T == 23) return PER * r0 + part; return r0 + RT * part; @@ -410,30 +443,45 @@ __global__ void __launch_bounds__(I8M_NT) i8m_quantize4_kernel(const float* __re } } -template -__global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8_t* __restrict__ W, size_t mat_bytes, size_t row_bytes, - int n_out, int K, - const uint8_t* __restrict__ X, const float* __restrict__ XD, - const float* __restrict__ XS, const int32_t* __restrict__ bounds, int ch_per_e, - const int32_t* __restrict__ ids, const int32_t* __restrict__ from, - const long long* __restrict__ at, float* __restrict__ Y, long long ldy) { +// The kernel's body (i8m_kernel below launches it) +template +__device__ __forceinline__ void i8m_tile(const uint8_t* __restrict__ W, size_t mat_bytes, size_t row_bytes, int n_out, int K, + const uint8_t* __restrict__ X, const float* __restrict__ XD, + const float* __restrict__ XS, const int32_t* __restrict__ bounds, int ch_per_e, + const int32_t* __restrict__ ids, const int32_t* __restrict__ from, + const long long* __restrict__ at, float* __restrict__ Y, long long ldy) { #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 750 // (a pass for an older card compiles this empty: the host asks i8m_arch_kernel first) - using SM = I8mSmem; + // KS values a stage: NB sub-blocks of 32, a row of activations TPR 16-byte pieces, LD words a shared row + constexpr int KS = i8m_ks(CF), NB = KS / 32, TPR = KS / 16; + static_assert(KS == 256 || !RAW, "i8m_kernel: the raw stage copy is of 256 values"); + constexpr bool SWZ = (CF & I8M_CF_SWZ) != 0; + using SM = I8mSmem; + constexpr int LD = SM::LD; + // words into a shared row of codes of its 16-byte piece c (row7: the row's index & 7) + const auto piece = [](int row7, int c) { return SWZ ? (c ^ row7) * 4 : c * 4; }; constexpr int MT = SM::MT, LDS = SM::LDS, LDB = SM::LDB, XQ = SM::XQ, XSTR = SM::XSTR; // NT threads. A warp multiplies NA tiles of 16 weight rows (two where the activation rows are many: each piece of // them read out of shared memory goes into two products) with NJW tiles of 8 activation rows; RG warps side by // side down the weight rows, CG across the activation rows. - constexpr int NT = i8m_threads(T, NJ, WR), NA = i8m_tiles_a(T, NJ, WR), RG = WR / (16 * NA), CG = NT / 32 / RG; - constexpr int NJW = 2 * NJ / CG, NX = MT * 16 / NT; - constexpr int RT = NT / WR; // threads of a weight row: each decodes 8 / RT of its 8 sub-blocks a stage + constexpr int NT = i8m_nt(T, NJ, WR, CF), NA = i8m_na(T, NJ, WR, CF), RG = WR / (16 * NA), CG = NT / 32 / RG; + constexpr int NJW = 2 * NJ / CG, NX = MT * TPR / NT; + static_assert(i8m_shape_ok(T, NJ, WR, CF), "i8m_kernel: a tile shape the threads do not cover"); + // the int32 sums as floats: c[0..3] by i8m_float() (I8M_CF_CVT), c[0..1] so and c[2..3] converted (I8M_CF_CVT2), or + // all converted + const auto fl = [](int c, int l) -> float { + if constexpr ((CF & I8M_CF_CVT) != 0) return i8m_float(c); + else if constexpr ((CF & I8M_CF_CVT2) != 0) return l < 2 ? i8m_float(c) : (float) c; + else return (float) c; + }; + constexpr int RT = NT / WR; // threads of a weight row: each decodes NB / RT of its NB sub-blocks a stage constexpr bool TWO = i8m_two(T), HAS_MIN = i8_min_type(T), MIN16 = i8m_min16(T); constexpr int LDM = SM::LDM, S16 = SM::S16; extern __shared__ uint4 i8m_dyn[]; uint32_t* const smem = (uint32_t*) i8m_dyn; uint32_t* const s_grid = smem; uint32_t* const s_sg = smem + SM::o_sg; - uint32_t* const s_w = smem + SM::o_w; // [WR][I8M_LD]: a stage of the weight rows' codes - uint32_t* const s_x = smem + SM::o_x; // [MT][I8M_LD]: a stage of the activation rows' codes + uint32_t* const s_w = smem + SM::o_w; // [WR][LD]: a stage of the weight rows' codes + uint32_t* const s_x = smem + SM::o_x; // [MT][LD]: a stage of the activation rows' codes float* const s_ws = (float*) (smem + SM::o_ws); // [WR][LDS]: scale of (weight row, sub-block or half of one) float* const s_wm = (float*) (smem + SM::o_wm); // [WR][LDB]: minimum of (weight row, sub-block) float* const s_xq = (float*) (smem + SM::o_xq); // [8][XSTR]: the activation rows' scales (I8mSmem) @@ -500,26 +548,26 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 // A stage as it is read: this thread's sub-blocks of its weight row, its 16-byte pieces of the activation rows // and its activation scales. - constexpr int PER = 8 / RT, NSC = (MT * 8 + NT - 1) / NT; + constexpr int PER = NB / RT, NSC = (MT * NB + NT - 1) / NT; uint32_t wraw[PER][I8M_RAW]; - uint4 xr[NX]; // piece n: activation row (NT / 16) n + tid / 16 + uint4 xr[NX]; // piece n: activation row (NT / TPR) n + tid / TPR float xdr[NSC], xsr[NSC]; float x16[MIN16 ? NX : 1]; // Q2_K: the scale of the 32-value block of this thread's 16 codes of each row // the rows of X this thread reads: the launch's row, or the one `from` gives for it int xrow[NX], srow[NSC]; #pragma unroll for (int n = 0; n < NX; ++n) { - const int tok = NT / 16 * n + (tid >> 4); + const int tok = NT / TPR * n + tid / TPR; xrow[n] = tok < ntok ? (from ? from[c0 + tok] : c0 + tok) : 0; } #pragma unroll for (int c = 0; c < NSC; ++c) { - const int i = tid + c * NT, tok = i >> 3; - srow[c] = i < MT * 8 && tok < ntok ? (from ? from[c0 + tok] : c0 + tok) : 0; + const int i = tid + c * NT, tok = i / NB; + srow[c] = i < MT * NB && tok < ntok ? (from ? from[c0 + tok] : c0 + tok) : 0; } // A row of 32-value blocks may end inside its last stage (Gemma 4 26B's down: 704 values): the sub-blocks past // its end are not read, and go in as zeros, codes and scales, so they add nothing. - const auto here = [&](int S, int ib) { return S * 256 + ib * 32 < K; }; + const auto here = [&](int S, int ib) { return S * KS + ib * 32 < K; }; // (ib: the sub-block of the stage) const auto fetch = [&](int S) { if constexpr (RAW) { // the pieces of each row's stage, 16 bytes at a time: consecutive threads, consecutive pieces constexpr int RB = i8m_raw_bytes(T), NP = SM::RW / 4; @@ -543,35 +591,37 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 i8m_cp_commit(); } } else if (w_in) { + // the loaders' stage of 256 values and sub-block in it (a stage of 128 is half of one) + const int S2 = S * KS / 256, ib0 = S * NB % 8; #pragma unroll for (int part = 0; part < PER; ++part) { - const int ib = i8m_sub(r0, part); + const int ib = i8m_sub(r0, part), ibg = ib0 + ib; if (!here(S, ib)) continue; - if constexpr (T == 11) i8m_q3k_load(blob + row_off, S, ib, wraw[part]); - else if constexpr (T == 10) i8m_q2k_load(blob + row_off, S, ib, wraw[part]); - else if constexpr (T == 8) i8m_q8_load(blob + row_off, S, ib, wraw[part]); - else if constexpr (T == 14) i8m_q6k_load(blob + row_off, S, ib, wraw[part]); - else if constexpr (T == 23) i8m_iq4xs_load(blob + row_off, S, ib, wraw[part]); - else if constexpr (T == 19) i8m_iq1s_load(blob + row_off, S, ib, wraw[part]); - else if constexpr (T == 39) i8m_mxfp4_load(blob + row_off, S, ib, wraw[part]); - else if constexpr (T == 40) i8m_nvfp4_load(blob + row_off, S, ib, wraw[part]); - else i8_load(blob, i8_offs(row_off, ib & 3), 2 * S + (ib >> 2), wraw[part]); + if constexpr (T == 11) i8m_q3k_load(blob + row_off, S2, ibg, wraw[part]); + else if constexpr (T == 10) i8m_q2k_load(blob + row_off, S2, ibg, wraw[part]); + else if constexpr (T == 8) i8m_q8_load(blob + row_off, S2, ibg, wraw[part]); + else if constexpr (T == 14) i8m_q6k_load(blob + row_off, S2, ibg, wraw[part]); + else if constexpr (T == 23) i8m_iq4xs_load(blob + row_off, S2, ibg, wraw[part]); + else if constexpr (T == 19) i8m_iq1s_load(blob + row_off, S2, ibg, wraw[part]); + else if constexpr (T == 39) i8m_mxfp4_load(blob + row_off, S2, ibg, wraw[part]); + else if constexpr (T == 40) i8m_nvfp4_load(blob + row_off, S2, ibg, wraw[part]); + else i8_load(blob, i8_offs(row_off, ibg & 3), 2 * S2 + (ibg >> 2), wraw[part]); } } #pragma unroll - for (int n = 0; n < NX; ++n) { // 16 bytes of activation row (NT / 16) n + tid / 16 - const int tok = NT / 16 * n + (tid >> 4); + for (int n = 0; n < NX; ++n) { // 16 bytes of activation row (NT / TPR) n + tid / TPR + const int tok = NT / TPR * n + tid / TPR; xr[n] = make_uint4(0, 0, 0, 0); - if (tok < ntok && S * 256 + (tid & 15) * 16 < K) - xr[n] = *(const uint4*) (X + (size_t) xrow[n] * K + (size_t) S * 256 + (tid & 15) * 16); - if constexpr (MIN16) x16[n] = tok < ntok ? XD[(size_t) xrow[n] * (K / 32) + (size_t) S * 8 + ((tid & 15) >> 1)] : 0.0f; + if (tok < ntok && S * KS + tid % TPR * 16 < K) + xr[n] = *(const uint4*) (X + (size_t) xrow[n] * K + (size_t) S * KS + tid % TPR * 16); + if constexpr (MIN16) x16[n] = tok < ntok ? XD[(size_t) xrow[n] * (K / 32) + (size_t) S * NB + (tid % TPR >> 1)] : 0.0f; } #pragma unroll for (int c = 0; c < NSC; ++c) { - const int i = tid + c * NT, tok = i >> 3; - const size_t at = (size_t) srow[c] * (K / 32) + (size_t) S * 8 + (i & 7); + const int i = tid + c * NT, tok = i / NB; + const size_t at = (size_t) srow[c] * (K / 32) + (size_t) S * NB + i % NB; xdr[c] = xsr[c] = 0.0f; - if (i < MT * 8 && tok < ntok && here(S, i & 7)) { + if (i < MT * NB && tok < ntok && here(S, i % NB)) { xdr[c] = XD[at]; if constexpr (HAS_MIN) xsr[c] = XS[at]; } @@ -593,35 +643,35 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 else i8_load(rb, i8_offs(0, ib & 3), 2 * S + (ib >> 2), wraw[part]); } } + const int ib0 = S * NB % 8; // (the sub-block of the loaders' stage of 256 that this stage's first is) #pragma unroll for (int part = 0; part < PER; ++part) { - const int ib = i8m_sub(r0, part); + const int ib = i8m_sub(r0, part), ibg = ib0 + ib; uint32_t q[8] = {0, 0, 0, 0, 0, 0, 0, 0}; float f0 = 0.0f, f1 = 0.0f, mn = 0.0f, mn1 = 0.0f; if (w_in && here(S, ib)) { if constexpr (T == 11) { - i8m_q3k_convert(wraw[part], ib, q, f0, f1); + i8m_q3k_convert(wraw[part], ibg, q, f0, f1); } else if constexpr (T == 10) { - i8m_q2k_convert(wraw[part], ib, q, f0, f1, mn, mn1); + i8m_q2k_convert(wraw[part], ibg, q, f0, f1, mn, mn1); } else if constexpr (T == 19) { i8m_iq1s_convert(wraw[part], grid_rd, q, f0); } else if constexpr (T == 39) { i8m_mxfp4_convert(wraw[part], kv, q, f0); } else if constexpr (T == 40) { - i8m_nvfp4_convert(wraw[part], ib, kv, q, f0, f1); + i8m_nvfp4_convert(wraw[part], ibg, kv, q, f0, f1); } else { uint32_t w[I8NW]; int m0, m1; float base; - i8_pack(wraw[part], ib, w); - i8_convert(w, grid_rd, (const uint2*) s_sg, kv, q, m0, m1, base, ib, &mn); + i8_pack(wraw[part], ibg, w); + i8_convert(w, grid_rd, (const uint2*) s_sg, kv, q, m0, m1, base, ibg, &mn); f0 = base * (float) m0; f1 = base * (float) m1; } } - uint4* dst = (uint4*) (s_w + wr * I8M_LD + ib * 8); - dst[0] = make_uint4(q[0], q[1], q[2], q[3]); - dst[1] = make_uint4(q[4], q[5], q[6], q[7]); + *(uint4*) (s_w + wr * LD + piece(wr & 7, 2 * ib)) = make_uint4(q[0], q[1], q[2], q[3]); + *(uint4*) (s_w + wr * LD + piece(wr & 7, 2 * ib + 1)) = make_uint4(q[4], q[5], q[6], q[7]); if constexpr (TWO) { s_ws[wr * LDS + 2 * ib] = f0; s_ws[wr * LDS + 2 * ib + 1] = f1; @@ -637,19 +687,19 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 } #pragma unroll for (int n = 0; n < NX; ++n) { - *(uint4*) (s_x + (NT / 16 * n + (tid >> 4)) * I8M_LD + (tid & 15) * 4) = xr[n]; + *(uint4*) (s_x + (NT / TPR * n + tid / TPR) * LD + piece((NT / TPR * n + tid / TPR) & 7, tid % TPR)) = xr[n]; if constexpr (MIN16) { // the sum of the thread's 16 codes, times their block's scale const int sum = __dp4a((int) xr[n].w, 0x01010101, __dp4a((int) xr[n].z, 0x01010101, __dp4a((int) xr[n].y, 0x01010101, __dp4a((int) xr[n].x, 0x01010101, 0)))); - s_x16[(tid & 15) * S16 + NT / 16 * n + (tid >> 4)] = x16[n] * (float) sum; + s_x16[tid % TPR * S16 + NT / TPR * n + tid / TPR] = x16[n] * (float) sum; } } #pragma unroll for (int c = 0; c < NSC; ++c) { const int i = tid + c * NT; - if (i < MT * 8) { - const int tok = i >> 3; - float* const p = s_xq + (i & 7) * XSTR + (tok >> 1) * XQ + (tok & 1); + if (i < MT * NB) { + const int tok = i / NB; + float* const p = s_xq + i % NB * XSTR + (tok >> 1) * XQ + (tok & 1); p[0] = xdr[c]; if constexpr (HAS_MIN) p[2] = xsr[c]; } @@ -662,8 +712,10 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 // are one 8- or 16-byte read. Each sum takes its terms in the order of the blocks, so a product does not depend // on how its launch is cut. const int lane = tid & 31; - const uint32_t* const a_row = s_w + (i0 + ((lane >> 3) & 1) * 8 + (lane & 7)) * I8M_LD + (lane >> 4) * 4; - const uint32_t* const b_row = s_x + (8 * jp + (lane & 7)) * I8M_LD + (lane >> 4) * 8 + ((lane >> 3) & 1) * 4; + // (a lane's rows of A and of B are lane & 7 of their 8, as piece() wants: i0 and the B tiles' rows are multiples of 8) + const uint32_t* const a_row = s_w + (i0 + ((lane >> 3) & 1) * 8 + (lane & 7)) * LD; + const uint32_t* const b_row = s_x + (8 * jp + (lane & 7)) * LD; + const int r7 = lane & 7, a_c = lane >> 4, b_c = (lane >> 3) & 3; // and the first piece of each it reads const auto scales = [&](int kb, int j0, float& x0, float& x1, float& s0, float& s1) { const float* const p = s_xq + kb * XSTR + (j0 / 2 + tg) * XQ; if constexpr (HAS_MIN) { @@ -677,13 +729,13 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 const auto multiply = [&]() { if constexpr (TWO) { #pragma unroll - for (int kq = 0; kq < 16; kq += 4) { + for (int kq = 0; kq < KS / 16; kq += 4) { uint32_t A[2][NA][4]; // [kq / 2 pair][tile]: {a0, a1} of kq, kq + 1 (or kq + 2, kq + 3) float w0[4][NA], w1[4][NA], m0[4][NA], m1[4][NA]; #pragma unroll for (int a = 0; a < NA; ++a) { - i8m_ldsm4(A[0][a], a_row + 16 * a * I8M_LD + kq * 4); - i8m_ldsm4(A[1][a], a_row + 16 * a * I8M_LD + (kq + 2) * 4); + i8m_ldsm4(A[0][a], a_row + 16 * a * LD + piece(r7, a_c + kq)); + i8m_ldsm4(A[1][a], a_row + 16 * a * LD + piece(r7, a_c + kq + 2)); #pragma unroll for (int h = 0; h < 4; ++h) { w0[h][a] = sa[16 * a * LDS + kq + h]; @@ -700,7 +752,7 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 const int j0 = 8 * (CG * n + jp); if (j0 >= ntok) break; uint32_t B[4]; // b0 of kq .. kq + 3 - i8m_ldsm4(B, b_row + 8 * CG * n * I8M_LD + kq * 4); + i8m_ldsm4(B, b_row + 8 * CG * n * LD + piece(r7, b_c + kq)); float x0[2], x1[2], s0, s1; scales(kq >> 1, j0, x0[0], x1[0], s0, s1); scales((kq >> 1) + 1, j0, x0[1], x1[1], s0, s1); @@ -713,10 +765,10 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 int c[4] = {0, 0, 0, 0}; i8m_mma16(c, A[h >> 1][a][2 * (h & 1)], A[h >> 1][a][2 * (h & 1) + 1], B[h]); const float xa = x0[h >> 1], xb = x1[h >> 1]; - acc[a][n][0] = fmaf((float) c[0], w0[h][a] * xa, acc[a][n][0]); - acc[a][n][1] = fmaf((float) c[1], w0[h][a] * xb, acc[a][n][1]); - acc[a][n][2] = fmaf((float) c[2], w1[h][a] * xa, acc[a][n][2]); - acc[a][n][3] = fmaf((float) c[3], w1[h][a] * xb, acc[a][n][3]); + acc[a][n][0] = fmaf(fl(c[0], 0), w0[h][a] * xa, acc[a][n][0]); + acc[a][n][1] = fmaf(fl(c[1], 1), w0[h][a] * xb, acc[a][n][1]); + acc[a][n][2] = fmaf(fl(c[2], 2), w1[h][a] * xa, acc[a][n][2]); + acc[a][n][3] = fmaf(fl(c[3], 3), w1[h][a] * xb, acc[a][n][3]); if constexpr (MIN16) { acc[a][n][0] = fmaf(m0[h][a], sm16.x, acc[a][n][0]); acc[a][n][1] = fmaf(m0[h][a], sm16.y, acc[a][n][1]); @@ -729,7 +781,7 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 } } else { #pragma unroll - for (int kb = 0; kb < 8; kb += 2) { + for (int kb = 0; kb < NB; kb += 2) { uint32_t A[2][NA][4]; // [block kb + h][tile]: a0 .. a3 float w0[2][NA], w1[2][NA], m0[2][NA], m1[2][NA]; if constexpr (SM::PK) { @@ -745,7 +797,7 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 for (int h = 0; h < 2; ++h) #pragma unroll for (int a = 0; a < NA; ++a) { - i8m_ldsm4(A[h][a], a_row + 16 * a * I8M_LD + (kb + h) * 8); + i8m_ldsm4(A[h][a], a_row + 16 * a * LD + piece(r7, a_c + 2 * (kb + h))); if constexpr (!SM::PK) { m0[h][a] = m1[h][a] = 0.0f; w0[h][a] = sa[16 * a * LDS + kb + h]; @@ -761,7 +813,7 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 const int j0 = 8 * (CG * n + jp); if (j0 >= ntok) break; uint32_t B[4]; // b0, b1 of block kb, then of kb + 1 - i8m_ldsm4(B, b_row + 8 * CG * n * I8M_LD + kb * 8); + i8m_ldsm4(B, b_row + 8 * CG * n * LD + piece(r7, b_c + 2 * kb)); #pragma unroll for (int h = 0; h < 2; ++h) { float x0, x1, s0, s1; @@ -770,10 +822,10 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 for (int a = 0; a < NA; ++a) { int c[4] = {0, 0, 0, 0}; i8m_mma32(c, A[h][a][0], A[h][a][1], A[h][a][2], A[h][a][3], B[2 * h], B[2 * h + 1]); - acc[a][n][0] = fmaf((float) c[0], w0[h][a] * x0, acc[a][n][0]); - acc[a][n][1] = fmaf((float) c[1], w0[h][a] * x1, acc[a][n][1]); - acc[a][n][2] = fmaf((float) c[2], w1[h][a] * x0, acc[a][n][2]); - acc[a][n][3] = fmaf((float) c[3], w1[h][a] * x1, acc[a][n][3]); + acc[a][n][0] = fmaf(fl(c[0], 0), w0[h][a] * x0, acc[a][n][0]); + acc[a][n][1] = fmaf(fl(c[1], 1), w0[h][a] * x1, acc[a][n][1]); + acc[a][n][2] = fmaf(fl(c[2], 2), w1[h][a] * x0, acc[a][n][2]); + acc[a][n][3] = fmaf(fl(c[3], 3), w1[h][a] * x1, acc[a][n][3]); if constexpr (HAS_MIN) { acc[a][n][0] = fmaf(m0[h][a], s0, acc[a][n][0]); acc[a][n][1] = fmaf(m0[h][a], s1, acc[a][n][1]); @@ -787,7 +839,7 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 } }; - const int stages = (K + 255) / 256; + const int stages = (K + KS - 1) / KS; fetch(0); for (int S = 0; S < stages; ++S) { if constexpr (RAW) i8m_cp_wait(); // this thread's pieces of the raw stage (fetch() after put() reads it: one copy) @@ -819,6 +871,21 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8 #endif } +// (stages of 128: two blocks a multiprocessor, so no more than half its registers a block.) A shape other than the +// one above (CF != 0) is Turing's only (i8m_cf75()): the passes for Ampere and newer compile it empty. +template +__global__ void __launch_bounds__(i8m_nt(T, NJ, WR, CF), (CF & I8M_CF_K128) ? 2 : 1) i8m_kernel(const uint8_t* __restrict__ W, size_t mat_bytes, size_t row_bytes, + int n_out, int K, + const uint8_t* __restrict__ X, const float* __restrict__ XD, + const float* __restrict__ XS, const int32_t* __restrict__ bounds, int ch_per_e, + const int32_t* __restrict__ ids, const int32_t* __restrict__ from, + const long long* __restrict__ at, float* __restrict__ Y, long long ldy) { +#if defined(__CUDA_ARCH__) + if constexpr (CF == 0 || __CUDA_ARCH__ < 800) + i8m_tile(W, mat_bytes, row_bytes, n_out, K, X, XD, XS, bounds, ch_per_e, ids, from, at, Y, ldy); +#endif +} + // the architecture the code running on this card was compiled for (a build may hold no pass for the card's own) __global__ void i8m_arch_kernel(int* out) { #if defined(__CUDA_ARCH__) @@ -866,17 +933,23 @@ I8mCard i8m_card() { return c; } -template +template bool i8m_launch(int dev, dim3 grid, cudaStream_t s, const uint8_t* W, size_t mat_bytes, size_t row_bytes, int n_out, int K, const uint8_t* X, const float* XD, const float* XS, const int32_t* bounds, int ch, const int32_t* ids, const int32_t* from, const long long* at, float* Y, long long ldy) { - constexpr size_t bytes = I8mSmem::bytes; + constexpr size_t bytes = I8mSmem::bytes; static bool attr[64]; // the attribute belongs to a card (see gemm_iq_f16_grouped) if (!attr[dev]) { - cudaFuncSetAttribute(i8m_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) bytes); + cudaFuncSetAttribute(i8m_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) bytes); + // stages of 128: all of the multiprocessor's shared memory, for two blocks + // (only a preference: without it the blocks may run one a multiprocessor, the same numbers slower; its error is + // not left for the launch's check below) + if constexpr ((CF & I8M_CF_K128) != 0) + if (cudaFuncSetAttribute(i8m_kernel, cudaFuncAttributePreferredSharedMemoryCarveout, 100) != cudaSuccess) + (void) cudaGetLastError(); attr[dev] = true; } - i8m_kernel<<>>(W, mat_bytes, row_bytes, n_out, K, X, XD, XS, bounds, ch, + i8m_kernel<<>>(W, mat_bytes, row_bytes, n_out, K, X, XD, XS, bounds, ch, ids, from, at, Y, ldy); return cudaGetLastError() == cudaSuccess; } @@ -894,6 +967,22 @@ size_t i8m_tile_bytes(int nj, int wr, bool raw) { return (size_t) -1; } +// The tile's shape on Turing (the code for sm_75: 64 KB of shared memory a multiprocessor, where a block of the shape +// above takes 59-63 KB and so runs alone, decoding and multiplying by turns), for Inkling 2 Mini's prompt types in +// tiles of 32 and 128 activation rows: stages of 128 and 256 threads, two blocks a multiprocessor, the int32 sums half +// by adds (I8M_CF_CVT2); the i-quants' rows also without padding (I8M_CF_SWZ), for their tables leave no room for two +// blocks otherwise. RTX 2070 Super, poly_i8m_shapes_test --time, against the shape above (the experts of 1,024 +// tokens' top-8 of 128, a launch for each class of rows as the engine's): IQ4_NL experts gate|up 5,489 -> 5,002 us, +// down 3,075 -> 2,551; Q8_0 dense [2304 -> 3072] of 1,024 rows 615 -> 543; IQ2_XXS experts 5,876 -> 5,095, IQ2_XS +// 7,435 -> 6,778, IQ2_S 7,763 -> 7,140; NVFP4 experts down 3,391 -> 2,681, gate|up 5,964 -> 5,247. +// STRATA_I8M_TURING=0: the shape above. +__host__ __device__ constexpr int i8m_cf75(int t, int nj, int wr) { + return wr != 64 || (nj != 2 && nj != 8) ? 0 + : t == 20 || t == 8 || t == 40 ? I8M_CF_K128 | I8M_CF_T256 | I8M_CF_CVT2 + : t == 16 || t == 17 || t == 22 ? I8M_CF_K128 | I8M_CF_T256 | I8M_CF_SWZ | I8M_CF_CVT2 + : 0; +} + // the tile as wide in activation rows as the launch's rows need, of what the card has the shared memory for template bool i8m_launch_type(const I8mCard& card, int dev, cudaStream_t s, const uint8_t* W, size_t mat_bytes, size_t row_bytes, int n_out, int K, @@ -916,7 +1005,13 @@ bool i8m_launch_type(const I8mCard& card, int dev, cudaStream_t s, const uint8_t const long long ch = (max_rows + 16 * nj - 1) / (16 * nj), gy = (long long) n_mats * ch; if (gy > 65535) return false; const dim3 grid((unsigned) ((n_out + wr - 1) / wr), (unsigned) gy); + static const bool turing_on = [] { const char* v = std::getenv("STRATA_I8M_TURING"); return v == nullptr || v[0] != '0'; }(); + const bool turing = turing_on && card.arch < 800 && !raw; #define STRATA_I8M_GO(NJ_, WR_) \ + if constexpr (i8m_cf75(T, NJ_, WR_) != 0) \ + if (nj == NJ_ && wr == WR_ && turing) \ + return i8m_launch(dev, grid, s, W, mat_bytes, row_bytes, n_out, K, X, XD, XS, \ + bounds, (int) ch, ids, from, at, Y, ldy); \ if (nj == NJ_ && wr == WR_) \ return raw ? i8m_launch(dev, grid, s, W, mat_bytes, row_bytes, n_out, K, X, XD, XS, \ bounds, (int) ch, ids, from, at, Y, ldy) \ diff --git a/engine/src/poly/i8m_shapes_test.cu b/engine/src/poly/i8m_shapes_test.cu new file mode 100644 index 000000000..601f5e59a --- /dev/null +++ b/engine/src/poly/i8m_shapes_test.cu @@ -0,0 +1,430 @@ +// src/poly/i8m_shapes_test.cu - the int8 prompt kernel (kernels/cuda/i8_mma.cuh) in each tile shape a card may be given +// (the CF bits of i8m_kernel: threads, tiles a warp, the sums' conversion; and the tile's width in activation rows) +// against the shape it had before, bit for bit, for Inkling 2 Mini's products: its experts' gate|up (2,304 values a +// row) and downs (1,152) of 128 experts with the rows of 1,024 tokens' top-8 picks, launched by class of rows as the +// engine does (up to 32 rows, up to 128, more), and its dense attention products (Q8_0, 1,024 rows). The weights are +// random blocks with sane scales, the activations quantized by the kernel's own quantizer. +// +// "the launcher's pick" is i8m_launch_type() itself (on Ampere and newer the raw stage copy where it takes it; on +// Turing i8m_cf75()), so every other shape is also checked against what a card runs by default. +// usage: poly_i8m_shapes_test [--time] (--time: also each shape's time on this card, the best of 3 runs of 10; +// exit 0: every shape gives the same bits; 77: no card or no int8 MMA) +#if !defined(__HIPCC__) +#include "strata/kernels/iq_kernels.hpp" +#include "strata/kernels/dp4a.hpp" + +#include +#include + +#define GGML_COMMON_DECL_CUDA +#define GGML_COMMON_IMPL_CUDA +#include "ggml-common.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ggml_cuda_dp4a(a, b, c) STRATA_DP4A((a), (b), (c)) + +namespace strata::kernels { +namespace { +#include "i8_expert.cuh" +#include "i8_mma.cuh" +} // namespace +} // namespace strata::kernels + +using namespace strata::kernels; + +namespace { + +bool ok(cudaError_t e, const char* what) { + if (e != cudaSuccess) std::printf("%s: %s\n", what, cudaGetErrorString(e)); + return e == cudaSuccess; +} + +// bytes and values of a type's block, and where its FP16 scale is (-1: none) +struct Blk { int bytes, vals, d_at; }; +Blk blk(int t) { + switch (t) { + case 20: return {18, 32, 0}; // IQ4_NL + case 8: return {34, 32, 0}; // Q8_0 + case 16: return {66, 256, 0}; // IQ2_XXS + case 17: return {74, 256, 0}; // IQ2_XS + case 22: return {82, 256, 0}; // IQ2_S + case 19: return {50, 256, 0}; // IQ1_S + case 40: return {36, 64, -1}; // NVFP4 (UE4M3 scales: any byte is a number) + default: return {0, 0, 0}; + } +} + +struct Launch { int nj, n_mats, first, most; }; // a class's launch: its tile width, experts, first expert, most rows + +// a problem: weights, the rows' quantized activations, the classes' launches +struct Problem { + int T, n_out, K; + size_t row_bytes, mat_bytes; + int n_distinct; // matrices stored; expert e uses matrix e % n_distinct (by offset, as mul_at()) + uint8_t* W = nullptr; + uint8_t* xq = nullptr; + long long* at = nullptr; // per expert, in launch order + int32_t* bounds = nullptr; // per expert, the first row (and the end) + int32_t* ids = nullptr; // scattered: the row of Y of each launch row, and the row of X it reads (from) + int32_t* from = nullptr; + std::vector launches; + int64_t rows = 0; + float* Y = nullptr; +}; + +// the tile width the engine's launcher gives a launch's most rows +int nj_of(int most) { return most <= 16 ? 1 : most <= 32 ? 2 : most <= 64 ? 4 : 8; } + +// scatter: the rows read by index and written by index (ids, from: two shuffles, as a call's rows of several tokens), +// and the experts without rows inside the launches (bounds that are equal), not left out +bool make(Problem& p, int T, int n_out, int K, const std::vector& rows_of, int n_distinct, uint32_t seed, bool scatter = false) { + p.T = T; p.n_out = n_out; p.K = K; + const Blk b = blk(T); + p.row_bytes = (size_t) K / b.vals * b.bytes; + p.mat_bytes = p.row_bytes * n_out; + p.n_distinct = n_distinct; + std::mt19937 rng(seed); + std::vector w(p.mat_bytes * n_distinct); + for (auto& v : w) v = (uint8_t) rng(); + if (b.d_at >= 0) { + std::uniform_real_distribution d(0.002f, 0.02f); + for (size_t o = 0; o + b.bytes <= w.size(); o += b.bytes) { + const __half h = __float2half(d(rng)); + std::memcpy(&w[o + b.d_at], &h, 2); + } + } + // experts by class (up to 32 rows, up to 128, more), as the engine lays them out; the empty ones in no launch + const int n_e = (int) rows_of.size(); + const auto cls = [&](int r) { return scatter && r == 0 ? 1 : r <= 32 ? 0 : r <= 128 ? 1 : 2; }; + std::vector order; + for (int c = 0; c < 3; ++c) + for (int e = 0; e < n_e; ++e) { + const int r = rows_of[(size_t) e]; + if ((r > 0 || scatter) && cls(r) == c) order.push_back(e); + } + std::vector bounds(order.size() + 1, 0); + std::vector at(order.size()); + for (size_t i = 0; i < order.size(); ++i) { + bounds[i + 1] = bounds[i] + rows_of[(size_t) order[i]]; + at[i] = (long long) ((size_t) (order[i] % n_distinct) * p.mat_bytes); + } + p.rows = bounds.back(); + for (size_t i = 0; i < order.size();) { + const int r = rows_of[(size_t) order[i]], c = cls(r); + size_t j = i; + int most = 0; + for (; j < order.size(); ++j) { + const int rj = rows_of[(size_t) order[j]]; + if (cls(rj) != c) break; + most = std::max(most, rj); + } + p.launches.push_back({nj_of(most), (int) (j - i), (int) i, most}); + i = j; + } + // activations: normal values with a few channels 30 times larger + std::vector x((size_t) p.rows * K); + std::normal_distribution nd(0.0f, 1.0f); + for (int64_t r = 0; r < p.rows; ++r) + for (int k = 0; k < K; ++k) x[(size_t) r * K + k] = nd(rng) * (k % 97 == 5 ? 30.0f : 1.0f); + float* dx = nullptr; + const size_t xq_bytes = (size_t) p.rows * K + 2 * (size_t) p.rows * (K / 32) * sizeof(float); + if (!ok(cudaMalloc(&p.W, w.size()), "cudaMalloc") || !ok(cudaMalloc(&p.xq, xq_bytes), "cudaMalloc") || + !ok(cudaMalloc(&dx, x.size() * sizeof(float)), "cudaMalloc") || !ok(cudaMalloc(&p.at, at.size() * sizeof(long long) + 8), "cudaMalloc") || + !ok(cudaMalloc(&p.bounds, bounds.size() * sizeof(int32_t)), "cudaMalloc") || + !ok(cudaMalloc(&p.Y, (size_t) p.rows * n_out * sizeof(float)), "cudaMalloc")) + return false; + cudaMemcpy(p.W, w.data(), w.size(), cudaMemcpyHostToDevice); + cudaMemcpy(dx, x.data(), x.size() * sizeof(float), cudaMemcpyHostToDevice); + cudaMemcpy(p.at, at.data(), at.size() * sizeof(long long), cudaMemcpyHostToDevice); + cudaMemcpy(p.bounds, bounds.data(), bounds.size() * sizeof(int32_t), cudaMemcpyHostToDevice); + if (scatter) { + std::vector ids((size_t) p.rows), from((size_t) p.rows); + std::iota(ids.begin(), ids.end(), 0); + std::iota(from.begin(), from.end(), 0); + std::shuffle(ids.begin(), ids.end(), rng); + std::shuffle(from.begin(), from.end(), rng); + if (!ok(cudaMalloc(&p.ids, ids.size() * sizeof(int32_t)), "cudaMalloc") || !ok(cudaMalloc(&p.from, from.size() * sizeof(int32_t)), "cudaMalloc")) + return false; + cudaMemcpy(p.ids, ids.data(), ids.size() * sizeof(int32_t), cudaMemcpyHostToDevice); + cudaMemcpy(p.from, from.data(), from.size() * sizeof(int32_t), cudaMemcpyHostToDevice); + } + gemm_iq_i8_quantize(dx, nullptr, K, p.rows, K, p.xq, nullptr); + cudaFree(dx); + return ok(cudaDeviceSynchronize(), "quantize"); +} + +void release(Problem& p) { + for (void* v : {(void*) p.W, (void*) p.xq, (void*) p.at, (void*) p.bounds, (void*) p.Y, (void*) p.ids, (void*) p.from}) cudaFree(v); + p = Problem{}; +} + +bool g_occupancy_ok = true; + +// one launch of the kernel in shape (NJ, CF) over a class's experts +template +bool launch(const Problem& p, const Launch& l) { + constexpr size_t bytes = I8mSmem::bytes; + static bool attr = false; + if (!attr) { + if (!ok(cudaFuncSetAttribute(i8m_kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, (int) bytes), "attribute")) + return false; + if (CF & I8M_CF_K128) cudaFuncSetAttribute(i8m_kernel, cudaFuncAttributePreferredSharedMemoryCarveout, 100); + int per_sm = 0; + cudaOccupancyMaxActiveBlocksPerMultiprocessor(&per_sm, i8m_kernel, i8m_nt(T, NJ, 64, CF), bytes); + std::printf(" [type %d, tile %d rows, CF %d: %zu bytes of shared memory, %d threads, %d blocks a multiprocessor]\n", T, + 16 * NJ, CF, bytes, i8m_nt(T, NJ, 64, CF), per_sm); + // Turing's shapes are there for two blocks a multiprocessor: fewer is a failure (the same bits, the speed lost) + if (CF != 0 && CF == i8m_cf75(T, NJ, 64) && i8m_card().arch < 800 && per_sm < 2) { + std::printf(" FAILED: type %d, tile %d rows, CF %d fits %d block a multiprocessor, not 2\n", T, 16 * NJ, CF, per_sm); + g_occupancy_ok = false; + } + attr = true; + } + const int ch = (l.most + 16 * NJ - 1) / (16 * NJ); + const dim3 grid((unsigned) ((p.n_out + 63) / 64), (unsigned) (l.n_mats * ch)); + const float* XD = (const float*) (p.xq + (size_t) p.rows * p.K); + const float* XS = XD + (size_t) p.rows * (p.K / 32); + i8m_kernel<<>>( + p.W, p.mat_bytes, p.row_bytes, p.n_out, p.K, p.xq, XD, XS, p.bounds + l.first, ch, p.ids, p.from, p.at + l.first, p.Y, + p.n_out); + return ok(cudaGetLastError(), "i8m_kernel"); +} + +using Fn = bool (*)(const Problem&, const Launch&); +template +Fn one() { + if constexpr (i8m_shape_ok(T, NJ, 64, CF)) return launch; + else return nullptr; +} +// (tiles of 16 and 64 rows only in the shapes that use them: fewer kernels to compile) +template +Fn by_nj(int nj) { + constexpr bool narrow = CF == 0 || CF == I8M_CF_CVT2 || CF == (I8M_CF_K128 | I8M_CF_T256 | I8M_CF_CVT2); + if constexpr (narrow) { + if (nj == 1) return one(); + if (nj == 4) return one(); + } + return nj == 2 ? one() : nj == 8 ? one() : nullptr; +} + +// A shape: CF, and the most activation rows a tile has (8: as the launcher picks, 4 / 2: no wider than 64 / 32) +struct Shape { const char* name; int cf, cap; }; +constexpr int K128 = I8M_CF_K128 | I8M_CF_T256; +constexpr Shape kShapes[] = { + {"as before", 0, 8}, + {"Turing's (i8m_cf75)", -1, 8}, + {"the launcher's pick", -2, 8}, + {"cvt", I8M_CF_CVT, 8}, + {"cvt2", I8M_CF_CVT2, 8}, + {"256 threads cvt2", I8M_CF_T256 | I8M_CF_CVT2, 8}, + {"na1", I8M_CF_NA1, 8}, + {"tile 64 rows cvt2", I8M_CF_CVT2, 4}, + {"k128", K128, 8}, + {"k128 cvt", K128 | I8M_CF_CVT, 8}, + {"k128 cvt2", K128 | I8M_CF_CVT2, 8}, + {"k128 tile 64 cvt2", K128 | I8M_CF_CVT2, 4}, + {"swz cvt2", I8M_CF_SWZ | I8M_CF_CVT2, 8}, + {"k128 swz", K128 | I8M_CF_SWZ, 8}, + {"k128 swz cvt2", K128 | I8M_CF_SWZ | I8M_CF_CVT2, 8}, +}; + +template +Fn pick(int cf, int nj) { + switch (cf) { +#define S8_CASE(C) case C: return by_nj(nj); + S8_CASE(0) S8_CASE(I8M_CF_CVT) S8_CASE(I8M_CF_CVT2) S8_CASE(I8M_CF_T256 | I8M_CF_CVT2) S8_CASE(I8M_CF_NA1) + S8_CASE(K128) S8_CASE(K128 | I8M_CF_CVT) S8_CASE(K128 | I8M_CF_CVT2) S8_CASE(I8M_CF_SWZ | I8M_CF_CVT2) + S8_CASE(K128 | I8M_CF_SWZ) S8_CASE(K128 | I8M_CF_SWZ | I8M_CF_CVT2) +#undef S8_CASE + default: return nullptr; + } +} +Fn pick_type(int T, int cf, int nj) { + switch (T) { + case 20: return pick<20>(cf, nj); + case 8: return pick<8>(cf, nj); + case 16: return pick<16>(cf, nj); + case 17: return pick<17>(cf, nj); + case 22: return pick<22>(cf, nj); + case 40: return pick<40>(cf, nj); + default: return nullptr; + } +} + +// all of a problem's launches in a shape (only = i: launch i alone); false: a launch failed +bool run(const Problem& p, const Shape& s, int only = -1) { + for (size_t i = 0; i < p.launches.size(); ++i) { + if (only >= 0 && (size_t) only != i) continue; + const Launch& l = p.launches[i]; + if (s.cf == -2) { // the launcher's own pick for this card (on Ampere and newer: the raw stage copy where it takes it) + const float* XD = (const float*) (p.xq + (size_t) p.rows * p.K); + int dev = 0; + cudaGetDevice(&dev); + bool done = false; + switch (p.T) { +#define S8_LAUNCHER(T) \ + case T: done = i8m_launch_type(i8m_card(), dev, nullptr, p.W, p.mat_bytes, p.row_bytes, p.n_out, p.K, p.xq, XD, \ + XD + (size_t) p.rows * (p.K / 32), p.bounds + l.first, l.n_mats, l.most, p.ids, \ + p.from, p.at + l.first, p.Y, p.n_out); break; + S8_LAUNCHER(20) S8_LAUNCHER(8) S8_LAUNCHER(16) S8_LAUNCHER(17) S8_LAUNCHER(22) S8_LAUNCHER(40) +#undef S8_LAUNCHER + default: break; + } + if (!done) return false; + continue; + } + const int nj = std::min(l.nj, s.cap), cf = s.cf >= 0 ? s.cf : i8m_cf75(p.T, nj, 64); + const Fn f = pick_type(p.T, cf, nj); + if (!f) { // (a shape not compiled for this tile: a failure, not a launch of another shape) + std::printf(" no kernel for type %d, tile %d rows, CF %d\n", p.T, 16 * nj, cf); + return false; + } + if (!f(p, l)) return false; + } + return true; +} + +} // namespace + +int main(int argc, char** argv) { + const bool timing = argc > 1 && std::strcmp(argv[1], "--time") == 0; + int cards = 0; + if (cudaGetDeviceCount(&cards) != cudaSuccess || cards == 0) { + std::puts("poly_i8m_shapes_test: no card, skipped"); + return 77; + } + gemm_iq_i8_turing(true); // the library's quantizer + strata::kernels::i8m_turing.store(true, std::memory_order_relaxed); // and this file's copy of the kernel + if (!i8m_card().on) { + std::puts("poly_i8m_shapes_test: no int8 MMA on this card's code, skipped"); + return 77; + } + cudaDeviceProp prop{}; + cudaGetDeviceProperties(&prop, 0); + std::printf("card: %s, sm_%d%d, %d multiprocessors\n", prop.name, prop.major, prop.minor, prop.multiProcessorCount); + + // 1,024 tokens' top-8 of 128 experts, the experts' popularity skewed as a layer's is (median ~50 rows, a few over 300) + std::mt19937 rng(7); + std::vector pop(128); + for (int e = 0; e < 128; ++e) pop[(size_t) e] = 1.0 / std::pow((double) (e + 4), 0.9); + std::shuffle(pop.begin(), pop.end(), rng); + std::vector rows_of(128, 0); + for (int t = 0; t < 1024; ++t) { + std::vector w = pop; + for (int k = 0; k < 8; ++k) { + std::discrete_distribution pick_e(w.begin(), w.end()); + const int e = pick_e(rng); + ++rows_of[(size_t) e]; + w[(size_t) e] = 0.0; + } + } + { + std::vector s = rows_of; + std::sort(s.begin(), s.end()); + std::printf("rows a expert: min %d, median %d, max %d\n", s.front(), s[64], s.back()); + } + struct Case { const char* name; int T, n_out, K; bool dense; bool scatter = false; }; + const Case cases[] = { + {"IQ4_NL experts gate|up", 20, 2304, 2304, false}, {"IQ4_NL experts down", 20, 2304, 1152, false}, + {"Q8_0 experts gate|up", 8, 2304, 2304, false}, {"Q8_0 dense [2304 -> 3072]", 8, 3072, 2304, true}, + {"Q8_0 dense [2304 -> 1024]", 8, 1024, 2304, true}, {"IQ2_S experts gate|up", 22, 2304, 2304, false}, + {"IQ2_XXS experts gate|up", 16, 2304, 2304, false}, {"IQ2_XS experts gate|up", 17, 2304, 2304, false}, + {"NVFP4 experts down", 40, 2304, 1152, false}, {"NVFP4 experts gate|up", 40, 2304, 2304, false}, + // rows that end inside a stage of 128 (Gemma 4 26B's downs: 704 values), outputs not a whole tile of 64, and + // the rows read and written by index with experts without rows among them + {"IQ4_NL K = 704", 20, 2304, 704, false}, {"Q8_0 K = 704", 8, 2304, 704, false}, + {"NVFP4 K = 1088", 40, 2304, 1088, false}, {"IQ4_NL 2280 outputs", 20, 2280, 2304, false}, + {"IQ2_XXS 2280 outputs", 16, 2280, 2304, false}, + {"IQ4_NL scattered", 20, 2304, 2304, false, true}, {"IQ2_S scattered", 22, 2304, 1152 * 2, false, true}, + {"NVFP4 down scattered", 40, 2304, 1152, false, true}, + }; + bool all = true; + cudaEvent_t e0, e1; + cudaEventCreate(&e0); + cudaEventCreate(&e1); + for (const Case& c : cases) { + Problem p; + const std::vector dense_rows = {1024}; + std::vector rows_s = rows_of; // scattered: two experts without rows + rows_s[5] = rows_s[77] = 0; + if (!make(p, c.T, c.n_out, c.K, c.dense ? dense_rows : c.scatter ? rows_s : rows_of, c.dense ? 1 : 16, 11u + (uint32_t) c.T, c.scatter)) + return 1; + const double macs = (double) p.rows * c.n_out * c.K; + std::vector ref((size_t) p.rows * c.n_out), got(ref.size()); + std::printf("%s (%lld rows, %zu launches):\n", c.name, (long long) p.rows, p.launches.size()); + { // the launcher's first tests for the raw stage copy (it also wants the tile to fit as many blocks a multiprocessor) + const bool raw = i8m_card().arch >= 800 && i8m_raw_type(c.T) && c.K % 256 == 0 && + (i8m_raw_bytes(c.T) % 16 != 0 || (p.row_bytes % 16 == 0 && p.mat_bytes % 16 == 0)); + std::printf(" (the launcher's pick: %s)\n", i8m_card().arch < 800 ? "i8m_cf75() on Turing" : raw ? "the raw stage copy, if the tile fits" : "no raw stage copy"); + } + float base_ms = 0; + for (const Shape& s : kShapes) { + if ((s.cf > 0 || s.cf == -1) && i8m_card().arch >= 800) { // (compiled empty there: Turing's only) + if (!timing) std::printf(" %-20s not compiled for this card\n", s.name); + continue; + } + cudaMemset(p.Y, 0xFF, got.size() * sizeof(float)); + if (!run(p, s) || !ok(cudaDeviceSynchronize(), s.name)) { all = false; continue; } + cudaMemcpy(got.data(), p.Y, got.size() * sizeof(float), cudaMemcpyDeviceToHost); + bool same = true; + if (s.cf == 0 && s.cap == 8) ref = got; + else same = std::memcmp(ref.data(), got.data(), ref.size() * sizeof(uint32_t)) == 0; + size_t diff = 0; + if (!same) + for (size_t i = 0; i < ref.size(); ++i) diff += ref[i] != got[i]; + all = all && same; + if (!timing) { + std::printf(" %-20s %s", s.name, same ? "same bits\n" : "DIFFERENT"); + if (!same) std::printf(" (%zu of %zu values)\n", diff, ref.size()); + continue; + } + float best = 1e30f; + for (int rep = 0; rep < 3; ++rep) { + cudaEventRecord(e0); + for (int i = 0; i < 10; ++i) run(p, s); + cudaEventRecord(e1); + cudaEventSynchronize(e1); + float ms = 0; + cudaEventElapsedTime(&ms, e0, e1); + best = std::min(best, ms / 10); + } + if (s.cf == 0 && s.cap == 8) base_ms = best; + std::printf(" %-22s %s %8.1f us %5.1f T MAC/s %+6.1f%% by launch:", s.name, same ? "same bits" : "DIFFERENT", best * 1e3, + macs / (best * 1e-3) / 1e12, base_ms > 0 ? (base_ms / best - 1) * 100 : 0.0); + for (size_t li = 0; li < p.launches.size(); ++li) { // each class's launch alone: its most rows, its time + float one = 1e30f; + for (int rep = 0; rep < 3; ++rep) { + cudaEventRecord(e0); + for (int i = 0; i < 10; ++i) run(p, s, (int) li); + cudaEventRecord(e1); + cudaEventSynchronize(e1); + float ms = 0; + cudaEventElapsedTime(&ms, e0, e1); + one = std::min(one, ms / 10); + } + std::printf(" [%d experts, up to %d rows: %.1f us]", p.launches[li].n_mats, p.launches[li].most, one * 1e3); + } + std::printf("\n"); + } + release(p); + } + all = all && g_occupancy_ok; + std::printf("poly_i8m_shapes_test: %s\n", all ? "every shape gives the same bits" : "FAILED"); + return all ? 0 : 1; +} +#else +int main() { return 77; } +#endif From 07d86b28f8bab25dccd0554c175a36ce2801fca6 Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Wed, 25 Nov 2026 12:45:21 +0900 Subject: [PATCH 2/4] settings register: STRATA_I8M_TURING Registered as a rollback of the 128-value int8 tile stages on Turing. Co-Authored-By: Claude Sonnet 5.5 --- terminal/data/settings-register.json | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 1b115a170..6852ac2c9 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -15761,6 +15761,39 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_I8M_TURING", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/kernels/cuda/i8_mma.cuh", + "line": 1008, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": "on", + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "first_commit": "0dd159c6a2", + "first_date": "2026-11-24", + "since": null, + "last_commit": "0dd159c6a2", + "last_date": "2026-11-24", + "status": "rollback", + "owner": "", + "note": "Inkling 2 on Turing (sm_75): 0 goes back to the earlier shape of the int8 prompt tiles (one block a multiprocessor) instead of the stages of 128 and 256 threads; the answers are the same.", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_I8_BISECT", "backends": [ From c94431724d36ccf8ef2ba1ebf6ea41161d787fab Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:07:35 +0900 Subject: [PATCH 3/4] poly: Inkling 2's 128-value int8 prompt tiles on the sm_86 pass where they beat the default (same bits) On a card with 100 KB of shared memory a multiprocessor (the sm_86 pass: RTX 30, and RTX 40 builds without their own pass) the int8 prompt kernel's 128-row tile also runs one block a multiprocessor. The Turing shapes (stages of 128, 256 threads, two blocks a multiprocessor) now run there too, for the products where they beat the launcher's own pick, its raw stage copy included (i8m_amp_shape()): IQ4_NL where the raw copy is not taken (the experts' downs), IQ2_XXS, NVFP4's downs, Q8_0 in the widest tile up to 1,024 outputs. Each output's terms are added in the same order: the same bits. STRATA_I8M_AMPERE= sets the types (0: none). RTX 3090, poly_i8m_shapes_test --ab 7 (medians, against main's pick): IQ4_NL down 965 -> 782 us, IQ2_XXS 1,672 -> 1,451, NVFP4 down 1,012 -> 940, Q8_0 [2304 -> 1024] 80.6 -> 74.0. Prompt tok/s, IQ4_NL, all experts in VRAM: 2K +2.2%, 8K +2.3%; UD-Q2_K_XL +0.5% / +0.6%. poly_i8m_shapes_test --ab N: main's pick, the launcher's pick and the new shape forced on, interleaved with the order turning every round; medians and every round. Co-Authored-By: Claude Opus 5.5 --- engine/src/kernels/cuda/i8_mma.cuh | 44 +++++++++++++++++++++--- engine/src/poly/i8m_shapes_test.cu | 55 ++++++++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 7 deletions(-) diff --git a/engine/src/kernels/cuda/i8_mma.cuh b/engine/src/kernels/cuda/i8_mma.cuh index 2b447e2c1..8fd28b76a 100644 --- a/engine/src/kernels/cuda/i8_mma.cuh +++ b/engine/src/kernels/cuda/i8_mma.cuh @@ -872,7 +872,7 @@ __device__ __forceinline__ void i8m_tile(const uint8_t* __restrict__ W, size_t m } // (stages of 128: two blocks a multiprocessor, so no more than half its registers a block.) A shape other than the -// one above (CF != 0) is Turing's only (i8m_cf75()): the passes for Ampere and newer compile it empty. +// one above (CF != 0) is Turing's (i8m_cf75()) and sm_86's (i8m_amp_shape()): the other passes compile it empty. template __global__ void __launch_bounds__(i8m_nt(T, NJ, WR, CF), (CF & I8M_CF_K128) ? 2 : 1) i8m_kernel(const uint8_t* __restrict__ W, size_t mat_bytes, size_t row_bytes, int n_out, int K, @@ -881,7 +881,7 @@ __global__ void __launch_bounds__(i8m_nt(T, NJ, WR, CF), (CF & I8M_CF_K128) ? 2 const int32_t* __restrict__ ids, const int32_t* __restrict__ from, const long long* __restrict__ at, float* __restrict__ Y, long long ldy) { #if defined(__CUDA_ARCH__) - if constexpr (CF == 0 || __CUDA_ARCH__ < 800) + if constexpr (CF == 0 || __CUDA_ARCH__ < 800 || __CUDA_ARCH__ == 860) i8m_tile(W, mat_bytes, row_bytes, n_out, K, X, XD, XS, bounds, ch_per_e, ids, from, at, Y, ldy); #endif } @@ -983,6 +983,39 @@ __host__ __device__ constexpr int i8m_cf75(int t, int nj, int wr) { : 0; } +// The sm_86 pass (Ampere and Ada with 100 KB of shared memory a multiprocessor; not sm_80's 164 KB, where the shape +// above already fits two blocks): the same shapes as Turing's, for the products where they beat the launcher's own +// pick, its raw stage copy included (i8m_amp_shape()). Bit t of i8m_amp_types: type t so; STRATA_I8M_AMPERE= sets them (0: none). +inline std::atomic& i8m_amp_types() { + static std::atomic m{[] { + const char* v = std::getenv("STRATA_I8M_AMPERE"); + uint64_t b = (uint64_t{1} << 20) | (uint64_t{1} << 16) | (uint64_t{1} << 40) | (uint64_t{1} << 8); + if (v) { + b = 0; + for (const char* q = v; *q;) { + char* e = nullptr; + const long t = std::strtol(q, &e, 10); + if (e == q) break; + if (t > 0 && t < 64) b |= uint64_t{1} << t; + q = *e == ',' ? e + 1 : e; + } + } + return b; + }()}; + return m; +} + +// The products of those types that take it (RTX 3090, poly_i8m_shapes_test --ab 7, medians against the launcher's own +// pick; Inkling 2 Mini's shapes): IQ4_NL where the raw stage copy is not taken (its experts' downs, 1,152 values a row: +// 969 -> 785 us; its gate|up with the raw copy was no faster), IQ2_XXS (1,752 -> 1,432), NVFP4's downs (rows that end +// inside a stage of 256: 1,051 -> 928; its gate|up was slower), Q8_0 in the widest tile up to 1,024 outputs (the +// attention's [2304 -> 1024] of 1,024 rows: 79.7 -> 74.2, in place of the raw copy; [2304 -> 3072] and the experts were +// not faster). +inline bool i8m_amp_shape(int t, int nj, int n_out, int K, bool raw) { + return t == 20 ? !raw : t == 16 ? true : t == 40 ? K % 256 != 0 : t == 8 ? nj == 8 && n_out <= 1024 : false; +} + // the tile as wide in activation rows as the launch's rows need, of what the card has the shared memory for template bool i8m_launch_type(const I8mCard& card, int dev, cudaStream_t s, const uint8_t* W, size_t mat_bytes, size_t row_bytes, int n_out, int K, @@ -996,7 +1029,7 @@ bool i8m_launch_type(const I8mCard& card, int dev, cudaStream_t s, const uint8_t static const bool raw_on = [] { const char* v = std::getenv("STRATA_I8M_RAW"); return v == nullptr || v[0] != '0'; }(); const auto per_sm = [&](size_t b) { return card.shared_sm / (b + 1024); }; // (Turing has no cp.async to copy it with) - const bool raw = raw_on && card.arch >= 800 && i8m_raw_type(T) && K % 256 == 0 && (uintptr_t) W % 16 == 0 && + bool raw = raw_on && card.arch >= 800 && i8m_raw_type(T) && K % 256 == 0 && (uintptr_t) W % 16 == 0 && (i8m_raw_bytes(T) % 16 != 0 || (row_bytes % 16 == 0 && mat_bytes % 16 == 0)) && i8m_tile_bytes(nj, wr, true) <= card.shared && per_sm(i8m_tile_bytes(nj, wr, true)) >= per_sm(i8m_tile_bytes(nj, wr, false)); @@ -1006,7 +1039,10 @@ bool i8m_launch_type(const I8mCard& card, int dev, cudaStream_t s, const uint8_t if (gy > 65535) return false; const dim3 grid((unsigned) ((n_out + wr - 1) / wr), (unsigned) gy); static const bool turing_on = [] { const char* v = std::getenv("STRATA_I8M_TURING"); return v == nullptr || v[0] != '0'; }(); - const bool turing = turing_on && card.arch < 800 && !raw; + const bool amp = card.arch == 860 && card.shared_sm <= 102400 && wr == 64 && i8m_cf75(T, nj, wr) != 0 && + ((i8m_amp_types().load(std::memory_order_relaxed) >> T) & 1u) && i8m_amp_shape(T, nj, n_out, K, raw); + if (amp) raw = false; + const bool turing = !raw && ((turing_on && card.arch < 800) || amp); #define STRATA_I8M_GO(NJ_, WR_) \ if constexpr (i8m_cf75(T, NJ_, WR_) != 0) \ if (nj == NJ_ && wr == WR_ && turing) \ diff --git a/engine/src/poly/i8m_shapes_test.cu b/engine/src/poly/i8m_shapes_test.cu index 601f5e59a..797e274ce 100644 --- a/engine/src/poly/i8m_shapes_test.cu +++ b/engine/src/poly/i8m_shapes_test.cu @@ -7,7 +7,7 @@ // // "the launcher's pick" is i8m_launch_type() itself (on Ampere and newer the raw stage copy where it takes it; on // Turing i8m_cf75()), so every other shape is also checked against what a card runs by default. -// usage: poly_i8m_shapes_test [--time] (--time: also each shape's time on this card, the best of 3 runs of 10; +// usage: poly_i8m_shapes_test [--time | --ab N] (--time: also each shape's time on this card, the best of 3 runs of 10; // exit 0: every shape gives the same bits; 77: no card or no int8 MMA) #if !defined(__HIPCC__) #include "strata/kernels/iq_kernels.hpp" @@ -227,6 +227,7 @@ constexpr Shape kShapes[] = { {"as before", 0, 8}, {"Turing's (i8m_cf75)", -1, 8}, {"the launcher's pick", -2, 8}, + {"main's pick (no Ampere selection)", -3, 8}, {"cvt", I8M_CF_CVT, 8}, {"cvt2", I8M_CF_CVT2, 8}, {"256 threads cvt2", I8M_CF_T256 | I8M_CF_CVT2, 8}, @@ -269,7 +270,10 @@ bool run(const Problem& p, const Shape& s, int only = -1) { for (size_t i = 0; i < p.launches.size(); ++i) { if (only >= 0 && (size_t) only != i) continue; const Launch& l = p.launches[i]; - if (s.cf == -2) { // the launcher's own pick for this card (on Ampere and newer: the raw stage copy where it takes it) + if (s.cf == -2 || s.cf == -3) { // the launcher's own pick for this card (on Ampere and newer: the raw stage copy where + // it takes it); -3: without the Ampere selection (i8m_amp_types() 0), as main + const uint64_t amp = i8m_amp_types().load(); + if (s.cf == -3) i8m_amp_types().store(0); const float* XD = (const float*) (p.xq + (size_t) p.rows * p.K); int dev = 0; cudaGetDevice(&dev); @@ -283,6 +287,7 @@ bool run(const Problem& p, const Shape& s, int only = -1) { #undef S8_LAUNCHER default: break; } + i8m_amp_types().store(amp); if (!done) return false; continue; } @@ -301,6 +306,9 @@ bool run(const Problem& p, const Shape& s, int only = -1) { int main(int argc, char** argv) { const bool timing = argc > 1 && std::strcmp(argv[1], "--time") == 0; + // --ab N: main's pick, the launcher's pick and the new shape forced on, N rounds each, interleaved (the order turns + // every round), each round the best of 3 runs of 10; then the median of the rounds + const int ab = argc > 2 && std::strcmp(argv[1], "--ab") == 0 ? std::atoi(argv[2]) : 0; int cards = 0; if (cudaGetDeviceCount(&cards) != cudaSuccess || cards == 0) { std::puts("poly_i8m_shapes_test: no card, skipped"); @@ -371,8 +379,49 @@ int main(int argc, char** argv) { std::printf(" (the launcher's pick: %s)\n", i8m_card().arch < 800 ? "i8m_cf75() on Turing" : raw ? "the raw stage copy, if the tile fits" : "no raw stage copy"); } float base_ms = 0; + if (ab > 0) { + const Shape sh[3] = {{"main's pick", -3, 8}, {"the launcher's pick", -2, 8}, {"new shape forced", -1, 8}}; + std::vector t[3]; + for (int k = 0; k < 3; ++k) { // bits first, against main's pick + cudaMemset(p.Y, 0xFF, got.size() * sizeof(float)); + if (!run(p, sh[k]) || !ok(cudaDeviceSynchronize(), sh[k].name)) { all = false; continue; } + cudaMemcpy(got.data(), p.Y, got.size() * sizeof(float), cudaMemcpyDeviceToHost); + if (k == 0) ref = got; + else if (std::memcmp(ref.data(), got.data(), ref.size() * sizeof(uint32_t)) != 0) { + all = false; + std::printf(" %s: DIFFERENT bits\n", sh[k].name); + } + } + for (int r = 0; r < ab; ++r) + for (int i = 0; i < 3; ++i) { + const int k = (r % 2 == 0 ? i + r : 2 - i + r) % 3; + float best = 1e30f; + for (int rep = 0; rep < 3; ++rep) { + cudaEventRecord(e0); + for (int it = 0; it < 10; ++it) run(p, sh[k]); + cudaEventRecord(e1); + cudaEventSynchronize(e1); + float ms = 0; + cudaEventElapsedTime(&ms, e0, e1); + best = std::min(best, ms / 10); + } + t[k].push_back(best * 1e3f); + } + float med[3]; + for (int k = 0; k < 3; ++k) { + std::vector v = t[k]; + std::sort(v.begin(), v.end()); + med[k] = v.empty() ? 0.0f : v[v.size() / 2]; + std::printf(" %-20s median %8.1f us %+6.1f%% vs main's pick rounds:", sh[k].name, med[k], + med[0] > 0 ? (med[0] / med[k] - 1) * 100 : 0.0); + for (float x : t[k]) std::printf(" %.1f", x); + std::printf("\n"); + } + release(p); + continue; + } for (const Shape& s : kShapes) { - if ((s.cf > 0 || s.cf == -1) && i8m_card().arch >= 800) { // (compiled empty there: Turing's only) + if ((s.cf > 0 || s.cf == -1) && i8m_card().arch >= 800 && i8m_card().arch != 860) { // (compiled empty there) if (!timing) std::printf(" %-20s not compiled for this card\n", s.name); continue; } From 1d104345a4328837e9649258b06482c37ebc607c Mon Sep 17 00:00:00 2001 From: Shogo Okada <33060490+okada-shogo@users.noreply.github.com> Date: Wed, 25 Nov 2026 12:45:31 +0900 Subject: [PATCH 4/4] settings register: STRATA_I8M_AMPERE Registered as a research setting (the types that take Turing's tile shapes on sm_86). Co-Authored-By: Claude Sonnet 5.5 --- terminal/data/settings-register.json | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 6852ac2c9..f9513a2e4 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -15728,6 +15728,39 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_I8M_AMPERE", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/kernels/cuda/i8_mma.cuh", + "line": 992, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": null, + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [], + "first_commit": "c94431724d", + "first_date": "2026-11-25", + "since": null, + "last_commit": "c94431724d", + "last_date": "2026-11-25", + "status": "research", + "owner": "", + "note": "Inkling 2 on sm_86 (Ampere, Ada with 100 KB of shared memory): the weight types (numbers, comma-separated) whose int8 prompt products use Turing's tile shapes instead of the launcher's own pick; unset: four types chosen by measurement, 0: none. Same answers.", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_I8M_RAW", "backends": [