- Notifications
You must be signed in to change notification settings - Fork26.3k
Build PyTorch and LibTorch on Windows ARM64
Build Tools for Visual Studio 2022OR any version ofVisual Studio 2022
UnderWorkloads, selectDesktop development with C++
UnderIndividual Components, selectARM64/ARM64EC build tools (latest)
Download and installArm Performance Libraries for Windows
%ARMPL_DIR%environment variable should be automatically added to yourPATHby default installation settings.
InstallRust
Buildinglibuv (v1.39.0) from source (we'll use the folder path inBuild)
- Open
ARM64 Native Tools Command Prompt for VS2022from start menu - Change path to the suitable folder
- Run following command to build
libuv
git clone https://github.com/libuv/libuv.git -b v1.39.0cd libuvecho Clean folder...git clean -fxdecho Configuring libuv...mkdir libuv\buildcd libuv\buildcmake .. -DBUILD_TESTING=OFFecho Building libuv...cmake --build . --config Releaseecho Installing libuv...cmake --install . --prefix ../installpause
- Open
(OPTIONAL) You may installsccache for speeding up future builds by cache support.
- You need to add
sccacheto yourPATHmanually.
- You need to add
Clone the PyTorch repository (or your fork)
git clone --recursive https://github.com/pytorch/pytorchcd pytorchInit and sync submodules
git submodule syncgit submodule update --init --recursive
NOTE: Since currently
condadoesn't support Windows ARM64, instead we're recommending to use built-in Pythonvenvfunctionality for similar isolating experience. (more info)
Creating a virtual environment (
venv)python -m pip install --upgrade pippython -m venv .venvecho *> .venv\.gitignorecall .\.venv\Scripts\activate
Install requirements
pip install -r requirements.txt
Activate
Arm64 Native Tools Command Prompt for VS 2022environment by calling one of the following commands based on what you've installed.For Build Tools:
"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" arm64
For Visual Studio 2022 Community:
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" arm64
For Visual Studio 2022 Enterprise:
"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" arm64
For Visual Studio 2022 Preview:
"C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Auxiliary\Build\vcvarsarm64.bat" arm64
Set the following environment variables:
setBLAS=APLsetUSE_LAPACK=1setMSSdk=1setDISTUTILS_USE_SDK=1:: please change {YOUR_LIBUV_FOLDER} to the your libuv foldersetlibuv_ROOT={YOUR_LIBUV_FOLDER}\install
Copy
libuvdll to thelibfolder to include it for packaging (please check:Prerequisites)copy%libuv_ROOT%\lib\Release\uv.dll torch\lib\uv.dll
(OPTIONAL) If you want to use
sccachefor speeding up your future build by cache also set following.setCMAKE_C_COMPILER_LAUNCHER=sccachesetCMAKE_CXX_COMPILER_LAUNCHER=sccache
You can generate a.whl (wheel) file forPyTorch (the Python package)OR a.zip file forLibTorch (the C++ core). If you'd like to use PyTorch right away without creating a wheel file, you can build it directly in your environment. Here are your options:
OPTION 1 - Generating and Installing Wheel File (for distribution or installation on other systems):
Call following command to start build and generate output
:: set PYTORCH_BUILD_VERSION=2.6.0 ## OPTIONAL: If you wish to give a specific versioning for `.whl` file :: set PYTORCH_BUILD_NUMBER=1 ## (for instance you can specify 2.6.0 as shown) python setup.py bdist_wheelOutput file will be under
distfolder with.whlextension.Format will look like
torch-FOO+gitBAR-cp312-cp312-win_arm64.whlOR if you specify version, format will be liketorch-2.6.0-cp312-cp312-win_arm64.whlNow you can copy and install this
.whlfile anywhere in your system by callingpip install torch-YOURVERSION-cp312-cp312-win_arm64.whlOPTION 2 - Directly Installing to Local Environment (for development on your system only)
Run one of the following commands to initiate the build:
- Install the package in development mode, ideal when making frequent changes to codebase
python setup.py develop- Install the package for regular use, rather than ongoing development
python setup.py install
Additional preparation for
libtorchfolder:: Options: Release, Debug or RelWithDebInfosetCMAKE_BUILD_TYPE=Release:: Prepare the environmentmkdir libtorchmkdir libtorch\binmkdir libtorch\cmakemkdir libtorch\includemkdir libtorch\libmkdir libtorch\sharemkdir libtorch\test
Call following command to start build and generate output
python ./tools/build_libtorch.py
Prepare
.zipfile fromlibtorchoutput:: Moving the files to the correct locationmove /Y torch\bin\*.* libtorch\bin\move /Y torch\cmake\*.* libtorch\cmake\robocopy /move /e torch\include\ libtorch\include\move /Y torch\lib\*.* libtorch\lib\robocopy /move /e torch\share\ libtorch\share\move /Y torch\test\*.* libtorch\test\move /Y libtorch\bin\*.dll libtorch\lib\:: Create output under distmkdir disttar -cvaf dist/libtorch-win.zip -C libtorch *:: Cleanup raw data to save spacermdir /s /q libtorch
Now you can copy and extract
dist/libtorch-win.zipfile anywhere in your systemFor C++ example, you can visitInstalling C++ Distributions of PyTorch
You can just add following line to your
CMakeLists.txtfile.set(CMAKE_PREFIX_PATH"C:/YOUR_CORRESPONDING_PATH/libtorch-win")
I would love to contribute to PyTorch!