Skip to content

Conversation

@cbalint13
Copy link
Contributor

@cbalint13 cbalint13 commented Aug 14, 2025

This PR enforce some of the ONNX constraint compliance for binary operations.


Changes

  • Mitigates the behaviour of numpy precision widening, e.g. np.div(int64,in64)->float64
  • This complies with the onnx.Div (also others) output constraint: C: Result, has same element type as two inputs
  • Adds optional ability to the test checker for dtype similarities, as exact or at least same subtype
  • Use a more appropriate public helper for the numpy type conversion, now also working with onnx 0.20

Failure example

After shape sampling and arithmetic with division in the graph (excerpt from a custom yolov11s model) np.div(5,2) yields result as float64 (due to numpy behaviour) and then it fails to be a parameter of Slice due to int64 constraint.

onnx-div-widening-issue
tvm.error.InternalError: Check failed: (is_base_of_tuple_of_int64(sinfo)) is false: 
Operator Op(relax.strided_slice) requires the end argument to be a tuple of int64 PrimValues.  
However, in expression {...}, the end argument (R.prim_value(T.float64(2.5)),) has struct info
R.Tuple(R.Prim(value=T.float64(2.5)))

@cbalint13 cbalint13 force-pushed the onnx-numpy-widening branch 7 times, most recently from 605c745 to 97a6bb6 Compare August 14, 2025 14:00
@cbalint13 cbalint13 force-pushed the onnx-numpy-widening branch from 97a6bb6 to 268fad6 Compare August 14, 2025 14:19
@cbalint13 cbalint13 marked this pull request as ready for review August 14, 2025 15:39
@cbalint13
Copy link
Contributor Author

Cc @Hzfengsy

Copy link
Member

@tlopex tlopex left a comment

Choose a reason for hiding this comment

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

LGTM!

@tlopex tlopex merged commit fcd266e into apache:main Aug 18, 2025
23 checks passed
@cbalint13 cbalint13 deleted the onnx-numpy-widening branch August 28, 2025 14:55
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.

2 participants