Skip to content

Conversation

@Andy-Jost
Copy link
Contributor

Summary

Complete hashability support for core API classes by adding __eq__ and __hash__ to Buffer, LaunchConfig, Kernel, and ObjectCode.

Changes

  • Add __eq__ and __hash__ to Buffer, LaunchConfig, Kernel, ObjectCode
  • Remove type(self) from __hash__ in Event, Context (and the new classes) - CUDA handles are already unique, and this maintains the hash/equality contract for subclasses
  • Add _LAUNCH_CONFIG_ATTRS tuple for forward-compatible identity in LaunchConfig
  • Update __repr__ in LaunchConfig to use the same attribute list
  • Add parameterized hash tests for all 8 core API classes

Identity basis:

Class Identity
Buffer (ptr, size)
LaunchConfig (grid, cluster, block, shmem_size, cooperative_launch)
Kernel handle
ObjectCode handle (triggers lazy load)

Stream, Event, Context, Device already had __eq__/__hash__.

Test Plan

  • New parameterized tests in test_hashable.py cover all 8 core classes
  • All existing hash/equality tests pass
  • Full test suite passes locally (1599 tests)

Make these classes hashable and comparable:
- Buffer: identity based on (type, ptr, size)
- LaunchConfig: uses _LAUNCH_CONFIG_ATTRS tuple for forward-compatible
  identity; also updates __repr__ to use the same attribute list
- Kernel: identity based on (type, handle)
- ObjectCode: identity based on (type, handle), triggers lazy load

Stream, Event, Context, Device already had __eq__/__hash__.
Make these classes hashable and comparable:
- Buffer: identity based on (ptr, size)
- LaunchConfig: uses _LAUNCH_CONFIG_ATTRS tuple for forward-compatible
  identity; also updates __repr__ to use the same attribute list
- Kernel: identity based on handle
- ObjectCode: identity based on handle, triggers lazy load

Remove type(self) from __hash__ in Event, Context, Buffer, LaunchConfig,
Kernel, ObjectCode - CUDA handles are already unique, and removing
type(self) maintains the hash/equality contract for subclasses.

Add parameterized hash tests for all 8 core API classes.

Stream, Device already had correct __eq__/__hash__ without type(self).
@Andy-Jost Andy-Jost added this to the cuda.core beta 12 milestone Jan 25, 2026
@Andy-Jost Andy-Jost added enhancement Any code-related improvements P1 Medium priority - Should do cuda.core Everything related to the cuda.core module labels Jan 25, 2026
@Andy-Jost Andy-Jost self-assigned this Jan 25, 2026
@copy-pr-bot
Copy link
Contributor

copy-pr-bot bot commented Jan 25, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@Andy-Jost
Copy link
Contributor Author

/ok to test e182f4c

Comment on lines +25 to +41
def sample_device(init_cuda):
return Device()


@pytest.fixture
def sample_stream(sample_device):
return sample_device.create_stream()


@pytest.fixture
def sample_event(sample_device):
return sample_device.create_event()


@pytest.fixture
def sample_context(sample_device):
return sample_device.context
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once this change and the change adding __weakref__ members are both merged, I plan to revisit these fixtures (which overlap with test_weakref.py). There is an opportunity to rewrite and simplify test_hashable.py, test_comparable.py and test_weakref.py, possibly combining them.

@github-actions
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.core Everything related to the cuda.core module enhancement Any code-related improvements P1 Medium priority - Should do

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant