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

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
861efcd
Update
XuehaiPan Jul 5, 2024
fad9485
Update
XuehaiPan Jul 5, 2024
53f4be2
Update
XuehaiPan Jul 5, 2024
0de481f
Update
XuehaiPan Jul 5, 2024
c264689
Update
XuehaiPan Jul 5, 2024
452f750
Update
XuehaiPan Jul 5, 2024
16f36df
Update
XuehaiPan Jul 5, 2024
15faca5
Update
XuehaiPan Jul 5, 2024
6353a5b
Update
XuehaiPan Jul 5, 2024
2dda457
Update
XuehaiPan Jul 5, 2024
9fb9f83
Update
XuehaiPan Jul 6, 2024
bb317d2
Update
XuehaiPan Jul 6, 2024
27f1564
Update
XuehaiPan Jul 6, 2024
b46a684
Update
XuehaiPan Jul 7, 2024
9a52dc8
Update
XuehaiPan Jul 7, 2024
077cd83
Update
XuehaiPan Jul 9, 2024
ccfdda0
Update
XuehaiPan Jul 9, 2024
fe30f9e
Update
XuehaiPan Jul 10, 2024
67c3ec8
Update
XuehaiPan Jul 10, 2024
33caa36
Update
XuehaiPan Jul 10, 2024
b2d7a02
Update
XuehaiPan Jul 10, 2024
d89607f
Update
XuehaiPan Jul 10, 2024
d1d0081
Update
XuehaiPan Jul 10, 2024
67e2504
Update
XuehaiPan Jul 10, 2024
b032828
Update
XuehaiPan Jul 11, 2024
b9b8593
Update
XuehaiPan Jul 11, 2024
74a6e05
Update
XuehaiPan Jul 11, 2024
21fc546
Update
XuehaiPan Jul 13, 2024
3daac60
Update
XuehaiPan Jul 13, 2024
9fd80bb
Update
XuehaiPan Jul 14, 2024
6df841f
Update
XuehaiPan Jul 15, 2024
a35fffb
Update
XuehaiPan Jul 15, 2024
8c99fd6
Update
XuehaiPan Jul 16, 2024
87c4a96
Update
XuehaiPan Jul 16, 2024
4ce9677
Update
XuehaiPan Jul 16, 2024
817a1ee
Update
XuehaiPan Jul 16, 2024
736c962
Update
XuehaiPan Jul 17, 2024
31d1a6f
Update
XuehaiPan Jul 17, 2024
f4a0c9e
Update
XuehaiPan Jul 17, 2024
afd2b8c
Update
XuehaiPan Jul 18, 2024
20dea73
Update
XuehaiPan Jul 18, 2024
c35030e
Update
XuehaiPan Jul 18, 2024
f7d20dc
Update
XuehaiPan Jul 19, 2024
66c37f2
Update
XuehaiPan Jul 19, 2024
dec5744
Update
XuehaiPan Jul 19, 2024
b8e8b81
Update
XuehaiPan Jul 24, 2024
99b014e
Update
XuehaiPan Aug 1, 2024
fb7328d
Update
XuehaiPan Aug 1, 2024
173c2f3
Update
XuehaiPan Aug 1, 2024
73e1869
Update
XuehaiPan Aug 4, 2024
100f2e6
Update
XuehaiPan Aug 5, 2024
8a7abfe
Update
XuehaiPan Aug 5, 2024
51880a1
Update
XuehaiPan Aug 12, 2024
3f6c2a8
Update
XuehaiPan Sep 18, 2024
2910219
Update
XuehaiPan Oct 3, 2024
f24f44b
Update
XuehaiPan Oct 4, 2024
0c21f56
Update
XuehaiPan Oct 5, 2024
d9ecf28
Update
XuehaiPan Oct 6, 2024
fba9dd4
Update
XuehaiPan Oct 6, 2024
0e9d092
Update
XuehaiPan Oct 13, 2024
e1ce981
Update
XuehaiPan Oct 16, 2024
db38bd4
Update
XuehaiPan Oct 16, 2024
aaeea9e
Update
XuehaiPan Oct 17, 2024
192ea19
Update
XuehaiPan Oct 25, 2024
6339736
Update
XuehaiPan Oct 26, 2024
ef82a9f
Update
XuehaiPan Oct 29, 2024
90c1f54
Update
XuehaiPan Oct 30, 2024
e49469f
Update
XuehaiPan Oct 30, 2024
13d6f33
Update
XuehaiPan Nov 2, 2024
7d2201a
Update
XuehaiPan Nov 5, 2024
1d52997
Update
XuehaiPan Nov 11, 2024
3612168
Update
XuehaiPan Nov 17, 2024
9b88227
Update
XuehaiPan Nov 20, 2024
07ba964
Update
XuehaiPan Nov 20, 2024
04649e9
Update
XuehaiPan Nov 20, 2024
347e92b
Update
XuehaiPan Nov 20, 2024
f36f3e2
Update
XuehaiPan Nov 20, 2024
7406d9f
Update
XuehaiPan Nov 20, 2024
3ec71ae
Update
XuehaiPan Nov 20, 2024
520e3ac
Update
XuehaiPan Nov 21, 2024
e27c1e5
Update
XuehaiPan Nov 21, 2024
a45e1e6
Update
XuehaiPan Nov 21, 2024
c0d8361
Update
XuehaiPan Nov 21, 2024
767416a
Update
XuehaiPan Nov 21, 2024
f2cd464
Update
XuehaiPan Nov 22, 2024
fa8e39d
Update
XuehaiPan Nov 22, 2024
0808273
Update
XuehaiPan Nov 26, 2024
934b8b3
Update
XuehaiPan Nov 26, 2024
5f8895e
Update
XuehaiPan Nov 27, 2024
9443b8b
Update
XuehaiPan Dec 2, 2024
39490a6
Update
XuehaiPan Dec 2, 2024
9506c58
Update
XuehaiPan Dec 7, 2024
23aeeb1
Update
XuehaiPan Dec 9, 2024
fe8083f
Update
XuehaiPan Dec 13, 2024
9163dbd
Update
XuehaiPan Dec 25, 2024
ad07899
Update
XuehaiPan Jan 7, 2025
72f38c3
Update
XuehaiPan Jan 8, 2025
830d96b
Update
XuehaiPan Jan 9, 2025
2b304b6
Update
XuehaiPan Jan 10, 2025
3f52774
Update
XuehaiPan Jan 10, 2025
7859447
Update
XuehaiPan Feb 4, 2025
c21880a
Update
XuehaiPan Feb 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 2 additions & 27 deletions test/test_pytree.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
run_tests,
skipIfTorchDynamo,
subtest,
TEST_WITH_TORCHDYNAMO,
TestCase,
)

