Skip to content
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

Hashing of jitclass doesn't work #4002

Open
louisabraham opened this issue Apr 21, 2019 · 3 comments
Open

Hashing of jitclass doesn't work #4002

louisabraham opened this issue Apr 21, 2019 · 3 comments

Comments

@louisabraham
Copy link

louisabraham commented Apr 21, 2019

Reporting a bug

This is my code

import numba
from numba import njit, jitclass


@jitclass([("a", numba.int64), ("b", numba.int64)])
class A:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def __hash__(self):
        return self.a + self.b


print(A.class_type())

x = A(1, 2)
y = A(1, 2)

print(hash(x))
print(hash(y))

It appears that:

  • the override of __hash__ doesn't work
  • the hash is not the same even for objects that are the same (like in Python)

Is there a way to get a value-dependent hashing?

Or is there a way to subclass namedtuples and still compile?

@stuartarchibald
Copy link
Contributor

Thanks for the report.

It appears that:

  • the override of __hash__ doesn't work
  • the hash is not the same even for objects that are the same (like in Python)

I think this needs qualifying, it seems like the override of the __hash__ method does not work as expected from the CPython interpreter, it does however work fine from compiled code, e.g.:

import numba
from numba import njit, jitclass


@jitclass([("a", numba.int64), ("b", numba.int64)])
class A:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def __hash__(self):
        return self.a + self.b


@njit
def printhash(z):
    print(hash(z))

print(A.class_type())

x = A(1, 2)
y = A(1, 2)

print(hash(x))
print(hash(y))

printhash(x)
printhash(y)

gives:

() -> jitclass.A#55d3d1eac8c8<a:int64,b:int64>
8766512762915
8766512086525
3
3

I think that this works because the compiled call to hash() forces resolution of the method on the compiled class whereas calling hash() on the jitclass instance object does not.

@sklam any thoughts on whether we ought to support this?

@louisabraham
Copy link
Author

Interesting! Does it mean that code relying on dicts won't produce the same results after compilation?

@sklam
Copy link
Member

sklam commented Apr 23, 2019

This should be a quick one. It should just be a matter for the interface not being exported. I will need to check on this.

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

No branches or pull requests

3 participants