def test_multiswap(self): with self.graph.as_default(): a3 = tf.constant(3.0, shape=[2], name="a3") ge.reroute.swap(ge.sgv(a3.op).remap_outputs([0, 0]), ge.sgv(self.a0.op, self.a1.op)) self.assertTrue(ge.matcher("c0").input_ops("a3", "b0")(self.c0.op)) self.assertTrue(ge.matcher("c1").input_ops("a3", "b1")(self.c1.op))
def test_reroute(self): ge.reroute.reroute_a2b_ts([self.a0, self.b0], [self.a1, self.b1]) self.assertTrue(ge.matcher("c0").input_ops("a0", "b0")(self.c0.op)) self.assertTrue(ge.matcher("c1").input_ops("a0", "b0")(self.c1.op)) ge.reroute.reroute_b2a_ts([self.a0, self.b0], [self.a1, self.b1]) self.assertTrue(ge.matcher("c0").input_ops("a1", "b1")(self.c0.op)) self.assertTrue(ge.matcher("c1").input_ops("a1", "b1")(self.c1.op))
def test_multiswap(self): with self.graph.as_default(): a3 = tf.constant(3.0, shape=[2], name="a3") ge.reroute.swap( ge.sgv(a3.op).remap_outputs([0, 0]), ge.sgv(self.a0.op, self.a1.op)) self.assertTrue(ge.matcher("c0").input_ops("a3", "b0")(self.c0.op)) self.assertTrue(ge.matcher("c1").input_ops("a3", "b1")(self.c1.op))
def test_detach(self): """Test for ge.detach.""" sgv = ge.sgv(self.c.op, self.a.op) control_outputs = ge.util.ControlOutputs(self.graph) ge.detach(sgv, control_inputs=control_outputs) # make sure the detached graph is as expected. self.assertTrue(ge.matcher("^foo/c$") .input_ops("geph__a_0", "geph__b_0")(self.c.op))
def test_connect(self): """Test for ge.connect.""" with self.graph.as_default(): x = tf.constant([1., 1.], shape=[2], name="x") y = tf.constant([2., 2.], shape=[2], name="y") z = tf.add(x, y, name="z") sgv = ge.sgv(x.op, y.op, z.op) ge.connect(sgv, ge.sgv(self.e.op).remap_inputs([0])) self.assertTrue(ge.matcher("^foo/bar/e$").input_ops("^z$", "foo/d$") (self.e.op))
def test_reroute_can_modify(self): graph = tf.Graph() # create a special graph where "a" is an ambiguous tensor. That is # it is both an input and an output of the ops in sgv0. with graph.as_default(): a = tf.constant(1.0, shape=[2], name="a") b = tf.constant(2.0, shape=[2], name="b") c = tf.add(a, b, name="c") d = tf.add(a, c, name="d") e = tf.constant(1.0, shape=[2], name="e") f = tf.constant(2.0, shape=[2], name="f") g = tf.add(e, f, name="g") sgv0 = ge.sgv(a.op, b.op, c.op) sgv1 = ge.sgv(e.op, f.op) ge.reroute.swap_outputs(sgv0, sgv1) self.assertTrue(ge.matcher("g").input_ops("a", ge.matcher("c") .input_ops("a", "b"))(g.op)) self.assertTrue(ge.matcher("d").input_ops("e", "f")(d.op))
def test_transform_in_place(self): transformer = ge.Transformer() def my_transform_op_handler_in_place(info, op): add_noise = op.name.startswith("Add") op = ge.transform.transform_op_in_place(info, op, detach_outputs=add_noise) if add_noise: # add some noise to op with info.graph_.as_default(): t = tf.add(tf.constant(1.0, shape=[10], name="Noise"), op.outputs[0], name="AddNoise") # return the "noisy" op return t.op else: return op transformer.transform_op_handler = my_transform_op_handler_in_place transformer(self.graph, self.graph, "", "") matcher0 = ge.matcher("AddNoise").input_ops( "Noise", ge.matcher("Add").input_ops("Const", "Input")) matcher1 = ge.matcher("AddNoise_1").input_ops( "Noise_1", ge.matcher("Add_1").input_ops("Const_1", matcher0)) matcher2 = ge.matcher("AddNoise_2").input_ops( "Noise_2", ge.matcher("Add_2").input_ops("Const_2", matcher1)) top = ge.select_ops("^AddNoise_2$", graph=self.graph)[0] self.assertTrue(matcher2(top))
def test_transform(self): transformer = ge.Transformer() def my_transform_op_handler(info, op): add_noise = op.name.startswith("Add") op_ = ge.transform.copy_op_handler(info, op) if add_noise: # add some noise to op with info.graph_.as_default(): t_ = math_ops.add(constant_op.constant(1.0, shape=[10], name="Noise"), op_.outputs[0], name="AddNoise") # return the "noisy" op return t_.op else: return op_ transformer.transform_op_handler = my_transform_op_handler graph = ops.Graph() transformer(self.graph, graph, "", "") matcher0 = ge.matcher("AddNoise").input_ops( "Noise", ge.matcher("Add").input_ops("Const", "Input")) matcher1 = ge.matcher("AddNoise_1").input_ops( "Noise_1", ge.matcher("Add_1").input_ops("Const_1", matcher0)) matcher2 = ge.matcher("AddNoise_2").input_ops( "Noise_2", ge.matcher("Add_2").input_ops("Const_2", matcher1)) top = ge.select_ops("^AddNoise_2$", graph=graph)[0] self.assertTrue(matcher2(top))
def test_transform(self): transformer = ge.Transformer() def my_transform_op_handler(info, op): add_noise = op.name.startswith("Add") op_ = ge.transform.copy_op_handler(info, op) if add_noise: # add some noise to op with info.graph_.as_default(): t_ = math_ops.add(constant_op.constant( 1.0, shape=[10], name="Noise"), op_.outputs[0], name="AddNoise") # return the "noisy" op return t_.op else: return op_ transformer.transform_op_handler = my_transform_op_handler graph = ops.Graph() transformer(self.graph, graph, "", "") matcher0 = ge.matcher("AddNoise").input_ops( "Noise", ge.matcher("Add").input_ops("Const", "Input")) matcher1 = ge.matcher("AddNoise_1").input_ops( "Noise_1", ge.matcher("Add_1").input_ops("Const_1", matcher0)) matcher2 = ge.matcher("AddNoise_2").input_ops( "Noise_2", ge.matcher("Add_2").input_ops("Const_2", matcher1)) top = ge.select_ops("^AddNoise_2$", graph=graph)[0] self.assertTrue(matcher2(top))
def test_reroute_can_modify(self): graph = tf.Graph() # create a special graph where "a" is an ambiguous tensor. That is # it is both an input and an output of the ops in sgv0. with graph.as_default(): a = tf.constant(1.0, shape=[2], name="a") b = tf.constant(2.0, shape=[2], name="b") c = tf.add(a, b, name="c") d = tf.add(a, c, name="d") e = tf.constant(1.0, shape=[2], name="e") f = tf.constant(2.0, shape=[2], name="f") g = tf.add(e, f, name="g") sgv0 = ge.sgv(a.op, b.op, c.op) sgv1 = ge.sgv(e.op, f.op) ge.reroute.swap_outputs(sgv0, sgv1) self.assertTrue( ge.matcher("g").input_ops("a", ge.matcher("c").input_ops("a", "b"))(g.op)) self.assertTrue(ge.matcher("d").input_ops("e", "f")(d.op))
def test_simple_match(self): self.assertTrue(ge.matcher("^.*/f$")(self.f.op)) self.assertTrue( ge.matcher("^.*/f$").input_ops("^.*/c$", "^.*/d$")(self.f.op)) self.assertTrue(ge.matcher("^.*/f$").input_ops(True, "^.*/d$")(self.f.op)) self.assertTrue( ge.matcher("^.*/f$").input_ops( ge.match.op_type("Add"), ge.match.op_type("Const"))(self.f.op)) self.assertTrue( ge.matcher("^.*/f$").input_ops("^.*/c$", "^.*/d$") .output_ops(ge.matcher("^.*/h$") .control_input_ops("^.*/c$"))(self.f.op)) self.assertTrue( ge.matcher("^.*/f$").input_ops("^.*/c$", "^.*/d$").output_ops( ge.matcher("^.*/h$").control_input_ops("^.*/c$") .output_ops([]))(self.f.op))
def test_simple_match(self): self.assertTrue(ge.matcher("^.*/f$")(self.f.op)) self.assertTrue( ge.matcher("^.*/f$").input_ops("^.*/c$", "^.*/d$")(self.f.op)) self.assertTrue( ge.matcher("^.*/f$").input_ops(True, "^.*/d$")(self.f.op)) self.assertTrue( ge.matcher("^.*/f$").input_ops(ge.match.op_type("Add"), ge.match.op_type("Const"))( self.f.op)) self.assertTrue( ge.matcher("^.*/f$").input_ops("^.*/c$", "^.*/d$").output_ops( ge.matcher("^.*/h$").control_input_ops("^.*/c$"))(self.f.op)) self.assertTrue( ge.matcher("^.*/f$").input_ops("^.*/c$", "^.*/d$").output_ops( ge.matcher("^.*/h$").control_input_ops("^.*/c$").output_ops( []))(self.f.op))
def test_bypass(self): """Test for ge.bypass.""" ge.bypass(ge.sgv(self.f.op).remap_inputs([0])) self.assertTrue(ge.matcher("^foo/bar/h$").input_ops("^foo/c$", "foo/bar/g$") (self.h.op))