Skip to content

Fix Select op with Mul inf input to nan error #2035

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

Merged
merged 3 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3488,6 +3488,17 @@ def func(x):
return tf.identity(picks, name=_TFOUTPUT)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})

@check_opset_min_version(9, "IsNaN")
def test_where_ismulinf(self):
x_val1 = np.array([np.inf], dtype=np.float32)
x_val2 = np.array([0], dtype=np.float32)
true_result = np.array([np.inf], dtype=np.float32)
def func(x1, x2):
mul = tf.multiply(x1, x2)
picks = tf.where(x1 < mul, true_result, x2)
return tf.identity(picks, name=_TFOUTPUT)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val1, _INPUT1: x_val2})

@check_opset_min_version(9, "Where for strings needs opset 9")
@skip_tfjs("Technically tf where doesn't support strings and tfjs doesn't like it")
def test_where_string(self):
Expand Down Expand Up @@ -5542,7 +5553,7 @@ def func(x):
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val0}, rtol=1e-6, atol=1e-4)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val}, rtol=1e-6, atol=1e-4)

x_val = np.random.random(size=[4, 3]).astype(np.float32) * 2048. - 1024
x_val = np.random.random(size=[4, 3]).astype(np.float32) * 2048. - 1024.
x_val[0, 0] = -1024
x_val[0, 1] = -1023
x_val[0, 2] = 1024
Expand Down Expand Up @@ -5579,7 +5590,7 @@ def func(x):
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val0}, rtol=1e-6, atol=1e-4)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val}, rtol=1e-6, atol=1e-4)

x_val = np.random.random(size=[4, 3]).astype(np.float32) * 2048. - 1024
x_val = np.random.random(size=[4, 3]).astype(np.float32) * 2048. - 1024.
x_val[0, 0] = -1024
x_val[0, 1] = -1023
x_val[0, 2] = 1024
Expand Down
9 changes: 8 additions & 1 deletion tf2onnx/onnx_opset/controlflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,15 @@ def version_9(cls, ctx, node, **kwargs):
if eq_node.input[0] == eq_node.input[1]:
handles_nan = True
for inp in node.inputs[1:]:
if inp.is_const() and np.any(np.isnan(inp.get_tensor_value(as_list=False))):
if handles_nan:
break
if inp.is_const() and (np.any(np.isnan(inp.get_tensor_value(as_list=False))) or \
np.any(np.isinf(inp.get_tensor_value(as_list=False)))):
handles_nan = True
if inp.type == "Mul":
inp0 = inp.inputs[0].is_const() and np.any(np.isinf(inp.inputs[0].get_tensor_value(as_list=False)))
inp1 = inp.inputs[1].is_const() and np.any(np.isinf(inp.inputs[1].get_tensor_value(as_list=False)))
handles_nan = inp0 or inp1

if ctx.get_dtype(node.output[0]) != TensorProto.STRING and not handles_nan:
# Due to bad ORT implementation, Mul/Add ops are faster than Where op
Expand Down