Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 1 | /* |
| 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 |
| 25 | precision highp float; |
| 26 | #elif defined(DATA_TYPE_FP16) |
| 27 | #if defined(LOGISTIC) || defined(TANH) || defined(SRELU) || defined(SQRT) |
| 28 | precision highp float; |
| 29 | #else /*LOGISTIC_TANH_SRELU_SQRT*/ |
| 30 | precision 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 |
| 46 | float logistic_op(float x) |
| 47 | { |
| 48 | return DIV_OP(CONST_ONE, ADD_OP(CONST_ONE, EXP_OP(-x))); |
| 49 | } |
| 50 | vec4 logistic_op(vec4 x) |
| 51 | { |
| 52 | return DIV_OP(vec4(CONST_ONE), ADD_OP(CONST_ONE, EXP_OP(-x))); |
| 53 | } |
| 54 | // Hyperbolic Tangent Activation |
| 55 | float 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 |
| 72 | float relu_op(float x) |
| 73 | { |
| 74 | return max(0.f, x); |
| 75 | } |
| 76 | vec4 relu_op(vec4 x) |
| 77 | { |
| 78 | return max(vec4(0.f), x); |
| 79 | } |
| 80 | // Bounded RELU Activation |
| 81 | float brelu_op(float x) |
| 82 | { |
| 83 | return min(float(A_VAL), max(float(0.0), x)); |
| 84 | } |
| 85 | // Lower Upper Bounded RELU Activation |
| 86 | float lu_brelu_op(float x) |
| 87 | { |
| 88 | return min(max(x, float(B_VAL)), float(A_VAL)); |
| 89 | } |
| 90 | // Leaky RELU Activation |
| 91 | float lrelu_op(float x) |
| 92 | { |
| 93 | return (x > float(0.0)) ? x : MUL_OP(float(A_VAL), x); |
| 94 | } |
| 95 | // Soft RELU Activation |
| 96 | float srelu_op(float x) |
| 97 | { |
| 98 | return LOG_OP(ADD_OP(CONST_ONE, EXP_OP(x))); |
| 99 | } |
| 100 | // Absolute Activation |
| 101 | float abs_op(float x) |
| 102 | { |
| 103 | return ABS_OP(x); |
| 104 | } |
| 105 | // Square Activation |
| 106 | float square_op(float x) |
| 107 | { |
| 108 | return MUL_OP(x, x); |
| 109 | } |
| 110 | // Square-root Activation |
| 111 | float sqrt_op(float x) |
| 112 | { |
| 113 | return SQRT_OP(x); |
| 114 | } |
| 115 | // Linear Activation |
| 116 | float linear_op(float x) |
| 117 | { |
| 118 | return MLA_OP(float(B_VAL), float(A_VAL), x); |
| 119 | } |