Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cuda_core/cuda/core/_module.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,16 @@ class ObjectCode:

"""

def get_module(self) -> object:
"""Return the :obj:`~driver.CUmodule` associated with this object code.

Returns
-------
:obj:`~driver.CUmodule`
Module handle for the current CUDA context, suitable for legacy
driver APIs that accept ``CUmodule``.
"""

@property
def code(self) -> CodeTypeT:
"""Return the underlying code object."""
Expand Down
16 changes: 16 additions & 0 deletions cuda_core/cuda/core/_module.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ from __future__ import annotations

cimport cython
from libc.stddef cimport size_t
from libc.stdint cimport intptr_t

from collections import namedtuple
from os import fsencode, fspath, PathLike
Expand Down Expand Up @@ -796,6 +797,21 @@ cdef class ObjectCode:
HANDLE_RETURN(get_last_error())
return Kernel._from_handle(h_kernel)

def get_module(self) -> object:
"""Return the :obj:`~driver.CUmodule` associated with this object code.

Returns
-------
:obj:`~driver.CUmodule`
Module handle for the current CUDA context, suitable for legacy
driver APIs that accept ``CUmodule``.
"""
self._lazy_load_module()
cdef cydriver.CUmodule mod
with nogil:
HANDLE_RETURN(cydriver.cuLibraryGetModule(&mod, as_cu(self._h_library)))
return driver.CUmodule(<intptr_t>mod)

@property
def code(self) -> CodeTypeT:
"""Return the underlying code object."""
Expand Down
15 changes: 15 additions & 0 deletions cuda_core/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,21 @@ def test_get_kernel(init_cuda):
assert object_code.get_kernel(b"ABC").handle is not None


def test_object_code_get_module_for_legacy_integration(init_cuda):
src = """
extern "C" __global__ void ABC() { }
extern "C" __global__ void DEF() { }
"""
object_code = Program(src, "c++").compile("cubin")

# Bridge: CUlibrary (new) → CUmodule (legacy)
module = object_code.get_module()

# Legacy module-only API consumes it directly
count = handle_return(driver.cuModuleGetFunctionCount(module))
assert count == 2


@pytest.mark.parametrize(
"attr, expected_type",
[
Expand Down
Loading