Skip to content

[dynamo][dicts] Decentralize and Improve key hash implementation for Dict variable tracker#169204

Closed
anijain2305 wants to merge 15 commits intogh/anijain2305/963/basefrom
gh/anijain2305/963/head
Closed

[dynamo][dicts] Decentralize and Improve key hash implementation for Dict variable tracker#169204
anijain2305 wants to merge 15 commits intogh/anijain2305/963/basefrom
gh/anijain2305/963/head

Conversation

@anijain2305
Copy link
Contributor

@anijain2305 anijain2305 commented Nov 28, 2025

Stack from ghstack (oldest at bottom):

Fixes #167956

Summary

This PR decentralizes and improves the hash implementation for dictionary keys in Dynamo's ConstDictVariable tracker. Instead of maintaining a centralized list of hashable types and custom equality logic in
_HashableTracker, we now delegate hashability checks, hash computation, and equality comparison to individual VariableTracker subclasses.

Motivation

The previous implementation had several issues:

  1. Centralized logic: All hashability checks and hash computations were centralized in dicts.py, making it difficult to add support for new hashable types
  2. Maintainability: Adding a new hashable type required modifying multiple locations in _HashableTracker (underlying_value, _eq_impl, and the is_hashable function)
  3. Scattered knowledge: Type-specific hashing logic was separated from the type's own implementation
  4. Limited extensibility: No clear protocol for VariableTracker subclasses to declare themselves as hashable

Changes

New Protocol Methods

Added three new methods to the VariableTracker base class:

  1. is_python_hashable(): Returns whether the underlying Python object is hashable
  2. get_python_hash(): Computes the hash value for the underlying Python object
  3. is_python_equal(other): Checks Python-level equality between two VariableTrackers

The base implementation raises unimplemented() with helpful error messages, and subclasses override these methods as appropriate.

Simplified _HashableTracker

The _HashableTracker class in ConstDictVariable is now much simpler:

  • Removed underlying_value property (centralized type handling)
  • Removed _eq_impl static method (centralized equality logic)
  • Simplified hash() to delegate to vt.get_python_hash()
  • Simplified eq() to delegate to vt.is_python_equal()

Decentralized Implementations

Implemented the new protocol methods across relevant VariableTracker subclasses:

  • ConstantVariable, TensorVariable, TupleVariable, ListVariable
  • FrozensetVariable, FrozenDataClassVariable
  • BuiltinVariable, UserFunctionVariable, SkipFunctionVariable
  • FunctoolsPartialVariable, WeakRefVariable
  • NumpyVariable, NNModuleVariable, MethodWrapperVariable
  • TorchInGraphFunctionVariable, TorchHigherOrderOperatorVariable
  • TypingVariable, UserDefinedObjectVariable, UserDefinedClassVariable
  • SymNodeVariable, EnumVariable

Enhanced Test Coverage

Added 14 new test cases covering various hashable types as dictionary keys:

  • range, tuples, enums, frozensets
  • Typing constructs (e.g., typing.Union)
  • NumPy dtypes, method wrappers
  • Torch builtin functions, frozen dataclasses
  • Custom objects with hash
  • Negative test for unhashable types (lists)

Improved Error Messages

Updated error messages to be more informative when encountering unhashable types, showing both the Python type and the VariableTracker type.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @Lucaskabela @jataylo

@anijain2305 anijain2305 requested a review from zou3519 as a code owner November 28, 2025 04:00
@pytorch-bot
Copy link

