[Relax][ONNX] Preserve NaN in Sign to align with ONNX Runtime#19674
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the ONNX frontend's Sign operator implementation to correctly handle NaN values for floating-point inputs by returning NaN instead of the default sign output. The review feedback suggests simplifying the extraction of the input's data type by directly checking if its structural information is an instance of TensorStructInfo, which avoids overly complex and redundant getattr calls.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
f69ea8a to
753c4c8
Compare
|
Thanks to @tlopex 😄 |
…ndefined) Remove the explicit NaN-preservation guards added in the ONNX frontend for Relu (apache#19750), Sign (apache#19674), Clip's input (apache#19535), and ReduceMax/ReduceMin (the _reduce_min_max_preserve_nan helper, apache#19750). Each paid an extra isnan + where -- the reduce helper a full sum(isnan) pass -- over the data solely to force a NaN input through, a corner case that mostly shows up in fuzzing. NaN handling for these ops is now left unspecified, matching the less-strict but efficient direction: relu/sign reduce to the plain relax op and ReduceMax/ReduceMin to relax.op.max/min. Clip keeps the cheap scalar NaN-bound sanitization: a NaN min/max bound is still treated as unbounded (ORT parity), which is a distinct concern from per-element input-NaN passthrough. Drop the corresponding tests: test_relu_nan_preserve, test_sign_nan_preserve, test_reduce_min_max_nan_preserve, and the NaN-input case of test_clip_v13.
This pr removes the explicit NaN-preservation guards added in the ONNX frontend for Relu (#19750), Sign (#19674), Clip's input (#19535), and ReduceMax/ReduceMin (the _reduce_min_max_preserve_nan helper, #19750). Each paid an extra isnan + where -- the reduce helper a full sum(isnan) pass This pr also drops the corresponding tests: test_relu_nan_preserve, test_sign_nan_preserve, test_reduce_min_max_nan_preserve, and the NaN-input case of test_clip_v13. PRs about NaN-preservation updated in backend will be followed up in the future
Hi Committers,
This PR fixes issues #19543 and #19572. Any suggestions would be appreciated if you are available.
Root cause:
The ONNX frontend
Signconverter directly returnedrelax.op.sign(x). After legalization, this maps totopi.sign, which is implemented via comparisons (x < 0 ? -1 : x > 0 ? 1 : 0). ForNaN, both comparisons are false, so TVM produced 0, while ONNX Runtime preserves NaN. This created a frontend semantic mismatch for imported ONNX models.Solution:
Apply a minimal ONNX-frontend-only fix in
onnx_frontend.py:Signaswhere(isnan(x), x, sign(x)).sign(x)).