Building Pytorch from Source on Windows

Hello,

I am having issues building pytorch 2.7.1 from source on windows. My stack is as follows:

  • Visual Studio 2019
  • OneAPI Intel
  • Cuda 11.7 Update 1, with cudnn 8.9.7
  • Python 3.11 (with requirements.txt installed) in Miniconda

When I try to naively run “python setup.py develop” inside of miniconda actiaved to 2019 visual studio command prompt it does not recognize MKL when building and for some reason fails to copy cupti64)2022.21.dll into torch\lib resulting in the following error:

OSError: [WinError 126] The specified module could not be found. Error loading “c:\users\admin\source\pytorch\torch\lib\aoti_custom_ops.dll” or one of its dependencies.

auoti_custom_ops.dll
→ torch_cpu.dll
→ cupti64_2022.2.1.dll (for some reason missing)

“Solution” copy file
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\extras\CUPTI\lib64\cupti64_2022.2.1.dll" → C:\Users\Admin\source\pytorch\torch\lib

There should be a much more controllable and elegant way to build it from source directly, ideally with MKL and arbitrary cuda arches, however I am unable to find a ready made script to do this.

Please help me solve this issue,
Ivan

For some reason this prototype builder succeeds in making the wheel but fails in linking dependencies for aoti_custom_ops.dll, (and crashing on launch) specifically:

OSError: [WinError 126] The specified module could not be found. Error loading “C:\Users\Admin\miniconda3\envs\py311\Lib\site-packages\torch\lib\aoti_custom_ops.dll” or one of its dependencies.
→ mkl_intel_thread.2.dll
→ libiomp5md.dll
→ cupti64_2022.2.1.dll

When I paste these into the C:\Users\Admin\miniconda3\envs\py311\Lib\site-packages\torch\lib\ folder, it works.

@echo off
setlocal EnableDelayedExpansion

REM 0: Clear Anything Stale Intel
REM ===============================
set CC=cl
set CXX=cl
set CMAKE_C_COMPILER=cl
set CMAKE_CXX_COMPILER=cl

set ONEAPI_ROOT=
set ICPP_COMPILER=
set ICX=
set ICC=

REM 1: Initialize Paths
REM ===============================
set “SRC_DIR=C:\Users%USERNAME%\source\pytorch”

REM A: Call builders
call “C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat” x64

REM B: Initialize MKL (properly, somehow)
set USE_MKL=0
set USE_MKLDNN=0

REM C: Initialize CUDA
set “CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7”

set “CUDNN_LIB_DIR=%CUDA_PATH%\lib\x64”
set “CUDNN_INCLUDE_DIR=%CUDA_PATH%\include”
set “CUDA_TOOLKIT_ROOT_DIR=%CUDA_PATH%”
set PATH=%CUDA_PATH%\bin;%CUDA_PATH%\libnvvp;%PATH%
set “TORCH_CUDA_ARCH_LIST=3.5;3.7;5.0;5.2;6.0;6.1;7.0;7.5”

REM 2: Enter Source and Build Wheel
REM ===============================
pushd “%SRC_DIR%” || (
echo ERROR: Failed to enter %SRC_DIR%
exit /b 1
)

python -m build --wheel --no-isolation

REM ===== Cleanup =====
popd
endlocal
exit /b 0

I dont understand:

  1. How come it ignores my mkl flags and puts them anyway
  2. How come it doesnt link the dll’s.
  3. How do I add these DLL’s to the wheel so it links them properly?

I feel like I am missing something very simple here.

My “very ugly” solution to get a working wheel was to then manually open the wheel, throw in the 3 DLL’s into torch/lib folder in the wheel and remake the wheel.

To have a closer look at my “ugly solution” wheel, consult: torch-on-clunkers/README.md at main · theIvanR/torch-on-clunkers · GitHub