From 4d68a01a08d448dca8f4c2c17235851227943117 Mon Sep 17 00:00:00 2001 From: mici81 Date: Thu, 22 Oct 2026 12:49:53 +0000 Subject: [PATCH] qwen35_fp8: the GDN state cache at f16 when a deployment asks (RADIANCE_GDN_STATE_F16=1) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit libr4d's recurrent update and chunk scan already read the state's width off the cache; the plugin declared it f32 unconditionally because f16 changes the generated text. A deployment may now trade that: RADIANCE_GDN_STATE_F16=1 (exactly) declares the cache f16, anything else keeps f32, and the declare log says so. The default does not change. Lossy. Qwen3.8-27B (MXFP4, TP2), mean KL against the bf16 reference, f32 -> f16: - k6_v4 KV: chunk 2048 0.040238 -> 0.040005; chunk 64 (the state stored and rounded every 64 tokens) 0.039607 -> 0.040468, top-1 94.07 -> 94.00 %; - fp8 KV: chunk 2048 0.039171 -> 0.039600 (+1.1 %), chunk 64 0.039327 -> 0.039690 (+0.9 %). It halves the state pool, its host checkpoints and the state's traffic, which grows with the decoding sequences. Decode on two R9700s, greedy, three rounds (medians, tok/s, f32 -> f16): - no speculation: 1 stream 67.1 -> 67.5 (+0.6 %), 4 246.8 -> 253.3 (+2.6 %), 16 708.0 -> 779.5 (+10.1 %); the rounds agree to a tenth; - speculation 7: 1 stream 222.2 -> 215.1 (-3.2 %), 4 636.9 -> 621.0 (-2.5 %), 16 1223.8 -> 1276.7 (+4.3 %); f16 also changes which drafts the greedy text accepts, and the text and the step were not separated. So it is for many concurrent sequences where the KL is acceptable. GUIDE §4.4 has the tables. arch_test: the state group is f32 unset, at "0" and at "yes", f16 at "1". Co-Authored-By: Claude Opus 5.5 (1M context) --- arch/qwen35_fp8/qwen35_fp8.cpp | 25 ++++++++++++++++++------- docs/ARCHITECTURES.md | 4 +++- docs/GUIDE.md | 31 +++++++++++++++++++++++++++++++ tests/arch_test.cpp | 21 +++++++++++++++++++++ 4 files changed, 73 insertions(+), 8 deletions(-) diff --git a/arch/qwen35_fp8/qwen35_fp8.cpp b/arch/qwen35_fp8/qwen35_fp8.cpp index ddfe97b5..2657848d 100644 --- a/arch/qwen35_fp8/qwen35_fp8.cpp +++ b/arch/qwen35_fp8/qwen35_fp8.cpp @@ -583,17 +583,28 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c "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. + /* The SSM state cache is f32 unless RADIANCE_GDN_STATE_F16=1 declares it f16. * * State reads and writes dominate the two GDN kernels: one [128,128] state is 64 KiB at f32, - * and a speculative step writes one per candidate token per head, since the surviving - * candidate is unknown until after the layer. At depth 7 that is hundreds of MB per rank per - * step; f16 would halve it. libr4d compiles both widths (`SDT`) and the f16 round-trip error is - * ~5e-4 relative (elements are O(0.1)); vLLM's `--mamba-ssm-cache-dtype` is commonly float16. - * It stays f32 because f16 changes the generated text. */ + * and a step reads a sequence's state and writes it back (under the anchor form once, whatever + * the draft depth). libr4d compiles both widths (`SDT`, from the cache's dtype) and the f16 + * round-trip error is ~5e-4 relative (elements are O(0.1)); vLLM's `--mamba-ssm-cache-dtype` + * is commonly float16. f16 changes the generated text, so it is a deployment's choice, not the + * default. Measured on Qwen3.8-27B (MXFP4, k6_v4, TP2), KL against the bf16 reference: at + * chunk 2048 0.040238 -> 0.040005, at chunk 64 (the state rounded every 64 tokens) 0.039607 -> + * 0.040468, top-1 94.07 -> 94.00 %; with an fp8 KV cache 0.039171 -> 0.039600 and 0.039327 -> + * 0.039690. Decode on two R9700s (three rounds, medians), greedy: without speculation one + * stream 67.1 -> 67.5 t/s, four 246.8 -> 253.3, sixteen 708.0 -> 779.5 (the state traffic grows + * with the sequences); at speculation 7 one stream 222.2 -> 215.1, four 636.9 -> 621.0, sixteen + * 1223.8 -> 1276.7, where f16 also changes the greedy text and so the drafts accepted. It + * halves the state pool and its host checkpoints. */ RadKVGroupDecl sd{}; sd.kind = RAD_KV_LINEAR; - sd.dtype = RAD_F32; + { + const char* e = std::getenv("RADIANCE_GDN_STATE_F16"); + sd.dtype = e && e[0] == '1' && e[1] == '\0' ? RAD_F16 : RAD_F32; + if (sd.dtype == RAD_F16) rad_note(b, "GDN state cache f16 (RADIANCE_GDN_STATE_F16=1)"); + } sd.n_head_kv = gc.n_head_v; sd.state_dim[0] = gc.head_v; sd.state_dim[1] = gc.head_k; diff --git a/docs/ARCHITECTURES.md b/docs/ARCHITECTURES.md index 48569564..71db4069 100644 --- a/docs/ARCHITECTURES.md +++ b/docs/ARCHITECTURES.md @@ -228,7 +228,9 @@ Three groups, which is why hybrid KV groups are first-class: across the 48 linear layers, and it is what the prefix-cache checkpoint of `spec.md` §7.3 snapshots. Its reads and writes dominate the two GDN kernels. libr4d also compiles an f16 form (elements are O(0.1); the f16 round trip is about 5e-4 relative) that would halve that traffic, -but the plugins declare f32 because f16 changes the generated text. How a speculative step keeps a +but the plugins declare f32 because f16 changes the generated text; `RADIANCE_GDN_STATE_F16=1` +declares f16 for a deployment that accepts the loss (GUIDE §4.4: on Qwen3.8-27B MXFP4, mean KL ++0.9..+2.2 %, decode +10 % at sixteen streams without speculation). How a speculative step keeps a state it can roll back is `docs/OPS.md`'s `state_form`. ## FP8 diff --git a/docs/GUIDE.md b/docs/GUIDE.md index a438b49e..db758f74 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -922,6 +922,36 @@ the pool that holds context; compare M with the sessions you expect to keep live wht6`, a decode step above the row count startup names is all-reduced lossily like a prefill chunk; at 8 sequences and depth 3, Flash-Next's decode steps are above it. +**The delta-net state at f16** (`RADIANCE_GDN_STATE_F16=1`). The `qwen35` plugins (`qwen35_fp8`, +and `qwen35moe_fp8` and `qwen35_w8a8`, built from it) declare the recurrent state cache f32. With the +variable set to exactly `1` they declare it f16, and the declare log says so (`GDN state cache f16 +(RADIANCE_GDN_STATE_F16=1)`); any other value, `0` and `yes` included, keeps f32. **It is lossy**: the +state is rounded wherever it is stored, so the generated text changes. It halves the state pool and +its host checkpoints, and the state's traffic, which grows with the number of decoding sequences. +Measured on `qwen35_fp8` only, Qwen3.8-27B MXFP4 at `--tp 2` on two R9700s (PCIe Gen5 x8, 265 W), +against the bf16 model: + +| state | mean KL, 2048 chunk | mean KL, 64 chunk | top-1, 64 chunk | +|---|---|---|---| +| f32, `k6_v4` KV | 0.040238 | 0.039607 | 94.07% | +| f16, `k6_v4` KV | 0.040005 | 0.040468 (+2.2%) | 94.00% | +| f32, `fp8` KV | 0.039171 | 0.039327 | | +| f16, `fp8` KV | 0.039600 (+1.1%) | 0.039690 (+0.9%) | | + +A 64-token chunk stores the state, and rounds it, every 64 tokens. Decode, greedy, three rounds +(medians, tok/s): + +| streams | f32, no speculation | f16 | f32, speculation 7 | f16 | +|---|---|---|---|---| +| 1 | 67.1 | 67.5 (+0.6%) | 222.2 | 215.1 (-3.2%) | +| 4 | 246.8 | 253.3 (+2.6%) | 636.9 | 621.0 (-2.5%) | +| 16 | 708.0 | 779.5 (+10.1%) | 1223.8 | 1276.7 (+4.3%) | + +Without speculation the rounds agree to a tenth and the gain grows with the streams. With +speculation f16 also changes which drafts the greedy text accepts, so one and four streams moved +down; whether that is the text or the step was not separated. Use it for many concurrent sequences +where the KL above is acceptable, and measure your own load. + ### 4.5 Prefill chunk size A long prompt is prefilled in chunks of at most `--max-num-batched-tokens`. On a mixture-of-experts @@ -3280,6 +3310,7 @@ reductions running on fewer blocks: Qwen3.8-27B's 41k-token prefill 9.5 s uncapp | `RADIANCE_CALIB_TOKENS` | with `RADIANCE_CALIB_DIR`, the tokens the calibration tap accumulates before it writes and switches itself off (default 65536) | | `RADIANCE_G3_CALIB_LAYERS` | `lo:hi`: `arch/gemma3`'s calibration tap over layers lo to hi - 1 only, to take a large model's Grams in passes (docs/ARCHITECTURES.md) | | `RADIANCE_PROFILE_EVERY` | with `--profile-ops`, dump the per-op table every N steps (default 32) | +| `RADIANCE_GDN_STATE_F16` | `1` exactly: the `qwen35` plugins declare the delta-net state cache f16 instead of f32. Lossy (the text changes); halves the state pool, faster with many decoding sequences (§4.4) | | `RADIANCE_LOG_STEPS` | log one line per step: its phase, sequences, and each one's token count and context (§4.5) | Engine development has its own instruments: `RADIANCE_DEBUG_*` (residual probes, routing and issue diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index c44abaa3..7577fbf8 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -6154,6 +6154,27 @@ void lint_qwen35_fp8_family(Fn declare, StepFn step, const RadModelMeta* meta, i } /* namespace */ +/* The GDN state cache's width is a deployment's choice: f32 by default, f16 with + * RADIANCE_GDN_STATE_F16=1 (libr4d's kernels read the width off the cache), anything else f32. */ +TEST(qwen35_fp8_gdn_state_is_f32_unless_the_deployment_asks_f16) { + MetaKV mk = q35fp8_meta(); + for (const char* v : { (const char*)nullptr, "1", "0", "yes" }) { + if (v) setenv("RADIANCE_GDN_STATE_F16", v, 1); + RadBuilder b; + RadBuildCtx ctx = build_ctx(); + const int rc = stderr_of_rc([&] { return qwen35_fp8::declare(&b, mk.meta(), &ctx); }); + unsetenv("RADIANCE_GDN_STATE_F16"); + REQUIRE_EQ(rc, RAD_OK); + int n = 0; + for (const RecKV& g : b.kv_groups) + if (g.name == "kv_gdn_state") { + ++n; + CHECK_EQ(g.d.dtype, (v && std::string(v) == "1") ? RAD_F16 : RAD_F32); + } + CHECK_EQ(n, 1); + } +} + /* The fp8 sibling of qwen35_bf16: the same layers with every large linear a block-fp8 gemm_nt_q * read at fp8a8, one quantiser per activation, and the attention's output gate, multiply and * quantiser fused into gate_quant_fp8 (the sigmoid/mul pair is the bf16-linear form only). The