blob: 8237fe17002b97b7b7e42dfa7b97169795286abe [file] [log] [blame]
Chunosovd621bca2017-11-03 17:33:15 +07001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Chunosovd621bca2017-11-03 17:33:15 +07003 *
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_asymm.h"
25
Sheri Zhang681f2d42020-02-20 11:23:08 +000026#undef CONVERT_SAT_STR
Chunosovd621bca2017-11-03 17:33:15 +070027#undef CONVERT_SAT
28
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010029#if defined(DATA_TYPE) && defined(STRIDE_X) && defined(WEIGHTS_DEPTH) && defined(OUTPUT_MULTIPLIER) && defined(OUTPUT_SHIFT)
Chunosovd621bca2017-11-03 17:33:15 +070030
Sheri Zhang681f2d42020-02-20 11:23:08 +000031#define CONVERT_SAT_STR(x, type) (convert_##type##8_sat((x)))
32#define CONVERT_SAT(x, type) CONVERT_SAT_STR(x, type)
33
Giorgio Arenae620a832020-02-17 16:33:20 +000034#if defined(DATA_LAYOUT_NHWC)
35
Georgios Pinitasaa95ddc2020-07-21 22:45:13 +010036#if KERNEL_SIZE == 9
37
38#if STRIDE_X == 1
39#define CONVOLUTION1x9(acc, src_ptr, weights_ptr) CONVOLUTION1x9_STRIDE1(acc, src_ptr, weights_ptr)
40#elif STRIDE_X == 2
41#define CONVOLUTION1x9(acc, src_ptr, weights_ptr) CONVOLUTION1x9_STRIDE2(acc, src_ptr, weights_ptr)
42#else /* STRIDE_X not equals 1 or 2 */
43#error "STRIDE_X larger than 2 is not supported"
44#endif /* STRIDE_X */
45
46#define CONVOLUTION1x9_STRIDE1(acc, src_ptr, weights_ptr) \
47 ({ \
48 int8 weights_values0 = 0; \
49 int weights_value1 = 0; \
50 weights_values0.s0 = convert_int(*(weights_ptr + 0 * weights_stride_y)); \
51 weights_values0.s1 = convert_int(*(weights_ptr + 1 * weights_stride_y)); \
52 weights_values0.s2 = convert_int(*(weights_ptr + 2 * weights_stride_y)); \
53 weights_values0.s3 = convert_int(*(weights_ptr + 3 * weights_stride_y)); \
54 weights_values0.s4 = convert_int(*(weights_ptr + 4 * weights_stride_y)); \
55 weights_values0.s5 = convert_int(*(weights_ptr + 5 * weights_stride_y)); \
56 weights_values0.s6 = convert_int(*(weights_ptr + 6 * weights_stride_y)); \
57 weights_values0.s7 = convert_int(*(weights_ptr + 7 * weights_stride_y)); \
58 weights_value1 = convert_int(*(weights_ptr + 8 * weights_stride_y)); \
59 \
60 int8 src0 = 0; \
61 int8 src1 = 0; \
62 src0.s0 = convert_int(*(src_ptr + 0 * weights_stride_y)); \
63 src0.s1 = convert_int(*(src_ptr + 1 * weights_stride_y)); \
64 src0.s2 = convert_int(*(src_ptr + 2 * weights_stride_y)); \
65 src0.s3 = convert_int(*(src_ptr + 3 * weights_stride_y)); \
66 src0.s4 = convert_int(*(src_ptr + 4 * weights_stride_y)); \
67 src0.s5 = convert_int(*(src_ptr + 5 * weights_stride_y)); \
68 src0.s6 = convert_int(*(src_ptr + 6 * weights_stride_y)); \
69 src0.s7 = convert_int(*(src_ptr + 7 * weights_stride_y)); \
70 src1.s0 = convert_int(*(src_ptr + 8 * weights_stride_y)); \
71 src1.s1 = convert_int(*(src_ptr + 9 * weights_stride_y)); \
72 src1.s2 = convert_int(*(src_ptr + 10 * weights_stride_y)); \
73 src1.s3 = convert_int(*(src_ptr + 11 * weights_stride_y)); \
74 src1.s4 = convert_int(*(src_ptr + 12 * weights_stride_y)); \
75 src1.s5 = convert_int(*(src_ptr + 13 * weights_stride_y)); \
76 src1.s6 = convert_int(*(src_ptr + 14 * weights_stride_y)); \
77 src1.s7 = convert_int(*(src_ptr + 15 * weights_stride_y)); \
78 \
79 acc += src0 * (int8)weights_values0.s0; \
80 acc += (int8)(src0.s1234, src0.s567, src1.s0) * (int8)weights_values0.s1; \
81 acc += (int8)(src0.s234, src0.s567, src1.s01) * (int8)weights_values0.s2; \
82 acc += (int8)(src0.s345, src0.s67, src1.s012) * (int8)weights_values0.s3; \
83 acc += (int8)(src0.s4567, src1.s0123) * (int8)weights_values0.s4; \
84 acc += (int8)(src0.s567, src1.s0123, src1.s4) * (int8)weights_values0.s5; \
85 acc += (int8)(src0.s67, src1.s012, src1.s345) * (int8)weights_values0.s6; \
86 acc += (int8)(src0.s7, src1.s0123, src1.s456) * (int8)weights_values0.s7; \
87 acc += src1 * (int8)weights_value1; \
88 })
89
90#define CONVOLUTION1x9_STRIDE2(acc, src_ptr, weights_ptr) \
91 ({ \
92 int8 weights_values0 = 0; \
93 int weights_value1 = 0; \
94 weights_values0.s0 = convert_int(*(weights_ptr + 0 * weights_stride_y)); \
95 weights_values0.s1 = convert_int(*(weights_ptr + 1 * weights_stride_y)); \
96 weights_values0.s2 = convert_int(*(weights_ptr + 2 * weights_stride_y)); \
97 weights_values0.s3 = convert_int(*(weights_ptr + 3 * weights_stride_y)); \
98 weights_values0.s4 = convert_int(*(weights_ptr + 4 * weights_stride_y)); \
99 weights_values0.s5 = convert_int(*(weights_ptr + 5 * weights_stride_y)); \
100 weights_values0.s6 = convert_int(*(weights_ptr + 6 * weights_stride_y)); \
101 weights_values0.s7 = convert_int(*(weights_ptr + 7 * weights_stride_y)); \
102 weights_value1 = convert_int(*(weights_ptr + 8 * weights_stride_y)); \
103 \
104 int16 src0 = 0; \
105 int8 src1 = 0; \
106 src0.s0 = convert_int(*(src_ptr + 0 * weights_stride_y)); \
107 src0.s1 = convert_int(*(src_ptr + 1 * weights_stride_y)); \
108 src0.s2 = convert_int(*(src_ptr + 2 * weights_stride_y)); \
109 src0.s3 = convert_int(*(src_ptr + 3 * weights_stride_y)); \
110 src0.s4 = convert_int(*(src_ptr + 4 * weights_stride_y)); \
111 src0.s5 = convert_int(*(src_ptr + 5 * weights_stride_y)); \
112 src0.s6 = convert_int(*(src_ptr + 6 * weights_stride_y)); \
113 src0.s7 = convert_int(*(src_ptr + 7 * weights_stride_y)); \
114 src0.s8 = convert_int(*(src_ptr + 8 * weights_stride_y)); \
115 src0.s9 = convert_int(*(src_ptr + 9 * weights_stride_y)); \
116 src0.sA = convert_int(*(src_ptr + 10 * weights_stride_y)); \
117 src0.sB = convert_int(*(src_ptr + 11 * weights_stride_y)); \
118 src0.sC = convert_int(*(src_ptr + 12 * weights_stride_y)); \
119 src0.sD = convert_int(*(src_ptr + 13 * weights_stride_y)); \
120 src0.sE = convert_int(*(src_ptr + 14 * weights_stride_y)); \
121 src0.sF = convert_int(*(src_ptr + 15 * weights_stride_y)); \
122 src1.s0 = convert_int(*(src_ptr + 16 * weights_stride_y)); \
123 src1.s1 = convert_int(*(src_ptr + 17 * weights_stride_y)); \
124 src1.s2 = convert_int(*(src_ptr + 18 * weights_stride_y)); \
125 src1.s3 = convert_int(*(src_ptr + 19 * weights_stride_y)); \
126 src1.s4 = convert_int(*(src_ptr + 20 * weights_stride_y)); \
127 src1.s5 = convert_int(*(src_ptr + 21 * weights_stride_y)); \
128 src1.s6 = convert_int(*(src_ptr + 22 * weights_stride_y)); \
129 src1.s7 = convert_int(*(src_ptr + 23 * weights_stride_y)); \
130 \
131 acc += src0.s02468ACE * (int8)weights_values0.s0; \
132 acc += (int8)(src0.s1357, src0.s9BDF) * (int8)weights_values0.s1; \
133 acc += (int8)(src0.s2468, src0.sACE, src1.s0) * (int8)weights_values0.s2; \
134 acc += (int8)(src0.s3579, src0.sBDF, src1.s1) * (int8)weights_values0.s3; \
135 acc += (int8)(src0.s468A, src0.sCE, src1.s02) * (int8)weights_values0.s4; \
136 acc += (int8)(src0.s579, src0.sBDF, src1.s13) * (int8)weights_values0.s5; \
137 acc += (int8)(src0.s68A, src0.sCE, src1.s024) * (int8)weights_values0.s6; \
138 acc += (int8)(src0.s79B, src0.sDF, src1.s135) * (int8)weights_values0.s7; \
139 acc += (int8)(src0.s8AC, src0.sE, src1.s0246) * (int8)weights_value1; \
140 })
141
142#elif KERNEL_SIZE == 5
Giorgio Arenae620a832020-02-17 16:33:20 +0000143
144#if STRIDE_X == 1
145#define CONVOLUTION1x5(acc, src_ptr, weights_ptr) CONVOLUTION1x5_STRIDE1(acc, src_ptr, weights_ptr)
146#elif STRIDE_X == 2
147#define CONVOLUTION1x5(acc, src_ptr, weights_ptr) CONVOLUTION1x5_STRIDE2(acc, src_ptr, weights_ptr)
148#else /* STRIDE_X not equals 1 or 2 */
149#error "STRIDE_X larger than 2 is not supported"
150#endif /* STRIDE_X */
151
152#define CONVOLUTION1x5_STRIDE1(acc, src_ptr, weights_ptr) \
153 ({ \
154 int4 weights_values0 = 0; \
155 int weights_value1 = 0; \
156 weights_values0.s0 = convert_int(*(weights_ptr + 0 * weights_stride_y)); \
157 weights_values0.s1 = convert_int(*(weights_ptr + 1 * weights_stride_y)); \
158 weights_values0.s2 = convert_int(*(weights_ptr + 2 * weights_stride_y)); \
159 weights_values0.s3 = convert_int(*(weights_ptr + 3 * weights_stride_y)); \
160 weights_value1 = convert_int(*(weights_ptr + 4 * weights_stride_y)); \
161 \
162 int8 src0 = 0; \
163 int4 src1 = 0; \
164 src0.s0 = convert_int(*(src_ptr + 0 * weights_stride_y)); \
165 src0.s1 = convert_int(*(src_ptr + 1 * weights_stride_y)); \
166 src0.s2 = convert_int(*(src_ptr + 2 * weights_stride_y)); \
167 src0.s3 = convert_int(*(src_ptr + 3 * weights_stride_y)); \
168 src0.s4 = convert_int(*(src_ptr + 4 * weights_stride_y)); \
169 src0.s5 = convert_int(*(src_ptr + 5 * weights_stride_y)); \
170 src0.s6 = convert_int(*(src_ptr + 6 * weights_stride_y)); \
171 src0.s7 = convert_int(*(src_ptr + 7 * weights_stride_y)); \
172 src1.s0 = convert_int(*(src_ptr + 8 * weights_stride_y)); \
173 src1.s1 = convert_int(*(src_ptr + 9 * weights_stride_y)); \
174 src1.s2 = convert_int(*(src_ptr + 10 * weights_stride_y)); \
175 src1.s3 = convert_int(*(src_ptr + 11 * weights_stride_y)); \
176 \
177 acc += (src0 + input_offset) * ((int8)weights_values0.s0 + weight_offset); \
178 acc += ((int8)(src0.s1234, src0.s567, src1.s0) + input_offset) * ((int8)weights_values0.s1 + weight_offset); \
179 acc += ((int8)(src0.s234, src0.s567, src1.s01) + input_offset) * ((int8)weights_values0.s2 + weight_offset); \
180 acc += ((int8)(src0.s345, src0.s67, src1.s012) + input_offset) * ((int8)weights_values0.s3 + weight_offset); \
181 acc += ((int8)(src0.s45, src0.s67, src1.s0123) + input_offset) * ((int8)weights_value1 + weight_offset); \
182 })
183
184#define CONVOLUTION1x5_STRIDE2(acc, src_ptr, weights_ptr) \
185 ({ \
186 int4 weights_values0 = 0; \
187 int weights_value1 = 0; \
188 weights_values0.s0 = convert_int(*(weights_ptr + 0 * weights_stride_y)); \
189 weights_values0.s1 = convert_int(*(weights_ptr + 1 * weights_stride_y)); \
190 weights_values0.s2 = convert_int(*(weights_ptr + 2 * weights_stride_y)); \
191 weights_values0.s3 = convert_int(*(weights_ptr + 3 * weights_stride_y)); \
192 weights_value1 = convert_int(*(weights_ptr + 4 * weights_stride_y)); \
193 \
194 int16 src0 = 0; \
195 int4 src1 = 0; \
196 src0.s0 = convert_int(*(src_ptr + 0 * weights_stride_y)); \
197 src0.s1 = convert_int(*(src_ptr + 1 * weights_stride_y)); \
198 src0.s2 = convert_int(*(src_ptr + 2 * weights_stride_y)); \
199 src0.s3 = convert_int(*(src_ptr + 3 * weights_stride_y)); \
200 src0.s4 = convert_int(*(src_ptr + 4 * weights_stride_y)); \
201 src0.s5 = convert_int(*(src_ptr + 5 * weights_stride_y)); \
202 src0.s6 = convert_int(*(src_ptr + 6 * weights_stride_y)); \
203 src0.s7 = convert_int(*(src_ptr + 7 * weights_stride_y)); \
204 src0.s8 = convert_int(*(src_ptr + 8 * weights_stride_y)); \
205 src0.s9 = convert_int(*(src_ptr + 9 * weights_stride_y)); \
206 src0.sa = convert_int(*(src_ptr + 10 * weights_stride_y)); \
207 src0.sb = convert_int(*(src_ptr + 11 * weights_stride_y)); \
208 src0.sc = convert_int(*(src_ptr + 12 * weights_stride_y)); \
209 src0.sd = convert_int(*(src_ptr + 13 * weights_stride_y)); \
210 src0.se = convert_int(*(src_ptr + 14 * weights_stride_y)); \
211 src0.sf = convert_int(*(src_ptr + 15 * weights_stride_y)); \
212 src1.s0 = convert_int(*(src_ptr + 16 * weights_stride_y)); \
213 src1.s1 = convert_int(*(src_ptr + 17 * weights_stride_y)); \
214 src1.s2 = convert_int(*(src_ptr + 18 * weights_stride_y)); \
215 src1.s3 = convert_int(*(src_ptr + 19 * weights_stride_y)); \
216 \
217 acc += (src0.even + input_offset) * ((int8)weights_values0.s0 + weight_offset); \
218 acc += ((int8)(src0.s1357, src0.s9BDF) + input_offset) * ((int8)weights_values0.s1 + weight_offset); \
219 acc += ((int8)(src0.s2468, src0.sACE, src1.s0) + input_offset) * ((int8)weights_values0.s2 + weight_offset); \
220 acc += ((int8)(src0.s3579, src0.sBDF, src1.s1) + input_offset) * ((int8)weights_values0.s3 + weight_offset); \
221 acc += ((int8)(src0.s468a, src0.sCE, src1.s02) + input_offset) * ((int8)weights_value1 + weight_offset); \
222 })
223
224#elif KERNEL_SIZE == 3
225
226#if STRIDE_X == 1
227#define CONVOLUTION1x3(acc, src_ptr, weights_ptr) CONVOLUTION1x3_STRIDE1(acc, src_ptr, weights_ptr)
228#elif STRIDE_X == 2
229#define CONVOLUTION1x3(acc, src_ptr, weights_ptr) CONVOLUTION1x3_STRIDE2(acc, src_ptr, weights_ptr)
230#else /* STRIDE_X not equals 1 or 2 */
231#error "STRIDE_X larger than 2 is not supported"
232#endif /* STRIDE_X */
233
234#define CONVOLUTION1x3_STRIDE1(acc, src_ptr, weights_ptr) \
235 ({ \
236 int3 weights_values0 = 0; \
237 weights_values0.s0 = convert_int(*(weights_ptr + 0 * weights_stride_y)); \
238 weights_values0.s1 = convert_int(*(weights_ptr + 1 * weights_stride_y)); \
239 weights_values0.s2 = convert_int(*(weights_ptr + 2 * weights_stride_y)); \
240 \
241 int8 src0 = 0; \
242 int2 src1 = 0; \
243 src0.s0 = convert_int(*(src_ptr + 0 * weights_stride_y)); \
244 src0.s1 = convert_int(*(src_ptr + 1 * weights_stride_y)); \
245 src0.s2 = convert_int(*(src_ptr + 2 * weights_stride_y)); \
246 src0.s3 = convert_int(*(src_ptr + 3 * weights_stride_y)); \
247 src0.s4 = convert_int(*(src_ptr + 4 * weights_stride_y)); \
248 src0.s5 = convert_int(*(src_ptr + 5 * weights_stride_y)); \
249 src0.s6 = convert_int(*(src_ptr + 6 * weights_stride_y)); \
250 src0.s7 = convert_int(*(src_ptr + 7 * weights_stride_y)); \
251 src1.s0 = convert_int(*(src_ptr + 8 * weights_stride_y)); \
252 src1.s1 = convert_int(*(src_ptr + 9 * weights_stride_y)); \
253 \
254 acc += (src0 + input_offset) * ((int8)weights_values0.s0 + weight_offset); \
255 acc += ((int8)(src0.s1234, src0.s567, src1.s0) + input_offset) * ((int8)weights_values0.s1 + weight_offset); \
256 acc += ((int8)(src0.s234, src0.s567, src1.s01) + input_offset) * ((int8)weights_values0.s2 + weight_offset); \
257 })
258
259#define CONVOLUTION1x3_STRIDE2(acc, src_ptr, weights_ptr) \
260 ({ \
261 int3 weights_values0 = 0; \
262 weights_values0.s0 = convert_int(*(weights_ptr + 0 * weights_stride_y)); \
263 weights_values0.s1 = convert_int(*(weights_ptr + 1 * weights_stride_y)); \
264 weights_values0.s2 = convert_int(*(weights_ptr + 2 * weights_stride_y)); \
265 \
266 int16 src0 = 0; \
267 int src1 = 0; \
268 src0.s0 = convert_int(*(src_ptr + 0 * src_stride_y)); \
269 src0.s1 = convert_int(*(src_ptr + 1 * src_stride_y)); \
270 src0.s2 = convert_int(*(src_ptr + 2 * src_stride_y)); \
271 src0.s3 = convert_int(*(src_ptr + 3 * src_stride_y)); \
272 src0.s4 = convert_int(*(src_ptr + 4 * src_stride_y)); \
273 src0.s5 = convert_int(*(src_ptr + 5 * src_stride_y)); \
274 src0.s6 = convert_int(*(src_ptr + 6 * src_stride_y)); \
275 src0.s7 = convert_int(*(src_ptr + 7 * src_stride_y)); \
276 src0.s8 = convert_int(*(src_ptr + 8 * src_stride_y)); \
277 src0.s9 = convert_int(*(src_ptr + 9 * src_stride_y)); \
278 src0.sa = convert_int(*(src_ptr + 10 * src_stride_y)); \
279 src0.sb = convert_int(*(src_ptr + 11 * src_stride_y)); \
280 src0.sc = convert_int(*(src_ptr + 12 * src_stride_y)); \
281 src0.sd = convert_int(*(src_ptr + 13 * src_stride_y)); \
282 src0.se = convert_int(*(src_ptr + 14 * src_stride_y)); \
283 src0.sf = convert_int(*(src_ptr + 15 * src_stride_y)); \
284 src1 = convert_int(*(src_ptr + 16 * src_stride_y)); \
285 acc += (src0.even + input_offset) * ((int8)weights_values0.s0 + weight_offset); \
286 acc += ((int8)(src0.s1357, src0.s9BDF) + input_offset) * ((int8)weights_values0.s1 + weight_offset); \
287 acc += ((int8)(src0.s2468, src0.sACE, src1) + input_offset) * ((int8)weights_values0.s2 + weight_offset); \
288 })
289
290#elif KERNEL_SIZE == 1
291
292#if STRIDE_X == 3
293#define INPUT_VALUE extract_input_stride3
294#elif STRIDE_X == 2
295#define INPUT_VALUE extract_input_stride2
296#elif STRIDE_X == 1
297#define INPUT_VALUE extract_input_stride1
298
299#else /* STRIDE_X not equals 1, 2 or 3 */
300#error "Only support strides 1, 2 and 3"
301#endif /* STRIDE_X */
302
303#endif // KERNEL_SIZE == 1
304
305/** Extracts a 1D horizontal vector from the input tensor with stride as 1.
306 *
307 * @param[in] input_value Pointer to the first value.
308 *
309 * @return extracted input values.
310 */
311inline VEC_DATA_TYPE(DATA_TYPE, 8) extract_input_stride1(__global const DATA_TYPE *input_value, const uchar stride_y)
312{
313 VEC_DATA_TYPE(DATA_TYPE, 8)
314 vals;
315 vals.s0 = *(input_value + 0 * stride_y);
316 vals.s1 = *(input_value + 1 * stride_y);
317 vals.s2 = *(input_value + 2 * stride_y);
318 vals.s3 = *(input_value + 3 * stride_y);
319 vals.s4 = *(input_value + 4 * stride_y);
320 vals.s5 = *(input_value + 5 * stride_y);
321 vals.s6 = *(input_value + 6 * stride_y);
322 vals.s7 = *(input_value + 7 * stride_y);
323
324 return vals;
325}
326
327/** Extracts a 1D horizontal vector from the input tensor with stride as 2.
328 *
329 * @param[in] input_value Pointer to the first value.
330 *
331 * @return extracted input values.
332 */
333inline VEC_DATA_TYPE(DATA_TYPE, 8) extract_input_stride2(__global const DATA_TYPE *input_value, const uchar stride_y)
334{
335 VEC_DATA_TYPE(DATA_TYPE, 8)
336 vals;
337 vals.s0 = *(input_value + 0 * stride_y);
338 vals.s1 = *(input_value + 2 * stride_y);
339 vals.s2 = *(input_value + 4 * stride_y);
340 vals.s3 = *(input_value + 6 * stride_y);
341 vals.s4 = *(input_value + 8 * stride_y);
342 vals.s5 = *(input_value + 10 * stride_y);
343 vals.s6 = *(input_value + 12 * stride_y);
344 vals.s7 = *(input_value + 14 * stride_y);
345
346 return vals;
347}
348
349/** Extracts a 1D horizontal vector from the input tensor with stride as 3 and 8-bit data size.
350 *
351 * @param[in] input_value Pointer to the first value.
352 *
353 * @return extracted input values.
354 */
355inline VEC_DATA_TYPE(DATA_TYPE, 8) extract_input_stride3(__global const DATA_TYPE *input_value, const uchar stride_y)
356{
357 VEC_DATA_TYPE(DATA_TYPE, 8)
358 vals;
359 vals.s0 = *(input_value + 0 * stride_y);
360 vals.s1 = *(input_value + 3 * stride_y);
361 vals.s2 = *(input_value + 6 * stride_y);
362 vals.s3 = *(input_value + 9 * stride_y);
363 vals.s4 = *(input_value + 12 * stride_y);
364 vals.s5 = *(input_value + 15 * stride_y);
365 vals.s6 = *(input_value + 18 * stride_y);
366 vals.s7 = *(input_value + 21 * stride_y);
367
368 return vals;
369}
370
371/** This kernel performs a direct convolution to convolve the low three dimensions.
372 *
373 * @note The convolution stride x must be passed at compile time using -DSTRIDE_X e.g. -DSTRIDE_X=1
374 * @note The third dimensions of the weights tensors must be passed at compile time using -DWEIGHTS_DEPTH
375 * @note If biases are used then -DHAS_BIAS has to be passed at compile time
376 * @note The output quantization multiplier must be passed at compile time using -DOUTPUT_MULTIPLIER e.g. -DOUTPUT_MULTIPLIER=1234
377 * @note The output quantization shift must be passed at compile time using -DOUTPUT_SHIFT e.g. -DOUTPUT_SHIFT=4
378 *
379 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED
380 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
381 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
382 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
383 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
384 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
385 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
386 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
387 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
388 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
389 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
390 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
391 * @param[in] dst_step_y dst_stride_y * number of elements along Z processed per workitem(in bytes)
392 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
393 * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
394 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
395 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr
396 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
397 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
398 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
399 * @param[in] weights_step_y weights_stride_y * number of elements along y processed per workitem(in bytes)
400 * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes)
401 * @param[in] weights_step_z weights_stride_z * number of elements along Z processed per workitem(in bytes)
402 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
403 * @param[in] biases_ptr Pointer to the biases tensor. Supported data types: S32
404 * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes)
405 * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes)
406 * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor
407 * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension
408 * @param[in] input_offset Input offset quantization parameter
409 * @param[in] weight_offset Weights offset quantization parameter
410 * @param[in] output_offset Output offset quantization parameter
411 */
412__kernel void direct_convolution_quantized(
413 TENSOR3D_DECLARATION(src),
414 TENSOR3D_DECLARATION(dst),
415 TENSOR3D_DECLARATION(weights),
416#ifdef HAS_BIAS
417 VECTOR_DECLARATION(biases),
418#endif /* defined(HAS_BIAS) */
419 unsigned int weights_stride_w,
420 int input_offset,
421 int weight_offset,
422 int output_offset)
423{
424 Image src = CONVERT_TO_IMAGE_STRUCT(src);
425 Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(weights);
426 Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst);
427
428 int8 values0 = 0;
429
Giorgio Arena3c4bf0c2020-03-02 09:49:29 +0000430 const int id0 = get_global_id(0);
Giorgio Arenae620a832020-02-17 16:33:20 +0000431 const int y_coord = (get_global_id(2) * STRIDE_Y) - PAD_TOP;
432
433 __global DATA_TYPE *weights_addr = (__global DATA_TYPE *)tensor3D_offset(&weights, 0, 0, 0);
Giorgio Arena3c4bf0c2020-03-02 09:49:29 +0000434 __global DATA_TYPE *src_addr = (__global DATA_TYPE *)offset(&src, 0, 0) - src_stride_x * id0 + y_coord * (int)src_stride_z;
Giorgio Arenae620a832020-02-17 16:33:20 +0000435
Giorgio Arena3c4bf0c2020-03-02 09:49:29 +0000436 weights_addr += id0 * weights_stride_w;
Giorgio Arenae620a832020-02-17 16:33:20 +0000437
438 for(volatile int d = 0; d < WEIGHTS_DEPTH; ++d)
439 {
Georgios Pinitasaa95ddc2020-07-21 22:45:13 +0100440#if KERNEL_SIZE == 9
441 if(y_coord < 0)
442 {
443 const int start_z = -y_coord;
444 for(int i = start_z; i < 9; ++i)
445 {
446 CONVOLUTION1x9(values0, (src_addr + i * (int)src_stride_z), (weights_addr + i * (int)weights_stride_z));
447 }
448 }
449 else if(y_coord > (SRC_HEIGHT - 9))
450 {
451 // Avoid loading rows beyond the input height
452 const int end_z = SRC_HEIGHT - y_coord;
453 for(int i = 0; i < end_z; ++i)
454 {
455 CONVOLUTION1x9(values0, (src_addr + i * (int)src_stride_z), (weights_addr + i * (int)weights_stride_z));
456 }
457 }
458 else
459 {
460 CONVOLUTION1x9(values0, src_addr, weights_addr);
461 CONVOLUTION1x9(values0, (src_addr + 1 * (int)src_stride_z), (weights_addr + 1 * (int)weights_stride_z));
462 CONVOLUTION1x9(values0, (src_addr + 2 * (int)src_stride_z), (weights_addr + 2 * (int)weights_stride_z));
463 CONVOLUTION1x9(values0, (src_addr + 3 * (int)src_stride_z), (weights_addr + 3 * (int)weights_stride_z));
464 CONVOLUTION1x9(values0, (src_addr + 4 * (int)src_stride_z), (weights_addr + 4 * (int)weights_stride_z));
465 CONVOLUTION1x9(values0, (src_addr + 5 * (int)src_stride_z), (weights_addr + 5 * (int)weights_stride_z));
466 CONVOLUTION1x9(values0, (src_addr + 6 * (int)src_stride_z), (weights_addr + 6 * (int)weights_stride_z));
467 CONVOLUTION1x9(values0, (src_addr + 7 * (int)src_stride_z), (weights_addr + 7 * (int)weights_stride_z));
468 CONVOLUTION1x9(values0, (src_addr + 8 * (int)src_stride_z), (weights_addr + 8 * (int)weights_stride_z));
469 }
470#elif KERNEL_SIZE == 5
Giorgio Arena3c4bf0c2020-03-02 09:49:29 +0000471#if(PAD_TOP == 1) || (PAD_BOTTM == 1)
Giorgio Arenae620a832020-02-17 16:33:20 +0000472 if(y_coord < 0) // special case Z = -1 doesn't exists
473 {
474 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_z));
475 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_z));
476 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 3 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 3 * weights_stride_z));
477 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 4 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 4 * weights_stride_z));
478 }
479 else if(get_global_id(2) == (DST_HEIGHT - 1))
480 {
481 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_z));
482 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_z));
483 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_z));
484 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 3 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 3 * weights_stride_z));
485 }
486 else
487 {
488 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_z));
489 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_z));
490 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_z));
491 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 3 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 3 * weights_stride_z));
492 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 4 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 4 * weights_stride_z));
493 }
Giorgio Arena3c4bf0c2020-03-02 09:49:29 +0000494#elif(PAD_TOP == 2) || (PAD_BOTTM == 2)
Giorgio Arenae620a832020-02-17 16:33:20 +0000495 if(y_coord < -1)
496 {
497 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_z));
498 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 3 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 3 * weights_stride_z));
499 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 4 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 4 * weights_stride_z));
500 }
501 else if(y_coord == -1)
502 {
503 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_z));
504 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_z));
505 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 3 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 3 * weights_stride_z));
506 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 4 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 4 * weights_stride_z));
507 }
508 else if(y_coord == (SRC_HEIGHT - 3))
509 {
510 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_z));
511 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_z));
512 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_z));
513 }
514 else if(y_coord >= (SRC_HEIGHT - 4))
515 {
516 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_z));
517 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_z));
518 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_z));
519 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 3 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 3 * weights_stride_z));
520 }
521 else
522 {
523 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_z));
524 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_z));
525 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_z));
526 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 3 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 3 * weights_stride_z));
527 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 4 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 4 * weights_stride_z));
528 }
Giorgio Arena3c4bf0c2020-03-02 09:49:29 +0000529#else /* PAD_TOP == 2 || || PAD_BOTTM == 2 */
Giorgio Arenae620a832020-02-17 16:33:20 +0000530 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_z));
531 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_z));
532 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_z));
533 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 3 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 3 * weights_stride_z));
534 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 4 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 4 * weights_stride_z));
Giorgio Arena3c4bf0c2020-03-02 09:49:29 +0000535#endif /* PAD_TOP == 1 || || PAD_BOTTM == 1 */
Giorgio Arenae620a832020-02-17 16:33:20 +0000536#elif KERNEL_SIZE == 3
Giorgio Arena3c4bf0c2020-03-02 09:49:29 +0000537#if(PAD_TOP > 0) || (PAD_BOTTOM > 0)
Giorgio Arenae620a832020-02-17 16:33:20 +0000538 if(y_coord < 0) // special case Z = -1 doesn't exists
539 {
540 //skip first row and load the two next ones
541 CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_z));
542 CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_z));
543 }
Giorgio Arena3c4bf0c2020-03-02 09:49:29 +0000544 else if(y_coord == (SRC_HEIGHT - PAD_BOTTOM - 1))
Giorgio Arenae620a832020-02-17 16:33:20 +0000545 {
546 // special case when computing the last row of the output we must read the last three rows from the input buffer (including padding) but the
547 // Z axis has no padding at all.
548 CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_z));
549 CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_z));
550 }
551 else
552 {
553 CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_z));
554 CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_z));
555 CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_z));
556 }
Giorgio Arena3c4bf0c2020-03-02 09:49:29 +0000557#else // PAD_TOP > 0 || PAD_BOTTOM > 0
Giorgio Arenae620a832020-02-17 16:33:20 +0000558 CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_z));
559 CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_z));
560 CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_z), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_z));
Giorgio Arena3c4bf0c2020-03-02 09:49:29 +0000561#endif // PAD_TOP > 0 || PAD_BOTTOM > 0
Giorgio Arenae620a832020-02-17 16:33:20 +0000562#elif KERNEL_SIZE == 1
563 int weight = convert_int(*(__global DATA_TYPE *)weights_addr);
564 int8 input_value = convert_int8(INPUT_VALUE((__global DATA_TYPE *)src_addr, src_stride_y));
565 values0 += (input_value + input_offset) * ((int8)weight + weight_offset);
566#endif /* (KERNEL_SIZE == 1) || (KERNEL_SIZE == 3) || (KERNEL_SIZE == 5) */
567
568 src_addr += src_stride_x;
569 weights_addr += weights_stride_x;
570 }
571
572#ifdef HAS_BIAS
573 Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases);
Giorgio Arena3c4bf0c2020-03-02 09:49:29 +0000574 __global int *bias_addr = ((__global int *)(vector_offset(&biases, id0)));
Giorgio Arenae620a832020-02-17 16:33:20 +0000575 values0 += (int8)(*bias_addr);
576#endif /* defined(HAS_BIAS) */
577
578#if OUTPUT_SHIFT < 0
579 values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
580#else // OUTPUT_SHIFT < 0
581 values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
582#endif // OUTPUT_SHIFT < 0
583 values0 = values0 + output_offset;
584
585 VEC_DATA_TYPE(DATA_TYPE, 8)
586 values = CONVERT_SAT(values0, DATA_TYPE);
587 *(dst.ptr + 0 * dst_stride_y) = values.s0;
588 *(dst.ptr + 1 * dst_stride_y) = values.s1;
589 *(dst.ptr + 2 * dst_stride_y) = values.s2;
590 *(dst.ptr + 3 * dst_stride_y) = values.s3;
591 *(dst.ptr + 4 * dst_stride_y) = values.s4;
592 *(dst.ptr + 5 * dst_stride_y) = values.s5;
593 *(dst.ptr + 6 * dst_stride_y) = values.s6;
594 *(dst.ptr + 7 * dst_stride_y) = values.s7;
595}
596
597#else // defined(DATA_LAYOUT_NHWC)
598
Sang-Hoon Parkab5b1a22019-10-15 09:29:13 +0100599#if KERNEL_SIZE == 9
600
601#if STRIDE_X == 1
602#define CONVOLUTION1x9(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x9_STRIDE1(acc, src_row_ptr, weights_row_ptr)
603#elif STRIDE_X == 2
604#define CONVOLUTION1x9(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x9_STRIDE2(acc, src_row_ptr, weights_row_ptr)
605#else /* STRIDE_X not equals 1 or 2 */
606#error "STRIDE_X larger than 2 is not supported"
607#endif /* STRIDE_X */
608
609#define CONVOLUTION1x9_STRIDE1(acc, src_row_ptr, weights_row_ptr) \
610 ({ \
611 int8 weights_values0 = convert_int8(vload8(0, weights_row_ptr)); \
612 int weights_value1 = convert_int(*(weights_row_ptr + 8)); \
613 int16 src0 = convert_int16(vload16(0, src_row_ptr)); \
614 acc += (src0.lo + input_offset) * ((int8)weights_values0.s0 + weight_offset); \
615 acc += ((int8)(src0.s1234, src0.s5678) + input_offset) * ((int8)weights_values0.s1 + weight_offset); \
616 acc += ((int8)(src0.s2345, src0.s6789) + input_offset) * ((int8)weights_values0.s2 + weight_offset); \
617 acc += ((int8)(src0.s3456, src0.s789A) + input_offset) * ((int8)weights_values0.s3 + weight_offset); \
618 acc += ((int8)(src0.s4567, src0.s89AB) + input_offset) * ((int8)weights_values0.s4 + weight_offset); \
619 acc += ((int8)(src0.s5678, src0.s9ABC) + input_offset) * ((int8)weights_values0.s5 + weight_offset); \
620 acc += ((int8)(src0.s6789, src0.sABCD) + input_offset) * ((int8)weights_values0.s6 + weight_offset); \
621 acc += ((int8)(src0.s789A, src0.sBCDE) + input_offset) * ((int8)weights_values0.s7 + weight_offset); \
622 acc += ((int8)(src0.s89AB, src0.sCDEF) + input_offset) * ((int8)weights_value1 + weight_offset); \
623 })
624
625#define CONVOLUTION1x9_STRIDE2(acc, src_row_ptr, weights_row_ptr) \
626 ({ \
627 int8 weights_values0 = convert_int8(vload8(0, weights_row_ptr)); \
628 int weights_value1 = convert_int(*(weights_row_ptr + 8)); \
629 int16 src0 = convert_int16(vload16(0, src_row_ptr)); \
630 int8 src1 = convert_int8(vload8(0, src_row_ptr + 16)); \
631 acc += (src0.even + input_offset) * ((int8)weights_values0.s0 + weight_offset); \
632 acc += ((int8)(src0.s1357, src0.s9BDF) + input_offset) * ((int8)weights_values0.s1 + weight_offset); \
633 acc += ((int8)(src0.s2468, src0.sACE, src1.s0) + input_offset) * ((int8)weights_values0.s2 + weight_offset); \
634 acc += ((int8)(src0.s3579, src0.sBDF, src1.s1) + input_offset) * ((int8)weights_values0.s3 + weight_offset); \
635 acc += ((int8)(src0.s468A, src0.sCE, src1.s02) + input_offset) * ((int8)weights_values0.s4 + weight_offset); \
636 acc += ((int8)(src0.s579B, src0.sDF, src1.s13) + input_offset) * ((int8)weights_values0.s5 + weight_offset); \
637 acc += ((int8)(src0.s68AC, src0.sE, src1.s024) + input_offset) * ((int8)weights_values0.s6 + weight_offset); \
638 acc += ((int8)(src0.s79BD, src0.sF, src1.s135) + input_offset) * ((int8)weights_values0.s7 + weight_offset); \
639 acc += ((int8)(src0.s8ACE, src1.s0246) + input_offset) * ((int8)weights_value1 + weight_offset); \
640 })
641
642#elif KERNEL_SIZE == 5
Chunosovd621bca2017-11-03 17:33:15 +0700643
644#if STRIDE_X == 1
645#define CONVOLUTION1x5(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x5_STRIDE1(acc, src_row_ptr, weights_row_ptr)
646#elif STRIDE_X == 2
647#define CONVOLUTION1x5(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x5_STRIDE2(acc, src_row_ptr, weights_row_ptr)
648#else /* STRIDE_X not equals 1 or 2 */
649#error "STRIDE_X larger than 2 is not supported"
650#endif /* STRIDE_X */
651
652#define CONVOLUTION1x5_STRIDE1(acc, src_row_ptr, weights_row_ptr) \
653 ({ \
654 int4 weights_values0 = convert_int4(vload4(0, weights_row_ptr)); \
655 int weights_value1 = convert_int(*(weights_row_ptr + 4)); \
656 int8 src0 = convert_int8(vload8(0, src_row_ptr)); \
657 int4 src1 = convert_int4(vload4(0, src_row_ptr + 8)); \
658 acc += (src0 + input_offset) * ((int8)weights_values0.s0 + weight_offset); \
659 acc += ((int8)(src0.s1234, src0.s567, src1.s0) + input_offset) * ((int8)weights_values0.s1 + weight_offset); \
660 acc += ((int8)(src0.s234, src0.s567, src1.s01) + input_offset) * ((int8)weights_values0.s2 + weight_offset); \
661 acc += ((int8)(src0.s345, src0.s67, src1.s012) + input_offset) * ((int8)weights_values0.s3 + weight_offset); \
662 acc += ((int8)(src0.s45, src0.s67, src1.s0123) + input_offset) * ((int8)weights_value1 + weight_offset); \
663 })
664
665#define CONVOLUTION1x5_STRIDE2(acc, src_row_ptr, weights_row_ptr) \
666 ({ \
667 int4 weights_values0 = convert_int4(vload4(0, weights_row_ptr)); \
668 int weights_value1 = convert_int(*(weights_row_ptr + 4)); \
669 int16 src0 = convert_int16(vload16(0, src_row_ptr)); \
670 int4 src1 = convert_int4(vload4(0, src_row_ptr + 16)); \
671 acc += (src0.even + input_offset) * ((int8)weights_values0.s0 + weight_offset); \
672 acc += ((int8)(src0.s1357, src0.s9BDF) + input_offset) * ((int8)weights_values0.s1 + weight_offset); \
673 acc += ((int8)(src0.s2468, src0.sACE, src1.s0) + input_offset) * ((int8)weights_values0.s2 + weight_offset); \
674 acc += ((int8)(src0.s3579, src0.sBDF, src1.s1) + input_offset) * ((int8)weights_values0.s3 + weight_offset); \
675 acc += ((int8)(src0.s468a, src0.sCE, src1.s02) + input_offset) * ((int8)weights_value1 + weight_offset); \
676 })
677
678#elif KERNEL_SIZE == 3
679
680#if STRIDE_X == 1
681#define CONVOLUTION1x3(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x3_STRIDE1(acc, src_row_ptr, weights_row_ptr)
682#elif STRIDE_X == 2
683#define CONVOLUTION1x3(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x3_STRIDE2(acc, src_row_ptr, weights_row_ptr)
684#else /* STRIDE_X not equals 1 or 2 */
685#error "STRIDE_X larger than 2 is not supported"
686#endif /* STRIDE_X */
687
688#define CONVOLUTION1x3_STRIDE1(acc, src_row_ptr, weights_row_ptr) \
689 ({ \
690 int3 weights_values0 = convert_int3(vload3(0, weights_row_ptr)); \
691 int8 src0 = convert_int8(vload8(0, src_row_ptr)); \
692 int2 src1 = convert_int2(vload2(0, src_row_ptr + 8)); \
693 acc += (src0 + input_offset) * ((int8)weights_values0.s0 + weight_offset); \
694 acc += ((int8)(src0.s1234, src0.s567, src1.s0) + input_offset) * ((int8)weights_values0.s1 + weight_offset); \
695 acc += ((int8)(src0.s234, src0.s567, src1.s01) + input_offset) * ((int8)weights_values0.s2 + weight_offset); \
696 })
697
698#define CONVOLUTION1x3_STRIDE2(acc, src_row_ptr, weights_row_ptr) \
699 ({ \
700 int3 weights_values0 = convert_int3(vload3(0, weights_row_ptr)); \
701 int16 src0 = convert_int16(vload16(0, src_row_ptr)); \
702 int src1 = convert_int(*(src_row_ptr + 16)); \
703 acc += (src0.even + input_offset) * ((int8)weights_values0.s0 + weight_offset); \
704 acc += ((int8)(src0.s1357, src0.s9BDF) + input_offset) * ((int8)weights_values0.s1 + weight_offset); \
705 acc += ((int8)(src0.s2468, src0.sACE, src1) + input_offset) * ((int8)weights_values0.s2 + weight_offset); \
706 })
707
708#elif KERNEL_SIZE == 1
709
710#if STRIDE_X == 3
Giorgio Arenae620a832020-02-17 16:33:20 +0000711#define INPUT_VALUE extract_input_stride3
Chunosovd621bca2017-11-03 17:33:15 +0700712#elif STRIDE_X == 2
Giorgio Arenae620a832020-02-17 16:33:20 +0000713#define INPUT_VALUE extract_input_stride2
Chunosovd621bca2017-11-03 17:33:15 +0700714#elif STRIDE_X == 1
Giorgio Arenae620a832020-02-17 16:33:20 +0000715#define INPUT_VALUE extract_input_stride1
Chunosovd621bca2017-11-03 17:33:15 +0700716
717#else /* STRIDE_X not equals 1, 2 or 3 */
718#error "Only support strides 1, 2 and 3"
719#endif /* STRIDE_X */
720
721/** Extracts a 1D horizontal vector from the input tensor with stride as 1.
722 *
Giorgio Arenae620a832020-02-17 16:33:20 +0000723 * @param[in] input_value Pointer to the first value.
Chunosovd621bca2017-11-03 17:33:15 +0700724 *
Giorgio Arenae620a832020-02-17 16:33:20 +0000725 * @return extracted input values.
Chunosovd621bca2017-11-03 17:33:15 +0700726 */
Giorgio Arenae620a832020-02-17 16:33:20 +0000727inline VEC_DATA_TYPE(DATA_TYPE, 8) extract_input_stride1(__global const DATA_TYPE *input_value)
Chunosovd621bca2017-11-03 17:33:15 +0700728{
Giorgio Arenae620a832020-02-17 16:33:20 +0000729 return vload8(0, input_value);
Chunosovd621bca2017-11-03 17:33:15 +0700730}
731
732/** Extracts a 1D horizontal vector from the input tensor with stride as 2.
733 *
Giorgio Arenae620a832020-02-17 16:33:20 +0000734 * @param[in] input_value Pointer to the first value.
Chunosovd621bca2017-11-03 17:33:15 +0700735 *
Giorgio Arenae620a832020-02-17 16:33:20 +0000736 * @return extracted input values.
Chunosovd621bca2017-11-03 17:33:15 +0700737 */
Giorgio Arenae620a832020-02-17 16:33:20 +0000738inline VEC_DATA_TYPE(DATA_TYPE, 8) extract_input_stride2(__global const DATA_TYPE *input_value)
Chunosovd621bca2017-11-03 17:33:15 +0700739{
Sheri Zhang681f2d42020-02-20 11:23:08 +0000740 VEC_DATA_TYPE(DATA_TYPE, 16)
Giorgio Arenae620a832020-02-17 16:33:20 +0000741 temp = vload16(0, input_value);
Chunosovd621bca2017-11-03 17:33:15 +0700742 return temp.s02468ace;
743}
744
745/** Extracts a 1D horizontal vector from the input tensor with stride as 3 and 8-bit data size.
746 *
Giorgio Arenae620a832020-02-17 16:33:20 +0000747 * @param[in] input_value Pointer to the first value.
Chunosovd621bca2017-11-03 17:33:15 +0700748 *
Giorgio Arenae620a832020-02-17 16:33:20 +0000749 * @return extracted input values.
Chunosovd621bca2017-11-03 17:33:15 +0700750 */
Giorgio Arenae620a832020-02-17 16:33:20 +0000751inline VEC_DATA_TYPE(DATA_TYPE, 8) extract_input_stride3(__global const DATA_TYPE *input_value)
Chunosovd621bca2017-11-03 17:33:15 +0700752{
Sheri Zhang681f2d42020-02-20 11:23:08 +0000753 VEC_DATA_TYPE(DATA_TYPE, 16)
Giorgio Arenae620a832020-02-17 16:33:20 +0000754 temp1 = vload16(0, input_value);
Sheri Zhang681f2d42020-02-20 11:23:08 +0000755 VEC_DATA_TYPE(DATA_TYPE, 16)
Giorgio Arenae620a832020-02-17 16:33:20 +0000756 temp2 = vload16(0, input_value + 12);
Sheri Zhang681f2d42020-02-20 11:23:08 +0000757 return (VEC_DATA_TYPE(DATA_TYPE, 8))(temp1.s0369, temp2.s0369);
Chunosovd621bca2017-11-03 17:33:15 +0700758}
759
Sang-Hoon Parkab5b1a22019-10-15 09:29:13 +0100760#else /* KERNEL_SIZE not equals 1, 3 , 5, 9 */
761#error "Only kernel sizes 1, 3, 5 and 9 are supported"
Chunosovd621bca2017-11-03 17:33:15 +0700762#endif /* KERNEL_SIZE */
763
764/** This kernel performs a direct convolution to convolve the low three dimensions.
765 *
766 * @note The convolution stride x must be passed at compile time using -DSTRIDE_X e.g. -DSTRIDE_X=1
767 * @note The third dimensions of the weights tensors must be passed at compile time using -DWEIGHTS_DEPTH
768 * @note If biases are used then -DHAS_BIAS has to be passed at compile time
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100769 * @note The output quantization multiplier must be passed at compile time using -DOUTPUT_MULTIPLIER e.g. -DOUTPUT_MULTIPLIER=1234
770 * @note The output quantization shift must be passed at compile time using -DOUTPUT_SHIFT e.g. -DOUTPUT_SHIFT=4
Chunosovd621bca2017-11-03 17:33:15 +0700771 *
Sheri Zhang681f2d42020-02-20 11:23:08 +0000772 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED
Chunosovd621bca2017-11-03 17:33:15 +0700773 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
774 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
775 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
776 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
777 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
778 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
779 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
780 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
781 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
782 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
783 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
784 * @param[in] dst_step_y dst_stride_y * number of elements along Z processed per workitem(in bytes)
785 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
786 * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
787 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
Sheri Zhang681f2d42020-02-20 11:23:08 +0000788 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr
Chunosovd621bca2017-11-03 17:33:15 +0700789 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
790 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
791 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
792 * @param[in] weights_step_y weights_stride_y * number of elements along y processed per workitem(in bytes)
793 * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes)
794 * @param[in] weights_step_z weights_stride_z * number of elements along Z processed per workitem(in bytes)
795 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
Georgios Pinitas540d0082017-11-17 10:55:00 +0000796 * @param[in] biases_ptr Pointer to the biases tensor. Supported data types: S32
Chunosovd621bca2017-11-03 17:33:15 +0700797 * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes)
798 * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes)
799 * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor
800 * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension
801 * @param[in] input_offset Input offset quantization parameter
802 * @param[in] weight_offset Weights offset quantization parameter
803 * @param[in] output_offset Output offset quantization parameter
Chunosovd621bca2017-11-03 17:33:15 +0700804 */
Sang-Hoon Parkab5b1a22019-10-15 09:29:13 +0100805__kernel void direct_convolution_quantized(
Chunosovd621bca2017-11-03 17:33:15 +0700806 TENSOR3D_DECLARATION(src),
807 TENSOR3D_DECLARATION(dst),
808 TENSOR3D_DECLARATION(weights),
809#ifdef HAS_BIAS
810 VECTOR_DECLARATION(biases),
811#endif /* defined(HAS_BIAS) */
812 unsigned int weights_stride_w,
813 int input_offset,
814 int weight_offset,
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100815 int output_offset)
Chunosovd621bca2017-11-03 17:33:15 +0700816{
817 Image src = CONVERT_TO_IMAGE_STRUCT(src);
818 Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(weights);
819 Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst);
820
Giorgio Arenae620a832020-02-17 16:33:20 +0000821 int8 values0 = 0;
Chunosovd621bca2017-11-03 17:33:15 +0700822
Sheri Zhang681f2d42020-02-20 11:23:08 +0000823 __global DATA_TYPE *weights_addr = (__global DATA_TYPE *)tensor3D_offset(&weights, 0, 0, 0);
824 __global DATA_TYPE *src_addr = (__global DATA_TYPE *)offset(&src, 0, 0);
Chunosovd621bca2017-11-03 17:33:15 +0700825
826 const int kernel_index = get_global_id(2);
827 weights_addr += kernel_index * weights_stride_w;
828
829 for(volatile int d = 0; d < WEIGHTS_DEPTH; ++d)
830 {
Sang-Hoon Parkab5b1a22019-10-15 09:29:13 +0100831#if KERNEL_SIZE == 9
Giorgio Arenae620a832020-02-17 16:33:20 +0000832 CONVOLUTION1x9(values0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_y));
833 CONVOLUTION1x9(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_y));
834 CONVOLUTION1x9(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_y));
835 CONVOLUTION1x9(values0, (__global DATA_TYPE *)(src_addr + 3 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 3 * weights_stride_y));
836 CONVOLUTION1x9(values0, (__global DATA_TYPE *)(src_addr + 4 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 4 * weights_stride_y));
837 CONVOLUTION1x9(values0, (__global DATA_TYPE *)(src_addr + 5 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 5 * weights_stride_y));
838 CONVOLUTION1x9(values0, (__global DATA_TYPE *)(src_addr + 6 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 6 * weights_stride_y));
839 CONVOLUTION1x9(values0, (__global DATA_TYPE *)(src_addr + 7 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 7 * weights_stride_y));
840 CONVOLUTION1x9(values0, (__global DATA_TYPE *)(src_addr + 8 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 8 * weights_stride_y));
Sang-Hoon Parkab5b1a22019-10-15 09:29:13 +0100841#elif KERNEL_SIZE == 5
Giorgio Arenae620a832020-02-17 16:33:20 +0000842 CONVOLUTION1x5(values0, (__global DATA_TYPE *)src_addr, (__global DATA_TYPE *)weights_addr);
843 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_y));
844 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_y));
845 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 3 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 3 * weights_stride_y));
846 CONVOLUTION1x5(values0, (__global DATA_TYPE *)(src_addr + 4 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 4 * weights_stride_y));
Chunosovd621bca2017-11-03 17:33:15 +0700847#elif KERNEL_SIZE == 3
Giorgio Arenae620a832020-02-17 16:33:20 +0000848 CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_y));
849 CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_y));
850 CONVOLUTION1x3(values0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_y));
Chunosovd621bca2017-11-03 17:33:15 +0700851#elif KERNEL_SIZE == 1
Sheri Zhang681f2d42020-02-20 11:23:08 +0000852 int weight = convert_int(*(__global DATA_TYPE *)weights_addr);
Giorgio Arenae620a832020-02-17 16:33:20 +0000853 int8 input_value = convert_int8(INPUT_VALUE((__global DATA_TYPE *)src_addr));
854 values0 += (input_value + input_offset) * ((int8)weight + weight_offset);
Chunosovd621bca2017-11-03 17:33:15 +0700855#endif /* (KERNEL_SIZE == 1) || (KERNEL_SIZE == 3) || (KERNEL_SIZE == 5) */
856
857 src_addr += src_stride_z;
858 weights_addr += weights_stride_z;
859 }
860
861#ifdef HAS_BIAS
Georgios Pinitas540d0082017-11-17 10:55:00 +0000862 Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases);
863 __global int *bias_addr = ((__global int *)(vector_offset(&biases, kernel_index)));
Giorgio Arenae620a832020-02-17 16:33:20 +0000864 values0 += (int8)(*bias_addr);
Chunosovd621bca2017-11-03 17:33:15 +0700865#endif /* defined(HAS_BIAS) */
866
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100867#if OUTPUT_SHIFT < 0
Giorgio Arenae620a832020-02-17 16:33:20 +0000868 values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100869#else // OUTPUT_SHIFT < 0
Giorgio Arenae620a832020-02-17 16:33:20 +0000870 values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100871#endif // OUTPUT_SHIFT < 0
Giorgio Arenae620a832020-02-17 16:33:20 +0000872 values0 = values0 + output_offset;
Chunosovd621bca2017-11-03 17:33:15 +0700873
Giorgio Arenae620a832020-02-17 16:33:20 +0000874 vstore8(CONVERT_SAT(values0, DATA_TYPE), 0, (__global DATA_TYPE *)dst.ptr);
Chunosovd621bca2017-11-03 17:33:15 +0700875}
Giorgio Arenae620a832020-02-17 16:33:20 +0000876
877#endif // defined(DATA_LAYOUT_NHWC)
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100878#endif // defined(DATA_TYPE) && defined(STRIDE_X) && defined(WEIGHTS_DEPTH) && defined(OUTPUT_MULTIPLIER) && defined(OUTPUT_SHIFT)