main
py 111 lines 3.33 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 from dataclasses import dataclass
5 from typing import Any
6
7 from vllm.model_executor.warmup import jit_warmup_cutedsl_helper
8 from vllm.model_executor.warmup.jit_warmup import kernel_launcher
9 from vllm.model_executor.warmup.jit_warmup_cutedsl_helper import (
10 CuTeDSLLaunchSpec,
11 VllmCuTeDSLJitKernel,
12 )
13
14
15 class _TestCuTeDSLKernel(VllmCuTeDSLJitKernel["_TestCuTeDSLKernel.CompileKey"]):
16 @dataclass(frozen=True)
17 class CompileKey:
18 variant: int
19
20 @staticmethod
21 def kernel(compile_key: CompileKey) -> tuple[str, int]:
22 return "entry", compile_key.variant
23
24 def dispatch(self, *, variant: int) -> CompileKey:
25 return self.CompileKey(variant=variant)
26
27 def get_warmup_keys(self) -> list[CompileKey]:
28 return self._trace_dispatch(self.dispatch)(variant=(1, 2))
29
30 def warmup_inputs(self, compile_key: CompileKey) -> tuple[Any, ...]:
31 return (f"fake-{compile_key.variant}",)
32
33 @kernel_launcher
34 def __call__(
35 self,
36 payload: str,
37 *,
38 variant: int,
39 ) -> CuTeDSLLaunchSpec[CompileKey]:
40 compile_key = self.dispatch(variant=variant)
41 return compile_key, (payload,)
42
43
44 def test_cutedsl_launcher_reuses_compiled_executor(monkeypatch) -> None:
45 compile_calls: list[tuple[Any, tuple[Any, ...]]] = []
46 launch_calls: list[tuple[int, tuple[Any, ...]]] = []
47
48 def compile_cutedsl(entry: tuple[str, int], *args: Any) -> Any:
49 compile_calls.append((entry, args))
50 variant = entry[1]
51
52 def executor(*runtime_args: Any) -> tuple[int, tuple[Any, ...]]:
53 launch_calls.append((variant, runtime_args))
54 return variant, runtime_args
55
56 return executor
57
58 monkeypatch.setattr(
59 jit_warmup_cutedsl_helper,
60 "compile_cutedsl",
61 compile_cutedsl,
62 )
63 kernel = _TestCuTeDSLKernel()
64 compile_key = kernel.CompileKey(variant=1)
65
66 kernel.compile(compile_key)
67 kernel.compile(compile_key)
68 assert compile_calls == [(("entry", 1), ("fake-1",))]
69
70 assert kernel("runtime", variant=1) == (1, ("runtime",))
71 assert kernel("other", variant=2) == (2, ("other",))
72 assert compile_calls == [
73 (("entry", 1), ("fake-1",)),
74 (("entry", 2), ("fake-2",)),
75 ]
76 assert launch_calls == [(1, ("runtime",)), (2, ("other",))]
77
78
79 def test_cutedsl_launcher_runs_post_launch_finalizer(monkeypatch) -> None:
80 events: list[str] = []
81
82 def compile_cutedsl(entry: tuple[str, int], *args: Any) -> Any:
83 def executor(*runtime_args: Any) -> None:
84 events.append("launch")
85
86 return executor
87
88 class FinalizedKernel(_TestCuTeDSLKernel):
89 @kernel_launcher
90 def __call__(
91 self,
92 payload: str,
93 *,
94 variant: int,
95 ) -> CuTeDSLLaunchSpec[_TestCuTeDSLKernel.CompileKey]:
96 compile_key = self.dispatch(variant=variant)
97
98 def finalize() -> str:
99 events.append("finalize")
100 return "output"
101
102 return compile_key, (payload,), "unused", finalize
103
104 monkeypatch.setattr(
105 jit_warmup_cutedsl_helper,
106 "compile_cutedsl",
107 compile_cutedsl,
108 )
109
110 assert FinalizedKernel()("runtime", variant=1) == "output"
111 assert events == ["launch", "finalize"]