Build PyTorch and LibTorch on Windows ARM64 - pytorch/pytorch GitHub Wiki
Building for Windows on ARM64
Prerequisites
-
Build Tools for Visual Studio 2022 OR any version of Visual Studio 2022
-
Under Workloads, select Desktop development with C++
-
Under Individual Components, select ARM64/ARM64EC build tools (latest)
-
-
Download and install Arm Performance Libraries for Windows
%ARMPL_DIR%
environment variable should be automatically added to yourPATH
by default installation settings.
-
Install Rust
-
Building libuv (v1.39.0) from source (we'll use the folder path in Build)
- Open
ARM64 Native Tools Command Prompt for VS2022
from 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.0 cd libuv echo Clean folder... git clean -fxd echo Configuring libuv... mkdir libuv\build cd libuv\build cmake .. -DBUILD_TESTING=OFF echo Building libuv... cmake --build . --config Release echo Installing libuv... cmake --install . --prefix ../install pause
- Open
-
(OPTIONAL) You may install sccache for speeding up future builds by cache support.
- You need to add
sccache
to yourPATH
manually.
- You need to add
Cloning PyTorch
-
Clone the PyTorch repository (or your fork)
git clone --recursive https://github.com/pytorch/pytorch cd pytorch
-
Init and sync submodules
git submodule sync git submodule update --init --recursive
Preparing Environment
NOTE: Since currently
conda
doesn't support Windows ARM64, instead we're recommending to use built-in Pythonvenv
functionality for similar isolating experience. (more info)
-
Creating a virtual environment (
venv
)python -m pip install --upgrade pip python -m venv .venv echo * > .venv\.gitignore call .\.venv\Scripts\activate
-
Install requirements
pip install -r requirements.txt
-
Activate
Arm64 Native Tools Command Prompt for VS 2022
environment 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
-
Setting Environment Variables
-
Set the following environment variables:
set BLAS=APL set USE_LAPACK=1 set MSSdk=1 set DISTUTILS_USE_SDK=1 :: please change {YOUR_LIBUV_FOLDER} to the your libuv folder set libuv_ROOT={YOUR_LIBUV_FOLDER}\install
-
Copy
libuv
dll to thelib
folder 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
sccache
for speeding up your future build by cache also set following.set CMAKE_C_COMPILER_LAUNCHER=sccache set CMAKE_CXX_COMPILER_LAUNCHER=sccache
Build
You can generate a .whl
(wheel) file for PyTorch (the Python package) OR a .zip
file for LibTorch (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:
PyTorch
-
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_wheel
Output file will be under
dist
folder with.whl
extension.Format will look like
torch-FOO+gitBAR-cp312-cp312-win_arm64.whl
OR if you specify version, format will be liketorch-2.6.0-cp312-cp312-win_arm64.whl
Now you can copy and install this
.whl
file anywhere in your system by callingpip install torch-YOURVERSION-cp312-cp312-win_arm64.whl
-
OPTION 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
LibTorch
-
Additional preparation for
libtorch
folder:: Options: Release, Debug or RelWithDebInfo set CMAKE_BUILD_TYPE=Release :: Prepare the environment mkdir libtorch mkdir libtorch\bin mkdir libtorch\cmake mkdir libtorch\include mkdir libtorch\lib mkdir libtorch\share mkdir libtorch\test
-
Call following command to start build and generate output
python ./tools/build_libtorch.py
-
Prepare
.zip
file fromlibtorch
output:: Moving the files to the correct location move /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 dist mkdir dist tar -cvaf dist/libtorch-win.zip -C libtorch * :: Cleanup raw data to save space rmdir /s /q libtorch
-
Now you can copy and extract
dist/libtorch-win.zip
file anywhere in your system-
For C++ example, you can visit Installing C++ Distributions of PyTorch
-
You can just add following line to your
CMakeLists.txt
file.set(CMAKE_PREFIX_PATH "C:/YOUR_CORRESPONDING_PATH/libtorch-win")
-