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
7 changes: 7 additions & 0 deletions docs/models/inkling.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ On an RTX 3090 that reads a 2,048-token prompt 3.1% faster (2,068 against 2,005
faster. Against a run with every product in halves, it moved the answers no more than the other int8 products do on
a 302- and an 8,192-token prompt (one prompt each). `STRATA_PREFILL_MXFP4=fp16` keeps them on halves, as before.

UD-Q2_K_XL's Q2_0 experts (the down of 12 layers, rows of 1,152 values) go to the same int8 kernel while it reads a
prompt: on an RTX 30 card or newer by default, on an RTX 20 card with `--prompt-int8`. On an RTX 2070 Super with
`--prompt-int8` that reads a 2,048-token prompt 3.2% faster (858 against 831 tokens/s) and an 8,192-token one 3.4%
faster (902 against 873). Against a run with every product in halves, on six prompts of 302 to 8,192 tokens, the
answers moved about as much as with the other int8 products alone (less on four prompts, more on two).
`STRATA_PF_I8MMA=0` keeps every prompt product on halves.

On an RTX 50 card the engine multiplies the experts kept in NVFP4 (most of them, in both sizes) on the FP4 tensor
cores while it reads a prompt, with the prompt's numbers rounded to about 8 bits instead of 11. That reads a prompt
1.5 to 1.9 times as fast as multiplying them as halves (`STRATA_PREFILL_NVFP4=fp16`). It moves the answers a little:
Expand Down
8 changes: 4 additions & 4 deletions engine/include/strata/kernels/iq_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ bool gemm_iq_f16_grouped(int ty, const void* W, size_t expert_bytes, int n_out,
/// 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 (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, MXFP4's too, of 64 for NVFP4; 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
/// or of 32 for a type of 32-value blocks, MXFP4's too, of 64 for NVFP4 and Q2_0; 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. With `at` (n_mats values on the device,
Expand All @@ -216,9 +216,9 @@ bool gemm_iq_i8_takes(int ty, int K);
/// with --prompt-int8).
void gemm_iq_i8_turing(bool on);
/// The int8 kernel also takes type ty, from now on in this process (on false: no longer), for a type it takes only when
/// asked: IQ1_S (19) and MXFP4 (39). A model's engine asks for them at load (Inkling 2: IQ1_S with
/// STRATA_PREFILL_IQ1S=int8, MXFP4 unless STRATA_PREFILL_MXFP4=fp16), so that other products stay on the FP16 kernel
/// as before. Any other type: nothing changes.
/// asked: IQ1_S (19), MXFP4 (39), and Q2_0 (42) in rows that are not whole 256s. A model's engine asks for them at
/// load (Inkling 2: IQ1_S with STRATA_PREFILL_IQ1S=int8, MXFP4 unless STRATA_PREFILL_MXFP4=fp16, Q2_0), so that other
/// products stay on the FP16 kernel as before. Any other type: nothing changes.
void gemm_iq_i8_allow(int ty, 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,
Expand Down
14 changes: 8 additions & 6 deletions engine/src/kernels/cuda/i8_mma.cu
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ namespace {
#include "i8_mma.cuh"

// The types this kernel takes only once a model's engine has asked for them (gemm_iq_i8_allow()): IQ1_S and MXFP4,
// whose prompt products went to the FP16 kernel before, so that another model's stay as they were
constexpr bool i8m_asked_type(int t) { return t == 19 || t == 39; }
// whose prompt products went to the FP16 kernel before, so that another model's stay as they were; and Q2_0 in rows
// of whole 64-value blocks that are not whole stages of 256 (Inkling 2 Mini's down, K = 1,152), likewise
constexpr bool i8m_asked_type(int t) { return t == 19 || t == 39 || t == 42; }
constexpr bool i8m_asked(int t, int K) { return t == 42 ? K % 256 != 0 : i8m_asked_type(t); }
std::atomic<uint64_t> i8m_allowed{0}; // bit t: type t asked for

void check(const char* what) {
Expand All @@ -40,11 +42,11 @@ void check(const char* what) {
} // namespace

// K whole stages of 256, or for a type of 32-value blocks whole blocks: the kernel leaves out what lies past K in its
// last stage (Gemma 4 26B's down, K = 704: Q8_0, Q5_1, IQ4_NL, Q4_0; Inkling 2 Mini's, K = 1,152: MXFP4); NVFP4 whole blocks of
// 64 (Inkling 2 Mini's down, K = 1,152)
// last stage (Gemma 4 26B's down, K = 704: Q8_0, Q5_1, IQ4_NL, Q4_0; Inkling 2 Mini's, K = 1,152: MXFP4); NVFP4 and Q2_0
// whole blocks of 64 (Inkling 2 Mini's down, K = 1,152)
bool gemm_iq_i8_takes(int ty, int K) {
if (i8m_asked_type(ty) && !((i8m_allowed.load(std::memory_order_relaxed) >> ty) & 1u)) return false;
const int step = ty == 40 ? 64 : ty == 39 || i8_block_vals(ty) == 32 ? 32 : 256;
if (i8m_asked(ty, K) && !((i8m_allowed.load(std::memory_order_relaxed) >> ty) & 1u)) return false;
const int step = ty == 40 || ty == 42 ? 64 : ty == 39 || i8_block_vals(ty) == 32 ? 32 : 256;
return i8m_type(ty) && K > 0 && K % step == 0 && i8m_card().on;
}

Expand Down
2 changes: 1 addition & 1 deletion engine/src/kernels/cuda/i8_mma.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// 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 (MXFP4's too; NVFP4: 64) may end inside the last one
// in such chunks). K goes in stages of 256 values; a type of 32-value blocks (MXFP4's too; NVFP4 and Q2_0: 64) may end inside the last one
// (gemm_iq_i8_takes()). Per stage every thread decodes 8 WR / NT sub-blocks of one weight row (NT threads, 256 or
// 512: i8m_threads()) into shared memory, the stage's activations are copied beside them, and a warp multiplies 16
// weight rows with its tiles of 8 activation rows: one MMA per 32 values, or per 16 for a type with a scale per 16.
Expand Down
5 changes: 5 additions & 0 deletions engine/src/poly/inkling2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ void Inkling2::load_geometry() {
const char* v = std::getenv("STRATA_PREFILL_MXFP4");
kernels::gemm_iq_i8_allow(39, !(v && std::string(v) == "fp16"));
}
// Q2_0 matrices whose rows are whole 64-value blocks but not whole stages of 256 (UD-Q2_K_XL's down in 12 layers,
// rows of 1,152) on the int8 prompt kernel, as the type already is in rows of whole 256s and as the file's other
// types are (on Turing with --prompt-int8). Other models never ask, so their Q2_0 rows of that kind stay on the
// FP16 kernel.
kernels::gemm_iq_i8_allow(42, true);
// On Turing, int8 products on (--prompt-int8): a prompt batch's cached experts in a launch a class of rows
// (Engine::expert_tiles_), the same numbers in fewer launches; STRATA_POLY_EXPERT_TILES=0: a launch a run of slots
// again. Not from Ampere on, where the runs' launches were not measured slower.
Expand Down
2 changes: 1 addition & 1 deletion engine/src/poly/mmq_own.cu
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// mmq.cu is the llama.cpp form of the same class (-DSTRATA_POLY_MMQ=ON); one of the two is built.
//
// On Ampere and newer, a type the int8 decoders have and rows of whole stages of 256 values (of whole blocks, for a
// type of 32-value blocks) go to the int8 tensor cores (gemm_iq_i8_grouped, i8_mma.cuh): the activations are int8 with
// type of 32-value blocks, and for NVFP4 and Q2_0 of 64) go to the int8 tensor cores (gemm_iq_i8_grouped, i8_mma.cuh): the activations are int8 with
// a scale per 32 values, MMQ's numbers, and a matrix may have any number of rows. quantize() and mul() choose by the type and the row length alone, so the rows
// one wrote are the rows the other reads.
//
Expand Down
13 changes: 7 additions & 6 deletions engine/src/poly/mmq_paths_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
// from the same rows gathered by ids, bit for bit; and for the int8 path the experts stored in another order and found
// by their offsets (Mmq::mul_at()) give mul()'s products, 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.
// IQ1_S and MXFP4 take the int8 tiles only where an engine asks for them (gemm_iq_i8_allow(), as Inkling 2 does), so the test
// IQ1_S, MXFP4 and Q2_0 in rows of 1,152 take the int8 tiles only where an engine asks for them (gemm_iq_i8_allow(), as Inkling 2 does), so the test
// asks; where it then takes them, its product is also made on the FP16 kernel (the path it had before) and the two
// are compared. STRATA_MMQ_TEST_BLOCKS=DIR: a type's weights are the bytes of DIR/<type name>.bin where that file is
// long enough (whole blocks cut from a model's tensor), in place of ggml's quantization of random weights.
// With STRATA_PREFILL_NVFP4=w4a8 NVFP4 takes the int8 tiles where the card has them, and is checked as the int8 types are.
//
// 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). Rows of 2,048 values for every type, then rows of 1,152 (Inkling 2's
// experts' downs) for Q8_0, IQ4_NL and NVFP4. Where a row's product does not depend on its launch
// experts' downs) for Q8_0, IQ4_NL, NVFP4 and Q2_0. Where a row's product does not depend on its launch
// (Mmq::split_exact()), 100 of the dense rows multiplied as a launch of their own give the same bits.
#include "mmq.hpp"
#include "ggml.h"
Expand Down Expand Up @@ -83,9 +83,10 @@ int main(int argc, char** argv) {
const char* forced = std::getenv("STRATA_POLY_PROMPT_BLAS");
std::printf("path: %s\n", forced && forced[0] == '1' ? "BLAS (STRATA_POLY_PROMPT_BLAS=1)" : "the card's");
// the 32-value types, the K-quants, the i-quants, MXFP4, NVFP4
const std::vector<int> types_2048 = {2, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 29, 39, 40};
const std::vector<int> types_1152 = {8, 20, 40};
const int asked[] = {19, 39}; // the types the int8 tiles take only when asked
const std::vector<int> types_2048 = {2, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 29, 39, 40, 42};
const std::vector<int> types_1152 = {8, 20, 40, 42};
const int asked[] = {19, 39, 42}; // the types the int8 tiles take only when asked (Q2_0: in rows not whole 256s)
const auto asked_at = [](int t, int64_t k) { return t == 42 ? k % 256 != 0 : t == 19 || t == 39; };
for (int t : asked) strata::kernels::gemm_iq_i8_allow(t, true);
const char* blocks_dir = std::getenv("STRATA_MMQ_TEST_BLOCKS");
constexpr int64_t kOut = 512, kTokens = 256, kExperts = 4;
Expand Down Expand Up @@ -236,7 +237,7 @@ int main(int argc, char** argv) {
experts8.top);
std::printf(" %s\n", fine ? "ok" : "WRONG");

if (int8 && std::find(std::begin(asked), std::end(asked), t) != std::end(asked)) {
if (int8 && asked_at(t, kIn)) {
// the same products on the FP16 kernel, as before the type was asked for: both against the reference, and
// the int8 products against them
strata::kernels::gemm_iq_i8_allow(t, false);
Expand Down
Loading