-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Relax][ONNX] Prevent Div divide-by-zero crashes
#19566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
10741f2
9e9f78c
4d4192f
9a9567c
a226cbe
37f13d8
cd19e36
67a7a77
b9ad74d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -72,6 +72,18 @@ def _relax_dtype_is_floating_point(dtype: str) -> bool: | |||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _const_integer_expr_has_zero(expr: relax.Expr) -> bool | None: | ||||||||||||||
| """Return whether a constant integer expression contains a zero value. | ||||||||||||||
|
|
||||||||||||||
| Returns None when expression is not statically inspectable. | ||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
| if isinstance(expr, relax.Constant): | ||||||||||||||
| return bool(_np.any(expr.data.numpy() == 0)) | ||||||||||||||
|
Comment on lines
+81
to
+82
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The helper function
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| return None | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def get_type(elem_type: str | int) -> str: | ||||||||||||||
| """Converts onnx integer datatype to numpy datatype""" | ||||||||||||||
| # If a string was passed instead of a tensor type, it does not need | ||||||||||||||
|
|
@@ -526,6 +538,21 @@ class Div(BinaryBase): | |||||||||||||
|
|
||||||||||||||
| @classmethod | ||||||||||||||
| def _impl_v7(cls, bb, inputs, attr, params): | ||||||||||||||
| try: | ||||||||||||||
| lhs_code = DataType(inputs[0].struct_info.dtype).type_code | ||||||||||||||
| rhs_code = DataType(inputs[1].struct_info.dtype).type_code | ||||||||||||||
| except (AttributeError, ValueError, TypeError, TVMError): | ||||||||||||||
| return cls.base_impl(bb, inputs, attr, params) | ||||||||||||||
|
|
||||||||||||||
| lhs_is_integer = lhs_code == DataTypeCode.INT or lhs_code == DataTypeCode.UINT | ||||||||||||||
| rhs_is_integer = rhs_code == DataTypeCode.INT or rhs_code == DataTypeCode.UINT | ||||||||||||||
| if not (lhs_is_integer and rhs_is_integer): | ||||||||||||||
| return cls.base_impl(bb, inputs, attr, params) | ||||||||||||||
|
|
||||||||||||||
| rhs_has_zero = _const_integer_expr_has_zero(inputs[1]) | ||||||||||||||
| if rhs_has_zero: | ||||||||||||||
| raise ValueError("ONNX Div with integer inputs encountered divisor value 0.") | ||||||||||||||
|
|
||||||||||||||
| return cls.base_impl(bb, inputs, attr, params) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -591,6 +591,51 @@ def test_binary(op_name: str): | |
| verify_binary_scalar(op_name) | ||
|
|
||
|
|
||
| def test_div_integer_constant_zero_divisor_raises_valueerror(): | ||
| b_init = numpy_helper.from_array(np.array([3, 0, -2, 1], dtype=np.int32), name="b") | ||
| node = helper.make_node("Div", ["a", "b"], ["y"]) | ||
| graph = helper.make_graph( | ||
| [node], | ||
| "div_const_zero", | ||
| [helper.make_tensor_value_info("a", TensorProto.INT32, [4])], | ||
| [helper.make_tensor_value_info("y", TensorProto.INT32, [4])], | ||
| initializer=[b_init], | ||
| ) | ||
| model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 18)]) | ||
| model.ir_version = 9 | ||
|
|
||
| with pytest.raises( | ||
| ValueError, match="ONNX Div with integer inputs encountered divisor value 0" | ||
| ): | ||
| from_onnx(model, opset=18, keep_params_in_input=False) | ||
|
|
||
|
|
||
| def test_div_integer_dynamic_nonzero_matches_ort(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need to add this test? It looks like it is already covered by test_binary. |
||
| node = helper.make_node("Div", ["a", "b"], ["y"]) | ||
| graph = helper.make_graph( | ||
| [node], | ||
| "div_dynamic_nonzero", | ||
| [ | ||
| helper.make_tensor_value_info("a", TensorProto.INT32, [4]), | ||
| helper.make_tensor_value_info("b", TensorProto.INT32, [4]), | ||
| ], | ||
| [helper.make_tensor_value_info("y", TensorProto.INT32, [4])], | ||
| ) | ||
| model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 18)]) | ||
| model.ir_version = 9 | ||
|
|
||
| check_correctness( | ||
| model, | ||
| inputs={ | ||
| "a": np.array([42, 99, -50, 7], dtype=np.int32), | ||
| "b": np.array([3, -2, 5, 1], dtype=np.int32), | ||
| }, | ||
| ir_version=9, | ||
| opset=18, | ||
| check_dtypes=True, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("int_mode", [True, False]) | ||
| def test_mod(int_mode: bool): | ||
| if int_mode: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t think we need to create a new three-line function if it is only called once from another function.