blob: 62c4cd31f5037fae6c7169098f1d980eed5fc324 [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Viet-Hoa Do82169b32022-05-26 16:50:21 +01002 * Copyright (c) 2017-2022 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"
Giorgio Arena5f6fdc12021-06-09 15:23:06 +010027#include "tile_helpers.h"
Gian Marco05288a22017-11-21 10:57:50 +000028
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000029#if defined(DATA_TYPE) && defined(ACC_DATA_TYPE)
30
Georgios Pinitasdaa38552018-08-28 17:43:18 +010031#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
32#if defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice4b908652018-10-18 10:21:02 +010033#define ARM_DOT(x, y, val) val = arm_dot_acc((x), (y), (val));
Georgios Pinitasdaa38552018-08-28 17:43:18 +010034#else // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice4b908652018-10-18 10:21:02 +010035#define ARM_DOT(x, y, val) val += arm_dot((x), (y));
Georgios Pinitasdaa38552018-08-28 17:43:18 +010036#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
37#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Giorgio Arenac50da382018-07-26 15:50:09 +010038
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010039#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
40
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
Gian Marco Iodiced11de982022-09-05 15:35:35 +0100293#if defined(GEMMLOWP_MM_RESHAPED_LHS_NT_RHS_T)
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
Michalis Spyrou2b7fee02021-04-27 14:10:20 +0100396 __global DATA_TYPE *lhs_addr = (__global DATA_TYPE *)(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
Michalis Spyrou2b7fee02021-04-27 14:10:20 +0100399 __global DATA_TYPE *rhs_addr = (__global DATA_TYPE *)(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}
Gian Marco Iodiced11de982022-09-05 15:35:35 +0100464#endif // defined(GEMMLOWP_MM_RESHAPED_LHS_NT_RHS_T)
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000465
Gian Marco Iodiced11de982022-09-05 15:35:35 +0100466#if defined(GEMMLOWP_MM_RESHAPED_ONLY_RHS_T_FUSED_OUTPUT_STAGE_FIXEDPOINT) || defined(GEMMLOWP_MM_RESHAPED_ONLY_RHS_T)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100467#if defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT)
468#define FUSED_OUTPUT_STAGE_FIXED_POINT
469#endif // defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000470
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000471/** This OpenCL kernel computes the matrix multiplication between 2 matrices with fused output stage using fixed-point arithmetic.
472 * The LHS matrix is NOT reshaped
473 * The RHS matrix is reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the block K0xN0 is transposed
474 *
475 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
476 * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
477 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
478 * @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).
479 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
480 * @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)
481 * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time.
482 * @note Only the following configurations of M0, N0 and K0 are currently supported:
483 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
484 * - N0 = 2, 3, 4, 8, 16
485 * - K0 = 2, 3, 4, 8, 16
486 * - H0 >= 1
487 *
488 * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time:
489 * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D
490 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
491 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
492 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
493 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix
494 *
495 * @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
496 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
497 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
498 * @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.
499 * These values can be used to implement "rectified linear unit" activation functions
500 * @note In case of per-channel quantization of matrix B, -DPER_CHANNEL_QUANTIZATION must be passed at compile time.
501 *
502 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM8_SIGNED
503 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
504 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
505 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
506 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
507 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
508 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
509 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
510 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
511 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
512 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
513 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
514 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as @p lhs_ptr
515 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
516 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
517 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
518 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
519 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
520 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
521 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
522 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
523 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
524 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
525 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: S32
526 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
527 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
528 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
529 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
530 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
531 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: S32
532 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
533 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
534 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
535 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
536 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
537 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: S32
538 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
539 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
540 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
541 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
542 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
543 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
544 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
545 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
546 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
547 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
548 * @param[in] result_shifts_offset_first_element_in_bytes (Optional) The offset of the first element in the output shifts vector
549 */
Gian Marco Iodiced11de982022-09-05 15:35:35 +0100550#if defined(GEMMLOWP_MM_RESHAPED_ONLY_RHS_T_FUSED_OUTPUT_STAGE_FIXEDPOINT)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100551__kernel void gemmlowp_mm_reshaped_only_rhs_t_fused_output_stage_fixedpoint
Gian Marco Iodiced11de982022-09-05 15:35:35 +0100552#elif defined(GEMMLOWP_MM_RESHAPED_ONLY_RHS_T) // defined(GEMMLOWP_MM_RESHAPED_ONLY_RHS_T_FUSED_OUTPUT_STAGE_FIXEDPOINT)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100553__kernel void gemmlowp_mm_reshaped_only_rhs_t
Gian Marco Iodiced11de982022-09-05 15:35:35 +0100554#endif // defined(GEMMLOWP_MM_RESHAPED_ONLY_RHS_T)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100555(IMAGE_DECLARATION(lhs),
556 IMAGE_DECLARATION(rhs),
557 IMAGE_DECLARATION(dst),
558 uint lhs_stride_z,
559 uint rhs_stride_z,
560 uint dst_stride_z
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000561#if defined(REINTERPRET_INPUT_AS_3D)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100562 ,
563 uint lhs_cross_plane_pad
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000564#endif // REINTERPRET_INPUT_AS_3D
565#if defined(REINTERPRET_OUTPUT_AS_3D)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100566 ,
567 uint dst_cross_plane_pad
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000568#endif // REINTERPRET_OUTPUT_AS_3D
569#if defined(A_OFFSET)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100570 ,
571 IMAGE_DECLARATION(sum_col)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000572#endif // defined(A_OFFSET)
573#if defined(B_OFFSET)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100574 ,
575 IMAGE_DECLARATION(sum_row)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000576#endif // defined(B_OFFSET)
577#if defined(ADD_BIAS)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100578 ,
579 VECTOR_DECLARATION(biases)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000580#endif // defined(ADD_BIAS)
581#if defined(PER_CHANNEL_QUANTIZATION)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100582 ,
583 VECTOR_DECLARATION(result_multipliers),
584 VECTOR_DECLARATION(result_shifts)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000585#endif // defined(PER_CHANNEL_QUANTIZATION)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100586)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000587{
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100588 // @note: replace with (DIMENSION + PAD) once we pass the relevant info at compile time
589#define FULL_LHS_HEIGHT (lhs_stride_z / lhs_stride_y)
590#define FULL_DST_HEIGHT (dst_stride_z / dst_stride_y)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000591
592 // RHS offset and step X
593#if defined(RHS_INTERLEAVE)
594#define RHS_OFFSET_X (K0)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100595#define RHS_STEP_X (K0 * H0)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000596#else // defined(RHS_INTERLEAVE)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100597#define RHS_OFFSET_X (K0 * N0)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000598#define RHS_STEP_X (K0)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000599#endif // defined(RHS_INTERLEAVE)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100600#define RHS_STEP_LOOP (N0 * K0 * H0)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000601
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100602 uint x = GET_SPATIAL_IDX(0, 1, 1);
603 uint y = GET_SPATIAL_IDX(1, M0, PARTIAL_STORE_M0);
604 uint z = GET_SPATIAL_IDX(2, 1, 1);
605 int xo = (x * N0);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000606
607#if defined(DUMMY_WORK_ITEMS)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100608 if((xo >= N) || (y >= M))
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000609 {
610 return;
611 }
612#endif // defined(DUMMY_WORK_ITEMS)
613
614 // Compute LHS matrix address
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100615 uint lhs_y = y + z * FULL_LHS_HEIGHT;
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000616
617 // Compute RHS matrix address
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100618 uint rhs_offset_x = (x % H0) * RHS_OFFSET_X;
619 uint rhs_offset_y = (x / H0) * rhs_stride_y;
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000620
621#if defined(MATRIX_B_DEPTH)
622 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100623 rhs_offset_y += (z % MATRIX_B_DEPTH) * rhs_stride_z;
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000624#else // defined(MATRIX_B_DEPTH)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100625 rhs_offset_y += z * rhs_stride_z;
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000626#endif // defined(MATRIX_B_DEPTH)
627
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000628 // Initialize the accumulators
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100629 TILE(ACC_DATA_TYPE, M0, N0, c);
630 LOOP_UNROLLING(int, i, 0, 1, M0,
631 {
632 c[i].v = 0;
633 })
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000634
Manuel Bottini488f5082020-10-29 13:51:23 +0000635 int i = 0;
636 for(; i <= (K - K0); i += K0)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000637 {
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100638 TILE(DATA_TYPE, M0, K0, a);
639 TILE(DATA_TYPE, N0, K0, b);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000640
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100641 // Load values from LHS matrix
642 T_LOAD(DATA_TYPE, M0, K0, BUFFER, lhs, i, lhs_y, 1, lhs_stride_y, a);
643
644 // // Load values from RHS matrix
645 LOOP_UNROLLING(int, _i, 0, 1, N0,
646 {
647 b[_i].v = VLOAD(K0)(0, (__global DATA_TYPE *)(rhs_ptr + rhs_offset_first_element_in_bytes + rhs_offset_x + rhs_offset_y + _i * RHS_STEP_X));
648 })
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000649
650 // Partial matrix multiplication M0,N0,K0
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100651 T_MMUL(DATA_TYPE, DATA_TYPE, ACC_DATA_TYPE, M0, N0, K0, NT, T, a, b, c);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000652
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100653 rhs_offset_x += RHS_STEP_LOOP;
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000654 }
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100655
656#if((K % K0) != 0)
657
Manuel Bottini488f5082020-10-29 13:51:23 +0000658 // Left-over accumulations
659 for(; i < K; ++i)
660 {
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100661 TILE(DATA_TYPE, M0, 1, a);
662 TILE(DATA_TYPE, N0, 1, b);
663
Manuel Bottini488f5082020-10-29 13:51:23 +0000664 // Load values from LHS matrix
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100665 T_LOAD(DATA_TYPE, M0, 1, BUFFER, lhs, i, lhs_y, 1, lhs_stride_y, a);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000666
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100667 LOOP_UNROLLING(int, _i, 0, 1, N0,
668 {
669 b[_i].v = *(__global DATA_TYPE *)(rhs_ptr + rhs_offset_first_element_in_bytes + rhs_offset_x + rhs_offset_y + _i * RHS_STEP_X);
670 })
Manuel Bottini488f5082020-10-29 13:51:23 +0000671
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100672 T_MMUL(DATA_TYPE, DATA_TYPE, ACC_DATA_TYPE, M0, N0, 1, NT, T, a, b, c);
673
674 rhs_offset_x += 1;
Manuel Bottini488f5082020-10-29 13:51:23 +0000675 }
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100676#endif // ((K % K0) != 0)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000677
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100678#if defined(FUSED_OUTPUT_STAGE_FIXED_POINT)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000679
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100680 TILE(int, M0, N0, c_int);
681 TILE(int, M0, N0, offset_s32);
682 LOOP_UNROLLING(int, i, 0, 1, M0,
683 {
684 offset_s32[i].v = (VEC_DATA_TYPE(int, N0))K_OFFSET;
685 })
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000686
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100687 LOOP_UNROLLING(int, i, 0, 1, M0,
688 {
689 c_int[i].v = CONVERT_SAT(c[i].v, VEC_DATA_TYPE(int, N0));
690 })
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000691
692#if defined(A_OFFSET)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000693
694#if defined(SUM_COL_HAS_BATCHES)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100695 int sum_col_y = z;
696#else // defined(SUM_COL_HAS_BATCHES)
697 int sum_col_y = 0;
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000698#endif // defined(SUM_COL_HAS_BATCHES)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100699 TILE(int, 1, N0, a_offset_s32);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000700
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100701 T_LOAD(int, 1, N0, BUFFER, sum_col, xo, sum_col_y, 1, sum_col_stride_y, a_offset_s32);
702
703 a_offset_s32[0].v *= A_OFFSET;
704
Michalis Spyroub1fcefd2022-06-15 19:02:28 +0100705 T_ELTWISE_BROADCAST_ADD_X(int, M0, N0, offset_s32, a_offset_s32, offset_s32);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000706#endif // defined(A_OFFSET)
707
708#if defined(B_OFFSET)
709 // Compute the offset contribution due to B_OFFSET
Gian Marco Iodice27423f02020-08-12 14:12:28 +0100710 // Note: The sum_row tensor is generated through CLGEMMLowpMatrixAReductionKernel which
711 // does not introduce paddings. For this reason is safe to access the tensor in this manner
712 // without considering that the coordinate "y" could come from an input 3D tensor
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100713 TILE(int, M0, N0, b_offset_s32);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000714
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100715 T_LOAD(int, M0, 1, BUFFER, sum_row, y + z * (sum_row_stride_y / sizeof(int)), 0, 1, sum_row_stride_x, b_offset_s32);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000716
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100717 LOOP_UNROLLING(int, i, 0, 1, M0,
718 {
719 offset_s32[i].v += b_offset_s32[i].v *B_OFFSET;
720 })
721
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000722#endif // defined(B_OFFSET)
723
724#if defined(ADD_BIAS)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000725
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100726 TILE(int, 1, N0, bias);
727
728 T_LOAD(int, 1, N0, BUFFER, biases, xo, 0, 1, 0, bias);
729
Michalis Spyroub1fcefd2022-06-15 19:02:28 +0100730 T_ELTWISE_BROADCAST_ADD_X(int, M0, N0, offset_s32, bias, offset_s32);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000731#endif // defined(ADD_BIAS)
732
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100733 LOOP_UNROLLING(int, i, 0, 1, M0,
734 {
735 c_int[i].v += offset_s32[i].v;
736 })
737
738 TILE(DATA_TYPE, M0, N0, c_lp);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000739
740 // Multiply by result_mult_int and shift
741#if defined(PER_CHANNEL_QUANTIZATION)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100742 TILE(int, 1, N0, res_mul);
743 TILE(int, 1, N0, res_shift);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000744
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100745 T_LOAD(int, 1, N0, BUFFER, result_multipliers, xo, 0, 0, 0, res_mul);
746 T_LOAD(int, 1, N0, BUFFER, result_shifts, xo, 0, 0, 0, res_shift);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000747
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100748 T_QUANTIZE8(int, DATA_TYPE, PER_CHANNEL, M0, N0, RESULT_OFFSET, RESULT_SHIFT, RESULT_MULTIPLIER, c_int, res_mul, res_shift, c_lp);
749#else // defined(PER_CHANNEL_QUANTIZATION)
750 T_QUANTIZE8(int, DATA_TYPE, PER_TENSOR, M0, N0, RESULT_OFFSET, RESULT_SHIFT, RESULT_MULTIPLIER, c_int, 0, 0, c_lp);
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000751#endif // defined(PER_CHANNEL_QUANTIZATION)
752
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000753#if defined(MIN_BOUND)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100754 LOOP_UNROLLING(int, i, 0, 1, M0,
755 {
756 c_lp[i].v = max(c_lp[i].v, (VEC_DATA_TYPE(DATA_TYPE, N0))MIN_BOUND);
757 })
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000758#endif // defined(MIN_BOUND)
759#if defined(MAX_BOUND)
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100760 LOOP_UNROLLING(int, i, 0, 1, M0,
761 {
762 c_lp[i].v = min(c_lp[i].v, (VEC_DATA_TYPE(DATA_TYPE, N0))MAX_BOUND);
763 })
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000764#endif // defined(MAX_BOUND)
765
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100766#else // defined(FUSED_OUTPUT_STAGE_FIXED_POINT)
767 TILE(int, M0, N0, c_lp);
Manuel Bottini488f5082020-10-29 13:51:23 +0000768
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100769 LOOP_UNROLLING(int, i, 0, 1, M0,
770 {
771 c_lp[i].v = CONVERT_SAT(c[i].v, VEC_DATA_TYPE(int, N0));
772 })
773#endif // defined(FUSED_OUTPUT_STAGE_FIXED_POINT)
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000774
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100775 TILE(uint, M0, 1, dst_indirect_y);
776
777 LOOP_UNROLLING(int, i, 0, 1, M0,
778 {
779#if defined(REINTERPRET_OUTPUT_AS_3D)
780 dst_indirect_y[i].v = (uint)min((int)((y + i) % HEIGHT_GEMM3D), (int)HEIGHT_GEMM3D - 1);
781 dst_indirect_y[i].v += (uint)min((int)((y + i) / HEIGHT_GEMM3D), (int)DEPTH_GEMM3D - 1) * FULL_DST_HEIGHT;
782 dst_indirect_y[i].v += z *FULL_DST_HEIGHT *DEPTH_GEMM3D;
783#else // (REINTERPRET_OUTPUT_AS_3D)
784 dst_indirect_y[i].v = (uint)min((int)y + i, (int)M - 1) + z *FULL_DST_HEIGHT;
785#endif // defined(REINTERPRET_OUTPUT_AS_3D)
786 })
787
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100788 const bool cond_x = (xo > (N - N0)) & (PARTIAL_STORE_N0 != 0);
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100789
790#if defined(FUSED_OUTPUT_STAGE_FIXED_POINT)
791 T_STORE_INDIRECT_WIDTH_SELECT(DATA_TYPE, M0, N0, PARTIAL_STORE_N0, BUFFER, dst, xo, dst_stride_y, cond_x, c_lp, dst_indirect_y);
792#else // defined(FUSED_OUTPUT_STAGE_FIXED_POINT)
793 T_STORE_INDIRECT_WIDTH_SELECT(int, M0, N0, PARTIAL_STORE_N0, BUFFER, dst, xo, dst_stride_y, cond_x, c_lp, dst_indirect_y);
794#endif // defined(FUSED_OUTPUT_STAGE_FIXED_POINT)
795
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000796#undef RHS_OFFSET_X
797#undef RHS_STEP_X
Giorgio Arena5f6fdc12021-06-09 15:23:06 +0100798#undef RHS_STEP_LOOP
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000799}
Gian Marco Iodiced11de982022-09-05 15:35:35 +0100800#endif // defined(GEMMLOWP_MM_RESHAPED_ONLY_RHS_T_FUSED_OUTPUT_STAGE_FIXEDPOINT) || defined(GEMMLOWP_MM_RESHAPED_ONLY_RHS_T)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000801
Gian Marco Iodiced11de982022-09-05 15:35:35 +0100802#if defined(GEMMLOWP_MM_NATIVE)
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100803
804/** This OpenCL kernel computes the matrix multiplication between 2 matrices.
805 * The LHS matrix is NOT reshaped
806 * The RHS matrix is NOT reshaped
807 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000808 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
809 * @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 +0100810 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
811 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
812 * @note The number of N0 columns to process must be passed at compile time using -DN0 (i.e. -DN0=2)
813 * @note The number of K0 partial accumulations must be passed at compile time using -DK0 (i.e., -DK0=2)
814 * @note Only the following configurations of M0, N0 and K0 are currently supported:
815 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
816 * - N0 = 2, 3, 4, 8, 16
817 * - K0 = 2, 3, 4, 8, 16
818 *
819 * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time:
820 * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D
821 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
822 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
823 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
824 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix
825 *
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000826 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100827 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
828 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
829 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
830 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
831 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
832 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
833 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
834 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
835 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
836 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
837 * @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 +0000838 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100839 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
840 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
841 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
842 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
843 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
844 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
845 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
846 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
847 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
848 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
849 */
850__kernel void gemmlowp_mm_native(IMAGE_DECLARATION(lhs),
851 IMAGE_DECLARATION(rhs),
852 IMAGE_DECLARATION(dst),
853 uint lhs_stride_z,
854 uint rhs_stride_z,
855 uint dst_stride_z
856#if defined(REINTERPRET_INPUT_AS_3D)
857 ,
858 uint lhs_cross_plane_pad
859#endif // REINTERPRET_INPUT_AS_3D
860#if defined(REINTERPRET_OUTPUT_AS_3D)
861 ,
862 uint dst_cross_plane_pad
863#endif // REINTERPRET_OUTPUT_AS_3D
864 )
865{
866 uint x = get_global_id(0);
867 uint y = get_global_id(1);
868 uint z = get_global_id(2);
869
870#if defined(DUMMY_WORK_ITEMS)
871 if((x * N0 >= N) || (y * M0 >= M))
872 {
873 return;
874 }
875#endif // defined(DUMMY_WORK_ITEMS)
876
877 // Compute LHS matrix address
morgolockcf343e32020-10-12 14:00:43 +0100878 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 +0100879
880 // Compute RHS matrix address
morgolockcf343e32020-10-12 14:00:43 +0100881 uint rhs_offset = rhs_offset_first_element_in_bytes + x * N0 * sizeof(DATA_TYPE);
882
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100883#if defined(MATRIX_B_DEPTH)
884 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
885 rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z;
886#else // defined(MATRIX_B_DEPTH)
887 rhs_offset += z * rhs_stride_z;
888#endif // defined(MATRIX_B_DEPTH)
889
890 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0);
891 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
892
893#if defined(REINTERPRET_INPUT_AS_3D)
894 // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice9ae06d42020-10-22 16:37:12 +0100895 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 +0100896
897 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
898 // multiply lhs_stride_z by DEPTH_GEMM3D
899 lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D;
900
901#else // defined(REINTERPRET_INPUT_AS_3D)
902
903 // Add offset for batched GEMM
904 lhs_offset += z * lhs_stride_z;
905
906#endif // defined(REINTERPRET_INPUT_AS_3D)
907
908 // Initialize the accumulators
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000909 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 +0100910
911 int i = 0;
912
913 for(; i <= (K - K0); i += K0)
914 {
915 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000916 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100917
918 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000919 LOAD_BLOCK(K0, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100920
SiCong Li738893e2020-05-01 12:55:16 +0100921 // Partial matrix multiplication M0,N0,K0
922#if(GPU_ARCH == GPU_ARCH_MIDGARD)
923 ARM_MM_NATIVE_N0XK0XM0(VEC_DATA_TYPE(ACC_DATA_TYPE, N0), M0, K0, a, b, c);
924#else // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100925 // Transpose the values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000926 TRANSPOSE_K0XN0(K0, N0, b_t, b, DATA_TYPE);
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100927
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100928 ARM_MM_K0XN0XM0(M0, N0, K0, a, b_t, c);
SiCong Li738893e2020-05-01 12:55:16 +0100929#endif // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100930
931 // Update the offset
932 lhs_offset += K0;
933 rhs_offset += K0 * rhs_stride_y;
934 }
935
936 // Left-over for loop
937 for(; i < K; ++i)
938 {
939 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000940 LOAD_BLOCK(M0, 1, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100941
942 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000943 LOAD_BLOCK(1, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100944
SiCong Li738893e2020-05-01 12:55:16 +0100945 // Partial matrix multiplication M0,N0,1
946#if(GPU_ARCH == GPU_ARCH_MIDGARD)
947 ARM_MM_NATIVE_N0XK0XM0(VEC_DATA_TYPE(ACC_DATA_TYPE, N0), M0, 1, a, b, c);
948#else // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100949 // Transpose the values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000950 TRANSPOSE_K0XN0(1, N0, b_t, b, DATA_TYPE);
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100951
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100952 ARM_MM_K0XN0XM0(M0, N0, 1, a, b_t, c);
SiCong Li738893e2020-05-01 12:55:16 +0100953#endif // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100954
955 // Update the offset
956 lhs_offset += 1;
957 rhs_offset += rhs_stride_y;
958 }
959
morgolockcf343e32020-10-12 14:00:43 +0100960 __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);
961
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100962 REPEAT_VAR_INIT_TO_CONST(M0, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
963
964#if defined(REINTERPRET_OUTPUT_AS_3D)
965 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice9ae06d42020-10-22 16:37:12 +0100966 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 +0100967
968 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
969 // multiply dst_stride_z by DEPTH_GEMM3D
970 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
971
972#else // defined(REINTERPRET_OUTPUT_AS_3D)
973
974 // Add offset for batched GEMM
975 dst_addr += z * dst_stride_z;
976
977#endif // defined(REINTERPRET_OUTPUT_AS_3D)
morgolockcf343e32020-10-12 14:00:43 +0100978 const bool cond_y = y == 0;
979 const bool cond_x = ((x + 1) * N0 >= N);
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100980
Michele Di Giorgio27d92fd2020-10-27 12:44:17 +0000981 // Convert and store output block
982 REPEAT_VAR_INIT_CONVERT(M0, VEC_DATA_TYPE(int, N0), c, res); // resN = CONVERT(cN, VEC_DATA_TYPE(int, N0));
983 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 +0100984}
Gian Marco Iodiced11de982022-09-05 15:35:35 +0100985#endif // defined(GEMMLOWP_MM_NATIVE)
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100986
Gian Marco Iodiced11de982022-09-05 15:35:35 +0100987#if defined(GEMMLOWP_MATRIX_A_REDUCTION)
Gian Marco05288a22017-11-21 10:57:50 +0000988/** 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 +0100989 * 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 +0000990 *
991 * @note This stage is needed to handle the offset of matrix product
992 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
993 *
994 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
Manuel Bottini959c26d2019-12-02 16:22:35 +0000995 * @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 +0000996 * @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 +0100997 * @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 +0000998 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100999 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8/QASYMM8_SIGNED/QSYMM8
Gian Marco05288a22017-11-21 10:57:50 +00001000 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1001 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1002 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1003 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1004 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1005 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1006 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1007 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1008 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1009 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1010 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1011 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1012 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1013 */
1014__kernel void gemmlowp_matrix_a_reduction(TENSOR3D_DECLARATION(src),
1015 IMAGE_DECLARATION(dst))
1016{
1017 // Compute source and destination addresses
1018 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1019 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1020
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001021 VEC_DATA_TYPE(ACC_DATA_TYPE, 4)
1022 sum_row_32 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))0;
1023 ACC_DATA_TYPE sum_row = 0;
Gian Marco05288a22017-11-21 10:57:50 +00001024
Manuel Bottini959c26d2019-12-02 16:22:35 +00001025 __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 +00001026
1027 int i = 0;
1028
1029 // This for loop performs 16 accumulations
1030 for(; i <= ((int)COLS_A - 16); i += 16)
1031 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001032 const VEC_DATA_TYPE(DATA_TYPE, 16) a0 = vload16(0, matrix_a + i);
Gian Marco05288a22017-11-21 10:57:50 +00001033
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001034 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,
1035 VEC_DATA_TYPE(ACC_DATA_TYPE, 4));
Gian Marco05288a22017-11-21 10:57:50 +00001036 }
1037
1038 // This for loop performs the leftover accumulations
1039 for(; i < COLS_A; ++i)
1040 {
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001041 sum_row += (ACC_DATA_TYPE)matrix_a[i];
Gian Marco05288a22017-11-21 10:57:50 +00001042 }
1043
Manuel Bottini959c26d2019-12-02 16:22:35 +00001044 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 +00001045
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001046#if defined(SCALAR)
1047 sum_row *= (int)SCALAR;
1048#endif // defined(SCALAR)
Gian Marco05288a22017-11-21 10:57:50 +00001049 *((__global int *)dst.ptr) = (int)sum_row;
1050}
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001051#endif // defined(GEMMLOWP_MATRIX_A_REDUCTION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001052
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001053#if defined(GEMMLOWP_MATRIX_A_REDUCTION_DOT8)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001054/** 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.
1055 * 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 +01001056 *
1057 * @note This stage is needed to handle the offset of matrix product
1058 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1059 *
1060 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
Manuel Bottini959c26d2019-12-02 16:22:35 +00001061 * @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 +00001062 * @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 +01001063 * @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 +01001064 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +01001065 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8/QASYMM8_SIGNED/QSYMM8
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001066 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1067 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1068 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1069 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1070 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1071 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1072 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1073 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1074 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1075 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1076 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1077 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1078 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1079 */
1080__kernel void gemmlowp_matrix_a_reduction_dot8(TENSOR3D_DECLARATION(src),
1081 IMAGE_DECLARATION(dst))
1082{
1083 // Compute source and destination addresses
1084 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1085 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1086
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001087 ACC_DATA_TYPE sum_row = 0;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001088
Manuel Bottini959c26d2019-12-02 16:22:35 +00001089 __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 +01001090
1091 int i = 0;
1092
1093 // This for loop performs 16 accumulations
1094 for(; i <= ((int)COLS_A - 32); i += 32)
1095 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001096 VEC_DATA_TYPE(DATA_TYPE, 16)
1097 a0 = vload16(0, matrix_a + i);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001098
Viet-Hoa Do82169b32022-05-26 16:50:21 +01001099 DOT_PRODUCT4_INTEGER8(DATA_TYPE, DATA_TYPE, DATA_TYPE, a0.s0123, (VEC_DATA_TYPE(DATA_TYPE, 4))(1), sum_row);
1100 DOT_PRODUCT4_INTEGER8(DATA_TYPE, DATA_TYPE, DATA_TYPE, a0.s4567, (VEC_DATA_TYPE(DATA_TYPE, 4))(1), sum_row);
1101 DOT_PRODUCT4_INTEGER8(DATA_TYPE, DATA_TYPE, DATA_TYPE, a0.s89AB, (VEC_DATA_TYPE(DATA_TYPE, 4))(1), sum_row);
1102 DOT_PRODUCT4_INTEGER8(DATA_TYPE, DATA_TYPE, DATA_TYPE, a0.sCDEF, (VEC_DATA_TYPE(DATA_TYPE, 4))(1), sum_row);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001103
Manuel Bottini959c26d2019-12-02 16:22:35 +00001104 a0 = vload16(1, matrix_a + i);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001105
Viet-Hoa Do82169b32022-05-26 16:50:21 +01001106 DOT_PRODUCT4_INTEGER8(DATA_TYPE, DATA_TYPE, DATA_TYPE, a0.s0123, (VEC_DATA_TYPE(DATA_TYPE, 4))(1), sum_row);
1107 DOT_PRODUCT4_INTEGER8(DATA_TYPE, DATA_TYPE, DATA_TYPE, a0.s4567, (VEC_DATA_TYPE(DATA_TYPE, 4))(1), sum_row);
1108 DOT_PRODUCT4_INTEGER8(DATA_TYPE, DATA_TYPE, DATA_TYPE, a0.s89AB, (VEC_DATA_TYPE(DATA_TYPE, 4))(1), sum_row);
1109 DOT_PRODUCT4_INTEGER8(DATA_TYPE, DATA_TYPE, DATA_TYPE, a0.sCDEF, (VEC_DATA_TYPE(DATA_TYPE, 4))(1), sum_row);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001110 }
1111
1112 // This for loop performs the leftover accumulations
1113 for(; i < COLS_A; ++i)
1114 {
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001115 sum_row += (ACC_DATA_TYPE)matrix_a[i];
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001116 }
1117
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001118#if defined(SCALAR)
1119 sum_row *= (int)SCALAR;
1120#endif // defined(SCALAR)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001121 *((__global int *)dst.ptr) = (int)sum_row;
1122}
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001123#endif // defined(GEMMLOWP_MATRIX_A_REDUCTION_DOT8)
Gian Marco05288a22017-11-21 10:57:50 +00001124
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001125#if defined(GEMMLOWP_MATRIX_B_REDUCTION)
Gian Marco05288a22017-11-21 10:57:50 +00001126/** 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 +01001127 * 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 +00001128 *
1129 * @note This stage is needed to handle the offset of matrix product
1130 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1131 *
1132 * @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 +00001133 * @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 +00001134 * @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 +01001135 * @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 +01001136 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1137 * @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 +00001138 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +01001139 * @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 +00001140 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1141 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1142 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1143 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1144 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1145 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1146 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1147 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1148 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1149 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1150 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1151 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1152 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1153 */
1154__kernel void gemmlowp_matrix_b_reduction(TENSOR3D_DECLARATION(src),
1155 IMAGE_DECLARATION(dst))
1156{
1157 // Compute source and destination addresses
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001158 const uint x_offs = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
1159 const uint y = get_global_id(1);
Gian Marco05288a22017-11-21 10:57:50 +00001160
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001161 __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);
1162 __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 +00001163
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001164 VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001165 sum_col_32 = (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))0;
Gian Marco05288a22017-11-21 10:57:50 +00001166
1167 int i = 0;
1168 // This for loop performs 4 accumulations
1169 for(; i <= ((int)ROWS_B - 4); i += 4)
1170 {
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001171 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1172 b0 = VLOAD(VEC_SIZE)(0, matrix_b + 0 * src_stride_y);
1173 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1174 b1 = VLOAD(VEC_SIZE)(0, matrix_b + 1 * src_stride_y);
1175 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1176 b2 = VLOAD(VEC_SIZE)(0, matrix_b + 2 * src_stride_y);
1177 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1178 b3 = VLOAD(VEC_SIZE)(0, matrix_b + 3 * src_stride_y);
Gian Marco05288a22017-11-21 10:57:50 +00001179
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001180 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,
1181 VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE));
Gian Marco05288a22017-11-21 10:57:50 +00001182
1183 matrix_b += 4 * src_stride_y;
1184 }
1185
1186 // This for loop perfoms the leftover accumulations
1187 for(; i < (int)ROWS_B; ++i)
1188 {
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001189 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1190 b0 = VLOAD(VEC_SIZE)(0, matrix_b);
Gian Marco05288a22017-11-21 10:57:50 +00001191
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001192 sum_col_32 += CONVERT(b0, VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE));
Gian Marco05288a22017-11-21 10:57:50 +00001193
1194 matrix_b += src_stride_y;
1195 }
1196
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001197#if defined(SCALAR)
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001198 sum_col_32 *= (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))SCALAR;
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001199#endif // defined(SCALAR)
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001200 VEC_DATA_TYPE(int, VEC_SIZE)
1201 res0 = CONVERT(sum_col_32, VEC_DATA_TYPE(int, VEC_SIZE));
1202
1203 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 +00001204}
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001205#endif // defined(GEMMLOWP_MATRIX_B_REDUCTION)
Gian Marco05288a22017-11-21 10:57:50 +00001206
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001207#endif // defined(DATA_TYPE) && defined(ACC_DATA_TYPE)
1208
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001209#if defined(K_OFFSET) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
1210
1211#define VEC_INT VEC_DATA_TYPE(int, VEC_SIZE)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001212
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001213/* Helper function used to calculate the offset contribution after matrix multiplication.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001214 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001215 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001216 * and calculates the offset contribution of matrix A and matrix B.
1217 *
1218 * @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)
1219 * @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)
1220 * @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)
1221 * @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 +01001222 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1223 * @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 +01001224 *
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001225 * @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 +01001226 * @param[in] y get_global_id(1)
1227 * @param[in] z get_global_id(2)
1228 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1229 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1230 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1231 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1232 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1233 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1234 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1235 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1236 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1237 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1238 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1239 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1240 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1241 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1242 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1243 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
1244 */
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001245inline VEC_INT offset_contribution(
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001246 int x,
1247 int y,
1248 int z
1249#if defined(A_OFFSET)
1250 ,
1251 IMAGE_DECLARATION(sum_col)
1252#endif // defined(A_OFFSET)
1253#if defined(B_OFFSET)
1254 ,
1255 IMAGE_DECLARATION(sum_row)
1256#endif // defined(B_OFFSET)
1257#if defined(ADD_BIAS)
1258 ,
1259 VECTOR_DECLARATION(biases)
1260#endif // defined(ADD_BIAS)
1261)
1262{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001263 VEC_INT a_offset_s32 = (VEC_INT)0;
1264 VEC_INT b_offset_s32 = (VEC_INT)0;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001265
1266 int batch_id = z;
1267#if defined(DEPTH_INPUT3D)
1268 batch_id /= (int)DEPTH_INPUT3D;
1269#endif // defined(DEPTH_INPUT3D)
1270
1271#if defined(A_OFFSET)
1272 // Compute the offset contribution due to A_OFFSET
1273 __global uchar *sum_col_addr = sum_col_ptr + sum_col_offset_first_element_in_bytes + x * sizeof(int);
1274
1275 // Compute the offset contribution due to A_OFFSET
1276#if defined(SUM_COL_HAS_BATCHES)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001277 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 +01001278#else // defined(SUM_COL_HAS_BATCHES)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001279 a_offset_s32 = VLOAD(VEC_SIZE)(0, (__global int *)sum_col_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001280#endif // defined(SUM_COL_HAS_BATCHES)
1281
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001282 a_offset_s32 *= (VEC_INT)A_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001283#endif // defined(A_OFFSET)
1284
1285#if defined(B_OFFSET)
1286 // Compute the offset contribution due to A_OFFSET
1287 __global uchar *sum_row_addr = sum_row_ptr + sum_row_offset_first_element_in_bytes + y * sizeof(int);
1288
1289 // Compute the offset contribution due to B_OFFSET
1290#if defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001291 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 +01001292#else // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001293 b_offset_s32 = (VEC_INT) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y)));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001294#endif // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001295 b_offset_s32 *= (VEC_INT)B_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001296#endif // defined(B_OFFSET)
1297
1298#if defined(ADD_BIAS)
1299 // Add bias
1300 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1301
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001302 VEC_INT biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
1303 b_offset_s32 += (VEC_INT)biases_values;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001304#endif // defined(ADD_BIAS)
1305
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001306 return (VEC_INT)K_OFFSET + a_offset_s32 + b_offset_s32;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001307}
1308
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001309#if defined(GEMMLOWP_OFFSET_CONTRIBUTION)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001310/* OpenCL kernel used to add the offset contribution after matrix multiplication. The computation is performed in-place
Gian Marco05288a22017-11-21 10:57:50 +00001311 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001312 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco05288a22017-11-21 10:57:50 +00001313 * and adds to it the offset contribution of matrix A and matrix B in-place.
1314 *
1315 * @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)
1316 * @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)
1317 * @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 +07001318 * @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 +01001319 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1320 * @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 +00001321 *
1322 * The final result is:
1323 *
1324 * mm_result[i][k] = mm_result[i][k] +
1325 * (sum_col[k] * A_OFFSET) +
1326 * (sum_row[i] * B_OFFSET) +
1327 * (K_OFFSET)
1328 *
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +01001329 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1330 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1331 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1332 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1333 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1334 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1335 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1336 * @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 +01001337 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1338 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1339 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1340 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1341 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1342 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1343 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1344 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1345 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1346 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1347 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1348 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1349 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1350 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1351 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1352 * @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 +00001353 */
1354__kernel void gemmlowp_offset_contribution(TENSOR3D_DECLARATION(mm_result)
1355#if defined(A_OFFSET)
1356 ,
1357 IMAGE_DECLARATION(sum_col)
1358#endif // defined(A_OFFSET)
1359#if defined(B_OFFSET)
1360 ,
1361 IMAGE_DECLARATION(sum_row)
1362#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001363#if defined(ADD_BIAS)
1364 ,
1365 VECTOR_DECLARATION(biases)
1366#endif // defined(ADD_BIAS))
Gian Marco05288a22017-11-21 10:57:50 +00001367 )
1368{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001369 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 +01001370 const int y = get_global_id(1);
1371 const int z = get_global_id(2);
1372
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001373 // Compute offset contribution
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001374 VEC_INT offset_term_s32 = offset_contribution(
1375 x, y, z
Gian Marco05288a22017-11-21 10:57:50 +00001376#if defined(A_OFFSET)
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001377 ,
1378 sum_col_ptr,
1379 sum_col_stride_x,
1380 sum_col_step_x,
1381 sum_col_stride_y,
1382 sum_col_step_y,
1383 sum_col_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001384#endif // defined(A_OFFSET)
Gian Marco05288a22017-11-21 10:57:50 +00001385#if defined(B_OFFSET)
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001386 ,
1387 sum_row_ptr,
1388 sum_row_stride_x,
1389 sum_row_step_x,
1390 sum_row_stride_y,
1391 sum_row_step_y,
1392 sum_row_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001393#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001394#if defined(ADD_BIAS)
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001395 ,
1396 biases_ptr,
1397 biases_stride_x,
1398 biases_step_x,
1399 biases_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001400#endif // defined(ADD_BIAS)
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001401 );
Gian Marco05288a22017-11-21 10:57:50 +00001402
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001403 __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 +00001404
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001405 VEC_INT in_s32_0 = VLOAD(VEC_SIZE)(0, (__global int *)mm_result_addr);
Gian Marco05288a22017-11-21 10:57:50 +00001406
1407 // Add the offset terms to GEMM's result
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001408 in_s32_0 += offset_term_s32;
Gian Marco05288a22017-11-21 10:57:50 +00001409
1410 // Store the result with the offset contribution
Michele Di Giorgio410bca42020-10-22 11:07:33 +01001411 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 +00001412}
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001413#endif // defined(GEMMLOWP_OFFSET_CONTRIBUTION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001414
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001415#if defined(GEMMLOWP_OFFSET_CONTRIBUTION_QUANTIZE_DOWN)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001416/* OpenCL kernel used to add the offset contribution after @ref CLGEMMLowpMatrixMultiplyKernel and it quantizes down to uint8.
1417 *
1418 * 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.
1419 *
1420 *
1421 * @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)
1422 * @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)
1423 * @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)
1424 * @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
1425 *
1426 * The result before the output stage is:
1427 *
1428 * mm_result[i][k] = mm_result[i][k] +
1429 * (sum_col[k] * A_OFFSET) +
1430 * (sum_row[i] * B_OFFSET) +
1431 * (K_OFFSET)
1432 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001433 * 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 +01001434 *
1435 * -# Add offset terms to final result
1436 * -# Multiply each entry of result by result_mult_int
1437 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
1438 * -# Shift the int32 accumulator by result_shift
1439 * -# 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 +00001440 * -# Clamp the resulting int32 values:
1441 * - to the [0..255] range and cast to QASYMM8.
1442 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001443 *
1444 * @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
1445 *
1446 * @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 +00001447 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001448 * @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.
1449 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001450 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1451 * @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 +01001452 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001453 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1454 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1455 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1456 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1457 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1458 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1459 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1460 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1461 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1462 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1463 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1464 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1465 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1466 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1467 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1468 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1469 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1470 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1471 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1472 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1473 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1474 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1475 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1476 * @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 +00001477 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001478 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1479 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1480 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1481 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1482 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1483 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1484 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1485 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1486 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1487 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1488 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1489 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1490 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1491 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1492 * @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 +01001493 */
1494__kernel void gemmlowp_offset_contribution_quantize_down(TENSOR3D_DECLARATION(mm_result)
1495#if defined(A_OFFSET)
1496 ,
1497 IMAGE_DECLARATION(sum_col)
1498#endif // defined(A_OFFSET)
1499#if defined(B_OFFSET)
1500 ,
1501 IMAGE_DECLARATION(sum_row)
1502#endif // defined(B_OFFSET)
1503 ,
1504#if defined(ADD_BIAS)
1505 VECTOR_DECLARATION(biases),
1506#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001507 TENSOR3D_DECLARATION(dst)
1508#if defined(PER_CHANNEL_QUANTIZATION)
1509 ,
1510 VECTOR_DECLARATION(result_multipliers),
1511 VECTOR_DECLARATION(result_shifts)
1512#endif // defined(PER_CHANNEL_QUANTIZATION)
1513 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001514{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001515 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 +01001516 const int y = get_global_id(1);
1517 const int z = get_global_id(2);
1518
1519 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1520
1521 // Compute offset contribution
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001522 VEC_INT offset_term_s32 = offset_contribution(
1523 x, y, z
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001524#if defined(A_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001525 ,
1526 sum_col_ptr,
1527 sum_col_stride_x,
1528 sum_col_step_x,
1529 sum_col_stride_y,
1530 sum_col_step_y,
1531 sum_col_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001532#endif // defined(A_OFFSET)
1533#if defined(B_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001534 ,
1535 sum_row_ptr,
1536 sum_row_stride_x,
1537 sum_row_step_x,
1538 sum_row_stride_y,
1539 sum_row_step_y,
1540 sum_row_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001541#endif // defined(B_OFFSET)
1542#if defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001543 ,
1544 biases_ptr,
1545 biases_stride_x,
1546 biases_step_x,
1547 biases_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001548#endif // defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001549 );
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001550
1551 __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;
1552
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001553 VEC_INT in_s32 = VLOAD(VEC_SIZE)(0, (__global int *)mm_result_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001554
1555 // Add the offset terms to GEMM's result
1556 in_s32 += offset_term_s32;
1557
1558 // -------------- OUTPUT STAGE
1559
1560 // Add the offset terms to GEMM's result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001561 in_s32 += (VEC_INT)RESULT_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001562
1563 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001564#if defined(PER_CHANNEL_QUANTIZATION)
1565 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
1566 __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 +01001567 VEC_INT result_multipliers_values = VLOAD(VEC_SIZE)(0, (__global int *)result_multipliers_addr);
1568 VEC_INT result_shifts_values = VLOAD(VEC_SIZE)(0, (__global int *)result_shifts_addr);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001569
1570 in_s32 *= result_multipliers_values;
1571 in_s32 >>= result_shifts_values;
1572#else // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001573 in_s32 *= RESULT_MULTIPLIER;
1574
1575 in_s32 >>= RESULT_SHIFT;
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001576#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001577
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001578 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
1579 res0 = CONVERT_SAT(in_s32, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001580
1581#if defined(MIN_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001582 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001583#endif // defined(MIN_BOUND)
1584#if defined(MAX_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001585 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001586#endif // defined(MAX_BOUND)
1587
1588 // Store the result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001589 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 +01001590}
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001591#endif // defined(GEMMLOWP_OFFSET_CONTRIBUTION_QUANTIZE_DOWN)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001592
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001593#if defined(GEMMLOWP_OFFSET_CONTRIBUTION_QUANTIZE_DOWN_FIXEDPOINT)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001594/* 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 +01001595 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001596 * 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 +01001597 *
1598 *
1599 * @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)
1600 * @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)
1601 * @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)
1602 * @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
1603 *
1604 * The result before the output stage is:
1605 *
1606 * mm_result[i][k] = mm_result[i][k] +
1607 * (sum_col[k] * A_OFFSET) +
1608 * (sum_row[i] * B_OFFSET) +
1609 * (K_OFFSET)
1610 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001611 * 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 +01001612 *
1613 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1614 * -# Add bias to final result if bias tensor is not a nullptr
1615 * -# Round to nearest division by a power-of-two using result_shift
1616 * -# Add offset to each result
1617 * -# Clamp the value between the specified min and max bounds
Manuel Bottini959c26d2019-12-02 16:22:35 +00001618 * -# Clamp the resulting int32 values:
1619 * - to the [0..255] range and cast to QASYMM8.
1620 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001621 *
1622 * @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
1623 *
1624 * @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 +00001625 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001626 * @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.
1627 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001628 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1629 * @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 +01001630 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001631 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1632 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1633 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1634 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1635 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1636 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1637 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1638 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1639 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1640 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1641 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1642 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1643 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1644 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1645 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1646 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1647 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1648 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1649 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1650 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1651 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1652 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1653 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1654 * @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 +01001655 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001656 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1657 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1658 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1659 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1660 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1661 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1662 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1663 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1664 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1665 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1666 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1667 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1668 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1669 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1670 * @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 +01001671 */
1672__kernel void gemmlowp_offset_contribution_quantize_down_fixedpoint(TENSOR3D_DECLARATION(mm_result)
1673#if defined(A_OFFSET)
1674 ,
1675 IMAGE_DECLARATION(sum_col)
1676#endif // defined(A_OFFSET)
1677#if defined(B_OFFSET)
1678 ,
1679 IMAGE_DECLARATION(sum_row)
1680#endif // defined(B_OFFSET)
1681 ,
1682#if defined(ADD_BIAS)
1683 VECTOR_DECLARATION(biases),
1684#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001685 TENSOR3D_DECLARATION(dst)
1686#if defined(PER_CHANNEL_QUANTIZATION)
1687 ,
1688 VECTOR_DECLARATION(result_multipliers),
1689 VECTOR_DECLARATION(result_shifts)
1690#endif // defined(PER_CHANNEL_QUANTIZATION)
1691 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001692{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001693 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 +01001694 const int y = get_global_id(1);
1695 const int z = get_global_id(2);
1696
1697 // Compute offset contribution
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001698 VEC_INT offset_term_s32 = offset_contribution(
1699 x, y, z
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001700#if defined(A_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001701 ,
1702 sum_col_ptr,
1703 sum_col_stride_x,
1704 sum_col_step_x,
1705 sum_col_stride_y,
1706 sum_col_step_y,
1707 sum_col_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001708#endif // defined(A_OFFSET)
1709#if defined(B_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001710 ,
1711 sum_row_ptr,
1712 sum_row_stride_x,
1713 sum_row_step_x,
1714 sum_row_stride_y,
1715 sum_row_step_y,
1716 sum_row_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001717#endif // defined(B_OFFSET)
1718#if defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001719 ,
1720 biases_ptr,
1721 biases_stride_x,
1722 biases_step_x,
1723 biases_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001724#endif // defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001725 );
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001726
1727 __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;
1728
1729 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1730
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001731 VEC_INT in_s32 = VLOAD(VEC_SIZE)(0, (__global int *)mm_result_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001732
1733 // Add the offset terms to GEMM's result
1734 in_s32 += offset_term_s32;
1735
1736 // -------------- OUTPUT STAGE
1737
1738 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001739#if defined(PER_CHANNEL_QUANTIZATION)
1740 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
1741 __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 +01001742 VEC_INT result_multipliers_values = VLOAD(VEC_SIZE)(0, (__global int *)result_multipliers_addr);
1743 VEC_INT result_shifts_values = VLOAD(VEC_SIZE)(0, (__global int *)result_shifts_addr);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001744
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001745 VEC_INT in_s32_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, VEC_SIZE);
1746 VEC_INT in_s32_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, VEC_SIZE);
1747 in_s32 = select(in_s32_shift_lt0, in_s32_shift_gt0, result_shifts_values >= 0);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001748#else // defined(PER_CHANNEL_QUANTIZATION)
1749
1750#if RESULT_SHIFT < 0
Adnan AlSinan3bedd2f2022-09-22 01:21:42 +01001751 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 +01001752#else // RESULT_SHIFT >= 0
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001753 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 +01001754#endif // RESULT_SHIFT < 0
1755
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001756#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001757
1758 // Add the offset terms to GEMM's result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001759 in_s32 += (VEC_INT)RESULT_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001760
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001761 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
1762 res0 = CONVERT_SAT(in_s32, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001763
1764#if defined(MIN_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001765 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001766#endif // defined(MIN_BOUND)
1767#if defined(MAX_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001768 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001769#endif // defined(MAX_BOUND)
1770
1771 // Store the result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001772 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 +01001773}
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001774#endif // defined(GEMMLOWP_OFFSET_CONTRIBUTION_QUANTIZE_DOWN_FIXEDPOINT)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001775
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001776#undef VEC_INT
1777
1778#endif // defined(K_OFFSET) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
Gian Marco05288a22017-11-21 10:57:50 +00001779
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001780#if defined(GEMMLOWP_OUTPUT_STAGE_QUANTIZE_DOWN)
Luca Foschiani689c9682020-02-26 14:30:14 +00001781/** 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 +00001782 *
Luca Foschiani689c9682020-02-26 14:30:14 +00001783 * 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 +00001784 * The following computations will be performed by the kernel:
1785 *
1786 * -# Add offset terms to final result
1787 * -# Multiply each entry of result by result_mult_int
1788 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
1789 * -# Shift the int32 accumulator by result_shift
1790 * -# 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 +00001791 * -# Clamp the resulting int32 values:
1792 * -# - to the [0..255] range and cast to QASYMM8.
1793 * -# - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco05288a22017-11-21 10:57:50 +00001794 *
1795 * @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
1796 *
1797 * @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 +00001798 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco05288a22017-11-21 10:57:50 +00001799 * @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.
1800 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001801 * @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 +00001802 *
1803 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
1804 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1805 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1806 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1807 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1808 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1809 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1810 * @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 +01001811 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1812 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1813 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1814 * @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 +00001815 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco05288a22017-11-21 10:57:50 +00001816 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1817 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1818 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1819 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1820 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1821 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1822 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1823 */
1824__kernel void gemmlowp_output_stage_quantize_down(TENSOR3D_DECLARATION(src),
1825#if defined(ADD_BIAS)
1826 VECTOR_DECLARATION(biases),
1827#endif // defined(ADD_BIAS)
1828 TENSOR3D_DECLARATION(dst))
1829{
1830 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001831 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 +01001832 int y = get_global_id(1);
1833 int z = get_global_id(2);
Gian Marco05288a22017-11-21 10:57:50 +00001834
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001835 __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 +00001836
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001837 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1838
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001839 VEC_DATA_TYPE(int, VEC_SIZE)
1840 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00001841
Gian Marco05288a22017-11-21 10:57:50 +00001842#if defined(ADD_BIAS)
1843 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001844 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1845
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001846 VEC_DATA_TYPE(int, VEC_SIZE)
1847 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
1848 input_values += biases_values;
Gian Marco05288a22017-11-21 10:57:50 +00001849#endif // defined(ADD_BIAS)
1850
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001851 // Add the offset terms to GEMM's result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001852 input_values += (VEC_DATA_TYPE(int, VEC_SIZE))RESULT_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001853
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +00001854 // Multiply by result_mult_int and shift
Gian Marco58c57942017-11-28 09:10:03 +00001855 input_values *= RESULT_MULT_INT;
Gian Marco05288a22017-11-21 10:57:50 +00001856
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001857#if RESULT_SHIFT < 0
1858 input_values >>= -RESULT_SHIFT;
1859#else // RESULT_SHIFT >= 0
Gian Marco58c57942017-11-28 09:10:03 +00001860 input_values >>= RESULT_SHIFT;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001861#endif // RESULT_SHIFT < 0
Gian Marco05288a22017-11-21 10:57:50 +00001862
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001863 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
1864 res0 = CONVERT_SAT(input_values, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco05288a22017-11-21 10:57:50 +00001865
1866#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001867 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00001868#endif // defined(MIN_BOUND)
1869#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001870 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00001871#endif // defined(MAX_BOUND)
1872
1873 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001874 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 +00001875}
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001876#endif // defined(GEMMLOWP_OUTPUT_STAGE_QUANTIZE_DOWN)
Gian Marco58c57942017-11-28 09:10:03 +00001877
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001878#if defined(GEMMLOWP_OUTPUT_STAGE_QUANTIZE_DOWN_FIXEDPOINT)
Manuel Bottini959c26d2019-12-02 16:22:35 +00001879/** 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 +00001880 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001881 * 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 +00001882 * The following computations will be performed by the kernel:
1883 *
1884 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1885 * -# Add bias to final result if bias tensor is not a nullptr
1886 * -# Round to nearest division by a power-of-two using result_shift
1887 * -# Add offset to each result
1888 * -# Clamp the value between the specified min and max bounds
Manuel Bottini1f332d42019-11-29 17:25:25 +00001889 * -# Clamp the resulting int32 values:
1890 * - to the [0..255] range and cast to QASYMM8.
1891 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco58c57942017-11-28 09:10:03 +00001892 *
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001893 * @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 +00001894 *
1895 * @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 +00001896 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco58c57942017-11-28 09:10:03 +00001897 * @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.
1898 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001899 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1900 * @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 +00001901 *
1902 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
1903 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1904 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1905 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1906 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1907 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1908 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1909 * @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 +01001910 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1911 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1912 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1913 * @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 +00001914 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco58c57942017-11-28 09:10:03 +00001915 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1916 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1917 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1918 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1919 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1920 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1921 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1922 */
1923__kernel void gemmlowp_output_stage_quantize_down_fixedpoint(TENSOR3D_DECLARATION(src),
1924#if defined(ADD_BIAS)
1925 VECTOR_DECLARATION(biases),
1926#endif // defined(ADD_BIAS)
1927 TENSOR3D_DECLARATION(dst))
1928{
1929 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001930 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 +01001931 int y = get_global_id(1);
1932 int z = get_global_id(2);
Georgios Pinitas932491f2018-09-21 16:33:15 +01001933
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001934 __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 +00001935
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001936 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1937
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001938 VEC_DATA_TYPE(int, VEC_SIZE)
1939 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00001940
1941#if defined(ADD_BIAS)
1942 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001943 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1944
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001945 VEC_DATA_TYPE(int, VEC_SIZE)
1946 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
1947 input_values += biases_values;
Gian Marco58c57942017-11-28 09:10:03 +00001948#endif // defined(ADD_BIAS)
1949
1950 // Multiply by result_mult_int and shift
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001951#if RESULT_SHIFT < 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001952 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 +01001953#else // RESULT_SHIFT >= 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001954 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 +01001955#endif // RESULT_SHIFT < 0
Gian Marco58c57942017-11-28 09:10:03 +00001956
1957 // Add the offset terms to GEMM's result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001958 input_values += (VEC_DATA_TYPE(int, VEC_SIZE))RESULT_OFFSET_AFTER_SHIFT;
Gian Marco58c57942017-11-28 09:10:03 +00001959
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001960 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
1961 res0 = CONVERT_SAT(input_values, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco58c57942017-11-28 09:10:03 +00001962
1963#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001964 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00001965#endif // defined(MIN_BOUND)
1966#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001967 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00001968#endif // defined(MAX_BOUND)
1969
1970 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001971 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 +00001972}
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001973#endif // defined(GEMMLOWP_OUTPUT_STAGE_QUANTIZE_DOWN_FIXEDPOINT)
Georgios Pinitas51e53a32018-10-22 13:49:08 +01001974
Gian Marco Iodiced11de982022-09-05 15:35:35 +01001975#if defined(GEMMLOWP_OUTPUT_STAGE_QUANTIZE_DOWN_FIXEDPOINT_QSYMM16)
Michalis Spyrou51146c52019-07-12 14:42:29 +01001976/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QSYMM16
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01001977 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001978 * 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 +01001979 * The following computations will be performed by the kernel:
1980 *
1981 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1982 * -# Add bias to final result if bias tensor is not a nullptr
1983 * -# Round to nearest division by a power-of-two using result_shift
1984 * -# Add offset to each result
1985 * -# Clamp the value between the specified min and max bounds
1986 * -# Clamp the resulting int32 values to the [-32768..32767] range and cast to QSYMM16.
1987 *
1988 * @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
1989 *
1990 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
1991 * @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.
1992 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001993 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1994 * @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 +01001995 *
1996 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
1997 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1998 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1999 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2000 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2001 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2002 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2003 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
2004 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
2005 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
2006 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
2007 * @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 +01002008 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QSYMM16
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002009 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2010 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2011 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2012 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2013 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2014 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2015 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2016 */
2017__kernel void gemmlowp_output_stage_quantize_down_fixedpoint_qsymm16(TENSOR3D_DECLARATION(src),
2018#if defined(ADD_BIAS)
2019 VECTOR_DECLARATION(biases),
2020#endif // defined(ADD_BIAS)
2021 TENSOR3D_DECLARATION(dst))
2022{
2023 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002024 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 +01002025 int y = get_global_id(1);
2026 int z = get_global_id(2);
2027
Michalis Spyrou51146c52019-07-12 14:42:29 +01002028 __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 +01002029
Michele Di Giorgioba14c922020-10-12 13:27:57 +01002030 __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 +01002031
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002032 VEC_DATA_TYPE(int, VEC_SIZE)
2033 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002034
2035#if defined(ADD_BIAS)
2036 // Add bias
Michalis Spyrou51146c52019-07-12 14:42:29 +01002037 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002038
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002039 VEC_DATA_TYPE(int, VEC_SIZE)
2040 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
2041 input_values += biases_values;
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002042#endif // defined(ADD_BIAS)
2043
2044 // Multiply by result_mult_int and shift
Manuel Bottini07263982019-10-17 18:37:26 +01002045#if RESULT_SHIFT < 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002046 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 +00002047#else // RESULT_SHIFT >= 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002048 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 +01002049#endif // RESULT_SHIFT < 0
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002050
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002051 VEC_DATA_TYPE(short, VEC_SIZE)
2052 res0 = CONVERT_SAT(input_values, VEC_DATA_TYPE(short, VEC_SIZE));
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002053
2054#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002055 res0 = max(res0, (VEC_DATA_TYPE(short, VEC_SIZE))MIN_BOUND);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002056#endif // defined(MIN_BOUND)
2057#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002058 res0 = min(res0, (VEC_DATA_TYPE(short, VEC_SIZE))MAX_BOUND);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002059#endif // defined(MAX_BOUND)
2060
2061 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002062 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 +01002063}
Gian Marco Iodiced11de982022-09-05 15:35:35 +01002064#endif // defined(GEMMLOWP_OUTPUT_STAGE_QUANTIZE_DOWN_FIXEDPOINT_QSYMM16)
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002065
Gian Marco Iodiced11de982022-09-05 15:35:35 +01002066#if defined(GEMMLOWP_OUTPUT_STAGE_QUANTIZE_DOWN_FLOAT)
Sheri Zhang1b14c752020-03-09 14:29:52 +00002067/** 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 +01002068 *
Sheri Zhang1b14c752020-03-09 14:29:52 +00002069 * 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 +01002070 * The following computations will be performed by the kernel:
2071 *
2072 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
2073 * -# Add bias to final result if bias tensor is not a nullptr
2074 * -# Requantize
2075 * -# Add offset to each result
2076 * -# Clamp the value between the specified min and max bounds
Sheri Zhang1b14c752020-03-09 14:29:52 +00002077 * -# Clamp the resulting int32 values:
2078 * - to the [0..255] range and cast to QASYMM8.
2079 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002080 *
2081 * @attention The offset and scalar scale factor must be passed at compile time using -DRESULT_OFFSET, -DREAL_MULTIPLIER
2082 *
2083 * @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 +00002084 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002085 * @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.
2086 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002087 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
2088 * @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 +01002089 *
2090 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2091 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2092 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2093 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2094 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2095 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2096 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2097 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
2098 * @param[in] biases_ptr Pointer to the biases tensor. Supported data type: same as @p src_ptr
2099 * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes)
2100 * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes)
2101 * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor
2102 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
2103 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2104 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2105 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2106 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2107 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2108 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2109 * @param[in] dst_stride_w Stride of the source tensor in W dimension (in bytes)
2110 * @param[in] dst_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
2111 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2112 */
2113__kernel void gemmlowp_output_stage_quantize_down_float(TENSOR3D_DECLARATION(src),
2114#if defined(ADD_BIAS)
2115 VECTOR_DECLARATION(biases),
2116#endif // defined(ADD_BIAS)
2117#if defined(DST_HEIGHT)
2118 TENSOR4D_DECLARATION(dst))
2119#else // defined(DST_HEIGHT)
2120 TENSOR3D_DECLARATION(dst))
2121#endif // defined(DST_HEIGHT)
2122{
2123 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002124 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 +00002125 int y = get_global_id(1);
2126 int z = get_global_id(2);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002127
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002128 __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 +01002129
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002130 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
2131
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002132 VEC_DATA_TYPE(int, VEC_SIZE)
2133 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002134
2135#if defined(ADD_BIAS)
2136 // Add bias
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002137 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
2138
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002139 VEC_DATA_TYPE(int, VEC_SIZE)
2140 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
Giorgio Arenafd83bc82021-05-12 12:44:47 +01002141 input_values += (VEC_DATA_TYPE(int, VEC_SIZE))biases_values;
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002142#endif // defined(ADD_BIAS)
2143
2144 // Convert to float
Giorgio Arenafd83bc82021-05-12 12:44:47 +01002145 VEC_DATA_TYPE(float, VEC_SIZE)
2146 input_values_f = CONVERT(input_values, VEC_DATA_TYPE(float, VEC_SIZE));
2147 input_values_f = round(input_values_f * (float)REAL_MULTIPLIER + (float)OUTPUT_OFFSET);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002148
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002149 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
2150 res0 = CONVERT_SAT(input_values_f, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002151
2152#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002153 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002154#endif // defined(MIN_BOUND)
2155#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002156 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002157#endif // defined(MAX_BOUND)
2158
2159 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002160 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 +01002161}
Gian Marco Iodiced11de982022-09-05 15:35:35 +01002162#endif // defined(GEMMLOWP_OUTPUT_STAGE_QUANTIZE_DOWN_FLOAT)