Skip to content

feat(studio): Mac adapter-format option for LoRA export, with GGUF adapters on macOS - #7539

Merged
danielhanchen merged 16 commits into
unslothai:mainfrom
Lyxot:feat/studio-lora-adapter-format
Sep 29, 2026
Merged

danielhanchen merged 16 commits into
unslothai:mainfrom
Lyxot:feat/studio-lora-adapter-format

Conversation

@Lyxot

@Lyxot Lyxot commented Jul 28, 2026 •

Copy link
Copy Markdown
Collaborator

What this does

Surfaces the new PEFT↔MLX adapter interop through Studio and the CLI, so a LoRA trained on Apple silicon can be exported in the standard Hugging Face format and used on a GPU machine, and vice versa.

Depends on unslothai/unsloth-zoo#957, which adds the conversion itself. Without that zoo change the new option surfaces a clear upgrade message rather than failing obscurely.

Three surfaces change:

  • Studio backend — export_lora accepts an adapter_format of "mlx" or "peft". Omitting it resolves to the platform default, which keeps every existing call byte-identical on both platforms.
  • Studio frontend — the LoRA export card gains an adapter-format selector on Apple silicon, with a compatibility tip driven by the adapter's actual features.
  • CLI — unsloth export --format lora gains --adapter-format.

Training and checkpoint output are untouched. Checkpoints remain MLX-format; only export is affected.

Why the option is Mac-only

On a CUDA machine PEFT already is the native format, so a selector there would offer a choice between one format and itself. The flag is registered on every platform so that explicit off-Mac use gets an actionable error instead of an unknown-argument failure, but it is advertised only where it applies.

GGUF LoRA adapters on macOS

GGUF LoRA files are built from a PEFT-format adapter, so this change also unlocks GGUF adapter export on Apple silicon: the MLX adapter is materialized as PEFT, then converted with llama.cpp's convert_lora_to_gguf.py — the same pipeline the CUDA path uses.

The GGUF method card is deliberately unchanged. Its LoRA-adapter target is not offered on Mac; GGUF adapters ship through the LoRA method's own toggle there, so the existing GGUF flow behaves exactly as before on both platforms.

Conversions that GGUF LoRA cannot represent are refused before converting rather than producing a misleading file: per-module alpha patterns (the format has one global alpha, and rsLoRA sources surface here because export bakes their scales into alphas), DoRA, full-module state, and expert-parameter adapters.

Feature detection is tri-state

The compatibility tip depends on knowing what an adapter contains, and a config marker alone cannot prove absence: PEFT auto-saves embedding and base state only as weight keys, and MLX trainer checkpoints may carry non-LoRA trainable tensors with no marker at all. Detection is therefore three-valued — a marker means true, a weight-header read can prove a negative, and a skipped or failed probe stays unverified.

This matters for one claim in particular. The tip only states vLLM compatibility when every relevant field is a verified false; an unverified field never earns that claim. Bulk listings skip the header probe for cost, which is safe because config markers still classify training-run adapters correctly.

Hub pushes

Hub uploads keep main's existing path (_open_hub_repo + upload_folder); the compare-and-swap commit rewrite was split out of this PR. On MLX, PEFT and GGUF adapter exports are built in a fresh sibling folder and moved into the destination, so repeating an export into the same folder overwrites it like the native save does. On a Mac, PEFT and GGUF adapter exports default to their own -peft folder, since one folder holds one adapter format.

Validation

python -m pytest studio/backend/tests/test_export_adapter_format.py unsloth_cli/tests/test_export_adapter_format_flag.py -q
# 20 passed

python -m pytest unsloth_cli/tests/ -q
# 665 passed, 4 skipped

Frontend tsc --noEmit is clean and the production build succeeds.

The backend's six-cell platform × field matrix is covered by tests: omission stays byte-identical to the pre-existing native call on both platforms, and explicit native values match it. That is the property that keeps this change inert for existing users.

