Skip to content

Qwen3.8-27B CUDA: --kv-dtype int8 (packed -cq 8 rows; drafted rounds -6% at 90k, -13% at 242k) - #247

Closed
Arminova wants to merge 1 commit into
ashhart:mainfrom
Arminova:feat/27b-cuda-int8-kv
Closed

Arminova wants to merge 1 commit into
ashhart:mainfrom
Arminova:feat/27b-cuda-int8-kv

Conversation

@Arminova

@Arminova Arminova commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

The 27B's CUDA engine can now keep its attention caches in int8 with --kv-dtype int8 (one GPU). The default stays bf16, and the bf16 path's output is byte-identical to 0.6.1/0.6.2 (checked on fixed inputs, 1 to 128 rows, up to 90k keys).

Why: on a DGX Spark the 27B's decode slows with depth because every round reads the whole bf16 cache (64 KiB a position over the 16 attention layers). Real long agentic sessions here sit at ~100k tokens, where a drafted round costs ~145 ms against ~106 ms at 3k. int8 halves those bytes.

Design

  • Same scheme as Flash Next's cache: ExLlamaV3 -cq 8 (each group of 32 values rotated by a 32-point Hadamard, fp16 absmax scale, midpoint grid). The quantizer is Flash Next's own kvquant.quant_groups_8/dequant_group_8, so the codes and scales are bit-identical to kvcache.quantize_ref.
  • Packed rows (tensorfold/cuda/kernels/kvpack.py): a (position, KV head) row is head_dim int8 codes followed by head_dim/32 fp16 scales (272 bytes at head dim 256). A layer's cache is still one tensor indexed by position, so grow, reserve, commit copies, clones, kept states and the KV room work unchanged.
  • Drafts equal serial: a verify window packs its keys and values before attention, and the tree attention dequantizes committed rows and the row's own path alike. A drafted row therefore reads exactly what serial decoding reads after a commit, which is now a byte copy of the accepted packed rows.
  • Prompts keep their bits in any chunking: prefill stores packed rows and attends over the dequantized prefix (kvpack.unpack, rotated back to the model's basis). Every key, the chunk's own included, passes through its codes, so a resumed prompt equals a fresh one. The CUDA prefill attention kernel is unchanged.
  • The query is rotated once (kvpack.rotate) and the merged output rotated back in _merge.
  • int8 plans hold 32 (row, head) pairs an item (PACKED_TILE). With 16, a 16-row window over 24 heads dequantizes each key tile six times and int8 gained nothing over bf16 at 16 rows. 64 pairs spilled registers. 32 with Flash Next's reshape dequant was the fastest of the variants measured on GB10.
  • Admission counts the packed bytes plus one layer's dequantized prompt read. The concurrent decoder's memory gate uses the packed row size. --tp 2 refuses --kv-dtype int8.

Measured on one DGX Spark (GB10)

Two public checkpoints:

  • Vontra/Qwen3.8-27B-MLX-4bit, the docs' standard checkpoint;
  • armin1/Qwen3.8-27B-NVFP4-FP8-Mixed-LH, a ModelOpt mixed checkpoint (NVFP4 MLP with the last 8 layers in FP8, FP8 attention/DeltaNet/head), served with --precision full.

Both used z-lab/Qwen3.8-27B-DFlash2 and --parallel 3 --thinking --prefill-fp8, with both cache dtypes in the same build (0.6.2 + this branch). Round time = decode seconds over rounds from /health (it separates the cache's cost from acceptance). Prompts are seeded non-repeating log lines plus one 512-token code request, greedy.

ms a drafted round:

Prompt tokens MLX 4-bit, bf16 int8 NVFP4/FP8 mixed, bf16 int8
2,890 82.9 82.8 106.1 105.8
45,100 102.2 96.3 125.4 120.5
90,220 121.6 113.6 (−7%) 144.9 136.0 (−6%)
180,500 159.9 144.1 (−10%) 184.0 168.3 (−9%)
242,500 188.6 164.5 (−13%) 213.1 186.4 (−13%)

"draft": false, ms a token: MLX 4-bit 96.4 → 86.5 at 90k and 143.1 → 114.4 at 242k (−10%, −20%); NVFP4/FP8 mixed 118.4 → 108.3 and 164.3 → 136.5 (−9%, −17%). Prompt fill is within 2% of bf16. The startup estimate at the 262,144 window drops from 37.39 to 30.38 GiB (MLX) and from 43.60 to 36.59 GiB (NVFP4/FP8). Short-context decode is unchanged (code / prose tok/s, bf16 vs int8: MLX 78.8 / 50.2 vs 81.2 / 50.1, NVFP4/FP8 mixed 66.5 / 39.4 vs 66.3 / 38.6).

Tree attention alone (24 q heads / 4 KV heads, head dim 256, median of 50), ms a layer:

Keys 16 rows bf16 int8 1 row bf16 int8
2,048 0.112 0.134 0.067 0.097
32,768 0.893 0.729 0.622 0.427
90,000 2.336 1.839 1.713 1.125
131,072 3.320 2.576 2.485 1.587
180,000 4.536 3.507 3.415 2.162

Quality (100 local tasks, greedy, thinking off, 1,024 tokens; needle at 194,893 tokens):

Checkpoint, cache GSM8K JSON Code Instructions Greedy agreement with BF16 Needle
MLX 4-bit, bf16 48/50 14/15 25/25 8/10 41.9% correct
MLX 4-bit, int8 47/50 14/15 24/25 9/10 43.0% correct
NVFP4/FP8 mixed, bf16 48/50 14/15 25/25 8/10 55.8% correct
NVFP4/FP8 mixed, int8 49/50 14/15 25/25 7/10 55.2% correct

Items change in both directions:

  • On NVFP4/FP8 mixed, int8 answers one GSM8K question bf16 misses and misses one instruction task. On both, it gives the unquantized model's result.
  • On MLX, int8 runs one GSM8K answer to the token cap and fails one code test.
  • On MLX, int8 also passes an instruction task that bf16 and the unquantized model both miss.

The EXL3 path shares the same forward and cache code but was not run end to end here.

Tests

tests/cuda/test_qwen27_kv_int8.py (22 tests):

  • packed codes and scales equal kvcache.quantize_ref bit for bit;
  • unpack and rotate equal the reference H32;
  • branch nodes equal their serial bits with packed caches, from 0 to 20,501 keys;
  • streams in one launch equal each alone;
  • packed attention matches torch on the dequantized rows;
  • bf16/packed mixing and a wrong-tile plan are refused;
  • every int8 prefill chunking gives the same state;
  • an int8 resume equals fresh, and drafts equal serial;
  • the int8 prompt tracks bf16 closely.

tests/test_cuda_cli.py now expects the 27B to accept int8. GLM still refuses it, and int4 stays refused for the 27B. On one GB10, the attention, prefill-attention, Flash Next kvquant, all test_qwen27_*, CLI, geometry and KV-room suites pass (415 passed, 20 skipped).

Not in this PR

  • int4 for the 27B.
  • Two ranks.
  • Fusing the dequant into the CUDA prefill attention kernel. Prompts read a bf16 copy of the prefix, one layer at a time, and admission counts it.
  • The fp32 chunk partials that _shared writes and _merge reads back. They cost int8 and bf16 alike, about 0.6 ms a layer at 90k with 16 rows, and are a separate change.

The 27B's attention caches can hold int8 rows packed by the new
cuda/kernels/kvpack.py: each (position, KV head) row is ExLlamaV3's -cq 8
codes (H32-rotated groups of 32, midpoint grid, Flash Next's quantizer)
followed by the groups' fp16 scales, so a cache is still one tensor a
layer that grows, slices, copies and clones by position.

- Decode windows pack their keys and values before attention, and the
  shared tree attention dequantizes committed and path rows alike, so a
  drafted row reads what serial decoding reads after a commit (which is
  now a byte copy of the accepted rows): drafts equal serial at int8.
- Prompts store packed rows and attend over the dequantized prefix, so
  every key passes through its codes and any chunking keeps its bits.
- The query is rotated once into the cache's rotation and the merged
  output rotated back; int8 plans take 32 (row, head) pairs an item.
- The bf16 path is unchanged, byte for byte against 0.6.1.
- Admission counts the packed bytes and one layer's dequantized prompt
  read; two ranks refuse --kv-dtype int8.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ashhart

ashhart commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Thank you, @Arminova, for this and for #268, which ships in 0.6.4 today.

We're closing this one. TensorFold keeps one rule on every engine: a speed or memory feature can't change the reply, and an int8 cache does, even with the bf16 default left identical.

The problem you measured is real. A drafted round at about 100k tokens costs about 145 ms against about 106 ms near the start, because every round reads the whole bf16 cache. A change that cuts those reads while keeping the bf16 output byte-identical would land, and we'd be glad to help with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants