activation_functions.h 8.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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
#include <math.h>
17

18
#include <stdexcept>
A
Abhinav Arora 已提交
19
#include <string>
20

P
PuQing 已提交
21
#include "paddle/phi/backends/cpu/cpu_info.h"
22
#include "paddle/phi/core/hostdevice.h"
23
#include "paddle/phi/core/macros.h"
F
Feiyu Chan 已提交
24 25
namespace phi {
namespace funcs {
26 27 28 29 30 31
namespace detail {

#define SIGMOID_THRESHOLD_MIN -40.0
#define SIGMOID_THRESHOLD_MAX 13.0
#define EXP_MAX_INPUT 40.0

32 33
enum ActivationType {
  kSigmoid,
34
  KSigmoidV2,
35 36
  kReLU,
  kTanh,
37
  kTanhV2,
38 39 40
  kIdentity,
};

D
dangqingqing 已提交
41
inline ActivationType GetActivationType(const std::string &type) {
42 43
  if (type == "sigmoid") {
    return ActivationType::kSigmoid;
44 45
  } else if (type == "sigmoid_v2") {
    return ActivationType::KSigmoidV2;
46 47 48 49
  } else if (type == "relu") {
    return ActivationType::kReLU;
  } else if (type == "tanh") {
    return ActivationType::kTanh;
50 51
  } else if (type == "tanh_v2") {
    return ActivationType::kTanhV2;
D
dangqingqing 已提交
52
  } else if (type == "identity" || type == "") {
53 54
    return ActivationType::kIdentity;
  }
55
  throw std::invalid_argument("The input type is not supported");
56 57
}

58 59 60
namespace forward {

template <typename T>
61
DEVICE T Identity(const T a) {
62 63 64 65
  return a;
}

template <typename T>
66
DEVICE T Relu(const T a) {
67 68 69 70
  return a > static_cast<T>(0.0) ? a : static_cast<T>(0.0);
}

template <typename T>
71
DEVICE T Sigmoid(const T a) {
72 73 74 75 76 77
  const T min = SIGMOID_THRESHOLD_MIN;
  const T max = SIGMOID_THRESHOLD_MAX;
  T tmp = (a < min) ? min : ((a > max) ? max : a);
  return static_cast<T>(1.0) / (static_cast<T>(1.0) + exp(-tmp));
}

78 79 80 81 82 83 84 85
/*
 * Don't limit input in a threshold range.
 */
template <typename T>
DEVICE T SigmoidV2(const T a) {
  return static_cast<T>(1.0) / (static_cast<T>(1.0) + exp(-a));
}

86
template <typename T>
87
DEVICE T Tanh(const T a) {
88 89 90 91 92
  T tmp = -2.0 * a;
  tmp = (tmp > EXP_MAX_INPUT) ? EXP_MAX_INPUT : tmp;
  return (2.0 / (1.0 + exp(tmp))) - 1.0;
}

93 94 95 96 97 98 99 100 101
/*
 * Don't limit input in a threshold range.
 */
template <typename T>
DEVICE T TanhV2(const T a) {
  T tmp = -2.0 * a;
  return (2.0 / (1.0 + exp(tmp))) - 1.0;
}

102 103 104 105 106
}  // namespace forward

namespace backward {

template <typename T>
107
DEVICE T Identity(const T a, const T b UNUSED) {
108 109 110 111
  return a;
}

template <typename T>
112
DEVICE T Relu(const T a, const T b) {
113 114 115 116
  return a * (b > 0.0 ? 1.0 : 0.0);
}

template <typename T>
117
DEVICE T Sigmoid(const T a, const T b) {
118 119 120 121
  return a * b * (1.0 - b);
}

template <typename T>
122
DEVICE T Tanh(const T a, const T b) {
123 124 125 126 127 128 129 130 131 132 133
  return a * (1.0 - b * b);
}

}  // namespace backward

template <typename T>
struct Active {
  typedef T (*Act)(T);
  typedef T (*ActGrad)(T, T);
};

134 135
#ifdef PADDLE_WITH_CUDA

F
Feiyu Chan 已提交
136 137 138 139 140 141
static DEVICE Active<float>::Act kActFloat[] = {&forward::Sigmoid<float>,
                                                &forward::SigmoidV2<float>,
                                                &forward::Relu<float>,
                                                &forward::Tanh<float>,
                                                &forward::TanhV2<float>,
                                                &forward::Identity<float>};
142 143

static DEVICE Active<float>::ActGrad kActGradFloat[] = {
F
Feiyu Chan 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156
    &backward::Sigmoid<float>,
    &backward::Sigmoid<float>,
    &backward::Relu<float>,
    &backward::Tanh<float>,
    &backward::Tanh<float>,
    &backward::Identity<float>};

static DEVICE Active<double>::Act kActDouble[] = {&forward::Sigmoid<double>,
                                                  &forward::SigmoidV2<double>,
                                                  &forward::Relu<double>,
                                                  &forward::Tanh<double>,
                                                  &forward::TanhV2<double>,
                                                  &forward::Identity<double>};
157 158

static DEVICE Active<double>::ActGrad kActGradDouble[] = {
F
Feiyu Chan 已提交
159 160 161 162 163 164
    &backward::Sigmoid<double>,
    &backward::Sigmoid<double>,
    &backward::Relu<double>,
    &backward::Tanh<double>,
    &backward::Tanh<double>,
    &backward::Identity<double>};
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

namespace forward {
inline DEVICE float activation(float a, int index) {
  return kActFloat[index](a);
}

inline DEVICE double activation(double a, int index) {
  return kActDouble[index](a);
}

}  // namespace forward

namespace backward {
inline DEVICE float activation(float a, float b, int index) {
  return kActGradFloat[index](a, b);
}

inline DEVICE double activation(double a, double b, int index) {
  return kActGradDouble[index](a, b);
}
}  // namespace backward

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
#else  // PADDLE_WITH_CUDA

// Note(qili93): The above implementing not work in HIP
// It will throw compile error when calling detail::forward::lstm<T>()
// Which used ActivationType in lstm_kernel.h, compile error is:
// lstm_gpu_kernel.h:33:17: error: unsupported indirect call to function
// <unknown>

// To-do(qili93): fix this after HIP issue fixed:
// https://github.com/ROCm-Developer-Tools/HIP/issues/2186

namespace forward {
inline DEVICE float activation(float a, int index) {
  switch (index) {
    case 0:
      return Sigmoid<float>(a);
    case 1:
      return SigmoidV2<float>(a);
    case 2:
      return Relu<float>(a);
    case 3:
      return Tanh<float>(a);
    case 4:
      return TanhV2<float>(a);
    case 5:
      return Identity<float>(a);
    default:
      return 0.0f;
  }
}

inline DEVICE double activation(double a, int index) {
  switch (index) {
    case 0:
      return Sigmoid<double>(a);
    case 1:
      return SigmoidV2<double>(a);
    case 2:
      return Relu<double>(a);
    case 3:
      return Tanh<double>(a);
    case 4:
      return TanhV2<double>(a);
    case 5:
      return Identity<double>(a);
    default:
      return 0.0f;
  }
}
}  // namespace forward

namespace backward {
inline DEVICE float activation(float a, float b, int index) {
  switch (index) {
    case 0:
      return Sigmoid<float>(a, b);
    case 1:
      return Sigmoid<float>(a, b);
    case 2:
      return Relu<float>(a, b);
    case 3:
      return Tanh<float>(a, b);
    case 4:
      return Tanh<float>(a, b);
    case 5:
      return Identity<float>(a, b);
    default:
      return 0.0f;
  }
}

inline DEVICE double activation(double a, double b, int index) {
  switch (index) {
    case 0:
      return Sigmoid<double>(a, b);
    case 1:
      return Sigmoid<double>(a, b);
    case 2:
      return Relu<double>(a, b);
    case 3:
      return Tanh<double>(a, b);
    case 4:
      return Tanh<double>(a, b);
    case 5:
      return Identity<double>(a, b);
    default:
      return 0.0f;
  }
}
}  // namespace backward

#endif  // PADDLE_WITH_CUDA

280 281 282
#ifdef __AVX__
namespace forward {
namespace avx {
283 284
__m256 Relu(const __m256 a);
__m256 Sigmoid(const __m256 a);
285
__m256 SigmoidV2(const __m256 a);
286
__m256 Tanh(const __m256 a);
287
__m256 TanhV2(const __m256 a);
288
__m256 Identity(const __m256 a);
289 290 291 292 293
}  // namespace avx
}  // namespace forward

namespace backward {
namespace avx {
294 295 296 297
__m256 Relu(const __m256 a, const __m256 b);
__m256 Sigmoid(const __m256 a, const __m256 b);
__m256 Tanh(const __m256 a, const __m256 b);
__m256 Identity(const __m256 a, const __m256 b);
298 299 300
}  // namespace avx
}  // namespace backward

F
Feiyu Chan 已提交
301 302 303 304 305 306
static Active<__m256>::Act kActAvx[] = {&forward::avx::Sigmoid,
                                        &forward::avx::SigmoidV2,
                                        &forward::avx::Relu,
                                        &forward::avx::Tanh,
                                        &forward::avx::TanhV2,
                                        &forward::avx::Identity};
307

F
Feiyu Chan 已提交
308 309 310 311 312 313
static Active<__m256>::ActGrad kActGradAvx[] = {&backward::avx::Sigmoid,
                                                &backward::avx::Sigmoid,
                                                &backward::avx::Relu,
                                                &backward::avx::Tanh,
                                                &backward::avx::Tanh,
                                                &backward::avx::Identity};
314 315 316 317 318 319 320 321 322 323 324 325 326 327

namespace forward {
inline __m256 activation(__m256 a, int index) { return kActAvx[index](a); }
}  // namespace forward

namespace backward {
inline __m256 activation(__m256 a, __m256 b, int index) {
  return kActGradAvx[index](a, b);
}
}  // namespace backward

#endif

}  // namespace detail
F
Feiyu Chan 已提交
328 329
}  // namespace funcs
}  // namespace phi