Skip to content

LoopyKeyBuilder: improve BasicSet handling #913

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions loopy/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ class LoopyKeyBuilder(KeyBuilderBase):
update_for_dict = KeyBuilderBase.update_for_constantdict
update_for_defaultdict = KeyBuilderBase.update_for_constantdict

def update_for_BasicSet(self, key_hash, key): # noqa
from islpy import Printer
prn = Printer.to_str(key.get_ctx())
getattr(prn, "print_"+key._base_name)(key)
key_hash.update(prn.get_str().encode("utf8"))
def update_for_BasicSet(self, key_hash, key): # noqa: N802
key_hash.update(str(type(key)).encode("utf-8"))
self.rec(key_hash, frozenset(key.get_var_dict().keys()))

def update_for_Map(self, key_hash, key): # noqa
constraints = set()
for constraint in key.get_constraints():
constraints.add(str(constraint).partition("->")[-1])

self.rec(key_hash, frozenset(constraints))

def update_for_Map(self, key_hash, key): # noqa: N802
if isinstance(key, isl.Map):
self.update_for_BasicSet(key_hash, key)
else:
Expand Down
25 changes: 25 additions & 0 deletions test/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,31 @@ def test_memoize_on_disk_with_pym_expr():
assert cached_result == uncached_result


def test_basicset_keybuilder():
# See https://github.com/inducer/loopy/issues/912 for context
import islpy as isl

# Both sets have the same variables and constraints, but in different order.
# These sets are generated in test_convolution() in test_apps.py
a = isl.BasicSet("[im_w, im_h, nimgs, nfeats] -> "
"{ : im_w >= 7 and im_h >= 7 and nimgs >= 0 and nfeats > 0 }")

b = isl.BasicSet("[nfeats, nimgs, im_h, im_w] -> "
"{ : nfeats > 0 and nimgs >= 0 and im_h >= 7 and im_w >= 7 }")

from loopy.tools import LoopyKeyBuilder

# Equality
assert a == b
assert a.is_equal(b)
assert not a.plain_is_equal(b)

# Hashing
assert hash(a) != hash(b)
assert a.get_hash() != b.get_hash()
assert LoopyKeyBuilder()(a) == LoopyKeyBuilder()(b)
Comment on lines +355 to +363
Copy link
Owner

Choose a reason for hiding this comment

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

This behavior is pretty 💩-tastic. I'm not super interested in codifying it with a test.

If I could snap my finger and have a solution enacted globally, probably I would like ==, plain_is_equal and __hash__ to all share a notion of equality. Something for namedisl to aspire to.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Aside from the test, would you still be interested in changing the way the hash is calculated, as in this PR?

Copy link
Owner

Choose a reason for hiding this comment

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

I think namedisl is the only reasonable path forward. I don't know that I'd like to mess with the status quo too much until that time, unless there is a pressing reason. Is there one, in the context of this PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think there is a pressing need for this PR. The goal was to enable running the tests with LOOPY_ABORT_ON_CACHE_MISS (#828).



if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
Expand Down
Loading