You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "OmarAzizi (via GitHub)" <gi...@apache.org> on 2026/04/02 23:32:57 UTC
[PR] [Frontend][ONNX] Support select_last_index for ArgMax and ArgMin [tvm]
OmarAzizi opened a new pull request, #18969:
URL: https://github.com/apache/tvm/pull/18969
### Summary
This PR implements the `select_last_index` attribute (introduced in opset 12) for the `ArgMax` and `ArgMin` ONNX operators.
Previously, setting `select_last_index=1` raised `OpAttributeUnImplemented`. This closes the limitation tracked in the ONNX frontend issue.
### Implementation
When `select_last_index=1`, the input tensor is reversed along the reduction axis using `relax.op.flip`, argmax/argmin is computed on the flipped copy, and the result is remapped back to the original index space via `last_idx = (axis_size - 1) - flipped_idx`
Closes part of #18945
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Frontend][ONNX] Support select_last_index for ArgMax and ArgMin [tvm]
Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #18969:
URL: https://github.com/apache/tvm/pull/18969#discussion_r3030815488
##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -3788,10 +3786,17 @@ def _impl_v12(cls, bb, inputs, attr, params):
axis, keepdims = cls._check_attrs(data, attr)
select_last_index = attr.get("select_last_index", False)
if select_last_index:
- # TODO(vvchernov): support attr
- raise tvm.error.OpAttributeUnImplemented(
- "'select_last_index' attribute has not been supported yet"
+ data_flipped = relax.op.flip(data, axis=axis)
+ flipped_idx = bb.normalize(relax.op.argmax(data_flipped, axis, keepdims))
+ axis_size = int(inputs[0].struct_info.shape[axis])
+ offset = bb.normalize(
+ relax.op.full(
+ flipped_idx.struct_info.shape,
+ relax.const(axis_size - 1, "int64"),
+ dtype="int64",
+ )
)
Review Comment:

The use of `int(inputs[0].struct_info.shape[axis])` assumes that the size of the reduction axis is a static integer. This will fail for models with dynamic shapes, where a shape dimension can be a `tir.Var`. To correctly handle dynamic shapes, you should use `relax.op.shape_of` to obtain the axis size at runtime and create the offset as a `relax.PrimValue`.
```python
offset = bb.normalize(
relax.op.full(
flipped_idx.struct_info.shape,
relax.PrimValue(relax.op.shape_of(data)[axis] - 1),
dtype="int64",
)
)
```
##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -3826,10 +3831,17 @@ def _impl_v12(cls, bb, inputs, attr, params):
axis, keepdims = cls._check_attrs(data, attr)
select_last_index = attr.get("select_last_index", False)
if select_last_index:
- # TODO(vvchernov): support attr
- raise tvm.error.OpAttributeUnImplemented(
- "'select_last_index' attribute has not been supported yet"
+ data_flipped = relax.op.flip(data, axis=axis)
+ flipped_idx = bb.normalize(relax.op.argmin(data_flipped, axis, keepdims))
+ axis_size = int(inputs[0].struct_info.shape[axis])
+ offset = bb.normalize(
+ relax.op.full(
+ flipped_idx.struct_info.shape,
+ relax.const(axis_size - 1, "int64"),
+ dtype="int64",
+ )
)
+ return relax.op.subtract(offset, flipped_idx)
return relax.op.argmin(data, axis, keepdims)
Review Comment:

This implementation for `select_last_index` is nearly identical to the one for `ArgMax`. To improve maintainability and avoid code duplication, consider refactoring this logic into a shared helper function. This would also allow you to apply the fix for the dynamic shape issue (mentioned in the `ArgMax` comment) in a single place.
A possible refactoring could look like this:
```python
def _get_last_index(bb, data, axis, keepdims, op):
data_flipped = relax.op.flip(data, axis=axis)
flipped_idx = bb.normalize(op(data_flipped, axis, keepdims))
offset = bb.normalize(
relax.op.full(
flipped_idx.struct_info.shape,
relax.PrimValue(relax.op.shape_of(data)[axis] - 1),
dtype="int64",
)
)
return relax.op.subtract(offset, flipped_idx)
...
# in ArgMax._impl_v12
if select_last_index:
return _get_last_index(bb, data, axis, keepdims, relax.op.argmax)
...
# in ArgMin._impl_v12
if select_last_index:
return _get_last_index(bb, data, axis, keepdims, relax.op.argmin)
...
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [Frontend][ONNX] Support select_last_index for ArgMax and ArgMin [tvm]
Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex merged PR #18969:
URL: https://github.com/apache/tvm/pull/18969
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org