Expand Down Expand Up @@ -805,7 +804,6 @@ def test_treespec_equality(self):
py_pytree.TreeSpec(tuple, None, []) != py_pytree.TreeSpec(list, None, []),
)

@unittest.skipIf(TEST_WITH_TORCHDYNAMO, "Dynamo test in test_treespec_repr_dynamo.")
def test_treespec_repr(self):
# Check that it looks sane
pytree = (0, [0, 0, [0]])
Expand All @@ -820,20 +818,6 @@ def test_treespec_repr(self):
),
)

@unittest.skipIf(not TEST_WITH_TORCHDYNAMO, "Eager test in test_treespec_repr.")
def test_treespec_repr_dynamo(self):
# Check that it looks sane
pytree = (0, [0, 0, [0]])
_, spec = py_pytree.tree_flatten(pytree)
self.assertExpectedInline(
repr(spec),
"""\
TreeSpec(tuple, None, [*,
TreeSpec(list, None, [*,
*,
TreeSpec(list, None, [*])])])""",
)

@parametrize(
"spec",
[
Expand Down Expand Up @@ -1365,21 +1349,12 @@ def setUp(self):
def test_treespec_equality(self):
self.assertEqual(cxx_pytree.LeafSpec(), cxx_pytree.LeafSpec())

@unittest.skipIf(TEST_WITH_TORCHDYNAMO, "Dynamo test in test_treespec_repr_dynamo.")
def test_treespec_repr(self):
# Check that it looks sane
pytree = (0, [0, 0, [0]])
_, spec = cxx_pytree.tree_flatten(pytree)
self.assertEqual(repr(spec), "PyTreeSpec((*, [*, *, [*]]), NoneIsLeaf)")

@unittest.skipIf(not TEST_WITH_TORCHDYNAMO, "Eager test in test_treespec_repr.")
def test_treespec_repr_dynamo(self):
# Check that it looks sane
pytree = (0, [0, 0, [0]])
_, spec = cxx_pytree.tree_flatten(pytree)
self.assertExpectedInline(
repr(spec),
"PyTreeSpec((*, [*, *, [*]]), NoneIsLeaf, namespace='torch')",
self.assertEqual(
repr(spec), "PyTreeSpec((*, [*, *, [*]]), NoneIsLeaf, namespace='torch')"
)

@parametrize(
Expand Down
27 changes: 13 additions & 14 deletions torch/utils/_cxx_pytree.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
]


__TORCH_DICT_SESSION = optree.dict_insertion_ordered(True, namespace="torch")
__TORCH_DICT_SESSION.__enter__() # enable globally and permanently
Comment on lines +64 to +65
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're sure about this. The fun thing is if someone imports torch cxx_pytree and uses optree then they will override the optree behavior

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only affects optree functions that passed with namespace="torch".



T = TypeVar("T")
S = TypeVar("S")
U = TypeVar("U")
Expand Down Expand Up @@ -285,20 +289,15 @@ def tree_flatten(

>>> tree = {"b": (2, [3, 4]), "a": 1, "c": None, "d": 5}
>>> tree_flatten(tree)
([1, 2, 3, 4, None, 5], PyTreeSpec({'a': *, 'b': (*, [*, *]), 'c': *, 'd': *}, NoneIsLeaf))
([2, 3, 4, 1, None, 5], PyTreeSpec({'b': (*, [*, *]), 'a': *, 'c': *, 'd': *}, NoneIsLeaf, namespace='torch'))
>>> tree_flatten(1)
([1], PyTreeSpec(*, NoneIsLeaf))
([1], PyTreeSpec(*, NoneIsLeaf, namespace='torch'))
>>> tree_flatten(None)
([None], PyTreeSpec(*, NoneIsLeaf))

For unordered dictionaries, :class:`dict` and :class:`collections.defaultdict`, the order is
dependent on the **sorted** keys in the dictionary. Please use :class:`collections.OrderedDict`
if you want to keep the keys in the insertion order.

([None], PyTreeSpec(*, NoneIsLeaf, namespace='torch'))
>>> from collections import OrderedDict
>>> tree = OrderedDict([("b", (2, [3, 4])), ("a", 1), ("c", None), ("d", 5)])
>>> tree_flatten(tree)
([2, 3, 4, 1, None, 5], PyTreeSpec(OrderedDict({'b': (*, [*, *]), 'a': *, 'c': *, 'd': *}), NoneIsLeaf))
([2, 3, 4, 1, None, 5], PyTreeSpec(OrderedDict({'b': (*, [*, *]), 'a': *, 'c': *, 'd': *}), NoneIsLeaf, namespace='torch'))

Args:
tree (pytree): A pytree to flatten.
Expand Down Expand Up @@ -357,7 +356,7 @@ def tree_iter(

>>> tree = {"b": (2, [3, 4]), "a": 1, "c": None, "d": 5}
>>> list(tree_iter(tree))
[1, 2, 3, 4, None, 5]
[2, 3, 4, 1, None, 5]
>>> list(tree_iter(1))
[1]
>>> list(tree_iter(None))
Expand Down Expand Up @@ -392,7 +391,7 @@ def tree_leaves(

>>> tree = {"b": (2, [3, 4]), "a": 1, "c": None, "d": 5}
>>> tree_leaves(tree)
[1, 2, 3, 4, None, 5]
[2, 3, 4, 1, None, 5]
>>> tree_leaves(1)
[1]
>>> tree_leaves(None)
Expand Down Expand Up @@ -427,11 +426,11 @@ def tree_structure(

>>> tree = {"b": (2, [3, 4]), "a": 1, "c": None, "d": 5}
>>> tree_structure(tree)
PyTreeSpec({'a': *, 'b': (*, [*, *]), 'c': *, 'd': *}, NoneIsLeaf)
PyTreeSpec({'b': (*, [*, *]), 'a': *, 'c': *, 'd': *}, NoneIsLeaf, namespace='torch')
>>> tree_structure(1)
PyTreeSpec(*, NoneIsLeaf)
PyTreeSpec(*, NoneIsLeaf, namespace='torch')
>>> tree_structure(None)
PyTreeSpec(*, NoneIsLeaf)
PyTreeSpec(*, NoneIsLeaf, namespace='torch')

Args:
tree (pytree): A pytree to flatten.
Expand Down