Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | e5bf4c5 | 2019-02-14 17:47:33 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 ARM Limited. |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +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 | |
| 26 | #define TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
Michele Di Giorgio | d304e80 | 2018-07-06 10:17:33 +0100 | [diff] [blame] | 27 | #define VEC_FLOAT VEC_DATA_TYPE(float, VEC_SIZE) |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 28 | |
Michele Di Giorgio | a1f7e33 | 2018-01-22 17:26:36 +0000 | [diff] [blame] | 29 | // RELU Activation |
| 30 | inline TYPE relu_op(TYPE x) |
| 31 | { |
| 32 | return max((TYPE)CONST_0, x); |
| 33 | } |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 34 | // Bounded RELU Activation |
| 35 | inline TYPE brelu_op(TYPE x) |
| 36 | { |
Michele Di Giorgio | a1f7e33 | 2018-01-22 17:26:36 +0000 | [diff] [blame] | 37 | return min((TYPE)A_VAL, max(CONST_0, x)); |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 38 | } |
| 39 | // Lower Upper Bounded RELU Activation |
| 40 | inline TYPE lu_brelu_op(TYPE x) |
| 41 | { |
| 42 | return min(max(x, (TYPE)B_VAL), (TYPE)A_VAL); |
| 43 | } |
| 44 | |
| 45 | #define ACTIVATION_OP2(op, x) op##_op(x) |
| 46 | #define ACTIVATION_OP(op, x) ACTIVATION_OP2(op, x) |
| 47 | |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 48 | #if defined(O1_VAL) && defined(O2_VAL) && defined(S1_VAL) && defined(S2_VAL) |
| 49 | #define PERFORM_ACTIVATION_QA8(act, data) \ |
| 50 | ({ \ |
| 51 | data = ACTIVATION_OP(act, data); \ |
| 52 | \ |
| 53 | VEC_DATA_TYPE(float, VEC_SIZE) \ |
| 54 | fdata = CONVERT(data, VEC_DATA_TYPE(float, VEC_SIZE)); \ |
| 55 | \ |
| 56 | fdata = round((fdata - (float)O1_VAL) * ((float)S1_VAL / (float)S2_VAL) + (float)O2_VAL); \ |
| 57 | data = CONVERT_SAT(fdata, VEC_DATA_TYPE(uchar, VEC_SIZE)); \ |
| 58 | }) |
| 59 | #else /* defined(O1_VAL) && defined(O2_VAL) && defined(S1_VAL) && defined(S2_VAL) */ |
| 60 | #define PERFORM_ACTIVATION_QA8(act, data) \ |
| 61 | ({ \ |
| 62 | data = ACTIVATION_OP(act, data); \ |
| 63 | }) |
| 64 | #endif /* defined(O1_VAL) && defined(O2_VAL) && defined(S1_VAL) && defined(S2_VAL) */ |
| 65 | |
| 66 | #if defined(ACT) |
| 67 | |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 68 | /** This performs an activation function on QASYMM8 inputs. |
| 69 | * |
| 70 | * @note In order to perform the activation function "in-place", the pre-processor -DIN_PLACE must be passed at compile time |
| 71 | * |
| 72 | * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short |
| 73 | * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 |
| 74 | * @note Activation function should be given as a preprocessor argument using -DACT=name. e.g. -DACT=TANH |
| 75 | * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively. |
| 76 | * @note Quantization scales of the input/output tensors are passed in with -DS1_VAL= and -DS2_VAL= respectively. |
| 77 | * @note Quantization offsets of the input/output tensors are passed in with -DO1_VAL= and -DO2_VAL= respectively. |
Michele Di Giorgio | a1f7e33 | 2018-01-22 17:26:36 +0000 | [diff] [blame] | 78 | * @note Quantized value of constant zero should be given as a preprocessor argument using -DCONST_0=value. e.g. -DCONST_0=128. |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 79 | * |
| 80 | * @param[in] input_ptr Pointer to the source image. Supported data types: QASYMM8 |
| 81 | * @param[in] input_stride_x Stride of the source image in X dimension (in bytes) |
| 82 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 83 | * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes) |
| 84 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 85 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 86 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 87 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image |
Michele Di Giorgio | e5bf4c5 | 2019-02-14 17:47:33 +0000 | [diff] [blame] | 88 | * @param[out] output_ptr (Optional) Pointer to the destination image. Supported data types: same as @p input_ptr |
| 89 | * @param[in] output_stride_x (Optional) Stride of the destination image in X dimension (in bytes) |
| 90 | * @param[in] output_step_x (Optional) output_stride_x * number of elements along X processed per workitem(in bytes) |
| 91 | * @param[in] output_stride_y (Optional) Stride of the destination image in Y dimension (in bytes) |
| 92 | * @param[in] output_step_y (Optional) output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 93 | * @param[in] output_stride_z (Optional) Stride of the source tensor in Z dimension (in bytes) |
| 94 | * @param[in] output_step_z (Optional) output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 95 | * @param[in] output_offset_first_element_in_bytes (Optional) The offset of the first element in the destination image |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 96 | */ |
| 97 | __kernel void activation_layer_qa8( |
| 98 | TENSOR3D_DECLARATION(input) |
| 99 | #ifndef IN_PLACE |
| 100 | , |
| 101 | TENSOR3D_DECLARATION(output) |
| 102 | #endif /* not IN_PLACE */ |
| 103 | ) |
| 104 | { |
| 105 | // Get pixels pointer |
| 106 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 107 | #ifdef IN_PLACE |
| 108 | Tensor3D output = input; |
| 109 | #else /* IN_PLACE */ |
| 110 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 111 | #endif /* IN_PLACE */ |
| 112 | |
| 113 | // Load data |
| 114 | TYPE data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input.ptr); |
| 115 | |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 116 | data = PERFORM_ACTIVATION_QA8(ACT, data); |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 117 | |
| 118 | // Store result |
| 119 | VSTORE(VEC_SIZE) |
Giorgio Arena | a0d1183 | 2018-01-17 16:13:46 +0000 | [diff] [blame] | 120 | (data, 0, (__global DATA_TYPE *)output.ptr); |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 121 | } |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 122 | |
Michele Di Giorgio | d304e80 | 2018-07-06 10:17:33 +0100 | [diff] [blame] | 123 | #endif /* defined(ACT) */ |
Michele Di Giorgio | e5bf4c5 | 2019-02-14 17:47:33 +0000 | [diff] [blame] | 124 | |
| 125 | #if defined(O2_VAL) && defined(S2_VAL) |
| 126 | #define OFFSET_OUT O2_VAL |
| 127 | #define SCALE_OUT S2_VAL |
| 128 | #else // defined(O2_VAL) && defined(S2_VAL) |
| 129 | #define OFFSET_OUT O1_VAL |
| 130 | #define SCALE_OUT S1_VAL |
| 131 | #endif // defined(O2_VAL) && defined(S2_VAL) |
| 132 | |
| 133 | /** This performs a Logistic activation function on QASYMM8 inputs. |
| 134 | * |
| 135 | * @note In order to perform the activation function "in-place", the pre-processor -DIN_PLACE must be passed at compile time |
| 136 | * |
| 137 | * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short |
| 138 | * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 |
| 139 | * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively. |
| 140 | * @note Quantization scales of the input/output tensors are passed in with -DS1_VAL= and -DS2_VAL= respectively. |
| 141 | * @note Quantization offsets of the input/output tensors are passed in with -DO1_VAL= and -DO2_VAL= respectively. |
| 142 | * @note Quantized value of constant zero should be given as a preprocessor argument using -DCONST_0=value. e.g. -DCONST_0=128. |
| 143 | * |
| 144 | * @param[in] input_ptr Pointer to the source image. Supported data types: QASYMM8 |
| 145 | * @param[in] input_stride_x Stride of the source image in X dimension (in bytes) |
| 146 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 147 | * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes) |
| 148 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 149 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 150 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 151 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image |
| 152 | * @param[out] output_ptr (Optional) Pointer to the destination image. Supported data types: same as @p input_ptr |
| 153 | * @param[in] output_stride_x (Optional) Stride of the destination image in X dimension (in bytes) |
| 154 | * @param[in] output_step_x (Optional) output_stride_x * number of elements along X processed per workitem(in bytes) |
| 155 | * @param[in] output_stride_y (Optional) Stride of the destination image in Y dimension (in bytes) |
| 156 | * @param[in] output_step_y (Optional) output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 157 | * @param[in] output_stride_z (Optional) Stride of the source tensor in Z dimension (in bytes) |
| 158 | * @param[in] output_step_z (Optional) output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 159 | * @param[in] output_offset_first_element_in_bytes (Optional) The offset of the first element in the destination image |
| 160 | */ |
| 161 | __kernel void activation_layer_logistic_qa8( |
| 162 | TENSOR3D_DECLARATION(input) |
| 163 | #ifndef IN_PLACE |
| 164 | , |
| 165 | TENSOR3D_DECLARATION(output) |
| 166 | #endif /* not IN_PLACE */ |
| 167 | ) |
| 168 | { |
| 169 | // Get pixels pointer |
| 170 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 171 | #ifdef IN_PLACE |
| 172 | Tensor3D output = input; |
| 173 | #else /* IN_PLACE */ |
| 174 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 175 | #endif /* IN_PLACE */ |
| 176 | |
| 177 | // Load data |
| 178 | TYPE data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input.ptr); |
| 179 | |
| 180 | VEC_FLOAT data_flt = CONVERT(data, VEC_FLOAT); |
| 181 | data_flt = round(data_flt - (float)O1_VAL) * ((float)S1_VAL); |
| 182 | data_flt = 1.f / (1.f + exp(-data_flt)); |
| 183 | |
| 184 | data = CONVERT_SAT(round(data_flt / ((float)SCALE_OUT)) + (float)OFFSET_OUT, TYPE); |
| 185 | |
| 186 | // Store result |
| 187 | VSTORE(VEC_SIZE) |
| 188 | (data, 0, (__global DATA_TYPE *)output.ptr); |
| 189 | } |