Skip to content

qwen4exp : halve the indexer score memory - #29825

Merged
ServeurpersoCom merged 7 commits into
ggml-org:masterfrom
ServeurpersoCom:qwen4exp-indexer-score-memory
Oct 3, 2026
Merged

ServeurpersoCom merged 7 commits into
ggml-org:masterfrom
ServeurpersoCom:qwen4exp-indexer-score-memory

Conversation

@ServeurpersoCom

Copy link
Copy Markdown
Contributor

Overview

The qwen4exp indexer was wasting memory: it computed the scores of all its heads at once, then made a full copy of them for the relu, before summing everything into a single score. At 131k context with -b 4096 that is about 4 GB of intermediates to produce a 0.5 GB result, and the largest buffers of the whole graph.

It now scores one head at a time, with the relu done in place, and adds each head into the score as it goes. Same computation, same top-k selection, half the compute buffer.

Confirmed by @LucaAmigoni on the full model with two GPUs: -b 4096 with the default -np loads again without OOM, as before #29751.

CUDA compute buffer, micro qwen4exp with the real QSA config (4 indexer heads, top_k 2048, kpool 4):

  • c 131072, ub 2048: 3.0 GiB -> 1.5 GiB
  • c 131072, ub 4096: 6.1 GiB -> 3.0 GiB
  • c 262144, ub 4096: 11.7 GiB -> 5.6 GiB

The scores only differ by fp32 rounding from the matmul reduction order, logits are bit-exact, test-llama-archs passes, and pp/tg speed is unchanged.

The rest of the reserve is the dense [n_kv, n_tokens] selection mask from the TODO in build_qsa_sel: it already reuses one buffer, so going below it needs flash_attn_ext to take the selected cell indices instead of a mask.

Additional information

Follow-up #29751

Requirements

@ServeurpersoCom
ServeurpersoCom requested a review from CISC as a code owner October 1, 2026 21:07
@github-actions github-actions Bot added the model Model specific label Oct 1, 2026
@ServeurpersoCom

Copy link
Copy Markdown
Contributor Author

Thanks a lot @LucaAmigoni for testing it on the full model, looking forward to the results of your stress test!

@ServeurpersoCom
ServeurpersoCom requested a review from am17an October 1, 2026 21:13
@LucaAmigoni

LucaAmigoni commented Oct 1, 2026 •

Copy link
Copy Markdown

Thanks a lot @LucaAmigoni for testing it on the full model, looking forward to the results of your stress test!

I've launched two parallel coding tasks (a code quality test and a complex plan execution), at now It have passed 20M tokens without issues. Context use for tasks is 21% and 62% (alloc 384k, -np default), VRAM is stable.

Edit: no MTP, -sm tensor on 2x GPUs

@Volunteer-1

Copy link
Copy Markdown

Still crashes same as before. Details: #29811 (comment)

@CISC CISC left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any measurable latency diff of doing it one head at a time?

Comment thread src/models/qwen4exp.cpp Resolved

@am17an am17an left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please measure performance at large depth (32k->128k). The better solution maybe to use the lightning indexer

@ServeurpersoCom

Copy link
Copy Markdown
Contributor Author

Please measure performance at large depth (32k->128k). The better solution maybe to use the lightning indexer

am17an

Switching to the lightning indexer touches the CUDA and Metal kernels (qwen4exp has only 4 indexer heads), so it will take several PRs, but it saves 2.3 GiB and beats master on TG at every depth up to 128k.

@ServeurpersoCom
ServeurpersoCom requested review from a team and ggerganov as code owners October 2, 2026 07:27
@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) CUDA Related to the CUDA backend labels Oct 2, 2026
@ServeurpersoCom

Copy link
Copy Markdown
Contributor Author

Turned out small enough to keep in this PR: CUDA gets a 4 head case in the vector kernel and Metal takes the head count as a function constant, both validated with test-backend-ops and the Metal fusion check. I benchmark :

@am17an

am17an commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

What about vulkan?

@ServeurpersoCom

Copy link
Copy Markdown
Contributor Author

What about vulkan?

Vulkan already handles it, the shader loops over the heads and supports_op only checks the head size, so no change is needed there. I'm measuring every backend against master, results coming.

@IMbackK

IMbackK commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

note that this conflicts with #29050 which probably should be merged before this.

edit: corrected pr num

@ServeurpersoCom

Copy link
Copy Markdown
Contributor Author

note that this conflicts with #29050 which probably should be merged before this.

edit: corrected pr num

No worries, I'll base my work on top of this PR

@am17an

am17an commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

No, this can be merged first.

@IMbackK

IMbackK commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

either way is fine with me, just fyi the other pr.

@ServeurpersoCom

Copy link
Copy Markdown
Contributor Author

What about vulkan?

On Vulkan the lightning indexer shader slows the prefill down at depth, 2879 -> 1936 t/s @ 128k (4215 -> 3561 @ 32k), while TG is unchanged and the compute buffer only drops from 6292 to 5892 MiB, I'm looking into optimizing it.

@ServeurpersoCom

Copy link
Copy Markdown
Contributor Author

Against master with my prod config: CUDA goes from 6056 to 3748 MiB of compute buffer with PP unchanged and TG 69.3 -> 70.2 t/s @ 128k, and Metal from 6224 to 3924 MiB with PP 674 -> 780 t/s and TG 39.8 -> 43.1 t/s @ 64k.

(For CUDA, I load the N-grams table into RAM)

@ServeurpersoCom
ServeurpersoCom requested a review from a team as a code owner October 2, 2026 08:23
@ServeurpersoCom

Copy link
Copy Markdown
Contributor Author

I added a commit that tiles the Vulkan lightning indexer shader over keys and tokens, so against master Vulkan now goes from 6292 to 5892 MiB of compute buffer with PP 2879 -> 2937 t/s and TG 73.8 -> 75.4 t/s @ 128k.

@ServeurpersoCom
ServeurpersoCom requested a review from am17an October 2, 2026 08:26
@am17an

am17an commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

@ServeurpersoCom wouldn't it be easier to use the vec kernel for vulkan too? cc: @0cc4m not sure what's happening. For vulkan is there a way to keep this lightining indexer off and fix it in a later PR? The changes are quite a lot and it needs to validated against other models which use it.

@ServeurpersoCom

ServeurpersoCom commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor Author

There is no per backend switch since #27877 made the fused op the only path, so should I move the Vulkan shader to its own PR for @0cc4m and keep this one on CUDA and Metal, with Vulkan losing some prefill at depth on qwen4exp until that one lands?

Or should I keep the Vulkan shader here and first validate it against the other lightning indexer models (deepseek4, glm-dsa, glm5-next) on Vulkan? <- I'm running a correctness test+benchmark on that in the meantime.

asf0 pushed a commit to asf0/strix-llama.cpp that referenced this pull request Oct 12, 2026
halo-box/llama.cpp is archived, so its last master comes in once: the
2026-10-07 upstream sync (halo halo-box#34) with its resolutions, plus the archival
README of that repository is not taken (strix keeps its own README).

Resolution rules: MTP follows upstream's design; everywhere else strix's
side wins where the two collide, and upstream's non-conflicting changes stay.

- qwen4exp: strix's QSA stack (selected-key / WMMA block selection,
  incremental pooled keys, PLE prefetch, HIP prefill work) is kept whole in
  qwen4exp.cpp and the qwen4exp struct of models.h. strix's graph_mtp is kept:
  it builds the same draft as upstream's ggml-org#29761 (eh_proj of [enorm(e);
  hnorm(h)], the draft's own nextn.hc_head_* mixer, sparse QSA attention) and
  also loads shared sidecars without embeddings / LM head. The converter,
  gguf-py tensor maps and arch tables are upstream's, so new conversions
  follow upstream's layout; strix loads both.
- llama-memory-hybrid-idx: both APIs kept side by side - strix's qsa_* for
  qwen4exp and upstream's k-pool for the new GLM5Next (ggml-org#27773 and follow-ups).
  strix's context set_input_qsa, which upstream deleted, is restored.
- llama-graph: strix's in-place gated-delta-net state path is kept; upstream's
  single recurrent-state gather (ggml-org#29856, avoids a realloc abort under
  GGML_SCHED_NO_REALLOC) is reverted on this path and needs porting.
- vulkan: strix's flash_attn.comp and lightning_indexer.comp are kept whole;
  upstream's FA shmem out-of-bounds fix (ggml-org#29988) is ported into strix's
  shader. Upstream's lightning indexer rewrite (ggml-org#29825) is reverted on the
  Vulkan host side to match strix's shader; its CUDA/Metal parts stay.
  Upstream's sparse FA path (ggml-org#29639) stays out: strix deleted its shader.
- ggml-alloc: upstream's alloc_buffer_n refactor (ggml-org#23671) replaces strix's
  backport of the view-init fix; strix's regression test is kept.
- cuda: strix's RDNA3.5 paths kept; upstream's shared-expert MoE fusion added
  next to strix's fused GDN decode, with both sets of fusion args.
- server: speculative accept keeps strix's replay branch, then upstream's
  rejection sampling at temp > 0.
