Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3191,7 +3191,9 @@ def func(X, K):
self._run_test_case(func, [_OUTPUT, _OUTPUT1], {_INPUT: x_val, _INPUT1: k_val})

@check_opset_min_version(12)
@test_ms_domain()
def test_inverse(self):
# this depends on onnx Inverse which was removed from opset-12 but does exists in the ms-domain
x_val = np.random.random([5, 5]).astype(np.float32)
def func(x):
return tf.linalg.inv(x, name=_TFOUTPUT)
Expand Down Expand Up @@ -3224,6 +3226,14 @@ def func(x, y):
tf.math.greater_equal(x, y, name=_TFOUTPUT1)
self._run_test_case(func, [_OUTPUT, _OUTPUT1], {_INPUT: x_val, _INPUT1: y_val})

@check_opset_min_version(10)
def test_is_finite(self):
x_val = np.array([5.0, 4.8, 6.8, np.inf, np.nan], dtype=np.float32)
Copy link
Contributor

Choose a reason for hiding this comment

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

Sometimes data has negative-infinity, so might be something to check.

def func(x):
y = tf.math.is_finite(x)
return tf.identity(y, name=_TFOUTPUT)
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})


if __name__ == '__main__':
unittest_main()
25 changes: 19 additions & 6 deletions tf2onnx/onnx_opset/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,23 @@ def version_12(cls, ctx, node, **kwargs):
del node.attr["N"]


@tf_op("MatrixInverse", onnx_op="Inverse")
class Inverse:
@tf_op("IsFinite")
class IsFinite:
@classmethod
def version_12(cls, ctx, node, **kwargs):
utils.make_sure(node.get_attr('adjoint').i == 0, "adjoint must be false")
del node.attr["adjoint"]
node.domain = constants.MICROSOFT_DOMAIN
def version_10(cls, ctx, node, **kwargs):
# map to onnx as:
# not (isinf(x) or isnan(x))

shapes = node.output_shapes
dtypes = [onnx_pb.TensorProto.BOOL] * len(node.output_dtypes)

ctx.remove_node(node.name)

inf_node = ctx.make_node("IsInf", inputs=node.input, name=utils.make_name(node.name),
shapes=shapes, dtypes=dtypes)
nan_node = ctx.make_node("IsNaN", inputs=node.input, name=utils.make_name(node.name),
shapes=shapes, dtypes=dtypes)
or_node = ctx.make_node("Or", inputs=[inf_node.output[0], nan_node.output[0]], name=utils.make_name(node.name),
shapes=shapes, dtypes=dtypes)
_ = ctx.make_node("Not", inputs=or_node.output, name=node.name,
shapes=shapes, dtypes=dtypes)
10 changes: 10 additions & 0 deletions tf2onnx/onnx_opset/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,16 @@ def version_7(cls, ctx, node, **kwargs):
return
raise ValueError("non-const dim is not supported")

@classmethod
def version_11(cls, ctx, node, **kwargs):
dim_node = node.inputs[1]
if dim_node.is_const():
node.type = "Unsqueeze"
dim = dim_node.get_tensor_value()
node.set_attr("axes", [dim])
ctx.remove_input(node, node.input[1])
return
raise ValueError("non-const dim is not supported")

Copy link
Contributor

Choose a reason for hiding this comment

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

Is ver_11 same as ver_7, but without the negative axis check? ... if yes, the should we just call ver_7 (less code)? If no, then ignore this comment :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

opset-11 added support for the negative axes.

@tf_op("StridedSlice")
class StridedSlice:
Expand Down