PyTorch build from source on Windows

Here it is:

Microsoft Windows [versão 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Todos os direitos reservados.

C:\Users\César>set
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\César\AppData\Roaming
asl.log=Destination=file
CFLAGS="C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include"
CommonProgramFiles=C:\Program Files\Common Files
CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files
CommonProgramW6432=C:\Program Files\Common Files
COMPUTERNAME=CÉSAR-PC
ComSpec=C:\Windows\system32\cmd.exe
CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1
CUDA_PATH_V10_1=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1
CUDA_PATH_V10_2=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Users\César
INCLUDE=C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_181
LDFLAGS=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\lib\x64
LIBRARY_PATH=C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um
LOCALAPPDATA=C:\Users\César\AppData\Local
LOGONSERVER=\\CÉSAR-PC
MAYA_SCRIPT_PATH=%VPROJECT%\..\sdktools\maya\scripts
NUMBER_OF_PROCESSORS=6
NVCC=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\bin\nvcc.exe
NVCUDASAMPLES10_1_ROOT=C:\ProgramData\NVIDIA Corporation\CUDA Samples\v10.1
NVCUDASAMPLES10_2_ROOT=C:\ProgramData\NVIDIA Corporation\CUDA Samples\v10.2
NVCUDASAMPLES9_2_ROOT=C:\ProgramData\NVIDIA Corporation\CUDA Samples\v9.2
NVCUDASAMPLES_ROOT=C:\ProgramData\NVIDIA Corporation\CUDA Samples\v10.1
NVTOOLSEXT_PATH=C:\Program Files\NVIDIA Corporation\NvToolsExt\
OS=Windows_NT
Path=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin;C:\Program Fil
es\NVIDIA GPU Computing Toolkit\CUDA\v10.1\libnvvp;;C:\Program Files\NVIDIA GPU
Computing Toolkit\CUDA\v10.2\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\C
UDA\v10.2\libnvvp;C:\ProgramData\Miniconda3;C:\ProgramData\Miniconda3\Library\mi
ngw-w64\bin;C:\ProgramData\Miniconda3\Library\usr\bin;C:\ProgramData\Miniconda3\
Library\bin;C:\ProgramData\Miniconda3\Scripts;C:\Program Files (x86)\Common File
s\Oracle\Java\javapath;H:\Rockwell\RSCommon;C:\Program Files\NVIDIA GPU Computin
g Toolkit\CUDA\v9.2\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\
libnvvp;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program F
iles (x86)\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Win
dows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Pro
gram Files (x86)\Windows Live\Shared;C:\Program Files\Java\jdk1.8.0_181\bin;C:\P
rogram Files\NVIDIA Corporation\NVIDIA NvDLISR;H:\MATLAB\runtime\win64;H:\MATLAB
\bin;H:\MATLAB\polyspace\bin;C:\Program Files\Calibre2\;C:\freeglut\bin;C:\freeg
lut\bin\x64;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.2\lib\x64;C:\P
rogram Files (x86)\NVIDIA Corporation\PhysX\Common;C:\FFmpeg\bin;C:\Program File
s (x86)\sox-14-4-2;H:\MikTex\miktex\bin\x64\;C:\Program Files\nodejs\;C:\Users\C
ésar\AppData\Local\GitHubDesktop\bin;C:\Program Files\Git\cmd;C:\Program Files\L
LVM\bin;C:\Program Files (x86)\Tesseract-OCR;C:\Program Files\dotnet\;C:\Program
 Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1;C:\Program Files\NVIDIA Corporati
on\Nsight Compute 2019.1\;C:\Program Files\CMake;C:\Users\César\AppData\Local\Pr
ograms\Python\Python38\Scripts\;C:\Users\César\AppData\Local\Programs\Python\Pyt
hon38\;C:\Python27\;C:\Users\César\AppData\Local\Programs\Python\Python36\Script
s\;C:\Users\César\AppData\Local\Programs\Python\Python36\;C:\Program Files\NVIDI
A GPU Computing Toolkit\CUDA\v9.0\bin;C:\Program Files (x86)\sox-14-4-2;%VPROJEC
T%\..\bin;C:\freeglut\bin\x64;C:\Users\César\AppData\Local\GitHubDesktop\bin;H:\
PyCharm\PyCharm Community Edition 2019.3\bin;C:\Users\César\AppData\Roaming\npm;
C:\Users\César\AppData\Local\Programs\Microsoft VS Code\bin
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=AMD64 Family 21 Model 2 Stepping 0, AuthenticAMD
PROCESSOR_LEVEL=21
PROCESSOR_REVISION=0200
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files
PROMPT=$P$G
PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
PUBLIC=C:\Users\Public
PyCharm Community Edition=H:\PyCharm\PyCharm Community Edition 2019.3\bin;
SESSIONNAME=Console
sourcesdk=H:\SteamLibrary\steamapps\common\SourceSDK
SystemDrive=C:
SystemRoot=C:\Windows
TEMP=C:\Users\CSAR~1\AppData\Local\Temp
TMP=C:\Users\CSAR~1\AppData\Local\Temp
USERDOMAIN=César-PC
USERNAME=César
USERPROFILE=C:\Users\César
VBOX_MSI_INSTALL_PATH=D:\Arquivos de Programas\Oracle\VirtualBox\
windir=C:\Windows
windows_tracing_flags=3
windows_tracing_logfile=C:\BVTBin\Tests\installpackage\csilogfile.log
XSI_PLUGINS=%VPROJECT%\..\sdktools\xsi\5.1\valvesource
XSI_VERSION=5.1

C:\Users\César>

Please clear the value of CFLAGS using set CFLAGS=, remove the cache in the build dir and try again.

Hello, I am having a similar issue and I am not sure how to fix it.

I am trying to build Pytorch from source on Windows 10. I have an old 780m GPU so I use CUDA 10.1.

What I have been doing is in this order:

  • Installed Visual Studio 2019 with the Desktop C++ toolset
  • Installed CUDA 10.1 with visual studio integration (everything checked under CUDA basically) => success
  • Added Cudnn 10.1 to the CUDA folder as explained in the nvidia documentation
  • conda install numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing_extensions future six requests dataclasses
  • git clone --recursive https://github.com/pytorch/pytorch
  • cd pytorch
  • python setup.py install

And everything seems to go smoothly, except that a pop-up saying that the C++ compiler stopped working appears during compilation. If I don’t touch this pop-up the program keeps compiling for several hours until it hangs at some point, and when I select “close the program” in the pop-up the following error appears in conda prompt:

C:\Users\Yann\Desktop\git\pytorch\third_party\ideep\mkl-dnn\src\cpu\gemm_convolution_utils.cpp(401) : fatal error C1001: Erreur interne du compilateur.
(fichier du compilateur 'd:\agent\_work\7\s\src\vctools\Compiler\Utc\src\p2\main.c', ligne 195)
 Pour contourner ce problème, essayez de simplifier ou de changer le programme à proximité des emplacements listés ci-dessus.
Si possible, indiquez ici la procédure permettant de reproduire le problème : https://developercommunity.visualstudio.com
Choisissez la commande Support technique du menu ? (Aide) de Visual C++,
 ou ouvrez le fichier d'aide du Support technique pour plus d'informations
  cl!RaiseException()+0x69
  cl!RaiseException()+0x69
  cl!CloseTypeServerPDB()+0x22e6b
  cl!CloseTypeServerPDB()+0xcd30a

ninja: build stopped: subcommand failed.
Traceback (most recent call last):
  File "setup.py", line 748, in <module>
    build_deps()
  File "setup.py", line 327, in build_deps
    build_caffe2(version=version,
  File "C:\Users\Yann\Desktop\git\pytorch\tools\build_pytorch_libs.py", line 62, in build_caffe2
    cmake.build(my_env)
  File "C:\Users\Yann\Desktop\git\pytorch\tools\setup_helpers\cmake.py", line 345, in build
    self.run(build_args, my_env)
  File "C:\Users\Yann\Desktop\git\pytorch\tools\setup_helpers\cmake.py", line 141, in run
    check_call(command, cwd=self.build_dir, env=env)
  File "C:\Users\Yann\anaconda3\envs\pytorch\lib\subprocess.py", line 364, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '--build', '.', '--target', 'install', '--config', 'Release', '--', '-j', '8']' returned non-zero exit status 1.

Did I do something wrong please? I have also tried installing VS2017 instead of 2019 but then the CUDA integration would fail.

I have tried the script from the author, and it failed with this output:

(pytorch) C:\Users\Yann\Desktop\git\pytorch>python setup.py build --cmake
Building wheel torch-1.7.0a0+ac1f471
– Building version 1.7.0a0+ac1f471
cmake -GNinja -DBUILD_PYTHON=True -DBUILD_TEST=True -DCMAKE_BUILD_TYPE=Release -DCMAKE_GENERATOR=Ninja -DCMAKE_INSTALL_PREFIX=C:\Users\Yann\Desktop\git\pytorch\torch -DCMAKE_PREFIX_PATH=C:\Users\Yann\anaconda3\envs\pytorch\Lib\site-packages -DNUMPY_INCLUDE_DIR=C:\Users\Yann\anaconda3\envs\pytorch\lib\site-packages\numpy\core\include -DPYTHON_EXECUTABLE=C:\Users\Yann\anaconda3\envs\pytorch\python.exe -DPYTHON_INCLUDE_DIR=C:\Users\Yann\anaconda3\envs\pytorch\include -DPYTHON_LIBRARY=C:\Users\Yann\anaconda3\envs\pytorch/libs/python38.lib -DTORCH_BUILD_VERSION=1.7.0a0+ac1f471 -DUSE_NUMPY=True C:\Users\Yann\Desktop\git\pytorch
CMake Error at C:/Users/Yann/anaconda3/envs/pytorch/Library/share/cmake-3.17/Modules/CMakeDetermineCXXCompiler.cmake:48 (message):
Could not find compiler set in environment variable CXX:

cl.exe.

Call Stack (most recent call first):
CMakeLists.txt:23 (project)

CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
– Configuring incomplete, errors occurred!
See also “C:/Users/Yann/Desktop/git/pytorch/build/CMakeFiles/CMakeOutput.log”.
See also “C:/Users/Yann/Desktop/git/pytorch/build/CMakeFiles/CMakeError.log”.
Traceback (most recent call last):
File “setup.py”, line 748, in
build_deps()
File “setup.py”, line 327, in build_deps
build_caffe2(version=version,
File “C:\Users\Yann\Desktop\git\pytorch\tools\build_pytorch_libs.py”, line 54, in build_caffe2
cmake.generate(version,
File “C:\Users\Yann\Desktop\git\pytorch\tools\setup_helpers\cmake.py”, line 329, in generate
self.run(args, env=my_env)
File “C:\Users\Yann\Desktop\git\pytorch\tools\setup_helpers\cmake.py”, line 141, in run
check_call(command, cwd=self.build_dir, env=env)
File “C:\Users\Yann\anaconda3\envs\pytorch\lib\subprocess.py”, line 364, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command ‘[‘cmake’, ‘-GNinja’, ‘-DBUILD_PYTHON=True’, ‘-DBUILD_TEST=True’, ‘-DCMAKE_BUILD_TYPE=Release’, ‘-DCMAKE_GENERATOR=Ninja’, ‘-DCMAKE_INSTALL_PREFIX=C:\Users\Yann\Desktop\git\pytorch\torch’, ‘-DCMAKE_PREFIX_PATH=C:\Users\Yann\anaconda3\envs\pytorch\Lib\site-packages’, ‘-DNUMPY_INCLUDE_DIR=C:\Users\Yann\anaconda3\envs\pytorch\lib\site-packages\numpy\core\include’, ‘-DPYTHON_EXECUTABLE=C:\Users\Yann\anaconda3\envs\pytorch\python.exe’, ‘-DPYTHON_INCLUDE_DIR=C:\Users\Yann\anaconda3\envs\pytorch\include’, ‘-DPYTHON_LIBRARY=C:\Users\Yann\anaconda3\envs\pytorch/libs/python38.lib’, ‘-DTORCH_BUILD_VERSION=1.7.0a0+ac1f471’, ‘-DUSE_NUMPY=True’, ‘C:\Users\Yann\Desktop\git\pytorch’]’ returned non-zero exit status 1.

Then I tried to set CC and CXX manually to the absolute path of cl.exe, and it failed with the following output:

(pytorch) C:\Users\Yann\Desktop\git\pytorch>python setup.py build --cmake
Building wheel torch-1.7.0a0+ac1f471
– Building version 1.7.0a0+ac1f471
cmake -GNinja -DBUILD_PYTHON=True -DBUILD_TEST=True -DCMAKE_BUILD_TYPE=Release -DCMAKE_GENERATOR=Ninja -DCMAKE_INSTALL_PREFIX=C:\Users\Yann\Desktop\git\pytorch\torch -DCMAKE_PREFIX_PATH=C:\Users\Yann\anaconda3\envs\pytorch\Lib\site-packages -DNUMPY_INCLUDE_DIR=C:\Users\Yann\anaconda3\envs\pytorch\lib\site-packages\numpy\core\include -DPYTHON_EXECUTABLE=C:\Users\Yann\anaconda3\envs\pytorch\python.exe -DPYTHON_INCLUDE_DIR=C:\Users\Yann\anaconda3\envs\pytorch\include -DPYTHON_LIBRARY=C:\Users\Yann\anaconda3\envs\pytorch/libs/python38.lib -DTORCH_BUILD_VERSION=1.7.0a0+ac1f471 -DUSE_NUMPY=True C:\Users\Yann\Desktop\git\pytorch
– The CXX compiler identification is MSVC 19.27.29111.0
– The C compiler identification is MSVC 19.27.29111.0
– Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe
– Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe - broken
CMake Error at C:/Users/Yann/anaconda3/envs/pytorch/Library/share/cmake-3.17/Modules/CMakeTestCXXCompiler.cmake:53 (message):
The C++ compiler

"C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.27.29110/bin/Hostx64/x64/cl.exe"

is not able to compile a simple test program.

It fails with the following output:

Change Dir: C:/Users/Yann/Desktop/git/pytorch/build/CMakeFiles/CMakeTmp

Run Build Command(s):C:/Users/Yann/anaconda3/envs/pytorch/Library/bin/ninja.exe cmTC_0a5a6 && [1/2] Building CXX object CMakeFiles\cmTC_0a5a6.dir\testCXXCompiler.cxx.obj
[2/2] Linking CXX executable cmTC_0a5a6.exe
FAILED: cmTC_0a5a6.exe
cmd.exe /C "cd . && C:\Users\Yann\anaconda3\envs\pytorch\Library\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_0a5a6.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests  -- C:\PROGRA~2\MICROS~3\2019\COMMUN~1\VC\Tools\MSVC\1427~1.291\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmTC_0a5a6.dir\testCXXCompiler.cxx.obj  /out:cmTC_0a5a6.exe /implib:cmTC_0a5a6.lib /pdb:cmTC_0a5a6.pdb /version:0.0  /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
RC Pass 1: command "rc /fo CMakeFiles\cmTC_0a5a6.dir/manifest.res CMakeFiles\cmTC_0a5a6.dir/manifest.rc" failed (exit code 0) with the following output:
Le fichier spécifié est introuvable
ninja: build stopped: subcommand failed.

CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:23 (project)

– Configuring incomplete, errors occurred!
See also “C:/Users/Yann/Desktop/git/pytorch/build/CMakeFiles/CMakeOutput.log”.
See also “C:/Users/Yann/Desktop/git/pytorch/build/CMakeFiles/CMakeError.log”.
Traceback (most recent call last):
File “setup.py”, line 748, in
build_deps()
File “setup.py”, line 327, in build_deps
build_caffe2(version=version,
File “C:\Users\Yann\Desktop\git\pytorch\tools\build_pytorch_libs.py”, line 54, in build_caffe2
cmake.generate(version,
File “C:\Users\Yann\Desktop\git\pytorch\tools\setup_helpers\cmake.py”, line 329, in generate
self.run(args, env=my_env)
File “C:\Users\Yann\Desktop\git\pytorch\tools\setup_helpers\cmake.py”, line 141, in run
check_call(command, cwd=self.build_dir, env=env)
File “C:\Users\Yann\anaconda3\envs\pytorch\lib\subprocess.py”, line 364, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command ‘[‘cmake’, ‘-GNinja’, ‘-DBUILD_PYTHON=True’, ‘-DBUILD_TEST=True’, ‘-DCMAKE_BUILD_TYPE=Release’, ‘-DCMAKE_GENERATOR=Ninja’, ‘-DCMAKE_INSTALL_PREFIX=C:\Users\Yann\Desktop\git\pytorch\torch’, ‘-DCMAKE_PREFIX_PATH=C:\Users\Yann\anaconda3\envs\pytorch\Lib\site-packages’, ‘-DNUMPY_INCLUDE_DIR=C:\Users\Yann\anaconda3\envs\pytorch\lib\site-packages\numpy\core\include’, ‘-DPYTHON_EXECUTABLE=C:\Users\Yann\anaconda3\envs\pytorch\python.exe’, ‘-DPYTHON_INCLUDE_DIR=C:\Users\Yann\anaconda3\envs\pytorch\include’, ‘-DPYTHON_LIBRARY=C:\Users\Yann\anaconda3\envs\pytorch/libs/python38.lib’, ‘-DTORCH_BUILD_VERSION=1.7.0a0+ac1f471’, ‘-DUSE_NUMPY=True’, ‘C:\Users\Yann\Desktop\git\pytorch’]’ returned non-zero exit status 1.

EDIT:

Now I have tried to uninstall everything, install VS2017 with both the 14.11 and the 14.13 toolsets, installed CUDA 10.0 (successfully) and the corresponding cudnn, and have been retrying the script from the author. This time it starts compiling for a while, but at some point ninja fails with this kind of output:

Remarque : inclusion du fichier : …\cmake..\third_party\googletest\googlemock\include\gmock/internal/custom/gmock-generated-actions.h
Remarque : inclusion du fichier : …\cmake..\third_party\googletest\googlemock\include\gmock/gmock-generated-function-mockers.h
Remarque : inclusion du fichier : …\cmake..\third_party\googletest\googlemock\include\gmock/gmock-spec-builders.h
Remarque : inclusion du fichier : …\cmake..\third_party\googletest\googlemock\include\gmock/gmock-matchers.h
Remarque : inclusion du fichier : C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\math.h
Remarque : inclusion du fichier : …\cmake..\third_party\googletest\googlemock\include\gmock/internal/custom/gmock-matchers.h
Remarque : inclusion du fichier : …\cmake..\third_party\googletest\googlemock\include\gmock/gmock-generated-matchers.h
Remarque : inclusion du fichier : …\cmake..\third_party\googletest\googlemock\include\gmock/gmock-generated-nice-strict.h
Remarque : inclusion du fichier : …\cmake..\third_party\googletest\googlemock\include\gmock/gmock-more-actions.h
Remarque : inclusion du fichier : …\cmake..\third_party\googletest\googlemock\include\gmock/gmock-more-matchers.hninja: build stopped: subcommand failed.
Traceback (most recent call last):
File “setup.py”, line 748, in
build_deps()
File “setup.py”, line 327, in build_deps
build_caffe2(version=version,
File “C:\Users\Yann\Desktop\git\pytorch\tools\build_pytorch_libs.py”, line 62, in build_caffe2
cmake.build(my_env)
File “C:\Users\Yann\Desktop\git\pytorch\tools\setup_helpers\cmake.py”, line 345, in build
self.run(build_args, my_env)
File “C:\Users\Yann\Desktop\git\pytorch\tools\setup_helpers\cmake.py”, line 141, in run
check_call(command, cwd=self.build_dir, env=env)
File “C:\Users\Yann\anaconda3\lib\subprocess.py”, line 364, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command ‘[‘cmake’, ‘–build’, ‘.’, ‘–target’, ‘install’, ‘–config’, ‘Release’, ‘–’, ‘-j’, ‘8’]’ returned non-zero exit status 1.

Been trying with both -vcvars_ver=14.11 and -vcvars_ver=14.13, I had the same issue in both cases. I don’t know what else to try, help would be appreciated please.