Skip to content

Multi-LoRA: export expert LoRA in the layout vanilla PEFT loads - #16568

Merged
danielhanchen merged 6 commits into
mainfrom
fix-multi-lora-expert-vanilla-export
Nov 4, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
fix-multi-lora-expert-vanilla-export

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Multi-LoRA expert adapters exported through adapter_state_dict could not be loaded by vanilla transformers + PEFT. A single adapter saved through the normal path loads fine.

Why

The normal save path converts expert LoRA factors to the layout PEFT expects, using unsloth_zoo's swap_expert_lora_factors. adapter_state_dict in unsloth/kernels/multi_lora.py never applied that conversion. A Qwen3-VL-MoE or Llama-4 expert adapter exported through it came out in Unsloth's internal layout, and vanilla PEFT refused it with size mismatch (for example lora_A [16, 64] vs [16, 40]).

Who hits it:

  • The rank-major fix is reached through the unsloth.kernels.multi_lora Python API. Studio's batched trainer only allows Qwen3-MoE / Qwen3.5-MoE today, and those experts were already exported correctly.
  • The legacy grouped_by_expert change below also reaches Studio: Qwen3-MoE jobs with UNSLOTH_MOE_LORA_B_LAYOUT=grouped_by_expert, once the matching unsloth-zoo change is installed.

Change

  • Conversion: adapter_state_dict now runs the export through a small helper that reuses zoo's own swap decision and converter, with nothing duplicated here.
  • Legacy packing: in the legacy grouped_by_expert mode, the expert lora_B is written in the same refuse-loudly shape that zoo's save uses. This takes effect once the matching unsloth-zoo change is installed and does nothing before that.

Tests

New file tests/test_multi_lora_expert_vanilla_export.py:

  • it trains a tiny multi-LoRA expert adapter and exports it;
  • it loads the export in a separate plain transformers + PEFT process and compares each expert's LoRA delta with the trained one;
  • it checks that the same folder still reloads under Unsloth.
main this branch
new test 1 failed (size mismatch), 1 skipped passes
with tests/test_multi_lora_experts.py 37 passed, 1 skipped

Evidence beyond the tests

Cell check on CPU, base vs this branch: every family x LoRA target x legacy switch x zoo main / zoo legacy fix, on transformers 5.17 and 5.22 and PEFT 0.18 / 0.20 / 0.21.

  • Dense and attention-only exports: byte-identical in all 144 cells.
  • Unflagged MoE experts (Qwen3-MoE, gpt-oss, OLMoE, Mixtral) in the default packing: identical.
  • Moved cells (Qwen3-VL-MoE and Llama-4 experts):
    • load in vanilla PEFT with per-expert LoRA delta error 0.0;
    • reload under Unsloth byte-equal.
  • Adapters written by the current release still reload under Unsloth unchanged.
  • Studio's test_multi_lora_trainer.py: 45 passed on both base and this branch.

Not covered

  • GPU: no run of the multi-LoRA expert kernels. The change is a CPU-side state-dict rewrite at export.
  • transformers 4.57.6: not run.
  • Llama-4 multi-LoRA forward: fails on main and on this branch alike, before any export. Zoo's Llama-4 MoE forward passes E*T rows, and MultiLoraExperts then asks for set_multi_lora_batch. That needs a separate fix.

adapter_state_dict builds expert factors as the installed PEFT reads Zoo's
re-laid-out stacks, but never applies Zoo's save-time factor swap
(moe_lora_factor_swap). Studio's batched trainer writes that dict directly,
so a Qwen3-VL-MoE expert adapter exported through multi-LoRA was refused
by vanilla transformers + PEFT (size mismatch) while the single-adapter
save of the same model loaded fine.

Apply Zoo's swap_expert_lora_factors under the same rules its patched
get_peft_model_state_dict uses: only flagged experts classes, only the
rank-major packing, never a square pair. An older Zoo without the module
keeps the previous behaviour.

The new test loads the export in a vanilla child process and checks each
expert's delta against the trained one, then reloads it under Unsloth.
Under UNSLOTH_MOE_LORA_B_LAYOUT=grouped_by_expert the expert lora_B has
PEFT's 2D shape with another column order, so vanilla PEFT loads it with
the experts mixed wherever the factors are not also swapped (PEFT 0.18,
transformers 4.x). A Zoo that saves that lora_B as [out, E, R], which
every PEFT refuses and Zoo's loaders flatten back, now gets the same from
the multi-LoRA export. An older Zoo keeps the 2D save on both paths.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 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 ✅ Completed 2026-11-03T16:51:07.672862Z 4630a80 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.

@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: 4630a8075c

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

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

I cancelled the CI runs on this PR for now, since Codex hasn't converged at the current head yet: the latest push hasn't been reviewed by Codex yet. CI here runs on the team's shared runner pool, and it's backed up right now (380+ runs queued), so runs on code that's still changing push everyone else's back.

Once Codex says "Didn't find any major issues" on the head commit, one push or a re-run brings CI back. A small ask going forward: hold the push until Codex is clean, and batch follow-up fixes into a single push. Thanks!

@danielhanchen
danielhanchen merged commit 02c8fa4 into main Nov 4, 2026
@danielhanchen
danielhanchen deleted the fix-multi-lora-expert-vanilla-export branch November 4, 2026 08:53
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.

2 participants