-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Fusion] [Graph]Add Matmul Allreduce Rmsnorm fusion Pass #5034
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
Merged
realliujiaxu
merged 19 commits into
vllm-project:main
from
wxsIcey:matmul_allreduce_addrmsnorm
Jan 19, 2026
+251
−1
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e07ae3f
[Fusion] [Graph]Add Matmul Allreduce Rmsnorm fusion Pass
wxsIcey 99c22fb
fix
wxsIcey efdc24d
fix
wxsIcey 89fec43
tiny fix
wxsIcey 450812e
fix
wxsIcey 2411842
fix
wxsIcey 96be8ce
add patch
wxsIcey 21593f7
fix
wxsIcey 08af085
fix
wxsIcey 1d31a4c
fix
wxsIcey ef80a9c
default close the matmulallreduceaddrmsnorm fusion
wxsIcey 43528b4
fix
wxsIcey 6425402
fix ut
wxsIcey 168a667
fix
wxsIcey 707eaeb
fix compile range
wxsIcey 5bb521c
fix typo
wxsIcey d602082
fix
wxsIcey 3f3e27f
fix
wxsIcey 4b9d9e3
fix lint
wxsIcey 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
153 changes: 153 additions & 0 deletions
153
vllm_ascend/compilation/passes/allreduce_rmsnorm_fusion_pass.py
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,153 @@ | ||
| # Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. | ||
| # This file is a part of the vllm-ascend project. | ||
| # | ||
| # | ||
| # 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. | ||
| # | ||
| import torch | ||
| import torch._inductor.pattern_matcher as pm | ||
| from torch._inductor.pattern_matcher import PatternMatcherPass, PatternPrettyPrinter | ||
| from vllm.compilation.vllm_inductor_pass import VllmInductorPass | ||
| from vllm.config import VllmConfig | ||
| from vllm.config.compilation import Range | ||
| from vllm.distributed import get_tensor_model_parallel_world_size, tensor_model_parallel_all_reduce | ||
| from vllm.distributed.parallel_state import get_tp_group | ||
| from vllm.logger import logger | ||
|
|
||
| # computation-communication tiling block is 512 | ||
| ALLREDUCE_NORM_FUSE_THREHOLD = 512 | ||
|
|
||
|
|
||
| class MiddleLayerMatmulAllReduceAddRMSNormPattern: | ||
| """ | ||
| recognizing the Matmul+AllReduce+AddRMSNorm computation pattern | ||
| AllReduce is optimized in the fusion operator to a two-stage communication of ReduceScatter+AllGather | ||
| """ | ||
|
|
||
| def __init__(self, vllm_config, eps=1e-6): | ||
| self.vllm_config = vllm_config | ||
| self.eps = eps | ||
| device_group = get_tp_group().device_group | ||
| backend = device_group._get_backend(torch.device("npu")) | ||
| self.local_rank = torch.distributed.get_rank(group=device_group) | ||
| self.tp_group_name = backend.get_hccl_comm_name(self.local_rank) | ||
| self.tp_size = get_tensor_model_parallel_world_size() | ||
|
|
||
| def get_inputs(self): | ||
| batch_size, seq_len = 2, 4 | ||
| hidden_size = 4096 | ||
| x = torch.randn(batch_size, seq_len, hidden_size, device="npu") | ||
| weight = torch.randn(hidden_size, hidden_size, device="npu") | ||
| residual = torch.randn(batch_size, seq_len, hidden_size, device="npu") | ||
| rms_norm_weight = torch.randn(hidden_size, device="npu") | ||
| return [x, weight, residual, rms_norm_weight] | ||
|
|
||
| def register(self, pm_pass: PatternMatcherPass): | ||
| def pattern(x, weight, residual, rms_norm_weight): | ||
| mm = torch.ops.vllm.unquantized_gemm(x, weight, None) | ||
| all_reduce_ = tensor_model_parallel_all_reduce(mm) | ||
| output = torch.ops.npu.npu_add_rms_norm(all_reduce_, residual, rms_norm_weight) | ||
| out0 = output[0] | ||
| out1 = output[2] | ||
|
|
||
| return out0, out1 | ||
|
|
||
| def replacement(x, weight, residual, rms_norm_weight): | ||
| out0, out1 = torch.ops._C_ascend.matmul_allreduce_add_rmsnorm( | ||
| x, | ||
| weight, | ||
| residual, | ||
| rms_norm_weight, | ||
| self.tp_group_name, | ||
| self.tp_size, | ||
| self.local_rank, | ||
| self.eps, | ||
| True, | ||
| False, | ||
| ) | ||
| return out0, out1 | ||
|
|
||
| pm.register_replacement(pattern, replacement, self.get_inputs(), pm.fwd_only, pm_pass) | ||
|
|
||
|
|
||
| class LastLayerMatmulAllReduceAddRMSNormPattern: | ||
| def __init__(self, vllm_config, eps=1e-6): | ||
| self.vllm_config = vllm_config | ||
| self.eps = eps | ||
| device_group = get_tp_group().device_group | ||
| backend = device_group._get_backend(torch.device("npu")) | ||
| self.local_rank = torch.distributed.get_rank(group=device_group) | ||
| self.tp_group_name = backend.get_hccl_comm_name(self.local_rank) | ||
| self.tp_size = get_tensor_model_parallel_world_size() | ||
|
|
||
| def get_inputs(self): | ||
| batch_size, seq_len = 2, 4 | ||
| hidden_size = 4096 | ||
| x = torch.randn(batch_size, seq_len, hidden_size, device="npu") | ||
| weight = torch.randn(hidden_size, hidden_size, device="npu") | ||
| residual = torch.randn(batch_size, seq_len, hidden_size, device="npu") | ||
| rms_norm_weight = torch.randn(hidden_size, device="npu") | ||
| return [x, weight, residual, rms_norm_weight] | ||
|
|
||
| def register(self, pm_pass: PatternMatcherPass): | ||
| def pattern(x, weight, residual, rms_norm_weight): | ||
| mm = torch.ops.vllm.unquantized_gemm(x, weight, None) | ||
| all_reduce_ = tensor_model_parallel_all_reduce(mm) | ||
| output = torch.ops.npu.npu_add_rms_norm(all_reduce_, residual, rms_norm_weight) | ||
|
|
||
| return output[0] | ||
|
|
||
| def replacement(x, weight, residual, rms_norm_weight): | ||
| out0, _ = torch.ops._C_ascend.matmul_allreduce_add_rmsnorm( | ||
| x, | ||
| weight, | ||
| residual, | ||
| rms_norm_weight, | ||
| self.tp_group_name, | ||
| self.tp_size, | ||
| self.local_rank, | ||
| self.eps, | ||
| True, | ||
| False, | ||
| ) | ||
| return out0 | ||
|
|
||
| pm.register_replacement(pattern, replacement, self.get_inputs(), pm.fwd_only, pm_pass) | ||
|
|
||
|
|
||
| class MatmulAllReduceAddRMSNormPass(VllmInductorPass): | ||
| def __init__(self, vllm_config: VllmConfig): | ||
| super().__init__(vllm_config) | ||
| self.pattern_match_passes: PatternMatcherPass = PatternMatcherPass(pass_name="allreduce_rmsnorm_fusion_pass") | ||
|
|
||
| MiddleLayerMatmulAllReduceAddRMSNormPattern(vllm_config).register(self.pattern_match_passes) | ||
| LastLayerMatmulAllReduceAddRMSNormPattern(vllm_config).register(self.pattern_match_passes) | ||
|
|
||
| def __call__(self, graph: torch.fx.Graph): | ||
| self.begin() | ||
| self.matched_count = self.pattern_match_passes.apply(graph) | ||
| pattern_idx = 0 | ||
| for pattern_entry in self.pattern_match_passes.patterns.values(): | ||
| for p in pattern_entry: | ||
| p_str = PatternPrettyPrinter.run(p.pattern) | ||
| logger.debug("Pattern %d: %s", pattern_idx, p_str) | ||
| pattern_idx += 1 | ||
| logger.debug("Replaced %s patterns", self.matched_count) | ||
| self.end_and_log() | ||
|
|
||
| def is_applicable_for_range(self, compile_range: Range) -> bool: | ||
| """ | ||
| Check if the pass is applicable for the current configuration. | ||
| """ | ||
| applicable = compile_range.start > ALLREDUCE_NORM_FUSE_THREHOLD | ||
| return applicable | ||
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
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,57 @@ | ||
| # | ||
| # Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. | ||
| # This file is a part of the vllm-ascend project. | ||
| # | ||
| # 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. | ||
| # | ||
| import torch | ||
| import vllm.model_executor.layers.utils | ||
| from vllm.utils.torch_utils import direct_register_custom_op | ||
|
|
||
|
|
||
| def unquantized_gemm( | ||
| x: torch.Tensor, | ||
| weight: torch.Tensor, | ||
| bias: torch.Tensor | None = None, | ||
| ) -> torch.Tensor: | ||
| return torch.nn.functional.linear(x, weight, bias) | ||
|
|
||
|
|
||
| def unquantized_gemm_fake( | ||
| x: torch.Tensor, | ||
| weight: torch.Tensor, | ||
| bias: torch.Tensor | None = None, | ||
| ) -> torch.Tensor: | ||
| output_shape = (x.shape[0], weight.shape[0]) | ||
| return torch.empty(output_shape, dtype=x.dtype, device=x.device) | ||
|
|
||
|
|
||
| direct_register_custom_op(op_name="unquantized_gemm", | ||
| op_func=unquantized_gemm, | ||
| fake_impl=unquantized_gemm_fake, | ||
| mutates_args=[], | ||
| dispatch_key="PrivateUse1") | ||
|
|
||
| def default_unquantized_gemm( | ||
| layer: torch.nn.Module, | ||
| x: torch.Tensor, | ||
| weight: torch.Tensor, | ||
| bias: torch.Tensor | None = None, | ||
| ) -> torch.Tensor: | ||
| if x.device.type == "npu": | ||
| return torch.ops.vllm.unquantized_gemm(x, weight, bias) | ||
| else: | ||
| return torch.nn.functional.linear(x, weight, bias) | ||
|
|
||
|
|
||
| vllm.model_executor.layers.utils.default_unquantized_gemm = default_unquantized_gemm | ||
|
wxsIcey marked this conversation as resolved.
|
||
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
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.
pass name should change?
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.
I suggest leave pass and pattern name as
MatmulAllReduceAddRMSNormPassandxxMatmulAllReduceAddRMSNormPatternrespectively. And using comments to explain that the fusion operator actually splits the allreduce into reducescatter and allgather.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.
Thanks. I have modified it.