Skip to content

[Relax][Frontend][Torch] Add amax, amin and min.dim converters - #20375

Open
hiyufan wants to merge 1 commit into
apache:mainfrom
hiyufan:feat/relax-torch-amax-amin-min-dim
Open

hiyufan wants to merge 1 commit into
apache:mainfrom
hiyufan:feat/relax-torch-amax-amin-min-dim

Conversation

@hiyufan

@hiyufan hiyufan commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

What this adds

Three reductions that had no converter, so any model using them failed at import with Unsupported function types:

op ATen overload note
torch.amax(x, dim, keepdim) amax.default torch.logsumexp decomposes through it, so that was blocked too
torch.amin(x, dim, keepdim) amin.default
torch.min(x, dim, keepdim)(values, indices) min.dim max.dim was already supported

Found by a differential sweep of the frontend against torch.export (88 ops × 7 input shapes, static and dynamic); these accounted for 18 of the 90 programs that raised.

How

  • _amax_amin(op)relax.op.max / relax.op.min over dim, a list where empty means every axis (torch's convention; _sum handles the same case). Dispatched in both the exported-program and fx translators.
  • _max_dim becomes _max_min_dim(largest) and serves both max.dim and min.dim through topk(k=1, largest=…), the same way max.dim was already implemented. No behaviour change for max.dim; test_max_dim passes unchanged.

Verification

Reduction-op raises in the sweep go from 90 to 72 with this change; the one remaining program per op is the 0-d () input, which is a separate issue across all reductions and out of scope here. test_frontend_from_exported_program.py and test_frontend_from_fx.py: failure sets identical before and after apart from the new tests. ruff check / ruff format --check (v0.12.3) clean.

Tests

  • test_amax_amin — IR-level: amax over one axis, amin over two axes with keepdim, amax over every axis; numeric checks of the three plus logsumexp.
  • test_min_dim — IR-level for (values, indices) without keepdim; numeric check of both branches with values and indices compared against torch on a permutation input, so the argmin is unambiguous.

Both fail against the previous head:

test_amax_amin   AssertionError: Unsupported function types ['amax.default']
test_min_dim     AssertionError: Unsupported function types ['min.dim']

Independent of #20372 / #20373 / #20374 (branched from main).


This change was prepared with AI assistance (Claude). I have reviewed and verified it, and can speak to it in review.

`torch.amax` / `torch.amin` (`amax.default`, `amin.default`) and the `(values,
indices)` form of `torch.min(x, dim)` (`min.dim`) had no converter, so a model using
any of them failed at import with `Unsupported function types`. `max.dim` was
already supported, and `logsumexp` decomposes through `amax`, so it was blocked by
the same gap.

`_amax_amin` maps to `relax.op.max` / `relax.op.min` over `dim` (a list; empty means
every axis, as in torch). `_max_dim` becomes `_max_min_dim(largest)` and serves
both `max.dim` and `min.dim` through `topk(k=1)`, exactly as `max.dim` was already
handled. `amax` / `amin` are also dispatched in the fx translator.

In the differential sweep of the frontend against torch.export (88 ops x 7 input
shapes x static/dynamic), the reduction ops that raised drop from 90 to 72
programs; the ones left for these ops are the 0-d input case, which is a
separate, op-wide issue.

Tests: IR-level checks for amax over one axis, amin over two axes with keepdim, and
amax over every axis; numeric checks of those three plus logsumexp; and min.dim with
and without keepdim, values and indices compared with torch on a permutation input
so the argmin is unambiguous. Both new tests fail against the previous head with
`Unsupported function types ['amax.default']` / `['min.dim']`.
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.

1 participant