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
3 changes: 2 additions & 1 deletion docs/models/inkling.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ 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
bits with a scale for every 32 (`STRATA_PF_I8MMA=0` turns the int8 products off on every card). `--prompt-int8` takes the
same kernel on an AMD RDNA3 or RDNA4 card (HIP; `--no-prompt-int8` says the default there too). It is not the
default 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
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; with gemm_iq_i8_turing() too). 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, and on HIP's RDNA3 / RDNA4, from now on in this process: the same
/// sums as on Ampere. Off by default there, where the products are the halves' (mmq_own.cu); a model's engine turns it
/// on (Inkling 2 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
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
383 changes: 363 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: on Turing and AMD's RDNA3 / RDNA4 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, 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
4 changes: 2 additions & 2 deletions engine/src/poly/inkling2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ void Inkling2::load_geometry() {
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), only with
// --prompt-int8: on Turing it gave the same most likely token as the halves at 560 of 576 steps (97.2%), under
// Turing's and RDNA3 / RDNA4'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);
Expand Down
13 changes: 7 additions & 6 deletions engine/src/poly/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1756,12 +1756,13 @@ void usage() {
" says the default (the last --attn-requant counts)\n"
" --loop-bf16 Inkling 2: its loop controllers' matrices in BF16, not F32;\n"
" --no-loop-bf16 says the default (F32; the last one counts)\n"
" --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"
" --prompt-int8 Inkling 2 on an RTX 20 (Turing) or an AMD RDNA3 / RDNA4 card: its prompt\n"
" products on the int8 tensor cores as on RTX 30 and newer, not as halves\n"
" and cuBLAS / BLAS (the default there): faster prompts, and the answers\n"
" move more, activations int8 a 32 values; --no-prompt-int8 says the\n"
" default; 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
24 changes: 23 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, with --prompt-int8 (gemm_iq_i8_turing()): the int8 kernel on RDNA3 / RDNA4 too, NVFP4 included, as on Ampere.
#include "mmq.hpp"
#if STRATA_POLY_X2
#include "nvfp4_x2.hpp"
Expand Down Expand Up @@ -284,9 +286,29 @@ 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 or the BLAS loop: opt-in with --prompt-int8 (Inkling 2), as on Turing; STRATA_PF_I8MMA=0 turns
// it off for every model. The activations are int8 a 32 values where the halves keep 11 bits, as on CUDA's Ampere and
// newer by default (the same kernel's numbers, bit for bit). NVFP4 goes with it unless STRATA_PREFILL_NVFP4 names a
// mode (w4a8: the same; any other: the halves).
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