From 249b67204e5c1eb4805b8b021386aa0364045063 Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Fri, 9 Feb 2018 11:14:03 -0800 Subject: [PATCH] Remove the unneeded "tf" argument for utility functions. PiperOrigin-RevId: 185166507 --- .../contrib/py2tf/converters/side_effect_guards.py | 6 +++--- .../py2tf/converters/side_effect_guards_test.py | 20 ++++++-------------- tensorflow/contrib/py2tf/utils/context_managers.py | 7 ++++--- .../contrib/py2tf/utils/context_managers_test.py | 9 ++++----- tensorflow/contrib/py2tf/utils/misc.py | 8 +++++--- tensorflow/contrib/py2tf/utils/misc_test.py | 14 ++------------ 6 files changed, 24 insertions(+), 40 deletions(-) diff --git a/tensorflow/contrib/py2tf/converters/side_effect_guards.py b/tensorflow/contrib/py2tf/converters/side_effect_guards.py index 5895dc4..30976b3 100644 --- a/tensorflow/contrib/py2tf/converters/side_effect_guards.py +++ b/tensorflow/contrib/py2tf/converters/side_effect_guards.py @@ -160,8 +160,8 @@ class SideEffectGuardTransformer(transformer.Base): [alias_map.get(s, s).ast() for s in guarded_args], None) template = """ - with py2tf_utils.control_dependency_on_returns(tf, call): - aliased_guarded_args = py2tf_utils.alias_tensors(tf, guarded_args) + with py2tf_utils.control_dependency_on_returns(call): + aliased_guarded_args = py2tf_utils.alias_tensors(guarded_args) """ control_deps_guard = templates.replace( template, @@ -172,7 +172,7 @@ class SideEffectGuardTransformer(transformer.Base): alias_map = {} template = """ - with py2tf_utils.control_dependency_on_returns(tf, call): + with py2tf_utils.control_dependency_on_returns(call): pass """ control_deps_guard = templates.replace(template, call=node.value)[-1] diff --git a/tensorflow/contrib/py2tf/converters/side_effect_guards_test.py b/tensorflow/contrib/py2tf/converters/side_effect_guards_test.py index b41c6fa..463db2e 100644 --- a/tensorflow/contrib/py2tf/converters/side_effect_guards_test.py +++ b/tensorflow/contrib/py2tf/converters/side_effect_guards_test.py @@ -23,7 +23,6 @@ from tensorflow.contrib.py2tf.converters import side_effect_guards from tensorflow.python.framework import constant_op from tensorflow.python.framework import errors_impl from tensorflow.python.framework import ops -from tensorflow.python.ops import array_ops from tensorflow.python.ops import control_flow_ops from tensorflow.python.ops import state_ops from tensorflow.python.ops import variables @@ -43,8 +42,7 @@ class SideEffectGuardsTest(converter_test_base.TestCase): node = self.parse_and_analyze(test_fn, {}) node = side_effect_guards.transform(node, self.ctx) - with self.compiled(node, state_ops.assign, ops.control_dependencies, - ops.Tensor) as result: + 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) @@ -64,8 +62,7 @@ class SideEffectGuardsTest(converter_test_base.TestCase): node = self.parse_and_analyze(test_fn, {}) node = side_effect_guards.transform(node, self.ctx) - with self.compiled(node, state_ops.assign, ops.control_dependencies, - ops.Tensor) as result: + 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) @@ -86,8 +83,7 @@ class SideEffectGuardsTest(converter_test_base.TestCase): node = self.parse_and_analyze(test_fn, {}) node = side_effect_guards.transform(node, self.ctx) - with self.compiled(node, array_ops.identity, control_flow_ops.Assert, - ops.control_dependencies, ops.Tensor) as result: + 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 @@ -111,8 +107,7 @@ class SideEffectGuardsTest(converter_test_base.TestCase): node = self.parse_and_analyze(test_fn, {}) node = side_effect_guards.transform(node, self.ctx) - with self.compiled(node, array_ops.identity, state_ops.assign, - ops.control_dependencies, ops.Tensor) as result: + 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) @@ -134,9 +129,7 @@ class SideEffectGuardsTest(converter_test_base.TestCase): node = self.parse_and_analyze(test_fn, {}) node = side_effect_guards.transform(node, self.ctx) - with self.compiled(node, array_ops.identity, state_ops.assign, - ops.control_dependencies, ops.name_scope, - ops.Tensor) as result: + with self.compiled(node, state_ops.assign, ops.name_scope) as result: self.assertEqual(len(node.body[0].body[0].body), 1) with self.test_session() as sess: v = variables.Variable(2) @@ -158,8 +151,7 @@ class SideEffectGuardsTest(converter_test_base.TestCase): node = self.parse_and_analyze(test_fn, {}) node = side_effect_guards.transform(node, self.ctx) - with self.compiled(node, array_ops.identity, state_ops.assign, - ops.control_dependencies, ops.Tensor) as result: + 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) diff --git a/tensorflow/contrib/py2tf/utils/context_managers.py b/tensorflow/contrib/py2tf/utils/context_managers.py index 47d9839..38d9e11 100644 --- a/tensorflow/contrib/py2tf/utils/context_managers.py +++ b/tensorflow/contrib/py2tf/utils/context_managers.py @@ -20,14 +20,15 @@ from __future__ import print_function import contextlib +from tensorflow.python.framework import ops -def control_dependency_on_returns(tf, return_value): + +def control_dependency_on_returns(return_value): """Create a TF control dependency on the return values of a function. If the function had no return value, a no-op context is returned. Args: - tf: The TensorFlow module. return_value: The return value to set as control dependency. Returns: @@ -38,4 +39,4 @@ def control_dependency_on_returns(tf, return_value): # TODO(mdan): Filter to tensor objects. if not isinstance(return_value, (list, tuple)): return_value = (return_value,) - return tf.control_dependencies(return_value) + return ops.control_dependencies(return_value) diff --git a/tensorflow/contrib/py2tf/utils/context_managers_test.py b/tensorflow/contrib/py2tf/utils/context_managers_test.py index c903f08..633ba93 100644 --- a/tensorflow/contrib/py2tf/utils/context_managers_test.py +++ b/tensorflow/contrib/py2tf/utils/context_managers_test.py @@ -20,7 +20,6 @@ from __future__ import print_function from tensorflow.contrib.py2tf.utils import context_managers from tensorflow.python.framework import constant_op -from tensorflow.python.framework import ops from tensorflow.python.platform import test @@ -28,14 +27,14 @@ class ContextManagersTest(test.TestCase): def test_control_dependency_on_returns(self): # Just dry run them. - with context_managers.control_dependency_on_returns(ops, None): + with context_managers.control_dependency_on_returns(None): pass with context_managers.control_dependency_on_returns( - ops, constant_op.constant(1)): + constant_op.constant(1)): pass with context_managers.control_dependency_on_returns( - ops, [constant_op.constant(1), - constant_op.constant(2)]): + [constant_op.constant(1), + constant_op.constant(2)]): pass diff --git a/tensorflow/contrib/py2tf/utils/misc.py b/tensorflow/contrib/py2tf/utils/misc.py index ee5cedd..1b06caf 100644 --- a/tensorflow/contrib/py2tf/utils/misc.py +++ b/tensorflow/contrib/py2tf/utils/misc.py @@ -18,14 +18,16 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function +from tensorflow.python.framework import ops +from tensorflow.python.ops import array_ops -def alias_tensors(tf, *args): + +def alias_tensors(*args): """Wrap any Tensor arguments with an identity op. Any other argument, including Variables, is returned unchanged. Args: - tf: The TensorFlow module. *args: Any arguments. Must contain at least one element. Returns: @@ -36,7 +38,7 @@ def alias_tensors(tf, *args): """ def alias_if_tensor(a): - return tf.identity(a) if isinstance(a, tf.Tensor) else a + return array_ops.identity(a) if isinstance(a, ops.Tensor) else a # TODO(mdan): Recurse into containers? # TODO(mdan): Anything we can do about variables? Fake a scope reuse? diff --git a/tensorflow/contrib/py2tf/utils/misc_test.py b/tensorflow/contrib/py2tf/utils/misc_test.py index 5613cc0..bfcb304 100644 --- a/tensorflow/contrib/py2tf/utils/misc_test.py +++ b/tensorflow/contrib/py2tf/utils/misc_test.py @@ -18,12 +18,8 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -import imp - from tensorflow.contrib.py2tf.utils import misc from tensorflow.python.framework import constant_op -from tensorflow.python.framework import ops -from tensorflow.python.ops import array_ops from tensorflow.python.ops import variables from tensorflow.python.platform import test @@ -32,11 +28,8 @@ class ContextManagersTest(test.TestCase): def test_alias_single_tensor(self): a = constant_op.constant(1) - fake_tf = imp.new_module('fake_tf') - fake_tf.identity = array_ops.identity - fake_tf.Tensor = ops.Tensor - new_a = misc.alias_tensors(fake_tf, a) + new_a = misc.alias_tensors(a) self.assertFalse(new_a is a) with self.test_session() as sess: self.assertEqual(1, sess.run(new_a)) @@ -46,11 +39,8 @@ class ContextManagersTest(test.TestCase): v = variables.Variable(2) s = 'a' l = [1, 2, 3] - fake_tf = imp.new_module('fake_tf') - fake_tf.identity = array_ops.identity - fake_tf.Tensor = ops.Tensor - new_a, new_v, new_s, new_l = misc.alias_tensors(fake_tf, a, v, s, l) + new_a, new_v, new_s, new_l = misc.alias_tensors(a, v, s, l) self.assertFalse(new_a is a) self.assertTrue(new_v is v) -- 2.7.4