Skip to content

Studio: harden DSpark/MTP drafter detection and companion reclaim - #8037

Merged
danielhanchen merged 7 commits into
mainfrom
studio-dspark-drafter-detection-fixes
Aug 7, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
studio-dspark-drafter-detection-fixes

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up to #7968. Three drafter-detection bugs found while reviewing that PR after it merged, each with a regression test that fails without its fix.

detect_dspark_file accepted a sidecar it cannot open

It never checked that the candidate is a readable file, where detect_mtp_file always has. Two shapes reach it:

dangling snapshot symlink:   dspark -> .../missing_blob     mtp -> None
a DIRECTORY named like one:  dspark -> .../dspark-...gguf   mtp -> None

llama-server then gets a --model-draft it cannot open, which fails the whole load rather than falling back to no speculative decoding. Now guarded with launch.is_file(), which follows the link and so covers both.

A base-family sidecar attached to a longer-named sibling's weights

_drafter_matches_weight requires the drafter's family to prefix the weight at a non-alphanumeric boundary. That blocks one direction of a DeepSeek-V4-Flash / DeepSeek-V4-Flash-0731 pair but not the other:

mtp-DeepSeek-V4-Flash-BF16.gguf      -> DeepSeek-V4-Flash-0731-Q2_K-...gguf   True
mtp-DeepSeek-V4-Flash-0731-BF16.gguf -> DeepSeek-V4-Flash-Q2_K.gguf           False

Both are real Hub filenames, and ggml-org/DeepSeek-V4-Flash-GGUF and ggml-org/DeepSeek-V4-Flash-0731-GGUF are flat repos, so one scan folder for both hits it.

The prefix rule cannot be tightened to equality: unsloth/gemma-4-12B-it-qat-GGUF really does ship mtp-gemma-4-12B-it.gguf beside gemma-4-12B-it-qat-*.gguf. So the remaining direction is settled by ranking instead, with a new _drafter_stem_rank putting the longest matching family first in both detectors. The qat pairing has its own negative-control test.

The deletion predicates never saw the directory

_repo_file_matches passed file_name, which huggingface_hub sets to file_path.name and our own recovery scan to entry.name. Both are basenames, so dspark/ and MTP/ were invisible to every predicate keyed on the parent directory.

Harmless for dspark-<model>.gguf, whose basename carries the marker. Not harmless for the other published scheme, <model>-dspark.gguf: its basename looks like an ordinary Q8_0 quant, so deleting the genuine Q8_0 variant also unlinked the sidecar the Q4_K_M still needed, and _has_remaining_main_gguf never went false, so no companion could ever be reclaimed.

_repo_file_matches now rebuilds the snapshot-relative path. Quant labels are unchanged by the prefix (extract_quant_label returns UD-Q4_K_XL either way), so only the directory-keyed answers move.

The test fixture was also passing relative paths as file_name, which is why this never showed up; it now mirrors production and the new test fails without the fix (Deleted 2 file(s) ... variant Q8_0).

Testing

727 backend tests pass across the detection, deletion, picker, offline-cache and training-guard suites. Each fix was confirmed to fail its test when reverted.

@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: 2922fd365e

