Skip to content

Commit 9495145

Browse files
Fix bug in casting Log inputs
Signed-off-by: Tom Wildenhain <[email protected]>
1 parent c9a0a5a commit 9495145

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

tests/test_backend.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,14 @@ def func(x):
903903
return tf.identity(x_, name=_TFOUTPUT)
904904
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})
905905

906+
@check_onnxruntime_incompatibility("Log")
907+
def test_log_double(self):
908+
x_val = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float64).reshape((2, 2))
909+
def func(x):
910+
x_ = tf.math.log(x)
911+
return tf.identity(x_, name=_TFOUTPUT)
912+
self._run_test_case(func, [_OUTPUT], {_INPUT: x_val})
913+
906914
def test_gather(self):
907915
x_val = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float32)
908916
idx = np.array([1, 0, 2], dtype=np.int32)

tf2onnx/onnx_opset/math.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,19 @@ def version_6(cls, ctx, node, **kwargs):
5151
if node.type == "Log":
5252
# ORT doesn't implement Log on doubles
5353
double_to_float = {onnx_pb.TensorProto.DOUBLE: onnx_pb.TensorProto.FLOAT}
54-
node.maybe_cast_input([[onnx_pb.TensorProto.FLOAT]], double_to_float)
54+
dtypes = node.output_dtypes
55+
if node.maybe_cast_input([[onnx_pb.TensorProto.FLOAT]], double_to_float):
56+
cast_back_node = ctx.insert_new_node_on_output(
57+
"Cast", node.output[0], name=utils.make_name(node.name + "_castback"),
58+
to=dtypes[0])
59+
ctx.set_dtype(cast_back_node.output[0], dtypes[0])
60+
ctx.copy_shape(node.name, cast_back_node.output[0])
61+
ctx.copy_dtype(node.input[0], node.output[0])
5562

63+
@classmethod
64+
def version_13(cls, ctx, node, **kwargs):
65+
# Latest ORT does implement Log on doubles
66+
pass
5667

5768
@tf_op(["Acos", "Asin", "Atan", "Cos", "Sin", "Tan"])
5869
class TrigOpSinceOpset7:

0 commit comments

Comments
 (0)