Repository navigation
GH-49437: [Python][GPU] Support legacy and native Numba CUDA context handles #50923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
contentis
wants to merge
1
commit into
apache:main
Choose a base branch
from
contentis:GH-49437-numba-context-handles
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,43 @@ | |
| # 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 | ||
| 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.""" | ||
| if hasattr(handle, "value"): | ||
| return handle.value | ||
| 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) | ||
|
|
||
| return ctypes.c_void_p(handle) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the legacy path, right? |
||
|
|
||
|
|
||
| cdef class Context(_Weakrefable): | ||
| """ | ||
| CUDA driver context. | ||
|
|
@@ -81,7 +111,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 +122,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): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,14 @@ 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) | ||
| if isinstance(converted.handle, ctypes.c_void_p): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here as well, can you comment on which path corresponds to which Numba version(s)? |
||
| 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 +211,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) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add comments telling which path is legacy or native?