Files
FreeToken/python/freetoken/kernel/backend.py
T
0d652e73a4 feat(rocm): add RDNA3 and RDNA4 runtime foundation (#132)
* feat: add simple ROCm GPU support for RDNA3 (gfx1100-1103)

- Add hip_compat.h shim mapping CUDA runtime API to HIP equivalents
- Update pinned_tensor.cpp to compile under both nvcc and hipcc
- Add ROCm detection in arch.py (is_rocm, get_rocm_gfx_arch, is_gfx11xx_family)
- Guard NVIDIA arch checks to return None on ROCm
- Skip nvcc version check in _toolchain.py when on ROCm
- Add ROCm build path in setup.py (ROCM_HOME, amdhip64, --offload-arch)
- Add _hip_cflags() in kernel/utils.py for JIT compilation on ROCm
- Add is_rocm() and driver_hip_version() in backend.py
- Add rocm-smi fallback in __main__.py for clangd generation
- Add TODO(ROCm) for NCCL->RCCL, flashinfer/sgl_kernel ROCm builds,
  Triton autotune RDNA3 tuning, PDL equivalent, hiprtc JIT cache
- Add AMD ROCm classifier in pyproject.toml

* fix(rocm): fall back for single-bank fast index copy

* test(rocm): cover fast index copy fallback

* test(rocm): use explicit fast copy module import

* fix(rocm): complete RDNA3 runtime path

* fix(rocm): preserve CPU MoE implementation

* build(rocm): support RDNA4 discovery and modular SDKs

* fix(rocm): complete modular SDK runtime support

* test(rocm): harden foundation platform contracts

* fix(rocm): pin jit target and split install guide

* perf(kernel): restore ROCm non-temporal index copy

---------

Co-authored-by: bouclem <bouclement2012@gmail.com>
Co-authored-by: Yuu <206304251+nekomario28@users.noreply.github.com>
Co-authored-by: LZ-QWQ <lz1830837889@163.com>
2026-09-25 17:49:45 -07:00

83 lines
2.6 KiB
Python

"""Availability probes for the optional native kernel packages.
When flashinfer / sgl_kernel are installed the call-sites use their fused CUDA
ops; otherwise they fall back to the pure-Triton kernels in
``freetoken.kernel.triton``. ``find_spec`` only checks that the package is
importable (no import side effects), and the result is cached.
"""
from __future__ import annotations
import functools
import importlib.util
import torch
def _importable(name: str) -> bool:
# find_spec normally returns None when a package is absent, but it can raise
# (broken parent package, or a meta_path finder that blocks the name); treat
# any failure as "not available" so callers cleanly fall back to triton.
try:
return importlib.util.find_spec(name) is not None
except Exception:
return False
@functools.cache
def is_flashinfer_installed() -> bool:
return _importable("flashinfer")
@functools.cache
def is_sgl_kernel_installed() -> bool:
return _importable("sgl_kernel")
@functools.cache
def is_vllm_installed() -> bool:
return _importable("vllm")
@functools.cache
def device_capability() -> tuple[int, int]:
"""Compute capability of the current device as (major, minor); (0, 0) without CUDA."""
if not torch.cuda.is_available():
return (0, 0)
major, minor = torch.cuda.get_device_capability()
return (int(major), int(minor))
@functools.cache
def is_rocm() -> bool:
"""True when torch is built for ROCm (AMD GPU)."""
import torch
return getattr(torch.version, "hip", None) is not None
@functools.cache
def driver_hip_version() -> int | None:
"""ROCm driver version, or None if undetermined."""
# TODO(ROCm): flashinfer/sgl_kernel have no ROCm builds — Triton fallback is used.
try:
from freetoken.kernel.pinned import _load_pinned_extension
return int(_load_pinned_extension().driver_cuda_version()) or None
except Exception:
return None
@functools.cache
def driver_cuda_version() -> int | None:
"""Max CUDA version the installed NVIDIA driver supports (``13000`` == CUDA 13.0),
or None if undetermined. Driver-JIT kernels (PTX compiled at runtime, e.g.
flashinfer's CuTe-DSL paths) are gated by this, not by any package's build-time
toolkit version. Resolved through the ``_pinned_tensor`` extension's link-time
cudart, so it works wherever the extension builds (including Windows) -- no dlopen
by soname."""
try:
from freetoken.kernel.pinned import _load_pinned_extension
version = int(_load_pinned_extension().driver_cuda_version())
except Exception:
return None
return version or None # 0 == no driver installed