Commit 9bfeccb
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: 1d74340b3dbafac698c8342e0a18d6ef24e8fc401 parent 16e8323 commit 9bfeccb
1 file changed
Lines changed: 0 additions & 21 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5502 | 5502 | | |
5503 | 5503 | | |
5504 | 5504 | | |
5505 | | - | |
5506 | | - | |
5507 | | - | |
5508 | | - | |
5509 | | - | |
5510 | | - | |
5511 | | - | |
5512 | | - | |
5513 | | - | |
5514 | | - | |
5515 | | - | |
5516 | | - | |
5517 | | - | |
5518 | | - | |
5519 | | - | |
5520 | | - | |
5521 | | - | |
5522 | | - | |
5523 | | - | |
5524 | | - | |
5525 | | - | |
5526 | 5505 | | |
5527 | 5506 | | |
5528 | 5507 | | |
| |||
0 commit comments