blob: 983b31debad34a47453cb6d3f3ffb202ec3a7dcc [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2019 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);
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +010072#elif defined(ELU) /*ELU*/
73 data_out = elu_op(data);
Anthony Barbier7068f992017-10-26 15:23:08 +010074#elif defined(ABS) /*ABS*/
75 data_out = abs_op(data);
76#elif defined(SQUARE) /*SQUARE*/
77 data_out = square_op(data);
78#elif defined(SQRT) /*SQRT*/
79 data_out = sqrt_op(data);
80#elif defined(LINEAR) /*LINEAR*/
81 data_out = linear_op(data);
Usama Arif3113a272019-05-16 17:34:14 +010082#elif defined(IDENTITY) /*IDENTITY*/
83 data_out = identity_op(data);
Anthony Barbier7068f992017-10-26 15:23:08 +010084#else /*LOGISTIC*/
85#error Activation function not provided
86#endif /*LOGISTIC*/
87
zhenglin08d54212017-12-27 14:33:06 +080088 STORE_CURRENT_ITEM(dst_ptr, dst_iter, data_out);
Anthony Barbier7068f992017-10-26 15:23:08 +010089}
90
91#elif defined(DATA_TYPE_FP16)
zhenglin08d54212017-12-27 14:33:06 +080092TENSOR_DECLARATION(1, srcBuffer, uint, src_ptr, src_shift, 2, readonly);
93TENSOR_DECLARATION(2, dstBuffer, uint, dst_ptr, dst_shift, 2, writeonly);
Anthony Barbier7068f992017-10-26 15:23:08 +010094
Anthony Barbier7068f992017-10-26 15:23:08 +010095void main(void)
96{
zhenglin08d54212017-12-27 14:33:06 +080097 Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift);
98 Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);
Anthony Barbier7068f992017-10-26 15:23:08 +010099
zhenglin08d54212017-12-27 14:33:06 +0800100 vec2 data = LOAD_UNPACK2_CURRENT_ITEM_HALF(src_ptr, src_iter);
Anthony Barbier7068f992017-10-26 15:23:08 +0100101 // Perform activation
zhenglin08d54212017-12-27 14:33:06 +0800102 float a = data.x;
103 float b = data.y;
Anthony Barbier7068f992017-10-26 15:23:08 +0100104 vec2 data_out;
105#ifdef LOGISTIC /*LOGISTIC*/
106 data_out.x = logistic_op(a);
107 data_out.y = logistic_op(b);
108#elif defined(TANH) /*TANH*/
109 data_out.x = tanh_op(a);
110 data_out.y = tanh_op(b);
111#elif defined(RELU) /*RELU*/
112 data_out.x = relu_op(a);
113 data_out.y = relu_op(b);
114#elif defined(BRELU) /*BRELU*/
115 data_out.x = brelu_op(a);
116 data_out.y = brelu_op(b);
117#elif defined(LU_BRELU) /*LU_BRELU*/
118 data_out.x = lu_brelu_op(a);
119 data_out.y = lu_brelu_op(b);
120#elif defined(LRELU) /*LRELU*/
121 data_out.x = lrelu_op(a);
122 data_out.y = lrelu_op(b);
123#elif defined(SRELU) /*SRELU*/
124 data_out.x = srelu_op(a);
125 data_out.y = srelu_op(b);
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +0100126#elif defined(ELU) /*ELU*/
127 data_out.x = elu_op(a);
128 data_out.y = elu_op(b);
Anthony Barbier7068f992017-10-26 15:23:08 +0100129#elif defined(ABS) /*ABS*/
130 data_out.x = abs_op(a);
131 data_out.y = abs_op(b);
132#elif defined(SQUARE) /*SQUARE*/
133 data_out.x = square_op(a);
134 data_out.y = square_op(b);
135#elif defined(SQRT) /*SQRT*/
136 data_out.x = sqrt_op(a);
137 data_out.y = sqrt_op(b);
138#elif defined(LINEAR) /*LINEAR*/
139 data_out.x = linear_op(a);
140 data_out.y = linear_op(b);
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +0100141#elif defined(IDENTITY) /*IDENTITY*/
Usama Arif3113a272019-05-16 17:34:14 +0100142 data_out.x = identity_op(a);
143 data_out.y = identity_op(b);
Anthony Barbier7068f992017-10-26 15:23:08 +0100144#else /*LOGISTIC*/
145#error Activation function not provided
146#endif /*LOGISTIC*/
147
zhenglin08d54212017-12-27 14:33:06 +0800148 STORE_PACK2_CURRENT_ITEM_HALF(dst_ptr, dst_iter, data_out);
Anthony Barbier7068f992017-10-26 15:23:08 +0100149}
zhenglin08d54212017-12-27 14:33:06 +0800150#endif /*DATA_TYPE_FP16*/