Skip to content

[megatron] Convert fused-MoE expert LoRA to vLLM layout on adapter sync#1857

Merged
erictang000 merged 2 commits into
NovaSky-AI:mainfrom
casper-hansen:casper/vllm-fused-moe-lora-export
Jul 2, 2026
Merged

[megatron] Convert fused-MoE expert LoRA to vLLM layout on adapter sync#1857
erictang000 merged 2 commits into
NovaSky-AI:mainfrom
casper-hansen:casper/vllm-fused-moe-lora-export

Conversation

@casper-hansen

Copy link
Copy Markdown
Contributor

Closes #1852 @erictang000

Megatron-Bridge exports fused-MoE expert LoRA as 3D (num_experts-leading) tensors keyed ...mlp.experts.gate_up_proj / ...mlp.experts.down_proj, but vLLM's fused-MoE LoRA loader (FusedMoE3DWithLoRA / _stack_moe_lora_weights) expects the flat PEFT layout keyed ...mlp.experts.base_layer (w13) and ...mlp.experts (w2). Without the rewrite, merge_lora=False on-policy sync is rejected because load_lora_adapter cannot resolve experts.down_proj.

Add _convert_moe_experts_lora_to_vllm to megatron_utils and apply it in MegatronPolicyWorkerBase._save_lora_adapters_and_sync before inferring target modules (dropping the synthetic base_layer module).

Megatron-Bridge exports fused-MoE expert LoRA as 3D (num_experts-leading)
tensors keyed `...mlp.experts.gate_up_proj` / `...mlp.experts.down_proj`,
but vLLM's fused-MoE LoRA loader (FusedMoE3DWithLoRA /
_stack_moe_lora_weights) expects the flat PEFT layout keyed
`...mlp.experts.base_layer` (w13) and `...mlp.experts` (w2). Without the
rewrite, merge_lora=False on-policy sync is rejected because
load_lora_adapter cannot resolve `experts.down_proj`.

Add `_convert_moe_experts_lora_to_vllm` to megatron_utils and apply it in
MegatronPolicyWorkerBase._save_lora_adapters_and_sync before inferring
target modules (dropping the synthetic `base_layer` module).

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a helper function _convert_moe_experts_lora_to_vllm to convert fused-MoE expert LoRA tensors from Megatron-Bridge's 3D layout to the flat PEFT layout expected by vLLM, ensuring compatibility during on-policy synchronization. Feedback suggests unpacking the tensor shapes explicitly before reshaping or permuting to improve code readability, prevent potential bugs during future refactoring, and avoid relying on tensor.shape after partial reassignments.

Comment thread skyrl/backends/skyrl_train/distributed/megatron/megatron_utils.py
@erictang000 erictang000 self-assigned this Jul 1, 2026
Condense the docstrings and inline comments added in this PR while
preserving their technical content.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@erictang000 erictang000 merged commit a479991 into NovaSky-AI:main Jul 2, 2026
4 of 6 checks passed
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.

[Bug] Megatron MoE LoRA with merge_lora=False fails the weight sync on Qwen3.6-35B MoE

2 participants