Related to: #277
Below is a very simple python script having while_loop.
# Create model
i = tf.compat.v1.constant(0, name="Hole")
c = lambda i: tf.equal(i, 10)
b = lambda i: tf.add(i, 1)
r = tf.while_loop(c, b, [i], name="While")
re_ = tf.identity(r, name="HoleR")
# Save model
sess = tf.compat.v1.Session()
tf.io.write_graph(sess.graph, 'my-model', 'while.pbtxt')
The above code creates a pbtxt file which has three subgraphs: main, cond, body. Using those subgraphs, TFLite executes the while operator as follows (copied from TFLite source code).
// The follow graph illustrates the current implementation.
//
// This Subgraph Cond Subgraph Body Subgraph
// +-----------+ (1) +------------+ (3) +------------+
// | WHILE |-------->| SUBGRAPH |-------->| SUBGRAPH |
// | INPUT | /| INPUT |<----- | INPUT |
// +-----------+ / +------------+ \ +------------+
// / | \ |
// (6) / | (2) (5) \ | (4)
// / v \ v
// +-----------+ / +------------+ +------------+
// | WHILE |<-- | SUBGRAPH | | SUBGRAPH |
// | OUTPUT | | OUTPUT | | OUTPUT |
// +-----------+ +------------+ +------------+
//
// (1) Copy the inputs of WHILE op to the inputs of condition subgraph.
// (2) Invoke condition subgraph.
// Jump to step 5 if result is false.
// (3) Copy the inputs of condition subgraph to the inputs of body subgraph.
// (4) Invoke body subgraph.
// (5) Copy the outputs of body subgraph to the inputs condition subgraph.
// Jump back to step 2!
// (6) Copy the inputs of condition subgraph to the outputs of WHILE op.
Based on the investigations on the TFLite code, I found that the subgraphs have following constraints.
Main subgraph (=this subgraph) should have the same number of inputs and outputs.
Input of cond subgraph should have the same shape and type with the input/output of body subgraph.
Output of cond subgraph is a bool type, and must be either a scalar or 1D with shape [1].
Most helpful comment
Below is a very simple python script having
while_loop.The above code creates a pbtxt file which has three subgraphs: main, cond, body. Using those subgraphs, TFLite executes the while operator as follows (copied from TFLite source code).
Based on the investigations on the TFLite code, I found that the subgraphs have following constraints.
Main subgraph (=this subgraph) should have the same number of inputs and outputs.
Input of cond subgraph should have the same shape and type with the input/output of body subgraph.
Output of cond subgraph is a bool type, and must be either a scalar or 1D with shape [1].