Skip to content

[Relax][Frontend][ONNX] Lower ThresholdedRelu with where in the input dtype - #20861

Open
dundysm wants to merge 3 commits into
apache:mainfrom
dundysm:fix-onnx-thresholdedrelu
Open

dundysm wants to merge 3 commits into
apache:mainfrom
dundysm:fix-onnx-thresholdedrelu

Conversation

@dundysm

@dundysm dundysm commented Oct 31, 2026

Copy link
Copy Markdown

fixes #20851

the ThresholdedRelu converter computed (x > alpha).astype("float32") * x. that gives nan for nan and -inf inputs (nan * 0 and -inf * 0 are nan) where onnx expects 0, and the float32 mask doesn't match float16 or float64 inputs, so those failed to convert.

this lowers it as where(x > alpha, x, 0) with alpha and zero built in the input dtype, which matches the Where-based function body in the onnx spec.

tests:

  • new test_thresholded_relu_nonfinite_and_dtypes runs [nan, -inf, -2, 0, 0.5, 1, 2, inf] in float32, float16 and float64 and checks the exact output and dtype; it fails before the change and passes after.
  • updated the expected IR in test_thresholded_relu.

note: i couldn't build libtvm from source on this machine, so i ran the changed converter and these tests (plus the other relu tests, 9 passed) against the 0.27.0.post1 wheel's runtime; the converter code is identical there. ruff format and check are clean.

… dtype

Multiplying x by a float32 mask turned nan and -inf into nan instead of 0
and failed for float16/float64 inputs. Select with where(x > alpha, x, 0)
using constants in the input dtype.

Fixes apache#20851

Signed-off-by: Dundy Pasupuleti <dundysm@gmail.com>
@dundysm

dundysm commented Nov 11, 2026

Copy link
Copy Markdown
Author

@mshr-h gentle ping; this lowers onnx ThresholdedRelu with where in the input dtype. a review would be great when you have time.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

An active optional nit recommends reusing run_in_tvm in the ONNX test to avoid duplicating the compilation/VM pipeline and prevent the opset-version warning.

1 open issue
What changed in this PR

Fixes Relax’s ONNX ThresholdedRelu conversion to handle nonfinite values correctly and support float16 and float64 inputs, addressing #20851.

Changes:

  • Replaces mask multiplication with where, using constants in the input dtype.
  • Updates expected IR and adds exact-output and dtype regression checks for float32, float16, and float64.
File Description
tests/​python/​relax/​test_frontend_onnx.py Updates expected IR and covers nonfinite values across three floating-point dtypes.
python/​tvm/​relax/​frontend/​onnx/​onnx_frontend.py Lowers ThresholdedRelu through typed conditional selection.

Review effort: Max

… test

Signed-off-by: Dundy Pasupuleti <dundysm@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the typed where lowering of ThresholdedRelu.

No open issues 🎉

1 resolved since last review

Review effort: Max

@mshr-h

mshr-h commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

@dundysm please check ci errors.

Signed-off-by: Dundy Pasupuleti <dundysm@gmail.com>
@dundysm

dundysm commented Nov 24, 2026

Copy link
Copy Markdown
Author

@mshr-h the macos and windows jobs failed at cmake configure (TVM requires apache-tvm-tirx 0.1.0rc2 or 0.1.0rc3) because the branch predated the tirx bump in #20976; nothing in the onnx change. merged main in 18b1431, so the next run should pick up the new tirx.

@dundysm

dundysm commented Dec 1, 2026

Copy link
Copy Markdown
Author

@guan404ming this is approved and ci is green on the latest head; could you merge it when you get a chance? thanks

@guan404ming

Copy link
Copy Markdown
Member

LGTM, waiting for the ci and I will merge this. Thanks!

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.

[Bug][Relax][ONNX] ThresholdedRelu multiply-mask lowering changes nonfinite values and hardcodes FLOAT

4 participants