blob: c1be4472a77afc4b6a5486f3f34d5dd5acb0a1d0 [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
Michalis Spyrouf6402dd2018-01-26 15:06:19 +00002 * Copyright (c) 2016-2018 ARM Limited.
Michalis Spyrou04f089c2017-08-08 17:42:38 +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#include "helpers.h"
25
26/** Calculate square sum of a vector
27 *
28 * @param[in] input Pointer to the first pixel.
29 *
30 * @return square sum of vector.
31 */
32inline DATA_TYPE square_sum(__global const DATA_TYPE *input)
33{
34 VEC_DATA_TYPE(DATA_TYPE, 16)
35 in = vload16(0, input);
36
37 in *= in;
38
39 in.s01234567 += in.s89ABCDEF;
40 in.s0123 += in.s4567;
41 in.s01 += in.s23;
42
43 return (in.s0 + in.s1);
44}
45
46/** Calculate sum of a vector
47 *
48 * @param[in] input Pointer to the first pixel.
49 *
50 * @return sum of vector.
51 */
52inline DATA_TYPE sum(__global const DATA_TYPE *input)
53{
54 VEC_DATA_TYPE(DATA_TYPE, 16)
55 in = vload16(0, input);
56
57 in.s01234567 += in.s89ABCDEF;
58 in.s0123 += in.s4567;
59 in.s01 += in.s23;
60
61 return (in.s0 + in.s1);
62}
63
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010064/** This kernel performs parallel reduction given an operation on x-axis.
Michalis Spyrou04f089c2017-08-08 17:42:38 +010065 *
66 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Michalis Spyrou04f089c2017-08-08 17:42:38 +010067 * @note The operation we want to perform must be passed at compile time using -DOPERATION e.g. -DOPERATION=square_sum
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010068 * @note The mean flag must be passed at compile time using -DMEAN if we want to compute the mean value
69 * @note The width size must be passed at compile time using -DWIDTH e.g. -DWIDTH=128 if we want to compute the mean value
Michalis Spyrou04f089c2017-08-08 17:42:38 +010070 *
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010071 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
Michalis Spyrou04f089c2017-08-08 17:42:38 +010072 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
73 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000074 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
75 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010076 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
77 * @param[in] partial_sum_ptr The local buffer to hold sumed values. Supported data types: same as @p src_ptt
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000078 * @param[in] partial_sum_stride_x Stride of the output tensor in X dimension (in bytes)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010079 * @param[in] partial_sum_step_x partial_sum_stride_x * number of elements along X processed per workitem(in bytes)
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000080 * @param[in] partial_sum_stride_y Stride of the output tensor in Y dimension (in bytes)
81 * @param[in] partial_sum_step_y partial_sum_stride_y * number of elements along Y processed per workitem(in bytes)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010082 * @param[in] partial_sum_offset_first_element_in_bytes The offset of the first element in the source tensor
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000083 * @param[in] local_sums Local buffer for storing the partial sum
Michalis Spyrou04f089c2017-08-08 17:42:38 +010084 */
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010085__kernel void reduction_operation_x(
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000086 IMAGE_DECLARATION(src),
87 IMAGE_DECLARATION(partial_sum),
Michalis Spyrou04f089c2017-08-08 17:42:38 +010088 __local DATA_TYPE *local_sums)
89{
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000090 Image src = CONVERT_TO_IMAGE_STRUCT(src);
91 Image partial_sum = CONVERT_TO_IMAGE_STRUCT(partial_sum);
Michalis Spyrou04f089c2017-08-08 17:42:38 +010092
93 unsigned int lsize = get_local_size(0);
94 unsigned int lid = get_local_id(0);
95
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000096 for(unsigned int y = 0; y < get_local_size(1); ++y)
Michalis Spyrou04f089c2017-08-08 17:42:38 +010097 {
Michalis Spyrouf6402dd2018-01-26 15:06:19 +000098 local_sums[lid] = OPERATION((__global DATA_TYPE *)offset(&src, 0, y));
Michalis Spyrou04f089c2017-08-08 17:42:38 +010099 barrier(CLK_LOCAL_MEM_FENCE);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100100
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000101 // Perform parallel reduction
102 for(unsigned int i = lsize >> 1; i > 0; i >>= 1)
103 {
104 if(lid < i)
105 {
106 local_sums[lid] += local_sums[lid + i];
107 }
108 barrier(CLK_LOCAL_MEM_FENCE);
109 }
110
111 if(lid == 0)
112 {
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100113#if defined(MEAN) && defined(WIDTH)
114 if(y == get_local_size(1) - 1)
115 {
116 local_sums[0] /= WIDTH;
117 }
118#endif /* defined(MEAN) && defined(WIDTH) */
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000119 ((__global DATA_TYPE *)offset(&partial_sum, get_group_id(0), y))[0] = local_sums[0];
120 }
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100121 }
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100122}
123
124#if defined(WIDTH)
125/** This kernel performs reduction on x-axis. (QASYMM8)
126 *
127 * @note The width size must be passed at compile time using -DWIDTH e.g. -DWIDTH=128
128 *
129 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8
130 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
131 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
132 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
133 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p src_ptt
134 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
135 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
136 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
137 */
138__kernel void reduction_operation_quantized_x(
139 VECTOR_DECLARATION(src),
140 VECTOR_DECLARATION(output))
141{
142 Vector src = CONVERT_TO_VECTOR_STRUCT(src);
143 Vector output = CONVERT_TO_VECTOR_STRUCT(output);
144
145 uint res = 0;
146
147 for(unsigned int x = 0; x < WIDTH; ++x)
148 {
149 res += *((__global uchar *)vector_offset(&src, x));
150 }
151
152#if defined(MEAN)
153 res /= WIDTH;
154#endif /* defined(MEAN) */
155
156 // Store result
157 *((__global uchar *)output.ptr) = convert_uchar(res);
158}
159#endif /* defined(HEIGHT) */
160
161#if defined(HEIGHT)
162/** This kernel performs reduction on y-axis.
163 *
164 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
165 * @note The height size must be passed at compile time using -DHEIGHT e.g. -DHEIGHT=128
166 *
167 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/F16/F32
168 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
169 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
170 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
171 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
172 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
173 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p src_ptt
174 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
175 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
176 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
177 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
178 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
179 */
180__kernel void reduction_operation_y(
181 IMAGE_DECLARATION(src),
182 IMAGE_DECLARATION(output))
183{
184 Image src = CONVERT_TO_IMAGE_STRUCT(src);
185 Image output = CONVERT_TO_IMAGE_STRUCT(output);
186
187 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
188 res = 0;
189
190 for(unsigned int y = 0; y < HEIGHT; ++y)
191 {
192 res += CONVERT(vload16(0, (__global DATA_TYPE *)offset(&src, 0, y)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
193 }
194
195#if defined(MEAN)
196 res /= HEIGHT;
197#endif /* defined(MEAN) */
198
199 // Store result
200 vstore16(CONVERT(res, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)output.ptr);
201}
202#endif /* defined(HEIGHT) */
203
204#if defined(DEPTH)
205/** This kernel performs reduction on z-axis.
206 *
207 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
208 * @note The depth size must be passed at compile time using -DDEPTH e.g. -DDEPTH=128
209 *
210 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/F16/F32
211 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
212 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
213 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
214 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
215 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
216 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
217 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
218 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p input_ptt
219 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
220 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
221 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
222 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
223 * @param[in] output_stride_z Stride of the output tensor in Z dimension (in bytes)
224 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
225 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
226 */
227__kernel void reduction_operation_z(
228 TENSOR3D_DECLARATION(input),
229 TENSOR3D_DECLARATION(output))
230{
231 Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
232 Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
233
234 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
235 res = 0;
236
237 for(unsigned int z = 0; z < DEPTH; ++z)
238 {
239 res += CONVERT(vload16(0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 0, z)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
240 }
241
242#if defined(MEAN)
243 res /= DEPTH;
244#endif /* defined(MEAN) */
245
246 // Store result
247 vstore16(CONVERT(res, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)output.ptr);
248}
249#endif /* defined(DEPTH) */
250
251#if defined(BATCH) && defined(DEPTH)
252/** This kernel performs reduction on w-axis.
253 *
254 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
255 * @note The batch size must be passed at compile time using -DBATCH e.g. -DBATCH=128
256 * @note The depth size must be passed at compile time using -DBATCH e.g. -DDEPTH=128
257 *
258 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/F16/F32
259 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
260 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
261 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
262 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
263 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
264 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
265 * @param[in] input_stride_w Stride of the source tensor in W dimension (in bytes)
266 * @param[in] input_step_w input_stride_w * number of elements along W processed per workitem(in bytes)
267 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
268 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p input_ptt
269 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
270 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
271 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
272 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
273 * @param[in] output_stride_z Stride of the output tensor in Z dimension (in bytes)
274 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
275 * @param[in] output_stride_w Stride of the output tensor in W dimension (in bytes)
276 * @param[in] output_step_w output_stride_w * number of elements along W processed per workitem(in bytes)
277 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
278 */
279__kernel void reduction_operation_w(
280 TENSOR4D_DECLARATION(input),
281 TENSOR4D_DECLARATION(output))
282{
283 Tensor4D input = CONVERT_TO_TENSOR4D_STRUCT(input, DEPTH);
284 Tensor4D output = CONVERT_TO_TENSOR4D_STRUCT(output, DEPTH);
285
286 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
287 res = 0;
288
289 for(unsigned int w = 0; w < BATCH; ++w)
290 {
291 res += CONVERT(vload16(0, (__global DATA_TYPE *)tensor4D_offset(&input, 0, 0, 0, w)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
292 }
293
294#if defined(MEAN)
295 res /= BATCH;
296#endif /* defined(MEAN) */
297
298 // Store result
299 vstore16(CONVERT(res, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)output.ptr);
300}
301#endif /* defined(BATCH) && defined(DEPTH) */