PyTorch build from source on Windows

I’m trying to build PyTorch from source on Windows 10 (as described in pytorch repo), and I’m getting an error:

Building wheel torch-1.1.0a0+542c273
-- Building version 1.1.0a0+542c273
Microsoft (R) Build Engine 15.9.21+g9802d43bc3 dla platformy .NET Framework
Copyright (C) Microsoft Corporation. Wszelkie prawa zastrzeżone.

MSBUILD : error MSB1009: Project file does not exist.
Switch: INSTALL.vcxproj
Traceback (most recent call last):
  File "setup.py", line 710, in <module>
    build_deps()
  File "setup.py", line 282, in build_deps
    build_dir='build')
  File "C:\Users\MarcinM\pytorch\tools\build_pytorch_libs.py", line 250, in build_caffe2
    cwd=build_dir, env=my_env)
  File "C:\Users\MarcinM\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['msbuild', 'INSTALL.vcxproj', '/p:Configuration=Release']' returned non-zero exit status 1.

What I’ve done:

  1. git cloned the pytorch repo
  2. Installed VS 2017 15.9.9 Community with checking:
    • Windows 10 SDK (10.0;16299.0) for Desktop C++ [x86 i x64]
    • Version 14.11 of toolset for version 15.4 of VC++ 2017
  3. run git submodule update --init --recursive
  4. run pip install numpy pyyaml mkl mkl-include setuptools cmake cffi typing
  5. run:
set "CUDAHOSTCXX=%VS140COMNTOOLS%..\..\VC\bin\amd64\cl.exe"
set CMAKE_GENERATOR=Visual Studio 15 2017 Win64
set DISTUTILS_USE_SDK=1
(edit:) for /f "usebackq tokens=*" %i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -version [15^,16^) -products * -latest -property installationPath`) do call "%i\VC\Auxiliary\Build\vcvarsall.bat" x64 -vcvars_ver=14.11
 
python setup.py install

Is it possible that the order of commands got me this error (I’ve run 4. after installing VS toolsets)?
Can you help me overvcome this error?

Windows 10
python 36
cuda 9.1

You are missing one line in the steps in the official readme.md.

for /f "usebackq tokens=*" %i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -version [15^,16^) -products * -latest -property installationPath`) do call "%i\VC\Auxiliary\Build\vcvarsall.bat" x64 -vcvars_ver=14.11

Peter, thanks for reply. My, bad, I forgot to include that in post, but I’ve also run it. Today I’ve made through everything in readme once again, but with no luck - the msbuild error is still there. Any other ideas?

Use where msbuild to locate whether it is in PATH. If it is not, then please add C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin into PATH. (P.S. Enterprise here can be replaced by Community or Professional)

Yes, where msbuild gives me exactly

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe

btw I’m using pip, not conda for pip install numpy pyyaml mkl mkl-include setuptools cmake cffi typing ( I know that in readme the is note that conda is strongly recommended, but somehow I’m unable to set up conda on my machine). Maybe the problem is here?

No, conda is not the problem here. Would you please do python setup.py build and send the full log here. There must be some problem during the configuration of CMake.

Yes, I’ve run it and the full log is exactly like the one I provided in the question.

I don’t think so. This one is apparently not the root cause. Would you please do python setup.py build --cmake?

Yes! That did the work, after

python setup.py build --cmake
python setup.py install

pytorch was finally installed.
However

python
>>> import torch
>>> torch.cuda.is_available()

returns False. When I run torch.cuda.current_device() i get:
AssertionError: Torch not compiled with CUDA enabled

Can this be Nsight Compute issue?

Yes, may be. Please ensure CUDA and the Visual Studio extension are installed. If the env var %NVTOOLSEXT_PATH% exists, then it should be fine. Otherwise, please reinstall CUDA and remember to tick VS extension.

I reinstalled CUDA to 10.1, ensuring that both Nsight Compute and VS extensions are checked. Now when I try to build pytorch witch python setup.py build --cmake I get this error (note that some of the phrases were in polish, so I’ve translated them):

"C:\Users\MarcinM\pytorch\build\INSTALL.vcxproj" (default target) (1)->
"C:\Users\MarcinM\pytorch\build\ALL_BUILD.vcxproj" (default target) (3)->
"C:\Users\MarcinM\pytorch\build\caffe2\AlgorithmsTest.vcxproj" (default target) (4)->
"C:\Users\MarcinM\pytorch\build\caffe2\caffe2_gpu.vcxproj" (default target) (21)->
(CustomBuild target) ->
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets(20
9,5): error MSB6006: "cmd.exe" exited with code 1. [C:\Users\MarcinM\pytorch\build\caffe2\caffe2_gpu.vcxp
roj]

    Warnings: 3661
    Errors: 1

