Skip to content

Studio: batch MoE fine-tuning jobs with per-job expert LoRA - #15809

Merged
danielhanchen merged 47 commits into
mainfrom
studio-ft-batch-moe-hybrid
Oct 28, 2026
Merged

danielhanchen merged 47 commits into
mainfrom
studio-ft-batch-moe-hybrid

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Queued LoRA / QLoRA fine-tuning jobs on fused-expert MoE models (Qwen3 MoE, Qwen3.5 / Qwen3.6 MoE such as Qwen3.6-35B-A3B) now batch together like dense models, with each job's expert LoRA on its own adapter through the expert multi-LoRA kernel from #15742. Until now every MoE job trained alone.

What batches:

  • qwen3_moe, qwen3_5_moe and qwen3_5_moe_text on transformers 5.x (expert-only targets such as just gate_up_proj included), where their experts are fused stacks that Unsloth Zoo's MoE forward reads LoRA for. Each job gets the expert LoRA its single run gets: the same PEFT target_parameters (from get_moe_target_parameters), the same per-expert init (fan-in over the input dimension, as PEFT inits its (E*r, in) matrix) and the same rank and alpha. Its saved adapter is the PEFT target_parameters adapter the single run saves.
  • Everything else that is MoE still trains alone, as before: other MoE layouts (per-expert Linears, transformers 4.57, packed or remote experts), jobs with a router auxiliary loss (the load-balancing loss would average every job's tokens together), and an installed Unsloth without the expert kernel.
  • State-space and short-convolution mixers (Mamba, Jamba, Zamba, Falcon-H1, Nemotron-H, LFM2 and similar) are now refused for batching up front by config and train alone; their recurrent and conv state is not checked batched. Qwen3.5's Gated DeltaNet layers are not matched by this and keep batching.

Tests (CPU, studio/backend/tests/test_multi_lora_trainer.py, test_openai_fine_tuning.py): a batched MoE member's losses, gradients and saved adapter (dense and expert LoRA) match its single run and do not move with its neighbour; every expert lora_B trains; MoE layouts and configs outside the list, router aux loss and SSM mixers are routed to train alone. 128 pass.

GPU, through the OpenAI-compatible API on a fresh install.sh --local Studio (tiny Qwen3.6 MoE, trl-internal-testing/tiny-Qwen3_5MoeForConditionalGeneration-3.6; a job trained alone, then the same job batched beside another while a third holds the GPU; 16 documents, 20 steps):

mode (L4, bf16) step 1 loss alone / batched step 10 step 20 learning rate saved adapter
4-bit 12.3861 / 12.3862 12.1959 / 12.1939 12.1697 / 12.1669 identical every step same 28 tensors and shapes
16-bit 12.3844 / 12.3843 12.1952 / 12.1944 12.1696 / 12.1678 identical every step same 28 tensors and shapes

On a T4 (fp16, each job with its own loss scale): 4-bit step 1 12.3861 / 12.3861, step 20 12.1697 / 12.1669; 16-bit step 1 12.3844 / 12.3844, step 20 12.1696 / 12.1679; learning rate identical every step, no skipped steps.

Every run trained all 20 steps on the same 8240 tokens, and the same with packing off. Step 1 gradient norms differ (0.64 alone, 0.55 batched) while the step 1 losses match: a batched member draws its LoRA A from its own seed (the same Kaiming distribution PEFT uses, independent of batch order, as dense members already do since #15344), so the random A differs from the single run's draw and with it the first gradient; the saved adapters hold the same tensors with norms within 5%.

Expert kernel tests (tests/test_multi_lora_experts.py, from #15742) pass on an L4 inside each of Studio's transformers environments.

[skip ci]
…brid

# Conflicts:
#	studio/backend/tests/test_multi_lora_trainer.py
# Conflicts:
#	studio/backend/tests/test_multi_lora_trainer.py
…r; pack with the single job's worker count

[skip ci]
# Conflicts:
#	studio/backend/tests/test_multi_lora_trainer.py
…ybrid

# Conflicts:
#	studio/backend/core/training/multi_lora_trainer.py
#	studio/backend/tests/test_multi_lora_trainer.py
…ybrid

# Conflicts:
#	studio/backend/core/training/multi_lora_trainer.py
#	studio/backend/tests/test_multi_lora_trainer.py
…ybrid

# Conflicts:
#	unsloth/kernels/multi_lora.py
@danielhanchen danielhanchen self-assigned this Oct 28, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 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-10-28T16:17:11.937498Z 1089103 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: 8921aaa296

ℹ️ 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 +1037 to +1040
expert_targets = _expert_targets(model, _lone_target_modules(config.get("target_modules")))
if expert_targets:
from unsloth.kernels.multi_lora import inject_multi_lora_experts
inject_multi_lora_experts(

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 Allow expert-only targets before requiring dense LoRA layers

For a Qwen3 MoE job targeting only gate_up_proj (or only gate_proj/up_proj on its fused-expert layout), there are no matching dense Linear modules. The unconditional inject_multi_lora() call above therefore raises ValueError: No linear layers named ... found before this expert injection is reached. These requests previously trained individually through get_moe_target_parameters(), but _moe_batches() now admits them to batching, so otherwise valid queued jobs fail simply because another matching job is present. Resolve expert targets first and permit an expert-only adapter without requiring a dense injection, or keep these requests on the single-job path.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 1089103: expert targets are resolved first, and when the requested names match no dense layer but do match fused experts (e.g. only gate_up_proj), the batch trains the expert LoRA alone, as the single job does; the saved adapter config keeps the requested target_modules. New test: an expert-only target trains batched and saves only expert tensors, all trained.


def _routed_modules(model):
"""The modules a forward calls: dense LoRA layers and expert containers (their stacks are not modules)."""
from unsloth.kernels.multi_lora import MultiLoraExperts, MultiLoraLinear

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 Preserve dense batching without the expert kernel installed

When Studio uses an installed Unsloth that has dense multi-LoRA kernels but predates expert support, _multi_lora_available() still enables dense batching and _multi_lora_experts_available() only excludes MoE jobs. However, _check_routed() now unconditionally calls this helper, whose import of MultiLoraExperts fails on that installation, causing every dense batch to error after loading and tokenizing. Keep the expert class optional for dense jobs, or update the batching capability check so such installations fall back to individual training.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 1089103: the routing check now takes MultiLoraExperts only when the installed kernel has it, so an Unsloth that predates expert support keeps batching dense jobs (MoE jobs are already routed alone there by _multi_lora_experts_available). New test runs the check against a kernel module with only MultiLoraLinear; it failed with the ImportError before.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 10891037bd

ℹ️ 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 2a9f230 into main Oct 28, 2026
@danielhanchen
danielhanchen deleted the studio-ft-batch-moe-hybrid branch October 28, 2026 16:49
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.

1 participant