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

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
#include "paddle/platform/enforce.h"
D
dangqingqing 已提交
18
#include "paddle/platform/hostdevice.h"
19 20 21 22 23 24 25 26 27 28 29 30 31 32

#ifdef __AVX__
#include <immintrin.h>
#endif

namespace paddle {
namespace operators {
namespace math {
namespace detail {

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

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

D
dangqingqing 已提交
40
inline ActivationType GetActivationType(const std::string &type) {
41 42 43 44 45 46
  if (type == "sigmoid") {
    return ActivationType::kSigmoid;
  } else if (type == "relu") {
    return ActivationType::kReLU;
  } else if (type == "tanh") {
    return ActivationType::kTanh;
D
dangqingqing 已提交
47
  } else if (type == "identity" || type == "") {
48 49 50 51 52
    return ActivationType::kIdentity;
  }
  PADDLE_THROW("Not support type %s.", type);
}

53 54 55
namespace forward {

template <typename T>
56
DEVICE T Identity(const T a) {
57 58 59 60
  return a;
}

template <typename T>
61
DEVICE T Relu(const T a) {
62 63 64 65
  return a > static_cast<T>(0.0) ? a : static_cast<T>(0.0);
}

template <typename T>
66
DEVICE T Sigmoid(const T a) {
67 68 69 70 71 72 73
  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));
}

template <typename T>
74
DEVICE T Tanh(const T a) {
75 76 77 78 79 80 81 82 83 84
  T tmp = -2.0 * a;
  tmp = (tmp > EXP_MAX_INPUT) ? EXP_MAX_INPUT : tmp;
  return (2.0 / (1.0 + exp(tmp))) - 1.0;
}

}  // namespace forward

namespace backward {

template <typename T>
85
DEVICE T Identity(const T a, const T b) {
86 87 88 89
  return a;
}

template <typename T>
90
DEVICE T Relu(const T a, const T b) {
91 92 93 94
  return a * (b > 0.0 ? 1.0 : 0.0);
}

template <typename T>
95
DEVICE T Sigmoid(const T a, const T b) {
96 97 98 99
  return a * b * (1.0 - b);
}

template <typename T>
100
DEVICE T Tanh(const T a, const T b) {
101 102 103 104 105 106 107 108 109 110 111 112
  return a * (1.0 - b * b);
}

}  // namespace backward

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

static DEVICE Active<float>::Act kActFloat[] = {
113 114
    &forward::Sigmoid<float>, &forward::Relu<float>, &forward::Tanh<float>,
    &forward::Identity<float>};
115 116

static DEVICE Active<float>::ActGrad kActGradFloat[] = {
117 118
    &backward::Sigmoid<float>, &backward::Relu<float>, &backward::Tanh<float>,
    &backward::Identity<float>};
119 120

static DEVICE Active<double>::Act kActDouble[] = {
121 122
    &forward::Sigmoid<double>, &forward::Relu<double>, &forward::Tanh<double>,
    &forward::Identity<double>};
123 124

static DEVICE Active<double>::ActGrad kActGradDouble[] = {
125 126
    &backward::Sigmoid<double>, &backward::Relu<double>,
    &backward::Tanh<double>, &backward::Identity<double>};
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

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

#ifdef __AVX__
namespace forward {
namespace avx {
152 153 154 155
__m256 Relu(const __m256 a);
__m256 Sigmoid(const __m256 a);
__m256 Tanh(const __m256 a);
__m256 Identity(const __m256 a);
156 157 158 159 160
}  // namespace avx
}  // namespace forward

namespace backward {
namespace avx {
161 162 163 164
__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);
165 166 167 168
}  // namespace avx
}  // namespace backward

static Active<__m256>::Act kActAvx[] = {
169 170
    &forward::avx::Sigmoid, &forward::avx::Relu, &forward::avx::Tanh,
    &forward::avx::Identity};
171 172

static Active<__m256>::ActGrad kActGradAvx[] = {
173 174
    &backward::avx::Sigmoid, &backward::avx::Relu, &backward::avx::Tanh,
    &backward::avx::Identity};
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

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
}  // namespace math
}  // namespace operators
}  // namespace paddle