// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "paddle/phi/kernels/compare_kernel.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/funcs/elementwise_base.h" #include "paddle/phi/kernels/impl/compare_kernel_impl.h" namespace phi { template inline void CompareRawKernelImpl(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, int axis, DenseTensor* out) { dev_ctx.template Alloc(out); if (x.dims().size() >= y.dims().size()) { funcs::ElementwiseCompute( dev_ctx, x, y, Functor(), out, axis); } else { funcs::ElementwiseCompute( dev_ctx, x, y, InverseFunctor(), out, axis); } } template void LessThanRawKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, int axis, DenseTensor* out) { CompareRawKernelImpl, funcs::GreaterThanFunctor>(dev_ctx, x, y, axis, out); } template void LessEqualRawKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, int axis, DenseTensor* out) { CompareRawKernelImpl, funcs::GreaterEqualFunctor>(dev_ctx, x, y, axis, out); } template void GreaterThanRawKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, int axis, DenseTensor* out) { CompareRawKernelImpl, funcs::LessThanFunctor>(dev_ctx, x, y, axis, out); } template void GreaterEqualRawKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, int axis, DenseTensor* out) { CompareRawKernelImpl, funcs::LessEqualFunctor>(dev_ctx, x, y, axis, out); } template void EqualRawKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, int axis, DenseTensor* out) { CompareRawKernelImpl, funcs::EqualFunctor>(dev_ctx, x, y, axis, out); } template void NotEqualRawKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& y, int axis, DenseTensor* out) { CompareRawKernelImpl, funcs::NotEqualFunctor>(dev_ctx, x, y, axis, out); } } // namespace phi PD_REGISTER_KERNEL(less_than_raw, CPU, ALL_LAYOUT, phi::LessThanRawKernel, bool, uint8_t, int8_t, int16_t, int, int64_t, phi::complex64, phi::complex128, float, double, phi::float16, phi::bfloat16) { kernel->OutputAt(0).SetDataType(phi::DataType::BOOL); } #define PD_REGISTER_COMPLEX_COMPARE_RAW_KERNEL(name, func) \ PD_REGISTER_KERNEL(name##_raw, \ CPU, \ ALL_LAYOUT, \ phi::func##RawKernel, \ bool, \ uint8_t, \ int8_t, \ int16_t, \ int, \ int64_t, \ phi::complex64, \ phi::complex128, \ float, \ double, \ phi::float16, \ phi::bfloat16) { \ kernel->OutputAt(0).SetDataType(phi::DataType::BOOL); \ } PD_REGISTER_COMPLEX_COMPARE_RAW_KERNEL(less_equal, LessEqual) PD_REGISTER_COMPLEX_COMPARE_RAW_KERNEL(greater_than, GreaterThan) PD_REGISTER_COMPLEX_COMPARE_RAW_KERNEL(greater_equal, GreaterEqual) PD_REGISTER_COMPLEX_COMPARE_RAW_KERNEL(equal, Equal) PD_REGISTER_COMPLEX_COMPARE_RAW_KERNEL(not_equal, NotEqual)