From def81b4f8642cbb27737009d1f3a73f76cd389a6 Mon Sep 17 00:00:00 2001 From: Zhang Ting Date: Mon, 24 Jan 2022 16:14:09 +0800 Subject: [PATCH] unify compare functor (#39024) --- .../fluid/operators/controlflow/compare_op.h | 59 ++++++++----------- paddle/fluid/operators/matrix_rank_op.cc | 18 +++--- paddle/fluid/operators/matrix_rank_op.cu | 8 +-- paddle/fluid/operators/matrix_rank_op.h | 11 +--- paddle/fluid/operators/viterbi_decode_op.cu | 5 +- paddle/fluid/operators/viterbi_decode_op.h | 5 +- .../fluid/tests/unittests/test_compare_op.py | 13 ++++ 7 files changed, 59 insertions(+), 60 deletions(-) diff --git a/paddle/fluid/operators/controlflow/compare_op.h b/paddle/fluid/operators/controlflow/compare_op.h index d2ef4c9befb..957efbff199 100644 --- a/paddle/fluid/operators/controlflow/compare_op.h +++ b/paddle/fluid/operators/controlflow/compare_op.h @@ -22,49 +22,40 @@ limitations under the License. */ namespace paddle { namespace operators { -template -struct LessThanFunctor { - using ELEM_TYPE = T; - HOSTDEVICE bool operator()(const T a, const T b) const { return a < b; } -}; - -template -struct LessEqualFunctor { - using ELEM_TYPE = T; - HOSTDEVICE bool operator()(const T a, const T b) const { return a <= b; } -}; - -template -struct GreaterThanFunctor { - using ELEM_TYPE = T; - HOSTDEVICE bool operator()(const T a, const T b) const { return a > b; } -}; - -template -struct GreaterEqualFunctor { - using ELEM_TYPE = T; - HOSTDEVICE bool operator()(const T a, const T b) const { return a >= b; } -}; - -template +#define COMPARE_FUNCTOR(func_name, op) \ + template \ + struct func_name { \ + using ELEM_TYPE = InT; \ + HOSTDEVICE OutT operator()(const InT a, const InT b) const { \ + return static_cast(a op b); \ + } \ + }; + +COMPARE_FUNCTOR(LessThanFunctor, <) +COMPARE_FUNCTOR(LessEqualFunctor, <=) +COMPARE_FUNCTOR(GreaterThanFunctor, >) +COMPARE_FUNCTOR(GreaterEqualFunctor, >=) +#undef COMPARE_FUNCTOR + +template struct EqualFunctor { - using ELEM_TYPE = T; - HOSTDEVICE bool operator()(const T a, const T b) const { - if (std::is_floating_point::value) { + using ELEM_TYPE = InT; + HOSTDEVICE OutT operator()(const InT a, const InT b) const { + if (std::is_floating_point::value) { // This branch will be optimized while compiling if T is integer. It is // safe to cast a and b to double. - return fabs(static_cast(a - b)) < 1e-8; + return static_cast(fabs(static_cast(a - b)) < 1e-8); } else { - return (a == b); + return static_cast(a == b); } } }; -template +template struct NotEqualFunctor { - using ELEM_TYPE = T; - HOSTDEVICE bool operator()(const T a, const T b) const { - return !EqualFunctor()(a, b); + using ELEM_TYPE = InT; + HOSTDEVICE bool operator()(const InT a, const InT b) const { + return !EqualFunctor()(a, b); } }; diff --git a/paddle/fluid/operators/matrix_rank_op.cc b/paddle/fluid/operators/matrix_rank_op.cc index 3038a16dc0a..ddfb8d50c4e 100644 --- a/paddle/fluid/operators/matrix_rank_op.cc +++ b/paddle/fluid/operators/matrix_rank_op.cc @@ -219,18 +219,20 @@ class MatrixRankCPUKernel : public framework::OpKernel { tol_tensor.Resize(detail::NewAxisDim(tol_tensor.dims(), 1)); Tensor compare_result; - compare_result.mutable_data(detail::NewAxisDim(dim_out, k), - context.GetPlace()); + compare_result.mutable_data(detail::NewAxisDim(dim_out, k), + context.GetPlace()); int axis = -1; if (eigenvalue_tensor.dims().size() >= tol_tensor.dims().size()) { - ElementwiseComputeEx, platform::CPUDeviceContext, T, - int>(context, &eigenvalue_tensor, &tol_tensor, axis, - GreaterThanFunctor(), &compare_result); + ElementwiseComputeEx, + platform::CPUDeviceContext, T, int>( + context, &eigenvalue_tensor, &tol_tensor, axis, + GreaterThanFunctor(), &compare_result); } else { - ElementwiseComputeEx, platform::CPUDeviceContext, T, - int>(context, &eigenvalue_tensor, &tol_tensor, axis, - LessThanFunctor(), &compare_result); + ElementwiseComputeEx, + platform::CPUDeviceContext, T, int>( + context, &eigenvalue_tensor, &tol_tensor, axis, + LessThanFunctor(), &compare_result); } auto dito_int = math::DeviceIndependenceTensorOperations { compare_result.mutable_data(detail::NewAxisDim(dim_out, k), context.GetPlace()); int axis = -1; - ElementwiseComputeEx, platform::CUDADeviceContext, T, - int64_t>(context, &eigenvalue_tensor, &tol_tensor, - axis, GreaterThanFunctor(), - &compare_result); + ElementwiseComputeEx, + platform::CUDADeviceContext, T, int64_t>( + context, &eigenvalue_tensor, &tol_tensor, axis, + GreaterThanFunctor(), &compare_result); auto dito_int = math::DeviceIndependenceTensorOperations(context); diff --git a/paddle/fluid/operators/matrix_rank_op.h b/paddle/fluid/operators/matrix_rank_op.h index c3d99a21b72..550bc445ac4 100644 --- a/paddle/fluid/operators/matrix_rank_op.h +++ b/paddle/fluid/operators/matrix_rank_op.h @@ -16,6 +16,7 @@ #include #include "paddle/fluid/framework/ddim.h" #include "paddle/fluid/framework/tensor.h" +#include "paddle/fluid/operators/controlflow/compare_op.h" namespace paddle { namespace operators { @@ -46,16 +47,6 @@ static DDim RemoveLastDim(const DDim& dim) { } } // namespace detail -template -struct GreaterThanFunctor { - HOSTDEVICE int operator()(const T a, const T b) const { return a > b; } -}; - -template -struct LessThanFunctor { - HOSTDEVICE int operator()(const T a, const T b) const { return a < b; } -}; - template struct GreaterElementFunctor { HOSTDEVICE T operator()(const T a, const T b) const { diff --git a/paddle/fluid/operators/viterbi_decode_op.cu b/paddle/fluid/operators/viterbi_decode_op.cu index d40d14435a5..7ad3335009b 100644 --- a/paddle/fluid/operators/viterbi_decode_op.cu +++ b/paddle/fluid/operators/viterbi_decode_op.cu @@ -72,7 +72,8 @@ struct BinaryOperation { } }; -template