Repository navigation
Multi-LoRA: export expert LoRA in the layout vanilla PEFT loads - #16568
Conversation
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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
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! |
Multi-LoRA expert adapters exported through
adapter_state_dictcould 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_dictinunsloth/kernels/multi_lora.pynever 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 withsize mismatch(for examplelora_A [16, 64]vs[16, 40]).Who hits it:
unsloth.kernels.multi_loraPython API. Studio's batched trainer only allows Qwen3-MoE / Qwen3.5-MoE today, and those experts were already exported correctly.grouped_by_expertchange below also reaches Studio: Qwen3-MoE jobs withUNSLOTH_MOE_LORA_B_LAYOUT=grouped_by_expert, once the matching unsloth-zoo change is installed.Change
adapter_state_dictnow runs the export through a small helper that reuses zoo's own swap decision and converter, with nothing duplicated here.grouped_by_expertmode, 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:size mismatch), 1 skippedtests/test_multi_lora_experts.pyEvidence 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.
test_multi_lora_trainer.py: 45 passed on both base and this branch.Not covered
E*Trows, andMultiLoraExpertsthen asks forset_multi_lora_batch. That needs a separate fix.