Skip to content

Studio: load a repo id from its scan-folder copy instead of re-downloading it - #13034

Merged
danielhanchen merged 12 commits into
mainfrom
studio-load-scan-folder-copy
Oct 8, 2026
Merged

danielhanchen merged 12 commits into
mainfrom
studio-load-scan-folder-copy

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Fixes #12253. Takes over #12254 (thanks @VTplus, your commit is kept as is with authorship) and adds a guard so ordinary Hugging Face cache loads are not affected.

Problem

POST /api/inference/load with a repo id such as unsloth/Devstral-Small-2-24B-Instruct-2512-GGUF only checks the active Hugging Face hub cache. If the complete repo lives in a registered scan folder (Hub, On Device), Studio does not look there and starts a full download into the hub cache, a byte-identical duplicate of weights already on disk. In the report this was a 16.8 GB download that stalled the load for over 25 minutes.

Fix

  1. _as_local_scan_folder_request (from Studio: resolve a repo-id load to the local scan-folder copy instead of downloading it #12254) runs right after the Ollama manifest rewrite in _load_model_impl. It asks the same local index the OpenAI auto-switch path uses (resolve_local_gguf). On a hit, the request is rewritten to the local path and quant; on a miss, nothing changes.

  2. New in this PR: that index also covers the active hub cache, and it is consulted before scan folders. Without a guard, every normal repo-id load of an already cached model was rewritten from org/name to its snapshot path, which changes the loaded model identity for the most common load path. The rewrite is now skipped when the hit lives inside the active hub cache, since the existing remote path already reuses that copy without downloading. Only copies the hub path cannot serve are rewritten, and only when the hit is a GGUF snapshot (models--org--name/snapshots/<rev>) of the requested repo, so its path maps back to the same org/name for status, /v1 ids, unload and the remote-code consent scan. LM Studio style folders and non-GGUF checkpoints keep the existing remote path.

  3. The same rewrite runs in POST /api/inference/validate. The chat flow validates before it loads, and without Hub access validate probed the repo id remotely and refused it as not cached, so /load was never sent. The validate response the frontend reads (flags such as is_lora) is unaffected; /load still receives the picked repo id.

  4. The rewrite runs only for the owner's own Studio session, the same rule the cached weights path uses (_refused_repo_cached_gguf): API key and managed account callers keep the repo id and its Hub access check. A rewritten load answers with the requested repo id in LoadResponse.model, which the chat UI keeps as the selected checkpoint; the snapshot path is only used to load.

  5. The bundled chat template matcher reads an HF cache snapshot path (models--org--name/snapshots/<rev>) as its repo id, so a scan-folder copy, a reload by the path /status publishes, and a cache row pinned to its snapshot all get the same bundled template (gemma-4 preserve_thinking) as loading org/name. Other local paths still never match.

Unchanged: bare names, absolute paths, Ollama refs, API key and managed account callers, native path leases, resolver errors, and a pinned quant the local copy does not hold (that one still goes remote rather than loading different weights). The rewrite is idempotent: a rewritten request carries a path, which the helper skips.

Behavior

Case Before After
Repo only in a registered scan folder Full download into the hub cache Loads the scan-folder copy
Same, pinned quant present Full download Loads that quant locally
Same, pinned quant missing locally Remote Remote (unchanged)
Repo in the active hub cache Repo id, no download Repo id, no download (unchanged)
Repo in both hub cache and scan folder Repo id, hub copy Repo id, hub copy (unchanged)
Empty hub skeleton plus complete scan-folder copy Download Loads the scan-folder copy
Validate, repo only in a scan folder, Hub unreachable 400, not in the local cache Valid

The table was produced against a real resolver index (temporary hub cache plus a registered scan folder, no resolver mocks), base main vs this branch.

Tests

studio/backend/tests/test_load_scan_folder_resolution.py: the 9 helper tests from #12254 plus real-index tests (validate offline, a scan folder nested inside the hub cache, scan-folder only copy with and without a pinned quant, active hub cache copy with and without a scan-folder duplicate, weightless hub skeleton). The hub-cache tests fail without the guard and the validate test fails without the validate rewrite; all pass with them.

pytest studio/backend/tests/test_load_scan_folder_resolution.py studio/backend/tests/test_gemma4_chat_template_override.py studio/backend/tests/test_inference_status_loaded_gguf.py studio/backend/tests/test_inference_status_route.py studio/backend/tests/test_account_local_model_resolver.py studio/backend/tests/test_ollama_manifest_load_resolution.py studio/backend/tests/test_cached_gguf_routes.py studio/backend/tests/test_local_model_format.py
453 passed

Before and after in Studio

Two fresh Studio installs (base main and this branch), Hub offline, empty hub cache, and a registered scan folder holding a Hugging Face layout copy of unsloth/Qwen3-0.6B-GGUF (Q4_K_M). Both sides send POST /api/inference/load for unsloth/Qwen3-0.6B-GGUF, then open the chat page.

Before: load fails and no model is selected. After: Qwen3-0.6B-GGUF Q4_K_M is loaded from the scan folder.

Before After
Load status 400 Bad Request loaded
LoadResponse.model none unsloth/Qwen3-0.6B-GGUF
Active model (/api/inference/status) none unsloth/Qwen3-0.6B-GGUF
Bytes written to the hub cache 0 0

Known limitations

  • If the hub cache holds one quant and only the scan folder holds another, pinning the second still downloads it. Same as today.
  • A model already resident under its repo id reloads once if the same id now resolves to a scan-folder path. Same weights, no download.

VTplus and others added 2 commits October 8, 2026 02:44
… remote path

The local resolver index also covers the active Hugging Face cache, so the scan-folder
rewrite fired for every ordinary cached repo-id load and swapped its identity for a
snapshot path. Rewrite only when the hit lives outside the active hub cache, and cover
source precedence with real-index tests instead of a mocked resolver.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 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-10-08T14:08:13.206616Z 0940aac New commits
🔒 Security Review ⚠️ Failed 2026-10-08T09:46:19.886190Z e5c1abd PR opened
ℹ️ 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.

The chat flow calls /validate before /load. Offline, validate probed the repo id
remotely and refused it as not cached, so the load never ran even though the weights
were in a registered scan folder. Apply the same rewrite in validate.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 8, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 8, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…e active hub cache

Take the local copy only when its snapshot path maps back to the requested repo id, so
status, unload and the remote-code consent scan keep one identity; LM Studio style dirs
and non-GGUF checkpoints keep the existing remote path. Compare the copy's repo directory
with the active hub cache directly, so a scan folder nested inside the cache is still
loaded from disk.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…he requested repo id

API key and managed callers may not read the owner's copies (same rule as the cached
weights path), so they keep the repo id and its Hub check. A rewritten load reports the
repo id it was asked for in LoadResponse.model, which the chat UI stores as the
checkpoint. Guard tests now use pytest.fail so the helper's broad except cannot hide
a consulted resolver.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@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: 2cf0df9ed6

