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