pool_grad_kernel.cc 4.1 KB
Newer Older
Z
zhangkaihuo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include "paddle/phi/kernels/sparse/pool_grad_kernel.h"
16

Z
zhangkaihuo 已提交
17
#include "paddle/phi/core/kernel_registry.h"
18
#include "paddle/phi/core/tensor_utils.h"
19
#include "paddle/phi/core/visit_type.h"
20
#include "paddle/phi/kernels/empty_kernel.h"
Z
zhangkaihuo 已提交
21 22 23 24 25 26
#include "paddle/phi/kernels/funcs/pooling.h"
#include "paddle/phi/kernels/funcs/sparse/convolution.h"

namespace phi {
namespace sparse {

27
template <typename T, typename IntT = int>
28 29 30 31 32 33 34
void MaxPoolCooGradCPUKernel(const CPUContext& dev_ctx,
                             const SparseCooTensor& x,
                             const DenseTensor& rulebook,
                             const SparseCooTensor& out,
                             const SparseCooTensor& out_grad,
                             const std::vector<int>& kernel_sizes,
                             SparseCooTensor* x_grad) {
Z
zhangkaihuo 已提交
35 36 37
  int kernel_size = kernel_sizes[0] * kernel_sizes[1] * kernel_sizes[2];
  const int channels = x.dims()[4];
  int rulebook_len = rulebook.dims()[1];
38
  const IntT* rulebook_ptr = rulebook.data<IntT>();
Z
zhangkaihuo 已提交
39 40 41 42 43 44 45 46
  std::vector<int> offsets(kernel_size + 1), counter(kernel_size, 0);
  for (int i = 0; i < rulebook_len; i++) {
    counter[rulebook_ptr[i]] += 1;
  }
  phi::funcs::sparse::PrefixSum(&counter[0], &offsets[0], kernel_size);

  const T* in_features_ptr = x.non_zero_elements().data<T>();
  const T* out_features_ptr = out.non_zero_elements().data<T>();
47 48 49 50 51 52 53
  const T* out_grad_ptr = out_grad.non_zero_elements().data<T>();
  // TODO(zhangkaihuo): call phi::sparse::EmptyLike
  DenseTensor x_grad_indices =
      phi::EmptyLike<IntT>(dev_ctx, x.non_zero_indices());
  DenseTensor x_grad_values = phi::EmptyLike<T>(dev_ctx, x.non_zero_elements());
  x_grad->SetMember(x_grad_indices, x_grad_values, x.dims(), true);
  T* x_grad_ptr = x_grad_values.data<T>();
Z
zhangkaihuo 已提交
54
  memset(x_grad_ptr, 0, sizeof(T) * x_grad_values.numel());
55 56 57 58 59
  phi::Copy<CPUContext>(dev_ctx,
                        x.non_zero_indices(),
                        dev_ctx.GetPlace(),
                        false,
                        &x_grad_indices);
Z
zhangkaihuo 已提交
60 61 62 63

  phi::funcs::MaxPoolGrad<T> grad_functor;
  for (int i = 0; i < kernel_size; i++) {
    for (int j = 0; j < counter[i]; j++) {
64 65
      IntT in_i = rulebook_ptr[rulebook_len + offsets[i] + j];
      IntT out_i = rulebook_ptr[rulebook_len * 2 + offsets[i] + j];
Z
zhangkaihuo 已提交
66 67 68 69 70 71 72 73 74 75 76
      for (int c = 0; c < channels; c++) {
        grad_functor.compute(in_features_ptr[in_i * channels + c],
                             out_features_ptr[out_i * channels + c],
                             out_grad_ptr[out_i * channels + c],
                             1,
                             &x_grad_ptr[in_i * channels + c]);
      }
    }
  }
}

77
template <typename T, typename Context>
78 79 80 81 82 83 84
void MaxPoolCooGradKernel(const Context& dev_ctx,
                          const SparseCooTensor& x,
                          const DenseTensor& rulebook,
                          const SparseCooTensor& out,
                          const SparseCooTensor& out_grad,
                          const std::vector<int>& kernel_sizes,
                          SparseCooTensor* x_grad) {
85
  PD_VISIT_INTEGRAL_TYPES(
86 87
      x.non_zero_indices().dtype(), "MaxPoolCooGradCPUKernel", ([&] {
        MaxPoolCooGradCPUKernel<T, data_t>(
88 89 90 91
            dev_ctx, x, rulebook, out, out_grad, kernel_sizes, x_grad);
      }));
}

Z
zhangkaihuo 已提交
92 93 94
}  // namespace sparse
}  // namespace phi

95
PD_REGISTER_KERNEL(maxpool_coo_grad,
Z
zhangkaihuo 已提交
96 97
                   CPU,
                   ALL_LAYOUT,
98
                   phi::sparse::MaxPoolCooGradKernel,
Z
zhangkaihuo 已提交
99 100 101 102
                   float,
                   double) {
  kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}