empty_kernel.cc 3.9 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/common/complex.h"
18
#include "paddle/phi/core/kernel_registry.h"
19

20
namespace phi {
21

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

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

39
}  // namespace phi
40

41
PD_REGISTER_KERNEL(empty,
42 43
                   CPU,
                   ALL_LAYOUT,
44
                   phi::EmptyKernel,
45 46
                   float,
                   double,
47
                   int8_t,
48 49 50 51 52
                   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
                   float,
                   double,
64
                   int8_t,
65 66 67 68 69
                   uint8_t,
                   int16_t,
                   int,
                   int64_t,
                   bool,
70 71 72
                   phi::dtype::float16,
                   phi::dtype::bfloat16,
                   phi::dtype::complex<float>,
73 74 75
                   phi::dtype::complex<double>) {
  kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
}
76 77

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

95
PD_REGISTER_KERNEL(empty_like,
96 97
                   GPU,
                   ALL_LAYOUT,
98
                   phi::EmptyLikeKernel,
99 100
                   float,
                   double,
101
                   int8_t,
102 103 104 105 106
                   uint8_t,
                   int16_t,
                   int,
                   int64_t,
                   bool,
107 108 109
                   phi::dtype::float16,
                   phi::dtype::bfloat16,
                   phi::dtype::complex<float>,
110 111 112
                   phi::dtype::complex<double>) {
  kernel->InputAt(0).SetBackend(phi::Backend::ALL_BACKEND);
}
113
#endif
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

#ifdef PADDLE_WITH_XPU
PD_REGISTER_KERNEL(empty,
                   XPU,
                   ALL_LAYOUT,
                   phi::EmptyKernel,
                   float,
                   double,
                   int8_t,
                   uint8_t,
                   int16_t,
                   int,
                   int64_t,
                   bool,
                   phi::dtype::float16) {}
#endif