Skip to content

[LinalgExt] fix arg_compare op with region and start index - #21106

Merged
bangtianliu merged 6 commits into
iree-org:mainfrom
bangtianliu:fixes_argmax
Jun 19, 2025
Merged

bangtianliu merged 6 commits into
iree-org:mainfrom
bangtianliu:fixes_argmax

Conversation

@bangtianliu

@bangtianliu bangtianliu commented Jun 16, 2025 •

Copy link
Copy Markdown
Contributor

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 arg_compare (argmax) op
  • Introduces an optional index_base operand to specify the starting index offset.

@bangtianliu
bangtianliu marked this pull request as draft June 16, 2025 21:18
@bangtianliu
bangtianliu marked this pull request as ready for review June 16, 2025 21:29
@bangtianliu
bangtianliu requested review from IanWood1 and kuhar June 16, 2025 21:29

@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 think the op needs to be renamed to arg_compare?

Ian pointed it out as well, I think you need some better verifiers in the region?. You essentially want to check that this is a binary comparator operator, i.e. its a (T a, T b) -> T c.

Actually working through this a bit more, I think we need this to be (T a, index a_index, Tb, index b_index) -> (T, index) . Reason is you need to compute the argmax or argmin. The current region will not let you do this.

Easy way to verify that you have what is needed is to implement the lowering of this operation to loops. If you can lower this to loops, then we should be able to do tiling etc. on it.

Signed-off-by: Bangtian Liu <[email protected]>
@bangtianliu

bangtianliu commented Jun 17, 2025 •

Copy link
Copy Markdown
Contributor Author

I think the op needs to be renamed to arg_compare?

Ian pointed it out as well, I think you need some better verifiers in the region?. You essentially want to check that this is a binary comparator operator, i.e. its a (T a, T b) -> T c.

Actually working through this a bit more, I think we need this to be (T a, index a_index, Tb, index b_index) -> (T, index) . Reason is you need to compute the argmax or argmin. The current region will not let you do this.

Easy way to verify that you have what is needed is to implement the lowering of this operation to loops. If you can lower this to loops, then we should be able to do tiling etc. on it.

Thanks for the feedback. I just sent out a commit that improves the verifier based on Ian's earlier comment. Regarding your suggestion here, I agree this requires further refinement to properly support argmax/argmin semantics (and also (T a, index a_index, Tb, index b_index) -> (T, index)). I’ll continue to implement the lowering of this operation to loops in this PR.

@bangtianliu

Copy link
Copy Markdown
Contributor Author

@MaheshRavishankar One question about the proposed region type (T a, index a_index, Tb, index b_index) -> (T, index): currently, the region in current implementation is defined as (T a, T b) -> i1 not (T a, T b) -> T c , following the approach used in the topk implementation. Since topk is conceptually similar to argmax, I thought it would be reasonable to adopt the same region structure. Please correct me if I’m missing something.

@IanWood1

IanWood1 commented Jun 17, 2025 •

Copy link
Copy Markdown
Member

@MaheshRavishankar One question about the proposed region type (T a, index a_index, Tb, index b_index) -> (T, index): currently, the region in current implementation is defined as (T a, T b) -> i1 not (T a, T b) -> T c , following the approach used in the topk implementation. Since topk is conceptually similar to argmax, I thought it would be reasonable to adopt the same region structure. Please correct me if I’m missing something.

(T a, T b) -> i1 made sense to me during review. But +1 on changing the name

My understanding was that it would be equivalent to this pseudo-code:

# `elems` represents the input tensor.
# `pred` represents the region as a predicate that takes the current val and the current max val.
def argMax(elems, pred):
	maxVal = elems[0]
	maxIdx = 0
	for i, elem in enumerate(elems):
		if pred(elem, maxVal):
			maxVal = elem
			maxIdx = i
	return maxVal, maxIdx

@MaheshRavishankar

MaheshRavishankar commented Jun 17, 2025 •

Copy link
Copy Markdown
Collaborator

Can you clarify what the yield is supposed to mean? What is the Boolean supposed to indicate?

EDIT : I see from @IanWood1 s explanation what it is. Ok, this makes sense then

Signed-off-by: Bangtian Liu <[email protected]>
@bangtianliu

Copy link
Copy Markdown
Contributor Author

Just sent the commit about op renaming. I will add the implementation of lowering to loops next.

@bangtianliu bangtianliu changed the title [LinalgExt] fix argmax op with region and start index [LinalgExt] fix arg_compare op with region and start index Jun 17, 2025

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

This looks fine, but it will be clear after the loop implementation if we missed something.

@bangtianliu
bangtianliu requested a review from kuhar June 19, 2025 17:09
Signed-off-by: Bangtian Liu <[email protected]>
@bangtianliu
bangtianliu merged commit 20fe312 into iree-org:main Jun 19, 2025
bangtianliu added a commit that referenced this pull request Jun 24, 2025
On top of #21106.

This PR Adds `generateScalarImplementation` for ArgCompareOp, defining
the scalar behavior within the innermost loop during loop lowering.

The lowering performs the following: 
- Loads the current best value and corresponding index (accumulator)
from the output tensor.
- Loads the candidate value at the current loop indices.
- Invokes the provided comparator region (T a, T b) -> i1 to compare
values.
- Selects the new best value and index based on the comparison result.
- Supports an optional index_base operand, which is added to the
computed index before storing.

Next, will add tiling support.

---------

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]>
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
On top of #21106.

This PR Adds `generateScalarImplementation` for ArgCompareOp, defining
the scalar behavior within the innermost loop during loop lowering.

The lowering performs the following: 
- Loads the current best value and corresponding index (accumulator)
from the output tensor.
- Loads the candidate value at the current loop indices.
- Invokes the provided comparator region (T a, T b) -> i1 to compare
values.
- Selects the new best value and index based on the comparison result.
- Supports an optional index_base operand, which is added to the
computed index before storing.

Next, will add tiling support.

---------

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