ℹ️ 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 +1813 to +1815
return (
_drafter_stem_rank(candidate.name, kind = "mtp"),
_drafter_total_size(candidate),

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 Apply specificity ranking to root-level MTP candidates

The new family-specificity key is used only when sorting candidates collected from MTP/; the root-level branch below still returns the first matching sorted(d.iterdir()) entry. Consequently, when the longer family does not happen to sort first—for example mtp-model.gguf and mtp-model_v2-Q8_0.gguf beside model_v2-Q4_K_M.gguf, or when the base candidate is in start_dir and the exact candidate is in search_root—detect_mtp_file still launches the base-family drafter despite _drafter_stem_rank identifying the exact one. Collect and rank root candidates as well before returning.

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.

Correct, and it means my own test for that fix was passing by luck: mtp-DeepSeek-V4-Flash-0731-Q8_0.gguf happens to sort ahead of mtp-DeepSeek-V4-Flash-Q8_0.gguf because 0 precedes Q. Your counterexample does not:

model_v2-Q4_K_M.gguf  +  mtp-model.gguf  +  mtp-model_v2-Q8_0.gguf
picked: mtp-model.gguf   (want mtp-model_v2-Q8_0.gguf)

Fixed in b7dc1c3: the root branch now collects every name-and-family match across both scanned directories, sorts by _drafter_stem_rank, and only then runs the file / _launchable / accept checks in that order. The sort is stable, so equal specificity keeps the previous directory order and nothing else about root preference moves.

Two tests, both failing before the change: the in-folder case above, and the cross-directory one you raised, where the base sits beside the weight and the exact match only in search_root.

detect_dspark_file was already safe here since it collects into one list and ranks with _rank, which carries the same key.

@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: b7dc1c377f

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

continue
if accept is not None and not accept(launch):
root_candidates.append(f)
for f in sorted(root_candidates, key = lambda c: _drafter_stem_rank(c.name, kind = "mtp")):

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 Rank specificity before preferring root MTP copies

In a multi-model directory where mtp-model.gguf is at the scan root but the matching MTP/mtp-model_v2-Q8_0.gguf is in the companion subdirectory beside model_v2-Q4_K_M.gguf, this loop still returns the less-specific root drafter before subdirectory candidates are even collected. Fresh evidence beyond the earlier root-to-root report is this cross-tier case: family specificity is only applied within each layout, so collect or compare both tiers before returning while retaining root preference only for equally specific families.

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.

Confirmed, and my previous fix only closed the within-tier half:

model_v2-Q4_K_M.gguf  +  mtp-model.gguf  +  MTP/mtp-model_v2-Q8_0.gguf
picked: mtp-model.gguf   (want mtp-model_v2-Q8_0.gguf)

The root loop returned before the companion directory was ever scanned.

Fixed in cfe9737. Both tiers are now collected first, each keeps its own internal ordering (root by specificity then scan order, subdir by _smallest_first), and the tier order is decided by the better specificity of the two:

order = (
    (_emit_subdir, _emit_root)
    if _best_rank(subdir_candidates) < _best_rank(root_candidates)
    else (_emit_root, _emit_subdir)
)

_best_rank defaults to 0, the worst score, so an empty tier never wins. Root therefore keeps its preference at equal specificity, which is every published layout, and loses only where the root file names a different family and so is not this weight's drafter at all. accept still falls through to the second tier, so a caller with extra rules keeps scanning.

Two tests: the cross-tier case above, and a negative control that mtp-model.gguf still beats MTP/model-Q8_0-MTP.gguf for model-Q4_K_M.gguf. 655 tests pass across the detection, picker, offline-cache and resolver suites.

@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: cfe97370b0

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

# the root file names a different family and is not this weight's drafter.
order = (
(_emit_subdir, _emit_root)
if _best_rank(subdir_candidates) < _best_rank(root_candidates)

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 Rank only launchable MTP candidates across tiers

When the root contains a dangling or incomplete release-specific candidate plus a usable base-family candidate, _best_rank(root_candidates) still uses the unusable candidate to put the root tier first; _emit_root then skips that candidate and returns the base-family drafter before checking a more-specific usable copy under MTP/. For example, beside model_v2_release-*.gguf, a broken mtp-model_v2_release.gguf, usable mtp-model.gguf, and usable MTP/mtp-model_v2-Q8_0.gguf select mtp-model.gguf. Filter root candidates for launchability before comparing tier ranks, or compare the first actually emittable candidate from each tier.

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.

Reproduced exactly as described:

model_v2_release-Q4_K_M.gguf
  mtp-model_v2_release.gguf   dangling link, most specific
  mtp-model.gguf              usable, least specific
  MTP/mtp-model_v2-Q8_0.gguf  usable, more specific
picked: mtp-model.gguf   (want mtp-model_v2-Q8_0.gguf)

The broken candidate spoke for the root tier in the comparison and was then skipped at emission.

Fixed in 23cb2c2 by taking your first option: root candidates are filtered for is_file() and _launchable() at collection, so only launchable files reach _best_rank or the emit loop. That also makes the two tiers symmetric, since the subdir tier has always filtered at collection. The now-redundant check inside _emit_root is gone; the try/except OSError around _drafter_launch_path stays, for a file that disappears between collection and emission.

accept is deliberately not part of the comparison: it is caller-specific, a rejection is meant to keep scanning, and if it rejects everything in the chosen tier the other tier is still tried.

test_an_unusable_candidate_does_not_win_the_tier_comparison pins the case above. 656 tests pass.

@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: 23cb2c2364

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

# the root file names a different family and is not this weight's drafter.
order = (
(_emit_subdir, _emit_root)
if _best_rank(subdir_candidates) < _best_rank(root_candidates)

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 Exclude rejected candidates from tier ranking

For a native-grant-backed load, _best_rank still considers candidates that the accept callback will reject. If the root tier contains a most-specific out-of-grant candidate, a less-specific accepted root candidate, and MTP/ contains an accepted candidate of intermediate specificity, line 1930 chooses the root tier based on the rejected file; _emit_root then skips that file and returns the base-family drafter without considering the more-specific accepted subdirectory copy. Rank the first acceptable candidate in each tier, just as this change now filters unlaunchable candidates before comparing tiers.

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.

You are right and I was wrong to rule this out last round. I argued accept should stay out of the comparison because a rejection keeps scanning; that is true within a tier but does not save the tier comparison, which is exactly the failure you describe. Reproduced:

model_v2_release-Q4_K_M.gguf
  mtp-model_v2_release.gguf   rejected by accept, most specific
  mtp-model.gguf              accepted, least specific
  MTP/mtp-model_v2-Q8_0.gguf  accepted, more specific
picked: mtp-model.gguf   (want mtp-model_v2-Q8_0.gguf)

Fixed in 0472c51 by taking your second option, which subsumes the launchability filter as well: each tier is now represented by the first candidate that survives every filter, i.e. the one it would really launch.

def _first_pick(candidates: list[Path]) -> Optional[tuple[int, str]]:
    for c in candidates:
        try:
            launch = _drafter_launch_path(c)
        except OSError:
            continue
        if accept is not None and not accept(launch):
            continue
        return _drafter_stem_rank(c.name, kind = "mtp"), launch
    return None

Ranks are then compared between those two picks, with root still winning ties. This also removes the _emit_root / _emit_subdir / _best_rank trio in favour of one helper, so there is a single place where "what would this tier launch" is decided.

Three cases verified against the same tree: the accept skew above, the dangling-link skew from the previous round, and the negative control that mtp-model.gguf still beats MTP/model-Q8_0-MTP.gguf at equal specificity. 734 tests pass.

@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: 0472c516eb

ℹ️ 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 added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 6, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 6, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 6, 2026
@danielhanchen
danielhanchen merged commit c61e6e1 into main Aug 7, 2026
49 of 50 checks passed
@danielhanchen
danielhanchen deleted the studio-dspark-drafter-detection-fixes branch August 7, 2026 04:04
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.

2 participants