blob: 728e9644b26278b7f3d2c0da1094e789c96139ff [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 */
24
25layout(local_size_x = LOCAL_SIZE_X, local_size_y = LOCAL_SIZE_Y, local_size_z = LOCAL_SIZE_Z) in;
26
Joel Liang09849a02018-01-05 15:12:53 +080027#include "helpers_cs.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010028
Isabella Gottardi3f217ec2018-02-12 14:59:19 +000029#ifdef FUSED_ACTIVATION
30#include "activation_layer_helpers_cs.h"
31#endif /* FUSED_ACTIVATION */
32
Joel Liang09849a02018-01-05 15:12:53 +080033#if defined(DATA_TYPE_FP16)
34precision mediump float;
35#endif // DATA_TYPE_FP16
ASIAPAC\steli0123ac91b2017-11-07 16:14:44 +080036
37/** This kernel performs a direct convolution to convolve the low three dimensions
38 *
Joel Liang09849a02018-01-05 15:12:53 +080039 * @note The data type must be passed at compile time using "#define DATA_TYPE_NAME". e.g. "#define DATA_TYPE_FP32"
40 * @note This kernel has multiple optimized direct convolution options for FP16.
41 * The direct convolution option must be passed at compile time using "#define PROCESS_nX_nY_nZ" e.g. "#define PROCESS_8X_1Y_1Z"
42 * @note The convolution stride x must be passed at compile time using "#define STRIDE_X n" e.g. "#define STRIDE_X 1"
43 * This OpenGL ES shader works with stride_x = 1 and 2
ASIAPAC\steli0123ac91b2017-11-07 16:14:44 +080044 * @note If biases are used then "define HAS_BIAS" has to be passed at compile time
45 *
Joel Liang09849a02018-01-05 15:12:53 +080046 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
47 * @param[in] src_attrs The attributes of the source tensor
48 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
49 * @param[in] dst_attrs The attributes of the destination tensor
50 * @param[out] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr
51 * @param[in] weights_attrs The attributes of the weights tensor
52 * @param[in] biases_ptr Pointer to the biases tensor. Same as @p src_ptr
53 * @param[in] biases_attrs The attributes of the weights tensor
54 * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension
55 * @param[in] weights_depth The third dimensions of the weights tensors
ASIAPAC\steli0123ac91b2017-11-07 16:14:44 +080056 */
Joel Liang09849a02018-01-05 15:12:53 +080057SHADER_PARAMS_DECLARATION
Anthony Barbier7068f992017-10-26 15:23:08 +010058{
Joel Liang09849a02018-01-05 15:12:53 +080059 Tensor3DAttributes src_attrs;
60 Tensor3DAttributes dst_attrs;
61 Tensor3DAttributes weights_attrs;
Anthony Barbier7068f992017-10-26 15:23:08 +010062#ifdef BIAS
Joel Liang09849a02018-01-05 15:12:53 +080063 VectorAttributes biases_attrs;
Anthony Barbier7068f992017-10-26 15:23:08 +010064#endif /* BIAS */
65 uint weights_stride_w;
66 uint weights_depth;
67};
68
Joel Liang09849a02018-01-05 15:12:53 +080069#ifdef DATA_TYPE_FP32
70TENSOR_DECLARATION(1, srcBuffer, float, src_ptr, src_shift, 2, readonly);
71TENSOR_DECLARATION(2, dstBuffer, float, dst_ptr, dst_shift, 2, writeonly);
72TENSOR_DECLARATION(3, weightsBuffer, float, weights_ptr, weights_shift, 2, readonly);
Anthony Barbier7068f992017-10-26 15:23:08 +010073#ifdef BIAS
Joel Liang09849a02018-01-05 15:12:53 +080074TENSOR_DECLARATION(4, biasesBuffer, float, biases_ptr, biases_shift, 2, readonly);
Anthony Barbier7068f992017-10-26 15:23:08 +010075#endif /* BIAS */
76
Anthony Barbier7068f992017-10-26 15:23:08 +010077void main()
78{
Joel Liang09849a02018-01-05 15:12:53 +080079 ImageIterator src_iter = CONVERT_TO_IMAGE_ITERATOR(src_attrs, src_shift);
80 Tensor3DIterator weights_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(weights_attrs, weights_shift);
81 Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);
Anthony Barbier7068f992017-10-26 15:23:08 +010082
83#ifdef BIAS
Joel Liang09849a02018-01-05 15:12:53 +080084 VectorIterator biases_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(biases_attrs, biases_shift);
Anthony Barbier7068f992017-10-26 15:23:08 +010085#endif /* BIAS */
86
Joel Liang09849a02018-01-05 15:12:53 +080087 float pixels = 0.f;
Anthony Barbier7068f992017-10-26 15:23:08 +010088 uint z_index = gl_GlobalInvocationID.z;
Joel Liang09849a02018-01-05 15:12:53 +080089 TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, z_index * weights_stride_w);
90
Anthony Barbier7068f992017-10-26 15:23:08 +010091 float temp[5];
92 float temp_weight[5];
Anthony Barbier7068f992017-10-26 15:23:08 +010093 for(int d = 0; d < int(weights_depth); ++d)
94 {
Joel Liang09849a02018-01-05 15:12:53 +080095 temp = VLOAD5(float[5], src_ptr, IMAGE_OFFSET(src_iter, 0, 0));
96 temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 0, 0));
Anthony Barbier7068f992017-10-26 15:23:08 +010097 pixels += temp[0] * temp_weight[0] + temp[1] * temp_weight[1] + temp[2] * temp_weight[2] + temp[3] * temp_weight[3] + temp[4] * temp_weight[4];
98
Joel Liang09849a02018-01-05 15:12:53 +080099 temp = VLOAD5(float[5], src_ptr, IMAGE_OFFSET(src_iter, 0, 1));
100 temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 1, 0));
Anthony Barbier7068f992017-10-26 15:23:08 +0100101 pixels += temp[0] * temp_weight[0] + temp[1] * temp_weight[1] + temp[2] * temp_weight[2] + temp[3] * temp_weight[3] + temp[4] * temp_weight[4];
102
Joel Liang09849a02018-01-05 15:12:53 +0800103 temp = VLOAD5(float[5], src_ptr, IMAGE_OFFSET(src_iter, 0, 2));
104 temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 2, 0));
Anthony Barbier7068f992017-10-26 15:23:08 +0100105 pixels += temp[0] * temp_weight[0] + temp[1] * temp_weight[1] + temp[2] * temp_weight[2] + temp[3] * temp_weight[3] + temp[4] * temp_weight[4];
106
Joel Liang09849a02018-01-05 15:12:53 +0800107 temp = VLOAD5(float[5], src_ptr, IMAGE_OFFSET(src_iter, 0, 3));
108 temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 3, 0));
Anthony Barbier7068f992017-10-26 15:23:08 +0100109 pixels += temp[0] * temp_weight[0] + temp[1] * temp_weight[1] + temp[2] * temp_weight[2] + temp[3] * temp_weight[3] + temp[4] * temp_weight[4];
110
Joel Liang09849a02018-01-05 15:12:53 +0800111 temp = VLOAD5(float[5], src_ptr, IMAGE_OFFSET(src_iter, 0, 4));
112 temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 4, 0));
Anthony Barbier7068f992017-10-26 15:23:08 +0100113 pixels += temp[0] * temp_weight[0] + temp[1] * temp_weight[1] + temp[2] * temp_weight[2] + temp[3] * temp_weight[3] + temp[4] * temp_weight[4];
114
Joel Liang09849a02018-01-05 15:12:53 +0800115 TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, src_attrs.stride_z);
116 TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, weights_attrs.stride_z);
Anthony Barbier7068f992017-10-26 15:23:08 +0100117 }
118
119#ifdef BIAS
Joel Liang09849a02018-01-05 15:12:53 +0800120 pixels += LOAD(biases_ptr, VECTOR_OFFSET(biases_iter, z_index));
Anthony Barbier7068f992017-10-26 15:23:08 +0100121#endif /* BIAS */
122
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000123#ifdef FUSED_ACTIVATION
124 pixels = ACT_OP(pixels);
125#endif /* FUSED_ACTIVATION */
126
Joel Liang09849a02018-01-05 15:12:53 +0800127 STORE_CURRENT_ITEM(dst_ptr, dst_iter, pixels);
Anthony Barbier7068f992017-10-26 15:23:08 +0100128}
Anthony Barbier7068f992017-10-26 15:23:08 +0100129#elif defined(DATA_TYPE_FP16)
130
Joel Liang09849a02018-01-05 15:12:53 +0800131// Common definitions for DATA_TYPE_FP16
Anthony Barbier7068f992017-10-26 15:23:08 +0100132#if STRIDE_X == 1
Joel Liang09849a02018-01-05 15:12:53 +0800133#define LOAD_SRC_AT_ROW(row) VLOAD2_UNPACK8_HALF(src_ptr, IMAGE_OFFSET(src_iter, 0, row))
Anthony Barbier7068f992017-10-26 15:23:08 +0100134#define CONVOLVE1x5(src, weight) convolve1x5_stride1(src, weight)
135#elif STRIDE_X == 2 /* STRIDE_X == 1 */
Joel Liang09849a02018-01-05 15:12:53 +0800136#define LOAD_SRC_AT_ROW(row) VLOAD3_UNPACK12_HALF(src_ptr, IMAGE_OFFSET(src_iter, 0, row))
Anthony Barbier7068f992017-10-26 15:23:08 +0100137#define CONVOLVE1x5(src, weight) convolve1x5_stride2(src, weight)
138#else /* STRDIDE_X == 1 */
139#error STRIDE_X larger than 2 is not supported
140#endif /* STRIDE_X == 1 */
141
Joel Liang09849a02018-01-05 15:12:53 +0800142#define LOAD_WEIGHT_AT_ROW(row) VLOAD3_UNPACK6_HALF(weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, row, 0))
Anthony Barbier7068f992017-10-26 15:23:08 +0100143
Anthony Barbier7068f992017-10-26 15:23:08 +0100144vec4 convolve1x5_stride1(vec4 tmp[2], vec2 w[3])
145{
146 vec4 src0 = tmp[0];
147 vec4 src1 = vec4(tmp[0].yzw, tmp[1].x);
148 vec4 src2 = vec4(tmp[0].zw, tmp[1].xy);
149 vec4 src3 = vec4(tmp[0].w, tmp[1].xyz);
150 vec4 src4 = tmp[1];
151 vec4 ret = src0 * w[0].x + src1 * w[0].y + src2 * w[1].x + src3 * w[1].y + src4 * w[2].x;
152
153 return ret;
154}
155
156vec4 convolve1x5_stride2(vec4 tmp[3], vec2 w[3])
157{
158 vec4 src0 = vec4(tmp[0].xz, tmp[1].xz);
159 vec4 src1 = vec4(tmp[0].yw, tmp[1].yw);
160 vec4 src2 = vec4(tmp[0].z, tmp[1].xz, tmp[2].x);
161 vec4 src3 = vec4(tmp[0].w, tmp[1].yw, tmp[2].y);
162 vec4 src4 = vec4(tmp[1].x, tmp[1].z, tmp[2].xz);
163 vec4 ret = src0 * w[0].x + src1 * w[0].y + src2 * w[1].x + src3 * w[1].y + src4 * w[2].x;
164
165 return ret;
166}
167
Joel Liang09849a02018-01-05 15:12:53 +0800168#if defined(PROCESS_4X_1Y_1Z)
169TENSOR_DECLARATION(1, srcBuffer, uvec2, src_ptr, src_shift, 3, readonly);
170TENSOR_DECLARATION(2, dstBuffer, uvec2, dst_ptr, dst_shift, 3, writeonly);
171TENSOR_DECLARATION(3, weightsBuffer, uint, weights_ptr, weights_shift, 2, readonly);
Anthony Barbier7068f992017-10-26 15:23:08 +0100172#ifdef BIAS
Joel Liang09849a02018-01-05 15:12:53 +0800173TENSOR_DECLARATION(4, biasesBuffer, uint, biases_ptr, biases_shift, 2, readonly);
Anthony Barbier7068f992017-10-26 15:23:08 +0100174#endif /* BIAS */
175
Joel Liang09849a02018-01-05 15:12:53 +0800176void main()
177{
178 ImageIterator src_iter = CONVERT_TO_IMAGE_ITERATOR(src_attrs, src_shift);
179 Tensor3DIterator weights_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(weights_attrs, weights_shift);
180 Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);
Anthony Barbier7068f992017-10-26 15:23:08 +0100181
Joel Liang09849a02018-01-05 15:12:53 +0800182#ifdef BIAS
183 VectorIterator biases_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(biases_attrs, biases_shift);
184#endif /* BIAS */
185
186 vec4 res = vec4(0);
187 vec2 w[3];
188 vec4 s[STRIDE_X + 1];
189
190 uint z_index = gl_GlobalInvocationID.z;
191 TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, z_index * weights_stride_w);
Anthony Barbier7068f992017-10-26 15:23:08 +0100192
193 for(int d = 0; d < int(weights_depth); ++d)
194 {
195 for(int row = 0; row < 5; row++)
196 {
Joel Liang09849a02018-01-05 15:12:53 +0800197 w = LOAD_WEIGHT_AT_ROW(row);
198 s = LOAD_SRC_AT_ROW(row);
Anthony Barbier7068f992017-10-26 15:23:08 +0100199 res += CONVOLVE1x5(s, w);
200 }
201
Joel Liang09849a02018-01-05 15:12:53 +0800202 TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, src_attrs.stride_z);
203 TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, weights_attrs.stride_z);
Anthony Barbier7068f992017-10-26 15:23:08 +0100204 }
205
206#ifdef BIAS
Joel Liang09849a02018-01-05 15:12:53 +0800207 vec2 vec2_b;
Anthony Barbier7068f992017-10-26 15:23:08 +0100208 float b;
209
Joel Liang09849a02018-01-05 15:12:53 +0800210 vec2_b = LOAD_UNPACK2_HALF(biases_ptr, VECTOR_OFFSET(biases_iter, z_index));
211 b = (z_index % uint(2) == uint(0)) ? vec2_b.x : vec2_b.y;
Anthony Barbier7068f992017-10-26 15:23:08 +0100212 res += vec4(b);
213#endif /* BIAS */
214
Isabella Gottardi3f217ec2018-02-12 14:59:19 +0000215#ifdef FUSED_ACTIVATION
216 res = ACT_OP(res);
217#endif /* FUSED_ACTIVATION */
218
Joel Liang09849a02018-01-05 15:12:53 +0800219 STORE_PACK4_CURRENT_ITEM_HALF(dst_ptr, dst_iter, res);
Anthony Barbier7068f992017-10-26 15:23:08 +0100220}
221
Joel Liang09849a02018-01-05 15:12:53 +0800222#endif /* PROCESS_nX_nY_nZ */
223#else /* DATA_TYPE_FP32 */
Anthony Barbier7068f992017-10-26 15:23:08 +0100224#error Data type not supported
Joel Liang09849a02018-01-05 15:12:53 +0800225#endif /* DATA_TYPE_FP32 */