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