NVRTC_ERROR_COMPILATION cuda build_error ai_generated true

RuntimeError: NVRTC compilation failed: error: cannot open source file 'cuda.h'

ID: cuda/nvrtc-cannot-open-source-file-cuda

Also available as: JSON · Markdown · 中文
85%Fix Rate
88%Confidence
1Evidence
2023-08-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
CUDA 11.7 active
CUDA 12.0 active
CUDA 12.2 active

Root Cause

The NVRTC (CUDA runtime compilation) library cannot find the CUDA header files because the CUDA include path is not set correctly or the CUDA toolkit is not installed.

generic

中文

NVRTC(CUDA 运行时编译)库无法找到 CUDA 头文件,因为 CUDA 包含路径未正确设置或未安装 CUDA 工具包。

Official Documentation

https://docs.nvidia.com/cuda/nvrtc/index.html

Workarounds

  1. 85% success Install the full CUDA toolkit (e.g., version 12.2) and set the CUDA_PATH environment variable to the installation directory. For Ubuntu: wget https://developer.download.nvidia.com/compute/cuda/12.2.0/local_installers/cuda_12.2.0_535.54.03_linux.run; sudo sh cuda_12.2.0_535.54.03_linux.run --toolkit --silent --toolkitpath=/usr/local/cuda-12.2; export CUDA_PATH=/usr/local/cuda-12.2; export CPATH=$CUDA_PATH/include:$CPATH; then retry NVRTC compilation.
    Install the full CUDA toolkit (e.g., version 12.2) and set the CUDA_PATH environment variable to the installation directory. For Ubuntu: wget https://developer.download.nvidia.com/compute/cuda/12.2.0/local_installers/cuda_12.2.0_535.54.03_linux.run; sudo sh cuda_12.2.0_535.54.03_linux.run --toolkit --silent --toolkitpath=/usr/local/cuda-12.2; export CUDA_PATH=/usr/local/cuda-12.2; export CPATH=$CUDA_PATH/include:$CPATH; then retry NVRTC compilation.
  2. 80% success If using conda, install the cuda-toolkit package: conda install -c nvidia cuda-toolkit=12.2. This places headers in the conda environment's include directory, which NVRTC can find automatically.
    If using conda, install the cuda-toolkit package: conda install -c nvidia cuda-toolkit=12.2. This places headers in the conda environment's include directory, which NVRTC can find automatically.

中文步骤

  1. Install the full CUDA toolkit (e.g., version 12.2) and set the CUDA_PATH environment variable to the installation directory. For Ubuntu: wget https://developer.download.nvidia.com/compute/cuda/12.2.0/local_installers/cuda_12.2.0_535.54.03_linux.run; sudo sh cuda_12.2.0_535.54.03_linux.run --toolkit --silent --toolkitpath=/usr/local/cuda-12.2; export CUDA_PATH=/usr/local/cuda-12.2; export CPATH=$CUDA_PATH/include:$CPATH; then retry NVRTC compilation.
  2. If using conda, install the cuda-toolkit package: conda install -c nvidia cuda-toolkit=12.2. This places headers in the conda environment's include directory, which NVRTC can find automatically.

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Reinstalling PyTorch doesn't fix missing CUDA headers because it ships with its own CUDA runtime but not the full toolkit headers.

  2. 80% fail

    Setting CUDA_HOME to a non-existent directory is a common mistake that doesn't resolve the include path.