Skip to content

Add rewrites to convert reshape to expand_dims or static_slice when possible#10002

Merged
copybara-service[bot] merged 1 commit intomasterfrom
test_901538372
Apr 18, 2026
Merged

Add rewrites to convert reshape to expand_dims or static_slice when possible#10002
copybara-service[bot] merged 1 commit intomasterfrom
test_901538372

Conversation

@copybara-service
Copy link
Copy Markdown
Contributor

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

  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.

…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
@copybara-service copybara-service Bot merged commit ef69e72 into master Apr 18, 2026
@copybara-service copybara-service Bot deleted the test_901538372 branch April 18, 2026 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant