From 6188e8e29dfc4fc151c8113218ba321398acaf4b Mon Sep 17 00:00:00 2001 From: Fei Hu Date: Thu, 8 Nov 2018 22:54:05 -0800 Subject: [PATCH] Add the test case for MaxPooling3DGrad in Eager mode --- tensorflow/python/eager/BUILD | 2 ++ tensorflow/python/eager/backprop_test.py | 28 +++++++++++++++++++++++ tensorflow/python/eager/pywrap_tfe_src.cc | 1 + 3 files changed, 31 insertions(+) diff --git a/tensorflow/python/eager/BUILD b/tensorflow/python/eager/BUILD index 9d6c4f73375..c42deb31463 100644 --- a/tensorflow/python/eager/BUILD +++ b/tensorflow/python/eager/BUILD @@ -114,9 +114,11 @@ cuda_py_test( ":backprop", ":context", ":test", + "//third_party/py/numpy", "//tensorflow/python:embedding_ops", "//tensorflow/python:array_ops", "//tensorflow/python:control_flow_ops", + "//tensorflow/python:layers", "//tensorflow/python:math_ops", "//tensorflow/python:nn_ops", "//tensorflow/python:resource_variable_ops", diff --git a/tensorflow/python/eager/backprop_test.py b/tensorflow/python/eager/backprop_test.py index 31ba6ad9b8d..f4ca33ca352 100644 --- a/tensorflow/python/eager/backprop_test.py +++ b/tensorflow/python/eager/backprop_test.py @@ -30,6 +30,7 @@ from tensorflow.python.framework import dtypes from tensorflow.python.framework import ops from tensorflow.python.framework import tensor_shape from tensorflow.python.framework import test_util +from tensorflow.python.layers.pooling import max_pooling3d from tensorflow.python.ops import array_ops from tensorflow.python.ops import control_flow_ops from tensorflow.python.ops import custom_gradient @@ -1140,5 +1141,32 @@ class BackpropTest(test.TestCase): g = f(c) self.assertAllEqual(self.evaluate(t.gradient(g, c)), 4.0) + @test_util.run_in_graph_and_eager_modes + def testMaxPooling3DGradient(self): + def forward(a): + r = max_pooling3d( + a, + pool_size=pool_size, + strides=strides, + padding="SAME") + return r + + input_sizes = [1, 3, 2, 4, 1] + pool_size = (2, 2, 1) + strides = (1, 1, 1) + + total_size = np.prod(input_sizes) + x = np.arange(1, total_size + 1, dtype=np.float32) + aa = constant_op.constant(x, shape=input_sizes, dtype=dtypes.float32) + da = backprop.gradients_function(forward)(aa) + + if not context.executing_eagerly(): + tf_aa = constant_op.constant(x, shape=input_sizes, dtype=dtypes.float32) + tf_max = max_pooling3d( + tf_aa, pool_size=pool_size, strides=strides, padding="SAME") + tf_da = gradients.gradients(tf_max, [tf_aa]) + self.assertAllEqual(da[0], tf_da[0].eval()) + + if __name__ == '__main__': test.main() diff --git a/tensorflow/python/eager/pywrap_tfe_src.cc b/tensorflow/python/eager/pywrap_tfe_src.cc index 705b3428309..6ca8eadbdeb 100644 --- a/tensorflow/python/eager/pywrap_tfe_src.cc +++ b/tensorflow/python/eager/pywrap_tfe_src.cc @@ -2020,6 +2020,7 @@ PyObject* RecordGradient(PyObject* op_name, PyObject* inputs, PyObject* attrs, PyObject* op_outputs; bool op_outputs_tuple_created = false; std::pair>* outputs_not_required; + if (OpGradientDoesntRequireOutputIndices(c_op_name, &outputs_not_required)) { if (outputs_not_required->first) { op_outputs = Py_None;