Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +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 | layout(local_size_x = LOCAL_SIZE_X, local_size_y = LOCAL_SIZE_Y, local_size_z = LOCAL_SIZE_Z) in; |
| 25 | |
Isabella Gottardi | 3f217ec | 2018-02-12 14:59:19 +0000 | [diff] [blame] | 26 | #include "activation_layer_helpers_cs.h" |
zhenglin | 08d5421 | 2017-12-27 14:33:06 +0800 | [diff] [blame] | 27 | #include "helpers_cs.h" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 28 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 29 | /** This performs an activation function floating point inputs. |
| 30 | * |
zhenglin | 08d5421 | 2017-12-27 14:33:06 +0800 | [diff] [blame] | 31 | * @note The data type must be passed at compile time using "#define DATA_TYPE_NAME". e.g. "#define DATA_TYPE_FP32" |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 32 | * @note Activation function should be given as a preprocessor argument using "#define act_name". e.g. "#define TANH" |
| 33 | * @note A, B variables required by some activation functions are set using A_VAL= and B_VAL= respectively. |
| 34 | * |
zhenglin | 08d5421 | 2017-12-27 14:33:06 +0800 | [diff] [blame] | 35 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32 |
| 36 | * @param[in] src_attrs The attributes of the source tensor |
| 37 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr |
| 38 | * @param[in] dst_attrs The attributes of the destination tensor |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 39 | */ |
zhenglin | 08d5421 | 2017-12-27 14:33:06 +0800 | [diff] [blame] | 40 | SHADER_PARAMS_DECLARATION |
| 41 | { |
| 42 | Tensor3DAttributes src_attrs; |
| 43 | Tensor3DAttributes dst_attrs; |
| 44 | }; |
| 45 | |
| 46 | #ifdef DATA_TYPE_FP32 |
| 47 | TENSOR_DECLARATION(1, srcBuffer, float, src_ptr, src_shift, 2, readonly); |
| 48 | TENSOR_DECLARATION(2, dstBuffer, float, dst_ptr, dst_shift, 2, writeonly); |
| 49 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 50 | void main(void) |
| 51 | { |
zhenglin | 08d5421 | 2017-12-27 14:33:06 +0800 | [diff] [blame] | 52 | Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift); |
| 53 | Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 54 | |
zhenglin | 08d5421 | 2017-12-27 14:33:06 +0800 | [diff] [blame] | 55 | float data = LOAD_CURRENT_ITEM(src_ptr, src_iter); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 56 | float data_out = 0.f; |
| 57 | // Perform activation |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 58 | #ifdef LOGISTIC |
| 59 | data_out = logistic_op(data); |
| 60 | #elif defined(TANH) /*LOGISTIC*/ |
| 61 | data_out = tanh_op(data); |
| 62 | #elif defined(RELU) /*RELU*/ |
| 63 | data_out = relu_op(data); |
| 64 | #elif defined(BRELU) /*BRELU*/ |
| 65 | data_out = brelu_op(data); |
| 66 | #elif defined(LU_BRELU) /*LU_BRELU*/ |
| 67 | data_out = lu_brelu_op(data); |
| 68 | #elif defined(LRELU) /*LRELU*/ |
| 69 | data_out = lrelu_op(data); |
| 70 | #elif defined(SRELU) /*SRELU*/ |
| 71 | data_out = srelu_op(data); |
| 72 | #elif defined(ABS) /*ABS*/ |
| 73 | data_out = abs_op(data); |
| 74 | #elif defined(SQUARE) /*SQUARE*/ |
| 75 | data_out = square_op(data); |
| 76 | #elif defined(SQRT) /*SQRT*/ |
| 77 | data_out = sqrt_op(data); |
| 78 | #elif defined(LINEAR) /*LINEAR*/ |
| 79 | data_out = linear_op(data); |
| 80 | #else /*LOGISTIC*/ |
| 81 | #error Activation function not provided |
| 82 | #endif /*LOGISTIC*/ |
| 83 | |
zhenglin | 08d5421 | 2017-12-27 14:33:06 +0800 | [diff] [blame] | 84 | STORE_CURRENT_ITEM(dst_ptr, dst_iter, data_out); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | #elif defined(DATA_TYPE_FP16) |
zhenglin | 08d5421 | 2017-12-27 14:33:06 +0800 | [diff] [blame] | 88 | TENSOR_DECLARATION(1, srcBuffer, uint, src_ptr, src_shift, 2, readonly); |
| 89 | TENSOR_DECLARATION(2, dstBuffer, uint, dst_ptr, dst_shift, 2, writeonly); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 90 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 91 | void main(void) |
| 92 | { |
zhenglin | 08d5421 | 2017-12-27 14:33:06 +0800 | [diff] [blame] | 93 | Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift); |
| 94 | Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 95 | |
zhenglin | 08d5421 | 2017-12-27 14:33:06 +0800 | [diff] [blame] | 96 | vec2 data = LOAD_UNPACK2_CURRENT_ITEM_HALF(src_ptr, src_iter); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 97 | // Perform activation |
zhenglin | 08d5421 | 2017-12-27 14:33:06 +0800 | [diff] [blame] | 98 | float a = data.x; |
| 99 | float b = data.y; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 100 | vec2 data_out; |
| 101 | #ifdef LOGISTIC /*LOGISTIC*/ |
| 102 | data_out.x = logistic_op(a); |
| 103 | data_out.y = logistic_op(b); |
| 104 | #elif defined(TANH) /*TANH*/ |
| 105 | data_out.x = tanh_op(a); |
| 106 | data_out.y = tanh_op(b); |
| 107 | #elif defined(RELU) /*RELU*/ |
| 108 | data_out.x = relu_op(a); |
| 109 | data_out.y = relu_op(b); |
| 110 | #elif defined(BRELU) /*BRELU*/ |
| 111 | data_out.x = brelu_op(a); |
| 112 | data_out.y = brelu_op(b); |
| 113 | #elif defined(LU_BRELU) /*LU_BRELU*/ |
| 114 | data_out.x = lu_brelu_op(a); |
| 115 | data_out.y = lu_brelu_op(b); |
| 116 | #elif defined(LRELU) /*LRELU*/ |
| 117 | data_out.x = lrelu_op(a); |
| 118 | data_out.y = lrelu_op(b); |
| 119 | #elif defined(SRELU) /*SRELU*/ |
| 120 | data_out.x = srelu_op(a); |
| 121 | data_out.y = srelu_op(b); |
| 122 | #elif defined(ABS) /*ABS*/ |
| 123 | data_out.x = abs_op(a); |
| 124 | data_out.y = abs_op(b); |
| 125 | #elif defined(SQUARE) /*SQUARE*/ |
| 126 | data_out.x = square_op(a); |
| 127 | data_out.y = square_op(b); |
| 128 | #elif defined(SQRT) /*SQRT*/ |
| 129 | data_out.x = sqrt_op(a); |
| 130 | data_out.y = sqrt_op(b); |
| 131 | #elif defined(LINEAR) /*LINEAR*/ |
| 132 | data_out.x = linear_op(a); |
| 133 | data_out.y = linear_op(b); |
| 134 | #else /*LOGISTIC*/ |
| 135 | #error Activation function not provided |
| 136 | #endif /*LOGISTIC*/ |
| 137 | |
zhenglin | 08d5421 | 2017-12-27 14:33:06 +0800 | [diff] [blame] | 138 | STORE_PACK2_CURRENT_ITEM_HALF(dst_ptr, dst_iter, data_out); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 139 | } |
zhenglin | 08d5421 | 2017-12-27 14:33:06 +0800 | [diff] [blame] | 140 | #endif /*DATA_TYPE_FP16*/ |