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