reshape_grad_kernel.cc 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//   Copyright (c) 2021 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/reshape_grad_kernel.h"
16

17 18
#include "paddle/phi/backends/all_context.h"
#include "paddle/phi/core/kernel_registry.h"
19
#include "paddle/phi/core/tensor_utils.h"
20

21
namespace phi {
22 23 24 25 26 27

template <typename Context>
void ReshapeGradKernel(const Context& dev_ctx,
                       const DenseTensor& out_grad,
                       DenseTensor* x_grad) {
  auto x_dims = x_grad->dims();
28
  phi::Copy(dev_ctx, out_grad, dev_ctx.GetPlace(), false, x_grad);
29 30 31 32 33
  x_grad->Resize(x_dims);
}

template <typename Context>
void ReshapeDoubleGradKernel(const Context& dev_ctx,
34
                             const DenseTensor& out_grad,
35 36 37 38 39
                             const DenseTensor& x_grad_grad,
                             DenseTensor* out_grad_grad) {
  ReshapeGradKernel(dev_ctx, x_grad_grad, out_grad_grad);
}

40
}  // namespace phi
41

42
PD_REGISTER_GENERAL_KERNEL(reshape_grad,
43 44
                           CPU,
                           ALL_LAYOUT,
45
                           phi::ReshapeGradKernel<phi::CPUContext>,
46
                           ALL_DTYPE) {}
47
PD_REGISTER_GENERAL_KERNEL(reshape_double_grad,
48 49
                           CPU,
                           ALL_LAYOUT,
50
                           phi::ReshapeDoubleGradKernel<phi::CPUContext>,
51 52 53
                           ALL_DTYPE) {}

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
54
PD_REGISTER_GENERAL_KERNEL(reshape_grad,
55 56
                           GPU,
                           ALL_LAYOUT,
57
                           phi::ReshapeGradKernel<phi::GPUContext>,
58
                           ALL_DTYPE) {}
59
PD_REGISTER_GENERAL_KERNEL(reshape_double_grad,
60 61
                           GPU,
                           ALL_LAYOUT,
62
                           phi::ReshapeDoubleGradKernel<phi::GPUContext>,
63 64 65 66
                           ALL_DTYPE) {}
#endif

#ifdef PADDLE_WITH_XPU
67
PD_REGISTER_GENERAL_KERNEL(reshape_grad,
68 69
                           XPU,
                           ALL_LAYOUT,
70
                           phi::ReshapeGradKernel<phi::XPUContext>,
71
                           ALL_DTYPE) {}
72
PD_REGISTER_GENERAL_KERNEL(reshape_double_grad,
73 74
                           XPU,
                           ALL_LAYOUT,
75
                           phi::ReshapeDoubleGradKernel<phi::XPUContext>,
76 77
                           ALL_DTYPE) {}
#endif