End-to-end, the export path was exercised against a real CUDA host: a Studio-style PEFT export of an MLX-trained adapter loads in transformers/PEFT on a GPU machine and continues training there.

Notes for reviewers

  • The adapter-format selector lives inline in the export page rather than in a separate component, matching how the other method-specific controls there are structured.
  • _resolve_adapter_format is the single place platform policy is decided; the backend never infers the format anywhere else.
  • If an installed unsloth-zoo predates feat(mlx): PEFT adapter interop for the MLX backend unsloth-zoo#957, the PEFT path raises a named upgrade message instead of an AttributeError.
  • Adapters the zoo converter does not accept — embedding LoRA, expert-parameter factors, LoRA wrappers plain PEFT cannot represent — surface its named refusal at export time rather than producing a partial adapter. This surface adds no detection of its own for them; the compatibility tip models DoRA, full-module state, expert parameters, and per-module rank/alpha.

Screenshots

Export page, LoRA Only card, merge base (BEFORE) vs this PR (AFTER). Both Studios were built from source on Linux with /api/health reporting an Apple-silicon Mac host, which is the only thing that switches the card to its Mac branch.

MLX selected (default): the GGUF adapter button is enabled and the Safetensors format selector appears.

MLX selected

PEFT selected: the compatibility tip switches to the PEFT copy.

PEFT selected

@Lyxot
Lyxot requested a review from danielhanchen as a code owner July 28, 2026 09:36
chatgpt-codex-connector[bot]

This comment was marked as outdated.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

Lyxot added 4 commits August 17, 2026 20:27
…s on macOS

Adds adapter_format ('mlx' | 'peft') to the LoRA export API, threaded through the route, orchestrator, worker, and export backend. Omitting the field resolves to the platform's native format — MLX servers keep writing MLX adapters, CUDA servers keep writing PEFT adapters, byte-identical to today — so existing clients are unaffected. Explicit 'peft' on an MLX server converts through unsloth-zoo's adapter exporter (an older unsloth-zoo without that capability surfaces an actionable upgrade error via a signature probe, so genuine zoo exceptions propagate untouched); explicit 'mlx' on a non-MLX server, or combined with gguf=True, is an error.

GGUF LoRA adapter export is now available on macOS/MLX: the backend materializes a PEFT adapter via unsloth-zoo, refuses adapters GGUF LoRA cannot represent (per-module alpha values, rsLoRA with per-module ranks, DoRA, full-module state, expert-parameter adapters) before checking dependencies, then runs llama.cpp's convert_lora_to_gguf.py with the same script discovery and source-checkout fallback the CUDA path uses, forwarding the approved trust_remote_code decision. A conversion failure removes the just-published directory so retries are not refused as existing destinations.

Adapter directories and Hub repos never mix formats: local exports refuse a destination already carrying the other format's weight files (recursively, covering peft named-adapter subdirectories and the legacy adapter_model.bin spelling), and Hub pushes go through a single create_commit pinned to a pre-checked revision (parent_commit) so the Hub itself rejects concurrent writers — with fresh single-use operations per attempt, one re-check retry, a post-commit verification covering the client's all-unchanged short-circuit, and preservation of a repo-authored README.

Adapter listings gain a compact adapter_features summary (dora / full_state / moe_target_parameters / non_uniform) parsed from adapter configs with typed checks and a safetensors header probe for auto-saved embedding state, as the data source for format-compatibility UI copy.
…ility tips

