Fail to build c++ extension in pytorch 2.0.0

yeungkong: <p>I am using pytorch2.0.0, vs19 and cuda11.8, and trying to build my custom c++ extension function. The code was built successfully when I was using pytorch1.11.0, vs19 and cuda11.3. The same code is built unsuccessfully when I am now using pytorch 2.0.0, vs19 and cuda11.8. I am not sure what the problem is. The following shows the error messages. Please kindly help me. Thank you very much.</p>
<pre><code class="lang-auto">D:\NP\self_code\det\extension\sigmoid_focal_loss\src&gt;python setup.py build
running build
running build_ext
D:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\utils\cpp_extension.py:359: UserWarning: Error checking compiler version for cl: [WinError 2] 系统找不到指定的文件。
  warnings.warn(f'Error checking compiler version for {compiler}: {error}')
building 'sigmoid_focal_loss_cuda' extension
Emitting ninja build file D:\NP\self_code\det\extension\sigmoid_focal_loss\src\build\temp.win-amd64-cpython-38\Release\build.ninja...
Compiling objects...
Allowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N)
[1/2] cl /showIncludes /nologo /O2 /W3 /GL /DNDEBUG /MD /MD /wd4819 /wd4251 /wd4244 /wd4267 /wd4275 /wd4018 /wd4190 /EHsc -ID:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include -ID:\Programs\Anaconda3\envs\NP\lib\site-packages
\torch\include\torch\csrc\api\include -ID:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include\TH -ID:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include\THC "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\in
clude" -ID:\Programs\Anaconda3\envs\NP\include -ID:\Programs\Anaconda3\envs\NP\Include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visua
l Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include" -c D:\NP\self_code\det\extension\sigmoid_focal_loss\src\sigmoid_focal_loss.cpp /FoD:\NP\self_code\det\extension\sigmoid_focal_loss\src\build\temp.win-amd64-cpython-38\Release\s
igmoid_focal_loss.obj -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=sigmoid_focal_loss_cuda -D_GLIBCXX_USE_CXX11_ABI=0 /std:c++17
FAILED: D:/NP/self_code/det/extension/sigmoid_focal_loss/src/build/temp.win-amd64-cpython-38/Release/sigmoid_focal_loss.obj
cl /showIncludes /nologo /O2 /W3 /GL /DNDEBUG /MD /MD /wd4819 /wd4251 /wd4244 /wd4267 /wd4275 /wd4018 /wd4190 /EHsc -ID:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include -ID:\Programs\Anaconda3\envs\NP\lib\site-packages\torch
\include\torch\csrc\api\include -ID:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include\TH -ID:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include\THC "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\include"
 -ID:\Programs\Anaconda3\envs\NP\include -ID:\Programs\Anaconda3\envs\NP\Include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Stud
