Skip to content

[Fix][Relax][ONNX] Support Hardmax over a symbolic axis extent - #20416

Open
LiRunGuo wants to merge 1 commit into
apache:mainfrom
LiRunGuo:fix/onnx-hardmax-symbolic-axis
Open

LiRunGuo wants to merge 1 commit into
apache:mainfrom
LiRunGuo:fix/onnx-hardmax-symbolic-axis

Conversation

@LiRunGuo

Copy link
Copy Markdown

Hardmax is lowered to argmax followed by one_hot, but relax.op.one_hot takes its depth as a static integer attribute. When the reduced axis has a symbolic extent, the importer passed a tir.Var as the depth and failed with TypeError: Mismatched type on argument #3 when calling relax.op.one_hot ... Expected int but got ir.Var. For opset 12 and below the input is first flattened to 2-D, so the reduced extent is the product of the trailing dimensions and fails the same way (as a prim.Mul) whenever any of them is symbolic, for example Hardmax(axis=1) on an [N, 3, W] input.

This keeps the one_hot lowering whenever the extent is static, so the IR for existing models is unchanged. For a symbolic extent it compares the keepdims argmax index against an arange over the axis, broadcast along it, and casts the result to the input dtype. argmax returns the first maximum, so ties still resolve to the first index as the ONNX spec requires.

The tests cover opsets 11 and 13 with the reduced axis symbolic, a flattened extent that is a product involving a symbolic dim, a static axis on an otherwise symbolic input, and inputs with ties, all compared against onnxruntime. Six of the seven fail without this change; the remaining one checks the unchanged static-extent path.

Hardmax is lowered to argmax followed by one_hot, but one_hot takes its
depth as a static integer attribute. When the reduced axis has a symbolic
extent the importer passed a tir.Var (or, for opset <= 12, the symbolic
product of the flattened dims) and failed with a TypeError.

Keep the one_hot lowering for static extents, and for symbolic extents
compare the keepdims argmax against an arange over the axis broadcast
along it. argmax returns the first maximum, so ties still resolve to the
first index as ONNX requires.

This branch has not been deployed

No deployments
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