Add rewrites to convert reshape to expand_dims or static_slice when possible#10002
Merged
copybara-service[bot] merged 1 commit intomasterfrom Apr 18, 2026
Merged
Add rewrites to convert reshape to expand_dims or static_slice when possible#10002copybara-service[bot] merged 1 commit intomasterfrom
copybara-service[bot] merged 1 commit intomasterfrom
Conversation
3a30183 to
4287d19
Compare
…ossible
Generated by AI first, and then I partially rewrote the rewrite to be simpler and smaller. Here is what it had to say:
✦ I have added a rewrite rule to fusion.cc that converts reshape operations into either expand_dims or slice (squeeze) when possible.
Summary of Changes
1. Refactored subgraph/copy.cc: Extracted the core logic for defining static_reshape and static_expand_dims into internal C++ functions (define_static_reshape and define_static_expand_dims) and exported them via subgraph/copy.h.
2. Updated subgraph/static_slice.cc: Similarly refactored static_slice logic into a reusable define_static_slice function and created a new internal header subgraph/static_slice.h for it.
3. Modified subgraph/fusion.cc:
* Implemented rewrite_reshape, which identifies if a static_reshape only adds or removes dimensions of size 1.
* If it adds dimensions, it rewrites it to a static_expand_dims node.
* If it removes dimensions, it rewrites it to a static_slice node with the slice_dims flag enabled (effectively a squeeze).
* Integrated this new rewrite into the main fusion loop.
4. Enhanced Testing Infrastructure:
* Added IsStaticReshape, IsStaticExpandDims, and IsStaticSlice matchers to subgraph/test/matchers.h.
* Created subgraph/test/fusion_reshape.cc with unit tests for these new rewrites, ensuring that reshape is correctly converted when possible and remains unchanged when it performs a more complex transformation.
5. Build System Updates: Updated subgraph/BUILD and subgraph/test/BUILD to include the new files and test cases.
All tests passed successfully, confirming the correctness of the rewrites.
PiperOrigin-RevId: 901557580
4287d19 to
ef69e72
Compare
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
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.
Add rewrites to convert reshape to expand_dims or static_slice when possible
Generated by AI first, and then I partially rewrote the rewrite to be simpler and smaller. Here is what it had to say:
✦ I have added a rewrite rule to fusion.cc that converts reshape operations into either expand_dims or slice (squeeze) when possible.
Summary of Changes
All tests passed successfully, confirming the correctness of the rewrites.