Skip to content

Commit b431bdb

Browse files
okada-shogoclaude
andcommitted
poly: --prompt-int8, Inkling 2's prompt products on Turing's int8 tensor cores
On Turing (sm_75) mmq_own.cu multiplies a prompt's quantized matrices as halves: each matrix dequantized and given to cuBLAS, since gemm_iq_i8_grouped (i8_mma.cuh) needs Ampere's m16n8k32 and m16n8k16 int8 MMAs. Turing has the int8 MMA as m8n8k16: an m16n8k32 is four of them and an m16n8k16 two, on the same operand registers, and ldmatrix exists from sm_75. So the same kernel now also builds for sm_75, with integer sums equal to Ampere's to the bit; the raw stage copy (cp.async, Ampere only) stays off there, the weights are read through registers as for the other types. gemm_iq_i8_turing() turns it on for the process. Inkling 2 does so with --prompt-int8; without it nothing changes on Turing (main's default, halves and cuBLAS), and on Ampere and newer the kernel is the default already. The flag is lossy against Turing's default: the activations are int8 with a scale per 32 values, as on Ampere. It covers the types of Inkling 2 Mini's GGUFs: IQ4_NL, Q8_0, IQ3_S, IQ3_XXS, IQ2_*, Q2_0 and the K-quants. poly_mmq_paths_test and poly_mmq_k32_test take --turing-int8 (ctest *_turing_int8). Class: lossy (opt-in, Inkling 2 only); the default path is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d1f711e commit b431bdb

11 files changed

Lines changed: 69 additions & 16 deletions

File tree

‎engine/cmake/strata_poly.cmake‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA
310310
${_strata_gpu_include_directories})
311311
target_link_libraries(poly_mmq_k32_test PRIVATE strata_poly_mmq ${_strata_gpu_blas_target} ${_strata_gpu_runtime_target})
312312
add_test(NAME poly_mmq_k32_test COMMAND poly_mmq_k32_test)
313-
set_tests_properties(poly_mmq_k32_test PROPERTIES SKIP_RETURN_CODE 77)
313+
add_test(NAME poly_mmq_k32_test_turing_int8 COMMAND poly_mmq_k32_test --turing-int8)
314+
set_tests_properties(poly_mmq_k32_test poly_mmq_k32_test_turing_int8 PROPERTIES SKIP_RETURN_CODE 77)
314315
endif()
315316
# a prompt product of each path (the card's, and the BLAS path with STRATA_POLY_PROMPT_BLAS=1) against a double-precision
316317
# product of ggml's dequantized weights; needs a card
@@ -322,7 +323,9 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA
322323
add_test(NAME poly_mmq_paths_test COMMAND poly_mmq_paths_test)
323324
add_test(NAME poly_mmq_paths_test_blas COMMAND poly_mmq_paths_test)
324325
add_test(NAME poly_mmq_paths_test_x2 COMMAND poly_mmq_paths_test)
325-
set_tests_properties(poly_mmq_paths_test poly_mmq_paths_test_blas poly_mmq_paths_test_x2 PROPERTIES SKIP_RETURN_CODE 77)
326+
add_test(NAME poly_mmq_paths_test_turing_int8 COMMAND poly_mmq_paths_test --turing-int8)
327+
set_tests_properties(poly_mmq_paths_test poly_mmq_paths_test_blas poly_mmq_paths_test_x2 poly_mmq_paths_test_turing_int8
328+
PROPERTIES SKIP_RETURN_CODE 77)
326329
set_tests_properties(poly_mmq_paths_test_blas PROPERTIES ENVIRONMENT "STRATA_POLY_PROMPT_BLAS=1")
327330
set_tests_properties(poly_mmq_paths_test_x2 PROPERTIES ENVIRONMENT "STRATA_PREFILL_NVFP4=w4a4x2")
328331
endif()

