Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 ARM Limited. |
| 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 | /** This performs an activation function floating point inputs. |
| 27 | * |
| 28 | * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short |
| 29 | * @note Activation function should be given as a preprocessor argument using -DNAME. e.g. -DTANH |
| 30 | * @note Distinction between floating point and integer is done using -DTYPE_FP and -DTYPE_INT preprocessor argument |
| 31 | * @note A, B variables required by some activation functions are set using -DA= and -DB= respectively. |
| 32 | * |
| 33 | * @param[in] input_ptr Pointer to the source image. Supported data types: F16, F32 |
| 34 | * @param[in] input_stride_x Stride of the source image in X dimension (in bytes) |
| 35 | * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes) |
| 36 | * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes) |
| 37 | * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes) |
| 38 | * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 39 | * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes) |
| 40 | * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source image |
| 41 | * @param[out] output_ptr Pointer to the destination image. Supported data types: F16, F32 |
| 42 | * @param[in] output_stride_x Stride of the destination image in X dimension (in bytes) |
| 43 | * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes) |
| 44 | * @param[in] output_stride_y Stride of the destination image in Y dimension (in bytes) |
| 45 | * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes) |
| 46 | * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes) |
| 47 | * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes) |
| 48 | * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image |
| 49 | */ |
| 50 | __kernel void activation_layer( |
| 51 | TENSOR3D_DECLARATION(input), |
| 52 | TENSOR3D_DECLARATION(output)) |
| 53 | { |
| 54 | // Get pixels pointer |
| 55 | Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input); |
| 56 | Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output); |
| 57 | |
| 58 | // Load data |
| 59 | VEC_DATA_TYPE(DATA_TYPE, 16) |
| 60 | data = vload16(0, (__global DATA_TYPE *)input.ptr); |
| 61 | |
| 62 | // Perform activation |
| 63 | #if defined LOGISTIC |
| 64 | data = 1 / (1 + exp(-data)); |
| 65 | #elif defined TANH |
| 66 | data = (VEC_DATA_TYPE(DATA_TYPE, 16))A * tanh((VEC_DATA_TYPE(DATA_TYPE, 16))B * data); |
| 67 | #elif defined RELU |
| 68 | data = max(0, data); |
| 69 | #elif defined BRELU |
| 70 | data = min((VEC_DATA_TYPE(DATA_TYPE, 16))A, max(0, data)); |
| 71 | #elif defined SRELU |
| 72 | data = log(1 + exp(data)); |
| 73 | #elif defined ABS |
| 74 | #if defined TYPE_INT |
| 75 | data = abs(data); |
| 76 | #else |
| 77 | data = fabs(data); |
| 78 | #endif |
| 79 | #elif defined SQUARE |
| 80 | data = data * data; |
| 81 | #elif defined SQRT |
| 82 | data = sqrt(data); |
| 83 | #elif defined LINEAR |
| 84 | data = (VEC_DATA_TYPE(DATA_TYPE, 16))A * data + (VEC_DATA_TYPE(DATA_TYPE, 16))B; |
| 85 | #endif |
| 86 | |
| 87 | // Store result |
| 88 | vstore16(data, 0, (__global DATA_TYPE *)output.ptr); |
| 89 | } |