Skip to content

Enable serialization/deserialization of ObjectCode instances #660

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

Merged
merged 7 commits into from
May 29, 2025
Merged
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
8 changes: 8 additions & 0 deletions cuda_core/cuda/core/experimental/_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ def _init(cls, module, code_type, *, symbol_mapping: Optional[dict] = None):

return self

@classmethod
def _reduce_helper(self, module, code_type, symbol_mapping):
# just for forwarding kwargs
return ObjectCode._init(module, code_type, symbol_mapping=symbol_mapping)

def __reduce__(self):
return ObjectCode._reduce_helper, (self._module, self._code_type, self._sym_map)

@staticmethod
def from_cubin(module: Union[bytes, str], *, symbol_mapping: Optional[dict] = None) -> "ObjectCode":
"""Create an :class:`ObjectCode` instance from an existing cubin.
Expand Down
11 changes: 11 additions & 0 deletions cuda_core/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: Apache-2.0

import ctypes
import pickle # nosec B403, B301
import warnings

import pytest
Expand Down Expand Up @@ -245,3 +246,13 @@ def test_num_args_error_handling(deinit_all_contexts_function, cuda12_prerequisi
with pytest.raises(CUDAError):
# assignment resolves linter error "B018: useless expression"
_ = krn.num_arguments


def test_module_serialization_roundtrip(get_saxpy_kernel):
_, objcode = get_saxpy_kernel
result = pickle.loads(pickle.dumps(objcode)) # nosec B403, B301

assert isinstance(result, ObjectCode)
assert objcode.code == result.code
assert objcode._sym_map == result._sym_map
assert objcode._code_type == result._code_type
Loading