/* Copyright (c) 2020 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. Indicesou 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 #ifdef __NVCC__ #include "cub/cub.cuh" #endif #ifdef __HIPCC__ #include namespace cub = hipcub; #endif #include "paddle/fluid/operators/amp/fp16_type_traits.h" #include "paddle/fluid/operators/elementwise/elementwise_op_impl.cu.h" #include "paddle/fluid/operators/p_norm_op.h" #include "paddle/fluid/operators/reduce_ops/reduce_op.cu.h" #include "paddle/fluid/operators/reduce_ops/reduce_op.h" #include "paddle/fluid/platform/float16.h" namespace paddle { namespace operators { template __device__ __forceinline__ int sgn(T val) { return (T(0) < val) - (val < T(0)); } __device__ __forceinline__ platform::float16 inline_abs(platform::float16 x) { return static_cast(abs(static_cast(x))); } __device__ __forceinline__ float inline_abs(float x) { return abs(x); } __device__ __forceinline__ double inline_abs(double x) { return abs(x); } __device__ __forceinline__ int inline_sign(platform::float16 x) { return sgn(x); } __device__ __forceinline__ int inline_sign(float x) { return sgn(x); } __device__ __forceinline__ int inline_sign(double x) { return sgn(x); } __device__ __forceinline__ platform::float16 inline_pow( platform::float16 base, platform::float16 exponent) { return static_cast( pow(static_cast(base), static_cast(exponent))); } __device__ __forceinline__ float inline_pow(float base, float exponent) { return pow(base, exponent); } __device__ __forceinline__ double inline_pow(double base, double exponent) { return pow(base, exponent); } template struct NonzeroFunctor { HOSTDEVICE explicit inline NonzeroFunctor() {} HOSTDEVICE inline T operator()(const T& x) const { return static_cast(static_cast(x) != 0); } }; template struct AbsFunctor { HOSTDEVICE explicit inline AbsFunctor() {} 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 Ty operator()(const Tx& x) const { return static_cast(inline_pow(inline_abs(x), static_cast(porder))); } float porder; }; template struct PowFunctor { HOSTDEVICE explicit inline PowFunctor(float porder) { this->porder = porder; } HOSTDEVICE inline Ty operator()(const Tx& x) const { return static_cast(inline_pow(x, static_cast(porder))); } float porder; }; template class PnormCUDAKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* in_x = ctx.Input("X"); auto* out_norm = ctx.Output("Out"); const T* x = in_x->data(); T* norm = out_norm->mutable_data(ctx.GetPlace()); auto xdim = in_x->dims(); auto ndim = out_norm->dims(); float porder = ctx.Attr("porder"); int axis = ctx.Attr("axis"); if (axis < 0) axis = xdim.size() + axis; std::vector reduce_axis = {axis}; auto stream = ctx.cuda_device_context().stream(); using MT = typename details::MPTypeTrait::Type; if (porder == 0) { TensorReduceFunctorImpl>( *in_x, out_norm, NonzeroFunctor(), reduce_axis, stream); } else if (porder == INFINITY) { TensorReduceFunctorImpl>( *in_x, out_norm, AbsFunctor(), reduce_axis, stream); } else if (porder == -INFINITY) { TensorReduceFunctorImpl>( *in_x, out_norm, AbsFunctor(), reduce_axis, stream); } else { framework::Tensor tmp_x; tmp_x.mutable_data(xdim, ctx.GetPlace()); std::vector ins = {in_x}; std::vector outs = {&tmp_x}; auto func = UnsignedPowFunctor(porder); const auto& cuda_ctx = ctx.template device_context(); LaunchSameDimsElementwiseCudaKernel>( cuda_ctx, ins, &outs, func); framework::Tensor tmp_y; tmp_y.mutable_data(ndim, ctx.GetPlace()); TensorReduceFunctorImpl>( tmp_x, &tmp_y, kps::IdentityFunctor(), reduce_axis, stream); const framework::Tensor* tmp_norm = &tmp_y; ins = {tmp_norm}; outs = {out_norm}; auto func_inverse = UnsignedPowFunctor(1. / porder); LaunchSameDimsElementwiseCudaKernel>( cuda_ctx, ins, &outs, func_inverse); } } }; template struct AbsMaxAndMinGradFunctor { template void operator()(const DeviceContext& place, X* x, Y* y, DX* dx, DY* dy, const Dim& dim, int size) { auto equals = ((*x).abs() == y->broadcast(dim)); auto ones = dx->constant(static_cast(1.)); auto negs = dx->constant(static_cast(-1.)); auto zeros = dx->constant(static_cast(0.)); auto positives = (*x) > zeros; dx->device(place) = dy->broadcast(dim) * equals.select(ones, zeros) * positives.select(ones, negs); } }; template struct PNormPostGradFunctor { template void operator()(const DeviceContext& place, X* x, Y* y, DX* dx, DY* dy, const Dim& dim, int size) { auto ones = dx->constant(static_cast(1.)); auto negs = dx->constant(static_cast(-1.)); auto zeros = dx->constant(static_cast(0.)); auto positives = (*x) > zeros; dx->device(place) = (*dx) * dy->broadcast(dim) * y->broadcast(dim) * positives.select(ones, negs); } }; template class PnormGradCUDAKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto* in_x = ctx.Input("X"); auto* in_norm = ctx.Input("Out"); auto* in_norm_dy = ctx.Input(framework::GradVarName("Out")); auto* out_dx = ctx.Output(framework::GradVarName("X")); T* dx = out_dx->mutable_data(ctx.GetPlace()); auto xdim = in_x->dims(); float porder = ctx.Attr("porder"); int axis = ctx.Attr("axis"); bool reduce_all = ((axis < 0) || (in_norm->numel() == 1)); if (axis < 0) axis = xdim.size() + axis; const std::vector dims = {axis}; auto& cuda_ctx = ctx.template device_context(); if (porder == 0) { math::SetConstant set_zero; set_zero(cuda_ctx, out_dx, static_cast(0)); } else if (porder == INFINITY || porder == -INFINITY) { LaunchReduceGradKernel>( ctx, in_x, in_norm, in_norm_dy, out_dx, dims, reduce_all); } else { framework::Tensor tmp_norm; tmp_norm.mutable_data(in_norm->dims(), ctx.GetPlace()); std::vector ins = {in_norm}; std::vector outs = {&tmp_norm}; auto pow_functor = PowFunctor(1. - porder); LaunchSameDimsElementwiseCudaKernel>(cuda_ctx, ins, &outs, pow_functor); ins = {in_x}; outs = {out_dx}; auto unsigned_pow = UnsignedPowFunctor(porder - 1.); LaunchSameDimsElementwiseCudaKernel>( cuda_ctx, ins, &outs, unsigned_pow); const framework::Tensor* tmp_norm_const = &tmp_norm; LaunchReduceGradKernel>( ctx, in_x, tmp_norm_const, in_norm_dy, out_dx, dims, reduce_all); } } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; using CUDA = paddle::platform::CUDADeviceContext; REGISTER_OP_CUDA_KERNEL(p_norm, ops::PnormCUDAKernel, ops::PnormCUDAKernel, ops::PnormCUDAKernel); REGISTER_OP_CUDA_KERNEL( p_norm_grad, ops::PnormGradCUDAKernel, ops::PnormGradCUDAKernel, ops::PnormGradCUDAKernel);