From 32b1267cd6743a40c76ac54ae10a627b6a559c35 Mon Sep 17 00:00:00 2001 From: alter-xp Date: Mon, 2 Nov 2020 11:16:03 +0800 Subject: [PATCH] TF frontend: add softsign op (#6799) --- python/tvm/relay/frontend/tensorflow.py | 11 +++++++++++ tests/python/frontend/tensorflow/test_forward.py | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index 1f5786f911cb7..6a23c8da97397 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -1923,6 +1923,16 @@ def _impl(inputs, attr, params, mod): return _impl +def _softsign(): + # op description: https://www.tensorflow.org/api_docs/python/tf/math/softsign + def _impl(inputs, attr, params, mod): + abs_out = get_relay_op("abs")(inputs[0]) + add_out = abs_out + tvm.relay.const(1, attr["T"].name) + return inputs[0] / add_out + + return _impl + + def _softplus(): # op description: https://www.tensorflow.org/api_docs/python/tf/math/softplus def _impl(inputs, attr, params, mod): @@ -2381,6 +2391,7 @@ def _impl(inputs, attr, params, mod): "Slice": _slice(), "Softmax": _softmax(), "Softplus": _softplus(), + "Softsign": _softsign(), "SpaceToBatchND": _space_to_batch_nd(), "SpaceToDepth": _space_to_depth(), "Split": _split(False), diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index a5c15c751b501..94c2c440e4d1c 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -3520,6 +3520,22 @@ def _test_forward_expm1(shape): _test_forward_expm1([2, 5, 2, 5]) +def test_forward_softsign(): + """test operator softsign """ + + def _test_forward_softsign(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.nn.softsign(in_data, name="softsign") + compare_tf_with_tvm([np_data], ["in_data:0"], "softsign:0") + + _test_forward_softsign([1, 100]) + _test_forward_softsign([1, 10, 10]) + _test_forward_softsign([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)