Skip to content

Studio: gate the MTP target-KV reserve to MTP spec mode, not just MLA - #6449

Merged
danielhanchen merged 1 commit into
mainfrom
mtp-target-copy-spec-gate
Jun 19, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
mtp-target-copy-spec-gate

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Follow-up to #6447. That PR reserves a duplicated f16 copy of the target model's KV context for MLA models under MTP speculative decoding (llama.cpp's ctx_tgt), which fixed the GLM-5.2 first-decode OOM. The reserve was gated only on MLA (kv_lora_rank present), but _estimate_mtp_overhead_bytes is also reached for the separate-drafter spec modes (draft-simple / draft-eagle3) via _user_draft_via_extras. Those modes load a small distinct drafter with its own KV (already counted in the draft KV + drafter weights) and keep no duplicated full target context; only MTP runs a second context over the target model's own KV geometry.

So on an MLA model, selecting --spec-type draft-simple or draft-eagle3 through the extra-args field charged a full ~main-KV-sized f16 copy (tens of GiB) that those modes never allocate, needlessly backing off the advertised context. That is the same under-advertising #6312 set out to fix, just in the opposite direction. It is in the safe direction (over-reserve, never OOM) but a real regression for that configuration.

Fix

Thread mtp_keeps_target_ctx through _estimate_mtp_overhead_bytes (True for MTP draft modes, False for the separate-drafter modes) and derive _engaged_is_mtp at the fit call site, so the target copy is added only when the engaged spec mode is actually MTP.

  • MLA + MTP (GLM-5.2 / DeepSeek / Kimi) is unchanged: the GLM-5.2 OOM fix is preserved.
  • Non-MLA MTP (Qwen / Gemma) was already unaffected (gated on kv_lora_rank) and stays so.
  • MLA + draft-simple / draft-eagle3 no longer pays the target copy.

Test

test_mtp_mla_target_ctx.py adds test_separate_drafter_mode_drops_target_copy: on an MLA backend the separate-drafter reserve collapses to exactly the draft KV (no target copy), while the default MTP path keeps draft KV + the full f16 target copy.

Addresses the Codex review on #6447 (r3437715720). Existing MTP budget + MLA reserve suites pass (134 tests).

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request updates the MTP overhead estimation logic in llama_cpp.py to distinguish between true MTP draft modes and separate-drafter speculative modes (such as draft-simple and draft-eagle3). It introduces the mtp_keeps_target_ctx parameter to avoid charging the duplicated target KV context overhead to separate-drafter modes, which do not maintain this copy. Additionally, a new unit test has been added to verify this behavior, and a minor lambda cleanup was performed. There are no review comments, so no further feedback is provided.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

_estimate_mtp_overhead_bytes is also reached for the separate-drafter spec modes
(draft-simple / draft-eagle3) through _user_draft_via_extras. Those modes load a
small distinct drafter with its own KV -- already counted in the draft KV +
weights -- and keep no duplicated full target context; only MTP runs a second
context over the target model's own KV geometry (llama.cpp ctx_tgt). Charging the
~main-KV-sized f16 copy there over-reserved by tens of GiB on an MLA model and
needlessly shrank the advertised context, the same under-advertising #6312 set
out to fix.

Thread mtp_keeps_target_ctx through _estimate_mtp_overhead_bytes (True for MTP,
False for separate-drafter modes) and derive _engaged_is_mtp at the fit call site
so the target copy is added only when the engaged mode is actually MTP. MLA + MTP
(GLM-5.2 / DeepSeek / Kimi) is unchanged, so the GLM-5.2 OOM fix is preserved;
non-MLA and the draft-simple / draft-eagle3 paths no longer pay the copy.

test_mtp_mla_target_ctx.py adds a case asserting the separate-drafter reserve
collapses to the draft KV (no target copy) while the default MTP path keeps it.
@danielhanchen
danielhanchen force-pushed the mtp-target-copy-spec-gate branch from e402555 to 2698f31 Compare June 19, 2026 12:45
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 2698f31eed

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 52877bb into main Jun 19, 2026
32 checks passed
@danielhanchen
danielhanchen deleted the mtp-target-copy-spec-gate branch June 19, 2026 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant