Skip to content

[Dispatch Creation] Drop unit dims from tensor.extract ops - #22503

Merged
IanWood1 merged 2 commits into
iree-org:mainfrom
IanWood1:fold_unit_dims_from_extract_op
Nov 5, 2025
Merged

IanWood1 merged 2 commits into
iree-org:mainfrom
IanWood1:fold_unit_dims_from_extract_op

Conversation

@IanWood1

@IanWood1 IanWood1 commented Oct 31, 2025 •

Copy link
Copy Markdown
Member

Adds pattern for tensor.extract to fold unit dimensions. Without this pattern there will be reshapes left in the program which may re-introduce the unit dims when propagated.

The added xfail already has an issue #20011.

@Groverkss Groverkss left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No opinion on this, but I wonder if a better way to handle this is actually to have a collapse/expand -> extract folding pattern just like we do for FoldMemrefAliasOps. It's effectively the same thing. It would handle things more generally and would ensure we always remove reshapes for tensor.extract.

@IanWood1

IanWood1 commented Nov 3, 2025

Copy link
Copy Markdown
Member Author

No opinion on this, but I wonder if a better way to handle this is actually to have a collapse/expand -> extract folding pattern just like we do for FoldMemrefAliasOps. It's effectively the same thing. It would handle things more generally and would ensure we always remove reshapes for tensor.extract.

I don't have a strong opinion here either but here are some thoughts:

First, this pattern is more "active". It doesn't require an directly adjacent reshape to remove the unit dims from the op. If every op only folded with collapse/expand ops, we wouldn’t be able to eliminate unit dims in cases where the reshape isn’t the immediate producer. That’s partly why the existing patterns in this pass are structured as they are. tensor.extract might have different constraints, but my patch directly addresses the immediate issue I ran into. I’m not opposed to adding both patterns, though.

Second, having a collapse/expand -> extract folding pattern could be harmful in some cases. For example, take a tensor.expand that expands from a 1D tensor to 4D. Folding this into the extract means you have to linearize the indices, which feels less canonical and might obscure the original intent (although it might help with some fusion). If it's just unit dims then it doesn't matter.

@Groverkss

Copy link
Copy Markdown
Contributor

No opinion on this, but I wonder if a better way to handle this is actually to have a collapse/expand -> extract folding pattern just like we do for FoldMemrefAliasOps. It's effectively the same thing. It would handle things more generally and would ensure we always remove reshapes for tensor.extract.

I don't have a strong opinion here either but here are some thoughts:

First, this pattern is more "active". It doesn't require an directly adjacent reshape to remove the unit dims from the op. If every op only folded with collapse/expand ops, we wouldn’t be able to eliminate unit dims in cases where the reshape isn’t the immediate producer. That’s partly why the existing patterns in this pass are structured as they are. tensor.extract might have different constraints, but my patch directly addresses the immediate issue I ran into. I’m not opposed to adding both patterns, though.

Second, having a collapse/expand -> extract folding pattern could be harmful in some cases. For example, take a tensor.expand that expands from a 1D tensor to 4D. Folding this into the extract means you have to linearize the indices, which feels less canonical and might obscure the original intent (although it might help with some fusion). If it's just unit dims then it doesn't matter.

When I say folding pattern I didn't mean a "canonicalization folder", sorry for mis communicating that. I just meant a pattern that you can run. Either is okay with me, I'm mainly offering another solution. Feel free to land what you think is better.

@MaheshRavishankar MaheshRavishankar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work for rank-reducing extract slices? The base logic should cause you are using just the source type and offsets/sizes. Just need to check the new operation created is valid.

@IanWood1

IanWood1 commented Nov 4, 2025 •

Copy link
Copy Markdown
Member Author

Does this work for rank-reducing extract slices? The base logic should cause you are using just the source type and offsets/sizes. Just need to check the new operation created is valid.

Are you referring to adding a different pattern with similar logic as this one but for extract_slices? I think that would work with some small changes.

@MaheshRavishankar MaheshRavishankar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didnt realize this is tensor.extract. That is strange that we have a tensor.extract outside of a linalg op. Whats the source of this error.

@IanWood1

IanWood1 commented Nov 4, 2025 •

Copy link
Copy Markdown
Member Author

I didn't put the tensor.extract ops inside of a linalg.generic for the tests since it didn't seem necessary for what I was testing for this pattern. But in the example I was looking at from SDXL, the extract ops were in "gather"-like linalg generics. Either way, this still causes unit dims to fail to completely fold away.

I can add a test with the tensor.extract nested in a linalg.generic if that seems needed.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

I didn't put the tensor.extract ops inside of a linalg.generic for the tests since it didn't seem necessary for what I was testing for this pattern. But in the example I was looking at from SDXL, the extract ops were in "gather"-like linalg generics. Either way, this still causes unit dims to fail to completely fold away.

I can add a test with the tensor.extract nested in a linalg.generic if that seems needed.

I was just curious. I already approved the PR.

@IanWood1
IanWood1 merged commit dca3747 into iree-org:main Nov 5, 2025
bangtianliu pushed a commit to bangtianliu/iree that referenced this pull request Nov 19, 2025
…22503)

Adds pattern for `tensor.extract` to fold unit dimensions. Without this
pattern there will be reshapes left in the program which may
re-introduce the unit dims when propagated.

The added xfail already has an issue
iree-org#20011.

---------

Signed-off-by: Ian Wood <[email protected]>
pstarkcdpr pushed a commit to pstarkcdpr/iree that referenced this pull request Nov 28, 2025
…22503)

Adds pattern for `tensor.extract` to fold unit dimensions. Without this
pattern there will be reshapes left in the program which may
re-introduce the unit dims when propagated.

The added xfail already has an issue
iree-org#20011.

---------

Signed-off-by: Ian Wood <[email protected]>
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.

3 participants