Skip to content

llama : keep the k-pool layout prefix on sequence edits - #32137

Merged
am17an merged 7 commits into
ggml-org:masterfrom
Maddimax:kpool-truncate-on-edit
Nov 17, 2026
Merged

am17an merged 7 commits into
ggml-org:masterfrom
Maddimax:kpool-truncate-on-edit

Conversation

@Maddimax

Copy link
Copy Markdown

Assisted-by: Claude Code

Overview

I realized that my GPU was not used to ~100% during long running coding sessions. I asked claude to investigate
and it found that the kpool_layout_update was taking a lot of CPU. I asked it to try and fix it and this is the result.
I don't have any experience with the code so its hard for me to judge, I can only report that my GPU usage is closer to 100% now.

Additional information

Claude said:

  • What it fixes: after every rejected speculative draft, kpool_layout_update() rebuilt the whole layout. That cost grows with context length.
  • What changed: for an edit at stale > 0, it keeps the cells and pools in front of stale and re-scans only the tail. An edit at pos_min or below, or a cell count that doesn’t match, still gets the full rebuild.
  • Why it’s safe: it relies on the existing mem_idx_stale contract, which says an edit at p0 leaves every pool that ends before p0 unchanged.
  • Test: check_draft_rollback in test-llama-archs covers qwen4exp, glm5-next and kimi-k31.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes, this was completely created by AI. I'm just the meat proxy here.

@Maddimax
Maddimax requested review from a team and ggerganov as code owners November 12, 2026 11:17
@am17an

am17an commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

changes look ok, automated review below

Tested on an RTX PRO 6000 with test-llama-archs and with a small speculative-decoding loop on the toy qwen4exp model. Each step decodes 1 token plus 2 drafts, then calls seq_rm to drop the drafts.

The change looks correct to me. An edit at stale > 0 can't touch the cells before stale. Restarting the pool scan at the end of the last kept pool gives the same pools as a scan from the start. I checked this with a temporary assert that compares every update with a layout rebuilt from the whole sequence. It covered 305 updates in test-llama-archs (qwen4exp and glm5-next) and 319 in the loop at 32k context, with no mismatches.

Time per kpool_layout_update after the drafts are rejected:

context master PR
4k 33 us 0.8 us
32k 308 us 0.9 us
128k 1.6 ms 1.0 us

The test needs some work:

  • glm5-next aborts in the Meta (-sm tensor) config, which also runs on a single GPU: ggml-backend-meta.cpp:762: view of permuted tensor not implemented. This happens whenever n_rs_seq > 0, even with single-token decodes. This PR doesn't cause it, but the abort stops the whole test binary. Please skip the new check for LLAMA_SPLIT_MODE_TENSOR.

  • The check doesn't really exercise the layout. The fixture's indexer top-k is 131072, so every pool is selected and a wrong layout rarely changes the logits. I broke the new code in four ways, and the test caught only one of them, and only on glm5-next:

    • keep the pools that cross the cut: passes
    • resume the scan at the cut instead of at the end of the last kept pool: fails on glm5-next only
    • keep the rejected draft cells (the bug the stale rebuild was added for): passes
    • treat an edit like an append and rebuild only when the cell count changes: passes

    Comparing the updated layout with a full rebuild catches the first and third at the first rollback. A check like that, in debug builds or behind an env var, would test this much better than logits do.

  • kimi-k31 has no k-pool and never calls kpool_layout_update, so it can be dropped from the list.

Smaller things:

  • Two comments are no longer true: the one above the rebuild ("a stale sequence must rebuild regardless") and the one at llama-memory-hybrid-idx.h:88 ("Sequence edits drop it").
  • !sq.cells.empty() && !sp.empty() && sq.pos_min == sp.begin()->first is now checked twice. The append and truncate cases could share one branch.
  • stale > sq.pos_min states the intent more directly than stale > 0. The two are equivalent here, because mem_idx_stale_pos returns 0 for edits at or below pos_min.

