From 5a6a0045820a4d8b804c7994d5738fb3d2e5c32b Mon Sep 17 00:00:00 2001 From: chuanqiw Date: Sun, 25 Aug 2024 09:40:52 -0700 Subject: [PATCH 1/2] Enable torch xpu build on Windows --- windows/build_pytorch.bat | 12 ++-- windows/internal/build_wheels.bat | 3 + windows/internal/cuda_install.bat | 4 ++ windows/internal/smoke_test.bat | 9 ++- windows/internal/vc_install_helper.bat | 5 +- windows/internal/xpu_install.bat | 97 ++++++++++++++++++++++++++ windows/xpu.bat | 34 +++++++++ 7 files changed, 152 insertions(+), 12 deletions(-) create mode 100644 windows/internal/xpu_install.bat create mode 100755 windows/xpu.bat diff --git a/windows/build_pytorch.bat b/windows/build_pytorch.bat index cd231d9a6..1b43df8bb 100644 --- a/windows/build_pytorch.bat +++ b/windows/build_pytorch.bat @@ -25,11 +25,9 @@ set PYTORCH_BUILD_NUMBER=%~3 :env_end -if not "%CUDA_VERSION%" == "cpu" ( - set CUDA_PREFIX=cuda%CUDA_VERSION% -) else ( - set CUDA_PREFIX=cpu -) +set CUDA_PREFIX=cuda%CUDA_VERSION% +if "%CUDA_VERSION%" == "cpu" set CUDA_PREFIX=cpu +if "%CUDA_VERSION%" == "xpu" set CUDA_PREFIX=xpu if "%DESIRED_PYTHON%" == "" set DESIRED_PYTHON=3.5;3.6;3.7 set DESIRED_PYTHON_PREFIX=%DESIRED_PYTHON:.=% @@ -72,7 +70,7 @@ if "%DEBUG%" == "1" ( set BUILD_TYPE=release ) -if not "%CUDA_VERSION%" == "cpu" ( +if not "%CUDA_VERSION%" == "cpu" if not "%CUDA_VERSION%" == "xpu" ( rmdir /s /q magma_%CUDA_PREFIX%_%BUILD_TYPE% del magma_%CUDA_PREFIX%_%BUILD_TYPE%.7z curl -k https://s3.amazonaws.com/ossci-windows/magma_%MAGMA_VERSION%_%CUDA_PREFIX%_%BUILD_TYPE%.7z -o magma_%CUDA_PREFIX%_%BUILD_TYPE%.7z @@ -120,7 +118,7 @@ for %%v in (%DESIRED_PYTHON_PREFIX%) do ( pip install ninja @setlocal :: Set Flags - if not "%CUDA_VERSION%"=="cpu" ( + if not "%CUDA_VERSION%"=="cpu" if not "%CUDA_VERSION%" == "xpu" ( set MAGMA_HOME=%cd%\magma_%CUDA_PREFIX%_%BUILD_TYPE% ) call %CUDA_PREFIX%.bat diff --git a/windows/internal/build_wheels.bat b/windows/internal/build_wheels.bat index fcca86a5f..c06c6f711 100644 --- a/windows/internal/build_wheels.bat +++ b/windows/internal/build_wheels.bat @@ -4,5 +4,8 @@ if errorlevel 1 exit /b 1 call windows/internal/cuda_install.bat if errorlevel 1 exit /b 1 +call windows/internal/xpu_install.bat +if errorlevel 1 exit /b 1 + call windows/build_pytorch.bat %CUDA_VERSION% %PYTORCH_BUILD_VERSION% %PYTORCH_BUILD_NUMBER% if errorlevel 1 exit /b 1 diff --git a/windows/internal/cuda_install.bat b/windows/internal/cuda_install.bat index d94c2c484..8d1d8f4ef 100644 --- a/windows/internal/cuda_install.bat +++ b/windows/internal/cuda_install.bat @@ -4,6 +4,10 @@ if "%CUDA_VERSION%" == "cpu" ( echo Skipping for CPU builds exit /b 0 ) +if "%CUDA_VERSION%" == "xpu" ( + echo Skipping for XPU builds + exit /b 0 +) set SRC_DIR=%~dp0\.. diff --git a/windows/internal/smoke_test.bat b/windows/internal/smoke_test.bat index c1ffb157b..41dc311db 100644 --- a/windows/internal/smoke_test.bat +++ b/windows/internal/smoke_test.bat @@ -2,9 +2,16 @@ set SRC_DIR=%~dp0 pushd %SRC_DIR%\.. -if not "%CUDA_VERSION%" == "cpu" call internal\driver_update.bat +if not "%CUDA_VERSION%" == "cpu" if not "%CUDA_VERSION%" == "xpu" call internal\driver_update.bat if errorlevel 1 exit /b 1 +if "%CUDA_VERSION%" == "xpu" ( + call internal\xpu_install.bat + if errorlevel 1 exit /b 1 + call "%ProgramFiles(x86)%\Intel\oneAPI\setvars.bat" + if errorlevel 1 exit /b 1 +) + set "ORIG_PATH=%PATH%" setlocal EnableDelayedExpansion diff --git a/windows/internal/vc_install_helper.bat b/windows/internal/vc_install_helper.bat index b8d705f93..61ab6d5f8 100644 --- a/windows/internal/vc_install_helper.bat +++ b/windows/internal/vc_install_helper.bat @@ -1,8 +1,5 @@ if "%VC_YEAR%" == "2019" powershell windows/internal/vs2019_install.ps1 -if "%VC_YEAR%" == "2022" ( - echo "Upgrading MSVC to 2022 is not supported in the CI for reliability reason. Please contact PyTorch Dev Infra for more information" - set VC_YEAR=2019 -) +if "%VC_YEAR%" == "2022" powershell windows/internal/vs2022_install.ps1 set VC_VERSION_LOWER=17 set VC_VERSION_UPPER=18 diff --git a/windows/internal/xpu_install.bat b/windows/internal/xpu_install.bat new file mode 100644 index 000000000..bd9efa41d --- /dev/null +++ b/windows/internal/xpu_install.bat @@ -0,0 +1,97 @@ + +@echo on +REM Description: Install Intel Support Packages on Windows +REM BKM reference: https://www.intel.com/content/www/us/en/developer/articles/tool/pytorch-prerequisites-for-intel-gpu/2-5.html + +if not "%CUDA_VERSION%" == "xpu" ( + echo Skipping for non XPU builds + exit /b 0 +) + +set XPU_INSTALL_MODE=%~1 +if "%XPU_INSTALL_MODE%"=="" goto xpu_bundle_install_start +if "%XPU_INSTALL_MODE%"=="bundle" goto xpu_bundle_install_start +if "%XPU_INSTALL_MODE%"=="driver" goto xpu_driver_install_start +if "%XPU_INSTALL_MODE%"=="all" goto xpu_driver_install_start + +:arg_error + +echo Illegal XPU installation mode. The value can be "bundle"/"driver"/"all" +echo If keep the value as space, will use default "bundle" mode +exit /b 1 + +:xpu_driver_install_start +:: TODO Need more testing for driver installation +set XPU_DRIVER_LINK=https://downloadmirror.intel.com/830975/gfx_win_101.5972.exe +curl -o xpu_driver.exe --retry 3 --retry-all-errors -k %XPU_DRIVER_LINK% +echo "XPU Driver installing..." +start /wait "Intel XPU Driver Installer" "xpu_driver.exe" +if errorlevel 1 exit /b 1 +del xpu_driver.exe +if "%XPU_INSTALL_MODE%"=="driver" goto xpu_install_end + +:xpu_bundle_install_start + +set XPU_BUNDLE_PARENT_DIR=C:\Program Files (x86)\Intel\oneAPI +set XPU_BUNDLE_URL=https://registrationcenter-download.intel.com/akdlm/IRC_NAS/9d1a91e2-e8b8-40a5-8c7f-5db768a6a60c/w_intel-for-pytorch-gpu-dev_p_0.5.3.37_offline.exe +set XPU_PTI_URL=https://registrationcenter-download.intel.com/akdlm/IRC_NAS/9d1a91e2-e8b8-40a5-8c7f-5db768a6a60c/w_intel-pti-dev_p_0.9.0.37_offline.exe +set XPU_BUNDLE_VERSION=0.5.3+31 +set XPU_PTI_VERSION=0.9.0+36 +set XPU_BUNDLE_PRODUCT_NAME=intel.oneapi.win.intel-for-pytorch-gpu-dev.product +set XPU_PTI_PRODUCT_NAME=intel.oneapi.win.intel-pti-dev.product +set XPU_BUNDLE_INSTALLED=0 +set XPU_PTI_INSTALLED=0 +set XPU_BUNDLE_UNINSTALL=0 +set XPU_PTI_UNINSTALL=0 + +:: Check if XPU bundle is target version or already installed +if exist "%XPU_BUNDLE_PARENT_DIR%\Installer\installer.exe" goto xpu_bundle_ver_check +goto xpu_bundle_install + +:xpu_bundle_ver_check + +"%XPU_BUNDLE_PARENT_DIR%\Installer\installer.exe" --list-products > xpu_bundle_installed_ver.log + +for /f "tokens=1,2" %%a in (xpu_bundle_installed_ver.log) do ( + if "%%a"=="%XPU_BUNDLE_PRODUCT_NAME%" ( + echo %%a Installed Version: %%b + set XPU_BUNDLE_INSTALLED=1 + if not "%XPU_BUNDLE_VERSION%"=="%%b" ( + start /wait "Installer Title" "%XPU_BUNDLE_PARENT_DIR%\Installer\installer.exe" --action=remove --eula=accept --silent --product-id %XPU_BUNDLE_PRODUCT_NAME% --product-ver %%b --log-dir uninstall_bundle + set XPU_BUNDLE_UNINSTALL=1 + ) + ) + if "%%a"=="%XPU_PTI_PRODUCT_NAME%" ( + echo %%a Installed Version: %%b + set XPU_PTI_INSTALLED=1 + if not "%XPU_PTI_VERSION%"=="%%b" ( + start /wait "Installer Title" "%XPU_BUNDLE_PARENT_DIR%\Installer\installer.exe" --action=remove --eula=accept --silent --product-id %XPU_PTI_PRODUCT_NAME% --product-ver %%b --log-dir uninstall_bundle + set XPU_PTI_UNINSTALL=1 + ) + ) +) +if errorlevel 1 exit /b 1 +if exist xpu_bundle_installed_ver.log del xpu_bundle_installed_ver.log +if "%XPU_BUNDLE_INSTALLED%"=="0" goto xpu_bundle_install +if "%XPU_BUNDLE_UNINSTALL%"=="1" goto xpu_bundle_install +if "%XPU_PTI_INSTALLED%"=="0" goto xpu_pti_install +if "%XPU_PTI_UNINSTALL%"=="1" goto xpu_pti_install +goto xpu_install_end + +:xpu_bundle_install + +curl -o xpu_bundle.exe --retry 3 --retry-all-errors -k %XPU_BUNDLE_URL% +echo "XPU Bundle installing..." +start /wait "Intel Pytorch Bundle Installer" "xpu_bundle.exe" --action=install --eula=accept --silent --log-dir install_bundle +if errorlevel 1 exit /b 1 +del xpu_bundle.exe + +:xpu_pti_install + +curl -o xpu_pti.exe --retry 3 --retry-all-errors -k %XPU_PTI_URL% +echo "XPU PTI installing..." +start /wait "Intel PTI Installer" "xpu_pti.exe" --action=install --eula=accept --silent --log-dir install_bundle +if errorlevel 1 exit /b 1 +del xpu_pti.exe + +:xpu_install_end diff --git a/windows/xpu.bat b/windows/xpu.bat new file mode 100755 index 000000000..2d703ccdd --- /dev/null +++ b/windows/xpu.bat @@ -0,0 +1,34 @@ +@echo on + +set MODULE_NAME=pytorch + +IF NOT EXIST "setup.py" IF NOT EXIST "%MODULE_NAME%" ( + call internal\clone.bat + cd .. +) ELSE ( + call internal\clean.bat +) +IF ERRORLEVEL 1 goto :eof + +call internal\check_deps.bat +IF ERRORLEVEL 1 goto :eof + +REM Check for optional components + +echo Disabling CUDA +set USE_CUDA=0 + +call internal\check_opts.bat +IF ERRORLEVEL 1 goto :eof + +call internal\copy_cpu.bat +IF ERRORLEVEL 1 goto :eof + +echo Activate XPU Bundle env +set VS2022INSTALLDIR=%VS15INSTALLDIR% +call "%ProgramFiles(x86)%\Intel\oneAPI\setvars.bat" +IF ERRORLEVEL 1 goto :eof +SET USE_KINETO=0 + +call internal\setup.bat +IF ERRORLEVEL 1 goto :eof From d0385e0176a38fa6872ce4432365dcab901ee949 Mon Sep 17 00:00:00 2001 From: chuanqiw Date: Fri, 30 Aug 2024 11:07:07 -0700 Subject: [PATCH 2/2] workaround for xpu windows build --- windows/internal/check_opts.bat | 2 ++ windows/internal/clone.bat | 29 +++++++++++++++++------------ windows/internal/setup.bat | 1 + windows/internal/xpu_install.bat | 5 ++--- windows/xpu.bat | 13 +++++++++---- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/windows/internal/check_opts.bat b/windows/internal/check_opts.bat index 5b8a67e25..6dd7395f9 100755 --- a/windows/internal/check_opts.bat +++ b/windows/internal/check_opts.bat @@ -9,6 +9,8 @@ IF NOT ERRORLEVEL 1 ( set CMAKE_GENERATOR=Ninja ) +IF "%USE_SCCACHE%" == "0" goto sccache_end + where /q clcache.exe IF NOT ERRORLEVEL 1 ( diff --git a/windows/internal/clone.bat b/windows/internal/clone.bat index 9a4701426..7841818d4 100755 --- a/windows/internal/clone.bat +++ b/windows/internal/clone.bat @@ -1,19 +1,24 @@ @echo off :: The conda and wheels jobs are separated on Windows, so we don't need to clone again. -if exist "%NIGHTLIES_PYTORCH_ROOT%" ( - :: Attempt to fix infinite copy of ittapi recursive symlinks on non-ephemeral runners - if exist pytorch ( - rmdir /s /q pytorch - ) - :: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy - :: Without symlink handling, robocopy could get into an infinite loop with ittapi. With /sl, - :: symlink is ignored so functorch/docs/source/notebooks won't work. The correct option is - :: /xj to exclude junction point (what the heck does that even mean Microsoft?) - robocopy "%NIGHTLIES_PYTORCH_ROOT%" pytorch\ /e /np /nfl /xjd - cd pytorch +if not exist "%NIGHTLIES_PYTORCH_ROOT%" goto clone_pytorch +:: XPU windows build can't work with long src path, use NIGHTLIES_PYTORCH_ROOT directly. +:: Refer https://github.com/pytorch/pytorch/issues/134956 +if "%CUDA_VERSION%" == "xpu" ( + cd "%NIGHTLIES_PYTORCH_ROOT%" + goto submodule ) -if exist "%NIGHTLIES_PYTORCH_ROOT%" goto submodule +:: Attempt to fix infinite copy of ittapi recursive symlinks on non-ephemeral runners +if exist pytorch rmdir /s /q pytorch +:: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy +:: Without symlink handling, robocopy could get into an infinite loop with ittapi. With /sl, +:: symlink is ignored so functorch/docs/source/notebooks won't work. The correct option is +:: /xj to exclude junction point (what the heck does that even mean Microsoft?) +robocopy "%NIGHTLIES_PYTORCH_ROOT%" pytorch\ /e /np /nfl /xjd +cd pytorch +goto submodule + +:clone_pytorch git clone https://github.com/%PYTORCH_REPO%/%MODULE_NAME% diff --git a/windows/internal/setup.bat b/windows/internal/setup.bat index 810d09db9..53b72ff11 100755 --- a/windows/internal/setup.bat +++ b/windows/internal/setup.bat @@ -9,6 +9,7 @@ if NOT "%CXX%"=="" echo CXX=%CXX% if NOT "%DISTUTILS_USE_SDK%"=="" echo DISTUTILS_USE_SDK=%DISTUTILS_USE_SDK% set SRC_DIR=%~dp0\.. +if exist "%NIGHTLIES_PYTORCH_ROOT%" if "%CUDA_VERSION%" == "xpu" set SRC_DIR=%NIGHTLIES_PYTORCH_ROOT% IF "%VSDEVCMD_ARGS%" == "" ( call "%VS15VCVARSALL%" x64 diff --git a/windows/internal/xpu_install.bat b/windows/internal/xpu_install.bat index bd9efa41d..535d69cce 100644 --- a/windows/internal/xpu_install.bat +++ b/windows/internal/xpu_install.bat @@ -1,4 +1,3 @@ - @echo on REM Description: Install Intel Support Packages on Windows REM BKM reference: https://www.intel.com/content/www/us/en/developer/articles/tool/pytorch-prerequisites-for-intel-gpu/2-5.html @@ -55,7 +54,7 @@ goto xpu_bundle_install for /f "tokens=1,2" %%a in (xpu_bundle_installed_ver.log) do ( if "%%a"=="%XPU_BUNDLE_PRODUCT_NAME%" ( echo %%a Installed Version: %%b - set XPU_BUNDLE_INSTALLED=1 + set XPU_BUNDLE_INSTALLED=1 if not "%XPU_BUNDLE_VERSION%"=="%%b" ( start /wait "Installer Title" "%XPU_BUNDLE_PARENT_DIR%\Installer\installer.exe" --action=remove --eula=accept --silent --product-id %XPU_BUNDLE_PRODUCT_NAME% --product-ver %%b --log-dir uninstall_bundle set XPU_BUNDLE_UNINSTALL=1 @@ -63,7 +62,7 @@ for /f "tokens=1,2" %%a in (xpu_bundle_installed_ver.log) do ( ) if "%%a"=="%XPU_PTI_PRODUCT_NAME%" ( echo %%a Installed Version: %%b - set XPU_PTI_INSTALLED=1 + set XPU_PTI_INSTALLED=1 if not "%XPU_PTI_VERSION%"=="%%b" ( start /wait "Installer Title" "%XPU_BUNDLE_PARENT_DIR%\Installer\installer.exe" --action=remove --eula=accept --silent --product-id %XPU_PTI_PRODUCT_NAME% --product-ver %%b --log-dir uninstall_bundle set XPU_PTI_UNINSTALL=1 diff --git a/windows/xpu.bat b/windows/xpu.bat index 2d703ccdd..11f1528c1 100755 --- a/windows/xpu.bat +++ b/windows/xpu.bat @@ -4,7 +4,7 @@ set MODULE_NAME=pytorch IF NOT EXIST "setup.py" IF NOT EXIST "%MODULE_NAME%" ( call internal\clone.bat - cd .. + cd %~dp0 ) ELSE ( call internal\clean.bat ) @@ -21,14 +21,19 @@ set USE_CUDA=0 call internal\check_opts.bat IF ERRORLEVEL 1 goto :eof -call internal\copy_cpu.bat +if exist "%NIGHTLIES_PYTORCH_ROOT%" cd %NIGHTLIES_PYTORCH_ROOT%\.. +call %~dp0\internal\copy_cpu.bat IF ERRORLEVEL 1 goto :eof echo Activate XPU Bundle env set VS2022INSTALLDIR=%VS15INSTALLDIR% call "%ProgramFiles(x86)%\Intel\oneAPI\setvars.bat" IF ERRORLEVEL 1 goto :eof -SET USE_KINETO=0 +set USE_KINETO=0 +:: Workaround for https://github.com/pytorch/pytorch/issues/134989 +set CMAKE_SHARED_LINKER_FLAGS=/FORCE:MULTIPLE +set CMAKE_MODULE_LINKER_FLAGS=/FORCE:MULTIPLE +set CMAKE_EXE_LINKER_FLAGS=/FORCE:MULTIPLE -call internal\setup.bat +call %~dp0\internal\setup.bat IF ERRORLEVEL 1 goto :eof