Fix CuPy NVRTC compilation errors on Windows. Trigger: NVRTC_ERROR_BUILTIN_OPERATION_FAILURE, nvrtc-builtins64 not found
| Item | Details |
|---|---|
| Date | 2025-12-11 |
| Goal | Fix CuPy GPU operations failing with NVRTC compilation errors on Windows |
| Environment | Windows 10/11, CUDA 12.1, CuPy 13.x, Conda environment |
| Status | Success |
CuPy uses NVRTC (NVIDIA Runtime Compiler) for JIT compilation of CUDA kernels. On Windows, the NVRTC DLLs are installed with CUDA but not automatically added to the system PATH. This causes CuPy operations to fail at runtime even when CUDA is properly installed.
cupy.cuda.compiler.CompileException: nvrtc: error: failed to open nvrtc-builtins64_121.dll.
Make sure that nvrtc-builtins64_121.dll is installed correctly.
Or:
cupy_backends.cuda.libs.nvrtc.NVRTCError: NVRTC_ERROR_BUILTIN_OPERATION_FAILURE (7)
import os
import platform
def setup_cuda_path():
"""Setup CUDA PATH for CuPy on Windows."""
if platform.system() != "Windows":
return True
# Common CUDA installation paths
cuda_paths = [
r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin",
r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.6\bin",
r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin",
]
for cuda_bin in cuda_paths:
if os.path.exists(cuda_bin):
# Check for nvrtc DLLs
nvrtc_files = [f for f in os.listdir(cuda_bin) if f.startswith('nvrtc')]
if nvrtc_files:
os.environ["PATH"] = cuda_bin + os.pathsep + os.environ.get("PATH", "")
print(f"Added CUDA to PATH: {cuda_bin}")
return True
print("CUDA not found - GPU acceleration may not work")
return False
# Call BEFORE importing CuPy-dependent code
setup_cuda_path()
# Now CuPy will work
import cupy as cp
Add this cell at the very beginning, before any other imports:
import os
cuda_bin = r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin"
os.environ["PATH"] = cuda_bin + os.pathsep + os.environ.get("PATH", "")
Add the CUDA bin directory to your system PATH:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin| Attempt | Why it Failed | Lesson Learned |
|---|---|---|
| Installing cuda-nvrtc via conda | DLLs installed to conda env, but PATH not updated | Conda doesn't auto-add Library/bin to PATH |
| Setting CUDA_PATH env variable | CuPy doesn't read CUDA_PATH for runtime libs | Must modify PATH directly |
| Adding PATH after CuPy import | CuPy caches paths at import time | PATH must be set BEFORE any CuPy import |
| CPU fallback as workaround | User explicitly wanted GPU, not CPU | Fix the root cause, don't mask it |
nvrtc64_121.dll) must match your CUDA toolkitcuda-nvrtc package, you may need to add $CONDA_PREFIX/Library/bin to PATHimport cupy as cp
x = cp.array([1, 2, 3])
print(x * 2) # Should work without errors
Works with: