Closed XuehaiPan closed 1 year ago
Patch coverage: 100.00
% and no project coverage change.
Comparison is base (
9e150ce
) 100.00% compared to head (e09a28b
) 100.00%.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.
Got around ~25% performance drop for tree-map on builtins.dict
.
now the performance downgrade on tree_unflatten is intolerable
Code robustness is more important than performance. The tree_map
function does not preserve dict key order with the identity function. It's not intuitive for normal users who do not read the documentation carefully.
This PR adds extra operations in tree_flatten
and tree_unflatten
.
tree_flatten
: add list.copy
before list.sort
($O(n \log n) \to O(n \log n) + O (n)$)tree_unflatten
: add dict.fromkeys
before dict.update
($O(n) \to O(n) + O (n)$)It would introduce some performance regression. But the tree operations sometimes are not the performance bottleneck in the whole pipeline. For example, you reduce a tree operation from 0.1ms
to 0.08ms
(20%), but your environment simulation takes 1ms
. The total performance gain is minor (1.1ms
vs. 1.08ms
(2%)).
It would introduce some performance regression. But the tree operations sometimes are not the performance bottleneck in the whole pipeline. For example, you reduce a tree operation from
0.1ms
to0.08ms
(20%), but your environment simulation takes1ms
. The total performance gain is minor (1.1ms
vs.1.08ms
(2%)).
Thanks for your reply! I will try to run the benchmark test with the whl in this pr on envpool these two days, I will get back to you when the results are out, please wait until the benchmark test is ready.
Description
Describe your changes in detail.
Add a new option in(enabled globally), which will save the original key order in_C.flatten
and_C.flatten_with_path
PyTreeSpec
and use it to preserve key order during unflattering.Now, the output of
tree_map
andtree_map_with_path
will guarantee the same key order as the input dictionaries.Before: output dict from
tree_unflatten
is in sorted order. The output key order changes even if mapped with an identity function.After: output dict from
tree_unflatten
is consistent with the input dict.Note that
tree_map
still maps the leaves in sorted key order (the same order astree_flatten
andtree_leaves
). This PR only changes the behavior fortree_unflatten
.Also, If the users manually maintain the treespec themselves. For example, 1) flatten the tree, 2) hold the reference to the treespec and do something with the leaves, 3) unflatten back the results into a tree with treespec. The key order in the resulting tree is still sorted. This PR only affects functionstree_map
andtree_map_with_path
.Motivation and Context
Why is this change required? What problem does it solve? If it fixes an open issue, please link to the issue here. You can use the syntax
close #15213
if this solves the issue #15213Resolves #45
Types of changes
What types of changes does your code introduce? Put an
x
in all the boxes that apply:Checklist
Go over all the following points, and put an
x
in all the boxes that apply. If you are unsure about any of these, don't hesitate to ask. We are here to help!make format
. (required)make lint
. (required)make test
pass. (required)