blob: c919e4ed8053f1f294db4e4b33fcbd58a2e65d1d [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Joel Liang09849a02018-01-05 15:12:53 +08002 * 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
Joel Liang09849a02018-01-05 15:12:53 +080029#if defined(DATA_TYPE_FP16)
30precision mediump float;
31#endif // DATA_TYPE_FP16
ASIAPAC\steli0123ac91b2017-11-07 16:14:44 +080032
33/** This kernel performs a direct convolution to convolve the low three dimensions
34 *
Joel Liang09849a02018-01-05 15:12:53 +080035 * @note The data type must be passed at compile time using "#define DATA_TYPE_NAME". e.g. "#define DATA_TYPE_FP32"
36 * @note This kernel has multiple optimized direct convolution options for FP16.
37 * The direct convolution option must be passed at compile time using "#define PROCESS_nX_nY_nZ" e.g. "#define PROCESS_8X_1Y_1Z"
38 * @note The convolution stride x must be passed at compile time using "#define STRIDE_X n" e.g. "#define STRIDE_X 1"
39 * This OpenGL ES shader works with stride_x = 1 and 2
ASIAPAC\steli0123ac91b2017-11-07 16:14:44 +080040 * @note If biases are used then "define HAS_BIAS" has to be passed at compile time
41 *
Joel Liang09849a02018-01-05 15:12:53 +080042 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
43 * @param[in] src_attrs The attributes of the source tensor
44 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
45 * @param[in] dst_attrs The attributes of the destination tensor
46 * @param[out] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr
47 * @param[in] weights_attrs The attributes of the weights tensor
48 * @param[in] biases_ptr Pointer to the biases tensor. Same as @p src_ptr
49 * @param[in] biases_attrs The attributes of the weights tensor
50 * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension
51 * @param[in] weights_depth The third dimensions of the weights tensors
ASIAPAC\steli0123ac91b2017-11-07 16:14:44 +080052 */
Joel Liang09849a02018-01-05 15:12:53 +080053SHADER_PARAMS_DECLARATION
Anthony Barbier7068f992017-10-26 15:23:08 +010054{
Joel Liang09849a02018-01-05 15:12:53 +080055 Tensor3DAttributes src_attrs;
56 Tensor3DAttributes dst_attrs;
57 Tensor3DAttributes weights_attrs;
Anthony Barbier7068f992017-10-26 15:23:08 +010058#ifdef BIAS
Joel Liang09849a02018-01-05 15:12:53 +080059 VectorAttributes biases_attrs;
Anthony Barbier7068f992017-10-26 15:23:08 +010060#endif /* BIAS */
61 uint weights_stride_w;
62 uint weights_depth;
63};
64
Joel Liang09849a02018-01-05 15:12:53 +080065#ifdef DATA_TYPE_FP32
66TENSOR_DECLARATION(1, srcBuffer, float, src_ptr, src_shift, 2, readonly);
67TENSOR_DECLARATION(2, dstBuffer, float, dst_ptr, dst_shift, 2, writeonly);
68TENSOR_DECLARATION(3, weightsBuffer, float, weights_ptr, weights_shift, 2, readonly);
Anthony Barbier7068f992017-10-26 15:23:08 +010069#ifdef BIAS
Joel Liang09849a02018-01-05 15:12:53 +080070TENSOR_DECLARATION(4, biasesBuffer, float, biases_ptr, biases_shift, 2, readonly);
Anthony Barbier7068f992017-10-26 15:23:08 +010071#endif /* BIAS */
72
Anthony Barbier7068f992017-10-26 15:23:08 +010073void main()
74{
Joel Liang09849a02018-01-05 15:12:53 +080075 ImageIterator src_iter = CONVERT_TO_IMAGE_ITERATOR(src_attrs, src_shift);
76 Tensor3DIterator weights_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(weights_attrs, weights_shift);
77 Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);
Anthony Barbier7068f992017-10-26 15:23:08 +010078
79#ifdef BIAS
Joel Liang09849a02018-01-05 15:12:53 +080080 VectorIterator biases_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(biases_attrs, biases_shift);
Anthony Barbier7068f992017-10-26 15:23:08 +010081#endif /* BIAS */
82
Joel Liang09849a02018-01-05 15:12:53 +080083 float pixels = 0.f;
Anthony Barbier7068f992017-10-26 15:23:08 +010084 uint z_index = gl_GlobalInvocationID.z;
Joel Liang09849a02018-01-05 15:12:53 +080085 TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, z_index * weights_stride_w);
86
Anthony Barbier7068f992017-10-26 15:23:08 +010087 float temp[5];
88 float temp_weight[5];
Anthony Barbier7068f992017-10-26 15:23:08 +010089 for(int d = 0; d < int(weights_depth); ++d)
90 {
Joel Liang09849a02018-01-05 15:12:53 +080091 temp = VLOAD5(float[5], src_ptr, IMAGE_OFFSET(src_iter, 0, 0));
92 temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 0, 0));
Anthony Barbier7068f992017-10-26 15:23:08 +010093 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];
94
Joel Liang09849a02018-01-05 15:12:53 +080095 temp = VLOAD5(float[5], src_ptr, IMAGE_OFFSET(src_iter, 0, 1));
96 temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 1, 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, 2));
100 temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 2, 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, 3));
104 temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 3, 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, 4));
108 temp_weight = VLOAD5(float[5], weights_ptr, TENSOR3D_OFFSET(weights_iter, 0, 4, 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 TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, src_attrs.stride_z);
112 TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, weights_attrs.stride_z);
Anthony Barbier7068f992017-10-26 15:23:08 +0100113 }
114
115#ifdef BIAS
Joel Liang09849a02018-01-05 15:12:53 +0800116 pixels += LOAD(biases_ptr, VECTOR_OFFSET(biases_iter, z_index));
Anthony Barbier7068f992017-10-26 15:23:08 +0100117#endif /* BIAS */
118
Joel Liang09849a02018-01-05 15:12:53 +0800119 STORE_CURRENT_ITEM(dst_ptr, dst_iter, pixels);
Anthony Barbier7068f992017-10-26 15:23:08 +0100120}
Anthony Barbier7068f992017-10-26 15:23:08 +0100121#elif defined(DATA_TYPE_FP16)
122
Joel Liang09849a02018-01-05 15:12:53 +0800123// Common definitions for DATA_TYPE_FP16
Anthony Barbier7068f992017-10-26 15:23:08 +0100124#if STRIDE_X == 1
Joel Liang09849a02018-01-05 15:12:53 +0800125#define LOAD_SRC_AT_ROW(row) VLOAD2_UNPACK8_HALF(src_ptr, IMAGE_OFFSET(src_iter, 0, row))
Anthony Barbier7068f992017-10-26 15:23:08 +0100126#define CONVOLVE1x5(src, weight) convolve1x5_stride1(src, weight)
127#elif STRIDE_X == 2 /* STRIDE_X == 1 */
Joel Liang09849a02018-01-05 15:12:53 +0800128#define LOAD_SRC_AT_ROW(row) VLOAD3_UNPACK12_HALF(src_ptr, IMAGE_OFFSET(src_iter, 0, row))
Anthony Barbier7068f992017-10-26 15:23:08 +0100129#define CONVOLVE1x5(src, weight) convolve1x5_stride2(src, weight)
130#else /* STRDIDE_X == 1 */
131#error STRIDE_X larger than 2 is not supported
132#endif /* STRIDE_X == 1 */
133
Joel Liang09849a02018-01-05 15:12:53 +0800134#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 +0100135
Anthony Barbier7068f992017-10-26 15:23:08 +0100136vec4 convolve1x5_stride1(vec4 tmp[2], vec2 w[3])
137{
138 vec4 src0 = tmp[0];
139 vec4 src1 = vec4(tmp[0].yzw, tmp[1].x);
140 vec4 src2 = vec4(tmp[0].zw, tmp[1].xy);
141 vec4 src3 = vec4(tmp[0].w, tmp[1].xyz);
142 vec4 src4 = tmp[1];
143 vec4 ret = src0 * w[0].x + src1 * w[0].y + src2 * w[1].x + src3 * w[1].y + src4 * w[2].x;
144
145 return ret;
146}
147
148vec4 convolve1x5_stride2(vec4 tmp[3], vec2 w[3])
149{
150 vec4 src0 = vec4(tmp[0].xz, tmp[1].xz);
151 vec4 src1 = vec4(tmp[0].yw, tmp[1].yw);
152 vec4 src2 = vec4(tmp[0].z, tmp[1].xz, tmp[2].x);
153 vec4 src3 = vec4(tmp[0].w, tmp[1].yw, tmp[2].y);
154 vec4 src4 = vec4(tmp[1].x, tmp[1].z, tmp[2].xz);
155 vec4 ret = src0 * w[0].x + src1 * w[0].y + src2 * w[1].x + src3 * w[1].y + src4 * w[2].x;
156
157 return ret;
158}
159
Joel Liang09849a02018-01-05 15:12:53 +0800160#if defined(PROCESS_4X_1Y_1Z)
161TENSOR_DECLARATION(1, srcBuffer, uvec2, src_ptr, src_shift, 3, readonly);
162TENSOR_DECLARATION(2, dstBuffer, uvec2, dst_ptr, dst_shift, 3, writeonly);
163TENSOR_DECLARATION(3, weightsBuffer, uint, weights_ptr, weights_shift, 2, readonly);
Anthony Barbier7068f992017-10-26 15:23:08 +0100164#ifdef BIAS
Joel Liang09849a02018-01-05 15:12:53 +0800165TENSOR_DECLARATION(4, biasesBuffer, uint, biases_ptr, biases_shift, 2, readonly);
Anthony Barbier7068f992017-10-26 15:23:08 +0100166#endif /* BIAS */
167
Joel Liang09849a02018-01-05 15:12:53 +0800168void main()
169{
170 ImageIterator src_iter = CONVERT_TO_IMAGE_ITERATOR(src_attrs, src_shift);
171 Tensor3DIterator weights_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(weights_attrs, weights_shift);
172 Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);
Anthony Barbier7068f992017-10-26 15:23:08 +0100173
Joel Liang09849a02018-01-05 15:12:53 +0800174#ifdef BIAS
175 VectorIterator biases_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(biases_attrs, biases_shift);
176#endif /* BIAS */
177
178 vec4 res = vec4(0);
179 vec2 w[3];
180 vec4 s[STRIDE_X + 1];
181
182 uint z_index = gl_GlobalInvocationID.z;
183 TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, z_index * weights_stride_w);
Anthony Barbier7068f992017-10-26 15:23:08 +0100184
185 for(int d = 0; d < int(weights_depth); ++d)
186 {
187 for(int row = 0; row < 5; row++)
188 {
Joel Liang09849a02018-01-05 15:12:53 +0800189 w = LOAD_WEIGHT_AT_ROW(row);
190 s = LOAD_SRC_AT_ROW(row);
Anthony Barbier7068f992017-10-26 15:23:08 +0100191 res += CONVOLVE1x5(s, w);
192 }
193
Joel Liang09849a02018-01-05 15:12:53 +0800194 TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, src_attrs.stride_z);
195 TENSOR_ITERATOR_ADVANCE_IN_BYTES(weights_iter, weights_attrs.stride_z);
Anthony Barbier7068f992017-10-26 15:23:08 +0100196 }
197
198#ifdef BIAS
Joel Liang09849a02018-01-05 15:12:53 +0800199 vec2 vec2_b;
Anthony Barbier7068f992017-10-26 15:23:08 +0100200 float b;
201
Joel Liang09849a02018-01-05 15:12:53 +0800202 vec2_b = LOAD_UNPACK2_HALF(biases_ptr, VECTOR_OFFSET(biases_iter, z_index));
203 b = (z_index % uint(2) == uint(0)) ? vec2_b.x : vec2_b.y;
Anthony Barbier7068f992017-10-26 15:23:08 +0100204 res += vec4(b);
205#endif /* BIAS */
206
Joel Liang09849a02018-01-05 15:12:53 +0800207 STORE_PACK4_CURRENT_ITEM_HALF(dst_ptr, dst_iter, res);
Anthony Barbier7068f992017-10-26 15:23:08 +0100208}
209
Joel Liang09849a02018-01-05 15:12:53 +0800210#endif /* PROCESS_nX_nY_nZ */
211#else /* DATA_TYPE_FP32 */
Anthony Barbier7068f992017-10-26 15:23:08 +0100212#error Data type not supported
Joel Liang09849a02018-01-05 15:12:53 +0800213#endif /* DATA_TYPE_FP32 */