Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | #include "helpers.h" |
| 25 | |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 26 | #define TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 27 | |
SiCong Li | 1e5c157 | 2017-07-27 17:58:52 +0100 | [diff] [blame] | 28 | #define CONST_ONE 1.f |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 29 | #define ABS_OP(a) fabs((a)) |
| 30 | #define ADD_OP(a, b) ((a) + (b)) |
| 31 | #define SUB_OP(a, b) ((a) - (b)) |
| 32 | #define MUL_OP(a, b) ((a) * (b)) |
| 33 | #define MLA_OP(a, b, c) ((b) * (c) + (a)) |
| 34 | #define DIV_OP(a, b) ((a) / (b)) |
| 35 | #define EXP_OP(a) exp((a)) |
| 36 | #define LOG_OP(a) log((a)) |
| 37 | #define SQRT_OP(a) sqrt((a)) |
| 38 | #define TANH_OP(a) tanh((a)) |
| 39 | |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 40 | // Logistic Activation |
| 41 | inline TYPE logistic_op(TYPE x) |
| 42 | { |
Moritz Pflanzer | a36ccf1 | 2017-07-24 15:05:12 +0100 | [diff] [blame] | 43 | return DIV_OP((TYPE)CONST_ONE, ADD_OP((TYPE)CONST_ONE, EXP_OP(-x))); |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 44 | } |
| 45 | // Hyperbolic Tangent Activation |
| 46 | inline TYPE tanh_op(TYPE x) |
| 47 | { |
| 48 | return MUL_OP((TYPE)A_VAL, TANH_OP(MUL_OP((TYPE)B_VAL, x))); |
| 49 | } |
| 50 | // RELU Tangent Activation |
| 51 | inline TYPE relu_op(TYPE x) |
| 52 | { |
| 53 | return max(0, x); |
| 54 | } |
| 55 | // Bounded RELU Activation |
| 56 | inline TYPE brelu_op(TYPE x) |
| 57 | { |
| 58 | return min((TYPE)A_VAL, max(0, x)); |
| 59 | } |
Georgios Pinitas | 64ebe5b | 2017-09-01 17:44:24 +0100 | [diff] [blame] | 60 | // Lower Upper Bounded RELU Activation |
| 61 | inline TYPE lu_brelu_op(TYPE x) |
| 62 | { |
| 63 | return min(max(x, (TYPE)B_VAL), (TYPE)A_VAL); |
| 64 | } |
Georgios Pinitas | 579c049 | 2017-07-12 16:12:12 +0100 | [diff] [blame] | 65 | // Leaky RELU Activation |
| 66 | inline TYPE lrelu_op(TYPE x) |
| 67 | { |
| 68 | return select(MUL_OP((TYPE)A_VAL, x), x, x > (TYPE)0); |
| 69 | } |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 70 | // Soft RELU Activation |
| 71 | inline TYPE srelu_op(TYPE x) |
| 72 | { |
Moritz Pflanzer | a36ccf1 | 2017-07-24 15:05:12 +0100 | [diff] [blame] | 73 | return LOG_OP(ADD_OP((TYPE)CONST_ONE, EXP_OP(x))); |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 74 | } |
| 75 | // Absolute Activation |
| 76 | inline TYPE abs_op(TYPE x) |
| 77 | { |
| 78 | return ABS_OP(x); |
| 79 | } |
| 80 | // Square Activation |
| 81 | inline TYPE square_op(TYPE x) |
| 82 | { |
| 83 | return MUL_OP(x, x); |
| 84 | } |
| 85 | // Square-root Activation |
| 86 | inline TYPE sqrt_op(TYPE x) |
| 87 | { |
| 88 | return SQRT_OP(x); |
| 89 | } |
| 90 | // Linear Activation |
| 91 | inline TYPE linear_op(TYPE x) |
| 92 | { |
| 93 | return MLA_OP((TYPE)B_VAL, (TYPE)A_VAL, x); |
| 94 | } |
| 95 | |
| 96 | #define ACTIVATION_OP2(op, x) op##_op(x) |
| 97 | #define ACTIVATION_OP(op, x) ACTIVATION_OP2(op, x) |
| 98 | |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 99 | #if defined(ACT) |
| 100 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | /** This performs an activation function floating point inputs. |
| 102 | * |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 103 | * @note In order to perform the activation function "in-place", the pre-processor -DIN_PLACE must be passed at compile time |
| 104 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 106 | * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 |
| 107 | * @note Activation function should be given as a preprocessor argument using -DACT=name. e.g. -DACT=TANH |
| 108 | * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 110 | * @param[in] input_ptr Pointer to the source image. Supported data types: F16/F32 |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | * @param[in] input_stride_x Stride of the source image in X dimension (in bytes) |
| 112 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 113 | * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes) |
| 114 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 115 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 116 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 117 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 118 | * @param[out] output_ptr Pointer to the destination image. Supported data types: same as @p input_ptr |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes) |
| 120 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 121 | * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes) |
| 122 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 123 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 124 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 125 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image |
| 126 | */ |
| 127 | __kernel void activation_layer( |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 128 | TENSOR3D_DECLARATION(input) |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 129 | #ifndef IN_PLACE |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 130 | , |
| 131 | TENSOR3D_DECLARATION(output) |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 132 | #endif /* not IN_PLACE */ |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 133 | ) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 134 | { |
| 135 | // Get pixels pointer |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 136 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 137 | #ifdef IN_PLACE |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 138 | Tensor3D output = input; |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 139 | #else /* IN_PLACE */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 140 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 141 | #endif /* IN_PLACE */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | |
| 143 | // Load data |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 144 | TYPE data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | |
| 146 | // Perform activation |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 147 | data = ACTIVATION_OP(ACT, data); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | |
| 149 | // Store result |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 150 | VSTORE(VEC_SIZE) |
| 151 | (data, 0, (__global DATA_TYPE *)output.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | } |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 153 | |
| 154 | #endif /* defined(ACT) */ |