pytorch-bot bot commented Nov 28, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/169204

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit 867cb44 with merge base 390790b (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

anijain2305 added a commit that referenced this pull request Nov 28, 2025
…Dict variable tracker

ghstack-source-id: e4d9ee5
Pull Request resolved: #169204
@anijain2305 anijain2305 added topic: not user facing topic category ciflow/trunk Trigger trunk jobs on your pull request labels Nov 28, 2025
…tation for Dict variable tracker"

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Nov 28, 2025
…Dict variable tracker

ghstack-source-id: 232d69d
Pull Request resolved: #169204
…tation for Dict variable tracker"

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Nov 28, 2025
…Dict variable tracker

ghstack-source-id: 3b267a0
Pull Request resolved: #169204
…tation for Dict variable tracker"

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Nov 28, 2025
…Dict variable tracker

ghstack-source-id: 990230c
Pull Request resolved: #169204
@anijain2305 anijain2305 added the keep-going Don't stop on first failure, keep running tests until the end label Nov 29, 2025
…tation for Dict variable tracker"

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Nov 29, 2025
…Dict variable tracker

ghstack-source-id: d257474
Pull Request resolved: #169204
…tation for Dict variable tracker"

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Nov 29, 2025
…Dict variable tracker

ghstack-source-id: 1110f3a
Pull Request resolved: #169204
…tation for Dict variable tracker"

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Nov 29, 2025
…Dict variable tracker

ghstack-source-id: 91bec51
Pull Request resolved: #169204
…tation for Dict variable tracker"

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo

[ghstack-poisoned]
@anijain2305
Copy link
Contributor Author

@pytorchbot merge -i

anijain2305 added a commit that referenced this pull request Dec 4, 2025
…Dict variable tracker

ghstack-source-id: e8a423e
Pull Request resolved: #169204
…tation for Dict variable tracker"


Fixes #167956

## Summary

  This PR decentralizes and improves the hash implementation for dictionary keys in Dynamo's ConstDictVariable tracker. Instead of maintaining a centralized list of hashable types and custom equality logic in
  _HashableTracker, we now delegate hashability checks, hash computation, and equality comparison to individual VariableTracker subclasses.

## Motivation

  The previous implementation had several issues:

  1. Centralized logic: All hashability checks and hash computations were centralized in dicts.py, making it difficult to add support for new hashable types
  2. Maintainability: Adding a new hashable type required modifying multiple locations in _HashableTracker (underlying_value, _eq_impl, and the is_hashable function)
  3. Scattered knowledge: Type-specific hashing logic was separated from the type's own implementation
  4. Limited extensibility: No clear protocol for VariableTracker subclasses to declare themselves as hashable

## Changes

  New Protocol Methods

  Added three new methods to the VariableTracker base class:

  1. is_python_hashable(): Returns whether the underlying Python object is hashable
  2. get_python_hash(): Computes the hash value for the underlying Python object
  3. is_python_equal(other): Checks Python-level equality between two VariableTrackers

  The base implementation raises unimplemented() with helpful error messages, and subclasses override these methods as appropriate.

## Simplified _HashableTracker

  The _HashableTracker class in ConstDictVariable is now much simpler:

  - Removed underlying_value property (centralized type handling)
  - Removed _eq_impl static method (centralized equality logic)
  - Simplified __hash__() to delegate to vt.get_python_hash()
  - Simplified __eq__() to delegate to vt.is_python_equal()

## Decentralized Implementations

  Implemented the new protocol methods across relevant VariableTracker subclasses:

  - ConstantVariable, TensorVariable, TupleVariable, ListVariable
  - FrozensetVariable, FrozenDataClassVariable
  - BuiltinVariable, UserFunctionVariable, SkipFunctionVariable
  - FunctoolsPartialVariable, WeakRefVariable
  - NumpyVariable, NNModuleVariable, MethodWrapperVariable
  - TorchInGraphFunctionVariable, TorchHigherOrderOperatorVariable
  - TypingVariable, UserDefinedObjectVariable, UserDefinedClassVariable
  - SymNodeVariable, EnumVariable

## Enhanced Test Coverage

  Added 14 new test cases covering various hashable types as dictionary keys:

  - range, tuples, enums, frozensets
  - Typing constructs (e.g., typing.Union)
  - NumPy dtypes, method wrappers
  - Torch builtin functions, frozen dataclasses
  - Custom objects with __hash__
  - Negative test for unhashable types (lists)

## Improved Error Messages

  Updated error messages to be more informative when encountering unhashable types, showing both the Python type and the VariableTracker type.



cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Dec 4, 2025
…Dict variable tracker

ghstack-source-id: e8a423e
Pull Request resolved: #169204
…tation for Dict variable tracker"


Fixes #167956

## Summary

  This PR decentralizes and improves the hash implementation for dictionary keys in Dynamo's ConstDictVariable tracker. Instead of maintaining a centralized list of hashable types and custom equality logic in
  _HashableTracker, we now delegate hashability checks, hash computation, and equality comparison to individual VariableTracker subclasses.

## Motivation

  The previous implementation had several issues:

  1. Centralized logic: All hashability checks and hash computations were centralized in dicts.py, making it difficult to add support for new hashable types
  2. Maintainability: Adding a new hashable type required modifying multiple locations in _HashableTracker (underlying_value, _eq_impl, and the is_hashable function)
  3. Scattered knowledge: Type-specific hashing logic was separated from the type's own implementation
  4. Limited extensibility: No clear protocol for VariableTracker subclasses to declare themselves as hashable

## Changes

  New Protocol Methods

  Added three new methods to the VariableTracker base class:

  1. is_python_hashable(): Returns whether the underlying Python object is hashable
  2. get_python_hash(): Computes the hash value for the underlying Python object
  3. is_python_equal(other): Checks Python-level equality between two VariableTrackers

  The base implementation raises unimplemented() with helpful error messages, and subclasses override these methods as appropriate.

## Simplified _HashableTracker

  The _HashableTracker class in ConstDictVariable is now much simpler:

  - Removed underlying_value property (centralized type handling)
  - Removed _eq_impl static method (centralized equality logic)
  - Simplified __hash__() to delegate to vt.get_python_hash()
  - Simplified __eq__() to delegate to vt.is_python_equal()

## Decentralized Implementations

  Implemented the new protocol methods across relevant VariableTracker subclasses:

  - ConstantVariable, TensorVariable, TupleVariable, ListVariable
  - FrozensetVariable, FrozenDataClassVariable
  - BuiltinVariable, UserFunctionVariable, SkipFunctionVariable
  - FunctoolsPartialVariable, WeakRefVariable
  - NumpyVariable, NNModuleVariable, MethodWrapperVariable
  - TorchInGraphFunctionVariable, TorchHigherOrderOperatorVariable
  - TypingVariable, UserDefinedObjectVariable, UserDefinedClassVariable
  - SymNodeVariable, EnumVariable

## Enhanced Test Coverage

  Added 14 new test cases covering various hashable types as dictionary keys:

  - range, tuples, enums, frozensets
  - Typing constructs (e.g., typing.Union)
  - NumPy dtypes, method wrappers
  - Torch builtin functions, frozen dataclasses
  - Custom objects with __hash__
  - Negative test for unhashable types (lists)

## Improved Error Messages

  Updated error messages to be more informative when encountering unhashable types, showing both the Python type and the VariableTracker type.



cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Dec 4, 2025
…Dict variable tracker

ghstack-source-id: 72993ed
Pull Request resolved: #169204
…tation for Dict variable tracker"


Fixes #167956

## Summary

  This PR decentralizes and improves the hash implementation for dictionary keys in Dynamo's ConstDictVariable tracker. Instead of maintaining a centralized list of hashable types and custom equality logic in
  _HashableTracker, we now delegate hashability checks, hash computation, and equality comparison to individual VariableTracker subclasses.

## Motivation

  The previous implementation had several issues:

  1. Centralized logic: All hashability checks and hash computations were centralized in dicts.py, making it difficult to add support for new hashable types
  2. Maintainability: Adding a new hashable type required modifying multiple locations in _HashableTracker (underlying_value, _eq_impl, and the is_hashable function)
  3. Scattered knowledge: Type-specific hashing logic was separated from the type's own implementation
  4. Limited extensibility: No clear protocol for VariableTracker subclasses to declare themselves as hashable

## Changes

  New Protocol Methods

  Added three new methods to the VariableTracker base class:

  1. is_python_hashable(): Returns whether the underlying Python object is hashable
  2. get_python_hash(): Computes the hash value for the underlying Python object
  3. is_python_equal(other): Checks Python-level equality between two VariableTrackers

  The base implementation raises unimplemented() with helpful error messages, and subclasses override these methods as appropriate.

## Simplified _HashableTracker

  The _HashableTracker class in ConstDictVariable is now much simpler:

  - Removed underlying_value property (centralized type handling)
  - Removed _eq_impl static method (centralized equality logic)
  - Simplified __hash__() to delegate to vt.get_python_hash()
  - Simplified __eq__() to delegate to vt.is_python_equal()

## Decentralized Implementations

  Implemented the new protocol methods across relevant VariableTracker subclasses:

  - ConstantVariable, TensorVariable, TupleVariable, ListVariable
  - FrozensetVariable, FrozenDataClassVariable
  - BuiltinVariable, UserFunctionVariable, SkipFunctionVariable
  - FunctoolsPartialVariable, WeakRefVariable
  - NumpyVariable, NNModuleVariable, MethodWrapperVariable
  - TorchInGraphFunctionVariable, TorchHigherOrderOperatorVariable
  - TypingVariable, UserDefinedObjectVariable, UserDefinedClassVariable
  - SymNodeVariable, EnumVariable

## Enhanced Test Coverage

  Added 14 new test cases covering various hashable types as dictionary keys:

  - range, tuples, enums, frozensets
  - Typing constructs (e.g., typing.Union)
  - NumPy dtypes, method wrappers
  - Torch builtin functions, frozen dataclasses
  - Custom objects with __hash__
  - Negative test for unhashable types (lists)

## Improved Error Messages

  Updated error messages to be more informative when encountering unhashable types, showing both the Python type and the VariableTracker type.



cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Dec 5, 2025
…Dict variable tracker

ghstack-source-id: ebe6726
Pull Request resolved: #169204
…tation for Dict variable tracker"


Fixes #167956

## Summary

  This PR decentralizes and improves the hash implementation for dictionary keys in Dynamo's ConstDictVariable tracker. Instead of maintaining a centralized list of hashable types and custom equality logic in
  _HashableTracker, we now delegate hashability checks, hash computation, and equality comparison to individual VariableTracker subclasses.

## Motivation

  The previous implementation had several issues:

  1. Centralized logic: All hashability checks and hash computations were centralized in dicts.py, making it difficult to add support for new hashable types
  2. Maintainability: Adding a new hashable type required modifying multiple locations in _HashableTracker (underlying_value, _eq_impl, and the is_hashable function)
  3. Scattered knowledge: Type-specific hashing logic was separated from the type's own implementation
  4. Limited extensibility: No clear protocol for VariableTracker subclasses to declare themselves as hashable

## Changes

  New Protocol Methods

  Added three new methods to the VariableTracker base class:

  1. is_python_hashable(): Returns whether the underlying Python object is hashable
  2. get_python_hash(): Computes the hash value for the underlying Python object
  3. is_python_equal(other): Checks Python-level equality between two VariableTrackers

  The base implementation raises unimplemented() with helpful error messages, and subclasses override these methods as appropriate.

## Simplified _HashableTracker

  The _HashableTracker class in ConstDictVariable is now much simpler:

  - Removed underlying_value property (centralized type handling)
  - Removed _eq_impl static method (centralized equality logic)
  - Simplified __hash__() to delegate to vt.get_python_hash()
  - Simplified __eq__() to delegate to vt.is_python_equal()

## Decentralized Implementations

  Implemented the new protocol methods across relevant VariableTracker subclasses:

  - ConstantVariable, TensorVariable, TupleVariable, ListVariable
  - FrozensetVariable, FrozenDataClassVariable
  - BuiltinVariable, UserFunctionVariable, SkipFunctionVariable
  - FunctoolsPartialVariable, WeakRefVariable
  - NumpyVariable, NNModuleVariable, MethodWrapperVariable
  - TorchInGraphFunctionVariable, TorchHigherOrderOperatorVariable
  - TypingVariable, UserDefinedObjectVariable, UserDefinedClassVariable
  - SymNodeVariable, EnumVariable

## Enhanced Test Coverage

  Added 14 new test cases covering various hashable types as dictionary keys:

  - range, tuples, enums, frozensets
  - Typing constructs (e.g., typing.Union)
  - NumPy dtypes, method wrappers
  - Torch builtin functions, frozen dataclasses
  - Custom objects with __hash__
  - Negative test for unhashable types (lists)

## Improved Error Messages

  Updated error messages to be more informative when encountering unhashable types, showing both the Python type and the VariableTracker type.



cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Dec 5, 2025
…Dict variable tracker

ghstack-source-id: 590d2fa
Pull Request resolved: #169204
@anijain2305
Copy link
Contributor Author

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command
For more information see pytorch-bot wiki.

@anijain2305
Copy link
Contributor Author

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

umechand-amd pushed a commit to ROCm/pytorch that referenced this pull request Dec 8, 2025
…rch#167742)"

This reverts commit d78f52b.

Reverted pytorch#167742 on behalf of https://github.com/huydhn due to Sorry, I need to revert this to cleanly revert pytorch#169204.  Please rebase and reland this ([comment](pytorch#167742 (comment)))
umechand-amd pushed a commit to ROCm/pytorch that referenced this pull request Dec 8, 2025
…ion for Dict variable tracker (pytorch#169204)"

This reverts commit c04e2c6.

Reverted pytorch#169204 on behalf of https://github.com/huydhn due to This has been reverted internally ([comment](pytorch#169204 (comment)))
umechand-amd pushed a commit to ROCm/pytorch that referenced this pull request Dec 8, 2025
…Dict variable tracker (pytorch#169204)

Fixes pytorch#167956

## Summary

  This PR decentralizes and improves the hash implementation for dictionary keys in Dynamo's ConstDictVariable tracker. Instead of maintaining a centralized list of hashable types and custom equality logic in
  _HashableTracker, we now delegate hashability checks, hash computation, and equality comparison to individual VariableTracker subclasses.

## Motivation

  The previous implementation had several issues:

  1. Centralized logic: All hashability checks and hash computations were centralized in dicts.py, making it difficult to add support for new hashable types
  2. Maintainability: Adding a new hashable type required modifying multiple locations in _HashableTracker (underlying_value, _eq_impl, and the is_hashable function)
  3. Scattered knowledge: Type-specific hashing logic was separated from the type's own implementation
  4. Limited extensibility: No clear protocol for VariableTracker subclasses to declare themselves as hashable

## Changes

  New Protocol Methods

  Added three new methods to the VariableTracker base class:

  1. is_python_hashable(): Returns whether the underlying Python object is hashable
  2. get_python_hash(): Computes the hash value for the underlying Python object
  3. is_python_equal(other): Checks Python-level equality between two VariableTrackers

  The base implementation raises unimplemented() with helpful error messages, and subclasses override these methods as appropriate.

## Simplified _HashableTracker

  The _HashableTracker class in ConstDictVariable is now much simpler:

  - Removed underlying_value property (centralized type handling)
  - Removed _eq_impl static method (centralized equality logic)
  - Simplified __hash__() to delegate to vt.get_python_hash()
  - Simplified __eq__() to delegate to vt.is_python_equal()

## Decentralized Implementations

  Implemented the new protocol methods across relevant VariableTracker subclasses:

  - ConstantVariable, TensorVariable, TupleVariable, ListVariable
  - FrozensetVariable, FrozenDataClassVariable
  - BuiltinVariable, UserFunctionVariable, SkipFunctionVariable
  - FunctoolsPartialVariable, WeakRefVariable
  - NumpyVariable, NNModuleVariable, MethodWrapperVariable
  - TorchInGraphFunctionVariable, TorchHigherOrderOperatorVariable
  - TypingVariable, UserDefinedObjectVariable, UserDefinedClassVariable
  - SymNodeVariable, EnumVariable

## Enhanced Test Coverage

  Added 14 new test cases covering various hashable types as dictionary keys:

  - range, tuples, enums, frozensets
  - Typing constructs (e.g., typing.Union)
  - NumPy dtypes, method wrappers
  - Torch builtin functions, frozen dataclasses
  - Custom objects with __hash__
  - Negative test for unhashable types (lists)

## Improved Error Messages

  Updated error messages to be more informative when encountering unhashable types, showing both the Python type and the VariableTracker type.

Pull Request resolved: pytorch#169204
Approved by: https://github.com/jansel
JacobSzwejbka pushed a commit that referenced this pull request Dec 8, 2025
…Dict variable tracker (#169204)

Fixes #167956

## Summary

  This PR decentralizes and improves the hash implementation for dictionary keys in Dynamo's ConstDictVariable tracker. Instead of maintaining a centralized list of hashable types and custom equality logic in
  _HashableTracker, we now delegate hashability checks, hash computation, and equality comparison to individual VariableTracker subclasses.

## Motivation

  The previous implementation had several issues:

  1. Centralized logic: All hashability checks and hash computations were centralized in dicts.py, making it difficult to add support for new hashable types
  2. Maintainability: Adding a new hashable type required modifying multiple locations in _HashableTracker (underlying_value, _eq_impl, and the is_hashable function)
  3. Scattered knowledge: Type-specific hashing logic was separated from the type's own implementation
  4. Limited extensibility: No clear protocol for VariableTracker subclasses to declare themselves as hashable

## Changes

  New Protocol Methods

  Added three new methods to the VariableTracker base class:

  1. is_python_hashable(): Returns whether the underlying Python object is hashable
  2. get_python_hash(): Computes the hash value for the underlying Python object
  3. is_python_equal(other): Checks Python-level equality between two VariableTrackers

  The base implementation raises unimplemented() with helpful error messages, and subclasses override these methods as appropriate.

## Simplified _HashableTracker

  The _HashableTracker class in ConstDictVariable is now much simpler:

  - Removed underlying_value property (centralized type handling)
  - Removed _eq_impl static method (centralized equality logic)
  - Simplified __hash__() to delegate to vt.get_python_hash()
  - Simplified __eq__() to delegate to vt.is_python_equal()

## Decentralized Implementations

  Implemented the new protocol methods across relevant VariableTracker subclasses:

  - ConstantVariable, TensorVariable, TupleVariable, ListVariable
  - FrozensetVariable, FrozenDataClassVariable
  - BuiltinVariable, UserFunctionVariable, SkipFunctionVariable
  - FunctoolsPartialVariable, WeakRefVariable
  - NumpyVariable, NNModuleVariable, MethodWrapperVariable
  - TorchInGraphFunctionVariable, TorchHigherOrderOperatorVariable
  - TypingVariable, UserDefinedObjectVariable, UserDefinedClassVariable
  - SymNodeVariable, EnumVariable

## Enhanced Test Coverage

  Added 14 new test cases covering various hashable types as dictionary keys:

  - range, tuples, enums, frozensets
  - Typing constructs (e.g., typing.Union)
  - NumPy dtypes, method wrappers
  - Torch builtin functions, frozen dataclasses
  - Custom objects with __hash__
  - Negative test for unhashable types (lists)

## Improved Error Messages

  Updated error messages to be more informative when encountering unhashable types, showing both the Python type and the VariableTracker type.

Pull Request resolved: #169204
Approved by: https://github.com/jansel
ghstack dependencies: #169233
JacobSzwejbka pushed a commit that referenced this pull request Dec 8, 2025
…ion for Dict variable tracker (#169204)"

This reverts commit 8414958.

Reverted #169204 on behalf of https://github.com/wdvr due to failing test/test_fx.py::TestFXAPIBackwardCompatibility::test_public_api_surface [GH job link](https://github.com/pytorch/pytorch/actions/runs/19803784913/job/56735267443) [HUD commit link](https://hud.pytorch.org/pytorch/pytorch/commit/84149583d483e9c973c9a0feda70e4f3964947b0) consistently ([comment](#169204 (comment)))
JacobSzwejbka pushed a commit that referenced this pull request Dec 8, 2025
…Dict variable tracker (#169204)

Fixes #167956

## Summary

  This PR decentralizes and improves the hash implementation for dictionary keys in Dynamo's ConstDictVariable tracker. Instead of maintaining a centralized list of hashable types and custom equality logic in
  _HashableTracker, we now delegate hashability checks, hash computation, and equality comparison to individual VariableTracker subclasses.

## Motivation

  The previous implementation had several issues:

  1. Centralized logic: All hashability checks and hash computations were centralized in dicts.py, making it difficult to add support for new hashable types
  2. Maintainability: Adding a new hashable type required modifying multiple locations in _HashableTracker (underlying_value, _eq_impl, and the is_hashable function)
  3. Scattered knowledge: Type-specific hashing logic was separated from the type's own implementation
  4. Limited extensibility: No clear protocol for VariableTracker subclasses to declare themselves as hashable

## Changes

  New Protocol Methods

  Added three new methods to the VariableTracker base class:

  1. is_python_hashable(): Returns whether the underlying Python object is hashable
  2. get_python_hash(): Computes the hash value for the underlying Python object
  3. is_python_equal(other): Checks Python-level equality between two VariableTrackers

  The base implementation raises unimplemented() with helpful error messages, and subclasses override these methods as appropriate.

## Simplified _HashableTracker

  The _HashableTracker class in ConstDictVariable is now much simpler:

  - Removed underlying_value property (centralized type handling)
  - Removed _eq_impl static method (centralized equality logic)
  - Simplified __hash__() to delegate to vt.get_python_hash()
  - Simplified __eq__() to delegate to vt.is_python_equal()

## Decentralized Implementations

  Implemented the new protocol methods across relevant VariableTracker subclasses:

  - ConstantVariable, TensorVariable, TupleVariable, ListVariable
  - FrozensetVariable, FrozenDataClassVariable
  - BuiltinVariable, UserFunctionVariable, SkipFunctionVariable
  - FunctoolsPartialVariable, WeakRefVariable
  - NumpyVariable, NNModuleVariable, MethodWrapperVariable
  - TorchInGraphFunctionVariable, TorchHigherOrderOperatorVariable
  - TypingVariable, UserDefinedObjectVariable, UserDefinedClassVariable
  - SymNodeVariable, EnumVariable

## Enhanced Test Coverage

  Added 14 new test cases covering various hashable types as dictionary keys:

  - range, tuples, enums, frozensets
  - Typing constructs (e.g., typing.Union)
  - NumPy dtypes, method wrappers
  - Torch builtin functions, frozen dataclasses
  - Custom objects with __hash__
  - Negative test for unhashable types (lists)

## Improved Error Messages

  Updated error messages to be more informative when encountering unhashable types, showing both the Python type and the VariableTracker type.

Pull Request resolved: #169204
Approved by: https://github.com/jansel
JacobSzwejbka pushed a commit that referenced this pull request Dec 8, 2025
)"

This reverts commit d78f52b.

Reverted #167742 on behalf of https://github.com/huydhn due to Sorry, I need to revert this to cleanly revert #169204.  Please rebase and reland this ([comment](#167742 (comment)))
JacobSzwejbka pushed a commit that referenced this pull request Dec 8, 2025
…ion for Dict variable tracker (#169204)"

This reverts commit c04e2c6.

Reverted #169204 on behalf of https://github.com/huydhn due to This has been reverted internally ([comment](#169204 (comment)))
JacobSzwejbka pushed a commit that referenced this pull request Dec 8, 2025
…Dict variable tracker (#169204)

Fixes #167956

## Summary

  This PR decentralizes and improves the hash implementation for dictionary keys in Dynamo's ConstDictVariable tracker. Instead of maintaining a centralized list of hashable types and custom equality logic in
  _HashableTracker, we now delegate hashability checks, hash computation, and equality comparison to individual VariableTracker subclasses.

## Motivation

  The previous implementation had several issues:

  1. Centralized logic: All hashability checks and hash computations were centralized in dicts.py, making it difficult to add support for new hashable types
  2. Maintainability: Adding a new hashable type required modifying multiple locations in _HashableTracker (underlying_value, _eq_impl, and the is_hashable function)
  3. Scattered knowledge: Type-specific hashing logic was separated from the type's own implementation
  4. Limited extensibility: No clear protocol for VariableTracker subclasses to declare themselves as hashable

## Changes

  New Protocol Methods

  Added three new methods to the VariableTracker base class:

  1. is_python_hashable(): Returns whether the underlying Python object is hashable
  2. get_python_hash(): Computes the hash value for the underlying Python object
  3. is_python_equal(other): Checks Python-level equality between two VariableTrackers

  The base implementation raises unimplemented() with helpful error messages, and subclasses override these methods as appropriate.

## Simplified _HashableTracker

  The _HashableTracker class in ConstDictVariable is now much simpler:

  - Removed underlying_value property (centralized type handling)
  - Removed _eq_impl static method (centralized equality logic)
  - Simplified __hash__() to delegate to vt.get_python_hash()
  - Simplified __eq__() to delegate to vt.is_python_equal()

## Decentralized Implementations

  Implemented the new protocol methods across relevant VariableTracker subclasses:

  - ConstantVariable, TensorVariable, TupleVariable, ListVariable
  - FrozensetVariable, FrozenDataClassVariable
  - BuiltinVariable, UserFunctionVariable, SkipFunctionVariable
  - FunctoolsPartialVariable, WeakRefVariable
  - NumpyVariable, NNModuleVariable, MethodWrapperVariable
  - TorchInGraphFunctionVariable, TorchHigherOrderOperatorVariable
  - TypingVariable, UserDefinedObjectVariable, UserDefinedClassVariable
  - SymNodeVariable, EnumVariable

## Enhanced Test Coverage

  Added 14 new test cases covering various hashable types as dictionary keys:

  - range, tuples, enums, frozensets
  - Typing constructs (e.g., typing.Union)
  - NumPy dtypes, method wrappers
  - Torch builtin functions, frozen dataclasses
  - Custom objects with __hash__
  - Negative test for unhashable types (lists)

## Improved Error Messages

  Updated error messages to be more informative when encountering unhashable types, showing both the Python type and the VariableTracker type.

Pull Request resolved: #169204
Approved by: https://github.com/jansel
@github-actions github-actions bot deleted the gh/anijain2305/963/head branch January 6, 2026 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request keep-going Don't stop on first failure, keep running tests until the end Merged module: dynamo Reverted topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants