nanmedian_grad_kernel.cc 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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/nanmedian_grad_kernel.h"
16

17 18 19
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/math_function.h"
20
#include "paddle/phi/kernels/funcs/nanmedian_utils.h"
21 22 23 24 25 26 27 28

namespace phi {

template <typename T, typename Context>
void CalcMedianGradKernel(const Context& dev_ctx,
                          const DenseTensor& x,
                          const DenseTensor& median_index,
                          const DenseTensor& out_grad,
29 30 31 32
                          DenseTensor* x_grad) {
  T* dx_data = dev_ctx.template Alloc<T>(x_grad);
  if (!dx_data) return;

33 34 35
  phi::funcs::SetConstant<Context, T> set_zero;
  set_zero(dev_ctx, x_grad, static_cast<T>(0));

36 37
  const int64_t* m_data = median_index.data<int64_t>();
  const T* dout_data = out_grad.data<T>();
38 39 40 41 42
  int64_t numel = x.numel();
  auto x_dim = x.dims();
  int64_t rank = x_dim.size();
  int64_t stride = x_dim[rank - 1];
  int64_t pre_dim = numel / stride;
43

44 45 46
  int64_t i = 0;
  int64_t offset = 0;
  for (i = 0; i < pre_dim; i++) {
47 48 49
    if (m_data[2 * i] >= 0) {
      if (m_data[2 * i] == m_data[2 * i + 1]) {
        dx_data[offset + m_data[2 * i]] = dout_data[i];
50
      } else {
51 52 53
        dx_data[offset + m_data[2 * i]] = dout_data[i] / static_cast<T>(2.0);
        dx_data[offset + m_data[2 * i + 1]] =
            dout_data[i] / static_cast<T>(2.0);
54 55 56 57 58 59 60 61
      }
    }
    offset += stride;
  }
}

template <typename T, typename Context>
void NanmedianGradKernel(const Context& dev_ctx,
62
                         const DenseTensor& x,
63 64 65
                         const DenseTensor& median_index,
                         const DenseTensor& out_grad,
                         const IntArray& axes,
66
                         bool keepdim UNUSED,
67
                         DenseTensor* x_grad) {
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  DenseTensor tmp_x;
  auto rank = x.dims().size();
  if ((axes.size() == 0) || rank <= 1) {
    tmp_x = x;
    tmp_x.Resize({x.numel()});
    CalcMedianGradKernel<T, Context>(
        dev_ctx, tmp_x, median_index, out_grad, x_grad);
  } else {
    funcs::PreprocessMedianKernel<T, Context>(dev_ctx, x, axes, &tmp_x);

    DenseTensor tmp_x_grad;
    tmp_x_grad.Resize(x_grad->dims());
    CalcMedianGradKernel<T, Context>(
        dev_ctx, tmp_x, median_index, out_grad, &tmp_x_grad);

    dev_ctx.template Alloc<T>(x_grad);
    funcs::PostprocessMedianGradKernel<T, Context>(
        dev_ctx, &tmp_x_grad, axes, x_grad);
  }
87 88 89 90 91 92 93 94 95 96 97 98
}

}  // namespace phi

PD_REGISTER_KERNEL(nanmedian_grad,
                   CPU,
                   ALL_LAYOUT,
                   phi::NanmedianGradKernel,
                   float,
                   double,
                   int,
                   int64_t) {}