Skip to content

Oversized --max-ctx silently destroys attention throughput (20× prefill slowdown at 32K, 8× at 64K) #10

Closed

Description

@dusterbloom

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        # oversized

Measurements

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

  1. dusterbloom commented on Apr 22, 2026

    @dusterbloom
    CollaboratorAuthor

    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_dflash 1K prompt)

    Scaling curve — max_ctx vs. prefill time (Q4_0 KV)

    max_ctx Prefill Peak VRAM
    2048 2.93 s ~18 GB
    8192 2.94 s ~18 GB
    32768 2.94 s ~20 GB
    49152 2.93 s ~20 GB
    65536 2.93 s ~21 GB
    81920 3.88 s ~22 GB
    98304 3.89 s ~22 GB
    131072 14.39 s 24.2 GB (98.4%)

    Flat 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:

    • Model weights: 14.91 GB (on GPU; tok_embd 682 MB on CPU)
    • Q4_0 KV cache: ~3.6 GB (Q8_0: ~6.8 GB)
    • DFlash draft weights: 3.46 GB
    • DeltaNet/SSM state + graph scratch: ~1.5 GB growing
    • Peak: 24.2 GB out of 24.6 GB (98.4%)

    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-bench with -c 131072 -ctk q4_0 -ctv q4_0 on the same model — runs fine. That's because llama-bench doesn'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 16384 so 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_ctx to ~98304 on 24 GB GPUs (suggested)

    Anyone asking for max_ctx > 98304 on 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×:

    • Q4_0 KV @ 131K: 3.6 GB
    • TBQ3 KV @ 131K: ~2.1 GB
    • Saves: ~1.5 GB

    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:

    1. Cherry-pick TBQ quant kernels (k_tbq*_dequant, FWHT rotations, fattn-tbq-fused.cuh) into the submodule — ~1.6k lines, self-contained additions
    2. Add GGML_TYPE_TBQ3_0 / GGML_TYPE_TBQ4_0 as kv_k_type/kv_v_type options in create_target_cache
    3. Verify DFlash's custom ops (ggml_ssm_conv_tree, ggml_gated_delta_net_tree) coexist with TBQ paths — both share the same fattn dispatcher base, so conflicts should be isolated

    This 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@b373478 with PR #11 merged, submodule Luce-Org/llama.cpp@b16de6590.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions