tvm-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-tvm] zhiics commented on a change in pull request #5020: [Frontend][TensorFlow]TensorFlow Parser Control Flow Enhancement
Date Fri, 13 Mar 2020 03:57:38 GMT
zhiics commented on a change in pull request #5020: [Frontend][TensorFlow]TensorFlow Parser
Control Flow Enhancement
URL: https://github.com/apache/incubator-tvm/pull/5020#discussion_r392017919
 
 

 ##########
 File path: python/tvm/relay/frontend/tensorflow.py
 ##########
 @@ -2029,6 +2096,73 @@ def if_node(self):
         return self._if
 
 
+class LoopBound(ExprVisitor):
+    """
+    When a loop body is create, we get a Relay expression backtracing all
+    the way back to input node. This will result in lots of unnecessary
+    expression placed into loop body and compute multiple times. For example,
+    consider the following tensorflow code:
+
+    .. code-block:: python
+
+        i = tf.constant(0)
+        data = tf.compat.v1.placeholder(tf.float32, shape=(1024, 1024))
+        slice = tf.strided_slice(data, 0, 512)
+        def c(i): return tf.less(i, 10)
+        def b(i): return [tf.add(i, 1), tf.add(i, 1) + slice]
+        r = tf.while_loop(c, b, [i])
+
+    If we directly create recursive function, slice will be placed into function body.
+    Instead, we recognize whether slice is inside while_loop block and pass it as an
+    extra loop variable to avoid duplicate computation.
+
+    """
+    def __init__(self, loop_name, hash2tfnode, while_loop_name_set):
+        ExprVisitor.__init__(self)
+        self._loop_name = loop_name
+        self._hash2tfnode = hash2tfnode
+        self._while_loop_name_set = while_loop_name_set
+        self.extra_loop_var_names = set()
+
+    def _find_parent_loop_name(self, node_name):
+        """Find name of direct parent while loop."""
+        ploop_name = ""
+        name_prefix = node_name.rsplit('/', 1)[0]
+        if name_prefix.startswith("^"):
+            name_prefix = name_prefix[1:]
+        for lname in self._while_loop_name_set:
+            # For nested loop, we should pick the inner most one.
+            if name_prefix.startswith(lname) and len(ploop_name) < len(lname):
+                ploop_name = lname
+
+        if len(ploop_name) == 0:
+            ploop_name = name_prefix
+
+        return ploop_name
+
+    def visit(self, expr):
+        """
+        For each expression in the body, look up the corresponding
+        tensorflow node with its structural hash. If the current loop is the
 
 Review comment:
   TensorFlow

----------------------------------------------------------------
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


With regards,
Apache Git Services

Mime
View raw message