From 85ba88c973f3dc108d543cb4b590b9f290bdd310 Mon Sep 17 00:00:00 2001 From: Pasquale Minervini Date: Fri, 19 Apr 2019 15:50:39 +0100 Subject: [PATCH 1/7] fix issue #27985 --- tensorflow/python/framework/ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow/python/framework/ops.py b/tensorflow/python/framework/ops.py index 7fb03155e09442..346c68f66c05b8 100644 --- a/tensorflow/python/framework/ops.py +++ b/tensorflow/python/framework/ops.py @@ -880,7 +880,7 @@ def _copy(self, ctx=None, device_name=None): self_device = self.device def grad_fun(dresult): - return [dresult._copy(device_name=self_device)] + return [dresult] if isinstance(dresult, IndexedSlices) else [dresult._copy(device_name=self_device)] tape.record_operation("_copy", [new_tensor], [self], grad_fun) return new_tensor From 124f1593587d683f9acd6b9d64195d86671cebda Mon Sep 17 00:00:00 2001 From: Pasquale Minervini Date: Fri, 19 Apr 2019 16:02:00 +0100 Subject: [PATCH 2/7] this is probably nicer --- tensorflow/python/framework/ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow/python/framework/ops.py b/tensorflow/python/framework/ops.py index 346c68f66c05b8..a14588f1914e7f 100644 --- a/tensorflow/python/framework/ops.py +++ b/tensorflow/python/framework/ops.py @@ -880,7 +880,7 @@ def _copy(self, ctx=None, device_name=None): self_device = self.device def grad_fun(dresult): - return [dresult] if isinstance(dresult, IndexedSlices) else [dresult._copy(device_name=self_device)] + return [dresult._copy(device_name=self_device)] if hasattr(dresult, '_copy') else [dresult] tape.record_operation("_copy", [new_tensor], [self], grad_fun) return new_tensor From 04f4037adf6c6af592372f1a9755af6a9872fe13 Mon Sep 17 00:00:00 2001 From: Pasquale Minervini Date: Fri, 19 Apr 2019 20:43:08 +0100 Subject: [PATCH 3/7] unit test for fix to issue #27985 --- tensorflow/python/framework/ops_test.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tensorflow/python/framework/ops_test.py b/tensorflow/python/framework/ops_test.py index d80b1080c01a31..871a40b2a03177 100644 --- a/tensorflow/python/framework/ops_test.py +++ b/tensorflow/python/framework/ops_test.py @@ -156,6 +156,19 @@ def testToTensor(self): tensor = ops.convert_to_tensor(x, name="tensor") self.assertAllEqual(self.evaluate(tensor), [[2, 3], [0, 0], [5, 7]]) + @test_util.run_in_graph_and_eager_modes + def testEagerCopy(self): + shape = (5, 5) + initializer = tf.random_uniform_initializer(-1.0, 1.0) + var = tfe.Variable(initializer(shape), name='tensor') + with tf.GradientTape() as tape: + a = tf.gather(tf.gather(var, [0, 1]), [0, 1]) + b = tf.gather(tf.gather(var, [2, 3]), [0, 1]) + r = tf.einsum('ij,ij->i', a, b) + g = tape.gradient(r, [var])[0] + values = g.values if isinstance(g, tf.IndexedSlices) else g + self.assertAllEqual(values.get_shape(), (4, 5)) + @test_util.run_deprecated_v1 def testNegation(self): with self.cached_session(): From 3612c5b827df7a46467eea1ed8b7d56d84d519cc Mon Sep 17 00:00:00 2001 From: Pasquale Minervini Date: Fri, 19 Apr 2019 23:27:52 +0100 Subject: [PATCH 4/7] with context.eager_mode() in new unit test --- tensorflow/python/framework/ops_test.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tensorflow/python/framework/ops_test.py b/tensorflow/python/framework/ops_test.py index 871a40b2a03177..8a26b72e103903 100644 --- a/tensorflow/python/framework/ops_test.py +++ b/tensorflow/python/framework/ops_test.py @@ -156,18 +156,18 @@ def testToTensor(self): tensor = ops.convert_to_tensor(x, name="tensor") self.assertAllEqual(self.evaluate(tensor), [[2, 3], [0, 0], [5, 7]]) - @test_util.run_in_graph_and_eager_modes def testEagerCopy(self): - shape = (5, 5) - initializer = tf.random_uniform_initializer(-1.0, 1.0) - var = tfe.Variable(initializer(shape), name='tensor') - with tf.GradientTape() as tape: + with context.eager_mode(): + shape = (5, 5) + initializer = tf.random_uniform_initializer(-1.0, 1.0) + var = tfe.Variable(initializer(shape), name='tensor') + with tf.GradientTape() as tape: a = tf.gather(tf.gather(var, [0, 1]), [0, 1]) b = tf.gather(tf.gather(var, [2, 3]), [0, 1]) r = tf.einsum('ij,ij->i', a, b) - g = tape.gradient(r, [var])[0] - values = g.values if isinstance(g, tf.IndexedSlices) else g - self.assertAllEqual(values.get_shape(), (4, 5)) + g = tape.gradient(r, [var])[0] + values = g.values if isinstance(g, tf.IndexedSlices) else g + self.assertAllEqual(values.get_shape(), [4, 5]) @test_util.run_deprecated_v1 def testNegation(self): From 87f3a1ffdfeb93f0f4b87b42822a11043801ea42 Mon Sep 17 00:00:00 2001 From: Pasquale Minervini Date: Sat, 20 Apr 2019 00:16:03 +0100 Subject: [PATCH 5/7] making the unit test comply with tensorflow's other tests --- tensorflow/python/framework/ops_test.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tensorflow/python/framework/ops_test.py b/tensorflow/python/framework/ops_test.py index 8a26b72e103903..bf534545e5f9a8 100644 --- a/tensorflow/python/framework/ops_test.py +++ b/tensorflow/python/framework/ops_test.py @@ -26,6 +26,7 @@ from tensorflow.core.framework import attr_value_pb2 from tensorflow.core.protobuf import config_pb2 from tensorflow.python.client import session +from tensorflow.python.eager import backprop from tensorflow.python.eager import context from tensorflow.python.eager import function as eager_function from tensorflow.python.framework import common_shapes @@ -47,6 +48,7 @@ from tensorflow.python.ops import math_ops from tensorflow.python.ops import resource_variable_ops from tensorflow.python.ops import resources +from tensorflow.python.ops import special_math_ops from tensorflow.python.ops import variable_scope from tensorflow.python.ops import variables import tensorflow.python.ops.gradients # pylint: disable=unused-import @@ -158,16 +160,14 @@ def testToTensor(self): def testEagerCopy(self): with context.eager_mode(): - shape = (5, 5) - initializer = tf.random_uniform_initializer(-1.0, 1.0) - var = tfe.Variable(initializer(shape), name='tensor') - with tf.GradientTape() as tape: - a = tf.gather(tf.gather(var, [0, 1]), [0, 1]) - b = tf.gather(tf.gather(var, [2, 3]), [0, 1]) - r = tf.einsum('ij,ij->i', a, b) + var = variables.Variable([[0.0], [0.0], [0.0], [0.0], [0.0]], name='tensor') + with backprop.GradientTape() as tape: + a = array_ops.gather(array_ops.gather(var, [0, 1]), [0, 1]) + b = array_ops.gather(array_ops.gather(var, [2, 3]), [0, 1]) + r = special_math_ops.einsum('ij,ij->i', a, b) g = tape.gradient(r, [var])[0] - values = g.values if isinstance(g, tf.IndexedSlices) else g - self.assertAllEqual(values.get_shape(), [4, 5]) + values = g.values if isinstance(g, ops.IndexedSlices) else g + self.assertAllEqual(values.get_shape(), [4, 1]) @test_util.run_deprecated_v1 def testNegation(self): From 416857741a305eac217cfa805b7708cf5ad8e650 Mon Sep 17 00:00:00 2001 From: Pasquale Minervini Date: Mon, 22 Apr 2019 17:17:56 +0100 Subject: [PATCH 6/7] lint, @test_util.run_gpu_only decorator --- tensorflow/python/framework/ops.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tensorflow/python/framework/ops.py b/tensorflow/python/framework/ops.py index a14588f1914e7f..b7a3f8644ba16a 100644 --- a/tensorflow/python/framework/ops.py +++ b/tensorflow/python/framework/ops.py @@ -880,7 +880,8 @@ def _copy(self, ctx=None, device_name=None): self_device = self.device def grad_fun(dresult): - return [dresult._copy(device_name=self_device)] if hasattr(dresult, '_copy') else [dresult] + return [dresult._copy(device_name=self_device) + if hasattr(dresult, '_copy') else dresult] tape.record_operation("_copy", [new_tensor], [self], grad_fun) return new_tensor From 8459d85cba0e85de68821a6932a15cec4d038cf1 Mon Sep 17 00:00:00 2001 From: Pasquale Minervini Date: Mon, 22 Apr 2019 18:08:00 +0100 Subject: [PATCH 7/7] making the linter happy --- tensorflow/python/framework/ops_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tensorflow/python/framework/ops_test.py b/tensorflow/python/framework/ops_test.py index bf534545e5f9a8..9f72ea522fa44e 100644 --- a/tensorflow/python/framework/ops_test.py +++ b/tensorflow/python/framework/ops_test.py @@ -158,9 +158,10 @@ def testToTensor(self): tensor = ops.convert_to_tensor(x, name="tensor") self.assertAllEqual(self.evaluate(tensor), [[2, 3], [0, 0], [5, 7]]) + @test_util.run_gpu_only def testEagerCopy(self): with context.eager_mode(): - var = variables.Variable([[0.0], [0.0], [0.0], [0.0], [0.0]], name='tensor') + var = variables.Variable([[0.0], [0.0], [0.0], [0.0]], name='tensor') with backprop.GradientTape() as tape: a = array_ops.gather(array_ops.gather(var, [0, 1]), [0, 1]) b = array_ops.gather(array_ops.gather(var, [2, 3]), [0, 1])