Skip to content

Commit 86c3e39

Browse files
committed
[build] Use static linking for CUDA build and allow dynamic AVX dispatch
Also add caching to speedup builds.
1 parent 072bda8 commit 86c3e39

File tree

1 file changed

+85
-17
lines changed

1 file changed

+85
-17
lines changed

.github/workflows/build-windows-cuda.yml

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,47 @@ jobs:
2424
- name: 📥 Checkout code
2525
uses: actions/checkout@v4
2626

27-
- name: 🏗️ Install Build Dependencies
28-
run: |
29-
echo "Installing CMake and Ninja for building..."
30-
choco install -y cmake ninja
27+
#- name: 🏗️ Install Build Dependencies
28+
# run: |
29+
# echo "Installing CMake and Ninja for building..."
30+
# choco install -y cmake ninja
3131

3232
- name: 🐍 Set up Python
3333
uses: actions/setup-python@v5
3434
with:
35-
python-version: '3.13'
35+
python-version: '3.12'
3636

3737
- name: 📦 Install Python Build Requirements
3838
run: |
3939
python -m pip install --upgrade pip
4040
pip install numpy wheel
4141
42+
- name: Setup MSBuild.exe
43+
uses: microsoft/[email protected]
44+
45+
- name: Setup NASM
46+
uses: ilammy/setup-nasm@v1
47+
48+
- name: Cache CUDA Toolkit Installer
49+
id: cache-cuda-installer
50+
uses: actions/cache@v3
51+
with:
52+
path: ./.cache/cuda_installer.exe
53+
key: cuda-installer-12.1.1
54+
4255
- name: 🔧 Install NVIDIA CUDA Toolkit
4356
run: |
44-
echo "Downloading CUDA Toolkit..."
45-
$cuda_installer_url = "https://developer.download.nvidia.com/compute/cuda/12.1.1/network_installers/cuda_12.1.1_windows_network.exe"
46-
# Use the runner's temp directory, which is guaranteed to exist.
47-
$installer_path = Join-Path $env:RUNNER_TEMP "cuda_installer.exe"
48-
curl -L -o $installer_path $cuda_installer_url
57+
$installer_path = "./.cache/cuda_installer.exe"
58+
if (-not (Test-Path $installer_path)) {
59+
echo "Downloading CUDA Toolkit..."
60+
$cuda_installer_url = "https://developer.download.nvidia.com/compute/cuda/12.1.1/network_installers/cuda_12.1.1_windows_network.exe"
61+
New-Item -Path (Split-Path $installer_path) -ItemType Directory -Force
62+
curl -L -o $installer_path $cuda_installer_url
63+
} else {
64+
echo "CUDA Toolkit installer found in cache."
65+
}
4966
5067
echo "Installing CUDA Toolkit silently..."
51-
# Use Start-Process for robust execution in PowerShell.
52-
# The arguments must be passed as a single string.
5368
$arguments = "-s nvcc_12.1 cudart_12.1"
5469
Start-Process -FilePath $installer_path -ArgumentList $arguments -Wait -NoNewWindow
5570
@@ -60,23 +75,76 @@ jobs:
6075
echo "$cuda_path\lib\x64" | Out-File -FilePath $env:GITHUB_PATH -Append
6176
shell: pwsh
6277

