elementwise_kernel.cu 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

#include "paddle/phi/backends/gpu/gpu_context.h"
16
#ifndef PADDLE_WITH_XPU_KP
Y
YuanRisheng 已提交
17 18
#include "paddle/phi/common/complex.h"
#include "paddle/phi/common/float16.h"
19
#endif
20 21
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/elementwise_kernel_impl.h"
Z
zhangyuqin1998 已提交
22
#include "paddle/phi/kernels/legacy/elementwise_kernel.h"
23

Y
YuanRisheng 已提交
24 25
namespace phi {

26 27 28 29 30 31 32 33
template <typename T, typename Context>
void MaximumKernel(const Context& dev_ctx,
                   const DenseTensor& x,
                   const DenseTensor& y,
                   DenseTensor* out) {
  int axis = -1;
  MaximumRawKernel<T>(dev_ctx, x, y, axis, out);
}
34

35 36 37 38 39 40 41 42
template <typename T, typename Context>
void MinimumKernel(const Context& dev_ctx,
                   const DenseTensor& x,
                   const DenseTensor& y,
                   DenseTensor* out) {
  int axis = -1;
  MinimumRawKernel<T>(dev_ctx, x, y, axis, out);
}
Z
zhangyuqin1998 已提交
43 44 45 46 47 48 49 50 51 52

template <typename T, typename Context>
void RemainderKernel(const Context& dev_ctx,
                     const DenseTensor& x,
                     const DenseTensor& y,
                     DenseTensor* out) {
  int axis = -1;
  RemainderRawKernel<T>(dev_ctx, x, y, axis, out);
}

53 54 55 56 57 58 59 60
template <typename T, typename Context>
void FloorDivideKernel(const Context& dev_ctx,
                       const DenseTensor& x,
                       const DenseTensor& y,
                       DenseTensor* out) {
  int axis = -1;
  FloorDivideRawKernel<T>(dev_ctx, x, y, axis, out);
}
61
// Create the definition of Heaviside
62 63 64 65 66 67 68 69 70 71 72 73 74
template <typename T, typename Context>
void HeavisideKernel(const Context& dev_ctx,
                     const DenseTensor& x,
                     const DenseTensor& y,
                     DenseTensor* out) {
  std::vector<const DenseTensor*> inputs;
  inputs.reserve(2);
  std::vector<DenseTensor*> outputs;
  outputs.reserve(1);
  inputs.emplace_back(&x);
  inputs.emplace_back(&y);
  outputs.emplace_back(out);
  dev_ctx.template Alloc<T>(out);
75 76
  funcs::BroadcastKernel<T>(
      dev_ctx, inputs, &outputs, funcs::ElementwiseHeavisideFunctor<T>());
77 78
}

79 80 81 82 83 84 85 86
template <typename T, typename Context>
void ElementwisePowKernel(const Context& dev_ctx,
                          const DenseTensor& x,
                          const DenseTensor& y,
                          DenseTensor* out) {
  int axis = -1;
  ElementwisePowRawKernel<T>(dev_ctx, x, y, axis, out);
}
Y
YuanRisheng 已提交
87 88 89

}  // namespace phi

Z
zhangyuqin1998 已提交
90
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
91

Z
zhangyuqin1998 已提交
92
PD_REGISTER_KERNEL(maximum,
93 94
                   KPS,
                   ALL_LAYOUT,
Z
zhangyuqin1998 已提交
95
                   phi::MaximumKernel,
96 97 98
                   float,
                   double,
                   int,
Z
zhangyuqin1998 已提交
99 100 101 102
                   int64_t,
                   phi::dtype::float16,
                   phi::dtype::bfloat16) {}
PD_REGISTER_KERNEL(minimum,
103 104
                   KPS,
                   ALL_LAYOUT,
Z
zhangyuqin1998 已提交
105
                   phi::MinimumKernel,
106 107 108
                   float,
                   double,
                   int,
Z
zhangyuqin1998 已提交
109 110 111 112 113
                   int64_t,
                   phi::dtype::float16,
                   phi::dtype::bfloat16) {}
PD_REGISTER_KERNEL(remainder,
                   GPU,
114
                   ALL_LAYOUT,
Z
zhangyuqin1998 已提交
115
                   phi::RemainderKernel,
116 117 118 119
                   float,
                   double,
                   int,
                   int64_t,
Z
zhangyuqin1998 已提交
120 121 122 123
                   phi::dtype::float16) {}
PD_REGISTER_KERNEL(
    floor_divide, KPS, ALL_LAYOUT, phi::FloorDivideKernel, int, int64_t) {}
PD_REGISTER_KERNEL(elementwise_pow,
124
                   KPS,
125
                   ALL_LAYOUT,
Z
zhangyuqin1998 已提交
126
                   phi::ElementwisePowKernel,
127 128 129 130
                   float,
                   double,
                   int,
                   int64_t,
Z
zhangyuqin1998 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
                   phi::dtype::float16,
                   phi::dtype::bfloat16) {}

#endif

#ifdef PADDLE_WITH_XPU_KP
PD_REGISTER_KERNEL(maximum, KPS, ALL_LAYOUT, phi::MaximumKernel, float) {}
PD_REGISTER_KERNEL(minimum, KPS, ALL_LAYOUT, phi::MinimumKernel, float) {}
PD_REGISTER_KERNEL(floor_divide, KPS, ALL_LAYOUT, phi::FloorDivideKernel, int) {
}
PD_REGISTER_KERNEL(
    elementwise_pow, KPS, ALL_LAYOUT, phi::ElementwisePowKernel, float) {}

#else
using float16 = phi::dtype::float16;
using bfloat16 = phi::dtype::bfloat16;
using complex64 = ::phi::dtype::complex<float>;
using complex128 = ::phi::dtype::complex<double>;

PD_REGISTER_KERNEL(fmax,
151
                   KPS,
152
                   ALL_LAYOUT,
Z
zhangyuqin1998 已提交
153
                   phi::FMaxKernel,
154 155 156
                   float,
                   double,
                   int,
157
                   float16,
158
                   bfloat16,
159
                   int64_t) {}
Z
zhangyuqin1998 已提交
160 161

PD_REGISTER_KERNEL(fmin,
162 163
                   KPS,
                   ALL_LAYOUT,
Z
zhangyuqin1998 已提交
164
                   phi::FMinKernel,
165 166 167
                   float,
                   double,
                   int,
168
                   float16,
169
                   bfloat16,
170
                   int64_t) {}
Z
zhangyuqin1998 已提交
171 172

PD_REGISTER_KERNEL(heaviside,
173
                   KPS,
174
                   ALL_LAYOUT,
Z
zhangyuqin1998 已提交
175
                   phi::HeavisideKernel,
176 177 178
                   float,
                   double,
                   int,
179
                   float16,
180
                   int64_t) {}
181
#endif