Skip to content

fix(onnx): preserve all MoE experts during ONNX export (#42) - #74

Merged
isLinXu merged 1 commit into
mainfrom
fix/issue42-onnx-moe-expert-loss
Jul 1, 2026
Merged

fix(onnx): preserve all MoE experts during ONNX export (#42)#74
isLinXu merged 1 commit into
mainfrom
fix/issue42-onnx-moe-expert-loss

Conversation

@isLinXu

@isLinXu isLinXu commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

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

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
isLinXu merged commit 9163e96 into main Jul 1, 2026
1 check passed
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)
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.

How to Export an ONNX Model with Dynamic Computation?

1 participant