Skip to content

Studio: export a downloaded model to a plain folder, and import one back - #12163

Closed
lonexreb wants to merge 8 commits into
unslothai:mainfrom
lonexreb:feat/portable-model-export
Closed

lonexreb wants to merge 8 commits into
unslothai:mainfrom
lonexreb:feat/portable-model-export

Conversation

@lonexreb

@lonexreb lonexreb commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Closes #8798.

What

A downloaded model lives in the Hugging Face cache as a tree of symlinks into blobs/, which survives neither a copy to an external drive nor a move to another machine, so a reinstall or a second computer means downloading everything again. Two routes and their frontend client calls:

  • POST /api/hub/export-model {repo_id, destination, variant?}: copies the newest cached snapshot (or one GGUF variant of it) into <destination>/<org--name> as ordinary files, every symlink dereferenced, beside a small unsloth-export.json naming the model, revision and files. A single-file GGUF exports as that file.
  • POST /api/hub/import-model {source}: reads that manifest and writes the files back the way huggingface_hub does, content under blobs/<sha256>, a symlink from snapshots/<revision>/, a plain copy where symlinks are refused (Windows without the privilege), and refs/main when the repo is new. The inventory scan lists the model as downloaded and a load needs no network. Importing the same folder twice is a no-op.

Refused with a message that says why: a destination inside the models cache, a folder without the manifest, an export missing a listed file, and any manifest entry that would leave its folder (../, absolute, a/../..).

Deliberately not in this cut: a .unsloth archive format (a folder is what every other tool already reads; a bundle can wrap the same copy later), and the mmproj/sidecar pairing for a GGUF variant beyond name matching. The buttons in the model list are the next commit on this PR; the API is the contract.

Verification

studio/backend/tests/test_portable_model_export.py, against a real cache layout (blobs + snapshot symlinks + refs/main): the dereferenced copy and manifest, the GGUF variant filter, the in-cache refusal, the round trip into a fresh cache (visible to _discover_hf_cache, idempotent on repeat), the missing-manifest refusal, and three traversal spellings: 9 passed. ruff + kwarg spacing clean. Frontend typecheck reports one error in tests/desktop-app-menus.test.ts, a file this branch does not touch (it is red on main since e05cdd9); api.ts itself type-checks.

🤖 Generated with Claude Code

https://claude.ai/code/session_012DRiTpjxAymRtv9p8Xr2kd

A model in the Hugging Face cache is a tree of symlinks into blobs/,
which survives neither a copy to an external drive nor a move to another
computer, so every reinstall meant downloading again (unslothai#8798).

POST /api/hub/export-model copies the newest cached snapshot of a model
(or one GGUF variant of it) into <destination>/<org--name> as ordinary
files, dereferencing every symlink, with a small unsloth-export.json
naming the model, revision and files. POST /api/hub/import-model reads
that manifest and writes the files back the way huggingface_hub does:
content under blobs/<sha256>, a symlink from snapshots/<revision>/, and
a plain copy where symlinks are refused, plus refs/main when the repo is
new. The inventory scan then lists the model as downloaded and a load
needs no network. A destination inside the cache, a folder without the
manifest, an incomplete export, and any manifest entry that would leave
its folder are refused with a message that says so. The frontend API
client gains the two matching calls; the buttons in the model list are
the next commit.

Tests cover the dereferenced copy and manifest, the GGUF variant filter,
the in-cache refusal, the round trip into a fresh cache (seen by
_discover_hf_cache, idempotent on repeat), the missing-manifest refusal
and three traversal spellings: 9 passed. ruff and kwarg spacing clean.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_012DRiTpjxAymRtv9p8Xr2kd
lonexreb and others added 2 commits September 30, 2026 22:56
Both routes answer the folder they touched, so an API-key caller now gets
that path redacted like every other hub inventory route, and the refusal
text goes through the same redaction. The routes also never imported
asyncio, so every call 500'd; the new route-level test catches both.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_012DRiTpjxAymRtv9p8Xr2kd
@lonexreb

lonexreb commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Live round trip against a real huggingface_hub cache (1.28.0), with the branch's export_cached_model / import_model_folder untouched:

  1. snapshot_download("hf-internal-testing/tiny-random-gpt2") into an empty cache A: 10 files, symlinked snapshot, as the hub writes it.
  2. Export from A to a folder: 10 plain files, 12,512,611 bytes, zero symlinks, manifest carrying the repo id and revision 71034c5d8bde.
  3. Import that folder into an empty cache B.
  4. Ask huggingface_hub itself about B: scan_cache_dir lists the repo at revision 71034c5d8bde, 10 files, same size; snapshot_download(..., local_files_only=True) resolves the snapshot offline; every file in B's snapshot is byte-identical to A's.

So the import writes a layout the hub's own tooling reads back as the original download, not just one the Studio inventory accepts.

@lonexreb

lonexreb commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Second live round trip, this time the GGUF variant path, ending in inference from the imported cache.

  1. snapshot_download("ggml-org/models", allow_patterns = [tinyllamas q4_0, q8_0]) into an empty cache A: two quants of the same model.
  2. export_cached_model(repo, "q4_0", folder): exactly one file plus the manifest, 19,077,344 bytes, folder ggml-org--models--q4_0; the q8_0 sibling stays behind.
  3. import_model_folder(folder) into an empty cache B.
  4. scan_cache_dir(B) lists the repo at revision 499bc8821c6b, one file.
  5. llama-server (b11342) pointed at that file where it sits inside B's snapshot: /health ok, and Once upon a time completes to , there was a little girl named Lily. She loved to play outside….

So a variant export carries only the quant the picker named, and what the import writes is a cache a real inference server reads without any further step.

@lonexreb

lonexreb commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

Two-machine round trip through the routes on this head (941a778), with two Studio backends on separate homes and separate HF_HUB_CACHE dirs, driven over HTTP with a session token:

Machine A (unsloth/gemma-3-270m-it-GGUF, UD-Q4_K_XL downloaded into A's cache, listed by /api/hub/local as hf_cache):
POST /api/hub/export-model {repo_id, destination, variant: "UD-Q4_K_XL"} → exported, 1 file, 253,934,624 bytes, folder unsloth--gemma-3-270m-it-GGUF--UD-Q4_K_XL/ holding the GGUF as a plain file plus unsloth-export.json (format, version, repo_id, variant, revision c90975d…, files).

Machine B (empty cache):
POST /api/hub/import-model {source} → imported; the cache now has blobs/<sha256>, snapshots/c90975d…/gemma-3-270m-it-UD-Q4_K_XL.gguf as a symlink into blobs, and refs/main. A second import answers already_present. B's /api/hub/local lists the model as hf_cache, the picker shows it under On Device → Unsloth as UD-Q4_K_XL · 254 MB, and it loads through llama-server and answers (Say hi, 234 tok/s) with zero requests to huggingface.co in B's log.

Refusals checked: a destination inside the models cache ("pick a folder outside it"), and a source folder without the manifest ("not an Unsloth export").

One fixture note for anyone repeating this: ggml-org/models (GGUFs in a tinyllamas/ subfolder) exports and imports fine too, but neither inventory lists it since it is not a model-shaped repo; a single-file GGUF repo is the right test subject.

@danielhanchen danielhanchen self-assigned this Oct 4, 2026
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.

[Feature] Desktop/Studio: Export and import downloaded models (.unsloth portable archive) for backup or transfer between computers

3 participants