conv_grad_kernel.cu 11.8 KB
Newer Older
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. */

Z
zhangkaihuo 已提交
15
#include "paddle/phi/kernels/sparse/conv_grad_kernel.h"
16

17
#include "glog/logging.h"
18 19 20 21
#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"
22
#include "paddle/phi/core/tensor_utils.h"
23
#include "paddle/phi/core/visit_type.h"
24 25
#include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/phi/kernels/funcs/math_function.h"
26
#include "paddle/phi/kernels/sparse/gpu/conv.cu.h"
27 28 29 30 31 32 33 34 35 36 37 38

namespace phi {
namespace sparse {

// rulebook[3, rulebook_len]:
//[
//  [kernel_index],
//  [in_i],
//  [out_i],
//]
// x_grad = out_grad * transpose(kenrel)
// kernel_grad = transpose(x) * out_grad
39
template <typename T, typename IntT>
Z
zhangkaihuo 已提交
40 41 42
void Conv3dCooGradGPUKernel(const GPUContext& dev_ctx,
                            const SparseCooTensor& x,
                            const DenseTensor& kernel,
43
                            const SparseCooTensor& out,
Z
zhangkaihuo 已提交
44
                            const DenseTensor& rulebook,
45
                            const DenseTensor& counter,
Z
zhangkaihuo 已提交
46 47 48 49 50 51
                            const SparseCooTensor& out_grad,
                            const std::vector<int>& paddings,
                            const std::vector<int>& dilations,
                            const std::vector<int>& strides,
                            const int groups,
                            const bool subm,
52
                            const std::string& key,
Z
zhangkaihuo 已提交
53 54
                            SparseCooTensor* x_grad,
                            DenseTensor* kernel_grad) {
55 56 57 58 59
  const auto& kernel_dims = kernel.dims();
  const int kernel_size = kernel_dims[0] * kernel_dims[1] * kernel_dims[2];
  const int in_channels = kernel_dims[3];
  const int out_channels = kernel_dims[4];

60 61 62 63
  int rulebook_len = 0;
  const IntT* rulebook_ptr = phi::funcs::sparse::GetRulebookPtr<IntT>(
      out, rulebook, key, &rulebook_len);
  const int* counter_ptr = phi::funcs::sparse::GetCounterPtr(out, counter, key);
64 65

  phi::DenseTensor in_features =
66
      phi::Empty<T>(dev_ctx, {rulebook_len, in_channels});
67
  phi::DenseTensor d_x_features =
68
      phi::Empty<T>(dev_ctx, {rulebook_len, in_channels});
69
  phi::DenseTensor out_grad_features =
70
      phi::Empty<T>(dev_ctx, {rulebook_len, out_channels});
71 72 73 74

  T* in_features_ptr = in_features.data<T>();
  T* d_x_features_ptr = d_x_features.data<T>();
  T* out_grad_features_ptr = out_grad_features.data<T>();
75
  *kernel_grad = phi::EmptyLike<T>(dev_ctx, kernel);
76
  T* d_kernel_ptr = kernel_grad->data<T>();
77 78
  phi::backends::gpu::GpuMemsetAsync(
      d_kernel_ptr, 0, sizeof(T) * kernel_grad->numel(), dev_ctx.stream());
79

Z
zhangkaihuo 已提交
80
  int half_kernel_size = kernel_size / 2;
81
  auto blas = phi::funcs::GetBlas<GPUContext, T>(dev_ctx);
82
  DenseTensor x_grad_indices =
83
      phi::EmptyLike<IntT>(dev_ctx, x.non_zero_indices());
84
  DenseTensor x_grad_values = phi::EmptyLike<T>(dev_ctx, x.values());
85
  T* x_grad_values_ptr = x_grad_values.data<T>();
86 87 88 89 90 91
  phi::backends::gpu::GpuMemsetAsync(x_grad_values_ptr,
                                     0,
                                     sizeof(T) * x_grad_values.numel(),
                                     dev_ctx.stream());
  phi::backends::gpu::GpuMemsetAsync(
      d_x_features_ptr, 0, sizeof(T) * d_x_features.numel(), dev_ctx.stream());
92 93 94 95 96
  phi::Copy<GPUContext>(dev_ctx,
                        x.non_zero_indices(),
                        dev_ctx.GetPlace(),
                        false,
                        &x_grad_indices);
97
  x_grad->SetMember(x_grad_indices, x_grad_values, x.dims(), true);
Z
zhangkaihuo 已提交
98

99
  std::vector<int> offsets(kernel_size + 1);
100

101
  int offset = 0, max_count = 0;
102 103
  for (int i = 0; i < kernel_size; i++) {
    offsets[i] = offset;
104
    offset += counter_ptr[i];
Z
zhangkaihuo 已提交
105
    if (i < half_kernel_size) {
106
      max_count = std::max(max_count, counter_ptr[i]);
Z
zhangkaihuo 已提交
107
    }
108 109 110
  }
  offsets[kernel_size] = offset;

Z
zhangkaihuo 已提交
111
  if (subm) {
112 113 114 115 116 117 118 119 120
    phi::funcs::sparse::SubmPreProcess<T, GPUContext>(dev_ctx,
                                                      x,
                                                      kernel,
                                                      out_grad.values(),
                                                      in_channels,
                                                      out_channels,
                                                      half_kernel_size,
                                                      kernel_grad,
                                                      &x_grad_values);
Z
zhangkaihuo 已提交
121 122 123 124 125
    if (max_count == 0) {
      return;
    }
  }

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  int max_voxel = counter_ptr[kernel_size];
  if (!subm) {
    const auto& x_dims = x.dims();
    Dims4D d_x_dims(x_dims[0], x_dims[3], x_dims[2], x_dims[1]);
    int64_t table_size = 1;
    for (int i = 0; i < x_dims.size() - 1; i++) {
      table_size *= x_dims[i];
    }
    DenseTensor in_index_table = phi::Empty<int>(dev_ctx, {table_size + 1});
    int* in_index_table_ptr = in_index_table.data<int>();
    phi::backends::gpu::GpuMemsetAsync(in_index_table_ptr,
                                       0,
                                       sizeof(int) * (table_size + 1),
                                       dev_ctx.stream());
    auto config = phi::backends::gpu::GetGpuLaunchConfig1D(dev_ctx, x.nnz(), 1);
    GetOutIndexTable<IntT, false>
        <<<config.block_per_grid,
           config.thread_per_block,
           0,
           dev_ctx.stream()>>>(x.non_zero_indices().data<IntT>(),
                               x.nnz(),
                               d_x_dims,
                               nullptr,
                               in_index_table_ptr,
                               in_index_table_ptr + table_size);

    phi::backends::gpu::GpuMemcpyAsync(&max_voxel,
                                       in_index_table_ptr + table_size,
                                       sizeof(int),
                                       gpuMemcpyDeviceToHost,
                                       dev_ctx.stream());
    dev_ctx.Wait();
  }

160 161 162
  auto config =
      phi::backends::gpu::GetGpuLaunchConfig1D(dev_ctx, rulebook_len, 1);
  DenseTensor unique_value = phi::Empty<int>(
163
      dev_ctx, {static_cast<int>(x_grad->nnz() * max_voxel * kernel_size * 2)});
164 165 166 167 168 169
  DenseTensor out_index =
      phi::Empty<int>(dev_ctx, {static_cast<int>(x.nnz() * 2)});
  int* out_index_ptr = out_index.data<int>();
  int* unique_value_ptr = unique_value.data<int>();
  phi::backends::gpu::GpuMemsetAsync(
      out_index_ptr, 0, sizeof(int) * x.nnz() * 2, dev_ctx.stream());
Z
zhangkaihuo 已提交
170

171 172 173 174 175
  GroupIndexsV2<<<config.block_per_grid,
                  config.thread_per_block,
                  0,
                  dev_ctx.stream()>>>(rulebook_len,
                                      x.nnz(),
176
                                      kernel_size * max_voxel,
177 178 179 180 181 182
                                      offsets[kernel_size / 2],
                                      rulebook_ptr,
                                      out_index_ptr,
                                      unique_value_ptr);

  GatherV2<T, IntT>(dev_ctx,
183
                    x.values().data<T>(),
184 185 186 187
                    out_index_ptr,
                    unique_value_ptr,
                    x.nnz(),
                    kernel_size,
188
                    max_voxel,
189 190 191 192 193
                    in_channels,
                    2,
                    in_features_ptr);

  Gather<T, IntT>(dev_ctx,
194
                  out_grad.values().data<T>(),
195 196 197 198
                  rulebook_ptr + rulebook_len,
                  rulebook_len,
                  out_channels,
                  out_grad_features_ptr);
Z
zhangkaihuo 已提交
199

200 201
  const T* kernel_ptr = kernel.data<T>();
  for (int i = 0; i < kernel_size; i++) {
202
    if (counter_ptr[i] <= 0 || (subm && i == half_kernel_size)) {
203 204 205
      continue;
    }

206
    const int M = counter_ptr[i];
207 208 209 210 211
    const int K = in_channels;
    const int N = out_channels;
    T* tmp_in_ptr = in_features_ptr + offsets[i] * in_channels;
    T* tmp_out_grad_ptr = out_grad_features_ptr + offsets[i] * out_channels;
    const T* tmp_kernel_ptr = kernel_ptr + i * in_channels * out_channels;
212
    T* tmp_d_x_ptr = d_x_features_ptr + offsets[i] * in_channels;
213 214 215 216 217 218 219
    T* tmp_d_kernel_ptr = d_kernel_ptr + i * in_channels * out_channels;

    // call gemm: d_kernel = transpose(x) * out_grad
    // (in_channels, n) * (n, out_channels)
    blas.GEMM(CblasTrans,
              CblasNoTrans,
              K,
220 221
              N,
              M,
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
              static_cast<T>(1),
              tmp_in_ptr,
              tmp_out_grad_ptr,
              static_cast<T>(0),
              tmp_d_kernel_ptr);

    // call gemm: d_x = out_grad * transpose(kernel)
    // (n, out_channels) * (out_channels, in_channels)
    blas.GEMM(CblasNoTrans,
              CblasTrans,
              M,
              K,
              N,
              static_cast<T>(1),
              tmp_out_grad_ptr,
              tmp_kernel_ptr,
              static_cast<T>(0),
              tmp_d_x_ptr);
  }

  // 4. scatter
243 244 245 246 247 248
  phi::funcs::sparse::ScatterV2<T>(dev_ctx,
                                   d_x_features_ptr,
                                   out_index.data<int>(),
                                   unique_value.data<int>(),
                                   x_grad->nnz(),
                                   kernel_size,
249
                                   max_voxel,
250 251 252
                                   in_channels,
                                   2,
                                   x_grad_values_ptr);
253 254
}

255
template <typename T, typename Context>
Z
zhangkaihuo 已提交
256 257 258
void Conv3dCooGradKernel(const Context& dev_ctx,
                         const SparseCooTensor& x,
                         const DenseTensor& kernel,
259
                         const SparseCooTensor& out,
Z
zhangkaihuo 已提交
260
                         const DenseTensor& rulebook,
261
                         const DenseTensor& counter,
Z
zhangkaihuo 已提交
262 263 264 265 266 267
                         const SparseCooTensor& out_grad,
                         const std::vector<int>& paddings,
                         const std::vector<int>& dilations,
                         const std::vector<int>& strides,
                         const int groups,
                         const bool subm,
268
                         const std::string& key,
Z
zhangkaihuo 已提交
269 270
                         SparseCooTensor* x_grad,
                         DenseTensor* kernel_grad) {
Z
zhangkaihuo 已提交
271
  PD_VISIT_BASE_INTEGRAL_TYPES(
Z
zhangkaihuo 已提交
272 273 274 275
      x.non_zero_indices().dtype(), "Conv3dCooGradGPUKernel", ([&] {
        Conv3dCooGradGPUKernel<T, data_t>(dev_ctx,
                                          x,
                                          kernel,
276
                                          out,
Z
zhangkaihuo 已提交
277
                                          rulebook,
278
                                          counter,
Z
zhangkaihuo 已提交
279 280 281 282 283 284
                                          out_grad,
                                          paddings,
                                          dilations,
                                          strides,
                                          groups,
                                          subm,
285
                                          key,
Z
zhangkaihuo 已提交
286 287
                                          x_grad,
                                          kernel_grad);
288 289 290
      }));
}

291 292 293
}  // namespace sparse
}  // namespace phi

Z
zhangkaihuo 已提交
294
PD_REGISTER_KERNEL(conv3d_coo_grad,
295 296
                   GPU,
                   ALL_LAYOUT,
Z
zhangkaihuo 已提交
297
                   phi::sparse::Conv3dCooGradKernel,
298 299 300 301 302
                   float,
                   double,
                   phi::dtype::float16) {
  kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO);
}