activation_kernel.h 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#pragma once

Y
YuanRisheng 已提交
17
#include "paddle/phi/common/scalar.h"
18 19 20 21 22 23 24 25 26 27
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/infermeta/unary.h"

namespace phi {

#define DECLARE_ACTIVATION_KERNEL(name)   \
  template <typename T, typename Context> \
  void name##Kernel(                      \
      const Context& dev_ctx, const DenseTensor& x, DenseTensor* out);

Y
YuanRisheng 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#define DECLARE_ACTIVATION_KERNEL_WITH_ONE_ATTRS(name, attr) \
  template <typename T, typename Context>                    \
  void name##Kernel(const Context& dev_ctx,                  \
                    const DenseTensor& x,                    \
                    float attr,                              \
                    DenseTensor* out);

#define DECLARE_ACTIVATION_KERNEL_WITH_TWO_ATTRS(name, attr1, attr2) \
  template <typename T, typename Context>                            \
  void name##Kernel(const Context& dev_ctx,                          \
                    const DenseTensor& x,                            \
                    float attr1,                                     \
                    float attr2,                                     \
                    DenseTensor* out);

43
DECLARE_ACTIVATION_KERNEL(Sin)
44 45 46 47
DECLARE_ACTIVATION_KERNEL(Cos)
DECLARE_ACTIVATION_KERNEL(Tan)
DECLARE_ACTIVATION_KERNEL(Asin)
DECLARE_ACTIVATION_KERNEL(Atan)
48
DECLARE_ACTIVATION_KERNEL(Acos)
49 50 51 52 53 54
DECLARE_ACTIVATION_KERNEL(Sinh)
DECLARE_ACTIVATION_KERNEL(Cosh)
DECLARE_ACTIVATION_KERNEL(Asinh)
DECLARE_ACTIVATION_KERNEL(Acosh)
DECLARE_ACTIVATION_KERNEL(Atanh)
DECLARE_ACTIVATION_KERNEL(Relu)
55
DECLARE_ACTIVATION_KERNEL(Tanh)
56 57
DECLARE_ACTIVATION_KERNEL(TanhShrink)
DECLARE_ACTIVATION_KERNEL(Silu)
58 59 60 61 62 63
DECLARE_ACTIVATION_KERNEL(Exp)
DECLARE_ACTIVATION_KERNEL(Expm1)
DECLARE_ACTIVATION_KERNEL(Reciprocal)
DECLARE_ACTIVATION_KERNEL(Square)
DECLARE_ACTIVATION_KERNEL(Sqrt)
DECLARE_ACTIVATION_KERNEL(Rsqrt)
Y
YuanRisheng 已提交
64 65
DECLARE_ACTIVATION_KERNEL(Sigmoid)
DECLARE_ACTIVATION_KERNEL(LogSigmoid)
66 67 68 69
DECLARE_ACTIVATION_KERNEL(Log)
DECLARE_ACTIVATION_KERNEL(Log2)
DECLARE_ACTIVATION_KERNEL(Log10)
DECLARE_ACTIVATION_KERNEL(Log1p)
Y
YuanRisheng 已提交
70 71 72
DECLARE_ACTIVATION_KERNEL(Round)
DECLARE_ACTIVATION_KERNEL(Floor)
DECLARE_ACTIVATION_KERNEL(Ceil)
73
DECLARE_ACTIVATION_KERNEL(Negative)
Y
YuanRisheng 已提交
74 75 76

DECLARE_ACTIVATION_KERNEL_WITH_ONE_ATTRS(LeakyRelu, alpha)
DECLARE_ACTIVATION_KERNEL_WITH_ONE_ATTRS(ThresholdedRelu, threshold)
77
DECLARE_ACTIVATION_KERNEL_WITH_ONE_ATTRS(Relu6, threshold)
Y
YuanRisheng 已提交
78
DECLARE_ACTIVATION_KERNEL_WITH_ONE_ATTRS(SoftShrink, lambda)
79
DECLARE_ACTIVATION_KERNEL_WITH_ONE_ATTRS(Mish, threshold)
Y
YuanRisheng 已提交
80
DECLARE_ACTIVATION_KERNEL_WITH_ONE_ATTRS(HardShrink, threshold)
81
DECLARE_ACTIVATION_KERNEL_WITH_ONE_ATTRS(SoftShrink, lambda)
Y
YuanRisheng 已提交
82
DECLARE_ACTIVATION_KERNEL_WITH_ONE_ATTRS(Elu, alpha)
Y
YuanRisheng 已提交
83
DECLARE_ACTIVATION_KERNEL_WITH_ONE_ATTRS(Swish, beta)
84
DECLARE_ACTIVATION_KERNEL_WITH_ONE_ATTRS(Celu, alpha)
85
DECLARE_ACTIVATION_KERNEL_WITH_ONE_ATTRS(Logit, eps)
86

Y
YuanRisheng 已提交
87
DECLARE_ACTIVATION_KERNEL_WITH_TWO_ATTRS(BRelu, t_min, t_max)
88 89
DECLARE_ACTIVATION_KERNEL_WITH_TWO_ATTRS(STanh, scale_a, scale_b)
DECLARE_ACTIVATION_KERNEL_WITH_TWO_ATTRS(Softplus, beta, threshold)
90
DECLARE_ACTIVATION_KERNEL_WITH_TWO_ATTRS(HardSigmoid, slope, offset)
91

Y
YuanRisheng 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105
template <typename T, typename Context>
void HardSwishKernel(const Context& dev_ctx,
                     const DenseTensor& x,
                     float threshold,
                     float scale,
                     float offset,
                     DenseTensor* out);

template <typename T, typename Context>
void PowKernel(const Context& dev_ctx,
               const DenseTensor& x,
               const Scalar& factor,
               DenseTensor* out);

X
xiongkun 已提交
106 107 108 109 110 111 112 113 114 115 116
template <typename T, typename Context>
DenseTensor Pow(const Context& dev_ctx,
                const DenseTensor& x,
                const Scalar& factor) {
  DenseTensor out;
  MetaTensor meta_out(out);
  UnchangedInferMeta(x, &meta_out);
  PowKernel<T, Context>(dev_ctx, x, factor, &out);
  return out;
}

117
}  // namespace phi