Skip to content

Studio: add a Download to folder for model downloads - #13557

Merged
danielhanchen merged 10 commits into
mainfrom
studio-download-to-folder
Oct 13, 2026
Merged

danielhanchen merged 10 commits into
mainfrom
studio-download-to-folder

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Fixes #10872

What this does

Adds a simple Download to choice for model downloads in Unsloth Studio and Unsloth Desktop, so a large model can go to another drive without changing the global Models folder and changing it back.

  • The Model hub header gets a small folder button next to the Auto / HTTP / Xet toggle. Its menu lists the Models folder (default), the existing Custom Folders, and Choose folder... (native folder dialog on Desktop, the folder browser on web). With the Models folder selected it is icon only, so the header layout is unchanged; with another folder selected it also shows that folder's name.
  • When a download starts with a chosen folder, the folder is used exactly like a Models folder (files go to <folder>/hub, same layout as the HF cache) and is added to Custom Folders, so the model shows up, loads, and can be deleted like any other cached model.
  • Leaving it on Models folder sends the same request as before, so existing flows, notebooks and API callers are unchanged.

How it works

Backend:

  • DownloadModelRequest.download_dir (optional). Omitted, blank, or equal to the Models folder means the Models folder, as before.
  • The folder goes through the same validation as the Models folder setting (_validate_cache_home: absolute path, not a drive root, not a system or credential folder, writable hub/ and xet/). A bad folder is a 400 with that message and nothing starts.
  • The chosen folder is stored in its own known-cache list (hugging_face_download_homes), separate from the Models folder history, so download progress, resume, cancel, the cached checks and delete all see it, and neither list can push the other out. Already listed is a no-op.
  • It is also added to Custom Folders through the existing add path, which is a no-op when already there.
  • The worker, the registry claim and the resume baseline are all pinned to that folder's cache, using the per-job cache routing that already exists for a Models folder change mid-download.
  • Only the owner can pick a folder (managed accounts get a 403). The UI hides the picker for them and never sends a stored choice from another account.

Frontend:

  • The choice is stored per browser (localStorage). Reset all local preferences clears it.
  • Each download pins its folder when it is requested. A multi-repo plan pins one folder for every entry, and the pin survives a page reload. Resume after a cancel goes back to the same folder. A failed start retries in the folder chosen now, so a missing drive is not retried forever.
  • download_dir is only sent when a folder is chosen.

Testing

  • Backend: hub/tests/test_download_to_folder.py, 10 tests. 9 fail on main and pass here; the 10th checks that no folder keeps the Models folder, and passes on both.
  • Neighbouring backend suites all pass except 5 test_delete_variant_* tests that fail on main too.
  • Frontend:
    • tests/download-location.test.ts: request body, pinned folder, and resume vs failed start.
    • A reload test in tests/download-transport-persistence.test.ts.
    • All 1335 hub and download-manager node tests pass.
    • tsc -b and the test typecheck are clean.
  • Cross-platform staging CI (ubuntu-latest, macos-15, windows-latest) runs both new test files: green.
  • Live Studio built from this branch (install.sh --local, isolated home and cache), all checks passing on the final head:
    • safetensors repo to a chosen folder: complete, files only under <folder>/hub, progress reads complete, listed once.
    • Starting the same download again leaves the folder registered once.
    • GGUF Q4_K_M to the folder: complete, shows in cached GGUFs and as On device in the hub.
    • Loading it from the folder works, and nothing is re-downloaded into the Models folder.
    • No folder chosen: the download goes to the Models folder as before.
    • Missing folder: 400 The parent folder does not exist.
    • From the UI (Chromium): picking a folder updates the button, and downloading Q8_0 sends download_dir. The file lands in the folder with the normal progress bar.

Screenshots

Model hub header at 1500 px, before and after, with the Download to menu open. The header keeps the same number of rows.

Download to in the Model hub header

@danielhanchen danielhanchen self-assigned this Oct 13, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 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-13T10:55:14.447754Z 79b8ec8 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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 13, 2026
@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: f712a17d07

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

hf_token: hfToken,
use_xet: useXet,
transport_mode,
download_dir: downloadDirForRequest(req.downloadDir),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Scope partial checks to the selected folder

When a cancelled HTTP download resides in a custom folder, the transport preflight still calls /api/hub/transport-status without the pinned destination, so the backend examines the active Models folder rather than the folder sent here. If the user switches to Xet and resumes, the preflight can report no partial and skip the restart warning; the worker then prepares the custom cache for Xet and discards the existing HTTP partial. Pass the destination through the transport-status request and scope the backend checks to that cache.

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.

Needs a cancelled HTTP partial in a custom folder plus a manual transport switch before resuming. The common resume path keeps the same transport, so leaving this one.

Comment on lines +47 to +48
/** Folder this job downloads into; null means the Models folder. */
downloadDir?: string | null;

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 Persist the pinned folder with active jobs

The new job field is not included by toPersistedJob or restored by sanitizePersistedJob. If a download starts in folder A, the user selects folder B, and the page reloads while the first job is active, the restored job loses A; after cancellation, withDownloadDir therefore resumes into B instead of reusing A's partial, duplicating or restarting a large transfer. Serialize and sanitize downloadDir, including null, along with the other active-job metadata.

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 00a87d5: downloadDir is now written and restored with the persisted job, with a reload test.

Comment on lines +797 to +800
...(req.downloadDir !== undefined
? { downloadDir: req.downloadDir }
: opts.adopt && existing?.downloadDir !== undefined
? { downloadDir: existing.downloadDir }

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 Record the destination of an attached job

When another browser already has this repo and variant downloading in folder A, a start from a browser configured for folder B attaches to the existing backend job because registry keys are destination-independent, but this code records B from the request as the job's destination. Cancelling and resuming from that browser then starts in B and abandons A's partial. The start/active-download response needs to expose the actual claimed cache so attached jobs store the backend job's destination rather than the requested one.

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.

Two browsers starting the same repo and variant into different folders is outside the case this targets, and the attached job still delivers the model.

req: DownloadRequest,
request: DownloadRequest,
): Promise<DownloadStartOutcome> {
const req = withDownloadDir(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 free space on the pinned destination

After pinning a custom destination, the pre-download disk check still calls /api/system/disk without that path; the route explicitly measures only the active Models-folder hub and Xet caches. When the chosen folder is on another drive, the UI can miss low space on the drive that will receive the model or warn about an unrelated full Models drive. Pass the pinned destination to a validated disk-space endpoint so the notice measures the actual download volumes.

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.

The low disk notice is advisory only and never gates a start. The worker checks free space on the destination it writes to (HF_HUB_CACHE is the chosen folder), so a real shortage still fails with a clear message.

Comment on lines +453 to +456
if len(homes) >= MAX_DOWNLOAD_HOMES or any(
os.path.normcase(str(path)) == key for path in homes
):
return

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 Refuse destinations that cannot be remembered

Once 64 download homes have been stored, a new valid destination reaches this early return but the caller still starts the worker there. Because that root is never added to known_hf_hub_caches, after live registry metadata is gone the cache-aware progress, cleanup, and deletion paths cannot resolve the downloaded files as a known HF cache. Either evict a stale entry, reject the download with an actionable error, or otherwise guarantee that every accepted destination is registered.

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.

64 distinct download folders is not a realistic setup, and the folder is still listed through Custom Folders even then.

Comment on lines +323 to +325
cache_paths = await asyncio.to_thread(
_download_cache_paths, getattr(body, "download_dir", None)
)

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 Scope peer blob protection to the same cache root

Allowing each job to select its own cache breaks the registry's existing assumption that every active variant of a repo writes into one root. If Q4 is downloading in folder A while Q8 starts in folder B, peer_blob_hashes still marks A's shared companion hashes as protected for B; cache preparation in B can then preserve an abandoned or transport-incompatible partial solely because an unrelated file with the same hash is live on another disk. Include the claimed hub_cache when selecting peers so purge protection applies only to writers that can actually touch this job's files.

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.

Over-protection only keeps a stale partial instead of purging it, never deletes live data, and needs two variants of one repo downloading into two different folders at once.

@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: 00a87d56d6

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

}
const previous = getState().jobs[jobKeyOf(req.kind, req.repoId, req.variant)];
const resumable =
previous?.state === "cancelled" || previous?.state === "error";

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 Let retries escape a rejected download folder

When the backend rejects a destination before starting—such as a native-picked credential directory, an unmounted drive, or a folder that is no longer writable—startJob retains that destination on the resulting error job. This condition then treats every error as resumable, so after the user selects a valid folder and clicks Download again, the request silently reuses the rejected path and fails repeatedly until the old job is manually dismissed. Only reuse the pinned directory for errors that can actually have resumable state, or allow the newly selected destination to replace it when no transfer began.

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 7e789ea: only a cancelled job resumes into its pinned folder; a failed start uses the current choice. Covered in download-location.test.ts.

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

ℹ️ 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 +170 to +173
downloadDir:
previous?.state === "cancelled" && previous.downloadDir !== undefined
? previous.downloadDir
: (downloadDirForRequest() ?? null),

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 Pin one destination for the entire staged plan

For a multi-entry model plan, useStagedDownload calls requestStart separately as each repo finishes, so this reads the current picker value again for every entry rather than preserving the value from when the user selected the model. If the user changes “Download to” while a long plan is running—or queues another model for a different drive—the remaining checkpoint/companion repos can be written to the newly selected folder, splitting one model across destinations and potentially consuming space on the wrong disk. Capture the directory when the plan is enqueued/staged and propagate that pinned value to every entry.

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 53ccc63: stage() captures the folder once and every entry of the plan starts with it.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 53ccc63095

ℹ️ 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 Etherll/unsloth that referenced this pull request Oct 13, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 13, 2026
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.

[Feature] Steam-like multi-drive model library management

1 participant