|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +import tempfile |
| 5 | +from contextlib import contextmanager |
| 6 | + |
| 7 | +import pytest |
| 8 | +import torch |
| 9 | + |
| 10 | +from vllm.compilation.decorators import support_torch_compile |
| 11 | +from vllm.config import ( |
| 12 | + CompilationConfig, |
| 13 | + CompilationLevel, |
| 14 | + VllmConfig, |
| 15 | + set_current_vllm_config, |
| 16 | +) |
| 17 | +from vllm.forward_context import set_forward_context |
| 18 | +from vllm.utils import is_torch_equal_or_newer |
| 19 | + |
| 20 | + |
| 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 |
| 27 | + |
| 28 | + |
| 29 | +@support_torch_compile |
| 30 | +class CompiledMod(torch.nn.Module): |
| 31 | + def __init__(self, **kwargs): |
| 32 | + super().__init__() |
| 33 | + |
| 34 | + def forward(self, x: torch.Tensor): |
| 35 | + return reference_fn(x) |
| 36 | + |
| 37 | + |
| 38 | +def make_vllm_config() -> VllmConfig: |
| 39 | + return VllmConfig( |
| 40 | + compilation_config=CompilationConfig( |
| 41 | + level=CompilationLevel.PIECEWISE, |
| 42 | + ) |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +@contextmanager |
| 47 | +def use_vllm_config(vllm_config: VllmConfig): |
| 48 | + with set_forward_context({}, vllm_config), set_current_vllm_config(vllm_config): |
| 49 | + yield |
| 50 | + |
| 51 | + |
| 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): |
| 56 | + with monkeypatch.context() as m: |
| 57 | + vllm_config = make_vllm_config() |
| 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 | + ): |
| 66 | + CompiledMod(vllm_config=vllm_config)(*args) |
| 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),) |
| 81 | + m.setenv("VLLM_USE_AOT_COMPILE", "1") |
| 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) |
| 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