78+
- name: Cache opencv-python repository
79+
id: cache-opencv-python
80+
uses: actions/cache@v3
81+
# TODO: Need to pin this to a version.
82+
with:
83+
path: opencv-python
84+
key: opencv-python-master
85+
6386
- name: Clone opencv-python repository
87+
if: steps.cache-opencv-python.outputs.cache-hit != 'true'
6488
run: |
6589
git clone --depth 1 --recursive https://github.com/opencv/opencv-python.git
66-
pip install numpy packaging scikit-build
90+
git checkout 31b0eeea0b44b370fd0712312df4214d4ae1b158
91+
92+
- name: Install opencv-python dependencies
93+
run: |
94+
pip install numpy setuptools scikit-build cmake==3.24.2
6795
6896
# - CUDA_ARCH_BIN: Specifies the CUDA architectures to build for.
6997
# - CUDA_ARCH_PTX: Specifies the virtual architecture for forward compatibility.
7098
# TODO: Set correct PTX versions below.
7199
# TODO: Validate the built wheel can be installed and run.
100+
# TODO: Slim down the modules we build to only the ones that DVR-Scan needs to run:
101+
# https://docs.opencv.org/4.x/db/d05/tutorial_config_reference.html#tutorial_config_reference_general_modules
72102
- name: 🛠️ Build opencv-python with CUDA
73103
run: |
74-
cd opencv-python
75-
$CMAKE_ARGS = '-D CMAKE_BUILD_TYPE=RELEASE -D WITH_CUDA=ON -D CUDA_ARCH_BIN="6.0;6.1;7.0;7.5" -D CUDA_ARCH_PTX=7.5 -D OPENCV_ENABLE_NONFREE=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF'
76-
pip wheel . --verbose
104+
cd opencv-python/opencv
105+
$env:ENABLE_CONTRIB = 1
106+
$env:CI_BUILD = 1
107+
$PYTHON_EXECUTABLE = (Get-Command python).Source
108+
$PYTHON_INCLUDE_DIR = $(python -c "import sysconfig; print(sysconfig.get_path('include'))")
109+
$PYTHON_NUMPY_INCLUDE_DIR = $(python -c "import numpy; print(numpy.get_include())")
110+
$PYTHON_PACKAGES_PATH = $(python -c "import sysconfig; print(sysconfig.get_path('platlib'))")
111+
112+
mkdir build
113+
cd build
114+
115+
# Configure the project using CMake
116+
cmake .. -G "Visual Studio 17 2022" -A x64 `
117+
-DCMAKE_BUILD_TYPE=RELEASE `
118+
-DBUILD_SHARED_LIBS=OFF `
119+
-DWITH_CUDA=ON `
120+
-DCUDA_ARCH_BIN="6.0;6.1;7.0;7.5" `
121+
-DCUDA_ARCH_PTX=7.5 `
122+
-DOPENCV_ENABLE_NONFREE=ON `
123+
-DENABLE_LTO=ON `
124+
-DCPU_DISPATCH="AVX,AVX2" `
125+
-DBUILD_TESTS=OFF `
126+
-DBUILD_PERF_TESTS=OFF `
127+
-DBUILD_EXAMPLES=OFF `
128+
-DOPENCV_EXTRA_MODULES_PATH="../opencv_contrib/modules" `
129+
-DBUILD_opencv_python_bindings=ON `
130+
-DINSTALL_PYTHON_EXAMPLES=OFF `
131+
-DPYTHON3_EXECUTABLE="$PYTHON_EXECUTABLE" `
132+
-DPYTHON3_INCLUDE_DIR="$PYTHON_INCLUDE_DIR" `
133+
-DPYTHON3_NUMPY_INCLUDE_DIRS="$PYTHON_NUMPY_INCLUDE_DIR" `
134+
-DPYTHON3_PACKAGES_PATH="$PYTHON_PACKAGES_PATH"
135+
136+
# Build the project
137+
cmake --build . --config Release -- /m
138+
139+
# Go back to the root of opencv-python to run setup.py
140+
cd ../..
141+
142+
# Package the built files into a wheel
143+
python setup.py bdist_wheel --build-dir build -v
144+
shell: pwsh
77145

78146
- name: 📤 Upload Artifact
79147
uses: actions/[email protected]
80148
with:
81149
name: opencv-python-cuda
82-
path: opencv-python/opencv_python*.whl
150+
path: opencv-python/dist/opencv_python*.whl

0 commit comments

Comments
 (0)