diff options
-rw-r--r-- | tensorflow/cc/gradients/math_grad.cc | 9 | ||||
-rw-r--r-- | tensorflow/cc/gradients/math_grad_test.cc | 6 | ||||
-rw-r--r-- | tensorflow/core/kernels/cwise_op_div.cc | 4 | ||||
-rw-r--r-- | tensorflow/core/ops/math_grad_test.cc | 23 | ||||
-rw-r--r-- | tensorflow/core/ops/math_ops.cc | 5 |
5 files changed, 24 insertions, 23 deletions
diff --git a/tensorflow/cc/gradients/math_grad.cc b/tensorflow/cc/gradients/math_grad.cc index 84552e7c5e..c6e60689fa 100644 --- a/tensorflow/cc/gradients/math_grad.cc +++ b/tensorflow/cc/gradients/math_grad.cc @@ -442,16 +442,17 @@ Status RealDivGrad(const Scope& scope, const Operation& op, REGISTER_GRADIENT_OP("RealDiv", RealDivGrad); Status UnsafeDivGrad(const Scope& scope, const Operation& op, - const std::vector<Output>& grad_inputs, - std::vector<Output>* grad_outputs) { + const std::vector<Output>& grad_inputs, + std::vector<Output>* grad_outputs) { auto x_1 = ConjugateHelper(scope, op.input(0)); auto x_2 = ConjugateHelper(scope, op.input(1)); // y = x_1 / x_2 // dy/dx_1 = 1/x_2 // dy/dx_2 = -x_1/x_2^2 auto gx_1 = UnsafeDiv(scope, grad_inputs[0], x_2); - auto gx_2 = Mul(scope, grad_inputs[0], - UnsafeDiv(scope, UnsafeDiv(scope, Neg(scope, x_1), x_2), x_2)); + auto gx_2 = + Mul(scope, grad_inputs[0], + UnsafeDiv(scope, UnsafeDiv(scope, Neg(scope, x_1), x_2), x_2)); return BinaryGradCommon(scope, op, grad_outputs, gx_1, gx_2); } REGISTER_GRADIENT_OP("UnsafeDiv", UnsafeDivGrad); diff --git a/tensorflow/cc/gradients/math_grad_test.cc b/tensorflow/cc/gradients/math_grad_test.cc index 330d1722af..12a19bcf28 100644 --- a/tensorflow/cc/gradients/math_grad_test.cc +++ b/tensorflow/cc/gradients/math_grad_test.cc @@ -860,7 +860,8 @@ TEST_F(NaryGradTest, UnsafeDiv) { const auto x = Placeholder(scope_, DT_FLOAT, Placeholder::Shape(x_shape)); // Test x / (1 + |x|) rather than x_1 / x_2 to avoid triggering large // division errors in the numeric estimator used by the gradient checker. - const auto y = UnsafeDiv(scope_, x, Add(scope_, Const<float>(scope_, 1), Abs(scope_, x))); + const auto y = UnsafeDiv( + scope_, x, Add(scope_, Const<float>(scope_, 1), Abs(scope_, x))); RunTest({x}, {x_shape}, {y}, {x_shape}); } { @@ -873,7 +874,8 @@ TEST_F(NaryGradTest, UnsafeDiv) { TF_EXPECT_OK(AddSymbolicGradients(scope_, {y}, {x}, &grad_outputs)); ClientSession session(scope_); std::vector<Tensor> grad_result; - TF_EXPECT_OK(session.Run({{x, {-3.0f, 0.0f, 3.0f}}}, grad_outputs, &grad_result)); + TF_EXPECT_OK( + session.Run({{x, {-3.0f, 0.0f, 3.0f}}}, grad_outputs, &grad_result)); EXPECT_EQ(grad_result.size(), 1); EXPECT_EQ(grad_result[0].NumElements(), 3); EXPECT_EQ(grad_result[0].flat<float>()(0), 0.0f); diff --git a/tensorflow/core/kernels/cwise_op_div.cc b/tensorflow/core/kernels/cwise_op_div.cc index e6d458925c..d6a2403816 100644 --- a/tensorflow/core/kernels/cwise_op_div.cc +++ b/tensorflow/core/kernels/cwise_op_div.cc @@ -24,8 +24,8 @@ REGISTER5(BinaryOp, CPU, "TruncateDiv", functor::safe_div, uint8, uint16, int16, int32, int64); REGISTER6(BinaryOp, CPU, "RealDiv", functor::div, float, Eigen::half, double, bfloat16, complex64, complex128); -REGISTER5(BinaryOp, CPU, "UnsafeDiv", functor::unsafe_div, - float, double, int16, int32, int64); +REGISTER5(BinaryOp, CPU, "UnsafeDiv", functor::unsafe_div, float, double, int16, + int32, int64); #if GOOGLE_CUDA REGISTER9(BinaryOp, GPU, "Div", functor::div, float, Eigen::half, double, uint8, diff --git a/tensorflow/core/ops/math_grad_test.cc b/tensorflow/core/ops/math_grad_test.cc index f05297d234..b0d1595c31 100644 --- a/tensorflow/core/ops/math_grad_test.cc +++ b/tensorflow/core/ops/math_grad_test.cc @@ -754,10 +754,8 @@ TEST_F(MathGradTest, Div) { } TEST_F(MathGradTest, UnsafeDiv) { - auto x = test::AsTensor<float>({0.f, -3.f, -2.f, - -1.f, 0.f, 1.f, - 2.f, 3.f, 0.f}, - TensorShape({3, 3})); + auto x = test::AsTensor<float>( + {0.f, -3.f, -2.f, -1.f, 0.f, 1.f, 2.f, 3.f, 0.f}, TensorShape({3, 3})); auto y = test::AsTensor<float>({-10.f, 0.f, 10.f}, TensorShape({3, 1})); Tensor dx; Tensor dy; @@ -781,8 +779,7 @@ TEST_F(MathGradTest, UnsafeDiv) { auto g = [](float x, float y) { if (y == 0.f) { return 0.f; - } - else { + } else { return -x / (y * y); } }; @@ -804,18 +801,18 @@ TEST_F(MathGradTest, UnsafeDiv) { return 1.f / y; } }; - test::ExpectClose(dy, test::AsTensor<float>( - {g(-10.f, 0.f) + g(-10.f, -3.f) + g(-10.f, -2.f), - g(0.f, -1.f) + g(0.f, 0.f) + g(0.f, 1.f), - g(10.f, 2.f) + g(10.f, 3.f) + g(10.f, 0.f)}, - TensorShape({3, 1}))); + test::ExpectClose(dy, + test::AsTensor<float>( + {g(-10.f, 0.f) + g(-10.f, -3.f) + g(-10.f, -2.f), + g(0.f, -1.f) + g(0.f, 0.f) + g(0.f, 1.f), + g(10.f, 2.f) + g(10.f, 3.f) + g(10.f, 0.f)}, + TensorShape({3, 1}))); } { auto g = [](float x, float y) { if (y == 0.f) { return 0.f; - } - else { + } else { return -x / (y * y); } }; diff --git a/tensorflow/core/ops/math_ops.cc b/tensorflow/core/ops/math_ops.cc index 4716f4341e..49646f1f3a 100644 --- a/tensorflow/core/ops/math_ops.cc +++ b/tensorflow/core/ops/math_ops.cc @@ -392,8 +392,9 @@ Returns x * y element-wise. REGISTER_OP("Div").BINARY_MORE().SetShapeFn( shape_inference::BroadcastBinaryOpShapeFn); -REGISTER_OP("UnsafeDiv").BINARY_MORE().SetShapeFn( - shape_inference::BroadcastBinaryOpShapeFn); +REGISTER_OP("UnsafeDiv") + .BINARY_MORE() + .SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn); REGISTER_OP("FloorDiv") .BINARY_MORE() |