Skip to content

[LinalgExt] Extend arg_compare to support both value and index provided ( explicit-index mode) - #23153

Merged
bangtianliu merged 2 commits into
iree-org:mainfrom
bangtianliu:arg_compare_fix
Jan 19, 2026
Merged

bangtianliu merged 2 commits into
iree-org:mainfrom
bangtianliu:arg_compare_fix

Conversation

@bangtianliu

@bangtianliu bangtianliu commented Jan 15, 2026 •

Copy link
Copy Markdown
Contributor

This PR extends the arg_compare operation to accept an optional second input for pre-computed indices, addressing a critical limitation discovered while implementing VectorDistribute pipeline support for argmax/argmin operations.

Motivation

While working on arg_compare support along the VectorDistribute pipeline, we encountered a fundamental issue with the merge reduction phase after partial reduction. Each tile produces a (max value, index) pair, and merging them creates this pattern:

  %19:2 = linalg.generic {
   indexing_maps = [...], iterator_types = ["reduction"]
 } ins(%18#0, %18#1 : tensor<2048xf16>, tensor<2048xi32>)
   outs(%12, %13 : tensor<f16>, tensor<i32>) {
 ^bb0(%in: f16, %in_3: i32, %out: f16, %out_4: i32):
   %20 = arith.cmpf ogt, %in, %out : f16
   %21 = arith.select %20, %in, %out : f16
   %22 = arith.select %20, %in_3, %out_4 : i32
   linalg.yield %21, %22 : f16, i32
 }

The upstream MLIR vectorizer cannot handle this pattern. The paired-select reduction (selecting both value and index based on the same condition) doesn't fit the single-combiner model that generic vectorization expects.

This PR extends arg_compare to optionally accept index inputs, enabling it to handle both:

  1. Initial argmax: Compute (value, index) from scratch using iteration variables (linalg.index)
  2. Merge reduction: Merge partial (value, index) pairs from distributed tiles

This PR is mainly update the iree_linalg_ext.arg_compare op definition and verification logic, along with roundtrip and invalid IR tests.

After this PR, there will be a few PRs about

  • Add tiling support
  • Add convert_to_loops lowering support
  • Update partial reduction interface support.
  • Update e2e test.

Refer to discord discussion for more details: https://discordapp.com/channels/689900678990135345/1461079481803608158/1461079488770212096

@bangtianliu
bangtianliu marked this pull request as draft January 15, 2026 21:01
@bangtianliu
bangtianliu force-pushed the arg_compare_fix branch 10 times, most recently from 7e59430 to 4174879 Compare January 15, 2026 22:03
@bangtianliu
bangtianliu requested a review from kuhar January 15, 2026 22:28
@bangtianliu
bangtianliu marked this pull request as ready for review January 15, 2026 22:28
@bangtianliu bangtianliu changed the title [LinalgExt] Extend arg_compare to support explicit-index mode [LinalgExt] Extend arg_compare to support both value and index provided ( explicit-index mode) Jan 16, 2026
Comment on lines +755 to +766
The operation supports two modes:

This region defines the sorting rule, e.g., "greater than" for argmax or
"less than" for argmin. It allows for generalization beyond simple argmax-style
1. Implicit-index mode (single input): Computes (value, index) pairs from scratch.
Indices are implicitly computed from the reduction dimension's iteration index
(using linalg.index on the reduction dimension). The comparator
region receives two candidate values and returns a single `i1` result indicating
whether the first argument should be preferred.

2. Explicit-index mode (two inputs): Reduces existing (value, index) pairs.
Indices are explicitly provided as input. The comparator region receives
two candidate values and returns `i1` to select the preferred value.
This mode is used to combine results from split-reduction operations.

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.

Here's an alternative design that I think might be cleaner and more general. Instead of special-casing a second "index input," what if we treat all inputs uniformly? In the case of merge reduction, the arg compare op would receive the partial values and indices and treat them both as normal inputs:

%0:3 = iree_linalg_ext.arg_compare dimension(1)
  ins(%partial_vals, %partial_idxs : tensor<2x4xf32>, tensor<2x4xi32>)
  outs(%max_val, %max_idx, %out_idx : tensor<2xf32>, tensor<2xi32>, tensor<2xi32>) {
  ^bb0(%val_a: f32, %idx_a: i32, %val_b: f32, %idx_b: i32):
    %cmp = arith.cmpf ogt, %val_a, %val_b : f32
    iree_linalg_ext.yield %cmp : i1
}

The third output (%out_idx) would be left unused because we don't care about the index into the partial reduction tensors. This approach seems more natural to me because in the merge step we are just applying an arg max over a tuple of values. Also, it might be easier to write code for this op since handling two different modes might be tricky. However, the trade off is extra block args and an unused result. What do you think?

@bangtianliu bangtianliu Jan 16, 2026 •

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.

what if we treat all inputs uniformly

No, that's not the semantics of arg_compare. The inputs should be either a value array (implicit-index mode), or a value-index pair with matching shapes (explicit-index mode). The index input has special semantics as position tracking - it's not just another arbitrary input.

I'd also like to clarify the semantics of the merge step. It's not about applying an argmax over a tuple of values. Rather, each tile produces a single (value, index) pair, and the merge step performs a pairwise reduction using the same predicate logic on the values. The indices are simply carried along - whichever value wins the comparison, its corresponding index is selected.

Regarding the region: in our design, the comparator always takes 2 arguments (just values), keeping it uniform across both modes:

^bb0(%val_a: f32, %val_b: f32):
  %cmp = arith.cmpf ogt, %val_a, %val_b : f32
  iree_linalg_ext.yield %cmp : i1

The region is just used to define predication logic. why would we need to expose indices to the comparator? For argmax/argmin, the comparison is purely value-based.

I think making it too general would blur its purpose and potentially complicate both the implementation and understanding.

@bangtianliu
bangtianliu requested a review from kuhar January 16, 2026 23:27
Signed-off-by: Bangtian Liu <[email protected]>
bangtianliu added a commit that referenced this pull request Jan 19, 2026
…de (#23193)

This PR extends the tiling interface implementation for `ArgCompareOp`
to support explicit-index mode (2 inputs: value + pre-computed indices),
which was introduced in PR #23153.

 **Key changes:**
- Extended `getTiledImplementation` (introduced in PR #21077) to slice
both value and index inputs in explicit-index mode
- Extended `generateScalarImplementation` (convert_to_loops) (introduced
in PR #21138) to load indices from input instead of computing from
iteration variables in explicit-index mode

Signed-off-by: Bangtian Liu <[email protected]>
keshavvinayak01 pushed a commit that referenced this pull request Jan 27, 2026
…ed ( explicit-index mode) (#23153)

This PR extends the `arg_compare `operation to accept an optional second
input for pre-computed indices, addressing a critical limitation
discovered while implementing VectorDistribute pipeline support for
argmax/argmin operations.

## Motivation
While working on arg_compare support along the VectorDistribute
pipeline, we encountered a fundamental issue with the merge reduction
phase after partial reduction. Each tile produces a (max value, index)
pair, and merging them creates this pattern:

 ```mlir
   %19:2 = linalg.generic {
    indexing_maps = [...], iterator_types = ["reduction"]
  } ins(%18#0, %18#1 : tensor<2048xf16>, tensor<2048xi32>)
    outs(%12, %13 : tensor<f16>, tensor<i32>) {
  ^bb0(%in: f16, %in_3: i32, %out: f16, %out_4: i32):
    %20 = arith.cmpf ogt, %in, %out : f16
    %21 = arith.select %20, %in, %out : f16
    %22 = arith.select %20, %in_3, %out_4 : i32
    linalg.yield %21, %22 : f16, i32
  }
 ```
The upstream MLIR vectorizer cannot handle this pattern. The
paired-select reduction (selecting both value and index based on the
same condition) doesn't fit the single-combiner model that generic
vectorization expects.

This PR extends arg_compare to optionally accept index inputs, enabling
it to handle both:
1. Initial argmax: Compute (value, index) from scratch using iteration
variables (linalg.index)
2. Merge reduction: Merge partial (value, index) pairs from distributed
tiles

This PR is mainly update the iree_linalg_ext.arg_compare op definition
and verification logic, along with roundtrip and invalid IR tests.
After this PR, there will be a few PRs about
- Add tiling support
- Add convert_to_loops lowering support
  - Update partial reduction interface support.
  - Update e2e test.

Refer to discord discussion for more details:
https://discordapp.com/channels/689900678990135345/1461079481803608158/1461079488770212096

---------

Signed-off-by: Bangtian Liu <[email protected]>
Signed-off-by: Keshav Vinayak Jha <[email protected]>
keshavvinayak01 pushed a commit that referenced this pull request Jan 27, 2026
…de (#23193)

This PR extends the tiling interface implementation for `ArgCompareOp`
to support explicit-index mode (2 inputs: value + pre-computed indices),
which was introduced in PR #23153.

 **Key changes:**
- Extended `getTiledImplementation` (introduced in PR #21077) to slice
both value and index inputs in explicit-index mode
- Extended `generateScalarImplementation` (convert_to_loops) (introduced
in PR #21138) to load indices from input instead of computing from
iteration variables in explicit-index mode

Signed-off-by: Bangtian Liu <[email protected]>
Signed-off-by: Keshav Vinayak Jha <[email protected]>
MaheshRavishankar pushed a commit to MaheshRavishankar/iree that referenced this pull request Feb 24, 2026
…ed ( explicit-index mode) (iree-org#23153)

This PR extends the `arg_compare `operation to accept an optional second
input for pre-computed indices, addressing a critical limitation
discovered while implementing VectorDistribute pipeline support for
argmax/argmin operations.
 
## Motivation
While working on arg_compare support along the VectorDistribute
pipeline, we encountered a fundamental issue with the merge reduction
phase after partial reduction. Each tile produces a (max value, index)
pair, and merging them creates this pattern:
 
 ```mlir
   %19:2 = linalg.generic {
    indexing_maps = [...], iterator_types = ["reduction"]
  } ins(%18#0, %18#1 : tensor<2048xf16>, tensor<2048xi32>)
    outs(%12, %13 : tensor<f16>, tensor<i32>) {
  ^bb0(%in: f16, %in_3: i32, %out: f16, %out_4: i32):
    %20 = arith.cmpf ogt, %in, %out : f16
    %21 = arith.select %20, %in, %out : f16
    %22 = arith.select %20, %in_3, %out_4 : i32
    linalg.yield %21, %22 : f16, i32
  }
 ```
The upstream MLIR vectorizer cannot handle this pattern. The
paired-select reduction (selecting both value and index based on the
same condition) doesn't fit the single-combiner model that generic
vectorization expects.
 
This PR extends arg_compare to optionally accept index inputs, enabling
it to handle both:
1. Initial argmax: Compute (value, index) from scratch using iteration
variables (linalg.index)
2. Merge reduction: Merge partial (value, index) pairs from distributed
tiles

This PR is mainly update the iree_linalg_ext.arg_compare op definition
and verification logic, along with roundtrip and invalid IR tests.
After this PR, there will be a few PRs about
- Add tiling support
- Add convert_to_loops lowering support
  - Update partial reduction interface support.  
  - Update e2e test.   

Refer to discord discussion for more details:
https://discordapp.com/channels/689900678990135345/1461079481803608158/1461079488770212096

---------

Signed-off-by: Bangtian Liu <[email protected]>
MaheshRavishankar pushed a commit to MaheshRavishankar/iree that referenced this pull request Feb 24, 2026
…de (iree-org#23193)

This PR extends the tiling interface implementation for `ArgCompareOp`
to support explicit-index mode (2 inputs: value + pre-computed indices),
which was introduced in PR iree-org#23153.

 **Key changes:**
- Extended `getTiledImplementation` (introduced in PR iree-org#21077) to slice
both value and index inputs in explicit-index mode
- Extended `generateScalarImplementation` (convert_to_loops) (introduced
in PR iree-org#21138) to load indices from input instead of computing from
iteration variables in explicit-index mode

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.

3 participants