Skip to content

[Relax][ONNX] Preserve bool dtype when folding constant comparisons - #20286

Merged
tlopex merged 1 commit into
apache:mainfrom
StrongbodyStrongmind:fix-20282
Sep 8, 2026
Merged

[Relax][ONNX] Preserve bool dtype when folding constant comparisons#20286
tlopex merged 1 commit into
apache:mainfrom
StrongbodyStrongmind:fix-20282

Conversation

@StrongbodyStrongmind

Copy link
Copy Markdown
Contributor

Fixes #20282.

The ONNX frontend constant-folds binary operators with NumPy. When both
operands have the same dtype, the result was unconditionally cast back to
the operand dtype to avoid NumPy precision widening.

This is correct for arithmetic operations, but incorrect for comparison
operators such as Less, LessOrEqual, Greater, and GreaterOrEqual,
whose result dtype must be bool.

As a result, constant-folded comparisons produced correct 0/1 values but
returned int32 or float32 tensors instead of bool.

This patch preserves NumPy boolean results while keeping the existing
no-widening behavior for non-boolean binary operations.

Tests:

  • Added regression coverage for constant-folded ONNX comparisons with
    int32 and float32 operands.
  • The 8 new cases fail before the fix and pass after it.
  • Related ONNX binary tests: 16 passed.
  • git diff --check upstream/main..HEAD: passed.

@tlopex
tlopex merged commit 2bc2db2 into apache:main Sep 8, 2026
5 checks passed
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] Constant folding a comparison drops its bool dtype: the function returns int32/float32 instead of bool

2 participants