Skip to content

Unsloth Studio: re-fit the context window when MTP speculative decoding reserves extra memory - #9572

Closed
danielhanchen wants to merge 5 commits into
mainfrom
fix-9550-mtp-context-refit
Closed

danielhanchen wants to merge 5 commits into
mainfrom
fix-9550-mtp-context-refit

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Aug 23, 2026 •

Copy link
Copy Markdown
Member

Problem

From #9550, the same 31.0 GB model loaded twice:

# Auto: the drafter does not fit, so it is dropped and the context is fit WITHOUT it
Speculative decoding disabled for this load: ... needs 46.7 GB of a 44.3 GB budget
Context auto-reduced: 262144 -> 112896 (model: 31.0 GB, est. KV cache: 11.2 GB)
GGUF size: 31.0 GB, ..., context: 112896, GPUs free: [(0, 45914), (1, 8032)], selected: [0]

# The user then picks MTP in the dropdown and reloads
GGUF size: 31.0 GB, ..., MTP reserve: 2.18 GB (draft KV @ 112896 + verify n_max=2),
context: 112896, GPUs free: [(0, 45914), (1, 8032)], selected: [0, 1]

The second load keeps 112896 and pays the reserve on top, pulling in the 8 GiB card, instead of re-deriving a context that fits with the reserve.

  1. The Auto drafter-drop probe clears mtp_overhead_fn, so the context it settles on was fit with no reserve in it.
  2. A same-model reload replays that resolved context as max_seq_length (resolveLoadMaxSeqLength returns loadedContextLength), which makes explicit_ctx true, so the planner honors it verbatim and spends the shortfall on another GPU.

The replay itself is needed (sending 0 on a pinned reload would ask for the native context), so it stays.

Fix

  • resolveLoadMaxSeqLengthDetailed returns the value plus where it came from; resolveLoadMaxSeqLength is a thin wrapper, so every existing caller is unchanged.
  • LoadRequest.max_seq_length_auto_derived (default False) carries that provenance to GgufLoadIntent.n_ctx_auto_derived. Old clients keep verbatim honoring.
  • Inside if explicit_ctx:, the context is re-fit only when it was ours, the drafter is forced (dropdown or advanced arguments, i.e. anything that bypasses the Auto drop probe), and the reserve costs GPU memory. The re-fit prices the cards the context was originally fit for, so the drafter shrinks the context instead of adding a card. It only ever moves downwards.

With the reporter's numbers:

Context auto-reduced: 262144 -> 122624 (model: 31.0 GB, est. KV cache: 12.2 GB)
Context re-fit for the forced drafter: 122624 -> 100864 (reserve 1.95 GB)
... context: 100864, GPUs free: [(0, 45914), (1, 8032)], selected: [0]

A context the user typed, Auto speculation, and loads with no reserve all take the existing path unchanged.

Tests

  • studio/backend/tests/test_mtp_context_refit_9550.py: the reserve costs context, Auto still drops the drafter, forcing MTP (dropdown and --spec-type draft-mtp in advanced arguments) re-fits, and a user-typed context is still honored verbatim. Disabling the re-fit fails the forced cases.
  • studio/frontend/tests/load-max-seq-length-source.test.ts: provenance per branch.
  • Related backend suites (context fit, placement, mmproj, tensor parallel, GPU memory mode, native context, MTP budget/detection/drafter): 1712 passed on main, 1717 passed on this branch (the 5 new tests), 0 failed. Frontend: tsc clean, 9322/9322 node tests pass.

Fixes #9550

…ver priced

Auto drops a drafter that does not fit rather than shrink the context for it,
and clears the MTP overhead when it does, so the context it settles on is fit
against a budget with no reserve in it. Reloading the same model replays that
resolved context as max_seq_length, which makes it explicit, and forcing the
drafter in the dropdown re-arms the reserve. The explicit branch then spends
the shortfall on another GPU instead of on the context that never accounted
for it, which is the reporter's 31 GB model taking a second card to hold a
2.18 GB drafter at 112896.

Reproduced with his numbers: 262144 -> 122624 under Auto with the drafter
dropped, then the forced load re-fits to 100864 and stays on one card.

The value the client sends does not change, only its provenance:
resolveLoadMaxSeqLength gained a detailed variant naming the branch that
supplied it, and the same-model replay is reported as
max_seq_length_auto_derived. The dedupe comparison, the staged preset config
and the settings sheet all keep reading the same number.

The re-fit is gated on three things together -- the context was ours, the
speculative choice is forced rather than Auto, and the reserve costs GPU
memory -- so a context the user typed is still honored verbatim, Auto still
keeps its context and drops the drafter, and a load with no reserve is
untouched. It prices the cards the context was fit for rather than the whole
pool, since a pooled budget would find room for the reserve and leave the
extra card in place, which is the thing being complained about.

Fixes #9550
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@LeoBorcherding LeoBorcherding changed the title Studio: re-fit the context when a forced drafter adds a reserve it never priced Unsloth Studio: re-fit the context window when MTP speculative decoding reserves extra memory Sep 25, 2026
@danielhanchen danielhanchen self-assigned this Sep 28, 2026
Resolve preset-policy.ts / use-chat-model-runtime.ts conflicts against main's
loadedContextLength / pinnedMaxSeqLength arguments. Re-fit now reads main's
_fit_ctx_checkpoints (the old _effective_ctx_checkpoints is bound later on main),
prices the base cards with _subset_model_size and per-device compute buffers,
selects the base placement at the unreserved fraction, and treats a drafter
forced through advanced arguments as forced. Trim comments.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 28, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 28, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-09-28T15:27:18.648953Z 3288ea0 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@NilayYadav NilayYadav added lora save / load Inconsistent results after `save_lora` / `load_lora` and removed lora save / load Inconsistent results after `save_lora` / `load_lora` labels Sep 28, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

Closing in favour of #12231, which fixes #9550 by sending a replayed context back through the normal fit when a drafter is forced. This PR re-derived the original GPU set with a stricter selector than the one Auto used, so on real hardware it priced both cards and never shrank the context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] AMD: Unsloth Studio drops MTP to fit context, and turning MTP back on doesn't re-fit (W7900 + W7500)

2 participants