NVIDIA / NVIDIA/cccl

[BUG]: Hash functions in classes defined in python/cuda_parallel need to mix in class type information

Open
#3,787 1 comment 0 reactions 0 assignees View on GitHub
cuda.compute
Dominant language
C++
Stars
2.5k
Forks
487
Avg merge
2d 7h
Merged PRs (30d)
296

Description

### Is this a duplicate?

- [x] I confirmed there appear to be no [duplicate issues](https://github.com/NVIDIA/cccl/issues) for this bug and that I agree to the [Code of Conduct](CODE_OF_CONDUCT.md)

### Type of Bug

Something else

### Component

cuda.parallel (Python)

### Describe the bug

Hash functions defined for classes that implement `cuda.parallel` has the content of the class, but do not mix the class type into the produced hash which invites hash collisions with the content itself. This toy reproduce illustrates the problem:

```
n [35]: class A:
...: def __init__(self, n : int):
...: self._value = n
...: def __eq__(self, other):
...: return type(self) == type(other) and (self._value == other._value)
...: def __hash__(self):
...: return hash((self.__class__, self._value))
...:

In [36]: class B:
...: def __init__(self, n : int):
...: self._value = n
...: def __eq__(self, other):
...: return type(self) == type(other) and (self._value == other._value)
...: def __hash__(self):
...: return hash(self._value)
...:

In [37]: hash(A(3)), hash(3)
Out[37]: (9201676693010777463, 3)

In [38]: hash(B(3)), hash(3)
Out[38]: (3, 3)
```

`cuda.parallel` currently does what `class B` does, and needs to be modified to do what `class A` does instead.

### How to Reproduce

```
In [8]: import cuda.parallel.experimental.iterators._iterators as m_iter

In [9]: import numpy as np

In [10]: o1 = m_iter.IteratorKind(np.int32)

In [11]: hash(o1), hash(np.int32)
Out[11]: (8025086715740, 8025086715740)
```

### Expected behavior

Expecting hashes to be different.

### Reproduction link

_No response_

### Operating System

_No response_

### nvidia-smi output

_No response_

### NVCC version

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.