Repository navigation
Studio: reserve the duplicated MTP target KV context for MLA models (GLM-5.2 OOM) - #6447
Conversation
GLM-5.2 UD-IQ1_S advertised its native 1,048,576-token context, loaded, then crashed cublasCreate on the first generation with "CUDA error: the resource allocation failed" on a 2x B200 box. The model loaded fine; the decode OOMed. Cause: when MTP speculative decoding is engaged, llama.cpp keeps a second full copy of the target model's KV context for draft verification (ctx_tgt=yes in the spec log), at f16. On an MLA model that copy is ~the main KV again -- for GLM-5.2 at 1M ctx llama.cpp sized it at ~97.5 GiB -- but the auto-fit reserve only counted the tiny embedded draft head (~2 GiB), 46x too low. So weights (~202 GiB) + main KV (~83 GiB) + a 2 GiB reserve looked like it fit in 2x182 GiB, when the real footprint with the ~97 GiB MTP copy is ~382 GiB and overruns the cards. Disabling speculative decoding removed the copy and the same context ran fine. _estimate_mtp_overhead_bytes now adds the duplicated target context (the main KV re-estimated at f16) for MLA models, so auto-fit backs the context off (or selects more GPUs) instead of advertising one that OOMs. It is gated strictly on MLA (kv_lora_rank present), which is exactly the family that keeps the extra copy (GLM-5.x, DeepSeek, Kimi-K2); non-MLA MTP (Qwen, Gemma) is byte-for-byte unchanged. The reserve stays deterministic from GGUF dims, matching #6312. test_mtp_mla_target_ctx.py covers it: the MLA reserve includes the f16 target copy and dominates the draft head, the copy is f16 regardless of the main cache type and scales with context, non-MLA embedded heads keep overhead == draft KV, and _fit_context_to_vram on the GLM-5.2 / 2x B200 budget now returns a context below the requested 1M where the old draft-only reserve kept the full 1M.
There was a problem hiding this comment.
Code Review
This pull request updates the MTP draft reserve estimation for MLA models (such as GLM-5.x, DeepSeek, and Kimi-K2) in llama_cpp.py to include a duplicated target KV context copy at f16, preventing out-of-memory (OOM) errors during draft verification. It also introduces comprehensive unit tests in test_mtp_mla_target_ctx.py to validate this overhead calculation and ensure non-MLA models remain unaffected. There are no review comments, so I have no feedback to provide.
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.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 21dae29241
ℹ️ 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".
|
|
||
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) | ||
|
|
||
| from core.inference.llama_cpp import ( # noqa: E402 |
There was a problem hiding this comment.
Stub app deps before importing llama_cpp
When this new pure unit test is collected before test_mtp_vram_budget.py (or run directly) in the lightweight test environment, this import goes through core.inference.__init__ and reaches orchestrator.py, which imports structlog; because this file does not install the same structlog/loggers stubs used by the neighboring MTP budget tests, collection aborts with ModuleNotFoundError: No module named 'structlog' before any tests run. Add the local stubs before this import or move them into shared test setup so the file is order-independent.
Useful? React with 👍 / 👎.
…independent The new test imports core.inference.llama_cpp, which pulls in orchestrator -> structlog. In the lightweight test env structlog is absent, so when this file is collected before test_mtp_vram_budget.py (it sorts first) or run directly, collection aborted with ModuleNotFoundError. Install the same loggers/structlog (+ conditional httpx) stubs the sibling MTP tests use before the import, matching the established per-file convention.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 787be38205
ℹ️ 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".
| if self._kv_lora_rank is not None: | ||
| target_ctx_copy = self._estimate_kv_cache_bytes(n_ctx, "f16", n_parallel = n_parallel) |
There was a problem hiding this comment.
Gate the target-KV copy to MTP spec mode
When extras select --spec-type draft-simple or draft-eagle3 with a separate drafter, _user_draft_via_extras still routes that load through this overhead function, but upstream --spec-type help separates draft-simple (simple draft model) from draft-mtp (MTP heads). This MLA-only addition therefore charges a full f16 target-KV copy for non-MTP draft modes, which can cut the advertised context by tens of GiB even though those modes only need the separate drafter's reserve; pass the effective spec type here and add the copy only for draft-mtp/MTP.
Useful? React with 👍 / 👎.
Summary
Loading
unsloth/GLM-5.2-GGUF:UD-IQ1_Sin Studio advertised the model's native 1,048,576-token context, loaded successfully, then crashed on the first generation:The weights fit; the decode OOMed. This makes the GGUF auto-fit reserve account for a cost it was missing on MLA models, so the chosen
-cactually holds at runtime.Cause
When MTP speculative decoding is engaged, llama.cpp keeps a second full copy of the target model's KV context for draft verification (
ctx_tgt=yesin the spec log), allocated at f16 regardless of the main cache type. On an MLA model (compressed latent KV) that copy is ~the main KV again. The spec log sized it directly:But the auto-fit MTP reserve (#6312) only counted the embedded draft head — for GLM-5.2 at 1M ctx that is ~2 GiB, 46x too low. So the budget math looked like it fit on 2x B200:
Disabling speculative decoding removes the copy, and the same 1M context runs fine — which confirmed MTP as the culprit.
#6312 made the MTP reserve byte-accurate, but it was measured on Qwen3.6 MTP (non-MLA, embedded head, ~512 MiB), which keeps no such target copy. The duplicate context only shows up on MLA models.
Fix
_estimate_mtp_overhead_bytesnow adds the duplicated target context — the main KV re-estimated at f16 — when the model is MLA. Auto-fit then backs the context off (or selects more GPUs) instead of advertising one that OOMs.kv_lora_rankpresent): exactly the family that keeps the extra copy (GLM-5.x, DeepSeek, Kimi-K2). Non-MLA MTP (Qwen, Gemma) is byte-for-byte unchanged.Tests
studio/backend/tests/test_mtp_mla_target_ctx.py:overhead == draft KV(no copy)._fit_context_to_vramon the GLM-5.2 / 2x B200 budget returns a context below the requested 1M, where the old draft-only reserve kept the full 1M.All 563 existing MTP/KV/context-fit tests still pass (
test_mtp_vram_budget,test_kv_cache_estimation,test_llama_cpp_context_fit,test_llama_cpp_max_context_threshold,test_mtp_drafter_companion,test_llama_cpp_mtp_detection).