- tests: both sides' new tests kept (rollback table gets both columns,
  save-load-state test 10 is strix's, 11 upstream's); strix's rollback test
  migrated to common_batch / llama_process.
- fusion.yml kept (upstream removed it), flash_attn_sparse_compact.comp
  stays deleted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
cwriter pushed a commit to cwriter/llama.cpp that referenced this pull request Oct 15, 2026
…_indexer

Upstream ggml-org#29825 computes the qwen4exp indexer score with
ggml_lightning_indexer, so the MUL_MAT -> RELU -> head-sum chain the
fusion matched is no longer built by any model. The op now runs on the
existing SYCL lightning indexer kernel.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012gLfeVbEnsVZPcPfgmDyqm
iki007 added a commit to iki007/llama.cpp that referenced this pull request Oct 21, 2026
Since ggml-org#29825 the QSA indexer scores every batch with ggml_lightning_indexer. On SYCL that kernel is slow for long
prefill batches: at depth 100000 a 2048-token Qwen3.8-Flash-Next ubatch spends 881 ms in it over the 12 QSA layers,
and prefill fell 13.8% (473.2 -> 407.7 t/s in a profiled run). Batches above 32 tokens, and backends without the fused
indexer, now score all heads with one matrix product, a ReLU and a sum over the heads, as before ggml-org#29825; batches of up
to 32 tokens (decode, draft verify) keep the lightning indexer. The product brings back the [n_pool, n_idx_h, n_tokens]
f32 buffer that ggml-org#29825 removed (1.13 GiB at context 147456 and ubatch 2048).

Two Arc Pro B70, Qwen3.8-Flash-Next UD-Q4_K_XL (20 CPU MoE layers, q8_0 cache), this build against the service snapshot
of the pre-ggml-org#29825 base, A B B A: pp2048 at depth 100000 433.5 -> 487.8 t/s, ranges separate (the build also carries
the other changes since that snapshot). Perplexity (4096 context, ubatch 2048, one chunk) 3.7556 -> 3.7555.

Assisted-by: Codex
Assisted-by: Claude Opus 5.5
iki007 added a commit to iki007/llama.cpp that referenced this pull request Oct 21, 2026
8113fa792 adds the pool mask to the score of batches above 32 tokens, but the mask stayed f16 for the lightning
indexer. The CPU backend has no f32 + f16 ADD and aborts ("binary_op: unsupported types"), so a qwen4exp prefill with
its QSA layers on the CPU stopped at the first batch. The mask is now f16 only when the lightning indexer reads it and
f32 otherwise, as before ggml-org#29825, and the scorer follows the mask type.

-inf and 0 are exact in both types: Qwen3.8-Flash-Next perplexity on two Arc Pro B70 (4096 context, ubatch 2048)
3.7555, as before.

Assisted-by: Claude Opus 5.5
iki007 added a commit to iki007/llama.cpp that referenced this pull request Oct 22, 2026
Since ggml-org#29825 the QSA indexer scores every batch with ggml_lightning_indexer. On SYCL that kernel is slow for long
prefill batches: at depth 100000 a 2048-token Qwen3.8-Flash-Next ubatch spends 881 ms in it over the 12 QSA layers,
and prefill fell 13.8% (473.2 -> 407.7 t/s in a profiled run). Batches above 32 tokens, and backends without the fused
indexer, now score all heads with one matrix product, a ReLU and a sum over the heads, as before ggml-org#29825; batches of up
to 32 tokens (decode, draft verify) keep the lightning indexer. The product brings back the [n_pool, n_idx_h, n_tokens]
f32 buffer that ggml-org#29825 removed (1.13 GiB at context 147456 and ubatch 2048).

Two Arc Pro B70, Qwen3.8-Flash-Next UD-Q4_K_XL (20 CPU MoE layers, q8_0 cache), this build against the service snapshot
of the pre-ggml-org#29825 base, A B B A: pp2048 at depth 100000 433.5 -> 487.8 t/s, ranges separate (the build also carries
the other changes since that snapshot). Perplexity (4096 context, ubatch 2048, one chunk) 3.7556 -> 3.7555.

Assisted-by: Codex
Assisted-by: Claude Opus 5.5
iki007 added a commit to iki007/llama.cpp that referenced this pull request Oct 22, 2026
8113fa792 adds the pool mask to the score of batches above 32 tokens, but the mask stayed f16 for the lightning
indexer. The CPU backend has no f32 + f16 ADD and aborts ("binary_op: unsupported types"), so a qwen4exp prefill with
its QSA layers on the CPU stopped at the first batch. The mask is now f16 only when the lightning indexer reads it and
f32 otherwise, as before ggml-org#29825, and the scorer follows the mask type.

