Skip to content

model : fix MTP context kv cache allocation for deepseek2, glm4moe, c… - #28630

Merged
ggerganov merged 3 commits into
ggml-org:masterfrom
LoganChu:mtp-nextn-layer-filter
Sep 11, 2026
Merged

ggerganov merged 3 commits into
ggml-org:masterfrom
LoganChu:mtp-nextn-layer-filter

Conversation

@LoganChu

@LoganChu LoganChu commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Overview

Fixes #28626. MTP draft contexts on DEEPSEEK2, GLM4_MOE and COHERE2MOE allocate KV for every layer instead of only the appended NextN block. This commit adds the three architectures to the existing NextN filter condition and includes a corresponding regression test. This allows for correct kv cache allocation in both the main and draft contexts.

Additional information

test-llama-archs gains a '--mtp-kv' flag. It builds all architectures that are MTP-capable as a synthetic model with an appended NextN block. It tests with a 2-layer and an 8-layer trunk, and checks the MTP context's KV cache is the same size at both depths. The MTP graph only runs one NextN block regardless of depth, so filtered draft context kv cache allocation is depth-invariant.

                     @2-layer     @8-layer   growth
before  deepseek2      884736 B    2654208 B    3.00x   unfiltered
        glm4moe        786432 B    2359296 B    3.00x   unfiltered
        cohere2moe     786432 B    2359296 B    3.00x   unfiltered
after   deepseek2      294912 B     294912 B    1.00x
        glm4moe        262144 B     262144 B    1.00x
        cohere2moe     262144 B     262144 B    1.00x

Adding a NextN block raises block_count and different architectures disagree on the resulting per-layer array length (cohere2moe/mimo2 read the SWA pattern at n_layer(), step35 at n_layer_all). The test matches how their respective loader and converters handle it.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes, for codebase investigation and reviewing and testing the code.

@github-actions github-actions Bot added the testing Everything test related label Sep 9, 2026
Comment thread src/llama-model.cpp Resolved
@LoganChu
LoganChu requested a review from ggerganov as a code owner September 9, 2026 22:17
Comment thread src/llama-model.cpp Resolved
Comment on lines 10 to 13
// TODO: replace with #include "llama-ext.h" in the future
#include "../src/llama-arch.h"
#include "../src/llama-ext.h"
#include "../src/llama-model-saver.h"

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.

Hummm, not sure what exactly this TODO refers to and if it is stale/can be resolved now? @ggerganov

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.

The idea of the TODO is to properly expose libllama internals through the llama-ext.h header only. It's still relevant.

For this PR, don't make any changes to test-llama-archs.cpp. The proposed changes are hard to follow and not generic enough.

@ggerganov ggerganov added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Sep 11, 2026
@ggerganov
ggerganov merged commit 5cdd3d1 into ggml-org:master Sep 11, 2026
24 of 27 checks passed
Patt92 pushed a commit to Patt92/llama.cpp that referenced this pull request Sep 11, 2026
…ggml-org#28630)

* model : fix MTP context kv cache allocation for deepseek2, glm4moe, cohere2moe architectures (ggml-org#28626)

* model: add inverse architecture gating and comprehensive architecture testing for mtp layer filtering

* model : slim NextN filter comment, drop test-llama-archs changes

(cherry picked from commit 5cdd3d1)
fencerJP pushed a commit to fencerJP/llama-apu that referenced this pull request Sep 16, 2026
…ggml-org#28630)

* model : fix MTP context kv cache allocation for deepseek2, glm4moe, cohere2moe architectures (ggml-org#28626)

* model: add inverse architecture gating and comprehensive architecture testing for mtp layer filtering

* model : slim NextN filter comment, drop test-llama-archs changes
quimmedes pushed a commit to quimmedes/cafe-llama.cpp that referenced this pull request Sep 16, 2026
…ggml-org#28630)

* model : fix MTP context kv cache allocation for deepseek2, glm4moe, cohere2moe architectures (ggml-org#28626)

* model: add inverse architecture gating and comprehensive architecture testing for mtp layer filtering

* model : slim NextN filter comment, drop test-llama-archs changes
x1250 pushed a commit to x1250/llama.cpp that referenced this pull request Sep 22, 2026
…ggml-org#28630)

* model : fix MTP context kv cache allocation for deepseek2, glm4moe, cohere2moe architectures (ggml-org#28626)

* model: add inverse architecture gating and comprehensive architecture testing for mtp layer filtering

* model : slim NextN filter comment, drop test-llama-archs changes

(cherry picked from commit 5cdd3d1)
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.

Misc. bug: MTP draft context allocates KV for every layer (deepseek2 / glm4moe / cohere2moe)

3 participants