Skip to content

dlpack Python support for cuda_GpuMat and cuda_GpuMatND#27581

Merged
asmorkalov merged 10 commits intoopencv:4.xfrom
dkurt:d.kuryaev/dlpack
Aug 20, 2025
Merged

dlpack Python support for cuda_GpuMat and cuda_GpuMatND#27581
asmorkalov merged 10 commits intoopencv:4.xfrom
dkurt:d.kuryaev/dlpack

Conversation

@dkurt
Copy link
Copy Markdown
Member

@dkurt dkurt commented Jul 26, 2025

Pull Request Readiness Checklist

resolves #16295

docker run --gpus 0 -v ~/opencv:/opencv -v ~/opencv_contrib:/opencv_contrib -it nvidia/cuda:12.8.1-cudnn-devel-ubuntu22.04
apt-get update && apt-get install -y cmake python3-dev python3-pip python3-venv &&
python3 -m venv .venv &&
source .venv/bin/activate &&
pip install -U pip &&
pip install -U numpy &&
pip install torch --index-url https://download.pytorch.org/whl/cu128 &&
cmake \
    -DWITH_OPENCL=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_DOCS=OFF \
    -DWITH_CUDA=ON \
    -DOPENCV_DNN_CUDA=ON \
    -DOPENCV_EXTRA_MODULES_PATH=/opencv_contrib/modules \
    -DBUILD_LIST=ts,cudev,python3 \
    -S /opencv -B /opencv_build &&
cmake --build /opencv_build -j16
export PYTHONPATH=/opencv_build/lib/python3/:$PYTHONPATH

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@asmorkalov asmorkalov self-requested a review July 28, 2025 05:50
@asmorkalov asmorkalov added this to the 4.13.0 milestone Jul 28, 2025
@asmorkalov asmorkalov added the category: gpu/cuda (contrib) OpenCV 4.0+: moved to opencv_contrib label Jul 28, 2025
@asmorkalov asmorkalov self-assigned this Jul 28, 2025
@asmorkalov
Copy link
Copy Markdown
Contributor

DLpack available as deb package on Ubuntu. It'll be great to support external instances via find_package too just i case of compatibility issues.

@asmorkalov
Copy link
Copy Markdown
Contributor

cc @cudawarped

@asmorkalov
Copy link
Copy Markdown
Contributor

Windows build warnings:

C:/GHA-OCV-2/_work/opencv/opencv/opencv/modules/core/misc/python/pyopencv_cuda.hpp(44): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data

C:/GHA-OCV-2/_work/opencv/opencv/opencv/modules/core/misc/python/pyopencv_cuda.hpp(60): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data

C:/GHA-OCV-2/_work/opencv/opencv/opencv/modules/core/misc/python/pyopencv_cuda.hpp(92): warning C4244: 'argument': conversion from 'int64_t' to 'int', possible loss of data

C:/GHA-OCV-2/_work/opencv/opencv/opencv/modules/core/misc/python/pyopencv_cuda.hpp(98): warning C4244: 'argument': conversion from 'int64_t' to 'int', possible loss of data

C:/GHA-OCV-2/_work/opencv/opencv/opencv/modules/core/misc/python/pyopencv_cuda.hpp(97): warning C4244: 'argument': conversion from 'int64_t' to 'int', possible loss of data

C:/GHA-OCV-2/_work/opencv/opencv/opencv/modules/core/misc/python/pyopencv_cuda.hpp(119): warning C4244: 'argument': conversion from 'int64_t' to 'int', possible loss of data

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(681): warning C4244: 'initializing': conversion from 'value_t' to '_Objty', possible loss of data

@dkurt dkurt force-pushed the d.kuryaev/dlpack branch from cb28a92 to 52af6a5 Compare August 5, 2025 09:52
@dkurt dkurt changed the title dlpack Python support for cuda_GpuMat dlpack Python support for cuda_GpuMat and cuda_GpuMatND Aug 7, 2025
@dkurt dkurt marked this pull request as ready for review August 7, 2025 10:58
@asmorkalov
Copy link
Copy Markdown
Contributor

