Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/models/inkling.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ only part of the experts was not measured. Three runs each, 32 tokens written af
| | 10,081 tokens | 3,340-3,360 tokens/s | 5,360-5,400 tokens/s |
| | 20,162 tokens | 3,390-3,400 tokens/s | 5,480-5,510 tokens/s |

On an RTX 20 card (Turing) the engine multiplies the experts on the int8 tensor cores while it reads a prompt, as
it does on RTX 30 and newer, with the prompt's numbers rounded to 8 bits with a scale for every 32. `--no-prompt-int8`
multiplies them as halves instead (`STRATA_PF_I8MMA=0` turns the int8 products off on every card). Measured on a
Quadro RTX 4000 (8 GB), which holds a third to two thirds of the experts: written after the halves' greedy answers to
three prompts of 302 to 8,192 tokens, 64 tokens each, with three files (the two below and an IQ3_S), it gives the
same most likely token at 560 of the 576 steps. Prompt speed, three runs each:

| Size | Prompt | Halves (`--no-prompt-int8`) | int8 (the default on RTX 20) |
| --- | ---: | ---: | ---: |
| **IQ4_NL** | 2,048 tokens | 329-338 tokens/s | 419-447 tokens/s |
| | 8,192 tokens | 355-361 tokens/s | 456-461 tokens/s |
| **UD-Q2_K_XL** | 2,048 tokens | 361-363 tokens/s | 434-439 tokens/s |
| | 8,192 tokens | 347-348 tokens/s | 411-412 tokens/s |

On a laptop with an RTX 4070 Laptop (8 GB), a Core i7-14700HX and 32 GB of RAM, on Windows, UD-Q3_K_XL has 1,216 of
its 3,072 experts on the card and the rest are computed on the CPU. On a card that does not hold every expert, setup
gives Inkling 2 Mini four settings that read fewer bytes for each token: `--attn-requant q5_0` (the attention's
Expand Down
7 changes: 5 additions & 2 deletions engine/cmake/strata_poly.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA
${_strata_gpu_include_directories})
target_link_libraries(poly_mmq_k32_test PRIVATE strata_poly_mmq ${_strata_gpu_blas_target} ${_strata_gpu_runtime_target})
add_test(NAME poly_mmq_k32_test COMMAND poly_mmq_k32_test)
set_tests_properties(poly_mmq_k32_test PROPERTIES SKIP_RETURN_CODE 77)
add_test(NAME poly_mmq_k32_test_turing_int8 COMMAND poly_mmq_k32_test --turing-int8)
set_tests_properties(poly_mmq_k32_test poly_mmq_k32_test_turing_int8 PROPERTIES SKIP_RETURN_CODE 77)
endif()
# a prompt product of each path (the card's, and the BLAS path with STRATA_POLY_PROMPT_BLAS=1) against a double-precision
# product of ggml's dequantized weights; needs a card
Expand All @@ -322,7 +323,9 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA
add_test(NAME poly_mmq_paths_test COMMAND poly_mmq_paths_test)
add_test(NAME poly_mmq_paths_test_blas COMMAND poly_mmq_paths_test)
add_test(NAME poly_mmq_paths_test_x2 COMMAND poly_mmq_paths_test)
set_tests_properties(poly_mmq_paths_test poly_mmq_paths_test_blas poly_mmq_paths_test_x2 PROPERTIES SKIP_RETURN_CODE 77)
add_test(NAME poly_mmq_paths_test_turing_int8 COMMAND poly_mmq_paths_test --turing-int8)
set_tests_properties(poly_mmq_paths_test poly_mmq_paths_test_blas poly_mmq_paths_test_x2 poly_mmq_paths_test_turing_int8
PROPERTIES SKIP_RETURN_CODE 77)
set_tests_properties(poly_mmq_paths_test_blas PROPERTIES ENVIRONMENT "STRATA_POLY_PROMPT_BLAS=1")
set_tests_properties(poly_mmq_paths_test_x2 PROPERTIES ENVIRONMENT "STRATA_PREFILL_NVFP4=w4a4x2")
endif()
Expand Down
6 changes: 5 additions & 1 deletion engine/include/strata/kernels/iq_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,16 @@ bool gemm_iq_f16_grouped(int ty, const void* W, size_t expert_bytes, int n_out,
const int32_t* bounds, int n_experts, int max_rows, float* Y, int ldy, void* stream);
/// The same products on the int8 tensor cores, for matrices of any number of rows (src/kernels/cuda/i8_mma.cuh):
/// the weights decoded to int8 a tile at a time, the activations int8 with a scale per 32 values, FP32 sums.
/// CUDA, Ampere and newer. gemm_iq_i8_takes: the current card runs it for K columns of type ty (K a multiple of 256,
/// 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,
/// 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
/// floats a row; xq holds rows * K * 5 / 4 bytes, 16-byte aligned. gemm_iq_i8_grouped: matrix e (n_out rows,
/// 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]
/// of Y (row i without ids). With `from`, row i is row from[i] of xq. false: not launched.
bool gemm_iq_i8_takes(int ty, int K);
/// The int8 kernel on Turing (sm_75) too, from now on in this process, by m8n8k16 MMAs: the same sums as on Ampere.
/// Off by default, where Turing's products are the halves' (mmq_own.cu); a model's engine turns it on (Inkling 2
/// does unless --no-prompt-int8).
void gemm_iq_i8_turing(bool on);
void gemm_iq_i8_quantize(const float* x, const int32_t* ids, int64_t ld, int64_t rows, int K, void* xq, void* stream);
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,
const int32_t* bounds, int n_mats, int max_rows, const int32_t* ids, float* Y, int64_t ldy, void* stream,
Expand Down
3 changes: 3 additions & 0 deletions engine/src/kernels/cuda/i8_mma.cu
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <atomic>
#include <mutex>
#include <type_traits>

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