-inf and 0 are exact in both types: Qwen3.8-Flash-Next perplexity on two Arc Pro B70 (4096 context, ubatch 2048)
3.7555, as before.

Assisted-by: Claude Opus 5.5
edwardyoon pushed a commit to edwardyoon/focus-llama that referenced this pull request Oct 24, 2026
iki007 added a commit to iki007/llama.cpp that referenced this pull request Oct 29, 2026
Since ggml-org#29825 the QSA indexer scores every batch with ggml_lightning_indexer. On SYCL that kernel is slow for long
prefill batches: at depth 100000 a 2048-token Qwen3.8-Flash-Next ubatch spends 881 ms in it over the 12 QSA layers,
and prefill fell 13.8% (473.2 -> 407.7 t/s in a profiled run). Batches above 32 tokens, and backends without the fused
indexer, now score all heads with one matrix product, a ReLU and a sum over the heads, as before ggml-org#29825; batches of up
to 32 tokens (decode, draft verify) keep the lightning indexer. The product brings back the [n_pool, n_idx_h, n_tokens]
f32 buffer that ggml-org#29825 removed (1.13 GiB at context 147456 and ubatch 2048).

Two Arc Pro B70, Qwen3.8-Flash-Next UD-Q4_K_XL (20 CPU MoE layers, q8_0 cache), this build against the service snapshot
of the pre-ggml-org#29825 base, A B B A: pp2048 at depth 100000 433.5 -> 487.8 t/s, ranges separate (the build also carries
the other changes since that snapshot). Perplexity (4096 context, ubatch 2048, one chunk) 3.7556 -> 3.7555.

Assisted-by: Codex
Assisted-by: Claude Opus 5.5
iki007 added a commit to iki007/llama.cpp that referenced this pull request Oct 29, 2026
8113fa792 adds the pool mask to the score of batches above 32 tokens, but the mask stayed f16 for the lightning
indexer. The CPU backend has no f32 + f16 ADD and aborts ("binary_op: unsupported types"), so a qwen4exp prefill with
its QSA layers on the CPU stopped at the first batch. The mask is now f16 only when the lightning indexer reads it and
f32 otherwise, as before ggml-org#29825, and the scorer follows the mask type.

-inf and 0 are exact in both types: Qwen3.8-Flash-Next perplexity on two Arc Pro B70 (4096 context, ubatch 2048)
3.7555, as before.

Assisted-by: Claude Opus 5.5
iki007 added a commit to iki007/llama.cpp that referenced this pull request Nov 4, 2026
Since ggml-org#29825 the QSA indexer scores every batch with ggml_lightning_indexer. On SYCL that kernel is slow for long
prefill batches: at depth 100000 a 2048-token Qwen3.8-Flash-Next ubatch spends 881 ms in it over the 12 QSA layers,
and prefill fell 13.8% (473.2 -> 407.7 t/s in a profiled run). Batches above 32 tokens, and backends without the fused
indexer, now score all heads with one matrix product, a ReLU and a sum over the heads, as before ggml-org#29825; batches of up
to 32 tokens (decode, draft verify) keep the lightning indexer. The product brings back the [n_pool, n_idx_h, n_tokens]
f32 buffer that ggml-org#29825 removed (1.13 GiB at context 147456 and ubatch 2048).

Two Arc Pro B70, Qwen3.8-Flash-Next UD-Q4_K_XL (20 CPU MoE layers, q8_0 cache), this build against the service snapshot
of the pre-ggml-org#29825 base, A B B A: pp2048 at depth 100000 433.5 -> 487.8 t/s, ranges separate (the build also carries
the other changes since that snapshot). Perplexity (4096 context, ubatch 2048, one chunk) 3.7556 -> 3.7555.

Assisted-by: Codex
Assisted-by: Claude Opus 5.5
iki007 added a commit to iki007/llama.cpp that referenced this pull request Nov 4, 2026
8113fa792 adds the pool mask to the score of batches above 32 tokens, but the mask stayed f16 for the lightning
indexer. The CPU backend has no f32 + f16 ADD and aborts ("binary_op: unsupported types"), so a qwen4exp prefill with
its QSA layers on the CPU stopped at the first batch. The mask is now f16 only when the lightning indexer reads it and
f32 otherwise, as before ggml-org#29825, and the scorer follows the mask type.

-inf and 0 are exact in both types: Qwen3.8-Flash-Next perplexity on two Arc Pro B70 (4096 context, ubatch 2048)
3.7555, as before.

Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning model Model specific testing Everything test related Vulkan Issues specific to the Vulkan backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants