Repository navigation
Conversation
With a quantized K/V cache (-ctk/-ctv q8_0) the qwen4exp block selection cannot use the maskless selected-key kernels, so every QSA layer takes the masked top-k route. That route scores the queries in strips. When a strip is shorter than the ubatch (ubatch > 512, or n_kv > 262144 where the score strip halves), each layer made its own view of the KQ mask and of the QSA bias for each strip. These are host inputs, and the scheduler gives each view of a host input its own device copy, kept for the whole graph. The compute buffer then grew with n_layer x n_strips x n_kv x strip. Measured on Strix Halo by the authors of ggml-org#219 and ggml-org#276 (HIP, q8_0 K/V, -ub 4096): the masked-route reserve slope is 0.148 MiB per context cell without sharing and 0.029 MiB per cell with it; 21578 MiB against 7026 MiB of ROCm0 compute buffer at -c 131072, and about 41 GB at -c 262144, which puts the main and the MTP draft context over the 128 GB of the box. The views now come from one map per graph, keyed by input, first query and strip size, so all layers share one view (and one device copy) per strip. The ops and their inputs do not change, so the output is bit-identical. The f16 maskless route does not make these views and does not change. This is the strip-view part of ggml-org#219 (closed unmerged), which ggml-org#276 keeps. It does not change a single-strip ubatch (ub <= 512 at n_kv <= 262144). Refs ggml-org#206 Assisted-by: Fable 5.2
dzannotti
left a comment
There was a problem hiding this comment.
Verdict
Request changes, for the PR text only. The code is correct, and the leak it fixes is real. But the PR does not fix the configuration reported in #206, so "Fixes #206" would close an issue that is still open for its reporters.
Direction
Accept after work. One shared view per strip is the right fix for the masked multi-strip route, it is fork-only code, and it is small. We want it after the description is corrected and has its own memory numbers.
Asks
- PR body: change "Fixes #206" to "Refs #206", as in the commit message. The #206 configuration is the default
-ub 512and the default context (262144 for Qwen3.8-Flash-Next) with one stream. At n_kv = 262144,qwen4exp_score_strip(src/models/qwen4exp.cpp:1084-1092) keeps the strip at 512 because 65536 x 4 x 512 is equal to 2^27, not greater. So that ubatch is one strip,wholeis true at qwen4exp.cpp:1603, and this PR makes no views there. The bisected commit in #206 (bb030af) is also older than the score strips (8026a5c, 98cf44f), so the reported regression has a different cause. - PR body (should fix): add before/after compute buffer sizes measured on this commit, with the environment block from CONTRIBUTING.md. For example ROCm0 and Vulkan0 at
-c 131072 -ub 4096, q8_0 K/V,-fa on, plus one-ub 512control that must not change. The numbers in the commit message come from the authors of #219 and #276, on larger diffs. - src/models/models.h:2640 (should fix): the scheduler does not keep each copy "for the whole graph".
ggml_backend_schedadds every input copy of a split at the start of that split (ggml/src/ggml-backend.cpp:1827-1842), so all copies that one split reads are live at the same time. Suggested text: "The scheduler makes one device copy per view of a host input, so all layers share one view per strip." Then the comment at qwen4exp.cpp:1604 can go.
Nit: qwen4exp_apply_compact_visibility (qwen4exp.cpp:1167-1177) makes per-layer views of the compact limits input in the same way. That is small and on the maskless route only, so a follow-up is fine.
Correctness
INCOMPLETE. Code reading confirms that the cache key (input, first query, strip size) is enough. n_kv and n_stream are fixed per graph (qwen4exp.cpp:1411, 1419), the strides and ne[0] come from the source tensor, each compression ratio has its own bias input, and the map is new for each graph build, so the MTP and reserve graphs do not share it. The ops and their inputs do not change, so the output should be byte-identical. The required checks for a model-graph change (test-llama-archs, logit parity on the four corpora, KLD/PPL, greedy decode, on ROCm and Vulkan) did not run in this pass.
Verification
Code review on the PR merged into master 093c552 (1 commit behind, clean merge). The PR also merges cleanly with #271, and the two do not overlap: #271 can move q8_0 to the maskless route on Vulkan, but ROCm stays on the masked route, where this PR helps. Either merge order works.
Not tested
No build and no hardware runs: compute buffer sizes, peak GTT, logit parity and speed are not measured yet. We will run them after the update.
Thanks for splitting this out of #219.
Review made with AI on behalf of the strix-llama.cpp maintainers.
Full verification (for reproduction and agents)
Environment
Strix Halo (gfx1151). Linux kernel 7.0.0, ROCm 10.1.0 (hipconfig 7.16.26385), AMD clang 24.0.0git d6f6cb69, Mesa RADV 26.0.8, cmake 4.2.3, gcc 15.2.0. Nothing was built or run in this pass.
Compared
- master tip: 093c552
- PR head: 06868e2
- PR merged into master (local, not pushed): 0d70638b8cdd955df7a3f761916663e0ed3f9c18
- merge base: 50c2c8c, 1 commit behind (093c552 changes CI workflows only)
git merge-treeof the PR head with #271 head 1d5bb5a: clean.
Build
Not run. Planned flags: -DGGML_VULKAN=ON -DGGML_HIP=OFF -DCMAKE_BUILD_TYPE=Release -G Ninja and -DGGML_HIP=ON -DGGML_VULKAN=OFF -DGPU_TARGETS=gfx1151 -DCMAKE_HIP_COMPILER=$(hipconfig -l)/clang -DCMAKE_PREFIX_PATH=$(hipconfig -R) -DCMAKE_BUILD_TYPE=Release -G Ninja.
Models
Read only, for metadata: Qwen3.8-Flash-Next-Gyro-M-TQ2_0.gguf (agentionai/Qwen3.8-Flash-Next-Gyro-GGUF, sha256 7aa36d15...). Metadata: context_length 262144, block_count 48, 12 QSA layers with compress ratio 4, indexer head_count 4, indexer top_k 2048.
Commands
Code reading only: git diff 50c2c8cb5...06868e274, git merge-tree --write-tree <pr> <#271 head>, git log/git show of bb030af, 87f1e90, 8026a5c, 98cf44f, and a GGUF header read for the metadata above.
Results
Strip count at the #206 configuration (n_stream 1, -ub 512, n_kv 262144): n_blocks = 262144/4 = 65536; 1 x 65536 x 4 x 512 = 2^27, and the loop at qwen4exp.cpp:1088 halves only while the product is greater than 2^27. Strip = 512 = n_tps, so whole is true and no strip views exist. kv-unified with -np 2 is still one stream. The MTP graph has one QSA layer, so sharing cannot save anything there.
Expected saving where the PR applies (n_stream 1, -fa on, F16 KQ mask plus F32 per-block bias, ratio 4): per QSA layer, summed over all strips, the copies are 2 x n_kv x n_tps + 4 x (n_kv/4) x n_tps = 3 x n_kv x n_tps bytes. 12 layers unshared: 36 x n_kv x n_tps. Shared: 3 x n_kv x n_tps. At -ub 4096 that is 0.141 MiB per context cell before and 0.012 MiB after. The commit message reports 0.148 and 0.029 MiB per cell, which agrees within about 10 % for the before value. Estimates: -c 131072 -ub 2048 about 8.25 GiB less, -c 131072 -ub 4096 about 16.5 GiB less, -c 32768 -ub 2048 about 2 GiB less, -c 262144 -ub 512 no change.
When the masked route runs on master: K or V not F16 (q8_0 always takes it today), no selected-key kernels, -fa off, KQV not offloaded, or n_kv above 1048576. More than one strip needs n_stream 1 with ubatch above 512, or n_kv above 262144.
Correctness
| Check | Method | Result |
|---|---|---|
| View cache key | code reading, qwen4exp.cpp:1411, 1419, 1437-1484, 1587-1603, models.h:2639 | OK: shape-defining values are fixed per graph or come from the source tensor |
| Map lifetime | code reading, qwen4exp.cpp:424 (new graph object per build) | OK |
| Root cause (one device copy per view) | ggml-backend.cpp:1687-1710, 1827-1842 | confirmed |
| test-llama-archs qwen4exp | not run | no hardware in this pass |
| f32 logit parity, 4 corpora, ROCm and Vulkan | not run | no hardware in this pass |
| KLD/PPL short and long | not run | no hardware in this pass |
| Greedy decode with A/A | not run | no hardware in this pass |
| ROCm with HIP_LAUNCH_BLOCKING unset and =1 | not run | no hardware in this pass |
| Memory: compute buffers, graph nodes, peak GTT, base vs head | not run | no hardware in this pass |
Performance
Not run.
Findings in full
- "Fixes #206" is wrong (see Results). The branch name mentions PLE, which the PR does not touch. 87f1e90 from #206 is the lazy-mode on-direct and PLE prefetch commit, and bb030af replaced the old selection with the whole-batch masked
build_qsa_top_kbefore strips existed. - No measurements for this commit in the PR (CONTRIBUTING.md benchmarking requirements).
- models.h:2640 comment: the copies are allocated at the start of each split that reads them, not for the whole graph.
- Nit: the same per-layer view pattern in
qwen4exp_apply_compact_visibility, maskless route, tens of MiB at-ub 4096. - Nit: the generic lambda and
std::tuplekey are heavier than the find/emplace pattern ofrs_rowsin the same struct. Fine as it is. - #276 (draft) carries the same hunk and will rebase cleanly on top of this.
Claims
- The compute buffer drops on the masked multi-strip route: not tested. The mechanism is confirmed in the scheduler code, and the estimate agrees with the reported slope.
- The output is bit-identical: not tested. The ops and their inputs do not change.
- The f16 maskless route does not change: not tested. By code reading it makes no strip views of these inputs.
- A single-strip ubatch does not change: not tested. By code reading
wholeis true there. - Fixes #206: not reproduced. By code reading the #206 configuration is a single strip.
Not tested
Every hardware check: builds, test-llama-archs, logit parity, KLD/PPL, greedy decode, memory and speed on ROCm and Vulkan. The hardware was not available for this pass.
Resolves memory regression described in #206 for non-F16 KV-cache and bigger ubatch sizes.
Fixes #206