/* 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. */ #pragma once #include "paddle/phi/common/place.h" #include "paddle/phi/kernels/copy_kernel.h" #include "paddle/phi/kernels/funcs/broadcast_function.h" #include "paddle/phi/kernels/funcs/elementwise_grad_base.h" #include "paddle/phi/kernels/funcs/reduce_function.h" namespace phi { template void ReduceWrapper(const GPUContext &dev_ctx, int axis, DenseTensor *src, DenseTensor *dst) { std::vector reduce_dims = funcs::GetReduceDim(dst->dims(), src->dims(), axis); funcs::ReduceKernel>( dev_ctx, *src, dst, kps::IdentityFunctor(), reduce_dims); } template void GetGradXAndYOut(const GPUContext &dev_ctx, const Place &place, int axis, std::vector ins, const DenseTensor &dout, DenseTensor *dx, DenseTensor *dy, Functor func) { DenseTensor tmp_dx; DenseTensor tmp_dy; dev_ctx.Alloc(dx); dev_ctx.Alloc(dy); std::vector outs; if (dx->dims() == dout.dims() && dy->dims() == dout.dims()) { outs = {dx, dy}; } else if (dx->dims() != dout.dims() && dy->dims() == dout.dims()) { tmp_dx.Resize(dout.dims()); dev_ctx.Alloc(&tmp_dx); outs = {&tmp_dx, dy}; } else if (dx->dims() == dout.dims() && dy->dims() != dout.dims()) { tmp_dy.Resize(dout.dims()); dev_ctx.Alloc(&tmp_dy); outs = {dx, &tmp_dy}; } else if (dx->dims() != dout.dims() && dy->dims() != dout.dims()) { tmp_dy.Resize(dout.dims()); dev_ctx.Alloc(&tmp_dy); tmp_dx.Resize(dout.dims()); dev_ctx.Alloc(&tmp_dx); outs = {&tmp_dx, &tmp_dy}; } funcs::BroadcastKernel( dev_ctx, ins, &outs, axis, func); if (dx->dims() != dout.dims() && dy->dims() == dout.dims()) { ReduceWrapper(dev_ctx, axis, &tmp_dx, dx); } else if (dx->dims() == dout.dims() && dy->dims() != dout.dims()) { ReduceWrapper(dev_ctx, axis, &tmp_dy, dy); } else if (dx->dims() != dout.dims() && dy->dims() != dout.dims()) { ReduceWrapper(dev_ctx, axis, &tmp_dx, dx); ReduceWrapper(dev_ctx, axis, &tmp_dy, dy); } } template void GetGradXOrYOut(const GPUContext &dev_ctx, const Place &place, int axis, std::vector ins, const DenseTensor &dout, DenseTensor *dxy, Functor func) { DenseTensor tmp_dxy; dev_ctx.Alloc(dxy); std::vector outs; if (dxy->dims() != dout.dims()) { tmp_dxy.Resize(dout.dims()); dev_ctx.Alloc(&tmp_dxy); outs = {&tmp_dxy}; } else { outs = {dxy}; } funcs::BroadcastKernel(dev_ctx, ins, &outs, axis, func); if (dxy->dims() != dout.dims()) { ReduceWrapper(dev_ctx, axis, &tmp_dxy, dxy); } } /* ****************************** Add Grad ****************************** */ template static __global__ void SimpleElemwiseAddGradCUDAKernel( const T *__restrict__ dout, int size, int vec_size, T *dx, T *dy) { int tid = BLOCK_ID_X * BLOCK_NUM_X + THREAD_ID_X; int stride = GRID_NUM_X * BLOCK_NUM_X; int loop = size / vec_size; int remainder = size % vec_size; const float4 *dout_vec = reinterpret_cast(dout); float4 *dx_vec = reinterpret_cast(dx); float4 *dy_vec = reinterpret_cast(dy); float4 tmp_loop; for (int i = tid; i < loop; i += stride) { tmp_loop = dout_vec[i]; dx_vec[i] = tmp_loop; dy_vec[i] = tmp_loop; } if (tid == loop && remainder != 0) { T tmp_rem; while (remainder) { int idx = size - remainder; remainder--; tmp_rem = dout[idx]; dx[idx] = tmp_rem; dy[idx] = tmp_rem; } } } template void DefaultElementwiseAddGrad(const GPUContext &ctx, const DenseTensor &x, const DenseTensor &y, const DenseTensor &out, const DenseTensor &dout, DenseTensor *dx, DenseTensor *dy, int axis = -1) { auto *dout_data = dout.data(); // dx if (dx != nullptr) { auto *dx_data = dx->mutable_data(ctx.GetPlace()); if (dx->dims() == dout.dims()) { if (dx_data != dout_data) { phi::Copy(ctx, dout, ctx.GetPlace(), false, dx); } } else { // For inplace strategy, dx will be stored in addr of dout, which makes // the result of dy wrong. if (dx->IsSharedBufferWith(dout)) { dx->clear(); dx->mutable_data(x.dims(), ctx.GetPlace()); } std::vector reduce_dims = funcs::GetReduceDim(x.dims(), out.dims(), axis); funcs::ReduceKernel>( ctx, dout, dx, kps::IdentityFunctor(), reduce_dims); } } // dy if (dy != nullptr) { auto *dy_data = dy->mutable_data(ctx.GetPlace()); if (dy->dims() == dout.dims()) { if (dy_data != dout_data) { phi::Copy(ctx, dout, ctx.GetPlace(), false, dy); } } else { std::vector reduce_dims = funcs::GetReduceDim(y.dims(), out.dims(), axis); funcs::ReduceKernel>( ctx, dout, dy, kps::IdentityFunctor(), reduce_dims); } } } template void ElementwiseAddGrad(const GPUContext &ctx, const DenseTensor &x, const DenseTensor &y, const DenseTensor &out, const DenseTensor &dout, DenseTensor *dx, DenseTensor *dy) { ctx.template Alloc(dx); ctx.template Alloc(dy); auto *dx_data = dx->data(); auto *dy_data = dy->data(); auto *dout_data = dout.data(); if (dx_data == dout_data && dy_data != dout_data) { VLOG(4) << "Special case when dx_data is the same as dout_data, " "only need copy dout to dy"; phi::Copy(ctx, dout, ctx.GetPlace(), false, dy); } else if (dx_data != dout_data && dy_data == dout_data) { VLOG(4) << "Special case when dy_data is the same as dout_data, " "only need copy dout to dx"; phi::Copy(ctx, dout, ctx.GetPlace(), false, dx); } else if (dx_data != dout_data && dy_data != dout_data) { auto size = x.numel(); int vec_size = max(static_cast(sizeof(float4) / sizeof(T)), 1); dim3 block_size = dim3(PREDEFINED_BLOCK_SIZE, 1); dim3 grid_size = dim3(((size + vec_size - 1) / vec_size + PREDEFINED_BLOCK_SIZE - 1) / PREDEFINED_BLOCK_SIZE, 1); SimpleElemwiseAddGradCUDAKernel <<>>( dout.data(), size, vec_size, dx->mutable_data(ctx.GetPlace()), dy->mutable_data(ctx.GetPlace())); } else { VLOG(4) << "Special case when dy_data is the same as dout_data, " "and dx_data is the same as dout_data, do not need " "any operator"; } } /* ****************************** Sub Grad ****************************** */ template static __global__ void SimpleElemwiseSubGradCUDAKernel(const T *dout, int64_t size, T *dx, T *dy) { int col = BLOCK_ID_X * BLOCK_NUM_X + THREAD_ID_X; while (col < size) { if (dx != nullptr) { dx[col] = dout[col]; } dy[col] = -dout[col]; col += BLOCK_NUM_X * GRID_NUM_X; } } template void default_elementwise_sub_grad(const GPUContext &ctx, const DenseTensor &x, const DenseTensor &y, const DenseTensor &out, const DenseTensor &dout, DenseTensor *dx, DenseTensor *dy, int axis = -1) { auto *dout_data = dout.data(); // dx if (dx != nullptr) { auto *dx_data = dx->mutable_data(ctx.GetPlace()); if (dx->dims() == dout.dims()) { if (dx_data != dout_data) { phi::Copy(ctx, dout, ctx.GetPlace(), false, dx); } } else { // For inplace strategy, dx will be stored in addr of dout, which makes // the result of dy wrong. if (dx->IsSharedBufferWith(dout)) { dx->clear(); dx->mutable_data(x.dims(), ctx.GetPlace()); } std::vector reduce_dims = funcs::GetReduceDim(x.dims(), out.dims(), axis); funcs::ReduceKernel>( ctx, dout, dx, kps::IdentityFunctor(), reduce_dims); } } // dy if (dy != nullptr) { auto *dy_data = dy->mutable_data(ctx.GetPlace()); if (dy->dims() == dout.dims()) { if (dy_data != dout_data) { dim3 block_size = dim3(PREDEFINED_BLOCK_SIZE, 1); auto size = dy->numel(); dim3 grid_size = dim3((size + PREDEFINED_BLOCK_SIZE - 1) / PREDEFINED_BLOCK_SIZE, 1); SimpleElemwiseSubGradCUDAKernel <<>>( dout.data(), size, nullptr, dy->mutable_data(ctx.GetPlace())); } } else { std::vector reduce_dims = funcs::GetReduceDim(y.dims(), out.dims(), axis); funcs::ReduceKernel>( ctx, dout, dy, kps::InverseFunctor(), reduce_dims); } } } template void elementwise_sub_grad(const GPUContext &ctx, const DenseTensor &x, const DenseTensor &y, const DenseTensor &out, const DenseTensor &dout, DenseTensor *dx, DenseTensor *dy) { dim3 block_size = dim3(PREDEFINED_BLOCK_SIZE, 1); auto size = x.numel(); dim3 grid_size = dim3((size + PREDEFINED_BLOCK_SIZE - 1) / PREDEFINED_BLOCK_SIZE, 1); SimpleElemwiseSubGradCUDAKernel <<>>( dout.data(), size, dx->mutable_data(ctx.GetPlace()), dy->mutable_data(ctx.GetPlace())); } /* ****************************** Div Grad ****************************** */ template void ElementwiseDivGrad(const GPUContext &dev_ctx, const DenseTensor &x, const DenseTensor &y, const DenseTensor &out, const DenseTensor &dout, DenseTensor *dx, DenseTensor *dy, int axis = -1) { const auto place = dev_ctx.GetPlace(); if (dx != nullptr && dy != nullptr) { std::vector ins = {&dout, &out, &y}; GetGradXAndYOut( dev_ctx, place, axis, ins, dout, dx, dy, funcs::DivGradXYFunctor()); } else if (dx != nullptr && dy == nullptr) { std::vector ins = {&dout, &y}; GetGradXOrYOut( dev_ctx, place, axis, ins, dout, dx, funcs::DivGradXFunctor()); } else if (dy != nullptr && dx == nullptr) { std::vector ins = {&dout, &out, &y}; GetGradXOrYOut( dev_ctx, place, axis, ins, dout, dy, funcs::DivGradYFunctor()); } } /* ****************************** Mul Grad ****************************** */ template void ElementwiseMulGrad(const GPUContext &dev_ctx, const DenseTensor &x, const DenseTensor &y, const DenseTensor &dout, DenseTensor *dx, DenseTensor *dy, int axis) { const auto place = dev_ctx.GetPlace(); if (dx != nullptr && dy != nullptr) { std::vector ins = {&dout, &y, &x}; GetGradXAndYOut( dev_ctx, place, axis, ins, dout, dx, dy, funcs::MultiplyGradXYFunctor()); } else if (dx != nullptr && dy == nullptr) { std::vector ins = {&dout, &y}; GetGradXOrYOut( dev_ctx, place, axis, ins, dout, dx, funcs::MultiplyGradFunctor()); } else if (dx == nullptr && dy != nullptr) { std::vector ins = {&dout, &x}; GetGradXOrYOut( dev_ctx, place, axis, ins, dout, dy, funcs::MultiplyGradFunctor()); } } } // namespace phi