void gemm_iq_i8_turing(bool on) { i8m_turing.store(on, std::memory_order_relaxed); }

void gemm_iq_i8_quantize(const float* x, const int32_t* ids, int64_t ld, int64_t rows, int K, void* xq, void* stream) {
if (rows <= 0) return;
float* d = (float*) ((char*) xq + (size_t) rows * (size_t) K);
Expand Down
40 changes: 33 additions & 7 deletions engine/src/kernels/cuda/i8_mma.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// i8_mma.cu inside its anonymous namespace, after i8_expert.cuh: a weight sub-block is decoded by that file's
// i8_load / i8_pack / i8_convert (32 values as 8 words of int8, their integer scales and float base), or read by the
// loaders here and decoded by i8_pack / i8_convert, Q3_K by the decoder here. CUDA only, Ampere and newer: mma.sync
// m16n8k32 and m16n8k16 with s8 operands.
// m16n8k32 and m16n8k16 with s8 operands; Turing when asked (gemm_iq_i8_turing()), by m8n8k16.
//
// 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
// in such chunks). K goes in stages of 256 values; a type of 32-value blocks may end inside the last one
Expand Down Expand Up @@ -86,25 +86,43 @@ struct I8mSmem {
};

// (not volatile: a product has no effect but its sums, and the compiler may then move it among the others)
// Turing (sm_75) has the int8 MMA only as m8n8k16: an m16n8k32 is four of them and an m16n8k16 two, on the same
// operand registers (a0 / a1 the fragment's rows g and g + 8, a2 / a3 its next 16 values; b0 / b1 likewise). The
// sums are integers, so they are the same as Ampere's to the bit.
__device__ __forceinline__ void i8m_mma8(int& c0, int& c1, uint32_t a, uint32_t b) {
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 750
asm("mma.sync.aligned.m8n8k16.row.col.s32.s8.s8.s32 {%0,%1}, {%2}, {%3}, {%0,%1};\n"
: "+r"(c0), "+r"(c1)
: "r"(a), "r"(b));
#endif
}
__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) {
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
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"
: "+r"(c[0]), "+r"(c[1]), "+r"(c[2]), "+r"(c[3])
: "r"(a0), "r"(a1), "r"(a2), "r"(a3), "r"(b0), "r"(b1));
#else
i8m_mma8(c[0], c[1], a0, b0);
i8m_mma8(c[0], c[1], a2, b1);
i8m_mma8(c[2], c[3], a1, b0);
i8m_mma8(c[2], c[3], a3, b1);
#endif
}
__device__ __forceinline__ void i8m_mma16(int (&c)[4], uint32_t a0, uint32_t a1, uint32_t b0) {
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
asm("mma.sync.aligned.m16n8k16.row.col.s32.s8.s8.s32 {%0,%1,%2,%3}, {%4,%5}, {%6}, {%0,%1,%2,%3};\n"
: "+r"(c[0]), "+r"(c[1]), "+r"(c[2]), "+r"(c[3])
: "r"(a0), "r"(a1), "r"(b0));
#else
i8m_mma8(c[0], c[1], a0, b0);
i8m_mma8(c[2], c[3], a1, b0);
#endif
}
// 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
// (lane & 3) of row (lane >> 2) of each piece, which is an MMA's operand as the tensor cores take it. One instruction
// for what took four 32-bit reads.
__device__ __forceinline__ void i8m_ldsm4(uint32_t (&r)[4], const uint32_t* p) {
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 750
asm volatile("ldmatrix.sync.aligned.m8n8.x4.shared.b16 {%0,%1,%2,%3}, [%4];\n"
: "=r"(r[0]), "=r"(r[1]), "=r"(r[2]), "=r"(r[3])
: "r"((uint32_t) __cvta_generic_to_shared(p)));
Expand Down Expand Up @@ -320,7 +338,7 @@ __global__ void __launch_bounds__(i8m_threads(T, NJ, WR)) i8m_kernel(const uint8
const float* __restrict__ XS, const int32_t* __restrict__ bounds, int ch_per_e,
const int32_t* __restrict__ ids, const int32_t* __restrict__ from,
float* __restrict__ Y, long long ldy) {
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 // (a pass for an older card compiles this empty: the host asks i8m_arch_kernel first)
#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<T, NJ, WR, RAW>;
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
Expand Down Expand Up @@ -722,10 +740,15 @@ __global__ void i8m_arch_kernel(int* out) {

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

// the current card: int8 tensor cores in the code it runs, and the shared memory it gives a block
// gemm_iq_i8_turing(): the kernel also on Turing's code (sm_75), whose products a model asks for
std::atomic<bool> i8m_turing{false};

// the current card: int8 tensor cores in the code it runs (Ampere's, or Turing's when asked), and the shared memory
// it gives a block
I8mCard i8m_card() {
static I8mCard known[64];
static std::mutex mu;
Expand All @@ -745,10 +768,12 @@ I8mCard i8m_card() {
cudaFree(d_arch);
}
(void) cudaGetLastError();
c.on = arch >= 800;
c.arch = arch;
known[dev] = c;
}
return known[dev];
I8mCard c = known[dev];
c.on = c.arch >= 800 || (c.arch >= 750 && i8m_turing.load(std::memory_order_relaxed));
return c;
}

template <int T, int NJ, int WR, bool RAW>
Expand Down Expand Up @@ -791,7 +816,8 @@ bool i8m_launch_type(const I8mCard& card, int dev, cudaStream_t s, const uint8_t
// two blocks to one: 6% slower). (The blocks a multiprocessor by shared memory, 1 KB of it the system's a block.)
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); };
const bool raw = raw_on && i8m_raw_type(T) && K % 256 == 0 && (uintptr_t) W % 16 == 0 &&
// (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 &&
(i8m_raw_bytes(T) % 16 != 0 || (row_bytes % 16 == 0 && mat_bytes % 16 == 0)) &&
i8m_tile_bytes<T>(nj, wr, true) <= card.shared &&
per_sm(i8m_tile_bytes<T>(nj, wr, true)) >= per_sm(i8m_tile_bytes<T>(nj, wr, false));
Expand Down
1 change: 1 addition & 0 deletions engine/src/kernels/cuda/iq_kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -7733,6 +7733,7 @@ bool gemm_iq_f16_grouped(int, const void*, size_t, int, int, const void*, int, c
return false;
}
bool gemm_iq_i8_takes(int, int) { return false; }
void gemm_iq_i8_turing(bool) {}
void gemm_iq_i8_quantize(const float*, const int32_t*, int64_t, int64_t, int, void*, void*) {}
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*,
int64_t, void*, const int32_t*) {
Expand Down
4 changes: 2 additions & 2 deletions engine/src/poly/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1878,8 +1878,8 @@ std::unique_ptr<Engine> Engine::open(const Options& o) {
// moe() weighs the picks as a softmax of their scores, which is how only this architecture's router does
if (o.cache_bias > 0.f && !qwen35)
throw std::runtime_error("--cache-bias is supported for qwen35moe models; this is a '" + name + "' model");
if ((!o.attn_requant.empty() || o.loop_bf16) && name != "inkling2_loop")
throw std::runtime_error("--attn-requant and --loop-bf16 are Inkling 2's; this is a '" + name + "' model");
if ((!o.attn_requant.empty() || o.loop_bf16 || o.prompt_int8 >= 0) && name != "inkling2_loop")
throw std::runtime_error("--attn-requant, --loop-bf16 and --prompt-int8 / --no-prompt-int8 are Inkling 2's; this is a '" + name + "' model");
if (o.expert_gpu >= 0 && (name != "inkling2_loop" || !o.layer_split.empty()))
throw std::runtime_error("--expert-gpu is Inkling 2's, on one card beside it (no --layer-split); this is a '" +
name + "' model");
Expand Down
3 changes: 3 additions & 0 deletions engine/src/poly/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ struct Options {
std::string attn_requant; ///< Inkling 2: its attention's six matrices quantized again to this type at load
///< (q4_0, q5_0 or iq4_nl; --attn-requant); empty: the file's types
bool loop_bf16 = false; ///< Inkling 2: its loop controllers' matrices in BF16, not the file's F32 (--loop-bf16)
int prompt_int8 = -1; ///< Inkling 2: on Turing its prompt products on the int8 tensor cores, as on Ampere
///< (gemm_iq_i8_turing()); -1: the default, on; 1: --prompt-int8; 0: --no-prompt-int8,
///< Turing's halves and cuBLAS
float cache_bias = 0; ///< a token's few-at-a-time routing adds this to the score of each expert in VRAM
///< before it picks its experts; their weights stay the softmax of the scores the
///< router gave (--cache-bias, qwen35moe only); 0: the router's own picks
Expand Down
8 changes: 8 additions & 0 deletions engine/src/poly/inkling2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "runtime.hpp"
#include "strata/poly/kernels.hpp"
#include "strata/kernels/native_mmvq.hpp"
#if !defined(STRATA_GPU_METAL)
#include "strata/kernels/iq_kernels.hpp"
#endif

#include <algorithm>
#include <cmath>
Expand Down Expand Up @@ -97,6 +100,11 @@ void Inkling2::load_geometry() {
attn_type_ = a == "q4_0" ? 2 : a == "q5_0" ? 6 : a == "iq4_nl" ? 20 : 0;
if (attn_type_) guess_type_ = attn_type_;
ctrl_bf16_ = opt_.loop_bf16;
#if !defined(STRATA_GPU_METAL)
// Turing's prompt products on the int8 kernel that Ampere and newer take by default (i8_mma.cuh), unless
// --no-prompt-int8 (STRATA_PF_I8MMA=0 turns the kernel off on every card)
kernels::gemm_iq_i8_turing(opt_.prompt_int8 != 0);
#endif
// --kv fp8: the attention caches in FP8 E4M3 with a scale a row's head, about half the bytes of fp16, as Strata
// v0.2.19's Inkling engine has them on small cards (ink_cuda_backend.cu): at 32K tokens the eight full-attention
// runs' caches are 1 GiB in fp16, VRAM the expert cache does without. Only where the flash kernel reads prompts
Expand Down
7 changes: 7 additions & 0 deletions engine/src/poly/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,11 @@ void usage() {
" or iq4_nl) at load: fewer bytes a token, and the answers move a little\n"
" (default: the file's types; setup gives an 8 GB card q5_0)\n"
" --loop-bf16 Inkling 2: its loop controllers' matrices in BF16, not F32\n"
" --no-prompt-int8 Inkling 2 on an RTX 20 (Turing): its prompt products as halves and cuBLAS,\n"
" not on the int8 tensor cores as on RTX 30 and newer (the default there\n"
" too: faster prompts, and the answers move a little, activations int8 a\n"
" 32 values; --prompt-int8 says the default; STRATA_PF_I8MMA=0 turns the\n"
" int8 kernel off on every card)\n"
" --skip-weight F in place of --tail-skip: an expert outside VRAM that every token of a\n"
" window weighs under F of its experts' weight is left out (default 0 = off)\n"
" --cache-bias F qwen35moe: a token's router scores get F added for each expert in VRAM\n"
Expand Down Expand Up @@ -1848,6 +1853,8 @@ int main(int argc, char** argv) {
else if (a == "--route-top") o.route_top = std::atoi(val());
else if (a == "--attn-requant") o.attn_requant = val();
else if (a == "--loop-bf16") o.loop_bf16 = true;
else if (a == "--prompt-int8") o.prompt_int8 = 1;
else if (a == "--no-prompt-int8") o.prompt_int8 = 0;
else if (a == "--skip-weight") o.skip_weight = (float) std::atof(val());
else if (a == "--cache-bias") o.cache_bias = (float) std::atof(val());
else if (a == "--layer-split") o.layer_split = val();
Expand Down
6 changes: 4 additions & 2 deletions engine/src/poly/mmq_k32_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
// must agree to the bit. The unpadded matrices are allocated to their last byte, so that a read past them shows
// under compute-sanitizer. Q4_0's products are checked against its weights' values as well.
//
// usage: poly_mmq_k32_test (exit 0: every case agrees; 77: no card, or no int8 product on it)
// 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)
#include "mmq.hpp"
#include "strata/kernels/iq_kernels.hpp"

#include <cuda_runtime.h>

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

} // namespace

int main() {
int main(int argc, char** argv) {
if (argc > 1 && std::strcmp(argv[1], "--turing-int8") == 0) strata::kernels::gemm_iq_i8_turing(true);
int devices = 0;
if (cudaGetDeviceCount(&devices) != cudaSuccess || devices == 0) {
std::printf("no card: skipped\n");
Expand Down
6 changes: 4 additions & 2 deletions engine/src/poly/mmq_paths_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
// from the same rows gathered by ids, bit for bit. On an RTX 50 card NVFP4 takes the two-term FP4 products
// (nvfp4_x2.hpp) unless STRATA_PREFILL_NVFP4 names another mode: run it with STRATA_PREFILL_NVFP4=fp16 too.
//
// usage: poly_mmq_paths_test (exit 0: every product within 2% RMS of the reference, and an int8 product within
// usage: poly_mmq_paths_test [--turing-int8] (--turing-int8: the int8 path on Turing too, gemm_iq_i8_turing(); exit 0: every product within 2% RMS of the reference, and an int8 product within
// 1e-5 RMS of its own numbers; 77: no card)
#include "mmq.hpp"
#include "ggml.h"
#include "strata/kernels/iq_kernels.hpp"

#include <cublas_v2.h>
#include <cuda_runtime.h>
Expand Down Expand Up @@ -62,7 +63,8 @@ Err compare(const std::vector<float>& y, const std::vector<double>& ref, int64_t

} // namespace

int main() {
int main(int argc, char** argv) {
if (argc > 1 && std::strcmp(argv[1], "--turing-int8") == 0) strata::kernels::gemm_iq_i8_turing(true);
int cards = 0;
if (cudaGetDeviceCount(&cards) != cudaSuccess || cards == 0) {
std::puts("poly_mmq_paths_test: no card, skipped");
Expand Down
Loading