|
4 | 4 |
|
5 | 5 | import torch |
6 | 6 |
|
7 | | -from vllm.compilation.wrapper import TorchCompileWrapperWithCustomDispatcher |
8 | | -from vllm.config import CompilationMode |
| 7 | +from vllm.compilation.wrapper import TorchCompileWithNoGuardsWrapper |
| 8 | +from vllm.config import ( |
| 9 | + CompilationConfig, |
| 10 | + CompilationMode, |
| 11 | + VllmConfig, |
| 12 | + set_current_vllm_config, |
| 13 | +) |
9 | 14 |
|
10 | 15 |
|
11 | 16 | class MyMod(torch.nn.Module): |
12 | 17 | def forward(self, x: torch.Tensor, cache: torch.Tensor | None = None): |
13 | | - if cache is not None: |
14 | | - return x + cache |
15 | | - return x * 2 |
| 18 | + if x.size()[0] >= 4: |
| 19 | + return x * 2 |
| 20 | + else: |
| 21 | + return x * 100 |
16 | 22 |
|
17 | 23 |
|
18 | | -class MyWrapper(TorchCompileWrapperWithCustomDispatcher): |
| 24 | +class MyWrapper(TorchCompileWithNoGuardsWrapper): |
19 | 25 | def __init__(self, model): |
20 | 26 | self.model = model |
21 | | - compiled_callable = torch.compile(self.forward, backend="eager") |
22 | | - super().__init__( |
23 | | - compiled_callable, compilation_mode=CompilationMode.DYNAMO_TRACE_ONCE |
24 | | - ) |
| 27 | + super().__init__() |
25 | 28 |
|
26 | | - def forward(self, x: torch.Tensor, cache: torch.Tensor | None = None): |
| 29 | + def forward(self, x: torch.Tensor): # type: ignore[override] |
27 | 30 | # this is the function to be compiled |
28 | | - return self.model(x, cache) |
29 | | - |
30 | | - def __call__(self, x: torch.Tensor, cache: torch.Tensor | None = None): |
31 | | - # let torch.compile compile twice |
32 | | - if len(self.compiled_codes) == 2: |
33 | | - dispatch_id = 0 if cache is None else 1 |
34 | | - with self.dispatch_to_code(dispatch_id): |
35 | | - return self.forward(x, cache) |
36 | | - else: |
37 | | - return self.compiled_callable(x, cache) |
| 31 | + return self.model(x) |
38 | 32 |
|
39 | 33 |
|
40 | 34 | def test_torch_compile_wrapper(): |
41 | | - mod = MyMod() |
42 | | - wrappers = [] |
43 | | - for i in range(3): |
| 35 | + """Test basic functionality of TorchCompileWithNoGuardsWrapper.""" |
| 36 | + # Create a proper vLLM config instead of mocking |
| 37 | + vllm_config = VllmConfig() |
| 38 | + vllm_config.compilation_config = CompilationConfig() |
| 39 | + vllm_config.compilation_config.mode = CompilationMode.DYNAMO_TRACE_ONCE |
| 40 | + vllm_config.compilation_config.backend = "inductor" |
| 41 | + |
| 42 | + # Test DYNAMO_TRACE_ONCE |
| 43 | + with set_current_vllm_config(vllm_config): |
| 44 | + torch._dynamo.reset() |
| 45 | + mod = MyMod() |
| 46 | + wrapper = MyWrapper(mod) |
| 47 | + |
| 48 | + # First call should trigger compilation |
| 49 | + x = torch.tensor([1, 2, 3, 4]) |
| 50 | + torch._dynamo.mark_dynamic(x, 0) |
| 51 | + |
| 52 | + result1 = wrapper(x) |
| 53 | + expected1 = torch.tensor([2, 4, 6, 8]) |
| 54 | + assert torch.allclose(result1, expected1), ( |
| 55 | + f"Expected {expected1}, got {result1}" |
| 56 | + ) |
| 57 | + |
| 58 | + # Second call should use compiled code |
| 59 | + x2 = torch.tensor([1, 2, 3]) |
| 60 | + result2 = wrapper(x2) |
| 61 | + expected2 = torch.tensor([2, 4, 6]) |
| 62 | + assert torch.allclose(result2, expected2), ( |
| 63 | + f"Expected {expected2}, got {result2}" |
| 64 | + ) |
| 65 | + |
| 66 | + # without the wrapper result would be different. |
| 67 | + result3 = mod(x2) |
| 68 | + expected3 = torch.tensor([100, 200, 300]) |
| 69 | + |
| 70 | + assert torch.allclose(result3, expected3), ( |
| 71 | + f"Expected {result3}, got {expected3}" |
| 72 | + ) |
| 73 | + |
| 74 | + # with STOCK_TORCH_COMPILE we do not remove guards. |
| 75 | + vllm_config.compilation_config.mode = CompilationMode.STOCK_TORCH_COMPILE |
| 76 | + torch._dynamo.reset() |
| 77 | + with set_current_vllm_config(vllm_config): |
44 | 78 | torch._dynamo.reset() |
| 79 | + mod = MyMod() |
45 | 80 | wrapper = MyWrapper(mod) |
46 | | - wrappers.append(wrapper) |
47 | | - x = torch.tensor([1]) |
48 | | - wrapper(x, None) # profile run, compile |
49 | | - # create a cache tensor |
50 | | - cache = torch.tensor([2]) |
51 | | - wrapper(x, cache) # warm up with cache, recompile |
52 | | - |
53 | | - # for new input, dispatch to the compiled code directly |
54 | | - new_x = torch.tensor([3]) |
55 | | - assert wrapper(new_x, None).item() == 6 # dispatch to the first compiled code |
56 | | - assert wrapper(new_x, cache).item() == 5 # dispatch to the second compiled code |
57 | | - |
58 | | - for wrapper in wrappers: |
59 | | - # make sure they have independent compiled codes |
60 | | - assert len(wrapper.compiled_codes) == 2 |
| 81 | + |
| 82 | + # First call should trigger compilation |
| 83 | + x = torch.tensor([1, 2, 3, 4]) |
| 84 | + torch._dynamo.mark_dynamic(x, 0) |
| 85 | + |
| 86 | + result1 = wrapper(x) |
| 87 | + expected1 = torch.tensor([2, 4, 6, 8]) |
| 88 | + assert torch.allclose(result1, expected1), ( |
| 89 | + f"Expected {expected1}, got {result1}" |
| 90 | + ) |
| 91 | + |
| 92 | + # Second call should triger another compilation |
| 93 | + x2 = torch.tensor([1, 2, 3]) |
| 94 | + result2 = wrapper(x2) |
| 95 | + expected2 = torch.tensor([100, 200, 300]) |
| 96 | + assert torch.allclose(result2, expected2), ( |
| 97 | + f"Expected {expected2}, got {result2}" |
| 98 | + ) |
| 99 | + |
| 100 | + # NO_COMPILATION level not supported. |
| 101 | + vllm_config.compilation_config.mode = None |
| 102 | + torch._dynamo.reset() |
| 103 | + with set_current_vllm_config(vllm_config): |
| 104 | + torch._dynamo.reset() |
| 105 | + mod = MyMod() |
| 106 | + |
| 107 | + try: |
| 108 | + wrapper = MyWrapper(mod) |
| 109 | + except Exception: |
| 110 | + return |
| 111 | + raise AssertionError("expected an exception to be raised") |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + test_torch_compile_wrapper() |
0 commit comments