hl_activation_functions.h 4.7 KB
Newer Older
D
dangqingqing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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. */

#ifndef HL_ACTIVATION_FUNCTIONS_H_
#define HL_ACTIVATION_FUNCTIONS_H_

#include "hl_functions.h"
19
#include "paddle/operators/math/lstm_compute.h"
D
dangqingqing 已提交
20 21 22 23

/**
 * Active functions: sigmoid, relu, tanh and linear.
 */
24 25 26 27 28 29 30 31 32 33 34 35 36
#define FLOAT_ACTIVE_FUNCTION                                   \
  {                                                             \
    hppl::typef::sigmoid, hppl::typef::relu, hppl::typef::tanh, \
        hppl::typef::linear                                     \
  }

#define DOUBLE_ACTIVE_FUNCTION                                  \
  {                                                             \
    hppl::typed::sigmoid, hppl::typed::relu, hppl::typed::tanh, \
        hppl::typed::linear                                     \
  }

#define AVX_ACTIVE_FUNCTION \
D
dangqingqing 已提交
37 38 39 40
  { hppl::sigmoid, hppl::relu, hppl::tanh, hppl::linear }

namespace hppl {

41 42
using activation_mode_t = paddle::operators::math::activation_mode_t;

D
dangqingqing 已提交
43 44 45 46 47 48 49 50 51 52 53
/**
 * Hppl supports sigmoid, relu, tanh, linear active functions
 * for neural networks' forward and backward activation.
 */
template <class T>
class Active {
 public:
  typedef T (*forward)(T);
  typedef T (*backward)(T, T);
};

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
template <typename T>
struct ForwardActType;

template <>
struct ForwardActType<float> {
  using type = Active<float>::forward;
};

template <>
struct ForwardActType<double> {
  using type = Active<double>::forward;
};

template <typename T>
struct BackwardActType;

template <>
struct BackwardActType<float> {
  using type = Active<float>::backward;
};

template <>
struct BackwardActType<double> {
  using type = Active<double>::backward;
};

D
dangqingqing 已提交
80 81
#ifdef __NVCC__
namespace gpu {
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static __device__ Active<float>::forward forward[] = FLOAT_ACTIVE_FUNCTION;
static __device__ Active<float>::backward backward[] = FLOAT_ACTIVE_FUNCTION;

static __device__ Active<double>::forward forward_d[] = DOUBLE_ACTIVE_FUNCTION;
static __device__ Active<double>::backward backward_d[] =
    DOUBLE_ACTIVE_FUNCTION;

template <typename T>
struct ForwardAct {
  __device__ typename ForwardActType<T>::type operator()(
      activation_mode_t type);
};

template <>
struct ForwardAct<float> {
  __device__ ForwardActType<float>::type operator()(activation_mode_t type) {
    return forward[type];
  }
};

template <>
struct ForwardAct<double> {
  __device__ ForwardActType<double>::type operator()(activation_mode_t type) {
    return forward_d[type];
  }
};

template <typename T>
struct BackwardAct {
  __device__ typename BackwardActType<T>::type operator()(
      activation_mode_t type);
};

template <>
struct BackwardAct<float> {
  __device__ BackwardActType<float>::type operator()(activation_mode_t type) {
    return backward[type];
  }
};

template <>
struct BackwardAct<double> {
  __device__ BackwardActType<double>::type operator()(activation_mode_t type) {
    return backward_d[type];
  }
};

D
dangqingqing 已提交
129 130 131
}  // namespace gpu
#else
namespace cpu {
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
static Active<float>::forward forward[] = FLOAT_ACTIVE_FUNCTION;
static Active<float>::backward backward[] = FLOAT_ACTIVE_FUNCTION;

static Active<double>::forward forward_d[] = DOUBLE_ACTIVE_FUNCTION;
static Active<double>::backward backward_d[] = DOUBLE_ACTIVE_FUNCTION;

template <typename T>
struct ForwardAct {
  typename ForwardActType<T>::type operator()(activation_mode_t type);
};

template <>
struct ForwardAct<float> {
  ForwardActType<float>::type operator()(activation_mode_t type) {
    return forward[type];
  }
};

template <>
struct ForwardAct<double> {
  ForwardActType<double>::type operator()(activation_mode_t type) {
    return forward_d[type];
  }
};

template <typename T>
struct BackwardAct {
  typename BackwardActType<T>::type operator()(activation_mode_t type);
};

template <>
struct BackwardAct<float> {
  BackwardActType<float>::type operator()(activation_mode_t type) {
    return backward[type];
  }
};

template <>
struct BackwardAct<double> {
  BackwardActType<double>::type operator()(activation_mode_t type) {
    return backward_d[type];
  }
};

D
dangqingqing 已提交
176 177 178 179
}  // namespace cpu

#ifdef __AVX__
namespace avx {
180 181
static Active<__m256>::forward forward[] = AVX_ACTIVE_FUNCTION;
static Active<__m256>::backward backward[] = AVX_ACTIVE_FUNCTION;
D
dangqingqing 已提交
182 183 184 185 186 187 188
}  // namespace avx
#endif
#endif

}  // namespace hppl

#endif  // HL_ACTIVATION_FUNCTIONS_H_