Skip to content

Commit 0f3a014

Browse files
committed
Fix backwards compatibility in ScalarOp hash
1 parent ee107cb commit 0f3a014

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

pytensor/scalar/basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ def __eq__(self, other):
13001300
return test
13011301

13021302
def __hash__(self):
1303-
return hash((type(self), getattr(self, "output_types_preference", 0)))
1303+
return hash((type(self), getattr(self, "output_types_preference", None)))
13041304

13051305
def __str__(self):
13061306
if hasattr(self, "name") and self.name:

tests/scalar/test_basic.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pytensor.graph.fg import FunctionGraph
99
from pytensor.link.c.basic import DualLinker
1010
from pytensor.scalar.basic import (
11+
EQ,
1112
ComplexError,
1213
Composite,
1314
InRange,
@@ -543,3 +544,18 @@ def test_grad_log10():
543544
b_grad = pytensor.gradient.grad(b, a)
544545
assert b.dtype == "float32"
545546
assert b_grad.dtype == "float32"
547+
548+
549+
def test_scalar_hash_default_output_type_preference():
550+
# Old hash used `getattr(self, "output_type_preference", 0)`
551+
# whereas equality used `getattr(self, "output_type_preference", None)`.
552+
# Since 27d797076668fbf0617654fd9b91f92ddb6737e6,
553+
# output_type_preference is always present (None if not specified),
554+
# which led to C-caching errors when comparing old cached Ops and fresh Ops ones
555+
# as they evaluated equal but hashed differently
556+
557+
new_eq = EQ()
558+
old_eq = EQ()
559+
del old_eq.output_types_preference # mimic old Op
560+
assert new_eq == old_eq
561+
assert hash(new_eq) == hash(old_eq)

0 commit comments

Comments
 (0)