blob: 08358755b1dc3300e207940956067dcb0a567045 [file] [log] [blame]
Dmitry Savenkod7295b72017-11-20 22:00:08 +07001/*
giuros016d109962019-01-07 17:47:19 +00002 * Copyright (c) 2017-2019 ARM Limited.
Dmitry Savenkod7295b72017-11-20 22:00:08 +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
25#include "helpers_asymm.h"
26
Michele Di Giorgioa046e162019-10-08 09:36:26 +010027#ifndef VEC_SIZE
28#if defined(N0)
29#define VEC_SIZE N0
30#else /* defined(N0) */
31#define VEC_SIZE 8
32#endif /* defined(N0) */
33#endif /* VEC_SIZE */
Giorgio Arena287b5702018-02-16 11:01:04 +000034
Usama Arif6a98a6e2019-05-10 17:07:27 +010035#if defined(ACTIVATION_TYPE) && defined(CONST_0)
Manuel Bottini30dbeef2019-06-26 16:23:03 +010036#include "activation_layer_quant.cl"
37#define ACTIVATION_FUNC(x) PERFORM_ACTIVATION_QUANT(ACTIVATION_TYPE, x)
Usama Arif6a98a6e2019-05-10 17:07:27 +010038#else /* defined(ACTIVATION_TYPE) && defined(CONST_0) */
Giorgio Arena99ac60b2018-02-16 15:17:23 +000039#define ACTIVATION_FUNC(x) (x)
Usama Arif6a98a6e2019-05-10 17:07:27 +010040#endif /* defined(ACTIVATION_TYPE) && defined(CONST_0) */
Giorgio Arena99ac60b2018-02-16 15:17:23 +000041
Michele Di Giorgioa046e162019-10-08 09:36:26 +010042#define VEC_INT VEC_DATA_TYPE(int, VEC_SIZE)
43#define VEC_FLOAT VEC_DATA_TYPE(float, VEC_SIZE)
Michele Di Giorgioa046e162019-10-08 09:36:26 +010044#define VEC_SHORT VEC_DATA_TYPE(short, VEC_SIZE)
45
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010046#if defined(DATA_TYPE) && defined(WEIGHTS_TYPE)
47
48#define VEC_TYPE(size) VEC_DATA_TYPE(DATA_TYPE, size)
49
50#if defined(WEIGHTS_OFFSET) && defined(INPUT_OFFSET) && defined(K_OFFSET) && (defined(OUTPUT_OFFSET) || defined(REAL_MULTIPLIER))
51
52#if defined(WEIGHTS_PROMOTED_TYPE)
53#define VEC_WEIGHTS_PROMOTED_TYPE(size) VEC_DATA_TYPE(WEIGHTS_PROMOTED_TYPE, size)
Michele Di Giorgioa046e162019-10-08 09:36:26 +010054
Georgios Pinitasdaa38552018-08-28 17:43:18 +010055#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
56#if defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice4b908652018-10-18 10:21:02 +010057#define ARM_DOT(x, y, val) val = arm_dot_acc((x), (y), val);
Georgios Pinitasdaa38552018-08-28 17:43:18 +010058#else // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice4b908652018-10-18 10:21:02 +010059#define ARM_DOT(x, y, val) val += arm_dot((x), (y));
Georgios Pinitasdaa38552018-08-28 17:43:18 +010060#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
61#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Giorgio Arenaeff8d952018-07-02 15:29:57 +010062
Georgios Pinitase55b40a2018-09-13 17:20:04 +010063#if defined(CONV_STRIDE_Y) && defined(CONV_STRIDE_X) && defined(DEPTH_MULTIPLIER) && defined(DST_CHANNELS)
Giorgio Arenadfca60b2018-01-31 10:30:59 +000064
Giorgio Arena287b5702018-02-16 11:01:04 +000065#if CONV_STRIDE_X > 3
66#error "Stride X not supported"
67#endif /* CONV_STRIDE_X > 3 */
Dmitry Savenkod7295b72017-11-20 22:00:08 +070068
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010069#if !defined(IS_DOT8)
Giorgio Arenaeff8d952018-07-02 15:29:57 +010070
Usama Arife73686a2019-04-08 17:30:48 +010071#if DILATION_X == 1
72
Dmitry Savenkod7295b72017-11-20 22:00:08 +070073#if CONV_STRIDE_X == 1
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010074#define GET_VALUES(first_value, left, middle, right) \
75 ({ \
76 int8 temp0 = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value)), int8); \
77 int2 temp1 = CONVERT(vload2(0, (__global DATA_TYPE *)(first_value + 8 * sizeof(DATA_TYPE))), int2); \
Giorgio Arena287b5702018-02-16 11:01:04 +000078 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010079 left = CONVERT(temp0.s01234567, int8); \
80 middle = CONVERT((int8)(temp0.s1234, temp0.s567, temp1.s0), int8); \
81 right = CONVERT((int8)(temp0.s2345, temp0.s67, temp1.s01), int8); \
Giorgio Arena287b5702018-02-16 11:01:04 +000082 })
Dmitry Savenkod7295b72017-11-20 22:00:08 +070083#elif CONV_STRIDE_X == 2
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010084#define GET_VALUES(first_value, left, middle, right) \
85 ({ \
86 int16 temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value)), int16); \
87 int temp1 = CONVERT(*((__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE))), int); \
Giorgio Arena287b5702018-02-16 11:01:04 +000088 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010089 left = CONVERT(temp0.s02468ace, int8); \
90 middle = CONVERT(temp0.s13579bdf, int8); \
91 right = CONVERT((int8)(temp0.s2468, temp0.sace, temp1), int8); \
Giorgio Arena287b5702018-02-16 11:01:04 +000092 })
Dmitry Savenkod7295b72017-11-20 22:00:08 +070093#else /* CONV_STRIDE_X */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010094#define GET_VALUES(first_value, left, middle, right) \
95 ({ \
96 int16 temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value)), int16); \
97 int8 temp1 = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE))), int8); \
Giorgio Arena287b5702018-02-16 11:01:04 +000098 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010099 left = CONVERT((int8)(temp0.s0369, temp0.scf, temp1.s25), int8); \
100 middle = CONVERT((int8)(temp0.s147a, temp0.sd, temp1.s036), int8); \
101 right = CONVERT((int8)(temp0.s258b, temp0.se, temp1.s147), int8); \
Giorgio Arena287b5702018-02-16 11:01:04 +0000102 })
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700103#endif /* CONV_STRIDE_X */
104
Usama Arife73686a2019-04-08 17:30:48 +0100105#else /* DILATION_X == 1 */
106
107#if CONV_STRIDE_X == 1
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100108#define GET_VALUES(first_value, left, middle, right) \
109 ({ \
110 left = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value)), int8); \
111 middle = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))), int8); \
112 right = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))), int8); \
Usama Arife73686a2019-04-08 17:30:48 +0100113 })
114#elif CONV_STRIDE_X == 2
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100115#define GET_VALUES(first_value, left, middle, right) \
116 ({ \
117 int16 temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value)), int16); \
118 left = CONVERT(temp0.s02468ace, int8); \
Usama Arife73686a2019-04-08 17:30:48 +0100119 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100120 temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))), int16); \
121 middle = CONVERT(temp0.s02468ace, int8); \
Usama Arife73686a2019-04-08 17:30:48 +0100122 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100123 temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))), int16); \
124 right = CONVERT(temp0.s02468ace, int8); \
Usama Arife73686a2019-04-08 17:30:48 +0100125 })
126#else /* CONV_STRIDE_X */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100127#define GET_VALUES(first_value, left, middle, right) \
128 ({ \
129 int16 temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value)), int16); \
130 int8 temp1 = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE))), int8); \
131 left = CONVERT((int8)(temp0.s0369, temp0.scf, temp1.s25), int8); \
Usama Arife73686a2019-04-08 17:30:48 +0100132 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100133 temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))), int16); \
134 temp1 = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value + (16 + DILATION_X) * sizeof(DATA_TYPE))), int8); \
135 middle = CONVERT((int8)(temp0.s0369, temp0.scf, temp1.s25), int8); \
Usama Arife73686a2019-04-08 17:30:48 +0100136 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100137 temp0 = CONVERT(vload16(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))), int16); \
138 temp1 = CONVERT(vload8(0, (__global DATA_TYPE *)(first_value + (16 + 2 * DILATION_X) * sizeof(DATA_TYPE))), int8); \
139 right = CONVERT((int8)(temp0.s0369, temp0.scf, temp1.s25), int8); \
Usama Arife73686a2019-04-08 17:30:48 +0100140 })
141
142#endif /* CONV_STRIDE_X */
143#endif /* DILATION_X==1 */
144
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000145/** This function computes the depthwise convolution quantized.
Anthony Barbierf202e502017-11-23 18:02:04 +0000146 *
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100147 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8
148 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
149 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
150 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
151 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
152 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
153 * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes)
154 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
155 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: QASYMM8
156 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
157 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
158 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
159 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
160 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
161 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
162 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
163 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8/QSYMM8_PER_CHANNEL
164 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
165 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
166 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
167 * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes)
168 * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes)
169 * @param[in] weights_step_z weights_stride_z * number of elements along Y processed per workitem(in bytes)
170 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
171 * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32
172 * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes)
173 * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
174 * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector
175 * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32
176 * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes)
177 * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
178 * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector
179 * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32
180 * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes)
181 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
182 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector
Anthony Barbierf202e502017-11-23 18:02:04 +0000183 */
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700184
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100185__kernel void dwc_3x3_native_quantized8_nchw(
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700186 TENSOR3D_DECLARATION(src),
187 TENSOR3D_DECLARATION(dst),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100188 TENSOR3D_DECLARATION(weights),
189 VECTOR_DECLARATION(output_multipliers),
190 VECTOR_DECLARATION(output_shifts)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700191#if defined(HAS_BIAS)
Georgios Pinitas5b2191e2018-02-22 12:56:51 +0000192 ,
Giorgio Arena287b5702018-02-16 11:01:04 +0000193 VECTOR_DECLARATION(biases)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700194#endif //defined(HAS_BIAS)
Giorgio Arena287b5702018-02-16 11:01:04 +0000195)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700196{
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100197 Image src = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(src);
198 Image dst = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(dst);
199 Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(weights);
200 Vector output_multipliers = CONVERT_TO_VECTOR_STRUCT_NO_STEP(output_multipliers);
201 Vector output_shifts = CONVERT_TO_VECTOR_STRUCT_NO_STEP(output_shifts);
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100202
203 // Extract channel and linearized batch indices
204 const int channel = get_global_id(2) % DST_CHANNELS;
205 const int batch = get_global_id(2) / DST_CHANNELS;
206
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700207#if defined(HAS_BIAS)
208 Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases);
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700209
Georgios Pinitas728d3cf2018-09-21 13:41:35 +0100210 int bias_value = *((__global int *)(vector_offset(&biases, channel)));
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700211#endif //defined(HAS_BIAS)
212
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100213 // Load relevant input and weights data (Accounts depth multiplier when indexing input, OFM = IFM * DEPTH_MULTIPLIER)
214 src.ptr -= batch * (DST_CHANNELS / DEPTH_MULTIPLIER) * (DEPTH_MULTIPLIER - 1) * src_step_z + (channel - (channel / DEPTH_MULTIPLIER)) * src_step_z;
215 __global uchar *weights_addr = weights.ptr + get_global_id(0) * weights_step_x + get_global_id(1) * weights_step_y + channel * weights_step_z;
Giorgio Arena76572242018-04-04 17:44:26 +0100216
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100217 VEC_DATA_TYPE(WEIGHTS_TYPE, 3)
218 w0 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 0 * weights_stride_y));
219 VEC_DATA_TYPE(WEIGHTS_TYPE, 3)
220 w1 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 1 * weights_stride_y));
221 VEC_DATA_TYPE(WEIGHTS_TYPE, 3)
222 w2 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 2 * weights_stride_y));
223
224#if defined(PER_CHANNEL_QUANTIZATION)
225 const int output_multiplier = *((__global int *)vector_offset(&output_multipliers, channel));
226 const int output_shift = *((__global int *)vector_offset(&output_shifts, channel));
227#else // defined(PER_CHANNEL_QUANTIZATION)
228 const int output_multiplier = *((__global int *)vector_offset(&output_multipliers, 0));
229 const int output_shift = *((__global int *)vector_offset(&output_shifts, 0));
230#endif // defined(PER_CHANNEL_QUANTIZATION)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700231
Giorgio Arena287b5702018-02-16 11:01:04 +0000232 int8 values0 = 0;
233 int8 sum0 = 0;
Usama Arife73686a2019-04-08 17:30:48 +0100234#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000235 int8 values1 = 0;
236 int8 sum1 = 0;
Usama Arife73686a2019-04-08 17:30:48 +0100237#endif /* CONV_STRIDE_Y &&DILATION_Y==1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000238
239 // Row0
240 int8 left, middle, right;
241 GET_VALUES(src.ptr + 0 * src_stride_y, left, middle, right);
242 values0 += left * (int8)(w0.s0);
243 values0 += middle * (int8)(w0.s1);
244 values0 += right * (int8)(w0.s2);
245
246#if WEIGHTS_OFFSET != 0
247 sum0 += left + middle + right;
248#endif /* WEIGHTS_OFFSET != 0 */
249
250 // Row1
Usama Arife73686a2019-04-08 17:30:48 +0100251 GET_VALUES(src.ptr + DILATION_Y * src_stride_y, left, middle, right);
Giorgio Arena287b5702018-02-16 11:01:04 +0000252 values0 += left * (int8)(w1.s0);
253 values0 += middle * (int8)(w1.s1);
254 values0 += right * (int8)(w1.s2);
Usama Arife73686a2019-04-08 17:30:48 +0100255#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000256 values1 += left * (int8)(w0.s0);
257 values1 += middle * (int8)(w0.s1);
258 values1 += right * (int8)(w0.s2);
Usama Arife73686a2019-04-08 17:30:48 +0100259#endif /* CONV_STRIDE_Y && DILATION_Y== 1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000260
261#if WEIGHTS_OFFSET != 0
262 int8 tmp = left + middle + right;
263 sum0 += tmp;
Usama Arife73686a2019-04-08 17:30:48 +0100264#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000265 sum1 += tmp;
Usama Arife73686a2019-04-08 17:30:48 +0100266#endif /* CONV_STRIDE_Y &&DILATION_Y== 1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000267#endif /* WEIGHTS_OFFSET != 0 */
268
269 // Row2
Usama Arife73686a2019-04-08 17:30:48 +0100270 GET_VALUES(src.ptr + 2 * DILATION_Y * src_stride_y, left, middle, right);
Giorgio Arena287b5702018-02-16 11:01:04 +0000271 values0 += left * (int8)(w2.s0);
272 values0 += middle * (int8)(w2.s1);
273 values0 += right * (int8)(w2.s2);
Usama Arife73686a2019-04-08 17:30:48 +0100274#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000275 values1 += left * (int8)(w1.s0);
276 values1 += middle * (int8)(w1.s1);
277 values1 += right * (int8)(w1.s2);
Usama Arife73686a2019-04-08 17:30:48 +0100278#endif /* CONV_STRIDE_Y &&DILATION_Y == 1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000279
280#if WEIGHTS_OFFSET != 0
281 tmp = left + middle + right;
282 sum0 += tmp;
Usama Arife73686a2019-04-08 17:30:48 +0100283#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000284 sum1 += tmp;
Usama Arife73686a2019-04-08 17:30:48 +0100285#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000286#endif /* WEIGHTS_OFFSET != 0 */
287
Usama Arife73686a2019-04-08 17:30:48 +0100288#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000289 // Row3
290 GET_VALUES(src.ptr + 3 * src_stride_y, left, middle, right);
291 values1 += left * (int8)(w2.s0);
292 values1 += middle * (int8)(w2.s1);
293 values1 += right * (int8)(w2.s2);
294
295#if WEIGHTS_OFFSET != 0
296 sum1 += left + middle + right;
297#endif /* WEIGHTS_OFFSET != 0 */
Usama Arife73686a2019-04-08 17:30:48 +0100298#endif /* CONV_STRIDE_Y && DILATION_Y == 1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000299
300#if defined(HAS_BIAS)
301 values0 += (int8)(bias_value);
Usama Arife73686a2019-04-08 17:30:48 +0100302#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000303 values1 += (int8)(bias_value);
Usama Arife73686a2019-04-08 17:30:48 +0100304#endif /* CONV_STRIDE_Y & &DILATION_Y == 1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000305#endif //defined(HAS_BIAS)
306
307#if WEIGHTS_OFFSET != 0
308 values0 += sum0 * (int8)(WEIGHTS_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100309#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000310 values1 += sum1 * (int8)(WEIGHTS_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100311#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000312#endif /* WEIGHTS_OFFSET != 0 */
313
314#if INPUT_OFFSET != 0
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100315 VEC_WEIGHTS_PROMOTED_TYPE(3)
316 tmp_we = CONVERT(w0, VEC_WEIGHTS_PROMOTED_TYPE(3)) + CONVERT(w1, VEC_WEIGHTS_PROMOTED_TYPE(3)) + CONVERT(w2, VEC_WEIGHTS_PROMOTED_TYPE(3));
317
318 WEIGHTS_PROMOTED_TYPE sum_weights = tmp_we.s0 + tmp_we.s1 + tmp_we.s2;
Giorgio Arena287b5702018-02-16 11:01:04 +0000319 values0 += sum_weights * (int8)(INPUT_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100320#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000321 values1 += sum_weights * (int8)(INPUT_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100322#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000323#endif /* INPUT_OFFSET != 0 */
324
325#if K_OFFSET != 0
326 values0 += (int8)(K_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100327#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000328 values1 += (int8)(K_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100329#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/
Giorgio Arena287b5702018-02-16 11:01:04 +0000330#endif /* K_OFFSET != 0 */
331
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100332#if defined(REAL_MULTIPLIER)
333
334 values0 = CONVERT(round(CONVERT(values0, float8) * (float8)REAL_MULTIPLIER), int8);
335
336#else // defined(REAL_MULTIPLIER)
337
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100338 values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values0, output_multiplier, output_shift, 8);
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100339
340#endif // defined(REAL_MULTIPLIER)
341
Giorgio Arena287b5702018-02-16 11:01:04 +0000342 values0 += (int8)OUTPUT_OFFSET;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100343 VEC_TYPE(8)
344 res0 = CONVERT_SAT(values0, VEC_TYPE(8));
Giorgio Arena287b5702018-02-16 11:01:04 +0000345
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000346 vstore8(ACTIVATION_FUNC(res0), 0, dst.ptr);
Usama Arife73686a2019-04-08 17:30:48 +0100347#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100348#if defined(REAL_MULTIPLIER)
349
350 values1 = CONVERT(round(CONVERT(values1, float8) * (float8)REAL_MULTIPLIER), int8);
351
352#else // defined(REAL_MULTIPLIER)
Giorgio Arena287b5702018-02-16 11:01:04 +0000353
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100354 values1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values1, output_multiplier, output_shift, 8);
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100355
356#endif // defined(REAL_MULTIPLIER)
357
Giorgio Arena287b5702018-02-16 11:01:04 +0000358 values1 += (int8)OUTPUT_OFFSET;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100359 VEC_TYPE(8)
360 res1 = CONVERT_SAT(values1, VEC_TYPE(8));
Giorgio Arena287b5702018-02-16 11:01:04 +0000361
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000362 vstore8(ACTIVATION_FUNC(res1), 0, dst.ptr + dst_stride_y);
Usama Arife73686a2019-04-08 17:30:48 +0100363#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700364}
Giorgio Arena287b5702018-02-16 11:01:04 +0000365
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100366#else // !defined(IS_DOT8)
367
Usama Arife73686a2019-04-08 17:30:48 +0100368#if DILATION_X == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100369#if CONV_STRIDE_X == 1
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100370#define GET_VALUES(first_value, left, middle, right) \
371 ({ \
372 VEC_TYPE(8) \
373 temp0 = vload8(0, (__global DATA_TYPE *)(first_value)); \
374 VEC_TYPE(2) \
375 temp1 = vload2(0, (__global DATA_TYPE *)(first_value + 8 * sizeof(DATA_TYPE))); \
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100376 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100377 left = temp0.s01234567; \
378 middle = (VEC_TYPE(8))(temp0.s1234, temp0.s567, temp1.s0); \
379 right = (VEC_TYPE(8))(temp0.s2345, temp0.s67, temp1.s01); \
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000380 })
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100381#elif CONV_STRIDE_X == 2
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100382#define GET_VALUES(first_value, left, middle, right) \
383 ({ \
384 VEC_TYPE(16) \
385 temp0 = vload16(0, (__global DATA_TYPE *)(first_value)); \
386 DATA_TYPE temp1 = *((__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE))); \
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100387 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100388 left = temp0.s02468ace; \
389 middle = temp0.s13579bdf; \
390 right = (VEC_TYPE(8))(temp0.s2468, temp0.sace, temp1); \
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100391 })
392#else /* CONV_STRIDE_X */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100393#define GET_VALUES(first_value, left, middle, right) \
394 ({ \
395 VEC_TYPE(16) \
396 temp0 = vload16(0, (__global DATA_TYPE *)(first_value)); \
397 VEC_TYPE(8) \
398 temp1 = vload8(0, (__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE))); \
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100399 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100400 left = (VEC_TYPE(8))(temp0.s0369, temp0.scf, temp1.s25); \
401 middle = (VEC_TYPE(8))(temp0.s147a, temp0.sd, temp1.s036); \
402 right = (VEC_TYPE(8))(temp0.s258b, temp0.se, temp1.s147); \
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100403 })
404#endif /* CONV_STRIDE_X */
Usama Arife73686a2019-04-08 17:30:48 +0100405#else /*DILATION_X==1*/
406
407#if CONV_STRIDE_X == 1
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100408#define GET_VALUES(first_value, left, middle, right) \
409 ({ \
410 left = vload8(0, (__global DATA_TYPE *)(first_value)); \
411 middle = vload8(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))); \
412 right = vload8(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))); \
Usama Arife73686a2019-04-08 17:30:48 +0100413 })
414#elif CONV_STRIDE_X == 2
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100415#define GET_VALUES(first_value, left, middle, right) \
416 ({ \
417 VEC_TYPE(16) \
418 temp0 = vload16(0, (__global DATA_TYPE *)(first_value)); \
419 left = temp0.s02468ace; \
420 temp0 = vload16(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))); \
421 middle = temp0.s02468ace; \
422 temp0 = vload16(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))); \
423 right = temp0.s02468ace; \
Usama Arife73686a2019-04-08 17:30:48 +0100424 })
425#else /* CONV_STRIDE_X */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100426#define GET_VALUES(first_value, left, middle, right) \
427 ({ \
428 VEC_TYPE(16) \
429 temp0 = vload16(0, (__global DATA_TYPE *)(first_value)); \
430 VEC_TYPE(8) \
431 temp1 = vload8(0, (__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE)))); \
432 left = (VEC_TYPE(8))(temp0.s0369, temp0.scf, temp1.s25); \
Usama Arife73686a2019-04-08 17:30:48 +0100433 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100434 temp0 = vload16(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))); \
435 temp1 = vload8(0, (__global DATA_TYPE *)(first_value + (16 + DILATION_X) * sizeof(DATA_TYPE))); \
436 middle = (VEC_TYPE(8))(temp0.s0369, temp0.scf, temp1.s25); \
Usama Arife73686a2019-04-08 17:30:48 +0100437 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100438 temp0 = vload16(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))); \
439 temp1 = vload8(0, (__global DATA_TYPE *)(first_value + (16 + 2 * DILATION_X) * sizeof(DATA_TYPE))); \
440 right = (VEC_TYPE(8))(temp0.s0369, temp0.scf, temp1.s25); \
Usama Arife73686a2019-04-08 17:30:48 +0100441 })
442
443#endif /* CONV_STRIDE_X */
444#endif /*DILATION_X==1*/
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100445/** This function computes the depthwise convolution quantized using dot product when the data layout is NCHW.
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000446 *
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100447 * @note Per-channel quantization is not supported by this kernel.
448 *
449 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8
450 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
451 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
452 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
453 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
454 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
455 * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes)
456 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
457 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: QASYMM8
458 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
459 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
460 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
461 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
462 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
463 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
464 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
465 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8/QSYMM8_PER_CHANNEL
466 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
467 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
468 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
469 * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes)
470 * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes)
471 * @param[in] weights_step_z weights_stride_z * number of elements along Y processed per workitem(in bytes)
472 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
473 * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32
474 * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes)
475 * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
476 * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector
477 * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32
478 * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes)
479 * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
480 * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector
481 * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32
482 * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes)
483 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
484 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000485 */
486
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100487__kernel void dwc_3x3_native_quantized8_dot8_nchw(
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100488 TENSOR3D_DECLARATION(src),
489 TENSOR3D_DECLARATION(dst),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100490 TENSOR3D_DECLARATION(weights),
491 VECTOR_DECLARATION(output_multipliers),
492 VECTOR_DECLARATION(output_shifts)
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100493#if defined(HAS_BIAS)
494 ,
495 VECTOR_DECLARATION(biases)
496#endif //defined(HAS_BIAS)
497)
498{
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100499 Image src = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(src);
500 Image dst = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(dst);
501 Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(weights);
502 Vector output_multipliers = CONVERT_TO_VECTOR_STRUCT_NO_STEP(output_multipliers);
503 Vector output_shifts = CONVERT_TO_VECTOR_STRUCT_NO_STEP(output_shifts);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100504
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100505 // Extract channel and linearized batch indices
506 const int channel = get_global_id(2) % DST_CHANNELS;
507 const int batch = get_global_id(2) / DST_CHANNELS;
508
509#if defined(HAS_BIAS)
510 Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases);
511
512 const int bias_value = *((__global int *)(vector_offset(&biases, channel)));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100513#endif //defined(HAS_BIAS)
514
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100515 // Load relevant input and weights data (Accounts depth multiplier when indexing input, OFM = IFM * DEPTH_MULTIPLIER)
516 src.ptr -= batch * (DST_CHANNELS / DEPTH_MULTIPLIER) * (DEPTH_MULTIPLIER - 1) * src_step_z + (channel - (channel / DEPTH_MULTIPLIER)) * src_step_z;
517 __global uchar *weights_addr = weights.ptr + get_global_id(0) * weights_step_x + get_global_id(1) * weights_step_y + channel * weights_step_z;
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100518
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100519 VEC_TYPE(3)
520 w0 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 0 * weights_stride_y));
521 VEC_TYPE(3)
522 w1 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 1 * weights_stride_y));
523 VEC_TYPE(3)
524 w2 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 2 * weights_stride_y));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100525
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100526 const int output_multiplier = *((__global int *)vector_offset(&output_multipliers, 0));
527 const int output_shift = *((__global int *)vector_offset(&output_shifts, 0));
528
529 VEC_TYPE(8)
530 left0, middle0, right0;
531 VEC_TYPE(8)
532 left1, middle1, right1;
533 VEC_TYPE(8)
534 left2, middle2, right2;
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100535
536 int8 values0 = 0;
537 int8 sum0 = 0;
538
539 GET_VALUES(src.ptr + 0 * src_stride_y, left0, middle0, right0);
Usama Arife73686a2019-04-08 17:30:48 +0100540 GET_VALUES(src.ptr + DILATION_Y * src_stride_y, left1, middle1, right1);
541 GET_VALUES(src.ptr + 2 * DILATION_Y * src_stride_y, left2, middle2, right2);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100542
543#if WEIGHTS_OFFSET != 0
544 sum0 += convert_int8(left0) + convert_int8(middle0) + convert_int8(right0);
545 sum0 += convert_int8(left1) + convert_int8(middle1) + convert_int8(right1);
546 sum0 += convert_int8(left2) + convert_int8(middle2) + convert_int8(right2);
547#endif /* WEIGHTS_OFFSET != 0 */
548
Usama Arife73686a2019-04-08 17:30:48 +0100549#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100550 // If conv_stride_y is equals to 1, we compute two output rows
551
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100552 VEC_TYPE(8)
553 left3, middle3, right3;
554 int8 values1 = 0;
555 int8 sum1 = 0;
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100556
557 GET_VALUES(src.ptr + 3 * src_stride_y, left3, middle3, right3);
558
559#if WEIGHTS_OFFSET != 0
560 sum1 += convert_int8(left1) + convert_int8(middle1) + convert_int8(right1);
561 sum1 += convert_int8(left2) + convert_int8(middle2) + convert_int8(right2);
562 sum1 += convert_int8(left3) + convert_int8(middle3) + convert_int8(right3);
563#endif /* WEIGHTS_OFFSET != 0 */
Usama Arife73686a2019-04-08 17:30:48 +0100564#endif // CONV_STRIDE_Y == 1 && DILATION_Y==1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100565
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100566 ARM_DOT((VEC_TYPE(4))(left0.s0, middle0.s0, right0.s0, left1.s0), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s0);
567 ARM_DOT((VEC_TYPE(4))(middle1.s0, right1.s0, left2.s0, middle2.s0), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s0);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100568 values0.s0 += right2.s0 * w2.s2;
569
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100570 ARM_DOT((VEC_TYPE(4))(left0.s1, middle0.s1, right0.s1, left1.s1), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s1);
571 ARM_DOT((VEC_TYPE(4))(middle1.s1, right1.s1, left2.s1, middle2.s1), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s1);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100572 values0.s1 += right2.s1 * w2.s2;
573
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100574 ARM_DOT((VEC_TYPE(4))(left0.s2, middle0.s2, right0.s2, left1.s2), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s2);
575 ARM_DOT((VEC_TYPE(4))(middle1.s2, right1.s2, left2.s2, middle2.s2), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s2);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100576 values0.s2 += right2.s2 * w2.s2;
577
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100578 ARM_DOT((VEC_TYPE(4))(left0.s3, middle0.s3, right0.s3, left1.s3), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s3);
579 ARM_DOT((VEC_TYPE(4))(middle1.s3, right1.s3, left2.s3, middle2.s3), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s3);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100580 values0.s3 += right2.s3 * w2.s2;
581
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100582 ARM_DOT((VEC_TYPE(4))(left0.s4, middle0.s4, right0.s4, left1.s4), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s4);
583 ARM_DOT((VEC_TYPE(4))(middle1.s4, right1.s4, left2.s4, middle2.s4), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s4);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100584 values0.s4 += right2.s4 * w2.s2;
585
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100586 ARM_DOT((VEC_TYPE(4))(left0.s5, middle0.s5, right0.s5, left1.s5), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s5);
587 ARM_DOT((VEC_TYPE(4))(middle1.s5, right1.s5, left2.s5, middle2.s5), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s5);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100588 values0.s5 += right2.s5 * w2.s2;
589
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100590 ARM_DOT((VEC_TYPE(4))(left0.s6, middle0.s6, right0.s6, left1.s6), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s6);
591 ARM_DOT((VEC_TYPE(4))(middle1.s6, right1.s6, left2.s6, middle2.s6), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s6);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100592 values0.s6 += right2.s6 * w2.s2;
593
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100594 ARM_DOT((VEC_TYPE(4))(left0.s7, middle0.s7, right0.s7, left1.s7), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values0.s7);
595 ARM_DOT((VEC_TYPE(4))(middle1.s7, right1.s7, left2.s7, middle2.s7), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values0.s7);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100596 values0.s7 += right2.s7 * w2.s2;
597
Usama Arife73686a2019-04-08 17:30:48 +0100598#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100599 ARM_DOT((VEC_TYPE(4))(left1.s0, middle1.s0, right1.s0, left2.s0), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s0);
600 ARM_DOT((VEC_TYPE(4))(middle2.s0, right2.s0, left3.s0, middle3.s0), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s0);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100601 values1.s0 += right3.s0 * w2.s2;
602
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100603 ARM_DOT((VEC_TYPE(4))(left1.s1, middle1.s1, right1.s1, left2.s1), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s1);
604 ARM_DOT((VEC_TYPE(4))(middle2.s1, right2.s1, left3.s1, middle3.s1), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s1);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100605 values1.s1 += right3.s1 * w2.s2;
606
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100607 ARM_DOT((VEC_TYPE(4))(left1.s2, middle1.s2, right1.s2, left2.s2), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s2);
608 ARM_DOT((VEC_TYPE(4))(middle2.s2, right2.s2, left3.s2, middle3.s2), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s2);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100609 values1.s2 += right3.s2 * w2.s2;
610
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100611 ARM_DOT((VEC_TYPE(4))(left1.s3, middle1.s3, right1.s3, left2.s3), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s3);
612 ARM_DOT((VEC_TYPE(4))(middle2.s3, right2.s3, left3.s3, middle3.s3), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s3);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100613 values1.s3 += right3.s3 * w2.s2;
614
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100615 ARM_DOT((VEC_TYPE(4))(left1.s4, middle1.s4, right1.s4, left2.s4), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s4);
616 ARM_DOT((VEC_TYPE(4))(middle2.s4, right2.s4, left3.s4, middle3.s4), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s4);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100617 values1.s4 += right3.s4 * w2.s2;
618
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100619 ARM_DOT((VEC_TYPE(4))(left1.s5, middle1.s5, right1.s5, left2.s5), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s5);
620 ARM_DOT((VEC_TYPE(4))(middle2.s5, right2.s5, left3.s5, middle3.s5), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s5);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100621 values1.s5 += right3.s5 * w2.s2;
622
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100623 ARM_DOT((VEC_TYPE(4))(left1.s6, middle1.s6, right1.s6, left2.s6), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s6);
624 ARM_DOT((VEC_TYPE(4))(middle2.s6, right2.s6, left3.s6, middle3.s6), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s6);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100625 values1.s6 += right3.s6 * w2.s2;
626
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100627 ARM_DOT((VEC_TYPE(4))(left1.s7, middle1.s7, right1.s7, left2.s7), (VEC_TYPE(4))(w0.s0, w0.s1, w0.s2, w1.s0), values1.s7);
628 ARM_DOT((VEC_TYPE(4))(middle2.s7, right2.s7, left3.s7, middle3.s7), (VEC_TYPE(4))(w1.s1, w1.s2, w2.s0, w2.s1), values1.s7);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100629 values1.s7 += right3.s7 * w2.s2;
Usama Arife73686a2019-04-08 17:30:48 +0100630#endif // CONV_STRIDE_Y == 1 && DILATION_Y==1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100631
632#if defined(HAS_BIAS)
633 values0 += (int8)(bias_value);
Usama Arife73686a2019-04-08 17:30:48 +0100634#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100635 values1 += (int8)(bias_value);
Usama Arife73686a2019-04-08 17:30:48 +0100636#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100637#endif //defined(HAS_BIAS)
638
639#if WEIGHTS_OFFSET != 0
640 values0 += sum0 * (int8)(WEIGHTS_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100641#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100642 values1 += sum1 * (int8)(WEIGHTS_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100643#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100644#endif /* WEIGHTS_OFFSET != 0 */
645
646#if INPUT_OFFSET != 0
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100647 WEIGHTS_PROMOTED_TYPE sum_weights = 0;
648 VEC_WEIGHTS_PROMOTED_TYPE(3)
649 tmp_we = CONVERT(w0, VEC_WEIGHTS_PROMOTED_TYPE(3)) + CONVERT(w1, VEC_WEIGHTS_PROMOTED_TYPE(3)) + CONVERT(w2, VEC_WEIGHTS_PROMOTED_TYPE(3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100650 sum_weights += tmp_we.s0 + tmp_we.s1 + tmp_we.s2;
651 values0 += sum_weights * (int8)(INPUT_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100652#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100653 values1 += sum_weights * (int8)(INPUT_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100654#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100655#endif /* INPUT_OFFSET != 0 */
656
657#if K_OFFSET != 0
658 values0 += (int8)(K_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100659#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100660 values1 += (int8)(K_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100661#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100662#endif /* K_OFFSET != 0 */
663
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100664#if defined(REAL_MULTIPLIER)
665
666 values0 = CONVERT(round(CONVERT(values0, float8) * (float8)REAL_MULTIPLIER), int8);
667
668#else // defined(REAL_MULTIPLIER)
669
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100670 values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values0, output_multiplier, output_shift, 8);
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100671
672#endif // defined(REAL_MULTIPLIER)
673
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100674 values0 += (int8)OUTPUT_OFFSET;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100675 VEC_TYPE(8)
676 res0 = CONVERT_SAT(values0, VEC_TYPE(8));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100677
678 vstore8(ACTIVATION_FUNC(res0), 0, dst.ptr);
Usama Arife73686a2019-04-08 17:30:48 +0100679#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100680
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100681#if defined(REAL_MULTIPLIER)
682
683 values1 = CONVERT(round(CONVERT(values1, float8) * (float8)REAL_MULTIPLIER), int8);
684
685#else // defined(REAL_MULTIPLIER)
686
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100687 values1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values1, output_multiplier, output_shift, 8);
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100688
689#endif // defined(REAL_MULTIPLIER)
690
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100691 values1 += (int8)OUTPUT_OFFSET;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100692 VEC_TYPE(8)
693 res1 = CONVERT_SAT(values1, VEC_TYPE(8));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100694
695 vstore8(ACTIVATION_FUNC(res1), 0, dst.ptr + dst_stride_y);
Usama Arife73686a2019-04-08 17:30:48 +0100696#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100697}
698
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100699#endif // !defined(IS_DOT8)
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100700
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100701#endif /* defined(CONV_STRIDE_Y) && defined(CONV_STRIDE_X) && defined(DEPTH_MULTIPLIER) && defined(DST_CHANNELS) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100702
703#if defined(VEC_SIZE) && defined(SRC_DIM_1) && defined(SRC_DIM_2) && defined(CONV_PAD_TOP) && defined(CONV_PAD_LEFT)
704
705#define asymm_mult_by_quant_multiplier_less_than_one(x, y, z) ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(x, y, z, VEC_SIZE)
706
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100707#define MULTIPLY_ADD(x, y, acc) acc += CONVERT(CONVERT(x, VEC_WEIGHTS_PROMOTED_TYPE(VEC_SIZE)) * CONVERT(y, VEC_WEIGHTS_PROMOTED_TYPE(VEC_SIZE)), VEC_INT)
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100708
709#if WEIGHTS_OFFSET != 0
710#define MULTIPLY_ADD_ACCUMULATE(x, y, acc, sum) \
711 ({ \
712 sum += CONVERT(x, VEC_INT); \
713 MULTIPLY_ADD(x, y, acc); \
714 })
715#else /* WEIGHTS_OFFSET != 0 */
716#define MULTIPLY_ADD_ACCUMULATE(x, y, acc, sum) MULTIPLY_ADD(x, y, acc)
717#endif /* WEIGHTS_OFFSET != 0 */
718
Georgios Pinitasdaa38552018-08-28 17:43:18 +0100719#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
giuros016d109962019-01-07 17:47:19 +0000720#define DOT_PRODUCT(acc, val0, val1, val2, val3, val4, val5, val6, val7, val8, w0, w1) \
721 ({ \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100722 ARM_DOT((VEC_TYPE(4))(val0, val1, val2, val3), w0.s0123, acc); \
723 ARM_DOT((VEC_TYPE(4))(val4, val5, val6, val7), w0.s4567, acc); \
giuros016d109962019-01-07 17:47:19 +0000724 acc += val8 * w1; \
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100725 })
726
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100727#define DOT_PRODUCT_REDUCTION(sum, val0, val1, val2, val3, val4, val5, val6, val7, val8) \
728 ({ \
giuros016d109962019-01-07 17:47:19 +0000729 sum = val0; \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100730 ARM_DOT((VEC_TYPE(4))(val1, val2, val3, val4), (VEC_TYPE(4))1, sum); \
731 ARM_DOT((VEC_TYPE(4))(val5, val6, val7, val8), (VEC_TYPE(4))1, sum); \
giuros016d109962019-01-07 17:47:19 +0000732 })
733
734#define DOT_PRODUCT_REDUCTION_WEIGHTS(sum, w0, w1) \
735 ({ \
736 sum = w1; \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100737 ARM_DOT(w0.s0123, (VEC_TYPE(4))1, sum); \
738 ARM_DOT(w0.s4567, (VEC_TYPE(4))1, sum); \
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100739 })
740
Georgios Pinitasdaa38552018-08-28 17:43:18 +0100741#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100742
Pablo Tello47104362019-02-27 13:32:51 +0000743#if defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y) && VEC_SIZE == 4
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100744/** This function computes the depthwise convolution quantized for NHWC data layout when the stride along the width or height is not 1.
745 *
Pablo Tello47104362019-02-27 13:32:51 +0000746 * @note This kernel assumes VEC_SIZE is 4.
giuros016d109962019-01-07 17:47:19 +0000747 * @note The weights tensor is expected to be reshaped using @ref CLDepthwiseConvolutionLayerReshapeWeightsKernel.
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100748 * @note The number of elements read per thread must be passed at compile time using -DVEC_SIZE (e.g. -DVEC_SIZE=2)
749 * @note Dimension two of the input tensor (height for NHWC data layout) must be passed at compile time using -DSRC_DIM2 (e.g. -DSRC_DIM_2=112)
750 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_TOP (e.g. -DCONV_PAD_TOP=1)
751 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_LEFT (e.g. -DCONV_PAD_LEFT=1)
752 * @note The convolution stride along the width must be passed at compile time using -DCONV_STRIDE_X (e.g. -DCONV_STRIDE_Y=X)
753 * @note The convolution stride along the height must be passed at compile time using -DCONV_STRIDE_Y (e.g. -DCONV_STRIDE_Y=1)
754 *
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100755 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8
756 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
757 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
758 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
759 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
760 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
761 * @param[in] src_step_z src_stride_y * number of elements along Z processed per workitem(in bytes)
762 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes)
763 * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
764 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
765 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
766 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
767 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
768 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
769 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
770 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
771 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
772 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes)
773 * @param[in] dst_step_w dst_stride_w * number of elements along W processed per workitem(in bytes)
774 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
775 * @param[in] weights_ptr Pointer to the weights tensor reshaped. Supported data types: QASYMM8/QSYMM8_PER_CHANNEL
776 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
777 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
778 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
779 * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes)
780 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
781 * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32
782 * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes)
783 * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
784 * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector
785 * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32
786 * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes)
787 * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
788 * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector
789 * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32
790 * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes)
791 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
792 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector
793 * @param[in] max_offset Max offset for the input tensor
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100794 */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100795__kernel void dwc_3x3_reshaped_quantized8_nhwc(
Georgios Pinitas37044642018-10-30 14:53:25 +0000796 TENSOR4D_DECLARATION(src),
797 TENSOR4D_DECLARATION(dst),
giuros016d109962019-01-07 17:47:19 +0000798 IMAGE_DECLARATION(weights),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100799 VECTOR_DECLARATION(output_multipliers),
800 VECTOR_DECLARATION(output_shifts),
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100801#if defined(HAS_BIAS)
802 VECTOR_DECLARATION(biases),
803#endif /* defined(HAS_BIAS) */
804 int max_offset)
805{
806 const int x = get_global_id(0); // channels
807 const int y = get_global_id(1); // spatial coordinate x
Georgios Pinitas37044642018-10-30 14:53:25 +0000808#if defined(DST_DEPTH)
809 int z = get_global_id(2) % (int)DST_DEPTH; // spatial coordinate y
810 int b = get_global_id(2) / (int)DST_DEPTH; // batch
Pablo Tello47104362019-02-27 13:32:51 +0000811#else // defined(DST_DEPTH)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100812 int z = get_global_id(2); // spatial coordinate y
Pablo Tello47104362019-02-27 13:32:51 +0000813#endif // defined(DST_DEPTH)
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100814
giuros016d109962019-01-07 17:47:19 +0000815 __global uchar *weights_addr = weights_ptr + weights_offset_first_element_in_bytes + x * weights_stride_y;
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100816
Georgios Pinitas37044642018-10-30 14:53:25 +0000817#if defined(DST_DEPTH)
818 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE + b * src_stride_w;
819#else /* defined(DST_DEPTH) */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100820 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE;
Georgios Pinitas37044642018-10-30 14:53:25 +0000821#endif /* defined(DST_DEPTH) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100822
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100823 int z_coord = 0;
824 int4 offset = 0;
Usama Arife73686a2019-04-08 17:30:48 +0100825 int4 y_coord = ((int4)(y * CONV_STRIDE_X) + (int4)(0, DILATION_X * 1, DILATION_X * 2, DILATION_X * 3)) - (int)CONV_PAD_LEFT;
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100826
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100827 // Only for y = 0 we can have a negative coordinate. If so, we convert it to SRC_DIM_1
828 y_coord.s0 = min((uint)y_coord.s0, (uint)SRC_DIM_1);
829 y_coord.s1 = min((uint)y_coord.s1, (uint)SRC_DIM_1);
830 y_coord.s2 = min((uint)y_coord.s2, (uint)SRC_DIM_1);
831 y_coord.s3 = min((uint)y_coord.s3, (uint)SRC_DIM_1);
832
833 int4 y_offset = convert_int4(y_coord * (int)src_stride_y);
834
giuros016d109962019-01-07 17:47:19 +0000835 // We compute VEC_SIZEx1x1 [C,W,H] elements
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100836 VEC_INT acc = 0, sum = 0;
837
838 // Load weights
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100839 VEC_DATA_TYPE(WEIGHTS_TYPE, 16)
840 w0_tmp = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr));
841 VEC_DATA_TYPE(WEIGHTS_TYPE, 16)
842 w1_tmp = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr + 16));
843 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
844 w8 = VLOAD(4)(0, (__global WEIGHTS_TYPE *)(weights_addr + 2 * 16));
Pablo Tello47104362019-02-27 13:32:51 +0000845
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100846 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
847 w0 = w0_tmp.s0123;
848 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
849 w1 = w0_tmp.s4567;
850 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
851 w2 = w0_tmp.s89AB;
852 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
853 w3 = w0_tmp.sCDEF;
Pablo Tello47104362019-02-27 13:32:51 +0000854
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100855 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
856 w4 = w1_tmp.s0123;
857 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
858 w5 = w1_tmp.s4567;
859 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
860 w6 = w1_tmp.s89AB;
861 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
862 w7 = w1_tmp.sCDEF;
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100863
864#if INPUT_OFFSET != 0
Georgios Pinitas4e0d3812018-08-01 20:16:34 +0100865 VEC_INT sum_we = CONVERT(w0, VEC_INT) + CONVERT(w1, VEC_INT) + CONVERT(w2, VEC_INT)
866 + CONVERT(w3, VEC_INT) + CONVERT(w4, VEC_INT) + CONVERT(w5, VEC_INT)
867 + CONVERT(w6, VEC_INT) + CONVERT(w7, VEC_INT) + CONVERT(w8, VEC_INT);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100868#endif /* INPUT_OFFSET != 0 */
869
870 // Load input values
871 // z == 0
872 // Clamp z_coord as for z = 0, it can be negative
873 // z_coord is casted to unsigned int in order to use just a min() operation
874 // A "-1" 32 bit signed variable converted to unsigned gives 4294967295
Georgios Pinitas4e0d3812018-08-01 20:16:34 +0100875 z_coord = z * (int)CONV_STRIDE_Y - (int)CONV_PAD_TOP;
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100876 z_coord = min((uint)z_coord, (uint)SRC_DIM_2);
877 offset = y_offset + (int4)(z_coord * src_stride_z);
Georgios Pinitas4e0d3812018-08-01 20:16:34 +0100878 offset = min(offset, (int4)max_offset);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100879
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100880 VEC_TYPE(VEC_SIZE)
881 values0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
882 VEC_TYPE(VEC_SIZE)
883 values1 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
884 VEC_TYPE(VEC_SIZE)
885 values2 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100886
887 // z == 1
888 // z_coord can be only negative for z = 0 so we do not need to clamp it
889 // Moreover z_coord cannot be out-of-bound for z = 1 so we do not need to clamp the offset
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100890 z_coord = z * (int)CONV_STRIDE_Y - (int)CONV_PAD_TOP + DILATION_Y;
891 offset = y_offset + (int4)(z_coord * src_stride_z);
892 VEC_TYPE(VEC_SIZE)
893 values3 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
894 VEC_TYPE(VEC_SIZE)
895 values4 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
896 VEC_TYPE(VEC_SIZE)
897 values5 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100898
899 // z == 2
Usama Arife73686a2019-04-08 17:30:48 +0100900 // Offset can be out-of-bound so we need to check if it is greater than max_offset
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100901 z_coord = z * (int)CONV_STRIDE_Y - (int)CONV_PAD_TOP + DILATION_Y * 2;
902 offset = y_offset + (int4)(z_coord * src_stride_z);
903 offset = min(offset, (int4)max_offset);
904 VEC_TYPE(VEC_SIZE)
905 values6 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
906 VEC_TYPE(VEC_SIZE)
907 values7 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
908 VEC_TYPE(VEC_SIZE)
909 values8 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100910
911 MULTIPLY_ADD_ACCUMULATE(values0, w0, acc, sum);
912 MULTIPLY_ADD_ACCUMULATE(values1, w1, acc, sum);
913 MULTIPLY_ADD_ACCUMULATE(values2, w2, acc, sum);
914
915 MULTIPLY_ADD_ACCUMULATE(values3, w3, acc, sum);
916 MULTIPLY_ADD_ACCUMULATE(values4, w4, acc, sum);
917 MULTIPLY_ADD_ACCUMULATE(values5, w5, acc, sum);
918
919 MULTIPLY_ADD_ACCUMULATE(values6, w6, acc, sum);
920 MULTIPLY_ADD_ACCUMULATE(values7, w7, acc, sum);
921 MULTIPLY_ADD_ACCUMULATE(values8, w8, acc, sum);
922
923#if defined(HAS_BIAS)
924 Vector biases = CONVERT_TO_VECTOR_STRUCT(biases);
925 VEC_INT bias_values = VLOAD(VEC_SIZE)(0, (__global int *)biases.ptr);
926 acc += bias_values;
927#endif // defined(HAS_BIAS)
928
929#if WEIGHTS_OFFSET != 0
930 acc += WEIGHTS_OFFSET * sum;
931#endif /* WEIGHTS_OFFSET != 0 */
932
933#if INPUT_OFFSET != 0
934 acc += INPUT_OFFSET * sum_we;
935#endif /* INPUT_OFFSET != 0 */
936
937#if K_OFFSET != 0
938 acc += (VEC_INT)K_OFFSET;
939#endif /* K_OFFSET != 0 */
940
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100941#if defined(REAL_MULTIPLIER)
942
943 acc = CONVERT(round(CONVERT(acc, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
944
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100945#else // defined(REAL_MULTIPLIER)
946#if defined(PER_CHANNEL_QUANTIZATION)
947 Vector output_multipliers = CONVERT_TO_VECTOR_STRUCT(output_multipliers);
948 Vector output_shifts = CONVERT_TO_VECTOR_STRUCT(output_shifts);
949 VEC_INT output_multiplier = VLOAD(VEC_SIZE)(0, (__global int *)output_multipliers.ptr);
950 VEC_INT output_shift = VLOAD(VEC_SIZE)(0, (__global int *)output_shifts.ptr);
951#else // defined(PER_CHANNEL_QUANTIZATION)
952 const int output_multiplier = *((__global int *)(output_multipliers_ptr + output_multipliers_offset_first_element_in_bytes));
953 const int output_shift = *((__global int *)(output_shifts_ptr + output_shifts_offset_first_element_in_bytes));
954#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100955
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100956 acc = asymm_mult_by_quant_multiplier_less_than_one(acc, output_multiplier, output_shift);
957
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100958#endif // defined(REAL_MULTIPLIER)
959
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100960 acc += (VEC_INT)OUTPUT_OFFSET;
961
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100962 VEC_TYPE(VEC_SIZE)
963 res = CONVERT_SAT(acc, VEC_TYPE(VEC_SIZE));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100964
Georgios Pinitas37044642018-10-30 14:53:25 +0000965#if defined(DST_DEPTH)
966 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * dst_step_x + y * dst_step_y + z * dst_step_z + b * dst_stride_w;
967#else /* defined(DST_DEPTH) */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100968 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * dst_step_x + y * dst_step_y + z * dst_step_z;
Georgios Pinitas37044642018-10-30 14:53:25 +0000969#endif /* defined(DST_DEPTH) */
970
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100971 VSTORE(VEC_SIZE)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100972 (ACTIVATION_FUNC(res), 0, (__global DATA_TYPE *)(dst_addr));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100973}
974#endif // defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y)
975
Pablo Tello47104362019-02-27 13:32:51 +0000976#if defined(NUM_ROWS_PROCESSED) && defined(NUM_PLANES_PROCESSED) && VEC_SIZE == 4
giuros016d109962019-01-07 17:47:19 +0000977/** This function computes the depthwise convolution quantized for NHWC data layout when the stride along the width and height is 1.
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100978 *
Pablo Tello47104362019-02-27 13:32:51 +0000979 * @note This kernel assumes VEC_SIZE is 4.
giuros016d109962019-01-07 17:47:19 +0000980 * @note The weights tensor is expected to be reshaped using @ref CLDepthwiseConvolutionLayerReshapeWeightsKernel.
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100981 * @note The number of elements read per thread must be passed at compile time using -DVEC_SIZE (e.g. -DVEC_SIZE=2)
982 * @note Dimension two of the input tensor (height for NHWC data layout) must be passed at compile time using -DSRC_DIM2 (e.g. -DSRC_DIM_2=112)
983 * @note The number of rows processed per thread must be passed at compile time using -DNUM_ROWS_PROCESSED (i.e. -DNUM_ROWS_PROCESSED=2)
984 * @note The number of planes processed per thread must be passed at compile time using -DNUM_PLANES_PROCESSED (i.e. -DNUM_PLANES_PROCESSED=2)
985 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_TOP (e.g. -DCONV_PAD_TOP=1)
986 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_LEFT (e.g. -DCONV_PAD_LEFT=1).
987 *
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100988 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8
989 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
990 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
991 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
992 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
993 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
994 * @param[in] src_step_z src_stride_y * number of elements along Z processed per workitem(in bytes)
995 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes)
996 * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
997 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
998 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
999 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1000 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
1001 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1002 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
1003 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
1004 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
1005 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes)
1006 * @param[in] dst_step_w dst_stride_w * number of elements along W processed per workitem(in bytes)
1007 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1008 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8/QSYMM8_PER_CHANNEL
1009 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
1010 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
1011 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
1012 * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes)
1013 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
1014 * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32
1015 * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes)
1016 * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1017 * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector
1018 * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32
1019 * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes)
1020 * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1021 * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector
1022 * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32
1023 * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes)
1024 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1025 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector
1026 * @param[in] max_offset Max offset for the input tensor
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001027 */
1028
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001029__kernel void dwc_3x3_reshaped_quantized8_stride1_nhwc(
Georgios Pinitas37044642018-10-30 14:53:25 +00001030 TENSOR4D_DECLARATION(src),
1031 TENSOR4D_DECLARATION(dst),
giuros016d109962019-01-07 17:47:19 +00001032 IMAGE_DECLARATION(weights),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001033 VECTOR_DECLARATION(output_multipliers),
1034 VECTOR_DECLARATION(output_shifts),
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001035#if defined(HAS_BIAS)
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001036 VECTOR_DECLARATION(biases),
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001037#endif /* defined(HAS_BIAS) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001038 int max_offset)
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001039{
Giorgio Arenafa23f112018-06-19 11:27:38 +01001040 int x = get_global_id(0);
1041 int y = get_global_id(1);
Georgios Pinitas37044642018-10-30 14:53:25 +00001042#if defined(DST_DEPTH)
1043 int z = get_global_id(2) % (int)DST_DEPTH; // spatial coordinate y
1044 int b = get_global_id(2) / (int)DST_DEPTH; // batch
Pablo Tello47104362019-02-27 13:32:51 +00001045#else // defined(DST_DEPTH)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001046 int z = get_global_id(2); // spatial coordinate y
Pablo Tello47104362019-02-27 13:32:51 +00001047#endif // defined(DST_DEPTH)
Giorgio Arenafa23f112018-06-19 11:27:38 +01001048
giuros016d109962019-01-07 17:47:19 +00001049 __global uchar *weights_addr = weights_ptr + weights_offset_first_element_in_bytes + x * weights_stride_y;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001050
Georgios Pinitas37044642018-10-30 14:53:25 +00001051#if defined(DST_DEPTH)
1052 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE + b * src_stride_w;
1053#else /* defined(DST_DEPTH) */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001054 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE;
Georgios Pinitas37044642018-10-30 14:53:25 +00001055#endif /* defined(DST_DEPTH) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001056
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001057 int z_coord = 0;
1058 int4 offset = 0;
1059 int4 y_coord = ((int4)(y * NUM_ROWS_PROCESSED) + (int4)(0, 1, 2, 3)) - (int)CONV_PAD_LEFT;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001060
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001061 // Only for y = 0 we can have a negative coordinate. If so, we convert it to SRC_DIM_1
1062 y_coord.s0 = min((uint)y_coord.s0, (uint)SRC_DIM_1);
1063 y_coord.s1 = min((uint)y_coord.s1, (uint)SRC_DIM_1);
1064 y_coord.s2 = min((uint)y_coord.s2, (uint)SRC_DIM_1);
1065 y_coord.s3 = min((uint)y_coord.s3, (uint)SRC_DIM_1);
1066
1067 int4 y_offset = convert_int4(y_coord * (int)src_stride_y);
1068
1069 // We compute 4x2x2 [C,W,H] elements
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001070 VEC_INT acc0 = 0, sum0 = 0;
1071 VEC_INT acc1 = 0, sum1 = 0;
1072 VEC_INT acc2 = 0, sum2 = 0;
1073 VEC_INT acc3 = 0, sum3 = 0;
1074
1075 // Load weights
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001076 VEC_DATA_TYPE(WEIGHTS_TYPE, 16)
1077 w0_tmp = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr));
1078 VEC_DATA_TYPE(WEIGHTS_TYPE, 16)
1079 w1_tmp = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr + 16));
1080 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1081 w8 = VLOAD(4)(0, (__global WEIGHTS_TYPE *)(weights_addr + 2 * 16));
Pablo Tello47104362019-02-27 13:32:51 +00001082
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001083 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1084 w0 = w0_tmp.s0123;
1085 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1086 w1 = w0_tmp.s4567;
1087 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1088 w2 = w0_tmp.s89AB;
1089 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1090 w3 = w0_tmp.sCDEF;
Pablo Tello47104362019-02-27 13:32:51 +00001091
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001092 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1093 w4 = w1_tmp.s0123;
1094 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1095 w5 = w1_tmp.s4567;
1096 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1097 w6 = w1_tmp.s89AB;
1098 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1099 w7 = w1_tmp.sCDEF;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001100
1101#if INPUT_OFFSET != 0
Georgios Pinitas4e0d3812018-08-01 20:16:34 +01001102 VEC_INT sum_we = CONVERT(w0, VEC_INT) + CONVERT(w1, VEC_INT) + CONVERT(w2, VEC_INT)
1103 + CONVERT(w3, VEC_INT) + CONVERT(w4, VEC_INT) + CONVERT(w5, VEC_INT)
1104 + CONVERT(w6, VEC_INT) + CONVERT(w7, VEC_INT) + CONVERT(w8, VEC_INT);
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001105#endif /* INPUT_OFFSET != 0 */
1106
1107 // Load input values
1108 // z == 0
1109 // Clamp z_coord as for z = 0, it can be negative
1110 // z_coord is casted to unsigned int in order to use just a min() operation
1111 // A "-1" 32 bit signed variable converted to unsigned gives 4294967295
Georgios Pinitas4e0d3812018-08-01 20:16:34 +01001112 z_coord = z * (int)NUM_PLANES_PROCESSED - (int)CONV_PAD_TOP;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001113 z_coord = min((uint)z_coord, (uint)SRC_DIM_2);
1114 offset = y_offset + (int4)(z_coord * src_stride_z);
Georgios Pinitas4e0d3812018-08-01 20:16:34 +01001115 offset = min(offset, (int4)max_offset);
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001116
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001117 VEC_TYPE(VEC_SIZE)
1118 values0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1119 VEC_TYPE(VEC_SIZE)
1120 values1 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1121 VEC_TYPE(VEC_SIZE)
1122 values2 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1123 VEC_TYPE(VEC_SIZE)
1124 values3 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001125
1126 // z == 1
1127 // z_coord can be only negative for z = 0 so we do not need to clamp it
1128 // Moreover z_coord cannot be out-of-bound for z = 1 so we do not need to clamp the offset
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001129 z_coord = z * (int)NUM_PLANES_PROCESSED - (int)CONV_PAD_TOP + 1;
1130 offset = y_offset + (int4)(z_coord * src_stride_z);
1131 VEC_TYPE(VEC_SIZE)
1132 values4 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1133 VEC_TYPE(VEC_SIZE)
1134 values5 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1135 VEC_TYPE(VEC_SIZE)
1136 values6 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1137 VEC_TYPE(VEC_SIZE)
1138 values7 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001139
1140 // z == 2
1141 // After z = 1 we can simply add src_stride_z to offset without updating z_coord
1142 // However offset can be out-of-bound so we need to check if it is greater than max_offset
1143 offset += (int4)src_stride_z;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001144 offset = min(offset, (int4)max_offset);
1145 VEC_TYPE(VEC_SIZE)
1146 values8 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1147 VEC_TYPE(VEC_SIZE)
1148 values9 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1149 VEC_TYPE(VEC_SIZE)
1150 values10 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1151 VEC_TYPE(VEC_SIZE)
1152 values11 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001153
1154 // z == 3
1155 // After z = 1 we can simply add src_stride_z to offset without updating z_coord
1156 // However offset can be out-of-bound so we need to check if it is greater than max_offset
1157 offset += (int4)(src_stride_z);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001158 offset = min(offset, (int4)max_offset);
1159 VEC_TYPE(VEC_SIZE)
1160 values12 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1161 VEC_TYPE(VEC_SIZE)
1162 values13 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1163 VEC_TYPE(VEC_SIZE)
1164 values14 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1165 VEC_TYPE(VEC_SIZE)
1166 values15 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001167
1168 MULTIPLY_ADD_ACCUMULATE(values0, w0, acc0, sum0);
1169 MULTIPLY_ADD_ACCUMULATE(values1, w1, acc0, sum0);
1170 MULTIPLY_ADD_ACCUMULATE(values2, w2, acc0, sum0);
1171 MULTIPLY_ADD_ACCUMULATE(values1, w0, acc1, sum1);
1172 MULTIPLY_ADD_ACCUMULATE(values2, w1, acc1, sum1);
1173 MULTIPLY_ADD_ACCUMULATE(values3, w2, acc1, sum1);
1174
1175 MULTIPLY_ADD_ACCUMULATE(values4, w3, acc0, sum0);
1176 MULTIPLY_ADD_ACCUMULATE(values5, w4, acc0, sum0);
1177 MULTIPLY_ADD_ACCUMULATE(values6, w5, acc0, sum0);
1178 MULTIPLY_ADD_ACCUMULATE(values5, w3, acc1, sum1);
1179 MULTIPLY_ADD_ACCUMULATE(values6, w4, acc1, sum1);
1180 MULTIPLY_ADD_ACCUMULATE(values7, w5, acc1, sum1);
1181
1182 MULTIPLY_ADD_ACCUMULATE(values8, w6, acc0, sum0);
1183 MULTIPLY_ADD_ACCUMULATE(values9, w7, acc0, sum0);
1184 MULTIPLY_ADD_ACCUMULATE(values10, w8, acc0, sum0);
1185 MULTIPLY_ADD_ACCUMULATE(values9, w6, acc1, sum1);
1186 MULTIPLY_ADD_ACCUMULATE(values10, w7, acc1, sum1);
1187 MULTIPLY_ADD_ACCUMULATE(values11, w8, acc1, sum1);
1188
1189 MULTIPLY_ADD_ACCUMULATE(values4, w0, acc2, sum2);
1190 MULTIPLY_ADD_ACCUMULATE(values5, w1, acc2, sum2);
1191 MULTIPLY_ADD_ACCUMULATE(values6, w2, acc2, sum2);
1192 MULTIPLY_ADD_ACCUMULATE(values5, w0, acc3, sum3);
1193 MULTIPLY_ADD_ACCUMULATE(values6, w1, acc3, sum3);
1194 MULTIPLY_ADD_ACCUMULATE(values7, w2, acc3, sum3);
1195
1196 MULTIPLY_ADD_ACCUMULATE(values8, w3, acc2, sum2);
1197 MULTIPLY_ADD_ACCUMULATE(values9, w4, acc2, sum2);
1198 MULTIPLY_ADD_ACCUMULATE(values10, w5, acc2, sum2);
1199 MULTIPLY_ADD_ACCUMULATE(values9, w3, acc3, sum3);
1200 MULTIPLY_ADD_ACCUMULATE(values10, w4, acc3, sum3);
1201 MULTIPLY_ADD_ACCUMULATE(values11, w5, acc3, sum3);
1202
1203 MULTIPLY_ADD_ACCUMULATE(values12, w6, acc2, sum2);
1204 MULTIPLY_ADD_ACCUMULATE(values13, w7, acc2, sum2);
1205 MULTIPLY_ADD_ACCUMULATE(values14, w8, acc2, sum2);
1206 MULTIPLY_ADD_ACCUMULATE(values13, w6, acc3, sum3);
1207 MULTIPLY_ADD_ACCUMULATE(values14, w7, acc3, sum3);
1208 MULTIPLY_ADD_ACCUMULATE(values15, w8, acc3, sum3);
1209
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001210#if defined(HAS_BIAS)
1211 Vector biases = CONVERT_TO_VECTOR_STRUCT(biases);
1212
1213 VEC_INT bias_values = VLOAD(VEC_SIZE)(0, (__global int *)biases.ptr);
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001214
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001215 acc0 += bias_values;
1216 acc1 += bias_values;
1217 acc2 += bias_values;
1218 acc3 += bias_values;
1219#endif /* defined(HAS_BIAS) */
1220
1221#if WEIGHTS_OFFSET != 0
1222 acc0 += WEIGHTS_OFFSET * sum0;
1223 acc1 += WEIGHTS_OFFSET * sum1;
1224 acc2 += WEIGHTS_OFFSET * sum2;
1225 acc3 += WEIGHTS_OFFSET * sum3;
1226#endif /* WEIGHTS_OFFSET != 0 */
1227
1228#if INPUT_OFFSET != 0
1229 VEC_INT offs = INPUT_OFFSET * sum_we;
1230
1231 acc0 += offs;
1232 acc1 += offs;
1233 acc2 += offs;
1234 acc3 += offs;
1235#endif /* INPUT_OFFSET != 0 */
1236
1237#if K_OFFSET != 0
1238 acc0 += (VEC_INT)K_OFFSET;
1239 acc1 += (VEC_INT)K_OFFSET;
1240 acc2 += (VEC_INT)K_OFFSET;
1241 acc3 += (VEC_INT)K_OFFSET;
1242#endif /* K_OFFSET != 0 */
1243
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001244#if defined(REAL_MULTIPLIER)
1245
1246 acc0 = CONVERT(round(CONVERT(acc0, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
1247 acc1 = CONVERT(round(CONVERT(acc1, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
1248 acc2 = CONVERT(round(CONVERT(acc2, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
1249 acc3 = CONVERT(round(CONVERT(acc3, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
1250
1251#else // defined(REAL_MULTIPLIER)
1252
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001253#if defined(PER_CHANNEL_QUANTIZATION)
1254 Vector output_multipliers = CONVERT_TO_VECTOR_STRUCT(output_multipliers);
1255 Vector output_shifts = CONVERT_TO_VECTOR_STRUCT(output_shifts);
1256 VEC_INT output_multiplier = VLOAD(VEC_SIZE)(0, (__global int *)output_multipliers.ptr);
1257 VEC_INT output_shift = VLOAD(VEC_SIZE)(0, (__global int *)output_shifts.ptr);
1258#else // defined(PER_CHANNEL_QUANTIZATION)
1259 const int output_multiplier = *((__global int *)(output_multipliers_ptr + output_multipliers_offset_first_element_in_bytes));
1260 const int output_shift = *((__global int *)(output_shifts_ptr + output_shifts_offset_first_element_in_bytes));
1261#endif // defined(PER_CHANNEL_QUANTIZATION)
1262
1263 acc0 = asymm_mult_by_quant_multiplier_less_than_one(acc0, output_multiplier, output_shift);
1264 acc1 = asymm_mult_by_quant_multiplier_less_than_one(acc1, output_multiplier, output_shift);
1265 acc2 = asymm_mult_by_quant_multiplier_less_than_one(acc2, output_multiplier, output_shift);
1266 acc3 = asymm_mult_by_quant_multiplier_less_than_one(acc3, output_multiplier, output_shift);
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001267
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001268#endif // defined(REAL_MULTIPLIER)
1269
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001270 acc0 += (VEC_INT)OUTPUT_OFFSET;
1271 acc1 += (VEC_INT)OUTPUT_OFFSET;
1272 acc2 += (VEC_INT)OUTPUT_OFFSET;
1273 acc3 += (VEC_INT)OUTPUT_OFFSET;
1274
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001275 VEC_TYPE(VEC_SIZE)
1276 res0 = CONVERT_SAT(acc0, VEC_TYPE(VEC_SIZE));
1277 VEC_TYPE(VEC_SIZE)
1278 res1 = CONVERT_SAT(acc1, VEC_TYPE(VEC_SIZE));
1279 VEC_TYPE(VEC_SIZE)
1280 res2 = CONVERT_SAT(acc2, VEC_TYPE(VEC_SIZE));
1281 VEC_TYPE(VEC_SIZE)
1282 res3 = CONVERT_SAT(acc3, VEC_TYPE(VEC_SIZE));
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001283
Georgios Pinitas37044642018-10-30 14:53:25 +00001284#if defined(DST_DEPTH)
1285 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * dst_step_x + y * dst_step_y + (z * NUM_PLANES_PROCESSED) * dst_step_z + b * dst_stride_w;
1286#else /* defined(DST_DEPTH) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001287 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * dst_step_x + y * dst_step_y + (z * NUM_PLANES_PROCESSED) * dst_step_z;
Georgios Pinitas37044642018-10-30 14:53:25 +00001288#endif /* defined(DST_DEPTH) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001289
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001290 VSTORE(VEC_SIZE)
Georgios Pinitas60e98252018-10-22 16:17:20 +01001291 (ACTIVATION_FUNC(res0), 0, dst_addr + 0 * dst_stride_y);
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001292 VSTORE(VEC_SIZE)
Georgios Pinitas60e98252018-10-22 16:17:20 +01001293 (ACTIVATION_FUNC(res1), 0, dst_addr + 1 * dst_stride_y);
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001294
1295#if((DST_DIM_2 % NUM_PLANES_PROCESSED) != 0)
1296 if((z * NUM_PLANES_PROCESSED + 1) < DST_DIM_2)
1297#endif // ((DST_DIM_2 % NUM_PLANES_PROCESSED) != 0)
1298 {
1299 VSTORE(VEC_SIZE)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001300 (ACTIVATION_FUNC(res2), 0, (__global DATA_TYPE *)(dst_addr + 0 * dst_stride_y + 1 * dst_stride_z));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001301 VSTORE(VEC_SIZE)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001302 (ACTIVATION_FUNC(res3), 0, (__global DATA_TYPE *)(dst_addr + 1 * dst_stride_y + 1 * dst_stride_z));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001303 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001304}
1305
giuros016d109962019-01-07 17:47:19 +00001306#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) && VEC_SIZE == 4
1307/** This function computes the depthwise convolution quantized for NHWC data layout when the stride along the width and height is 1 using dot product.
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001308 *
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001309 * @note Per-channel quantization is not supported by this kernel.
giuros016d109962019-01-07 17:47:19 +00001310 * @note This kernel assumes VEC_SIZE is 4.
1311 * @note The weights tensor is expected to be reshaped using @ref CLDepthwiseConvolutionLayerReshapeWeightsKernel.
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001312 * @note The number of elements read per thread must be passed at compile time using -DVEC_SIZE (e.g. -DVEC_SIZE=2)
1313 * @note Dimension two of the input tensor (height for NHWC data layout) must be passed at compile time using -DSRC_DIM2 (e.g. -DSRC_DIM_2=112)
1314 * @note The number of rows processed per thread must be passed at compile time using -DNUM_ROWS_PROCESSED (i.e. -DNUM_ROWS_PROCESSED=2)
1315 * @note The number of planes processed per thread must be passed at compile time using -DNUM_PLANES_PROCESSED (i.e. -DNUM_PLANES_PROCESSED=2)
1316 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_TOP (e.g. -DCONV_PAD_TOP=1)
1317 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_LEFT (e.g. -DCONV_PAD_LEFT=1).
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001318 * @note If REAL_MULTIPLIER is passed at compile time (i.e. -DREAL_MULTIPLIER=1.355f), the final quantization is performed using a floating point multiplication.
1319 * If not, the quantization will be performed using a fixed point multiplication
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001320 *
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001321 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8
1322 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1323 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1324 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1325 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1326 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1327 * @param[in] src_step_z src_stride_y * number of elements along Z processed per workitem(in bytes)
1328 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes)
1329 * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
1330 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1331 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
1332 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1333 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
1334 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1335 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
1336 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
1337 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
1338 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes)
1339 * @param[in] dst_step_w dst_stride_w * number of elements along W processed per workitem(in bytes)
1340 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1341 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr
1342 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
1343 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
1344 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
1345 * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes)
1346 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
1347 * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32
1348 * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes)
1349 * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1350 * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector
1351 * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32
1352 * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes)
1353 * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1354 * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector
1355 * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32
1356 * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes)
1357 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1358 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector
1359 * @param[in] max_offset The maximum allowed offset for the input tensor
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001360 */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001361__kernel void dwc_3x3_reshaped_quantized8_dot8_stride1_nhwc(
Georgios Pinitas37044642018-10-30 14:53:25 +00001362 TENSOR4D_DECLARATION(src),
1363 TENSOR4D_DECLARATION(dst),
giuros016d109962019-01-07 17:47:19 +00001364 IMAGE_DECLARATION(weights),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001365 VECTOR_DECLARATION(output_multipliers),
1366 VECTOR_DECLARATION(output_shifts),
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001367#if defined(HAS_BIAS)
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001368 VECTOR_DECLARATION(biases),
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001369#endif // defined(HAS_BIAS)
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001370 int max_offset)
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001371{
Giorgio Arenafa23f112018-06-19 11:27:38 +01001372 int x = get_global_id(0);
1373 int y = get_global_id(1);
Georgios Pinitas37044642018-10-30 14:53:25 +00001374#if defined(DST_DEPTH)
1375 int z = get_global_id(2) % (int)DST_DEPTH; // spatial coordinate y
1376 int b = get_global_id(2) / (int)DST_DEPTH; // batch
Pablo Tello47104362019-02-27 13:32:51 +00001377#else // defined(DST_DEPTH)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001378 int z = get_global_id(2); // spatial coordinate y
Pablo Tello47104362019-02-27 13:32:51 +00001379#endif // defined(DST_DEPTH)
Giorgio Arenafa23f112018-06-19 11:27:38 +01001380
giuros016d109962019-01-07 17:47:19 +00001381 __global uchar *weights_addr = weights_ptr + weights_offset_first_element_in_bytes + x * weights_stride_y;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001382
Georgios Pinitas37044642018-10-30 14:53:25 +00001383#if defined(DST_DEPTH)
1384 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE + b * src_stride_w;
1385#else /* defined(DST_DEPTH) */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001386 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE;
Georgios Pinitas37044642018-10-30 14:53:25 +00001387#endif /* defined(DST_DEPTH) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001388
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001389 int z_coord = 0;
1390 int4 offset = 0;
1391 int4 y_coord = ((int4)(y * NUM_ROWS_PROCESSED) + (int4)(0, 1, 2, 3)) - (int)CONV_PAD_LEFT;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001392
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001393 // Only for y = 0 we can have a negative coordinate. If so, we convert it to SRC_DIM_1
1394 y_coord.s0 = min((uint)y_coord.s0, (uint)SRC_DIM_1);
1395 y_coord.s1 = min((uint)y_coord.s1, (uint)SRC_DIM_1);
1396 y_coord.s2 = min((uint)y_coord.s2, (uint)SRC_DIM_1);
1397 y_coord.s3 = min((uint)y_coord.s3, (uint)SRC_DIM_1);
1398
1399 int4 y_offset = convert_int4(y_coord * (int)src_stride_y);
1400
1401 // We compute 4x2x1 [C,W,H] elements
1402 VEC_INT acc0 = 0;
1403 VEC_INT acc1 = 0;
1404 VEC_INT sum0 = 0;
1405 VEC_INT sum1 = 0;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001406
1407 // Load weights
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001408 VEC_TYPE(16)
1409 w0 = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr));
1410 VEC_TYPE(16)
1411 w1 = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr + 16));
1412 VEC_TYPE(4)
1413 w2 = VLOAD(4)(0, (__global WEIGHTS_TYPE *)(weights_addr + 32));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001414
1415#if INPUT_OFFSET != 0
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001416 // Initilize the final result with the weights reduction multiplied by INPUT_OFFSET
giuros016d109962019-01-07 17:47:19 +00001417 DOT_PRODUCT_REDUCTION_WEIGHTS(acc0.s0, w0.s01234567, w0.s8);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001418 DOT_PRODUCT_REDUCTION_WEIGHTS(acc0.s1, (VEC_TYPE(8))((w0.s9ABC), (w0.sDEF), w1.s0), w1.s1);
giuros016d109962019-01-07 17:47:19 +00001419 DOT_PRODUCT_REDUCTION_WEIGHTS(acc0.s2, w1.s23456789, w1.sA);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001420 DOT_PRODUCT_REDUCTION_WEIGHTS(acc0.s3, (VEC_TYPE(8))((w1.sBCD), (w1.sEF), (w2.s012)), w2.s3);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001421
1422 // Multiply the weights reduction with INPUT_OFFSET
1423 acc0 = INPUT_OFFSET * acc0;
1424
1425 acc1 = acc0;
1426#endif // INPUT_OFFSET != 0
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001427
1428 // Load input values
1429 // z == 0
1430 // Clamp z_coord as for z = 0, it can be negative
1431 // z_coord is casted to unsigned int in order to use just a min() operation
1432 // A "-1" 32 bit signed variable converted to unsigned gives 4294967295
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001433 z_coord = z - (int)CONV_PAD_TOP;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001434 z_coord = min((uint)z_coord, (uint)SRC_DIM_2);
1435 offset = y_offset + (int4)(z_coord * src_stride_z);
Georgios Pinitas4e0d3812018-08-01 20:16:34 +01001436 offset = min(offset, (int4)max_offset);
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001437
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001438 VEC_TYPE(VEC_SIZE)
1439 values0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1440 VEC_TYPE(VEC_SIZE)
1441 values1 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1442 VEC_TYPE(VEC_SIZE)
1443 values2 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1444 VEC_TYPE(VEC_SIZE)
1445 values3 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001446
1447 // z == 1
1448 // z_coord can be only negative for z = 0 so we do not need to clamp it
1449 // Moreover z_coord cannot be out-of-bound for z = 1 so we do not need to clamp the offset
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001450 z_coord = z - (int)CONV_PAD_TOP + 1;
1451 offset = y_offset + (int4)(z_coord * src_stride_z);
1452 VEC_TYPE(VEC_SIZE)
1453 values4 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1454 VEC_TYPE(VEC_SIZE)
1455 values5 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1456 VEC_TYPE(VEC_SIZE)
1457 values6 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1458 VEC_TYPE(VEC_SIZE)
1459 values7 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001460
1461 // z == 2
1462 // After z = 1 we can simply add src_stride_z to offset without updating z_coord
1463 // However offset can be out-of-bound so we need to check if it is greater than max_offset
1464 offset += (int4)src_stride_z;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001465 offset = min(offset, (int4)max_offset);
1466 VEC_TYPE(VEC_SIZE)
1467 values8 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1468 VEC_TYPE(VEC_SIZE)
1469 values9 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1470 VEC_TYPE(VEC_SIZE)
1471 values10 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1472 VEC_TYPE(VEC_SIZE)
1473 values11 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001474
giuros016d109962019-01-07 17:47:19 +00001475 DOT_PRODUCT_REDUCTION(sum0.s0, values0.s0, values1.s0, values2.s0, values4.s0, values5.s0, values6.s0, values8.s0, values9.s0, values10.s0);
1476 DOT_PRODUCT_REDUCTION(sum1.s0, values1.s0, values2.s0, values3.s0, values5.s0, values6.s0, values7.s0, values9.s0, values10.s0, values11.s0);
1477 DOT_PRODUCT(acc0.s0, values0.s0, values1.s0, values2.s0, values4.s0, values5.s0, values6.s0, values8.s0, values9.s0, values10.s0, w0.s01234567, w0.s8);
1478 DOT_PRODUCT(acc1.s0, values1.s0, values2.s0, values3.s0, values5.s0, values6.s0, values7.s0, values9.s0, values10.s0, values11.s0, w0.s01234567, w0.s8);
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001479
giuros016d109962019-01-07 17:47:19 +00001480 DOT_PRODUCT_REDUCTION(sum0.s1, values0.s1, values1.s1, values2.s1, values4.s1, values5.s1, values6.s1, values8.s1, values9.s1, values10.s1);
1481 DOT_PRODUCT_REDUCTION(sum1.s1, values1.s1, values2.s1, values3.s1, values5.s1, values6.s1, values7.s1, values9.s1, values10.s1, values11.s1);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001482 DOT_PRODUCT(acc0.s1, values0.s1, values1.s1, values2.s1, values4.s1, values5.s1, values6.s1, values8.s1, values9.s1, values10.s1, (VEC_TYPE(8))((w0.s9ABC), (w0.sDEF), w1.s0), w1.s1);
1483 DOT_PRODUCT(acc1.s1, values1.s1, values2.s1, values3.s1, values5.s1, values6.s1, values7.s1, values9.s1, values10.s1, values11.s1, (VEC_TYPE(8))((w0.s9ABC), (w0.sDEF), w1.s0), w1.s1);
giuros016d109962019-01-07 17:47:19 +00001484
1485 DOT_PRODUCT_REDUCTION(sum0.s2, values0.s2, values1.s2, values2.s2, values4.s2, values5.s2, values6.s2, values8.s2, values9.s2, values10.s2);
1486 DOT_PRODUCT_REDUCTION(sum1.s2, values1.s2, values2.s2, values3.s2, values5.s2, values6.s2, values7.s2, values9.s2, values10.s2, values11.s2);
1487 DOT_PRODUCT(acc0.s2, values0.s2, values1.s2, values2.s2, values4.s2, values5.s2, values6.s2, values8.s2, values9.s2, values10.s2, w1.s23456789, w1.sA);
1488 DOT_PRODUCT(acc1.s2, values1.s2, values2.s2, values3.s2, values5.s2, values6.s2, values7.s2, values9.s2, values10.s2, values11.s2, w1.s23456789, w1.sA);
1489
1490 DOT_PRODUCT_REDUCTION(sum0.s3, values0.s3, values1.s3, values2.s3, values4.s3, values5.s3, values6.s3, values8.s3, values9.s3, values10.s3);
1491 DOT_PRODUCT_REDUCTION(sum1.s3, values1.s3, values2.s3, values3.s3, values5.s3, values6.s3, values7.s3, values9.s3, values10.s3, values11.s3);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001492 DOT_PRODUCT(acc0.s3, values0.s3, values1.s3, values2.s3, values4.s3, values5.s3, values6.s3, values8.s3, values9.s3, values10.s3, (VEC_TYPE(8))((w1.sBCD), (w1.sEF), (w2.s012)), w2.s3);
1493 DOT_PRODUCT(acc1.s3, values1.s3, values2.s3, values3.s3, values5.s3, values6.s3, values7.s3, values9.s3, values10.s3, values11.s3, (VEC_TYPE(8))((w1.sBCD), (w1.sEF), (w2.s012)), w2.s3);
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001494
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001495#if defined(HAS_BIAS)
1496 Vector biases = CONVERT_TO_VECTOR_STRUCT(biases);
1497
1498 VEC_INT bias_values = VLOAD(VEC_SIZE)(0, (__global int *)biases.ptr);
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001499
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001500 acc0 += bias_values;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001501 acc1 += bias_values;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001502
1503#endif // defined(HAS_BIAS)
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001504
1505#if WEIGHTS_OFFSET != 0
1506 acc0 += WEIGHTS_OFFSET * sum0;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001507 acc1 += WEIGHTS_OFFSET * sum1;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001508#endif // WEIGHTS_OFFSET != 0
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001509
1510#if K_OFFSET != 0
1511 acc0 += (VEC_INT)K_OFFSET;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001512 acc1 += (VEC_INT)K_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001513
1514#endif // K_OFFSET != 0
1515
1516#if defined(REAL_MULTIPLIER)
1517
1518 acc0 = CONVERT(round(CONVERT(acc0, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
1519 acc1 = CONVERT(round(CONVERT(acc1, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
1520
1521#else // defined(REAL_MULTIPLIER)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001522 const int output_multiplier = *((__global int *)(output_multipliers_ptr + output_multipliers_offset_first_element_in_bytes));
1523 const int output_shift = *((__global int *)(output_shifts_ptr + output_shifts_offset_first_element_in_bytes));
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001524
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001525 acc0 = asymm_mult_by_quant_multiplier_less_than_one(acc0, output_multiplier, output_shift);
1526 acc1 = asymm_mult_by_quant_multiplier_less_than_one(acc1, output_multiplier, output_shift);
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001527
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001528#endif // defined(REAL_MULTIPLIER)
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001529 acc0 += (VEC_INT)OUTPUT_OFFSET;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001530 acc1 += (VEC_INT)OUTPUT_OFFSET;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001531
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001532 VEC_TYPE(VEC_SIZE)
1533 res0 = CONVERT_SAT(acc0, VEC_TYPE(VEC_SIZE));
1534 VEC_TYPE(VEC_SIZE)
1535 res1 = CONVERT_SAT(acc1, VEC_TYPE(VEC_SIZE));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001536
Georgios Pinitas37044642018-10-30 14:53:25 +00001537#if defined(DST_DEPTH)
1538 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * dst_step_x + y * dst_step_y + z * dst_step_z + b * dst_stride_w;
1539#else /* defined(DST_DEPTH) */
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001540 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * dst_step_x + y * dst_step_y + z * dst_step_z;
Georgios Pinitas37044642018-10-30 14:53:25 +00001541#endif /* defined(DST_DEPTH) */
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001542
1543 VSTORE(VEC_SIZE)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001544 (ACTIVATION_FUNC(res0), 0, (__global DATA_TYPE *)(dst_addr + 0 * dst_stride_y));
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001545 VSTORE(VEC_SIZE)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001546 (ACTIVATION_FUNC(res1), 0, (__global DATA_TYPE *)(dst_addr + 1 * dst_stride_y));
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001547}
giuros016d109962019-01-07 17:47:19 +00001548#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) && VEC_SIZE==4
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001549
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001550#endif // defined(NUM_ROWS_PROCESSED) && defined(NUM_PLANES_PROCESSED)
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001551
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001552#endif // defined(VEC_SIZE) && defined(SRC_DIM_1) && defined(SRC_DIM_2) && defined(CONV_PAD_TOP) && defined(CONV_PAD_LEFT)
1553
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001554#endif // defined(WEIGHTS_PROMOTED_TYPE)
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001555
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001556#endif // defined(WEIGHTS_OFFSET) && defined(INPUT_OFFSET) && defined(K_OFFSET) && (defined(OUTPUT_OFFSET) || defined(REAL_MULTIPLIER))
1557
1558#if defined(SRC_DIM1) && defined(SRC_DIM2) && defined(KERNEL_WIDTH) && defined(KERNEL_HEIGHT) && defined(N0) && defined(DILATION_X) && defined(DILATION_Y) && defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y) && defined(CONV_PAD_LEFT) && defined(CONV_PAD_TOP) && defined(INPUT_OFFSET) && defined(WEIGHTS_OFFSET) && defined(OUTPUT_OFFSET)
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001559/** This function computes the depthwise convolution for NHWC data layout. This kernel assumes that the weights tensor is NOT reshaped
1560 *
1561 * @note The number of elements processed must be passed at compile time using -DN0 (e.g. -DN0=2)
1562 * @note The depth multiplier must be passed at compile time using -DDEPTH_MULTIPLIER (e.g. -DDEPTH_MULTIPLIER=1)
1563 * @note The first dimension of the input tensor must be passed at compile time using -DSRC_DIM1 (e.g. -DSRC_DIM1=112)
1564 * @note The second dimension of the input tensor must be passed at compile time using -DSRC_DIM2 (e.g. -DSRC_DIM2=80)
1565 * @note The kernel width must be passed at compile time using -DKERNEL_WIDTH (e.g. -DKERNEL_WIDTH=5)
1566 * @note The kernel height must be passed at compile time using -DKERNEL_HEIGHT (e.g. -DKERNEL_HEIGHT=5)
1567 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_TOP (e.g. -DCONV_PAD_TOP=1)
1568 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_LEFT (e.g. -DCONV_PAD_LEFT=1)
1569 * @note The convolution stride along the width must be passed at compile time using -DCONV_STRIDE_X (e.g. -DCONV_STRIDE_Y=X)
1570 * @note The convolution stride along the height must be passed at compile time using -DCONV_STRIDE_Y (e.g. -DCONV_STRIDE_Y=1)
1571 * @note It is possible to select the activation function to apply using -DACTIVATION_TYPE e.g. -DACTIVATION_TYPE=relu
1572 * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively
1573 *
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001574 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8
1575 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1576 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1577 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1578 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1579 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1580 * @param[in] src_step_z src_stride_y * number of elements along Z processed per workitem(in bytes)
1581 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes)
1582 * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
1583 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1584 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
1585 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1586 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
1587 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1588 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
1589 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
1590 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
1591 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes)
1592 * @param[in] dst_step_w dst_stride_w * number of elements along W processed per workitem(in bytes)
1593 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1594 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8/QSYMM8_PER_CHANNEL
1595 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
1596 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
1597 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
1598 * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes)
1599 * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes)
1600 * @param[in] weights_step_z weights_stride_z * number of elements along Y processed per workitem(in bytes)
1601 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
1602 * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32
1603 * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes)
1604 * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1605 * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector
1606 * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32
1607 * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes)
1608 * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1609 * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector
1610 * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32
1611 * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes)
1612 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1613 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001614 */
1615__kernel void dwc_MxN_native_quantized8_nhwc(
1616 TENSOR4D_DECLARATION(src),
1617 TENSOR4D_DECLARATION(dst),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001618 TENSOR3D_DECLARATION(weights),
1619 VECTOR_DECLARATION(output_multipliers),
1620 VECTOR_DECLARATION(output_shifts)
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001621#if defined(HAS_BIAS)
Michele Di Giorgio1dce3102019-10-22 10:29:03 +01001622 ,
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001623 VECTOR_DECLARATION(biases)
1624#endif // defined(HAS_BIAS)
1625)
1626{
1627 int x = get_global_id(0); // channels
1628 int y = get_global_id(1); // spatial coordinate x
1629#if defined(DST_DEPTH)
1630 int z = get_global_id(2) % (int)DST_DEPTH; // spatial coordinate y
1631 int b = get_global_id(2) / (int)DST_DEPTH; // batch
1632#else // defined(DST_DEPTH)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001633 int z = get_global_id(2); // spatial coordinate y
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001634#endif // defined(DST_DEPTH)
1635
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001636 __global uchar *s_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(DATA_TYPE) * (int)N0;
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001637
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001638 __global uchar *d_addr = dst_ptr + dst_offset_first_element_in_bytes + x * sizeof(DATA_TYPE) * (int)DEPTH_MULTIPLIER * (int)N0 + y * dst_stride_y + z * dst_stride_z;
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001639
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001640 __global uchar *w_addr = weights_ptr + weights_offset_first_element_in_bytes + x * sizeof(WEIGHTS_TYPE) * (int)DEPTH_MULTIPLIER * (int)N0;
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001641
1642#if defined(HAS_BIAS)
1643 __global uchar *b_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int) * (int)DEPTH_MULTIPLIER * (int)N0;
1644#endif // defined(HAS_BIAS)
1645
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001646#if defined(PER_CHANNEL_QUANTIZATION)
1647 __global uchar *out_mul_addr = output_multipliers_ptr + output_multipliers_offset_first_element_in_bytes + x * sizeof(int) * (int)DEPTH_MULTIPLIER * (int)N0;
1648 __global uchar *out_shift_addr = output_shifts_ptr + output_shifts_offset_first_element_in_bytes + x * sizeof(int) * (int)DEPTH_MULTIPLIER * (int)N0;
1649
1650 VEC_INT output_multiplier = (VEC_INT)0;
1651 VEC_INT output_shift = (VEC_INT)0;
1652#else // defined(PER_CHANNEL_QUANTIZATION)
1653 const int output_multiplier = *((__global int *)(output_multipliers_ptr + output_multipliers_offset_first_element_in_bytes));
1654 const int output_shift = *((__global int *)(output_shifts_ptr + output_shifts_offset_first_element_in_bytes));
1655#endif // defined(PER_CHANNEL_QUANTIZATION)
1656
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001657#if defined(DST_DEPTH)
1658 s_addr += b * src_stride_w;
1659 d_addr += b * dst_stride_w;
1660#endif // defined(DST_DEPTH)
1661
1662#if DEPTH_MULTIPLIER > 1
1663 for(int d = 0; d < (int)DEPTH_MULTIPLIER; ++d)
1664 {
1665#endif // DEPTH_MULTIPLIER > 1
1666 // Each work-item computes N0x1x1 elements
Sang-Hoon Park15396ff2019-11-07 09:37:29 +00001667 VEC_INT res = 0;
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001668
1669 int x_coord = y * CONV_STRIDE_X - (int)CONV_PAD_LEFT;
1670 int y_coord = z * CONV_STRIDE_Y - (int)CONV_PAD_TOP;
1671
1672 for(int yk = 0; yk < KERNEL_HEIGHT; ++yk)
1673 {
1674 if(y_coord >= 0 && y_coord < SRC_DIM2)
1675 {
1676 int x_coord_tmp = x_coord;
1677
1678 for(int xk = 0; xk < KERNEL_WIDTH; ++xk)
1679 {
1680 if(x_coord_tmp >= 0 && x_coord_tmp < SRC_DIM1)
1681 {
1682 int s_offset = x_coord_tmp * (int)src_stride_y + y_coord * (int)src_stride_z;
1683 int w_offset = xk * weights_stride_y + yk * weights_stride_z;
1684
1685 // Load input and weights values
Sang-Hoon Park15396ff2019-11-07 09:37:29 +00001686 VEC_INT i = CONVERT(VLOAD(N0)(0, (__global DATA_TYPE *)(s_addr + s_offset)), VEC_INT);
1687 VEC_INT w = CONVERT(VLOAD(N0)(0, (__global WEIGHTS_TYPE *)(w_addr + w_offset)), VEC_INT);
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001688
Sang-Hoon Park15396ff2019-11-07 09:37:29 +00001689 res += (i + (VEC_INT)INPUT_OFFSET) * (w + (VEC_INT)WEIGHTS_OFFSET);
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001690 }
1691 x_coord_tmp += DILATION_X;
1692 }
1693 }
1694 y_coord += DILATION_Y;
1695 }
1696
1697#if defined(HAS_BIAS)
Sang-Hoon Park15396ff2019-11-07 09:37:29 +00001698 VEC_INT bias = VLOAD(N0)(0, (__global int *)(b_addr));
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001699 res += bias;
1700#endif // defined(HAS_BIAS)
1701
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001702#if defined(PER_CHANNEL_QUANTIZATION)
1703 output_multiplier = VLOAD(N0)(0, (__global int *)(out_mul_addr));
1704 output_shift = VLOAD(N0)(0, (__global int *)(out_shift_addr));
1705#endif // defined(PER_CHANNEL_QUANTIZATION)
1706
Sang-Hoon Park15396ff2019-11-07 09:37:29 +00001707 res = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(res, output_multiplier, output_shift, N0);
1708 res += (VEC_INT)OUTPUT_OFFSET;
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001709
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001710 VEC_TYPE(VEC_SIZE)
1711 res1 = CONVERT_SAT(res, VEC_TYPE(VEC_SIZE));
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001712
1713 VSTORE(N0)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001714 (ACTIVATION_FUNC(res1), 0, (__global DATA_TYPE *)(d_addr));
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001715
1716#if DEPTH_MULTIPLIER > 1
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001717 w_addr += sizeof(WEIGHTS_TYPE);
1718 d_addr += sizeof(DATA_TYPE);
1719#if defined(PER_CHANNEL_QUANTIZATION)
1720 out_mul_addr += sizeof(int);
1721 out_shift_addr += sizeof(int);
1722#endif // defined(PER_CHANNEL_QUANTIZATION)
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001723#if defined(HAS_BIAS)
1724 b_addr += sizeof(int);
1725#endif // defined(HAS_BIAS)
1726 }
1727#endif // DEPTH_MULTIPLIER > 1
1728}
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001729#endif // defined(SRC_DIM1) && defined(SRC_DIM2) && defined(KERNEL_WIDTH) && defined(KERNEL_HEIGHT) && defiend(N0) && defined(DILATION_X) && defined(DILATION_Y) && defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y) && defined(CONV_PAD_LEFT) && defined(CONV_PAD_TOP) && defined(INPUT_OFFSET) && defined(WEIGHTS_OFFSET) && defined(OUTPUT_OFFSET)
1730#endif // defined(DATA_TYPE) && defined(WEIGHTS_TYPE)