Repository navigation
[LFM2-MoE] Support Transformers v5 packed MoE expert weights - #29659
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for loading packed 3D MoE expert weights (gate_up_proj and down_proj) from Transformers >= v5.0 into the fused w13_weight and w2_weight parameters for the LFM2-MoE model. Feedback was provided to explicitly raise a ValueError instead of silently skipping weight loading if the loaded weight tensor does not have exactly 3 dimensions, preventing potential uninitialized weight issues.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if fused_name in params_dict and loaded_weight.dim() == 3: | ||
| param = params_dict[fused_name] |
There was a problem hiding this comment.
If fused_name is in params_dict but loaded_weight does not have 3 dimensions, the weight loading will be silently skipped, which can lead to uninitialized weights and hard-to-debug runtime issues. It is safer to explicitly raise a ValueError if the dimension is unexpected.
if fused_name in params_dict:
if loaded_weight.dim() != 3:
raise ValueError(
f"Expected 3D tensor for packed MoE weight {name}, "
f"but got {loaded_weight.dim()}D tensor with shape {tuple(loaded_weight.shape)}"
)
param = params_dict[fused_name]| if fused_name in params_dict and loaded_weight.dim() == 3: | ||
| param = params_dict[fused_name] |
There was a problem hiding this comment.
If fused_name is in params_dict but loaded_weight does not have 3 dimensions, the weight loading will be silently skipped. Explicitly raising a ValueError here prevents silent failures and ensures any shape mismatch is caught early.
if fused_name in params_dict:
if loaded_weight.dim() != 3:
raise ValueError(
f"Expected 3D tensor for packed MoE weight {name}, "
f"but got {loaded_weight.dim()}D tensor with shape {tuple(loaded_weight.shape)}"
)
param = params_dict[fused_name]Transformers >= v5.0 (huggingface/transformers#41580) packs MoE expert weights into a single 3D tensor per projection (`experts.gate_up_proj` / `experts.down_proj`) instead of per-expert weights (`experts.{i}.w{1,2,3}.weight`). An in-memory Transformers v5 model therefore exposes the packed layout in its state_dict -- this is the layout seen on the `update_weights_from_tensor` / RLHF weight-sync path -- and `lfm2_moe.py`'s loader, which only handles the per-expert format, silently skips those tensors, leaving the experts uninitialized (garbage output). Map the packed tensors onto the fused FusedMoE params per expert: gate_up_proj -> w13_weight (split into w1/w3 halves), down_proj -> w2_weight. The per-expert path is unchanged, so on-disk pre-v5 checkpoints keep loading as before; the new block only triggers on the packed names. LFM2-MoE packs out-features-major (gate_up_proj as [num_experts, 2*intermediate, hidden]), which already matches the FusedMoE w13_weight/w2_weight layout, so no transpose is required. Verified on MI325X with an LFM2-8B-A1B checkpoint converted to the Transformers v5 packed format: without this patch the packed checkpoint produces garbage ("- - - -"); with it the output is identical to the original per-expert checkpoint. Co-Authored-By: Changyi Yang <[email protected]>
8c87f27 to
5e3731f
Compare
|
Thanks @gemini-code-assist — good catch. Applied to both the |
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
|
/tag-and-rerun-ci |
…ject#29659) Co-authored-by: Piotr Mazurek <[email protected]> Co-authored-by: Changyi Yang <[email protected]> return collect_test_items update
…ped in v0.5.15 Ancestry-checked each rolling-:dev model against v0.5.15.post1. Support that landed in the release moves to the stable tag: - Baidu Unlimited-OCR (sgl-project#29186), LFM2.5 (sgl-project#29659) -> :latest - MiniCPM-V-4.6 (sgl-project#24855): :dev -> :latest, :dev-cu12 -> :latest-cu129 Kept on :dev (support NOT in the release cut): - LongCat-2.0 (FP8 support sgl-project#30275 not in v0.5.15.post1) - Hy3 (HunyuanV3 NextN/MTP fix sgl-project#30331 not in release; MTP is a headline feature) Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ject#29659) Co-authored-by: Piotr Mazurek <[email protected]> Co-authored-by: Changyi Yang <[email protected]>
What
Adds support for loading LFM2-MoE expert weights in the Transformers ≥ v5.0 packed format to
lfm2_moe.py'sload_weights.Transformers ≥ 5.0 (huggingface/transformers#41580, shipped in v5.0.0) packs MoE expert weights into a single 3D tensor per projection:
…experts.{i}.w1.weight,…experts.{i}.w3.weight…experts.gate_up_proj[E, 2·I, H]…experts.{i}.w2.weight…experts.down_proj[E, H, I]Why we need this
The current loader only understands the per-expert names. When weights arrive in the packed layout — which is what an in-memory Transformers v5 model exposes in its
state_dict, i.e. theupdate_weights_from_tensor/ RLHF (verl) weight-sync path — the packed expert tensors match no name in the loader and are silently dropped. The experts stay uninitialised and the model emits garbage, with no error raised.This is increasingly common: anyone doing RL / online weight updates on LFM2-MoE with a Transformers ≥ 5 training stack hits it. Plain on-disk inference is unaffected (pre-v5 checkpoints are still per-expert), which is why it slips through until the weight-sync path is exercised.
Fix
Map the packed tensors onto the fused
FusedMoEparams per expert:gate_up_proj → w13_weight(split into w1/w3 halves),down_proj → w2_weight. The existing per-expert path is untouched, so pre-v5 on-disk checkpoints load exactly as before; the new block only triggers on the packedexperts.gate_up_proj/experts.down_projnames.LFM2-MoE packs out-features-major (
gate_up_projas[E, 2·I, H]), which already matches theFusedMoEw13_weight/w2_weightlayout, so no transpose is needed (unlike GPT-OSS). Per-expert loading with an explicit integerexpert_idis used because the whole-tensorexpert_id=Nonefused path is gated to mxfp4-quantised models only — it would not work for unquantised bf16 LFM2-MoE.How to reproduce
A public fixture is provided:
tugot17/LFM2-8B-A1B-5.2-packed—LFM2-8B-A1Bconverted to the Transformers v5 packed format (authentic packed shapes:gate_up_proj (32, 3584, 2048),down_proj (32, 2048, 1792))." – – – – – – – – – – …"(garbage — experts dropped)" Paris. It is located in the northern part of the country, along the Seine"— identical to loading the original per-expertLiquidAI/LFM2-8B-A1B.Backward compatibility
The old per-expert on-disk format was tested to confirm no regression — loading
LiquidAI/LFM2-8B-A1B(per-expert) produces byte-identical output with and without this patch (the new block only fires on the packedgate_up_proj/down_projnames, so per-expert checkpoints take the unchanged path):mainVerified on MI325X / ROCm 7.2 against this branch (
sglang 0.5.15.dev). The change itself is device/backend-agnostic — it only remaps weight names during loading.Co-Authored-By: Changyi Yang [email protected]
CI States
Latest PR Test (Base): ✅ Run #28386441288
Latest PR Test (Extra): ❌ Run #28386441152