main
py 105 lines 3.67 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 import pytest
5 import torch
6
7 import vllm.ir.ops
8 from tests.compile.backend import TestBackend
9 from vllm.compilation.passes.fusion.add_rms_fusion import (
10 AddRMSNormFusionPass,
11 RMSNormReshapeFusionPass,
12 )
13 from vllm.compilation.passes.fx_utils import find_op_nodes, is_func
14 from vllm.compilation.passes.utility.noop_elimination import NoOpEliminationPass
15 from vllm.compilation.passes.utility.post_cleanup import PostCleanupPass
16 from vllm.config import CompilationConfig, CompilationMode, VllmConfig
17 from vllm.platforms import current_platform
18
19 pytestmark = pytest.mark.skipif(
20 not current_platform.is_cuda_alike(), reason="Requires CUDA or ROCm"
21 )
22
23
24 class RMSNormModel(torch.nn.Module):
25 def __init__(self, hidden_size: int) -> None:
26 super().__init__()
27 self.weight = torch.nn.Parameter(torch.randn(hidden_size))
28
29 def forward(self, x: torch.Tensor) -> torch.Tensor:
30 rms = vllm.ir.ops.rms_norm(x, self.weight, 1e-6)
31 return rms.reshape(-1, rms.shape[-1])
32
33
34 class AddRMSNormModel(torch.nn.Module):
35 def __init__(self, hidden_size: int, residual_first: bool) -> None:
36 super().__init__()
37 self.weight = torch.nn.Parameter(torch.randn(hidden_size))
38 self.residual_first = residual_first
39
40 def forward(
41 self, x: torch.Tensor, residual: torch.Tensor
42 ) -> tuple[torch.Tensor, torch.Tensor]:
43 residual = residual + x if self.residual_first else x + residual
44 rms = vllm.ir.ops.rms_norm(residual, self.weight, 1e-6)
45 return rms.reshape(-1, rms.shape[-1]), residual
46
47 def ops_in_model_before(self):
48 return [torch.ops.aten.add, torch.ops.vllm_ir.rms_norm]
49
50 def ops_in_model_after(self):
51 return [torch.ops.vllm_ir.fused_add_rms_norm]
52
53
54 def _run_fusion_test(model, config, passes, *inputs):
55 backend = TestBackend(NoOpEliminationPass(config), *passes, PostCleanupPass(config))
56 outputs_unfused = model(*inputs)
57 outputs_fused = torch.compile(model, backend=backend)(*inputs)
58 torch.testing.assert_close(outputs_fused, outputs_unfused)
59 return backend
60
61
62 def _is_reshape(node):
63 return isinstance(node, torch.fx.Node) and is_func(
64 node, torch.ops.aten.reshape.default
65 )
66
67
68 @pytest.fixture
69 def vllm_config():
70 config = VllmConfig(
71 compilation_config=CompilationConfig(mode=CompilationMode.VLLM_COMPILE)
72 )
73 with vllm.config.set_current_vllm_config(config):
74 torch.set_default_device("cuda")
75 torch.set_default_dtype(torch.bfloat16)
76 torch.manual_seed(0)
77 yield config
78
79
80 def test_rmsnorm_reshape_fusion(vllm_config):
81 fusion_pass = RMSNormReshapeFusionPass(vllm_config)
82 model = RMSNormModel(hidden_size=32)
83 x = torch.randn(2, 7, 32)
84 backend = _run_fusion_test(model, vllm_config, [fusion_pass], x)
85
86 assert fusion_pass.matched_count == 1
87 (rms_node,) = find_op_nodes(torch.ops.vllm_ir.rms_norm, backend.graph_post_pass)
88 assert _is_reshape(rms_node.args[0])
89
90
91 @pytest.mark.parametrize("residual_first", [True, False])
92 def test_add_rmsnorm_reshape_fusion(vllm_config, residual_first):
93 add_fusion = AddRMSNormFusionPass(vllm_config)
94 reshape_fusion = RMSNormReshapeFusionPass(vllm_config)
95 model = AddRMSNormModel(hidden_size=32, residual_first=residual_first)
96 x = torch.randn(2, 7, 32)
97 residual = torch.randn_like(x)
98 backend = _run_fusion_test(
99 model, vllm_config, [add_fusion, reshape_fusion], x, residual
100 )
101
102 assert add_fusion.matched_count == 1
103 assert reshape_fusion.matched_count == 1
104 backend.check_before_ops(model.ops_in_model_before())
105 backend.check_after_ops(model.ops_in_model_after())