Failed to run torch.utils.cpp_extension

ali-software: <p>When I run the following code which is a simple test of cpp code in python, I get errors from Ninja and cl.exe.</p>
<blockquote>
<p>Blockquote<br>
import os<br>
import torch<br>
from torch.utils.cpp_extension import load_inline<br>
cpp_source = “”"<br>
<span class="hashtag-raw">#include</span> &lt;torch/extension.h&gt;<br>
torch::Tensor add_one(torch::Tensor input) {<br>
return input + 1;<br>
}<br>
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {<br>
m.def(“add_one”, &amp;add_one, “Add one to each element of the tensor”);<br>
}<br>
“”"<br>
build_dir = “G:\test-python\HSH-Challenge\MyTests-Pytorch”<br>
os.environ[“VS_PATH”] = “C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\bin\Hostx64\x64”<br>
module = load_inline(<br>
name=‘add_one_extension’,<br>
cpp_sources=cpp_source,<br>
functions=[‘add_one’],<br>
verbose=True,<br>
build_directory=build_dir<br>
)<br>
tensor = torch.tensor([1, 2, 3])<br>
print(module.add_one(tensor))</p>
</blockquote>
<p>The error is :</p>
<blockquote>
<p>Blockquote<br>
Emitting ninja build file G:\test-python\HSH-Challenge\MyTests-Pytorch\build.ninja…<br>
Building extension module add_one_extension…<br>
Allowing ninja to set a default number of workers… (overridable by setting the environment variable MAX_JOBS=N)<br>
[1/2] cl /showIncludes -DTORCH_EXTENSION_NAME=add_one_extension -DTORCH_API_INCLUDE_EXTENSION_H -IC:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\include -IC:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\include\torch\csrc\api\include -IC:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\include\TH -IC:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\include\THC -IC:\ProgramData\anaconda3\envs\diffrl\Include -D_GLIBCXX_USE_CXX11_ABI=0 /MD /wd4819 /wd4251 /wd4244 /wd4267 /wd4275 /wd4018 /wd4190 /EHsc -c G:\test-python\HSH-Challenge\MyTests-Pytorch\main.cpp /Fomain.o<br>
FAILED: main.o<br>
cl /showIncludes -DTORCH_EXTENSION_NAME=add_one_extension -DTORCH_API_INCLUDE_EXTENSION_H -IC:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\include -IC:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\include\torch\csrc\api\include -IC:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\include\TH -IC:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\include\THC -IC:\ProgramData\anaconda3\envs\diffrl\Include -D_GLIBCXX_USE_CXX11_ABI=0 /MD /wd4819 /wd4251 /wd4244 /wd4267 /wd4275 /wd4018 /wd4190 /EHsc -c G:\test-python\HSH-Challenge\MyTests-Pytorch\main.cpp /Fomain.o<br>
Microsoft (R) C/C++ Optimizing Compiler Version 19.21.27702.2 for x64<br>
Copyright (C) Microsoft Corporation.  All rights reserved.<br>
C:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\include\c10/macros/Macros.h(3): fatal error C1083: Cannot open include file: ‘cassert’: No such file or directory<br>
ninja: build stopped: subcommand failed.<br>
Traceback (most recent call last):<br>
File “C:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\utils\cpp_extension.py”, line 1900, in _run_ninja_build<br>
subprocess.run(<br>
File “C:\ProgramData\anaconda3\envs\diffrl\lib\subprocess.py”, line 526, in run<br>
raise CalledProcessError(retcode, process.args,<br>
subprocess.CalledProcessError: Command ‘[‘ninja’, ‘-v’]’ returned non-zero exit status 1.<br>
The above exception was the direct cause of the following exception:<br>
Traceback (most recent call last):<br>
File “g:\test-python\HSH-Challenge\MyTests-Pytorch\my_torch.py”, line 22, in <br>
module = load_inline(<br>
File “C:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\utils\cpp_extension.py”, line 1433, in load_inline<br>
return _jit_compile(<br>
File “C:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\utils\cpp_extension.py”, line 1508, in _jit_compile<br>
_write_ninja_file_and_build_library(<br>
File “C:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\utils\cpp_extension.py”, line 1623, in _write_ninja_file_and_build_library<br>
_run_ninja_build(<br>
File “C:\ProgramData\anaconda3\envs\diffrl\lib\site-packages\torch\utils\cpp_extension.py”, line 1916, in _run_ninja_build<br>
raise RuntimeError(message) from e<br>
RuntimeError: Error building extension ‘add_one_extension’</p>
</blockquote>
<p>It seems that the problem is about cl.exe but I have no idea about it. My Visual Studio version is 2019, Pytorch version is 1.13.1 and Cuda version is 11.7.</p>
<p>Also Ninja has built main.cpp and build.ninja files. Thanks for your help</p>

syed-ahmed: <p>It seems like the real error is that <code>cassert</code> is not found: <code>Cannot open include file: ‘cassert’: No such file or directory</code>. May be you need to first figure out where cassert/assert.h is in windows and adjust the VS_PATH you provided above (disclaimer: I have zero experience developing on windows and just taking a guess here).</p>

ali-software: <aside class="quote no-group" data-username="syed-ahmed" data-post="2" data-topic="206896">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/syed-ahmed/48/71522_2.png" class="avatar"> syed-ahmed:</div>
<blockquote>
<p>cassert</p>
</blockquote>
</aside>
<p>Thanks for your response. I do this but nothing has changed. I think there should be a problem with MSVC however I can’t detect it.</p>

syed-ahmed: <p>May be you can also try Step 1 and 2 from here: <a href="https://pytorch.org/audio/stable/build.windows.html" class="inline-onebox" rel="noopener nofollow ugc">Building on Windows — Torchaudio 2.4.0 documentation</a></p>

ali-software: <p>Your guidance is highly appreciated.<br>
I did them again, but I got the same error as before.</p>