fix(onnx): preserve all MoE experts during ONNX export (#42) - #74
Merged
Conversation
Problem: During ONNX export, PyTorch tracing mode cannot capture data-dependent control flow. Inactive MoE experts were permanently stripped from the exported graph, resulting in only 7 Conv nodes instead of the expected 11. Root Cause: The dynamic expert routing (top-k selection) uses Python-level control flow that tracing cannot follow. Experts not activated by the dummy input during tracing are lost from the final ONNX graph. Fix: Detect ONNX export mode via torch.onnx.is_in_onnx_export and switch to a dense computation path that statically iterates over ALL experts using torch.gather, ensuring every expert node is preserved. Files Modified: utils.py (BatchedExpertComputation), modules.py (OptimizedMOE, OptimizedMOEImproved), experts.py (SharedInvertedExpertGroup) Verification: All 7 tests pass. ONNX graph: 11 Conv nodes (was 7), no If operators. Numerical consistency: max diff < 2.4e-6 (threshold: 1e-4). Export requires: dynamo=False, opset_version=18. Closes #42
isLinXu
added a commit
that referenced
this pull request
Jul 6, 2026
fix(onnx): preserve all MoE experts during ONNX export (#42)
isLinXu
added a commit
that referenced
this pull request
Jul 6, 2026
fix(onnx): preserve all MoE experts during ONNX export (#42)
isLinXu
added a commit
that referenced
this pull request
Jul 9, 2026
fix(onnx): preserve all MoE experts during ONNX export (#42)
isLinXu
added a commit
that referenced
this pull request
Jul 11, 2026
fix(onnx): preserve all MoE experts during ONNX export (#42)
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.
Problem: During ONNX export, PyTorch tracing mode cannot capture data-dependent control flow. Inactive MoE experts were permanently stripped from the exported graph, resulting in only 7 Conv nodes instead of the expected 11.
Root Cause: The dynamic expert routing (top-k selection) uses Python-level control flow that tracing cannot follow. Experts not activated by the dummy input during tracing are lost from the final ONNX graph.
Fix: Detect ONNX export mode via torch.onnx.is_in_onnx_export and switch to a dense computation path that statically iterates over ALL experts using torch.gather, ensuring every expert node is preserved.
Files Modified: utils.py (BatchedExpertComputation), modules.py (OptimizedMOE, OptimizedMOEImproved), experts.py (SharedInvertedExpertGroup)
Verification: All 7 tests pass. ONNX graph: 11 Conv nodes (was 7), no If operators. Numerical consistency: max diff < 2.4e-6 (threshold: 1e-4). Export requires: dynamo=False, opset_version=18.
Closes #42