ℹ️ 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 +8829 to +8831
wanted = f"{identifier}:{request.gguf_variant}" if request.gguf_variant else identifier
try:
resolved = resolve_local_gguf(wanted)

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 Search later local copies for the requested quant

When the active Hub cache already contains this repo but lacks the explicitly requested quant, while a registered scan folder contains that quant, resolve_local_gguf(wanted) cannot find the scan-folder copy. _build_index() processes the active cache before custom scan folders and uses setdefault, so only the active cache's entry is retained for the repo ID; resolving repo:Q8_0 therefore returns None if that entry only has Q4, even when the later scan-folder entry has Q8. The request remains a repo ID and downloads Q8 again (or fails offline), defeating this change for mixed-cache repositories; the lookup needs to consider all matching local entries before falling back to the Hub.

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.

Not changing this: that case falls back to today's behaviour (the quant is fetched as before), and it is listed under known limitations. This PR only removes the duplicate download where no hub copy serves the repo.

Comment on lines +8834 to +8839
if resolved is None:
return request
load_path, variant = str(resolved[0]), resolved[1]
# GGUF snapshots of this very repo only: their path maps back to the repo id for status,
# unload and consent, which an LM Studio dir or a non-GGUF checkpoint would not.
if not variant or (hf_cache_repo_id(load_path) or "").lower() != identifier.lower():

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 Verify cached resolver hits still exist before rewriting

