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
5 changes: 3 additions & 2 deletions docs/models/inkling.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ only part of the experts was not measured. Three runs each, 32 tokens written af
On an RTX 20 card (Turing) the engine multiplies the experts as halves while it reads a prompt. With `--prompt-int8`
it multiplies them on the int8 tensor cores, as it does on RTX 30 and newer, with the prompt's numbers rounded to 8
bits with a scale for every 32 (`STRATA_PF_I8MMA=0` turns the int8 products off on every card). It is not the
default because it moves the answers more: measured on a Quadro RTX 4000 (8 GB),
default on RTX 20 because it moves the answers more: 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 (97.2%). Prompt speed on that card, three runs each:
at 560 of the 576 steps (97.2%). On an AMD RDNA3 or RDNA4 card (HIP) the same kernel is the default, and
`--no-prompt-int8` turns it off. Prompt speed on the RTX 20 card, three runs each:

| Size | Prompt | Halves (the default on RTX 20) | int8 (`--prompt-int8`) |
| --- | ---: | ---: | ---: |
Expand Down
9 changes: 5 additions & 4 deletions engine/include/strata/kernels/iq_kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,17 @@ 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 (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,
/// CUDA, Ampere and newer (Turing with gemm_iq_i8_turing()); HIP, RDNA3 and RDNA4 (gfx11 / gfx12 WMMA, the same sums
/// and so the same bits; on by default there, off with gemm_iq_i8_turing(false)). 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
/// 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,
/// multiples of 16), matrix e is at W + at[e] in place of W + e * mat_bytes. 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
/// with --prompt-int8).
/// The int8 kernel on Turing (sm_75) by m8n8k16 MMAs, or not on HIP's RDNA3 / RDNA4, from now on in this process: the
/// same sums as on Ampere. Off by default on Turing, where the products are the halves' (mmq_own.cu), and on by
/// default on HIP; a model's engine says (Inkling 2: --prompt-int8 on Turing, --no-prompt-int8 on HIP).
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
Expand Down
8 changes: 6 additions & 2 deletions engine/src/kernels/cuda/i8_mma.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
// include/strata/kernels/iq_kernels.hpp; the kernel is i8_mma.cuh). A file of their own so that a change to them
// builds without iq_kernels.cu. The weight sub-blocks are decoded by i8_expert.cuh, which this file includes as
// iq_kernels.cu does, in an anonymous namespace (only what i8_mma.cuh instantiates of it is compiled here).
// CUDA only: a HIP build compiles this file empty and takes iq_kernels.cu's stubs.
#if !defined(__HIPCC__)
// HIP: RDNA3 and RDNA4 (i8_mma.cuh's i8m_wmma); a gfx906 build (wave64) compiles this file empty and takes
// iq_kernels.cu's stubs.
#if !defined(STRATA_HIP_GFX906)
#include "strata/kernels/iq_kernels.hpp"
#include "strata/kernels/dp4a.hpp"
#if defined(__HIPCC__)
#include "strata/kernels/gfx_arch.hpp"
#endif

#include <cuda_fp16.h>
#include <cuda_runtime.h>
Expand Down
387 changes: 367 additions & 20 deletions engine/src/kernels/cuda/i8_mma.cuh

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion engine/src/kernels/cuda/iq_kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -8461,7 +8461,7 @@ bool gemm_iq_f16_grouped(int ty, const void* W, size_t expert_bytes, int n_out,
}

} // namespace strata::kernels
#if defined(__HIPCC__)
#if defined(STRATA_HIP_GFX906) // (i8_mma.cu is empty there; the other HIP builds have it on RDNA3 / RDNA4)
namespace strata::kernels {
bool gemm_iq_i8_takes(int, int) { return false; }
void gemm_iq_i8_turing(bool) {}
Expand Down
6 changes: 3 additions & 3 deletions engine/src/poly/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +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, off; 1: --prompt-int8; 0: --no-prompt-int8,
///< Turing's halves and cuBLAS
int prompt_int8 = -1; ///< Inkling 2: its prompt products on the int8 tensor cores, as on Ampere, on Turing and
///< AMD's RDNA3 / RDNA4 (gemm_iq_i8_turing()); -1: the default (Turing off, AMD on);
///< 1: --prompt-int8; 0: --no-prompt-int8, the halves and BLAS
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 router's own
///< (--cache-bias, qwen35moe and inkling2_loop, route_cached()); 0: the router's own
Expand Down
9 changes: 6 additions & 3 deletions engine/src/poly/inkling2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,12 @@ void Inkling2::load_geometry() {
#if !defined(STRATA_GPU_METAL)
// Turing's prompt products on the int8 kernel that Ampere and newer take by default (i8_mma.cuh), only with
// --prompt-int8: on Turing it gave the same most likely token as the halves at 560 of 576 steps (97.2%), under
// the 97.8% a default change needs, so the halves and cuBLAS stay Turing's default (STRATA_PF_I8MMA=0 turns the
// kernel off on every card)
kernels::gemm_iq_i8_turing(opt_.prompt_int8 == 1);
// the 97.8% a default change needs, so the halves and cuBLAS stay Turing's default. On AMD's RDNA3 / RDNA4 the
// kernel is the default and --no-prompt-int8 turns it off. STRATA_PF_I8MMA=0 turns the kernel off on every card.
#if defined(STRATA_USE_HIP)
if (opt_.prompt_int8 >= 0) // on AMD the int8 kernel is the default: only --no-prompt-int8 (or --prompt-int8) says
#endif
kernels::gemm_iq_i8_turing(opt_.prompt_int8 == 1);
// STRATA_PREFILL_IQ1S=int8: IQ1_S (UD-Q2_K_XL's gate|up in 6 layers) on the int8 prompt kernel, as the file's
// other types are on Ampere and newer (i8_mma.cuh: activations int8 a 32 values). Opt-in: on a 302-token prompt
// it cost more KL a matrix than those types do. Unset or =fp16: the FP16 kernel, as before. Other models never
Expand Down
8 changes: 5 additions & 3 deletions engine/src/poly/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1759,9 +1759,11 @@ void usage() {
" --prompt-int8 Inkling 2 on an RTX 20 (Turing): its prompt products on the int8 tensor\n"
" cores as on RTX 30 and newer, not as halves and cuBLAS (the default on\n"
" Turing): faster prompts, and the answers move more, activations int8 a\n"
" 32 values; --no-prompt-int8 says the default; STRATA_PF_I8MMA=0 turns the\n"
" int8 kernel off on every card; with it, a prompt batch's experts in VRAM\n"
" go in one launch a class of rows, the same numbers in fewer launches\n"
" 32 values; --no-prompt-int8 says the default (on an AMD RDNA3 / RDNA4\n"
" card the int8 kernel is the default and --no-prompt-int8 turns it off);\n"
" STRATA_PF_I8MMA=0 turns the int8 kernel off on every card; on\n"
" Turing with it, a prompt batch's experts in VRAM go in one launch a class\n"
" of rows, the same numbers in fewer launches\n"
" (STRATA_POLY_EXPERT_TILES=0: a launch a run of slots again)\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"
Expand Down
27 changes: 26 additions & 1 deletion engine/src/poly/mmq_own.cu
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
// block-scaled FP4 tensor cores instead, the activations two FP4 terms under a float a row (nvfp4_x2.hpp, about 8
// bits against a half's 11), its products straight to dst's rows and its rows read by index as the int8 ones are.
// NVFP4 takes the int8 kernel only with STRATA_PREFILL_NVFP4=w4a8 (nvfp4_w4a8()), on any card that has it.
//
// HIP on RDNA3 / RDNA4: the int8 kernel too, NVFP4 included, as on Ampere (--no-prompt-int8 and STRATA_PF_I8MMA=0 turn it off).
#include "mmq.hpp"
#if STRATA_POLY_X2
#include "nvfp4_x2.hpp"
Expand Down Expand Up @@ -284,9 +286,32 @@ bool nvfp4_w4a8() {
return asked;
}

#ifdef STRATA_MMQ_OWN_HIP
// The int8 kernel on RDNA3's and RDNA4's matrix cores (i8_mma.cuh's i8m_wmma), for the types and rows it takes, in
// place of the FP16 tiles and the per-expert dequantize + rocBLAS loop. On by default; --no-prompt-int8 (Inkling 2) or
// STRATA_PF_I8MMA=0 (every model) turns it off: the FP16 tiles and the BLAS loop, 0.2.20-poly.4's path. The
// activations are int8 a 32 values there where the halves keep 11 bits, as on CUDA's Ampere and newer by default (the
// same kernel's numbers, bit for bit), and as llama.cpp's MMQ, which the HIP build used up to 0.1.45. NVFP4 goes with
// it unless STRATA_PREFILL_NVFP4 names a mode (w4a8: the same; any other: the halves).
// Issue #289, RX 7800 XT, Qwen3.6-35B-A3B UD-Q3_K_M, prompts of 4,096 tokens: 546 tok/s off, 2,340 on (0.1.45's MMQ
// build: 2,104); keeping the FP16 tiles for the matrices they take (its Q8_0 ones) and int8 for the rest: 1,313.
bool nvfp4_default() {
static const bool unset = [] {
const char* v = std::getenv("STRATA_PREFILL_NVFP4");
return !v || !*v;
}();
return unset;
}
#endif

// the int8 kernel takes a matrix of this type with rows this long, whatever their number
bool int8_rows(int t, int64_t cols) {
return !blas_only() && cols <= 0x7fffffff && (t != 40 || nvfp4_w4a8()) && kernels::gemm_iq_i8_takes(t, (int) cols);
#ifdef STRATA_MMQ_OWN_HIP
const bool nv4 = nvfp4_w4a8() || nvfp4_default();
#else
const bool nv4 = nvfp4_w4a8();
#endif
return !blas_only() && cols <= 0x7fffffff && (t != 40 || nv4) && kernels::gemm_iq_i8_takes(t, (int) cols);
}

// the rows' halves, then a float a row (back)
Expand Down
Loading
Loading