Skip to content

Conversation

@SimonRit
Copy link
Collaborator

Adding export code to #42

@SimonRit SimonRit force-pushed the cuda-array-interface branch from 9e356cf to e176586 Compare December 19, 2024 17:47
@LucasGandel LucasGandel mentioned this pull request Dec 20, 2024
6 tasks
@SimonRit
Copy link
Collaborator Author

This is a detail but fc4cfa8 and 792d00e could be squashed into a single commit. This PR will be merged for CudaCommon v2, after a first release of CudaCommon v1.2, based on ITK v5.4.1 and/or v5.4.2.

@SimonRit SimonRit added this to the CudaCommon 2.0 milestone Mar 24, 2025
@SimonRit SimonRit force-pushed the cuda-array-interface branch from fc4cfa8 to a6729f8 Compare March 25, 2025 14:40
@SimonRit SimonRit force-pushed the cuda-array-interface branch 5 times, most recently from f7a5762 to 214cfbe Compare April 5, 2025 11:27
@LucasGandel LucasGandel force-pushed the cuda-array-interface branch from 214cfbe to dd33430 Compare April 25, 2025 12:21
@LucasGandel
Copy link
Collaborator

@SimonRit please review. The code has been updated to consume cuda_array_interface. Below is a quick code snippet to test:

import itk
import cupy as cp
x_gpu = cp.array([[0, 1, 2],[3, 4, 5]], dtype=cp.float32, order='C')

cuda_img = itk.cuda_image_from_cuda_array(x_gpu)
pixelIndex = itk.Index[2]()
pixelIndex[0] = 2
pixelIndex[1] = 1
print(f"5 = {cuda_img.GetPixel(pixelIndex)}")

@SimonRit
Copy link
Collaborator Author

SimonRit commented Sep 4, 2025

TODO:

  • Doc page describing how to use cuda_image_from_cuda_array and the cuda array interface
  • Tests
  • Examples

@axel-grc axel-grc force-pushed the cuda-array-interface branch from 7e8572b to 94dced1 Compare September 9, 2025 11:23
Copy link
Collaborator Author

@SimonRit SimonRit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general. I think we need to be more general than CuPy and Torch as it can be used with many packages. We should add a few hyperlinks.


## Overview

ITK's `CudaImage` implements the `__cuda_array_interface__` protocol, allowing it to be used directly as a CuPy array or PyTorch tensor without copying data to/from the GPU.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make __cuda_array_interface__ an hyperlink to https://numba.pydata.org/numba-doc/0.43.0/cuda/cuda_array_interface.html

@@ -0,0 +1,26 @@
# CUDA Array Interface in ITK

The CUDA Array Interface allows seamless interoperability between ITK's `CudaImage` and GPU arrays from libraries like CuPy and PyTorch. This enables zero-copy data sharing, efficient GPU operations, and integration with deep learning pipelines.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would indicate in parentheses the structure names in the libraries, i.e., CuPy (cupy.ndarray) and PyTorch (torch.Tensor).


ITK's `CudaImage` implements the `__cuda_array_interface__` protocol, allowing it to be used directly as a CuPy array or PyTorch tensor without copying data to/from the GPU.

- **Zero-copy access**: Convert `CudaImage` to CuPy/Torch views instantly.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use cupy.ndarray / torch.Tensor.

@axel-grc axel-grc force-pushed the cuda-array-interface branch 2 times, most recently from ee768ae to 22bdfbb Compare September 12, 2025 14:52
@SimonRit SimonRit force-pushed the cuda-array-interface branch 3 times, most recently from 5b1a577 to e19ca53 Compare September 14, 2025 20:50
@axel-grc axel-grc force-pushed the cuda-array-interface branch 4 times, most recently from bb453c4 to 726ff1b Compare September 15, 2025 09:32
@SimonRit SimonRit force-pushed the cuda-array-interface branch 2 times, most recently from ba4828d to e19ca53 Compare September 17, 2025 06:12
@axel-grc axel-grc force-pushed the cuda-array-interface branch from e19ca53 to 726ff1b Compare September 18, 2025 12:16
@SimonRit SimonRit force-pushed the cuda-array-interface branch from 726ff1b to cb1e549 Compare September 23, 2025 11:00
@SimonRit SimonRit force-pushed the cuda-array-interface branch from cb1e549 to 611b407 Compare September 23, 2025 14:47
@SimonRit
Copy link
Collaborator Author

@LucasGandel I changed your wrapping/itkCudaImageFromCudaArray.py code in the last forced push because I don't think it makes sense to set buffered region without setting the other regions.

@axel-grc Your examples and test, presumably based on this, have also been modified. Note that the same code with an itk.Image would crash with a seg fault.

Simon Rit and others added 20 commits September 24, 2025 21:53
Prior to this commit CudaDataManager::GetGPUBufferPointer() returned a
pointer to the GPU buffer pointer. Its return type should have been void**.
Every usage of this method across RTK and CudaCommon dereference the
returned pointer pointer, so just return the buffer pointer directly as it
required for future commits adding support for __cuda_array_interface__
Adapt ITK's PyBuffer approach to inject __cuda_array_interface__ in wrapped
code.
Closes RTKConsortium#10
This allows modules like RTK to easily reuse files from the CudaCommon
sources, e.g. CudaImage.i.init and CudaImage.i.in
Add function to create a CudaImage from an object providing
__cuda_array_interface__
Allows the CPU buffer to be allocated automatically when only the GPU
pointer is set on the image.
Suppresses the message:
```
ERROR: pip's dependency resolver does not currently take into account
all the packages that are installed. This behaviour is the source of the
following dependency conflicts.
```
@SimonRit
Copy link
Collaborator Author

SimonRit commented Oct 2, 2025

Thanks again @LucasGandel, time to merge!

@SimonRit SimonRit merged commit d8dfe12 into RTKConsortium:main Oct 2, 2025
12 checks passed
@SimonRit SimonRit deleted the cuda-array-interface branch October 14, 2025 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants