-
Notifications
You must be signed in to change notification settings - Fork 2k
[#9717][chore] Refactor MoE code to use enums for configuration #9796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
tcherckez-nvidia
wants to merge
3
commits into
NVIDIA:main
from
tcherckez-nvidia:refactor/moe-enums-config
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
556 changes: 400 additions & 156 deletions
556
tensorrt_llm/_torch/auto_deploy/custom_ops/fused_moe/torch_moe.py
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| Shared enums for AutoDeploy. | ||
| """ | ||
|
|
||
| from enum import Enum | ||
|
|
||
|
|
||
| class MLPStyle(Enum): | ||
| """MLP style for MoE layers.""" | ||
|
|
||
| GATED_MLP = "gated_mlp" # Mixtral/DeepSeek/Llama4-style: y = W2(act(W1 x) * (W3 x)) | ||
| MLP = "mlp" # NemotronH-style 2-layer: y = W_down(act(W_up x)) | ||
|
|
||
|
|
||
| class ActivationFunction(Enum): | ||
| """Activation functions for MoE layers.""" | ||
|
|
||
| SILU = "silu" # SiLU activation | ||
| RELU2 = "relu2" # ReLU then square | ||
| SWIGLU = "swiglu" # SwiGLU activation | ||
|
|
||
|
|
||
| class WeightsFormat(Enum): | ||
| """Weight tensor organization for MoE layers.""" | ||
|
|
||
| PER_EXPERT = "per_expert" # Separate weight tensors per expert in lists | ||
| STACKED = "stacked" # All expert weights stacked in single tensors | ||
|
|
||
|
|
||
| class WeightsFusion(Enum): | ||
| """Weight tensor ordering and storage for gated MLP layers.""" | ||
|
|
||
| GATE_UP_DOWN = "w1_w2_w3_separate" # w1, w2, w3 stored separately (matches parameter order) | ||
| GATEUP_DOWN = ( | ||
| "w1w3_w2" # w1 and w3 concatenated as [w1, w3], w2 separate (Llama4 native format) | ||
| ) | ||
| UPGATE_DOWN = ( | ||
| "w3w1_w2" # w3 and w1 concatenated as [w3, w1], w2 separate | ||
| # (TRT-LLM format, Llama4 weights swapped during load) | ||
| ) | ||
|
|
||
|
|
||
| def mlp_style_from_str(s: str) -> MLPStyle: | ||
| """Convert string to MLPStyle enum.""" | ||
| s = s.lower() | ||
| for style in MLPStyle: | ||
| if style.value == s: | ||
| return style | ||
| valid_values = [style.value for style in MLPStyle] | ||
| raise ValueError(f"Unknown mlp_style '{s}'. Valid values: {valid_values}") | ||
|
|
||
|
|
||
| def act_fn_from_str(s: str) -> ActivationFunction: | ||
| """Convert string to ActivationFunction enum.""" | ||
| s = s.lower() | ||
| for act in ActivationFunction: | ||
| if act.value == s: | ||
| return act | ||
| valid_values = [act.value for act in ActivationFunction] | ||
| raise ValueError(f"Unknown act_fn '{s}'. Valid values: {valid_values}") | ||
|
|
||
|
|
||
| def weights_format_from_str(s: str) -> WeightsFormat: | ||
| """Convert string to WeightsFormat enum.""" | ||
| s = s.lower() | ||
| for fmt in WeightsFormat: | ||
| if fmt.value == s: | ||
| return fmt | ||
| valid_values = [fmt.value for fmt in WeightsFormat] | ||
| raise ValueError(f"Unknown weights_format '{s}'. Valid values: {valid_values}") | ||
|
|
||
|
|
||
| def weights_fusion_from_str(s: str) -> WeightsFusion: | ||
| """Convert string to WeightsFusion enum.""" | ||
| s = s.lower() | ||
| for fusion in WeightsFusion: | ||
| if fusion.value == s: | ||
| return fusion | ||
| valid_values = [fusion.value for fusion in WeightsFusion] | ||
| raise ValueError(f"Unknown weights_fusion '{s}'. Valid values: {valid_values}") | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.