blob: 4bfac282e259dc01035f2f1fb75aabcc7ee238ef [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
zhenglin57b20102018-01-05 14:39:50 +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;
Anthony Barbier7068f992017-10-26 15:23:08 +010026
zhenglin57b20102018-01-05 14:39:50 +080027#include "helpers_cs.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010028
zhenglin57b20102018-01-05 14:39:50 +080029#if defined(DATA_TYPE_FP16)
Anthony Barbier7068f992017-10-26 15:23:08 +010030precision mediump float;
zhenglin57b20102018-01-05 14:39:50 +080031#endif // DATA_TYPE_FP16
Anthony Barbier7068f992017-10-26 15:23:08 +010032
33#ifdef IM2COL_GENERIC
34/** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM.
35 *
36 * @note The data type must be passed at compile time using "#define DATA_TYPE_FP32"
37 * @note In case biases will be added to the convolution "#define HAS_BIAS" has to be passed to append the final matrix with 1 in each row.
38 *
zhenglin57b20102018-01-05 14:39:50 +080039 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
40 * @param[in] src_attrs The attributes of the source tensor
41 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
42 * @param[in] dst_attrs The attributes of the destination tensor
43 * @param[in] filter_depth The depth of the used filter
44 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes).
45 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes).
Anthony Barbier7068f992017-10-26 15:23:08 +010046 */
zhenglin57b20102018-01-05 14:39:50 +080047SHADER_PARAMS_DECLARATION
48{
49 Tensor3DAttributes src_attrs;
50 ImageAttributes dst_attrs;
51 uint filter_depth;
52 uint src_stride_w;
53 uint dst_stride_w;
54};
55
56#ifdef DATA_TYPE_FP32
57TENSOR_DECLARATION(1, srcBuffer, float, src_ptr, src_shift, 2, readonly);
58TENSOR_DECLARATION(2, dstBuffer, float, dst_ptr, dst_shift, 2, restrict);
Anthony Barbier7068f992017-10-26 15:23:08 +010059void main(void)
60{
zhenglin57b20102018-01-05 14:39:50 +080061 Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(src_attrs, src_shift);
62 ImageIterator dst_iter = CONVERT_TO_IMAGE_ITERATOR_NO_STEP(dst_attrs, dst_shift);
63
Anthony Barbier7068f992017-10-26 15:23:08 +010064 uint xc = gl_GlobalInvocationID.x; // x coordinate in the convolved tensor
65 uint yc = gl_GlobalInvocationID.y; // y coordinate in the convolved tensor
66 uint ch = gl_GlobalInvocationID.z % filter_depth; // input feature map
67 uint batch = gl_GlobalInvocationID.z / filter_depth; // the batch
68
69 // Calculate input indeces
70 uint xi = xc * uint(STRIDE_X) - uint(PAD_X);
71 uint yi = yc * uint(STRIDE_Y) - uint(PAD_Y);
zhenglin57b20102018-01-05 14:39:50 +080072 uint input_offset = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, (ch * src_attrs.stride_z) + (batch * src_stride_w));
Anthony Barbier7068f992017-10-26 15:23:08 +010073
74 // Calculate output indeces
75 uint xo = ch * uint(KERNEL_WIDTH) * uint(KERNEL_HEIGHT);
76 uint yo = xc + yc * uint(CONVOLVED_WIDTH); // Index of the convolution
zhenglin57b20102018-01-05 14:39:50 +080077 uint output_offset = TENSOR_OFFSET_ADVANCE_IN_BYTES(dst_iter, (yo * dst_attrs.stride_y) + (batch * dst_stride_w) + xo);
Anthony Barbier7068f992017-10-26 15:23:08 +010078
79 // Linearize convolution elements
80 for(uint y = yi, y_e = yi + uint(KERNEL_HEIGHT); y < y_e; ++y)
81 {
82 for(uint x = xi, x_e = xi + uint(KERNEL_WIDTH); x < x_e; ++x)
83 {
84#if PAD_X == 0 && PAD_Y == 0
zhenglin57b20102018-01-05 14:39:50 +080085 output_offset = input_offset + ((x * src_attrs.stride_x + y * src_attrs.stride_y) >> uint(2));
86 STORE(dst_ptr, output_offset, LOAD(src_ptr, input_offset));
87
Anthony Barbier7068f992017-10-26 15:23:08 +010088#else // PAD_X == 0 && PAD_Y == 0
89 if(x < 0 || x >= SRC_WIDTH || y < 0 || y >= SRC_HEIGHT)
90 {
zhenglin57b20102018-01-05 14:39:50 +080091 STORE(dst_ptr, output_offset, 0.0f);
Anthony Barbier7068f992017-10-26 15:23:08 +010092 }
93 else
94 {
zhenglin57b20102018-01-05 14:39:50 +080095 output_offset = input_offset + (x * srcs_attrs.stride_x + y * src_attrs.stride_y) >> uint(2));
96 STORE(dst_ptr, output_offset, LOAD(src_ptr, input_offset));
Anthony Barbier7068f992017-10-26 15:23:08 +010097 }
98#endif // PAD_X == 0 && PAD_Y == 0
99 }
100 }
101
102#ifdef HAS_BIAS
103 if(ch == (uint(KERNEL_DEPTH) - 1))
104 {
zhenglin57b20102018-01-05 14:39:50 +0800105 STORE(dst_ptr, output_offset, 1.0f);
Anthony Barbier7068f992017-10-26 15:23:08 +0100106 }
107#endif // HAS_BIAS
108}
zhenglin57b20102018-01-05 14:39:50 +0800109
110#elif defined(DATA_TYPE_FP16)
111TENSOR_DECLARATION(1, srcBuffer, uint, src_ptr, src_shift, 2, readonly);
112TENSOR_DECLARATION(2, dstBuffer, uint, dst_ptr, dst_shift, 2, writeonly);
113
114void main(void)
115{
116}
117
118#else /* DATA_TYPE_FP32 */
119#error Data type not supported
120#endif /* DATA_TYPE_FP32 */
121#endif /* IM2COL_GENERIC */
Anthony Barbier7068f992017-10-26 15:23:08 +0100122
123#ifdef IM2COL_REDUCED
124/** This kernel reshapes the tensor's low three dimensions to single row for GEMM operation
125 *
zhenglin57b20102018-01-05 14:39:50 +0800126 * @note The data type must be passed at compile time using "#define DATA_TYPE_FP16"
Anthony Barbier7068f992017-10-26 15:23:08 +0100127 * @note In case biases will be added in late stage, "#define HAS_BIAS" has to be passed to append the final matrix with 1 in each row.
128 *
zhenglin57b20102018-01-05 14:39:50 +0800129 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
130 * @param[in] src_attrs The attributes of the source tensor
131 * @param[out] dst_ptr Pointer to the destination tensor. Same as @p src_ptr
132 * @param[in] dst_attrs The attributes of the destination tensor
133 * @param[in] width The width of the input tensor
134 * @param[in] height The height of the input tensor
Anthony Barbier7068f992017-10-26 15:23:08 +0100135 */
zhenglin57b20102018-01-05 14:39:50 +0800136SHADER_PARAMS_DECLARATION
137{
138 Tensor3DAttributes src_attrs;
139 VectorAttributes dst_attrs;
140 uint width;
141 uint height;
142};
143
144#ifdef DATA_TYPE_FP32
145TENSOR_DECLARATION(1, srcBuffer, float, src_ptr, src_shift, 2, readonly);
146TENSOR_DECLARATION(2, dstBuffer, float, dst_ptr, dst_shift, 2, restrict);
147
Anthony Barbier7068f992017-10-26 15:23:08 +0100148void main(void)
149{
zhenglin57b20102018-01-05 14:39:50 +0800150 Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift);
151 VectorIterator dst_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(dst_attrs, dst_shift);
Anthony Barbier7068f992017-10-26 15:23:08 +0100152
zhenglin57b20102018-01-05 14:39:50 +0800153 uvec3 pos = uvec3(gl_GlobalInvocationID.xyz);
154 uvec3 size = uvec3(gl_WorkGroupSize.xyz);
155 uint image_size = width * height;
156 uint tmp_out_offset = VECTOR_OFFSET(dst_iter, pos.x + pos.y * width + pos.z * image_size);
157
158 STORE(dst_ptr, tmp_out_offset, LOAD_CURRENT_ITEM(src_ptr, src_iter));
Anthony Barbier7068f992017-10-26 15:23:08 +0100159
160#ifdef HAS_BIAS
161 // If it is the last thread in the 3 dimensional workgroup
162 if(pos.x == (size.x - 1) && pos.y == (size.y - 1) && pos.z == (size.z - 1))
163 {
zhenglin57b20102018-01-05 14:39:50 +0800164 tmp_out_offset += (dst_attrs.stride_x >> uint(2));
165 STORE(dst_ptr, tmp_out_offset, 1.f);
Anthony Barbier7068f992017-10-26 15:23:08 +0100166 }
167#endif // HAS_BIAS
168}
zhenglin57b20102018-01-05 14:39:50 +0800169
170#elif defined(DATA_TYPE_FP16)
171
172#if defined(IM2COL_REDUCED_8X)
173TENSOR_DECLARATION(1, srcBuffer, uvec4, src_ptr, src_shift, 4, readonly);
174TENSOR_DECLARATION(2, dstBuffer, uvec4, dst_ptr, dst_shift, 4, restrict);
175#elif defined(IM2COL_REDUCED_4X) /* IM2COL_REDUCED_8X */
176TENSOR_DECLARATION(1, srcBuffer, uvec2, src_ptr, src_shift, 3, readonly);
177TENSOR_DECLARATION(2, dstBuffer, uvec2, dst_ptr, dst_shift, 3, restrict);
178#else /* IM2COL_REDUCED_8X */
179TENSOR_DECLARATION(1, srcBuffer, uint, src_ptr, src_shift, 2, readonly);
180TENSOR_DECLARATION(2, dstBuffer, uint, dst_ptr, dst_shift, 2, restrict);
181#endif /* IM2COL_REDUCED_8X */
182
183#if defined(IM2COL_REDUCED_GENERIC)
184void main(void)
185{
186 Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift);
187 Tensor3DIterator src_nostep_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(src_attrs, src_shift);
188 VectorIterator dst_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(dst_attrs, dst_shift);
189
190 uvec3 pos = uvec3(gl_GlobalInvocationID.xyz);
191 uvec3 size = uvec3(gl_WorkGroupSize.xyz);
192 uint image_size = width * height;
193 uint element_count = src_attrs.step_x / src_attrs.stride_x;
194 uint tmp_out_offset = VECTOR_OFFSET(dst_iter, pos.x * element_count + pos.y * width + pos.z * image_size);
195 uint width_fp16 = (width + uint(1)) >> uint(1);
196 uint tmp;
197
198 // odd width
199 if(width % uint(2) != uint(0))
200 {
201 // even row
202 if((pos.y + pos.z * height) % uint(2) == uint(0))
203 {
204 tmp = LOAD_CURRENT_ITEM(src_ptr, src_iter);
205 STORE(dst_ptr, tmp_out_offset, tmp);
206 }
207 else
208 {
209 // special op
210 uint tmpleft = uint(0);
211 uint tmpright = uint(0);
212 tmpright = LOAD_CURRENT_ITEM(src_ptr, src_iter); //right half
213 if(pos.x == uint(0))
214 {
215 tmpleft = LOAD(src_ptr, TENSOR3D_OFFSET(src_nostep_iter, int(width), int(pos.y) - 1, int(pos.z))); //left half
216 tmpright = (tmpleft & uint(0xffff)) + (tmpright << uint(16));
217 }
218 else
219 {
220 tmpleft = LOAD(src_ptr, TENSOR3D_OFFSET(src_nostep_iter, (int(pos.x) - 1) * int(element_count), int(pos.y), int(pos.z)));
221 tmpright = ((tmpleft >> uint(16)) + (tmpright << uint(16)));
222 }
223 STORE(dst_ptr, tmp_out_offset, tmpright);
224 }
225 }
226 else
227 {
228 tmp = LOAD_CURRENT_ITEM(src_ptr, src_iter);
229 STORE(dst_ptr, tmp_out_offset, tmp);
230
231#ifdef HAS_BIAS
232 // If it is the last thread in the 3 dimensional workgroup
233 if(pos.x == (size.x - 1) && pos.y == (size.y - 1) && pos.z == (size.z - 1))
234 {
235 tmp_out_offset += (dst_attrs.stride_x >> dst_shift);
236
237 // FIXME: need odd/even detection for tmp_out_offset?
238 mediump vec2 bias_vec = vec2(1.0f, 1.0f);
239 STORE_PACK2_HALF(dst_ptr, tmp_out_offset, bias_vec);
240 }
241#endif // HAS_BIAS
242 }
243}
244
245#else /* IM2COL_REDUCED_GENERIC */
246void main(void)
247{
248 Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift);
249 VectorIterator dst_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(dst_attrs, dst_shift);
250
251 uvec3 pos = uvec3(gl_GlobalInvocationID.xyz);
252#if defined(IM2COL_REDUCED_8X)
253 uint tmp_out_offset = VECTOR_OFFSET(dst_iter, pos.x * uint(8) + pos.y * width + pos.z * uint(IMAGE_SIZE));
254 uvec4 tmp = LOAD_CURRENT_ITEM(src_ptr, src_iter);
255 STORE(dst_ptr, tmp_out_offset, tmp);
256#elif defined(IM2COL_REDUCED_4X) /* IM2COL_REDUCED_8X */
257 uint tmp_out_offset = VECTOR_OFFSET(dst_iter, pos.x * uint(4) + pos.y * width + pos.z * uint(IMAGE_SIZE));
258 uvec2 tmp = LOAD_CURRENT_ITEM(src_ptr, src_iter);
259 STORE(dst_ptr, tmp_out_offset, tmp);
260#else /* IM2COL_REDUCED_8X */
261 uint tmp_out_offset = VECTOR_OFFSET(dst_iter, pos.x * uint(2) + pos.y * width + pos.z * uint(IMAGE_SIZE));
262 uint tmp = LOAD_CURRENT_ITEM(src_ptr, src_iter);
263 STORE(dst_ptr, tmp_out_offset, tmp);
264#endif /* IM2COL_REDUCED_8X */
265}
266#endif /* IM2COL_REDUCED_GENERIC */
267#else /* DATA_TYPE_FP32 */
268#error Data type not supported
269#endif /* DATA_TYPE_FP32 */
270#endif /* IM2COL_REDUCED */
Anthony Barbier7068f992017-10-26 15:23:08 +0100271
272#ifdef COL2IM
273/** This kernel performs a reshaping of the output of the convolution layer.
274 *
275 * @note The data type must be passed at compile time using "#define DATA_TYPE_FP32"
276 *
zhenglin57b20102018-01-05 14:39:50 +0800277 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
278 * @param[in] src_attrs The attributes of the source tensor
279 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
280 * @param[in] dst_attrs The attributes of the destination tensor
281 * @param[in] width The width of output convolved dimensions
Anthony Barbier7068f992017-10-26 15:23:08 +0100282 */
zhenglin57b20102018-01-05 14:39:50 +0800283SHADER_PARAMS_DECLARATION
284{
285 ImageAttributes src_attrs;
286 Tensor3DAttributes dst_attrs;
287 uint width;
288};
289
290#ifdef DATA_TYPE_FP32
291TENSOR_DECLARATION(1, srcBuffer, float, src_ptr, src_shift, 2, readonly);
292TENSOR_DECLARATION(2, dstBuffer, float, dst_ptr, dst_shift, 2, restrict);
Anthony Barbier7068f992017-10-26 15:23:08 +0100293void main(void)
294{
zhenglin57b20102018-01-05 14:39:50 +0800295 ImageIterator src_iter = CONVERT_TO_IMAGE_ITERATOR(src_attrs, src_shift);
296 Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);
Anthony Barbier7068f992017-10-26 15:23:08 +0100297
zhenglin57b20102018-01-05 14:39:50 +0800298 uvec2 pos = uvec2(gl_GlobalInvocationID.xy);
299 uint tmp_out_offset = TENSOR3D_OFFSET(dst_iter, pos.y % width, pos.y / width, pos.x);
Anthony Barbier7068f992017-10-26 15:23:08 +0100300
zhenglin57b20102018-01-05 14:39:50 +0800301 STORE(dst_ptr, tmp_out_offset, LOAD_CURRENT_ITEM(src_ptr, src_iter));
Anthony Barbier7068f992017-10-26 15:23:08 +0100302}
Anthony Barbier7068f992017-10-26 15:23:08 +0100303
zhenglin57b20102018-01-05 14:39:50 +0800304#elif defined(DATA_TYPE_FP16)
305
306#else /* DATA_TYPE_FP32 */
Anthony Barbier7068f992017-10-26 15:23:08 +0100307#error Data type not supported
zhenglin57b20102018-01-05 14:39:50 +0800308#endif /* DATA_TYPE_FP32 */
309#endif /* COL2IM */