Enable constant propagation across Switch(x,x) by rewriting the two outputs as Const...
authorA. Unique TensorFlower <gardener@tensorflow.org>
Fri, 23 Feb 2018 03:47:03 +0000 (19:47 -0800)
committerTensorFlower Gardener <gardener@tensorflow.org>
Fri, 23 Feb 2018 03:51:04 +0000 (19:51 -0800)
commitbefd8234e1c209b26457eb5df37d2952004bdeaf
tree20b822c63007855a34507447780160a46e1143b7
parent1dcd464f249ddcdc9c3864aff0a881c0948d08fb
Enable constant propagation across Switch(x,x) by rewriting the two outputs as Const(false), Const(true) with appropriate control dependencies. This is a fairly common pattern when the graph contains assertions.

By rewriting the graph a bit, we can propagate the constants down the two output branches, and just use control dependencies to trigger the selected one at runtime. For example,
         +------+
     x-->|Switch|-->a
     x-->|      |-->b
         +------+

Is rewritten as

         +------+
     x-->|Switch|-->Identity--^>Const(false)-->a
     x-->|      |-->Identity--^>Const(true)-->b
         +------+

(In practice there may be multiple consumers of each output branch.)

PiperOrigin-RevId: 186714991
tensorflow/core/grappler/optimizers/arithmetic_optimizer.cc
tensorflow/core/grappler/optimizers/constant_folding.cc
tensorflow/core/grappler/optimizers/constant_folding_test.cc
tensorflow/core/grappler/utils.cc
tensorflow/core/grappler/utils.h