Repository navigation
Conversation
--kv-dtype int8 stores each attention layer's keys and values as 8-bit codes with one fp16 scale per 32 values, the same arithmetic as ExLlamaV3's -cq 8 (H32 rotation, midpoint grid). The default stays bf16. The MLX path and the other CUDA families refuse anything else. Indexer keys and pooled block keys stay bf16. The query rides the rotation, so a group is read as stored.
…izer Codes and scales match a torch reference of the same arithmetic, bit for bit. Attention stays within a stated tolerance of the bf16 cache. Windows match serial steps, chunks match one shot, and drafted token ids match serial.
The recipe, the serve option table, and the third-party notice name the ExLlamaV3 cache scheme the int8 path follows.
Same scheme as the int8 cache, one bits parameter: groups of 32, one fp16 absmax scale, H32, midpoint grid. Two unsigned codes per byte, low nibble first. Indexer keys and pooled block keys stay bf16. The MLX path still refuses anything but bf16.
The int8 tests are parametrized over both widths. Scales match ExLlamaV3's own quantizer bit for bit at 4 and at 8. H32 matches their butterfly. The rotation trick matches an explicit un-rotate, including a window that crosses a chunk boundary and a group size other than 16. The MTP cache is the same dtype and the same write path.
Flash Next's CUDA default stops a draft chain under 0.30. The measured policies are 0.60 and 0.75, so the serve command has to be able to say so.
Byte counts include the fp16 scales and the MTP head's cache. 11,648 bytes a token at int4 is 2.64x on the whole cache, not 4x, because indexer keys and pooled block keys stay bf16.
|
Thanks @vcruz305, a flag-gated int8/int4 KV cache for Flash Next on CUDA is a useful option for long contexts on a single Spark, and keeping bf16 the default is the right call. 0.3.5 is out as of tonight, so we'll review this against it and run our Spark checks: drafted replies equal serial ones and concurrent streams equal their solo runs at each KV dtype, plus the memory and speed trade you describe. It's targeted with #42 for 0.3.6, and your commits will land under your name. |
|
Added one commit to this branch, 43c9349: a stop-token fix for Flash Next on EXL3 checkpoints. On 0.3.6 the CUDA engine takes its stop ids from config.json only (the top-level
Checked on one DGX Spark (GB10) serving the SAGE 3.5 bpw pack on 0.3.6 with this change: a ~16k-token prompt with thinking on, then a get_weather tool request, gives one tool call, The commit also applies cleanly to main (0.3.6.1). If you would rather take it on its own, the same change sits on 0.3.6 as vcruz305:fix/exl3-eos-generation-config. This PR still needs a rebase onto 0.3.6; I am happy to do that if it helps. |
|
Thank you, @vcruz305. The quantized KV cache is in 0.3.6.2, all eight commits under your name (549b7a72 to 5f4e940f). On the same
|
TensorFold's Flash Next CUDA engine can now keep its KV cache in 8 or 4 bits instead of bf16, behind a flag. The command is
tensorfold serve MODEL --kv-dtype int8(orint4). The default stays bf16, so nothing changes for an existing command, and the MLX path refuses anything but bf16 because this only exists on CUDA.What you get is memory, not speed. On one DGX Spark the 8-bit cache is 1.68x smaller than bf16 and decodes at the same speed on short prompts, a few percent faster once the prompt is a few thousand tokens. The 4-bit cache is 3.17x smaller, costs about 12% decode speed at short context, and is roughly even by 4,000 tokens. Both change the output. I measured how much, and the numbers are below rather than summarized.
This is the same scheme ExLlamaV3 uses for
-cq 8and-cq 4(the default grid, no companding): groups of 32 along the head dim, the group rotated by a 32-point Hadamard, one fp16 scale per group taken as the group's absmax, and a midpoint grid. 4-bit packs two codes per byte. The indexer keys and the pooled block keys stay bf16, so the saving applies to the K and V tensors only. The query is rotated on the way in and the merged attention output on the way out, which lets the stored keys and values stay in the rotated domain instead of being rotated back on every read.What changed
cd90ecc, the 8-bit cache.src/tensorfold/families/qwen4_exp/cuda/kvcache.pyis new and holds the storage, the Hadamard, and the quantize and dequantize kernels, plus a pure-torch reference quantizer that the tests check against.glue.pyquantizes keys and values as it writes the cache and rotates the query.attention.pydequantizes inside the attention kernel and rotates the merged output back.forward.py,decode.pyandengine.pythread the dtype through, and the MTP drafter's own cache uses the same dtype as the main cache, so drafts and verification see the same representation.cli.pygains--kv-dtype {bf16,int8,int4}, CUDA only.710ad04, the 4-bit cache, on the same path. Onebitsparameter runs through the kernels rather than a second copy of them. The quantize and dequantize each had to be split into per-width kernels: Triton compiles every branch of a function, and a shared function returned a different shape at 4 bits (two codes per byte) than at 8, which broke compilation of the 8-bit path.attention.pyloads the packed nibbles and unpacks before the dot.09eb756,--mtp-confidenceon the CUDA serve path. The draft chain stops early once a draft's probability drops below this, and the default is 0.3. I added it because the speed numbers people quote for this model were measured at 0.6, and there was no way to ask for that. It is included here because the measurements below depend on it.33a151eand05b49a8, the tests.tests/cuda/test_flashnext_kvquant.pychecks both widths: the kernels write the reference quantizer's bits exactly, the scales match ExLlamaV3's own quantizer, the cache round-trips inside the grid, attention output stays within a stated tolerance of bf16, a window of rows matches the same rows computed one at a time, chunked prefill matches one shot, drafted decoding matches serial by token-id SHA-256, and the end-to-end logits stay close to bf16.tests/test_cuda_cli.pycovers the flag parsing, including the refusal on the MLX path.139b0a4and1f4131e, the docs.docs/recipes/qwen3.8-flash-next.mdgains a section on the flag, the README's option list names it, andTHIRD_PARTY_NOTICES.mdcredits the scheme to ExLlamaV3 (MIT, Copyright (c) 2025 Turboderp).13 files, +920 / -49, 8 commits, no merge commits, based on
bb4b4a3.Measured on a GB10 (DGX Spark)
One DGX Spark (GB10, driver 580.178.04, CUDA 13.0, torch 2.11, 128 GB unified). Every timing arm ran in an exclusive session with the GPU otherwise idle. The model is
Vontra/Qwen3.8-Flash-Next-MLX-4bit-MTP, the MLX 4-bit affine checkpoint (group size 32) with MTP drafting. It is not an EXL3 checkpoint. This branch is cut from upstreammain, and upstream cannot load EXL3 packs. That is what #42 adds, so the EXL3 numbers and these are not comparable and I have not mixed them.Memory
Bytes per token for one attention layer's K and V plus the fp16 scales, head dim 256, 2 KV heads. The scales are included. Leaving them out overstates the saving, and the whole-cache figure is the one that matters:
Across the whole model the saving is smaller, because the indexer keys and the pooled block keys stay bf16 either way. For Flash Next that works out to 1.68x at int8 (30,784 bytes per token down to 18,304) and 3.17x at int4. At a 262,144-token context the K and V pool drops from 7.52 GiB to 4.47 GiB at int8.
Speed
Drafted decode, greedy, 400 tokens, median of 3 reps, thinking off. Depth 6 with confidence 0.6 is the policy, and depth 7 at 0.75 is the single best point I measured. SM clock was 2502 to 2528 MHz of a 3003 max during decode, so these are not peak-clock numbers. The client is the same for every arm.
Short prompts, 8,192-token context, tok/s:
Depth 7 at confidence 0.75, code prompt only: bf16 88.16, int8 87.16, int4 77.88.
A 4,121-token prompt in a 32,768-token context, depth 6 at confidence 0.6. Here the reps are tight enough to trust the gap, so I list them:
Read together: at short context the dequantize costs more than the bandwidth saves, so int8 is a wash (about 98% of bf16) and int4 gives up about 12% on code. By 4,000 tokens of context int8 is 5.7% faster and its slowest rep beat bf16's fastest, while int4 is even with bf16, inside the noise. Prefill is identical across all three, which is what you would expect, since prefill is dominated by the weights and not the cache.
Quality
Teacher-forced, 2,048 positions from the pack's own bench corpus, full vocabulary, the KL summed in fp32. The same weights in every arm, only the cache dtype changes. NLL is in bits per token.
The floor of this measurement is zero in the only sense I could test. I scored the bf16 cache, reloaded the model, and scored it again: same NLL to the digit, KL of 0. The engine is deterministic, so the KL above is the cache and not run-to-run noise. I had first tried to get a floor by varying the prefill chunk size, and that came back as zero too, but for a bad reason: the chunk size never reaches the forward pass, so the two arms were the same run. I am not using that zero.
Two things about this table that are easy to misread. The median KL is small (0.012 bits at int8, 0.024 at int4) while the mean is large, which means most positions barely move and a few move a lot. And int4's NLL is lower than bf16's. That is not int4 being more accurate. A quantized cache shifts the distribution, and a lower NLL on one corpus is a sign of that shift, not a quality win. Top-1 agreement is the cleaner summary: int8 keeps the bf16 top token on 89.2% of positions, int4 on 86.6%.
Tests
test_flashnext_kvquant.pyholds 30 tests across the two widths. The checks that matter:torch.equal), and chunked prefill matches one shot;tensorfold serve, at both widths;The host suite, run with the same exclusions as upstream (
--ignore=tests/test_alternating_kv.py --ignore=tests/test_dflash_tree_search.py), shows the same 4 failures and 130 passes asupstream/main. Those 4 are environmental (this box has no MLX) and are present on main.Overlap with open PRs
git merge-treeis clean againstupstream/main. It is also clean against #42 (pr/exl3), even though the two branches both editdocs/recipes/qwen3.8-flash-next.md,README.md,src/tensorfold/families/qwen4_exp/cuda/forward.py,src/tensorfold/families/qwen4_exp/__init__.pyandTHIRD_PARTY_NOTICES.md. The edits are in different regions and merge without help. I will rebase on #42 if it lands first.#40 also touches
families/qwen4_exp/cuda/{attention,decode,engine,forward}.py, and #29 touchesdocs/recipes/qwen3.8-flash-next.mdand the MLX-side decode path. I have not merge-tested those two. Both are small overlaps and I will rebase on whichever lands first.This also answers the request in #23, partly. That issue is a GLM model running out of memory at 64k on two Sparks and asks for an fp8 cache or a
--kv-dtype. The flag is the one asked for, but the implementation covers Flash Next only. GLM, the 27B and MiMo are not included, and I do not have a GLM checkpoint here to test against.What is not claimed