InsightSoftwareConsortium / ITK

Insight Toolkit (ITK) -- Official Repository. ITK builds on a proven, spatially-oriented architecture for processing, segmentation, and registration of scientific images in two, three, or more dimensions.
https://itk.org
Apache License 2.0
1.43k stars 664 forks source link

GPU filters fail when invoked from Python #4846

Open allemangD opened 2 months ago

allemangD commented 2 months ago

Creating this to track the remaining issue after #4842. See the description there for details.

Description

Python-wrapped GPU filters fail with OpenCL Error : CL_INVALID_MEM_OBJECT; they work correctly from C++.

Steps to Reproduce

Build ITK with GPU and Python wrapping; run existing itkGPU* test suite.

cmake -DITK_WRAP_PYTHON=ON -DITK_USE_GPU=ON ...
cmake --build ...
ctest -R itkGPU
Test project ...
      Start  959: itkGPUGradientAnisotropicDiffusionImageFilterTest2D
 1/24 Test  #959: itkGPUGradientAnisotropicDiffusionImageFilterTest2D .........   Passed    0.15 sec
      Start  960: itkGPUGradientAnisotropicDiffusionImageFilterTest3D
 2/24 Test  #960: itkGPUGradientAnisotropicDiffusionImageFilterTest3D .........   Passed    0.42 sec
...
      Start 3151: itkGPUGradientAnisotropicDiffusionImageFilterPythonTest2D
20/24 Test #3151: itkGPUGradientAnisotropicDiffusionImageFilterPythonTest2D ...   Passed    4.43 sec
      Start 3152: itkGPUGradientAnisotropicDiffusionImageFilterPythonTest3D
21/24 Test #3152: itkGPUGradientAnisotropicDiffusionImageFilterPythonTest3D ...***Failed    0.81 sec
...
The following tests FAILED:
    3152 - itkGPUGradientAnisotropicDiffusionImageFilterPythonTest3D (Failed)
    3157 - itkGPUMeanImageFilterPythonTest2D (Failed)

There are also filters which do not currently have Python wrapping tests; the issue also affects these. For example itkGPUDiscreteGaussianImageFilter:

>>> import numpy as np
>>> import itk
>>> im = itk.GetImageFromArray(np.random.normal(size=(50,50,50)).astype('f'))
>>> gpu_im = itk.cast_image_filter(im, ttype=(itk.Image[itk.F, 3], itk.GPUImage[itk.F, 3]))
Platform  : NVIDIA CUDA
...
>>> gpu_im.UpdateBuffers()
>>> blur = itk.gpu_discrete_gaussian_image_filter(gpu_im, variance=2)
Platform  : NVIDIA CUDA
...
Traceback (most recent call last):
...
  File ".../Wrapping/Generators/Python/itk/ITKCommonBasePython.py", line 3100, in UpdateLargestPossibleRegion
    return _ITKCommonBasePython.itkProcessObject_UpdateLargestPossibleRegion(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: .../ITK/Modules/Core/GPUCommon/include/itkGPUKernelManager.h:130:
OpenCL Error : CL_INVALID_MEM_OBJECT

Note the cast to GPUImage succeeds and the first UpdateBuffers() succeeds. The cast outputs OpenCL platform information. The failure occurs only in filter Update().

Versions

Checked ITK 5.4 and current (as of #4842) master f8d8cf94c9c316cc6a7c4a28d86105951b5ecb06.

Python 3.9.19, 3.12.4

Environment


cc @jcfr since you mentioned interest.

github-actions[bot] commented 2 months ago

Thank you for contributing an issue! 🙏

Welcome to the ITK community! 🤗👋☀️

We are glad you are here and appreciate your contribution. Please keep in mind our community participation guidelines. 📜 Also, please check existing open issues and consider discussion on the ITK Discourse. 📖

This is an automatic message. Allow for time for the ITK community to be able to read the issue and comment on it.