Leszek Bukowski commited on
Commit ·
45b7d9f
1
Parent(s): 55983af
Match the demo extension compiler to CUDA 13.0
Browse files1 unmodified line234567891011121379 unmodified lines9394959697989910010110210310416 unmodified lines12112212312412512623 unmodified lines15015115215315415519 unmodified lines1751761771781791801811 unmodified linefrom contextlib import contextmanagerimport csvimport hashlibfrom importlib.metadata import distribution, PackageNotFoundErrorimport jsonimport mathimport osfrom pathlib import Pathimport platformimport signalimport shutilimport subprocess79 unmodified linesprint("Python and memory passed; native BF16 support will be verified after installation.", flush=True)def run_command(command, log_path, allowed=(0,)):"""Keep detailed output on disk while the enclosing stage shows heartbeats."""with Path(log_path).open("a", encoding="utf-8") as log:log.write(f"\n{time.strftime('%Y-%m-%d %H:%M:%S')} — {' '.join(command)}\n")log.flush()child = subprocess.Popen(command, stdout=log, stderr=subprocess.STDOUT, start_new_session=True)try:returncode = child.wait()except BaseException:16 unmodified linesreturn returncodedef install_wheel(variant, log_path):from huggingface_hub import hf_hub_downloadfrom huggingface_hub.errors import HfHubHTTPError23 unmodified linesdef verify_live_runtime():from thesystem._vendor.lora.setup_runtime import verify_runtimeverify_runtime()import torch19 unmodified lineswith visible_progress("[3/5] Download, verify and install the release wheel"):install_wheel(variant, log_path)with visible_progress("[4/5] Prepare GPU libraries — first setup may take several minutes"):code = run_command([sys.executable, "-m", "thesystem._vendor.lora.setup_runtime"], log_path, allowed=(0, 10))if code == 10:raise RestartRequired("GPU libraries were installed or updated.")with visible_progress("[5/5] Verify the active kernel, CUDA and native BF16"):1 unmodified line23456789101112131479 unmodified lines94959697989910010110210310410516 unmodified lines12212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917023 unmodified lines19419519619719819920019 unmodified lines2202212222232242252262272281 unmodified linefrom contextlib import contextmanagerimport csvimport hashlibfrom importlib.metadata import distribution, PackageNotFoundError, versionimport jsonimport mathimport osfrom pathlib import Pathimport platformimport reimport signalimport shutilimport subprocess79 unmodified linesprint("Python and memory passed; native BF16 support will be verified after installation.", flush=True)def run_command(command, log_path, allowed=(0,), env=None):"""Keep detailed output on disk while the enclosing stage shows heartbeats."""with Path(log_path).open("a", encoding="utf-8") as log:log.write(f"\n{time.strftime('%Y-%m-%d %H:%M:%S')} — {' '.join(command)}\n")log.flush()child = subprocess.Popen(command, stdout=log, stderr=subprocess.STDOUT, start_new_session=True, env=env)try:returncode = child.wait()except BaseException:16 unmodified linesreturn returncodedef nvcc_release(path):if not path or not Path(path).is_file():return Noneresult = subprocess.run([str(path), "--version"], capture_output=True, text=True, timeout=30)match = re.search(r"release (\d+\.\d+)", result.stdout) if result.returncode == 0 else Nonereturn match.group(1) if match else Nonedef pip_nvcc():try:package = distribution("nvidia-cuda-nvcc")except PackageNotFoundError:return Nonereturn next((Path(package.locate_file(f)) for f in package.files or []if str(f).endswith("/bin/nvcc")), None)def prepare_cuda_compiler(log_path):"""Match the extension compiler to the pinned CUDA 13.0 PyTorch build."""configured = Path(os.environ["CUDA_HOME"]) / "bin/nvcc" if os.environ.get("CUDA_HOME") else Nonecandidates = (configured, shutil.which("nvcc"), pip_nvcc())nvcc = next((Path(path) for path in candidates if nvcc_release(path) == "13.0"), None)if nvcc is None:print("Installing NVIDIA CUDA 13.0 compiler and headers for the GPU extension…", flush=True)run_command([sys.executable, "-m", "pip", "install", "cuda-toolkit[nvcc,cccl]==13.0.3"], log_path)nvcc = pip_nvcc()if nvcc_release(nvcc) != "13.0":raise RuntimeError("CUDA 13.0 compiler setup failed. Inspect the setup log before continuing.")# Set this before starting the extension builder: its CUDA_HOME is read at import time.os.environ["CUDA_HOME"] = str(nvcc.parent.parent)os.environ["PATH"] = str(nvcc.parent) + os.pathsep + os.environ.get("PATH", "")print(f"CUDA compiler verified: 13.0 ({nvcc}). Host driver unchanged.", flush=True)def check_loaded_versions():"""Colab may preload NumPy; replacing its files does not refresh those imports."""for name in ("numpy", "torch", "transformers"):module = sys.modules.get(name)loaded = getattr(module, "__version__", None)if loaded and str(loaded).split("+")[0] != version(name).split("+")[0]:raise RestartRequired(f"{name} is still loaded from an earlier installation.")def install_wheel(variant, log_path):from huggingface_hub import hf_hub_downloadfrom huggingface_hub.errors import HfHubHTTPError23 unmodified linesdef verify_live_runtime():check_loaded_versions()from thesystem._vendor.lora.setup_runtime import verify_runtimeverify_runtime()import torch19 unmodified lineswith visible_progress("[3/5] Download, verify and install the release wheel"):install_wheel(variant, log_path)with visible_progress("[4/5] Prepare GPU libraries — first setup may take several minutes"):prepare_cuda_compiler(log_path)code = run_command([sys.executable, "-m", "thesystem._vendor.lora.setup_runtime"], log_path,allowed=(0, 10), env={**os.environ, "PIP_VERBOSE": "1"})if code == 10:raise RestartRequired("GPU libraries were installed or updated.")with visible_progress("[5/5] Verify the active kernel, CUDA and native BF16"):
prepare_runtime.py+51-4