| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Deprecated compatibility registry for legacy CuTeDSL warmups.""" |
| 4 | |
| 5 | # TODO(roberto): Remove this compatibility registry after registered CuTeDSL |
| 6 | # warmups are migrated to the shared JIT warmup infrastructure. |
| 7 | # https://github.com/vllm-project/vllm/pull/47451 |
| 8 | |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | import weakref |
| 12 | from collections.abc import Callable, Hashable, Iterable |
| 13 | from dataclasses import dataclass |
| 14 | |
| 15 | import torch |
| 16 | from tqdm import tqdm |
| 17 | |
| 18 | from vllm.distributed import is_global_first_rank |
| 19 | from vllm.logger import init_logger |
| 20 | from vllm.platforms import current_platform |
| 21 | from vllm.tracing import instrument |
| 22 | |
| 23 | logger = init_logger(__name__) |
| 24 | |
| 25 | CuTeDSLCompileFn = Callable[[], None] |
| 26 | |
| 27 | |
| 28 | @dataclass(frozen=True) |
| 29 | class CuTeDSLCompileUnit: |
| 30 | name: str |
| 31 | key: Hashable |
| 32 | compile: CuTeDSLCompileFn |
| 33 | |
| 34 | |
| 35 | _CUTEDSL_WARMUP_PROVIDERS: weakref.WeakSet[object] = weakref.WeakSet() |
| 36 | |
| 37 | |
| 38 | def register_cutedsl_warmup_provider(provider: object) -> None: |
| 39 | """Register an object that can expose CuTeDSL warmup compile units.""" |
| 40 | _CUTEDSL_WARMUP_PROVIDERS.add(provider) |
| 41 | |
| 42 | |
| 43 | # Yield compile units from registered providers. |
| 44 | def _iter_cutedsl_warmup_compile_units() -> Iterable[CuTeDSLCompileUnit]: |
| 45 | for provider in tuple(_CUTEDSL_WARMUP_PROVIDERS): |
| 46 | get_units = getattr(provider, "get_cutedsl_warmup_compile_units", None) |
| 47 | if not callable(get_units): |
| 48 | continue |
| 49 | |
| 50 | compile_units = get_units() |
| 51 | if compile_units is None: |
| 52 | continue |
| 53 | for unit in compile_units: |
| 54 | if not isinstance(unit, CuTeDSLCompileUnit): |
| 55 | raise TypeError( |
| 56 | "get_cutedsl_warmup_compile_units must return " |
| 57 | "CuTeDSLCompileUnit objects" |
| 58 | ) |
| 59 | yield unit |
| 60 | |
| 61 | |
| 62 | # Drop duplicate compile units across providers. |
| 63 | def _collect_unique_compile_units( |
| 64 | compile_units: Iterable[CuTeDSLCompileUnit], |
| 65 | ) -> list[CuTeDSLCompileUnit]: |
| 66 | seen: set[Hashable] = set() |
| 67 | unique_compile_units: list[CuTeDSLCompileUnit] = [] |
| 68 | |
| 69 | for unit in compile_units: |
| 70 | if unit.key in seen: |
| 71 | continue |
| 72 | |
| 73 | seen.add(unit.key) |
| 74 | unique_compile_units.append(unit) |
| 75 | |
| 76 | return unique_compile_units |
| 77 | |
| 78 | |
| 79 | # Execute compile units under inference mode. |
| 80 | def _compile_cutedsl_warmup_units( |
| 81 | compile_units: Iterable[CuTeDSLCompileUnit], |
| 82 | ) -> int: |
| 83 | compiled = 0 |
| 84 | if is_global_first_rank(): |
| 85 | compile_units = tqdm(compile_units, desc="Compiling CuTeDSL kernels") |
| 86 | with torch.inference_mode(): |
| 87 | for unit in compile_units: |
| 88 | unit.compile() |
| 89 | compiled += 1 |
| 90 | torch.accelerator.synchronize() |
| 91 | return compiled |
| 92 | |
| 93 | |
| 94 | # Run all CuTeDSL warmup before serving. |
| 95 | @instrument(span_name="CuTeDSL warmup") |
| 96 | def cutedsl_warmup() -> None: |
| 97 | """Run CuTeDSL compile providers before serving.""" |
| 98 | if not current_platform.is_cuda(): |
| 99 | logger.debug("Skipping CuTeDSL warmup on non-CUDA platform.") |
| 100 | return |
| 101 | |
| 102 | compile_units = _collect_unique_compile_units(_iter_cutedsl_warmup_compile_units()) |
| 103 | if not compile_units: |
| 104 | logger.debug("Skipping CuTeDSL warmup because no compile units were requested.") |
| 105 | return |
| 106 | |
| 107 | logger.info_once( |
| 108 | "Warming up CuTeDSL compile_units=%d names=%s.", |
| 109 | len(compile_units), |
| 110 | tuple(dict.fromkeys(unit.name for unit in compile_units)), |
| 111 | ) |
| 112 | _compile_cutedsl_warmup_units(compile_units) |