Files
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

79 lines
2.2 KiB
Python

"""CUDA/HIP toolchain/torch consistency checks.
Standalone on purpose: setup.py and the kernel-cache build backend load this
file by path, so it must not import the freetoken package.
"""
from __future__ import annotations
import functools
import os
import re
import shutil
import subprocess
ALLOW_MISMATCH_ENV = "FREETOKEN_ALLOW_CUDA_MISMATCH"
_TRUE_VALUES = {"1", "true", "yes", "on"}
def _is_rocm() -> bool:
import torch
return getattr(torch.version, "hip", None) is not None
def _nvcc_path() -> str | None:
from torch.utils.cpp_extension import CUDA_HOME
if CUDA_HOME:
return os.path.join(CUDA_HOME, "bin", "nvcc")
return shutil.which("nvcc")
def nvcc_release(nvcc: str) -> tuple[int, int] | None:
try:
proc = subprocess.run([nvcc, "--version"], capture_output=True, text=True, check=True)
except (OSError, subprocess.CalledProcessError):
return None
match = re.search(r"release (\d+)\.(\d+)", proc.stdout)
if match is None:
return None
return int(match.group(1)), int(match.group(2))
def torch_cuda_major() -> int | None:
import torch
cuda = getattr(torch.version, "cuda", None)
return int(cuda.split(".")[0]) if cuda else None
@functools.cache
def check_nvcc_matches_torch() -> None:
"""Refuse to nvcc-compile kernels across CUDA majors.
nvcc-built binaries link libcudart.so.<nvcc major>; at runtime only the
torch wheel's own CUDA runtime is guaranteed to be loadable.
"""
if _is_rocm():
return # ROCm uses hipcc, not nvcc
if os.getenv(ALLOW_MISMATCH_ENV, "").strip().lower() in _TRUE_VALUES:
return
torch_major = torch_cuda_major()
if torch_major is None:
return
nvcc = _nvcc_path()
if nvcc is None:
return
release = nvcc_release(nvcc)
if release is None:
return
if release[0] != torch_major:
import torch
raise RuntimeError(
f"nvcc {release[0]}.{release[1]} would build kernels linking "
f"libcudart.so.{release[0]}, but torch {torch.__version__} ships CUDA "
f"{torch.version.cuda} (libcudart.so.{torch_major}). Install a CUDA "
f"{torch_major}.x toolkit, or set {ALLOW_MISMATCH_ENV}=1 to override."
)