xref: /aosp_15_r20/external/pytorch/.ci/pytorch/win-test-helpers/build_pytorch.bat (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1if "%DEBUG%" == "1" (
2  set BUILD_TYPE=debug
3) ELSE (
4  set BUILD_TYPE=release
5)
6
7set PATH=C:\Program Files\CMake\bin;C:\Program Files\7-Zip;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\Program Files\Amazon\AWSCLI;C:\Program Files\Amazon\AWSCLI\bin;%PATH%
8
9:: This inflates our log size slightly, but it is REALLY useful to be
10:: able to see what our cl.exe commands are (since you can actually
11:: just copy-paste them into a local Windows setup to just rebuild a
12:: single file.)
13:: log sizes are too long, but leaving this here incase someone wants to use it locally
14:: set CMAKE_VERBOSE_MAKEFILE=1
15
16
17set INSTALLER_DIR=%SCRIPT_HELPERS_DIR%\installation-helpers
18
19call %INSTALLER_DIR%\install_magma.bat
20if errorlevel 1 goto fail
21if not errorlevel 0 goto fail
22
23call %INSTALLER_DIR%\install_sccache.bat
24if errorlevel 1 goto fail
25if not errorlevel 0 goto fail
26
27if "%USE_XPU%"=="1" (
28  :: Install xpu support packages
29  call %INSTALLER_DIR%\install_xpu.bat
30  if errorlevel 1 exit /b 1
31)
32
33:: Miniconda has been installed as part of the Windows AMI with all the dependencies.
34:: We just need to activate it here
35call %INSTALLER_DIR%\activate_miniconda3.bat
36if errorlevel 1 goto fail
37if not errorlevel 0 goto fail
38
39call pip install mkl-include==2021.4.0 mkl-devel==2021.4.0
40if errorlevel 1 goto fail
41if not errorlevel 0 goto fail
42
43:: Override VS env here
44pushd .
45if "%VC_VERSION%" == "" (
46    call "C:\Program Files (x86)\Microsoft Visual Studio\%VC_YEAR%\%VC_PRODUCT%\VC\Auxiliary\Build\vcvarsall.bat" x64
47) else (
48    call "C:\Program Files (x86)\Microsoft Visual Studio\%VC_YEAR%\%VC_PRODUCT%\VC\Auxiliary\Build\vcvarsall.bat" x64 -vcvars_ver=%VC_VERSION%
49)
50if errorlevel 1 goto fail
51if not errorlevel 0 goto fail
52
53if "%USE_XPU%"=="1" (
54  :: Activate xpu environment - VS env is required for xpu
55  call "C:\Program Files (x86)\Intel\oneAPI\setvars.bat"
56  if errorlevel 1 exit /b 1
57  :: Reduce build time. Only have MTL self-hosted runner now
58  SET TORCH_XPU_ARCH_LIST=xe-lpg
59  SET USE_KINETO=0
60)
61
62@echo on
63popd
64
65if not "%USE_CUDA%"=="1" goto cuda_build_end
66
67set CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v%CUDA_VERSION%
68
69if x%CUDA_VERSION:.=%==x%CUDA_VERSION% (
70    echo CUDA version %CUDA_VERSION% format isn't correct, which doesn't contain '.'
71    goto fail
72)
73rem version transformer, for example 10.1 to 10_1.
74if x%CUDA_VERSION:.=%==x%CUDA_VERSION% (
75    echo CUDA version %CUDA_VERSION% format isn't correct, which doesn't contain '.'
76    goto fail
77)
78set VERSION_SUFFIX=%CUDA_VERSION:.=_%
79set CUDA_PATH_V%VERSION_SUFFIX%=%CUDA_PATH%
80
81set CUDNN_LIB_DIR=%CUDA_PATH%\lib\x64
82set CUDA_TOOLKIT_ROOT_DIR=%CUDA_PATH%
83set CUDNN_ROOT_DIR=%CUDA_PATH%
84set PATH=%CUDA_PATH%\bin;%CUDA_PATH%\libnvvp;%PATH%
85
86:cuda_build_end
87
88set DISTUTILS_USE_SDK=1
89set PATH=%TMP_DIR_WIN%\bin;%PATH%
90
91:: The latest Windows CUDA test is running on AWS G5 runner with A10G GPU
92if "%TORCH_CUDA_ARCH_LIST%" == "" set TORCH_CUDA_ARCH_LIST=8.6
93
94:: The default sccache idle timeout is 600, which is too short and leads to intermittent build errors.
95set SCCACHE_IDLE_TIMEOUT=0
96set SCCACHE_IGNORE_SERVER_IO_ERROR=1
97sccache --stop-server
98sccache --start-server
99sccache --zero-stats
100set CMAKE_C_COMPILER_LAUNCHER=sccache
101set CMAKE_CXX_COMPILER_LAUNCHER=sccache
102
103set CMAKE_GENERATOR=Ninja
104
105if "%USE_CUDA%"=="1" (
106  :: randomtemp is used to resolve the intermittent build error related to CUDA.
107  :: code: https://github.com/peterjc123/randomtemp-rust
108  :: issue: https://github.com/pytorch/pytorch/issues/25393
109  ::
110  :: CMake requires a single command as CUDA_NVCC_EXECUTABLE, so we push the wrappers
111  :: randomtemp.exe and sccache.exe into a batch file which CMake invokes.
112  curl -kL https://github.com/peterjc123/randomtemp-rust/releases/download/v0.4/randomtemp.exe --output %TMP_DIR_WIN%\bin\randomtemp.exe
113  if errorlevel 1 goto fail
114  if not errorlevel 0 goto fail
115  echo @"%TMP_DIR_WIN%\bin\randomtemp.exe" "%TMP_DIR_WIN%\bin\sccache.exe" "%CUDA_PATH%\bin\nvcc.exe" %%* > "%TMP_DIR%/bin/nvcc.bat"
116  cat %TMP_DIR%/bin/nvcc.bat
117  set CUDA_NVCC_EXECUTABLE=%TMP_DIR%/bin/nvcc.bat
118  for /F "usebackq delims=" %%n in (`cygpath -m "%CUDA_PATH%\bin\nvcc.exe"`) do set CMAKE_CUDA_COMPILER=%%n
119  set CMAKE_CUDA_COMPILER_LAUNCHER=%TMP_DIR%/bin/randomtemp.exe;%TMP_DIR%\bin\sccache.exe
120)
121
122:: Print all existing environment variable for debugging
123set
124
125python setup.py bdist_wheel
126if errorlevel 1 goto fail
127if not errorlevel 0 goto fail
128sccache --show-stats
129python -c "import os, glob; os.system('python -mpip install --no-index --no-deps ' + glob.glob('dist/*.whl')[0])"
130(
131  if "%BUILD_ENVIRONMENT%"=="" (
132    echo NOTE: To run `import torch`, please make sure to activate the conda environment by running `call %CONDA_PARENT_DIR%\Miniconda3\Scripts\activate.bat %CONDA_PARENT_DIR%\Miniconda3` in Command Prompt before running Git Bash.
133  ) else (
134    copy /Y "dist\*.whl" "%PYTORCH_FINAL_PACKAGE_DIR%"
135
136    :: export test times so that potential sharded tests that'll branch off this build will use consistent data
137    python tools/stats/export_test_times.py
138    robocopy /E ".additional_ci_files" "%PYTORCH_FINAL_PACKAGE_DIR%\.additional_ci_files"
139
140    :: Also save build/.ninja_log as an artifact
141    copy /Y "build\.ninja_log" "%PYTORCH_FINAL_PACKAGE_DIR%\"
142  )
143)
144
145sccache --show-stats --stats-format json | jq .stats > sccache-stats-%BUILD_ENVIRONMENT%-%OUR_GITHUB_JOB_ID%.json
146sccache --stop-server
147
148exit /b 0
149
150:fail
151exit /b 1
152