Czas, który upłynął: 00:32:23.65
Traceback (most recent call last):
  File "setup.py", line 710, in <module>
    build_deps()
  File "setup.py", line 282, in build_deps
    build_dir='build')
  File "C:\Users\MarcinM\pytorch\tools\build_pytorch_libs.py", line 264, in build_caffe2
    cwd=build_dir, env=my_env)
  File "C:\Users\MarcinM\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['msbuild', 'INSTALL.vcxproj', '/p:Configuration=Release']' returned non-zero exit status 1.

I’ve also made clean build, but with no success. Any other thoughts?

Would you please run the building process inside VS (only caffe2_gpu is enough and make sure you choose Release and x64) ? Alternatively, you can use Ninja, which is quite simple to use. And then you can see the internal error.

pip install ninja
set CMAKE_GENERATOR=Ninja

Thanks, I really appreciate your commitment for this! So I’ve run this by Ninja, and it exited pretty fast. The error log is quite long:

-- The CXX compiler identification is GNU 7.2.0
-- The C compiler identification is GNU 7.2.0
-- Check for working CXX compiler: C:/Program Files/Haskell Platform/8.4.3/mingw/bin/c++.exe
-- Check for working CXX compiler: C:/Program Files/Haskell Platform/8.4.3/mingw/bin/c++.exe -- broken
CMake Error at C:/Users/MarcinM/AppData/Local/Programs/Python/Python36/Lib/site-packages/cmake/data/share/cmake-3.13/Modules/CMakeTestCXXCompiler.cmake:45 (message):
  The C++ compiler

    "C:/Program Files/Haskell Platform/8.4.3/mingw/bin/c++.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/Users/MarcinM/pytorch/build/CMakeFiles/CMakeTmp

    Run Build Command:"C:/Users/MarcinM/AppData/Local/Programs/Python/Python36/Scripts/ninja.exe" "cmTC_42959"
    [1/2] Building CXX object CMakeFiles/cmTC_42959.dir/testCXXCompiler.cxx.obj
    FAILED: CMakeFiles/cmTC_42959.dir/testCXXCompiler.cxx.obj
    C:\PROGRA~1\HASKEL~1\847A5C~1.3\mingw\bin\C__~1.EXE    /EHa -o CMakeFiles/cmTC_42959.dir/testCXXCompiler.cxx.obj -c testCXXCompiler.cxx
    C__~1.EXE: error: /EHa: No such file or directory
    ninja: build stopped: subcommand failed.




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


-- Configuring incomplete, errors occurred!
See also "C:/Users/MarcinM/pytorch/build/CMakeFiles/CMakeOutput.log".
See also "C:/Users/MarcinM/pytorch/build/CMakeFiles/CMakeError.log".
Traceback (most recent call last):
  File "setup.py", line 710, in <module>
    build_deps()
  File "setup.py", line 282, in build_deps
    build_dir='build')
  File "C:\Users\MarcinM\pytorch\tools\build_pytorch_libs.py", line 253, in build_caffe2
    my_env)
  File "C:\Users\MarcinM\pytorch\tools\build_pytorch_libs.py", line 233, in run_cmake
    check_call(cmake_args, cwd=build_dir, env=my_env)
  File "C:\Users\MarcinM\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '-GNinja', '-DBUILDING_WITH_TORCH_LIBS=ON', '-DBUILD_BINARY=False', '-DBUILD_CAFFE2_OPS=True', '-DBUILD_PYTHON=True', '-DBUILD_SHARED_LIBS=ON', '-DBUILD_TEST=True', '-DBUILD_TORCH=ON', '-DCAFFE2_STATIC_LINK_CUDA=False', '-DCMAKE_BUILD_TYPE=Release', '-DCMAKE_CXX_FLAGS=  /EHa', '-DCMAKE_C_FLAGS=  /EHa', '-DCMAKE_EXE_LINKER_FLAGS=', '-DCMAKE_INSTALL_PREFIX=C:\\Users\\MarcinM\\pytorch/torch', '-DCMAKE_PREFIX_PATH=C:\\Users\\MarcinM\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages', '-DCMAKE_SHARED_LINKER_FLAGS=', '-DINSTALL_TEST=True', '-DNCCL_EXTERNAL=False', '-DNUMPY_INCLUDE_DIR=C:/Users/MarcinM/AppData/Local/Programs/Python/Python36/lib/site-packages/numpy/core/include', '-DONNX_ML=False', '-DONNX_NAMESPACE=onnx_torch', '-DPYTHON_EXECUTABLE=C:/Users/MarcinM/AppData/Local/Programs/Python/Python36/python.exe', '-DPYTHON_INCLUDE_DIR=C:/Users/MarcinM/AppData/Local/Programs/Python/Python36/include', '-DPYTHON_LIBRARY=C:/Users/MarcinM/AppData/Local/Programs/Python/Python36/libs/python36.lib', '-DTHD_SO_VERSION=1', '-DTORCH_BUILD_VERSION=1.1.0a0+e5cdd94', '-DUSE_CUDA=True', '-DUSE_DISTRIBUTED=False', '-DUSE_FBGEMM=True', '-DUSE_FFMPEG=False', '-DUSE_LEVELDB=False', '-DUSE_LMDB=False', '-DUSE_MKLDNN=True', '-DUSE_NCCL=False', '-DUSE_NNPACK=True', '-DUSE_NUMPY=True', '-DUSE_OPENCV=False', '-DUSE_QNNPACK=True', '-DUSE_ROCM=False', '-DUSE_SYSTEM_EIGEN_INSTALL=OFF', '-DUSE_SYSTEM_NCCL=False', '-DUSE_TENSORRT=False', '-DMKLDNN_ENABLE_CONCURRENT_EXEC=ON', 'C:\\Users\\MarcinM\\pytorch']' returned non-zero exit status 1.

The folder C:/Users/MarcinM/pytorch/build/CMakeFiles/CMakeTmp is empty if this is any help. Also the c++.exe file is present in C:\Program Files\Haskell Platform\8.4.3\mingw\bin.

Oh, I’m missing these variables.

set CC=cl.exe
set CXX=cl.exe

Ok, so there is a lot of similar errors like this (I’ve also changed polish phrases):

[1624/2499] Building NVCC (Device) object caffe2/CMakeFile...en/src/ATen/native/cuda/caffe2_gpu_generated_Reduce.cu.obj
FAILED: caffe2/CMakeFiles/caffe2_gpu.dir/__/aten/src/ATen/native/cuda/caffe2_gpu_generated_Reduce.cu.obj
cmd.exe /C "cd /D C:\Users\MarcinM\pytorch\build\caffe2\CMakeFiles\caffe2_gpu.dir\__\aten\src\ATen\native\cuda && C:\Users\MarcinM\AppData\Local\Programs\Python\Python36\Lib\site-packages\cmake\data\bin\cmake.exe -E make_directory C:/Users/MarcinM/pytorch/build/caffe2/CMakeFiles/caffe2_gpu.dir/__/aten/src/ATen/native/cuda/. && C:\Users\MarcinM\AppData\Local\Programs\Python\Python36\Lib\site-packages\cmake\data\bin\cmake.exe -D verbose:BOOL=OFF -D build_configuration:STRING=Release -D generated_file:STRING=C:/Users/MarcinM/pytorch/build/caffe2/CMakeFiles/caffe2_gpu.dir/__/aten/src/ATen/native/cuda/./caffe2_gpu_generated_Reduce.cu.obj -D generated_cubin_file:STRING=C:/Users/MarcinM/pytorch/build/caffe2/CMakeFiles/caffe2_gpu.dir/__/aten/src/ATen/native/cuda/./caffe2_gpu_generated_Reduce.cu.obj.cubin.txt -P C:/Users/MarcinM/pytorch/build/caffe2/CMakeFiles/caffe2_gpu.dir/__/aten/src/ATen/native/cuda/caffe2_gpu_generated_Reduce.cu.obj.Release.cmake"
Failed to run %VS140COMNTOOLS%../../VC/bin/amd64/cl.exe (No such file).
CMake Error at caffe2_gpu_generated_Reduce.cu.obj.Release.cmake:219 (message):
  Error generating
  C:/Users/MarcinM/pytorch/build/caffe2/CMakeFiles/caffe2_gpu.dir/__/aten/src/ATen/native/cuda/./caffe2_gpu_generated_Reduce.cu.obj

and one last error is:

