|
4 | 4 | import os |
5 | 5 | from typing import Sequence, Union |
6 | 6 | import logging |
| 7 | +import functools |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from packaging.version import Version |
| 11 | +import re |
7 | 12 |
|
8 | 13 | import time |
9 | 14 | import random |
10 | 15 |
|
11 | 16 | import torch |
12 | 17 | from torch.utils.cpp_extension import load as _load |
| 18 | +from torch.utils.cpp_extension import _find_cuda_home |
| 19 | + |
13 | 20 |
|
14 | 21 | # print("INCLUDE:", torch.utils.cpp_extension.include_paths(cuda=True)) |
15 | 22 | # print("C++ compat", torch.utils.cpp_extension.check_compiler_abi_compatibility("g++")) |
16 | 23 | # print("C compat", torch.utils.cpp_extension.check_compiler_abi_compatibility("gcc")) |
17 | 24 |
|
18 | 25 | LOGGER = logging.getLogger(__name__) |
| 26 | +CUDA_HOME = _find_cuda_home() |
| 27 | +IS_WINDOWS = sys.platform == 'win32' |
| 28 | +IS_MACOS = sys.platform.startswith('darwin') |
| 29 | +SUBPROCESS_DECODE_ARGS = ('oem',) if IS_WINDOWS else () |
| 30 | + |
| 31 | +@functools.cache |
| 32 | +def get_cuda_version() -> Version | None: |
| 33 | + try: |
| 34 | + nvcc = os.path.join(CUDA_HOME, 'bin', 'nvcc.exe' if IS_WINDOWS else 'nvcc') |
| 35 | + cuda_version_str = subprocess.check_output([nvcc, '--version']).strip().decode(*SUBPROCESS_DECODE_ARGS) |
| 36 | + cuda_version = re.search(r'release (\d+[.]\d+)', cuda_version_str) |
| 37 | + |
| 38 | + if cuda_version is None: |
| 39 | + return |
| 40 | + cuda_str_version = cuda_version.group(1) |
| 41 | + cuda_version = Version(cuda_str_version) |
| 42 | + return cuda_version |
| 43 | + except (RuntimeError, FileNotFoundError, subprocess.CalledProcessError) as e: |
| 44 | + # raise RuntimeError( |
| 45 | + # "Could not determine CUDA version." |
| 46 | + # ) from e |
| 47 | + return |
19 | 48 |
|
20 | 49 |
|
21 | 50 | def defines_to_cflags( |
@@ -124,6 +153,11 @@ def load(*, name, sources, extra_cflags=(), extra_cuda_cflags=(), **kwargs): |
124 | 153 | *extra_cuda_cflags, |
125 | 154 | ], |
126 | 155 | } |
| 156 | + |
| 157 | + cuda_version = get_cuda_version() |
| 158 | + if cuda_version is not None and cuda_version >= Version("12.8"): |
| 159 | + myargs['extra_cuda_cflags'].append("-static-global-template-stub=false") |
| 160 | + |
127 | 161 | LOGGER.info("Kernel compilation arguments", myargs) |
128 | 162 | myargs.update(**kwargs) |
129 | 163 | # add random waiting time to minimize deadlocks because of badly managed multicompile of pytorch ext |
|
0 commit comments