[Llama4] Adopt @use_experts_implementation standard interface#45989
Closed
Chao1Han wants to merge 1 commit into
Closed
[Llama4] Adopt @use_experts_implementation standard interface#45989Chao1Han wants to merge 1 commit into
@use_experts_implementation standard interface#45989Chao1Han wants to merge 1 commit into
Conversation
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: llama4 |
Refactor Llama4TextExperts to use the standard @use_experts_implementation decorator with the 3-argument forward interface (hidden_states, top_k_index, top_k_weights), aligning with other MoE models (Gemma4, Qwen3-MoE, etc). Key changes: - Add @use_experts_implementation(is_transposed=True) to Llama4TextExperts - Change experts forward from single pre-weighted tensor to standard 3-arg - Update Llama4Router to return (router_logits, top_k_weights, top_k_index) - Simplify Llama4TextMoe.forward to use standard calling convention Benefits: - Automatically supports batched_mm, grouped_mm backends via config - Compatible with existing EP/TP hooks in tensor_parallel.py - Consistent interface across all MoE models in transformers Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2e36317 to
e5c8338
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Llama4's
Llama4TextExpertscurrently uses a non-standard single-argument forward interface where routing weights are pre-multiplied into the hidden states before being passed to experts:This differs from all other MoE models in transformers (Gemma4, Qwen3-MoE, DeepSeek, PhiMoE, etc.) which use the standard 3-argument interface via
@use_experts_implementation:This inconsistency causes problems:
tensor_parallel.py(MoeTensorParallelExperts) assume the standard 3-arg interface — requiring special-case handling for Llama4batched_mmorgrouped_mmoptimized implementations(router_scores, router_logits)(2 values) whileRouterParallel._prepare_output_fnexpects(router_logits, router_scores, router_indices)(3 values), breaking EP mode entirelyChanges
modeling_llama4.pyLlama4TextExperts: Add@use_experts_implementation(is_transposed=True)decorator and change forward signature to(self, hidden_states, top_k_index, top_k_weights)is_transposed=Truebecause Llama4 stores weights as[E, H, 2*I]instead of standard[E, 2*I, H]Llama4Router: Change return from(router_scores, router_logits)to(router_logits, top_k_weights, top_k_index)— consistent with other routers (Gemma4, etc.) and compatible withRouterParallelEP hooksLlama4TextMoe.forward: Simplify to standard calling convention:Benefits
experts_implementation="batched_mm"/"grouped_mm"works out of the boxtensor_parallel.pywork without special casestensor_parallel.py— unlike FixMoeTensorParalellExpertscrash for Llama4 pre-weighted MoE experts (EP support) #45976 which requires modifyingMoeTensorParallelExpertsto handle the pre-weighted single-tensor conventionComparison with #45976
tensor_parallel.pylen(inputs) == 1)modeling_llama4.pyexperts_implementationsupportTesting
Verified locally:
experts_implementation=None(eager bmm): ✅ correct outputexperts_implementation="batched_mm": ✅ numerically matches eager (max diff < 1e-8)from_config(): ✅ logits shape correct_can_set_experts_implementation()heuristic: ✅ correctly detects decorator