-
Notifications
You must be signed in to change notification settings - Fork 202
[codex] perf: fuse MiniMax M3 allreduce and Gemma RMSNorm on MI300X #1778
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
Open
Oseltamivir
wants to merge
1
commit into
main
Choose a base branch
from
codex/minimax-m3-mi300x-aiter-rmsnorm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
141 changes: 141 additions & 0 deletions
141
benchmarks/single_node/fixed_seq_len/minimaxm3_mi300x_deferred_ffn_ar.patch
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,141 @@ | ||
| diff --git a/vllm/models/minimax_m3/amd/model.py b/vllm/models/minimax_m3/amd/model.py | ||
| index 27650c8e6..ac5e260a8 100644 | ||
| --- a/vllm/models/minimax_m3/amd/model.py | ||
| +++ b/vllm/models/minimax_m3/amd/model.py | ||
| @@ -17,6 +17,7 @@ The MiniMax-M3-preview config selects a single set of branches: | ||
| "index" attention branch. | ||
| """ | ||
|
|
||
| +import os | ||
| from collections.abc import Iterable | ||
|
|
||
| import torch | ||
| @@ -37,10 +38,12 @@ from vllm.model_executor.layers.attention_layer_base import AttentionLayerBase | ||
| from vllm.model_executor.layers.fused_allreduce_gemma_rms_norm import ( | ||
| fused_allreduce_gemma_rms_norm, | ||
| + initialize_m3_aiter_allreduce, | ||
| ) | ||
| from vllm.model_executor.layers.fused_moe import ( | ||
| FusedMoE, | ||
| GateLinear, | ||
| + MoERunner, | ||
| fused_moe_make_expert_params_mapping, | ||
| ) | ||
| ) | ||
| from vllm.model_executor.layers.linear import ( | ||
| @@ -117,6 +119,17 @@ def _is_moe_layer(config: PretrainedConfig, layer_id: int) -> bool: | ||
| return moe_layer_freq[layer_id] != 0 | ||
|
|
||
|
|
||
| +def _defer_ffn_allreduce() -> bool: | ||
| + """Whether M3 FFN reductions are completed by the following Gemma norm.""" | ||
| + parallel_config = get_current_vllm_config().parallel_config | ||
| + return ( | ||
| + os.getenv("M3_AITER_AR_RMS_MODE") in {"control", "fused"} | ||
| + and parallel_config.tensor_parallel_size > 1 | ||
| + and parallel_config.pipeline_parallel_size == 1 | ||
| + and parallel_config.data_parallel_size == 1 | ||
| + ) | ||
| + | ||
| + | ||
| def _build_rotary_emb(config: PretrainedConfig, head_dim: int): | ||
| """Build the (partial NeoX) RoPE, honoring an optional ``rope_scaling`` config. | ||
|
|
||
| @@ -243,6 +256,25 @@ class MiniMaxM3MLP(nn.Module): | ||
| return x | ||
|
|
||
|
|
||
| +class MiniMaxM3DeferredMoERunner(MoERunner): | ||
| + """Leave the M3 MoE output rank-local for the following fused AR+RMSNorm.""" | ||
| + | ||
| + def _maybe_reduce_final_output( | ||
| + self, | ||
| + states: torch.Tensor, | ||
| + trunc_size: int | None, | ||
| + ) -> torch.Tensor: | ||
| + if self._fused_output_is_reduced: | ||
| + raise RuntimeError( | ||
| + "M3 deferred MoE allreduce requires an unreduced MoE backend" | ||
| + ) | ||
| + if self.moe_config.is_sequence_parallel: | ||
| + raise RuntimeError( | ||
| + "M3 deferred MoE allreduce does not support sequence parallelism" | ||
| + ) | ||
| + return states[..., :trunc_size] if trunc_size is not None else states | ||
| + | ||
| + | ||
| class MiniMaxM3MoE(nn.Module): | ||
| """Sigmoid-routed MoE block with a routing-bias correction and a shared | ||
| expert.""" | ||
| @@ -316,6 +348,9 @@ class MiniMaxM3MoE(nn.Module): | ||
| shared_experts=self.shared_experts, | ||
| quant_config=quant_config, | ||
| prefix=f"{prefix}.experts", | ||
| + runner_cls=MiniMaxM3DeferredMoERunner | ||
| + if _defer_ffn_allreduce() | ||
| + else None, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| @@ -732,6 +767,8 @@ class MiniMaxM3DecoderLayer(nn.Module): | ||
| # with the layer's index. | ||
| layer_id = int(prefix.split(sep=".")[-1]) | ||
| self.layer_id = layer_id | ||
| + self.defer_ffn_allreduce = _defer_ffn_allreduce() | ||
| + self.fuse_input_allreduce = self.defer_ffn_allreduce and layer_id > 0 | ||
|
|
||
| is_sparse_attention_layer = ( | ||
| force_sparse_attn or layer_id in _sparse_attention_layer_ids(config) | ||
| @@ -769,6 +806,7 @@ class MiniMaxM3DecoderLayer(nn.Module): | ||
| config=config, | ||
| intermediate_size=config.dense_intermediate_size, | ||
| quant_config=quant_config, | ||
| + reduce_results=not self.defer_ffn_allreduce, | ||
| prefix=f"{prefix}.mlp", | ||
| ) | ||
|
|
||
| @@ -787,11 +825,16 @@ class MiniMaxM3DecoderLayer(nn.Module): | ||
| residual: torch.Tensor | None, | ||
| ) -> tuple[torch.Tensor, torch.Tensor]: | ||
| # Self Attention | ||
| - if residual is None: | ||
| - residual = hidden_states | ||
| - hidden_states = self.input_layernorm(hidden_states) | ||
| + if self.fuse_input_allreduce and residual is not None: | ||
| + hidden_states, residual = fused_allreduce_gemma_rms_norm( | ||
| + hidden_states, residual, self.input_layernorm | ||
| + ) | ||
| else: | ||
| - hidden_states, residual = self.input_layernorm(hidden_states, residual) | ||
| + if residual is None: | ||
| + residual = hidden_states | ||
| + hidden_states = self.input_layernorm(hidden_states) | ||
| + else: | ||
| + hidden_states, residual = self.input_layernorm(hidden_states, residual) | ||
| hidden_states = self.self_attn( | ||
| positions=positions, | ||
| hidden_states=hidden_states, | ||
| @@ -815,6 +858,9 @@ class MiniMaxM3Model(nn.Module): | ||
| cache_config = vllm_config.cache_config | ||
| quant_config = vllm_config.quant_config | ||
| self.config = config | ||
| + self.defer_ffn_allreduce = _defer_ffn_allreduce() | ||
| + if self.defer_ffn_allreduce: | ||
| + initialize_m3_aiter_allreduce() | ||
|
|
||
| self.vocab_size = config.vocab_size | ||
|
|
||
| @@ -856,7 +902,12 @@ class MiniMaxM3Model(nn.Module): | ||
| for layer in self.layers[self.start_layer : self.end_layer]: | ||
| hidden_states, residual = layer(positions, hidden_states, residual) | ||
|
|
||
| - hidden_states, _ = self.norm(hidden_states, residual) | ||
| + if self.defer_ffn_allreduce: | ||
| + hidden_states, _ = fused_allreduce_gemma_rms_norm( | ||
| + hidden_states, residual, self.norm | ||
| + ) | ||
| + else: | ||
| + hidden_states, _ = self.norm(hidden_states, residual) | ||
| return hidden_states | ||
|
|
||
| def get_expert_mapping(self) -> list[tuple[str, str, int, str]]: |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Result suffix ignores concurrency override
Medium Severity
When
m3-aiter-ar-rms-modeisfusedand concurrency is 1, the MI300X recipe forcesM3_AITER_AR_RMS_MODEtooff, butEXPERIMENT_SUFFIXandRESULT_FILENAMEstill use the workflow input (_m3ar-fused). Stored artifacts and job labels can describe a fused run while the server actually used the default path.Additional Locations (2)
benchmarks/single_node/fixed_seq_len/minimaxm3_fp8_mi300x.sh#L38-L44.github/workflows/benchmark-tmpl.yml#L186-L187Reviewed by Cursor Bugbot for commit 9f83809. Configure here.