|
1 | 1 | # SPDX-License-Identifier: Apache-2.0 |
2 | 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
3 | 3 |
|
| 4 | +import tempfile |
4 | 5 | from contextlib import contextmanager |
5 | 6 |
|
6 | 7 | import pytest |
7 | 8 | import torch |
8 | 9 |
|
9 | 10 | from vllm.compilation.decorators import support_torch_compile |
10 | | -from vllm.config import (CompilationConfig, CompilationLevel, VllmConfig, |
11 | | - set_current_vllm_config) |
| 11 | +from vllm.config import ( |
| 12 | + CompilationConfig, |
| 13 | + CompilationLevel, |
| 14 | + VllmConfig, |
| 15 | + set_current_vllm_config, |
| 16 | +) |
12 | 17 | from vllm.forward_context import set_forward_context |
| 18 | +from vllm.utils import is_torch_equal_or_newer |
13 | 19 |
|
14 | 20 |
|
15 | | -class MyMod(torch.nn.Module): |
| 21 | +def reference_fn(x: torch.Tensor): |
| 22 | + assert x.shape[0] <= 42 |
| 23 | + assert x.shape[0] % 2 == 0 |
| 24 | + for _ in range(3000): |
| 25 | + x = x + x.shape[0] |
| 26 | + return x |
16 | 27 |
|
| 28 | + |
| 29 | +@support_torch_compile |
| 30 | +class CompiledMod(torch.nn.Module): |
17 | 31 | def __init__(self, **kwargs): |
18 | 32 | super().__init__() |
19 | 33 |
|
20 | 34 | def forward(self, x: torch.Tensor): |
21 | | - for _ in range(3000): |
22 | | - x = x + x.shape[0] |
23 | | - return x |
| 35 | + return reference_fn(x) |
24 | 36 |
|
25 | 37 |
|
26 | 38 | def make_vllm_config() -> VllmConfig: |
27 | | - return VllmConfig(compilation_config=CompilationConfig( |
28 | | - level=CompilationLevel.PIECEWISE, )) |
| 39 | + return VllmConfig( |
| 40 | + compilation_config=CompilationConfig( |
| 41 | + level=CompilationLevel.PIECEWISE, |
| 42 | + ) |
| 43 | + ) |
29 | 44 |
|
30 | 45 |
|
31 | 46 | @contextmanager |
32 | 47 | def use_vllm_config(vllm_config: VllmConfig): |
33 | | - with set_forward_context( |
34 | | - {}, vllm_config), set_current_vllm_config(vllm_config): |
| 48 | + with set_forward_context({}, vllm_config), set_current_vllm_config(vllm_config): |
35 | 49 | yield |
36 | 50 |
|
37 | 51 |
|
38 | | -def test_no_eval_frame(monkeypatch: pytest.MonkeyPatch): |
| 52 | +@pytest.mark.skipif( |
| 53 | + not is_torch_equal_or_newer("2.10.0.dev"), reason="requires torch 2.10" |
| 54 | +) |
| 55 | +def test_no_dynamo_cache_entry(monkeypatch: pytest.MonkeyPatch): |
39 | 56 | with monkeypatch.context() as m: |
40 | | - mod = MyMod() |
41 | | - args = (torch.randn(10, 10), ) |
42 | | - expected = mod(*args) |
43 | | - CompiledMod = support_torch_compile(MyMod) |
44 | | - |
45 | 57 | vllm_config = make_vllm_config() |
46 | | - m.setenv("VLLM_USE_AOT_COMPILE", "0") |
47 | | - try: |
48 | | - with use_vllm_config(vllm_config), torch.compiler.set_stance( |
49 | | - "fail_on_recompile"): |
| 58 | + args = (torch.randn(10, 10),) |
| 59 | + expected = reference_fn(*args) |
| 60 | + with use_vllm_config(vllm_config): |
| 61 | + m.setenv("VLLM_USE_AOT_COMPILE", "0") |
| 62 | + with ( |
| 63 | + pytest.raises(RuntimeError, match="Detected recompile"), |
| 64 | + torch.compiler.set_stance("fail_on_recompile"), |
| 65 | + ): |
50 | 66 | CompiledMod(vllm_config=vllm_config)(*args) |
51 | | - except RuntimeError as e: |
52 | | - assert "Detected recompile" in str(e) |
53 | | - else: |
54 | | - raise AssertionError("Expected exception to be raised") |
55 | 67 |
|
| 68 | + m.setenv("VLLM_USE_AOT_COMPILE", "1") |
| 69 | + torch._dynamo.reset() |
| 70 | + with torch.compiler.set_stance("fail_on_recompile"): |
| 71 | + actual = CompiledMod(vllm_config=vllm_config)(*args) |
| 72 | + assert torch.allclose(actual, expected) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.skipif( |
| 76 | + not is_torch_equal_or_newer("2.10.0.dev"), reason="requires torch 2.10" |
| 77 | +) |
| 78 | +def test_force_aot_load(monkeypatch: pytest.MonkeyPatch): |
| 79 | + with tempfile.TemporaryDirectory() as tmpdirname, monkeypatch.context() as m: |
| 80 | + args = (torch.randn(10, 10),) |
56 | 81 | m.setenv("VLLM_USE_AOT_COMPILE", "1") |
57 | | - torch._dynamo.reset() |
58 | | - with use_vllm_config(vllm_config), torch.compiler.set_stance( |
59 | | - "fail_on_recompile"): |
60 | | - ret = CompiledMod(vllm_config=vllm_config)(*args) |
| 82 | + m.setenv("VLLM_FORCE_AOT_LOAD", "1") |
| 83 | + m.setenv("VLLM_CACHE_ROOT", tmpdirname) |
| 84 | + vllm_config = make_vllm_config() |
| 85 | + with use_vllm_config(vllm_config), pytest.raises(FileNotFoundError): |
| 86 | + CompiledMod(vllm_config=vllm_config)(*args) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.skipif( |
| 90 | + not is_torch_equal_or_newer("2.10.0.dev"), reason="requires torch 2.10" |
| 91 | +) |
| 92 | +def test_save_and_load(monkeypatch: pytest.MonkeyPatch): |
| 93 | + with monkeypatch.context() as m: |
| 94 | + args = (torch.randn(10, 10),) |
| 95 | + |
| 96 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 97 | + m.setenv("VLLM_CACHE_ROOT", tmpdirname) |
| 98 | + m.setenv("VLLM_USE_AOT_COMPILE", "1") |
| 99 | + vllm_config = make_vllm_config() |
| 100 | + with use_vllm_config(vllm_config): |
| 101 | + expected = CompiledMod(vllm_config=vllm_config)(*args) |
| 102 | + |
| 103 | + m.setenv("VLLM_FORCE_AOT_LOAD", "1") |
| 104 | + vllm_config = make_vllm_config() |
| 105 | + with use_vllm_config(vllm_config): |
| 106 | + ret = CompiledMod(vllm_config=vllm_config)(*args) |
61 | 107 | assert torch.allclose(ret, expected) |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.skipif( |
| 111 | + not is_torch_equal_or_newer("2.10.0.dev"), reason="requires torch 2.10" |
| 112 | +) |
| 113 | +def test_shape_env(monkeypatch: pytest.MonkeyPatch): |
| 114 | + """ |
| 115 | + Test that the shape environment is correctly serialized and preserved |
| 116 | + when loading from cache. |
| 117 | + """ |
| 118 | + with monkeypatch.context() as m: |
| 119 | + args = (torch.randn(10, 10),) |
| 120 | + |
| 121 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 122 | + m.setenv("VLLM_CACHE_ROOT", tmpdirname) |
| 123 | + m.setenv("VLLM_USE_AOT_COMPILE", "1") |
| 124 | + vllm_config = make_vllm_config() |
| 125 | + with use_vllm_config(vllm_config): |
| 126 | + compiled_mod = CompiledMod(vllm_config=vllm_config) |
| 127 | + compiled_mod(*args) |
| 128 | + artifacts = compiled_mod.aot_compiled_fn._artifacts |
| 129 | + guards_string = artifacts.compiled_fn.shape_env.format_guards() |
| 130 | + assert guards_string == " - s77 <= 42\n - Eq(Mod(s77, 2), 0)" |
| 131 | + |
| 132 | + m.setenv("VLLM_FORCE_AOT_LOAD", "1") |
| 133 | + vllm_config = make_vllm_config() |
| 134 | + with use_vllm_config(vllm_config): |
| 135 | + compiled_mod = CompiledMod(vllm_config=vllm_config) |
| 136 | + compiled_mod(*args) |
| 137 | + artifacts = compiled_mod.aot_compiled_fn._artifacts |
| 138 | + guards_string = artifacts.compiled_fn.shape_env.format_guards() |
| 139 | + assert guards_string == " - s77 <= 42\n - Eq(Mod(s77, 2), 0)" |
0 commit comments