From 49a3eb441a7530d82b23128c5e0b00c4267ad0f5 Mon Sep 17 00:00:00 2001 From: naurril Date: Fri, 4 Aug 2017 22:48:03 +0800 Subject: [PATCH 001/445] Add int64_t typemap for swig --- tensorflow/python/platform/base.i | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tensorflow/python/platform/base.i b/tensorflow/python/platform/base.i index aa33eb52c97..dbefca2be96 100644 --- a/tensorflow/python/platform/base.i +++ b/tensorflow/python/platform/base.i @@ -106,6 +106,10 @@ limitations under the License. $1 = &temp; } +%typemap(out) int64_t { + $result = PyLong_FromLongLong($1); +} + %typemap(out) string { $result = PyBytes_FromStringAndSize($1.data(), $1.size()); } From 3b6d5d9df888181e5fb5a49885c0d4eb0b58c5eb Mon Sep 17 00:00:00 2001 From: Dan Jarvis Date: Thu, 23 Nov 2017 12:43:24 -0500 Subject: [PATCH 002/445] Update .gitignore files for Android Studio Without these, Git shows lots of noisy build/IDE config files. These files are based on these two sources (plus the list of added files that Git shows when I build in Android Studio): https://github.com/github/gitignore/blob/master/Android.gitignore https://github.com/github/gitignore/pull/2103/files --- tensorflow/contrib/lite/java/demo/.gitignore | 28 ++++++++++++++++--- tensorflow/examples/android/.gitignore | 29 ++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 tensorflow/examples/android/.gitignore diff --git a/tensorflow/contrib/lite/java/demo/.gitignore b/tensorflow/contrib/lite/java/demo/.gitignore index 39fb081a42a..d245ab61095 100644 --- a/tensorflow/contrib/lite/java/demo/.gitignore +++ b/tensorflow/contrib/lite/java/demo/.gitignore @@ -1,9 +1,29 @@ +# This file is based on https://github.com/github/gitignore/blob/master/Android.gitignore *.iml +.idea/compiler.xml +.idea/copyright +.idea/dictionaries +.idea/gradle.xml +.idea/libraries +.idea/inspectionProfiles +.idea/misc.xml +.idea/modules.xml +.idea/runConfigurations.xml +.idea/tasks.xml +.idea/workspace.xml .gradle -/local.properties -/.idea/workspace.xml -/.idea/libraries +local.properties .DS_Store -/build +build/ +gradleBuild/ +*.apk +*.ap_ +*.dex +*.class +bin/ +gen/ +out/ +*.log +.navigation/ /captures .externalNativeBuild diff --git a/tensorflow/examples/android/.gitignore b/tensorflow/examples/android/.gitignore new file mode 100644 index 00000000000..d245ab61095 --- /dev/null +++ b/tensorflow/examples/android/.gitignore @@ -0,0 +1,29 @@ +# This file is based on https://github.com/github/gitignore/blob/master/Android.gitignore +*.iml +.idea/compiler.xml +.idea/copyright +.idea/dictionaries +.idea/gradle.xml +.idea/libraries +.idea/inspectionProfiles +.idea/misc.xml +.idea/modules.xml +.idea/runConfigurations.xml +.idea/tasks.xml +.idea/workspace.xml +.gradle +local.properties +.DS_Store +build/ +gradleBuild/ +*.apk +*.ap_ +*.dex +*.class +bin/ +gen/ +out/ +*.log +.navigation/ +/captures +.externalNativeBuild From 6f1a30c2589e2b9adf3d34a4d87fb280b2e6b149 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Tue, 3 Apr 2018 23:44:22 +0000 Subject: [PATCH 003/445] Add RecordInput documentation to tf.contrib.framework This fix tries to address the issue raised in 10622 where the RecordInput is not available in the documentation. As was suggested in 10622, this fix exposes RecordInput to tf.contrib (tf.contrib.framework) so that docs could shown up. This fix fixes 10622. Signed-off-by: Yong Tang --- tensorflow/contrib/framework/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tensorflow/contrib/framework/__init__.py b/tensorflow/contrib/framework/__init__.py index 11397e86bd8..5435c98978d 100644 --- a/tensorflow/contrib/framework/__init__.py +++ b/tensorflow/contrib/framework/__init__.py @@ -119,6 +119,7 @@ from tensorflow.python.framework.smart_cond import smart_constant_value from tensorflow.python.framework.tensor_spec import BoundedTensorSpec from tensorflow.python.framework.tensor_spec import TensorSpec from tensorflow.python.ops.array_ops import broadcast_to +from tensorflow.python.ops.data_flow_ops import RecordInput from tensorflow.python.ops.init_ops import convolutional_delta_orthogonal from tensorflow.python.ops.init_ops import convolutional_orthogonal_1d from tensorflow.python.ops.init_ops import convolutional_orthogonal_2d From 5dc8f313b1808ae893edb3b8b03ba06ce4968437 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Tue, 3 Apr 2018 23:47:02 +0000 Subject: [PATCH 004/445] Add `@@RecordInput` so that docs could be exposed After this change and run with: ``` bazel run tools/docs:generate -- --src_dir="$(pwd)/docs_src/" --output_dir=/tmp/tfdocs/ ``` The documentation correctly shown up in: `/tmp/tfdocs/api_docs/python/tf/contrib/framework/RecordInput.md` Signed-off-by: Yong Tang --- tensorflow/contrib/framework/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tensorflow/contrib/framework/__init__.py b/tensorflow/contrib/framework/__init__.py index 5435c98978d..ffcfd6961ef 100644 --- a/tensorflow/contrib/framework/__init__.py +++ b/tensorflow/contrib/framework/__init__.py @@ -100,6 +100,8 @@ See the @{$python/contrib.framework} guide. @@BoundedTensorSpec @@TensorSpec + +@@RecordInput """ from __future__ import absolute_import From 14a9c4b3c9ca8aa93aeff6b8bc4ef2413cea1d24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yan=20Facai=20=28=E9=A2=9C=E5=8F=91=E6=89=8D=29?= Date: Fri, 4 May 2018 18:12:35 +0800 Subject: [PATCH 005/445] ENH: add UnsafeDiv op --- tensorflow/core/kernels/cwise_op_div.cc | 3 +++ tensorflow/core/kernels/cwise_ops.h | 24 ++++++++++++++++++++++++ tensorflow/core/ops/math_ops.cc | 3 +++ 3 files changed, 30 insertions(+) diff --git a/tensorflow/core/kernels/cwise_op_div.cc b/tensorflow/core/kernels/cwise_op_div.cc index b12652f7fba..97f457d7d71 100644 --- a/tensorflow/core/kernels/cwise_op_div.cc +++ b/tensorflow/core/kernels/cwise_op_div.cc @@ -24,6 +24,9 @@ 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); +REGISTER3(BinaryOp, CPU, "UnsafeDiv", functor::unsafe_div, + float, Eigen::half, double); + #if GOOGLE_CUDA REGISTER9(BinaryOp, GPU, "Div", functor::div, float, Eigen::half, double, uint8, uint16, int16, int64, complex64, complex128); diff --git a/tensorflow/core/kernels/cwise_ops.h b/tensorflow/core/kernels/cwise_ops.h index a80905d1450..a54892c53f3 100644 --- a/tensorflow/core/kernels/cwise_ops.h +++ b/tensorflow/core/kernels/cwise_ops.h @@ -153,6 +153,27 @@ struct functor_traits> { }; }; +template +struct unsafe_div_op { + EIGEN_EMPTY_STRUCT_CTOR(unsafe_div_op) + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T operator()(const T& a, + const T& b) const { + if (TF_PREDICT_TRUE(b != 0)) { + return scalar_quotient_op()(a, b); + } else { + return 0; + } + } +}; + +template +struct functor_traits> { + enum { + Cost = functor_traits>::Cost + NumTraits::AddCost, + PacketAccess = false, + }; +}; + // scalar_left and scalar_right are template helpers to partially // apply a binary function. // @@ -714,6 +735,9 @@ struct safe_div : base +struct unsafe_div : base> {}; + template struct fmod : base> {}; diff --git a/tensorflow/core/ops/math_ops.cc b/tensorflow/core/ops/math_ops.cc index 8f8443a46cf..1a2e1d70c17 100644 --- a/tensorflow/core/ops/math_ops.cc +++ b/tensorflow/core/ops/math_ops.cc @@ -375,6 +375,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("FloorDiv") .BINARY_MORE() .SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn); From 1bb62968ec5bd726f7cbc11a00b6001d64407ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yan=20Facai=20=28=E9=A2=9C=E5=8F=91=E6=89=8D=29?= Date: Sat, 5 May 2018 12:29:20 +0800 Subject: [PATCH 006/445] TST: add test case --- tensorflow/python/ops/math_ops.py | 22 ++++++++++++++++++++++ tensorflow/python/ops/math_ops_test.py | 15 +++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/tensorflow/python/ops/math_ops.py b/tensorflow/python/ops/math_ops.py index 477f8700604..29c14b975a5 100644 --- a/tensorflow/python/ops/math_ops.py +++ b/tensorflow/python/ops/math_ops.py @@ -1034,6 +1034,28 @@ def div(x, y, name=None): return _div_python2(x, y, name) +def unsafe_div(x, y, name=None): + """Divide two values using Python 2 semantics. Used for Tensor.__div__. + + Args: + x: `Tensor` numerator of real numeric type. + y: `Tensor` denominator of real numeric type. + name: A name for the operation (optional). + Returns: + `x / y` returns the quotient of x and y. + """ + + with ops.name_scope(name, "unsafe_div", [x, y]) as name: + x = ops.convert_to_tensor(x, name="x") + y = ops.convert_to_tensor(y, name="y", dtype=x.dtype.base_dtype) + x_dtype = x.dtype.base_dtype + y_dtype = y.dtype.base_dtype + if x_dtype != y_dtype: + raise TypeError("x and y must have the same dtype, got %r != %r" % + (x_dtype, y_dtype)) + return gen_math_ops.unsafe_div(x, y, name=name) + + # TODO(aselle): This should be removed mod = gen_math_ops.floor_mod diff --git a/tensorflow/python/ops/math_ops_test.py b/tensorflow/python/ops/math_ops_test.py index 05bcee88012..2b78a433f72 100644 --- a/tensorflow/python/ops/math_ops_test.py +++ b/tensorflow/python/ops/math_ops_test.py @@ -474,5 +474,20 @@ class DivAndModTest(test_util.TensorFlowTestCase): self.assertAllEqual(tf_result, expanded_nums) +@test_util.with_c_api +class UnsafeDivTest(test_util.TensorFlowTestCase): + + def testBasic(self): + nums = np.arange(-10, 10, .25).reshape(80, 1) + divs = np.arange(-3, 3, .25).reshape(1, 24) + + np_result = np.true_divide(nums, divs) + np_result[:, divs[0] == 0] = 0 + + with self.test_session(): + tf_result = math_ops.unsafe_div(nums, divs).eval() + self.assertAllEqual(tf_result, np_result) + + if __name__ == "__main__": googletest.main() From 043b574402c58e1cf629242b3faad3ec071e5ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yan=20Facai=20=28=E9=A2=9C=E5=8F=91=E6=89=8D=29?= Date: Sat, 5 May 2018 17:16:04 +0800 Subject: [PATCH 007/445] ENH: add gradient function --- tensorflow/cc/gradients/math_grad.cc | 15 ++++++++++++++ tensorflow/cc/gradients/math_grad_test.cc | 10 +++++++++ tensorflow/core/ops/math_ops_test.cc | 3 ++- tensorflow/python/ops/math_grad.py | 18 ++++++++++++++++ tensorflow/python/ops/math_grad_test.py | 25 +++++++++++++++++++++++ 5 files changed, 70 insertions(+), 1 deletion(-) diff --git a/tensorflow/cc/gradients/math_grad.cc b/tensorflow/cc/gradients/math_grad.cc index 52c177212a8..ea86fc0a7ca 100644 --- a/tensorflow/cc/gradients/math_grad.cc +++ b/tensorflow/cc/gradients/math_grad.cc @@ -440,6 +440,21 @@ Status RealDivGrad(const Scope& scope, const Operation& op, } REGISTER_GRADIENT_OP("RealDiv", RealDivGrad); +Status UnsafeDivGrad(const Scope& scope, const Operation& op, + const std::vector& grad_inputs, + std::vector* 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)); + return BinaryGradCommon(scope, op, grad_outputs, gx_1, gx_2); +} +REGISTER_GRADIENT_OP("UnsafeDiv", DivGrad); + Status SquaredDifferenceGrad(const Scope& scope, const Operation& op, const std::vector& grad_inputs, std::vector* grad_outputs) { diff --git a/tensorflow/cc/gradients/math_grad_test.cc b/tensorflow/cc/gradients/math_grad_test.cc index 1b4c7c26880..0cc398abcf1 100644 --- a/tensorflow/cc/gradients/math_grad_test.cc +++ b/tensorflow/cc/gradients/math_grad_test.cc @@ -46,6 +46,7 @@ using ops::RealDiv; using ops::SquaredDifference; using ops::Sub; using ops::Sum; +using ops::UnsafeDiv; using ops::Where3; // TODO(andydavis) Test gradient function against numeric gradients output. @@ -856,6 +857,15 @@ TEST_F(NaryGradTest, RealDiv) { RunTest({x}, {x_shape}, {y}, {x_shape}); } +TEST_F(NaryGradTest, UnsafeDiv) { + TensorShape x_shape({3, 2, 5}); + 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. + auto y = UnsafeDiv(scope_, x, Add(scope_, Const(scope_, 1), Abs(scope_, x))); + RunTest({x}, {x_shape}, {y}, {x_shape}); +} + TEST_F(NaryGradTest, SquaredDifference) { TensorShape x1_shape({3, 2, 5}); TensorShape x2_shape({2, 5}); diff --git a/tensorflow/core/ops/math_ops_test.cc b/tensorflow/core/ops/math_ops_test.cc index 8f974d5367a..33d91ec6177 100644 --- a/tensorflow/core/ops/math_ops_test.cc +++ b/tensorflow/core/ops/math_ops_test.cc @@ -120,7 +120,8 @@ TEST(MathOpsTest, BroadcastBinaryOps_ShapeFn) { "Maximum", "Minimum", "Mod", "Mul", "NotEqual", "Pow", - "Sub", "SquaredDifference"}) { + "Sub", "SquaredDifference", + "UnsafeDiv"}) { ShapeInferenceTestOp op(op_name); INFER_OK(op, "?;?", "?"); INFER_OK(op, "[1,2];?", "?"); diff --git a/tensorflow/python/ops/math_grad.py b/tensorflow/python/ops/math_grad.py index 02e07dc7b1f..339ae6039fc 100644 --- a/tensorflow/python/ops/math_grad.py +++ b/tensorflow/python/ops/math_grad.py @@ -940,6 +940,24 @@ def _RealDivGrad(op, grad): grad * math_ops.realdiv(math_ops.realdiv(-x, y), y), ry), sy)) +@ops.RegisterGradient("UnsafeDiv") +def _UnsafeDivGrad(op, grad): + """UnsafeDiv op gradient.""" + x = op.inputs[0] + y = op.inputs[1] + sx = array_ops.shape(x) + sy = array_ops.shape(y) + rx, ry = gen_array_ops.broadcast_gradient_args(sx, sy) + x = math_ops.conj(x) + y = math_ops.conj(y) + return (array_ops.reshape( + math_ops.reduce_sum(math_ops.unsafe_div(grad, y), rx), sx), + array_ops.reshape( + math_ops.reduce_sum( + grad * math_ops.unsafe_div(math_ops.unsafe_div(-x, y), y), + ry), sy)) + + @ops.RegisterGradient("Pow") def _PowGrad(op, grad): """Returns grad * (y*x^(y-1), z*log(x)).""" diff --git a/tensorflow/python/ops/math_grad_test.py b/tensorflow/python/ops/math_grad_test.py index 04eeb00518a..525cbd202c2 100644 --- a/tensorflow/python/ops/math_grad_test.py +++ b/tensorflow/python/ops/math_grad_test.py @@ -25,6 +25,7 @@ from tensorflow.python.framework import dtypes from tensorflow.python.framework import ops from tensorflow.python.ops import array_ops from tensorflow.python.ops import gradient_checker +from tensorflow.python.ops import gradients from tensorflow.python.ops import math_ops from tensorflow.python.platform import test @@ -208,5 +209,29 @@ class FloorModGradientTest(test.TestCase): self.assertLess(error, 1e-4) +class UnsafeDivGradientTest(test.TestCase): + + def testBasicGradient(self): + inputs = constant_op.constant(np.arange(-3, 3), + dtype=dtypes.float32) + outputs = math_ops.unsafe_div(inputs, 1 + abs(inputs)) + with self.test_session(): + error = gradient_checker.compute_gradient_error( + inputs, inputs.get_shape().as_list(), + outputs, outputs.get_shape().as_list()) + self.assertLess(error, 1e-4) + + def testGradientWithDenominatorIsZero(self): + x = constant_op.constant(np.arange(-3, 3), + dtype=dtypes.float32) + y = array_ops.zeros_like(x, + dtype=dtypes.float32) + outputs = math_ops.unsafe_div(x, y) + with self.test_session(): + dx, dy = gradients.gradients(outputs, [x, y]) + self.assertAllClose(dx.eval(), np.zeros(x.shape.as_list())) + self.assertAllClose(dy.eval(), np.zeros(y.shape.as_list())) + + if __name__ == "__main__": test.main() From 7ce6f5464f82c81383d3c4fddae01ed202902ebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yan=20Facai=20=28=E9=A2=9C=E5=8F=91=E6=89=8D=29?= Date: Sat, 5 May 2018 18:08:34 +0800 Subject: [PATCH 008/445] DOC: add document --- .../core/api_def/base_api/api_def_UnsafeDiv.pbtxt | 10 ++++++++++ .../core/api_def/python_api/api_def_UnsafeDiv.pbtxt | 4 ++++ tensorflow/python/ops/math_ops.py | 11 +++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 tensorflow/core/api_def/base_api/api_def_UnsafeDiv.pbtxt create mode 100644 tensorflow/core/api_def/python_api/api_def_UnsafeDiv.pbtxt diff --git a/tensorflow/core/api_def/base_api/api_def_UnsafeDiv.pbtxt b/tensorflow/core/api_def/base_api/api_def_UnsafeDiv.pbtxt new file mode 100644 index 00000000000..c8115b59a27 --- /dev/null +++ b/tensorflow/core/api_def/base_api/api_def_UnsafeDiv.pbtxt @@ -0,0 +1,10 @@ +op { + graph_op_name: "UnsafeDiv" + visibility: HIDDEN + summary: "Returns 0 if the denominator is zero." + description: <