The LoRA export card on Apple-silicon hosts gains a Safetensors-format choice (MLX, the default and today's native output, or PEFT via the new backend conversion), and its GGUF adapter toggle is enabled there — dispatching through the LoRA-adapter export path with the conversion limitations named in the helper copy (per-module alpha values, rsLoRA with per-module ranks, DoRA, replaced embeddings or modules_to_save, expert-parameter targets). The GGUF / Llama.cpp method is untouched: it stays a CUDA-side surface, and Mac hosts export GGUF LoRA adapters only through the LoRA card's toggle. Payloads carry an explicit adapter_format only from Mac hosts for non-GGUF adapter exports; other platforms keep omitting the field, preserving their native behavior.

Compatibility copy is driven by real adapter metadata: checkpoint listings now expose an adapter_features summary (dora / full_state / moe_target_parameters / non_uniform) parsed per training run. full_state is tri-state in both artifact formats — a config marker is a verified positive, a weight-header probe verifies a negative, and a skipped (bulk listings) or failed probe stays unknown — because absence of a marker proves nothing: PEFT auto-saves embedding state only as weight keys, and native MLX trainer checkpoints can carry unmarked non-LoRA trainable tensors. The vLLM compatibility claim renders only when all four flags are verified false; DoRA and full-module state render a not-supported-by-vLLM note, per-module patterns note vLLM's single global rank/alpha, expert-parameter adapters note that support varies, and non-uniform MLX exports carry the stock-mlx-lm loading caveat. Unknown or missing metadata always gets neutral copy.

The repository intentionally gains no JS test harness; the selector, tips, payload, and error-surface behaviors are validated by recorded checks kept outside the repo, and the in-repo gates remain typecheck and build.
Adds --adapter-format (mlx | peft) to the export command's lora format, forwarding the value to the export backend, which resolves omission to the platform's native format. The option is registered everywhere but advertised only on Apple-silicon hosts, where the format choice exists; explicit use elsewhere — or with a non-lora format, or with an invalid value — errors on stderr with exit 2 before any backend work. Conversion failures (for example an unsloth-zoo without PEFT export support) surface through the command's existing error path: the backend's actionable message on stderr and a non-zero exit.
… over them

parent_commit pinned the adapter commit to a pre-checked head so the Hub
rejects a concurrent writer, but the handler then re-read the head and
committed on top, replacing an adapter another process had just published.
Raise instead of retrying, and claim only what holds for every create_commit
failure: the commit was not confirmed and was not re-applied over a newer
head.
Resolve conflicts against main's Hub push rework (_open_hub_repo, model
card, reused-folder staging). Keep main's Hub upload path instead of the
compare-and-swap commit rewrite, which is split out of this PR. PEFT and
GGUF adapter exports on MLX now stage in a fresh sibling and move into
the destination, so a repeat export into the same folder overwrites like
the native save instead of failing the zoo's fresh-destination check.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 29, 2026
A Hub push always saves locally first, and the Hub destination hides the
folder picker, so after a native MLX export the PEFT push to the same
default folder hit the one-format-per-folder refusal with no way out.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 29, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 29, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 29, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 29, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 29, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 29, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 29, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 29, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 29, 2026
install_llama_cpp probes apt-get for build dependencies even with
just_clone_repo=True, so GGUF adapter export failed on macOS with
'apt-get does not exist'. A shallow clone is all convert_lora_to_gguf.py
needs; UNSLOTH_AUTO_INSTALL=0 is still honored. The converter test no
longer depends on torch being installed on the runner.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 29, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 29, 2026
Without safetensors installed, a config-only adapter raised instead of
reporting full_state as unverified.
Clone llama.cpp at the revision zoo resolves for the installed binaries
(the latest release otherwise) instead of the moving default branch, in
a per-revision folder. Grant the converter child its credential through
apply_token_to_child_env, which also resets an inherited
HF_HUB_DISABLE_IMPLICIT_TOKEN=1 for explicit tokens.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 29, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 29, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 29, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 29, 2026
danielhanchen added a commit to Datta0/unsloth-staging-3 that referenced this pull request Sep 29, 2026
@danielhanchen
danielhanchen merged commit 46161d7 into unslothai:main Sep 29, 2026
66 of 71 checks passed
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.

2 participants