-
Notifications
You must be signed in to change notification settings - Fork 262
Add pickle/cloudpickle regression tests for cuda.core objects #1810
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
+67
−3
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """ | ||
| Tests for Python object protocols (__eq__, __hash__, __weakref__, __repr__). | ||
| Tests for Python object protocols (__eq__, __hash__, __weakref__, __repr__, pickle). | ||
|
|
||
| This module tests that core cuda.core classes properly implement standard Python | ||
| object protocols for identity, hashing, weak references, and string representation. | ||
| object protocols for identity, hashing, weak references, string representation, | ||
| and serialization. | ||
| """ | ||
|
|
||
| import itertools | ||
|
|
@@ -15,7 +16,17 @@ | |
| from helpers.graph_kernels import compile_common_kernels | ||
| from helpers.misc import try_create_condition | ||
|
|
||
| from cuda.core import Buffer, Device, Kernel, LaunchConfig, Program, Stream, system | ||
| from cuda.core import ( | ||
| Buffer, | ||
| Device, | ||
| DeviceMemoryResource, | ||
| DeviceMemoryResourceOptions, | ||
| Kernel, | ||
| LaunchConfig, | ||
| Program, | ||
| Stream, | ||
| system, | ||
| ) | ||
| from cuda.core._graph._graphdef import GraphDef | ||
| from cuda.core._program import _can_load_generated_ptx | ||
|
|
||
|
|
@@ -208,6 +219,30 @@ def sample_kernel_alt(sample_object_code_alt): | |
| return sample_object_code_alt.get_kernel("test_kernel_alt") | ||
|
|
||
|
|
||
| # ============================================================================= | ||
| # Fixtures - IPC samples (for pickle tests) | ||
| # ============================================================================= | ||
|
|
||
| POOL_SIZE = 2097152 | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sample_ipc_buffer_descriptor(ipc_device): | ||
| """An IPCBufferDescriptor.""" | ||
| options = DeviceMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True) | ||
| mr = DeviceMemoryResource(ipc_device, options=options) | ||
| buf = mr.allocate(64) | ||
| return buf.get_ipc_descriptor() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sample_ipc_event_descriptor(ipc_device): | ||
| """An IPCEventDescriptor.""" | ||
| stream = ipc_device.create_stream() | ||
| e = stream.record(options={"ipc_enabled": True}) | ||
| return e.get_ipc_descriptor() | ||
|
|
||
|
|
||
| # ============================================================================= | ||
| # Fixtures - Graph types (GraphDef and GraphNode) | ||
| # ============================================================================= | ||
|
|
@@ -606,6 +641,20 @@ def sample_switch_node_alt(sample_graphdef): | |
| ("sample_kernel", lambda k: Kernel.from_handle(int(k.handle))), | ||
| ] | ||
|
|
||
| # Types with __reduce__ support (pickle/cloudpickle). | ||
| # Event, Buffer, and memory resources are excluded: Event only supports | ||
| # IPC-based serialization via multiprocessing reduction; Buffer and memory | ||
| # resource __reduce__ use a cross-process registry that doesn't support | ||
| # same-process roundtrips. | ||
| PICKLE_TYPES = [ | ||
| "sample_device", | ||
| "sample_object_code_cubin", | ||
| "sample_ipc_buffer_descriptor", | ||
| "sample_ipc_event_descriptor", | ||
| ] | ||
|
|
||
| PICKLE_MODULES = ["pickle", "cloudpickle"] | ||
|
Collaborator
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.
Contributor
Author
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. I will follow up in #1811 |
||
|
|
||
| # Derived type groupings for collection tests | ||
| DICT_KEY_TYPES = sorted(set(HASH_TYPES) & set(EQ_TYPES)) | ||
| WEAK_KEY_TYPES = sorted(set(HASH_TYPES) & set(EQ_TYPES) & set(WEAKREF_TYPES)) | ||
|
|
@@ -796,3 +845,18 @@ def test_repr_format(fixture_name, pattern, request): | |
| obj = request.getfixturevalue(fixture_name) | ||
| result = repr(obj) | ||
| assert re.fullmatch(pattern, result) | ||
|
|
||
|
|
||
| # ============================================================================= | ||
| # Pickle tests | ||
| # ============================================================================= | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("pickle_module", PICKLE_MODULES) | ||
| @pytest.mark.parametrize("fixture_name", PICKLE_TYPES) | ||
| def test_pickle_roundtrip(fixture_name, pickle_module, request): | ||
| """Object survives a pickle/cloudpickle roundtrip.""" | ||
| mod = pytest.importorskip(pickle_module) | ||
| obj = request.getfixturevalue(fixture_name) | ||
| result = mod.loads(mod.dumps(obj)) | ||
| assert type(result) is type(obj) | ||
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.
Nice-to-have: a comment to explain why this value was chosen.