abs_grad_kernel_impl.h 2.0 KB
Newer Older
F
From00 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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/fluid/platform/for_range.h"
18 19
#include "paddle/phi/kernels/abs_grad_kernel.h"
#include "paddle/phi/kernels/funcs/complex_functors.h"
F
From00 已提交
20

21
namespace phi {
F
From00 已提交
22 23 24 25 26 27 28

template <typename T, typename Context>
void AbsGradKernel(const Context& ctx,
                   const DenseTensor& x,
                   const DenseTensor& dout,
                   DenseTensor* dx) {
  auto numel = dout.numel();
29
  auto* dout_data = dout.data<phi::funcs::Real<T>>();
F
From00 已提交
30 31 32 33 34 35
  auto* x_data = x.data<T>();

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

  paddle::platform::ForRange<Context> for_range(ctx, numel);
36
  phi::funcs::AbsGradFunctor<T> functor(dout_data, x_data, dx_data, numel);
F
From00 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  for_range(functor);
}

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>();

  paddle::platform::ForRange<Context> for_range(ctx, numel);
52
  phi::funcs::AbsGradGradFunctor<T> functor(
F
From00 已提交
53 54 55 56
      ddx_data, x_data, ddout_data, numel);
  for_range(functor);
}

57
}  // namespace phi