Skip to content

Commit caa6b1b

Browse files
committed
Test Windows static lib
Add support for testing Windows Cuda static lib
1 parent d41bcbf commit caa6b1b

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed

test_example_code/CMakeLists.txt

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2+
project(simple-torch-test)
3+
4+
find_package(Torch REQUIRED)
5+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
6+
7+
8+
add_executable(simple-torch-test simple-torch-test.cpp)
9+
target_include_directories(simple-torch-test PRIVATE ${TORCH_INCLUDE_DIRS})
10+
target_link_libraries(simple-torch-test "${TORCH_LIBRARIES}")
11+
set_property(TARGET simple-torch-test PROPERTY CXX_STANDARD 17)
12+
13+
find_package(CUDAToolkit 11.8)
14+
15+
target_link_libraries(simple-torch-test CUDA::cudart CUDA::cufft CUDA::cusparse CUDA::cublas CUDA::cusolver)
16+
find_library(CUDNN_LIBRARY NAMES cudnn)
17+
target_link_libraries(simple-torch-test ${CUDNN_LIBRARY} )
18+
if (MSVC)
19+
file(GLOB TORCH_DLLS "$ENV{CUDA_PATH}/bin/cudnn64_8.dll" "$ENV{NVTOOLSEXT_PATH}/bin/x64/*.dll")
20+
message("dlls to copy " ${TORCH_DLLS})
21+
add_custom_command(TARGET simple-torch-test
22+
POST_BUILD
23+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
24+
${TORCH_DLLS}
25+
$<TARGET_FILE_DIR:simple-torch-test>)
26+
endif (MSVC)

