Skip to content

[LinalgExt] Add argmax op with rountrip and invalid mlir test - #21021

Merged
bangtianliu merged 4 commits into
iree-org:mainfrom
bangtianliu:linalg_ext_argmax
Jun 9, 2025
Merged

bangtianliu merged 4 commits into
iree-org:mainfrom
bangtianliu:linalg_ext_argmax

Conversation

@bangtianliu

Copy link
Copy Markdown
Contributor

This PR introduces the iree_linalg_ext.argmax operation and defines its verifier along with corresponding roundtrip and invalid tests (roundtrip.mlir and invalid.mlir).

Support for argmax will be added incrementally across multiple PRs:

  • Add the iree_linalg_ext.argmax op definition and verification logic, along with roundtrip and invalid IR tests (this PR)
  • Add tiling support
  • Add convert_to_loops lowering support
  • Add e2e support with other changes in compiler/Codegen/Interfaces (also split-k and ukernel)

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

drive-by: to make this op work e2e, you'll need to implement tiling interface, generateScalarImplementation, and add interface registration like this.

The interface registration is a little annoying. IMO, it can be fixed, but I did not get cycles on this. How to make it work is documented in the issue: #20862

Signed-off-by: Bangtian Liu <[email protected]>
@bangtianliu
bangtianliu requested a review from kuhar June 6, 2025 15:37
Signed-off-by: Bangtian Liu <[email protected]>
@bangtianliu
bangtianliu requested a review from kuhar June 6, 2025 19:16

@kuhar kuhar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks OK % nits.

I guess the next step is to implement the interfaces mentioned by @hanhanW?

@bangtianliu

Copy link
Copy Markdown
Contributor Author

Looks OK % nits.

I guess the next step is to implement the interfaces mentioned by @hanhanW?

Yeah, just step by step send all the required PRs.

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

Sorry I wasnt able to review this PR before it landed, but I think it might be better to address comments here before moving forward.

let arguments = (ins
Variadic<AnyShaped>:$inputs,
Variadic<AnyShaped>:$outputs,
I64Attr:$dimension

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.

The intent of this operation is that this should be tilable. To make that correct I think we need a couple of mor things.

  1. I think defining an operation just for argmax is not scalable. I think we should define an operation that llows you to do "max" of any operation. So it might be better to add an operation that allows you to specify different functions to compute the "max".

Basically do

%0:2 = iree_linalg_ext.arg_compare ins(%0 : tensor<?x?xf32>) outs(...) dimension(...) {
  ^bb0(%b0 : f32, %b1: f32):
    %1 = arith.cmpf ge, %b0, %b1 : f32
    iree_linalg_ext.yield %1 : f32
} -> tensor<?xf32>

(Think how you would do this with something like C++ std lib which generally allows you to take a comparator function, this is essentially that).

  1. I think you also need an additional argument that represents the "global start index", that is by default 0. After tiling you will need to update the global start index appropriately.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I actually considered both adding a region and supporting a global start index.
My initial plan was to start with a basic implementation to get the core functionality in place, and then extend it to support custom comparators via regions for more flexibility . Similarly, the global start index will be important for correctness when enabling split-K.
Thanks for the suggestions, I'll make sure to address both aspects in the follow-up PR.

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.

That would create too much churn no? It will be a pain to update all the tests after the fact etc. I think it would be better to just add this from the get go.
If we find out later that we missed something then there is nothing we can do, but we might as well start with accounting for things we already know?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It will be a pain to update all the tests after the fact etc

Ok, then I will add all the required changes directly to the second PR #21077.

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.

Ok that's fine. Will be easier to stage it, i.e. fix the op and then the PR on top of that, but your call

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok that's fine. Will be easier to stage it, i.e. fix the op and then the PR on top of that, but your call

Sure, sounds good and I’ll follow your suggestion. I’ll first send a follow-up to address the argmax op fixes, then rebase and update the tiling PR accordingly. This should help keep the diffs cleaner and easier to review.

bangtianliu added a commit that referenced this pull request Jun 19, 2025
This is a follow-up PR to address feedback on the previously landed PR
for the argmax op #21021 according to comments in
#21021 (comment):
- Adds support for a comparator region in the argmax op
- Introduces an optional index_base operand to specify the starting
index offset.

---------

Signed-off-by: Bangtian Liu <[email protected]>
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
This PR introduces the `iree_linalg_ext.argmax` operation and defines
its verifier along with corresponding roundtrip and invalid tests
(`roundtrip.mlir` and `invalid.mlir`).

Support for argmax will be added incrementally across multiple PRs:
- Add the iree_linalg_ext.argmax op definition and verification logic,
along with roundtrip and invalid IR tests (this PR)
- Add tiling support 
- Add convert_to_loops lowering support 
- Add e2e support with other changes in compiler/Codegen/Interfaces
(also split-k and ukernel)

---------

Signed-off-by: Bangtian Liu <[email protected]>
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
This is a follow-up PR to address feedback on the previously landed PR
for the argmax op #21021 according to comments in
#21021 (comment):
- Adds support for a comparator region in the argmax op
- Introduces an optional index_base operand to specify the starting
index offset.

---------

Signed-off-by: Bangtian Liu <[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.

4 participants