`cuda.h` missing error with `torch.compile`

Hello, I am using PyTorch 2.0 and having issues with torch.compile

I have installed the nightly build of torch using the instructions on the pytorch website with CUDA 11.7

# pytorch install command
❯ conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch-nightly -c nvidia
# confirming versions
❯ python -V
Python 3.10.8
❯ python -c 'import torch;print(torch.__version__)'
2.0.0.dev20230105
❯ conda list | grep cuda
cuda                      11.7.1                        0    nvidia
...
pytorch                   2.0.0.dev20230105 py3.10_cuda11.7_cudnn8.5.0_0    pytorch-nightly
pytorch-cuda              11.7                 h67b0de4_2    pytorch-nightly

When I try torch.compile I get no issues with the first example provided in the docs

In [1]: import torch
   ...:
   ...: def foo(x, y):
   ...:     a = torch.sin(x)
   ...:     b = torch.cos(x)
   ...:     return a + b
   ...: opt_foo1 = torch.compile(foo)
   ...: print(opt_foo1(torch.randn(10, 10), torch.randn(10, 10)))
# works !

However, trying the next example that uses a resnet18 model on GPU I get many errors of the following form

/tmp/tmpsov1pory/main.c:2:10: fatal error: cuda.h: No such file or directory
 #include "cuda.h"
          ^~~~~~~~
compilation terminated.

This message prints ~ 30 times listing different tmp directories.

Is there a certain environment variable that needs to be set to allow torch to find cuda.h. I tried running find /path/to/my/conda/env -name cuda.h and adding the directories to my LD_LIBRARY_PATH but still got the same error.

Please let me know if anyone can suggest what to troubleshoot next.

Thank you

Thanks for reporting as this issue should not happen. Could you try to use CUDA_HOME=/path/to/cuda python script.py args as described here, please?

I encounter the same problem, and I solved it by setting the environment variable $C_INCLUDE_PATH

The reason for this problem is that I am using HPC cluster of my institution, and they install cuda in a weird place. Therefore in order for the GCC compiler to find cuda.h, I need to manually include the path to $C_INCLUDE_PATH

You can locate where you have your cuda installed by which nvcc. Mine is /sw/cuda/11.8.0/bin/nvcc, therefore the path I am looking for is /sw/cuda/11.8.0/include

Then type in your terminal export C_INCLUDE_PATH=$C_INCLUDE_PATH:/the/path/to/library/include. Note that the path should end with include.

This solves my problem.