Skip to content

Studio: resolve the repo-root MTP drafter after the MTP/ GGUF rename - #7031

Merged
danielhanchen merged 5 commits into
mainfrom
mtp-drafter-root-select
Jul 9, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
mtp-drafter-root-select

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

The Gemma 4 QAT mobile GGUF repos renamed the higher-precision MTP drafter copies under MTP/:

Old name New name
MTP/gemma-4-E4B-it-BF16-MTP.gguf MTP/mtp-gemma-4-E4B-it-BF16.gguf
MTP/gemma-4-E4B-it-Q8_0-MTP.gguf MTP/mtp-gemma-4-E4B-it-Q8_0.gguf

The -MTP suffix was dropped and a mtp- prefix was added, so the MTP/ basenames now begin with the same mtp- prefix as the small repo-root drafter mtp-gemma-4-E4B-it.gguf (unchanged).

The problem

The drafter selectors filter candidates by a mtp- basename prefix and take the first in sort order. Under the new names every MTP/ copy also matches that filter, and because MTP/ (uppercase M) sorts ahead of the lowercase root mtp-..., selection flips to the largest copy, MTP/mtp-gemma-4-E4B-it-BF16.gguf (about 172 MB), instead of the root drafter (about 60 MB) that both functions document they should pick.

Effect when loading a Gemma 4 quant in Studio: speculative decoding still works and output is unchanged (the target verifies every drafted token), but Studio fetches the larger BF16 copy and reserves more VRAM than intended. On the Hub download path it attaches that copy into MTP/, where the loader's root-only local detection then cannot find it.

The fix

Restrict the drafter selection, and the companion byte estimate, to root-level mtp-*.gguf so the MTP/ copies stay explicit-selection only:

  • core/inference/llama_cpp.py _pick_mtp (loader auto-download)
  • hub/utils/gguf_plan.py preferred_mtp_sibling (Hub variant plans)
  • routes/inference.py _remote_gguf_companion_bytes (VRAM headroom estimate)

Also reuse a drafter already in the local cache before downloading, so a device that already holds a copy on disk (the root file, or an older MTP/ copy from a prior build) is reused rather than re-fetched.

Compatibility

Old-scheme repos keep working: their MTP/...-MTP.gguf copies have no root-level mtp- sibling to mis-select, so the root drafter is still chosen. The classification predicates are unchanged, so MTP/ copies remain excluded from the main-model and quant menus under both naming schemes.

Tests

studio/backend/tests/test_mtp_drafter_companion.py:

  • New-scheme predicate case, plus preferred_mtp_sibling and build_gguf_variant_plans asserting the root drafter wins over the new MTP/ copies.
  • _pick_mtp selection pinned through the companion-download seam.
  • On-disk reuse: reuse the cached root drafter, and fall back to a cached MTP/ copy when no root file is present.
34 passed

Adjacent MTP and llama.cpp suites run clean (800 passed alongside; the single unrelated wait_for_health httpx monkeypatch failure reproduces identically on main).

danielhanchen and others added 2 commits July 9, 2026 11:03
The Gemma 4 QAT GGUF repos renamed the higher-precision MTP/ subdir
copies from gemma-4-...-<quant>-MTP.gguf to mtp-gemma-4-...-<quant>.gguf,
so their basenames now start with the same mtp- prefix as the small
repo-root drafter (mtp-gemma-4-E4B-it.gguf).

The drafter selectors filtered candidates by a mtp- basename prefix and
took the first in sort order. With the new names the MTP/ copies also
match, and because MTP/ (uppercase) sorts before the lowercase root file,
selection flipped to the large BF16 copy under MTP/ instead of the root
drafter both functions document they should pick.

Restrict both selectors, and the companion byte estimate, to root-level
mtp-*.gguf so the MTP/ copies stay explicit-selection only:
- core/inference/llama_cpp.py _pick_mtp (loader auto-download)
- hub/utils/gguf_plan.py preferred_mtp_sibling (Hub variant plans)
- routes/inference.py _remote_gguf_companion_bytes (VRAM headroom)

Also reuse a drafter already in the local cache before downloading, so a
device that already holds a copy on disk does not re-fetch it.

Old-scheme names keep working (they have no root-level mtp- sibling to
mis-select). Adds regression tests for the new naming, both selection
paths, and the on-disk reuse.

@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 drafter resolution and download logic to ensure that the small repo-root drafter is preferred over the higher-precision subdirectory copies under MTP/ (which now share the mtp- prefix). It also introduces local cache reuse via _cached_repo_mtp_drafter to avoid unnecessary re-downloads. The review feedback correctly identifies a platform-compatibility issue on Windows, where path checks using only forward slashes (/) will fail to detect subdirectory files, and provides a code suggestion to handle backslashes as well.

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.

if not drafters:
continue
root = sorted(f for f in drafters if "/" not in f)
chosen = snap / (root[0] if root else sorted(drafters)[0])

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.

high

On Windows, local paths returned by scanning the HF cache directory may contain backslashes (\) instead of forward slashes (/). Using only '"/" not in f' to identify root-level files will fail on Windows, causing subdirectory files (like MTP\mtp-...) to be incorrectly classified as root-level files. Since uppercase 'M' sorts before lowercase 'm', this would cause the larger subdirectory drafter to be selected over the root drafter on Windows, defeating the purpose of this fix. To ensure platform independence, check for both forward and backward slashes.

Suggested change
chosen = snap / (root[0] if root else sorted(drafters)[0])
root = sorted(f for f in drafters if "/" not in f and "\\" not in f)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The candidate paths here come from _gguf_snapshot_files, which returns Path.relative_to(snapshot).as_posix(), so they use forward slashes on every OS including Windows; the online path likewise consumes list_repo_files, whose repo-relative paths are always posix. So a backslash-separated MTP\... can never reach this filter and the '/' check is sufficient. Verified by seeding an MTP/ copy in a snapshot: the returned path is MTP/mtp-...gguf (no backslash) and is correctly excluded.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 400cdb9e74

ℹ️ 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".

Comment on lines +4717 to +4720
cached = self._cached_repo_mtp_drafter(hf_repo)
if cached:
logger.info(f"Reusing cached MTP drafter: {cached}")
return cached

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid reusing stale cached MTP drafters online

Because this cache probe runs before _download_companion_gguf, online loads return the first cached drafter from any HF snapshot and never let hf_hub_download check the current revision/etag. If a Gemma GGUF repo updates mtp-gemma-...gguf under the same filename, _download_gguf can fetch the new main weights while this path keeps pairing them with a stale cached draft (or old MTP/ copy), which can make llama-server load an incompatible drafter; limit this reuse to offline mode or verify the cached file is from the current revision.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5aec80f: the cache-reuse probe is now gated to offline mode. Online loads go back through _download_companion_gguf/hf_hub_download, which resolves the current revision and refetches the drafter when its etag changed, so freshly downloaded weights are never paired with a stale cached draft. This matches the offline-only cross-snapshot reuse already used for the main GGUF in _download_gguf.

Reuse the cached drafter only when HF is offline. Online, route back
through _download_companion_gguf/hf_hub_download so the current revision
is checked (etag) and a changed drafter is refetched, matching the
offline-only cross-snapshot reuse already used for the main GGUF. This
avoids pairing freshly downloaded weights with a stale cached draft.
Make the reuse tests offline and add an online-skips-reuse test.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Jul 9, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5aec80f476

ℹ️ 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".

Comment on lines +4691 to +4692
root = sorted(f for f in drafters if "/" not in f)
chosen = snap / (root[0] if root else sorted(drafters)[0])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Prefer root drafters across cached snapshots

When HF_HUB_OFFLINE is set and the HF cache has multiple snapshots, this chooses the first snapshot that contains any drafter and only prefers the root mtp-*.gguf within that single snapshot. If a newer partial/pre-fix snapshot contains only MTP/mtp-...-BF16.gguf while an older complete snapshot still has the small repo-root drafter, offline loads will return the large MTP/ copy, reintroducing the VRAM/download regression this change is trying to avoid. Scan all snapshots for root drafters before falling back to subdir copies.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5fd0d3b: _cached_repo_mtp_drafter now collects drafters across all cached snapshots and prefers any repo-root mtp-*.gguf before falling back to an MTP/ copy, so a partial newer snapshot can no longer shadow the small root drafter in an older one. Added test_download_mtp_prefers_root_across_snapshots_offline to pin it.

Offline reuse scanned snapshots one at a time and returned the first
snapshot that held any drafter, only preferring root within it. A newer
partial snapshot with just the MTP/ copy could shadow the small root
drafter in an older snapshot. Collect drafters across all snapshots and
prefer any repo-root file before an MTP/ copy.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5fd0d3b806

ℹ️ 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".

(roots if "/" not in f else subdirs).append(snap / f)
# Root before any MTP/ copy, across every snapshot, so a partial newer
# snapshot can't shadow the small root drafter in an older one.
for cand in sorted(roots) + sorted(subdirs):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve HF snapshot order for cached drafters

When offline cache contains multiple snapshots for the same repo and more than one has a root mtp-*.gguf, this sorts the absolute snapshot paths and can pick an older drafter even though utils.models.model_config._iter_hf_cache_snapshots deliberately yields snapshots newest-first and _download_gguf resolves the main model from that order. In that situation the load can pair a newer cached main GGUF with a stale MTP drafter from another revision; keep the collected root candidates in snapshot order while still preferring roots over MTP/ copies.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in fcf3360: the cross-snapshot collection now preserves _iter_hf_cache_snapshots newest-first order (root still preferred over MTP/ copies) instead of sorting by snapshot path, so the reused drafter comes from the same newest snapshot the main GGUF is resolved from. Added test_download_mtp_reuse_follows_snapshot_order_offline.

Collecting root candidates and sorting by absolute snapshot path could
pick a drafter from an older snapshot. _iter_hf_cache_snapshots yields
newest first and the main GGUF is resolved in that order, so preserve it
(root still preferred over MTP/ copies) to avoid pairing a fresh main
weight with a stale drafter revision.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: fcf33602b7

ℹ️ 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 b5aef63 into main Jul 9, 2026
48 checks passed
@danielhanchen
danielhanchen deleted the mtp-drafter-root-select branch July 9, 2026 13:46
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