windows/internal/static_lib_test.bat

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
set SRC_DIR=%~dp0
2+
3+
pushd %SRC_DIR%\..
4+
5+
if "%CUDA_VERSION%" == "cpu" call internal\driver_update.bat
6+
if errorlevel 1 exit /b 1
7+
8+
call internal\cuda_install.bat
9+
set LIB=%CUDA_PATH%\lib\x64;%LIB%
10+
if errorlevel 1 exit /b 1
11+
set "ORIG_PATH=%PATH%"
12+
13+
setlocal EnableDelayedExpansion
14+
set NVIDIA_GPU_EXISTS=0
15+
for /F "delims=" %%i in ('wmic path win32_VideoController get name') do (
16+
set GPUS=%%i
17+
if not "x!GPUS:NVIDIA=!" == "x!GPUS!" (
18+
SET NVIDIA_GPU_EXISTS=1
19+
goto gpu_check_end
20+
)
21+
)
22+
:gpu_check_end
23+
endlocal & set NVIDIA_GPU_EXISTS=%NVIDIA_GPU_EXISTS%
24+
25+
:: Download MAGMA Files on CUDA builds
26+
set MAGMA_VERSION=2.5.4
27+
set CUDA_PREFIX=cuda%CUDA_VERSION%
28+
if "%CUDA_VERSION%" == "92" set MAGMA_VERSION=2.5.2
29+
if "%CUDA_VERSION%" == "100" set MAGMA_VERSION=2.5.2
30+
31+
if "%DEBUG%" == "1" (
32+
set BUILD_TYPE=debug
33+
) else (
34+
set BUILD_TYPE=release
35+
)
36+
37+
if not "%CUDA_VERSION%" == "cpu" (
38+
rmdir /s /q magma_%CUDA_PREFIX%_%BUILD_TYPE%
39+
del magma_%CUDA_PREFIX%_%BUILD_TYPE%.7z
40+
curl -k https://s3.amazonaws.com/ossci-windows/magma_%MAGMA_VERSION%_%CUDA_PREFIX%_%BUILD_TYPE%.7z -o magma_%CUDA_PREFIX%_%BUILD_TYPE%.7z
41+
7z x -aoa magma_%CUDA_PREFIX%_%BUILD_TYPE%.7z -omagma_%CUDA_PREFIX%_%BUILD_TYPE%
42+
set LIB=%CD%\magma_%CUDA_PREFIX%_%BUILD_TYPE%\lib;%LIB%
43+
)
44+
45+
echo "install conda package"
46+
47+
:: Install Miniconda3
48+
set "CONDA_HOME=%CD%\conda"
49+
set "tmp_conda=%CONDA_HOME%"
50+
set "miniconda_exe=%CD%\miniconda.exe"
51+
52+
rmdir /s /q conda
53+
del miniconda.exe
54+
curl -k https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe -o "%miniconda_exe%"
55+
call ..\conda\install_conda.bat
56+
if ERRORLEVEL 1 exit /b 1
57+
58+
set "PATH=%CONDA_HOME%;%CONDA_HOME%\scripts;%CONDA_HOME%\Library\bin;%PATH%"
59+
60+
conda create -qyn testenv python=%DESIRED_PYTHON%
61+
if errorlevel 1 exit /b 1
62+
63+
call %CONDA_HOME%\condabin\activate.bat testenv
64+
if errorlevel 1 exit /b 1
65+
66+
call conda install -y -q -c conda-forge libuv=1.39
67+
call conda install -y -q intel-openmp
68+
69+
echo "install and test libtorch"
70+
pip install cmake
71+
echo "installing cmake"
72+
73+
curl https://s3.amazonaws.com/ossci-windows/mkl_2020.2.254.7z -k -O
74+
7z x -aoa mkl_2020.2.254.7z -omkl
75+
set LIB=%CD%\mkl\lib;%LIB%
76+
77+
78+
if "%VC_YEAR%" == "2019" powershell internal\vs2019_install.ps1
79+
if "%VC_YEAR%" == "2022" powershell internal\vs2022_install.ps1
80+
81+
if ERRORLEVEL 1 exit /b 1
82+
83+
for /F "delims=" %%i in ('where /R "%PYTORCH_FINAL_PACKAGE_DIR:/=\%" *-latest.zip') do 7z x "%%i" -otmp
84+
if ERRORLEVEL 1 exit /b 1
85+
86+
87+
pushd tmp\libtorch
88+
89+
set VC_VERSION_LOWER=17
90+
set VC_VERSION_UPPER=18
91+
IF "%VC_YEAR%" == "2019" (
92+
set VC_VERSION_LOWER=16
93+
set VC_VERSION_UPPER=17
94+
)
95+
96+
for /f "usebackq tokens=*" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -legacy -products * -version [%VC_VERSION_LOWER%^,%VC_VERSION_UPPER%^) -property installationPath`) do (
97+
if exist "%%i" if exist "%%i\VC\Auxiliary\Build\vcvarsall.bat" (
98+
set "VS15INSTALLDIR=%%i"
99+
set "VS15VCVARSALL=%%i\VC\Auxiliary\Build\vcvarsall.bat"
100+
goto vswhere
101+
)
102+
)
103+
104+
:vswhere
105+
IF "%VS15VCVARSALL%"=="" (
106+
echo Visual Studio %VC_YEAR% C++ BuildTools is required to compile PyTorch test on Windows
107+
exit /b 1
108+
)
109+
call "%VS15VCVARSALL%" x64
110+
111+
set install_root=%CD%
112+
set INCLUDE=%INCLUDE%;%install_root%\include;%install_root%\include\torch\csrc\api\include
113+
set LIB=%LIB%;%install_root%\lib\x64
114+
set PATH=%PATH%;%install_root%\lib
115+
116+
117+
cd %BUILDER_ROOT%\test_example_code\
118+
mkdir build
119+
cd build
120+
121+
cmake -DCMAKE_PREFIX_PATH=%install_root% ..
122+
123+
if ERRORLEVEL 1 exit /b 1
124+
125+
cmake --build . --config Release
126+
127+
.\Release\simple-torch-test.exe
128+
if ERRORLEVEL 1 exit /b 1
129+
130+
popd
131+
132+
echo Cleaning temp files
133+
rd /s /q "tmp" || ver > nul
134+
135+
:end
136+
set "PATH=%ORIG_PATH%"
137+
popd

0 commit comments

Comments
 (0)