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

Hash collisions in memoization result in invalid values #4506

Closed
brandonwillard opened this issue Mar 6, 2021 · 0 comments · Fixed by #4509
Closed

Hash collisions in memoization result in invalid values #4506

brandonwillard opened this issue Mar 6, 2021 · 0 comments · Fixed by #4509
Assignees
Labels

Comments

@brandonwillard
Copy link
Contributor

brandonwillard commented Mar 6, 2021

Our custom memoize function is mapping inputs to their hashes, then those hashes are mapped to cached values, and, because the inputs and the hashes' original values aren't compared, a simple hash collision will cause memoize to return the wrong values.

Here's a simple example:

import pymc3 as pm


class Bad1:
    def __hash__(self):
        return 329


class Bad2:
    def __hash__(self):
        return 329


b1 = Bad1()
b2 = Bad2()


# Here's what we expect:
d = {}

d[b1] = 1
d[b2] = 2

assert hash(b1) == hash(b2)
assert d[b1] != d[b2]

# Because `memoize` doesn't use the inputs as keys in the `dict`-based
# cache, we don't get the expected post-hash-lookup equality checks that
# prevent errors when faced with hash collisions

@pm.memoize.memoize
def test_func(x):
    return x


assert b1 != b2
assert test_func(b1) != test_func(b2)

The last assert will currently fail, although it should be equivalent to the assert preceding it.

@brandonwillard brandonwillard pinned this issue Mar 6, 2021
@brandonwillard brandonwillard changed the title Hash collisions in memoization Hash collisions in memoization result in invalid values Mar 6, 2021
@ricardoV94 ricardoV94 unpinned this issue Mar 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants