blob: 9a1e233624a9b4ff75e00bebc748b61226274d52 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Isabella Gottardi3f217ec2018-02-12 14:59:19 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +01003 *
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 */
24layout(local_size_x = LOCAL_SIZE_X, local_size_y = LOCAL_SIZE_Y, local_size_z = LOCAL_SIZE_Z) in;
25
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000026#include "activation_layer_helpers_cs.h"
zhenglin08d54212017-12-27 14:33:06 +080027#include "helpers_cs.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010028
Anthony Barbier7068f992017-10-26 15:23:08 +010029/** This performs an activation function floating point inputs.
30 *
zhenglin08d54212017-12-27 14:33:06 +080031 * @note The data type must be passed at compile time using "#define DATA_TYPE_NAME". e.g. "#define DATA_TYPE_FP32"
Anthony Barbier7068f992017-10-26 15:23:08 +010032 * @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 *
zhenglin08d54212017-12-27 14:33:06 +080035 * @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 Barbier7068f992017-10-26 15:23:08 +010039 */
zhenglin08d54212017-12-27 14:33:06 +080040SHADER_PARAMS_DECLARATION
41{
42 Tensor3DAttributes src_attrs;
43 Tensor3DAttributes dst_attrs;
44};
45
46#ifdef DATA_TYPE_FP32
47TENSOR_DECLARATION(1, srcBuffer, float, src_ptr, src_shift, 2, readonly);
48TENSOR_DECLARATION(2, dstBuffer, float, dst_ptr, dst_shift, 2, writeonly);
49
Anthony Barbier7068f992017-10-26 15:23:08 +010050void main(void)
51{
zhenglin08d54212017-12-27 14:33:06 +080052 Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift);
53 Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);
Anthony Barbier7068f992017-10-26 15:23:08 +010054
zhenglin08d54212017-12-27 14:33:06 +080055 float data = LOAD_CURRENT_ITEM(src_ptr, src_iter);
Anthony Barbier7068f992017-10-26 15:23:08 +010056 float data_out = 0.f;
57 // Perform activation
Anthony Barbier7068f992017-10-26 15:23:08 +010058#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
zhenglin08d54212017-12-27 14:33:06 +080084 STORE_CURRENT_ITEM(dst_ptr, dst_iter, data_out);
Anthony Barbier7068f992017-10-26 15:23:08 +010085}
86
87#elif defined(DATA_TYPE_FP16)
zhenglin08d54212017-12-27 14:33:06 +080088TENSOR_DECLARATION(1, srcBuffer, uint, src_ptr, src_shift, 2, readonly);
89TENSOR_DECLARATION(2, dstBuffer, uint, dst_ptr, dst_shift, 2, writeonly);
Anthony Barbier7068f992017-10-26 15:23:08 +010090
Anthony Barbier7068f992017-10-26 15:23:08 +010091void main(void)
92{
zhenglin08d54212017-12-27 14:33:06 +080093 Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift);
94 Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);
Anthony Barbier7068f992017-10-26 15:23:08 +010095
zhenglin08d54212017-12-27 14:33:06 +080096 vec2 data = LOAD_UNPACK2_CURRENT_ITEM_HALF(src_ptr, src_iter);
Anthony Barbier7068f992017-10-26 15:23:08 +010097 // Perform activation
zhenglin08d54212017-12-27 14:33:06 +080098 float a = data.x;
99 float b = data.y;
Anthony Barbier7068f992017-10-26 15:23:08 +0100100 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
zhenglin08d54212017-12-27 14:33:06 +0800138 STORE_PACK2_CURRENT_ITEM_HALF(dst_ptr, dst_iter, data_out);
Anthony Barbier7068f992017-10-26 15:23:08 +0100139}
zhenglin08d54212017-12-27 14:33:06 +0800140#endif /*DATA_TYPE_FP16*/