"No nvcc in $PATH" output, compiling an extension with CPU optimized pytorch

username: mtgd
cooked: <p>If I try to compile a C++ extension (both JIT and setup.py variants) the first line of output is</p>
<pre><code class="lang-bash">which: no nvcc in ($PATH)
</code></pre>
<p>where $PATH is my actual path.</p>
<p>Since I installed the CPU optimized version without CUDA on my machine, I wonder whether this hints at either a problem in my installation or a bug.</p>
<p>My version is ‘1.3.1+cpu’, but the same occurs also for the nightly build. My python version is 3.7 and I installed with pip in a virtual environment.</p>

username: ptrblck
cooked: <p>Are you providing any CUDA source files or use <code>with_cuda=True</code>?<br>
I just tested a simple C++ extension on my non-GPU machine and it worked fine:</p>
<pre><code class="lang-python">cpp_source = '''
torch::Tensor my_fun(torch::Tensor z) {
  auto s = torch::relu(z);
  return s;
}
'''

module = load_inline(name='inline_extension',
                     cpp_sources=[cpp_source],
                     functions=['my_fun'])

module.my_fun(torch.randn(10))
</code></pre>

username: mtgd
cooked: <p>Loading extensions works fine, I asked this because I really have another issue I’m trying to troubleshoot, see here: <a href="https://discuss.pytorch.org/t/tensor-backward-called-within-c-extension-hangs/65473">Tensor.backward called within C++ extension hangs</a>.</p>
<p>I am just confused why that line appears and whether it might indicate a problem.<br>
Here is what I get for your specific example</p>
<pre><code class="lang-python">&gt;&gt;&gt; import torch, os
&gt;&gt;&gt; os.environ['CXX'] = 'g++'  # my default is c++, which fails
&gt;&gt;&gt; from torch.utils.cpp_extension import load_inline
which: no nvcc in (/home/...:... i.e. my $PATH)
&gt;&gt;&gt; cpp_source = '''
... torch::Tensor my_fun(torch::Tensor z) {
...   auto s = torch::relu(z);
...   return s;
... }
... '''
&gt;&gt;&gt; module = load_inline(name='inline_extension', cpp_sources=[cpp_source], functions=['my_fun'])
&gt;&gt;&gt; module.my_fun(torch.randn(10))  # everything fine.
</code></pre>
<p>Pytorch is only installed in the virtual environment, using pip. Do you not get the “no nvcc” line? For reference, here is exactly how I installed pytorch from scratch:</p>
<pre><code class="lang-bash">python3.7 -m venv env
source env/bin/activate
pip3 install torch==1.3.1+cpu torchvision==0.4.2+cpu -f https://download.pytorch.org/whl/torch_stable.html
</code></pre>

username: ptrblck
cooked: <p>No, I don’t get the message regarding the missing <code>nvcc</code> when I execute the code.<br>
Is this just a warning and your module still works or does it raise an error?</p>

username: mtgd
cooked: <p>It seems to be just a warning, since the module works. However, the message doesn’t look intentional which is why I to asked if something might be wrong.</p>

username: ptrblck
cooked: <p>I’m not sure, why this warning is thrown and it seems PyTorch thinks you are trying to load a CUDA extension.<br>
Do you have any paths or arguments in your environment, which could point to a (non-existing) <code>nvcc</code> or some CUDA folders?</p>

username: hartbx
cooked: <p>I think this is caused by this code:</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/v1.3.1/torch/utils/cpp_extension.py#L26" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/v1.3.1/torch/utils/cpp_extension.py#L26" target="_blank" rel="nofollow noopener">pytorch/pytorch/blob/v1.3.1/torch/utils/cpp_extension.py#L26</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="16" style="counter-reset: li-counter 15 ;">
<li>from .file_baton import FileBaton</li>
<li>from ._cpp_extension_versioner import ExtensionVersioner</li>
<li>
</li>
<li>from setuptools.command.build_ext import build_ext</li>
<li>
</li>
<li>
</li>
<li>IS_WINDOWS = sys.platform == 'win32'</li>
<li>
</li>
<li>BUILD_NAMEDTENSOR = os.getenv('BUILD_NAMEDTENSOR', '').upper() == '1'</li>
<li>
</li>
<li class="selected">def _find_cuda_home():</li>
<li>    '''Finds the CUDA install path.'''</li>
<li>    # Guess #1</li>
<li>    cuda_home = os.environ.get('CUDA_HOME') or os.environ.get('CUDA_PATH')</li>
<li>    if cuda_home is None:</li>
<li>        # Guess #2</li>
<li>        try:</li>
<li>            which = 'where' if IS_WINDOWS else 'which'</li>
<li>            nvcc = subprocess.check_output(</li>
<li>                [which, 'nvcc']).decode().rstrip('\r\n')</li>
<li>            cuda_home = os.path.dirname(os.path.dirname(nvcc))</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>that’s executed unconditionally when building extensions.</p>
<p>If it’s not causing an actual failure and is just alarming, you might be able to shut it up by setting <code>CUDA_HOME</code> or <code>CUDA_PATH</code> in the env even without CUDA installed.</p>