* Append and edit share one branch, using stale > sq.pos_min.
* LLAMA_KPOOL_VERIFY=1 checks every update against a full rebuild. The scan moved into kpool_scan() so the check can reuse it.
* The test turns the check on for its rollback context, skips -sm tensor, and drops kimi-k31.
* Two comments that were no longer true are fixed.

Assisted-by: Claude Code
@github-actions github-actions Bot added the testing Everything test related label Nov 12, 2026
Comment thread src/llama-memory-hybrid-idx.cpp Outdated
nullptr, filter_idx, nullptr, nullptr, "idx_");
}()) {}
}()) {
const char * LLAMA_KPOOL_VERIFY = getenv("LLAMA_KPOOL_VERIFY");

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.

remove this env flag

@funk8bit

Copy link
Copy Markdown

Just wanted to comment I independently worked the same issue tonight (because search is hard, sigh). But the reason to add here is that the performance hit was very significant in my case; Qwen 3.8 Flash Next Q8, running on RTX5000, at ~80% of a 256K context I was getting 20% of the performance of a fresh context. Like @Maddimax I was seeing ~25% utilization on the GPUs because they were stalled waiting for the single threaded CPU core. My patch is functionally equivalent so I'll leave it out, but this is quite impactful.

@am17an

am17an commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Just need to remove the env flag I think

@Maddimax

Maddimax commented Nov 15, 2026 via email

Copy link
Copy Markdown
Author

@am17an am17an added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Nov 15, 2026
@am17an

am17an commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

@ServeurpersoCom @sanmai for a final look

Comment thread src/llama-memory-hybrid-idx.cpp Resolved
@sanmai
sanmai self-requested a review November 16, 2026 08:25
@sanmai

sanmai commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

GPU usage did went up from 40% to about 42% at 145k context with MTP. Benchmarks are positive.

config decode t/s GPU utilization
with fix 55.2, 50.4, 54.4, 55.4 ~42%
baseline 49.3, 52.3, 51.0, 51.4 ~40%

(I updated the table with the correct config labels; there were no differences in acceptance)

@am17an
am17an merged commit 08a9b35 into ggml-org:master Nov 17, 2026
20 of 22 checks passed
gabe-l-hart added a commit to gabe-l-hart/llama.cpp that referenced this pull request Nov 17, 2026
* origin/master: (138 commits)
qwen4exp : fix graph reallocation from the n-gram table scale (ggml-org#32483)
server : return 400 when grammar and ignore_eos are on (ggml-org#32377)
ggml-cuda: disable quantized matvec fusion on all Pascal GPUs (ggml-org#32239)
Title: CUDA: few-row MMQ for MUL_MAT_ID (ggml-org#32065)
ui: turn the live markdown preview off by default (ggml-org#32469)
CUDA: use block reductions for whole-row pool_1d (ggml-org#31855)
ggml : decode F8_E4M3 to fp32 without ldexpf (ggml-org#32477)
llama : keep the k-pool layout prefix on sequence edits (ggml-org#32137)
vocab : speed up PLaMo-2/3 tokenizer table build (ggml-org#32249)
convert : fix FP8 qwen4exp checkpoints (n-gram table scale, block-scaled FP8) (ggml-org#32476)
ui: Llama UI design system: tokens, surfaces and fonts (ggml-org#32468)
mtmd: support Minimax-Music3 (ggml-org#32435)
chat: add tts_dict for kokoro G2P template (ggml-org#32397)
Rotate K/V read order for better L2 cache hit rates (ggml-org#32407)
ui: turn conversation tabs off by default (ggml-org#32402)
ui: move Llama Chat to SvelteKit 3 and its built-in service worker (ggml-org#32463)
qwen3next : apply output projection weight scale (ggml-org#32393)
cuda : raise shared mem limit only for the launched kernel (ggml-org#32392)
nix: expose a SYCL package (ggml-org#31066)
vulkan: disable flip Flash Attention shader on AMD Windows due to a driver bug (ggml-org#32415)
...
dzannotti added a commit to halo-box/strix-llama.cpp that referenced this pull request Nov 17, 2026
Upstream: ggml-org/llama.cpp d3064fd..08a9b35 (2 commits)

Conflicts:
- tests/test-llama-archs.cpp: kept both new functions, the fork test_merged_weights and upstream ggml-org#32137 check_draft_rollback; the fork function gets its closing brace back at the junction.

Assisted-by: Claude Opus 5.5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YKmjzcT1LTvKtbw9EzBVGE
fencerJP pushed a commit to fencerJP/llama-apu that referenced this pull request Nov 21, 2026
* llama : keep the k-pool layout prefix on sequence edits

Assisted-by: Claude Code

* llama : address review of the k-pool layout update

* Append and edit share one branch, using stale > sq.pos_min.
* LLAMA_KPOOL_VERIFY=1 checks every update against a full rebuild. The scan moved into kpool_scan() so the check can reuse it.
* The test turns the check on for its rollback context, skips -sm tensor, and drops kimi-k31.
* Two comments that were no longer true are fixed.

Assisted-by: Claude Code

* llama : check the k-pool layout update in debug builds instead of LLAMA_KPOOL_VERIFY

* llama : remove the k-pool layout verification

* llama : inline the k-pool scan again

* remove dangling new line

* do not search twice

---------

Co-authored-by: Aman Gupta <[email protected]>
Co-authored-by: Alexey Kopytko <[email protected]>
Annieren pushed a commit to Annieren/llama.cpp-upstream that referenced this pull request Nov 23, 2026
* llama : keep the k-pool layout prefix on sequence edits

Assisted-by: Claude Code

* llama : address review of the k-pool layout update

* Append and edit share one branch, using stale > sq.pos_min.
* LLAMA_KPOOL_VERIFY=1 checks every update against a full rebuild. The scan moved into kpool_scan() so the check can reuse it.
* The test turns the check on for its rollback context, skips -sm tensor, and drops kimi-k31.
* Two comments that were no longer true are fixed.

Assisted-by: Claude Code

* llama : check the k-pool layout update in debug builds instead of LLAMA_KPOOL_VERIFY

* llama : remove the k-pool layout verification

* llama : inline the k-pool scan again

* remove dangling new line

* do not search twice

---------

Co-authored-by: Aman Gupta <[email protected]>
Co-authored-by: Alexey Kopytko <[email protected]>
Mr-J-369 pushed a commit to Mr-J-369/llama.cpp-Android that referenced this pull request Nov 25, 2026
* llama : keep the k-pool layout prefix on sequence edits

Assisted-by: Claude Code

* llama : address review of the k-pool layout update

* Append and edit share one branch, using stale > sq.pos_min.
* LLAMA_KPOOL_VERIFY=1 checks every update against a full rebuild. The scan moved into kpool_scan() so the check can reuse it.
* The test turns the check on for its rollback context, skips -sm tensor, and drops kimi-k31.
* Two comments that were no longer true are fixed.

Assisted-by: Claude Code

* llama : check the k-pool layout update in debug builds instead of LLAMA_KPOOL_VERIFY

* llama : remove the k-pool layout verification

* llama : inline the k-pool scan again

* remove dangling new line

* do not search twice

---------

Co-authored-by: Aman Gupta <[email protected]>
Co-authored-by: Alexey Kopytko <[email protected]>
(cherry picked from commit 08a9b35)
Nindaleth pushed a commit to Nindaleth/llama.cpp that referenced this pull request Nov 26, 2026
* llama : keep the k-pool layout prefix on sequence edits

Assisted-by: Claude Code

* llama : address review of the k-pool layout update

* Append and edit share one branch, using stale > sq.pos_min.
* LLAMA_KPOOL_VERIFY=1 checks every update against a full rebuild. The scan moved into kpool_scan() so the check can reuse it.
* The test turns the check on for its rollback context, skips -sm tensor, and drops kimi-k31.
* Two comments that were no longer true are fixed.

Assisted-by: Claude Code

* llama : check the k-pool layout update in debug builds instead of LLAMA_KPOOL_VERIFY

* llama : remove the k-pool layout verification

* llama : inline the k-pool scan again

* remove dangling new line

* do not search twice

---------

Co-authored-by: Aman Gupta <[email protected]>
Co-authored-by: Alexey Kopytko <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants