[GH-ISSUE #3700] [Engine] qwen35 path: opt-in int8 K/V cache for 262K on 16 GB cards (measured, asking for scope) #1537

Closed
opened 2026-11-04 01:52:05 +07:00 by bsakaguchi · 3 comments
Owner

Originally created by @xyehya on GitHub (Oct 22, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/3700

User problem: on the Qwen3.6 engine path (src/qwen35) the K/V cache is fp32 only, 45 KB per token (10 attention layers + the MTP block, 2 KV heads x 256, K and V). On a 16 GB card that limits the context hard, and it takes the room the routed experts would use:

--max-context (fp32 K/V) VRAM free after init experts per layer in VRAM
32,768 9.9 GiB 112 / 256
131,072 5.2 GiB 47 / 256
196,608 2.1 GiB 4 / 256
262,144 cudaMalloc ... out of memory (q36_engine.cu:1127) -

(Sage-A1.5 Q4_K_M, a Qwen3.6-35B-A3B fine-tune, RTX 5080 16 GB, engine 0.1.47 + #3699.) Flash-Next has --kv int8 and KV streaming; this path has neither, so 262K agent contexts are out of reach on 16 GB.

Proposed behaviour: --kv int8 (opt-in, Strata's existing name) for the qwen35 path. fp32 stays the default and unchanged.

What I have working locally (not submitted; asking for scope agreement first, per CONTRIBUTING):

  • Each K/V cell stored as llama.cpp's q8_0 grid: 256 int8 values + 8 fp32 scales (per 32 values, d = amax / 127), 288 bytes instead of 1,024 (3.56x smaller). 262K then takes ~3.3 GB.
  • Quantize in k_attn_prep (one warp per 32-value block, __shfl_xor amax), dequantize in k_attn_part (16-byte loads for K) and in k_kv_f16 (the prompt path already runs attention in fp16 GEMMs, so the int8 cells are widened there). Allocation, the session copy (cudaMemcpy2DAsync widths) and the auto-chunk rule's per-cell constant take the cell size from one helper. About 100 lines in q36_engine.cu.
  • 262K now loads: context 262144 cells (int8 K / V) ... 6.85 GiB of VRAM free, hybrid experts: 70 of 256 per layer in VRAM.

Measured, int8 vs fp32 on the same build and weights, --tokens + --force the fp32 run's 64 greedy tokens + --dump-full-logits, full-vocabulary KL(fp32 || x). The noise floor is fp32 with STRATA_Q36_CHUNK=1024 (the same comparison the QWEN36.md chunk section uses):

prompt (llama.cpp source code) int8 mean KL (max) floor mean KL (max) top-1 agreement decode fp32 -> int8
13,709 tokens 1.21e-3 (1.46e-2) 1.26e-3 (1.19e-2) 64/64 95.7 -> 115.3 tok/s
55,997 tokens 7.16e-3 (2.99e-1) 2.89e-3 (6.17e-2) 64/64 28.4 -> 99.3 tok/s

Prompt speed is unchanged (5,419 vs 5,412 tok/s at 13.7K; 3,814 vs 3,812 at 56K). Decode gets faster because the decode attention reads 3.5x fewer bytes (fp32 decode at 56K is 28 tok/s on this card).

Tradeoffs / open questions:

  • At 56K the KL is ~2.5x the chunk-size floor (top-1 still 64/64); at 14K it is at the floor. Similar in size to what q8_0 K/V costs in llama.cpp, but I have not measured llama.cpp's q8_0 vs f16 on the same prompts yet.
  • Only CUDA built and run (RTX 5080). HIP not built.
  • Batch slots (--batch) and pictures were not exercised with int8; the slot tables only carry pointers, so they should follow, but that is untested.
  • Not done: KV streaming for this path, fp16 cells as a middle option.

Would you take this as a PR? If yes, I would open it with the batch / vision gates from tools/qwen35/ run on int8 as well. The model file used is at https://huggingface.co/ykarout/Strata-Sage-A1.5-GGUF (the patch there is the local state, including #3699).

Originally created by @xyehya on GitHub (Oct 22, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/3700 **User problem:** on the Qwen3.6 engine path (`src/qwen35`) the K/V cache is fp32 only, 45 KB per token (10 attention layers + the MTP block, 2 KV heads x 256, K and V). On a 16 GB card that limits the context hard, and it takes the room the routed experts would use: | `--max-context` (fp32 K/V) | VRAM free after init | experts per layer in VRAM | |---|---|---| | 32,768 | 9.9 GiB | 112 / 256 | | 131,072 | 5.2 GiB | 47 / 256 | | 196,608 | 2.1 GiB | 4 / 256 | | 262,144 | `cudaMalloc ... out of memory` (q36_engine.cu:1127) | - | (Sage-A1.5 Q4_K_M, a Qwen3.6-35B-A3B fine-tune, RTX 5080 16 GB, engine 0.1.47 + #3699.) Flash-Next has `--kv int8` and KV streaming; this path has neither, so 262K agent contexts are out of reach on 16 GB. **Proposed behaviour:** `--kv int8` (opt-in, Strata's existing name) for the qwen35 path. fp32 stays the default and unchanged. **What I have working locally** (not submitted; asking for scope agreement first, per CONTRIBUTING): - Each K/V cell stored as llama.cpp's q8_0 grid: 256 int8 values + 8 fp32 scales (per 32 values, `d = amax / 127`), 288 bytes instead of 1,024 (3.56x smaller). 262K then takes ~3.3 GB. - Quantize in `k_attn_prep` (one warp per 32-value block, `__shfl_xor` amax), dequantize in `k_attn_part` (16-byte loads for K) and in `k_kv_f16` (the prompt path already runs attention in fp16 GEMMs, so the int8 cells are widened there). Allocation, the session copy (`cudaMemcpy2DAsync` widths) and the auto-chunk rule's per-cell constant take the cell size from one helper. About 100 lines in `q36_engine.cu`. - 262K now loads: `context 262144 cells (int8 K / V) ... 6.85 GiB of VRAM free`, `hybrid experts: 70 of 256 per layer in VRAM`. Measured, int8 vs fp32 on the same build and weights, `--tokens` + `--force` the fp32 run's 64 greedy tokens + `--dump-full-logits`, full-vocabulary KL(fp32 || x). The noise floor is fp32 with `STRATA_Q36_CHUNK=1024` (the same comparison the QWEN36.md chunk section uses): | prompt (llama.cpp source code) | int8 mean KL (max) | floor mean KL (max) | top-1 agreement | decode fp32 -> int8 | |---|---|---|---|---| | 13,709 tokens | 1.21e-3 (1.46e-2) | 1.26e-3 (1.19e-2) | 64/64 | 95.7 -> 115.3 tok/s | | 55,997 tokens | 7.16e-3 (2.99e-1) | 2.89e-3 (6.17e-2) | 64/64 | 28.4 -> 99.3 tok/s | Prompt speed is unchanged (5,419 vs 5,412 tok/s at 13.7K; 3,814 vs 3,812 at 56K). Decode gets faster because the decode attention reads 3.5x fewer bytes (fp32 decode at 56K is 28 tok/s on this card). **Tradeoffs / open questions:** - At 56K the KL is ~2.5x the chunk-size floor (top-1 still 64/64); at 14K it is at the floor. Similar in size to what q8_0 K/V costs in llama.cpp, but I have not measured llama.cpp's q8_0 vs f16 on the same prompts yet. - Only CUDA built and run (RTX 5080). HIP not built. - Batch slots (`--batch`) and pictures were not exercised with int8; the slot tables only carry pointers, so they should follow, but that is untested. - Not done: KV streaming for this path, fp16 cells as a middle option. Would you take this as a PR? If yes, I would open it with the batch / vision gates from `tools/qwen35/` run on int8 as well. The model file used is at https://huggingface.co/ykarout/Strata-Sage-A1.5-GGUF (the patch there is the local state, including #3699).
Author
Owner

@xyehya commented on GitHub (Oct 22, 2026):

Heads up: #3641 (acrogenesis) overlaps this one, so linking them.

  • It adds --kv f16 to the same qwen35 path. It touches the same places as my int8 patch: K/V element size, k_kv_f16, session copies, the attention launch. It says it adds no int8 format, so the two fit together: int8 could just be another value of the same --kv option rather than a separate flag.
  • It also adds a grouped-query decode attention kernel (q36attn::part in q36_attn.cuh): one block reads each K/V tile once and shares it across the 8 query heads of that KV head. Right now it only switches on with --f16-weights or --kv f16. That's the bottleneck I see on Sage Q4_K_M at long context: decode drops from ~137 t/s to ~80 t/s at ~90K because the old k_attn_part reads every cache row once per query head. The kernel is templated on the KV element type, so an int8 version on top of it looks straightforward.
  • Why int8 is still worth having next to f16 on 16 GB: at 262K, f16 K/V is ~5.9 GB vs ~3.3 GB for int8. That difference decides how many routed experts fit in VRAM (with int8 at 262K I get 70/256 per layer).

So my plan is to build int8 on top of #3641 (its --kv plumbing plus its grouped attention), not next to it, and rebase onto it if it lands first. If you'd rather keep KV formats out of scope until #3641 is decided, that's fine too, I'll wait.

<!-- gh-comment-id:6339718829 --> @xyehya commented on GitHub (Oct 22, 2026): Heads up: #3641 (acrogenesis) overlaps this one, so linking them. - It adds `--kv f16` to the same qwen35 path. It touches the same places as my int8 patch: K/V element size, `k_kv_f16`, session copies, the attention launch. It says it adds no int8 format, so the two fit together: int8 could just be another value of the same `--kv` option rather than a separate flag. - It also adds a grouped-query decode attention kernel (`q36attn::part` in `q36_attn.cuh`): one block reads each K/V tile once and shares it across the 8 query heads of that KV head. Right now it only switches on with `--f16-weights` or `--kv f16`. That's the bottleneck I see on Sage Q4_K_M at long context: decode drops from ~137 t/s to ~80 t/s at ~90K because the old `k_attn_part` reads every cache row once per query head. The kernel is templated on the KV element type, so an int8 version on top of it looks straightforward. - Why int8 is still worth having next to f16 on 16 GB: at 262K, f16 K/V is ~5.9 GB vs ~3.3 GB for int8. That difference decides how many routed experts fit in VRAM (with int8 at 262K I get 70/256 per layer). So my plan is to build int8 on top of #3641 (its `--kv` plumbing plus its grouped attention), not next to it, and rebase onto it if it lands first. If you'd rather keep KV formats out of scope until #3641 is decided, that's fine too, I'll wait.
Author
Owner

@xyehya commented on GitHub (Oct 24, 2026):

An update with more measurements, and a question about how to split this so it fits with #3678 and #3641.

Since opening this I took the same path further: the int8 cache on its own fixes the 262K out-of-memory, but decode and prompt
reads still slow down a lot with depth. The two places where the time goes, measured on an RTX 5080 16 GB with Sage-A1.5
Q4_K_M (Qwen3.6-35B-A3B fine-tune), 262K context, int8 cache:

  • Decode attention. The int8 decode kernel is latency-bound: at 262K cells one layer's attention for a 3-row MTP verify
    window takes 1.55 ms (microbench). Two changes: a reworked int8 kernel (bit-identical results: full logits compared
    with cmp) brings it to 0.93 ms, and an opt-in tensor-core version (fp16 mma.sync over the exact int8 cells,
    sm_80+) to 0.43 ms. One row alone: 555 -> 361 -> 331 us (84% of the card's measured bandwidth).
  • Prompt attention. The prompt path dequantizes the int8 cells to fp16 and runs unfused GEMMs on fp32 scores. A fused,
    flash-style kernel that reads the int8 cells directly (opt-in) makes a layer's prompt attention 5-9x faster at 262K and
    frees about 0.9 GB.

With these plus some placement and pool work (separate PRs), a server sweep on this card went from 86 to 162 tok/s decode
and from 806 to 2,477 tok/s prompt read at 240K (same config, two runs each, back to back). I would not claim those totals
for the attention PRs alone; each PR would carry its own before/after.

Quality, since the tensor-core kernel computes in fp16:

  • Against an fp32-cache reference (full-vocabulary logits, 195 teacher-forced steps at 14K / 57K / 76K), every new path is at
    least as close as the current one: mean KL 6.6-8.5e-3 vs 1.55e-2, top-1 184-189 of 195 vs 185.
  • A parity test with peaky scores (Q scaled up to x16, logits up to 39, planted keys, up to 262K cells): the mma kernel's error
    stays under the fp16 bound (<= 2.7e-4 of the output), and its distance to the fp32 values is within 2.8% of the int8 kernel's.
  • At 200K, the new paths move the output no more than changing the prompt chunk size does (mean KL 6.8e-3 vs 6.9e-3, top-1
    63/65 both). Needle retrieval at ~100K and ~200K: 40/40, same as before.

Question. #3678 (yang478) adds --kv int8 / fp8 / q4 to this path and #3641 (acrogenesis) adds F16 and the grouped-query
kernel that my work builds on. I would rather not open a competing KV PR. Would this split work for you?

  1. KV formats: whichever of #3678 or my int8 patch you prefer; I can rebase onto #3678.
  2. Decode attention at long context: the grouped-query kernel for the quantized caches, the reworked int8 kernel, and the
    opt-in tensor-core kernel, with the parity test above.
  3. Prompt attention at long context: the fused kernel and a chunked DeltaNet prompt pass, both opt-in.

Each would be opt-in, with the default path byte-identical (checked by comparing full logits with every flag off against a
main build), and a same-day before/after on this card. Built and run: CUDA only (sm_120; sm_75 compiles and refuses the
sm_80 kernels at runtime). Not run: HIP and SYCL builds; I would keep those PRs as drafts and ask for help there.

🤖 Generated with Claude Code

<!-- gh-comment-id:6379679338 --> @xyehya commented on GitHub (Oct 24, 2026): An update with more measurements, and a question about how to split this so it fits with #3678 and #3641. Since opening this I took the same path further: the int8 cache on its own fixes the 262K out-of-memory, but decode and prompt reads still slow down a lot with depth. The two places where the time goes, measured on an RTX 5080 16 GB with Sage-A1.5 Q4_K_M (Qwen3.6-35B-A3B fine-tune), 262K context, int8 cache: - **Decode attention.** The int8 decode kernel is latency-bound: at 262K cells one layer's attention for a 3-row MTP verify window takes 1.55 ms (microbench). Two changes: a reworked int8 kernel (bit-identical results: full logits compared with `cmp`) brings it to 0.93 ms, and an opt-in tensor-core version (fp16 `mma.sync` over the exact int8 cells, sm_80+) to 0.43 ms. One row alone: 555 -> 361 -> 331 us (84% of the card's measured bandwidth). - **Prompt attention.** The prompt path dequantizes the int8 cells to fp16 and runs unfused GEMMs on fp32 scores. A fused, flash-style kernel that reads the int8 cells directly (opt-in) makes a layer's prompt attention 5-9x faster at 262K and frees about 0.9 GB. With these plus some placement and pool work (separate PRs), a server sweep on this card went from 86 to 162 tok/s decode and from 806 to 2,477 tok/s prompt read at 240K (same config, two runs each, back to back). I would not claim those totals for the attention PRs alone; each PR would carry its own before/after. Quality, since the tensor-core kernel computes in fp16: - Against an fp32-cache reference (full-vocabulary logits, 195 teacher-forced steps at 14K / 57K / 76K), every new path is at least as close as the current one: mean KL 6.6-8.5e-3 vs 1.55e-2, top-1 184-189 of 195 vs 185. - A parity test with peaky scores (Q scaled up to x16, logits up to 39, planted keys, up to 262K cells): the mma kernel's error stays under the fp16 bound (<= 2.7e-4 of the output), and its distance to the fp32 values is within 2.8% of the int8 kernel's. - At 200K, the new paths move the output no more than changing the prompt chunk size does (mean KL 6.8e-3 vs 6.9e-3, top-1 63/65 both). Needle retrieval at ~100K and ~200K: 40/40, same as before. **Question.** #3678 (yang478) adds `--kv int8 / fp8 / q4` to this path and #3641 (acrogenesis) adds F16 and the grouped-query kernel that my work builds on. I would rather not open a competing KV PR. Would this split work for you? 1. KV formats: whichever of #3678 or my int8 patch you prefer; I can rebase onto #3678. 2. Decode attention at long context: the grouped-query kernel for the quantized caches, the reworked int8 kernel, and the opt-in tensor-core kernel, with the parity test above. 3. Prompt attention at long context: the fused kernel and a chunked DeltaNet prompt pass, both opt-in. Each would be opt-in, with the default path byte-identical (checked by comparing full logits with every flag off against a main build), and a same-day before/after on this card. Built and run: CUDA only (sm_120; sm_75 compiles and refuses the sm_80 kernels at runtime). Not run: HIP and SYCL builds; I would keep those PRs as drafts and ask for help there. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

@Niko1221 commented on GitHub (Nov 5, 2026):

Thanks for taking the time to report this.

Strata has changed a lot since this was opened, and 0.2.11 is out now, so many older reports no longer apply. We're closing issues older than a week that have had no new reports in the last week. Closed here does not mean rejected.

To reopen it takes about two minutes:

  1. Re-test on 0.2.11. It may already be fixed. Write down what you see.
  2. Start the title with its area: [Engine], [GUI], [TUI], [Benchmark], [Docs] or [Other].
  3. Fill in the parts of the issue form: what happened, the Strata version, your graphics card, memory and system.
  4. Comment /reopen. A check runs and either reopens the issue or lists what is still missing.

The rules are in CONTRIBUTING.md.

<!-- gh-comment-id:6659639590 --> @Niko1221 commented on GitHub (Nov 5, 2026): Thanks for taking the time to report this. Strata has changed a lot since this was opened, and 0.2.11 is out now, so many older reports no longer apply. We're closing issues older than a week that have had no new reports in the last week. Closed here does not mean rejected. To reopen it takes about two minutes: 1. Re-test on 0.2.11. It may already be fixed. Write down what you see. 2. Start the title with its area: [Engine], [GUI], [TUI], [Benchmark], [Docs] or [Other]. 3. Fill in the parts of the issue form: what happened, the Strata version, your graphics card, memory and system. 4. Comment `/reopen`. A check runs and either reopens the issue or lists what is still missing. The rules are in [CONTRIBUTING.md](https://github.com/Niko1221/Strata/blob/main/CONTRIBUTING.md).
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#1537
No description provided.