From e43d8e51c8db0f34152c63b23b917c220399f0f9 Mon Sep 17 00:00:00 2001 From: xup Date: Fri, 23 Oct 2020 19:55:18 +0800 Subject: [PATCH 1/4] TF frontend: add expm1 op --- python/tvm/relay/frontend/tensorflow.py | 9 +++++++++ tests/python/frontend/tensorflow/test_forward.py | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index 89b36256152e..907980f7ede7 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -788,6 +788,14 @@ def _impl(inputs, attr, params, mod): return _impl +def _expm1(): + def _impl(inputs, attr, params, mod): + lh = get_relay_op("exp")(inputs[0]) + return get_relay_op("subtract")(lh, tvm.relay.const(1, attr["T"].name)) + + return _impl + + def _resize(method): def _impl(inputs, attr, params, mod): if attr["_output_shapes"][0] is not None: @@ -2297,6 +2305,7 @@ def _impl(inputs, attr, params, mod): "EuclideanNorm": _euclidean_norm(), "Exp": AttrCvt("exp"), "ExpandDims": _expand_dims(), + "Expm1": _expm1(), "Fill": _fill(), "Floor": AttrCvt("floor"), "FloorDiv": _floordiv(), diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 0bcac7fb1a21..f9e698c5f028 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -3488,6 +3488,22 @@ def test_forward_atan2(): compare_tf_with_tvm([np_data_1, np_data_2], ["in_data_1:0", "in_data_2:0"], "atan2:0") +def test_forward_expm1(): + """test operator expm1 """ + + def _test_forward_expm1(shape): + tf.disable_eager_execution() + np_data = np.random.uniform(1, 100, size=shape).astype(np.float32) + tf.reset_default_graph() + in_data = tf.placeholder(tf.float32, shape, name="in_data") + tf.expm1(in_data, name="expm1") + compare_tf_with_tvm([np_data], ["in_data:0"], "expm1:0") + + _test_forward_expm1([1, 100]) + _test_forward_expm1([1, 10, 10]) + _test_forward_expm1([2, 5, 2, 5]) + + def test_forward_negative(): """test tf operator Neg """ np_data = np.random.uniform(-100, 255, size=(224, 224, 3)).astype(np.float32) From 14f2b77694a2fdbfc309e30c93c0d62084771a11 Mon Sep 17 00:00:00 2001 From: xp56 Date: Thu, 29 Oct 2020 14:47:43 +0800 Subject: [PATCH 2/4] TF frontend: add description for expm1 --- python/tvm/relay/frontend/tensorflow.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index 907980f7ede7..c26ebf47d71d 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -789,6 +789,7 @@ def _impl(inputs, attr, params, mod): def _expm1(): + # op description: https://www.tensorflow.org/api_docs/python/tf/math/expm1 def _impl(inputs, attr, params, mod): lh = get_relay_op("exp")(inputs[0]) return get_relay_op("subtract")(lh, tvm.relay.const(1, attr["T"].name)) From 001ec7ac8b0ff6e72c63fee5f8c53f97047f05f2 Mon Sep 17 00:00:00 2001 From: xp56 Date: Thu, 29 Oct 2020 14:54:03 +0800 Subject: [PATCH 3/4] TF frontend: use overload operator - instead of subtract --- python/tvm/relay/frontend/tensorflow.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index c26ebf47d71d..32dd90c28bc2 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -791,8 +791,8 @@ def _impl(inputs, attr, params, mod): def _expm1(): # op description: https://www.tensorflow.org/api_docs/python/tf/math/expm1 def _impl(inputs, attr, params, mod): - lh = get_relay_op("exp")(inputs[0]) - return get_relay_op("subtract")(lh, tvm.relay.const(1, attr["T"].name)) + exp_out = get_relay_op("exp")(inputs[0]) + return exp_out - tvm.relay.const(1.0) return _impl From 3231d762a0b7496284bd6d9d132ec06bec52c28a Mon Sep 17 00:00:00 2001 From: xp56 Date: Thu, 29 Oct 2020 19:08:08 +0800 Subject: [PATCH 4/4] TF frontend: Limits the range of input data in the Expm1 test --- tests/python/frontend/tensorflow/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index f9e698c5f028..7130d81488ed 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -3493,7 +3493,7 @@ def test_forward_expm1(): def _test_forward_expm1(shape): tf.disable_eager_execution() - np_data = np.random.uniform(1, 100, size=shape).astype(np.float32) + np_data = np.random.uniform(1, 10, size=shape).astype(np.float32) tf.reset_default_graph() in_data = tf.placeholder(tf.float32, shape, name="in_data") tf.expm1(in_data, name="expm1")