Repository navigation
Description
Symptom
When running test_dflash with --max-ctx set larger than the actual required capacity (prompt_len + n_gen + verify_block), attention throughput collapses. The slowdown scales roughly with max_ctx / kv_len and is especially brutal with --kv-q4.
The README example "128K context in 24 GB (134.78 tok/s at ctx=131072)" and scripts/run.py's help text "Q4_0 KV cache (required for max_ctx=131072)" nudge users toward --max-ctx=131072, which turns into a performance trap whenever the real prompt is smaller.
Repro (RTX 3090, Qwen3.5-27B Q4_K_M target + BF16 draft, pristine main at 962c54a)
# same binary, same prompt, only --max-ctx differs:
./build/test_dflash models/Qwen3.5-27B-Q4_K_M.gguf models/draft/model.safetensors \
mid_prompt_32k.bin 256 /tmp/out.bin \
--fast-rollback --ddtree --ddtree-budget=22 --max-ctx=32768 # tight
DFLASH27B_KV_Q4=1 ./build/test_dflash ... --max-ctx=131072 # oversizedMeasurements
| Prompt | KV | --max-ctx |
Prefill | Gen tok/s | Accept-length |
|---|---|---|---|---|---|
| 32K | Q8_0 (default) | 32768 (tight) | 38 s | 42.0 | 4.41 |
| 32K | Q4_0 (--kv-q4) |
131072 (oversize) | 1035 s | 1.10 | 4.13 |
| 64K | Q4_0 (--kv-q4) |
131072 (oversize) | 1014 s | 3.97 | 4.06 |
| 64K | Q4_0 (--kv-q4) |
66048 (tight) | 126 s | 18.35 | 4.20 |
So at 32K the same prompt runs 27× slower in prefill and 38× slower in decode purely because of `--max-ctx`. At 64K it's 8.1× / 4.6×.
Output quality is unaffected (`last_tok` bit-exact across both paths), so this is strictly a perf bug, not correctness.
Root cause
The FA kernel's per-step work scales with `align_up(max_ctx, FATTN_KQ_STRIDE=256)`, not with the actual filled `kv_len`. With Q4_0 KV the per-key dequant cost is paid over the full allocated context even though the mask will zero out the unused tail, so allocating 131K for a 32K prompt burns ~100K keys of dequant + score + mask work per head per step per layer.
Fix (script-side, 10 lines)
`scripts/run.py` and `scripts/bench_llm.py` currently don't pass `--max-ctx` at all, so anyone running a long-prompt test copies `--max-ctx=131072` from the README and trips this. Auto-fitting solves it without touching the C++ path:
```python
scripts/run.py (after tokenize)
if args.max_ctx > 0:
max_ctx = args.max_ctx
else:
pad = 64 # q_len=16 + ddtree budget=22 + margin
max_ctx = ((n_tok + args.n_gen + pad + 255) // 256) * 256
cmd += [f"--max-ctx={max_ctx}"]
```
Happy to open a PR with the patch + updated README note. Separately, it might be worth clamping `g_max_ctx_override` inside `test_dflash.cpp` to the minimum valid value so manual invocations with oversized `--max-ctx` can't shoot users in the foot either — but that's a bigger behavior change, so a script-side fix + README warning is probably the right first step.
Environment
- NVIDIA RTX 3090, driver 595.79, CUDA 12.6
- WSL2 Ubuntu 22.04
- lucebox-hub `main` at 962c54a, submodule `Luce-Org/llama.cpp@b16de6590`
- Python 3.12.3
Activity
fix(scripts): auto-fit --max-ctx to prompt size in run.py / bench_llm.py
dusterbloom commentedon Apr 22, 2026
Follow-up diagnosis: the root cause isn't the FA kernel striding over
max_ctx. It's a VRAM fragmentation cliff at 98% utilization on 24 GB GPUs.Characterization (RTX 3090 24 GB, Qwen3.5-27B Q4_K_M target + BF16 draft,
test_dflash1K prompt)Scaling curve —
max_ctxvs. prefill time (Q4_0 KV)max_ctxFlat up to ~98K, then discrete 5× cliff at 131072. Not a gradual scaling issue.
Not a Q4_0-specific bug
Same repro with default Q8_0 KV also hits 24.2 GB peak and ~14 s prefill at
max_ctx=131072. My earlier "Q8_0 is fast" measurement was from transient GPU state. Both KV types push the system over the edge.The 98.4% VRAM cliff
Budget at
max_ctx=131072:tok_embd682 MB on CPU)At 98%, any small transient allocation (fp16 scratch, FA workspace, per-chunk temp buffers) can't find contiguous VRAM. CUDA falls back to host-mapped / paged memory paths — ~10× slower. This is what kills prefill.
Doesn't repro in vanilla llama-bench
I tried
llama-benchwith-c 131072 -ctk q4_0 -ctv q4_0on the same model — runs fine. That's becausellama-benchdoesn't load a separate 3.46 GB draft + target_feat ring + SSM state alongside the target. DFlash's combined footprint is what pushes over the edge.Three fix angles
(1) Lower default
max_ctx(done in PR #13)Cap default to
16384so the daemon doesn't trip the cliff unless a user explicitly asks for more. Users on 40+ GB GPUs can safely bump. This is a user-facing release fix.(2) Cap effective
max_ctxto ~98304 on 24 GB GPUs (suggested)Anyone asking for
max_ctx > 98304on a 3090 is going to hit the cliff. A startup warning + soft-cap, or auto-detect VRAM and clamp, would prevent the footgun.(3) TurboQuant KV — the real long-term fix
TBQ3 KV compression (3.25 bits/weight vs Q4_0's effective ~5.5) reduces KV footprint ~1.7×:
That 1.5 GB is exactly the headroom needed to drop peak VRAM from 24.2 → 22.7 GB, staying below the cliff.
TBQ kernels live in dusterbloom-turboquant — same ggml/llama.cpp base as
Luce-Org/llama.cpp@luce-dflash. Porting them requires:k_tbq*_dequant, FWHT rotations,fattn-tbq-fused.cuh) into the submodule — ~1.6k lines, self-contained additionsGGML_TYPE_TBQ3_0/GGML_TYPE_TBQ4_0askv_k_type/kv_v_typeoptions increate_target_cacheggml_ssm_conv_tree,ggml_gated_delta_net_tree) coexist with TBQ paths — both share the same fattn dispatcher base, so conflicts should be isolatedThis would also unlock the other TurboQuant win — FA at 128K context via their chunked-prefill path (measured there at 671 tok/s @ 128K, vs DFlash's current 64.7 tok/s at 64K).
Environment
RTX 3090 24 GB, driver 595.79, CUDA 12.6, WSL2,
lucebox-hub@b373478with PR #11 merged, submoduleLuce-Org/llama.cpp@b16de6590.fix(server): sensible default max_ctx + tokenizer auto-detect + Qwen3…
fix(server): restore 16384 default max-ctx (regressed by package refa…
fix(gemma4): gate pFlash to decode-only when KV is TQ3_0
fix(scripts): auto-fit --max-ctx to prompt size in run.py / bench_llm.py
fix(server): sensible default max_ctx + tokenizer auto-detect + Qwen3…
fix(p2): cubic P2 batch — pin actions, sanitize tags, mirror shell wr…
feat(pflash): empty-response guard + bandit floor reconciliation (task …
feat(pflash): empty-response guard + bandit floor reconciliation (task …
feat(pflash): empty-response guard + bandit floor reconciliation (task …
feat(pflash): empty-response guard + bandit floor reconciliation (task …
feat(kvflash): mode 4's exact GPU scorer uses a fused chunk-mass kern…