From 187e081f47ec51c0b7484a3703d4db8af8a32921 Mon Sep 17 00:00:00 2001 From: zxy844288792 Date: Tue, 23 Feb 2021 01:13:28 +0000 Subject: [PATCH 1/3] add TF2.x raw_ops.all axis range support --- python/tvm/relay/frontend/tensorflow.py | 6 +++ .../frontend/tensorflow/test_forward.py | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index ac52ab768066..12b7dcbd842c 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -1976,6 +1976,12 @@ def _impl(inputs, attr, params, mod): # Symbolic delta delta = inputs[2] + # if all attributes are constant, evalute the range function and return relay.const + if all([isinstance(start, (np.int32, np.int64, int, np.float32, np.float64, float)), + isinstance(limit, (np.int32, np.int64, int, np.float32, np.float64, float)), + isinstance(delta, (np.int32, np.int64, int, np.float32, np.float64, float))]): + return tvm.relay.const(list(range(start, limit, delta))) + dtype = attr["Tidx"].name if "Tidx" in attr else str(start.dtype) if isinstance(start, (np.int32, np.int64, int, np.float32, np.float64, float)): start = _expr.const(start, dtype=dtype) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index ecf6441bc6b9..135c09f583be 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -3948,6 +3948,44 @@ def _test_math_op(op, dtypes=["int32", "float32"]): _test_math_op(tf.math.reduce_euclidean_norm) +####################################################################### +# All, Max, Min +# ------------------------------------------------------------------ + + +def test_forward_raw_reduce(): + def _check_op(tf_op, ishape, axis, keepdims, range_axis=False, dtype="float32"): + tf.reset_default_graph() + if dtype == "bool": + np_data = np.random.choice([True, False], size=ishape) + else: + np_data = np.random.uniform(size=ishape).astype(dtype) + if tf_op == tf.math.reduce_prod: + axis = 1 + np_data = np_data.reshape(1, -1) + with tf.Graph().as_default(): + if range_axis: + axis = tf.range(axis[0], axis[1], axis[2], name="range", dtype='int32') + in_data = tf.placeholder(dtype, name="in_data") + reduce_op = tf_op(input=in_data, axis=axis, keep_dims=keepdims, name="reduce_std") + compare_tf_with_tvm([np_data], ["in_data:0"], reduce_op.name) + + def _test_raw_reduce_op(op, dtypes=["int32", "float32"]): + for dtype in dtypes: + _check_op(op, (3, 10), axis=(-1), keepdims=False, dtype=dtype) + _check_op(op, (8, 16, 32), axis=(-1), keepdims=False, dtype=dtype) + _check_op(op, (1, 8, 8, 3), axis=(2, 3), keepdims=True, dtype=dtype) + _check_op(op, (2, 3, 10, 10), axis=(1, 2), keepdims=True, dtype=dtype) + _check_op(op, (1, 8, 8, 3), axis=(2, 4, 1), keepdims=True, range_axis=True, dtype=dtype) + _check_op(op, (2, 3, 10, 10), axis=(1, 3, 1), keepdims=True, range_axis=True, + dtype=dtype) + + if package_version.parse(tf.VERSION) >= package_version.parse("2.4.1"): + _test_raw_reduce_op(tf.raw_ops.All, dtypes=["bool"]) + _test_raw_reduce_op(tf.raw_ops.Max) + _test_raw_reduce_op(tf.raw_ops.Min) + + ####################################################################### # Relational operators # -------------------- From 8755b8768fe458e79606336882d1174f2ec5c190 Mon Sep 17 00:00:00 2001 From: zxy844288792 Date: Tue, 23 Feb 2021 01:53:42 +0000 Subject: [PATCH 2/3] apply linting --- python/tvm/relay/frontend/tensorflow.py | 8 ++++++-- tests/python/frontend/tensorflow/test_forward.py | 7 ++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index 12b7dcbd842c..0351c5d15f98 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -1977,9 +1977,13 @@ def _impl(inputs, attr, params, mod): delta = inputs[2] # if all attributes are constant, evalute the range function and return relay.const - if all([isinstance(start, (np.int32, np.int64, int, np.float32, np.float64, float)), + if all( + [ + isinstance(start, (np.int32, np.int64, int, np.float32, np.float64, float)), isinstance(limit, (np.int32, np.int64, int, np.float32, np.float64, float)), - isinstance(delta, (np.int32, np.int64, int, np.float32, np.float64, float))]): + isinstance(delta, (np.int32, np.int64, int, np.float32, np.float64, float)), + ] + ): return tvm.relay.const(list(range(start, limit, delta))) dtype = attr["Tidx"].name if "Tidx" in attr else str(start.dtype) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 135c09f583be..d0038caea09f 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -3965,7 +3965,7 @@ def _check_op(tf_op, ishape, axis, keepdims, range_axis=False, dtype="float32"): np_data = np_data.reshape(1, -1) with tf.Graph().as_default(): if range_axis: - axis = tf.range(axis[0], axis[1], axis[2], name="range", dtype='int32') + axis = tf.range(axis[0], axis[1], axis[2], name="range", dtype="int32") in_data = tf.placeholder(dtype, name="in_data") reduce_op = tf_op(input=in_data, axis=axis, keep_dims=keepdims, name="reduce_std") compare_tf_with_tvm([np_data], ["in_data:0"], reduce_op.name) @@ -3977,8 +3977,9 @@ def _test_raw_reduce_op(op, dtypes=["int32", "float32"]): _check_op(op, (1, 8, 8, 3), axis=(2, 3), keepdims=True, dtype=dtype) _check_op(op, (2, 3, 10, 10), axis=(1, 2), keepdims=True, dtype=dtype) _check_op(op, (1, 8, 8, 3), axis=(2, 4, 1), keepdims=True, range_axis=True, dtype=dtype) - _check_op(op, (2, 3, 10, 10), axis=(1, 3, 1), keepdims=True, range_axis=True, - dtype=dtype) + _check_op( + op, (2, 3, 10, 10), axis=(1, 3, 1), keepdims=True, range_axis=True, dtype=dtype + ) if package_version.parse(tf.VERSION) >= package_version.parse("2.4.1"): _test_raw_reduce_op(tf.raw_ops.All, dtypes=["bool"]) From b1aefa1677013e4995b02fea83050cec42d3e886 Mon Sep 17 00:00:00 2001 From: zxy844288792 Date: Tue, 23 Feb 2021 18:35:56 +0000 Subject: [PATCH 3/3] fix range() func input --- python/tvm/relay/frontend/tensorflow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index 0351c5d15f98..3a3c5fcecd42 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -1984,7 +1984,7 @@ def _impl(inputs, attr, params, mod): isinstance(delta, (np.int32, np.int64, int, np.float32, np.float64, float)), ] ): - return tvm.relay.const(list(range(start, limit, delta))) + return tvm.relay.const(list(range(int(start), int(limit), int(delta)))) dtype = attr["Tidx"].name if "Tidx" in attr else str(start.dtype) if isinstance(start, (np.int32, np.int64, int, np.float32, np.float64, float)):