-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[pytree] Add arg_tree_leaves to optimize flattening function arguments #112393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We commonly do some variation of `tree_leaves((args, kwargs))`. This adds a new function `arg_tree_leaves(*args, **kwargs)` which takes advantage of the known structure of `args` and `kwargs` to skip their `flatten_fn`. I see ~1 us improvement per call for args + kwargs, or a 0.5 us improvement when passing just one of `args` or `kwargs`. For shallow structures, this can be proportionally quite significant. For example, the empty_strided call I've been using as a benchmark: ``` args = ((100, 100), (100, 1)) kwargs = dict(device="cuda") ``` Sees a 30% speedup from this. [ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/112393
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit ca986a8 with merge base 29844ad ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This was referenced Oct 30, 2023
…on arguments" We commonly do some variation of `tree_leaves((args, kwargs))`. This adds a new function `arg_tree_leaves(*args, **kwargs)` which takes advantage of the known structure of `args` and `kwargs` to skip their `flatten_fn`. I see ~1 us improvement per call for args + kwargs, or a 0.5 us improvement when passing just one of `args` or `kwargs`. For shallow structures, this can be proportionally quite significant. For example, the empty_strided call I've been using as a benchmark: ``` args = ((100, 100), (100, 1)) kwargs = dict(device="cuda") ``` Sees a 30% speedup from this. cc zou3519 [ghstack-poisoned]
lezcano
approved these changes
Oct 30, 2023
This was referenced Oct 30, 2023
peterbell10
added a commit
to peterbell10/pytorch
that referenced
this pull request
Oct 31, 2023
We commonly do some variation of `tree_leaves((args, kwargs))`. This adds a new function `arg_tree_leaves(*args, **kwargs)` which takes advantage of the known structure of `args` and `kwargs` to skip their `flatten_fn`. I see ~1 us improvement per call for args + kwargs, or a 0.5 us improvement when passing just one of `args` or `kwargs`. For shallow structures, this can be proportionally quite significant. For example, the empty_strided call I've been using as a benchmark: ``` args = ((100, 100), (100, 1)) kwargs = dict(device="cuda") ``` Sees a 30% speedup from this. ghstack-source-id: 3f5310d Pull Request resolved: pytorch#112393
pytorchmergebot
pushed a commit
that referenced
this pull request
Oct 31, 2023
Pull Request resolved: #112394 Approved by: https://github.com/lezcano ghstack dependencies: #112391, #112392, #112393
xuhancn
pushed a commit
to xuhancn/pytorch
that referenced
this pull request
Nov 7, 2023
pytorch#112393) We commonly do some variation of `tree_leaves((args, kwargs))`. This adds a new function `arg_tree_leaves(*args, **kwargs)` which takes advantage of the known structure of `args` and `kwargs` to skip their `flatten_fn`. I see ~1 us improvement per call for args + kwargs, or a 0.5 us improvement when passing just one of `args` or `kwargs`. For shallow structures, this can be proportionally quite significant. For example, the empty_strided call I've been using as a benchmark: ``` args = ((100, 100), (100, 1)) kwargs = dict(device="cuda") ``` Sees a 30% speedup from this. Pull Request resolved: pytorch#112393 Approved by: https://github.com/lezcano ghstack dependencies: pytorch#112391, pytorch#112392
xuhancn
pushed a commit
to xuhancn/pytorch
that referenced
this pull request
Nov 7, 2023
Pull Request resolved: pytorch#112394 Approved by: https://github.com/lezcano ghstack dependencies: pytorch#112391, pytorch#112392, pytorch#112393
xuhancn
pushed a commit
to xuhancn/pytorch
that referenced
this pull request
Nov 7, 2023
Wherever we discard the output of `tree_map` it's better to call `tree_map_` which doesn't unflatten the mapped results and so is a lot cheaper. Pull Request resolved: pytorch#112417 Approved by: https://github.com/lezcano ghstack dependencies: pytorch#112391, pytorch#112392, pytorch#112393, pytorch#112394
Skylion007
pushed a commit
to Skylion007/pytorch
that referenced
this pull request
Nov 14, 2023
pytorch#112393) We commonly do some variation of `tree_leaves((args, kwargs))`. This adds a new function `arg_tree_leaves(*args, **kwargs)` which takes advantage of the known structure of `args` and `kwargs` to skip their `flatten_fn`. I see ~1 us improvement per call for args + kwargs, or a 0.5 us improvement when passing just one of `args` or `kwargs`. For shallow structures, this can be proportionally quite significant. For example, the empty_strided call I've been using as a benchmark: ``` args = ((100, 100), (100, 1)) kwargs = dict(device="cuda") ``` Sees a 30% speedup from this. Pull Request resolved: pytorch#112393 Approved by: https://github.com/lezcano ghstack dependencies: pytorch#112391, pytorch#112392
Skylion007
pushed a commit
to Skylion007/pytorch
that referenced
this pull request
Nov 14, 2023
Pull Request resolved: pytorch#112394 Approved by: https://github.com/lezcano ghstack dependencies: pytorch#112391, pytorch#112392, pytorch#112393
Skylion007
pushed a commit
to Skylion007/pytorch
that referenced
this pull request
Nov 14, 2023
Wherever we discard the output of `tree_map` it's better to call `tree_map_` which doesn't unflatten the mapped results and so is a lot cheaper. Pull Request resolved: pytorch#112417 Approved by: https://github.com/lezcano ghstack dependencies: pytorch#112391, pytorch#112392, pytorch#112393, pytorch#112394
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):
ShapeEnv#112493pytree.tree_map_everywhere #112417pytree.arg_tree_leaveseverywhere #112394We commonly do some variation of
tree_leaves((args, kwargs)). This adds a newfunction
arg_tree_leaves(*args, **kwargs)which takes advantage of the knownstructure of
argsandkwargsto skip theirflatten_fn.I see ~1 us improvement per call for args + kwargs, or a 0.5 us improvement
when passing just one of
argsorkwargs. For shallow structures, this can beproportionally quite significant. For example, the empty_strided call I've been
using as a benchmark:
Sees a 30% speedup from this.
cc @zou3519