|
5 | 5 | from pathlib import Path |
6 | 6 |
|
7 | 7 | import torch |
| 8 | +import functools |
| 9 | +import sysconfig |
| 10 | +import subprocess |
| 11 | +import re |
| 12 | +import tempfile |
| 13 | +import shlex |
| 14 | +from triton.runtime.cache import get_cache_manager |
| 15 | +from triton.experimental.tle.raw.cuda.runtime import _get_cuda_gpu_arch |
| 16 | +from triton.experimental.tle.raw.cache_key import compute_tle_raw_host_cache_key, bind_tle_raw_common_host_cache_key, bind_tle_raw_host_cache_key |
8 | 17 |
|
9 | 18 | try: |
10 | 19 | from cuda.bindings import driver as cuda |
|
20 | 29 | from cuda import cudart as cudart |
21 | 30 |
|
22 | 31 |
|
| 32 | +@functools.lru_cache() |
| 33 | +def get_nvshmem_home() -> Path: |
| 34 | + if (nvshmem_home := os.getenv("NVSHMEM_HOME")) is not None: |
| 35 | + return Path(nvshmem_home) |
| 36 | + |
| 37 | + try: |
| 38 | + import nvidia.nvshmem |
| 39 | + return Path(nvidia.nvshmem.__path__[0]) |
| 40 | + except Exception: |
| 41 | + pass |
| 42 | + |
| 43 | + |
| 44 | +@functools.lru_cache() |
| 45 | +def get_nvcc(): |
| 46 | + return _path_to_binary("nvcc") |
| 47 | + |
| 48 | + |
| 49 | +@functools.lru_cache() |
| 50 | +def _path_to_binary(binary: str): |
| 51 | + binary += sysconfig.get_config_var("EXE") |
| 52 | + paths = [ |
| 53 | + os.environ.get(f"TRITON_{binary.upper()}_PATH", ""), |
| 54 | + os.path.join(Path(os.path.dirname(__file__)).parent, "triton/backends/nvidia/bin", binary), |
| 55 | + ] |
| 56 | + |
| 57 | + cuda_home = os.getenv("CUDA_HOME", "/usr/local/cuda") |
| 58 | + |
| 59 | + paths += [f"{cuda_home}/bin/{binary}"] |
| 60 | + |
| 61 | + for path in paths: |
| 62 | + if os.path.exists(path) and os.path.isfile(path): |
| 63 | + result = subprocess.check_output([path, "--version"], stderr=subprocess.STDOUT) |
| 64 | + if result is not None: |
| 65 | + version = re.search(r".*release (\d+\.\d+).*", result.decode("utf-8"), flags=re.MULTILINE) |
| 66 | + if version is not None: |
| 67 | + return path, version.group(1) |
| 68 | + raise RuntimeError(f"Cannot find {binary}") |
| 69 | + |
| 70 | + |
| 71 | +def _compile_cuda_host_to_cache( |
| 72 | + source, |
| 73 | + nvshmem_home, |
| 74 | + arch: str = None, |
| 75 | + force: bool = False, |
| 76 | +) -> tuple[Path, str, bool]: |
| 77 | + source_path = Path(source).expanduser().resolve() |
| 78 | + if not arch: |
| 79 | + arch = _get_cuda_gpu_arch().split('=')[1] |
| 80 | + nvshmem_home = get_nvshmem_home() |
| 81 | + host_cache_key = compute_tle_raw_host_cache_key(source_path, arch) |
| 82 | + output_name = source_path.with_suffix(".so").name |
| 83 | + cache = get_cache_manager(host_cache_key) |
| 84 | + |
| 85 | + cached = None if force else cache.get_file(output_name) |
| 86 | + if cached is not None: |
| 87 | + return Path(cached), host_cache_key, True |
| 88 | + |
| 89 | + temporary = tempfile.NamedTemporaryFile( |
| 90 | + prefix=f".{output_name}.", |
| 91 | + suffix=".tmp", |
| 92 | + delete=False, |
| 93 | + ) |
| 94 | + temporary_path = Path(temporary.name) |
| 95 | + temporary.close() |
| 96 | + nvcc, _ = get_nvcc() |
| 97 | + command = [ |
| 98 | + nvcc, |
| 99 | + "-shared", |
| 100 | + "-Xcompiler", |
| 101 | + "-fPIC", |
| 102 | + "-rdc=true", |
| 103 | + f"-arch={arch}", |
| 104 | + f"-I{nvshmem_home / 'include'}", |
| 105 | + f"-L{nvshmem_home / 'lib'}", |
| 106 | + "-lnvshmem_host", |
| 107 | + "-lnvshmem_device", |
| 108 | + "-o", |
| 109 | + str(temporary_path), |
| 110 | + str(source_path), |
| 111 | + ] |
| 112 | + try: |
| 113 | + build = subprocess.run(command, capture_output=True) |
| 114 | + if build.returncode != 0: |
| 115 | + raise RuntimeError("nvcc failed while compiling CUDA host library\n" |
| 116 | + f"command: {shlex.join(command)}\n" |
| 117 | + f"stderr:\n{build.stderr.decode()}") |
| 118 | + cached_path = cache.put(temporary_path.read_bytes(), output_name, binary=True) |
| 119 | + return Path(cached_path), host_cache_key, False |
| 120 | + finally: |
| 121 | + temporary_path.unlink(missing_ok=True) |
| 122 | + |
| 123 | + |
| 124 | +class CudaHostLibrary: |
| 125 | + |
| 126 | + def __init__(self, bind_edsls, library_path, host_cache_key, type): |
| 127 | + self.edsls = bind_edsls |
| 128 | + self.path = Path(library_path).expanduser().resolve() |
| 129 | + self.host_cache_key = host_cache_key |
| 130 | + self.library = ctypes.CDLL(str(self.path)) |
| 131 | + |
| 132 | + if type == "common": |
| 133 | + for edsl in self.edsls: |
| 134 | + bind_tle_raw_common_host_cache_key(edsl, key=host_cache_key) |
| 135 | + elif type == "op": |
| 136 | + for edsl in self.edsls: |
| 137 | + bind_tle_raw_host_cache_key(edsl, key=host_cache_key) |
| 138 | + else: |
| 139 | + raise RuntimeError("Unsupported cuda-host type.") |
| 140 | + |
| 141 | + def __getattr__(self, name): |
| 142 | + return getattr(self.library, name) |
| 143 | + |
| 144 | + |
| 145 | +def get_common_host_source() -> Path: |
| 146 | + return Path(__file__).resolve().parents[1] / "cuda" / "common-host.cu" |
| 147 | + |
| 148 | + |
| 149 | +def load_host(bind_edsls, source, type="op", nvshmem_home=None, arch: str | None = None, |
| 150 | + force: bool = False) -> CudaHostLibrary: |
| 151 | + path, host_cache_key, _ = _compile_cuda_host_to_cache(source, nvshmem_home, arch, force=force) |
| 152 | + return CudaHostLibrary(bind_edsls, path, host_cache_key, type) |
| 153 | + |
| 154 | + |
| 155 | +def load_common_host(bind_edsls, source=None, type="common", nvshmem_home=None, arch: str | None = None, |
| 156 | + force: bool = False) -> CudaHostLibrary: |
| 157 | + return load_host(bind_edsls, source or get_common_host_source(), type=type, nvshmem_home=nvshmem_home, arch=arch, |
| 158 | + force=force) |
| 159 | + |
| 160 | + |
23 | 161 | def CUDA_CHECK(err): |
24 | 162 | if isinstance(err, cuda.CUresult): |
25 | 163 | if err != cuda.CUresult.CUDA_SUCCESS: |
|
0 commit comments