[1625/2499] Linking CXX shared module caffe2\python\caffe2_pybind11_state.cp36-win_amd64.pyd
   Creating library lib\caffe2_pybind11_state.lib and object lib\caffe2_pybind11_state.exp
pybind_state.cc.obj : warning LNK4217: locally defined symbol ?PybindAdditionRegistry@python@caffe2@@YAPEAV?$Registry@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$unique_ptr@UPybindAddition@python@caffe2@@U?$default_delete@UPybindAddition@python@caffe2@@@std@@@2@AEAVmodule@pybind11@@@c10@@XZ (class c10::Registry<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::unique_ptr<struct caffe2::python::PybindAddition,struct std::default_delete<struct caffe2::python::PybindAddition> >,class pybind11::module &> * __cdecl caffe2::python::PybindAdditionRegistry(void)) was imported in function "void __cdecl caffe2::python::pybind11_init_caffe2_pybind11_state(class pybind11::module &)" (?pybind11_init_caffe2_pybind11_state@python@caffe2@@YAXAEAVmodule@pybind11@@@Z)
pybind_state_nomni.cc.obj : warning LNK4049: locally defined symbol ?PybindAdditionRegistry@python@caffe2@@YAPEAV?$Registry@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$unique_ptr@UPybindAddition@python@caffe2@@U?$default_delete@UPybindAddition@python@caffe2@@@std@@@2@AEAVmodule@pybind11@@@c10@@XZ (class c10::Registry<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::unique_ptr<struct caffe2::python::PybindAddition,struct std::default_delete<struct caffe2::python::PybindAddition> >,class pybind11::module &> * __cdecl caffe2::python::PybindAdditionRegistry(void)) was imported
pybind_state_int8.cc.obj : warning LNK4217: locally defined symbol ?BlobFetcherRegistry@python@caffe2@@YAPEAV?$Registry@VTypeIdentifier@caffe2@@V?$unique_ptr@VBlobFetcherBase@python@caffe2@@U?$default_delete@VBlobFetcherBase@python@caffe2@@@std@@@std@@$$V@c10@@XZ (class c10::Registry<class caffe2::TypeIdentifier,class std::unique_ptr<class caffe2::python::BlobFetcherBase,struct std::default_delete<class caffe2::python::BlobFetcherBase> > > * __cdecl caffe2::python::BlobFetcherRegistry(void)) zostal zaimportowany w funkcji "void __cdecl caffe2::python::`dynamic initializer for 'g_BlobFetcherRegistry0''(void)" (??__Eg_BlobFetcherRegistry0@python@caffe2@@YAXXZ)
ninja: build stopped: subcommand failed.
Traceback (most recent call last):
  File "setup.py", line 710, in <module>
    build_deps()
  File "setup.py", line 282, in build_deps
    build_dir='build')
  File "C:\Users\MarcinM\pytorch\tools\build_pytorch_libs.py", line 261, in build_caffe2
    cwd=build_dir, env=my_env)
  File "C:\Users\MarcinM\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '--build', '.', '--target', 'install', '--config', 'Release', '--', '-j', '11']' returned non-zero exit status 1.

About Failed to run %VS140COMNTOOLS%../../VC/bin/amd64/cl.exe (No such file)., indeed there is no such path as C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\bin.
Maybe the path should be C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX64\x64\cl.exe as implied in caffe2 old repo discussion?

Edit: I’ve changed the path to but with no luck:

Failed to run %VS140COMNTOOLS%../../VC/Tools/MSVC/14.11.25503/bin/HostX64/x64/cl.exe (No such file).

Which is quite weird because the file cl.exe is definitely there.

please try
set CUDAHOSTCXX=

1 Like

Yes! That worked! Everything went well with it. Pytorch was installed, and is working with cuda. Thank you so much peterjc123!

For future readers, to sum up:

  1. I reinstalled drivers for CUDA, reinstalled CUDA with VS extensions and Nsight Compute (actually with everything checked), reinstaled CUDnn
  2. went with
pip install ninja
set CMAKE_GENERATOR=Ninja
set CC=cl.exe
set CXX=cl.exe
set CUDAHOSTCXX=
set DISTUTILS_USE_SDK=1
for /f "usebackq tokens=*" %i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -version [15^,16^) -products * -latest -property installationPath`) do call "%i\VC\Auxiliary\Build\vcvarsall.bat" x64 -vcvars_ver=14.11
python setup.py build --cmake
python setup.py install
2 Likes