Skip to content

[Fix][Relax]: ONNX Clip NaN bounds and preserve input NaN (ORT parity)#19535

Merged
tlopex merged 4 commits into
apache:mainfrom
ConvolutedDog:fix-19533
May 12, 2026
Merged

[Fix][Relax]: ONNX Clip NaN bounds and preserve input NaN (ORT parity)#19535
tlopex merged 4 commits into
apache:mainfrom
ConvolutedDog:fix-19533

Conversation

@ConvolutedDog

Copy link
Copy Markdown
Contributor

This PR fixes #19533:

  • Sanitize floating tensor min/max: replace NaN with +inf/-inf before topi max/min so bounds match ONNX "unbounded" semantics where NaN bounds default to no constraint.
  • After clamping, preserve NaNs from the input tensor on floating dtypes.
  • Extend check_correctness with equal_nan for float outputs containing NaN.
  • Add parametrized Clip opset-13 tests for NaN min/max tensor bounds.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request improves the ONNX Clip operator implementation in Relax to correctly handle NaN values in both the input tensor and the clip bounds (min/max). It introduces a helper function to identify floating-point types and a sanitization method for NaN bounds. Feedback includes correcting the casing of DataTypeCode constants to avoid runtime errors, refactoring duplicated comparison logic in tests, removing redundant test parameters, and expanding test coverage to explicitly verify that input NaNs are preserved.

Comment thread python/tvm/relax/frontend/onnx/onnx_frontend.py
Comment thread tests/python/relax/test_frontend_onnx.py Outdated
Comment thread tests/python/relax/test_frontend_onnx.py
Comment thread tests/python/relax/test_frontend_onnx.py Outdated
…k_correctness

- Extend tvm.testing.assert_allclose with equal_nan (forwarded to NumPy) and document parameters.
- Refactor check_correctness _check_output to normalize ndarray once and call a single assert helper.
- Parametrize test_clip_v13 over input vectors (finite vs NaN); adjust Clip v13 min/max fixture where needed.
@ConvolutedDog ConvolutedDog marked this pull request as draft May 12, 2026 02:10
@ConvolutedDog ConvolutedDog marked this pull request as ready for review May 12, 2026 08:35
@tlopex tlopex merged commit b1918c7 into apache:main May 12, 2026
10 checks passed
tlopex added a commit to tlopex/tvm that referenced this pull request Jun 19, 2026
…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.
tqchen pushed a commit that referenced this pull request Jun 20, 2026
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
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 Clip with NaN max bound returns all NaNs

2 participants