One: Compiler FE: Enable While Op

Created on 28 Apr 2020  路  1Comment  路  Source: Samsung/ONE

  • [x] tflchef
  • [x] tfldump
  • [x] luci.IR
  • [x] luci.Import
  • [x] luci.Export
  • [x] luci.logex
  • [x] luci.Service
  • [x] res/TensorFlowPythonExamples
  • [x] res/TensorFlowLiteRecipes
  • [x] tflite2circle
  • [x] luci.tests

Related to: #277

Most helpful comment

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

>All comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

periannath picture periannath  路  3Comments

ragmani picture ragmani  路  4Comments

periannath picture periannath  路  3Comments

jinevening picture jinevening  路  3Comments

seanshpark picture seanshpark  路  3Comments