flatten_kernel.cc 3.8 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 16 17 18 19 20
#include "paddle/phi/kernels/flatten_kernel.h"
#include "paddle/phi/backends/all_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/infermeta/unary.h"
#include "paddle/phi/kernels/copy_kernel.h"
#include "paddle/phi/kernels/funcs/common_shape.h"
21

22
namespace phi {
23

24
template <typename T, typename Context>
25 26 27 28 29
void FlattenKernel(const Context& dev_ctx,
                   const DenseTensor& x,
                   int start_axis,
                   int stop_axis,
                   DenseTensor* out) {
30
  dev_ctx.Alloc(out, x.dtype());
31
  auto out_dims = out->dims();
32 33
  phi::Copy(dev_ctx, x, dev_ctx.GetPlace(), false, out);
  out->Resize(out_dims);
34 35 36 37 38
}

// TODO(yuanrisheng): this kernel is for training and xshape is a Intermediate
// Output Tensor,
// is there a more flexible way to deal with this case?
39 40
template <typename T, typename Context>
void FlattenWithXShape(const Context& dev_ctx,
41 42 43 44 45
                       const DenseTensor& x,
                       int start_axis,
                       int stop_axis,
                       DenseTensor* out,
                       DenseTensor* xshape) {
46
  FlattenKernel<T, Context>(dev_ctx, x, start_axis, stop_axis, out);
47 48
}

49
}  // namespace phi
50

51
PD_REGISTER_KERNEL(flatten,
52 53
                   CPU,
                   ALL_LAYOUT,
54
                   phi::FlattenKernel,
55 56 57 58
                   float,
                   double,
                   uint8_t,
                   int8_t,
59
                   int16_t,
60 61
                   int,
                   int64_t) {}
62

63
PD_REGISTER_KERNEL(flatten_with_xshape,
64 65
                   CPU,
                   ALL_LAYOUT,
66
                   phi::FlattenWithXShape,
67 68 69 70
                   float,
                   double,
                   uint8_t,
                   int8_t,
71
                   int16_t,
72 73
                   int,
                   int64_t) {}
74 75

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
76
PD_REGISTER_KERNEL(flatten,
77 78
                   GPU,
                   ALL_LAYOUT,
79
                   phi::FlattenKernel,
80
                   float,
81
                   phi::dtype::float16,
82 83 84
                   double,
                   uint8_t,
                   int8_t,
85
                   int16_t,
86 87
                   int,
                   int64_t) {}
88

89
PD_REGISTER_KERNEL(flatten_with_xshape,
90 91
                   GPU,
                   ALL_LAYOUT,
92
                   phi::FlattenWithXShape,
93
                   float,
94
                   phi::dtype::float16,
95 96 97
                   double,
                   uint8_t,
                   int8_t,
98
                   int16_t,
99 100
                   int,
                   int64_t) {}
101 102 103
#endif

#ifdef PADDLE_WITH_XPU
104
PD_REGISTER_KERNEL(flatten,
105 106
                   XPU,
                   ALL_LAYOUT,
107
                   phi::FlattenKernel,
108
                   float,
109
                   phi::dtype::float16,
110
                   int8_t,
111
                   int16_t,
112 113
                   int,
                   int64_t) {}
114

115
PD_REGISTER_KERNEL(flatten_with_xshape,
116 117
                   XPU,
                   ALL_LAYOUT,
118
                   phi::FlattenWithXShape,
119
                   float,
120
                   phi::dtype::float16,
121
                   int8_t,
122
                   int16_t,
123 124
                   int,
                   int64_t) {}
125
#endif