blob: 97150e05a2bc234594324cbb5dd1455ff61a7e78 [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Gian Marco05288a22017-11-21 10:57:50 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010024#include "gemm_helpers.h"
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000025#include "helpers_asymm.h"
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +000026#include "repeat.h"
Gian Marco05288a22017-11-21 10:57:50 +000027
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000028#if defined(DATA_TYPE) && defined(ACC_DATA_TYPE)
29
Georgios Pinitasdaa38552018-08-28 17:43:18 +010030#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
31#if defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice4b908652018-10-18 10:21:02 +010032#define ARM_DOT(x, y, val) val = arm_dot_acc((x), (y), (val));
Georgios Pinitasdaa38552018-08-28 17:43:18 +010033#else // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice4b908652018-10-18 10:21:02 +010034#define ARM_DOT(x, y, val) val += arm_dot((x), (y));
Georgios Pinitasdaa38552018-08-28 17:43:18 +010035#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
36#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Giorgio Arenac50da382018-07-26 15:50:09 +010037
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010038#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
39
40/** Specialized macros to perform the dot product instruction between two vectors of size N [1,16]. These macros use the dot8 instruction */
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000041#define ARM_DOT1(a, b, c) \
42 ({ \
43 ARM_DOT((VEC_DATA_TYPE(DATA_TYPE, 4))(a, (VEC_DATA_TYPE(DATA_TYPE, 3))0), (VEC_DATA_TYPE(DATA_TYPE, 4))(b, (VEC_DATA_TYPE(DATA_TYPE, 3))0), c); \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010044 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000045#define ARM_DOT2(a, b, c) \
46 ({ \
47 ARM_DOT((VEC_DATA_TYPE(DATA_TYPE, 4))(a, (VEC_DATA_TYPE(DATA_TYPE, 2))0), (VEC_DATA_TYPE(DATA_TYPE, 4))(b, (VEC_DATA_TYPE(DATA_TYPE, 2))0), c); \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010048 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000049#define ARM_DOT3(a, b, c) \
50 ({ \
51 ARM_DOT((VEC_DATA_TYPE(DATA_TYPE, 4))(a, (DATA_TYPE)0), (VEC_DATA_TYPE(DATA_TYPE, 4))(b, (DATA_TYPE)0), c); \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010052 })
53#define ARM_DOT4(a, b, c) \
54 ({ \
55 ARM_DOT(a, b, c); \
56 })
57#define ARM_DOT8(a, b, c) \
58 ({ \
59 ARM_DOT4((a.lo), (b.lo), c); \
60 ARM_DOT4((a.hi), (b.hi), c); \
61 })
62#define ARM_DOT16(a, b, c) \
63 ({ \
64 ARM_DOT8((a.lo), (b.lo), c); \
65 ARM_DOT8((a.hi), (b.hi), c); \
66 })
67
68#else // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
69
70/** Specialized macros to perform the dot product instruction between two vectors of size K0 [1,16] without using the dot8 instruction. */
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000071#define ARM_DOT1(a, b, c) \
72 ({ \
73 c += (ACC_DATA_TYPE)a * b; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010074 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000075#define ARM_DOT2(a, b, c) \
76 ({ \
77 c += (ACC_DATA_TYPE)a.s0 * b.s0; \
78 c += (ACC_DATA_TYPE)a.s1 * b.s1; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010079 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000080#define ARM_DOT3(a, b, c) \
81 ({ \
82 ARM_DOT2(a, b, c); \
83 c += (ACC_DATA_TYPE)a.s2 * b.s2; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010084 })
Michele Di Giorgiof9179d32019-11-27 16:17:30 +000085#define ARM_DOT4(a, b, c) \
86 ({ \
87 ARM_DOT3(a, b, c); \
88 c += (ACC_DATA_TYPE)a.s3 * b.s3; \
Gian Marco Iodice43a129e2019-05-14 10:14:08 +010089 })
90#define ARM_DOT8(a, b, c) \
91 ({ \
92 ARM_DOT4((a.lo), (b.lo), c); \
93 ARM_DOT4((a.hi), (b.hi), c); \
94 })
95#define ARM_DOT16(a, b, c) \
96 ({ \
97 ARM_DOT8((a.lo), (b.lo), c); \
98 ARM_DOT8((a.hi), (b.hi), c); \
99 })
100#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
101
102/** Specialized macros to perform a broadcast dot product operation between one vector "a" and N0 vectors "b" of size K0 [1,16] */
Gian Marco Iodice061eefd2020-04-23 13:40:00 +0100103#define ARM_DOT_K0X1(k0, a, b, c) \
104 ({ \
105 ARM_DOT_K0(k0, (a), (b##0), (c)); \
106 })
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100107#define ARM_DOT_K0X2(k0, a, b, c) \
108 ({ \
109 ARM_DOT_K0(k0, (a), (b##0), (c.s0)); \
110 ARM_DOT_K0(k0, (a), (b##1), (c.s1)); \
111 })
112#define ARM_DOT_K0X3(k0, a, b, c) \
113 ({ \
114 ARM_DOT_K0X2(k0, a, b, c); \
115 ARM_DOT_K0(k0, (a), (b##2), (c.s2)); \
116 })
117#define ARM_DOT_K0X4(k0, a, b, c) \
118 ({ \
119 ARM_DOT_K0X3(k0, a, b, c); \
120 ARM_DOT_K0(k0, (a), (b##3), (c.s3)); \
121 })
122#define ARM_DOT_K0X8(k0, a, b, c) \
123 ({ \
124 ARM_DOT_K0X4(k0, a, b, c); \
125 ARM_DOT_K0(k0, (a), (b##4), (c.s4)); \
126 ARM_DOT_K0(k0, (a), (b##5), (c.s5)); \
127 ARM_DOT_K0(k0, (a), (b##6), (c.s6)); \
128 ARM_DOT_K0(k0, (a), (b##7), (c.s7)); \
129 })
130#define ARM_DOT_K0X16(k0, a, b, c) \
131 ({ \
132 ARM_DOT_K0X8(k0, a, b, c); \
133 ARM_DOT_K0(k0, (a), (b##8), (c.s8)); \
134 ARM_DOT_K0(k0, (a), (b##9), (c.s9)); \
135 ARM_DOT_K0(k0, (a), (b##A), (c.sA)); \
136 ARM_DOT_K0(k0, (a), (b##B), (c.sB)); \
137 ARM_DOT_K0(k0, (a), (b##C), (c.sC)); \
138 ARM_DOT_K0(k0, (a), (b##D), (c.sD)); \
139 ARM_DOT_K0(k0, (a), (b##E), (c.sE)); \
140 ARM_DOT_K0(k0, (a), (b##F), (c.sF)); \
141 })
142
SiCong Li738893e2020-05-01 12:55:16 +0100143/** Specialized macros to perform a partial matrix multiplication with dimensions M0,N0,K0 */
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100144#define ARM_MM_K0XN0X1(n0, k0, a, b, c) \
145 ({ \
146 ARM_DOT_K0XN0(n0, k0, (a##0), b, (c##0)); \
147 })
148#define ARM_MM_K0XN0X2(n0, k0, a, b, c) \
149 ({ \
150 ARM_MM_K0XN0X1(n0, k0, a, b, c); \
151 ARM_DOT_K0XN0(n0, k0, (a##1), b, (c##1)); \
152 })
153#define ARM_MM_K0XN0X3(n0, k0, a, b, c) \
154 ({ \
155 ARM_MM_K0XN0X2(n0, k0, a, b, c); \
156 ARM_DOT_K0XN0(n0, k0, (a##2), b, (c##2)); \
157 })
158#define ARM_MM_K0XN0X4(n0, k0, a, b, c) \
159 ({ \
160 ARM_MM_K0XN0X3(n0, k0, a, b, c); \
161 ARM_DOT_K0XN0(n0, k0, (a##3), b, (c##3)); \
162 })
163#define ARM_MM_K0XN0X5(n0, k0, a, b, c) \
164 ({ \
165 ARM_MM_K0XN0X4(n0, k0, a, b, c); \
166 ARM_DOT_K0XN0(n0, k0, (a##4), b, (c##4)); \
167 })
168#define ARM_MM_K0XN0X6(n0, k0, a, b, c) \
169 ({ \
170 ARM_MM_K0XN0X5(n0, k0, a, b, c); \
171 ARM_DOT_K0XN0(n0, k0, (a##5), b, (c##5)); \
172 })
173#define ARM_MM_K0XN0X7(n0, k0, a, b, c) \
174 ({ \
175 ARM_MM_K0XN0X6(n0, k0, a, b, c); \
176 ARM_DOT_K0XN0(n0, k0, (a##6), b, (c##6)); \
177 })
178#define ARM_MM_K0XN0X8(n0, k0, a, b, c) \
179 ({ \
180 ARM_MM_K0XN0X7(n0, k0, a, b, c); \
181 ARM_DOT_K0XN0(n0, k0, (a##7), b, (c##7)); \
182 })
183
184#define ARM_DOT_K0(k0, a, b, c) \
185 ({ \
186 CONCAT(ARM_DOT, k0) \
187 ((a), (b), (c)); \
188 })
189
190#define ARM_DOT_K0XN0(n0, k0, a, b, c) \
191 ({ \
192 CONCAT(ARM_DOT_K0X, n0) \
193 (k0, (a), b, (c)); \
194 })
195
196#define ARM_MM_K0XN0XM0(m0, n0, k0, a, b, c) \
197 ({ \
198 CONCAT(ARM_MM_K0XN0X, m0) \
199 (n0, k0, a, b, c); \
200 })
201
SiCong Li738893e2020-05-01 12:55:16 +0100202/** Specialized macros to perform a broadcast dot product operation between one vector "a" and N0 vectors "b" of size K0 [1,16] */
203#define ARM_MUL_N0X1(VECTOR_ACC_TYPE, a, b, c) \
204 ({ \
205 c += CONVERT(b##0, VECTOR_ACC_TYPE) * a; \
206 })
207#define ARM_MUL_N0X2(VECTOR_ACC_TYPE, a, b, c) \
208 ({ \
209 c += CONVERT(b##0, VECTOR_ACC_TYPE) * a.s##0; \
210 c += CONVERT(b##1, VECTOR_ACC_TYPE) * a.s##1; \
211 })
212#define ARM_MUL_N0X3(VECTOR_ACC_TYPE, a, b, c) \
213 ({ \
214 ARM_MUL_N0X2(VECTOR_ACC_TYPE, a, b, c); \
215 c += CONVERT(b##2, VECTOR_ACC_TYPE) * a.s##2; \
216 })
217#define ARM_MUL_N0X4(VECTOR_ACC_TYPE, a, b, c) \
218 ({ \
219 ARM_MUL_N0X3(VECTOR_ACC_TYPE, a, b, c); \
220 c += CONVERT(b##3, VECTOR_ACC_TYPE) * a.s##3; \
221 })
222#define ARM_MUL_N0X8(VECTOR_ACC_TYPE, a, b, c) \
223 ({ \
224 ARM_MUL_N0X4(VECTOR_ACC_TYPE, a, b, c); \
225 c += CONVERT(b##4, VECTOR_ACC_TYPE) * a.s##4; \
226 c += CONVERT(b##5, VECTOR_ACC_TYPE) * a.s##5; \
227 c += CONVERT(b##6, VECTOR_ACC_TYPE) * a.s##6; \
228 c += CONVERT(b##7, VECTOR_ACC_TYPE) * a.s##7; \
229 })
230#define ARM_MUL_N0X16(VECTOR_ACC_TYPE, a, b, c) \
231 ({ \
232 ARM_MUL_N0X8(VECTOR_ACC_TYPE, a, b, c); \
233 c += CONVERT(b##8, VECTOR_ACC_TYPE) * a.s##8; \
234 c += CONVERT(b##9, VECTOR_ACC_TYPE) * a.s##9; \
235 c += CONVERT(b##A, VECTOR_ACC_TYPE) * a.s##A; \
236 c += CONVERT(b##B, VECTOR_ACC_TYPE) * a.s##B; \
237 c += CONVERT(b##C, VECTOR_ACC_TYPE) * a.s##C; \
238 c += CONVERT(b##D, VECTOR_ACC_TYPE) * a.s##D; \
239 c += CONVERT(b##E, VECTOR_ACC_TYPE) * a.s##E; \
240 c += CONVERT(b##F, VECTOR_ACC_TYPE) * a.s##F; \
241 })
242/** Specialized macros to perform a a partial matrix multiplication with dimensions M0,N0,K0 */
243#define ARM_MM_NATIVE_N0XK0X1(VECTOR_ACC_TYPE, k0, a, b, c) \
244 ({ \
245 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##0), b, (c##0)); \
246 })
247#define ARM_MM_NATIVE_N0XK0X2(VECTOR_ACC_TYPE, k0, a, b, c) \
248 ({ \
249 ARM_MM_NATIVE_N0XK0X1(VECTOR_ACC_TYPE, k0, a, b, c); \
250 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##1), b, (c##1)); \
251 })
252#define ARM_MM_NATIVE_N0XK0X3(VECTOR_ACC_TYPE, k0, a, b, c) \
253 ({ \
254 ARM_MM_NATIVE_N0XK0X2(VECTOR_ACC_TYPE, k0, a, b, c); \
255 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##2), b, (c##2)); \
256 })
257#define ARM_MM_NATIVE_N0XK0X4(VECTOR_ACC_TYPE, k0, a, b, c) \
258 ({ \
259 ARM_MM_NATIVE_N0XK0X3(VECTOR_ACC_TYPE, k0, a, b, c); \
260 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##3), b, (c##3)); \
261 })
262#define ARM_MM_NATIVE_N0XK0X5(VECTOR_ACC_TYPE, k0, a, b, c) \
263 ({ \
264 ARM_MM_NATIVE_N0XK0X4(VECTOR_ACC_TYPE, k0, a, b, c); \
265 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##4), b, (c##4)); \
266 })
267#define ARM_MM_NATIVE_N0XK0X6(VECTOR_ACC_TYPE, k0, a, b, c) \
268 ({ \
269 ARM_MM_NATIVE_N0XK0X5(VECTOR_ACC_TYPE, k0, a, b, c); \
270 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##5), b, (c##5)); \
271 })
272#define ARM_MM_NATIVE_N0XK0X7(VECTOR_ACC_TYPE, k0, a, b, c) \
273 ({ \
274 ARM_MM_NATIVE_N0XK0X6(VECTOR_ACC_TYPE, k0, a, b, c); \
275 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##6), b, (c##6)); \
276 })
277#define ARM_MM_NATIVE_N0XK0X8(VECTOR_ACC_TYPE, k0, a, b, c) \
278 ({ \
279 ARM_MM_NATIVE_N0XK0X7(VECTOR_ACC_TYPE, k0, a, b, c); \
280 ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, (a##7), b, (c##7)); \
281 })
282#define ARM_MUL_N0XK0(VECTOR_ACC_TYPE, k0, a, b, c) \
283 ({ \
284 CONCAT(ARM_MUL_N0X, k0) \
285 (VECTOR_ACC_TYPE, (a), b, (c)); \
286 })
287#define ARM_MM_NATIVE_N0XK0XM0(VECTOR_ACC_TYPE, m0, k0, a, b, c) \
288 ({ \
289 CONCAT(ARM_MM_NATIVE_N0XK0X, m0) \
290 (VECTOR_ACC_TYPE, k0, a, b, c); \
291 })
292
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000293#if defined(M0) && defined(N0) && defined(K0) && defined(V0) && defined(H0) && defined(M) && defined(N)
Sheri Zhang28287af2020-02-25 14:13:54 +0000294/** This OpenCL kernel computes the matrix multiplication between 2 matrices with QASYMM/QASYMM_SIGNED data type.
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000295 * The LHS matrix must be reshaped with @ref CLGEMMReshapeLHSMatrixKernel and the M0xK0 must be NOT transposed
296 * The RHS matrix must be reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the K0xN0 must be transposed
297 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000298 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
299 * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000300 * @note If the first two dimensions of NDRange have been dispatched with "dummy_work_items" support, the option -DDUMMY_WORK_ITEMS must be passed at compile time.
301 * @note The GEMM's dimensions M and N must be passed at compile time using -DM and -DN (i.e. -DM=52 and -DN=90).
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000302 * @note The block's dimensions used for reshaping the LHS matrix and the RHS matrix (M0, N0 and K0) must be passed at compile time using -DM0, -DN0 and -DK0 (i.e. -DM0=4, -DN0=8, -DK0=4).
303 * @note The number of M0xK0 vertical blocks stored on the same output row of the reshaped LHS matrix must be passed at compile time using -DV0 (i.e. -DV0=2)
304 * @note The number of K0xN0 horizontal blocks stored on the same output row of the reshaped RHS matrix must be passed at compile time using -DH0 (i.e. -DH0=2)
305 * @note If the M0xK0 blocks in the reshaped LHS matrix have been interleaved, the option -DLHS_INTERLEAVE must passed at compile time.
306 * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time.
307 * @note Only the following configurations of M0, N0 and K0 are currently supported:
308 * - M0 = 2, 3, 4, 5, 6, 7, 8
309 * - N0 = 2, 3, 4, 8, 16
310 * - K0 = 2, 3, 4, 8, 16
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000311 * - V0 >= 1
312 * - H0 >= 1
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000313 *
314 * @note In case the output has to be reinterpreted as a 3D tensor (i.e. output of convolution layer), the following information must be passed at compile time:
315 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
316 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
317 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
318 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix NOT reshaped
319 *
Sheri Zhang28287af2020-02-25 14:13:54 +0000320 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM_SIGNED
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000321 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
322 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
323 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
324 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
325 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
326 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
327 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
328 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
329 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
330 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
331 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
Sheri Zhang28287af2020-02-25 14:13:54 +0000332 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000333 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
334 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
335 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
336 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
337 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
338 * @param[in] k Number of columns in LHS matrix and rows in RHS matrix not reshaped.
339 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
340 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
341 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
342 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
343 */
344__kernel void gemmlowp_mm_reshaped_lhs_nt_rhs_t(IMAGE_DECLARATION(lhs),
345 IMAGE_DECLARATION(rhs),
346 IMAGE_DECLARATION(dst),
347 uint k,
348 uint lhs_stride_z,
349 uint rhs_stride_z,
350 uint dst_stride_z
351#if defined(REINTERPRET_OUTPUT_AS_3D)
352 ,
353 uint dst_cross_plane_pad
354#endif // REINTERPRET_OUTPUT_AS_3D
355 )
356{
357 // Block size
358#define LHS_BLOCK_SIZE ((K0) * (M0))
359
360#if defined(LHS_INTERLEAVE)
361#define LHS_OFFSET_X (K0)
362#define LHS_STEP_X ((K0) * (V0))
363#define LHS_STEP_LOOP (1)
364#else // defined(INTERLEAVE)
365#define LHS_OFFSET_X (LHS_BLOCK_SIZE)
366#define LHS_STEP_X (K0)
367#define LHS_STEP_LOOP (V0)
368#endif // defined(INTERLEAVE)
369
370 // Block size
371#define RHS_BLOCK_SIZE ((K0) * (N0))
372
373 // RHS offset and step X
374#if defined(RHS_INTERLEAVE)
375#define RHS_OFFSET_X (K0)
376#define RHS_STEP_X ((K0) * (H0))
377#define RHS_STEP_LOOP (1)
378#else // defined(RHS_INTERLEAVE)
379#define RHS_OFFSET_X (RHS_BLOCK_SIZE)
380#define RHS_STEP_X (K0)
381#define RHS_STEP_LOOP (H0)
382#endif // defined(RHS_INTERLEAVE)
383
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100384 uint x = get_global_id(0);
385 uint y = get_global_id(1);
386 uint z = get_global_id(2);
387
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000388#if defined(DUMMY_WORK_ITEMS)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100389 if((x * N0 >= N) || (y * M0 >= M))
Gian Marco Iodiceb0c50372019-03-15 10:13:05 +0000390 {
391 return;
392 }
393#endif // defined(DUMMY_WORK_ITEMS)
394
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000395 // Compute LHS matrix address
Sheri Zhang28287af2020-02-25 14:13:54 +0000396 __global DATA_TYPE *lhs_addr = lhs_ptr + lhs_offset_first_element_in_bytes + (y % V0) * (uint)LHS_OFFSET_X + (y / V0) * (uint)lhs_stride_y + (z * lhs_stride_z);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000397
398 // Compute RHS matrix address
Sheri Zhang28287af2020-02-25 14:13:54 +0000399 __global DATA_TYPE *rhs_addr = rhs_ptr + rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000400
401#if defined(MATRIX_B_DEPTH)
402 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100403 rhs_addr += (z % MATRIX_B_DEPTH) * rhs_stride_z;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000404#else // defined(MATRIX_B_DEPTH)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100405 rhs_addr += z * rhs_stride_z;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000406#endif // defined(MATRIX_B_DEPTH)
407
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100408 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
409 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
410
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000411 // Initialize the accumulators
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000412 REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(ACC_DATA_TYPE, N0), c, 0); //VEC_DATA_TYPE(ACC_DATA_TYPE, N0) c0=0,c1=0,c2=0,... c(M0-1)=0;
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000413
414 for(int i = 0; i < k; i += K0)
415 {
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000416 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000417 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_addr, 0, LHS_STEP_X, zlhs);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000418
419 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000420 LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_addr, 0, RHS_STEP_X, zrhs);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000421
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100422 // Partial matrix multiplication M0,N0,K0
423 ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000424
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100425 // Update address
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000426 lhs_addr += (M0 * LHS_STEP_X * LHS_STEP_LOOP);
427 rhs_addr += (N0 * RHS_STEP_X * RHS_STEP_LOOP);
428 }
429
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100430 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0 * sizeof(int)) + (y * (uint)M0 * dst_stride_y);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000431
432 REPEAT_VAR_INIT_TO_CONST(8, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
433
434#if defined(REINTERPRET_OUTPUT_AS_3D)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100435 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
436 CALCULATE_Z_OFFSET(M0, uint, zout, y, 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
450 CONVERT_STORE_BLOCK(M0, N0, int, c, dst_addr, dst_stride_y, zout);
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000451
452#undef LHS_BLOCK_SIZE
453#undef LHS_OFFSET_X
454#undef LHS_STEP_X
455#undef RHS_BLOCK_SIZE
456#undef RHS_OFFSET_X
457#undef RHS_STEP_X
458}
Gian Marco Iodicedb63b9c2019-01-17 09:47:04 +0000459#endif // defined(M0) && defined(N0) && defined(K0) && defined(V0) && defined(H0) && defined(K)
460
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000461#if defined(M0) && defined(N0) && defined(K0) && defined(H0) && defined(K)
462
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000463/** This OpenCL kernel computes the matrix multiplication between 2 matrices.
464 * The LHS matrix is NOT reshaped
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100465 * The RHS matrix is reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the block K0xN0 is transposed
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000466 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000467 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
468 * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000469 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
470 * @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).
471 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
472 * @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)
473 * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time.
474 * @note Only the following configurations of M0, N0 and K0 are currently supported:
475 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
476 * - N0 = 2, 3, 4, 8, 16
477 * - K0 = 2, 3, 4, 8, 16
478 * - H0 >= 1
479 *
480 * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time:
481 * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D
482 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
483 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
484 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
485 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix
486 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000487 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000488 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
489 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
490 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
491 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
492 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
493 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
494 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
495 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
496 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
497 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
498 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000499 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000500 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
501 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
502 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
503 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
504 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
505 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
506 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
507 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
508 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
509 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
510 */
511__kernel void gemmlowp_mm_reshaped_only_rhs_t(IMAGE_DECLARATION(lhs),
512 IMAGE_DECLARATION(rhs),
513 IMAGE_DECLARATION(dst),
514 uint lhs_stride_z,
515 uint rhs_stride_z,
516 uint dst_stride_z
517#if defined(REINTERPRET_INPUT_AS_3D)
518 ,
519 uint lhs_cross_plane_pad
520#endif // REINTERPRET_INPUT_AS_3D
521#if defined(REINTERPRET_OUTPUT_AS_3D)
522 ,
523 uint dst_cross_plane_pad
524#endif // REINTERPRET_OUTPUT_AS_3D
525 )
526{
527 // Block size
528#define RHS_BLOCK_SIZE ((K0) * (N0))
529
530 // RHS offset and step X
531#if defined(RHS_INTERLEAVE)
532#define RHS_OFFSET_X (K0)
533#define RHS_STEP_X ((K0) * (H0))
534#define RHS_STEP_LOOP (1)
535#else // defined(RHS_INTERLEAVE)
536#define RHS_OFFSET_X (RHS_BLOCK_SIZE)
537#define RHS_STEP_X (K0)
538#define RHS_STEP_LOOP (H0)
539#endif // defined(RHS_INTERLEAVE)
540
541 uint x = get_global_id(0);
542 uint y = get_global_id(1);
543 uint z = get_global_id(2);
544
Gian Marco Iodice86cfffe2019-04-02 11:02:20 +0100545#if defined(DUMMY_WORK_ITEMS)
546 if((x * N0 >= N) || (y * M0 >= M))
547 {
548 return;
549 }
550#endif // defined(DUMMY_WORK_ITEMS)
551
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000552 // Compute LHS matrix address
553 uint lhs_offset = lhs_offset_first_element_in_bytes + y * M0 * (uint)lhs_stride_y;
554
555 // Compute RHS matrix address
556 uint rhs_offset = rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y;
557
558#if defined(MATRIX_B_DEPTH)
559 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
560 rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z;
561#else // defined(MATRIX_B_DEPTH)
562 rhs_offset += z * rhs_stride_z;
563#endif // defined(MATRIX_B_DEPTH)
564
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100565 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
566 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000567
568#if defined(REINTERPRET_INPUT_AS_3D)
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100569 // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
570 CALCULATE_Z_OFFSET(M0, uint, zlhs, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000571
572 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
573 // multiply lhs_stride_z by DEPTH_GEMM3D
574 lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D;
575
576#else // defined(REINTERPRET_INPUT_AS_3D)
577
578 // Add offset for batched GEMM
579 lhs_offset += z * lhs_stride_z;
580
581#endif // defined(REINTERPRET_INPUT_AS_3D)
582
583 // Initialize the accumulators
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000584 REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(ACC_DATA_TYPE, N0), c, 0); //VEC_DATA_TYPE(ACC_DATA_TYPE, N0) c0=0,c1=0,c2=0,... c(N0-1)=0;
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000585
586 for(int i = 0; i < K; i += K0)
587 {
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000588 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000589 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000590
591 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000592 LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_ptr, rhs_offset, RHS_STEP_X, zrhs);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000593
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100594 // Partial matrix multiplication M0,N0,K0
595 ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000596
597 lhs_offset += K0;
598 rhs_offset += N0 * RHS_STEP_X * RHS_STEP_LOOP;
599 }
600
601 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int) + (y * (uint)M0 * dst_stride_y);
602
603 REPEAT_VAR_INIT_TO_CONST(8, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
604
605#if defined(REINTERPRET_OUTPUT_AS_3D)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000606 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100607 CALCULATE_Z_OFFSET(M0, uint, zout, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000608
609 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
610 // multiply dst_stride_z by DEPTH_GEMM3D
611 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
612
613#else // defined(REINTERPRET_OUTPUT_AS_3D)
614
615 // Add offset for batched GEMM
616 dst_addr += z * dst_stride_z;
617
618#endif // defined(REINTERPRET_OUTPUT_AS_3D)
619
Gian Marco Iodice43a129e2019-05-14 10:14:08 +0100620 // Convert and store output block
621 CONVERT_STORE_BLOCK(M0, N0, int, c, dst_addr, dst_stride_y, zout);
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000622
623#undef RHS_BLOCK_SIZE
624#undef RHS_OFFSET_X
625#undef RHS_STEP_X
626}
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000627
628#if defined(RESULT_OFFSET) && defined(RESULT_SHIFT) && defined(RESULT_MULTIPLIER)
629/** This OpenCL kernel computes the matrix multiplication between 2 matrices with fused output stage using fixed-point arithmetic.
630 * The LHS matrix is NOT reshaped
631 * The RHS matrix is reshaped with @ref CLGEMMReshapeRHSMatrixKernel and the block K0xN0 is transposed
632 *
633 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
634 * @note The accumulator data type must be passed at compile time using -DACC_DATA_TYPE (i.e. -DACC_DATA_TYPE=uint)
635 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
636 * @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).
637 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
638 * @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)
639 * @note If the K0xN0 blocks in the reshaped RHS matrix have been interleaved, the option -DRHS_INTERLEAVE must passed at compile time.
640 * @note Only the following configurations of M0, N0 and K0 are currently supported:
641 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
642 * - N0 = 2, 3, 4, 8, 16
643 * - K0 = 2, 3, 4, 8, 16
644 * - H0 >= 1
645 *
646 * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time:
647 * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D
648 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
649 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
650 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
651 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix
652 *
653 * @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
654 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
655 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
656 * @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.
657 * These values can be used to implement "rectified linear unit" activation functions
658 * @note In case of per-channel quantization of matrix B, -DPER_CHANNEL_QUANTIZATION must be passed at compile time.
659 *
660 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8/QASYMM8_SIGNED
661 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
662 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
663 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
664 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
665 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
666 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
667 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
668 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
669 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
670 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
671 * @param[in] rhs_offset_first_element_in_bytes The offset of the first element in the RHS reshaped matrix
672 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: same as @p lhs_ptr
673 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
674 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
675 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
676 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
677 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
678 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
679 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
680 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
681 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
682 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
683 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: S32
684 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
685 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
686 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
687 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
688 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
689 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: S32
690 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
691 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
692 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
693 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
694 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
695 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: S32
696 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
697 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
698 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
699 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
700 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
701 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
702 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
703 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
704 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
705 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
706 * @param[in] result_shifts_offset_first_element_in_bytes (Optional) The offset of the first element in the output shifts vector
707 */
708__kernel void gemmlowp_mm_reshaped_only_rhs_t_fused_output_stage_fixedpoint(IMAGE_DECLARATION(lhs),
709 IMAGE_DECLARATION(rhs),
710 IMAGE_DECLARATION(dst),
711 uint lhs_stride_z,
712 uint rhs_stride_z,
713 uint dst_stride_z
714#if defined(REINTERPRET_INPUT_AS_3D)
715 ,
716 uint lhs_cross_plane_pad
717#endif // REINTERPRET_INPUT_AS_3D
718#if defined(REINTERPRET_OUTPUT_AS_3D)
719 ,
720 uint dst_cross_plane_pad
721#endif // REINTERPRET_OUTPUT_AS_3D
722#if defined(A_OFFSET)
723 ,
724 IMAGE_DECLARATION(sum_col)
725#endif // defined(A_OFFSET)
726#if defined(B_OFFSET)
727 ,
728 IMAGE_DECLARATION(sum_row)
729#endif // defined(B_OFFSET)
730#if defined(ADD_BIAS)
731 ,
732 VECTOR_DECLARATION(biases)
733#endif // defined(ADD_BIAS)
734#if defined(PER_CHANNEL_QUANTIZATION)
735 ,
736 VECTOR_DECLARATION(result_multipliers),
737 VECTOR_DECLARATION(result_shifts)
738#endif // defined(PER_CHANNEL_QUANTIZATION)
739 )
740{
741 // Block size
742#define RHS_BLOCK_SIZE ((K0) * (N0))
743
744 // RHS offset and step X
745#if defined(RHS_INTERLEAVE)
746#define RHS_OFFSET_X (K0)
747#define RHS_STEP_X ((K0) * (H0))
748#define RHS_STEP_LOOP (1)
749#else // defined(RHS_INTERLEAVE)
750#define RHS_OFFSET_X (RHS_BLOCK_SIZE)
751#define RHS_STEP_X (K0)
752#define RHS_STEP_LOOP (H0)
753#endif // defined(RHS_INTERLEAVE)
754
755 uint x = get_global_id(0);
756 uint y = get_global_id(1);
757 uint z = get_global_id(2);
758
759#if defined(DUMMY_WORK_ITEMS)
760 if((x * N0 >= N) || (y * M0 >= M))
761 {
762 return;
763 }
764#endif // defined(DUMMY_WORK_ITEMS)
765
766 // Compute LHS matrix address
767 uint lhs_offset = lhs_offset_first_element_in_bytes + y * M0 * (uint)lhs_stride_y;
768
769 // Compute RHS matrix address
770 uint rhs_offset = rhs_offset_first_element_in_bytes + (x % H0) * (uint)RHS_OFFSET_X + (x / (uint)H0) * rhs_stride_y;
771
772#if defined(MATRIX_B_DEPTH)
773 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
774 rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z;
775#else // defined(MATRIX_B_DEPTH)
776 rhs_offset += z * rhs_stride_z;
777#endif // defined(MATRIX_B_DEPTH)
778
779 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
780 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
781
782#if defined(REINTERPRET_INPUT_AS_3D)
783 // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
784 CALCULATE_Z_OFFSET(M0, uint, zlhs, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y);
785
786 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
787 // multiply lhs_stride_z by DEPTH_GEMM3D
788 lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D;
789
790#else // defined(REINTERPRET_INPUT_AS_3D)
791
792 // Add offset for batched GEMM
793 lhs_offset += z * lhs_stride_z;
794
795#endif // defined(REINTERPRET_INPUT_AS_3D)
796
797 // Initialize the accumulators
798 REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(ACC_DATA_TYPE, N0), c, 0); //VEC_DATA_TYPE(ACC_DATA_TYPE, N0) c0=0,c1=0,c2=0,... c(N0-1)=0;
799
800 for(int i = 0; i < K; i += K0)
801 {
802 // Load values from LHS matrix
803 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
804
805 // Load values from RHS matrix
806 LOAD_BLOCK(N0, K0, DATA_TYPE, b, rhs_ptr, rhs_offset, RHS_STEP_X, zrhs);
807
808 // Partial matrix multiplication M0,N0,K0
809 ARM_MM_K0XN0XM0(M0, N0, K0, a, b, c);
810
811 lhs_offset += K0;
812 rhs_offset += N0 * RHS_STEP_X * RHS_STEP_LOOP;
813 }
814
815 // Result of MM is of type DATA_TYPE
816 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(DATA_TYPE) + (y * (uint)M0 * dst_stride_y);
817
818 REPEAT_VAR_INIT_TO_CONST(8, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
819
820#if defined(REINTERPRET_OUTPUT_AS_3D)
821 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
822 CALCULATE_Z_OFFSET(M0, uint, zout, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
823
824 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
825 // multiply dst_stride_z by DEPTH_GEMM3D
826 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
827
828#else // defined(REINTERPRET_OUTPUT_AS_3D)
829
830 // Add offset for batched GEMM
831 dst_addr += z * dst_stride_z;
832
833#endif // defined(REINTERPRET_OUTPUT_AS_3D)
834
835 // Convert result of matrix multiplication to S32
836 REPEAT_VAR_INIT_CONVERT_SAT(M0, VEC_DATA_TYPE(int, N0), c, c_int);
837
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000838 // Offset contribution: c += (A_OFFSET * sum_col) + (B_OFFSET * sum_row) + K_OFFSET;
839 REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(int, N0), offset_s32_, K_OFFSET);
840
841#if defined(A_OFFSET)
842 // Compute the offset contribution due to A_OFFSET
843 __global uchar *sum_col_addr = sum_col_ptr + sum_col_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
844
845#if defined(SUM_COL_HAS_BATCHES)
846 sum_col_addr += z * sum_col_stride_y;
847#endif // defined(SUM_COL_HAS_BATCHES)
848 VEC_DATA_TYPE(int, N0)
849 a_offset_s32 = VLOAD(N0)(0, (__global int *)sum_col_addr);
850 a_offset_s32 *= (VEC_DATA_TYPE(int, N0))A_OFFSET;
851
852 REPEAT_ADD_VECTOR_TO_VAR(M0, offset_s32_, a_offset_s32);
853#endif // defined(A_OFFSET)
854
855#if defined(B_OFFSET)
856 // Compute the offset contribution due to B_OFFSET
Gian Marco Iodice27423f02020-08-12 14:12:28 +0100857 // Note: The sum_row tensor is generated through CLGEMMLowpMatrixAReductionKernel which
858 // does not introduce paddings. For this reason is safe to access the tensor in this manner
859 // without considering that the coordinate "y" could come from an input 3D tensor
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000860 __global uchar *sum_row_addr = sum_row_ptr + sum_row_offset_first_element_in_bytes + (y * (uint)M0) * sizeof(int) + z * sum_row_stride_y;
861
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000862 LOAD_SCALAR_AS_VECTOR(M0, N0, int, b_offset_s32_, sum_row_addr, 0, sum_row_stride_x);
863
864 REPEAT_MLA_VAR_WITH_CONST_VEC(M0, offset_s32_, b_offset_s32_, (VEC_DATA_TYPE(int, N0))B_OFFSET);
865#endif // defined(B_OFFSET)
866
867#if defined(ADD_BIAS)
868 // Add bias
869 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
870
871 VEC_DATA_TYPE(int, N0)
872 bias_values = VLOAD(N0)(0, (__global int *)bias_addr);
873 REPEAT_ADD_VECTOR_TO_VAR(M0, offset_s32_, bias_values);
874#endif // defined(ADD_BIAS)
875
876 REPEAT_ADD_TWO_VARS(M0, c_int, offset_s32_);
877
878 // Multiply by result_mult_int and shift
879#if defined(PER_CHANNEL_QUANTIZATION)
880 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
881 __global uchar *result_shifts_addr = result_shifts_ptr + result_shifts_offset_first_element_in_bytes + (x * (uint)N0) * sizeof(int);
882
883 VEC_DATA_TYPE(int, N0)
884 res_mul = VLOAD(N0)(0, (__global int *)result_multipliers_addr);
885 VEC_DATA_TYPE(int, N0)
886 res_shift = VLOAD(N0)(0, (__global int *)result_shifts_addr);
887
888 REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_PER_CHANNEL(M0, N0, c_int, res_mul, res_shift);
889#else // defined(PER_CHANNEL_QUANTIZATION)
890
891#if RESULT_SHIFT < 0
892 REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(M0, N0, c_int, RESULT_MULTIPLIER, RESULT_SHIFT);
893#else // RESULT_SHIFT >= 0
894 REPEAT_ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(M0, N0, c_int, RESULT_MULTIPLIER, RESULT_SHIFT);
895#endif // RESULT_SHIFT < 0
896
897#endif // defined(PER_CHANNEL_QUANTIZATION)
898
899 // Add the offset terms to GEMM's result
900 REPEAT_ADD_CONST_TO_VAR(M0, VEC_DATA_TYPE(int, N0), c_int, RESULT_OFFSET);
901
902#if defined(MIN_BOUND)
903 REPEAT_MAX_CONST_VAR(M0, VEC_DATA_TYPE(int, N0), c_int, MIN_BOUND);
904#endif // defined(MIN_BOUND)
905#if defined(MAX_BOUND)
906 REPEAT_MIN_CONST_VAR(M0, VEC_DATA_TYPE(int, N0), c_int, MAX_BOUND);
907#endif // defined(MAX_BOUND)
908
909 // Convert and store output block (does convert saturate)
910 CONVERT_STORE_BLOCK(M0, N0, DATA_TYPE, c_int, dst_addr, dst_stride_y, zout);
911
912#undef RHS_BLOCK_SIZE
913#undef RHS_OFFSET_X
914#undef RHS_STEP_X
915}
916#endif // defined(RESULT_OFFSET) && defined(RESULT_SHIFT) && defined(RESULT_MULTIPLIER)
Gian Marco Iodice62251f72019-03-11 16:07:12 +0000917#endif // defined(M0) && defined(N0) && defined(K0) && defined(H0) && defined(DATA_TYPE) && defined(K)
918
SiCong Lied5fb392020-10-20 18:07:27 +0100919#if defined(M0) && defined(N0) && defined(K0) && defined(K) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0)
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100920
921/** This OpenCL kernel computes the matrix multiplication between 2 matrices.
922 * The LHS matrix is NOT reshaped
923 * The RHS matrix is NOT reshaped
924 *
Michele Di Giorgiof9179d32019-11-27 16:17:30 +0000925 * @note The input data type must be passed at compile time using -DDATA_TYPE (i.e. -DDATA_TYPE=uchar)
926 * @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 +0100927 * @note The number of columns of LHS matrix must be passed at compile time using -DK (i.e. -DK=64)
928 * @note The number of M0 rows to process must be passed at compile time using -DM0 (i.e. -DM0=2)
929 * @note The number of N0 columns to process must be passed at compile time using -DN0 (i.e. -DN0=2)
930 * @note The number of K0 partial accumulations must be passed at compile time using -DK0 (i.e., -DK0=2)
931 * @note Only the following configurations of M0, N0 and K0 are currently supported:
932 * - M0 = 1, 2, 3, 4, 5, 6, 7, 8
933 * - N0 = 2, 3, 4, 8, 16
934 * - K0 = 2, 3, 4, 8, 16
935 *
936 * @note In case the input or output have to be reinterpreted as a 3D tensor, the following information must be passed at compile time:
937 * -# REINTERPRET_INPUT_AS_3D: To reinterpret the input as 3D
938 * -# REINTERPRET_OUTPUT_AS_3D: To reinterpret the output as 3D
939 * -# HEIGHT_GEMM3D: The height of the output in case it has to be reinterpreted as a 3D tensor.
940 * -# DEPTH_GEMM3D: The depth of the output in case it has to be reinterpreted as a 3D tensor
941 * (HEIGHT_GEMM3D * DEPTH_GEMM3D) = columns LHS matrix
942 *
Michele Di Giorgiob54ba282020-01-14 15:31:55 +0000943 * @param[in] lhs_ptr Pointer to the LHS reshaped matrix. Supported data type: QASYMM8
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100944 * @param[in] lhs_stride_x Stride of the LHS reshaped matrix in X dimension (in bytes)
945 * @param[in] lhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
946 * @param[in] lhs_stride_y Stride of the LHS reshaped matrix in Y dimension (in bytes)
947 * @param[in] lhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
948 * @param[in] lhs_offset_first_element_in_bytes The offset of the first element in the LHS reshaped matrix
949 * @param[in] rhs_ptr Pointer to the RHS reshaped matrix. Supported data type: same as @p lhs_ptr
950 * @param[in] rhs_stride_x Stride of the RHS reshaped matrix in X dimension (in bytes)
951 * @param[in] rhs_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
952 * @param[in] rhs_stride_y Stride of the RHS reshaped matrix in Y dimension (in bytes)
953 * @param[in] rhs_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
954 * @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 +0000955 * @param[out] dst_ptr Pointer to the destination matrix Supported data type: S32
Gian Marco Iodicee7510622019-06-03 17:28:17 +0100956 * @param[in] dst_stride_x Stride of the destination matrix in X dimension (in bytes)
957 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
958 * @param[in] dst_stride_y Stride of the destination matrix in Y dimension (in bytes)
959 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
960 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination matrix
961 * @param[in] lhs_stride_z Stride of the LHS reshaped matrix in Z dimension (in bytes)
962 * @param[in] rhs_stride_z Stride of the RHS reshaped matrix in Z dimension (in bytes)
963 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
964 * @param[in] lhs_cross_plane_pad (Optional) Bottom paddings for LHS matrix in unit of elements (only if defined REINTERPRET_INPUT_AS_3D)
965 * @param[in] dst_cross_plane_pad (Optional) Bottom paddings for the output matrix in unit of elements (only if defined REINTERPRET_OUTPUT_AS_3D)
966 */
967__kernel void gemmlowp_mm_native(IMAGE_DECLARATION(lhs),
968 IMAGE_DECLARATION(rhs),
969 IMAGE_DECLARATION(dst),
970 uint lhs_stride_z,
971 uint rhs_stride_z,
972 uint dst_stride_z
973#if defined(REINTERPRET_INPUT_AS_3D)
974 ,
975 uint lhs_cross_plane_pad
976#endif // REINTERPRET_INPUT_AS_3D
977#if defined(REINTERPRET_OUTPUT_AS_3D)
978 ,
979 uint dst_cross_plane_pad
980#endif // REINTERPRET_OUTPUT_AS_3D
981 )
982{
983 uint x = get_global_id(0);
984 uint y = get_global_id(1);
985 uint z = get_global_id(2);
986
987#if defined(DUMMY_WORK_ITEMS)
988 if((x * N0 >= N) || (y * M0 >= M))
989 {
990 return;
991 }
992#endif // defined(DUMMY_WORK_ITEMS)
993
994 // Compute LHS matrix address
morgolockcf343e32020-10-12 14:00:43 +0100995 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 +0100996
997 // Compute RHS matrix address
morgolockcf343e32020-10-12 14:00:43 +0100998 uint rhs_offset = rhs_offset_first_element_in_bytes + x * N0 * sizeof(DATA_TYPE);
999
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001000#if defined(MATRIX_B_DEPTH)
1001 // Do not slide matrix B if the matrix B has 3 dimensions and matrix A more than 3
1002 rhs_offset += (z % MATRIX_B_DEPTH) * rhs_stride_z;
1003#else // defined(MATRIX_B_DEPTH)
1004 rhs_offset += z * rhs_stride_z;
1005#endif // defined(MATRIX_B_DEPTH)
1006
1007 REPEAT_VAR_INIT_TO_CONST(8, uint, zlhs, 0);
1008 REPEAT_VAR_INIT_TO_CONST(16, uint, zrhs, 0);
1009
1010#if defined(REINTERPRET_INPUT_AS_3D)
1011 // The plane (zlhs) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
1012 CALCULATE_Z_OFFSET(M0, uint, zlhs, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, lhs_cross_plane_pad, lhs_stride_y);
1013
1014 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
1015 // multiply lhs_stride_z by DEPTH_GEMM3D
1016 lhs_offset += z * lhs_stride_z * DEPTH_GEMM3D;
1017
1018#else // defined(REINTERPRET_INPUT_AS_3D)
1019
1020 // Add offset for batched GEMM
1021 lhs_offset += z * lhs_stride_z;
1022
1023#endif // defined(REINTERPRET_INPUT_AS_3D)
1024
1025 // Initialize the accumulators
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001026 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 +01001027
1028 int i = 0;
1029
1030 for(; i <= (K - K0); i += K0)
1031 {
1032 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001033 LOAD_BLOCK(M0, K0, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001034
1035 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001036 LOAD_BLOCK(K0, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001037
SiCong Li738893e2020-05-01 12:55:16 +01001038 // Partial matrix multiplication M0,N0,K0
1039#if(GPU_ARCH == GPU_ARCH_MIDGARD)
1040 ARM_MM_NATIVE_N0XK0XM0(VEC_DATA_TYPE(ACC_DATA_TYPE, N0), M0, K0, a, b, c);
1041#else // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001042 // Transpose the values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001043 TRANSPOSE_K0XN0(K0, N0, b_t, b, DATA_TYPE);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001044
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001045 ARM_MM_K0XN0XM0(M0, N0, K0, a, b_t, c);
SiCong Li738893e2020-05-01 12:55:16 +01001046#endif // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001047
1048 // Update the offset
1049 lhs_offset += K0;
1050 rhs_offset += K0 * rhs_stride_y;
1051 }
1052
1053 // Left-over for loop
1054 for(; i < K; ++i)
1055 {
1056 // Load values from LHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001057 LOAD_BLOCK(M0, 1, DATA_TYPE, a, lhs_ptr, lhs_offset, lhs_stride_y, zlhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001058
1059 // Load values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001060 LOAD_BLOCK(1, N0, DATA_TYPE, b, rhs_ptr, rhs_offset, rhs_stride_y, zrhs);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001061
SiCong Li738893e2020-05-01 12:55:16 +01001062 // Partial matrix multiplication M0,N0,1
1063#if(GPU_ARCH == GPU_ARCH_MIDGARD)
1064 ARM_MM_NATIVE_N0XK0XM0(VEC_DATA_TYPE(ACC_DATA_TYPE, N0), M0, 1, a, b, c);
1065#else // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001066 // Transpose the values from RHS matrix
Michele Di Giorgiof9179d32019-11-27 16:17:30 +00001067 TRANSPOSE_K0XN0(1, N0, b_t, b, DATA_TYPE);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001068
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001069 ARM_MM_K0XN0XM0(M0, N0, 1, a, b_t, c);
SiCong Li738893e2020-05-01 12:55:16 +01001070#endif // GPU_ARCH == GPU_ARCH_MIDGARD
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001071
1072 // Update the offset
1073 lhs_offset += 1;
1074 rhs_offset += rhs_stride_y;
1075 }
1076
morgolockcf343e32020-10-12 14:00:43 +01001077 __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);
1078
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001079 REPEAT_VAR_INIT_TO_CONST(M0, uint, zout, 0); //uint zout0=0,zout1=0,zout2=0,... zout7=0;
1080
1081#if defined(REINTERPRET_OUTPUT_AS_3D)
1082 // The plane (zout) is calculated dividing M (y * M0) by HEIGHT_GEMM3D
1083 CALCULATE_Z_OFFSET(M0, uint, zout, y, HEIGHT_GEMM3D, DEPTH_GEMM3D, dst_cross_plane_pad, dst_stride_y);
1084
1085 // Add offset for batched GEMM. The batches will be in the fourth dimension and for this reason we
1086 // multiply dst_stride_z by DEPTH_GEMM3D
1087 dst_addr += z * dst_stride_z * DEPTH_GEMM3D;
1088
1089#else // defined(REINTERPRET_OUTPUT_AS_3D)
1090
1091 // Add offset for batched GEMM
1092 dst_addr += z * dst_stride_z;
1093
1094#endif // defined(REINTERPRET_OUTPUT_AS_3D)
morgolockcf343e32020-10-12 14:00:43 +01001095 const bool cond_y = y == 0;
1096 const bool cond_x = ((x + 1) * N0 >= N);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001097
SiCong Lied5fb392020-10-20 18:07:27 +01001098 // Store output block
Giorgio Arena1e2af2a2020-10-15 17:39:41 +01001099 STORE_BLOCK_BOUNDARY_AWARE(M0, N0, int, c, dst_addr, dst_stride_y, zout, PARTIAL_STORE_M0, PARTIAL_STORE_N0, cond_y, cond_x);
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001100}
SiCong Lied5fb392020-10-20 18:07:27 +01001101#endif // defined(M0) && defined(N0) && defined(K0) && defined(K) && defined(PARTIAL_STORE_M0) && defined(PARTIAL_STORE_N0)
Gian Marco Iodicee7510622019-06-03 17:28:17 +01001102
Gian Marco05288a22017-11-21 10:57:50 +00001103#if defined(COLS_A)
1104/** 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 +01001105 * 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 +00001106 *
1107 * @note This stage is needed to handle the offset of matrix product
1108 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1109 *
1110 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
Manuel Bottini959c26d2019-12-02 16:22:35 +00001111 * @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 +00001112 * @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 +01001113 * @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 +00001114 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +01001115 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8/QASYMM8_SIGNED/QSYMM8
Gian Marco05288a22017-11-21 10:57:50 +00001116 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1117 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1118 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1119 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1120 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1121 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1122 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1123 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1124 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1125 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1126 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1127 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1128 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1129 */
1130__kernel void gemmlowp_matrix_a_reduction(TENSOR3D_DECLARATION(src),
1131 IMAGE_DECLARATION(dst))
1132{
1133 // Compute source and destination addresses
1134 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1135 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1136
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001137 VEC_DATA_TYPE(ACC_DATA_TYPE, 4)
1138 sum_row_32 = (VEC_DATA_TYPE(ACC_DATA_TYPE, 4))0;
1139 ACC_DATA_TYPE sum_row = 0;
Gian Marco05288a22017-11-21 10:57:50 +00001140
Manuel Bottini959c26d2019-12-02 16:22:35 +00001141 __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 +00001142
1143 int i = 0;
1144
1145 // This for loop performs 16 accumulations
1146 for(; i <= ((int)COLS_A - 16); i += 16)
1147 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001148 const VEC_DATA_TYPE(DATA_TYPE, 16) a0 = vload16(0, matrix_a + i);
Gian Marco05288a22017-11-21 10:57:50 +00001149
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001150 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,
1151 VEC_DATA_TYPE(ACC_DATA_TYPE, 4));
Gian Marco05288a22017-11-21 10:57:50 +00001152 }
1153
1154 // This for loop performs the leftover accumulations
1155 for(; i < COLS_A; ++i)
1156 {
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001157 sum_row += (ACC_DATA_TYPE)matrix_a[i];
Gian Marco05288a22017-11-21 10:57:50 +00001158 }
1159
Manuel Bottini959c26d2019-12-02 16:22:35 +00001160 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 +00001161
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001162#if defined(SCALAR)
1163 sum_row *= (int)SCALAR;
1164#endif // defined(SCALAR)
Gian Marco05288a22017-11-21 10:57:50 +00001165 *((__global int *)dst.ptr) = (int)sum_row;
1166}
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001167
1168#if defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001169/** 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.
1170 * 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 +01001171 *
1172 * @note This stage is needed to handle the offset of matrix product
1173 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1174 *
1175 * @attention The number of matrix A columns needs to be passed at compile time using -DCOLS_A
Manuel Bottini959c26d2019-12-02 16:22:35 +00001176 * @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 +00001177 * @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 +01001178 * @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 +01001179 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +01001180 * @param[in] src_ptr Pointer to the source tensor. Supported data type: QASYMM8/QASYMM8_SIGNED/QSYMM8
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001181 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1182 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1183 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1184 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1185 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1186 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1187 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1188 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1189 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1190 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1191 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1192 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1193 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1194 */
1195__kernel void gemmlowp_matrix_a_reduction_dot8(TENSOR3D_DECLARATION(src),
1196 IMAGE_DECLARATION(dst))
1197{
1198 // Compute source and destination addresses
1199 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
1200 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
1201
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001202 ACC_DATA_TYPE sum_row = 0;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001203
Manuel Bottini959c26d2019-12-02 16:22:35 +00001204 __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 +01001205
1206 int i = 0;
1207
1208 // This for loop performs 16 accumulations
1209 for(; i <= ((int)COLS_A - 32); i += 32)
1210 {
Manuel Bottini959c26d2019-12-02 16:22:35 +00001211 VEC_DATA_TYPE(DATA_TYPE, 16)
1212 a0 = vload16(0, matrix_a + i);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001213
Manuel Bottini959c26d2019-12-02 16:22:35 +00001214 sum_row += arm_dot(a0.s0123, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1215 sum_row += arm_dot(a0.s4567, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1216 sum_row += arm_dot(a0.s89AB, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1217 sum_row += arm_dot(a0.sCDEF, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001218
Manuel Bottini959c26d2019-12-02 16:22:35 +00001219 a0 = vload16(1, matrix_a + i);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001220
Manuel Bottini959c26d2019-12-02 16:22:35 +00001221 sum_row += arm_dot(a0.s0123, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1222 sum_row += arm_dot(a0.s4567, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1223 sum_row += arm_dot(a0.s89AB, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
1224 sum_row += arm_dot(a0.sCDEF, (VEC_DATA_TYPE(DATA_TYPE, 4))(1));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001225 }
1226
1227 // This for loop performs the leftover accumulations
1228 for(; i < COLS_A; ++i)
1229 {
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001230 sum_row += (ACC_DATA_TYPE)matrix_a[i];
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001231 }
1232
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001233#if defined(SCALAR)
1234 sum_row *= (int)SCALAR;
1235#endif // defined(SCALAR)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001236 *((__global int *)dst.ptr) = (int)sum_row;
1237}
1238#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Gian Marco05288a22017-11-21 10:57:50 +00001239#endif // defined(COLS_A)
1240
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001241#if defined(COLS_B) && defined(ROWS_B) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
Gian Marco05288a22017-11-21 10:57:50 +00001242/** 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 +01001243 * 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 +00001244 *
1245 * @note This stage is needed to handle the offset of matrix product
1246 * https://github.com/google/gemmlowp/blob/master/doc/low-precision.md
1247 *
1248 * @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 +00001249 * @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 +00001250 * @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 +01001251 * @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 +01001252 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1253 * @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 +00001254 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +01001255 * @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 +00001256 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1257 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1258 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1259 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1260 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1261 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1262 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
1263 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: S32
1264 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1265 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1266 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1267 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1268 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1269 */
1270__kernel void gemmlowp_matrix_b_reduction(TENSOR3D_DECLARATION(src),
1271 IMAGE_DECLARATION(dst))
1272{
1273 // Compute source and destination addresses
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001274 const uint x_offs = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
1275 const uint y = get_global_id(1);
Gian Marco05288a22017-11-21 10:57:50 +00001276
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001277 __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);
1278 __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 +00001279
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001280 VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE)
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001281 sum_col_32 = (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))0;
Gian Marco05288a22017-11-21 10:57:50 +00001282
1283 int i = 0;
1284 // This for loop performs 4 accumulations
1285 for(; i <= ((int)ROWS_B - 4); i += 4)
1286 {
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001287 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1288 b0 = VLOAD(VEC_SIZE)(0, matrix_b + 0 * src_stride_y);
1289 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1290 b1 = VLOAD(VEC_SIZE)(0, matrix_b + 1 * src_stride_y);
1291 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1292 b2 = VLOAD(VEC_SIZE)(0, matrix_b + 2 * src_stride_y);
1293 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1294 b3 = VLOAD(VEC_SIZE)(0, matrix_b + 3 * src_stride_y);
Gian Marco05288a22017-11-21 10:57:50 +00001295
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001296 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,
1297 VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE));
Gian Marco05288a22017-11-21 10:57:50 +00001298
1299 matrix_b += 4 * src_stride_y;
1300 }
1301
1302 // This for loop perfoms the leftover accumulations
1303 for(; i < (int)ROWS_B; ++i)
1304 {
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001305 const VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
1306 b0 = VLOAD(VEC_SIZE)(0, matrix_b);
Gian Marco05288a22017-11-21 10:57:50 +00001307
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001308 sum_col_32 += CONVERT(b0, VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE));
Gian Marco05288a22017-11-21 10:57:50 +00001309
1310 matrix_b += src_stride_y;
1311 }
1312
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001313#if defined(SCALAR)
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001314 sum_col_32 *= (VEC_DATA_TYPE(ACC_DATA_TYPE, VEC_SIZE))SCALAR;
Michele Di Giorgiof64d3362020-04-03 12:40:10 +01001315#endif // defined(SCALAR)
Michele Di Giorgioed902bc2020-10-22 12:05:09 +01001316 VEC_DATA_TYPE(int, VEC_SIZE)
1317 res0 = CONVERT(sum_col_32, VEC_DATA_TYPE(int, VEC_SIZE));
1318
1319 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 +00001320}
Michele Di Giorgioaae34102020-10-19 15:31:45 +01001321#endif // defined(COLS_B) && defined(ROWS_B) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
Gian Marco05288a22017-11-21 10:57:50 +00001322
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +00001323#endif // defined(DATA_TYPE) && defined(ACC_DATA_TYPE)
1324
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001325#if defined(K_OFFSET) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
1326
1327#define VEC_INT VEC_DATA_TYPE(int, VEC_SIZE)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001328
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001329/* Helper function used to calculate the offset contribution after matrix multiplication.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001330 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001331 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001332 * and calculates the offset contribution of matrix A and matrix B.
1333 *
1334 * @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)
1335 * @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)
1336 * @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)
1337 * @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 +01001338 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1339 * @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 +01001340 *
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001341 * @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 +01001342 * @param[in] y get_global_id(1)
1343 * @param[in] z get_global_id(2)
1344 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1345 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1346 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1347 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1348 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1349 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1350 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1351 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1352 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1353 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1354 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1355 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1356 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1357 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1358 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1359 * @param[in] biases_offset_first_element_in_bytes (Optional) The offset of the first element in the biases tensor
1360 */
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001361inline VEC_INT offset_contribution(
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001362 int x,
1363 int y,
1364 int z
1365#if defined(A_OFFSET)
1366 ,
1367 IMAGE_DECLARATION(sum_col)
1368#endif // defined(A_OFFSET)
1369#if defined(B_OFFSET)
1370 ,
1371 IMAGE_DECLARATION(sum_row)
1372#endif // defined(B_OFFSET)
1373#if defined(ADD_BIAS)
1374 ,
1375 VECTOR_DECLARATION(biases)
1376#endif // defined(ADD_BIAS)
1377)
1378{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001379 VEC_INT a_offset_s32 = (VEC_INT)0;
1380 VEC_INT b_offset_s32 = (VEC_INT)0;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001381
1382 int batch_id = z;
1383#if defined(DEPTH_INPUT3D)
1384 batch_id /= (int)DEPTH_INPUT3D;
1385#endif // defined(DEPTH_INPUT3D)
1386
1387#if defined(A_OFFSET)
1388 // Compute the offset contribution due to A_OFFSET
1389 __global uchar *sum_col_addr = sum_col_ptr + sum_col_offset_first_element_in_bytes + x * sizeof(int);
1390
1391 // Compute the offset contribution due to A_OFFSET
1392#if defined(SUM_COL_HAS_BATCHES)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001393 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 +01001394#else // defined(SUM_COL_HAS_BATCHES)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001395 a_offset_s32 = VLOAD(VEC_SIZE)(0, (__global int *)sum_col_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001396#endif // defined(SUM_COL_HAS_BATCHES)
1397
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001398 a_offset_s32 *= (VEC_INT)A_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001399#endif // defined(A_OFFSET)
1400
1401#if defined(B_OFFSET)
1402 // Compute the offset contribution due to A_OFFSET
1403 __global uchar *sum_row_addr = sum_row_ptr + sum_row_offset_first_element_in_bytes + y * sizeof(int);
1404
1405 // Compute the offset contribution due to B_OFFSET
1406#if defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001407 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 +01001408#else // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001409 b_offset_s32 = (VEC_INT) * (((__global int *)(sum_row_addr + batch_id * sum_row_stride_y)));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001410#endif // defined(HEIGHT_INPUT3D) && defined(DEPTH_INPUT3D)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001411 b_offset_s32 *= (VEC_INT)B_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001412#endif // defined(B_OFFSET)
1413
1414#if defined(ADD_BIAS)
1415 // Add bias
1416 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1417
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001418 VEC_INT biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
1419 b_offset_s32 += (VEC_INT)biases_values;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001420#endif // defined(ADD_BIAS)
1421
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001422 return (VEC_INT)K_OFFSET + a_offset_s32 + b_offset_s32;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001423}
1424
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001425/* OpenCL kernel used to add the offset contribution after matrix multiplication. The computation is performed in-place
Gian Marco05288a22017-11-21 10:57:50 +00001426 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001427 * This kernel takes a final int32 accumulator value (the output of matrix multiplication),
Gian Marco05288a22017-11-21 10:57:50 +00001428 * and adds to it the offset contribution of matrix A and matrix B in-place.
1429 *
1430 * @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)
1431 * @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)
1432 * @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 +07001433 * @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 +01001434 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1435 * @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 +00001436 *
1437 * The final result is:
1438 *
1439 * mm_result[i][k] = mm_result[i][k] +
1440 * (sum_col[k] * A_OFFSET) +
1441 * (sum_row[i] * B_OFFSET) +
1442 * (K_OFFSET)
1443 *
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +01001444 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1445 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1446 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1447 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1448 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1449 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1450 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1451 * @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 +01001452 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1453 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1454 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1455 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1456 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1457 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1458 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1459 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1460 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1461 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1462 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1463 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1464 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1465 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1466 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1467 * @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 +00001468 */
1469__kernel void gemmlowp_offset_contribution(TENSOR3D_DECLARATION(mm_result)
1470#if defined(A_OFFSET)
1471 ,
1472 IMAGE_DECLARATION(sum_col)
1473#endif // defined(A_OFFSET)
1474#if defined(B_OFFSET)
1475 ,
1476 IMAGE_DECLARATION(sum_row)
1477#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001478#if defined(ADD_BIAS)
1479 ,
1480 VECTOR_DECLARATION(biases)
1481#endif // defined(ADD_BIAS))
Gian Marco05288a22017-11-21 10:57:50 +00001482 )
1483{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001484 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 +01001485 const int y = get_global_id(1);
1486 const int z = get_global_id(2);
1487
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001488 // Compute offset contribution
1489 int4 offset_term_s32 = offset_contribution(
1490 x, y, z
Gian Marco05288a22017-11-21 10:57:50 +00001491#if defined(A_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001492 ,
1493 sum_col_ptr,
1494 sum_col_stride_x,
1495 sum_col_step_x,
1496 sum_col_stride_y,
1497 sum_col_step_y,
1498 sum_col_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001499#endif // defined(A_OFFSET)
Gian Marco05288a22017-11-21 10:57:50 +00001500#if defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001501 ,
1502 sum_row_ptr,
1503 sum_row_stride_x,
1504 sum_row_step_x,
1505 sum_row_stride_y,
1506 sum_row_step_y,
1507 sum_row_offset_first_element_in_bytes
Gian Marco05288a22017-11-21 10:57:50 +00001508#endif // defined(B_OFFSET)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001509#if defined(ADD_BIAS)
1510 ,
1511 biases_ptr,
1512 biases_stride_x,
1513 biases_step_x,
1514 biases_offset_first_element_in_bytes
1515#endif // defined(ADD_BIAS)
1516 );
Gian Marco05288a22017-11-21 10:57:50 +00001517
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001518 __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 +00001519
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001520 int4 in_s32 = vload4(0, (__global int *)mm_result_addr);
Gian Marco05288a22017-11-21 10:57:50 +00001521
1522 // Add the offset terms to GEMM's result
1523 in_s32 += offset_term_s32;
1524
1525 // Store the result with the offset contribution
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001526 vstore4(in_s32, 0, (__global int *)mm_result_addr);
Gian Marco05288a22017-11-21 10:57:50 +00001527}
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001528
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00001529#if defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT) && defined(OUTPUT_DATA_TYPE)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001530/* OpenCL kernel used to add the offset contribution after @ref CLGEMMLowpMatrixMultiplyKernel and it quantizes down to uint8.
1531 *
1532 * 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.
1533 *
1534 *
1535 * @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)
1536 * @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)
1537 * @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)
1538 * @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
1539 *
1540 * The result before the output stage is:
1541 *
1542 * mm_result[i][k] = mm_result[i][k] +
1543 * (sum_col[k] * A_OFFSET) +
1544 * (sum_row[i] * B_OFFSET) +
1545 * (K_OFFSET)
1546 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001547 * 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 +01001548 *
1549 * -# Add offset terms to final result
1550 * -# Multiply each entry of result by result_mult_int
1551 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
1552 * -# Shift the int32 accumulator by result_shift
1553 * -# 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 +00001554 * -# Clamp the resulting int32 values:
1555 * - to the [0..255] range and cast to QASYMM8.
1556 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001557 *
1558 * @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
1559 *
1560 * @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 +00001561 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001562 * @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.
1563 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001564 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1565 * @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 +01001566 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001567 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1568 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1569 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1570 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1571 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1572 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1573 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1574 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1575 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1576 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1577 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1578 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1579 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1580 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1581 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1582 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1583 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1584 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1585 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1586 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1587 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1588 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1589 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1590 * @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 +00001591 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001592 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1593 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1594 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1595 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1596 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1597 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1598 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1599 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1600 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1601 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1602 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1603 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1604 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1605 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1606 * @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 +01001607 */
1608__kernel void gemmlowp_offset_contribution_quantize_down(TENSOR3D_DECLARATION(mm_result)
1609#if defined(A_OFFSET)
1610 ,
1611 IMAGE_DECLARATION(sum_col)
1612#endif // defined(A_OFFSET)
1613#if defined(B_OFFSET)
1614 ,
1615 IMAGE_DECLARATION(sum_row)
1616#endif // defined(B_OFFSET)
1617 ,
1618#if defined(ADD_BIAS)
1619 VECTOR_DECLARATION(biases),
1620#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001621 TENSOR3D_DECLARATION(dst)
1622#if defined(PER_CHANNEL_QUANTIZATION)
1623 ,
1624 VECTOR_DECLARATION(result_multipliers),
1625 VECTOR_DECLARATION(result_shifts)
1626#endif // defined(PER_CHANNEL_QUANTIZATION)
1627 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001628{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001629 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 +01001630 const int y = get_global_id(1);
1631 const int z = get_global_id(2);
1632
1633 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1634
1635 // Compute offset contribution
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001636 VEC_INT offset_term_s32 = offset_contribution(
1637 x, y, z
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001638#if defined(A_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001639 ,
1640 sum_col_ptr,
1641 sum_col_stride_x,
1642 sum_col_step_x,
1643 sum_col_stride_y,
1644 sum_col_step_y,
1645 sum_col_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001646#endif // defined(A_OFFSET)
1647#if defined(B_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001648 ,
1649 sum_row_ptr,
1650 sum_row_stride_x,
1651 sum_row_step_x,
1652 sum_row_stride_y,
1653 sum_row_step_y,
1654 sum_row_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001655#endif // defined(B_OFFSET)
1656#if defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001657 ,
1658 biases_ptr,
1659 biases_stride_x,
1660 biases_step_x,
1661 biases_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001662#endif // defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001663 );
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001664
1665 __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;
1666
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001667 VEC_INT in_s32 = VLOAD(VEC_SIZE)(0, (__global int *)mm_result_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001668
1669 // Add the offset terms to GEMM's result
1670 in_s32 += offset_term_s32;
1671
1672 // -------------- OUTPUT STAGE
1673
1674 // Add the offset terms to GEMM's result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001675 in_s32 += (VEC_INT)RESULT_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001676
1677 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001678#if defined(PER_CHANNEL_QUANTIZATION)
1679 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
1680 __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 +01001681 VEC_INT result_multipliers_values = VLOAD(VEC_SIZE)(0, (__global int *)result_multipliers_addr);
1682 VEC_INT result_shifts_values = VLOAD(VEC_SIZE)(0, (__global int *)result_shifts_addr);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001683
1684 in_s32 *= result_multipliers_values;
1685 in_s32 >>= result_shifts_values;
1686#else // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001687 in_s32 *= RESULT_MULTIPLIER;
1688
1689 in_s32 >>= RESULT_SHIFT;
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001690#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001691
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001692 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
1693 res0 = CONVERT_SAT(in_s32, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001694
1695#if defined(MIN_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001696 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001697#endif // defined(MIN_BOUND)
1698#if defined(MAX_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001699 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001700#endif // defined(MAX_BOUND)
1701
1702 // Store the result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001703 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 +01001704}
1705
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001706/* 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 +01001707 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001708 * 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 +01001709 *
1710 *
1711 * @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)
1712 * @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)
1713 * @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)
1714 * @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
1715 *
1716 * The result before the output stage is:
1717 *
1718 * mm_result[i][k] = mm_result[i][k] +
1719 * (sum_col[k] * A_OFFSET) +
1720 * (sum_row[i] * B_OFFSET) +
1721 * (K_OFFSET)
1722 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001723 * 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 +01001724 *
1725 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1726 * -# Add bias to final result if bias tensor is not a nullptr
1727 * -# Round to nearest division by a power-of-two using result_shift
1728 * -# Add offset to each result
1729 * -# Clamp the value between the specified min and max bounds
Manuel Bottini959c26d2019-12-02 16:22:35 +00001730 * -# Clamp the resulting int32 values:
1731 * - to the [0..255] range and cast to QASYMM8.
1732 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001733 *
1734 * @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
1735 *
1736 * @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 +00001737 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001738 * @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.
1739 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001740 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
1741 * @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 +01001742 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001743 * @param[in] mm_result_ptr Pointer to the source tensor. Supported data type: S32
1744 * @param[in] mm_result_stride_x Stride of the source tensor in X dimension (in bytes)
1745 * @param[in] mm_result_step_x mm_result_stride_x * number of elements along X processed per workitem(in bytes)
1746 * @param[in] mm_result_stride_y Stride of the source tensor in Y dimension (in bytes)
1747 * @param[in] mm_result_step_y mm_result_stride_y * number of elements along Y processed per workitem(in bytes)
1748 * @param[in] mm_result_stride_z Stride of the source tensor in Z dimension (in bytes)
1749 * @param[in] mm_result_step_z mm_result_stride_z * number of elements along Z processed per workitem(in bytes)
1750 * @param[in] mm_result_offset_first_element_in_bytes The offset of the first element in the source tensor
1751 * @param[in] sum_col_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1752 * @param[in] sum_col_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1753 * @param[in] sum_col_step_x (Optional) sum_col_stride_x * number of elements along X processed per workitem(in bytes)
1754 * @param[in] sum_col_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1755 * @param[in] sum_col_step_y (Optional) sum_col_stride_y * number of elements along Y processed per workitem(in bytes)
1756 * @param[in] sum_col_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1757 * @param[in] sum_row_ptr (Optional) Pointer to the source tensor. Supported data type: same as @p mm_result_ptr
1758 * @param[in] sum_row_stride_x (Optional) Stride of the source tensor in X dimension (in bytes)
1759 * @param[in] sum_row_step_x (Optional) sum_row_stride_x * number of elements along X processed per workitem(in bytes)
1760 * @param[in] sum_row_stride_y (Optional) Stride of the source tensor in Y dimension (in bytes)
1761 * @param[in] sum_row_step_y (Optional) sum_row_stride_y * number of elements along Y processed per workitem(in bytes)
1762 * @param[in] sum_row_offset_first_element_in_bytes (Optional) The offset of the first element in the source tensor
1763 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1764 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1765 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1766 * @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 +01001767 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001768 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1769 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1770 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1771 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1772 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1773 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1774 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1775 * @param[in] result_multipliers_ptr (Optional) Pointer to the output multipliers vector for per-channel quantization. Supported data types: S32
1776 * @param[in] result_multipliers_stride_x (Optional) Stride of the output multipliers vector in X dimension (in bytes)
1777 * @param[in] result_multipliers_step_x (Optional) output_multipliers_stride_x * number of elements along X processed per workitem(in bytes)
1778 * @param[in] result_multipliers_offset_first_element_in_bytes (Optional) The offset of the first element in the output multipliers vector
1779 * @param[in] result_shifts_ptr (Optional) Pointer to the output shifts vector for per-channel quantization. Supported data types: S32
1780 * @param[in] result_shifts_stride_x (Optional) Stride of the output shifts vector in X dimension (in bytes)
1781 * @param[in] result_shifts_step_x (Optional) output_shifts_stride_x * number of elements along X processed per workitem(in bytes)
1782 * @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 +01001783 */
1784__kernel void gemmlowp_offset_contribution_quantize_down_fixedpoint(TENSOR3D_DECLARATION(mm_result)
1785#if defined(A_OFFSET)
1786 ,
1787 IMAGE_DECLARATION(sum_col)
1788#endif // defined(A_OFFSET)
1789#if defined(B_OFFSET)
1790 ,
1791 IMAGE_DECLARATION(sum_row)
1792#endif // defined(B_OFFSET)
1793 ,
1794#if defined(ADD_BIAS)
1795 VECTOR_DECLARATION(biases),
1796#endif // defined(ADD_BIAS)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001797 TENSOR3D_DECLARATION(dst)
1798#if defined(PER_CHANNEL_QUANTIZATION)
1799 ,
1800 VECTOR_DECLARATION(result_multipliers),
1801 VECTOR_DECLARATION(result_shifts)
1802#endif // defined(PER_CHANNEL_QUANTIZATION)
1803 )
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001804{
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001805 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 +01001806 const int y = get_global_id(1);
1807 const int z = get_global_id(2);
1808
1809 // Compute offset contribution
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001810 VEC_INT offset_term_s32 = offset_contribution(
1811 x, y, z
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001812#if defined(A_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001813 ,
1814 sum_col_ptr,
1815 sum_col_stride_x,
1816 sum_col_step_x,
1817 sum_col_stride_y,
1818 sum_col_step_y,
1819 sum_col_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001820#endif // defined(A_OFFSET)
1821#if defined(B_OFFSET)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001822 ,
1823 sum_row_ptr,
1824 sum_row_stride_x,
1825 sum_row_step_x,
1826 sum_row_stride_y,
1827 sum_row_step_y,
1828 sum_row_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001829#endif // defined(B_OFFSET)
1830#if defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001831 ,
1832 biases_ptr,
1833 biases_stride_x,
1834 biases_step_x,
1835 biases_offset_first_element_in_bytes
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001836#endif // defined(ADD_BIAS)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001837 );
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001838
1839 __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;
1840
1841 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1842
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001843 VEC_INT in_s32 = VLOAD(VEC_SIZE)(0, (__global int *)mm_result_addr);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001844
1845 // Add the offset terms to GEMM's result
1846 in_s32 += offset_term_s32;
1847
1848 // -------------- OUTPUT STAGE
1849
1850 // Multiply by result_mult_int and shift
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001851#if defined(PER_CHANNEL_QUANTIZATION)
1852 __global uchar *result_multipliers_addr = result_multipliers_ptr + result_multipliers_offset_first_element_in_bytes + x * sizeof(int);
1853 __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 +01001854 VEC_INT result_multipliers_values = VLOAD(VEC_SIZE)(0, (__global int *)result_multipliers_addr);
1855 VEC_INT result_shifts_values = VLOAD(VEC_SIZE)(0, (__global int *)result_shifts_addr);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001856
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001857 VEC_INT in_s32_shift_lt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, VEC_SIZE);
1858 VEC_INT in_s32_shift_gt0 = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(in_s32, result_multipliers_values, result_shifts_values, VEC_SIZE);
1859 in_s32 = select(in_s32_shift_lt0, in_s32_shift_gt0, result_shifts_values >= 0);
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001860#else // defined(PER_CHANNEL_QUANTIZATION)
1861
1862#if RESULT_SHIFT < 0
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001863 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 +01001864#else // RESULT_SHIFT >= 0
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001865 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 +01001866#endif // RESULT_SHIFT < 0
1867
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001868#endif // defined(PER_CHANNEL_QUANTIZATION)
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001869
1870 // Add the offset terms to GEMM's result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001871 in_s32 += (VEC_INT)RESULT_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001872
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001873 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
1874 res0 = CONVERT_SAT(in_s32, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001875
1876#if defined(MIN_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001877 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001878#endif // defined(MIN_BOUND)
1879#if defined(MAX_BOUND)
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001880 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001881#endif // defined(MAX_BOUND)
1882
1883 // Store the result
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001884 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 +01001885}
Michele Di Giorgiob54ba282020-01-14 15:31:55 +00001886#endif // defined(RESULT_OFFSET) && defined(RESULT_MULTIPLIER) && defined(RESULT_SHIFT) && defined(OUTPUT_DATA_TYPE)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00001887
Michele Di Giorgio0bfe39f2020-10-21 11:36:21 +01001888#undef VEC_INT
1889
1890#endif // defined(K_OFFSET) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
Gian Marco05288a22017-11-21 10:57:50 +00001891
1892#if defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT)
Luca Foschiani689c9682020-02-26 14:30:14 +00001893/** 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 +00001894 *
Luca Foschiani689c9682020-02-26 14:30:14 +00001895 * 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 +00001896 * The following computations will be performed by the kernel:
1897 *
1898 * -# Add offset terms to final result
1899 * -# Multiply each entry of result by result_mult_int
1900 * -# Add bias to final result (if -DADD_BIAS is passed at compile time)
1901 * -# Shift the int32 accumulator by result_shift
1902 * -# 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 +00001903 * -# Clamp the resulting int32 values:
1904 * -# - to the [0..255] range and cast to QASYMM8.
1905 * -# - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco05288a22017-11-21 10:57:50 +00001906 *
1907 * @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
1908 *
1909 * @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 +00001910 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco05288a22017-11-21 10:57:50 +00001911 * @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.
1912 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001913 * @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 +00001914 *
1915 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
1916 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
1917 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
1918 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
1919 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
1920 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
1921 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1922 * @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 +01001923 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
1924 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
1925 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
1926 * @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 +00001927 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco05288a22017-11-21 10:57:50 +00001928 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
1929 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
1930 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
1931 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
1932 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
1933 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
1934 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
1935 */
1936__kernel void gemmlowp_output_stage_quantize_down(TENSOR3D_DECLARATION(src),
1937#if defined(ADD_BIAS)
1938 VECTOR_DECLARATION(biases),
1939#endif // defined(ADD_BIAS)
1940 TENSOR3D_DECLARATION(dst))
1941{
1942 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001943 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 +01001944 int y = get_global_id(1);
1945 int z = get_global_id(2);
Gian Marco05288a22017-11-21 10:57:50 +00001946
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001947 __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 +00001948
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001949 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
1950
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001951 VEC_DATA_TYPE(int, VEC_SIZE)
1952 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00001953
Gian Marco05288a22017-11-21 10:57:50 +00001954#if defined(ADD_BIAS)
1955 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001956 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
1957
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001958 VEC_DATA_TYPE(int, VEC_SIZE)
1959 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
1960 input_values += biases_values;
Gian Marco05288a22017-11-21 10:57:50 +00001961#endif // defined(ADD_BIAS)
1962
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001963 // Add the offset terms to GEMM's result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001964 input_values += (VEC_DATA_TYPE(int, VEC_SIZE))RESULT_OFFSET;
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001965
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +00001966 // Multiply by result_mult_int and shift
Gian Marco58c57942017-11-28 09:10:03 +00001967 input_values *= RESULT_MULT_INT;
Gian Marco05288a22017-11-21 10:57:50 +00001968
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001969#if RESULT_SHIFT < 0
1970 input_values >>= -RESULT_SHIFT;
1971#else // RESULT_SHIFT >= 0
Gian Marco58c57942017-11-28 09:10:03 +00001972 input_values >>= RESULT_SHIFT;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01001973#endif // RESULT_SHIFT < 0
Gian Marco05288a22017-11-21 10:57:50 +00001974
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001975 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
1976 res0 = CONVERT_SAT(input_values, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco05288a22017-11-21 10:57:50 +00001977
1978#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001979 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00001980#endif // defined(MIN_BOUND)
1981#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001982 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco05288a22017-11-21 10:57:50 +00001983#endif // defined(MAX_BOUND)
1984
1985 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01001986 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 +00001987}
Gian Marco58c57942017-11-28 09:10:03 +00001988#endif // defined(RESULT_OFFSET) && defined(RESULT_MULT_INT) && defined(RESULT_SHIFT)
1989
1990#if defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
Manuel Bottini959c26d2019-12-02 16:22:35 +00001991/** 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 +00001992 *
Manuel Bottini959c26d2019-12-02 16:22:35 +00001993 * 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 +00001994 * The following computations will be performed by the kernel:
1995 *
1996 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
1997 * -# Add bias to final result if bias tensor is not a nullptr
1998 * -# Round to nearest division by a power-of-two using result_shift
1999 * -# Add offset to each result
2000 * -# Clamp the value between the specified min and max bounds
Manuel Bottini1f332d42019-11-29 17:25:25 +00002001 * -# Clamp the resulting int32 values:
2002 * - to the [0..255] range and cast to QASYMM8.
2003 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Gian Marco58c57942017-11-28 09:10:03 +00002004 *
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002005 * @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 +00002006 *
2007 * @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 +00002008 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Gian Marco58c57942017-11-28 09:10:03 +00002009 * @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.
2010 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002011 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
2012 * @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 +00002013 *
2014 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2015 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2016 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2017 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2018 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2019 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2020 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2021 * @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 +01002022 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
2023 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
2024 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
2025 * @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 +00002026 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8/QASYMM8_SIGNED
Gian Marco58c57942017-11-28 09:10:03 +00002027 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2028 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2029 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2030 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2031 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2032 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2033 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2034 */
2035__kernel void gemmlowp_output_stage_quantize_down_fixedpoint(TENSOR3D_DECLARATION(src),
2036#if defined(ADD_BIAS)
2037 VECTOR_DECLARATION(biases),
2038#endif // defined(ADD_BIAS)
2039 TENSOR3D_DECLARATION(dst))
2040{
2041 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002042 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 +01002043 int y = get_global_id(1);
2044 int z = get_global_id(2);
Georgios Pinitas932491f2018-09-21 16:33:15 +01002045
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002046 __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 +00002047
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002048 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
2049
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002050 VEC_DATA_TYPE(int, VEC_SIZE)
2051 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Gian Marco58c57942017-11-28 09:10:03 +00002052
2053#if defined(ADD_BIAS)
2054 // Add bias
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002055 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
2056
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002057 VEC_DATA_TYPE(int, VEC_SIZE)
2058 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
2059 input_values += biases_values;
Gian Marco58c57942017-11-28 09:10:03 +00002060#endif // defined(ADD_BIAS)
2061
2062 // Multiply by result_mult_int and shift
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +01002063#if RESULT_SHIFT < 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002064 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 +01002065#else // RESULT_SHIFT >= 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002066 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 +01002067#endif // RESULT_SHIFT < 0
Gian Marco58c57942017-11-28 09:10:03 +00002068
2069 // Add the offset terms to GEMM's result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002070 input_values += (VEC_DATA_TYPE(int, VEC_SIZE))RESULT_OFFSET_AFTER_SHIFT;
Gian Marco58c57942017-11-28 09:10:03 +00002071
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002072 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
2073 res0 = CONVERT_SAT(input_values, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Gian Marco58c57942017-11-28 09:10:03 +00002074
2075#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002076 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00002077#endif // defined(MIN_BOUND)
2078#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002079 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Gian Marco58c57942017-11-28 09:10:03 +00002080#endif // defined(MAX_BOUND)
2081
2082 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002083 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 +00002084}
Chunosov5124be52017-11-22 20:42:13 +07002085#endif // defined(RESULT_OFFSET_AFTER_SHIFT) && defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002086
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002087#if defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
2088
Michalis Spyrou51146c52019-07-12 14:42:29 +01002089/** This OpenCL kernel is used to quantize down the int32 accumulator values of GEMMLowp to QSYMM16
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002090 *
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +00002091 * 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 +01002092 * The following computations will be performed by the kernel:
2093 *
2094 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
2095 * -# Add bias to final result if bias tensor is not a nullptr
2096 * -# Round to nearest division by a power-of-two using result_shift
2097 * -# Add offset to each result
2098 * -# Clamp the value between the specified min and max bounds
2099 * -# Clamp the resulting int32 values to the [-32768..32767] range and cast to QSYMM16.
2100 *
2101 * @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
2102 *
2103 * @note In case the addition of int32 biases is required, -DADD_BIAS should be passed at compile time
2104 * @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.
2105 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002106 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
2107 * @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 +01002108 *
2109 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2110 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2111 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2112 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2113 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2114 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2115 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2116 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
2117 * @param[in] biases_ptr (Optional) Pointer to the biases tensor. Supported data type: same as @p src_ptr
2118 * @param[in] biases_stride_x (Optional) Stride of the biases tensor in X dimension (in bytes)
2119 * @param[in] biases_step_x (Optional) biases_stride_x * number of elements along X processed per workitem(in bytes)
2120 * @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 +01002121 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QSYMM16
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002122 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2123 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2124 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2125 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2126 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2127 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2128 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2129 */
2130__kernel void gemmlowp_output_stage_quantize_down_fixedpoint_qsymm16(TENSOR3D_DECLARATION(src),
2131#if defined(ADD_BIAS)
2132 VECTOR_DECLARATION(biases),
2133#endif // defined(ADD_BIAS)
2134 TENSOR3D_DECLARATION(dst))
2135{
2136 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002137 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 +01002138 int y = get_global_id(1);
2139 int z = get_global_id(2);
2140
Michalis Spyrou51146c52019-07-12 14:42:29 +01002141 __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 +01002142
Michele Di Giorgioba14c922020-10-12 13:27:57 +01002143 __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 +01002144
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002145 VEC_DATA_TYPE(int, VEC_SIZE)
2146 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002147
2148#if defined(ADD_BIAS)
2149 // Add bias
Michalis Spyrou51146c52019-07-12 14:42:29 +01002150 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002151
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002152 VEC_DATA_TYPE(int, VEC_SIZE)
2153 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
2154 input_values += biases_values;
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002155#endif // defined(ADD_BIAS)
2156
2157 // Multiply by result_mult_int and shift
Manuel Bottini07263982019-10-17 18:37:26 +01002158#if RESULT_SHIFT < 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002159 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 +00002160#else // RESULT_SHIFT >= 0
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002161 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 +01002162#endif // RESULT_SHIFT < 0
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002163
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002164 VEC_DATA_TYPE(short, VEC_SIZE)
2165 res0 = CONVERT_SAT(input_values, VEC_DATA_TYPE(short, VEC_SIZE));
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002166
2167#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002168 res0 = max(res0, (VEC_DATA_TYPE(short, VEC_SIZE))MIN_BOUND);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002169#endif // defined(MIN_BOUND)
2170#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002171 res0 = min(res0, (VEC_DATA_TYPE(short, VEC_SIZE))MAX_BOUND);
Manuel Bottini9c9b70b2019-07-01 17:35:56 +01002172#endif // defined(MAX_BOUND)
2173
2174 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002175 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 +01002176}
2177#endif // defined(RESULT_FIXEDPOINT_MULTIPLIER) && defined(RESULT_SHIFT)
2178
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002179#if defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET)
Sheri Zhang1b14c752020-03-09 14:29:52 +00002180/** 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 +01002181 *
Sheri Zhang1b14c752020-03-09 14:29:52 +00002182 * 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 +01002183 * The following computations will be performed by the kernel:
2184 *
2185 * -# Compute fixed point multiplication between each entry of input by result_fixedpoint_multiplier
2186 * -# Add bias to final result if bias tensor is not a nullptr
2187 * -# Requantize
2188 * -# Add offset to each result
2189 * -# Clamp the value between the specified min and max bounds
Sheri Zhang1b14c752020-03-09 14:29:52 +00002190 * -# Clamp the resulting int32 values:
2191 * - to the [0..255] range and cast to QASYMM8.
2192 * - to the [-128..127] range and cast to QASYMM8_SIGNED.
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002193 *
2194 * @attention The offset and scalar scale factor must be passed at compile time using -DRESULT_OFFSET, -DREAL_MULTIPLIER
2195 *
2196 * @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 +00002197 * @note The output datatype should be passed at compile time using -DOUTPUT_DATA_TYPE
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002198 * @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.
2199 * These values can be used to implement "rectified linear unit" activation functions
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002200 * @note Vector size should be given as a preprocessor argument using -DVEC_SIZE=size. e.g. -DVEC_SIZE=16
2201 * @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 +01002202 *
2203 * @param[in] src_ptr Pointer to the source tensor. Supported data type: S32
2204 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
2205 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
2206 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
2207 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
2208 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
2209 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2210 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
2211 * @param[in] biases_ptr Pointer to the biases tensor. Supported data type: same as @p src_ptr
2212 * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes)
2213 * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes)
2214 * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor
2215 * @param[out] dst_ptr Pointer to the destination tensor Supported data type: QASYMM8
2216 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
2217 * @param[in] dst_step_x dst_gx_stride_x * number of elements along X processed per workitem(in bytes)
2218 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
2219 * @param[in] dst_step_y dst_gx_stride_y * number of elements along Y processed per workitem(in bytes)
2220 * @param[in] dst_stride_z Stride of the source tensor in Z dimension (in bytes)
2221 * @param[in] dst_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
2222 * @param[in] dst_stride_w Stride of the source tensor in W dimension (in bytes)
2223 * @param[in] dst_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
2224 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
2225 */
2226__kernel void gemmlowp_output_stage_quantize_down_float(TENSOR3D_DECLARATION(src),
2227#if defined(ADD_BIAS)
2228 VECTOR_DECLARATION(biases),
2229#endif // defined(ADD_BIAS)
2230#if defined(DST_HEIGHT)
2231 TENSOR4D_DECLARATION(dst))
2232#else // defined(DST_HEIGHT)
2233 TENSOR3D_DECLARATION(dst))
2234#endif // defined(DST_HEIGHT)
2235{
2236 // Compute source and destination addresses
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002237 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 +00002238 int y = get_global_id(1);
2239 int z = get_global_id(2);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002240
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002241 __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 +01002242
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002243 __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x + y * dst_stride_y + z * dst_stride_z;
2244
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002245 VEC_DATA_TYPE(int, VEC_SIZE)
2246 input_values = VLOAD(VEC_SIZE)(0, (__global int *)src_addr);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002247
2248#if defined(ADD_BIAS)
2249 // Add bias
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002250 __global uchar *bias_addr = biases_ptr + biases_offset_first_element_in_bytes + x * sizeof(int);
2251
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002252 VEC_DATA_TYPE(int, VEC_SIZE)
2253 biases_values = VLOAD(VEC_SIZE)(0, (__global int *)bias_addr);
Gian Marco Iodice0c54a622018-10-30 12:20:03 +00002254 input_values += (int4)biases_values;
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002255#endif // defined(ADD_BIAS)
2256
2257 // Convert to float
Sheri Zhang1b14c752020-03-09 14:29:52 +00002258 float4 input_values_f = convert_float4(input_values);
2259 input_values_f = round(input_values_f * (float)REAL_MULTIPLIER + (float)OUTPUT_OFFSET);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002260
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002261 VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE)
2262 res0 = CONVERT_SAT(input_values_f, VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE));
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002263
2264#if defined(MIN_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002265 res0 = max(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MIN_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002266#endif // defined(MIN_BOUND)
2267#if defined(MAX_BOUND)
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002268 res0 = min(res0, (VEC_DATA_TYPE(OUTPUT_DATA_TYPE, VEC_SIZE))MAX_BOUND);
Georgios Pinitas51e53a32018-10-22 13:49:08 +01002269#endif // defined(MAX_BOUND)
2270
2271 // Store the result
Michele Di Giorgio671d4f02020-10-14 12:26:51 +01002272 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 +01002273}
Gian Marco Iodice27423f02020-08-12 14:12:28 +01002274#endif // defined(REAL_MULTIPLIER) && defined(OUTPUT_OFFSET)