Skip to content

Migrate Flow dependencies in Stream/Codegen to TensorExt - #20564

Merged
jtuyls merged 1 commit into
iree-org:mainfrom
jtuyls:flow-to-tensor-migration
Apr 22, 2025
Merged

jtuyls merged 1 commit into
iree-org:mainfrom
jtuyls:flow-to-tensor-migration

Conversation

@jtuyls

@jtuyls jtuyls commented Apr 16, 2025 •

Copy link
Copy Markdown
Contributor

Migrates a set of Flow constructs to TensorExt to reduce the dependency in Stream/Codegen on Flow and to avoid a circular dependency from Encoding on Flow encountered during the MaterializeEncodingIntoPadding collapsing work (#20160 (comment)):

  • IREE::Flow::DispatchWorkloadOrdinalOp
  • IREE::Flow::DispatchTensorLoadOp
  • IREE::Flow::DispatchTensorStoreOp
  • IREE::Flow::DispatchWorkgroupCountFromSliceOp
  • IREE::Flow::DispatchWorkgroupCountFromDagRootOp -> moved because it shares a base class with DispatchWorkgroupCountFromSliceOp
  • IREE::Flow::DispatchTensorType

Resolves: #20249

Note for downstream users, you can use the following commands (in the provided order) to help update your codebase for these changes:

# Update the op names in the MLIR files
grep -rl flow.dispatch.workload.ordinal | xargs -d '\n' sed -i 's/flow.dispatch.workload.ordinal/iree_tensor_ext.dispatch.workload.ordinal/g'
grep -rl flow.dispatch.tensor | xargs -d '\n' sed -i 's/flow.dispatch.tensor/iree_tensor_ext.dispatch.tensor/g'
grep -rl flow.dispatch.workgroup_count_from | xargs -d '\n' sed -i 's/flow.dispatch.workgroup_count_from/iree_tensor_ext.dispatch.workgroup_count_from/g'

# Add 'TensorExtOps.h' header to files using the ops
{ grep -rl 'IREE::Flow::DispatchWorkloadOrdinalOp' ./; grep -rl 'IREE::Flow::DispatchTensorLoadOp' ./; grep -rl 'IREE::Flow::DispatchTensorStoreOp' ./; grep -rl 'IREE::Flow::DispatchWorkgroupCount' ./; } | xargs -d '\n' sed -i '/#include "iree\/compiler\/Dialect\/Flow\/IR\/FlowOps.h"/a #include "iree/compiler/Dialect/TensorExt/IR/TensorExtOps.h"'

# Add 'TensorExtTypes.h' header to files using 'DispatchTensorType'
grep -rl 'IREE::Flow::DispatchTensorType' ./ | xargs -d '\n' sed -i '/#include "iree\/compiler\/Dialect\/Flow\/IR\/FlowTypes.h"/a #include "iree/compiler/Dialect/TensorExt/IR/TensorExtTypes.h"'

# Update the op names
grep -rl 'IREE::Flow::DispatchWorkloadOrdinalOp' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchWorkloadOrdinalOp/IREE::TensorExt::DispatchWorkloadOrdinalOp/g'
grep -rl 'IREE::Flow::DispatchTensorLoadOp' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchTensorLoadOp/IREE::TensorExt::DispatchTensorLoadOp/g'
grep -rl 'IREE::Flow::DispatchTensorStoreOp' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchTensorStoreOp/IREE::TensorExt::DispatchTensorStoreOp/g'
grep -rl 'IREE::Flow::DispatchWorkgroupCount' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchWorkgroupCount/IREE::TensorExt::DispatchWorkgroupCount/g'

# Update 'DispatchTensorType' 
grep -rl 'IREE::Flow::DispatchTensorType' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchTensorType/IREE::TensorExt::DispatchTensorType/g'

Afterwards, you might need to perform following updates manually:

  • Remove FlowOps.h and FlowTypes.h include lines from files where they are not needed anymore.
  • Update BUILD.bazel and/or CmakeLists.txt files to include the new TensorExt dependency and potentially remove Flow dependencies if possible. Note that you can use python build_tools/bazel_to_cmake/bazel_to_cmake.py to automatically update cmake files once you have updated the BUILD.bazel (if using bazel).
  • Lint your code using pre-commit run --all-files, clang-format or your own linter if applicable.

@hanhanW

hanhanW commented Apr 16, 2025 •

Copy link
Copy Markdown
Contributor

My github review UI sucks because there are too many files. It also happens when I set filters on the files. So I'm going to review it in few steps on my local machine:

  • Check bazel BUILD files.
  • Check if the changes are all flow.* -> iree_tensor_ext.* for MLIR files.
  • Check cpp/h files.
  • Review the new files under TensorExt/

For bazel BUILD files, I think we can remove more Flow deps in a follow-up. Some CodeGen passes still depend on flow dialect, and I can help clean up later. For this PR, I think we can scope it to pure migration, to reduce review complexity. The removal of deps can be done later because the PR does not add "new" dependencies. The only new dependency should all be TensorExt. Does it sound good?

@hanhanW

hanhanW commented Apr 16, 2025

Copy link
Copy Markdown
Contributor

This is going to be a great cleanup, many thanks to @jtuyls!

@jtuyls

jtuyls commented Apr 16, 2025

Copy link
Copy Markdown
Contributor Author

For bazel BUILD files, I think we can remove more Flow deps in a follow-up. Some CodeGen passes still depend on flow dialect, and I can help clean up later. For this PR, I think we can scope it to pure migration, to reduce review complexity. The removal of deps can be done later because the PR does not add "new" dependencies. The only new dependency should all be TensorExt. Does it sound good?

Yeah, sounds good, thanks. I tried to remove all Flow deps that I could, but there are indeed other places where codegen passes still depend on the Flow dialect and keeping this PR as a pure migration is good to reduce review complexity.

@jtuyls
jtuyls force-pushed the flow-to-tensor-migration branch from 3ddd5c3 to 58ad601 Compare April 16, 2025 23:24
@hanhanW

hanhanW commented Apr 17, 2025

Copy link
Copy Markdown
Contributor

RE mlir files: Skimmed through the diff, which looks good to me. The file was generated by running git diff HEAD~ **/*.mlir > ~/mlir.patch.

Most of them are replacing flow with iree_tensor_ext, except:

  • TensorExt/IR/test/dispatch_tensor_folding.mlir
  • TensorExt/IR/test/dispatch_workload_ordinal_folding.mlir

I confirmed that the dispatch_tensor_folding.mlir is moved from Flow to TensorExt with the replacement: https://gist.github.com/hanhanW/9764fa9539dc4954ef617e9542c6b15c

There are three tests are moved from Flow/IR/test/dispatch_workgroups_folding.mlir to dispatch_workload_ordinal_folding.mlir with the replacement, too.

@hanhanW

hanhanW commented Apr 17, 2025

Copy link
Copy Markdown
Contributor

@hanhanW

hanhanW commented Apr 17, 2025

Copy link
Copy Markdown
Contributor

These three diff files, that exclude the changes in TensorExt/, are generated by the below command. (cpp, .h, .td)

cd compiler/src/iree/compiler
gd HEAD~ **/*.h ':(exclude)Dialect/TensorExt/*' > ~/exclude_tensor_ext_h.patc
gd HEAD~ **/*.cpp ':(exclude)Dialect/TensorExt/*' > ~/exclude_tensor_ext_h.patch
gd HEAD~ **/*.td ':(exclude)Dialect/TensorExt/*' > ~/exclude_tensor_ext_td.patc

The replacement part looks good to me. There are some removal looks reasonable, and I believe that they are moved to TensorExt/. I'll review this part later.

@jtuyls
jtuyls force-pushed the flow-to-tensor-migration branch 3 times, most recently from 9d84146 to 2104ef6 Compare April 17, 2025 14:07
@hanhanW

hanhanW commented Apr 17, 2025

Copy link
Copy Markdown
Contributor

I reviewed the other TensorExt files and most of the code are moved from Flow with dialect name update. Overall it looks good to me.

I think the next question is how to land it properly. @ScottTodd for this kind of change, do we need to announce it somewhere?

@jtuyls do you have some scripts that can help downstream projects integrate this? If so, can you add them to PR description?

I have few commands in my mind, and I can share it here. For mlir files, I think we'll need the commands for the replacement, like below. We'll need some similar commands for other operations and the type.

find ./ -type f -exec sed -i 's/flow.dispatch.workload.ordinal/iree_tensor_ext.dispatch.workload.ordinal/g' {} \;

And the below command helps add the includes to c++ files. It adds TensorExtOps.h to the file when the file includes the FlowOps.h. We'll need some similar commands for other headers, like TensorExtDialect.h.

sed -i '/#include "iree\/compiler\/Dialect\/Flow\/IR\/FlowOps.h"/a #include "iree/compiler/Dialect/TensorExt/IR/TensorExtOps.h"' $(grep -rl '#include "iree/compiler/Dialect/Flow/IR/FlowOps.h"' ./compiler/)

And we can do something similar for BUILD files and run python build_tools/bazel_to_cmake/bazel_to_cmake.py to fix cmake files.

Additionally, they need to run clang-format and buildifier to fix lint issues. (buildifier is not required if they can fix the BUILD files manually.)

@jtuyls

jtuyls commented Apr 17, 2025 •

Copy link
Copy Markdown
Contributor Author

I reviewed the other TensorExt files and most of the code are moved from Flow with dialect name update. Overall it looks good to me.

I think the next question is how to land it properly. @ScottTodd for this kind of change, do we need to announce it somewhere?

I think Scott is on holiday, so we might want to check with someone else. And I think we could mention it somewhere on an appropriate channel on the IREE discord?

@jtuyls do you have some scripts that can help downstream projects integrate this? If so, can you add them to PR description?

I have few commands in my mind, and I can share it here. For mlir files, I think we'll need the commands for the replacement, like below. We'll need some similar commands for other operations and the type.

find ./ -type f -exec sed -i 's/flow.dispatch.workload.ordinal/iree_tensor_ext.dispatch.workload.ordinal/g' {} \;

And the below command helps add the includes to c++ files. It adds TensorExtOps.h to the file when the file includes the FlowOps.h. We'll need some similar commands for other headers, like TensorExtDialect.h.

sed -i '/#include "iree\/compiler\/Dialect\/Flow\/IR\/FlowOps.h"/a #include "iree/compiler/Dialect/TensorExt/IR/TensorExtOps.h"' $(grep -rl '#include "iree/compiler/Dialect/Flow/IR/FlowOps.h"' ./compiler/)

I think sed commands are the most useful to help update the files so I added them to PR description. I updated them a bit to look for the op names instead of the FlowOps.h header. Could you check it?

And we can do something similar for BUILD files and run python build_tools/bazel_to_cmake/bazel_to_cmake.py to fix cmake files.

Additionally, they need to run clang-format and buildifier to fix lint issues. (buildifier is not required if they can fix the BUILD files manually.)

I am not sure how to best update the BUILD.bazel files automatically. Just because they have a flow dependency doesn't mean that they need to have a TensorExt dependency after this change as the updated ops might not be used in the respective code files.

@hanhanW hanhanW 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.

It looks good, I have a quick chat with Mahesh on discord. Let's land it early next week because Canada/UK folks have holidays.

I'm approving the PR, so you can rebase and land it after you're back from holidays, if you have the holidays.

@jtuyls

jtuyls commented Apr 17, 2025

Copy link
Copy Markdown
Contributor Author

It looks good, I have a quick chat with Mahesh on discord. Let's land it early next week because Canada/UK folks have holidays.

I'm approving the PR, so you can rebase and land it after you're back from holidays, if you have the holidays.

Ok, thanks, sounds good.

@jtuyls
jtuyls force-pushed the flow-to-tensor-migration branch from 2104ef6 to e9eae44 Compare April 22, 2025 09:02
@jtuyls
jtuyls merged commit 9902283 into iree-org:main Apr 22, 2025
@hanhanW

hanhanW commented Apr 28, 2025

Copy link
Copy Markdown
Contributor

@jtuyls do you have cycles to address the post comments? I can help if you are busy on other things.

@jtuyls

jtuyls commented Apr 29, 2025

Copy link
Copy Markdown
Contributor Author

@jtuyls do you have cycles to address the post comments? I can help if you are busy on other things.

Yes, I can address the above comments.

@jtuyls jtuyls mentioned this pull request Mar 31, 2025
5 of 6 tasks
@jtuyls
jtuyls deleted the flow-to-tensor-migration branch April 29, 2025 07:58
yzhang93 added a commit to nod-ai/iree-amd-aie that referenced this pull request Apr 29, 2025
Update `Flow` dependencies to `TensorExt` according to IREE changes
iree-org/iree#20564.
KyleHerndon pushed a commit to KyleHerndon/iree that referenced this pull request May 7, 2025
)

Migrates a set of Flow constructs to TensorExt to reduce the dependency
in Stream/Codegen on Flow and to avoid a circular dependency from
Encoding on Flow encountered during the MaterializeEncodingIntoPadding
collapsing work
(iree-org#20160 (comment)):
- IREE::Flow::DispatchWorkloadOrdinalOp
- IREE::Flow::DispatchTensorLoadOp
- IREE::Flow::DispatchTensorStoreOp
- IREE::Flow::DispatchWorkgroupCountFromSliceOp
- IREE::Flow::DispatchWorkgroupCountFromDagRootOp -> moved because it
shares a base class with DispatchWorkgroupCountFromSliceOp
- IREE::Flow::DispatchTensorType

Resolves: [iree-org#20249](iree-org#20249)

Note for downstream users, you can use the following commands (in the
provided order) to help update your codebase for these changes:
```
# Update the op names in the MLIR files
grep -rl flow.dispatch.workload.ordinal | xargs -d '\n' sed -i 's/flow.dispatch.workload.ordinal/iree_tensor_ext.dispatch.workload.ordinal/g'
grep -rl flow.dispatch.tensor | xargs -d '\n' sed -i 's/flow.dispatch.tensor/iree_tensor_ext.dispatch.tensor/g'
grep -rl flow.dispatch.workgroup_count_from | xargs -d '\n' sed -i 's/flow.dispatch.workgroup_count_from/iree_tensor_ext.dispatch.workgroup_count_from/g'

# Add 'TensorExtOps.h' header to files using the ops
{ grep -rl 'IREE::Flow::DispatchWorkloadOrdinalOp' ./; grep -rl 'IREE::Flow::DispatchTensorLoadOp' ./; grep -rl 'IREE::Flow::DispatchTensorStoreOp' ./; grep -rl 'IREE::Flow::DispatchWorkgroupCount' ./; } | xargs -d '\n' sed -i '/#include "iree\/compiler\/Dialect\/Flow\/IR\/FlowOps.h"/a #include "iree/compiler/Dialect/TensorExt/IR/TensorExtOps.h"'

# Add 'TensorExtTypes.h' header to files using 'DispatchTensorType'
grep -rl 'IREE::Flow::DispatchTensorType' ./ | xargs -d '\n' sed -i '/#include "iree\/compiler\/Dialect\/Flow\/IR\/FlowTypes.h"/a #include "iree/compiler/Dialect/TensorExt/IR/TensorExtTypes.h"'

# Update the op names
grep -rl 'IREE::Flow::DispatchWorkloadOrdinalOp' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchWorkloadOrdinalOp/IREE::TensorExt::DispatchWorkloadOrdinalOp/g'
grep -rl 'IREE::Flow::DispatchTensorLoadOp' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchTensorLoadOp/IREE::TensorExt::DispatchTensorLoadOp/g'
grep -rl 'IREE::Flow::DispatchTensorStoreOp' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchTensorStoreOp/IREE::TensorExt::DispatchTensorStoreOp/g'
grep -rl 'IREE::Flow::DispatchWorkgroupCount' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchWorkgroupCount/IREE::TensorExt::DispatchWorkgroupCount/g'

# Update 'DispatchTensorType' 
grep -rl 'IREE::Flow::DispatchTensorType' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchTensorType/IREE::TensorExt::DispatchTensorType/g'
```
Afterwards, you might need to perform following updates manually:
- Remove `FlowOps.h` and `FlowTypes.h` include lines from files where
they are not needed anymore.
- Update `BUILD.bazel` and/or `CmakeLists.txt` files to include the new
`TensorExt` dependency and potentially remove `Flow` dependencies if
possible. Note that you can use `python
build_tools/bazel_to_cmake/bazel_to_cmake.py` to automatically update
cmake files once you have updated the `BUILD.bazel` (if using bazel).
- Lint your code using `pre-commit run --all-files`, `clang-format` or
your own linter if applicable.

Signed-off-by: Jorn Tuyls <[email protected]>
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
Migrates a set of Flow constructs to TensorExt to reduce the dependency
in Stream/Codegen on Flow and to avoid a circular dependency from
Encoding on Flow encountered during the MaterializeEncodingIntoPadding
collapsing work
(#20160 (comment)):
- IREE::Flow::DispatchWorkloadOrdinalOp
- IREE::Flow::DispatchTensorLoadOp
- IREE::Flow::DispatchTensorStoreOp
- IREE::Flow::DispatchWorkgroupCountFromSliceOp
- IREE::Flow::DispatchWorkgroupCountFromDagRootOp -> moved because it
shares a base class with DispatchWorkgroupCountFromSliceOp
- IREE::Flow::DispatchTensorType

Resolves: [#20249](#20249)

Note for downstream users, you can use the following commands (in the
provided order) to help update your codebase for these changes:
```
# Update the op names in the MLIR files
grep -rl flow.dispatch.workload.ordinal | xargs -d '\n' sed -i 's/flow.dispatch.workload.ordinal/iree_tensor_ext.dispatch.workload.ordinal/g'
grep -rl flow.dispatch.tensor | xargs -d '\n' sed -i 's/flow.dispatch.tensor/iree_tensor_ext.dispatch.tensor/g'
grep -rl flow.dispatch.workgroup_count_from | xargs -d '\n' sed -i 's/flow.dispatch.workgroup_count_from/iree_tensor_ext.dispatch.workgroup_count_from/g'

# Add 'TensorExtOps.h' header to files using the ops
{ grep -rl 'IREE::Flow::DispatchWorkloadOrdinalOp' ./; grep -rl 'IREE::Flow::DispatchTensorLoadOp' ./; grep -rl 'IREE::Flow::DispatchTensorStoreOp' ./; grep -rl 'IREE::Flow::DispatchWorkgroupCount' ./; } | xargs -d '\n' sed -i '/#include "iree\/compiler\/Dialect\/Flow\/IR\/FlowOps.h"/a #include "iree/compiler/Dialect/TensorExt/IR/TensorExtOps.h"'

# Add 'TensorExtTypes.h' header to files using 'DispatchTensorType'
grep -rl 'IREE::Flow::DispatchTensorType' ./ | xargs -d '\n' sed -i '/#include "iree\/compiler\/Dialect\/Flow\/IR\/FlowTypes.h"/a #include "iree/compiler/Dialect/TensorExt/IR/TensorExtTypes.h"'

# Update the op names
grep -rl 'IREE::Flow::DispatchWorkloadOrdinalOp' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchWorkloadOrdinalOp/IREE::TensorExt::DispatchWorkloadOrdinalOp/g'
grep -rl 'IREE::Flow::DispatchTensorLoadOp' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchTensorLoadOp/IREE::TensorExt::DispatchTensorLoadOp/g'
grep -rl 'IREE::Flow::DispatchTensorStoreOp' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchTensorStoreOp/IREE::TensorExt::DispatchTensorStoreOp/g'
grep -rl 'IREE::Flow::DispatchWorkgroupCount' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchWorkgroupCount/IREE::TensorExt::DispatchWorkgroupCount/g'

# Update 'DispatchTensorType' 
grep -rl 'IREE::Flow::DispatchTensorType' | xargs -d '\n' sed -i 's/IREE::Flow::DispatchTensorType/IREE::TensorExt::DispatchTensorType/g'
```
Afterwards, you might need to perform following updates manually:
- Remove `FlowOps.h` and `FlowTypes.h` include lines from files where
they are not needed anymore.
- Update `BUILD.bazel` and/or `CmakeLists.txt` files to include the new
`TensorExt` dependency and potentially remove `Flow` dependencies if
possible. Note that you can use `python
build_tools/bazel_to_cmake/bazel_to_cmake.py` to automatically update
cmake files once you have updated the `BUILD.bazel` (if using bazel).
- Lint your code using `pre-commit run --all-files`, `clang-format` or
your own linter if applicable.

Signed-off-by: Jorn Tuyls <[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.

Move Flow ops that are depended by CodeGen/Stream to TensorExt dialect

3 participants