diff --git a/paddle/fluid/operators/controlflow/compare_op.h b/paddle/fluid/operators/controlflow/compare_op.h index d2ef4c9befba99290008508e43df6c84f969b710..957efbff1993792c1cc6162296dbdcf00abb61cf 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 3038a16dc0a5e53a0b8f7aa49942bcb916191f48..ddfb8d50c4e11ba47b7ac13b9da6405955d0ebf6 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 c3d99a21b72358df5dedc7741072a7913de174af..550bc445ac4e66a74965fe635a36c95b33dbed29 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 d40d14435a5fd016a9ab5aaeb0436f13654a510b..7ad3335009b06056fb624ef97305ac549b14035f 100644 --- a/paddle/fluid/operators/viterbi_decode_op.cu +++ b/paddle/fluid/operators/viterbi_decode_op.cu @@ -72,7 +72,8 @@ struct BinaryOperation { } }; -template