mirror of
https://github.com/FlashML-org/FreeToken.git
synced 2026-11-23 02:00:02 +08:00
72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
"""CUDA 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 _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 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."
|
|
)
|