// Copyright (c) 2022 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/p_norm_kernel.h" #include "paddle/phi/common/amp_type_traits.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/funcs/elementwise_base.h" #include "paddle/phi/kernels/funcs/p_norm_utils.h" #include "paddle/phi/kernels/funcs/reduce_function.h" #include "paddle/phi/kernels/gpu/reduce.h" #include "paddle/phi/kernels/activation_kernel.h" namespace phi { template struct NonzeroFunctor { HOSTDEVICE explicit inline NonzeroFunctor() = default; HOSTDEVICE inline T operator()(const T x) const { return static_cast(static_cast(x) != 0); } }; template struct AbsFunctor { HOSTDEVICE explicit inline AbsFunctor() = default; HOSTDEVICE inline T operator()(const T x) const { return static_cast(inline_abs(x)); } }; template struct UnsignedPowFunctor { HOSTDEVICE explicit inline UnsignedPowFunctor(float porder) { this->porder = porder; } HOSTDEVICE inline T operator()(const T x) const { return static_cast(inline_pow(inline_abs(x), static_cast(porder))); } float porder; }; #ifndef _WIN32 // To avoid large .so size in Windows cuda11.8 template struct FabsFunctor { HOSTDEVICE explicit inline FabsFunctor() = default; HOSTDEVICE inline T operator()(const T x) const { return static_cast(inline_fabs(x)); } }; template struct SquareFunctor { HOSTDEVICE explicit inline SquareFunctor() = default; HOSTDEVICE inline T operator()(const T x) const { return static_cast(inline_square(x)); } }; template struct FabsCubicFunctor { HOSTDEVICE explicit inline FabsCubicFunctor() = default; HOSTDEVICE inline T operator()(const T x) const { return static_cast(inline_fabs_cubic(x)); } }; #endif template void PNormKernel(const Context& dev_ctx, const DenseTensor& x, double porder, int axis, float epsilon, bool keepdim, bool asvector, DenseTensor* out) { auto* in_x = &x; auto* out_norm = out; T* norm = dev_ctx.template Alloc(out); auto xdim = in_x->dims(); std::vector axis_dims = {static_cast(axis)}; std::vector reduce_axis = funcs::details::GetReduceDim(axis_dims, xdim.size(), asvector); if (x.numel() == 0) { if (out->numel() > 0) { std::vector vec_dims = vectorize(out->dims()); Full(dev_ctx, IntArray(vec_dims), static_cast(0), out); } return; } if (porder == 0) { funcs::ReduceGpuKernel( dev_ctx, *in_x, out_norm, reduce_axis); } else if (porder == INFINITY) { funcs::ReduceGpuKernel( dev_ctx, *in_x, out_norm, reduce_axis); } else if (porder == -INFINITY) { funcs::ReduceGpuKernel( dev_ctx, *in_x, out_norm, reduce_axis); } else { #ifdef _WIN32 funcs::ReduceKernel>( dev_ctx, *in_x, out_norm, UnsignedPowFunctor(porder), reduce_axis); const DenseTensor* tmp_norm = out_norm; std::vector ins = {tmp_norm}; std::vector outs = {out_norm}; funcs::ElementwiseKernel( dev_ctx, ins, &outs, UnsignedPowFunctor(1. / porder)); #else if (porder == 1.0) { // fast 1-norm funcs::ReduceGpuKernel( dev_ctx, *in_x, out_norm, reduce_axis); } else if (porder == 2.0) { // fast 2-norm funcs::ReduceGpuKernel( dev_ctx, *in_x, out_norm, reduce_axis); } else { // vanilla norm using MT = typename MPTypeTrait::Type; funcs::ReduceGpuKernel( dev_ctx, *in_x, out_norm, reduce_axis, porder); } #endif } } } // namespace phi PD_REGISTER_KERNEL(p_norm, GPU, ALL_LAYOUT, phi::PNormKernel, float, double, phi::float16, phi::bfloat16) {}