From 312e5c29a415deb97f3095a584dff8901ce27fcd Mon Sep 17 00:00:00 2001 From: "xiaoqiang.dan" Date: Fri, 2 Apr 2021 15:28:01 +0800 Subject: [PATCH 1/9] Avoid making a new node when already has span info --- 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 1946223a50a4..6dd164c6e35e 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -3851,11 +3851,11 @@ def _convert_operator( @staticmethod def _set_span(sym, node_name): span = tvm.relay.Span(tvm.relay.SourceName(node_name), 0, 0, 0, 0) - if isinstance(sym, _expr.Call): + if isinstance(sym, _expr.Call) and sym.span is None: sym = _expr.Call(sym.op, sym.args, sym.attrs, sym.type_args, span) elif isinstance(sym, _expr.TupleWrapper): tuple_value = sym.tuple_value - if isinstance(tuple_value, _expr.Call): + if isinstance(tuple_value, _expr.Call) and tuple_value.span is None: tuple_value = _expr.Call( tuple_value.op, tuple_value.args, tuple_value.attrs, tuple_value.type_args, span ) From 97b4eb673f0043199faa96db4b5b6d86f998c3b3 Mon Sep 17 00:00:00 2001 From: "xiaoqiang.dan" Date: Tue, 6 Apr 2021 17:48:25 +0800 Subject: [PATCH 2/9] add test --- .../tensorflow/test_from_tensorflow_ir.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tests/python/frontend/tensorflow/test_from_tensorflow_ir.py diff --git a/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py b/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py new file mode 100644 index 000000000000..7cc398572f58 --- /dev/null +++ b/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py @@ -0,0 +1,61 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Unit tests for converting TensorFlow debugging ops to Relay.""" +try: + import tensorflow.compat.v1 as tf +except ImportError: + import tensorflow as tf +import numpy as np +import tvm +from tvm import relay +from tvm.relay.frontend.tensorflow import from_tensorflow +from tensorflow.python.ops import nn + +SEMVER = '#[version = "0.0.5"]\n' + +def run_from_tensorflow(graph): + mod, _ = from_tensorflow(graph.as_graph_def(add_shapes=True)) + return mod + +def test_moments(): + g = tf.Graph() + shape = [4, 176, 8, 8] + dtype = "float32" + with g.as_default(): + A = tf.placeholder(shape=shape, dtype=dtype, name="A") + B = tf.placeholder(shape=shape, dtype=dtype, name="B") + mean, variance = tf.nn.moments(A, [1], keep_dims=True) + normalised_input = (A - mean) / tf.sqrt(variance + 0.0005) + + mod = run_from_tensorflow(g) + program = """ + def @main(%A: Tensor[(4, 176, 8, 8), float32]) { + %527 = mean(%A, axis=[1], keepdims=True) /* moments/mean */; + %528 = subtract(%A, %527) /* sub */; + %529 = subtract(%A, %527); + %530 = multiply(%529, %529) /* moments/SquaredDifference */; + %531 = mean(%530, axis=[1], keepdims=True) /* moments/variance */; + %532 = add(%531, 0.0005f) /* add */; + %533 = sqrt(%532) /* Sqrt */; + divide(%528, %533) /* truediv */ + } + """ + mod_golden = tvm.parser.parse(SEMVER + program) + tvm.ir.assert_structural_equal(mod["main"].body, mod_golden["main"].body, map_free_vars=True) + +if __name__ == "__main__": + test_moments() From 60562a8eafbac2a2e0ba8deab46e28dff30ba601 Mon Sep 17 00:00:00 2001 From: "xiaoqiang.dan" Date: Tue, 6 Apr 2021 17:50:05 +0800 Subject: [PATCH 3/9] add test --- tests/python/frontend/tensorflow/test_from_tensorflow_ir.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py b/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py index 7cc398572f58..b1ae37ab7c9e 100644 --- a/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py +++ b/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py @@ -14,7 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -"""Unit tests for converting TensorFlow debugging ops to Relay.""" +"""Unit tests for converting TensorFlow graph to Relay ir.""" try: import tensorflow.compat.v1 as tf except ImportError: From 3b1cd8af0b8d8906fc3226e09c277be3b6d0dabf Mon Sep 17 00:00:00 2001 From: "xiaoqiang.dan" Date: Tue, 6 Apr 2021 17:58:05 +0800 Subject: [PATCH 4/9] add test --- tests/python/frontend/tensorflow/test_from_tensorflow_ir.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py b/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py index b1ae37ab7c9e..f08626b13991 100644 --- a/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py +++ b/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py @@ -14,7 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -"""Unit tests for converting TensorFlow graph to Relay ir.""" +"""Unit tests for converting TensorFlow graph to Relay graph ir.""" try: import tensorflow.compat.v1 as tf except ImportError: From 51f59e7a1a6d5515b24ec6d51c4063dec156bc57 Mon Sep 17 00:00:00 2001 From: "xiaoqiang.dan" Date: Tue, 6 Apr 2021 21:44:04 +0800 Subject: [PATCH 5/9] fix --- .../frontend/tensorflow/test_from_tensorflow_ir.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py b/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py index f08626b13991..ed1d0725f3d7 100644 --- a/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py +++ b/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py @@ -27,20 +27,22 @@ SEMVER = '#[version = "0.0.5"]\n' + def run_from_tensorflow(graph): mod, _ = from_tensorflow(graph.as_graph_def(add_shapes=True)) return mod + def test_moments(): g = tf.Graph() shape = [4, 176, 8, 8] dtype = "float32" with g.as_default(): - A = tf.placeholder(shape=shape, dtype=dtype, name="A") - B = tf.placeholder(shape=shape, dtype=dtype, name="B") - mean, variance = tf.nn.moments(A, [1], keep_dims=True) - normalised_input = (A - mean) / tf.sqrt(variance + 0.0005) - + A = tf.placeholder(shape=shape, dtype=dtype, name="A") + B = tf.placeholder(shape=shape, dtype=dtype, name="B") + mean, variance = tf.nn.moments(A, [1], keep_dims=True) + normalised_input = (A - mean) / tf.sqrt(variance + 0.0005) + mod = run_from_tensorflow(g) program = """ def @main(%A: Tensor[(4, 176, 8, 8), float32]) { @@ -57,5 +59,6 @@ def @main(%A: Tensor[(4, 176, 8, 8), float32]) { mod_golden = tvm.parser.parse(SEMVER + program) tvm.ir.assert_structural_equal(mod["main"].body, mod_golden["main"].body, map_free_vars=True) + if __name__ == "__main__": test_moments() From 0cf35795cd1807f472f6884cf3b7c3350c91025e Mon Sep 17 00:00:00 2001 From: "xiaoqiang.dan" Date: Tue, 6 Apr 2021 21:47:12 +0800 Subject: [PATCH 6/9] fix --- tests/python/frontend/tensorflow/test_from_tensorflow_ir.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py b/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py index ed1d0725f3d7..59665b996fcc 100644 --- a/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py +++ b/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py @@ -42,7 +42,7 @@ def test_moments(): B = tf.placeholder(shape=shape, dtype=dtype, name="B") mean, variance = tf.nn.moments(A, [1], keep_dims=True) normalised_input = (A - mean) / tf.sqrt(variance + 0.0005) - + mod = run_from_tensorflow(g) program = """ def @main(%A: Tensor[(4, 176, 8, 8), float32]) { From 60403481759bcae337b02a3c4819b889e02b3367 Mon Sep 17 00:00:00 2001 From: "xiaoqiang.dan" Date: Thu, 8 Apr 2021 14:06:40 +0800 Subject: [PATCH 7/9] move test to test_forward.py --- .../frontend/tensorflow/test_forward.py | 39 +++++++++++ .../tensorflow/test_from_tensorflow_ir.py | 64 ------------------- 2 files changed, 39 insertions(+), 64 deletions(-) delete mode 100644 tests/python/frontend/tensorflow/test_from_tensorflow_ir.py diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 26e9476d15c7..644de0dc0943 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -5451,5 +5451,44 @@ def test_forward_unique_with_counts(): _test_unique_with_counts(20, dtype, is_dyn) +####################################################################### +# check graph ir for nn.moments +# ------------ + +SEMVER = '#[version = "0.0.5"]\n' + + +def run_from_tensorflow(graph): + mod, _ = from_tensorflow(graph.as_graph_def(add_shapes=True)) + return mod + + +def test_moments(): + g = tf.Graph() + shape = [4, 176, 8, 8] + dtype = "float32" + with g.as_default(): + A = tf.placeholder(shape=shape, dtype=dtype, name="A") + B = tf.placeholder(shape=shape, dtype=dtype, name="B") + mean, variance = tf.nn.moments(A, [1], keep_dims=True) + normalised_input = (A - mean) / tf.sqrt(variance + 0.0005) + + mod = run_from_tensorflow(g) + program = """ + def @main(%A: Tensor[(4, 176, 8, 8), float32]) { + %527 = mean(%A, axis=[1], keepdims=True) /* moments/mean */; + %528 = subtract(%A, %527) /* sub */; + %529 = subtract(%A, %527); + %530 = multiply(%529, %529) /* moments/SquaredDifference */; + %531 = mean(%530, axis=[1], keepdims=True) /* moments/variance */; + %532 = add(%531, 0.0005f) /* add */; + %533 = sqrt(%532) /* Sqrt */; + divide(%528, %533) /* truediv */ + } + """ + mod_golden = tvm.parser.parse(SEMVER + program) + tvm.ir.assert_structural_equal(mod["main"].body, mod_golden["main"].body, map_free_vars=True) + + if __name__ == "__main__": pytest.main([__file__]) diff --git a/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py b/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py deleted file mode 100644 index 59665b996fcc..000000000000 --- a/tests/python/frontend/tensorflow/test_from_tensorflow_ir.py +++ /dev/null @@ -1,64 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -"""Unit tests for converting TensorFlow graph to Relay graph ir.""" -try: - import tensorflow.compat.v1 as tf -except ImportError: - import tensorflow as tf -import numpy as np -import tvm -from tvm import relay -from tvm.relay.frontend.tensorflow import from_tensorflow -from tensorflow.python.ops import nn - -SEMVER = '#[version = "0.0.5"]\n' - - -def run_from_tensorflow(graph): - mod, _ = from_tensorflow(graph.as_graph_def(add_shapes=True)) - return mod - - -def test_moments(): - g = tf.Graph() - shape = [4, 176, 8, 8] - dtype = "float32" - with g.as_default(): - A = tf.placeholder(shape=shape, dtype=dtype, name="A") - B = tf.placeholder(shape=shape, dtype=dtype, name="B") - mean, variance = tf.nn.moments(A, [1], keep_dims=True) - normalised_input = (A - mean) / tf.sqrt(variance + 0.0005) - - mod = run_from_tensorflow(g) - program = """ - def @main(%A: Tensor[(4, 176, 8, 8), float32]) { - %527 = mean(%A, axis=[1], keepdims=True) /* moments/mean */; - %528 = subtract(%A, %527) /* sub */; - %529 = subtract(%A, %527); - %530 = multiply(%529, %529) /* moments/SquaredDifference */; - %531 = mean(%530, axis=[1], keepdims=True) /* moments/variance */; - %532 = add(%531, 0.0005f) /* add */; - %533 = sqrt(%532) /* Sqrt */; - divide(%528, %533) /* truediv */ - } - """ - mod_golden = tvm.parser.parse(SEMVER + program) - tvm.ir.assert_structural_equal(mod["main"].body, mod_golden["main"].body, map_free_vars=True) - - -if __name__ == "__main__": - test_moments() From 3ed760ce9b2746ac17bb343254aea39e39d03b4b Mon Sep 17 00:00:00 2001 From: "xiaoqiang.dan" Date: Thu, 8 Apr 2021 14:54:07 +0800 Subject: [PATCH 8/9] fix --- tests/python/frontend/tensorflow/test_forward.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 644de0dc0943..e2e8d493858d 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -5455,13 +5455,6 @@ def test_forward_unique_with_counts(): # check graph ir for nn.moments # ------------ -SEMVER = '#[version = "0.0.5"]\n' - - -def run_from_tensorflow(graph): - mod, _ = from_tensorflow(graph.as_graph_def(add_shapes=True)) - return mod - def test_moments(): g = tf.Graph() @@ -5473,7 +5466,7 @@ def test_moments(): mean, variance = tf.nn.moments(A, [1], keep_dims=True) normalised_input = (A - mean) / tf.sqrt(variance + 0.0005) - mod = run_from_tensorflow(g) + mod, _ = from_tensorflow(g.as_graph_def(add_shapes=True)) program = """ def @main(%A: Tensor[(4, 176, 8, 8), float32]) { %527 = mean(%A, axis=[1], keepdims=True) /* moments/mean */; @@ -5486,7 +5479,7 @@ def @main(%A: Tensor[(4, 176, 8, 8), float32]) { divide(%528, %533) /* truediv */ } """ - mod_golden = tvm.parser.parse(SEMVER + program) + mod_golden = tvm.parser.parse('#[version = "0.0.5"]\n' + program) tvm.ir.assert_structural_equal(mod["main"].body, mod_golden["main"].body, map_free_vars=True) From 693a51ee214f53c9c025cab1b7a722f5658e5c00 Mon Sep 17 00:00:00 2001 From: "xiaoqiang.dan" Date: Thu, 8 Apr 2021 19:12:22 +0800 Subject: [PATCH 9/9] fix --- tests/python/frontend/tensorflow/test_forward.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index e2e8d493858d..8446ef3d590b 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -48,6 +48,7 @@ from tvm import relay import tvm.relay.testing.tf as tf_testing from tvm.runtime.vm import VirtualMachine +from tvm.relay.frontend.tensorflow import from_tensorflow from packaging import version as package_version import tvm.testing