Skip to content

feat(studio): list and load Ollama MLX-format tags, with their bundled drafters - #18253

Merged
danielhanchen merged 16 commits into
unslothai:mainfrom
Lyxot:studio-ollama-mlx
Nov 18, 2026
Merged

danielhanchen merged 16 commits into
unslothai:mainfrom
Lyxot:studio-ollama-mlx

Conversation

@Lyxot

@Lyxot Lyxot commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Ollama's MLX-format tags (gemma4:*-mlx, qwen3.5:*-mlx, qwen3.6:*-mlx) were not listed in the Unsloth Studio model picker, and neither mlx-lm nor mlx-vlm can load them as pulled:

  • Each tensor is its own safetensors blob, and the quantization companions use Ollama's names (.weight.scale, .weight.bias, .weight.global_scale).
  • The config carries a ModelOpt quantization_config.
  • Drafter tensors are mixed into the same tensor set.
  • Gemma 4 tags ship no chat template.

What changed

Listing and loading (feat(studio): list and load Ollama MLX-format tags)

  • On Apple Silicon, MLX tags are listed as chat-only safetensors rows. Tags whose SYSTEM prompt or preset messages the checkpoint would drop are not listed.
  • A load materializes a standard MLX checkpoint beside the tag's other links, at almost no extra disk:
    • unchanged blobs are symlinked;
    • blobs whose companions need renaming are APFS clones with the header rewritten in place, padded to the same length;
    • tensors under 1 MiB are packed into one shard, to stay under mlx-lm's 2048 open-file limit;
    • the config gets an MLX quantization block;
    • Gemma 4 gets the bundled chat template its renderer names.
  • A volume without clonefile refuses the load with a clear reason rather than copying gigabytes.
  • Identity. The row id, the load path and the loaded model's identifier are one stable path: <links>/<tag hash>/mlx/<repo-tag>. mlx is a symlink switched atomically to the content-addressed build. As a result:
    • re-picking the loaded tag does not reload it;
    • per-model settings survive an ollama pull;
    • the "loaded" notice shows the tag's name.
  • The scan names that path without writing anything. The load and validate routes build it and advertise the model as ollama/<repo>:<tag>.
  • Re-pull. The build that was current before stays, because a resident model reads its shards by path (the offload opens them lazily). Older builds are removed, since their clones would keep removed blobs' bytes alive.
  • The memory/offload probe installs the unsloth-zoo hook for mixed-mode and NVFP4-scaled-embedding checkpoints.

Bundled drafters (feat(studio): speculate with the drafters Ollama MLX tags bundle)

  • Qwen3.6 MTP head. The mtp.* tensors stay in the checkpoint. mlx-vlm's sanitize keeps them out of the target, and unsloth-zoo builds them as the built-in head.
  • Gemma 4 assistant. The draft.* tensors and draft/*.json become a checkpoint of their own at draft/ inside the tag's checkpoint, with tensors named as the standalone assistant release names them. Dropping the prefix shortens each header, so the large tensors are clones too.
  • Resolution order. MLX speculation offers a companion bundled at draft/ after the built-in head and before cached companions, under the same kind, shape and weights checks.
  • Memory estimate fix. The estimate's check for tensors an architecture has no home for now skips NVFP4 .weight_scale_2 global scales, as it already skipped .scales and .biases. Before, every MLX NVFP4 checkpoint with global scales was unsizable, so auto speculation silently skipped its drafter. This applies to all such checkpoints, not only Ollama's.

Depends on

unslothai/unsloth-zoo#2584: loads mixed-mode and NVFP4-scaled-embedding checkpoints on the mlx-vlm path. Without it, most Gemma 4 MLX tags fail to load.

Known limits

  • A re-pull while a model stays loaded keeps the old weights until the next reload.
  • Two re-pulls with no reload in between remove the build the resident model reads.
  • Materialization tests run only on macOS, since they need APFS clones.

Validation

studio/backend: tests/test_ollama_manifest_load_resolution.py tests/test_mlx_speculative.py  -> 43 passed
studio/frontend: model-identity, share-run-configs.editor, qwen38-sampling-defaults, chat-local-model-options  -> 53 passed

Mutation checks. All 26 hand-written mutations of the materializer, identity round trip, prune rule, load route and bundled-drafter resolution make their test fail.

End to end in Unsloth Studio, with real tags:

  • Listing and identity.
    • All 8 local MLX tags are listed.
    • gemma4:e4b-mlx loads, and /status reports the stable path.
    • Re-picking the loaded tag sends no /load.
    • The notice reads "gemma4-e4b-mlx loaded".
  • Chat.
    • Chat, page reload and unload work.
    • The Qwen sampling defaults apply.
    • Tool calls work.
  • Speculation under auto mode, with the GPU shared by other jobs:
    • gemma4:e4b-mlx attached its assistant and accepted 136 of 186 drafted tokens, at 11.7 tok/s.
    • qwen3.6:35b-mlx attached its MTP head and accepted 23 of 30, at 12.1 tok/s.
  • Build cost. A build takes under a second. For e4b it is about 85 MB of real disk; most of the rest of its 2.5 GB logical size is clones.
  • Greedy output against Ollama, with penalties neutral:
    • qwen3.5:4b, qwen3.6:35b and gemma4:26b match Ollama for 40 of 40 tokens;
    • the others diverge only at near-ties.

Model picker, before and after

Two isolated Studio installs (merge base vs this PR) on Linux. OLLAMA_MODELS points at a store holding gemma4:e2b-mlx and gemma4:e4b-mlx with their real manifests and tensor headers and zero-filled data. The Apple Silicon check is stubbed to true on both sides, because listing only happens on Apple Silicon. Before: the search finds nothing. After: both tags are listed as MLX rows.

Model picker before and after

Ollama's `*-mlx` tags store one safetensors blob per tensor with Ollama's names for the quantization companions (`.weight.scale`, `.weight.bias`, `.weight.global_scale`), a ModelOpt `quantization_config`, drafter tensors mixed in, and no chat template for Gemma 4, so neither mlx-lm nor mlx-vlm can load them as pulled, and the model picker only listed GGUF Ollama tags.

On Apple Silicon these tags are now listed as chat-only safetensors rows. Loading one materializes a standard MLX checkpoint beside the tag's other links: unchanged blobs are symlinked, blobs whose companions need renaming are APFS clones with the header rewritten in place, tensors under 1 MiB are packed into one shard to stay under mlx-lm's open-file limit, and the config gets an MLX `quantization` block. Tags carrying SYSTEM or preset-message layers are not listed, since the checkpoint would drop them.

Each build is keyed by the manifest. The row, the load and the loaded model all name the tag by one stable path, `<links>/<tag hash>/mlx/<repo-tag>`, where `mlx` is a symlink switched to the current build. Re-picking the loaded tag therefore does not reload it, per-model settings survive an `ollama pull`, and the "loaded" notice shows the tag's name. The scan names that path without writing anything; the load route builds it and advertises the model as `ollama/<repo>:<tag>`. A re-pull keeps the build that was current before, since a resident model reads its shards by path, and removes older ones, whose clones would keep removed blobs' bytes alive.

The memory and offload probe installs the unsloth-zoo hook that reads mixed-mode and NVFP4-scaled-embedding checkpoints, so it can build these models.
Ollama's MLX tags carry their own drafters: Gemma 4 tags a Gemma 4 assistant under `draft.*` with `draft/*.json`, Qwen3.6 tags the model's MTP head under `mtp.*`. The checkpoint built for a tag dropped both.

The MTP head now stays in the checkpoint, where mlx-vlm's sanitize keeps it out of the target and unsloth-zoo builds it as the built-in head. The assistant becomes a checkpoint of its own at `draft/` inside the tag's checkpoint, named as its standalone release names its tensors; dropping the `draft.` prefix shortens each header, so its large tensors are clones too. MLX speculation offers a companion bundled at `draft/` after the built-in head and before cached companions, under the same kind, shape and weights checks. A build's key now includes a layout version, so builds made before this change are rebuilt.

The memory estimate's check for tensors a checkpoint's architecture has no home for skips NVFP4's `.weight_scale_2` global scales, as it already skips `.scales` and `.biases`: the unquantized model it builds has no place for any of them. Every MLX NVFP4 checkpoint with global scales, not only Ollama's, was unsizable before, so auto speculation skipped its drafter.

Validated in Unsloth Studio under auto speculation: gemma4:e4b-mlx attached its assistant and accepted 136 of 186 drafted tokens; qwen3.6:35b-mlx attached its MTP head and accepted 23 of 30.
@Lyxot

Lyxot commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T16:32:02.106250Z a6a8adc Manual request
ℹ️ 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.

@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: 3706c7d6b6

ℹ️ 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 thread studio/backend/routes/inference.py Resolved
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@Lyxot

Lyxot commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator 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: 8d2fe5b493

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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 5e53a4dfc3

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

@Lyxot

Lyxot commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

Codex review converged on the current head 5e53a4d: #18253 (comment) ("Didn't find any major issues").

CI (staging copy Lyxot#255, same head and base, since the upstream jobs were queued): all 75 checks pass on 5e53a4d; the 4 skipped are conditional legs. Two jobs failed on the first attempt, and their failed jobs passed on rerun without changes. Repo tests (CPU, rest) failed because test_block_compile.py::test_outer_compile_prepares_gdn_eagerly timed out downloading from the Hugging Face Hub. Frontend unit tests (Windows) failed on a Playwright wait_for_function timeout in a long-thread render. Neither touches code in this PR.

What changed during review (3 commits on top of the original 2):

  • 90e1e46 fix(studio): build an Ollama MLX tag only after the load's access check. /load materialized the tag's checkpoint before require_model_access, so a rejected managed caller could still build, switch or prune a tag's storage. Materialization now runs in _load_model_impl after the access and drafter checks, the order /validate already uses. Requests carrying a native_path_lease are left unchanged, because their access check waits for the lease.
  • 8d2fe5b fix(studio): write an Ollama MLX checkpoint's JSON as UTF-8. CI's text-encoding tests flagged two write_text() calls with no explicit encoding.
  • 5e53a4d fix(studio): create an Ollama MLX build's generated files exclusively. A manifest layer named like a generated file (ollama_tag.txt, or Config.json on case-insensitive APFS) was symlinked to its blob first, and the later write then went through the link into a shared blob. Every generated file is now created with open(..., "x"), which refuses any existing path.

Two findings were answered in their threads and not changed. Auto-switching the API to an unloaded MLX tag is a new feature, since MLX tags were not listed before this PR. A dangling shard after re-pulling a loaded tag and then deleting another tag that shares its blobs is a rare three-step edge case; it fails loudly and is fixed by a reload.

@danielhanchen danielhanchen self-assigned this Nov 18, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@unslothai unslothai deleted a comment from Lyxot Nov 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@unslothai unslothai deleted a comment from Lyxot Nov 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 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: f700aafdce

ℹ️ 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 +481 to +483
for stale in tag_dir.iterdir():
if stale.name.startswith(("mlx-", ".mlx-")) and stale not in (build, previous):
shutil.rmtree(stale, ignore_errors = True)

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 Reclaim builds whose Ollama manifests have been removed

If a tag is loaded, unloaded, and then its manifest and blobs are removed outside Studio, its hidden mlx-* builds are never reclaimed. This pruning only runs during materialization of an existing tag, and the other whole-tag cleanup is delete_ollama_model_ref(), which external deletion does not call. APFS clones and packed shards continue retaining the weights' disk blocks, so deleting multi-GB models can leave their storage allocated indefinitely, with no inventory row left to delete through Studio. Add orphan-tag build cleanup that protects any still-resident model.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not changing this: Studio's own delete already removes the tag's link dirs including every build. A tag removed outside Studio leaves its GGUF hardlinks behind the same way today, and sweeping a build whose manifest is gone could pull files out from under a resident model reading them lazily.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 18, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 18, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: a6a8adceb7

ℹ️ 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 3a4d3e0 into unslothai:main Nov 18, 2026
danielhanchen added a commit that referenced this pull request Nov 18, 2026
## Summary


`tests/studio/test_model_picker_contracts.py::test_the_chat_picker_marks_ollama_targets_unloadable_by_the_api`
fails on main:

```
AssertionError: assert 'return !isLora && !ids.some(isOllamaLinkPath);' in '...'
```

Seen in "Repo tests (CPU, studio)" on the staging run for #17742, which
is on current main.

## Cause

#18253 lists and loads Ollama MLX-format tags. Their checkpoint
directories sit under the same `ollama_links` tree, but the resolver
does index them. So `apiAutoSwitchMayLoad` and the override backfill
(`migrate-model-overrides.ts`) now exclude only a materialized `.gguf`
link, `isMaterializedOllamaLink`, instead of every `isOllamaLinkPath`.
That change is intended and covered by `model-identity.test.ts`. The
contract test still pinned the old line.

## Fix

Test-only:

- Pin the new predicate in `apiAutoSwitchMayLoad` and in the backfill.
- Also pin `isMaterializedOllamaLink`'s definition
(`isOllamaLinkPath(modelId) && !isOllamaMlxModelId(modelId)`). A
materialized `.gguf` link stays unloadable, which is what this test
exists to guard.

## Verification

| `features/hub/lib/model-identity.ts` | Result |
|---|---|
| main | 205 passed (before: 1 failed) |
| `isMaterializedOllamaLink` reduced back to every link (sabotage) |
this test fails |

ruff is clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants