You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2021/05/19 00:17:08 UTC

[GitHub] [tvm] rohanmukh opened a new pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

rohanmukh opened a new pull request #8074:
URL: https://github.com/apache/tvm/pull/8074


   Test infrastructure for TF2 models that are frozen inside the script. Both ways of creating models in TF2 are tested -
   
   1. Using `tf.function` calls as available in tests/frontend/tensorflow2/test_functional.py
   1. Using `tf.Sequential` API as available in tests/frontend/tensorflow2/test_sequential.py
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] trevor-m commented on a change in pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

Posted by GitBox <gi...@apache.org>.
trevor-m commented on a change in pull request #8074:
URL: https://github.com/apache/tvm/pull/8074#discussion_r635655013



##########
File path: tests/python/frontend/tensorflow2/test_sequential_models.py
##########
@@ -0,0 +1,118 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test: testing models built with tf.keras.Sequential()"""
+
+import tempfile
+import numpy as np
+import pytest
+import tensorflow as tf
+from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
+
+from common import compare_tf_tvm
+from common import run_tf_code
+
+
+def run_sequential_model(model_fn, input_shape):
+    def get_input(shape):
+        _input = np.random.uniform(0, 1, shape).astype(dtype="float32")
+        return _input
+
+    def save_and_reload(_model):
+        with tempfile.TemporaryDirectory() as model_path:
+            tf.saved_model.save(_model, model_path)
+            loaded = tf.saved_model.load(model_path)
+            func = loaded.signatures["serving_default"]
+            frozen_func = convert_variables_to_constants_v2(func)
+        return frozen_func
+
+    def model_graph(model, input_shape):
+        _input = get_input(input_shape)
+        f = save_and_reload(model(input_shape))
+        _output = run_tf_code(f, _input)
+        gdef = f.graph.as_graph_def(add_shapes=True)
+        return gdef, _input, _output
+
+    compare_tf_tvm(*model_graph(model_fn, input_shape), vm=True, output_sig=None)
+
+
+def dense_model(input_shape, num_units=128):

Review comment:
       Same here, I would put these model functions inside the test functions which they are used.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] zhiics commented on pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

Posted by GitBox <gi...@apache.org>.
zhiics commented on pull request #8074:
URL: https://github.com/apache/tvm/pull/8074#issuecomment-847523963


   Thanks @rohanmukh @yongwww @trevor-m 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] zhiics commented on a change in pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

Posted by GitBox <gi...@apache.org>.
zhiics commented on a change in pull request #8074:
URL: https://github.com/apache/tvm/pull/8074#discussion_r638342154



##########
File path: tests/python/frontend/tensorflow2/common.py
##########
@@ -0,0 +1,106 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test utilities"""
+
+import tvm
+from tvm import relay
+
+from tvm.runtime.vm import VirtualMachine
+import tvm.contrib.graph_executor as runtime
+from tvm.relay.frontend.tensorflow import from_tensorflow
+
+import tvm.testing
+from tvm.relay.testing.tf import vmobj_to_list as vmobj_to_list
+
+import tensorflow as tf
+from tensorflow.python.eager.def_function import Function
+
+
+def run_tf_code(func, input_):
+    print(type(func))

Review comment:
       remove or change to log

##########
File path: python/tvm/relay/testing/tf.py
##########
@@ -73,6 +75,46 @@ def convert_to_list(x):
     return x
 
 
+def vmobj_to_list(o):

Review comment:
       is this copied from somewhere? We can add this to testing to remove the other places




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] rohanmukh commented on a change in pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

Posted by GitBox <gi...@apache.org>.
rohanmukh commented on a change in pull request #8074:
URL: https://github.com/apache/tvm/pull/8074#discussion_r635878452



##########
File path: tests/python/frontend/tensorflow2/common.py
##########
@@ -0,0 +1,120 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test utilities"""
+
+import tvm
+from tvm import relay
+
+from tvm.runtime.vm import VirtualMachine
+import tvm.contrib.graph_executor as runtime
+from tvm.relay.frontend.tensorflow import from_tensorflow
+import tvm.testing
+
+import tensorflow as tf
+from tensorflow.python.eager.def_function import Function
+
+
+def vmobj_to_list(o):

Review comment:
       Thanks @yongwww for the comments. I addressed them in the new commits.
   
   In this commit I do not introduce a new frontend code for TF2 models. That I will address in a new PR that is designed to handle TF2 style control flows and will also inherit some code from TF1 frontend code, mostly the ops. The structure of the PRs should follow the pattern as discussed in this thread, especially in this comment as below:
   
   https://github.com/apache/tvm/issues/4102#issuecomment-784456088
   
   
   
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] rohanmukh commented on a change in pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

Posted by GitBox <gi...@apache.org>.
rohanmukh commented on a change in pull request #8074:
URL: https://github.com/apache/tvm/pull/8074#discussion_r638356575



##########
File path: python/tvm/relay/testing/tf.py
##########
@@ -73,6 +75,46 @@ def convert_to_list(x):
     return x
 
 
+def vmobj_to_list(o):

Review comment:
       This is copied from `relay/frontend/tensorflow.py`
   Do you mean I should remove it from there and import its invocations from here (i.e. `relay.testing.tf` ) in this PR? Or do you mean that should be done in another PR? Similar code exists in other places as well like `test_vm.py`, `test_tensorrt.py` etc. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] rohanmukh commented on a change in pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

Posted by GitBox <gi...@apache.org>.
rohanmukh commented on a change in pull request #8074:
URL: https://github.com/apache/tvm/pull/8074#discussion_r638356575



##########
File path: python/tvm/relay/testing/tf.py
##########
@@ -73,6 +75,46 @@ def convert_to_list(x):
     return x
 
 
+def vmobj_to_list(o):

Review comment:
       This is copied from `tests/python/frontend/tensorflow/test_forward.py`
   Do you mean I should remove it from there and import its invocations from here (i.e. `relay.testing.tf` ) in this PR? Or do you mean that should be done in another PR? Similar code exists in other places as well like `test_vm.py`, `test_tensorrt.py` etc. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] trevor-m commented on a change in pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

Posted by GitBox <gi...@apache.org>.
trevor-m commented on a change in pull request #8074:
URL: https://github.com/apache/tvm/pull/8074#discussion_r635654606



##########
File path: tests/python/frontend/tensorflow2/test_functional_models.py
##########
@@ -0,0 +1,368 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test: tests basic examples"""
+
+import tempfile
+import tensorflow as tf
+import numpy as np
+import pytest
+from common import compare_tf_tvm
+from common import run_tf_code
+
+
+class AddOne(tf.Module):

Review comment:
       For all the tests in this file, I think it would be best to encapsulate the tf module class(es) inside the test function. For example:
   
   ```
   def test_expand_dims():
       class ExpandDims(tf.Module):
           def get_input(self):
               return np.ones((1, 30), dtype=np.float32)
   
           @tf.function(input_signature=[tf.TensorSpec(shape=(1, 30), dtype=tf.float32)])
           def func(self, x):
               return tf.expand_dims(x, axis=2)
   
       run_all(ExpandDims)
   ```

##########
File path: tests/python/frontend/tensorflow2/common.py
##########
@@ -0,0 +1,120 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test utilities"""
+
+import tvm
+from tvm import relay
+
+from tvm.runtime.vm import VirtualMachine
+import tvm.contrib.graph_executor as runtime
+from tvm.relay.frontend.tensorflow import from_tensorflow
+import tvm.testing
+
+import tensorflow as tf
+from tensorflow.python.eager.def_function import Function
+
+
+def vmobj_to_list(o):
+    if isinstance(o, tvm.nd.NDArray):
+        out = o.asnumpy().tolist()
+    elif isinstance(o, tvm.runtime.container.ADT):
+        result = []
+        for f in o:
+            result.append(vmobj_to_list(f))
+        out = result
+    else:
+        raise RuntimeError("Unknown object type: %s" % type(o))
+    return out
+
+
+def run_tf_code(func, input_):
+    if type(func) is Function:
+        out = func(input_)
+        if isinstance(out, list):
+            a = [x.numpy() for x in out]
+        else:
+            a = out.numpy()
+    else:
+        a = func(tf.constant(input_))
+        if type(a) is dict:
+            a = [x.numpy() for x in a.values()]
+            if len(a) == 1:
+                a = a[0]
+        elif type(a) is list:
+            a = [x.numpy() for x in a]
+            if len(a) == 1:
+                a = a[0]
+        else:
+            a = a.numpy()
+    return a
+
+
+def compile_graph_runtime(

Review comment:
       Graph Runtime is now Graph Executor, may want to rename these functions

##########
File path: tests/python/frontend/tensorflow2/test_sequential_models.py
##########
@@ -0,0 +1,118 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test: testing models built with tf.keras.Sequential()"""
+
+import tempfile
+import numpy as np
+import pytest
+import tensorflow as tf
+from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
+
+from common import compare_tf_tvm
+from common import run_tf_code
+
+
+def run_sequential_model(model_fn, input_shape):
+    def get_input(shape):
+        _input = np.random.uniform(0, 1, shape).astype(dtype="float32")
+        return _input
+
+    def save_and_reload(_model):
+        with tempfile.TemporaryDirectory() as model_path:
+            tf.saved_model.save(_model, model_path)
+            loaded = tf.saved_model.load(model_path)
+            func = loaded.signatures["serving_default"]
+            frozen_func = convert_variables_to_constants_v2(func)
+        return frozen_func
+
+    def model_graph(model, input_shape):
+        _input = get_input(input_shape)
+        f = save_and_reload(model(input_shape))
+        _output = run_tf_code(f, _input)
+        gdef = f.graph.as_graph_def(add_shapes=True)
+        return gdef, _input, _output
+
+    compare_tf_tvm(*model_graph(model_fn, input_shape), vm=True, output_sig=None)
+
+
+def dense_model(input_shape, num_units=128):

Review comment:
       Same here, I would put these functions inside the test functions which they are used.

##########
File path: tests/python/frontend/tensorflow2/common.py
##########
@@ -0,0 +1,120 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test utilities"""
+
+import tvm
+from tvm import relay
+
+from tvm.runtime.vm import VirtualMachine
+import tvm.contrib.graph_executor as runtime
+from tvm.relay.frontend.tensorflow import from_tensorflow
+import tvm.testing
+
+import tensorflow as tf
+from tensorflow.python.eager.def_function import Function
+
+
+def vmobj_to_list(o):
+    if isinstance(o, tvm.nd.NDArray):
+        out = o.asnumpy().tolist()
+    elif isinstance(o, tvm.runtime.container.ADT):
+        result = []
+        for f in o:
+            result.append(vmobj_to_list(f))
+        out = result
+    else:
+        raise RuntimeError("Unknown object type: %s" % type(o))
+    return out
+
+
+def run_tf_code(func, input_):
+    if type(func) is Function:
+        out = func(input_)
+        if isinstance(out, list):
+            a = [x.numpy() for x in out]
+        else:
+            a = out.numpy()
+    else:
+        a = func(tf.constant(input_))
+        if type(a) is dict:
+            a = [x.numpy() for x in a.values()]
+            if len(a) == 1:
+                a = a[0]
+        elif type(a) is list:
+            a = [x.numpy() for x in a]
+            if len(a) == 1:
+                a = a[0]
+        else:
+            a = a.numpy()
+    return a
+
+
+def compile_graph_runtime(
+    mod, params, target="llvm", target_host="llvm", opt_level=3, output_sig=None
+):
+    with tvm.transform.PassContext(opt_level):
+        lib = relay.build(mod, target=target, target_host=target_host, params=params)
+    return lib
+
+
+def compile_vm(
+    mod, params, target="llvm", target_host="llvm", opt_level=3, disabled_pass=None, output_sig=None
+):
+    with tvm.transform.PassContext(opt_level, disabled_pass=disabled_pass):
+        mod = relay.transform.InferType()(mod)

Review comment:
       I don't think InferType is required here

##########
File path: tests/python/frontend/tensorflow2/common.py
##########
@@ -0,0 +1,120 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test utilities"""
+
+import tvm
+from tvm import relay
+
+from tvm.runtime.vm import VirtualMachine
+import tvm.contrib.graph_executor as runtime
+from tvm.relay.frontend.tensorflow import from_tensorflow
+import tvm.testing
+
+import tensorflow as tf
+from tensorflow.python.eager.def_function import Function
+
+
+def vmobj_to_list(o):
+    if isinstance(o, tvm.nd.NDArray):
+        out = o.asnumpy().tolist()
+    elif isinstance(o, tvm.runtime.container.ADT):
+        result = []
+        for f in o:
+            result.append(vmobj_to_list(f))
+        out = result
+    else:
+        raise RuntimeError("Unknown object type: %s" % type(o))
+    return out
+
+
+def run_tf_code(func, input_):
+    if type(func) is Function:
+        out = func(input_)
+        if isinstance(out, list):
+            a = [x.numpy() for x in out]
+        else:
+            a = out.numpy()
+    else:
+        a = func(tf.constant(input_))
+        if type(a) is dict:
+            a = [x.numpy() for x in a.values()]
+            if len(a) == 1:
+                a = a[0]
+        elif type(a) is list:
+            a = [x.numpy() for x in a]
+            if len(a) == 1:
+                a = a[0]
+        else:
+            a = a.numpy()
+    return a
+
+
+def compile_graph_runtime(
+    mod, params, target="llvm", target_host="llvm", opt_level=3, output_sig=None

Review comment:
       `output_sig` is unused here and in `compile_vm()`, is this intended? What is `output_sig`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] rohanmukh commented on pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

Posted by GitBox <gi...@apache.org>.
rohanmukh commented on pull request #8074:
URL: https://github.com/apache/tvm/pull/8074#issuecomment-843655211


   @yongwww @zhiics @zxy844288792 @wx3000 @srkreddy1238 @dvhg


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] yongwww commented on a change in pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

Posted by GitBox <gi...@apache.org>.
yongwww commented on a change in pull request #8074:
URL: https://github.com/apache/tvm/pull/8074#discussion_r635633295



##########
File path: tests/python/frontend/tensorflow2/common.py
##########
@@ -0,0 +1,120 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test utilities"""
+
+import tvm
+from tvm import relay
+
+from tvm.runtime.vm import VirtualMachine
+import tvm.contrib.graph_executor as runtime
+from tvm.relay.frontend.tensorflow import from_tensorflow
+import tvm.testing
+
+import tensorflow as tf
+from tensorflow.python.eager.def_function import Function
+
+
+def vmobj_to_list(o):

Review comment:
       there is another vmobj_to_list in `tests/python/frontend/tensorflow/test_forward.py`, it would be good to merge this same-name function together, looks they are quite similar.

##########
File path: tests/python/frontend/tensorflow2/common.py
##########
@@ -0,0 +1,120 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test utilities"""
+
+import tvm
+from tvm import relay
+
+from tvm.runtime.vm import VirtualMachine
+import tvm.contrib.graph_executor as runtime
+from tvm.relay.frontend.tensorflow import from_tensorflow
+import tvm.testing
+
+import tensorflow as tf
+from tensorflow.python.eager.def_function import Function
+
+
+def vmobj_to_list(o):
+    if isinstance(o, tvm.nd.NDArray):
+        out = o.asnumpy().tolist()
+    elif isinstance(o, tvm.runtime.container.ADT):
+        result = []
+        for f in o:
+            result.append(vmobj_to_list(f))
+        out = result
+    else:
+        raise RuntimeError("Unknown object type: %s" % type(o))
+    return out
+
+
+def run_tf_code(func, input_):
+    if type(func) is Function:
+        out = func(input_)
+        if isinstance(out, list):
+            a = [x.numpy() for x in out]
+        else:
+            a = out.numpy()
+    else:
+        a = func(tf.constant(input_))
+        if type(a) is dict:
+            a = [x.numpy() for x in a.values()]
+            if len(a) == 1:
+                a = a[0]
+        elif type(a) is list:
+            a = [x.numpy() for x in a]
+            if len(a) == 1:
+                a = a[0]
+        else:
+            a = a.numpy()
+    return a
+
+
+def compile_graph_runtime(
+    mod, params, target="llvm", target_host="llvm", opt_level=3, output_sig=None
+):
+    with tvm.transform.PassContext(opt_level):
+        lib = relay.build(mod, target=target, target_host=target_host, params=params)
+    return lib
+
+
+def compile_vm(
+    mod, params, target="llvm", target_host="llvm", opt_level=3, disabled_pass=None, output_sig=None
+):
+    with tvm.transform.PassContext(opt_level, disabled_pass=disabled_pass):
+        mod = relay.transform.InferType()(mod)
+        vm_exec = relay.vm.compile(mod, target, target_host, params=params)
+    return vm_exec
+
+
+def run_vm(vm_exec, input_, ctx=tvm.cpu(0)):
+    vm = VirtualMachine(vm_exec, ctx)
+    _out = vm.invoke("main", input_)
+    return vmobj_to_list(_out)
+
+
+def run_graph(lib, input_, ctx=tvm.cpu(0)):
+    mod = runtime.GraphModule(lib["default"](ctx))
+    mod.set_input(0, input_)
+    mod.run()
+    _out = mod.get_output(0).asnumpy()
+    return _out
+
+
+def compare_tf_tvm(gdef, input_, output_, vm=True, output_sig=None):

Review comment:
       how about changing `vm` to "runtime_mode" or `runtime`, since we have vm, graphruntime, interpreter

##########
File path: tests/python/frontend/tensorflow2/common.py
##########
@@ -0,0 +1,120 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test utilities"""
+
+import tvm
+from tvm import relay
+
+from tvm.runtime.vm import VirtualMachine
+import tvm.contrib.graph_executor as runtime
+from tvm.relay.frontend.tensorflow import from_tensorflow
+import tvm.testing
+
+import tensorflow as tf
+from tensorflow.python.eager.def_function import Function
+
+
+def vmobj_to_list(o):
+    if isinstance(o, tvm.nd.NDArray):
+        out = o.asnumpy().tolist()
+    elif isinstance(o, tvm.runtime.container.ADT):
+        result = []
+        for f in o:
+            result.append(vmobj_to_list(f))
+        out = result
+    else:
+        raise RuntimeError("Unknown object type: %s" % type(o))
+    return out
+
+
+def run_tf_code(func, input_):
+    if type(func) is Function:
+        out = func(input_)
+        if isinstance(out, list):
+            a = [x.numpy() for x in out]
+        else:
+            a = out.numpy()
+    else:
+        a = func(tf.constant(input_))
+        if type(a) is dict:
+            a = [x.numpy() for x in a.values()]
+            if len(a) == 1:
+                a = a[0]
+        elif type(a) is list:
+            a = [x.numpy() for x in a]
+            if len(a) == 1:
+                a = a[0]
+        else:
+            a = a.numpy()
+    return a
+
+
+def compile_graph_runtime(
+    mod, params, target="llvm", target_host="llvm", opt_level=3, output_sig=None
+):
+    with tvm.transform.PassContext(opt_level):
+        lib = relay.build(mod, target=target, target_host=target_host, params=params)
+    return lib
+
+
+def compile_vm(
+    mod, params, target="llvm", target_host="llvm", opt_level=3, disabled_pass=None, output_sig=None
+):
+    with tvm.transform.PassContext(opt_level, disabled_pass=disabled_pass):
+        mod = relay.transform.InferType()(mod)
+        vm_exec = relay.vm.compile(mod, target, target_host, params=params)
+    return vm_exec
+
+
+def run_vm(vm_exec, input_, ctx=tvm.cpu(0)):
+    vm = VirtualMachine(vm_exec, ctx)
+    _out = vm.invoke("main", input_)
+    return vmobj_to_list(_out)
+
+
+def run_graph(lib, input_, ctx=tvm.cpu(0)):
+    mod = runtime.GraphModule(lib["default"](ctx))
+    mod.set_input(0, input_)
+    mod.run()
+    _out = mod.get_output(0).asnumpy()
+    return _out
+
+
+def compare_tf_tvm(gdef, input_, output_, vm=True, output_sig=None):
+    """compare tf and tvm execution for the same input.
+
+    Parameters
+    ----------
+    func: tf function. can be from saved model or not. different ways to pass input

Review comment:
       don't see func in the arg list of the function `compare_tf_tvm`, please update them to keep docstring and definition identical

##########
File path: tests/python/frontend/tensorflow2/common.py
##########
@@ -0,0 +1,120 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test utilities"""
+
+import tvm
+from tvm import relay
+
+from tvm.runtime.vm import VirtualMachine
+import tvm.contrib.graph_executor as runtime
+from tvm.relay.frontend.tensorflow import from_tensorflow
+import tvm.testing
+
+import tensorflow as tf
+from tensorflow.python.eager.def_function import Function
+
+
+def vmobj_to_list(o):
+    if isinstance(o, tvm.nd.NDArray):
+        out = o.asnumpy().tolist()
+    elif isinstance(o, tvm.runtime.container.ADT):
+        result = []
+        for f in o:
+            result.append(vmobj_to_list(f))
+        out = result
+    else:
+        raise RuntimeError("Unknown object type: %s" % type(o))
+    return out
+
+
+def run_tf_code(func, input_):
+    if type(func) is Function:
+        out = func(input_)
+        if isinstance(out, list):
+            a = [x.numpy() for x in out]
+        else:
+            a = out.numpy()
+    else:
+        a = func(tf.constant(input_))
+        if type(a) is dict:
+            a = [x.numpy() for x in a.values()]
+            if len(a) == 1:
+                a = a[0]
+        elif type(a) is list:
+            a = [x.numpy() for x in a]
+            if len(a) == 1:
+                a = a[0]
+        else:
+            a = a.numpy()
+    return a
+
+
+def compile_graph_runtime(
+    mod, params, target="llvm", target_host="llvm", opt_level=3, output_sig=None
+):
+    with tvm.transform.PassContext(opt_level):
+        lib = relay.build(mod, target=target, target_host=target_host, params=params)
+    return lib
+
+
+def compile_vm(
+    mod, params, target="llvm", target_host="llvm", opt_level=3, disabled_pass=None, output_sig=None
+):
+    with tvm.transform.PassContext(opt_level, disabled_pass=disabled_pass):
+        mod = relay.transform.InferType()(mod)
+        vm_exec = relay.vm.compile(mod, target, target_host, params=params)
+    return vm_exec
+
+
+def run_vm(vm_exec, input_, ctx=tvm.cpu(0)):
+    vm = VirtualMachine(vm_exec, ctx)
+    _out = vm.invoke("main", input_)
+    return vmobj_to_list(_out)
+
+
+def run_graph(lib, input_, ctx=tvm.cpu(0)):

Review comment:
       personally, I like name`run_graph_runtime` more




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] rohanmukh commented on a change in pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

Posted by GitBox <gi...@apache.org>.
rohanmukh commented on a change in pull request #8074:
URL: https://github.com/apache/tvm/pull/8074#discussion_r635696186



##########
File path: tests/python/frontend/tensorflow2/common.py
##########
@@ -0,0 +1,120 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test utilities"""
+
+import tvm
+from tvm import relay
+
+from tvm.runtime.vm import VirtualMachine
+import tvm.contrib.graph_executor as runtime
+from tvm.relay.frontend.tensorflow import from_tensorflow
+import tvm.testing
+
+import tensorflow as tf
+from tensorflow.python.eager.def_function import Function
+
+
+def vmobj_to_list(o):
+    if isinstance(o, tvm.nd.NDArray):
+        out = o.asnumpy().tolist()
+    elif isinstance(o, tvm.runtime.container.ADT):
+        result = []
+        for f in o:
+            result.append(vmobj_to_list(f))
+        out = result
+    else:
+        raise RuntimeError("Unknown object type: %s" % type(o))
+    return out
+
+
+def run_tf_code(func, input_):
+    if type(func) is Function:
+        out = func(input_)
+        if isinstance(out, list):
+            a = [x.numpy() for x in out]
+        else:
+            a = out.numpy()
+    else:
+        a = func(tf.constant(input_))
+        if type(a) is dict:
+            a = [x.numpy() for x in a.values()]
+            if len(a) == 1:
+                a = a[0]
+        elif type(a) is list:
+            a = [x.numpy() for x in a]
+            if len(a) == 1:
+                a = a[0]
+        else:
+            a = a.numpy()
+    return a
+
+
+def compile_graph_runtime(
+    mod, params, target="llvm", target_host="llvm", opt_level=3, output_sig=None

Review comment:
       It is a mistake from refactoring, output_sig are the outputs that should only be passed to frontend parser code, not to compiler




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] zhiics merged pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

Posted by GitBox <gi...@apache.org>.
zhiics merged pull request #8074:
URL: https://github.com/apache/tvm/pull/8074


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] rohanmukh commented on a change in pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

Posted by GitBox <gi...@apache.org>.
rohanmukh commented on a change in pull request #8074:
URL: https://github.com/apache/tvm/pull/8074#discussion_r635875512



##########
File path: tests/python/frontend/tensorflow2/test_sequential_models.py
##########
@@ -0,0 +1,118 @@
+# 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.
+# pylint: disable=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition, broad-except
+# pylint: disable=import-outside-toplevel, redefined-builtin
+"""TF2 to relay converter test: testing models built with tf.keras.Sequential()"""
+
+import tempfile
+import numpy as np
+import pytest
+import tensorflow as tf
+from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
+
+from common import compare_tf_tvm
+from common import run_tf_code
+
+
+def run_sequential_model(model_fn, input_shape):
+    def get_input(shape):
+        _input = np.random.uniform(0, 1, shape).astype(dtype="float32")
+        return _input
+
+    def save_and_reload(_model):
+        with tempfile.TemporaryDirectory() as model_path:
+            tf.saved_model.save(_model, model_path)
+            loaded = tf.saved_model.load(model_path)
+            func = loaded.signatures["serving_default"]
+            frozen_func = convert_variables_to_constants_v2(func)
+        return frozen_func
+
+    def model_graph(model, input_shape):
+        _input = get_input(input_shape)
+        f = save_and_reload(model(input_shape))
+        _output = run_tf_code(f, _input)
+        gdef = f.graph.as_graph_def(add_shapes=True)
+        return gdef, _input, _output
+
+    compare_tf_tvm(*model_graph(model_fn, input_shape), vm=True, output_sig=None)
+
+
+def dense_model(input_shape, num_units=128):

Review comment:
       Thanks @trevor-m  I addressed the comments in the new commits.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] rohanmukh commented on pull request #8074: [Frontend] [Tensorflow2] Added test infrastructure for TF2 frozen models

Posted by GitBox <gi...@apache.org>.
rohanmukh commented on pull request #8074:
URL: https://github.com/apache/tvm/pull/8074#issuecomment-844369105


   @trevor-m 
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org