Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[BUG][pytree] equal dicts do not imply equal leaves and equal treespecsΒ #114392

@XuehaiPan

Description

@XuehaiPan

πŸ› Describe the bug

Currently, the Python pytree does not guarantee "equal dicts imply equal leaves and equal treespecs".

In [1]: import torch.utils._pytree as pytree

In [2]: {'a': 1, 'b': 2} == {'b': 2, 'a': 1}
Out[2]: True

In [3]: pytree.tree_leaves({'a': 1, 'b': 2})
Out[3]: [1, 2]

In [4]: pytree.tree_leaves({'b': 2, 'a': 1})
Out[4]: [2, 1]

In [5]: pytree.tree_structure({'a': 1, 'b': 2})
Out[5]: 
TreeSpec(dict, ['a', 'b'], [*,
  *])

In [6]: pytree.tree_structure({'b': 2, 'a': 1})
Out[6]: 
TreeSpec(dict, ['b', 'a'], [*,
  *])

In [7]: pytree.tree_leaves({'a': 1, 'b': 2}) == pytree.tree_leaves({'b': 2, 'a': 1})
Out[7]: False

In [8]: pytree.tree_structure({'a': 1, 'b': 2}) == pytree.tree_structure({'b': 2, 'a': 1})
Out[8]: False

See #114947 (comment) for more explanation.

Versions

Versions
Collecting environment information...
PyTorch version: 2.2.0.dev20231121
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.7.1 (x86_64)
GCC version: Could not collect
Clang version: 17.0.5
CMake version: version 3.27.8
Libc version: N/A

Python version: 3.11.5 (main, Sep 11 2023, 08:19:27) [Clang 14.0.6 ] (64-bit runtime)
Python platform: macOS-10.16-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz

Versions of relevant libraries:
[pip3] flake8==6.0.0
[pip3] flake8-bugbear==23.3.23
[pip3] flake8-comprehensions==3.12.0
[pip3] flake8-executable==2.1.3
[pip3] flake8-logging-format==0.9.0
[pip3] flake8-pyi==23.3.1
[pip3] flake8-simplify==0.19.3
[pip3] mypy==1.6.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.0
[pip3] optree==0.10.0
[conda] blas                      1.0                         mkl  
[conda] mkl                       2023.1.0         h8e150cf_43560  
[conda] numpy                     1.26.0                   pypi_0  
[conda] optree                    0.10.0                   pypi_0  
[conda] torchfix                  0.2.0                    pypi_0

cc @zou3519

Metadata

Metadata

Assignees

Labels

module: pytreetriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions