pool_grad_kernel.cu 5.6 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 18 19 20
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/backends/gpu/gpu_info.h"
#include "paddle/phi/backends/gpu/gpu_launch_config.h"
#include "paddle/phi/core/kernel_registry.h"
21
#include "paddle/phi/core/tensor_utils.h"
22
#include "paddle/phi/core/visit_type.h"
23
#include "paddle/phi/kernels/empty_kernel.h"
Z
zhangkaihuo 已提交
24 25 26 27 28 29 30
#include "paddle/phi/kernels/funcs/math_function.h"
#include "paddle/phi/kernels/funcs/pooling.h"
#include "paddle/phi/kernels/funcs/sparse/convolution.h"

namespace phi {
namespace sparse {

31
template <typename T, typename IntT = int>
Z
zhangkaihuo 已提交
32 33 34
__global__ void MaxPoolGradCudaKernel(const T* in_features_ptr,
                                      const T* out_features_ptr,
                                      const T* out_grad_ptr,
35
                                      const IntT* rulebook_ptr,
Z
zhangkaihuo 已提交
36 37 38 39 40 41 42 43
                                      const int n,
                                      const int rulebook_len,
                                      const int channels,
                                      T* x_grad_ptr) {
  phi::funcs::MaxPoolGrad<T> grad_functor;
  CUDA_KERNEL_LOOP_TYPE(i, n * channels, int64_t) {
    int real_i = i / channels;
    int c = i - real_i * channels;
44 45
    IntT in_i = rulebook_ptr[real_i];
    IntT out_i = rulebook_ptr[real_i + rulebook_len];
Z
zhangkaihuo 已提交
46 47 48 49 50 51 52 53
    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]);
  }
}

54
template <typename T, typename IntT = int>
55 56 57
void MaxPoolCooGradGPUKernel(const GPUContext& dev_ctx,
                             const SparseCooTensor& x,
                             const DenseTensor& rulebook,
58
                             const DenseTensor& counter,
59 60 61 62
                             const SparseCooTensor& out,
                             const SparseCooTensor& out_grad,
                             const std::vector<int>& kernel_sizes,
                             SparseCooTensor* x_grad) {
Z
zhangkaihuo 已提交
63 64 65
  int kernel_size = kernel_sizes[0] * kernel_sizes[1] * kernel_sizes[2];
  const int in_channels = x.dims()[4];
  int rulebook_len = rulebook.dims()[1];
66
  const IntT* rulebook_ptr = rulebook.data<IntT>();
67 68 69
  std::vector<int> offsets(kernel_size + 1);
  const int* counter_ptr = counter.data<int>();
  phi::funcs::sparse::PrefixSum(counter_ptr, &offsets[0], kernel_size);
Z
zhangkaihuo 已提交
70 71 72

  const T* in_features_ptr = x.non_zero_elements().data<T>();
  const T* out_features_ptr = out.non_zero_elements().data<T>();
73 74 75 76 77 78 79 80 81 82 83 84 85 86
  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>();
  phi::funcs::SetConstant<GPUContext, T> set_zero;
  set_zero(dev_ctx, &x_grad_values, static_cast<T>(0.0f));
  phi::Copy<GPUContext>(dev_ctx,
                        x.non_zero_indices(),
                        dev_ctx.GetPlace(),
                        false,
                        &x_grad_indices);
Z
zhangkaihuo 已提交
87 88

  for (int i = 0; i < kernel_size; i++) {
89
    if (counter_ptr[i] <= 0) {
Z
zhangkaihuo 已提交
90 91 92 93
      continue;
    }

    auto config = phi::backends::gpu::GetGpuLaunchConfig1D(
94
        dev_ctx, counter_ptr[i] * in_channels, 1);
95 96 97 98 99 100 101
    MaxPoolGradCudaKernel<T, IntT>
        <<<config.block_per_grid.x,
           config.thread_per_block.x,
           0,
           dev_ctx.stream()>>>(in_features_ptr,
                               out_features_ptr,
                               out_grad_ptr,
102 103
                               rulebook_ptr + offsets[i],
                               counter_ptr[i],
104 105 106
                               rulebook_len,
                               in_channels,
                               x_grad_ptr);
Z
zhangkaihuo 已提交
107 108 109
  }
}

110
template <typename T, typename Context>
111 112 113
void MaxPoolCooGradKernel(const Context& dev_ctx,
                          const SparseCooTensor& x,
                          const DenseTensor& rulebook,
114
                          const DenseTensor& counter,
115 116 117 118
                          const SparseCooTensor& out,
                          const SparseCooTensor& out_grad,
                          const std::vector<int>& kernel_sizes,
                          SparseCooTensor* x_grad) {
Z
zhangkaihuo 已提交
119
  PD_VISIT_BASE_INTEGRAL_TYPES(
120 121
      x.non_zero_indices().dtype(), "MaxPoolCooGradGPUKernel", ([&] {
        MaxPoolCooGradGPUKernel<T, data_t>(
122
            dev_ctx, x, rulebook, counter, out, out_grad, kernel_sizes, x_grad);
123 124 125
      }));
}

Z
zhangkaihuo 已提交
126 127 128
}  // namespace sparse
}  // namespace phi

129
PD_REGISTER_KERNEL(maxpool_coo_grad,
Z
zhangkaihuo 已提交
130 131
                   GPU,
                   ALL_LAYOUT,
132
                   phi::sparse::MaxPoolCooGradKernel,
Z
zhangkaihuo 已提交
133 134 135 136
                   float,
                   double) {
  kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}