Skip to content

Commit 9bfeccb

Browse files
williamwen42meta-codesync[bot]
authored andcommitted
mimic tp_hash handling (#181328)
Summary: Add user-facing hash() / __hash__() support to torch.compile by implementing CPython's PyObject_Hash dispatch on Dynamo's VariableTracker hierarchy. - Add hash_impl(self, tx) -> (int, bool) slot on each VT subclass, mirroring CPython's tp_hash. The bool tracks whether the hash is "fake" (identity-based on a sourceless object). - Add generic_hash / generic_hash_impl dispatch in object_protocol.py, call_hash in builtin.py, and __hash__ routing in call_method. - Simplify HashableTracker to delegate to generic_hash_impl instead of the old is_python_hashable() / get_python_hash() per-VT methods (removed from all VTs). - UserDefinedObjectVariable.hash_impl walks the MRO to trace Python __hash__, call known-safe C hashes, or graph-break on unknown C tp_hash with a hint to use allow_c_hash(). - Non-constant __hash__ returns are handled for FakeIdVariable (propagates is_fake) and SymNodeVariable (specializes via evaluate_expr()). - Add torch._dynamo.allow_c_hash(tp) API for registering C extension types whose tp_hash is safe to call at trace time. Built-in allowlist covers datetime, decimal.Decimal, and re.Pattern. X-link: pytorch/pytorch#181328 Approved by: https://github.com/anijain2305 Reviewed By: huydhn Differential Revision: D104082788 fbshipit-source-id: 1d74340b3dbafac698c8342e0a18d6ef24e8fc40
1 parent 16e8323 commit 9bfeccb

1 file changed

Lines changed: 0 additions & 21 deletions

File tree

  • userbenchmark/dynamo/dynamobench/_dynamo

userbenchmark/dynamo/dynamobench/_dynamo/utils.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5502,27 +5502,6 @@ def is_pybind11_enum_member(value: Any) -> bool:
55025502
return name is not None and members.get(name) is value
55035503

55045504

5505-
def raise_on_overridden_hash(obj: Any, vt: VariableTracker) -> None:
5506-
from . import graph_break_hints
5507-
from .exc import unimplemented
5508-
5509-
is_overridden = type(obj).__dict__.get("__hash__", False)
5510-
5511-
if is_overridden and is_pybind11_enum_member(obj):
5512-
return
5513-
5514-
if is_overridden:
5515-
unimplemented(
5516-
gb_type="User-defined object with overridden __hash__",
5517-
context=f"hashing object of type={type(obj)} and variable tracker {vt}",
5518-
explanation=f"Found a user-defined object {vt} with overridden __hash__ when attempting to hash it",
5519-
hints=[
5520-
"Dynamo does not support hashing user-defined objects with overridden __hash__",
5521-
*graph_break_hints.SUPPORTABLE,
5522-
],
5523-
)
5524-
5525-
55265505
def _make_inlined(
55275506
tx: InstructionTranslator, f: Callable[..., Any]
55285507
) -> Callable[..., VariableTracker]:

0 commit comments

Comments
 (0)