def test_side_effect_on_tensor(self): def test_fn(a): tf.Assert(a > 0, ['expected in throw']) return a node, ctx = self.prepare(test_fn, {}) node = side_effect_guards.transform(node, ctx) self.assertEqual(len(node.body), 1) with self.compiled(node, {}, control_flow_ops.Assert) as result: with self.cached_session() as sess: with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, 'expected in throw'): sess.run(result.test_fn(constant_op.constant(-1)))
def test_side_effect_on_used_variable(self): def test_fn(a): tf.assign(a, a + 1) return a + 1 node, ctx = self.prepare(test_fn, {}) node = side_effect_guards.transform(node, ctx) self.assertEqual(len(node.body), 1) with self.compiled(node, {}, state_ops.assign) as result: with self.cached_session() as sess: v = variable_scope.get_variable('test', initializer=2) sess.run(v.initializer) sess.run(result.test_fn(v)) # TODO(mdan): Ensure the result of test_fn(v) is also deterministic. # Right now it's 3 or 4 based on whether the read is synchronized. self.assertEqual(3, sess.run(v))
def test_side_effect_on_return_only_variable(self): def test_fn(a): tf.assign(a, a + 1) return a node, ctx = self.prepare(test_fn, {}) node = side_effect_guards.transform(node, ctx) self.assertEqual(len(node.body), 1) with self.compiled(node, {}, state_ops.assign) as result: with self.cached_session() as sess: v = variable_scope.get_variable('test', initializer=2) sess.run(v.initializer) sess.run(result.test_fn(v)) # TODO(mdan): Add support for this use case. # Right now the variable `a` is not conditioned on the `assign` because # there's no way to add control dependencies to a variable object. self.assertEqual(2, sess.run(v))
def test_side_effect_on_tensor(self): tf = None def test_fn(a): tf.Assert(a > 0, ['expected in throw']) return a node = self.parse_and_analyze(test_fn, {}) node = side_effect_guards.transform(node, self.ctx) with self.compiled(node, control_flow_ops.Assert) as result: self.assertEqual(len(node.body[0].body), 1) with self.test_session() as sess: # NOTE: In this case we can also capture the side effect because the # argument is a tensor ans we can wrap it inside an identity. with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, 'expected in throw'): sess.run(result.test_fn(constant_op.constant(-1)))
def test_side_effect_on_return_only_variable(self): tf = None def test_fn(a): tf.assign(a, a + 1) return a node = self.parse_and_analyze(test_fn, {}) node = side_effect_guards.transform(node, self.ctx) with self.compiled(node, state_ops.assign) as result: self.assertEqual(len(node.body[0].body), 1) with self.test_session() as sess: v = variables.Variable(2) sess.run(v.initializer) # NOTE: We don't expect the assignment to execute in this case, because # variables cannot be reliably guarded. self.assertEqual(2, sess.run(result.test_fn(v)))
def test_multiline_nested_block(self): def test_fn(a): with tf.name_scope('foo'): tf.assign(a, a + 1) b = a + 1 return b node, ctx = self.prepare(test_fn, {}) node = side_effect_guards.transform(node, ctx) self.assertEqual(len(node.body[0].body), 1) with self.compiled(node, {}, state_ops.assign, ops.name_scope) as result: with self.cached_session() as sess: v = variable_scope.get_variable('test', initializer=2) sess.run(v.initializer) sess.run(result.test_fn(v)) # TODO(mdan): Ensure the result of test_fn(v) is also deterministic. self.assertEqual(3, sess.run(v))
def test_side_effect_on_used_variable(self): tf = None def test_fn(a): tf.assign(a, a + 1) return a + 1 node = self.parse_and_analyze(test_fn, {}) node = side_effect_guards.transform(node, self.ctx) with self.compiled(node, state_ops.assign) as result: self.assertEqual(len(node.body[0].body), 1) with self.test_session() as sess: v = variables.Variable(2) sess.run(v.initializer) # NOTE: Unlike test_side_effect_on_return_only_variable, the variable # was used in the local scope and so we could catch the assign's side # effect. self.assertEqual(4, sess.run(result.test_fn(v)))
def test_multiline_block(self): def test_fn(a): tf.assign_add(a, 1) b = a + 1 tf.assign_add(a, 1) b += 1 return b node, ctx = self.prepare(test_fn, {}) node = side_effect_guards.transform(node, ctx) self.assertEqual(len(node.body), 1) with self.compiled(node, {}, state_ops.assign_add) as result: with self.cached_session() as sess: v = variable_scope.get_variable('test', initializer=2) sess.run(v.initializer) sess.run(result.test_fn(v)) # TODO(mdan): Ensure the result of test_fn(v) is also deterministic. self.assertEqual(4, sess.run(v))
def test_multiline_block(self): tf = None def test_fn(a): tf.assign(a, a + 1) b = a + 1 tf.assign(a, b + 1) c = b + 1 d = c + 1 return d node = self.parse_and_analyze(test_fn, {}) node = side_effect_guards.transform(node, self.ctx) with self.compiled(node, state_ops.assign) as result: self.assertEqual(len(node.body[0].body), 1) with self.test_session() as sess: v = variables.Variable(2) sess.run(v.initializer) self.assertEqual(6, sess.run(result.test_fn(v)))
def test_multiline_block_unsafe(self): def test_fn(a): tf.assign(a, a + 1) b = a + 1 tf.assign_add(a, 1) c = b + 1 return c node, ctx = self.prepare(test_fn, {}) node = side_effect_guards.transform(node, ctx) self.assertEqual(len(node.body[0].body), 1) with self.compiled(node, {}, state_ops.assign, state_ops.assign_add) as result: with self.test_session() as sess: v = variable_scope.get_variable('test', initializer=2) sess.run(v.initializer) sess.run(result.test_fn(v)) # TODO(mdan): Ensure the result of test_fn(v) is also deterministic. self.assertEqual(4, sess.run(v))
def test_multiline_block_unsafe(self): tf = None def test_fn(a): tf.assign(a, a + 1) b = a + 1 tf.assign(a, a + 1) c = b + 1 d = c + 1 return d node = self.parse_and_analyze(test_fn, {}) node = side_effect_guards.transform(node, self.ctx) with self.compiled(node, state_ops.assign) as result: self.assertEqual(len(node.body[0].body), 1) with self.test_session() as sess: v = variables.Variable(2) sess.run(v.initializer) # NOTE: This intentionally highlights the flakiness. The test should be # tightened down once that is solved. self.assertTrue(sess.run(result.test_fn(v)) in (6, 7))
def node_to_graph(node, ctx, nocompile_decorators): """Convert Python code to equivalent TF graph mode code. Args: node: A Python AST node representing the code to convert. ctx: An EntityContext object. nocompile_decorators: A tuple containing decorators to be stripped from functions during conversion. Returns: A tuple (node, deps): * node: A Python ast node, representing the converted code. * deps: A set of strings, the fully qualified names of entity dependencies that this node has. """ # TODO(mdan): Verify arguments for correctness. # TODO(mdan): Factor out common elements. # These include: # * code move between blocks # * visiting blocks in transformers # Certain steps, especially canonicalization, insert new symbols into the # tree, which must be accounted. Although less efficient, it is most robust # to re-run the analysis. node = _static_analysis_pass(node, ctx) # TODO(mdan): Clean this up. # Some intermediate analyses are not required, and some comments got orphaned. # Past this point, line numbers are no longer accurate so we ignore the # source. # TODO(mdan): Is it feasible to reconstruct intermediate source code? ctx.source_code = None node = ifexp.transform(node, ctx) node, deps = decorators.transform(node, nocompile_decorators) node = break_statements.transform(node, ctx) node = asserts.transform(node, ctx) # Note: sequencing continue canonicalization before for loop one avoids # dealing with the extra loop increment operation that the for # canonicalization creates. node = continue_statements.transform(node, ctx) ctx.namespace['len'] = len node = _static_analysis_pass(node, ctx) node = single_return.transform(node, ctx) node = _static_analysis_pass(node, ctx) node = lists.transform(node, ctx) node = builtin_functions.transform(node, ctx) node = _static_analysis_pass(node, ctx) node = call_trees.transform(node, ctx, config.DEFAULT_UNCOMPILED_MODULES, nocompile_decorators) node = control_flow.transform(node, ctx) # control_flow may create new symbols and change scopes. node = _static_analysis_pass(node, ctx) node = logical_expressions.transform(node, ctx) node = side_effect_guards.transform(node, ctx) node = name_scopes.transform(node, ctx) return node, deps