@dkurt Thanks a lot for the contribution! Several questions and proposals:

  • ls dlpack designed to work with regular CPU memory? Do we need cv::Mat/ndarray integration?
  • Cuda is not enabled in default opencv-python builds. What is expected behaviour?
  • Imho it make sense to move the test to regular test suite from CUDA.

@dkurt
Copy link
Copy Markdown
Member Author

dkurt commented Aug 12, 2025

ls dlpack designed to work with regular CPU memory? Do we need cv::Mat/ndarray integration?

Yes, but as we already work with OpenCV memory as numpy array, we can use numpy's dlpack.

Cuda is not enabled in default opencv-python builds. What is expected behaviour?

CUDA is required so actual for build from source.

Imho it make sense to move the test to regular test suite from CUDA.

Do you mean opencv_contrib?

@asmorkalov
Copy link
Copy Markdown
Contributor

Do you mean opencv_contrib?

No. test_cuda.py is not triggered by CI now.

@dkurt
Copy link
Copy Markdown
Member Author

dkurt commented Aug 14, 2025

@asmorkalov I see only dnn tests here: https://github.com/opencv/opencv/actions/runs/16773287951/job/47493324938?pr=27581 . Shall I extend the test scope to include Python tests to CUDA runner?

@asmorkalov
Copy link
Copy Markdown
Contributor

Most probably you need to push something to refresh CI branch.

@dkurt dkurt force-pushed the d.kuryaev/dlpack branch from c88371f to 052b79f Compare August 15, 2025 12:16
@asmorkalov asmorkalov merged commit ba19416 into opencv:4.x Aug 20, 2025
23 of 28 checks passed
@asmorkalov asmorkalov mentioned this pull request Aug 20, 2025
@asmorkalov asmorkalov mentioned this pull request Aug 27, 2025
D00E pushed a commit to D00E/opencv that referenced this pull request Sep 19, 2025
### Pull Request Readiness Checklist

resolves opencv#16295

```
docker run --gpus 0 -v ~/opencv:/opencv -v ~/opencv_contrib:/opencv_contrib -it nvidia/cuda:12.8.1-cudnn-devel-ubuntu22.04
apt-get update && apt-get install -y cmake python3-dev python3-pip python3-venv &&
python3 -m venv .venv &&
source .venv/bin/activate &&
pip install -U pip &&
pip install -U numpy &&
pip install torch --index-url https://download.pytorch.org/whl/cu128 &&
cmake \
    -DWITH_OPENCL=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_DOCS=OFF \
    -DWITH_CUDA=ON \
    -DOPENCV_DNN_CUDA=ON \
    -DOPENCV_EXTRA_MODULES_PATH=/opencv_contrib/modules \
    -DBUILD_LIST=ts,cudev,python3 \
    -S /opencv -B /opencv_build &&
cmake --build /opencv_build -j16
export PYTHONPATH=/opencv_build/lib/python3/:$PYTHONPATH
```

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
@opencv-alalek
Copy link
Copy Markdown
Contributor

Win32 warnings:

C:/build/precommit_windows32/4.x/opencv/modules/core/misc/python/pyopencv_cuda.hpp(116): warning C4244: 'argument': conversion from 'int64_t' to 'std::size_t', possible loss of data (compiling source file C:\build\precommit_windows32\4.x\opencv\modules\python\src2\cv2.cpp) [C:\build\precommit_windows32\build\modules\python3\opencv_python3.vcxproj]
C:/build/precommit_windows32/4.x/opencv/modules/core/misc/python/pyopencv_cuda.hpp(143): warning C4244: '=': conversion from 'int64_t' to 'unsigned int', possible loss of data (compiling source file C:\build\precommit_windows32\4.x\opencv\modules\python\src2\cv2.cpp) [C:\build\precommit_windows32\build\modules\python3\opencv_python3.vcxproj]

asmorkalov pushed a commit that referenced this pull request Sep 25, 2025
Add 5.x types for DLPack. Keep uint32/int64/uint64 data type for conversion to Numpy. More types support for GpuMat::convertTo #27779

### Pull Request Readiness Checklist

**Merge with contrib**: opencv/opencv_contrib#4000

related: #27581

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Any intention to add interoperability with DLPACK or mpi4py?

3 participants