blob: f43a33fe876f5e376f1a57039aec57380e8e43d3 [file] [log] [blame]
Isabella Gottardi3f217ec2018-02-12 14:59:19 +00001/*
2 * Copyright (c) 2018 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifdef DATA_TYPE_FP32
25precision highp float;
26#elif defined(DATA_TYPE_FP16)
27#if defined(LOGISTIC) || defined(TANH) || defined(SRELU) || defined(SQRT)
28precision highp float;
29#else /*LOGISTIC_TANH_SRELU_SQRT*/
30precision mediump float;
31#endif /*LOGISTIC_TANH_SRELU_SQRT*/
32#endif /*DATA_TYPE_FP32*/
33
34#define ABS_OP(a) abs((a))
35#define ADD_OP(a, b) ((a) + (b))
36#define SUB_OP(a, b) ((a) - (b))
37#define MUL_OP(a, b) ((a) * (b))
38#define MLA_OP(a, b, c) ((b) * (c) + (a))
39#define DIV_OP(a, b) ((a) / (b))
40#define EXP_OP(a) exp((a))
41#define LOG_OP(a) log((a))
42#define SQRT_OP(a) sqrt((a))
43#define CONST_ONE (1.f)
44
45// Logistic Activation
46float logistic_op(float x)
47{
48 return DIV_OP(CONST_ONE, ADD_OP(CONST_ONE, EXP_OP(-x)));
49}
50vec4 logistic_op(vec4 x)
51{
52 return DIV_OP(vec4(CONST_ONE), ADD_OP(CONST_ONE, EXP_OP(-x)));
53}
54// Hyperbolic Tangent Activation
55float tanh_op(float x)
56{
57 float tmp = float(B_VAL) * x;
58 if(tmp > 10.f)
59 {
60 return MUL_OP(float(A_VAL), 1.f);
61 }
62 else if(tmp < -10.f)
63 {
64 return MUL_OP(float(A_VAL), -1.f);
65 }
66 else
67 {
68 return MUL_OP(float(A_VAL), tanh(tmp + 0.000001f));
69 }
70}
71// RELU Tangent Activation
72float relu_op(float x)
73{
74 return max(0.f, x);
75}
76vec4 relu_op(vec4 x)
77{
78 return max(vec4(0.f), x);
79}
80// Bounded RELU Activation
81float brelu_op(float x)
82{
83 return min(float(A_VAL), max(float(0.0), x));
84}
85// Lower Upper Bounded RELU Activation
86float lu_brelu_op(float x)
87{
88 return min(max(x, float(B_VAL)), float(A_VAL));
89}
90// Leaky RELU Activation
91float lrelu_op(float x)
92{
93 return (x > float(0.0)) ? x : MUL_OP(float(A_VAL), x);
94}
95// Soft RELU Activation
96float srelu_op(float x)
97{
98 return LOG_OP(ADD_OP(CONST_ONE, EXP_OP(x)));
99}
100// Absolute Activation
101float abs_op(float x)
102{
103 return ABS_OP(x);
104}
105// Square Activation
106float square_op(float x)
107{
108 return MUL_OP(x, x);
109}
110// Square-root Activation
111float sqrt_op(float x)
112{
113 return SQRT_OP(x);
114}
115// Linear Activation
116float linear_op(float x)
117{
118 return MLA_OP(float(B_VAL), float(A_VAL), x);
119}