blob: 3cfa707e240e223ce4b65984afe372067ea76446 [file] [log] [blame]
Dmitry Savenkod7295b72017-11-20 22:00:08 +07001/*
Michele Di Giorgio4cd4cde2020-01-06 14:07:44 +00002 * Copyright (c) 2017-2020 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
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010050#if defined(WEIGHTS_OFFSET) && defined(INPUT_OFFSET) && defined(K_OFFSET) && ((defined(OUTPUT_OFFSET) && defined(OUTPUT_MULTIPLIER) && defined(OUTPUT_SHIFT)) || defined(REAL_MULTIPLIER))
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +010051
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));
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100227#endif // defined(PER_CHANNEL_QUANTIZATION)
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700228
Giorgio Arena287b5702018-02-16 11:01:04 +0000229 int8 values0 = 0;
230 int8 sum0 = 0;
Usama Arife73686a2019-04-08 17:30:48 +0100231#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000232 int8 values1 = 0;
233 int8 sum1 = 0;
Usama Arife73686a2019-04-08 17:30:48 +0100234#endif /* CONV_STRIDE_Y &&DILATION_Y==1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000235
236 // Row0
237 int8 left, middle, right;
238 GET_VALUES(src.ptr + 0 * src_stride_y, left, middle, right);
239 values0 += left * (int8)(w0.s0);
240 values0 += middle * (int8)(w0.s1);
241 values0 += right * (int8)(w0.s2);
242
243#if WEIGHTS_OFFSET != 0
244 sum0 += left + middle + right;
245#endif /* WEIGHTS_OFFSET != 0 */
246
247 // Row1
Usama Arife73686a2019-04-08 17:30:48 +0100248 GET_VALUES(src.ptr + DILATION_Y * src_stride_y, left, middle, right);
Giorgio Arena287b5702018-02-16 11:01:04 +0000249 values0 += left * (int8)(w1.s0);
250 values0 += middle * (int8)(w1.s1);
251 values0 += right * (int8)(w1.s2);
Usama Arife73686a2019-04-08 17:30:48 +0100252#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000253 values1 += left * (int8)(w0.s0);
254 values1 += middle * (int8)(w0.s1);
255 values1 += right * (int8)(w0.s2);
Usama Arife73686a2019-04-08 17:30:48 +0100256#endif /* CONV_STRIDE_Y && DILATION_Y== 1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000257
258#if WEIGHTS_OFFSET != 0
259 int8 tmp = left + middle + right;
260 sum0 += tmp;
Usama Arife73686a2019-04-08 17:30:48 +0100261#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000262 sum1 += tmp;
Usama Arife73686a2019-04-08 17:30:48 +0100263#endif /* CONV_STRIDE_Y &&DILATION_Y== 1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000264#endif /* WEIGHTS_OFFSET != 0 */
265
266 // Row2
Usama Arife73686a2019-04-08 17:30:48 +0100267 GET_VALUES(src.ptr + 2 * DILATION_Y * src_stride_y, left, middle, right);
Giorgio Arena287b5702018-02-16 11:01:04 +0000268 values0 += left * (int8)(w2.s0);
269 values0 += middle * (int8)(w2.s1);
270 values0 += right * (int8)(w2.s2);
Usama Arife73686a2019-04-08 17:30:48 +0100271#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000272 values1 += left * (int8)(w1.s0);
273 values1 += middle * (int8)(w1.s1);
274 values1 += right * (int8)(w1.s2);
Usama Arife73686a2019-04-08 17:30:48 +0100275#endif /* CONV_STRIDE_Y &&DILATION_Y == 1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000276
277#if WEIGHTS_OFFSET != 0
278 tmp = left + middle + right;
279 sum0 += tmp;
Usama Arife73686a2019-04-08 17:30:48 +0100280#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000281 sum1 += tmp;
Usama Arife73686a2019-04-08 17:30:48 +0100282#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000283#endif /* WEIGHTS_OFFSET != 0 */
284
Usama Arife73686a2019-04-08 17:30:48 +0100285#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000286 // Row3
287 GET_VALUES(src.ptr + 3 * src_stride_y, left, middle, right);
288 values1 += left * (int8)(w2.s0);
289 values1 += middle * (int8)(w2.s1);
290 values1 += right * (int8)(w2.s2);
291
292#if WEIGHTS_OFFSET != 0
293 sum1 += left + middle + right;
294#endif /* WEIGHTS_OFFSET != 0 */
Usama Arife73686a2019-04-08 17:30:48 +0100295#endif /* CONV_STRIDE_Y && DILATION_Y == 1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000296
297#if defined(HAS_BIAS)
298 values0 += (int8)(bias_value);
Usama Arife73686a2019-04-08 17:30:48 +0100299#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000300 values1 += (int8)(bias_value);
Usama Arife73686a2019-04-08 17:30:48 +0100301#endif /* CONV_STRIDE_Y & &DILATION_Y == 1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000302#endif //defined(HAS_BIAS)
303
304#if WEIGHTS_OFFSET != 0
305 values0 += sum0 * (int8)(WEIGHTS_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100306#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000307 values1 += sum1 * (int8)(WEIGHTS_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100308#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000309#endif /* WEIGHTS_OFFSET != 0 */
310
311#if INPUT_OFFSET != 0
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100312 VEC_WEIGHTS_PROMOTED_TYPE(3)
313 tmp_we = CONVERT(w0, VEC_WEIGHTS_PROMOTED_TYPE(3)) + CONVERT(w1, VEC_WEIGHTS_PROMOTED_TYPE(3)) + CONVERT(w2, VEC_WEIGHTS_PROMOTED_TYPE(3));
314
315 WEIGHTS_PROMOTED_TYPE sum_weights = tmp_we.s0 + tmp_we.s1 + tmp_we.s2;
Giorgio Arena287b5702018-02-16 11:01:04 +0000316 values0 += sum_weights * (int8)(INPUT_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100317#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000318 values1 += sum_weights * (int8)(INPUT_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100319#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */
Giorgio Arena287b5702018-02-16 11:01:04 +0000320#endif /* INPUT_OFFSET != 0 */
321
322#if K_OFFSET != 0
323 values0 += (int8)(K_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100324#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arena287b5702018-02-16 11:01:04 +0000325 values1 += (int8)(K_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100326#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/
Giorgio Arena287b5702018-02-16 11:01:04 +0000327#endif /* K_OFFSET != 0 */
328
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100329#if defined(REAL_MULTIPLIER)
330
331 values0 = CONVERT(round(CONVERT(values0, float8) * (float8)REAL_MULTIPLIER), int8);
332
333#else // defined(REAL_MULTIPLIER)
334
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100335#if defined(PER_CHANNEL_QUANTIZATION)
336 int8 res0_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values0, output_multiplier, output_shift, 8);
337 int8 res0_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values0, output_multiplier, output_shift, 8);
338 values0 = select(res0_shift_lt0, res0_shift_gt0, (int8)(output_shift) >= 0);
339#else // defined(PER_CHANNEL_QUANTIZATION)
340#if OUTPUT_SHIFT < 0
341 values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
342#else // OUTPUT_SHIFT < 0
343 values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
344#endif // OUTPUT_OFFSET < 0
345#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100346
347#endif // defined(REAL_MULTIPLIER)
348
Giorgio Arena287b5702018-02-16 11:01:04 +0000349 values0 += (int8)OUTPUT_OFFSET;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100350 VEC_TYPE(8)
351 res0 = CONVERT_SAT(values0, VEC_TYPE(8));
Giorgio Arena287b5702018-02-16 11:01:04 +0000352
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000353 vstore8(ACTIVATION_FUNC(res0), 0, dst.ptr);
Usama Arife73686a2019-04-08 17:30:48 +0100354#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100355#if defined(REAL_MULTIPLIER)
356
357 values1 = CONVERT(round(CONVERT(values1, float8) * (float8)REAL_MULTIPLIER), int8);
358
359#else // defined(REAL_MULTIPLIER)
Giorgio Arena287b5702018-02-16 11:01:04 +0000360
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100361#if defined(PER_CHANNEL_QUANTIZATION)
362 int8 res1_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values1, output_multiplier, output_shift, 8);
363 int8 res1_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values1, output_multiplier, output_shift, 8);
364 values1 = select(res1_shift_lt0, res1_shift_gt0, (int8)(output_shift) >= 0);
365#else // defined(PER_CHANNEL_QUANTIZATION)
366#if OUTPUT_SHIFT < 0
367 values1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
368#else // OUTPUT_SHIFT < 0
369 values1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
370#endif // OUTPUT_OFFSET < 0
371#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100372
373#endif // defined(REAL_MULTIPLIER)
374
Giorgio Arena287b5702018-02-16 11:01:04 +0000375 values1 += (int8)OUTPUT_OFFSET;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100376 VEC_TYPE(8)
377 res1 = CONVERT_SAT(values1, VEC_TYPE(8));
Giorgio Arena287b5702018-02-16 11:01:04 +0000378
Giorgio Arena99ac60b2018-02-16 15:17:23 +0000379 vstore8(ACTIVATION_FUNC(res1), 0, dst.ptr + dst_stride_y);
Usama Arife73686a2019-04-08 17:30:48 +0100380#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/
Dmitry Savenkod7295b72017-11-20 22:00:08 +0700381}
Giorgio Arena287b5702018-02-16 11:01:04 +0000382
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100383#else // !defined(IS_DOT8)
384
Usama Arife73686a2019-04-08 17:30:48 +0100385#if DILATION_X == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100386#if CONV_STRIDE_X == 1
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100387#define GET_VALUES(first_value, left, middle, right) \
388 ({ \
389 VEC_TYPE(8) \
390 temp0 = vload8(0, (__global DATA_TYPE *)(first_value)); \
391 VEC_TYPE(2) \
392 temp1 = vload2(0, (__global DATA_TYPE *)(first_value + 8 * sizeof(DATA_TYPE))); \
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100393 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100394 left = temp0.s01234567; \
395 middle = (VEC_TYPE(8))(temp0.s1234, temp0.s567, temp1.s0); \
396 right = (VEC_TYPE(8))(temp0.s2345, temp0.s67, temp1.s01); \
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000397 })
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100398#elif CONV_STRIDE_X == 2
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100399#define GET_VALUES(first_value, left, middle, right) \
400 ({ \
401 VEC_TYPE(16) \
402 temp0 = vload16(0, (__global DATA_TYPE *)(first_value)); \
403 DATA_TYPE temp1 = *((__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE))); \
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100404 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100405 left = temp0.s02468ace; \
406 middle = temp0.s13579bdf; \
407 right = (VEC_TYPE(8))(temp0.s2468, temp0.sace, temp1); \
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100408 })
409#else /* CONV_STRIDE_X */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100410#define GET_VALUES(first_value, left, middle, right) \
411 ({ \
412 VEC_TYPE(16) \
413 temp0 = vload16(0, (__global DATA_TYPE *)(first_value)); \
414 VEC_TYPE(8) \
415 temp1 = vload8(0, (__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE))); \
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100416 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100417 left = (VEC_TYPE(8))(temp0.s0369, temp0.scf, temp1.s25); \
418 middle = (VEC_TYPE(8))(temp0.s147a, temp0.sd, temp1.s036); \
419 right = (VEC_TYPE(8))(temp0.s258b, temp0.se, temp1.s147); \
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100420 })
421#endif /* CONV_STRIDE_X */
Usama Arife73686a2019-04-08 17:30:48 +0100422#else /*DILATION_X==1*/
423
424#if CONV_STRIDE_X == 1
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100425#define GET_VALUES(first_value, left, middle, right) \
426 ({ \
427 left = vload8(0, (__global DATA_TYPE *)(first_value)); \
428 middle = vload8(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))); \
429 right = vload8(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))); \
Usama Arife73686a2019-04-08 17:30:48 +0100430 })
431#elif CONV_STRIDE_X == 2
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100432#define GET_VALUES(first_value, left, middle, right) \
433 ({ \
434 VEC_TYPE(16) \
435 temp0 = vload16(0, (__global DATA_TYPE *)(first_value)); \
436 left = temp0.s02468ace; \
437 temp0 = vload16(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))); \
438 middle = temp0.s02468ace; \
439 temp0 = vload16(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))); \
440 right = temp0.s02468ace; \
Usama Arife73686a2019-04-08 17:30:48 +0100441 })
442#else /* CONV_STRIDE_X */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100443#define GET_VALUES(first_value, left, middle, right) \
444 ({ \
445 VEC_TYPE(16) \
446 temp0 = vload16(0, (__global DATA_TYPE *)(first_value)); \
447 VEC_TYPE(8) \
448 temp1 = vload8(0, (__global DATA_TYPE *)(first_value + 16 * sizeof(DATA_TYPE)))); \
449 left = (VEC_TYPE(8))(temp0.s0369, temp0.scf, temp1.s25); \
Usama Arife73686a2019-04-08 17:30:48 +0100450 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100451 temp0 = vload16(0, (__global DATA_TYPE *)(first_value + DILATION_X * sizeof(DATA_TYPE))); \
452 temp1 = vload8(0, (__global DATA_TYPE *)(first_value + (16 + DILATION_X) * sizeof(DATA_TYPE))); \
453 middle = (VEC_TYPE(8))(temp0.s0369, temp0.scf, temp1.s25); \
Usama Arife73686a2019-04-08 17:30:48 +0100454 \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100455 temp0 = vload16(0, (__global DATA_TYPE *)(first_value + 2 * DILATION_X * sizeof(DATA_TYPE))); \
456 temp1 = vload8(0, (__global DATA_TYPE *)(first_value + (16 + 2 * DILATION_X) * sizeof(DATA_TYPE))); \
457 right = (VEC_TYPE(8))(temp0.s0369, temp0.scf, temp1.s25); \
Usama Arife73686a2019-04-08 17:30:48 +0100458 })
459
460#endif /* CONV_STRIDE_X */
461#endif /*DILATION_X==1*/
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100462/** This function computes the depthwise convolution quantized using dot product when the data layout is NCHW.
Giorgio Arenadfca60b2018-01-31 10:30:59 +0000463 *
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100464 * @note Per-channel quantization is not supported by this kernel.
465 *
466 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8
467 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
468 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
469 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
470 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
471 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
472 * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes)
473 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
474 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: QASYMM8
475 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
476 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
477 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
478 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
479 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
480 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
481 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
482 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8/QSYMM8_PER_CHANNEL
483 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
484 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
485 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
486 * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes)
487 * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes)
488 * @param[in] weights_step_z weights_stride_z * number of elements along Y processed per workitem(in bytes)
489 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
490 * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32
491 * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes)
492 * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
493 * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector
494 * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32
495 * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes)
496 * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
497 * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector
498 * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32
499 * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes)
500 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
501 * @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 +0000502 */
503
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100504__kernel void dwc_3x3_native_quantized8_dot8_nchw(
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100505 TENSOR3D_DECLARATION(src),
506 TENSOR3D_DECLARATION(dst),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100507 TENSOR3D_DECLARATION(weights),
508 VECTOR_DECLARATION(output_multipliers),
509 VECTOR_DECLARATION(output_shifts)
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100510#if defined(HAS_BIAS)
511 ,
512 VECTOR_DECLARATION(biases)
513#endif //defined(HAS_BIAS)
514)
515{
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100516 Image src = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(src);
517 Image dst = CONVERT_TENSOR3D_TO_IMAGE_STRUCT(dst);
518 Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(weights);
519 Vector output_multipliers = CONVERT_TO_VECTOR_STRUCT_NO_STEP(output_multipliers);
520 Vector output_shifts = CONVERT_TO_VECTOR_STRUCT_NO_STEP(output_shifts);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100521
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100522 // Extract channel and linearized batch indices
523 const int channel = get_global_id(2) % DST_CHANNELS;
524 const int batch = get_global_id(2) / DST_CHANNELS;
525
526#if defined(HAS_BIAS)
527 Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases);
528
529 const int bias_value = *((__global int *)(vector_offset(&biases, channel)));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100530#endif //defined(HAS_BIAS)
531
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100532 // Load relevant input and weights data (Accounts depth multiplier when indexing input, OFM = IFM * DEPTH_MULTIPLIER)
533 src.ptr -= batch * (DST_CHANNELS / DEPTH_MULTIPLIER) * (DEPTH_MULTIPLIER - 1) * src_step_z + (channel - (channel / DEPTH_MULTIPLIER)) * src_step_z;
534 __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 +0100535
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100536 VEC_TYPE(3)
537 w0 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 0 * weights_stride_y));
538 VEC_TYPE(3)
539 w1 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 1 * weights_stride_y));
540 VEC_TYPE(3)
541 w2 = vload3(0, (__global WEIGHTS_TYPE *)(weights_addr + 2 * weights_stride_y));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100542
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100543 const int output_multiplier = *((__global int *)vector_offset(&output_multipliers, 0));
544 const int output_shift = *((__global int *)vector_offset(&output_shifts, 0));
545
546 VEC_TYPE(8)
547 left0, middle0, right0;
548 VEC_TYPE(8)
549 left1, middle1, right1;
550 VEC_TYPE(8)
551 left2, middle2, right2;
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100552
553 int8 values0 = 0;
554 int8 sum0 = 0;
555
556 GET_VALUES(src.ptr + 0 * src_stride_y, left0, middle0, right0);
Usama Arife73686a2019-04-08 17:30:48 +0100557 GET_VALUES(src.ptr + DILATION_Y * src_stride_y, left1, middle1, right1);
558 GET_VALUES(src.ptr + 2 * DILATION_Y * src_stride_y, left2, middle2, right2);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100559
560#if WEIGHTS_OFFSET != 0
561 sum0 += convert_int8(left0) + convert_int8(middle0) + convert_int8(right0);
562 sum0 += convert_int8(left1) + convert_int8(middle1) + convert_int8(right1);
563 sum0 += convert_int8(left2) + convert_int8(middle2) + convert_int8(right2);
564#endif /* WEIGHTS_OFFSET != 0 */
565
Usama Arife73686a2019-04-08 17:30:48 +0100566#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100567 // If conv_stride_y is equals to 1, we compute two output rows
568
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100569 VEC_TYPE(8)
570 left3, middle3, right3;
571 int8 values1 = 0;
572 int8 sum1 = 0;
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100573
574 GET_VALUES(src.ptr + 3 * src_stride_y, left3, middle3, right3);
575
576#if WEIGHTS_OFFSET != 0
577 sum1 += convert_int8(left1) + convert_int8(middle1) + convert_int8(right1);
578 sum1 += convert_int8(left2) + convert_int8(middle2) + convert_int8(right2);
579 sum1 += convert_int8(left3) + convert_int8(middle3) + convert_int8(right3);
580#endif /* WEIGHTS_OFFSET != 0 */
Usama Arife73686a2019-04-08 17:30:48 +0100581#endif // CONV_STRIDE_Y == 1 && DILATION_Y==1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100582
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100583 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);
584 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 +0100585 values0.s0 += right2.s0 * w2.s2;
586
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100587 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);
588 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 +0100589 values0.s1 += right2.s1 * w2.s2;
590
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100591 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);
592 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 +0100593 values0.s2 += right2.s2 * w2.s2;
594
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100595 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);
596 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 +0100597 values0.s3 += right2.s3 * w2.s2;
598
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100599 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);
600 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 +0100601 values0.s4 += right2.s4 * w2.s2;
602
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100603 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);
604 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 +0100605 values0.s5 += right2.s5 * w2.s2;
606
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100607 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);
608 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 +0100609 values0.s6 += right2.s6 * w2.s2;
610
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100611 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);
612 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 +0100613 values0.s7 += right2.s7 * w2.s2;
614
Usama Arife73686a2019-04-08 17:30:48 +0100615#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100616 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);
617 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 +0100618 values1.s0 += right3.s0 * w2.s2;
619
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100620 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);
621 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 +0100622 values1.s1 += right3.s1 * w2.s2;
623
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100624 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);
625 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 +0100626 values1.s2 += right3.s2 * w2.s2;
627
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100628 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);
629 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 +0100630 values1.s3 += right3.s3 * w2.s2;
631
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100632 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);
633 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 +0100634 values1.s4 += right3.s4 * w2.s2;
635
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100636 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);
637 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 +0100638 values1.s5 += right3.s5 * w2.s2;
639
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100640 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);
641 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 +0100642 values1.s6 += right3.s6 * w2.s2;
643
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100644 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);
645 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 +0100646 values1.s7 += right3.s7 * w2.s2;
Usama Arife73686a2019-04-08 17:30:48 +0100647#endif // CONV_STRIDE_Y == 1 && DILATION_Y==1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100648
649#if defined(HAS_BIAS)
650 values0 += (int8)(bias_value);
Usama Arife73686a2019-04-08 17:30:48 +0100651#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100652 values1 += (int8)(bias_value);
Usama Arife73686a2019-04-08 17:30:48 +0100653#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100654#endif //defined(HAS_BIAS)
655
656#if WEIGHTS_OFFSET != 0
657 values0 += sum0 * (int8)(WEIGHTS_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100658#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100659 values1 += sum1 * (int8)(WEIGHTS_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100660#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1 */
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100661#endif /* WEIGHTS_OFFSET != 0 */
662
663#if INPUT_OFFSET != 0
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100664 WEIGHTS_PROMOTED_TYPE sum_weights = 0;
665 VEC_WEIGHTS_PROMOTED_TYPE(3)
666 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 +0100667 sum_weights += tmp_we.s0 + tmp_we.s1 + tmp_we.s2;
668 values0 += sum_weights * (int8)(INPUT_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100669#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100670 values1 += sum_weights * (int8)(INPUT_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100671#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100672#endif /* INPUT_OFFSET != 0 */
673
674#if K_OFFSET != 0
675 values0 += (int8)(K_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100676#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100677 values1 += (int8)(K_OFFSET);
Usama Arife73686a2019-04-08 17:30:48 +0100678#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100679#endif /* K_OFFSET != 0 */
680
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100681#if defined(REAL_MULTIPLIER)
682
683 values0 = CONVERT(round(CONVERT(values0, float8) * (float8)REAL_MULTIPLIER), int8);
684
685#else // defined(REAL_MULTIPLIER)
686
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100687#if defined(PER_CHANNEL_QUANTIZATION)
688 int8 res0_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values0, output_multiplier, output_shift, 8);
689 int8 res0_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values0, output_multiplier, output_shift, 8);
690 values0 = select(res0_shift_lt0, res0_shift_gt0, (int8)(output_shift) >= 0);
691#else // defined(PER_CHANNEL_QUANTIZATION)
692#if OUTPUT_SHIFT < 0
693 values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
694#else // OUTPUT_SHIFT < 0
695 values0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
696#endif // OUTPUT_OFFSET < 0
697#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100698
699#endif // defined(REAL_MULTIPLIER)
700
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100701 values0 += (int8)OUTPUT_OFFSET;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100702 VEC_TYPE(8)
703 res0 = CONVERT_SAT(values0, VEC_TYPE(8));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100704
705 vstore8(ACTIVATION_FUNC(res0), 0, dst.ptr);
Usama Arife73686a2019-04-08 17:30:48 +0100706#if CONV_STRIDE_Y == 1 && DILATION_Y == 1
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100707
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100708#if defined(REAL_MULTIPLIER)
709
710 values1 = CONVERT(round(CONVERT(values1, float8) * (float8)REAL_MULTIPLIER), int8);
711
712#else // defined(REAL_MULTIPLIER)
713
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100714#if defined(PER_CHANNEL_QUANTIZATION)
715 int8 res1_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values1, output_multiplier, output_shift, 8);
716 int8 res1_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values1, output_multiplier, output_shift, 8);
717 values1 = select(res1_shift_lt0, res1_shift_gt0, (int8)(output_shift) >= 0);
718#else // defined(PER_CHANNEL_QUANTIZATION)
719#if OUTPUT_SHIFT < 0
720 values1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(values1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
721#else // OUTPUT_SHIFT < 0
722 values1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(values1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, 8);
723#endif // OUTPUT_OFFSET < 0
724#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100725
726#endif // defined(REAL_MULTIPLIER)
727
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100728 values1 += (int8)OUTPUT_OFFSET;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100729 VEC_TYPE(8)
730 res1 = CONVERT_SAT(values1, VEC_TYPE(8));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100731
732 vstore8(ACTIVATION_FUNC(res1), 0, dst.ptr + dst_stride_y);
Usama Arife73686a2019-04-08 17:30:48 +0100733#endif /* CONV_STRIDE_Y == 1 && DILATION_Y==1*/
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100734}
735
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100736#endif // !defined(IS_DOT8)
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100737
Georgios Pinitase55b40a2018-09-13 17:20:04 +0100738#endif /* defined(CONV_STRIDE_Y) && defined(CONV_STRIDE_X) && defined(DEPTH_MULTIPLIER) && defined(DST_CHANNELS) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100739
740#if defined(VEC_SIZE) && defined(SRC_DIM_1) && defined(SRC_DIM_2) && defined(CONV_PAD_TOP) && defined(CONV_PAD_LEFT)
741
742#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)
743
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100744#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 +0100745
746#if WEIGHTS_OFFSET != 0
747#define MULTIPLY_ADD_ACCUMULATE(x, y, acc, sum) \
748 ({ \
749 sum += CONVERT(x, VEC_INT); \
750 MULTIPLY_ADD(x, y, acc); \
751 })
752#else /* WEIGHTS_OFFSET != 0 */
753#define MULTIPLY_ADD_ACCUMULATE(x, y, acc, sum) MULTIPLY_ADD(x, y, acc)
754#endif /* WEIGHTS_OFFSET != 0 */
755
Georgios Pinitasdaa38552018-08-28 17:43:18 +0100756#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
giuros016d109962019-01-07 17:47:19 +0000757#define DOT_PRODUCT(acc, val0, val1, val2, val3, val4, val5, val6, val7, val8, w0, w1) \
758 ({ \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100759 ARM_DOT((VEC_TYPE(4))(val0, val1, val2, val3), w0.s0123, acc); \
760 ARM_DOT((VEC_TYPE(4))(val4, val5, val6, val7), w0.s4567, acc); \
giuros016d109962019-01-07 17:47:19 +0000761 acc += val8 * w1; \
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100762 })
763
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100764#define DOT_PRODUCT_REDUCTION(sum, val0, val1, val2, val3, val4, val5, val6, val7, val8) \
765 ({ \
giuros016d109962019-01-07 17:47:19 +0000766 sum = val0; \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100767 ARM_DOT((VEC_TYPE(4))(val1, val2, val3, val4), (VEC_TYPE(4))1, sum); \
768 ARM_DOT((VEC_TYPE(4))(val5, val6, val7, val8), (VEC_TYPE(4))1, sum); \
giuros016d109962019-01-07 17:47:19 +0000769 })
770
771#define DOT_PRODUCT_REDUCTION_WEIGHTS(sum, w0, w1) \
772 ({ \
773 sum = w1; \
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100774 ARM_DOT(w0.s0123, (VEC_TYPE(4))1, sum); \
775 ARM_DOT(w0.s4567, (VEC_TYPE(4))1, sum); \
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100776 })
777
Georgios Pinitasdaa38552018-08-28 17:43:18 +0100778#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100779
Pablo Tello47104362019-02-27 13:32:51 +0000780#if defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y) && VEC_SIZE == 4
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100781/** This function computes the depthwise convolution quantized for NHWC data layout when the stride along the width or height is not 1.
782 *
Pablo Tello47104362019-02-27 13:32:51 +0000783 * @note This kernel assumes VEC_SIZE is 4.
giuros016d109962019-01-07 17:47:19 +0000784 * @note The weights tensor is expected to be reshaped using @ref CLDepthwiseConvolutionLayerReshapeWeightsKernel.
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100785 * @note The number of elements read per thread must be passed at compile time using -DVEC_SIZE (e.g. -DVEC_SIZE=2)
786 * @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)
787 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_TOP (e.g. -DCONV_PAD_TOP=1)
788 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_LEFT (e.g. -DCONV_PAD_LEFT=1)
789 * @note The convolution stride along the width must be passed at compile time using -DCONV_STRIDE_X (e.g. -DCONV_STRIDE_Y=X)
790 * @note The convolution stride along the height must be passed at compile time using -DCONV_STRIDE_Y (e.g. -DCONV_STRIDE_Y=1)
791 *
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100792 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8
793 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
794 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
795 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
796 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
797 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
798 * @param[in] src_step_z src_stride_y * number of elements along Z processed per workitem(in bytes)
799 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes)
800 * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
801 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
802 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
803 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
804 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
805 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
806 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
807 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
808 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
809 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes)
810 * @param[in] dst_step_w dst_stride_w * number of elements along W processed per workitem(in bytes)
811 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
812 * @param[in] weights_ptr Pointer to the weights tensor reshaped. Supported data types: QASYMM8/QSYMM8_PER_CHANNEL
813 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
814 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
815 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
816 * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes)
817 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
818 * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32
819 * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes)
820 * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
821 * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector
822 * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32
823 * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes)
824 * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
825 * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector
826 * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32
827 * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes)
828 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
829 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector
830 * @param[in] max_offset Max offset for the input tensor
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100831 */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100832__kernel void dwc_3x3_reshaped_quantized8_nhwc(
Georgios Pinitas37044642018-10-30 14:53:25 +0000833 TENSOR4D_DECLARATION(src),
834 TENSOR4D_DECLARATION(dst),
giuros016d109962019-01-07 17:47:19 +0000835 IMAGE_DECLARATION(weights),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100836 VECTOR_DECLARATION(output_multipliers),
837 VECTOR_DECLARATION(output_shifts),
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100838#if defined(HAS_BIAS)
839 VECTOR_DECLARATION(biases),
840#endif /* defined(HAS_BIAS) */
841 int max_offset)
842{
843 const int x = get_global_id(0); // channels
844 const int y = get_global_id(1); // spatial coordinate x
Georgios Pinitas37044642018-10-30 14:53:25 +0000845#if defined(DST_DEPTH)
846 int z = get_global_id(2) % (int)DST_DEPTH; // spatial coordinate y
847 int b = get_global_id(2) / (int)DST_DEPTH; // batch
Pablo Tello47104362019-02-27 13:32:51 +0000848#else // defined(DST_DEPTH)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100849 int z = get_global_id(2); // spatial coordinate y
Pablo Tello47104362019-02-27 13:32:51 +0000850#endif // defined(DST_DEPTH)
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100851
giuros016d109962019-01-07 17:47:19 +0000852 __global uchar *weights_addr = weights_ptr + weights_offset_first_element_in_bytes + x * weights_stride_y;
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100853
Georgios Pinitas37044642018-10-30 14:53:25 +0000854#if defined(DST_DEPTH)
855 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE + b * src_stride_w;
856#else /* defined(DST_DEPTH) */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100857 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE;
Georgios Pinitas37044642018-10-30 14:53:25 +0000858#endif /* defined(DST_DEPTH) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100859
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100860 int z_coord = 0;
861 int4 offset = 0;
Usama Arife73686a2019-04-08 17:30:48 +0100862 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 +0100863
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100864 // Only for y = 0 we can have a negative coordinate. If so, we convert it to SRC_DIM_1
865 y_coord.s0 = min((uint)y_coord.s0, (uint)SRC_DIM_1);
866 y_coord.s1 = min((uint)y_coord.s1, (uint)SRC_DIM_1);
867 y_coord.s2 = min((uint)y_coord.s2, (uint)SRC_DIM_1);
868 y_coord.s3 = min((uint)y_coord.s3, (uint)SRC_DIM_1);
869
870 int4 y_offset = convert_int4(y_coord * (int)src_stride_y);
871
giuros016d109962019-01-07 17:47:19 +0000872 // We compute VEC_SIZEx1x1 [C,W,H] elements
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100873 VEC_INT acc = 0, sum = 0;
874
875 // Load weights
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100876 VEC_DATA_TYPE(WEIGHTS_TYPE, 16)
877 w0_tmp = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr));
878 VEC_DATA_TYPE(WEIGHTS_TYPE, 16)
879 w1_tmp = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr + 16));
880 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
881 w8 = VLOAD(4)(0, (__global WEIGHTS_TYPE *)(weights_addr + 2 * 16));
Pablo Tello47104362019-02-27 13:32:51 +0000882
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100883 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
884 w0 = w0_tmp.s0123;
885 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
886 w1 = w0_tmp.s4567;
887 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
888 w2 = w0_tmp.s89AB;
889 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
890 w3 = w0_tmp.sCDEF;
Pablo Tello47104362019-02-27 13:32:51 +0000891
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100892 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
893 w4 = w1_tmp.s0123;
894 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
895 w5 = w1_tmp.s4567;
896 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
897 w6 = w1_tmp.s89AB;
898 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
899 w7 = w1_tmp.sCDEF;
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100900
901#if INPUT_OFFSET != 0
Georgios Pinitas4e0d3812018-08-01 20:16:34 +0100902 VEC_INT sum_we = CONVERT(w0, VEC_INT) + CONVERT(w1, VEC_INT) + CONVERT(w2, VEC_INT)
903 + CONVERT(w3, VEC_INT) + CONVERT(w4, VEC_INT) + CONVERT(w5, VEC_INT)
904 + CONVERT(w6, VEC_INT) + CONVERT(w7, VEC_INT) + CONVERT(w8, VEC_INT);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100905#endif /* INPUT_OFFSET != 0 */
906
907 // Load input values
908 // z == 0
909 // Clamp z_coord as for z = 0, it can be negative
910 // z_coord is casted to unsigned int in order to use just a min() operation
911 // A "-1" 32 bit signed variable converted to unsigned gives 4294967295
Georgios Pinitas4e0d3812018-08-01 20:16:34 +0100912 z_coord = z * (int)CONV_STRIDE_Y - (int)CONV_PAD_TOP;
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100913 z_coord = min((uint)z_coord, (uint)SRC_DIM_2);
914 offset = y_offset + (int4)(z_coord * src_stride_z);
Georgios Pinitas4e0d3812018-08-01 20:16:34 +0100915 offset = min(offset, (int4)max_offset);
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100916
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100917 VEC_TYPE(VEC_SIZE)
918 values0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
919 VEC_TYPE(VEC_SIZE)
920 values1 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
921 VEC_TYPE(VEC_SIZE)
922 values2 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100923
924 // z == 1
925 // z_coord can be only negative for z = 0 so we do not need to clamp it
926 // 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 +0100927 z_coord = z * (int)CONV_STRIDE_Y - (int)CONV_PAD_TOP + DILATION_Y;
928 offset = y_offset + (int4)(z_coord * src_stride_z);
929 VEC_TYPE(VEC_SIZE)
930 values3 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
931 VEC_TYPE(VEC_SIZE)
932 values4 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
933 VEC_TYPE(VEC_SIZE)
934 values5 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100935
936 // z == 2
Usama Arife73686a2019-04-08 17:30:48 +0100937 // 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 +0100938 z_coord = z * (int)CONV_STRIDE_Y - (int)CONV_PAD_TOP + DILATION_Y * 2;
939 offset = y_offset + (int4)(z_coord * src_stride_z);
940 offset = min(offset, (int4)max_offset);
941 VEC_TYPE(VEC_SIZE)
942 values6 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
943 VEC_TYPE(VEC_SIZE)
944 values7 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
945 VEC_TYPE(VEC_SIZE)
946 values8 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
Giorgio Arenaeff8d952018-07-02 15:29:57 +0100947
948 MULTIPLY_ADD_ACCUMULATE(values0, w0, acc, sum);
949 MULTIPLY_ADD_ACCUMULATE(values1, w1, acc, sum);
950 MULTIPLY_ADD_ACCUMULATE(values2, w2, acc, sum);
951
952 MULTIPLY_ADD_ACCUMULATE(values3, w3, acc, sum);
953 MULTIPLY_ADD_ACCUMULATE(values4, w4, acc, sum);
954 MULTIPLY_ADD_ACCUMULATE(values5, w5, acc, sum);
955
956 MULTIPLY_ADD_ACCUMULATE(values6, w6, acc, sum);
957 MULTIPLY_ADD_ACCUMULATE(values7, w7, acc, sum);
958 MULTIPLY_ADD_ACCUMULATE(values8, w8, acc, sum);
959
960#if defined(HAS_BIAS)
961 Vector biases = CONVERT_TO_VECTOR_STRUCT(biases);
962 VEC_INT bias_values = VLOAD(VEC_SIZE)(0, (__global int *)biases.ptr);
963 acc += bias_values;
964#endif // defined(HAS_BIAS)
965
966#if WEIGHTS_OFFSET != 0
967 acc += WEIGHTS_OFFSET * sum;
968#endif /* WEIGHTS_OFFSET != 0 */
969
970#if INPUT_OFFSET != 0
971 acc += INPUT_OFFSET * sum_we;
972#endif /* INPUT_OFFSET != 0 */
973
974#if K_OFFSET != 0
975 acc += (VEC_INT)K_OFFSET;
976#endif /* K_OFFSET != 0 */
977
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100978#if defined(REAL_MULTIPLIER)
979
980 acc = CONVERT(round(CONVERT(acc, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
981
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100982#else // defined(REAL_MULTIPLIER)
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100983
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +0100984#if defined(PER_CHANNEL_QUANTIZATION)
985 Vector output_multipliers = CONVERT_TO_VECTOR_STRUCT(output_multipliers);
986 Vector output_shifts = CONVERT_TO_VECTOR_STRUCT(output_shifts);
987 VEC_INT output_multiplier = VLOAD(VEC_SIZE)(0, (__global int *)output_multipliers.ptr);
988 VEC_INT output_shift = VLOAD(VEC_SIZE)(0, (__global int *)output_shifts.ptr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100989
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100990 VEC_INT res_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc, output_multiplier, output_shift, VEC_SIZE);
991 VEC_INT res_shift_gt0 = asymm_mult_by_quant_multiplier_less_than_one(acc, output_multiplier, output_shift);
992 acc = select(res_shift_lt0, res_shift_gt0, output_shift >= 0);
993#else // defined(PER_CHANNEL_QUANTIZATION)
994#if OUTPUT_SHIFT < 0
995 acc = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE);
996#else // OUTPUT_SHIFT < 0
997 acc = asymm_mult_by_quant_multiplier_less_than_one(acc, OUTPUT_MULTIPLIER, OUTPUT_SHIFT);
998#endif // OUTPUT_SHIFT < 0
999#endif // defined(PER_CHANNEL_QUANTIZATION)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001000
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001001#endif // defined(REAL_MULTIPLIER)
1002
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001003 acc += (VEC_INT)OUTPUT_OFFSET;
1004
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001005 VEC_TYPE(VEC_SIZE)
1006 res = CONVERT_SAT(acc, VEC_TYPE(VEC_SIZE));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001007
Georgios Pinitas37044642018-10-30 14:53:25 +00001008#if defined(DST_DEPTH)
1009 __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;
1010#else /* defined(DST_DEPTH) */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001011 __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 +00001012#endif /* defined(DST_DEPTH) */
1013
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001014 VSTORE(VEC_SIZE)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001015 (ACTIVATION_FUNC(res), 0, (__global DATA_TYPE *)(dst_addr));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001016}
1017#endif // defined(CONV_STRIDE_X) && defined(CONV_STRIDE_Y)
1018
Pablo Tello47104362019-02-27 13:32:51 +00001019#if defined(NUM_ROWS_PROCESSED) && defined(NUM_PLANES_PROCESSED) && VEC_SIZE == 4
giuros016d109962019-01-07 17:47:19 +00001020/** 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 +01001021 *
Pablo Tello47104362019-02-27 13:32:51 +00001022 * @note This kernel assumes VEC_SIZE is 4.
giuros016d109962019-01-07 17:47:19 +00001023 * @note The weights tensor is expected to be reshaped using @ref CLDepthwiseConvolutionLayerReshapeWeightsKernel.
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001024 * @note The number of elements read per thread must be passed at compile time using -DVEC_SIZE (e.g. -DVEC_SIZE=2)
1025 * @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)
1026 * @note The number of rows processed per thread must be passed at compile time using -DNUM_ROWS_PROCESSED (i.e. -DNUM_ROWS_PROCESSED=2)
1027 * @note The number of planes processed per thread must be passed at compile time using -DNUM_PLANES_PROCESSED (i.e. -DNUM_PLANES_PROCESSED=2)
1028 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_TOP (e.g. -DCONV_PAD_TOP=1)
1029 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_LEFT (e.g. -DCONV_PAD_LEFT=1).
1030 *
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001031 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8
1032 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1033 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1034 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1035 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1036 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1037 * @param[in] src_step_z src_stride_y * number of elements along Z processed per workitem(in bytes)
1038 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes)
1039 * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
1040 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1041 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
1042 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1043 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
1044 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1045 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
1046 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
1047 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
1048 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes)
1049 * @param[in] dst_step_w dst_stride_w * number of elements along W processed per workitem(in bytes)
1050 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1051 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8/QSYMM8_PER_CHANNEL
1052 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
1053 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
1054 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
1055 * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes)
1056 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
1057 * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32
1058 * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes)
1059 * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1060 * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector
1061 * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32
1062 * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes)
1063 * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1064 * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector
1065 * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32
1066 * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes)
1067 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1068 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector
1069 * @param[in] max_offset Max offset for the input tensor
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001070 */
1071
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001072__kernel void dwc_3x3_reshaped_quantized8_stride1_nhwc(
Georgios Pinitas37044642018-10-30 14:53:25 +00001073 TENSOR4D_DECLARATION(src),
1074 TENSOR4D_DECLARATION(dst),
giuros016d109962019-01-07 17:47:19 +00001075 IMAGE_DECLARATION(weights),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001076 VECTOR_DECLARATION(output_multipliers),
1077 VECTOR_DECLARATION(output_shifts),
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001078#if defined(HAS_BIAS)
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001079 VECTOR_DECLARATION(biases),
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001080#endif /* defined(HAS_BIAS) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001081 int max_offset)
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001082{
Giorgio Arenafa23f112018-06-19 11:27:38 +01001083 int x = get_global_id(0);
1084 int y = get_global_id(1);
Georgios Pinitas37044642018-10-30 14:53:25 +00001085#if defined(DST_DEPTH)
1086 int z = get_global_id(2) % (int)DST_DEPTH; // spatial coordinate y
1087 int b = get_global_id(2) / (int)DST_DEPTH; // batch
Pablo Tello47104362019-02-27 13:32:51 +00001088#else // defined(DST_DEPTH)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001089 int z = get_global_id(2); // spatial coordinate y
Pablo Tello47104362019-02-27 13:32:51 +00001090#endif // defined(DST_DEPTH)
Giorgio Arenafa23f112018-06-19 11:27:38 +01001091
giuros016d109962019-01-07 17:47:19 +00001092 __global uchar *weights_addr = weights_ptr + weights_offset_first_element_in_bytes + x * weights_stride_y;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001093
Georgios Pinitas37044642018-10-30 14:53:25 +00001094#if defined(DST_DEPTH)
1095 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE + b * src_stride_w;
1096#else /* defined(DST_DEPTH) */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001097 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE;
Georgios Pinitas37044642018-10-30 14:53:25 +00001098#endif /* defined(DST_DEPTH) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001099
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001100 int z_coord = 0;
1101 int4 offset = 0;
1102 int4 y_coord = ((int4)(y * NUM_ROWS_PROCESSED) + (int4)(0, 1, 2, 3)) - (int)CONV_PAD_LEFT;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001103
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001104 // Only for y = 0 we can have a negative coordinate. If so, we convert it to SRC_DIM_1
1105 y_coord.s0 = min((uint)y_coord.s0, (uint)SRC_DIM_1);
1106 y_coord.s1 = min((uint)y_coord.s1, (uint)SRC_DIM_1);
1107 y_coord.s2 = min((uint)y_coord.s2, (uint)SRC_DIM_1);
1108 y_coord.s3 = min((uint)y_coord.s3, (uint)SRC_DIM_1);
1109
1110 int4 y_offset = convert_int4(y_coord * (int)src_stride_y);
1111
1112 // We compute 4x2x2 [C,W,H] elements
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001113 VEC_INT acc0 = 0, sum0 = 0;
1114 VEC_INT acc1 = 0, sum1 = 0;
1115 VEC_INT acc2 = 0, sum2 = 0;
1116 VEC_INT acc3 = 0, sum3 = 0;
1117
1118 // Load weights
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001119 VEC_DATA_TYPE(WEIGHTS_TYPE, 16)
1120 w0_tmp = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr));
1121 VEC_DATA_TYPE(WEIGHTS_TYPE, 16)
1122 w1_tmp = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr + 16));
1123 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1124 w8 = VLOAD(4)(0, (__global WEIGHTS_TYPE *)(weights_addr + 2 * 16));
Pablo Tello47104362019-02-27 13:32:51 +00001125
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001126 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1127 w0 = w0_tmp.s0123;
1128 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1129 w1 = w0_tmp.s4567;
1130 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1131 w2 = w0_tmp.s89AB;
1132 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1133 w3 = w0_tmp.sCDEF;
Pablo Tello47104362019-02-27 13:32:51 +00001134
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001135 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1136 w4 = w1_tmp.s0123;
1137 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1138 w5 = w1_tmp.s4567;
1139 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1140 w6 = w1_tmp.s89AB;
1141 VEC_DATA_TYPE(WEIGHTS_TYPE, 4)
1142 w7 = w1_tmp.sCDEF;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001143
1144#if INPUT_OFFSET != 0
Georgios Pinitas4e0d3812018-08-01 20:16:34 +01001145 VEC_INT sum_we = CONVERT(w0, VEC_INT) + CONVERT(w1, VEC_INT) + CONVERT(w2, VEC_INT)
1146 + CONVERT(w3, VEC_INT) + CONVERT(w4, VEC_INT) + CONVERT(w5, VEC_INT)
1147 + CONVERT(w6, VEC_INT) + CONVERT(w7, VEC_INT) + CONVERT(w8, VEC_INT);
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001148#endif /* INPUT_OFFSET != 0 */
1149
1150 // Load input values
1151 // z == 0
1152 // Clamp z_coord as for z = 0, it can be negative
1153 // z_coord is casted to unsigned int in order to use just a min() operation
1154 // A "-1" 32 bit signed variable converted to unsigned gives 4294967295
Georgios Pinitas4e0d3812018-08-01 20:16:34 +01001155 z_coord = z * (int)NUM_PLANES_PROCESSED - (int)CONV_PAD_TOP;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001156 z_coord = min((uint)z_coord, (uint)SRC_DIM_2);
1157 offset = y_offset + (int4)(z_coord * src_stride_z);
Georgios Pinitas4e0d3812018-08-01 20:16:34 +01001158 offset = min(offset, (int4)max_offset);
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001159
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001160 VEC_TYPE(VEC_SIZE)
1161 values0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1162 VEC_TYPE(VEC_SIZE)
1163 values1 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1164 VEC_TYPE(VEC_SIZE)
1165 values2 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1166 VEC_TYPE(VEC_SIZE)
1167 values3 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001168
1169 // z == 1
1170 // z_coord can be only negative for z = 0 so we do not need to clamp it
1171 // 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 +01001172 z_coord = z * (int)NUM_PLANES_PROCESSED - (int)CONV_PAD_TOP + 1;
1173 offset = y_offset + (int4)(z_coord * src_stride_z);
1174 VEC_TYPE(VEC_SIZE)
1175 values4 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1176 VEC_TYPE(VEC_SIZE)
1177 values5 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1178 VEC_TYPE(VEC_SIZE)
1179 values6 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1180 VEC_TYPE(VEC_SIZE)
1181 values7 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001182
1183 // z == 2
1184 // After z = 1 we can simply add src_stride_z to offset without updating z_coord
1185 // However offset can be out-of-bound so we need to check if it is greater than max_offset
1186 offset += (int4)src_stride_z;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001187 offset = min(offset, (int4)max_offset);
1188 VEC_TYPE(VEC_SIZE)
1189 values8 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1190 VEC_TYPE(VEC_SIZE)
1191 values9 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1192 VEC_TYPE(VEC_SIZE)
1193 values10 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1194 VEC_TYPE(VEC_SIZE)
1195 values11 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001196
1197 // z == 3
1198 // After z = 1 we can simply add src_stride_z to offset without updating z_coord
1199 // However offset can be out-of-bound so we need to check if it is greater than max_offset
1200 offset += (int4)(src_stride_z);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001201 offset = min(offset, (int4)max_offset);
1202 VEC_TYPE(VEC_SIZE)
1203 values12 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1204 VEC_TYPE(VEC_SIZE)
1205 values13 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1206 VEC_TYPE(VEC_SIZE)
1207 values14 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1208 VEC_TYPE(VEC_SIZE)
1209 values15 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001210
1211 MULTIPLY_ADD_ACCUMULATE(values0, w0, acc0, sum0);
1212 MULTIPLY_ADD_ACCUMULATE(values1, w1, acc0, sum0);
1213 MULTIPLY_ADD_ACCUMULATE(values2, w2, acc0, sum0);
1214 MULTIPLY_ADD_ACCUMULATE(values1, w0, acc1, sum1);
1215 MULTIPLY_ADD_ACCUMULATE(values2, w1, acc1, sum1);
1216 MULTIPLY_ADD_ACCUMULATE(values3, w2, acc1, sum1);
1217
1218 MULTIPLY_ADD_ACCUMULATE(values4, w3, acc0, sum0);
1219 MULTIPLY_ADD_ACCUMULATE(values5, w4, acc0, sum0);
1220 MULTIPLY_ADD_ACCUMULATE(values6, w5, acc0, sum0);
1221 MULTIPLY_ADD_ACCUMULATE(values5, w3, acc1, sum1);
1222 MULTIPLY_ADD_ACCUMULATE(values6, w4, acc1, sum1);
1223 MULTIPLY_ADD_ACCUMULATE(values7, w5, acc1, sum1);
1224
1225 MULTIPLY_ADD_ACCUMULATE(values8, w6, acc0, sum0);
1226 MULTIPLY_ADD_ACCUMULATE(values9, w7, acc0, sum0);
1227 MULTIPLY_ADD_ACCUMULATE(values10, w8, acc0, sum0);
1228 MULTIPLY_ADD_ACCUMULATE(values9, w6, acc1, sum1);
1229 MULTIPLY_ADD_ACCUMULATE(values10, w7, acc1, sum1);
1230 MULTIPLY_ADD_ACCUMULATE(values11, w8, acc1, sum1);
1231
1232 MULTIPLY_ADD_ACCUMULATE(values4, w0, acc2, sum2);
1233 MULTIPLY_ADD_ACCUMULATE(values5, w1, acc2, sum2);
1234 MULTIPLY_ADD_ACCUMULATE(values6, w2, acc2, sum2);
1235 MULTIPLY_ADD_ACCUMULATE(values5, w0, acc3, sum3);
1236 MULTIPLY_ADD_ACCUMULATE(values6, w1, acc3, sum3);
1237 MULTIPLY_ADD_ACCUMULATE(values7, w2, acc3, sum3);
1238
1239 MULTIPLY_ADD_ACCUMULATE(values8, w3, acc2, sum2);
1240 MULTIPLY_ADD_ACCUMULATE(values9, w4, acc2, sum2);
1241 MULTIPLY_ADD_ACCUMULATE(values10, w5, acc2, sum2);
1242 MULTIPLY_ADD_ACCUMULATE(values9, w3, acc3, sum3);
1243 MULTIPLY_ADD_ACCUMULATE(values10, w4, acc3, sum3);
1244 MULTIPLY_ADD_ACCUMULATE(values11, w5, acc3, sum3);
1245
1246 MULTIPLY_ADD_ACCUMULATE(values12, w6, acc2, sum2);
1247 MULTIPLY_ADD_ACCUMULATE(values13, w7, acc2, sum2);
1248 MULTIPLY_ADD_ACCUMULATE(values14, w8, acc2, sum2);
1249 MULTIPLY_ADD_ACCUMULATE(values13, w6, acc3, sum3);
1250 MULTIPLY_ADD_ACCUMULATE(values14, w7, acc3, sum3);
1251 MULTIPLY_ADD_ACCUMULATE(values15, w8, acc3, sum3);
1252
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001253#if defined(HAS_BIAS)
1254 Vector biases = CONVERT_TO_VECTOR_STRUCT(biases);
1255
1256 VEC_INT bias_values = VLOAD(VEC_SIZE)(0, (__global int *)biases.ptr);
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001257
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001258 acc0 += bias_values;
1259 acc1 += bias_values;
1260 acc2 += bias_values;
1261 acc3 += bias_values;
1262#endif /* defined(HAS_BIAS) */
1263
1264#if WEIGHTS_OFFSET != 0
1265 acc0 += WEIGHTS_OFFSET * sum0;
1266 acc1 += WEIGHTS_OFFSET * sum1;
1267 acc2 += WEIGHTS_OFFSET * sum2;
1268 acc3 += WEIGHTS_OFFSET * sum3;
1269#endif /* WEIGHTS_OFFSET != 0 */
1270
1271#if INPUT_OFFSET != 0
1272 VEC_INT offs = INPUT_OFFSET * sum_we;
1273
1274 acc0 += offs;
1275 acc1 += offs;
1276 acc2 += offs;
1277 acc3 += offs;
1278#endif /* INPUT_OFFSET != 0 */
1279
1280#if K_OFFSET != 0
1281 acc0 += (VEC_INT)K_OFFSET;
1282 acc1 += (VEC_INT)K_OFFSET;
1283 acc2 += (VEC_INT)K_OFFSET;
1284 acc3 += (VEC_INT)K_OFFSET;
1285#endif /* K_OFFSET != 0 */
1286
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001287#if defined(REAL_MULTIPLIER)
1288
1289 acc0 = CONVERT(round(CONVERT(acc0, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
1290 acc1 = CONVERT(round(CONVERT(acc1, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
1291 acc2 = CONVERT(round(CONVERT(acc2, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
1292 acc3 = CONVERT(round(CONVERT(acc3, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
1293
1294#else // defined(REAL_MULTIPLIER)
1295
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001296#if defined(PER_CHANNEL_QUANTIZATION)
1297 Vector output_multipliers = CONVERT_TO_VECTOR_STRUCT(output_multipliers);
1298 Vector output_shifts = CONVERT_TO_VECTOR_STRUCT(output_shifts);
1299 VEC_INT output_multiplier = VLOAD(VEC_SIZE)(0, (__global int *)output_multipliers.ptr);
1300 VEC_INT output_shift = VLOAD(VEC_SIZE)(0, (__global int *)output_shifts.ptr);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001301
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001302 res0_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc0, output_multiplier, output_shift, VEC_SIZE);
1303 res1_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc1, output_multiplier, output_shift, VEC_SIZE);
1304 res2_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc2, output_multiplier, output_shift, VEC_SIZE);
1305 res3_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc3, output_multiplier, output_shift, VEC_SIZE);
1306 res0_shift_gt0 = asymm_mult_by_quant_multiplier_less_than_one(acc0, output_multiplier, output_shift);
1307 res1_shift_gt0 = asymm_mult_by_quant_multiplier_less_than_one(acc1, output_multiplier, output_shift);
1308 res2_shift_gt0 = asymm_mult_by_quant_multiplier_less_than_one(acc2, output_multiplier, output_shift);
1309 res3_shift_gt0 = asymm_mult_by_quant_multiplier_less_than_one(acc3, output_multiplier, output_shift);
1310 acc0 = select(res0_shift_lt0, res0_shift_gt0, output_shift >= 0);
1311 acc1 = select(res1_shift_lt0, res1_shift_gt0, output_shift >= 0);
1312 acc2 = select(res2_shift_lt0, res2_shift_gt0, output_shift >= 0);
1313 acc3 = select(res3_shift_lt0, res3_shift_gt0, output_shift >= 0);
1314#else // defined(PER_CHANNEL_QUANTIZATION)
1315#if OUTPUT_SHIFT < 0
Michele Di Giorgiob598afa2019-12-20 16:22:20 +00001316 acc0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE);
1317 acc1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE);
1318 acc2 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc2, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE);
1319 acc3 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc3, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001320#else // OUTPUT_SHIFT < 0
Michele Di Giorgiob598afa2019-12-20 16:22:20 +00001321 acc0 = asymm_mult_by_quant_multiplier_less_than_one(acc0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT);
1322 acc1 = asymm_mult_by_quant_multiplier_less_than_one(acc1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT);
1323 acc2 = asymm_mult_by_quant_multiplier_less_than_one(acc2, OUTPUT_MULTIPLIER, OUTPUT_SHIFT);
1324 acc3 = asymm_mult_by_quant_multiplier_less_than_one(acc3, OUTPUT_MULTIPLIER, OUTPUT_SHIFT);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001325#endif // OUTPUT_SHIFT < 0
1326#endif // defined(PER_CHANNEL_QUANTIZATION)
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001327
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001328#endif // defined(REAL_MULTIPLIER)
1329
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001330 acc0 += (VEC_INT)OUTPUT_OFFSET;
1331 acc1 += (VEC_INT)OUTPUT_OFFSET;
1332 acc2 += (VEC_INT)OUTPUT_OFFSET;
1333 acc3 += (VEC_INT)OUTPUT_OFFSET;
1334
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001335 VEC_TYPE(VEC_SIZE)
1336 res0 = CONVERT_SAT(acc0, VEC_TYPE(VEC_SIZE));
1337 VEC_TYPE(VEC_SIZE)
1338 res1 = CONVERT_SAT(acc1, VEC_TYPE(VEC_SIZE));
1339 VEC_TYPE(VEC_SIZE)
1340 res2 = CONVERT_SAT(acc2, VEC_TYPE(VEC_SIZE));
1341 VEC_TYPE(VEC_SIZE)
1342 res3 = CONVERT_SAT(acc3, VEC_TYPE(VEC_SIZE));
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001343
Georgios Pinitas37044642018-10-30 14:53:25 +00001344#if defined(DST_DEPTH)
1345 __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;
1346#else /* defined(DST_DEPTH) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001347 __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 +00001348#endif /* defined(DST_DEPTH) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001349
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001350 VSTORE(VEC_SIZE)
Georgios Pinitas60e98252018-10-22 16:17:20 +01001351 (ACTIVATION_FUNC(res0), 0, dst_addr + 0 * dst_stride_y);
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001352 VSTORE(VEC_SIZE)
Georgios Pinitas60e98252018-10-22 16:17:20 +01001353 (ACTIVATION_FUNC(res1), 0, dst_addr + 1 * dst_stride_y);
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001354
1355#if((DST_DIM_2 % NUM_PLANES_PROCESSED) != 0)
1356 if((z * NUM_PLANES_PROCESSED + 1) < DST_DIM_2)
1357#endif // ((DST_DIM_2 % NUM_PLANES_PROCESSED) != 0)
1358 {
1359 VSTORE(VEC_SIZE)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001360 (ACTIVATION_FUNC(res2), 0, (__global DATA_TYPE *)(dst_addr + 0 * dst_stride_y + 1 * dst_stride_z));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001361 VSTORE(VEC_SIZE)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001362 (ACTIVATION_FUNC(res3), 0, (__global DATA_TYPE *)(dst_addr + 1 * dst_stride_y + 1 * dst_stride_z));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001363 }
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001364}
1365
giuros016d109962019-01-07 17:47:19 +00001366#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) && VEC_SIZE == 4
1367/** 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 +01001368 *
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001369 * @note Per-channel quantization is not supported by this kernel.
giuros016d109962019-01-07 17:47:19 +00001370 * @note This kernel assumes VEC_SIZE is 4.
1371 * @note The weights tensor is expected to be reshaped using @ref CLDepthwiseConvolutionLayerReshapeWeightsKernel.
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001372 * @note The number of elements read per thread must be passed at compile time using -DVEC_SIZE (e.g. -DVEC_SIZE=2)
1373 * @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)
1374 * @note The number of rows processed per thread must be passed at compile time using -DNUM_ROWS_PROCESSED (i.e. -DNUM_ROWS_PROCESSED=2)
1375 * @note The number of planes processed per thread must be passed at compile time using -DNUM_PLANES_PROCESSED (i.e. -DNUM_PLANES_PROCESSED=2)
1376 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_TOP (e.g. -DCONV_PAD_TOP=1)
1377 * @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 +01001378 * @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.
1379 * If not, the quantization will be performed using a fixed point multiplication
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001380 *
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001381 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8
1382 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1383 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1384 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1385 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1386 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1387 * @param[in] src_step_z src_stride_y * number of elements along Z processed per workitem(in bytes)
1388 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes)
1389 * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
1390 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1391 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
1392 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1393 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
1394 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1395 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
1396 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
1397 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
1398 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes)
1399 * @param[in] dst_step_w dst_stride_w * number of elements along W processed per workitem(in bytes)
1400 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1401 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr
1402 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
1403 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
1404 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
1405 * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes)
1406 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
1407 * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32
1408 * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes)
1409 * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1410 * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector
1411 * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32
1412 * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes)
1413 * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1414 * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector
1415 * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32
1416 * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes)
1417 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1418 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases vector
1419 * @param[in] max_offset The maximum allowed offset for the input tensor
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001420 */
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001421__kernel void dwc_3x3_reshaped_quantized8_dot8_stride1_nhwc(
Georgios Pinitas37044642018-10-30 14:53:25 +00001422 TENSOR4D_DECLARATION(src),
1423 TENSOR4D_DECLARATION(dst),
giuros016d109962019-01-07 17:47:19 +00001424 IMAGE_DECLARATION(weights),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001425 VECTOR_DECLARATION(output_multipliers),
1426 VECTOR_DECLARATION(output_shifts),
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001427#if defined(HAS_BIAS)
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001428 VECTOR_DECLARATION(biases),
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001429#endif // defined(HAS_BIAS)
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001430 int max_offset)
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001431{
Giorgio Arenafa23f112018-06-19 11:27:38 +01001432 int x = get_global_id(0);
1433 int y = get_global_id(1);
Georgios Pinitas37044642018-10-30 14:53:25 +00001434#if defined(DST_DEPTH)
1435 int z = get_global_id(2) % (int)DST_DEPTH; // spatial coordinate y
1436 int b = get_global_id(2) / (int)DST_DEPTH; // batch
Pablo Tello47104362019-02-27 13:32:51 +00001437#else // defined(DST_DEPTH)
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001438 int z = get_global_id(2); // spatial coordinate y
Pablo Tello47104362019-02-27 13:32:51 +00001439#endif // defined(DST_DEPTH)
Giorgio Arenafa23f112018-06-19 11:27:38 +01001440
giuros016d109962019-01-07 17:47:19 +00001441 __global uchar *weights_addr = weights_ptr + weights_offset_first_element_in_bytes + x * weights_stride_y;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001442
Georgios Pinitas37044642018-10-30 14:53:25 +00001443#if defined(DST_DEPTH)
1444 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE + b * src_stride_w;
1445#else /* defined(DST_DEPTH) */
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001446 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * VEC_SIZE;
Georgios Pinitas37044642018-10-30 14:53:25 +00001447#endif /* defined(DST_DEPTH) */
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001448
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001449 int z_coord = 0;
1450 int4 offset = 0;
1451 int4 y_coord = ((int4)(y * NUM_ROWS_PROCESSED) + (int4)(0, 1, 2, 3)) - (int)CONV_PAD_LEFT;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001452
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001453 // Only for y = 0 we can have a negative coordinate. If so, we convert it to SRC_DIM_1
1454 y_coord.s0 = min((uint)y_coord.s0, (uint)SRC_DIM_1);
1455 y_coord.s1 = min((uint)y_coord.s1, (uint)SRC_DIM_1);
1456 y_coord.s2 = min((uint)y_coord.s2, (uint)SRC_DIM_1);
1457 y_coord.s3 = min((uint)y_coord.s3, (uint)SRC_DIM_1);
1458
1459 int4 y_offset = convert_int4(y_coord * (int)src_stride_y);
1460
1461 // We compute 4x2x1 [C,W,H] elements
1462 VEC_INT acc0 = 0;
1463 VEC_INT acc1 = 0;
1464 VEC_INT sum0 = 0;
1465 VEC_INT sum1 = 0;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001466
1467 // Load weights
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001468 VEC_TYPE(16)
1469 w0 = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr));
1470 VEC_TYPE(16)
1471 w1 = VLOAD(16)(0, (__global WEIGHTS_TYPE *)(weights_addr + 16));
1472 VEC_TYPE(4)
1473 w2 = VLOAD(4)(0, (__global WEIGHTS_TYPE *)(weights_addr + 32));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001474
1475#if INPUT_OFFSET != 0
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001476 // Initilize the final result with the weights reduction multiplied by INPUT_OFFSET
giuros016d109962019-01-07 17:47:19 +00001477 DOT_PRODUCT_REDUCTION_WEIGHTS(acc0.s0, w0.s01234567, w0.s8);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001478 DOT_PRODUCT_REDUCTION_WEIGHTS(acc0.s1, (VEC_TYPE(8))((w0.s9ABC), (w0.sDEF), w1.s0), w1.s1);
giuros016d109962019-01-07 17:47:19 +00001479 DOT_PRODUCT_REDUCTION_WEIGHTS(acc0.s2, w1.s23456789, w1.sA);
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001480 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 +01001481
1482 // Multiply the weights reduction with INPUT_OFFSET
1483 acc0 = INPUT_OFFSET * acc0;
1484
1485 acc1 = acc0;
1486#endif // INPUT_OFFSET != 0
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001487
1488 // Load input values
1489 // z == 0
1490 // Clamp z_coord as for z = 0, it can be negative
1491 // z_coord is casted to unsigned int in order to use just a min() operation
1492 // A "-1" 32 bit signed variable converted to unsigned gives 4294967295
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001493 z_coord = z - (int)CONV_PAD_TOP;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001494 z_coord = min((uint)z_coord, (uint)SRC_DIM_2);
1495 offset = y_offset + (int4)(z_coord * src_stride_z);
Georgios Pinitas4e0d3812018-08-01 20:16:34 +01001496 offset = min(offset, (int4)max_offset);
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001497
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001498 VEC_TYPE(VEC_SIZE)
1499 values0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1500 VEC_TYPE(VEC_SIZE)
1501 values1 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1502 VEC_TYPE(VEC_SIZE)
1503 values2 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1504 VEC_TYPE(VEC_SIZE)
1505 values3 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001506
1507 // z == 1
1508 // z_coord can be only negative for z = 0 so we do not need to clamp it
1509 // 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 +01001510 z_coord = z - (int)CONV_PAD_TOP + 1;
1511 offset = y_offset + (int4)(z_coord * src_stride_z);
1512 VEC_TYPE(VEC_SIZE)
1513 values4 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1514 VEC_TYPE(VEC_SIZE)
1515 values5 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1516 VEC_TYPE(VEC_SIZE)
1517 values6 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1518 VEC_TYPE(VEC_SIZE)
1519 values7 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001520
1521 // z == 2
1522 // After z = 1 we can simply add src_stride_z to offset without updating z_coord
1523 // However offset can be out-of-bound so we need to check if it is greater than max_offset
1524 offset += (int4)src_stride_z;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001525 offset = min(offset, (int4)max_offset);
1526 VEC_TYPE(VEC_SIZE)
1527 values8 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s0));
1528 VEC_TYPE(VEC_SIZE)
1529 values9 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s1));
1530 VEC_TYPE(VEC_SIZE)
1531 values10 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s2));
1532 VEC_TYPE(VEC_SIZE)
1533 values11 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(src_addr + offset.s3));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001534
giuros016d109962019-01-07 17:47:19 +00001535 DOT_PRODUCT_REDUCTION(sum0.s0, values0.s0, values1.s0, values2.s0, values4.s0, values5.s0, values6.s0, values8.s0, values9.s0, values10.s0);
1536 DOT_PRODUCT_REDUCTION(sum1.s0, values1.s0, values2.s0, values3.s0, values5.s0, values6.s0, values7.s0, values9.s0, values10.s0, values11.s0);
1537 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);
1538 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 +01001539
giuros016d109962019-01-07 17:47:19 +00001540 DOT_PRODUCT_REDUCTION(sum0.s1, values0.s1, values1.s1, values2.s1, values4.s1, values5.s1, values6.s1, values8.s1, values9.s1, values10.s1);
1541 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 +01001542 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);
1543 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 +00001544
1545 DOT_PRODUCT_REDUCTION(sum0.s2, values0.s2, values1.s2, values2.s2, values4.s2, values5.s2, values6.s2, values8.s2, values9.s2, values10.s2);
1546 DOT_PRODUCT_REDUCTION(sum1.s2, values1.s2, values2.s2, values3.s2, values5.s2, values6.s2, values7.s2, values9.s2, values10.s2, values11.s2);
1547 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);
1548 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);
1549
1550 DOT_PRODUCT_REDUCTION(sum0.s3, values0.s3, values1.s3, values2.s3, values4.s3, values5.s3, values6.s3, values8.s3, values9.s3, values10.s3);
1551 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 +01001552 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);
1553 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 +01001554
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001555#if defined(HAS_BIAS)
1556 Vector biases = CONVERT_TO_VECTOR_STRUCT(biases);
1557
1558 VEC_INT bias_values = VLOAD(VEC_SIZE)(0, (__global int *)biases.ptr);
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001559
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001560 acc0 += bias_values;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001561 acc1 += bias_values;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001562
1563#endif // defined(HAS_BIAS)
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001564
1565#if WEIGHTS_OFFSET != 0
1566 acc0 += WEIGHTS_OFFSET * sum0;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001567 acc1 += WEIGHTS_OFFSET * sum1;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001568#endif // WEIGHTS_OFFSET != 0
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001569
1570#if K_OFFSET != 0
1571 acc0 += (VEC_INT)K_OFFSET;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001572 acc1 += (VEC_INT)K_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001573
1574#endif // K_OFFSET != 0
1575
1576#if defined(REAL_MULTIPLIER)
1577
1578 acc0 = CONVERT(round(CONVERT(acc0, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
1579 acc1 = CONVERT(round(CONVERT(acc1, VEC_FLOAT) * (VEC_FLOAT)REAL_MULTIPLIER), VEC_INT);
1580
1581#else // defined(REAL_MULTIPLIER)
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001582
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001583#if OUTPUT_SHIFT < 0
1584 acc0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE);
1585 acc1 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(acc1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, VEC_SIZE);
1586#else // OUTPUT_SHIFT < 0
1587 acc0 = asymm_mult_by_quant_multiplier_less_than_one(acc0, OUTPUT_MULTIPLIER, OUTPUT_SHIFT);
1588 acc1 = asymm_mult_by_quant_multiplier_less_than_one(acc1, OUTPUT_MULTIPLIER, OUTPUT_SHIFT);
1589#endif // OUTPUT_SHIFT < 0
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001590
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001591#endif // defined(REAL_MULTIPLIER)
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001592 acc0 += (VEC_INT)OUTPUT_OFFSET;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001593 acc1 += (VEC_INT)OUTPUT_OFFSET;
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001594
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001595 VEC_TYPE(VEC_SIZE)
1596 res0 = CONVERT_SAT(acc0, VEC_TYPE(VEC_SIZE));
1597 VEC_TYPE(VEC_SIZE)
1598 res1 = CONVERT_SAT(acc1, VEC_TYPE(VEC_SIZE));
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001599
Georgios Pinitas37044642018-10-30 14:53:25 +00001600#if defined(DST_DEPTH)
1601 __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;
1602#else /* defined(DST_DEPTH) */
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001603 __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 +00001604#endif /* defined(DST_DEPTH) */
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001605
1606 VSTORE(VEC_SIZE)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001607 (ACTIVATION_FUNC(res0), 0, (__global DATA_TYPE *)(dst_addr + 0 * dst_stride_y));
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001608 VSTORE(VEC_SIZE)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001609 (ACTIVATION_FUNC(res1), 0, (__global DATA_TYPE *)(dst_addr + 1 * dst_stride_y));
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001610}
giuros016d109962019-01-07 17:47:19 +00001611#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) && VEC_SIZE==4
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001612
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001613#endif // defined(NUM_ROWS_PROCESSED) && defined(NUM_PLANES_PROCESSED)
Giorgio Arenadfca60b2018-01-31 10:30:59 +00001614
Giorgio Arenaeff8d952018-07-02 15:29:57 +01001615#endif // defined(VEC_SIZE) && defined(SRC_DIM_1) && defined(SRC_DIM_2) && defined(CONV_PAD_TOP) && defined(CONV_PAD_LEFT)
1616
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001617#endif // defined(WEIGHTS_PROMOTED_TYPE)
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001618
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001619#endif // defined(WEIGHTS_OFFSET) && defined(INPUT_OFFSET) && defined(K_OFFSET) && ((defined(OUTPUT_OFFSET) && defined(OUTPUT_MULTIPLIER) && defined(OUTPUT_SHIFT)) || defined(REAL_MULTIPLIER))
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001620
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001621#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) && defined(OUTPUT_SHIFT) && defined(OUTPUT_MULTIPLIER)
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001622/** This function computes the depthwise convolution for NHWC data layout. This kernel assumes that the weights tensor is NOT reshaped
1623 *
1624 * @note The number of elements processed must be passed at compile time using -DN0 (e.g. -DN0=2)
1625 * @note The depth multiplier must be passed at compile time using -DDEPTH_MULTIPLIER (e.g. -DDEPTH_MULTIPLIER=1)
1626 * @note The first dimension of the input tensor must be passed at compile time using -DSRC_DIM1 (e.g. -DSRC_DIM1=112)
1627 * @note The second dimension of the input tensor must be passed at compile time using -DSRC_DIM2 (e.g. -DSRC_DIM2=80)
1628 * @note The kernel width must be passed at compile time using -DKERNEL_WIDTH (e.g. -DKERNEL_WIDTH=5)
1629 * @note The kernel height must be passed at compile time using -DKERNEL_HEIGHT (e.g. -DKERNEL_HEIGHT=5)
1630 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_TOP (e.g. -DCONV_PAD_TOP=1)
1631 * @note The convolution pad top must be passed at compile time using -DCONV_PAD_LEFT (e.g. -DCONV_PAD_LEFT=1)
1632 * @note The convolution stride along the width must be passed at compile time using -DCONV_STRIDE_X (e.g. -DCONV_STRIDE_Y=X)
1633 * @note The convolution stride along the height must be passed at compile time using -DCONV_STRIDE_Y (e.g. -DCONV_STRIDE_Y=1)
1634 * @note It is possible to select the activation function to apply using -DACTIVATION_TYPE e.g. -DACTIVATION_TYPE=relu
1635 * @note A, B variables required by some activation functions are set using -DA_VAL= and -DB_VAL= respectively
1636 *
Michele Di Giorgio4cd4cde2020-01-06 14:07:44 +00001637 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001638 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1639 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1640 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1641 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1642 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1643 * @param[in] src_step_z src_stride_y * number of elements along Z processed per workitem(in bytes)
1644 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes)
1645 * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
1646 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1647 * @param[in] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
1648 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1649 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
1650 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1651 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
1652 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
1653 * @param[in] dst_step_z dst_stride_z * number of elements along Y processed per workitem(in bytes)
1654 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes)
1655 * @param[in] dst_step_w dst_stride_w * number of elements along W processed per workitem(in bytes)
1656 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
Michele Di Giorgio4cd4cde2020-01-06 14:07:44 +00001657 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/QSYMM8_PER_CHANNEL
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001658 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
1659 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
1660 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
1661 * @param[in] weights_step_y weights_stride_y * number of elements along Y processed per workitem(in bytes)
1662 * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes)
1663 * @param[in] weights_step_z weights_stride_z * number of elements along Y processed per workitem(in bytes)
1664 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
1665 * @param[in] output_multipliers_ptr Pointer to the output multipliers vector. Supported data types: S32
1666 * @param[in] output_multipliers_stride_x Stride of the output multipliers vector in X dimension (in bytes)
1667 * @param[in] output_multipliers_step_x output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1668 * @param[in] output_multipliers_offset_first_element_in_bytes The offset of the first element in the output multipliers vector
1669 * @param[in] output_shifts_ptr Pointer to the output shifts vector. Supported data types: S32
1670 * @param[in] output_shifts_stride_x Stride of the output shifts vector in X dimension (in bytes)
1671 * @param[in] output_shifts_step_x output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1672 * @param[in] output_shifts_offset_first_element_in_bytes The offset of the first element in the output shifts vector
1673 * @param[in] biases_ptr (Optional) Pointer to the biases vector. Supported data types: S32
1674 * @param[in] biases_stride_x (Optional) Stride of the biases vector in X dimension (in bytes)
1675 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1676 * @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 +01001677 */
1678__kernel void dwc_MxN_native_quantized8_nhwc(
1679 TENSOR4D_DECLARATION(src),
1680 TENSOR4D_DECLARATION(dst),
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001681 TENSOR3D_DECLARATION(weights),
1682 VECTOR_DECLARATION(output_multipliers),
1683 VECTOR_DECLARATION(output_shifts)
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001684#if defined(HAS_BIAS)
Michele Di Giorgio1dce3102019-10-22 10:29:03 +01001685 ,
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001686 VECTOR_DECLARATION(biases)
1687#endif // defined(HAS_BIAS)
1688)
1689{
1690 int x = get_global_id(0); // channels
1691 int y = get_global_id(1); // spatial coordinate x
1692#if defined(DST_DEPTH)
1693 int z = get_global_id(2) % (int)DST_DEPTH; // spatial coordinate y
1694 int b = get_global_id(2) / (int)DST_DEPTH; // batch
1695#else // defined(DST_DEPTH)
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001696 int z = get_global_id(2); // spatial coordinate y
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001697#endif // defined(DST_DEPTH)
1698
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001699 __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 +01001700
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001701 __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 +01001702
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001703 __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 +01001704
1705#if defined(HAS_BIAS)
1706 __global uchar *b_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int) * (int)DEPTH_MULTIPLIER * (int)N0;
1707#endif // defined(HAS_BIAS)
1708
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001709#if defined(PER_CHANNEL_QUANTIZATION)
1710 __global uchar *out_mul_addr = output_multipliers_ptr + output_multipliers_offset_first_element_in_bytes + x * sizeof(int) * (int)DEPTH_MULTIPLIER * (int)N0;
1711 __global uchar *out_shift_addr = output_shifts_ptr + output_shifts_offset_first_element_in_bytes + x * sizeof(int) * (int)DEPTH_MULTIPLIER * (int)N0;
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001712#endif // defined(PER_CHANNEL_QUANTIZATION)
1713
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001714#if defined(DST_DEPTH)
1715 s_addr += b * src_stride_w;
1716 d_addr += b * dst_stride_w;
1717#endif // defined(DST_DEPTH)
1718
1719#if DEPTH_MULTIPLIER > 1
1720 for(int d = 0; d < (int)DEPTH_MULTIPLIER; ++d)
1721 {
1722#endif // DEPTH_MULTIPLIER > 1
1723 // Each work-item computes N0x1x1 elements
Sang-Hoon Park15396ff2019-11-07 09:37:29 +00001724 VEC_INT res = 0;
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001725
1726 int x_coord = y * CONV_STRIDE_X - (int)CONV_PAD_LEFT;
1727 int y_coord = z * CONV_STRIDE_Y - (int)CONV_PAD_TOP;
1728
1729 for(int yk = 0; yk < KERNEL_HEIGHT; ++yk)
1730 {
1731 if(y_coord >= 0 && y_coord < SRC_DIM2)
1732 {
1733 int x_coord_tmp = x_coord;
1734
1735 for(int xk = 0; xk < KERNEL_WIDTH; ++xk)
1736 {
1737 if(x_coord_tmp >= 0 && x_coord_tmp < SRC_DIM1)
1738 {
1739 int s_offset = x_coord_tmp * (int)src_stride_y + y_coord * (int)src_stride_z;
1740 int w_offset = xk * weights_stride_y + yk * weights_stride_z;
1741
1742 // Load input and weights values
Sang-Hoon Park15396ff2019-11-07 09:37:29 +00001743 VEC_INT i = CONVERT(VLOAD(N0)(0, (__global DATA_TYPE *)(s_addr + s_offset)), VEC_INT);
1744 VEC_INT w = CONVERT(VLOAD(N0)(0, (__global WEIGHTS_TYPE *)(w_addr + w_offset)), VEC_INT);
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001745
Sang-Hoon Park15396ff2019-11-07 09:37:29 +00001746 res += (i + (VEC_INT)INPUT_OFFSET) * (w + (VEC_INT)WEIGHTS_OFFSET);
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001747 }
1748 x_coord_tmp += DILATION_X;
1749 }
1750 }
1751 y_coord += DILATION_Y;
1752 }
1753
1754#if defined(HAS_BIAS)
Sang-Hoon Park15396ff2019-11-07 09:37:29 +00001755 VEC_INT bias = VLOAD(N0)(0, (__global int *)(b_addr));
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001756 res += bias;
1757#endif // defined(HAS_BIAS)
1758
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001759#if defined(PER_CHANNEL_QUANTIZATION)
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001760 VEC_INT output_multiplier = VLOAD(N0)(0, (__global int *)(out_mul_addr));
1761 VEC_INT output_shift = VLOAD(N0)(0, (__global int *)(out_shift_addr));
1762
1763 VEC_INT res_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(res, output_multiplier, output_shift, N0);
1764 VEC_INT res_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(res, output_multiplier, output_shift, N0);
1765 res = select(res_shift_lt0, res_shift_gt0, (VEC_INT)(output_shift) >= 0);
1766#else // defined(PER_CHANNEL_QUANTIZATION)
1767#if OUTPUT_SHIFT < 0
1768 res = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(res, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, N0);
1769#else // OUTPUT_SHIFT < 0
1770 res = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(res, OUTPUT_MULTIPLIER, OUTPUT_SHIFT, N0);
1771#endif // OUTPUT_OFFSET < 0
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001772#endif // defined(PER_CHANNEL_QUANTIZATION)
1773
Sang-Hoon Park15396ff2019-11-07 09:37:29 +00001774 res += (VEC_INT)OUTPUT_OFFSET;
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001775
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001776 VEC_TYPE(VEC_SIZE)
1777 res1 = CONVERT_SAT(res, VEC_TYPE(VEC_SIZE));
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001778
1779 VSTORE(N0)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001780 (ACTIVATION_FUNC(res1), 0, (__global DATA_TYPE *)(d_addr));
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001781
1782#if DEPTH_MULTIPLIER > 1
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001783 w_addr += sizeof(WEIGHTS_TYPE);
1784 d_addr += sizeof(DATA_TYPE);
1785#if defined(PER_CHANNEL_QUANTIZATION)
1786 out_mul_addr += sizeof(int);
1787 out_shift_addr += sizeof(int);
1788#endif // defined(PER_CHANNEL_QUANTIZATION)
Michele Di Giorgioa046e162019-10-08 09:36:26 +01001789#if defined(HAS_BIAS)
1790 b_addr += sizeof(int);
1791#endif // defined(HAS_BIAS)
1792 }
1793#endif // DEPTH_MULTIPLIER > 1
1794}
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001795#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) && defined(OUTPUT_SHIFT) && defined(OUTPUT_MULTIPLIER)
Michele Di Giorgiodf4cf572019-10-09 15:32:39 +01001796#endif // defined(DATA_TYPE) && defined(WEIGHTS_TYPE)