Best Practices to Edit and Compile PyTorch Source Code On Windows - pytorch/pytorch GitHub Wiki
Page Maintainers:: @mszhanyi , @ozanMSFT
In this guide, we'll cover some best practices to work with PyTorch C++ Source code on Windows.
Note: In this guide, we'll only cover
libtorch
compilation as an example. As a general base guide; you can also refer the page building libtorch.
Installing CUDA
is not mandatory for compilation, but you may want to install it depending on your needs.
Please take a look on this page for the supported CUDA versions and installation: https://github.com/pytorch/pytorch#from-source
Although MKL
, cuDNN
, oneAPI
, ONNX Runtime
and some other dependencies may not be necessary for this guide's scope, you may want to install them depending on your needs for other compilation scenarios. [Note: MKL installation is recommended]
You need to have Microsoft Visual C++ (MSVC) compiler toolset
on your system.
With Visual Studio Installer
, you need to install Desktop Development with C++
workload.
Option 1: If you already have Visual Studio 2022
or Visual Studio 2019
You can open Visual Studio Installer
by searching from Start Menu and click Modify
for your relevant Visual Studio installment.
Then you can check if Desktop Development with C++
workload is checked.
Option 2: If you're planning to use Visual Studio Code
You can install Build Tools for Visual Studio
from following URL without installing full Visual Studio IDE.
https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2022
After installation, you can open Visual Studio Installer
and install Desktop Development with C++
workload.
Note: Please check installation path, we'll need it when defining PATH.
Option 1: Use Ninja
executable from Desktop Development with C++
workload
If you followed MSVC - Build Tools, Ninja
should be already under build tools installation folder.
If you've Visual Studio 2022
or Visual Studio 2019
: Ninja
should be already under your Visual Studio folder:
{VSPATH}\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.exe
If you've Visual Studio Code
: Ninja
should be already under installation folder which you've specified while installing Desktop Development with C++
workload.
{BUILDTOOLSPATH\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.exe
Option 2: Use Ninja
from GitHub Release
Download Ninja
separately and add it to PATH
.
https://github.com/ninja-build/ninja/releases
You need to install Python
to your system.
https://www.python.org/downloads/windows/
Note: This guide is tested with the latest stable Python version "3.12" (November 2023)
Source code should be fetched as instructed in the main page. Get the PyTorch Source
Install requirements.txt
from root folder.
pip install -r requirements.txt
You can open PyTorch
project with Open a local folder
option.
After the opening source code folder, Visual Studio should automatically detect CMake
type project.
-
Click
Manage Configurations...
to openCMake Settings
window. -
Click
Edit JSON
shortcut to edit parameters ofCMake
. -
Copy and paste following example JSON content into
CMakeSettings.json
file and save it after replacing placeholders accordingly. Especially, please do not forget to replace{{PYTHON_ROOT}}
parameter as suggested below.Note: Please replace
{{PYTHON_ROOT}}
with actual root folder ofPython
. You can find it withcmd
commandwhere python
Note: Settings and parameters may change depending on your needs. Currently, we're focusing for an example
libtorch
build.Note: For this example, following parameters are different than default parameters
installRoot
,cmakeCommandArgs
andvariables
section{ "configurations": [ { "name": "x64-Debug", "generator": "Ninja", "configurationType": "Debug", "inheritEnvironments": [ "msvc_x64_x64" ], "buildRoot": "${projectDir}\\out\\build\\${name}", "installRoot": "${projectDir}\\torch", "cmakeCommandArgs": "-DVS_STARTUP_PROJECT=Torch", "ctestCommandArgs": "", "variables": [ { "name": "BUILD_PYTHON", "value": "False", "type": "BOOL" }, { "name": "BUILD_TEST", "value": "True", "type": "BOOL" }, { "name": "CMAKE_PREFIX_PATH", "value": "{{PYTHON_ROOT}}\\Lib\\site-packages", "type": "PATH" }, { "name": "NUMPY_INCLUDE_DIR", "value": "{{PYTHON_ROOT}}\\Lib\\site-packages\\numpy\\core\\include", "type": "PATH" }, { "name": "PYTHON_EXECUTABLE", "value": "{{PYTHON_ROOT}}\\python.exe", "type": "PATH" }, { "name": "PYTHON_INCLUDE_DIR", "value": "{{PYTHON_ROOT}}\\Include", "type": "PATH" }, { "name": "USE_NUMPY", "value": "True", "type": "BOOL" } ] } ] }
-
After saving the file,
CMake
output should automatically start to prepare configuration, wait for a while and you should seeCMake generation finished
message on the bottom.Note: If you're seeing an error, please check parameters before continue.
-
Now, navigate
Build -> Build All
menu to start build. Build result will be underout\build\x64-Debug
folder.Note: First time build might take some time depending on build machine's specifications.
-
As an example change; let's open
test\cpp\api\nn_utils.cpp
file and search forTEST_F(NNUtilsTest, ConvertParameters)
test. Let's add aprintf
in this test. -
Build project again by navigating
Build -> Build All
.Note: Following builds after the first time build should take only a few minutes.
-
Let's setup
Debug Target
astest_api.exe (bin\test_api.exe)
for our project's scope.Note: You can hide all unnecessary targets for clean view.
-
Let's also modify
Debug and Launch Settings for test_api
to specifiy additional parameters for debugging, this will openlaunch.vs.json
file. -
For simplicity, let's add this filter as an argument to run only our modified test and save
launch.vs.json
file."args": [ "--gtest_filter=NNUtilsTest.ConvertParameters" ]
-
Run project and see the result.
-
Open
PyTorch
folder withVisual Studio Code
. -
Install CMake Tools extension by Microsoft.
-
Navigate
.vscode
folder from root and createsettings.json
file. Copy following JSON content and save it after replacing placeholders accordingly. Especially, please do not forget to replace{{PYTHON_ROOT}}
parameter as suggested below.Note: Please replace
{{PYTHON_ROOT}}
with actual root folder ofPython
. You can find it withcmd
commandwhere python
Note: Settings and parameters may change depending on your needs. Currently, we're focusing for an example
libtorch
build.{ "cmake.generator": "Ninja", "cmake.configureSettings":{ "BUILD_PYTHON":"False", "BUILD_TEST":"True", "CMAKE_PREFIX_PATH":"{{PYTHON_ROOT}}\\Lib\\site-packages", "NUMPY_INCLUDE_DIR":"{{PYTHON_ROOT}}\\Lib\\site-packages\\numpy\\core\\include", "PYTHON_EXECUTABLE":"{{PYTHON_ROOT}}\\python.exe", "PYTHON_INCLUDE_DIR":"{{PYTHON_ROOT}}\\Include", "USE_NUMPY":"True" } }
-
In the
Status Bar
at bottom, selectDebug
as build variant and desiredtoolkit
based on MSVC - Build Tools setup. -
Open
Command Palette
withCtrl + Shift + P
and search forCMake: Configure
and run it. Please wait until CMake configure is done successfully by following up output at the bottom. -
After the
configure
step is done; ClickBuild
button in theStatus Bar
. Build result will be underbuild
folder.Note: First time build might take some time depending on build machine's specifications.
-
As an example change; let's open
test\cpp\api\nn_utils.cpp
file. Then, search forTEST_F(NNUtilsTest, ConvertParameters)
test. Let's add aprintf
in this test.Note: You can use
Ctrl + P
and typenn_utils.cpp
as a shortcut. -
Build project again by clicking
Build
in theStatus Bar
.Note: Following builds after the first time build should take only a few minutes
-
Open
settings.json
to configureDebug Config
. For this example, let's add--gtest_filter=NNUtilsTest.ConvertParameters
filter to thecmake.debugConfig
."cmake.debugConfig": { "args": ["--gtest_filter=NNUtilsTest.ConvertParameters"] }
-
Click
Launch the selected target
button in theStatus Bar
and searchtest_api
to selecttest_api.exe
. -
Previous step will automatically trigger run, you see result at
Debug Console
.Note: You can run again
Ctrl + F5
shortcut or withLaunch the selected target
button any time.
You might find some changes you made do not work and after recompiling, the changes dismissed. That is because some PyTorch C++ code is generated during compilation. For more information on code generation, you could check out Codegen and Structured Kernels