|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +"""Assemble part of a CUDA toolkit from NVIDIA's redistributable component archives. |
| 9 | +
|
| 10 | +The Windows CUDA wheel end-to-end jobs cover several CUDA trains while the CI images carry |
| 11 | +one toolkit, and NVIDIA publishes no full Windows installer for every release. The redist |
| 12 | +archives exist for every release on both platforms, need no installer or admin rights, and |
| 13 | +are checksummed in a JSON index. Merging the components a job needs into one directory |
| 14 | +gives the layout nvcc, CMake's FindCUDAToolkit and torch's cross-compile step expect. |
| 15 | +
|
| 16 | + install_cuda_redist.py --train 13.4 --platform linux-x86_64 --dest /opt/cuda-13.4 |
| 17 | + install_cuda_redist.py --train 13.4 --platform windows-x86_64 --dest DIR --components cuda_cudart |
| 18 | +""" |
| 19 | + |
| 20 | +import argparse |
| 21 | +import hashlib |
| 22 | +import json |
| 23 | +import shutil |
| 24 | +import sys |
| 25 | +import tarfile |
| 26 | +import tempfile |
| 27 | +import urllib.request |
| 28 | +import zipfile |
| 29 | +from pathlib import Path |
| 30 | + |
| 31 | +_REDIST = "https://developer.download.nvidia.com/compute/cuda/redist" |
| 32 | + |
| 33 | +# The patch release each train resolves to: one entry per train in filter_cuda_matrix.py's |
| 34 | +# SUPPORTED_CUDA_VERSIONS, which the end-to-end jobs take their matrix from, and 13.0, which |
| 35 | +# is no longer published but still builds from source and is what local tests of it use. A |
| 36 | +# train missing here fails the job at argument parsing rather than testing a different |
| 37 | +# release. |
| 38 | +_RELEASES = {"13.0": "13.0.2", "13.2": "13.2.2", "13.4": "13.4.1"} |
| 39 | + |
| 40 | +# What a build of the CUDA backend needs. "a|b" names one component a release publishes |
| 41 | +# under either name (CCCL was renamed between releases). |
| 42 | +_BUILD_COMPONENTS = [ |
| 43 | + "cuda_nvcc", |
| 44 | + "cuda_cudart", |
| 45 | + "cuda_crt", |
| 46 | + "cccl|cuda_cccl", |
| 47 | + "libnvvm", |
| 48 | + "cuda_cuobjdump", |
| 49 | + "cuda_nvrtc", |
| 50 | + "cuda_profiler_api", |
| 51 | + "cuda_nvtx", |
| 52 | + # rand.cu includes curand_kernel.h. |
| 53 | + "libcurand", |
| 54 | +] |
| 55 | + |
| 56 | + |
| 57 | +def _fetch(url: str, destination: Path) -> None: |
| 58 | + with urllib.request.urlopen(url, timeout=300) as response, open( |
| 59 | + destination, "wb" |
| 60 | + ) as handle: |
| 61 | + shutil.copyfileobj(response, handle) |
| 62 | + |
| 63 | + |
| 64 | +def _extract(archive: Path, into: Path) -> Path: |
| 65 | + if archive.suffix == ".zip": |
| 66 | + with zipfile.ZipFile(archive) as bundle: |
| 67 | + bundle.extractall(into) |
| 68 | + else: |
| 69 | + with tarfile.open(archive) as bundle: |
| 70 | + if hasattr(tarfile, "fully_trusted_filter"): |
| 71 | + bundle.extractall(into, filter="fully_trusted") |
| 72 | + else: |
| 73 | + bundle.extractall(into) |
| 74 | + # Each archive holds one top-level directory named after itself. |
| 75 | + (top,) = [entry for entry in into.iterdir() if entry.is_dir()] |
| 76 | + return top |
| 77 | + |
| 78 | + |
| 79 | +def main() -> None: |
| 80 | + parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) |
| 81 | + parser.add_argument("--train", required=True, choices=sorted(_RELEASES)) |
| 82 | + parser.add_argument( |
| 83 | + "--platform", required=True, choices=["linux-x86_64", "windows-x86_64"] |
| 84 | + ) |
| 85 | + parser.add_argument("--dest", required=True, type=Path) |
| 86 | + parser.add_argument("--components", nargs="+", default=_BUILD_COMPONENTS) |
| 87 | + arguments = parser.parse_args() |
| 88 | + |
| 89 | + release = _RELEASES[arguments.train] |
| 90 | + with urllib.request.urlopen( |
| 91 | + f"{_REDIST}/redistrib_{release}.json", timeout=120 |
| 92 | + ) as response: |
| 93 | + index = json.load(response) |
| 94 | + arguments.dest.mkdir(parents=True, exist_ok=True) |
| 95 | + with tempfile.TemporaryDirectory() as work: |
| 96 | + work = Path(work) |
| 97 | + for choice in arguments.components: |
| 98 | + names = [ |
| 99 | + name |
| 100 | + for name in choice.split("|") |
| 101 | + if arguments.platform in index.get(name, {}) |
| 102 | + ] |
| 103 | + if not names: |
| 104 | + sys.exit( |
| 105 | + f"CUDA {release} publishes none of {choice} for {arguments.platform}" |
| 106 | + ) |
| 107 | + entry = index[names[0]][arguments.platform] |
| 108 | + archive = work / Path(entry["relative_path"]).name |
| 109 | + _fetch(f"{_REDIST}/{entry['relative_path']}", archive) |
| 110 | + digest = hashlib.sha256(archive.read_bytes()).hexdigest() |
| 111 | + if digest != entry["sha256"]: |
| 112 | + sys.exit(f"checksum mismatch for {archive.name}") |
| 113 | + staging = work / "staging" |
| 114 | + top = _extract(archive, staging) |
| 115 | + shutil.copytree(top, arguments.dest, symlinks=True, dirs_exist_ok=True) |
| 116 | + shutil.rmtree(staging) |
| 117 | + archive.unlink() |
| 118 | + print( |
| 119 | + f"merged {names[0]} {index[names[0]]['version']} into {arguments.dest}" |
| 120 | + ) |
| 121 | + # The Linux archives keep libraries in lib/, while nvcc's link step and CMake's |
| 122 | + # FindCUDAToolkit look in lib64/, which is where an installed toolkit has them. |
| 123 | + lib, lib64 = arguments.dest / "lib", arguments.dest / "lib64" |
| 124 | + if arguments.platform == "linux-x86_64" and lib.is_dir() and not lib64.exists(): |
| 125 | + lib64.symlink_to("lib", target_is_directory=True) |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + main() |
0 commit comments