‎engine/include/strata/kernels/iq_kernels.hpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,15 @@ bool gemm_iq_f16_grouped(int ty, const void* W, size_t expert_bytes, int n_out,
204204
const int32_t* bounds, int n_experts, int max_rows, float* Y, int ldy, void* stream);
205205
/// The same products on the int8 tensor cores, for matrices of any number of rows (src/kernels/cuda/i8_mma.cuh):
206206
/// the weights decoded to int8 a tile at a time, the activations int8 with a scale per 32 values, FP32 sums.
207-
/// CUDA, Ampere and newer. gemm_iq_i8_takes: the current card runs it for K columns of type ty (K a multiple of 256,
207+
/// CUDA, Ampere and newer (Turing with gemm_iq_i8_turing()). gemm_iq_i8_takes: the current card runs it for K columns of type ty (K a multiple of 256,
208208
/// or of 32 for a type of 32-value blocks; STRATA_PF_I8MMA=0 turns it off). gemm_iq_i8_quantize: row i of xq = row ids[i] of x (row i without ids), ld
209209
/// floats a row; xq holds rows * K * 5 / 4 bytes, 16-byte aligned. gemm_iq_i8_grouped: matrix e (n_out rows,
210210
/// mat_bytes apart) times rows bounds[e]..bounds[e+1] of an xq of total_rows rows; row i's products go to row ids[i]
211211
/// of Y (row i without ids). With `from`, row i is row from[i] of xq. false: not launched.
212212
bool gemm_iq_i8_takes(int ty, int K);
213+
/// The int8 kernel on Turing (sm_75) too, from now on in this process, by m8n8k16 MMAs: the same sums as on Ampere.
214+
/// Off by default, where Turing's products are the halves' (mmq_own.cu); a model's engine turns it on when asked.
215+
void gemm_iq_i8_turing(bool on);
213216
void gemm_iq_i8_quantize(const float* x, const int32_t* ids, int64_t ld, int64_t rows, int K, void* xq, void* stream);
214217
bool gemm_iq_i8_grouped(int ty, const void* W, size_t mat_bytes, int n_out, int K, const void* xq, int64_t total_rows,
215218
const int32_t* bounds, int n_mats, int max_rows, const int32_t* ids, float* Y, int64_t ldy, void* stream,

‎engine/src/kernels/cuda/i8_mma.cu‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <cstdint>
1818
#include <cstdio>
1919
#include <cstdlib>
20+
#include <atomic>
2021
#include <mutex>
2122
#include <type_traits>
2223

@@ -39,6 +40,8 @@ bool gemm_iq_i8_takes(int ty, int K) {
3940
return i8m_type(ty) && K > 0 && K % (i8_block_vals(ty) == 32 ? 32 : 256) == 0 && i8m_card().on;
4041
}
4142

43+
void gemm_iq_i8_turing(bool on) { i8m_turing.store(on, std::memory_order_relaxed); }
44+
4245
void gemm_iq_i8_quantize(const float* x, const int32_t* ids, int64_t ld, int64_t rows, int K, void* xq, void* stream) {
4346
if (rows <= 0) return;
4447
float* d = (float*) ((char*) xq + (size_t) rows * (size_t) K);

‎engine/src/kernels/cuda/i8_mma.cuh‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// i8_mma.cu inside its anonymous namespace, after i8_expert.cuh: a weight sub-block is decoded by that file's
44
// i8_load / i8_pack / i8_convert (32 values as 8 words of int8, their integer scales and float base), or read by the
55
// loaders here and decoded by i8_pack / i8_convert, Q3_K by the decoder here. CUDA only, Ampere and newer: mma.sync
6-
// m16n8k32 and m16n8k16 with s8 operands.
6+
// m16n8k32 and m16n8k16 with s8 operands; Turing when asked (gemm_iq_i8_turing()), by m8n8k16.
77
//
88
// A block: WR = 64 weight rows of one matrix x up to 16 NJ of its activation rows (blockIdx.y walks a matrix's rows
99
// in such chunks). K goes in stages of 256 values; a type of 32-value blocks may end inside the last one
@@ -86,25 +86,43 @@ struct I8mSmem {
8686
};
8787

8888
// (not volatile: a product has no effect but its sums, and the compiler may then move it among the others)
89+
// Turing (sm_75) has the int8 MMA only as m8n8k16: an m16n8k32 is four of them and an m16n8k16 two, on the same
90+
// operand registers (a0 / a1 the fragment's rows g and g + 8, a2 / a3 its next 16 values; b0 / b1 likewise). The
91+
// sums are integers, so they are the same as Ampere's to the bit.
92+
__device__ __forceinline__ void i8m_mma8(int& c0, int& c1, uint32_t a, uint32_t b) {
93+
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 750
94+
asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0,%1}, {%2}, {%3}, {%0,%1};\n"
95+
: "+r"(c0), "+r"(c1)
96+
: "r"(a), "r"(b));
97+
#endif
98+
}
8999
__device__ __forceinline__ void i8m_mma32(int (&c)[4], uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3, uint32_t b0, uint32_t b1) {
90100
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
91101
asm("mma.sync.aligned.m16n8k32.row.col.s32.s8.s8.s32 {%0,%1,%2,%3}, {%4,%5,%6,%7}, {%8,%9}, {%0,%1,%2,%3};\n"
92102
: "+r"(c[0]), "+r"(c[1]), "+r"(c[2]), "+r"(c[3])
93103
: "r"(a0), "r"(a1), "r"(a2), "r"(a3), "r"(b0), "r"(b1));
104+
#else
105+
i8m_mma8(c[0], c[1], a0, b0);
106+
i8m_mma8(c[0], c[1], a2, b1);
107+
i8m_mma8(c[2], c[3], a1, b0);
108+
i8m_mma8(c[2], c[3], a3, b1);
94109
#endif
95110
}
96111
__device__ __forceinline__ void i8m_mma16(int (&c)[4], uint32_t a0, uint32_t a1, uint32_t b0) {
97112
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
98113
asm("mma.sync.aligned.m16n8k16.row.col.s32.s8.s8.s32 {%0,%1,%2,%3}, {%4,%5}, {%6}, {%0,%1,%2,%3};\n"
99114
: "+r"(c[0]), "+r"(c[1]), "+r"(c[2]), "+r"(c[3])
100115
: "r"(a0), "r"(a1), "r"(b0));
116+
#else
117+
i8m_mma8(c[0], c[1], a0, b0);
118+
i8m_mma8(c[2], c[3], a1, b0);
101119
#endif
102120
}
103121
// Four 8 x 16-byte pieces of shared memory, a lane's 16 bytes at p (lanes 8 m to 8 m + 7: the rows of piece m): word
104122
// (lane & 3) of row (lane >> 2) of each piece, which is an MMA's operand as the tensor cores take it. One instruction
105123
// for what took four 32-bit reads.
106124
__device__ __forceinline__ void i8m_ldsm4(uint32_t (&r)[4], const uint32_t* p) {
107-
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
125+
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 750
108126
asm volatile("ldmatrix.sync.aligned.m8n8.x4.shared.b16 {%0,%1,%2,%3}, [%4];\n"
109127
: "=r"(r[0]), "=r"(r[1]), "=r"(r[2]), "=r"(r[3])
110128
: "r"((uint32_t) __cvta_generic_to_shared(p)));
@@ -320,7 +338,7 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8
320338
const float* __restrict__ XS, const int32_t* __restrict__ bounds, int ch_per_e,
321339
const int32_t* __restrict__ ids, const int32_t* __restrict__ from,
322340
float* __restrict__ Y, long long ldy) {
323-
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 // (a pass for an older card compiles this empty: the host asks i8m_arch_kernel first)
341+
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 750 // (a pass for an older card compiles this empty: the host asks i8m_arch_kernel first)
324342
using SM = I8mSmem<T, NJ, WR, RAW>;
325343
constexpr int MT = SM::MT, LDS = SM::LDS, LDB = SM::LDB, XQ = SM::XQ, XSTR = SM::XSTR;
326344
// NT threads. A warp multiplies NA tiles of 16 weight rows (two where the activation rows are many: each piece of
@@ -722,10 +740,15 @@ __global__ void i8m_arch_kernel(int* out) {
722740

723741
struct I8mCard {
724742
bool read = false, on = false;
743+
int arch = 0; // the architecture of the code it runs (i8m_arch_kernel)
725744
size_t shared = 0, shared_sm = 0; // a block's most, a multiprocessor's
726745
};
727746

728-
// the current card: int8 tensor cores in the code it runs, and the shared memory it gives a block
747+
// gemm_iq_i8_turing(): the kernel also on Turing's code (sm_75), whose products a model asks for
748+
std::atomic<bool> i8m_turing{false};
749+
750+
// the current card: int8 tensor cores in the code it runs (Ampere's, or Turing's when asked), and the shared memory
751+
// it gives a block
729752
I8mCard i8m_card() {
730753
static I8mCard known[64];
731754
static std::mutex mu;
@@ -745,10 +768,12 @@ I8mCard i8m_card() {
745768
cudaFree(d_arch);
746769
}
747770
(void) cudaGetLastError();
748-
c.on = arch >= 800;
771+
c.arch = arch;
749772
known[dev] = c;
750773
}
751-
return known[dev];
774+
I8mCard c = known[dev];
775+
c.on = c.arch >= 800 || (c.arch >= 750 && i8m_turing.load(std::memory_order_relaxed));
776+
return c;
752777
}
753778

754779
template <int T, int NJ, int WR, bool RAW>
@@ -791,7 +816,8 @@ bool i8m_launch_type(const I8mCard& card, int dev, cudaStream_t s, const uint8_t
791816
// two blocks to one: 6% slower). (The blocks a multiprocessor by shared memory, 1 KB of it the system's a block.)
792817
static const bool raw_on = [] { const char* v = std::getenv("STRATA_I8M_RAW"); return v == nullptr || v[0] != '0'; }();
793818
const auto per_sm = [&](size_t b) { return card.shared_sm / (b + 1024); };
794-
const bool raw = raw_on && i8m_raw_type(T) && K % 256 == 0 && (uintptr_t) W % 16 == 0 &&
819+
// (Turing has no cp.async to copy it with)
820+
const bool raw = raw_on && card.arch >= 800 && i8m_raw_type(T) && K % 256 == 0 && (uintptr_t) W % 16 == 0 &&
795821
(i8m_raw_bytes(T) % 16 != 0 || (row_bytes % 16 == 0 && mat_bytes % 16 == 0)) &&
796822
i8m_tile_bytes<T>(nj, wr, true) <= card.shared &&
797823
per_sm(i8m_tile_bytes<T>(nj, wr, true)) >= per_sm(i8m_tile_bytes<T>(nj, wr, false));

‎engine/src/kernels/cuda/iq_kernels.cu‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7733,6 +7733,7 @@ bool gemm_iq_f16_grouped(int, const void*, size_t, int, int, const void*, int, c
77337733
return false;
77347734
}
77357735
bool gemm_iq_i8_takes(int, int) { return false; }
7736+
void gemm_iq_i8_turing(bool) {}
77367737
void gemm_iq_i8_quantize(const float*, const int32_t*, int64_t, int64_t, int, void*, void*) {}
77377738
bool gemm_iq_i8_grouped(int, const void*, size_t, int, int, const void*, int64_t, const int32_t*, int, int, const int32_t*, float*,
77387739
int64_t, void*, const int32_t*) {

‎engine/src/poly/engine.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,8 +1878,8 @@ std::unique_ptr<Engine> Engine::open(const Options& o) {
18781878
// moe() weighs the picks as a softmax of their scores, which is how only this architecture's router does
18791879
if (o.cache_bias > 0.f && !qwen35)
18801880
throw std::runtime_error("--cache-bias is supported for qwen35moe models; this is a '" + name + "' model");
1881-
if ((!o.attn_requant.empty() || o.loop_bf16) && name != "inkling2_loop")
1882-
throw std::runtime_error("--attn-requant and --loop-bf16 are Inkling 2's; this is a '" + name + "' model");
1881+
if ((!o.attn_requant.empty() || o.loop_bf16 || o.prompt_int8) && name != "inkling2_loop")
1882+
throw std::runtime_error("--attn-requant, --loop-bf16 and --prompt-int8 are Inkling 2's; this is a '" + name + "' model");
18831883
if (o.expert_gpu >= 0 && (name != "inkling2_loop" || !o.layer_split.empty()))
18841884
throw std::runtime_error("--expert-gpu is Inkling 2's, on one card beside it (no --layer-split); this is a '" +
18851885
name + "' model");

‎engine/src/poly/engine.hpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ struct Options {
131131
std::string attn_requant; ///< Inkling 2: its attention's six matrices quantized again to this type at load
132132
///< (q4_0, q5_0 or iq4_nl; --attn-requant); empty: the file's types
133133
bool loop_bf16 = false; ///< Inkling 2: its loop controllers' matrices in BF16, not the file's F32 (--loop-bf16)
134+
bool prompt_int8 = false; ///< Inkling 2: on Turing its prompt products on the int8 tensor cores, as on Ampere
135+
///< (gemm_iq_i8_turing(); --prompt-int8); off: Turing's halves and cuBLAS
134136
float cache_bias = 0; ///< a token's few-at-a-time routing adds this to the score of each expert in VRAM
135137
///< before it picks its experts; their weights stay the softmax of the scores the
136138
///< router gave (--cache-bias, qwen35moe only); 0: the router's own picks

‎engine/src/poly/inkling2.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include "runtime.hpp"
66
#include "strata/poly/kernels.hpp"
77
#include "strata/kernels/native_mmvq.hpp"
8+
#if !defined(STRATA_GPU_METAL)
9+
#include "strata/kernels/iq_kernels.hpp"
10+
#endif
811

912
#include <algorithm>
1013
#include <cmath>
@@ -97,6 +100,10 @@ void Inkling2::load_geometry() {
97100
attn_type_ = a == "q4_0" ? 2 : a == "q5_0" ? 6 : a == "iq4_nl" ? 20 : 0;
98101
if (attn_type_) guess_type_ = attn_type_;
99102
ctrl_bf16_ = opt_.loop_bf16;
103+
#if !defined(STRATA_GPU_METAL)
104+
// --prompt-int8: Turing's prompt products on the int8 kernel that Ampere and newer take by default (i8_mma.cuh)
105+
if (opt_.prompt_int8) kernels::gemm_iq_i8_turing(true);
106+
#endif
100107
// --kv fp8: the attention caches in FP8 E4M3 with a scale a row's head, about half the bytes of fp16, as Strata
101108
// v0.2.19's Inkling engine has them on small cards (ink_cuda_backend.cu): at 32K tokens the eight full-attention
102109
// runs' caches are 1 GiB in fp16, VRAM the expert cache does without. Only where the flash kernel reads prompts

‎engine/src/poly/main.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,9 @@ void usage() {
17211721
" or iq4_nl) at load: fewer bytes a token, and the answers move a little\n"
17221722
" (default: the file's types; setup gives an 8 GB card q5_0)\n"
17231723
" --loop-bf16 Inkling 2: its loop controllers' matrices in BF16, not F32\n"
1724+
" --prompt-int8 Inkling 2 on an RTX 20 (Turing): its prompt products on the int8 tensor\n"
1725+
" cores as on RTX 30 and newer, in place of halves and cuBLAS: faster\n"
1726+
" prompts, and the answers move a little (activations int8 a 32 values)\n"
17241727
" --skip-weight F in place of --tail-skip: an expert outside VRAM that every token of a\n"
17251728
" window weighs under F of its experts' weight is left out (default 0 = off)\n"
17261729
" --cache-bias F qwen35moe: a token's router scores get F added for each expert in VRAM\n"
@@ -1848,6 +1851,7 @@ int main(int argc, char** argv) {
18481851
else if (a == "--route-top") o.route_top = std::atoi(val());
18491852
else if (a == "--attn-requant") o.attn_requant = val();
18501853
else if (a == "--loop-bf16") o.loop_bf16 = true;
1854+
else if (a == "--prompt-int8") o.prompt_int8 = true;
18511855
else if (a == "--skip-weight") o.skip_weight = (float) std::atof(val());
18521856
else if (a == "--cache-bias") o.cache_bias = (float) std::atof(val());
18531857
else if (a == "--layer-split") o.layer_split = val();

‎engine/src/poly/mmq_k32_test.cu‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
// must agree to the bit. The unpadded matrices are allocated to their last byte, so that a read past them shows
55
// under compute-sanitizer. Q4_0's products are checked against its weights' values as well.
66
//
7-
// usage: poly_mmq_k32_test (exit 0: every case agrees; 77: no card, or no int8 product on it)
7+
// usage: poly_mmq_k32_test [--turing-int8] (--turing-int8: the int8 product on Turing too, gemm_iq_i8_turing(); exit 0: every case agrees; 77: no card, or no int8 product on it)
88
#include "mmq.hpp"
9+
#include "strata/kernels/iq_kernels.hpp"
910

1011
#include <cuda_runtime.h>
1112

@@ -121,7 +122,8 @@ int run_case(const Quant& q, int k, int n, const std::vector<int>& rows, std::mt
121122

122123
} // namespace
123124

124-
int main() {
125+
int main(int argc, char** argv) {
126+
if (argc > 1 && std::strcmp(argv[1], "--turing-int8") == 0) strata::kernels::gemm_iq_i8_turing(true);
125127
int devices = 0;
126128
if (cudaGetDeviceCount(&devices) != cudaSuccess || devices == 0) {
127129
std::printf("no card: skipped\n");

0 commit comments

Comments
 (0)