Find a CUDA toolkit installation with nvcc that supports the current GPU.
()
| 4 | |
| 5 | |
| 6 | def _find_cuda_home() -> str | None: |
| 7 | """Find a CUDA toolkit installation with nvcc that supports the current GPU.""" |
| 8 | # Respect explicit CUDA_HOME |
| 9 | if os.environ.get("CUDA_HOME"): |
| 10 | return os.environ["CUDA_HOME"] |
| 11 | |
| 12 | # Check /usr/local/cuda symlink (standard CUDA toolkit location) |
| 13 | if os.path.isfile("/usr/local/cuda/bin/nvcc"): |
| 14 | return "/usr/local/cuda" |
| 15 | |
| 16 | # Search /usr/local/cuda-* directories (prefer highest version) |
| 17 | import glob |
| 18 | |
| 19 | candidates = sorted(glob.glob("/usr/local/cuda-*/bin/nvcc"), reverse=True) |
| 20 | for nvcc_path in candidates: |
| 21 | cuda_home = os.path.dirname(os.path.dirname(nvcc_path)) |
| 22 | return cuda_home |
| 23 | |
| 24 | # Fall back to system nvcc location |
| 25 | nvcc = shutil.which("nvcc") |
| 26 | if nvcc: |
| 27 | real = os.path.realpath(nvcc) |
| 28 | bin_dir = os.path.dirname(real) |
| 29 | return os.path.dirname(bin_dir) |
| 30 | |
| 31 | return None |
| 32 | |
| 33 | |
| 34 | def _check_nvcc_supports_gpu(cuda_home: str) -> None: |