resolve_local_gguf() can return its index unchanged for _CACHE_TTL_S (currently five seconds), but this code only validates the path lexically with hf_cache_repo_id; it never checks that the selected snapshot and quant are still readable. If a scan-folder model is deleted, moved, or its drive is unmounted after the index was built, a normal repo-ID load during that window is rewritten to the stale local path and ModelConfig.from_identifier() fails instead of falling back to the Hub. Confirm that the resolved variant still exists—or invalidate/retry the index—before replacing the otherwise valid remote identifier.

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.

Not changing this: it needs the scan folder removed inside the 5 s index TTL between two loads; the load then reports the missing path, the same as any local load whose folder disappears.

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 8, 2026
…a scan-folder load

Bundled templates (gemma-4 preserve_thinking) are keyed by repo id, so a copy loaded
from its snapshot path launched with the GGUF's embedded template instead.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 8, 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: cad4b443d0

ℹ️ 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 +19061 to 19062
else scan_folder_public_id or public_model_identifier,
display_name = model_log_label if native_grant_backed else config.display_name,

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 the repo display name after rewriting

For a newly loaded scan-folder copy, model is restored to the requested repo ID, but display_name still comes from the local ModelConfig. Because the rewritten identifier is a snapshot directory such as .../snapshots/abc, ModelConfig derives the display name as abc; frontend paths that add a missing model use this response field directly, so a manually entered repo or otherwise absent catalog row appears under its snapshot hash instead of its model name. Derive the display name from scan_folder_public_id (and the selected variant) when this rewrite occurred.

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.

Not reproduced: a real rewritten GGUF load answers display_name from the GGUF file (Qwen3-0.6B-Q4_K_M for unsloth/Qwen3-0.6B-GGUF in the before/after run), not the snapshot hash.

Comment on lines +18101 to +18103
request = await asyncio.to_thread(
_as_local_scan_folder_request, request, _owner_session(fastapi_request)
)

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 Check the llama runtime before treating the repo as local

When a scan-folder GGUF is selected on an installation without llama-server, this rewrite makes ModelConfig.from_identifier() take its local-path branch, which omits the runtime preflight performed for remote and refused-cache repo IDs. As a result /validate can succeed and /load can pass all rejection checks, unload a currently resident Transformers or NPU model, and only then raise LlamaServerNotFoundError from the backend launch. Preserve the repo path's runtime preflight—or add an equivalent check before teardown—for rewritten requests.

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.

Not changing this: a rewritten request is an ordinary local GGUF path load, which already runs this way for every picker row loaded by path; it only differs on an install whose llama.cpp binary is missing.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

A scan-folder copy of a gemma-4 GGUF runs from its snapshot path, so /status recomputed
no bundled template and reported the bundled one as the user's override.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

A client reloads a scan-folder copy by the snapshot path that /status publishes, which
skipped the bundled gemma-4 template. Read an HF cache snapshot path as its repo id in the
template matcher instead of patching each caller; other local paths still never match.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 8, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

The repo-name segment check read the snapshot revision when a copy loads from its
snapshot dir, so the high effort tier was dropped.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 8, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 8, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: ff7a9c2335

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

@VTplus

VTplus commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Thanks @danielhanchen for taking this over (my #12254) and keeping the commit
authorship — much appreciated. The rewrite has been working well here: repo-id
loads of models parked in registered scan folders resolve to the local copy
instead of re-downloading (re-verified on current main).

Follow-up for the two remaining cases in this family — the org/repo/File.gguf
form the quant picker sends, and API-key sessions loading a locally present
model by repo id: #13343 covers both. The file form resolves under these same
guards; API-key sessions widen the gate to locally managed sessions, with
managed accounts keeping remote semantics. Picker (#12258) and catalog
(#12803) are unchanged in goal.

VTplus added a commit to VTplus/unsloth that referenced this pull request Oct 13, 2026
… claim

A quant pin resolves to the row holding that quant (alternates beside the cache-first key, per the unslothai#13034 chosen-artifact ordering), /v1/models advertises the row a bare id loads, and only a row with weights claims a repo id.
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.

[Bug] Unsloth Loading a repo id that exists in a registered scan folder downloads it into the hub cache instead of using the local copy

2 participants