This is an automated email from the ASF dual-hosted git repository.

tadeja pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new ca39779fae0 GH-49437: [Python][GPU] Support legacy and native Numba 
CUDA context handles (#50923)
ca39779fae0 is described below

commit ca39779fae059b5a70b4581b2bbedde8b26ab8e3
Author: contentis <[email protected]>
AuthorDate: Wed Nov 25 17:45:11 2026 +0100

    GH-49437: [Python][GPU] Support legacy and native Numba CUDA context 
handles (#50923)
    
    Closes #49437
    
    Numba-CUDA 0.28 changed CUDA context handles from `ctypes.c_void_p` to 
native `cuda.bindings.driver.CUcontext` objects. PyArrow currently expects 
incoming handles to have `.value` and also always returns `ctypes.c_void_p` 
when converting back to Numba.
    
    I added a small compatibility layer which:
    - Accepts both legacy `ctypes` handles and native `CUcontext` objects.
    - Returns the representation expected by the installed Numba implementation.
    - Explicitly tests both representations instead of assuming `.value`.
    - Removes CUDA 11 from the Python CUDA jobs while keeping the existing C++ 
CUDA 11 coverage.
    - Numba and `cuda.bindings` are intentionally imported lazily here since 
they are optional dependencies. Not sure how happy I am with this bit but it 
was the best solution in my opinion.
    
    There is also a temporary `numpy<2.5` constraint for the Numba-CUDA 
environment. Numba-CUDA 0.30.4 currently uses the removed `np.row_stack` 
attribute with NumPy 2.5. ~This is already being handled upstream in 
NVIDIA/numba-cuda#916 and the constraint can be removed once that fix is 
released.~  Edit: Numba-CUDA is in maintenance mode now and upstream won't fix 
this, per 
https://github.com/NVIDIA/numba-cuda/issues/907#issuecomment-5620678407 `NumPy 
<=2.4` is recommended. The pin `numpy< [...]
    
    ### Testing
    I tested the full CUDA Numba interop suite locally with:
    - Numba-CUDA 0.27: 50 passed
    - Numba-CUDA 0.28: 50 passed
    - Numba-CUDA 0.30.4: 50 passed
    
    Cython compilation, linting and ShellCheck also pass.
    * GitHub Issue: #49437
    
    Lead-authored-by: lspindler <[email protected]>
    Co-authored-by: tadeja <[email protected]>
    Signed-off-by: Tadeja Kadunc <[email protected]>
---
 .github/workflows/cuda_extra.yml                |  4 ---
 ci/scripts/install_numba.sh                     |  8 ++++++
 python/pyarrow/_cuda.pyx                        | 38 +++++++++++++++++++++++--
 python/pyarrow/tests/test_cuda_numba_interop.py | 16 +++++++++--
 4 files changed, 56 insertions(+), 10 deletions(-)

diff --git a/.github/workflows/cuda_extra.yml b/.github/workflows/cuda_extra.yml
index 26480c3223b..31e680308aa 100644
--- a/.github/workflows/cuda_extra.yml
+++ b/.github/workflows/cuda_extra.yml
@@ -90,10 +90,6 @@ jobs:
             ubuntu: 24.04
             image: ubuntu-cuda-python
             title: AMD64 Ubuntu 24 CUDA 12.9.0 Python
-          - cuda: 11.7.1
-            ubuntu: 22.04
-            image: ubuntu-cuda-python
-            title: AMD64 Ubuntu 22 CUDA 11.7.1 Python
     env:
       ARCHERY_DEBUG: 1
       ARROW_ENABLE_TIMING_TESTS: OFF
diff --git a/ci/scripts/install_numba.sh b/ci/scripts/install_numba.sh
index 91c36ca5713..29dad6f46c7 100755
--- a/ci/scripts/install_numba.sh
+++ b/ci/scripts/install_numba.sh
@@ -34,6 +34,14 @@ if [ -n "${ARROW_PYTHON_VENV:-}" ]; then
   . "${ARROW_PYTHON_VENV}/bin/activate"
 fi
 
+# Numba-CUDA 0.30.4 uses np.row_stack, which was removed in NumPy 2.5.
+# Upstream recommends NumPy <=2.4, no plan to fix this:
+# https://github.com/NVIDIA/numba-cuda/issues/907#issuecomment-5620678407
+# Drop this pin when numba-cuda support is removed with GH-51204
+if [ "$#" -ge 2 ]; then
+  pip install "numpy<2.5"
+fi
+
 if [ "${numba}" = "master" ]; then
   pip install https://github.com/numba/numba/archive/main.tar.gz#egg=numba
 elif [ "${numba}" = "latest" ]; then
diff --git a/python/pyarrow/_cuda.pyx b/python/pyarrow/_cuda.pyx
index a6d65e517c0..a842ec80076 100644
--- a/python/pyarrow/_cuda.pyx
+++ b/python/pyarrow/_cuda.pyx
@@ -16,6 +16,10 @@
 # under the License.
 
 
+import ctypes
+
+from pyarrow.vendored.version import Version
+
 from pyarrow.lib cimport *
 from pyarrow.includes.libarrow_cuda cimport *
 from pyarrow.lib import allocate_buffer, as_buffer, ArrowTypeError
@@ -23,6 +27,35 @@ from pyarrow.util import get_contiguous_span
 cimport cpython as cp
 
 
+_NUMBA_CUDA_NATIVE_CONTEXT_VERSION = Version("0.28")
+
+
+def _numba_context_handle_value(handle):
+    """Return the integer value of a legacy or native Numba context handle."""
+    # Legacy ctypes.c_void_p handle (built-in Numba CUDA or numba-cuda <0.28)
+    if hasattr(handle, "value"):
+        return handle.value
+    # Native cuda.bindings.driver.CUcontext handle (numba-cuda >=0.28)
+    return int(handle)
+
+
+def _make_numba_context_handle(uintptr_t handle):
+    """Create the context handle representation expected by Numba."""
+    import numba.cuda
+
+    # numba-cuda 0.28 replaced ctypes context handles with CUContext type.
+    if getattr(numba.cuda, "implementation", None) == "NVIDIA":
+        import numba_cuda
+
+        version = Version(numba_cuda.__version__)
+        if version >= _NUMBA_CUDA_NATIVE_CONTEXT_VERSION:
+            from cuda.bindings.driver import CUcontext
+            return CUcontext(handle)
+
+    # Legacy handle for built-in Numba CUDA and numba-cuda <0.28
+    return ctypes.c_void_p(handle)
+
+
 cdef class Context(_Weakrefable):
     """
     CUDA driver context.
@@ -81,7 +114,7 @@ cdef class Context(_Weakrefable):
             import numba.cuda
             context = numba.cuda.current_context()
         return Context(device_number=context.device.id,
-                       handle=context.handle.value)
+                       handle=_numba_context_handle_value(context.handle))
 
     def to_numba(self):
         """
@@ -92,10 +125,9 @@ cdef class Context(_Weakrefable):
         context : numba.cuda.cudadrv.driver.Context
           Numba CUDA context instance.
         """
-        import ctypes
         import numba.cuda
         device = numba.cuda.gpus[self.device_number]
-        handle = ctypes.c_void_p(self.handle)
+        handle = _make_numba_context_handle(self.handle)
         context = numba.cuda.cudadrv.driver.Context(device, handle)
 
         class DummyPendingDeallocs(object):
diff --git a/python/pyarrow/tests/test_cuda_numba_interop.py 
b/python/pyarrow/tests/test_cuda_numba_interop.py
index fa91cd0b490..964b6de74b0 100644
--- a/python/pyarrow/tests/test_cuda_numba_interop.py
+++ b/python/pyarrow/tests/test_cuda_numba_interop.py
@@ -15,6 +15,8 @@
 # specific language governing permissions and limitations
 # under the License.
 
+import ctypes
+
 import pytest
 import pyarrow as pa
 try:
@@ -50,8 +52,16 @@ def teardown_module(module):
                          ids=context_choice_ids)
 def test_context(c):
     ctx, nb_ctx = context_choices[c]
-    assert ctx.handle == nb_ctx.handle.value
-    assert ctx.handle == ctx.to_numba().handle.value
+    converted = ctx.to_numba()
+    assert type(converted.handle) is type(nb_ctx.handle)
+    # Built-in Numba CUDA and numba-cuda <0.28 use ctypes.c_void_p (legacy),
+    # numba-cuda >=0.28 uses cuda.bindings.driver.CUcontext
+    if isinstance(converted.handle, ctypes.c_void_p):
+        assert converted.handle.value == ctx.handle
+    else:
+        from cuda.bindings import driver
+        assert type(converted.handle) is driver.CUcontext
+        assert int(converted.handle) == ctx.handle
     ctx2 = cuda.Context.from_numba(nb_ctx)
     assert ctx.handle == ctx2.handle
     size = 10
@@ -203,7 +213,7 @@ def test_numba_context(c, dtype):
     with nb_cuda.gpus[0]:
         arr, cbuf = make_random_buffer(size, target='device',
                                        dtype=dtype, ctx=ctx)
-        assert cbuf.context.handle == nb_ctx.handle.value
+        assert cbuf.context.handle == cuda.Context.from_numba(nb_ctx).handle
         mem = cbuf.to_numba()
         darr = DeviceNDArray(arr.shape, arr.strides, arr.dtype, gpu_data=mem)
         np.testing.assert_equal(darr.copy_to_host(), arr)

Reply via email to