kevinthesun 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_r392375860
##########
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:
Review comment:
Added.
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services