blob: 048505abe49fd0e4e8d654dd38c32be53044a936 [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Gian Marco05288a22017-11-21 10:57:50 +00003 *
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 */
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010024#include "gemm_helpers.h"
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000025#include "helpers_asymm.h"
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +000026#include "repeat.h"
Gian Marco05288a22017-11-21 10:57:50 +000027
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000028#if defined(DATA_TYPE) && defined(ACC_DATA_TYPE)
29
Georgios Pinitasdaa38552018-08-28 17:43:18 +010030#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
31#if defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice4b908652018-10-18 10:21:02 +010032#define ARM_DOT(x, y, val) val = arm_dot_acc((x), (y), (val));
Georgios Pinitasdaa38552018-08-28 17:43:18 +010033#else // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice4b908652018-10-18 10:21:02 +010034#define ARM_DOT(x, y, val) val += arm_dot((x), (y));
Georgios Pinitasdaa38552018-08-28 17:43:18 +010035#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
36#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Giorgio Arenac50da382018-07-26 15:50:09 +010037
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010038#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
39
40/** Specialized macros to perform the dot product instruction between two vectors of size N [1,16]. These macros use the dot8 instruction */
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000041#define ARM_DOT1(a, b, c) \
42 ({ \
43 ARM_DOT((VEC_DATA_TYPE(DATA_TYPE, 4))(a, (VEC_DATA_TYPE(DATA_TYPE, 3))0), (VEC_DATA_TYPE(DATA_TYPE, 4))(b, (VEC_DATA_TYPE(DATA_TYPE, 3))0), c); \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010044 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000045#define ARM_DOT2(a, b, c) \
46 ({ \
47 ARM_DOT((VEC_DATA_TYPE(DATA_TYPE, 4))(a, (VEC_DATA_TYPE(DATA_TYPE, 2))0), (VEC_DATA_TYPE(DATA_TYPE, 4))(b, (VEC_DATA_TYPE(DATA_TYPE, 2))0), c); \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010048 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000049#define ARM_DOT3(a, b, c) \
50 ({ \
51 ARM_DOT((VEC_DATA_TYPE(DATA_TYPE, 4))(a, (DATA_TYPE)0), (VEC_DATA_TYPE(DATA_TYPE, 4))(b, (DATA_TYPE)0), c); \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010052 })
53#define ARM_DOT4(a, b, c) \
54 ({ \
55 ARM_DOT(a, b, c); \
56 })
57#define ARM_DOT8(a, b, c) \
58 ({ \
59 ARM_DOT4((a.lo), (b.lo), c); \
60 ARM_DOT4((a.hi), (b.hi), c); \
61 })
62#define ARM_DOT16(a, b, c) \
63 ({ \
64 ARM_DOT8((a.lo), (b.lo), c); \
65 ARM_DOT8((a.hi), (b.hi), c); \
66 })
67
68#else // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
69
70/** Specialized macros to perform the dot product instruction between two vectors of size K0 [1,16] without using the dot8 instruction. */
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000071#define ARM_DOT1(a, b, c) \
72 ({ \
73 c += (ACC_DATA_TYPE)a * b; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010074 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000075#define ARM_DOT2(a, b, c) \
76 ({ \
77 c += (ACC_DATA_TYPE)a.s0 * b.s0; \
78 c += (ACC_DATA_TYPE)a.s1 * b.s1; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010079 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000080#define ARM_DOT3(a, b, c) \
81 ({ \
82 ARM_DOT2(a, b, c); \
83 c += (ACC_DATA_TYPE)a.s2 * b.s2; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010084 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000085#define ARM_DOT4(a, b, c) \
86 ({ \
87 ARM_DOT3(a, b, c); \
88 c += (ACC_DATA_TYPE)a.s3 * b.s3; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010089 })
90#define ARM_DOT8(a, b, c) \
91 ({ \
92 ARM_DOT4((a.lo), (b.lo), c); \
93 ARM_DOT4((a.hi), (b.hi), c); \
94 })
95#define ARM_DOT16(a, b, c) \
96 ({ \
97 ARM_DOT8((a.lo), (b.lo), c); \
98 ARM_DOT8((a.hi), (b.hi), c); \
99 })
100#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
101
102/** Specialized macros to perform a broadcast dot product operation between one vector "a" and N0 vectors "b" of size K0 [1,16] */
Gian Marco Iodice061eefd2020-04-23 13:40:00 +0100103#define ARM_DOT_K0X1(k0, a, b, c) \
104 ({ \
105 ARM_DOT_K0(k0, (a), (b##0), (c)); \
106 })
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100107#define ARM_DOT_K0X2(k0, a, b, c) \
108 ({ \
109 ARM_DOT_K0(k0, (a), (b##0), (c.s0)); \
110 ARM_DOT_K0(k0, (a), (b##1), (c.s1)); \
111 })
112#define ARM_DOT_K0X3(k0, a, b, c) \
113 ({ \
114 ARM_DOT_K0X2(k0, a, b, c); \
115 ARM_DOT_K0(k0, (a), (b##2), (c.s2)); \
116 })
117#define ARM_DOT_K0X4(k0, a, b, c) \
118 ({ \
119 ARM_DOT_K0X3(k0, a, b, c); \
120 ARM_DOT_K0(k0, (a), (b##3), (c.s3)); \
121 })
122#define ARM_DOT_K0X8(k0, a, b, c) \
123 ({ \
124 ARM_DOT_K0X4(k0, a, b, c); \
125 ARM_DOT_K0(k0, (a), (b##4), (c.s4)); \
126 ARM_DOT_K0(k0, (a), (b##5), (c.s5)); \
127 ARM_DOT_K0(k0, (a), (b##6), (c.s6)); \
128 ARM_DOT_K0(k0, (a), (b##7), (c.s7)); \
129 })
130#define ARM_DOT_K0X16(k0, a, b, c) \
131 ({ \
132 ARM_DOT_K0X8(k0, a, b, c); \
133 ARM_DOT_K0(k0, (a), (b##8), (c.s8)); \
134 ARM_DOT_K0(k0, (a), (b##9), (c.s9)); \
135 ARM_DOT_K0(k0, (a), (b##A), (c.sA)); \
136 ARM_DOT_K0(k0, (a), (b##B), (c.sB)); \
137 ARM_DOT_K0(k0, (a), (b##C), (c.sC)); \
138 ARM_DOT_K0(k0, (a), (b##D), (c.sD)); \
139 ARM_DOT_K0(k0, (a), (b##E), (c.sE)); \
140 ARM_DOT_K0(k0, (a), (b##F), (c.sF)); \
141 })
142
SiCong Li738893e2020-05-01 12:55:16 +0100143/** Specialized macros to perform a partial matrix multiplication with dimensions M0,N0,K0 */
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100144#define ARM_MM_K0XN0X1(n0, k0, a, b, c) \
145 ({ \
146 ARM_DOT_K0XN0(n0, k0, (a##0), b, (c##0)); \
147 })
148#define ARM_MM_K0XN0X2(n0, k0, a, b, c) \
149 ({ \
150 ARM_MM_K0XN0X1(n0, k0, a, b, c); \
151 ARM_DOT_K0XN0(n0, k0, (a##1), b, (c##1)); \
152 })
153#define ARM_MM_K0XN0X3(n0, k0, a, b, c) \
154 ({ \
155 ARM_MM_K0XN0X2(n0, k0, a, b, c); \
156 ARM_DOT_K0XN0(n0, k0, (a##2), b, (c##2)); \
157 })
158#define ARM_MM_K0XN0X4(n0, k0, a, b, c) \
159 ({ \
160 ARM_MM_K0XN0X3(n0, k0, a, b, c); \
161 ARM_DOT_K0XN0(n0, k0, (a##3), b, (c##3)); \
162 })
163#define ARM_MM_K0XN0X5(n0, k0, a, b, c) \
164 ({ \
165 ARM_MM_K0XN0X4(n0, k0, a, b, c); \
166 ARM_DOT_K0XN0(n0, k0, (a##4), b, (c##4)); \
167 })
168#define ARM_MM_K0XN0X6(n0, k0, a, b, c) \
169 ({ \
170 ARM_MM_K0XN0X5(n0, k0, a, b, c); \
171 ARM_DOT_K0XN0(n0, k0, (a##5), b, (c##5)); \
172 })
173#define ARM_MM_K0XN0X7(n0, k0, a, b, c) \
174 ({ \
175 ARM_MM_K0XN0X6(n0, k0, a, b, c); \
176 ARM_DOT_K0XN0(n0, k0, (a##6), b, (c##6)); \
177 })
178#define ARM_MM_K0XN0X8(n0, k0, a, b, c) \
179 ({ \
180 ARM_MM_K0XN0X7(n0, k0, a, b, c); \
181 ARM_DOT_K0XN0(n0, k0, (a##7), b, (c##7)); \
182 })
183
184#define ARM_DOT_K0(k0, a, b, c) \
185 ({ \
186 CONCAT(ARM_DOT, k0) \
187 ((a), (b), (c)); \
188 })
189
190#define ARM_DOT_K0XN0(n0, k0, a, b, c) \
191 ({ \
192 CONCAT(ARM_DOT_K0X, n0) \
193 (k0, (a), b, (c)); \
194 })
195
196#define ARM_MM_K0XN0XM0(m0, n0, k0, a, b, c) \
197 ({ \
198 CONCAT(ARM_MM_K0XN0X, m0) \
199 (n0, k0, a, b, c); \
200 })
201
SiCong Li738893e2020-05-01 12:55:16 +0100202/** Specialized macros to perform a broadcast dot product operation between one vector "a" and N0 vectors "b" of size K0 [1,16] */
203#define ARM_MUL_N0X1(VECTOR_ACC_TYPE, a, b, c) \
204 ({ \
205 c += CONVERT(b##0, VECTOR_ACC_TYPE) * a; \
206 })
207#define ARM_MUL_N0X2(VECTOR_ACC_TYPE, a, b, c) \
208 ({ \
209 c += CONVERT(b##0, VECTOR_ACC_TYPE) * a.s##0; \
210 c += CONVERT(b##1, VECTOR_ACC_TYPE) * a.s##1; \
211 })
212#define ARM_MUL_N0X3(VECTOR_ACC_TYPE, a, b, c) \
213 ({ \
214 ARM_MUL_N0X2(VECTOR_ACC_TYPE, a, b, c); \
215 c += CONVERT(b##2, VECTOR_ACC_TYPE) * a.s##2; \
216 })
217#define ARM_MUL_N0X4(VECTOR_ACC_TYPE, a, b, c) \
218 ({ \
219 ARM_MUL_N0X3(VECTOR_ACC_TYPE, a, b, c); \
220 c += CONVERT(b##3, VECTOR_ACC_TYPE) * a.s##3; \
221 })
222#define ARM_MUL_N0X8(VECTOR_ACC_TYPE, a, b, c) \
223 ({ \
224 ARM_MUL_N0X4(VECTOR_ACC_TYPE, a, b, c); \
225 c += CONVERT(b##4, VECTOR_ACC_TYPE) * a.s##4; \
226 c += CONVERT(b##5, VECTOR_ACC_TYPE) * a.s##5; \
227 c += CONVERT(b##6, VECTOR_ACC_TYPE) * a.s##6; \
228 c += CONVERT(b##7, VECTOR_ACC_TYPE) * a.s##7; \
229 })
230#define ARM_MUL_N0X16(VECTOR_ACC_TYPE, a, b, c) \
231 ({ \
232 ARM_MUL_N0X8(VECTOR_ACC_TYPE, a, b, c); \
233 c += CONVERT(b##8, VECTOR_ACC_TYPE) * a.s##8; \
234 c += CONVERT(b##9, VECTOR_ACC_TYPE) * a.s##9; \
235 c += CONVERT(b##A, VECTOR_ACC_TYPE) * a.s##A; \
236 c += CONVERT(b##B, VECTOR_ACC_TYPE) * a.s##B; \
237 c += CONVERT(b##C, VECTOR_ACC_TYPE) * a.s##C; \
238 c += CONVERT(b##D, VECTOR_ACC_TYPE) * a.s##D; \
239 c += CONVERT(b##E, VECTOR_ACC_TYPE) * a.s##E; \
240 c += CONVERT(b##F, VECTOR_ACC_TYPE) * a.s##F; \
241 })
242/** Specialized macros to perform a a partial matrix multiplication with dimensions M0,N0,K0 */
243#define ARM_MM_NATIVE_N0XK0X1(VECTOR_ACC_TYPE, k0, a, b, c) \
244 ({ \
245 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##0), b, (c##0)); \
246 })
247#define ARM_MM_NATIVE_N0XK0X2(VECTOR_ACC_TYPE, k0, a, b, c) \
248 ({ \
249 ARM_MM_NATIVE_N0XK0X1(VECTOR_ACC_TYPE, k0, a, b, c); \
250 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##1), b, (c##1)); \
251 })
252#define ARM_MM_NATIVE_N0XK0X3(VECTOR_ACC_TYPE, k0, a, b, c) \
253 ({ \
254 ARM_MM_NATIVE_N0XK0X2(VECTOR_ACC_TYPE, k0, a, b, c); \
255 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##2), b, (c##2)); \
256 })
257#define ARM_MM_NATIVE_N0XK0X4(VECTOR_ACC_TYPE, k0, a, b, c) \
258 ({ \
259 ARM_MM_NATIVE_N0XK0X3(VECTOR_ACC_TYPE, k0, a, b, c); \
260 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##3), b, (c##3)); \
261 })
262#define ARM_MM_NATIVE_N0XK0X5(VECTOR_ACC_TYPE, k0, a, b, c) \
263 ({ \
264 ARM_MM_NATIVE_N0XK0X4(VECTOR_ACC_TYPE, k0, a, b, c); \
265 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##4), b, (c##4)); \
266 })
267#define ARM_MM_NATIVE_N0XK0X6(VECTOR_ACC_TYPE, k0, a, b, c) \
268 ({ \
269 ARM_MM_NATIVE_N0XK0X5(VECTOR_ACC_TYPE, k0, a, b, c); \
270 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##5), b, (c##5)); \
271 })
272#define ARM_MM_NATIVE_N0XK0X7(VECTOR_ACC_TYPE, k0, a, b, c) \
273 ({ \
274 ARM_MM_NATIVE_N0XK0X6(VECTOR_ACC_TYPE, k0, a, b, c); \
275 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##6), b, (c##6)); \
276 })
277#define ARM_MM_NATIVE_N0XK0X8(VECTOR_ACC_TYPE, k0, a, b, c) \
278 ({ \
279 ARM_MM_NATIVE_N0XK0X7(VECTOR_ACC_TYPE, k0, a, b, c); \
280 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##7), b, (c##7)); \
281 })
282#define ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, a, b, c) \
283 ({ \
284 CONCAT(ARM_MUL_N0X, k0) \
285 (VECTOR_ACC_TYPE, (a), b, (c)); \
286 })
287#define ARM_MM_NATIVE_N0XK0XM0(VECTOR_ACC_TYPE, m0, k0, a, b, c) \
288 ({ \
289 CONCAT(ARM_MM_NATIVE_N0XK0X, m0) \
290 (VECTOR_ACC_TYPE, k0, a, b, c); \
291 })
292
Manuel Bottini8cf753f2020-10-21 12:34:38 +0100293#if defined(M0) && defined(N0) && defined(K0) && defined(V0) && defined(H0) && defined(M) && defined(N) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0)
Sheri Zhang28287af2020-02-25 14:13:54 +0000294/** This OpenCL kernel computes the matrix multiplication between 2 matrices with QASYMM/QASYMM_SIGNED data type.
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000295 * The LHS matrix must be reshaped with @ref CLGEMMReshapeLHSMatrixKernel and the M0xK0 must be NOT transposed
296 * The RHS matrix must be reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the K0xN0 must be transposed
297 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000298 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
299 * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000300 * @note If the first two dimensions of NDRange have been dispatched with "dummy_work_items" support, the option -DDUMMY_WORK_ITEMS must be passed at compile time.
301 * @note The GEMM's dimensions M and N must be passed at compile time using -DM and -DN (i.e. -DM=52 and -DN=90).
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000302 * @note The block's dimensions used for reshaping the LHS matrix and the RHS matrix (M0, N0 and K0) must be passed at compile time using -DM0, -DN0 and -DK0 (i.e. -DM0=4, -DN0=8, -DK0=4).
303 * @note The number of M0xK0 vertical blocks stored on the same output row of the reshaped LHS matrix must be passed at compile time using -DV0 (i.e. -DV0=2)
304 * @note The number of K0xN0 horizontal blocks stored on the same output row of the reshaped RHS matrix must be passed at compile time using -DH0 (i.e. -DH0=2)
305 * @note If the M0xK0 blocks in the reshaped LHS matrix have been interleaved, the option -DLHS_INTERLEAVE must passed at compile time.
306 * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time.
307 * @note Only the following configurations of M0, N0 and K0 are currently supported:
308 * - M0 = 2, 3, 4, 5, 6, 7, 8
309 * - N0 = 2, 3, 4, 8, 16
310 * - K0 = 2, 3, 4, 8, 16
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000311 * - V0 >= 1
312 * - H0 >= 1
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000313 *
314 * @note In case the output has to be reinterpreted as a 3D tensor (i.e. output of convolution layer), the following information must be passed at compile time:
315 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
316 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
317 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
318 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix NOT reshaped
319 *
Sheri Zhang28287af2020-02-25 14:13:54 +0000320 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM_SIGNED
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000321 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
322 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
323 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
324 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
325 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
326 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
327 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
328 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
329 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
330 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
331 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
Sheri Zhang28287af2020-02-25 14:13:54 +0000332 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000333 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
334 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
335 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
336 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
337 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
338 * @param[in] k Number of columns in LHS matrix and rows in RHS matrix not reshaped.
339 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
340 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
341 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
342 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
343 */
344__kernel void gemmlowp_mm_reshaped_lhs_nt_rhs_t(IMAGE_DECLARATION(lhs),
345 IMAGE_DECLARATION(rhs),
346 IMAGE_DECLARATION(dst),
347 uint k,
348 uint lhs_stride_z,
349 uint rhs_stride_z,
350 uint dst_stride_z
351#if defined(REINTERPRET_OUTPUT_AS_3D)
352 ,
353 uint dst_cross_plane_pad
354#endif // REINTERPRET_OUTPUT_AS_3D
355 )
356{
357 // Block size
358#define LHS_BLOCK_SIZE ((K0) * (M0))
359
360#if defined(LHS_INTERLEAVE)
361#define LHS_OFFSET_X (K0)
362#define LHS_STEP_X ((K0) * (V0))
363#define LHS_STEP_LOOP (1)
364#else // defined(INTERLEAVE)
365#define LHS_OFFSET_X (LHS_BLOCK_SIZE)
366#define LHS_STEP_X (K0)
367#define LHS_STEP_LOOP (V0)
368#endif // defined(INTERLEAVE)
369
370 // Block size
371#define RHS_BLOCK_SIZE ((K0) * (N0))
372
373 // RHS offset and step X
374#if defined(RHS_INTERLEAVE)
375#define RHS_OFFSET_X (K0)
376#define RHS_STEP_X ((K0) * (H0))
377#define RHS_STEP_LOOP (1)
378#else // defined(RHS_INTERLEAVE)
379#define RHS_OFFSET_X (RHS_BLOCK_SIZE)
380#define RHS_STEP_X (K0)
381#define RHS_STEP_LOOP (H0)
382#endif // defined(RHS_INTERLEAVE)
383
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100384 uint x = get_global_id(0);
385 uint y = get_global_id(1);
386 uint z = get_global_id(2);
387
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000388#if defined(DUMMY_WORK_ITEMS)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100389 if((x * N0 >= N) || (y * M0 >= M))
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000390 {
391 return;
392 }
393#endif // defined(DUMMY_WORK_ITEMS)
394
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000395 // Compute LHS matrix address
Sheri Zhang28287af2020-02-25 14:13:54 +0000396 __global DATA_TYPE *lhs_addr = lhs_ptr + lhs_offset_first_element_in_bytes + (y % V0) * (uint)LHS_OFFSET_X + (y / V0) * (uint)lhs_stride_y + (z * lhs_stride_z);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000397
398 // Compute RHS matrix address
Sheri Zhang28287af2020-02-25 14:13:54 +0000399 __global DATA_TYPE *rhs_addr = rhs_ptr + rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000400
401#if defined(MATRIX_B_DEPTH)
402 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100403 rhs_addr += (z % MATRIX_B_DEPTH) * rhs_stride_z;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000404#else // defined(MATRIX_B_DEPTH)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100405 rhs_addr += z * rhs_stride_z;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000406#endif // defined(MATRIX_B_DEPTH)
407
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100408 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
409 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
410
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000411 // Initialize the accumulators
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000412 REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(ACC_DATA_TYPE, N0), c, 0); //VEC_DATA_TYPE(ACC_DATA_TYPE, N0) c0=0,c1=0,c2=0,... c(M0-1)=0;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000413
414 for(int i = 0; i < k; i += K0)
415 {
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000416 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000417 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_addr, 0, LHS_STEP_X, zlhs);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000418
419 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000420 LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_addr, 0, RHS_STEP_X, zrhs);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000421
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100422 // Partial matrix multiplication M0,N0,K0
423 ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000424
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100425 // Update address
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000426 lhs_addr += (M0 * LHS_STEP_X * LHS_STEP_LOOP);
427 rhs_addr += (N0 * RHS_STEP_X * RHS_STEP_LOOP);
428 }
429
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100430 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0 * sizeof(int)) + (y * (uint)M0 * dst_stride_y);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000431
432 REPEAT_VAR_INIT_TO_CONST(8, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
433
434#if defined(REINTERPRET_OUTPUT_AS_3D)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100435 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice9ae06d42020-10-22 16:37:12 +0100436 CALCULATE_Z_OFFSET(M0, uint, zout, y * M0, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000437
438 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
439 // multiply dst_stride_z by DEPTH_GEMM3D
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100440 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000441
442#else // defined(REINTERPRET_OUTPUT_AS_3D)
443
444 // Add offset for batched GEMM
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100445 dst_addr += z * dst_stride_z;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000446
447#endif // defined(REINTERPRET_OUTPUT_AS_3D)
448
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100449 // Convert and store output block
Manuel Bottini8cf753f2020-10-21 12:34:38 +0100450 const bool cond_y = ((get_global_id(1) + 1) * M0 >= M);
451 const bool cond_x = ((get_global_id(0) + 1) * N0 >= N);
452
453 // Store output block
454 REPEAT_VAR_INIT_CONVERT_SAT(M0, VEC_DATA_TYPE(int, N0), c, c_lp);
455 STORE_BLOCK_BOUNDARY_AWARE(M0, N0, int, c_lp, dst_addr, dst_stride_y, zout, PARTIAL_STORE_M0, PARTIAL_STORE_N0, cond_y, cond_x);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000456
457#undef LHS_BLOCK_SIZE
458#undef LHS_OFFSET_X
459#undef LHS_STEP_X
460#undef RHS_BLOCK_SIZE
461#undef RHS_OFFSET_X
462#undef RHS_STEP_X
463}
Manuel Bottini8cf753f2020-10-21 12:34:38 +0100464#endif // defined(M0) && defined(N0) && defined(K0) && defined(V0) && defined(H0) && defined(M) && defined(N) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0)
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000465
Manuel Bottini488f5082020-10-29 13:51:23 +0000466#if defined(M0) && defined(N0) && defined(K0) && defined(H0) && defined(K) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000467
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000468/** This OpenCL kernel computes the matrix multiplication between 2 matrices.
469 * The LHS matrix is NOT reshaped
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100470 * The RHS matrix is reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the block K0xN0 is transposed
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000471 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000472 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
473 * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000474 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
475 * @note The block's dimensions used for reshaping the RHS matrix (N0 and K0) must be passed at compile time using -DN0 and -DK0 (i.e. -DN0=8, -DK0=4).
476 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
477 * @note The number of K0xN0 horizontal blocks stored on the same output row of the reshaped RHS matrix must be passed at compile time using -DH0 (i.e. -DH0=2)
478 * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time.
479 * @note Only the following configurations of M0, N0 and K0 are currently supported:
480 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
481 * - N0 = 2, 3, 4, 8, 16
482 * - K0 = 2, 3, 4, 8, 16
483 * - H0 >= 1
484 *
485 * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time:
486 * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D
487 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
488 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
489 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
490 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix
491 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000492 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000493 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
494 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
495 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
496 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
497 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
498 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
499 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
500 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
501 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
502 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
503 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000504 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000505 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
506 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
507 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
508 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
509 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
510 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
511 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
512 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
513 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
514 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
515 */
516__kernel void gemmlowp_mm_reshaped_only_rhs_t(IMAGE_DECLARATION(lhs),
517 IMAGE_DECLARATION(rhs),
518 IMAGE_DECLARATION(dst),
519 uint lhs_stride_z,
520 uint rhs_stride_z,
521 uint dst_stride_z
522#if defined(REINTERPRET_INPUT_AS_3D)
523 ,
524 uint lhs_cross_plane_pad
525#endif // REINTERPRET_INPUT_AS_3D
526#if defined(REINTERPRET_OUTPUT_AS_3D)
527 ,
528 uint dst_cross_plane_pad
529#endif // REINTERPRET_OUTPUT_AS_3D
530 )
531{
532 // Block size
533#define RHS_BLOCK_SIZE ((K0) * (N0))
534
535 // RHS offset and step X
536#if defined(RHS_INTERLEAVE)
537#define RHS_OFFSET_X (K0)
538#define RHS_STEP_X ((K0) * (H0))
539#define RHS_STEP_LOOP (1)
540#else // defined(RHS_INTERLEAVE)
541#define RHS_OFFSET_X (RHS_BLOCK_SIZE)
542#define RHS_STEP_X (K0)
543#define RHS_STEP_LOOP (H0)
544#endif // defined(RHS_INTERLEAVE)
545
546 uint x = get_global_id(0);
547 uint y = get_global_id(1);
548 uint z = get_global_id(2);
549
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100550#if defined(DUMMY_WORK_ITEMS)
551 if((x * N0 >= N) || (y * M0 >= M))
552 {
553 return;
554 }
555#endif // defined(DUMMY_WORK_ITEMS)
556
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000557 // Compute LHS matrix address
Manuel Bottini488f5082020-10-29 13:51:23 +0000558 uint lhs_offset = lhs_offset_first_element_in_bytes + COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0) * (uint)lhs_stride_y;
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000559
560 // Compute RHS matrix address
561 uint rhs_offset = rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y;
562
563#if defined(MATRIX_B_DEPTH)
564 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
565 rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z;
566#else // defined(MATRIX_B_DEPTH)
567 rhs_offset += z * rhs_stride_z;
568#endif // defined(MATRIX_B_DEPTH)
569
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100570 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
571 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000572
573#if defined(REINTERPRET_INPUT_AS_3D)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100574 // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Manuel Bottini488f5082020-10-29 13:51:23 +0000575 CALCULATE_Z_OFFSET(M0, uint, zlhs, COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0), HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000576
577 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
578 // multiply lhs_stride_z by DEPTH_GEMM3D
579 lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D;
580
581#else // defined(REINTERPRET_INPUT_AS_3D)
582
583 // Add offset for batched GEMM
584 lhs_offset += z * lhs_stride_z;
585
586#endif // defined(REINTERPRET_INPUT_AS_3D)
587
588 // Initialize the accumulators
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000589 REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(ACC_DATA_TYPE, N0), c, 0); //VEC_DATA_TYPE(ACC_DATA_TYPE, N0) c0=0,c1=0,c2=0,... c(N0-1)=0;
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000590
Manuel Bottini488f5082020-10-29 13:51:23 +0000591 int i = 0;
592 for(; i <= (K - K0); i += K0)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000593 {
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000594 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000595 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000596
597 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000598 LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_ptr, rhs_offset, RHS_STEP_X, zrhs);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000599
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100600 // Partial matrix multiplication M0,N0,K0
601 ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000602
603 lhs_offset += K0;
604 rhs_offset += N0 * RHS_STEP_X * RHS_STEP_LOOP;
605 }
Manuel Bottini488f5082020-10-29 13:51:23 +0000606 // Left-over accumulations
607 for(; i < K; ++i)
608 {
609 // Load values from LHS matrix
610 LOAD_BLOCK(M0, 1, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000611
Manuel Bottini488f5082020-10-29 13:51:23 +0000612 // Load values from RHS reshaped matrix
613 LOAD_BLOCK(N0, 1, DATA_TYPE, b, rhs_ptr, rhs_offset, RHS_STEP_X, zlhs);
614
615 ARM_MM_K0XN0XM0(M0, N0, 1, a, b, c);
616 lhs_offset += 1;
617 rhs_offset += 1;
618 }
619 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0 * sizeof(int)) + (COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0) * dst_stride_y);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000620
621 REPEAT_VAR_INIT_TO_CONST(8, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
622
623#if defined(REINTERPRET_OUTPUT_AS_3D)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000624 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Manuel Bottini488f5082020-10-29 13:51:23 +0000625 CALCULATE_Z_OFFSET(M0, uint, zout, COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0), HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000626
627 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
628 // multiply dst_stride_z by DEPTH_GEMM3D
629 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
630
631#else // defined(REINTERPRET_OUTPUT_AS_3D)
632
633 // Add offset for batched GEMM
634 dst_addr += z * dst_stride_z;
635
636#endif // defined(REINTERPRET_OUTPUT_AS_3D)
637
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100638 // Convert and store output block
Manuel Bottini488f5082020-10-29 13:51:23 +0000639 const bool cond_y = y == 0;
640 const bool cond_x = ((x + 1) * N0 >= N);
641
642 // Store output block
643 REPEAT_VAR_INIT_CONVERT_SAT(M0, VEC_DATA_TYPE(int, N0), c, c_lp);
644 STORE_BLOCK_BOUNDARY_AWARE(M0, N0, int, c_lp, dst_addr, dst_stride_y, zout, PARTIAL_STORE_M0, PARTIAL_STORE_N0, cond_y, cond_x);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000645
646#undef RHS_BLOCK_SIZE
647#undef RHS_OFFSET_X
648#undef RHS_STEP_X
649}
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000650
651#if defined(RESULT_OFFSET) && defined(RESULT_SHIFT) && defined(RESULT_MULTIPLIER)
652/** This OpenCL kernel computes the matrix multiplication between 2 matrices with fused output stage using fixed-point arithmetic.
653 * The LHS matrix is NOT reshaped
654 * The RHS matrix is reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the block K0xN0 is transposed
655 *
656 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
657 * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
658 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
659 * @note The block's dimensions used for reshaping the RHS matrix (N0 and K0) must be passed at compile time using -DN0 and -DK0 (i.e. -DN0=8, -DK0=4).
660 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
661 * @note The number of K0xN0 horizontal blocks stored on the same output row of the reshaped RHS matrix must be passed at compile time using -DH0 (i.e. -DH0=2)
662 * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time.
663 * @note Only the following configurations of M0, N0 and K0 are currently supported:
664 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
665 * - N0 = 2, 3, 4, 8, 16
666 * - K0 = 2, 3, 4, 8, 16
667 * - H0 >= 1
668 *
669 * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time:
670 * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D
671 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
672 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
673 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
674 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix
675 *
676 * @note The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_OFFSET, -RESULT_MULTIPLIER and -DRESULT_SHIFT
677 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
678 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
679 * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND.
680 * These values can be used to implement "rectified linear unit" activation functions
681 * @note In case of per-channel quantization of matrix B, -DPER_CHANNEL_QUANTIZATION must be passed at compile time.
682 *
683 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM8_SIGNED
684 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
685 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
686 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
687 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
688 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
689 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
690 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
691 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
692 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
693 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
694 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
695 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as @p lhs_ptr
696 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
697 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
698 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
699 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
700 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
701 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
702 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
703 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
704 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
705 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
706 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: S32
707 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
708 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
709 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
710 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
711 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
712 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: S32
713 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
714 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
715 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
716 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
717 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
718 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: S32
719 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
720 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
721 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
722 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
723 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
724 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
725 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
726 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
727 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
728 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
729 * @param[in] result_shifts_offset_first_element_in_bytes (Optional) The offset of the first element in the output shifts vector
730 */
731__kernel void gemmlowp_mm_reshaped_only_rhs_t_fused_output_stage_fixedpoint(IMAGE_DECLARATION(lhs),
732 IMAGE_DECLARATION(rhs),
733 IMAGE_DECLARATION(dst),
734 uint lhs_stride_z,
735 uint rhs_stride_z,
736 uint dst_stride_z
737#if defined(REINTERPRET_INPUT_AS_3D)
738 ,
739 uint lhs_cross_plane_pad
740#endif // REINTERPRET_INPUT_AS_3D
741#if defined(REINTERPRET_OUTPUT_AS_3D)
742 ,
743 uint dst_cross_plane_pad
744#endif // REINTERPRET_OUTPUT_AS_3D
745#if defined(A_OFFSET)
746 ,
747 IMAGE_DECLARATION(sum_col)
748#endif // defined(A_OFFSET)
749#if defined(B_OFFSET)
750 ,
751 IMAGE_DECLARATION(sum_row)
752#endif // defined(B_OFFSET)
753#if defined(ADD_BIAS)
754 ,
755 VECTOR_DECLARATION(biases)
756#endif // defined(ADD_BIAS)
757#if defined(PER_CHANNEL_QUANTIZATION)
758 ,
759 VECTOR_DECLARATION(result_multipliers),
760 VECTOR_DECLARATION(result_shifts)
761#endif // defined(PER_CHANNEL_QUANTIZATION)
762 )
763{
764 // Block size
765#define RHS_BLOCK_SIZE ((K0) * (N0))
766
767 // RHS offset and step X
768#if defined(RHS_INTERLEAVE)
769#define RHS_OFFSET_X (K0)
770#define RHS_STEP_X ((K0) * (H0))
771#define RHS_STEP_LOOP (1)
772#else // defined(RHS_INTERLEAVE)
773#define RHS_OFFSET_X (RHS_BLOCK_SIZE)
774#define RHS_STEP_X (K0)
775#define RHS_STEP_LOOP (H0)
776#endif // defined(RHS_INTERLEAVE)
777
778 uint x = get_global_id(0);
779 uint y = get_global_id(1);
780 uint z = get_global_id(2);
781
782#if defined(DUMMY_WORK_ITEMS)
783 if((x * N0 >= N) || (y * M0 >= M))
784 {
785 return;
786 }
787#endif // defined(DUMMY_WORK_ITEMS)
788
789 // Compute LHS matrix address
Manuel Bottini488f5082020-10-29 13:51:23 +0000790 uint lhs_offset = lhs_offset_first_element_in_bytes + COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0) * (uint)lhs_stride_y;
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000791
792 // Compute RHS matrix address
793 uint rhs_offset = rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y;
794
795#if defined(MATRIX_B_DEPTH)
796 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
797 rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z;
798#else // defined(MATRIX_B_DEPTH)
799 rhs_offset += z * rhs_stride_z;
800#endif // defined(MATRIX_B_DEPTH)
801
802 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
803 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
804
805#if defined(REINTERPRET_INPUT_AS_3D)
806 // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Manuel Bottini488f5082020-10-29 13:51:23 +0000807 CALCULATE_Z_OFFSET(M0, uint, zlhs, COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0), HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000808
809 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
810 // multiply lhs_stride_z by DEPTH_GEMM3D
811 lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D;
812
813#else // defined(REINTERPRET_INPUT_AS_3D)
814
815 // Add offset for batched GEMM
816 lhs_offset += z * lhs_stride_z;
817
818#endif // defined(REINTERPRET_INPUT_AS_3D)
819
820 // Initialize the accumulators
821 REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(ACC_DATA_TYPE, N0), c, 0); //VEC_DATA_TYPE(ACC_DATA_TYPE, N0) c0=0,c1=0,c2=0,... c(N0-1)=0;
822
Manuel Bottini488f5082020-10-29 13:51:23 +0000823 int i = 0;
824 for(; i <= (K - K0); i += K0)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000825 {
826 // Load values from LHS matrix
827 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
828
829 // Load values from RHS matrix
830 LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_ptr, rhs_offset, RHS_STEP_X, zrhs);
831
832 // Partial matrix multiplication M0,N0,K0
833 ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c);
834
835 lhs_offset += K0;
836 rhs_offset += N0 * RHS_STEP_X * RHS_STEP_LOOP;
837 }
Manuel Bottini488f5082020-10-29 13:51:23 +0000838 // Left-over accumulations
839 for(; i < K; ++i)
840 {
841 // Load values from LHS matrix
842 LOAD_BLOCK(M0, 1, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000843
Manuel Bottini488f5082020-10-29 13:51:23 +0000844 // Load values from RHS reshaped matrix
845 LOAD_BLOCK(N0, 1, DATA_TYPE, b, rhs_ptr, rhs_offset, RHS_STEP_X, zlhs);
846
847 ARM_MM_K0XN0XM0(M0, N0, 1, a, b, c);
848 lhs_offset += 1;
849 rhs_offset += 1;
850 }
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000851 // Result of MM is of type DATA_TYPE
Manuel Bottini488f5082020-10-29 13:51:23 +0000852 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0 * sizeof(DATA_TYPE)) + (COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0) * dst_stride_y);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000853
854 REPEAT_VAR_INIT_TO_CONST(8, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
855
856#if defined(REINTERPRET_OUTPUT_AS_3D)
857 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Manuel Bottini488f5082020-10-29 13:51:23 +0000858 CALCULATE_Z_OFFSET(M0, uint, zout, COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0), HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000859
860 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
861 // multiply dst_stride_z by DEPTH_GEMM3D
862 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
863
864#else // defined(REINTERPRET_OUTPUT_AS_3D)
865
866 // Add offset for batched GEMM
867 dst_addr += z * dst_stride_z;
868
869#endif // defined(REINTERPRET_OUTPUT_AS_3D)
870
871 // Convert result of matrix multiplication to S32
872 REPEAT_VAR_INIT_CONVERT_SAT(M0, VEC_DATA_TYPE(int, N0), c, c_int);
873
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000874 // Offset contribution: c += (A_OFFSET * sum_col) + (B_OFFSET * sum_row) + K_OFFSET;
875 REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(int, N0), offset_s32_, K_OFFSET);
876
877#if defined(A_OFFSET)
878 // Compute the offset contribution due to A_OFFSET
879 __global uchar *sum_col_addr = sum_col_ptr + sum_col_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
880
881#if defined(SUM_COL_HAS_BATCHES)
882 sum_col_addr += z * sum_col_stride_y;
883#endif // defined(SUM_COL_HAS_BATCHES)
884 VEC_DATA_TYPE(int, N0)
885 a_offset_s32 = VLOAD(N0)(0, (__global int *)sum_col_addr);
886 a_offset_s32 *= (VEC_DATA_TYPE(int, N0))A_OFFSET;
887
888 REPEAT_ADD_VECTOR_TO_VAR(M0, offset_s32_, a_offset_s32);
889#endif // defined(A_OFFSET)
890
891#if defined(B_OFFSET)
892 // Compute the offset contribution due to B_OFFSET
Gian Marco Iodice27423f02020-08-12 14:12:28 +0100893 // Note: The sum_row tensor is generated through CLGEMMLowpMatrixAReductionKernel which
894 // does not introduce paddings. For this reason is safe to access the tensor in this manner
895 // without considering that the coordinate "y" could come from an input 3D tensor
Manuel Bottini488f5082020-10-29 13:51:23 +0000896 __global uchar *sum_row_addr = sum_row_ptr + sum_row_offset_first_element_in_bytes + (COMPUTE_M0_START_ROW(y, (uint)M0, PARTIAL_STORE_M0)) * sizeof(int) + z * sum_row_stride_y;
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000897
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000898 LOAD_SCALAR_AS_VECTOR(M0, N0, int, b_offset_s32_, sum_row_addr, 0, sum_row_stride_x);
899
900 REPEAT_MLA_VAR_WITH_CONST_VEC(M0, offset_s32_, b_offset_s32_, (VEC_DATA_TYPE(int, N0))B_OFFSET);
901#endif // defined(B_OFFSET)
902
903#if defined(ADD_BIAS)
904 // Add bias
905 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
906
907 VEC_DATA_TYPE(int, N0)
908 bias_values = VLOAD(N0)(0, (__global int *)bias_addr);
909 REPEAT_ADD_VECTOR_TO_VAR(M0, offset_s32_, bias_values);
910#endif // defined(ADD_BIAS)
911
912 REPEAT_ADD_TWO_VARS(M0, c_int, offset_s32_);
913
914 // Multiply by result_mult_int and shift
915#if defined(PER_CHANNEL_QUANTIZATION)
916 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
917 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
918
919 VEC_DATA_TYPE(int, N0)
920 res_mul = VLOAD(N0)(0, (__global int *)result_multipliers_addr);
921 VEC_DATA_TYPE(int, N0)
922 res_shift = VLOAD(N0)(0, (__global int *)result_shifts_addr);
923
924 REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_PER_CHANNEL(M0, N0, c_int, res_mul, res_shift);
925#else // defined(PER_CHANNEL_QUANTIZATION)
926
927#if RESULT_SHIFT < 0
928 REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(M0, N0, c_int, RESULT_MULTIPLIER, RESULT_SHIFT);
929#else // RESULT_SHIFT >= 0
930 REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(M0, N0, c_int, RESULT_MULTIPLIER, RESULT_SHIFT);
931#endif // RESULT_SHIFT < 0
932
933#endif // defined(PER_CHANNEL_QUANTIZATION)
934
935 // Add the offset terms to GEMM's result
936 REPEAT_ADD_CONST_TO_VAR(M0, VEC_DATA_TYPE(int, N0), c_int, RESULT_OFFSET);
937
938#if defined(MIN_BOUND)
939 REPEAT_MAX_CONST_VAR(M0, VEC_DATA_TYPE(int, N0), c_int, MIN_BOUND);
940#endif // defined(MIN_BOUND)
941#if defined(MAX_BOUND)
942 REPEAT_MIN_CONST_VAR(M0, VEC_DATA_TYPE(int, N0), c_int, MAX_BOUND);
943#endif // defined(MAX_BOUND)
944
Manuel Bottini488f5082020-10-29 13:51:23 +0000945 // Convert and store output block
946 const bool cond_y = y == 0;
947 const bool cond_x = ((x + 1) * N0 >= N);
948
949 // Store output block
950 REPEAT_VAR_INIT_CONVERT_SAT(M0, VEC_DATA_TYPE(DATA_TYPE, N0), c_int, c_lp);
951 STORE_BLOCK_BOUNDARY_AWARE(M0, N0, DATA_TYPE, c_lp, dst_addr, dst_stride_y, zout, PARTIAL_STORE_M0, PARTIAL_STORE_N0, cond_y, cond_x);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000952
953#undef RHS_BLOCK_SIZE
954#undef RHS_OFFSET_X
955#undef RHS_STEP_X
956}
957#endif // defined(RESULT_OFFSET) && defined(RESULT_SHIFT) && defined(RESULT_MULTIPLIER)
Manuel Bottini488f5082020-10-29 13:51:23 +0000958#endif // defined(M0) && defined(N0) && defined(K0) && defined(H0) && defined(K) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000959
SiCong Lied5fb392020-10-20 18:07:27 +0100960#if defined(M0) && defined(N0) && defined(K0) && defined(K) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0)
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100961
962/** This OpenCL kernel computes the matrix multiplication between 2 matrices.
963 * The LHS matrix is NOT reshaped
964 * The RHS matrix is NOT reshaped
965 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000966 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
967 * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100968 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
969 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
970 * @note The number of N0 columns to process must be passed at compile time using -DN0 (i.e. -DN0=2)
971 * @note The number of K0 partial accumulations must be passed at compile time using -DK0 (i.e., -DK0=2)
972 * @note Only the following configurations of M0, N0 and K0 are currently supported:
973 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
974 * - N0 = 2, 3, 4, 8, 16
975 * - K0 = 2, 3, 4, 8, 16
976 *
977 * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time:
978 * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D
979 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
980 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
981 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
982 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix
983 *
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000984 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100985 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
986 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
987 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
988 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
989 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
990 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
991 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
992 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
993 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
994 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
995 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000996 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100997 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
998 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
999 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
1000 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
1001 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
1002 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
1003 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
1004 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
1005 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
1006 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
1007 */
1008__kernel void gemmlowp_mm_native(IMAGE_DECLARATION(lhs),
1009 IMAGE_DECLARATION(rhs),
1010 IMAGE_DECLARATION(dst),
1011 uint lhs_stride_z,
1012 uint rhs_stride_z,
1013 uint dst_stride_z
1014#if defined(REINTERPRET_INPUT_AS_3D)
1015 ,
1016 uint lhs_cross_plane_pad
1017#endif // REINTERPRET_INPUT_AS_3D
1018#if defined(REINTERPRET_OUTPUT_AS_3D)
1019 ,
1020 uint dst_cross_plane_pad
1021#endif // REINTERPRET_OUTPUT_AS_3D
1022 )
1023{
1024 uint x = get_global_id(0);
1025 uint y = get_global_id(1);
1026 uint z = get_global_id(2);
1027
1028#if defined(DUMMY_WORK_ITEMS)
1029 if((x * N0 >= N) || (y * M0 >= M))
1030 {
1031 return;
1032 }
1033#endif // defined(DUMMY_WORK_ITEMS)
1034
1035 // Compute LHS matrix address
morgolockcf343e32020-10-12 14:00:43 +01001036 uint lhs_offset = lhs_offset_first_element_in_bytes + COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0) * (uint)lhs_stride_y;
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001037
1038 // Compute RHS matrix address
morgolockcf343e32020-10-12 14:00:43 +01001039 uint rhs_offset = rhs_offset_first_element_in_bytes + x * N0 * sizeof(DATA_TYPE);
1040
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001041#if defined(MATRIX_B_DEPTH)
1042 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
1043 rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z;
1044#else // defined(MATRIX_B_DEPTH)
1045 rhs_offset += z * rhs_stride_z;
1046#endif // defined(MATRIX_B_DEPTH)
1047
1048 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0);
1049 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
1050
1051#if defined(REINTERPRET_INPUT_AS_3D)
1052 // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice9ae06d42020-10-22 16:37:12 +01001053 CALCULATE_Z_OFFSET(M0, uint, zlhs, COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0), HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001054
1055 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
1056 // multiply lhs_stride_z by DEPTH_GEMM3D
1057 lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D;
1058
1059#else // defined(REINTERPRET_INPUT_AS_3D)
1060
1061 // Add offset for batched GEMM
1062 lhs_offset += z * lhs_stride_z;
1063
1064#endif // defined(REINTERPRET_INPUT_AS_3D)
1065
1066 // Initialize the accumulators
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001067 REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(ACC_DATA_TYPE, N0), c, 0); //VEC_DATA_TYPE(ACC_DATA_TYPE, N0) c0=0,c1=0,c2=0,... c(M0-1)=0;
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001068
1069 int i = 0;
1070
1071 for(; i <= (K - K0); i += K0)
1072 {
1073 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001074 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001075
1076 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001077 LOAD_BLOCK(K0, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001078
SiCong Li738893e2020-05-01 12:55:16 +01001079 // Partial matrix multiplication M0,N0,K0
1080#if(GPU_ARCH == GPU_ARCH_MIDGARD)
1081 ARM_MM_NATIVE_N0XK0XM0(VEC_DATA_TYPE(ACC_DATA_TYPE, N0), M0, K0, a, b, c);
1082#else // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001083 // Transpose the values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001084 TRANSPOSE_K0XN0(K0, N0, b_t, b, DATA_TYPE);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001085
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001086 ARM_MM_K0XN0XM0(M0, N0, K0, a, b_t, c);
SiCong Li738893e2020-05-01 12:55:16 +01001087#endif // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001088
1089 // Update the offset
1090 lhs_offset += K0;
1091 rhs_offset += K0 * rhs_stride_y;
1092 }
1093
1094 // Left-over for loop
1095 for(; i < K; ++i)
1096 {
1097 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001098 LOAD_BLOCK(M0, 1, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001099
1100 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001101 LOAD_BLOCK(1, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001102
SiCong Li738893e2020-05-01 12:55:16 +01001103 // Partial matrix multiplication M0,N0,1
1104#if(GPU_ARCH == GPU_ARCH_MIDGARD)
1105 ARM_MM_NATIVE_N0XK0XM0(VEC_DATA_TYPE(ACC_DATA_TYPE, N0), M0, 1, a, b, c);
1106#else // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001107 // Transpose the values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001108 TRANSPOSE_K0XN0(1, N0, b_t, b, DATA_TYPE);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001109
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001110 ARM_MM_K0XN0XM0(M0, N0, 1, a, b_t, c);
SiCong Li738893e2020-05-01 12:55:16 +01001111#endif // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001112
1113 // Update the offset
1114 lhs_offset += 1;
1115 rhs_offset += rhs_stride_y;
1116 }
1117
morgolockcf343e32020-10-12 14:00:43 +01001118 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0 * sizeof(int)) + (COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0) * dst_stride_y);
1119
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001120 REPEAT_VAR_INIT_TO_CONST(M0, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
1121
1122#if defined(REINTERPRET_OUTPUT_AS_3D)
1123 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice9ae06d42020-10-22 16:37:12 +01001124 CALCULATE_Z_OFFSET(M0, uint, zout, COMPUTE_M0_START_ROW(y, M0, PARTIAL_STORE_M0), HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001125
1126 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
1127 // multiply dst_stride_z by DEPTH_GEMM3D
1128 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
1129
1130#else // defined(REINTERPRET_OUTPUT_AS_3D)
1131
1132 // Add offset for batched GEMM
1133 dst_addr += z * dst_stride_z;
1134
1135#endif // defined(REINTERPRET_OUTPUT_AS_3D)
morgolockcf343e32020-10-12 14:00:43 +01001136 const bool cond_y = y == 0;
1137 const bool cond_x = ((x + 1) * N0 >= N);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001138
Michele Di Giorgio27d92fd2020-10-27 12:44:17 +00001139 // Convert and store output block
1140 REPEAT_VAR_INIT_CONVERT(M0, VEC_DATA_TYPE(int, N0), c, res); // resN = CONVERT(cN, VEC_DATA_TYPE(int, N0));
1141 STORE_BLOCK_BOUNDARY_AWARE(M0, N0, int, res, dst_addr, dst_stride_y, zout, PARTIAL_STORE_M0, PARTIAL_STORE_N0, cond_y, cond_x);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001142}
SiCong Lied5fb392020-10-20 18:07:27 +01001143#endif // defined(M0) && defined(N0) && defined(K0) && defined(K) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0)
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001144
Gian Marco05288a22017-11-21 10:57:50 +00001145#if defined(COLS_A)
1146/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each row of Matrix A.
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001147 * It is also possible to multiply each reduced row by a scalar value, if SCALAR is passed at compile time.
Gian Marco05288a22017-11-21 10:57:50 +00001148 *
1149 * @note This stage is needed to handle the offset of matrix product
1150 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1151 *
1152 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
Manuel Bottini959c26d2019-12-02 16:22:35 +00001153 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001154 * @note The data type for the accumulation must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001155 * @note In case of scaling the scalar value must be passed at compile time using -DSCALAR (e.g. -DSCALAR=3)
Gian Marco05288a22017-11-21 10:57:50 +00001156 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +01001157 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8/QASYMM8_SIGNED/QSYMM8
Gian Marco05288a22017-11-21 10:57:50 +00001158 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1159 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1160 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1161 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1162 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1163 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1164 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1165 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1166 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1167 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1168 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1169 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1170 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1171 */
1172__kernel void gemmlowp_matrix_a_reduction(TENSOR3D_DECLARATION(src),
1173 IMAGE_DECLARATION(dst))
1174{
1175 // Compute source and destination addresses
1176 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1177 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1178
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001179 VEC_DATA_TYPE(ACC_DATA_TYPE, 4)
1180 sum_row_32 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))0;
1181 ACC_DATA_TYPE sum_row = 0;
Gian Marco05288a22017-11-21 10:57:50 +00001182
Manuel Bottini959c26d2019-12-02 16:22:35 +00001183 __global const DATA_TYPE *matrix_a = (__global const DATA_TYPE *)(src.ptr + get_global_id(0) * src_stride_y + get_global_id(1) * src_stride_z);
Gian Marco05288a22017-11-21 10:57:50 +00001184
1185 int i = 0;
1186
1187 // This for loop performs 16 accumulations
1188 for(; i <= ((int)COLS_A - 16); i += 16)
1189 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001190 const VEC_DATA_TYPE(DATA_TYPE, 16) a0 = vload16(0, matrix_a + i);
Gian Marco05288a22017-11-21 10:57:50 +00001191
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001192 sum_row_32 += CONVERT(a0.s0123, VEC_DATA_TYPE(ACC_DATA_TYPE, 4)) + CONVERT(a0.s4567, VEC_DATA_TYPE(ACC_DATA_TYPE, 4)) + CONVERT(a0.s89AB, VEC_DATA_TYPE(ACC_DATA_TYPE, 4)) + CONVERT(a0.sCDEF,
1193 VEC_DATA_TYPE(ACC_DATA_TYPE, 4));
Gian Marco05288a22017-11-21 10:57:50 +00001194 }
1195
1196 // This for loop performs the leftover accumulations
1197 for(; i < COLS_A; ++i)
1198 {
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001199 sum_row += (ACC_DATA_TYPE)matrix_a[i];
Gian Marco05288a22017-11-21 10:57:50 +00001200 }
1201
Manuel Bottini959c26d2019-12-02 16:22:35 +00001202 sum_row += sum_row_32.s0 + sum_row_32.s1 + sum_row_32.s2 + sum_row_32.s3;
Gian Marco05288a22017-11-21 10:57:50 +00001203
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001204#if defined(SCALAR)
1205 sum_row *= (int)SCALAR;
1206#endif // defined(SCALAR)
Gian Marco05288a22017-11-21 10:57:50 +00001207 *((__global int *)dst.ptr) = (int)sum_row;
1208}
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001209
1210#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001211/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each row of Matrix A using the arm dot product instruction.
1212 * It is also possible to multiply each reduced row by a scalar value, if SCALAR is passed at compile time.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001213 *
1214 * @note This stage is needed to handle the offset of matrix product
1215 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1216 *
1217 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
Manuel Bottini959c26d2019-12-02 16:22:35 +00001218 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001219 * @note The data type for the accumulation must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001220 * @note In case of scaling the scalar value must be passed at compile time using -DSCALAR (e.g. -DSCALAR=3)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001221 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +01001222 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8/QASYMM8_SIGNED/QSYMM8
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001223 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1224 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1225 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1226 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1227 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1228 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1229 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1230 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1231 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1232 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1233 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1234 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1235 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1236 */
1237__kernel void gemmlowp_matrix_a_reduction_dot8(TENSOR3D_DECLARATION(src),
1238 IMAGE_DECLARATION(dst))
1239{
1240 // Compute source and destination addresses
1241 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1242 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1243
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001244 ACC_DATA_TYPE sum_row = 0;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001245
Manuel Bottini959c26d2019-12-02 16:22:35 +00001246 __global const DATA_TYPE *matrix_a = (__global const DATA_TYPE *)(src.ptr + get_global_id(0) * src_stride_y + get_global_id(1) * src_stride_z);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001247
1248 int i = 0;
1249
1250 // This for loop performs 16 accumulations
1251 for(; i <= ((int)COLS_A - 32); i += 32)
1252 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001253 VEC_DATA_TYPE(DATA_TYPE, 16)
1254 a0 = vload16(0, matrix_a + i);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001255
Manuel Bottini959c26d2019-12-02 16:22:35 +00001256 sum_row += arm_dot(a0.s0123, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1257 sum_row += arm_dot(a0.s4567, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1258 sum_row += arm_dot(a0.s89AB, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1259 sum_row += arm_dot(a0.sCDEF, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001260
Manuel Bottini959c26d2019-12-02 16:22:35 +00001261 a0 = vload16(1, matrix_a + i);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001262
Manuel Bottini959c26d2019-12-02 16:22:35 +00001263 sum_row += arm_dot(a0.s0123, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1264 sum_row += arm_dot(a0.s4567, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1265 sum_row += arm_dot(a0.s89AB, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1266 sum_row += arm_dot(a0.sCDEF, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001267 }
1268
1269 // This for loop performs the leftover accumulations
1270 for(; i < COLS_A; ++i)
1271 {
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001272 sum_row += (ACC_DATA_TYPE)matrix_a[i];
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001273 }
1274
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001275#if defined(SCALAR)
1276 sum_row *= (int)SCALAR;
1277#endif // defined(SCALAR)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001278 *((__global int *)dst.ptr) = (int)sum_row;
1279}
1280#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Gian Marco05288a22017-11-21 10:57:50 +00001281#endif // defined(COLS_A)
1282
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001283#if defined(COLS_B) && defined(ROWS_B) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
Gian Marco05288a22017-11-21 10:57:50 +00001284/** OpenCL kernel used to compute the row-vectors of sums of all the entries in each column of Matrix B.
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001285 * It is also possible to multiply each reduced column by a scalar value, if SCALAR is passed at compile time.
Gian Marco05288a22017-11-21 10:57:50 +00001286 *
1287 * @note This stage is needed to handle the offset of matrix product
1288 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1289 *
1290 * @attention The number of matrix B columns and rows needs to be passed at compile time using -DCOLS_B and -DROWS_B
Manuel Bottini959c26d2019-12-02 16:22:35 +00001291 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001292 * @note The data type for the accumulation must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001293 * @note In case of scaling the scalar value must be passed at compile time using -DSCALAR (i.e. -DSCALAR=3)
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001294 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1295 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco05288a22017-11-21 10:57:50 +00001296 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +01001297 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8/QASYMM8_SIGNED/QSYMM8/QSYMM8_PER_CHANNEL
Gian Marco05288a22017-11-21 10:57:50 +00001298 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1299 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1300 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1301 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1302 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1303 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1304 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1305 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1306 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1307 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1308 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1309 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1310 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1311 */
1312__kernel void gemmlowp_matrix_b_reduction(TENSOR3D_DECLARATION(src),
1313 IMAGE_DECLARATION(dst))
1314{
1315 // Compute source and destination addresses
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001316 const uint x_offs = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
1317 const uint y = get_global_id(1);
Gian Marco05288a22017-11-21 10:57:50 +00001318
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001319 __global const DATA_TYPE *matrix_b = (__global const DATA_TYPE *)(src_ptr + src_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + y * src_step_y + y * src_stride_z);
1320 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x_offs * sizeof(int) + y * dst_stride_y;
Gian Marco05288a22017-11-21 10:57:50 +00001321
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001322 VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001323 sum_col_32 = (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))0;
Gian Marco05288a22017-11-21 10:57:50 +00001324
1325 int i = 0;
1326 // This for loop performs 4 accumulations
1327 for(; i <= ((int)ROWS_B - 4); i += 4)
1328 {
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001329 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1330 b0 = VLOAD(VEC_SIZE)(0, matrix_b + 0 * src_stride_y);
1331 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1332 b1 = VLOAD(VEC_SIZE)(0, matrix_b + 1 * src_stride_y);
1333 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1334 b2 = VLOAD(VEC_SIZE)(0, matrix_b + 2 * src_stride_y);
1335 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1336 b3 = VLOAD(VEC_SIZE)(0, matrix_b + 3 * src_stride_y);
Gian Marco05288a22017-11-21 10:57:50 +00001337
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001338 sum_col_32 += CONVERT(b0, VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)) + CONVERT(b1, VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)) + CONVERT(b2, VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)) + CONVERT(b3,
1339 VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE));
Gian Marco05288a22017-11-21 10:57:50 +00001340
1341 matrix_b += 4 * src_stride_y;
1342 }
1343
1344 // This for loop perfoms the leftover accumulations
1345 for(; i < (int)ROWS_B; ++i)
1346 {
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001347 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1348 b0 = VLOAD(VEC_SIZE)(0, matrix_b);
Gian Marco05288a22017-11-21 10:57:50 +00001349
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001350 sum_col_32 += CONVERT(b0, VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE));
Gian Marco05288a22017-11-21 10:57:50 +00001351
1352 matrix_b += src_stride_y;
1353 }
1354
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001355#if defined(SCALAR)
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001356 sum_col_32 *= (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))SCALAR;
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001357#endif // defined(SCALAR)
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001358 VEC_DATA_TYPE(int, VEC_SIZE)
1359 res0 = CONVERT(sum_col_32, VEC_DATA_TYPE(int, VEC_SIZE));
1360
1361 STORE_VECTOR_SELECT(res, int, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Gian Marco05288a22017-11-21 10:57:50 +00001362}
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001363#endif // defined(COLS_B) && defined(ROWS_B) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
Gian Marco05288a22017-11-21 10:57:50 +00001364
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001365#endif // defined(DATA_TYPE) && defined(ACC_DATA_TYPE)
1366
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001367#if defined(K_OFFSET) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
1368
1369#define VEC_INT VEC_DATA_TYPE(int, VEC_SIZE)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001370
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001371/* Helper function used to calculate the offset contribution after matrix multiplication.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001372 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001373 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001374 * and calculates the offset contribution of matrix A and matrix B.
1375 *
1376 * @attention The k_offset = a_offset * b_offset * k (where k is the number of matrix A columns) needs to be passed at compile time using -DK_OFFSET (i.e. -DK_OFFSET=1200)
1377 * @note In case the offset contribution due to a_offset is required, a_offset needs to be passed at compile time using -DA_OFFSET (i.e. -DA_OFFSET=1)
1378 * @note In case the offset contribution due to b_offset is required, b_offset needs to be passed at compile time using -DB_OFFSET (i.e. -DB_OFFSET=6)
1379 * @note In case sum_col has batches, -DSUM_COL_HAS_BATCHES must be passed at compile time. Usually if gemmlowp is used to accelerate convolution layer, sum_col will not have batches
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001380 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1381 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001382 *
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001383 * @param[in] x max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001384 * @param[in] y get_global_id(1)
1385 * @param[in] z get_global_id(2)
1386 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1387 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1388 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1389 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1390 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1391 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1392 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1393 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1394 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1395 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1396 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1397 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1398 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1399 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1400 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1401 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
1402 */
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001403inline VEC_INT offset_contribution(
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001404 int x,
1405 int y,
1406 int z
1407#if defined(A_OFFSET)
1408 ,
1409 IMAGE_DECLARATION(sum_col)
1410#endif // defined(A_OFFSET)
1411#if defined(B_OFFSET)
1412 ,
1413 IMAGE_DECLARATION(sum_row)
1414#endif // defined(B_OFFSET)
1415#if defined(ADD_BIAS)
1416 ,
1417 VECTOR_DECLARATION(biases)
1418#endif // defined(ADD_BIAS)
1419)
1420{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001421 VEC_INT a_offset_s32 = (VEC_INT)0;
1422 VEC_INT b_offset_s32 = (VEC_INT)0;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001423
1424 int batch_id = z;
1425#if defined(DEPTH_INPUT3D)
1426 batch_id /= (int)DEPTH_INPUT3D;
1427#endif // defined(DEPTH_INPUT3D)
1428
1429#if defined(A_OFFSET)
1430 // Compute the offset contribution due to A_OFFSET
1431 __global uchar *sum_col_addr = sum_col_ptr + sum_col_offset_first_element_in_bytes + x * sizeof(int);
1432
1433 // Compute the offset contribution due to A_OFFSET
1434#if defined(SUM_COL_HAS_BATCHES)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001435 a_offset_s32 = VLOAD(VEC_SIZE)(0, (__global int *)(sum_col_addr + batch_id * sum_col_stride_y));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001436#else // defined(SUM_COL_HAS_BATCHES)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001437 a_offset_s32 = VLOAD(VEC_SIZE)(0, (__global int *)sum_col_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001438#endif // defined(SUM_COL_HAS_BATCHES)
1439
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001440 a_offset_s32 *= (VEC_INT)A_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001441#endif // defined(A_OFFSET)
1442
1443#if defined(B_OFFSET)
1444 // Compute the offset contribution due to A_OFFSET
1445 __global uchar *sum_row_addr = sum_row_ptr + sum_row_offset_first_element_in_bytes + y * sizeof(int);
1446
1447 // Compute the offset contribution due to B_OFFSET
1448#if defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001449 b_offset_s32 = (VEC_INT) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y)) + (z % (int)DEPTH_INPUT3D) * (int)HEIGHT_INPUT3D);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001450#else // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001451 b_offset_s32 = (VEC_INT) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y)));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001452#endif // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001453 b_offset_s32 *= (VEC_INT)B_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001454#endif // defined(B_OFFSET)
1455
1456#if defined(ADD_BIAS)
1457 // Add bias
1458 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1459
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001460 VEC_INT biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
1461 b_offset_s32 += (VEC_INT)biases_values;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001462#endif // defined(ADD_BIAS)
1463
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001464 return (VEC_INT)K_OFFSET + a_offset_s32 + b_offset_s32;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001465}
1466
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001467/* OpenCL kernel used to add the offset contribution after matrix multiplication. The computation is performed in-place
Gian Marco05288a22017-11-21 10:57:50 +00001468 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001469 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco05288a22017-11-21 10:57:50 +00001470 * and adds to it the offset contribution of matrix A and matrix B in-place.
1471 *
1472 * @attention The k_offset = a_offset * b_offset * k (where k is the number of matrix A columns) needs to be passed at compile time using -DK_OFFSET (i.e. -DK_OFFSET=1200)
1473 * @note In case the offset contribution due to a_offset is required, a_offset needs to be passed at compile time using -DA_OFFSET (i.e. -DA_OFFSET=1)
1474 * @note In case the offset contribution due to b_offset is required, b_offset needs to be passed at compile time using -DB_OFFSET (i.e. -DB_OFFSET=6)
Chunosov5124be52017-11-22 20:42:13 +07001475 * @note In case sum_col has batches, -DSUM_COL_HAS_BATCHES must be passed at compile time. Usually if gemmlowp is used to accelerate convolution layer, sum_col will not have batches
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001476 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1477 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco05288a22017-11-21 10:57:50 +00001478 *
1479 * The final result is:
1480 *
1481 * mm_result[i][k] = mm_result[i][k] +
1482 * (sum_col[k] * A_OFFSET) +
1483 * (sum_row[i] * B_OFFSET) +
1484 * (K_OFFSET)
1485 *
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +01001486 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1487 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1488 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1489 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1490 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1491 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1492 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1493 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001494 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1495 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1496 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1497 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1498 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1499 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1500 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1501 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1502 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1503 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1504 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1505 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1506 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1507 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1508 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1509 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Gian Marco05288a22017-11-21 10:57:50 +00001510 */
1511__kernel void gemmlowp_offset_contribution(TENSOR3D_DECLARATION(mm_result)
1512#if defined(A_OFFSET)
1513 ,
1514 IMAGE_DECLARATION(sum_col)
1515#endif // defined(A_OFFSET)
1516#if defined(B_OFFSET)
1517 ,
1518 IMAGE_DECLARATION(sum_row)
1519#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001520#if defined(ADD_BIAS)
1521 ,
1522 VECTOR_DECLARATION(biases)
1523#endif // defined(ADD_BIAS))
Gian Marco05288a22017-11-21 10:57:50 +00001524 )
1525{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001526 const int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +01001527 const int y = get_global_id(1);
1528 const int z = get_global_id(2);
1529
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001530 // Compute offset contribution
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001531 VEC_INT offset_term_s32 = offset_contribution(
1532 x, y, z
Gian Marco05288a22017-11-21 10:57:50 +00001533#if defined(A_OFFSET)
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001534 ,
1535 sum_col_ptr,
1536 sum_col_stride_x,
1537 sum_col_step_x,
1538 sum_col_stride_y,
1539 sum_col_step_y,
1540 sum_col_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001541#endif // defined(A_OFFSET)
Gian Marco05288a22017-11-21 10:57:50 +00001542#if defined(B_OFFSET)
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001543 ,
1544 sum_row_ptr,
1545 sum_row_stride_x,
1546 sum_row_step_x,
1547 sum_row_stride_y,
1548 sum_row_step_y,
1549 sum_row_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001550#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001551#if defined(ADD_BIAS)
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001552 ,
1553 biases_ptr,
1554 biases_stride_x,
1555 biases_step_x,
1556 biases_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001557#endif // defined(ADD_BIAS)
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001558 );
Gian Marco05288a22017-11-21 10:57:50 +00001559
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001560 __global uchar *mm_result_addr = mm_result_ptr + mm_result_offset_first_element_in_bytes + x * sizeof(int) + y * mm_result_stride_y + z * mm_result_stride_z;
Gian Marco05288a22017-11-21 10:57:50 +00001561
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001562 VEC_INT in_s32_0 = VLOAD(VEC_SIZE)(0, (__global int *)mm_result_addr);
Gian Marco05288a22017-11-21 10:57:50 +00001563
1564 // Add the offset terms to GEMM's result
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001565 in_s32_0 += offset_term_s32;
Gian Marco05288a22017-11-21 10:57:50 +00001566
1567 // Store the result with the offset contribution
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001568 STORE_VECTOR_SELECT(in_s32_, int, mm_result_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Gian Marco05288a22017-11-21 10:57:50 +00001569}
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001570
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00001571#if defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT) && defined(OUTPUT_DATA_TYPE)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001572/* OpenCL kernel used to add the offset contribution after @ref CLGEMMLowpMatrixMultiplyKernel and it quantizes down to uint8.
1573 *
1574 * This kernel takes a final int32 accumulator value (the output of @CLGEMMLowpMatrixMultiplyKernel), adds to it the offset contribution of matrix A and matrix B and quantizes to uint8 through the output stage.
1575 *
1576 *
1577 * @attention The k_offset = a_offset * b_offset * k (where k is the number of matrix A columns) needs to be passed at compile time using -DK_OFFSET (i.e. -DK_OFFSET=1200)
1578 * @note In case the offset contribution due to a_offset is required, a_offset needs to be passed at compile time using -DA_OFFSET (i.e. -DA_OFFSET=1)
1579 * @note In case the offset contribution due to b_offset is required, b_offset needs to be passed at compile time using -DB_OFFSET (i.e. -DB_OFFSET=6)
1580 * @note In case sum_col has batches, -DSUM_COL_HAS_BATCHES must be passed at compile time. Usually if gemmlowp is used to accelerate convolution layer, sum_col will not have batches
1581 *
1582 * The result before the output stage is:
1583 *
1584 * mm_result[i][k] = mm_result[i][k] +
1585 * (sum_col[k] * A_OFFSET) +
1586 * (sum_row[i] * B_OFFSET) +
1587 * (K_OFFSET)
1588 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001589 * This result is quantized down to uint8/int8 using the output stage. The output stage computes the following operations:
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001590 *
1591 * -# Add offset terms to final result
1592 * -# Multiply each entry of result by result_mult_int
1593 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
1594 * -# Shift the int32 accumulator by result_shift
1595 * -# Clamp the value between the specified min and max bounds (if -DMIN_BOUND and/or -DMAX_BOUND are passed at compile time)
Manuel Bottini959c26d2019-12-02 16:22:35 +00001596 * -# Clamp the resulting int32 values:
1597 * - to the [0..255] range and cast to QASYMM8.
1598 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001599 *
1600 * @attention The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_OFFSET, -RESULT_MULT_INT and -DRESULT_SHIFT
1601 *
1602 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Manuel Bottini959c26d2019-12-02 16:22:35 +00001603 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001604 * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND.
1605 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001606 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1607 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001608 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001609 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1610 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1611 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1612 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1613 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1614 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1615 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1616 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1617 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1618 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1619 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1620 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1621 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1622 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1623 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1624 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1625 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1626 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1627 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1628 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1629 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1630 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1631 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1632 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Manuel Bottini959c26d2019-12-02 16:22:35 +00001633 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001634 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1635 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1636 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1637 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1638 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1639 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1640 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1641 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1642 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1643 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1644 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1645 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1646 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1647 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1648 * @param[in] result_shifts_offset_first_element_in_bytes (Optional) The offset of the first element in the output shifts vector
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001649 */
1650__kernel void gemmlowp_offset_contribution_quantize_down(TENSOR3D_DECLARATION(mm_result)
1651#if defined(A_OFFSET)
1652 ,
1653 IMAGE_DECLARATION(sum_col)
1654#endif // defined(A_OFFSET)
1655#if defined(B_OFFSET)
1656 ,
1657 IMAGE_DECLARATION(sum_row)
1658#endif // defined(B_OFFSET)
1659 ,
1660#if defined(ADD_BIAS)
1661 VECTOR_DECLARATION(biases),
1662#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001663 TENSOR3D_DECLARATION(dst)
1664#if defined(PER_CHANNEL_QUANTIZATION)
1665 ,
1666 VECTOR_DECLARATION(result_multipliers),
1667 VECTOR_DECLARATION(result_shifts)
1668#endif // defined(PER_CHANNEL_QUANTIZATION)
1669 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001670{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001671 const int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001672 const int y = get_global_id(1);
1673 const int z = get_global_id(2);
1674
1675 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1676
1677 // Compute offset contribution
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001678 VEC_INT offset_term_s32 = offset_contribution(
1679 x, y, z
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001680#if defined(A_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001681 ,
1682 sum_col_ptr,
1683 sum_col_stride_x,
1684 sum_col_step_x,
1685 sum_col_stride_y,
1686 sum_col_step_y,
1687 sum_col_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001688#endif // defined(A_OFFSET)
1689#if defined(B_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001690 ,
1691 sum_row_ptr,
1692 sum_row_stride_x,
1693 sum_row_step_x,
1694 sum_row_stride_y,
1695 sum_row_step_y,
1696 sum_row_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001697#endif // defined(B_OFFSET)
1698#if defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001699 ,
1700 biases_ptr,
1701 biases_stride_x,
1702 biases_step_x,
1703 biases_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001704#endif // defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001705 );
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001706
1707 __global uchar *mm_result_addr = mm_result_ptr + mm_result_offset_first_element_in_bytes + x * sizeof(int) + y * mm_result_stride_y + z * mm_result_stride_z;
1708
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001709 VEC_INT in_s32 = VLOAD(VEC_SIZE)(0, (__global int *)mm_result_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001710
1711 // Add the offset terms to GEMM's result
1712 in_s32 += offset_term_s32;
1713
1714 // -------------- OUTPUT STAGE
1715
1716 // Add the offset terms to GEMM's result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001717 in_s32 += (VEC_INT)RESULT_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001718
1719 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001720#if defined(PER_CHANNEL_QUANTIZATION)
1721 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
1722 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + x * sizeof(int);
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001723 VEC_INT result_multipliers_values = VLOAD(VEC_SIZE)(0, (__global int *)result_multipliers_addr);
1724 VEC_INT result_shifts_values = VLOAD(VEC_SIZE)(0, (__global int *)result_shifts_addr);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001725
1726 in_s32 *= result_multipliers_values;
1727 in_s32 >>= result_shifts_values;
1728#else // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001729 in_s32 *= RESULT_MULTIPLIER;
1730
1731 in_s32 >>= RESULT_SHIFT;
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001732#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001733
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001734 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
1735 res0 = CONVERT_SAT(in_s32, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001736
1737#if defined(MIN_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001738 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001739#endif // defined(MIN_BOUND)
1740#if defined(MAX_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001741 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001742#endif // defined(MAX_BOUND)
1743
1744 // Store the result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001745 STORE_VECTOR_SELECT(res, OUTPUT_DATA_TYPE, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001746}
1747
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001748/* OpenCL kernel used to add the offset contribution after matrix multiplication and it quantizes down to uint8.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001749 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001750 * This kernel takes a final int32 accumulator value (the output of matrix multiplication), adds to it the offset contribution of matrix A and matrix B and quantizes to uint8 through the output stage.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001751 *
1752 *
1753 * @attention The k_offset = a_offset * b_offset * k (where k is the number of matrix A columns) needs to be passed at compile time using -DK_OFFSET (i.e. -DK_OFFSET=1200)
1754 * @note In case the offset contribution due to a_offset is required, a_offset needs to be passed at compile time using -DA_OFFSET (i.e. -DA_OFFSET=1)
1755 * @note In case the offset contribution due to b_offset is required, b_offset needs to be passed at compile time using -DB_OFFSET (i.e. -DB_OFFSET=6)
1756 * @note In case sum_col has batches, -DSUM_COL_HAS_BATCHES must be passed at compile time. Usually if gemmlowp is used to accelerate convolution layer, sum_col will not have batches
1757 *
1758 * The result before the output stage is:
1759 *
1760 * mm_result[i][k] = mm_result[i][k] +
1761 * (sum_col[k] * A_OFFSET) +
1762 * (sum_row[i] * B_OFFSET) +
1763 * (K_OFFSET)
1764 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001765 * This result is quantized down to uint8/int8 using the output stage. The output stage computes the following operations:
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001766 *
1767 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1768 * -# Add bias to final result if bias tensor is not a nullptr
1769 * -# Round to nearest division by a power-of-two using result_shift
1770 * -# Add offset to each result
1771 * -# Clamp the value between the specified min and max bounds
Manuel Bottini959c26d2019-12-02 16:22:35 +00001772 * -# Clamp the resulting int32 values:
1773 * - to the [0..255] range and cast to QASYMM8.
1774 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001775 *
1776 * @attention The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_OFFSET, -RESULT_MULT_INT and -DRESULT_SHIFT
1777 *
1778 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Manuel Bottini959c26d2019-12-02 16:22:35 +00001779 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001780 * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND.
1781 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001782 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1783 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001784 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001785 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1786 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1787 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1788 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1789 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1790 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1791 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1792 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1793 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1794 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1795 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1796 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1797 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1798 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1799 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1800 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1801 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1802 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1803 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1804 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1805 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1806 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1807 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1808 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001809 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001810 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1811 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1812 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1813 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1814 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1815 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1816 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1817 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1818 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1819 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1820 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1821 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1822 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1823 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1824 * @param[in] result_shifts_offset_first_element_in_bytes (Optional) The offset of the first element in the output shifts vector
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001825 */
1826__kernel void gemmlowp_offset_contribution_quantize_down_fixedpoint(TENSOR3D_DECLARATION(mm_result)
1827#if defined(A_OFFSET)
1828 ,
1829 IMAGE_DECLARATION(sum_col)
1830#endif // defined(A_OFFSET)
1831#if defined(B_OFFSET)
1832 ,
1833 IMAGE_DECLARATION(sum_row)
1834#endif // defined(B_OFFSET)
1835 ,
1836#if defined(ADD_BIAS)
1837 VECTOR_DECLARATION(biases),
1838#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001839 TENSOR3D_DECLARATION(dst)
1840#if defined(PER_CHANNEL_QUANTIZATION)
1841 ,
1842 VECTOR_DECLARATION(result_multipliers),
1843 VECTOR_DECLARATION(result_shifts)
1844#endif // defined(PER_CHANNEL_QUANTIZATION)
1845 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001846{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001847 const int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001848 const int y = get_global_id(1);
1849 const int z = get_global_id(2);
1850
1851 // Compute offset contribution
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001852 VEC_INT offset_term_s32 = offset_contribution(
1853 x, y, z
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001854#if defined(A_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001855 ,
1856 sum_col_ptr,
1857 sum_col_stride_x,
1858 sum_col_step_x,
1859 sum_col_stride_y,
1860 sum_col_step_y,
1861 sum_col_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001862#endif // defined(A_OFFSET)
1863#if defined(B_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001864 ,
1865 sum_row_ptr,
1866 sum_row_stride_x,
1867 sum_row_step_x,
1868 sum_row_stride_y,
1869 sum_row_step_y,
1870 sum_row_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001871#endif // defined(B_OFFSET)
1872#if defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001873 ,
1874 biases_ptr,
1875 biases_stride_x,
1876 biases_step_x,
1877 biases_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001878#endif // defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001879 );
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001880
1881 __global uchar *mm_result_addr = mm_result_ptr + mm_result_offset_first_element_in_bytes + x * sizeof(int) + y * mm_result_stride_y + z * mm_result_stride_z;
1882
1883 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1884
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001885 VEC_INT in_s32 = VLOAD(VEC_SIZE)(0, (__global int *)mm_result_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001886
1887 // Add the offset terms to GEMM's result
1888 in_s32 += offset_term_s32;
1889
1890 // -------------- OUTPUT STAGE
1891
1892 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001893#if defined(PER_CHANNEL_QUANTIZATION)
1894 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
1895 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + x * sizeof(int);
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001896 VEC_INT result_multipliers_values = VLOAD(VEC_SIZE)(0, (__global int *)result_multipliers_addr);
1897 VEC_INT result_shifts_values = VLOAD(VEC_SIZE)(0, (__global int *)result_shifts_addr);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001898
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001899 VEC_INT in_s32_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, VEC_SIZE);
1900 VEC_INT in_s32_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, VEC_SIZE);
1901 in_s32 = select(in_s32_shift_lt0, in_s32_shift_gt0, result_shifts_values >= 0);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001902#else // defined(PER_CHANNEL_QUANTIZATION)
1903
1904#if RESULT_SHIFT < 0
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001905 in_s32 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, RESULT_MULTIPLIER, RESULT_SHIFT, VEC_SIZE);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001906#else // RESULT_SHIFT >= 0
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001907 in_s32 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(in_s32, RESULT_MULTIPLIER, RESULT_SHIFT, VEC_SIZE);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001908#endif // RESULT_SHIFT < 0
1909
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001910#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001911
1912 // Add the offset terms to GEMM's result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001913 in_s32 += (VEC_INT)RESULT_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001914
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001915 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
1916 res0 = CONVERT_SAT(in_s32, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001917
1918#if defined(MIN_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001919 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001920#endif // defined(MIN_BOUND)
1921#if defined(MAX_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001922 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001923#endif // defined(MAX_BOUND)
1924
1925 // Store the result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001926 STORE_VECTOR_SELECT(res, OUTPUT_DATA_TYPE, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001927}
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00001928#endif // defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT) && defined(OUTPUT_DATA_TYPE)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001929
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001930#undef VEC_INT
1931
1932#endif // defined(K_OFFSET) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
Gian Marco05288a22017-11-21 10:57:50 +00001933
1934#if defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT)
Luca Foschiani689c9682020-02-26 14:30:14 +00001935/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8/QASYMM8_SIGNED
Gian Marco05288a22017-11-21 10:57:50 +00001936 *
Luca Foschiani689c9682020-02-26 14:30:14 +00001937 * This kernel takes a final int32 accumulator value and processes it to obtain the final QASYMM8/QASYMM8_SIGNED value.
Gian Marco05288a22017-11-21 10:57:50 +00001938 * The following computations will be performed by the kernel:
1939 *
1940 * -# Add offset terms to final result
1941 * -# Multiply each entry of result by result_mult_int
1942 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
1943 * -# Shift the int32 accumulator by result_shift
1944 * -# Clamp the value between the specified min and max bounds (if -DMIN_BOUND and/or -DMAX_BOUND are passed at compile time)
Luca Foschiani689c9682020-02-26 14:30:14 +00001945 * -# Clamp the resulting int32 values:
1946 * -# - to the [0..255] range and cast to QASYMM8.
1947 * -# - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco05288a22017-11-21 10:57:50 +00001948 *
1949 * @attention The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_OFFSET, -RESULT_MULT_INT and -DRESULT_SHIFT
1950 *
1951 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Luca Foschiani689c9682020-02-26 14:30:14 +00001952 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco05288a22017-11-21 10:57:50 +00001953 * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND.
1954 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001955 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco05288a22017-11-21 10:57:50 +00001956 *
1957 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
1958 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1959 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1960 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1961 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1962 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1963 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1964 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001965 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1966 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1967 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1968 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Luca Foschiani689c9682020-02-26 14:30:14 +00001969 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco05288a22017-11-21 10:57:50 +00001970 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1971 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1972 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1973 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1974 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1975 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1976 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1977 */
1978__kernel void gemmlowp_output_stage_quantize_down(TENSOR3D_DECLARATION(src),
1979#if defined(ADD_BIAS)
1980 VECTOR_DECLARATION(biases),
1981#endif // defined(ADD_BIAS)
1982 TENSOR3D_DECLARATION(dst))
1983{
1984 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001985 int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001986 int y = get_global_id(1);
1987 int z = get_global_id(2);
Gian Marco05288a22017-11-21 10:57:50 +00001988
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001989 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(int) + y * src_stride_y + z * src_stride_z;
Gian Marco05288a22017-11-21 10:57:50 +00001990
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001991 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1992
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001993 VEC_DATA_TYPE(int, VEC_SIZE)
1994 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00001995
Gian Marco05288a22017-11-21 10:57:50 +00001996#if defined(ADD_BIAS)
1997 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001998 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1999
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002000 VEC_DATA_TYPE(int, VEC_SIZE)
2001 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
2002 input_values += biases_values;
Gian Marco05288a22017-11-21 10:57:50 +00002003#endif // defined(ADD_BIAS)
2004
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002005 // Add the offset terms to GEMM's result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002006 input_values += (VEC_DATA_TYPE(int, VEC_SIZE))RESULT_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002007
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +00002008 // Multiply by result_mult_int and shift
Gian Marco58c57942017-11-28 09:10:03 +00002009 input_values *= RESULT_MULT_INT;
Gian Marco05288a22017-11-21 10:57:50 +00002010
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002011#if RESULT_SHIFT < 0
2012 input_values >>= -RESULT_SHIFT;
2013#else // RESULT_SHIFT >= 0
Gian Marco58c57942017-11-28 09:10:03 +00002014 input_values >>= RESULT_SHIFT;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002015#endif // RESULT_SHIFT < 0
Gian Marco05288a22017-11-21 10:57:50 +00002016
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002017 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
2018 res0 = CONVERT_SAT(input_values, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco05288a22017-11-21 10:57:50 +00002019
2020#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002021 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00002022#endif // defined(MIN_BOUND)
2023#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002024 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00002025#endif // defined(MAX_BOUND)
2026
2027 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002028 STORE_VECTOR_SELECT(res, OUTPUT_DATA_TYPE, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Gian Marco05288a22017-11-21 10:57:50 +00002029}
Gian Marco58c57942017-11-28 09:10:03 +00002030#endif // defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT)
2031
2032#if defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
Manuel Bottini959c26d2019-12-02 16:22:35 +00002033/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8/QASYMM8_SIGNED
Gian Marco58c57942017-11-28 09:10:03 +00002034 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00002035 * This kernel takes a final int32 accumulator value (the output of matrix multiplication), and processes it to obtain the final QASYMM8/QASYMM8_SIGNED value.
Gian Marco58c57942017-11-28 09:10:03 +00002036 * The following computations will be performed by the kernel:
2037 *
2038 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
2039 * -# Add bias to final result if bias tensor is not a nullptr
2040 * -# Round to nearest division by a power-of-two using result_shift
2041 * -# Add offset to each result
2042 * -# Clamp the value between the specified min and max bounds
Manuel Bottini1f332d42019-11-29 17:25:25 +00002043 * -# Clamp the resulting int32 values:
2044 * - to the [0..255] range and cast to QASYMM8.
2045 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco58c57942017-11-28 09:10:03 +00002046 *
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002047 * @attention The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_OFFSET_AFTER_SHIFT, -DRESULT_FIXEDPOINT_MULTIPLIER and -DRESULT_SHIFT
Gian Marco58c57942017-11-28 09:10:03 +00002048 *
2049 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Manuel Bottini1f332d42019-11-29 17:25:25 +00002050 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco58c57942017-11-28 09:10:03 +00002051 * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND.
2052 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002053 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
2054 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Gian Marco58c57942017-11-28 09:10:03 +00002055 *
2056 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2057 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2058 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2059 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2060 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2061 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2062 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2063 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002064 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
2065 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
2066 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
2067 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Sheri Zhang0cdbda52020-02-25 15:57:21 +00002068 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco58c57942017-11-28 09:10:03 +00002069 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2070 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2071 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2072 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2073 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2074 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2075 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2076 */
2077__kernel void gemmlowp_output_stage_quantize_down_fixedpoint(TENSOR3D_DECLARATION(src),
2078#if defined(ADD_BIAS)
2079 VECTOR_DECLARATION(biases),
2080#endif // defined(ADD_BIAS)
2081 TENSOR3D_DECLARATION(dst))
2082{
2083 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002084 int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002085 int y = get_global_id(1);
2086 int z = get_global_id(2);
Georgios Pinitas932491f2018-09-21 16:33:15 +01002087
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002088 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(int) + y * src_stride_y + z * src_stride_z;
Gian Marco58c57942017-11-28 09:10:03 +00002089
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002090 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
2091
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002092 VEC_DATA_TYPE(int, VEC_SIZE)
2093 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00002094
2095#if defined(ADD_BIAS)
2096 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002097 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
2098
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002099 VEC_DATA_TYPE(int, VEC_SIZE)
2100 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
2101 input_values += biases_values;
Gian Marco58c57942017-11-28 09:10:03 +00002102#endif // defined(ADD_BIAS)
2103
2104 // Multiply by result_mult_int and shift
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002105#if RESULT_SHIFT < 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002106 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, VEC_SIZE);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002107#else // RESULT_SHIFT >= 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002108 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, VEC_SIZE);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002109#endif // RESULT_SHIFT < 0
Gian Marco58c57942017-11-28 09:10:03 +00002110
2111 // Add the offset terms to GEMM's result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002112 input_values += (VEC_DATA_TYPE(int, VEC_SIZE))RESULT_OFFSET_AFTER_SHIFT;
Gian Marco58c57942017-11-28 09:10:03 +00002113
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002114 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
2115 res0 = CONVERT_SAT(input_values, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco58c57942017-11-28 09:10:03 +00002116
2117#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002118 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00002119#endif // defined(MIN_BOUND)
2120#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002121 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00002122#endif // defined(MAX_BOUND)
2123
2124 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002125 STORE_VECTOR_SELECT(res, OUTPUT_DATA_TYPE, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Gian Marco58c57942017-11-28 09:10:03 +00002126}
Chunosov5124be52017-11-22 20:42:13 +07002127#endif // defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002128
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002129#if defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
2130
Michalis Spyrou51146c52019-07-12 14:42:29 +01002131/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QSYMM16
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002132 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00002133 * This kernel takes a final int32 accumulator value (the output of matrix multiplication), and processes it to obtain the final QSYMM16 value.
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002134 * The following computations will be performed by the kernel:
2135 *
2136 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
2137 * -# Add bias to final result if bias tensor is not a nullptr
2138 * -# Round to nearest division by a power-of-two using result_shift
2139 * -# Add offset to each result
2140 * -# Clamp the value between the specified min and max bounds
2141 * -# Clamp the resulting int32 values to the [-32768..32767] range and cast to QSYMM16.
2142 *
2143 * @attention The offset, scalar scale factor and number of bits to shift right of output tensor must be passed at compile time using -DRESULT_FIXEDPOINT_MULTIPLIER and -DRESULT_SHIFT
2144 *
2145 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
2146 * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND.
2147 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002148 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
2149 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002150 *
2151 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2152 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2153 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2154 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2155 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2156 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2157 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2158 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
2159 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
2160 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
2161 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
2162 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
Sheri Zhangb18252d2020-04-07 11:04:57 +01002163 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QSYMM16
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002164 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2165 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2166 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2167 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2168 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2169 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2170 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2171 */
2172__kernel void gemmlowp_output_stage_quantize_down_fixedpoint_qsymm16(TENSOR3D_DECLARATION(src),
2173#if defined(ADD_BIAS)
2174 VECTOR_DECLARATION(biases),
2175#endif // defined(ADD_BIAS)
2176 TENSOR3D_DECLARATION(dst))
2177{
2178 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002179 int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002180 int y = get_global_id(1);
2181 int z = get_global_id(2);
2182
Michalis Spyrou51146c52019-07-12 14:42:29 +01002183 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(int) + y * src_stride_y + z * src_stride_z;
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002184
Michele Di Giorgioba14c922020-10-12 13:27:57 +01002185 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * sizeof(short) + y * dst_stride_y + z * dst_stride_z;
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002186
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002187 VEC_DATA_TYPE(int, VEC_SIZE)
2188 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002189
2190#if defined(ADD_BIAS)
2191 // Add bias
Michalis Spyrou51146c52019-07-12 14:42:29 +01002192 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002193
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002194 VEC_DATA_TYPE(int, VEC_SIZE)
2195 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
2196 input_values += biases_values;
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002197#endif // defined(ADD_BIAS)
2198
2199 // Multiply by result_mult_int and shift
Manuel Bottini07263982019-10-17 18:37:26 +01002200#if RESULT_SHIFT < 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002201 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, VEC_SIZE);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00002202#else // RESULT_SHIFT >= 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002203 input_values = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(input_values, RESULT_FIXEDPOINT_MULTIPLIER, RESULT_SHIFT, VEC_SIZE);
Manuel Bottini07263982019-10-17 18:37:26 +01002204#endif // RESULT_SHIFT < 0
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002205
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002206 VEC_DATA_TYPE(short, VEC_SIZE)
2207 res0 = CONVERT_SAT(input_values, VEC_DATA_TYPE(short, VEC_SIZE));
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002208
2209#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002210 res0 = max(res0, (VEC_DATA_TYPE(short, VEC_SIZE))MIN_BOUND);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002211#endif // defined(MIN_BOUND)
2212#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002213 res0 = min(res0, (VEC_DATA_TYPE(short, VEC_SIZE))MAX_BOUND);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002214#endif // defined(MAX_BOUND)
2215
2216 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002217 STORE_VECTOR_SELECT(res, short, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002218}
2219#endif // defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
2220
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002221#if defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET)
Sheri Zhang1b14c752020-03-09 14:29:52 +00002222/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QASYMM8/QASYMM8_SIGNED
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002223 *
Sheri Zhang1b14c752020-03-09 14:29:52 +00002224 * This kernel takes a final int32 accumulator value (the output of matrix multiplication), and processes it to obtain the final QASYMM8/QASYMM8_SIGNED value.
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002225 * The following computations will be performed by the kernel:
2226 *
2227 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
2228 * -# Add bias to final result if bias tensor is not a nullptr
2229 * -# Requantize
2230 * -# Add offset to each result
2231 * -# Clamp the value between the specified min and max bounds
Sheri Zhang1b14c752020-03-09 14:29:52 +00002232 * -# Clamp the resulting int32 values:
2233 * - to the [0..255] range and cast to QASYMM8.
2234 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002235 *
2236 * @attention The offset and scalar scale factor must be passed at compile time using -DRESULT_OFFSET, -DREAL_MULTIPLIER
2237 *
2238 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
Sheri Zhang1b14c752020-03-09 14:29:52 +00002239 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002240 * @note In case the clamping of the result is required, the min and max bounds can be passed at compile time using -DMIN_BOUND and -DMAX_BOUND.
2241 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002242 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
2243 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002244 *
2245 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2246 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2247 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2248 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2249 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2250 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2251 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2252 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
2253 * @param[in] biases_ptr Pointer to the biases tensor. Supported data type: same as @p src_ptr
2254 * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes)
2255 * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes)
2256 * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor
2257 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
2258 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2259 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2260 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2261 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2262 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2263 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2264 * @param[in] dst_stride_w Stride of the source tensor in W dimension (in bytes)
2265 * @param[in] dst_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
2266 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2267 */
2268__kernel void gemmlowp_output_stage_quantize_down_float(TENSOR3D_DECLARATION(src),
2269#if defined(ADD_BIAS)
2270 VECTOR_DECLARATION(biases),
2271#endif // defined(ADD_BIAS)
2272#if defined(DST_HEIGHT)
2273 TENSOR4D_DECLARATION(dst))
2274#else // defined(DST_HEIGHT)
2275 TENSOR3D_DECLARATION(dst))
2276#endif // defined(DST_HEIGHT)
2277{
2278 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002279 int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002280 int y = get_global_id(1);
2281 int z = get_global_id(2);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002282
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002283 __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(int) + y * src_stride_y + z * src_stride_z;
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002284
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002285 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
2286
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002287 VEC_DATA_TYPE(int, VEC_SIZE)
2288 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002289
2290#if defined(ADD_BIAS)
2291 // Add bias
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002292 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
2293
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002294 VEC_DATA_TYPE(int, VEC_SIZE)
2295 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002296 input_values += (int4)biases_values;
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002297#endif // defined(ADD_BIAS)
2298
2299 // Convert to float
Sheri Zhang1b14c752020-03-09 14:29:52 +00002300 float4 input_values_f = convert_float4(input_values);
2301 input_values_f = round(input_values_f * (float)REAL_MULTIPLIER + (float)OUTPUT_OFFSET);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002302
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002303 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
2304 res0 = CONVERT_SAT(input_values_f, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002305
2306#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002307 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002308#endif // defined(MIN_BOUND)
2309#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002310 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002311#endif // defined(MAX_BOUND)
2312
2313 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002314 STORE_VECTOR_SELECT(res, OUTPUT_DATA_TYPE, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002315}
Gian Marco Iodice27423f02020-08-12 14:12:28 +01002316#endif // defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET)