Skip to content

Implement __hash__ and __eq__ for cuda.core classes - #1198

Merged
rparolin merged 38 commits into
NVIDIA:mainfrom
rparolin:rparolin/cuda.core.hashable
Nov 10, 2025
Merged

Implement __hash__ and __eq__ for cuda.core classes#1198
rparolin merged 38 commits into
NVIDIA:mainfrom
rparolin:rparolin/cuda.core.hashable

Conversation

@rparolin

@rparolin rparolin commented Oct 29, 2025

Copy link
Copy Markdown
Collaborator

Implement __hash__ and __eq__ for cuda.core classes

This PR implements __hash__() and __eq__() methods for cuda.core classes (Stream, Event, Context, Device), enabling them to be used as dictionary keys and in sets.

Fixes #664

…lasses, enabling their use as dictionary keys and in sets.
@copy-pr-bot

copy-pr-bot Bot commented Oct 29, 2025

Copy link
Copy Markdown
Contributor

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.

@rparolin
rparolin requested a review from leofang October 29, 2025 18:32
@rparolin

Copy link
Copy Markdown
Collaborator Author

/ok to test eeef948

@rparolin
rparolin marked this pull request as ready for review October 29, 2025 19:07
@rparolin rparolin added the cuda.core Everything related to the cuda.core module label Oct 29, 2025
@rparolin rparolin added this to the cuda.core beta 9 milestone Oct 29, 2025
@rparolin
rparolin requested a review from mdboom October 29, 2025 19:11
@Andy-Jost

Copy link
Copy Markdown
Contributor

+1 for this change. Longer term, it would be great to achieve this sort of thing with a mix-in class. I think what gets in the way is some internal inconsistency, e.g., using _id versus _handle for handle-like members. Perhaps something to consider if we refactor or reimplement anything.

@github-actions

This comment has been minimized.

Comment thread cuda_core/cuda/core/experimental/_event.pyx Outdated
Comment thread cuda_core/cuda/core/experimental/_event.pyx Outdated
Comment thread cuda_core/cuda/core/experimental/_event.pyx
@rparolin

Copy link
Copy Markdown
Collaborator Author

+1 for this change. Longer term, it would be great to achieve this sort of thing with a mix-in class. I think what gets in the way is some internal inconsistency, e.g., using _id versus _handle for handle-like members. Perhaps something to consider if we refactor or reimplement anything.

What would be the benefit of using a mixin? I feel like it would require a bunch of machinary of parameterize it to the point it solves the problems you pointed out (ie. being able to handle different handle member names)

Co-authored-by: Oleksandr Pavlyk <21087696+oleksandr-pavlyk@users.noreply.github.com>
@rparolin

Copy link
Copy Markdown
Collaborator Author

/ok to test 714091c

@rparolin

Copy link
Copy Markdown
Collaborator Author

/ok to test fff600c

@rparolin

Copy link
Copy Markdown
Collaborator Author

/ok to test 8227c21

@rparolin rparolin added the P1 Medium priority - Should do label Oct 29, 2025
@rparolin rparolin self-assigned this Oct 29, 2025
Comment thread cuda_core/cuda/core/experimental/_device.pyx Outdated
@leofang leofang added the enhancement Any code-related improvements label Oct 29, 2025
@rparolin

Copy link
Copy Markdown
Collaborator Author

/ok to test b8cc00d

Comment thread cuda_core/cuda/core/experimental/_stream.pyx Outdated
Comment thread cuda_core/cuda/core/experimental/_event.pyx Outdated
Comment thread cuda_core/cuda/core/experimental/_stream.pyx
Comment thread cuda_core/cuda/core/experimental/_stream.pyx Outdated
Comment thread cuda_core/cuda/core/experimental/_stream.pyx Outdated
@rparolin

rparolin commented Nov 4, 2025

Copy link
Copy Markdown
Collaborator Author

/ok to test af5c52e

@rparolin

rparolin commented Nov 4, 2025

Copy link
Copy Markdown
Collaborator Author

/ok to test 8c9900d

@rparolin

rparolin commented Nov 4, 2025

Copy link
Copy Markdown
Collaborator Author

/ok to test 95047e4

@rparolin
rparolin requested review from Andy-Jost and leofang November 4, 2025 23:33
Comment thread cuda_core/cuda/core/experimental/_device.pyx Outdated
Comment thread cuda_core/cuda/core/experimental/_device.pyx Outdated
Comment thread cuda_core/cuda/core/experimental/_event.pyx
Comment thread cuda_core/cuda/core/experimental/_context.pyx
Comment on lines +14 to +16
# ============================================================================
# Context Equality Tests
# ============================================================================

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It would probably be difficult, but is there any way we can add a test for when we have different contexts and/or an invalid context in some way?

Comment thread cuda_core/cuda/core/experimental/_stream.pyx

@mdboom mdboom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My comments aren't requirements -- I mostly defer to previous reviewers here.

Comment thread cuda_core/cuda/core/experimental/_context.pyx
Comment thread cuda_core/cuda/core/experimental/_device.pyx Outdated
Comment thread cuda_core/tests/test_comparable.py
@rparolin

rparolin commented Nov 6, 2025

Copy link
Copy Markdown
Collaborator Author

/ok to test ae74733

@rparolin

rparolin commented Nov 6, 2025

Copy link
Copy Markdown
Collaborator Author

/ok to test 98e8fe0

@rparolin
rparolin merged commit 7ca634b into NVIDIA:main Nov 10, 2025
106 of 111 checks passed
@github-actions

Copy link
Copy Markdown
Doc Preview CI
Preview removed because the pull request was closed or merged.

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.

[FEA]: cuda.core constructs which share underlying C structure should hash to same value

8 participants