blob: 25c129d0aa3a3c659462029a8a13f90ecb376b0e [file] [log] [blame]
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +00001/*
2 * Copyright (c) 2018 ARM Limited.
3 *
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#include "helpers.h"
25
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000026#if defined(NUM_CHANNELS)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +000027
Gian Marco Iodiced2fab732018-03-02 11:18:12 +000028/** This OpenCL kernel performs Winograd filter transform 3x3 when the data format is NCHW and the output tile is 2x2
29 *
30 * @note The number of channels must be passed at compile time using -DNUM_CHANNELS: e.g. -DNUM_CHANNELS=64
31 *
32 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F32
33 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
34 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
35 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
36 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
37 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
38 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
39 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes)
40 * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
41 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
42 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
43 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
44 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
45 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
46 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
47 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
48 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
49 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
50 */
51__kernel void winograd_filter_transform_2x2_3x3_nchw(
52 TENSOR4D_DECLARATION(src),
53 TENSOR3D_DECLARATION(dst))
54{
55 Tensor4D src = CONVERT_TO_TENSOR4D_STRUCT(src, NUM_CHANNELS);
56
57 const __global uchar *src_addr = tensor4D_offset(&src, 0, 0, 0, 0);
58
59 // Load the values from the input tensor
60 float3 w0 = vload3(0, (__global float *)(src_addr + 0 * src_stride_y));
61 float3 w1 = vload3(0, (__global float *)(src_addr + 1 * src_stride_y));
62 float3 w2 = vload3(0, (__global float *)(src_addr + 2 * src_stride_y));
63
64 // Transform the 3x3 tile in a 4x4 tile
65 float4 out0 = 0.0f;
66 float4 out1 = 0.0f;
67 float4 out2 = 0.0f;
68 float4 out3 = 0.0f;
69
70 // Row 0
71 out0.s0 = (w0.s0);
72 out0.s1 = (w0.s0 + w0.s1 + w0.s2) * 0.5f;
73 out0.s2 = (w0.s0 + w0.s2 - w0.s1) * 0.5f;
74 out0.s3 = (w0.s2);
75
76 // Row 1
77 out1.s0 = (w0.s0 + w1.s0 + w2.s0) * 0.5f;
78 out1.s1 = (w0.s0 + w1.s0 + w2.s0 + w0.s1 + w1.s1 + w2.s1 + w0.s2 + w1.s2 + w2.s2) * 0.25f;
79 out1.s2 = (w0.s0 + w1.s0 + w2.s0 + w0.s2 + w1.s2 + w2.s2 - w0.s1 - w1.s1 - w2.s1) * 0.25f;
80 out1.s3 = (w0.s2 + w1.s2 + w2.s2) * 0.5f;
81
82 // Row 2
83 out2.s0 = (w0.s0 + w2.s0 - w1.s0) * 0.5f;
84 out2.s1 = (w0.s0 + w2.s0 + w0.s1 + w2.s1 + w0.s2 + w2.s2 - w1.s0 - w1.s1 - w1.s2) * 0.25f;
85 out2.s2 = (w0.s0 + w2.s0 + w1.s1 + w0.s2 + w2.s2 - w1.s0 - w0.s1 - w2.s1 - w1.s2) * 0.25f;
86 out2.s3 = (w0.s2 + w2.s2 - w1.s2) * 0.5f;
87
88 // Row 3
89 out3.s0 = (w2.s0);
90 out3.s1 = (w2.s0 + w2.s1 + w2.s2) * 0.5f;
91 out3.s2 = (w2.s0 + w2.s2 - w2.s1) * 0.5f;
92 out3.s3 = (w2.s2);
93
94 int z = get_global_id(2);
95 int x0 = z / NUM_CHANNELS; // idx filter
96 int y0 = z % NUM_CHANNELS; // idx channel
97
98 // Get output address
99 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x0 * dst_stride_x + y0 * dst_stride_y;
100
101 // Store the 16 values across the 16 channels
102 *(__global float *)(dst_addr + 0 * dst_stride_z) = out0.s0;
103 *(__global float *)(dst_addr + 1 * dst_stride_z) = out0.s1;
104 *(__global float *)(dst_addr + 2 * dst_stride_z) = out0.s2;
105 *(__global float *)(dst_addr + 3 * dst_stride_z) = out0.s3;
106 *(__global float *)(dst_addr + 4 * dst_stride_z) = out1.s0;
107 *(__global float *)(dst_addr + 5 * dst_stride_z) = out1.s1;
108 *(__global float *)(dst_addr + 6 * dst_stride_z) = out1.s2;
109 *(__global float *)(dst_addr + 7 * dst_stride_z) = out1.s3;
110 *(__global float *)(dst_addr + 8 * dst_stride_z) = out2.s0;
111 *(__global float *)(dst_addr + 9 * dst_stride_z) = out2.s1;
112 *(__global float *)(dst_addr + 10 * dst_stride_z) = out2.s2;
113 *(__global float *)(dst_addr + 11 * dst_stride_z) = out2.s3;
114 *(__global float *)(dst_addr + 12 * dst_stride_z) = out3.s0;
115 *(__global float *)(dst_addr + 13 * dst_stride_z) = out3.s1;
116 *(__global float *)(dst_addr + 14 * dst_stride_z) = out3.s2;
117 *(__global float *)(dst_addr + 15 * dst_stride_z) = out3.s3;
118}
119#endif // defined(NUM_CHANNELS)
120
121#if defined(NUM_TILES_X) && defined(PAD_LEFT) && defined(PAD_TOP)
Giorgio Arena1f9ca1d2018-03-01 11:13:45 +0000122/** This OpenCL kernel computes the input transform when the kernel size is 3x3 and the output tile is 2x2
123 *
124 * @note The number of tiles in the x axis must be passed at compile time using -DNUM_TILES_X (i.e.-DNUM_TILES_X=5).
125 * @note The pad left and pad top must be passed at compile time using -DPAD_LEFT and -DPAD_TOP (i.e.-DPAD_LEFT=1 and -DPAD_TOP=0).
126 *
127 * @param[in] src_ptr Pointer to the source image. Supported data types: F32
128 * @param[in] src_stride_x Stride of the source image in X dimension (in bytes)
129 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
130 * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes)
131 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
132 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image
133 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
134 * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes)
135 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: as @p src_ptr
136 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
137 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
138 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
139 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
140 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
141 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
142 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
143 */
144__kernel void winograd_input_transform_2x2_3x3_stepz1_nchw(
145 TENSOR3D_DECLARATION(src),
146 TENSOR3D_DECLARATION(dst))
147{
148 int x = get_global_id(0);
149 int y = get_global_id(1);
150 int z = get_global_id(2);
151
152 // Compute input address
153 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * 2 * src_stride_x + y * 2 * src_stride_y + z * src_stride_z;
154
155 src_addr = src_addr - ((int)PAD_LEFT * src_stride_x) - ((int)PAD_TOP * src_stride_y);
156
157 float4 in_row0 = vload4(0, (__global float *)(src_addr + 0 * src_stride_y));
158 float4 in_row1 = vload4(0, (__global float *)(src_addr + 1 * src_stride_y));
159 float4 in_row2 = vload4(0, (__global float *)(src_addr + 2 * src_stride_y));
160 float4 in_row3 = vload4(0, (__global float *)(src_addr + 3 * src_stride_y));
161
162 float4 tmp0 = in_row0 - in_row2;
163 float4 tmp1 = in_row1 + in_row2;
164 float4 tmp2 = in_row2 - in_row1;
165 float4 tmp3 = in_row1 - in_row3;
166
167 float out00 = tmp0.s0 - tmp0.s2;
168 float out01 = tmp0.s1 + tmp0.s2;
169 float out02 = tmp0.s2 - tmp0.s1;
170 float out03 = tmp0.s1 - tmp0.s3;
171
172 float out10 = tmp1.s0 - tmp1.s2;
173 float out11 = tmp1.s1 + tmp1.s2;
174 float out12 = tmp1.s2 - tmp1.s1;
175 float out13 = tmp1.s1 - tmp1.s3;
176
177 float out20 = tmp2.s0 - tmp2.s2;
178 float out21 = tmp2.s1 + tmp2.s2;
179 float out22 = tmp2.s2 - tmp2.s1;
180 float out23 = tmp2.s1 - tmp2.s3;
181
182 float out30 = tmp3.s0 - tmp3.s2;
183 float out31 = tmp3.s1 + tmp3.s2;
184 float out32 = tmp3.s2 - tmp3.s1;
185 float out33 = tmp3.s1 - tmp3.s3;
186
187 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + z * dst_stride_x + (x + y * (int)NUM_TILES_X) * dst_stride_y;
188
189 *((__global float *)(dst_addr + 0 * dst_stride_z)) = out00;
190 *((__global float *)(dst_addr + 1 * dst_stride_z)) = out01;
191 *((__global float *)(dst_addr + 2 * dst_stride_z)) = out02;
192 *((__global float *)(dst_addr + 3 * dst_stride_z)) = out03;
193 *((__global float *)(dst_addr + 4 * dst_stride_z)) = out10;
194 *((__global float *)(dst_addr + 5 * dst_stride_z)) = out11;
195 *((__global float *)(dst_addr + 6 * dst_stride_z)) = out12;
196 *((__global float *)(dst_addr + 7 * dst_stride_z)) = out13;
197 *((__global float *)(dst_addr + 8 * dst_stride_z)) = out20;
198 *((__global float *)(dst_addr + 9 * dst_stride_z)) = out21;
199 *((__global float *)(dst_addr + 10 * dst_stride_z)) = out22;
200 *((__global float *)(dst_addr + 11 * dst_stride_z)) = out23;
201 *((__global float *)(dst_addr + 12 * dst_stride_z)) = out30;
202 *((__global float *)(dst_addr + 13 * dst_stride_z)) = out31;
203 *((__global float *)(dst_addr + 14 * dst_stride_z)) = out32;
204 *((__global float *)(dst_addr + 15 * dst_stride_z)) = out33;
205}
206
207/** This OpenCL kernel computes the input transform when the kernel size is 3x3, the output tile is 2x2 and the number of channels is multiple of 2
208 *
209 * @note The number of tiles in the x axis must be passed at compile time using -DNUM_TILES_X (i.e.-DNUM_TILES_X=5).
210 * @note The pad left and pad top must be passed at compile time using -DPAD_LEFT and -DPAD_TOP (i.e.-DPAD_LEFT=1 and -DPAD_TOP=0).
211 *
212 * @param[in] src_ptr Pointer to the source image. Supported data types: F32
213 * @param[in] src_stride_x Stride of the source image in X dimension (in bytes)
214 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
215 * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes)
216 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
217 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image
218 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
219 * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes)
220 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: as @p src_ptr
221 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
222 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
223 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
224 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
225 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
226 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
227 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
228 */
229__kernel void winograd_input_transform_2x2_3x3_stepz2_nchw(
230 TENSOR3D_DECLARATION(src),
231 TENSOR3D_DECLARATION(dst))
232{
233 int x = get_global_id(0);
234 int y = get_global_id(1);
235 int z = get_global_id(2) * 2;
236
237 // Compute input address
238 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * 2 * src_stride_x + y * 2 * src_stride_y + z * src_stride_z;
239
240 src_addr = src_addr - ((int)PAD_LEFT * src_stride_x) - ((int)PAD_TOP * src_stride_y);
241
242 float4 in_row0 = vload4(0, (__global float *)(src_addr + 0 * src_stride_y));
243 float4 in_row1 = vload4(0, (__global float *)(src_addr + 1 * src_stride_y));
244 float4 in_row2 = vload4(0, (__global float *)(src_addr + 2 * src_stride_y));
245 float4 in_row3 = vload4(0, (__global float *)(src_addr + 3 * src_stride_y));
246
247 src_addr += src_stride_z;
248 float4 in_row4 = vload4(0, (__global float *)(src_addr + 0 * src_stride_y));
249 float4 in_row5 = vload4(0, (__global float *)(src_addr + 1 * src_stride_y));
250 float4 in_row6 = vload4(0, (__global float *)(src_addr + 2 * src_stride_y));
251 float4 in_row7 = vload4(0, (__global float *)(src_addr + 3 * src_stride_y));
252
253 float4 tmp0 = in_row0 - in_row2;
254 float4 tmp1 = in_row1 + in_row2;
255 float4 tmp2 = in_row2 - in_row1;
256 float4 tmp3 = in_row1 - in_row3;
257
258 float4 tmp4 = in_row4 - in_row6;
259 float4 tmp5 = in_row5 + in_row6;
260 float4 tmp6 = in_row6 - in_row5;
261 float4 tmp7 = in_row5 - in_row7;
262
263 float2 out00 = (float2)(tmp0.s0 - tmp0.s2, tmp4.s0 - tmp4.s2);
264 float2 out01 = (float2)(tmp0.s1 + tmp0.s2, tmp4.s1 + tmp4.s2);
265 float2 out02 = (float2)(tmp0.s2 - tmp0.s1, tmp4.s2 - tmp4.s1);
266 float2 out03 = (float2)(tmp0.s1 - tmp0.s3, tmp4.s1 - tmp4.s3);
267
268 float2 out10 = (float2)(tmp1.s0 - tmp1.s2, tmp5.s0 - tmp5.s2);
269 float2 out11 = (float2)(tmp1.s1 + tmp1.s2, tmp5.s1 + tmp5.s2);
270 float2 out12 = (float2)(tmp1.s2 - tmp1.s1, tmp5.s2 - tmp5.s1);
271 float2 out13 = (float2)(tmp1.s1 - tmp1.s3, tmp5.s1 - tmp5.s3);
272
273 float2 out20 = (float2)(tmp2.s0 - tmp2.s2, tmp6.s0 - tmp6.s2);
274 float2 out21 = (float2)(tmp2.s1 + tmp2.s2, tmp6.s1 + tmp6.s2);
275 float2 out22 = (float2)(tmp2.s2 - tmp2.s1, tmp6.s2 - tmp6.s1);
276 float2 out23 = (float2)(tmp2.s1 - tmp2.s3, tmp6.s1 - tmp6.s3);
277
278 float2 out30 = (float2)(tmp3.s0 - tmp3.s2, tmp7.s0 - tmp7.s2);
279 float2 out31 = (float2)(tmp3.s1 + tmp3.s2, tmp7.s1 + tmp7.s2);
280 float2 out32 = (float2)(tmp3.s2 - tmp3.s1, tmp7.s2 - tmp7.s1);
281 float2 out33 = (float2)(tmp3.s1 - tmp3.s3, tmp7.s1 - tmp7.s3);
282
283 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + z * dst_stride_x + (x + y * (int)NUM_TILES_X) * dst_stride_y;
284
285 vstore2(out00, 0, (__global float *)(dst_addr + 0 * dst_stride_z));
286 vstore2(out01, 0, (__global float *)(dst_addr + 1 * dst_stride_z));
287 vstore2(out02, 0, (__global float *)(dst_addr + 2 * dst_stride_z));
288 vstore2(out03, 0, (__global float *)(dst_addr + 3 * dst_stride_z));
289 vstore2(out10, 0, (__global float *)(dst_addr + 4 * dst_stride_z));
290 vstore2(out11, 0, (__global float *)(dst_addr + 5 * dst_stride_z));
291 vstore2(out12, 0, (__global float *)(dst_addr + 6 * dst_stride_z));
292 vstore2(out13, 0, (__global float *)(dst_addr + 7 * dst_stride_z));
293 vstore2(out20, 0, (__global float *)(dst_addr + 8 * dst_stride_z));
294 vstore2(out21, 0, (__global float *)(dst_addr + 9 * dst_stride_z));
295 vstore2(out22, 0, (__global float *)(dst_addr + 10 * dst_stride_z));
296 vstore2(out23, 0, (__global float *)(dst_addr + 11 * dst_stride_z));
297 vstore2(out30, 0, (__global float *)(dst_addr + 12 * dst_stride_z));
298 vstore2(out31, 0, (__global float *)(dst_addr + 13 * dst_stride_z));
299 vstore2(out32, 0, (__global float *)(dst_addr + 14 * dst_stride_z));
300 vstore2(out33, 0, (__global float *)(dst_addr + 15 * dst_stride_z));
301}
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000302#endif // defined(NUM_TILES_X) && defined(PAD_LEFT) && defined(PAD_TOP)
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000303
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000304#if defined(NUM_TILES_X)
305/** This OpenCL kernel performs Winograd output transform when the output tile is 2x2, the filter size 3x3 and the data format is NCHW
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000306 *
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000307 * @note The number of tiles along the X direction must be passed at compile time using -DNUM_TILES_X: e.g. -DNUM_TILES_X=16
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000308 *
309 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F32
310 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
311 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
312 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
313 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
314 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
315 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000316 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
317 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
318 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
319 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
320 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
321 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
322 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
323 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
324 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
325 */
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000326__kernel void winograd_output_transform_2x2_3x3_nchw(
327 TENSOR3D_DECLARATION(src),
328 TENSOR3D_DECLARATION(dst)
329#if defined(HAS_BIAS)
330 ,
331 VECTOR_DECLARATION(bias)
332#endif // defined(HAS_BIAS)
333)
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000334{
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000335 // Each thread stores a 2x2 tile
336 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000337
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000338 const __global uchar *src_addr = tensor3D_offset(&src, 0, 0, 0);
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000339
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000340 // Load the values across the 16 channels to compose the 4x4 tile
341 float d00 = *((__global float *)(src_addr + 0 * src_stride_z));
342 float d01 = *((__global float *)(src_addr + 1 * src_stride_z));
343 float d02 = *((__global float *)(src_addr + 2 * src_stride_z));
344 float d03 = *((__global float *)(src_addr + 3 * src_stride_z));
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000345
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000346 float d10 = *((__global float *)(src_addr + 4 * src_stride_z));
347 float d11 = *((__global float *)(src_addr + 5 * src_stride_z));
348 float d12 = *((__global float *)(src_addr + 6 * src_stride_z));
349 float d13 = *((__global float *)(src_addr + 7 * src_stride_z));
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000350
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000351 float d20 = *((__global float *)(src_addr + 8 * src_stride_z));
352 float d21 = *((__global float *)(src_addr + 9 * src_stride_z));
353 float d22 = *((__global float *)(src_addr + 10 * src_stride_z));
354 float d23 = *((__global float *)(src_addr + 11 * src_stride_z));
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000355
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000356 float d30 = *((__global float *)(src_addr + 12 * src_stride_z));
357 float d31 = *((__global float *)(src_addr + 13 * src_stride_z));
358 float d32 = *((__global float *)(src_addr + 14 * src_stride_z));
359 float d33 = *((__global float *)(src_addr + 15 * src_stride_z));
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000360
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000361 // Compute the 2x2 output tile
362 float k0 = d01 + d11 + d21;
363 float k1 = d02 + d12 + d22;
364 float k2 = d11 - d21 - d31;
365 float k3 = d12 - d22 - d32;
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000366
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000367 // out00 = d00 + d10 + d20 + d01 + d11 + d21 + d02 + d12 + d22
368 // out01 = d01 + d11 + d21 - (d02 + d12 + d22) - (d03 + d13 + d23)
369 // out10 = d10 - d20 - d30 + (d11 - d21 - d31) + (d12 - d22 - d32)
370 // out11 = d11 - d21 - d31 - (d12 - d22 - d32) - (d13 - d23 - d33)
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000371
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000372 float out00 = d10;
373 float out01 = -d13;
374 float out10 = d10;
375 float out11 = -d13;
376
377 out00 += d00 + d20 + k0 + k1;
378 out01 += k0 - k1 - (d03 + d23);
379 out10 += -d20 - d30 + k2 + k3;
380 out11 += k2 - k3 + d23 + d33;
381
382 int y_in = get_global_id(1);
383 int x_out = (y_in % NUM_TILES_X) * 2;
384 int y_out = (y_in / NUM_TILES_X) * 2;
385 int z_out = get_global_id(0);
386
387#if defined(HAS_BIAS)
388 // Add bias
389 Vector bias = CONVERT_TO_VECTOR_STRUCT_NO_STEP(bias);
390
391 float b = (float) * ((__global float *)(vector_offset(&bias, z_out)));
392
393 out00 += (float)b;
394 out01 += (float)b;
395 out10 += (float)b;
396 out11 += (float)b;
397#endif // defined(HAS_BIAS)
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000398
399 // Get output address
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000400 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x_out * dst_stride_x + y_out * dst_stride_y + z_out * dst_stride_z;
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000401
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000402 // Store the 2x2 output tile
403 vstore2((float2)(out00, out01), 0, (__global float *)(dst_addr + 0 * dst_stride_y));
404 vstore2((float2)(out10, out11), 0, (__global float *)(dst_addr + 1 * dst_stride_y));
Gian Marco Iodice7e4b2392018-02-22 16:17:20 +0000405}
Gian Marco Iodiced2fab732018-03-02 11:18:12 +0000406#endif // defined(NUM_TILES_X)