empty_kernel.cc 3.4 KB
Newer Older
1 2
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

3 4 5
 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
6

7
     http://www.apache.org/licenses/LICENSE-2.0
8

9 10 11 12 13
 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. */
14
#include "paddle/phi/kernels/empty_kernel.h"
15

16 17
#include "paddle/phi/backends/all_context.h"
#include "paddle/phi/core/kernel_registry.h"
18

19
#include "paddle/phi/common/complex.h"
20

21
namespace phi {
22

23 24
template <typename T, typename Context>
void EmptyKernel(const Context& dev_ctx,
25
                 const ScalarArray& shape,
26
                 DataType dtype,
27
                 DenseTensor* out) {
28 29
  out->Resize(phi::make_ddim(shape.GetData()));
  dev_ctx.template Alloc<T>(out);
30 31
}

32
template <typename T, typename Context>
33 34 35 36
void EmptyLikeKernel(const Context& dev_ctx,
                     const DenseTensor& x,
                     DataType dtype,
                     DenseTensor* out) {
37
  dev_ctx.template Alloc<T>(out);
38 39
}

40
}  // namespace phi
41

42
PD_REGISTER_KERNEL(empty,
43 44
                   CPU,
                   ALL_LAYOUT,
45
                   phi::EmptyKernel,
46 47 48 49 50 51 52
                   float,
                   double,
                   uint8_t,
                   int16_t,
                   int,
                   int64_t,
                   bool,
53 54 55 56
                   phi::dtype::float16,
                   phi::dtype::bfloat16,
                   phi::dtype::complex<float>,
                   phi::dtype::complex<double>) {}
57

58
PD_REGISTER_KERNEL(empty_like,
59 60
                   CPU,
                   ALL_LAYOUT,
61
                   phi::EmptyLikeKernel,
62 63 64 65 66 67 68
                   float,
                   double,
                   uint8_t,
                   int16_t,
                   int,
                   int64_t,
                   bool,
69 70 71
                   phi::dtype::float16,
                   phi::dtype::bfloat16,
                   phi::dtype::complex<float>,
72 73 74
                   phi::dtype::complex<double>) {
  kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
}
75 76

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
77
PD_REGISTER_KERNEL(empty,
78 79
                   GPU,
                   ALL_LAYOUT,
80
                   phi::EmptyKernel,
81 82 83 84 85 86 87
                   float,
                   double,
                   uint8_t,
                   int16_t,
                   int,
                   int64_t,
                   bool,
88 89 90
                   phi::dtype::float16,
                   phi::dtype::complex<float>,
                   phi::dtype::complex<double>) {}
91

92
PD_REGISTER_KERNEL(empty_like,
93 94
                   GPU,
                   ALL_LAYOUT,
95
                   phi::EmptyLikeKernel,
96 97 98 99 100 101 102
                   float,
                   double,
                   uint8_t,
                   int16_t,
                   int,
                   int64_t,
                   bool,
103 104 105
                   phi::dtype::float16,
                   phi::dtype::bfloat16,
                   phi::dtype::complex<float>,
106 107 108
                   phi::dtype::complex<double>) {
  kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
}
109
#endif