You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+45Lines changed: 45 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -505,6 +505,51 @@ The same applies to [`collections.defaultdict`](https://docs.python.org/3/librar
505
505
([1, 2, 3], PyTreeSpec({'a': [*, *], 'b': [*]}))
506
506
```
507
507
508
+
Sorting ensures that equal dictionaries always flatten to the same leaf sequence, regardless of insertion order. This is critical for operations that rely on positional correspondence between leaves. Consider two parameter `dict`s that are equal but constructed in different orders:
array([1., 2., 3., 4., 5., 6.]) # weight, bias (insertion order of params1)
547
+
>>> flat2
548
+
array([5., 6., 1., 2., 3., 4.]) # bias, weight (insertion order of params2)
549
+
>>> flat1 - flat2 # WRONG! Should be all zeros for equal params
550
+
array([-4., -4., 2., 2., 2., 2.])
551
+
```
552
+
508
553
To preserve insertion order during pytree traversal, use [`collections.OrderedDict`](https://docs.python.org/3/library/collections.html#collections.OrderedDict), which considers key order in equality checks:
0 commit comments