From 3e3de0fdf5b333995442d8082a6b8af979ba4d88 Mon Sep 17 00:00:00 2001 From: Peng Wang Date: Wed, 22 Apr 2020 13:16:23 -0700 Subject: [PATCH] Improves tf-numpy's `clip` to pass testClipStaticBounds, and adds uint8 kernels to tf.minimum and tf.maximum. PiperOrigin-RevId: 307882806 Change-Id: If5d7cf3c8943dd22b5bbf30c3a5582091e626fcb --- tensorflow/core/kernels/cwise_op_gpu_maximum.cu.cc | 2 +- tensorflow/core/kernels/cwise_op_gpu_minimum.cu.cc | 2 +- tensorflow/core/kernels/cwise_op_maximum.cc | 8 ++++---- tensorflow/core/kernels/cwise_op_minimum.cc | 8 ++++---- tensorflow/core/ops/math_ops.cc | 4 ++-- tensorflow/python/kernel_tests/cwise_ops_test.py | 3 ++- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/tensorflow/core/kernels/cwise_op_gpu_maximum.cu.cc b/tensorflow/core/kernels/cwise_op_gpu_maximum.cu.cc index a68791da499..53530f91b2a 100644 --- a/tensorflow/core/kernels/cwise_op_gpu_maximum.cu.cc +++ b/tensorflow/core/kernels/cwise_op_gpu_maximum.cu.cc @@ -19,7 +19,7 @@ limitations under the License. namespace tensorflow { namespace functor { -DEFINE_BINARY5(maximum, Eigen::half, float, double, int16, int64); +DEFINE_BINARY6(maximum, Eigen::half, float, double, uint8, int16, int64); } // namespace functor } // namespace tensorflow diff --git a/tensorflow/core/kernels/cwise_op_gpu_minimum.cu.cc b/tensorflow/core/kernels/cwise_op_gpu_minimum.cu.cc index df0f4311262..beab671616d 100644 --- a/tensorflow/core/kernels/cwise_op_gpu_minimum.cu.cc +++ b/tensorflow/core/kernels/cwise_op_gpu_minimum.cu.cc @@ -19,7 +19,7 @@ limitations under the License. namespace tensorflow { namespace functor { -DEFINE_BINARY5(minimum, Eigen::half, float, double, int16, int64); +DEFINE_BINARY6(minimum, Eigen::half, float, double, uint8, int16, int64); } // namespace functor } // namespace tensorflow diff --git a/tensorflow/core/kernels/cwise_op_maximum.cc b/tensorflow/core/kernels/cwise_op_maximum.cc index a7b58d8f7ac..5ebfa74eb4e 100644 --- a/tensorflow/core/kernels/cwise_op_maximum.cc +++ b/tensorflow/core/kernels/cwise_op_maximum.cc @@ -16,11 +16,11 @@ limitations under the License. #include "tensorflow/core/kernels/cwise_ops_common.h" namespace tensorflow { -REGISTER7(BinaryOp, CPU, "Maximum", functor::maximum, float, Eigen::half, - bfloat16, double, int16, int32, int64); +REGISTER8(BinaryOp, CPU, "Maximum", functor::maximum, float, Eigen::half, + bfloat16, double, uint8, int16, int32, int64); #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM -REGISTER5(BinaryOp, GPU, "Maximum", functor::maximum, float, Eigen::half, - double, int16, int64); +REGISTER6(BinaryOp, GPU, "Maximum", functor::maximum, float, Eigen::half, + double, uint8, int16, int64); // A special GPU kernel for int32. // TODO(b/25387198): Also enable int32 in device memory. This kernel diff --git a/tensorflow/core/kernels/cwise_op_minimum.cc b/tensorflow/core/kernels/cwise_op_minimum.cc index 25cbbc87072..8b301e8ce64 100644 --- a/tensorflow/core/kernels/cwise_op_minimum.cc +++ b/tensorflow/core/kernels/cwise_op_minimum.cc @@ -16,11 +16,11 @@ limitations under the License. #include "tensorflow/core/kernels/cwise_ops_common.h" namespace tensorflow { -REGISTER7(BinaryOp, CPU, "Minimum", functor::minimum, float, Eigen::half, - bfloat16, double, int16, int32, int64); +REGISTER8(BinaryOp, CPU, "Minimum", functor::minimum, float, Eigen::half, + bfloat16, double, uint8, int16, int32, int64); #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM -REGISTER5(BinaryOp, GPU, "Minimum", functor::minimum, float, Eigen::half, - double, int16, int64); +REGISTER6(BinaryOp, GPU, "Minimum", functor::minimum, float, Eigen::half, + double, uint8, int16, int64); // A special GPU kernel for int32. // TODO(b/25387198): Also enable int32 in device memory. This kernel diff --git a/tensorflow/core/ops/math_ops.cc b/tensorflow/core/ops/math_ops.cc index 8f4a9c8dca8..650360a50fb 100644 --- a/tensorflow/core/ops/math_ops.cc +++ b/tensorflow/core/ops/math_ops.cc @@ -549,7 +549,7 @@ REGISTER_OP("Maximum") .Input("x: T") .Input("y: T") .Output("z: T") - .Attr("T: {bfloat16, half, float, double, int16, int32, int64}") + .Attr("T: {bfloat16, half, float, double, uint8, int16, int32, int64}") .SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn); // Note: This op is not commutative w.r.t. to all its inputs. @@ -573,7 +573,7 @@ REGISTER_OP("Minimum") .Input("x: T") .Input("y: T") .Output("z: T") - .Attr("T: {bfloat16, half, float, double, int16, int32, int64}") + .Attr("T: {bfloat16, half, float, double, uint8, int16, int32, int64}") .SetShapeFn(shape_inference::BroadcastBinaryOpShapeFn); REGISTER_OP("Mod") diff --git a/tensorflow/python/kernel_tests/cwise_ops_test.py b/tensorflow/python/kernel_tests/cwise_ops_test.py index 4f9e35a42e8..d01d647bc83 100644 --- a/tensorflow/python/kernel_tests/cwise_ops_test.py +++ b/tensorflow/python/kernel_tests/cwise_ops_test.py @@ -733,7 +733,8 @@ class MinMaxOpTest(test.TestCase): def testBasic(self): x = np.random.rand(1, 3, 2) * 100. y = np.random.rand(1, 3, 2) * 100. - for t in [np.float16, np.float32, np.float64, np.int16, np.int32, np.int64]: + for t in [np.float16, np.float32, np.float64, np.uint8, np.int16, np.int32, + np.int64]: self._compare(x.astype(t), y.astype(t), use_gpu=False) self._compare(x.astype(t), y.astype(t), use_gpu=True)