From 1823ccd5e9b5e77ebfaf20a37c8329e76a50f725 Mon Sep 17 00:00:00 2001 From: mici81 Date: Sun, 18 Oct 2026 14:13:45 +0000 Subject: [PATCH] --kv-cache-dtype fp8_v4 | k6_v4 (RadBuildCtx::kv_quant, RadArchProbe::kv_quant_ok; ABI 23) The flag sets the trunk's cache to E4M3 plus a RAD_KVQ_* layout; a plugin offers the layouts it serves in its probe and the server refuses any other, naming the plugin. qwen35_fp8 offers both at head_dim 256, gqa 6, and declares the group at head_dim 200 / 176 (the row's bytes / 2), so the core sizes pages as for fp8; a drafter's cache stays fp8. Qwen3.8-27B on two R9700s, KL against bf16 on 17 long documents: fp8 0.039963 (518,800 tokens of KV), fp8_v4 0.040076 (664,400), k6_v4 0.039965 (755,184); prefill and decode at fp8's speed. ABI 23: two trailing fields, the layout table regenerated. Co-Authored-By: Claude Opus 5.5 --- abi/rad_abi.h | 2 +- abi/rad_builder.h | 38 ++++++++++++++- arch/qwen35_fp8/qwen35_fp8.cpp | 34 +++++++++++++- core/config.cpp | 9 ++-- core/engine_bringup.cpp | 17 ++++++- core/rad_core.h | 9 ++-- docs/ARCHITECTURES.md | 2 +- docs/GUIDE.md | 19 +++++++- docs/OPS.md | 19 ++++++++ tests/abi_layout_rows.h | 2 + tests/abi_layout_table.h | 6 ++- tests/abi_layout_test.cpp | 6 +-- tests/arch_test.cpp | 86 ++++++++++++++++++++++++++++++++++ tests/config_test.cpp | 9 +++- 14 files changed, 238 insertions(+), 20 deletions(-) diff --git a/abi/rad_abi.h b/abi/rad_abi.h index 834efdd1..ad302026 100644 --- a/abi/rad_abi.h +++ b/abi/rad_abi.h @@ -17,7 +17,7 @@ extern "C" { * kind of plugin reports it, and a plugin that does not report exactly this version is refused at * load with both versions printed. A new optional export (e.g. rad_plugin_reference) is not a * change of shape: a plugin without it gets the default. */ -#define RAD_ABI_VERSION 22u +#define RAD_ABI_VERSION 23u /* ================================================================== plugin identity */ /* One value per plugin, not flags (QUANT is 3). RAD_PLUGIN_QUANT is a quantiser plugin: diff --git a/abi/rad_builder.h b/abi/rad_builder.h index d9b667af..70993b69 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -56,6 +56,29 @@ double rad_meta_getf(const RadModelMeta* m, const char* key, double dflt); const char* rad_meta_gets(const RadModelMeta* m, const char* key, const char* dflt); /* ================================================================== build context */ +/* RadBuildCtx::kv_quant. A layout groups each head's dims by 32 with an f16 scale per group, the + * group's absmax over the largest code. A token's row in a head, at head_dim HD (a multiple of 32): + * + * RAD_KVQ_V4 (fp8_v4), 25 * HD / 16 bytes: + * [0, HD) K, E4M3 as the fp8 cache stores it + * [HD, 3HD/2) V's int4 codes, dim d in the nibble at bit 4 * (d % 2) of byte d / 2 + * [3HD/2, 25HD/16) V's scales (absmax / 7) + * RAD_KVQ_K6V4 (k6_v4), 22 * HD / 16 bytes: + * [0, HD/2) K's low four bits, as V's codes above + * [HD/2, 3HD/4) K's high two bits, a 4-byte word per 16 dims whose byte j holds dims 2j + * and 2j+1 in bits 0-3 and dims 8+2j and 9+2j in bits 4-7 + * [3HD/4, 5HD/4) V's int4 codes + * [5HD/4, 21HD/16) K's scales (absmax / 31) + * [21HD/16, 22HD/16) V's scales (absmax / 7) + * + * Codes are two's complement (int4 -7..7, int6 -31..31). The group is declared at RAD_F8E4M3 with + * head_dim the row's bytes / 2 so the core sizes pages from it; only the layout's kernels read it. */ +enum { + RAD_KVQ_NONE = 0, + RAD_KVQ_V4 = 1, + RAD_KVQ_K6V4 = 2 +}; + typedef struct RadBuildCtx { int rank; /* declare runs once per rank; the plugin declares dimensions */ int world_size; /* already divided -- the core inserts no collectives (spec §9) */ @@ -86,7 +109,7 @@ typedef struct RadBuildCtx { * RAD_DT_INVALID (the zero-initialised default) means the plugin's default, bf16 for every * architecture in this tree. RAD_F8E4M3 is the only other implemented value: the fp8kv * attention family is band-gated on it, and the store side writes E4M3 with per-(sequence, - * head) descales. + * head) descales. `kv_quant` below narrows the trunk's attention cache further. * * An ABI field rather than an environment variable because it changes what the model * computes, so it must have one visible answer: --kv-cache-dtype. */ @@ -140,6 +163,15 @@ typedef struct RadBuildCtx { * * Trailing and zero-defaulted, so a plugin built against the older struct is unchanged. */ int64_t max_mixed_tok; + + /* A narrower layout of the trunk's attention cache under kv_dtype RAD_F8E4M3, from + * --kv-cache-dtype fp8_v4 or k6_v4: a RAD_KVQ_* (below). The engine sets it only for a plugin + * whose probe has the layout's bit in RadArchProbe::kv_quant_ok and refuses the flag + * otherwise, so no plugin sees it that would store plain E4M3 instead. A drafter's cache stays + * E4M3. + * + * Trailing and zero-defaulted, so a plugin built against the older struct is unchanged. */ + int kv_quant; } RadBuildCtx; /* ================================================================== weights */ @@ -993,6 +1025,10 @@ typedef struct RadArchProbe { * `gen_rows` (its largest pass: a step's packed sequence at the largest size and length). It * keeps no KV. */ int32_t gen_video; + /* The RadBuildCtx::kv_quant layouts the plugin serves for this model, bit 1 << RAD_KVQ_*: + * libr4d has their rows for its attention geometry. A layout whose bit is clear is refused at + * startup with the plugin named. */ + int32_t kv_quant_ok; } RadArchProbe; int rad_arch_probe(const RadModelMeta* meta, RadArchProbe* out); diff --git a/arch/qwen35_fp8/qwen35_fp8.cpp b/arch/qwen35_fp8/qwen35_fp8.cpp index c1b80735..915e7238 100644 --- a/arch/qwen35_fp8/qwen35_fp8.cpp +++ b/arch/qwen35_fp8/qwen35_fp8.cpp @@ -554,9 +554,32 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c fd.dtype = rad_kv_cache_dtype(ctx, RAD_BF16); fd.n_head_kv = g.n_head_kv; fd.head_dim = g.head_dim; + /* --kv-cache-dtype fp8_v4 / k6_v4 (RadBuildCtx::kv_quant): V as int4 in groups of 32 with f16 + * scales, K E4M3 as above or int6 the same way (libr4d's fp8_v4 / k6_v4 rows). A token's row + * is 25 or 22 * head_dim / 16 bytes instead of 2 * head_dim, so the group is declared at half + * that and the core sizes pages from it; only those kernels read the layout. The drafter's + * group stays E4M3 (declare_dflash2). The engine asks only for a layout probe() offered. */ + const int kvq = fd.dtype == RAD_F8E4M3 ? ctx->kv_quant : RAD_KVQ_NONE; + const char* kvq_name = kvq == RAD_KVQ_V4 ? "fp8_v4" : kvq == RAD_KVQ_K6V4 ? "k6_v4" : nullptr; + if (kvq != RAD_KVQ_NONE && !kvq_name) { + fprintf(stderr, "radiance: KV cache layout %d is not one this plugin knows\n", kvq); + return RAD_E_UNSUPPORTED; + } + if (kvq_name && !(g.head_dim == 256 && g.n_head == 6 * g.n_head_kv)) { + fprintf(stderr, "radiance: --kv-cache-dtype %s has libr4d rows at head_dim 256, gqa 6 " + "only; this model is head_dim %lld, gqa %lld\n", kvq_name, + (long long)g.head_dim, (long long)(g.n_head_kv ? g.n_head / g.n_head_kv : 0)); + return RAD_E_UNSUPPORTED; + } + const int64_t row16 = kvq == RAD_KVQ_V4 ? 25 : kvq == RAD_KVQ_K6V4 ? 22 : 32; + fd.head_dim = g.head_dim * row16 / 32; m.kv_full = rad_decl_kv_group(b, nm.f("kv_attn"), &fd); /* The op parameter must use the spelling the rows band on, which is rad_dtype_name's. */ - g.kv_dtype = rad_dtype_name(fd.dtype); + g.kv_dtype = kvq_name ? kvq_name : rad_dtype_name(fd.dtype); + if (kvq_name) + rad_note(b, "kv cache %s: %s K, int4 V, in groups of 32 with f16 scales, %lld bytes a token " + "and head instead of %lld", kvq_name, kvq == RAD_KVQ_V4 ? "E4M3" : "int6", + (long long)(g.head_dim * row16 / 16), (long long)(2 * g.head_dim)); /* The SSM state cache is f32. * @@ -1175,6 +1198,15 @@ static void step(RadCtx* c, const RadBatch* batch) { * 0 when the container carries no drafter. */ static int probe(const RadModelMeta* meta, RadArchProbe* out) { if (!meta || !out) return RAD_E_INVAL; + /* --kv-cache-dtype fp8_v4 / k6_v4: libr4d's rows for them are the h256/gqa6 attention + * family's (Qwen3.8-27B), so other geometries are refused at startup rather than in declare. */ + { + const long long hd = meta->head_dim ? meta->head_dim + : (meta->n_head ? meta->n_embd / meta->n_head : 0); + const long long nkv = meta->n_head_kv ? meta->n_head_kv : meta->n_head; + if (hd == 256 && nkv > 0 && meta->n_head == 6 * nkv) + out->kv_quant_ok = (1 << RAD_KVQ_V4) | (1 << RAD_KVQ_K6V4); + } const char* da = rad_meta_gets(meta, "draft.architectures", ""); if (da && std::strstr(da, "DFlash2")) { const long long bs = rad_meta_geti(meta, "draft.dflash_config.block_size", 8); diff --git a/core/config.cpp b/core/config.cpp index 871f9820..c3eeb23e 100644 --- a/core/config.cpp +++ b/core/config.cpp @@ -71,7 +71,9 @@ static const Flag g_flags[] = { "keys, no reconvert (docs/YARN.md); none: off" }, { "--gpu-headroom-mib", "N", "VRAM left unclaimed on every card (default 96)" }, - { "--kv-cache-dtype", "T", "bf16 | fp8 -- fp8 halves what a token of context costs" }, + { "--kv-cache-dtype", "T", "bf16 | fp8 | fp8_v4 | k6_v4 -- fp8 halves what a token of\n" + "context costs; fp8_v4 keeps V in 4 bits (0.78 of fp8), k6_v4\n" + "also K in 6 (0.69); both Qwen3.8-27B" }, { "--host-pool-mib", "N", "pinned host tier" }, { "--host-headroom-mib", "N", "host RAM to leave free for the system when radiance\n" " pins its host memory at startup (default 4096)" }, @@ -466,8 +468,9 @@ int config_parse(int argc, char** argv, Config* c) { else if (S("--host-headroom-mib")) { if (!num(0, INT64_MAX, &c->host_headroom_mib)) return RAD_E_INVAL; } else if (S("--kv-cache-dtype")) { if (!(v = need(i))) return RAD_E_INVAL; - if (std::strcmp(v, "bf16") && std::strcmp(v, "fp8")) { - RAD_ERR("--kv-cache-dtype takes bf16 or fp8, not '%s'", v); + if (std::strcmp(v, "bf16") && std::strcmp(v, "fp8") && std::strcmp(v, "fp8_v4") && + std::strcmp(v, "k6_v4")) { + RAD_ERR("--kv-cache-dtype takes bf16, fp8, fp8_v4 or k6_v4, not '%s'", v); return RAD_E_INVAL; } c->kv_cache_dtype = v; diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index 934dca63..58046b1e 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -881,7 +881,9 @@ RadBuildCtx Engine::build_ctx(int rank, int64_t max_tok) const { bc.tp_wire_lossy = cfg_.tp_wire_exact ? 0 : 1; /* The engine always states the KV dtype. RAD_DT_INVALID (zero) remains valid in the ABI for * other callers and means the plugin's default. */ - bc.kv_dtype = cfg_.kv_cache_dtype == "fp8" ? RAD_F8E4M3 : RAD_BF16; + bc.kv_dtype = cfg_.kv_cache_dtype == "bf16" ? RAD_BF16 : RAD_F8E4M3; + bc.kv_quant = cfg_.kv_cache_dtype == "fp8_v4" ? RAD_KVQ_V4 + : cfg_.kv_cache_dtype == "k6_v4" ? RAD_KVQ_K6V4 : RAD_KVQ_NONE; bc.tp_wire_min_bytes = cfg_.tp_wire_min_kb * 1024; bc.scope = ""; bc.max_enc_patches = cfg_.mm_max_patches > 0 ? cfg_.mm_max_patches : 0; @@ -987,6 +989,19 @@ int Engine::declare() { cfg_.prefix_cache = false; } } + /* fp8_v4 and k6_v4 are layouts only the plugin and libr4d's rows for them know. A plugin that + * does not say it serves one (RadArchProbe::kv_quant_ok) would store plain E4M3 under a name + * that promised a smaller cache, so the flag is refused rather than ignored. */ + if (cfg_.kv_cache_dtype == "fp8_v4" || cfg_.kv_cache_dtype == "k6_v4") { + const int q = cfg_.kv_cache_dtype == "fp8_v4" ? RAD_KVQ_V4 : RAD_KVQ_K6V4; + RadArchProbe pb{}; + (void)rad_tools_probe(plugins_, meta_, &pb); + if (!(pb.kv_quant_ok & (1 << q))) { + RAD_ERR("--kv-cache-dtype %s: the '%s' plugin does not serve it for this model " + "(fp8 does)", cfg_.kv_cache_dtype.c_str(), plugins_.arch_plugin.c_str()); + return RAD_E_UNSUPPORTED; + } + } /* Resolved here, not at argument parsing: the plugin states its drafter's depth, and it is * selected only once the container is open. Every consumer of `n_spec` (buffer sizing, kernel * bands, the scheduler's window) runs after this. rad_arch_probe is the core's only source of diff --git a/core/rad_core.h b/core/rad_core.h index 3e625702..a0dab54a 100644 --- a/core/rad_core.h +++ b/core/rad_core.h @@ -737,10 +737,11 @@ struct Config { int64_t vram_weights_mib = 0; int64_t vram_kv_mib = 0; int64_t gpu_headroom_mib = 96; /* --gpu-headroom-mib, left unclaimed on every card */ - /* --kv-cache-dtype bf16|fp8. fp8 is an E4M3 cache with per-(sequence, head) descales; it - * halves the per-token cost, most of the KV pool at production context. It changes the - * model's output slightly, so it is explicit and bf16 is the default. No "auto": it would - * always resolve to bf16 and only add a third name. */ + /* --kv-cache-dtype bf16|fp8|fp8_v4|k6_v4. fp8 is an E4M3 cache with per-(sequence, head) + * descales; it halves the per-token cost, most of the KV pool at production context. fp8_v4 + * keeps K as E4M3 and V as int4 in groups of 32, k6_v4 also K as int6 (RadBuildCtx::kv_quant), + * served only where the plugin says so. Each changes the model's output slightly, so it is explicit and bf16 is the + * default. No "auto": it would always resolve to bf16 and only add another name. */ std::string kv_cache_dtype = "bf16"; int64_t host_pool_mib = 0; int64_t host_headroom_mib = 4096; /* --host-headroom-mib: RAM left free for the system at startup */ diff --git a/docs/ARCHITECTURES.md b/docs/ARCHITECTURES.md index 415f04a0..8a946764 100644 --- a/docs/ARCHITECTURES.md +++ b/docs/ARCHITECTURES.md @@ -194,7 +194,7 @@ Three groups, which is why hybrid KV groups are first-class: | group | kind | geometry | |---|---|---| -| full attention | `RAD_KV_FULL` | 16 layers, head_dim 256, 4 KV heads; block size from the resolved kernel | +| full attention | `RAD_KV_FULL` | 16 layers, head_dim 256, 4 KV heads; block size from the resolved kernel. `--kv-cache-dtype fp8_v4` / `k6_v4` (`qwen35_fp8` only) declare it at head_dim 200 / 176, the bytes of a token's quantised row over two (docs/OPS.md) | | GDN recurrent state | `RAD_KV_LINEAR` | 48 layers, 48 states of 128 x 128 | | GDN conv state | `RAD_KV_CONV` | 48 layers, 2*16 + 48 = 80 heads x 128 = 10240 channels, a rolling `conv_width - 1 + n_spec` window | diff --git a/docs/GUIDE.md b/docs/GUIDE.md index 5aec839e..56967b33 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -783,6 +783,23 @@ widths, and the flag sets every attention cache of the deployment, a drafter's ( included. The delta-net state and the QSA indexer's key tail are recurrent state and keep their own widths. +`fp8_v4` and `k6_v4` narrow the trunk's attention cache further, in groups of 32 dimensions with an +f16 scale each: `fp8_v4` keeps K in E4M3 and stores V as int4, `k6_v4` stores K as int6 too. A token +then costs 25/32 or 22/32 of what fp8 costs, and a drafter's cache stays fp8. libr4d has their +kernels for Qwen3.8-27B's attention (head_dim 256, gqa 6), so the plugin offers them there and the +server refuses them, naming the plugin, anywhere else. On two R9700s at the serve flags of +`qwen3.8-27b-mxfp4.yaml` with `--max-num-batched-tokens 2048` and 12 GiB a card left to another +engine, against the bf16 model on 17 long log-analysis documents (60 368 scored positions): + +| `--kv-cache-dtype` | KV pool | mean KL | top-1 | 41k-token prefill | decode, 1 / 16 streams | +|---|---|---|---|---|---| +| `fp8` | 518 800 tokens | 0.039963 | 94.03% | 9.76 s | 134-140 / 550-555 tok/s | +| `fp8_v4` | 664 400 (+28%) | 0.040076 | 94.03% | 9.76 s | 130-139 / 553-557 | +| `k6_v4` | 755 184 (+46%) | 0.039965 | 94.06% | 9.84 s | 136-138 / 553-561 | + +The decode spread is run to run; the three are the same speed there. Each layout's row and the +kernels that read it are in [OPS.md](OPS.md#the-kv-cache-operand). + **Many sequences.** Every model here serves `--max-num-seqs` up to 32. A sequence holds only the blocks its tokens occupy, but two costs exist per sequence regardless: @@ -1322,7 +1339,7 @@ still verified by the adapter, so output is exact and only the acceptance rate c | flag | default | | |---|---|---| | `--gpu-headroom-mib N` | 96 | VRAM left unclaimed on every card | -| `--kv-cache-dtype T` | bf16 | `bf16` or `fp8` | +| `--kv-cache-dtype T` | bf16 | `bf16`, `fp8`, or on Qwen3.8-27B `fp8_v4` or `k6_v4` (§4.4) | | `--host-pool-mib N` | 0 | pinned host tier (weights) | | `--host-headroom-mib N` | 4096 | host RAM left free for the system when startup pins host memory — kept for the unpooled allocations, staging and page cache (§4.3) | | `--embedding-placement WHERE` | ram | `ram` or `vram`: the token embedding pinned in host memory beside `--host-pool-mib` and read in place, or on the card (§4.3) | diff --git a/docs/OPS.md b/docs/OPS.md index 3592b153..77c6c0ad 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -812,6 +812,25 @@ declares bound it lower. `RadKVGroupBatch` carries the slot mapping, the block table and the sequence lengths, but the pool base belongs to the block manager, not the batch. +**`kv_dtype` `fp8_v4` and `k6_v4`** (device-only) are the quantised layouts of +`--kv-cache-dtype` (`RadBuildCtx::kv_quant`). A head's dims are grouped by 32, each group with an +f16 scale (its absmax over the largest code), codes two's complement. A token's row in a head, at +head_dim HD: + +| bytes | `fp8_v4` (25 HD / 16) | `k6_v4` (22 HD / 16) | +|---|---|---| +| K | [0, HD): E4M3, as `fp8_e4m3` | [0, HD/2): the codes' low four bits; [HD/2, 3HD/4): their high two bits, a word per 16 dims whose byte j holds dims 2j, 2j+1 in bits 0-3 and 8+2j, 9+2j in bits 4-7 | +| V | [HD, 3HD/2): int4 codes, dim d in the nibble at bit 4 (d mod 2) of byte d/2 | [3HD/4, 5HD/4): the same | +| scales | [3HD/2, 25HD/16): V's (absmax / 7) | [5HD/4, 21HD/16): K's (absmax / 31); [21HD/16, 22HD/16): V's | + +The group is declared at `RAD_F8E4M3` with head_dim the row's bytes / 2, so the core sizes pages +as it does for fp8, and the cache operand's last extent is the row. The ops take the model's +head_dim. libr4d's rows are `kv_store_fp8v4` / `kv_store_k6v4`, +`qk_norm_rope_gate_kv_store_fp8v4` / `_k6v4` (byte-identical to the prologue and the store, as at +the other widths), and `attn_prefill_h256_gqa6_{fp8v4,k6v4}` and `attn_decode_h256_gqa6_{fp8v4,k6v4}`, +which widen the codes to f16 in a tile's staging. `r4d_selftest` holds the stores to an exactly +representable set and the attention rows to its inline oracle. + ### `qk_norm_rope_gate` (device-only) | op | params | operands | diff --git a/tests/abi_layout_rows.h b/tests/abi_layout_rows.h index 26fac72c..22691472 100644 --- a/tests/abi_layout_rows.h +++ b/tests/abi_layout_rows.h @@ -16,6 +16,7 @@ ABI_FIELD(RadArchProbe, gen_patches) ABI_FIELD(RadArchProbe, gen_audio) ABI_FIELD(RadArchProbe, gen_ctx) ABI_FIELD(RadArchProbe, gen_video) +ABI_FIELD(RadArchProbe, kv_quant_ok) ABI_STRUCT(RadArgs) ABI_FIELD(RadArgs, t) ABI_FIELD(RadArgs, n_t) @@ -99,6 +100,7 @@ ABI_FIELD(RadBuildCtx, max_enc_patches) ABI_FIELD(RadBuildCtx, max_out_rows) ABI_FIELD(RadBuildCtx, max_audio_frames) ABI_FIELD(RadBuildCtx, max_mixed_tok) +ABI_FIELD(RadBuildCtx, kv_quant) ABI_STRUCT(RadAudioGenBatch) ABI_FIELD(RadAudioGenBatch, n_lane) ABI_FIELD(RadAudioGenBatch, lane) diff --git a/tests/abi_layout_table.h b/tests/abi_layout_table.h index 1217e181..67222040 100644 --- a/tests/abi_layout_table.h +++ b/tests/abi_layout_table.h @@ -1,4 +1,4 @@ -/* abi_layout_table.h -- every abi/ struct's size and field offsets at RAD_ABI_VERSION 21 and +/* abi_layout_table.h -- every abi/ struct's size and field offsets at RAD_ABI_VERSION 23 and * RAD_FORMAT_VER 2, as { type, field, offset, size } rows ("" is the struct itself). A snapshot * of the headers on x86-64 Linux, which abi_layout_test.cpp holds the layout to; regenerate it * from the test's printout only together with a version bump. */ @@ -13,6 +13,7 @@ { "RadArchProbe", "gen_audio", 48, 4 }, { "RadArchProbe", "gen_ctx", 56, 8 }, { "RadArchProbe", "gen_video", 64, 4 }, +{ "RadArchProbe", "kv_quant_ok", 68, 4 }, { "RadArgs", "", 0, 64 }, { "RadArgs", "t", 0, 8 }, { "RadArgs", "n_t", 8, 4 }, @@ -79,7 +80,7 @@ { "RadBufDecl", "domain", 60, 4 }, { "RadBufDecl", "derive", 64, 8 }, { "RadBufDecl", "kv", 72, 4 }, -{ "RadBuildCtx", "", 0, 104 }, +{ "RadBuildCtx", "", 0, 112 }, { "RadBuildCtx", "rank", 0, 4 }, { "RadBuildCtx", "world_size", 4, 4 }, { "RadBuildCtx", "max_tok", 8, 8 }, @@ -96,6 +97,7 @@ { "RadBuildCtx", "max_out_rows", 80, 8 }, { "RadBuildCtx", "max_audio_frames", 88, 8 }, { "RadBuildCtx", "max_mixed_tok", 96, 8 }, +{ "RadBuildCtx", "kv_quant", 104, 4 }, { "RadAudioGenBatch", "", 0, 264 }, { "RadAudioGenBatch", "n_lane", 0, 8 }, { "RadAudioGenBatch", "lane", 8, 8 }, diff --git a/tests/abi_layout_test.cpp b/tests/abi_layout_test.cpp index eae119e5..bc804bc0 100644 --- a/tests/abi_layout_test.cpp +++ b/tests/abi_layout_test.cpp @@ -8,7 +8,7 @@ * * Checked: a C translation unit (abi_c_test.c, strict C11) lays every struct out as C++ does; every * struct's listed fields account for its size, so the list in abi_layout_rows.h is complete; and - * the layout agrees with the table below, taken at RAD_ABI_VERSION 22 and RAD_FORMAT_VER 2. Under + * the layout agrees with the table below, taken at RAD_ABI_VERSION 23 and RAD_FORMAT_VER 2. Under * those versions a listed field may not move or change size, an ABI struct may only grow by fields * past its old end, and an on-disk struct may not change at all. The table is a snapshot of the * headers: nothing else states the layout. When it disagrees the test prints the live table. @@ -57,8 +57,8 @@ std::vector cpp_rows() { }; } -/* The layout at RAD_ABI_VERSION 22 / RAD_FORMAT_VER 2 (see the file header). */ -constexpr uint32_t kTableAbi = 22, kTableFormat = 2; +/* The layout at RAD_ABI_VERSION 23 / RAD_FORMAT_VER 2 (see the file header). */ +constexpr uint32_t kTableAbi = 23, kTableFormat = 2; const AbiRow kTable[] = { #include "abi_layout_table.h" }; diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index b4e70477..f858d8ba 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -6081,6 +6081,92 @@ TEST(gemma3_dflash2_drafter_taps_and_block) { CHECK_EQ(q.draft_depth, 0); } +/* --kv-cache-dtype fp8_v4: the trunk's attention cache is E4M3 K (256 bytes), int4 V (128) and + * eight f16 scales, 400 bytes a token and head, declared as 25 * 256 / 32 = 200 E4M3 columns so the + * core sizes its pages; every trunk op on it bands on kv_dtype fp8_v4. The DFlash2 drafter's own + * group stays E4M3 at its head_dim. Without kv_quant the same build is plain fp8. */ +TEST(qwen35_fp8_fp8_v4_cache_is_400_bytes_a_token_and_head) { + MetaKV mk = with_dflash2(q35fp8_meta()); + auto count = [](const RadBuilder& b, const char* kd) { + int n = 0; + for (const auto& o : b.ops) if (pstr(&o, "kv_dtype") == kd) ++n; + return n; + }; + RadBuilder f8; + RadBuildCtx c8 = spec_ctx(0, 1, 7); + c8.kv_dtype = RAD_F8E4M3; + REQUIRE_EQ(qwen35_fp8::declare(&f8, mk.meta(), &c8), RAD_OK); + CHECK_EQ(f8.kv_groups[0].d.head_dim, 256); + CHECK_EQ(count(f8, "fp8_v4"), 0); + const int n8 = count(f8, "fp8_e4m3"); + + RadBuilder b; + RadBuildCtx ctx = c8; + ctx.kv_quant = RAD_KVQ_V4; + REQUIRE_EQ(qwen35_fp8::declare(&b, mk.meta(), &ctx), RAD_OK); + CHECK_EQ(b.kv_groups[0].name, std::string("kv_attn")); + const RadKVGroupDecl& f = b.kv_groups[0].d; + CHECK_EQ(f.kind, RAD_KV_FULL); + CHECK_EQ(f.dtype, (uint32_t)RAD_F8E4M3); + CHECK_EQ(f.n_head_kv, 4); + CHECK_EQ(f.head_dim, 200); + const RadKVGroupDecl& d = b.kv_groups.back().d; + CHECK_EQ(d.dtype, (uint32_t)RAD_F8E4M3); + CHECK_EQ(d.head_dim, 128); + const int v4 = count(b, "fp8_v4"), e4 = count(b, "fp8_e4m3"); + CHECK(v4 > 0); + CHECK(e4 > 0); /* the drafter's attention */ + CHECK_EQ(v4 + e4, n8); + CHECK(noted(b, "kv cache fp8_v4")); + + RadArchProbe p{}; + CHECK_OK(qwen35_fp8::probe(mk.meta(), &p)); + CHECK_EQ(p.kv_quant_ok, (1 << RAD_KVQ_V4) | (1 << RAD_KVQ_K6V4)); +} + +/* --kv-cache-dtype k6_v4: int6 K and int4 V, each in groups of 32 with an f16 scale, 352 bytes a + * token and head, declared as 176 E4M3 columns; the trunk's ops band on kv_dtype k6_v4 and the + * drafter's group stays E4M3. */ +TEST(qwen35_fp8_k6_v4_cache_is_352_bytes_a_token_and_head) { + MetaKV mk = with_dflash2(q35fp8_meta()); + RadBuilder b; + RadBuildCtx ctx = spec_ctx(0, 1, 7); + ctx.kv_dtype = RAD_F8E4M3; + ctx.kv_quant = RAD_KVQ_K6V4; + REQUIRE_EQ(qwen35_fp8::declare(&b, mk.meta(), &ctx), RAD_OK); + const RadKVGroupDecl& f = b.kv_groups[0].d; + CHECK_EQ(f.dtype, (uint32_t)RAD_F8E4M3); + CHECK_EQ(f.head_dim, 176); + CHECK_EQ(b.kv_groups.back().d.head_dim, 128); + int k6 = 0, v4 = 0, e4 = 0; + for (const auto& o : b.ops) { + const std::string kd = pstr(&o, "kv_dtype"); + k6 += kd == "k6_v4"; v4 += kd == "fp8_v4"; e4 += kd == "fp8_e4m3"; + } + CHECK(k6 > 0); + CHECK(e4 > 0); + CHECK_EQ(v4, 0); + CHECK(noted(b, "kv cache k6_v4")); +} + +/* libr4d's fp8_v4 and k6_v4 rows are h256/gqa6 only: the probe answers no for another geometry, so the + * engine refuses the flag at startup, and declare refuses it too if asked anyway. */ +TEST(qwen35_fp8_quantised_kv_is_refused_outside_h256_gqa6) { + MetaKV mk = q35fp8_meta(); + mk.m.n_head_kv = 2; /* gqa 12 */ + RadArchProbe p{}; + CHECK_OK(qwen35_fp8::probe(mk.meta(), &p)); + CHECK_EQ(p.kv_quant_ok, 0); + for (int q : { (int)RAD_KVQ_V4, (int)RAD_KVQ_K6V4 }) { + RadBuilder b; + RadBuildCtx ctx = build_ctx(); + ctx.kv_dtype = RAD_F8E4M3; + ctx.kv_quant = q; + CHECK_EQ(stderr_of_rc([&] { return qwen35_fp8::declare(&b, mk.meta(), &ctx); }), + RAD_E_UNSUPPORTED); + } +} + /* The 35B-A3B: a replicated router over every expert, a normalised top-k (norm_topk_prob * defaults on in this family; without it the weights sum to about 0.4), fp8a8 grouped GEMMs over * the expert planes, and the shared expert folded into the gather behind a sigmoid gate. */ diff --git a/tests/config_test.cpp b/tests/config_test.cpp index 3f43b87d..78af66f2 100644 --- a/tests/config_test.cpp +++ b/tests/config_test.cpp @@ -621,16 +621,21 @@ TEST(the_p2p_mode_is_one_of_three_names) { CHECK_EQ(parse({ "--p2p", "OFF" }, &c), RAD_E_INVAL); } -TEST(the_kv_cache_dtype_is_one_of_two_names) { +TEST(the_kv_cache_dtype_is_one_of_four_names) { Config c; CHECK_EQ(parse({ "--kv-cache-dtype", "bf16" }, &c), RAD_OK); CHECK_EQ(c.kv_cache_dtype, std::string("bf16")); CHECK_EQ(parse({ "--kv-cache-dtype", "fp8" }, &c), RAD_OK); CHECK_EQ(c.kv_cache_dtype, std::string("fp8")); - /* Two values only: an "auto" resolving to bf16 would be a second name for the default, and + CHECK_EQ(parse({ "--kv-cache-dtype", "fp8_v4" }, &c), RAD_OK); + CHECK_EQ(c.kv_cache_dtype, std::string("fp8_v4")); + CHECK_EQ(parse({ "--kv-cache-dtype", "k6_v4" }, &c), RAD_OK); + CHECK_EQ(c.kv_cache_dtype, std::string("k6_v4")); + /* Four values only: an "auto" resolving to bf16 would be a second name for the default, and * each value should be unambiguous in a log. */ CHECK_EQ(parse({ "--kv-cache-dtype", "auto" }, &c), RAD_E_INVAL); CHECK_EQ(parse({ "--kv-cache-dtype", "fp16" }, &c), RAD_E_INVAL); + CHECK_EQ(parse({ "--kv-cache-dtype", "v4" }, &c), RAD_E_INVAL); Config d; CHECK_EQ(parse({}, &d), RAD_OK);