Skip to content

Commit fd6d225

Browse files
committed
[dist] Build module with CUDA enabled
1 parent ba84fb8 commit fd6d225

File tree

2 files changed

+77
-8
lines changed

2 files changed

+77
-8
lines changed

.github/workflows/build_wheels_windows.yml

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
name: Windows x86_64
1+
# GitHub Actions Workflow: Build opencv-python with CUDA support on Windows
2+
#
3+
# This workflow compiles opencv-python from source with CUDA enabled on a
4+
# GitHub-hosted Windows runner. The resulting Python wheel is then uploaded
5+
# as a build artifact.
6+
#
7+
# This is a complex and long-running process. It is configured to run only
8+
# on manual trigger (workflow_dispatch).
9+
10+
name: Windows CUDA x64
211

312
on:
413
workflow_dispatch:
@@ -48,6 +57,54 @@ jobs:
4857
uses: microsoft/setup-msbuild@v1.1
4958
- name: Setup NASM
5059
uses: ilammy/setup-nasm@v1
60+
- name: Cache CUDA Toolkit Installer
61+
id: cache-cuda-installer
62+
uses: actions/cache@v3
63+
with:
64+
path: ./.cache/cuda_installer.exe
65+
key: cuda-installer-12.0.0
66+
- name: 🔧 Install NVIDIA CUDA Toolkit
67+
run: |
68+
$installer_path = "./.cache/cuda_installer.exe"
69+
if (-not (Test-Path $installer_path)) {
70+
echo "Downloading CUDA Toolkit..."
71+
$cuda_installer_url = "https://developer.download.nvidia.com/compute/cuda/12.0.0/network_installers/cuda_12.0.0_windows_network.exe"
72+
New-Item -Path (Split-Path $installer_path) -ItemType Directory -Force
73+
curl -L -o $installer_path $cuda_installer_url
74+
} else {
75+
echo "CUDA Toolkit installer found in cache."
76+
}
77+
echo "Installing CUDA Toolkit silently..."
78+
$arguments = "-s -n"
79+
Start-Process -FilePath $installer_path -ArgumentList $arguments -Wait -NoNewWindow
80+
echo "Adding CUDA to PATH..."
81+
$CUDA_PATH = "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.0"
82+
echo "CUDA_PATH=$CUDA_PATH" | Out-File -FilePath $env:GITHUB_ENV -Append
83+
shell: pwsh
84+
- name: Cache CuDNN Installer
85+
id: cache-cudnn-installer
86+
uses: actions/cache@v3
87+
with:
88+
path: ./.cache/cudnn.zip
89+
key: cudnn-8.9.7.29-cuda-12-windows
90+
- name: 🔧 Install NVIDIA CuDNN
91+
run: |
92+
$cudnn_path = "./.cache/cudnn.zip"
93+
if (-not (Test-Path $cudnn_path)) {
94+
echo "Downloading CuDNN..."
95+
$cudnn_installer_url = "https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/windows-x86_64/cudnn-windows-x86_64-8.9.7.29_cuda12-archive.zip"
96+
New-Item -Path (Split-Path $cudnn_path) -ItemType Directory -Force
97+
curl -L -o $cudnn_path $cudnn_installer_url
98+
} else {
99+
echo "CuDNN installer found in cache."
100+
}
101+
echo "Installing CuDNN..."
102+
7z x $cudnn_path
103+
$CUDNN_PATH = "/a/opencv-python-cuda/opencv-python-cuda/cudnn-windows-x86_64-8.9.7.29_cuda12-archive"
104+
echo "CUDNN_LIBRARY=$CUDNN_PATH/lib/x64" | Out-File -FilePath $env:GITHUB_ENV -Append
105+
echo "CUDNN_INCLUDE_DIR=$CUDNN_PATH/include" | Out-File -FilePath $env:GITHUB_ENV -Append
106+
shell: pwsh
107+
51108
- name: Build a package
52109
# CMake 3.25 regression fix. See https://stackoverflow.com/questions/74162633/problem-compiling-from-source-opencv-with-mvsc2019-in-64-bit-version
53110
run: |

setup.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ def main():
8686
if build_headless and not build_contrib:
8787
package_name = "opencv-python-headless"
8888

89-
if build_rolling:
90-
package_name += "-rolling"
89+
#if build_rolling:
90+
# package_name += "-rolling"
9191

9292
package_name = os.environ.get('OPENCV_PYTHON_PACKAGE_NAME', package_name)
9393

@@ -162,18 +162,23 @@ def main():
162162
else ["-G", "Unix Makefiles"]
163163
)
164164

165+
cudnn_library = os.environ["CUDNN_LIBRARY"]
166+
cudnn_include_dir = os.environ["CUDNN_INCLUDE_DIR"]
167+
cuda_arch_bin = "6.0;6.1;7.0;7.5"
168+
cuda_arch_ptx = "7.5"
169+
165170
cmake_args = (
166171
(ci_cmake_generator if is_CI_build else [])
167172
+ [
168173
# skbuild inserts PYTHON_* vars. That doesn't satisfy opencv build scripts in case of Py3
169-
"-DPYTHON3_EXECUTABLE=%s" % sys.executable,
170-
"-DPYTHON_DEFAULT_EXECUTABLE=%s" % sys.executable,
171-
"-DPYTHON3_INCLUDE_DIR=%s" % python_include_dir,
172-
"-DPYTHON3_LIBRARY=%s" % python_lib_path,
174+
f"-DPYTHON3_EXECUTABLE={sys.executable}",
175+
f"-DPYTHON_DEFAULT_EXECUTABLE={sys.executable}",
176+
f"-DPYTHON3_INCLUDE_DIR=\"{python_include_dir}\"",
177+
f"-DPYTHON3_LIBRARY=\"{python_lib_path}\"",
173178
"-DBUILD_opencv_python3=ON",
174179
"-DBUILD_opencv_python2=OFF",
175180
# Disable the Java build by default as it is not needed
176-
"-DBUILD_opencv_java=%s" % build_java,
181+
f"-DBUILD_opencv_java={build_java}",
177182
# Relative dir to install the built module to in the build tree.
178183
# The default is generated from sysconfig, we'd rather have a constant for simplicity
179184
"-DOPENCV_PYTHON3_INSTALL_PATH=python",
@@ -189,6 +194,13 @@ def main():
189194
"-DBUILD_DOCS=OFF",
190195
"-DPYTHON3_LIMITED_API=ON",
191196
"-DBUILD_OPENEXR=ON",
197+
"-DWITH_CUDA=ON",
198+
"-DWITH_NVCUVID=OFF",
199+
f"-DCUDA_ARCH_BIN=\"{cuda_arch_bin}\"",
200+
f"-DCUDA_ARCH_PTX=\"{cuda_arch_ptx}\"",
201+
"-DOPENCV_ENABLE_NONFREE=ON",
202+
f"-DCUDNN_LIBRARY=\"{cudnn_library}\"",
203+
f"-DCUDNN_INCLUDE_DIR=\"{cudnn_include_dir}\"",
192204
]
193205
+ (
194206
# CMake flags for windows/arm64 build

0 commit comments

Comments
 (0)