abs_grad_kernel_impl.h 2.7 KB
Newer Older
F
From00 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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

17 18
#include "paddle/phi/kernels/abs_grad_kernel.h"
#include "paddle/phi/kernels/funcs/complex_functors.h"
19
#include "paddle/phi/kernels/funcs/elementwise_base.h"
C
Chen Weihang 已提交
20
#include "paddle/phi/kernels/funcs/for_range.h"
F
From00 已提交
21

22
namespace phi {
F
From00 已提交
23

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#if defined(__NVCC__)
template <typename T>
void AbsGradKernelImpl(const GPUContext& dev_ctx,
                       const DenseTensor& x,
                       const DenseTensor& dout,
                       DenseTensor* dx) {
  std::vector<const DenseTensor*> ins = {&x, &dout};
  std::vector<DenseTensor*> outs = {dx};
  dev_ctx.Alloc<T>(dx);
  phi::funcs::AbsGradCUDAFunctor<T> abs_grad_cuda_functor;
  phi::funcs::ElementwiseKernel<T>(dev_ctx, ins, &outs, abs_grad_cuda_functor);
}
template <typename T, typename Context>
void AbsGradKernel(const Context& dev_ctx,
                   const DenseTensor& x,
                   const DenseTensor& dout,
                   DenseTensor* dx) {
  AbsGradKernelImpl<T>(dev_ctx, x, dout, dx);
}
#else
F
From00 已提交
44 45 46 47 48 49
template <typename T, typename Context>
void AbsGradKernel(const Context& ctx,
                   const DenseTensor& x,
                   const DenseTensor& dout,
                   DenseTensor* dx) {
  auto numel = dout.numel();
50
  auto* dout_data = dout.data<phi::dtype::Real<T>>();
F
From00 已提交
51 52 53 54 55
  auto* x_data = x.data<T>();

  ctx.template Alloc<T>(dx, static_cast<size_t>(numel * sizeof(T)));
  auto* dx_data = dx->data<T>();

C
Chen Weihang 已提交
56
  phi::funcs::ForRange<Context> for_range(ctx, numel);
57
  phi::funcs::AbsGradFunctor<T> functor(dout_data, x_data, dx_data, numel);
F
From00 已提交
58 59 60
  for_range(functor);
}

61
#endif
F
From00 已提交
62 63 64 65 66 67 68 69 70 71 72
template <typename T, typename Context>
void AbsDoubleGradKernel(const Context& ctx,
                         const DenseTensor& x,
                         const DenseTensor& ddx,
                         DenseTensor* ddout) {
  auto numel = ddx.numel();
  auto* ddx_data = ddx.data<T>();
  auto* x_data = x.data<T>();
  ctx.template Alloc<T>(ddout, static_cast<size_t>(numel * sizeof(T)));
  auto* ddout_data = ddout->data<T>();

C
Chen Weihang 已提交
73
  phi::funcs::ForRange<Context> for_range(ctx, numel);
74
  phi::funcs::AbsGradGradFunctor<T> functor(
F
From00 已提交
75 76 77 78
      ddx_data, x_data, ddout_data, numel);
  for_range(functor);
}

79
}  // namespace phi