1 #ifndef _OPERATORS_SMV_KERNELS_ACTIVATION_FUNCTIONS_SIMD_H_
2 #define _OPERATORS_SMV_KERNELS_ACTIVATION_FUNCTIONS_SIMD_H_
8 #include "smaug/operators/smv/kernels/params.h"
23 static inline void relu_vec(
v8fp_t* inputs,
v8fp_t* results,
int inputs_size) {
25 for (
int i = 0; i < inputs_size /
VECTOR_SIZE; i++) {
26 results[i] = relu_vec_unit(inputs[i]);
32 static inline v8fp_t lrelu_vec_unit(
v8fp_t a,
float slope) {
35 (
v8fp_t){ slope, slope, slope, slope, slope, slope, slope, slope };
38 v8fp_t scaled = slope_vec * a;
45 static inline void lrelu_vec(
v8fp_t* inputs,
50 for (
int i = 0; i < inputs_size /
VECTOR_SIZE; i++) {
51 results[i] = lrelu_vec_unit(inputs[i], slope);
57 static inline v8fp_t elu_vec_unit(
v8fp_t a,
float alpha) {
62 a[i] = alpha * (exp(value) - 1);
69 static inline void elu_vec(
v8fp_t* inputs,
74 for (
int i = 0; i < inputs_size /
VECTOR_SIZE; i++) {
75 results[i] = elu_vec_unit(inputs[i], alpha);
81 static inline v8fp_t selu_vec_unit(
v8fp_t a,
float alpha,
float lambda) {
82 a = elu_vec_unit(a, alpha);
91 static inline void selu_vec(
v8fp_t* inputs,
97 for (
int i = 0; i < inputs_size /
VECTOR_SIZE; i++) {
98 results[i] = selu_vec_unit(inputs[i], alpha, lambda);
109 a[i] = 1.0 / (1.0 + exp(-a[i]));
115 static inline void sigmoid_vec(
v8fp_t* inputs,
119 for (
int i = 0; i < inputs_size /
VECTOR_SIZE; i++) {
120 results[i] = sigmoid_vec_unit(inputs[i]);
127 v8fp_t one = { 1, 1, 1, 1, 1, 1, 1, 1 };
128 v8fp_t two = { 2, 2, 2, 2, 2, 2, 2, 2 };
130 v8fp_t sig = sigmoid_vec_unit(two_a);
131 return two * sig - one;
135 static inline void tanh_vec(
v8fp_t* inputs,
v8fp_t* results,
int inputs_size) {
137 for (
int i = 0; i < inputs_size /
VECTOR_SIZE; i++) {
138 results[i] = tanh_vec_unit(inputs[i]);
144 static inline v8fp_t hard_tanh_vec_unit(
v8fp_t a,
float min,
float max) {
148 a[i] = value < min ? min : value > max ? max : value;
154 static inline void hard_tanh_vec(
v8fp_t* inputs,
160 for (
int i = 0; i < inputs_size /
VECTOR_SIZE; i++) {
161 results[i] = hard_tanh_vec_unit(inputs[i], min, max);
166 static inline void activation_fun_vec(
float* inputs,
171 VEC_ARRAY_1D(
v8fp_t, _inputs, inputs);
172 VEC_ARRAY_1D(
v8fp_t, _results, results);
173 if (
function == RELU) {
174 relu_vec(_inputs, _results, inputs_size);
175 }
else if (
function == LRELU) {
176 lrelu_vec(_inputs, _results, inputs_size, params.slope);
177 }
else if (
function == ELU) {
178 elu_vec(_inputs, _results, inputs_size, params.alpha);
179 }
else if (
function == SELU) {
180 selu_vec(_inputs, _results, inputs_size, params.alpha, params.lambda);
181 }
else if (
function == TANH) {
182 tanh_vec(_inputs, _results, inputs_size);
183 }
else if (
function == HARD_TANH) {
184 hard_tanh_vec(_inputs, _results, inputs_size, params.min, params.max);
185 }
else if (
function == SIGMOID) {
186 sigmoid_vec(_inputs, _results, inputs_size);
187 }
else if (
function == SOFTMAX) {
188 assert(
false &&
"Softmax SIMD shouldn't be called from here!");