diagonal_grad_kernel.cu 7.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.

15 16
#include "paddle/phi/kernels/diagonal_grad_kernel.h"

17 18
#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/platform/device/gpu/gpu_primitives.h"
19 20
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/diagonal.h"
21

22
namespace phi {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

using paddle::platform::PADDLE_CUDA_NUM_THREADS;

template <typename T, typename Context>
void DiagonalGradKernel(const Context& dev_ctx,
                        const DenseTensor& x,
                        const DenseTensor& out_grad,
                        int offset,
                        int axis1,
                        int axis2,
                        DenseTensor* in_grad) {
  const auto* dout = &out_grad;
  const auto* dout_data = dout->data<T>();
  auto dout_dim = dout->dims().Get();
  auto dout_dim_size = dout->dims().size();

39
  std::vector<int64_t> res_dout = vectorize(phi::stride(dout->dims()));
40 41 42 43 44 45 46 47 48 49
  DenseTensor dout_stride_tensor;
  paddle::framework::TensorFromVector<int64_t>(
      res_dout, dev_ctx, &dout_stride_tensor);
  int64_t* dout_stride = dout_stride_tensor.data<int64_t>();

  auto* dx = in_grad;
  auto* dx_data = dev_ctx.template Alloc<T>(dx);
  auto dx_dim = dx->dims().Get();
  auto dx_dim_size = dx->dims().size();

50
  std::vector<int64_t> res_dx = vectorize(phi::stride(dx->dims()));
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 160
  DenseTensor dx_stride_tensor;
  paddle::framework::TensorFromVector<int64_t>(
      res_dx, dev_ctx, &dx_stride_tensor);
  int64_t* dx_stride = dx_stride_tensor.data<int64_t>();

  const int64_t offset_ = offset;
  int64_t axis1_ = axis1 < 0 ? dx_dim_size + axis1 : axis1;
  int64_t axis2_ = axis2 < 0 ? dx_dim_size + axis2 : axis2;

  int64_t numel = dx->numel();

  int threads = PADDLE_CUDA_NUM_THREADS;
  int blocks = (numel + threads - 1) / threads;

  switch (dx_dim_size) {
    case 2:
      funcs::DiagonalCuda<T, 2, 1><<<blocks, threads>>>(dout_data,
                                                        dx_data,
                                                        offset_,
                                                        axis1_,
                                                        axis2_,
                                                        dx_stride,
                                                        dout_stride,
                                                        numel,
                                                        true);
      break;
    case 3:
      funcs::DiagonalCuda<T, 3, 2><<<blocks, threads>>>(dout_data,
                                                        dx_data,
                                                        offset_,
                                                        axis1_,
                                                        axis2_,
                                                        dx_stride,
                                                        dout_stride,
                                                        numel,
                                                        true);
      break;
    case 4:
      funcs::DiagonalCuda<T, 4, 3><<<blocks, threads>>>(dout_data,
                                                        dx_data,
                                                        offset_,
                                                        axis1_,
                                                        axis2_,
                                                        dx_stride,
                                                        dout_stride,
                                                        numel,
                                                        true);
      break;
    case 5:
      funcs::DiagonalCuda<T, 5, 4><<<blocks, threads>>>(dout_data,
                                                        dx_data,
                                                        offset_,
                                                        axis1_,
                                                        axis2_,
                                                        dx_stride,
                                                        dout_stride,
                                                        numel,
                                                        true);
      break;
    case 6:
      funcs::DiagonalCuda<T, 6, 5><<<blocks, threads>>>(dout_data,
                                                        dx_data,
                                                        offset_,
                                                        axis1_,
                                                        axis2_,
                                                        dx_stride,
                                                        dout_stride,
                                                        numel,
                                                        true);
      break;
    case 7:
      funcs::DiagonalCuda<T, 7, 6><<<blocks, threads>>>(dout_data,
                                                        dx_data,
                                                        offset_,
                                                        axis1_,
                                                        axis2_,
                                                        dx_stride,
                                                        dout_stride,
                                                        numel,
                                                        true);
      break;
    case 8:
      funcs::DiagonalCuda<T, 8, 7><<<blocks, threads>>>(dout_data,
                                                        dx_data,
                                                        offset_,
                                                        axis1_,
                                                        axis2_,
                                                        dx_stride,
                                                        dout_stride,
                                                        numel,
                                                        true);
      break;
    case 9:
      funcs::DiagonalCuda<T, 9, 8><<<blocks, threads>>>(dout_data,
                                                        dx_data,
                                                        offset_,
                                                        axis1_,
                                                        axis2_,
                                                        dx_stride,
                                                        dout_stride,
                                                        numel,
                                                        true);
      break;
    default:
      PADDLE_THROW(errors::InvalidArgument(
          "The rank of output(input@Grad) should be less than 10, but "
          "received %d.",
          dx_dim_size));
  }
}
161
}  // namespace phi
162
PD_REGISTER_KERNEL(diagonal_grad,
163 164
                   GPU,
                   ALL_LAYOUT,
165
                   phi::DiagonalGradKernel,
166 167 168
                   float,
                   double,
                   int,
169 170 171 172 173 174
                   int64_t,
                   bool,
                   phi::dtype::float16,
                   phi::dtype::bfloat16,
                   phi::dtype::complex<float>,
                   phi::dtype::complex<double>) {}