main
py 83 lines 2.56 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 import copy
4
5 import pytest
6 import torch
7
8 from vllm.compilation.passes.inductor_pass import (
9 CallableInductorPass,
10 InductorPass,
11 pass_context,
12 )
13 from vllm.compilation.passes.pass_manager import PostGradPassManager
14 from vllm.config import ModelConfig, VllmConfig
15 from vllm.config.utils import Range
16
17
18 # dummy custom pass that doesn't inherit
19 def simple_callable(graph: torch.fx.Graph):
20 pass
21
22
23 # Should fail to add directly to the pass manager
24 def test_bad_callable():
25 config = VllmConfig()
26
27 pass_manager = PostGradPassManager()
28 pass_manager.configure(config)
29
30 with pytest.raises(AssertionError):
31 pass_manager.add(simple_callable)
32
33
34 # Pass that inherits from InductorPass
35 class ProperPass(InductorPass):
36 def __call__(self, graph: torch.fx.graph.Graph) -> None:
37 pass
38
39
40 @pytest.mark.parametrize(
41 "callable",
42 [
43 ProperPass(),
44 # Can also wrap callables in CallableInductorPass for compliance
45 CallableInductorPass(simple_callable),
46 CallableInductorPass(simple_callable, InductorPass.hash_source(__file__)),
47 ],
48 )
49 def test_pass_manager_uuid(callable):
50 # Set the pass context as PassManager uuid uses it
51 with pass_context(Range(start=1, end=8)):
52 # Some passes need dtype to be set
53 config = VllmConfig(model_config=ModelConfig(dtype=torch.bfloat16))
54
55 pass_manager = PostGradPassManager()
56 pass_manager.configure(config)
57
58 # Check that UUID is different if the same pass is added 2x
59 pass_manager.add(callable)
60 uuid1 = pass_manager.uuid()
61 pass_manager.add(callable)
62 uuid2 = pass_manager.uuid()
63 assert uuid1 != uuid2
64
65 # UUID should be the same as the original one,
66 # as we constructed in the same way.
67 pass_manager2 = PostGradPassManager()
68 pass_manager2.configure(config)
69 pass_manager2.add(callable)
70 assert uuid1 == pass_manager2.uuid()
71
72 # UUID should be different due to config change
73 config2 = copy.deepcopy(config)
74 config2.compilation_config.pass_config.fuse_norm_quant = (
75 not config2.compilation_config.pass_config.fuse_norm_quant
76 )
77 config2.compilation_config.pass_config.fuse_act_quant = (
78 not config2.compilation_config.pass_config.fuse_act_quant
79 )
80 pass_manager3 = PostGradPassManager()
81 pass_manager3.configure(config2)
82 pass_manager3.add(callable)
83 assert uuid1 != pass_manager3.uuid()