Skip to content

Misc. bug: qwen4exp indexer cache doubled by QSA kpool (row=2) and mirrored per device, costing ~400 MiB VRAM per GPU under -sm tensor #30005

Open

Description

@gordan-bobic

Name and Version

version: 11436 (259d37719) with GNU 14.3.1 for Linux x86_64

Operating systems

Linux

Which llama.cpp modules do you know to be affected?

llama-server

Command line

./build/bin/llama-server \
  -m /models/Qwen3.8-Flash-Next/Qwen3.8-Flash-Next-UD-Q4_K_XL-00001-of-00004.gguf \
  --split-mode tensor -ts 1,1,1,1,1 \
  -c 221184 -np 1 -fa on \
  --type-k q8_0 --type-v q5_1 \
  --spec-type draft-mtp \
  --spec-draft-model /models/Qwen3.8-Flash-Next/MTP/mtp-Qwen3.8-Flash-Next-shared-Q4_K_M.gguf \
  --spec-draft-n-max 3 --spec-draft-type-k q8_0 --spec-draft-type-v q5_1 \
  -lv 5

Problem description & steps to reproduce

Commit 66e0c17ee ("llama: fix qwen4exp", #29751) changed the QSA indexer cache layout to store the raw key and the pooled key side by side per token:

// src/models/qwen4exp.cpp (b11330+)
hparams.indexer_kpool_row = 2; // raw key | pooled key

// src/llama-memory-hybrid-idx.cpp
hparams_idx.n_embd_head_k_full =
    model.hparams.indexer_head_size * (model.hparams.indexer_kpool > 0 ? model.hparams.indexer_kpool_row : 1);

Before that commit the indexer cache row was single-width (indexer_head_size). The change doubles the per-token size of the indexer cache on the target context. Because cache_idx_* tensors are MIRRORED in the tensor-split state map (that part predates #29751, from #27742), every tensor-parallel worker allocates a full copy, so the doubling lands on every GPU, not once per model.

Commit c061df198 ("Qwen4Exp: add MTP", #29761) adds a second regression on top: the MTP draft context gets its own indexer cache plane (the generic MTP memory filter keeps il >= n_layer() for the idx filter, introduced for GLM-5.2 where the MTP layer is sparse). For qwen4exp shared drafts the nextn layer has no compress ratio, so this plane is allocated at full n_ctx and never written.

Measured on my rig, same flags, only the version differs (older run at 221184 cells, newer forced down to 163840 cells; per-token rates below are normalized):

plane v0.5.0 line b11381 rate change per token
attention KV, 12 layers (sharded) 2349.00 MiB @221K 1740.00 MiB @164K unchanged (10.875 KiB/tok)
indexer cache, target (mirrored per GPU) 344.25 MiB @221K 510.00 MiB @164K 1.59 -> 3.19 KiB/tok (2x)
indexer cache, MTP draft (mirrored per GPU) not allocated 42.50 MiB @164K new (+0.27 KiB/tok)

Normalized to the same 221184-cell context, b11381 needs about +344 MiB (target idx delta) plus ~57 MiB (draft idx plane) of cache state per GPU where the v0.5.0 line needed none of it. On our 5-GPU box that is over 2 GB aggregate, and because the planes are mirrored, the per-GPU number is what caps context: we had to cut from 216 Ki to 160 Ki tokens (~50K tokens lost) to fit.

The pooled-key layout presumably buys the two-stage QSA scoring speedup (we do see faster prefill on this build), but there is currently no way to opt out: indexer_kpool_row is hard-coded in the model loader and no CLI argument touches the kpool layout. For users running several small GPUs instead of one large one, mirrored per-device overhead scales with GPU count while context headroom does not, and for many workloads 50K tokens of context are worth more than the prefill speedup.

Requested change:

  1. A memory-vs-speed trade-off for the mirrored idx plane. Two options, in order of preference:
    • Pack the pooled keys instead of giving every token a pooled slot. Only each block's representative token holds a valid pooled key ("the pooled key lives in the last member's row"; cpy_k zeroes the others), so storing pooled data at ceil(n_cells / compress_ratio) entries rather than n_cells recovers about three quarters of the doubling at ratio 4, with no semantic change and the speed win intact.
    • A runtime switch restoring the pre-llama: fix qwen4exp #29751 scoring path (single-width cache, pooling computed on the fly per ubatch). Note neither existing knob is a flag flip for this: setting indexer_kpool = 0 removes QSA selection entirely (layers fall back to dense attention over full KV, different numerics), and indexer_kpool_row = 1 trips GGML_ASSERT(n_tok > 0) in kpool_access, because the current graph consumes both the per-token and pooled segments every forward (gather_key_gate re-pools new blocks, gather_pooled scores). The old on-the-fly path was deleted by llama: fix qwen4exp #29751 as too slow (~865 us at 33k context), so this option means keeping that deleted algorithm behind a flag.
  2. Independently: skip allocating the draft-context indexer plane when the nextn layer's compress ratio is zero. For a dense MTP draft the plane is dead allocation; nothing reads or writes it.

Steps to reproduce

  1. Load any qwen4exp checkpoint with QSA layers (attention.compress_ratios containing nonzero values) under --split-mode tensor on 2+ GPUs, with -lv 5.
  2. Observe two facts in the log: the indexer cache describe line reports double the per-token size of a pre-b11330 build at equal n_ctx, and creating indexer KV cache appears twice when running --spec-type draft-mtp (target + draft), once before.
  3. Compare against any tag before b11330 (e.g. v0.5.0): one indexer plane, half the size - note that you will have to patch v0.5.0 with model : re-enable -sm tensor for qwen4exp #28569 to enable tensor parallelism and for MTP with models: Qwen3.8-Flash-Next MTP #28243 and speculative: fix draft model loading target path instead of draft path #28442 (draft model path fix), with a self-contained draft GGUF, to make it work for comparison .

First Bad Commit

  • Indexer cache doubling: 66e0c17ee1741fef493312e17fe60a5d2cf5f7d5 ("llama: fix qwen4exp", llama: fix qwen4exp #29751), first tag b11330.
  • Draft-context indexer plane on dense MTP drafts: c061df198 ("Qwen4Exp: add MTP", Qwen4Exp: add MTP #29761), first tag b11330.

Relevant log output

Logs

Same model, same flags, two builds. Older build (v0.5.0 line, 221184 cells):

llama_kv_cache: size = 2349.00 MiB (221184 cells,  12 layers,  1/1 seqs), K (q8_0): 1377.00 MiB, V (q5_1):  972.00 MiB
operator(): creating indexer KV cache, size = 221184 cells
llama_kv_cache: size =  344.25 MiB (221184 cells,  12 layers,  1/1 seqs), K (q8_0):  344.25 MiB, V (q5_1):    0.00 MiB
llama_kv_cache: size =  195.75 MiB (221184 cells,   1 layers,  1/1 seqs), K (q8_0):  114.75 MiB, V (q5_1):   81.00 MiB

Newer build (b11381 line, forced to 163840 cells by VRAM pressure):

llama_kv_cache: size = 1740.00 MiB (163840 cells,  12 layers,  1/1 seqs), K (q8_0): 1020.00 MiB, V (q5_1):  720.00 MiB
operator(): creating indexer KV cache, size = 163840 cells
llama_kv_cache: size =  510.00 MiB (163840 cells,  12 layers,  1/1 seqs), K (q8_0):  510.00 MiB, V (q5_1):    0.00 MiB
llama_kv_cache: size =  145.00 MiB (163840 cells,   1 layers,  1/1 seqs), K (q8_0):   85.00 MiB, V (q5_1):   60.00 MiB
operator(): creating indexer KV cache, size = 163840 cells
llama_kv_cache: size =   42.50 MiB (163840 cells,   1 layers,  1/1 seqs), K (q8_0):   42.50 MiB, V (q5_1):    0.00 MiB

Attention KV per-token rate is identical between builds; the target indexer plane doubled and a second (draft) indexer plane appeared that does not exist in the older build. All three indexer/idx allocations are mirrored across the 5 tensor-parallel workers.

Activity

  1. doktorjw commented on Oct 5, 2026

    @doktorjw

    I ran into the same issue (I am pretty new to digging into details of llama.cpp, so please forgive me if I miss anything here); note that I had help in identify what I was seeing from my running of qwen3.8-flash-next on my setup (2x5090, CachyOS, just built the head of llama.cpp as of a few hours ago ... right around the time 0.60 was tagged/released):

    1. A second context-scaling is mirrored that this issue doesn't list:

    cache_ple_r_* (the PLE dilated-conv history) has an explicit MIRRORED rule — "every device runs the whole conv and needs the whole history" (src/llama-model.cpp:517-520).

    1. The MTP draft pool scales with the target as reported here (cparams.n_ctx = llama_n_ctx(ctx_tgt), common/speculative.cpp:2611). Pinning the draft to one GPU via --spec-draft-device drops the second meta device and the draft's share of both mirrored planes; that could be used as a mitigation while the mirroring itself stands.

    If you need logs about sizing, I can go back and grab those and include them.

    The short of it is ... yes, I'm seeing the same issue as this reported issue; I am also see another in that "the qwen4exp-specific weights (hc_attn/hc_ffn, ple_, indexer., nextn.*) have no split-state rules at all and fall through to the mirrored default at src/llama-model.cpp:605-606" --- the the weights are all mirrored on each GPU and taking up additional memory as well.

    If you like, I can create a separate issue for that weight mirroring.

  2. gordan-bobic commented on Oct 14, 2026

    @gordan-bobic
    Author

    It looks like VRAM usage since 11436 has been reduced reduced, but I am not yet sure if it is because this particular bug has been fixed or because improvements have been made elsewhere.

  3. gordan-bobic commented on Nov 8, 2026

    @gordan-bobic
    Author

    This VRAM wasting problem still seems to be present in master. And it is pretty significant, Across multiple GPUs this can amount to tens of thousands of tokens of context, which can make a big difference to output quality and performance on long context tasks, both through increased need for context compacting and the lossiness arising from compaction.

  4. gordan-bobic commented on Nov 19, 2026

    @gordan-bobic
    Author

    The excessive VRAM consumption still seems to be present in 1.0.0

  5. gordan-bobic commented on Dec 2, 2026

    @gordan-bobic
    Author

    Something really bizarre happens that may be related to this in 1.1.0.
    The VRAM usage is down by approximately the same amount that goes missing due to the bug described here, about 400MB per GPU.
    But - if I bump what I know to be the stable context size available by even a few tousand tokens that should fit in the extra 400MB/GPU - I get CUDA OOMs. The memory spikes must be fast enough that I cannot spot them on monitoring, my most VRAM-loaded GPU goes from 21.4GB to OOM instantaneously and llama-server crashes out.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions