[ROCm] Defer tilelang import through its import from vllm.tilelang_utils import tilelang and relaxed has_tilelang (#51159)

Signed-off-by: Felix Marty <Felix.Marty@amd.com>
Co-authored-by: Andreas Karatzas <akaratza@amd.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
fxmarty-amd
2026-08-13 13:12:40 -05:00
committed by GitHub
co-authored by Andreas Karatzas mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent e6b2a8ad5e
commit c5b7c069a4
8 changed files with 313 additions and 52 deletions
@@ -1,29 +1,17 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from __future__ import annotations
import math
from functools import cache
from typing import TYPE_CHECKING, Any
import torch
from vllm.platforms import current_platform
from vllm.utils.import_utils import has_tilelang
from vllm.tilelang_utils import T, tilelang, tilelang_jit
from vllm.utils.math_utils import cdiv
# TileLang is used for MHC on CUDA and ROCm. Keep non-GPU imports cheap so
# registering the Python wrapper modules does not require TileLang everywhere.
if TYPE_CHECKING or current_platform.is_cuda_alike():
if not has_tilelang():
raise ImportError(
"tilelang is required for mhc but is not installed. Install it with "
"`pip install tilelang`."
)
import tilelang
import tilelang.language as T
else:
tilelang = None # type: ignore[assignment]
T = None # type: ignore[assignment]
ENABLE_PDL = current_platform.is_arch_support_pdl() and current_platform.is_cuda()
@@ -40,18 +28,7 @@ def compute_num_split(block_k: int, k: int | None, grid_size: int) -> int:
return split_k
pass_configs: dict[tilelang.PassConfigKey, Any] = {
tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True,
tilelang.PassConfigKey.TL_DISABLE_TMA_LOWER: True,
}
if current_platform.is_cuda():
pass_configs[tilelang.PassConfigKey.TL_PTXAS_REGISTER_USAGE_LEVEL] = 10
@tilelang.jit(
pass_configs=pass_configs,
)
@tilelang_jit
def mhc_pre_big_fuse_tilelang(
gemm_out_mul,
gemm_out_sqrsum,
@@ -191,9 +168,7 @@ def mhc_pre_big_fuse_tilelang(
# Copied from https://github.com/sgl-project/sglang/blob/main/python/sglang/srt/layers/mhc.py#L478
@tilelang.jit(
pass_configs=pass_configs,
)
@tilelang_jit
def mhc_pre_big_fuse_with_norm_tilelang(
gemm_out_mul,
gemm_out_sqrsum,
@@ -353,9 +328,7 @@ def mhc_pre_big_fuse_with_norm_tilelang(
T.pdl_trigger()
@tilelang.jit(
pass_configs=pass_configs,
)
@tilelang_jit
def mhc_pre_big_fuse_broadcast_with_norm_tilelang(
gemm_out_mul,
gemm_out_sqrsum,
@@ -517,9 +490,7 @@ def mhc_pre_big_fuse_broadcast_with_norm_tilelang(
T.pdl_trigger()
@tilelang.jit(
pass_configs=pass_configs,
)
@tilelang_jit
def mhc_fused_tilelang(
comb_mix,
residual_in,
@@ -640,9 +611,7 @@ def mhc_fused_tilelang(
T.pdl_trigger()
@tilelang.jit(
pass_configs=pass_configs,
)
@tilelang_jit
def mhc_post_tilelang(
a,
b,
@@ -695,9 +664,7 @@ def mhc_post_tilelang(
T.pdl_trigger()
@tilelang.jit(
pass_configs=pass_configs,
)
@tilelang_jit
def hc_prenorm_gemm_tilelang(
x,
fn,
@@ -781,9 +748,7 @@ def hc_prenorm_gemm_tilelang(
T.pdl_trigger()
@tilelang.jit(
pass_configs=pass_configs,
)
@tilelang_jit
def hc_prenorm_gemm_block_m_tilelang(
x,
fn,
@@ -878,9 +843,7 @@ def hc_prenorm_gemm_block_m_tilelang(
T.pdl_trigger()
@tilelang.jit(
pass_configs=pass_configs,
)
@tilelang_jit
def hc_head_fuse_tilelang(
residual,
fn,
+91
View File
@@ -0,0 +1,91 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from __future__ import annotations
import functools
from collections.abc import Callable
from functools import cache
from typing import TYPE_CHECKING, Any
from vllm.platforms import current_platform
from vllm.utils.import_utils import has_tilelang
if TYPE_CHECKING or current_platform.is_cuda():
if not has_tilelang():
raise ImportError(
"tilelang is required for mhc but is not installed. Install it with "
"`pip install tilelang`."
)
import tilelang
import tilelang.language as T
else:
tilelang = None # type: ignore[assignment]
T = None # type: ignore[assignment]
def _ensure_tilelang_imported() -> None:
"""Bind the `tilelang` and `T` module globals, importing them if needed.
On ROCm, this runs on the first kernel call instead of at import time.
Raises:
ImportError: If TileLang is not installed.
"""
global T, tilelang
if tilelang is not None:
return
if not has_tilelang():
raise ImportError(
"tilelang is required for mhc but is not installed. Install it with "
"`pip install tilelang`."
)
import tilelang as tilelang_module
import tilelang.language as tilelang_language
tilelang = tilelang_module
T = tilelang_language
@cache
def _get_pass_configs() -> dict[Any, Any]:
_ensure_tilelang_imported()
pass_configs: dict[Any, Any] = {
tilelang.PassConfigKey.TL_DISABLE_WARP_SPECIALIZED: True,
tilelang.PassConfigKey.TL_DISABLE_TMA_LOWER: True,
}
if current_platform.is_cuda():
pass_configs[tilelang.PassConfigKey.TL_PTXAS_REGISTER_USAGE_LEVEL] = 10
return pass_configs
def tilelang_jit(kernel_function: Callable[..., Any]) -> Callable[..., Any]:
"""Apply `tilelang.jit`, deferring until first call on ROCm.
ROCm defers JIT decoration so importing the caller's module does not
require TileLang immediately. CUDA keeps the eager decoration behavior.
The kernel body parsed by TileLang references `T` as an unqualified
global, so on the deferred ROCm path this rebinds `T`/`tilelang` in the
decorated function's own module globals once they become available.
"""
if not current_platform.is_rocm():
_ensure_tilelang_imported()
return tilelang.jit(pass_configs=_get_pass_configs())(kernel_function)
compiled_kernel: Callable[..., Any] | None = None
@functools.wraps(kernel_function)
def wrapper(*args: Any, **kwargs: Any) -> Any:
nonlocal compiled_kernel
if compiled_kernel is None:
_ensure_tilelang_imported()
kernel_function.__globals__["tilelang"] = tilelang
kernel_function.__globals__["T"] = T
compiled_kernel = tilelang.jit(pass_configs=_get_pass_configs())(
kernel_function
)
return compiled_kernel(*args, **kwargs)
return wrapper
+22 -2
View File
@@ -412,6 +412,21 @@ def _has_module(module_name: str) -> bool:
return True
@cache
def _has_module_spec(module_name: str) -> bool:
"""Return True if *module_name* is installed, without importing it.
Unlike [`_has_module`][vllm.utils.import_utils._has_module], this only
resolves the import spec. It therefore does not pay the import cost of
heavyweight modules, at the price of not verifying that native
dependencies (shared libraries, etc.) are satisfied. The result is cached.
"""
try:
return importlib.util.find_spec(module_name) is not None
except Exception:
return False
def has_deep_ep() -> bool:
"""Whether the optional `deep_ep` package is available."""
return _has_module("deep_ep")
@@ -504,8 +519,13 @@ def has_triton_kernels() -> bool:
@cache
def has_tilelang() -> bool:
"""Whether the optional `tilelang` package is available."""
if not _has_module("tilelang"):
"""Whether the optional `tilelang` package is available.
Only the import spec is checked: importing `tilelang` is expensive, so
callers must import it lazily at their point of use rather than relying
on this function to have imported it already.
"""
if not _has_module_spec("tilelang"):
return False
# ROCm-only guard, imported lazily to avoid loading rocm on CUDA.
from vllm.platforms import current_platform
+7 -1
View File
@@ -30,6 +30,7 @@ from contextlib import suppress
from typing import Any, Literal, cast
from vllm.logger import init_logger
from vllm.platforms import current_platform
from vllm.triton_utils.importing import HAS_TRITON
logger = init_logger(__name__)
@@ -74,7 +75,12 @@ def activate(*, mode: JitMonitorMode = "warn", verbose: bool = False) -> None:
_setup_triton_autotuning_print()
_setup_triton_jit_hook()
_setup_cutedsl_jit_hook()
_setup_tilelang_jit_hook()
# Refer to #51159. tilelang ships broken symbols
# on rocm.
# TODO: Remove the guard once tilelang upstream is fixed.
if not current_platform.is_rocm():
_setup_tilelang_jit_hook()
logger.info(
"Kernel JIT monitor activated; monitored JIT compilations during "