Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Usama Arif | e2428a0 | 2019-05-09 11:03:17 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 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 | */ |
Usama Arif | e2428a0 | 2019-05-09 11:03:17 +0100 | [diff] [blame] | 24 | #if defined(ACT) && defined(DATA_TYPE) && defined(VEC_SIZE) |
| 25 | |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 26 | #define TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE) |
| 27 | |
Usama Arif | e2428a0 | 2019-05-09 11:03:17 +0100 | [diff] [blame] | 28 | #include "activation_float_helpers.h" |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 29 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | /** This performs an activation function floating point inputs. |
| 31 | * |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 32 | * @note In order to perform the activation function "in-place", the pre-processor -DIN_PLACE must be passed at compile time |
| 33 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | * @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] | 35 | * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16 |
| 36 | * @note Activation function should be given as a preprocessor argument using -DACT=name. e.g. -DACT=TANH |
| 37 | * @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] | 38 | * |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 39 | * @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] | 40 | * @param[in] input_stride_x Stride of the source image in X dimension (in bytes) |
| 41 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 42 | * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes) |
| 43 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 44 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 45 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 46 | * @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] | 47 | * @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] | 48 | * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes) |
| 49 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 50 | * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes) |
| 51 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 52 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 53 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 54 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image |
| 55 | */ |
| 56 | __kernel void activation_layer( |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 57 | TENSOR3D_DECLARATION(input) |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 58 | #ifndef IN_PLACE |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 59 | , |
| 60 | TENSOR3D_DECLARATION(output) |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 61 | #endif /* not IN_PLACE */ |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 62 | ) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | { |
| 64 | // Get pixels pointer |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 65 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 66 | #ifdef IN_PLACE |
Gian Marco Iodice | b30dcc5 | 2017-06-20 09:07:21 +0100 | [diff] [blame] | 67 | Tensor3D output = input; |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 68 | #else /* IN_PLACE */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 70 | #endif /* IN_PLACE */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | |
| 72 | // Load data |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 73 | TYPE data = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 74 | |
| 75 | // Perform activation |
Usama Arif | e2428a0 | 2019-05-09 11:03:17 +0100 | [diff] [blame] | 76 | data = ACTIVATION(ACT, DATA_TYPE, data, A_VAL, B_VAL); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | |
| 78 | // Store result |
Georgios Pinitas | 00394ae | 2017-06-22 18:13:55 +0100 | [diff] [blame] | 79 | VSTORE(VEC_SIZE) |
| 80 | (data, 0, (__global DATA_TYPE *)output.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | } |
Giorgio Arena | 99ac60b | 2018-02-16 15:17:23 +0000 | [diff] [blame] | 82 | |
Usama Arif | e2428a0 | 2019-05-09 11:03:17 +0100 | [diff] [blame] | 83 | #endif /* defined(ACT) */ |