io\2019\Community\VC\Tools\MSVC\14.29.30133\include" -c D:\NP\self_code\det\extension\sigmoid_focal_loss\src\sigmoid_focal_loss.cpp /FoD:\NP\self_code\det\extension\sigmoid_focal_loss\src\build\temp.win-amd64-cpython-38\Release\sigmoid
_focal_loss.obj -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=sigmoid_focal_loss_cuda -D_GLIBCXX_USE_CXX11_ABI=0 /std:c++17
注意: 包含文件:  D:\NP\self_code\det\extension\sigmoid_focal_loss\src\pytorch_cpp_helper.hpp
注意: 包含文件:   D:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include\torch\csrc\api\include\torch/types.h
注意: 包含文件:    D:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include\ATen/ATen.h
注意: 包含文件:     D:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include\ATen/Context.h
注意: 包含文件:      D:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include\ATen/CPUGeneratorImpl.h
注意: 包含文件:       D:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include\ATen/core/Generator.h
注意: 包含文件:        C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\stdint.h
注意: 包含文件:         C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vcruntime.h
注意: 包含文件:          C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\sal.h
注意: 包含文件:           C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\concurrencysal.h
注意: 包含文件:          C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\vadefs.h
注意: 包含文件:        C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\mutex
注意: 包含文件:         C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\yvals_core.h
注意: 包含文件:          C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xkeycheck.h
注意: 包含文件:         C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\__msvc_chrono.hpp
注意: 包含文件:          C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\yvals.h
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\yvals.h(12): fatal error C1083: 无法打开包括文件: “crtdbg.h”: No such file or directory
[2/2] C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin\nvcc --generate-dependencies-with-compile --dependency-output D:\NP\self_code\det\extension\sigmoid_focal_loss\src\build\temp.win-amd64-cpython-38\Release\sigmoid_focal
_loss_cuda.obj.d --use-local-env -Xcompiler /MD -Xcompiler /wd4819 -Xcompiler /wd4251 -Xcompiler /wd4244 -Xcompiler /wd4267 -Xcompiler /wd4275 -Xcompiler /wd4018 -Xcompiler /wd4190 -Xcompiler /EHsc -Xcudafe --diag_suppress=base_class_h
as_different_dll_interface -Xcudafe --diag_suppress=field_without_dll_interface -Xcudafe --diag_suppress=dll_interface_conflict_none_assumed -Xcudafe --diag_suppress=dll_interface_conflict_dllexport_assumed -ID:\Programs\Anaconda3\envs
\NP\lib\site-packages\torch\include -ID:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include\torch\csrc\api\include -ID:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include\TH -ID:\Programs\Anaconda3\envs\NP\lib\site-pack
ages\torch\include\THC "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\include" -ID:\Programs\Anaconda3\envs\NP\include -ID:\Programs\Anaconda3\envs\NP\Include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Communit
y\VC\Tools\MSVC\14.29.30133\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include" -c D:\NP\self_code\det\extension\sigmoid_focal_loss\src\sigmoid_focal_loss_cuda.cu -o D:\NP
\self_code\det\extension\sigmoid_focal_loss\src\build\temp.win-amd64-cpython-38\Release\sigmoid_focal_loss_cuda.obj -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERAT
ORS__ --expt-relaxed-constexpr -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=sigmoid_focal_loss_cuda -D_GLIBCXX_USE_CXX11_ABI=0 -gencode=arch=compute_89,code=compute_89 -gencode=arch=compute_89,code=sm_89
FAILED: D:/NP/self_code/det/extension/sigmoid_focal_loss/src/build/temp.win-amd64-cpython-38/Release/sigmoid_focal_loss_cuda.obj
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin\nvcc --generate-dependencies-with-compile --dependency-output D:\NP\self_code\det\extension\sigmoid_focal_loss\src\build\temp.win-amd64-cpython-38\Release\sigmoid_focal_loss_
cuda.obj.d --use-local-env -Xcompiler /MD -Xcompiler /wd4819 -Xcompiler /wd4251 -Xcompiler /wd4244 -Xcompiler /wd4267 -Xcompiler /wd4275 -Xcompiler /wd4018 -Xcompiler /wd4190 -Xcompiler /EHsc -Xcudafe --diag_suppress=base_class_has_dif
ferent_dll_interface -Xcudafe --diag_suppress=field_without_dll_interface -Xcudafe --diag_suppress=dll_interface_conflict_none_assumed -Xcudafe --diag_suppress=dll_interface_conflict_dllexport_assumed -ID:\Programs\Anaconda3\envs\NP\li
b\site-packages\torch\include -ID:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include\torch\csrc\api\include -ID:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\include\TH -ID:\Programs\Anaconda3\envs\NP\lib\site-packages\t
orch\include\THC "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\include" -ID:\Programs\Anaconda3\envs\NP\include -ID:\Programs\Anaconda3\envs\NP\Include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\T
ools\MSVC\14.29.30133\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include" -c D:\NP\self_code\det\extension\sigmoid_focal_loss\src\sigmoid_focal_loss_cuda.cu -o D:\NP\self_
code\det\extension\sigmoid_focal_loss\src\build\temp.win-amd64-cpython-38\Release\sigmoid_focal_loss_cuda.obj -D__CUDA_NO_HALF_OPERATORS__ -D__CUDA_NO_HALF_CONVERSIONS__ -D__CUDA_NO_BFLOAT16_CONVERSIONS__ -D__CUDA_NO_HALF2_OPERATORS__
--expt-relaxed-constexpr -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=sigmoid_focal_loss_cuda -D_GLIBCXX_USE_CXX11_ABI=0 -gencode=arch=compute_89,code=compute_89 -gencode=arch=compute_89,code=sm_89
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/include\crtdefs.h(10): fatal error C1083: 无法打开包括文件: “corecrt.h”: No such file or directory
sigmoid_focal_loss_cuda.cu
ninja: build stopped: subcommand failed.
Traceback (most recent call last):
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\utils\cpp_extension.py", line 1893, in _run_ninja_build
    subprocess.run(
  File "D:\Programs\Anaconda3\envs\NP\lib\subprocess.py", line 516, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['ninja', '-v']' returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "setup.py", line 8, in &lt;module&gt;
    setup(name='sigmoid_focal_loss',
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\__init__.py", line 107, in setup
    return distutils.core.setup(**attrs)
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\_distutils\core.py", line 185, in setup
    return run_commands(dist)
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\_distutils\core.py", line 201, in run_commands
    dist.run_commands()
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\_distutils\dist.py", line 969, in run_commands
    self.run_command(cmd)
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\dist.py", line 1244, in run_command
    super().run_command(command)
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\_distutils\dist.py", line 988, in run_command
    cmd_obj.run()
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\_distutils\command\build.py", line 131, in run
    self.run_command(cmd_name)
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\_distutils\cmd.py", line 318, in run_command
    self.distribution.run_command(command)
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\dist.py", line 1244, in run_command
    super().run_command(command)
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\_distutils\dist.py", line 988, in run_command
    cmd_obj.run()
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\command\build_ext.py", line 84, in run
    _build_ext.run(self)
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\_distutils\command\build_ext.py", line 345, in run
    self.build_extensions()
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\utils\cpp_extension.py", line 843, in build_extensions
    build_ext.build_extensions(self)
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\_distutils\command\build_ext.py", line 467, in build_extensions
    self._build_extensions_serial()
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\_distutils\command\build_ext.py", line 493, in _build_extensions_serial
    self.build_extension(ext)
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\command\build_ext.py", line 246, in build_extension
    _build_ext.build_extension(self, ext)
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\Cython\Distutils\build_ext.py", line 127, in build_extension
    super(build_ext, self).build_extension(ext)
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\setuptools\_distutils\command\build_ext.py", line 548, in build_extension
    objects = self.compiler.compile(
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\utils\cpp_extension.py", line 815, in win_wrap_ninja_compile
    _write_ninja_file_and_compile_objects(
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\utils\cpp_extension.py", line 1574, in _write_ninja_file_and_compile_objects
    _run_ninja_build(
  File "D:\Programs\Anaconda3\envs\NP\lib\site-packages\torch\utils\cpp_extension.py", line 1909, in _run_ninja_build
    raise RuntimeError(message) from e
RuntimeError: Error compiling objects for extension
</code></pre>
<h3><a name="versions-1" class="anchor" href="#versions-1"></a>Versions</h3>
<p>PyTorch version: 2.0.0<br>
Is debug build: False<br>
CUDA used to build PyTorch: 11.8<br>
ROCM used to build PyTorch: N/A</p>
<p>OS: Microsoft Windows 10 专业版<br>
GCC version: Could not collect<br>
Clang version: Could not collect<br>
CMake version: Could not collect<br>
Libc version: N/A</p>
<p>Python version: 3.8.17 (default, Jul 5 2023, 20:44:21) [MSC v.1916 64 bit (AMD64)] (64-bit runtime)<br>
Python platform: Windows-10-10.0.19045-SP0<br>
Is CUDA available: True<br>
CUDA runtime version: 11.8.89<br>
CUDA_MODULE_LOADING set to: LAZY<br>
GPU models and configuration: Could not collect<br>
Nvidia driver version: Could not collect<br>
cuDNN version: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin\cudnn_ops_train64_8.dll<br>
HIP runtime version: N/A<br>
MIOpen runtime version: N/A<br>
Is XNNPACK available: True</p>
<p>CPU:<br>
‘wmic’ 不是内部或外部命令，也不是可运行的程序<br>
或批处理文件。</p>
<p>Versions of relevant libraries:<br>
[pip3] numpy==1.23.2<br>
[pip3] torch==2.0.0<br>
[pip3] torchaudio==2.0.0<br>
[pip3] torchvision==0.15.0<br>
[conda] blas 2.117 mkl conda-forge<br>
[conda] blas-devel 3.9.0 17_win64_mkl conda-forge<br>
[conda] libblas 3.9.0 17_win64_mkl conda-forge<br>
[conda] libcblas 3.9.0 17_win64_mkl conda-forge<br>
[conda] liblapack 3.9.0 17_win64_mkl conda-forge<br>
[conda] liblapacke 3.9.0 17_win64_mkl conda-forge<br>
[conda] mkl 2022.1.0 h6a75c08_874 conda-forge<br>
[conda] mkl-devel 2022.1.0 h57928b3_875 conda-forge<br>
[conda] mkl-include 2022.1.0 h6a75c08_874 conda-forge<br>
[conda] numpy 1.23.2 py38h223ccf5_0 conda-forge<br>
[conda] pytorch 2.0.0 py3.8_cuda11.8_cudnn8_0 <a href="https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch" class="inline-onebox" rel="noopener nofollow ugc">Index of /anaconda/cloud/pytorch/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror</a><br>
[conda] pytorch-cuda 11.8 h24eeafa_5 <a href="https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch" class="inline-onebox" rel="noopener nofollow ugc">Index of /anaconda/cloud/pytorch/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror</a><br>
[conda] pytorch-mutex 1.0 cuda <a href="https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch" class="inline-onebox" rel="noopener nofollow ugc">Index of /anaconda/cloud/pytorch/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror</a><br>
[conda] torchaudio 2.0.0 pypi_0 pypi<br>
[conda] torchvision 0.15.0 pypi_0 pypi</p>

ptrblck: <p>The error is raised in:</p>
<pre><code class="lang-auto">C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/include\crtdefs.h(10): fatal error C1083: 无法打开包括文件: “corecrt.h”: No such file or directory
</code></pre>
<p>and seems to be VisualStudio specific. I’m not deeply familiar with Windows development, but you could search for similar issues and check how these were resolved.</p>