blob: eba23162436e4ac5b3d9f25839bcb80b05e90055 [file] [log] [blame]
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001/*
Gian Marco Iodice10e88a72021-11-29 12:49:19 +00002 * Copyright (c) 2021-2022 Arm Limited.
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +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 */
24
Giorgio Arenabdd16d12021-05-13 16:58:51 +010025// *INDENT-OFF*
26// clang-format off
27
Gian Marco Iodice8155c022021-04-16 15:08:59 +010028#define TILE_VECTOR_SIZE1 1
29#define TILE_VECTOR_SIZE2 2
30#define TILE_VECTOR_SIZE3 3
31#define TILE_VECTOR_SIZE4 4
32#define TILE_VECTOR_SIZE5 8
33#define TILE_VECTOR_SIZE6 8
34#define TILE_VECTOR_SIZE7 8
35#define TILE_VECTOR_SIZE8 8
36#define TILE_VECTOR_SIZE9 16
37#define TILE_VECTOR_SIZE10 16
38#define TILE_VECTOR_SIZE11 16
39#define TILE_VECTOR_SIZE12 16
40#define TILE_VECTOR_SIZE13 16
41#define TILE_VECTOR_SIZE14 16
42#define TILE_VECTOR_SIZE15 16
43#define TILE_VECTOR_SIZE16 16
44
45#define TILE_VECTOR_TYPE1(DATA_TYPE) DATA_TYPE##1
46#define TILE_VECTOR_TYPE2(DATA_TYPE) DATA_TYPE##2
47#define TILE_VECTOR_TYPE3(DATA_TYPE) DATA_TYPE##3
48#define TILE_VECTOR_TYPE4(DATA_TYPE) DATA_TYPE##4
49#define TILE_VECTOR_TYPE5(DATA_TYPE) DATA_TYPE##8
50#define TILE_VECTOR_TYPE6(DATA_TYPE) DATA_TYPE##8
51#define TILE_VECTOR_TYPE7(DATA_TYPE) DATA_TYPE##8
52#define TILE_VECTOR_TYPE8(DATA_TYPE) DATA_TYPE##8
53#define TILE_VECTOR_TYPE9(DATA_TYPE) DATA_TYPE##16
54#define TILE_VECTOR_TYPE10(DATA_TYPE) DATA_TYPE##16
55#define TILE_VECTOR_TYPE11(DATA_TYPE) DATA_TYPE##16
56#define TILE_VECTOR_TYPE12(DATA_TYPE) DATA_TYPE##16
57#define TILE_VECTOR_TYPE13(DATA_TYPE) DATA_TYPE##16
58#define TILE_VECTOR_TYPE14(DATA_TYPE) DATA_TYPE##16
59#define TILE_VECTOR_TYPE15(DATA_TYPE) DATA_TYPE##16
60#define TILE_VECTOR_TYPE16(DATA_TYPE) DATA_TYPE##16
61
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000062/** Tile object
63 * A tile object is a 2D memory block and can be accessed using the following syntax:
64 * -# a[m0].v = access the the vector at row "m0" (OpenCL vector)
65 * -# a[m0].s[x] = access the scalar element at row "m0" and column "n0" (scalar access)
66 *
67 * @param[in] DATA_TYPE Data type of the tile
68 * @param[in] H Number of tile rows
69 * @param[in] W Number of tile colums
70 * @param[in] BASENAME Tile's name
71 */
72#define TILE(DATA_TYPE, H, W, BASENAME) TILE_STR(DATA_TYPE, H, W, BASENAME)
73#define TILE_STR(DATA_TYPE, H, W, BASENAME) \
74 union { \
Gian Marco Iodice8155c022021-04-16 15:08:59 +010075 DATA_TYPE s[TILE_VECTOR_SIZE##W]; \
76 TILE_VECTOR_TYPE##W(DATA_TYPE) v; \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000077 } BASENAME[H]
78
Giorgio Arenabdd16d12021-05-13 16:58:51 +010079#define TENSOR4D_IMAGE(name) \
80 __read_only image2d_t name##_img, \
81 __global uchar *name##_ptr, \
82 uint name##_stride_x, \
83 uint name##_step_x, \
84 uint name##_stride_y, \
85 uint name##_step_y, \
86 uint name##_stride_z, \
87 uint name##_step_z, \
88 uint name##_stride_w, \
89 uint name##_step_w, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000090 uint name##_offset_first_element_in_bytes
91
Giorgio Arenabdd16d12021-05-13 16:58:51 +010092#define TENSOR4D_BUFFER(name) \
93 __global uchar *name##_ptr, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +000094 uint name##_stride_x, \
95 uint name##_step_x, \
96 uint name##_stride_y, \
97 uint name##_step_y, \
98 uint name##_stride_z, \
99 uint name##_step_z, \
100 uint name##_stride_w, \
101 uint name##_step_w, \
102 uint name##_offset_first_element_in_bytes
103
104#define TENSOR4D_STR(name, type) TENSOR4D_##type(name)
105#define TENSOR4D(name, type) TENSOR4D_STR(name, type)
106
Adnan AlSinan17975a62021-11-08 17:46:39 +0000107#define TENSOR4D_T_IMAGE(name) \
108 __read_only image2d_t name##_img, \
109 __global uchar *name##_ptr, \
110 uint name##_stride_y, \
111 uint name##_stride_z, \
112 uint name##_stride_w, \
113 uint name##_c, \
114 uint name##_w, \
115 uint name##_h, \
116 uint name##_n, \
117 uint name##_offset_first_element_in_bytes
118
119#define TENSOR4D_T_BUFFER(name) \
120 __global uchar *name##_ptr, \
121 uint name##_stride_y, \
122 uint name##_stride_z, \
123 uint name##_stride_w, \
124 uint name##_c, \
125 uint name##_w, \
126 uint name##_h, \
127 uint name##_n, \
128 uint name##_offset_first_element_in_bytes
129
130#define TENSOR4D_T_STR(name, type) TENSOR4D_T_##type(name)
131#define TENSOR4D_T(name, type) TENSOR4D_T_STR(name, type)
132
Gian Marco Iodice4fb56702021-11-10 11:18:50 +0000133#define TENSOR3D_T_IMAGE(name) \
134 __read_only image2d_t name##_img, \
135 __global uchar *name##_ptr, \
136 uint name##_stride_y, \
137 uint name##_stride_z, \
138 uint name##_w, \
139 uint name##_h, \
140 uint name##_n, \
141 uint name##_offset_first_element_in_bytes
142
143#define TENSOR3D_T_BUFFER(name) \
144 __global uchar *name##_ptr, \
145 uint name##_stride_y, \
146 uint name##_stride_z, \
147 uint name##_w, \
148 uint name##_h, \
149 uint name##_n, \
150 uint name##_offset_first_element_in_bytes
151
152#define TENSOR3D_T_STR(name, type) TENSOR3D_T_##type(name)
153#define TENSOR3D_T(name, type) TENSOR3D_T_STR(name, type)
154
Giorgio Arenaea8d2662021-05-20 11:36:56 +0100155#if !defined(UNROLL_WITH_PRAGMA)
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100156#define UNROLL_INCR(idx, step, macro) idx += (step); (macro)
157
158#define LOOP_UNROLLING_1(idx, step, macro) (macro)
159#define LOOP_UNROLLING_2(idx, step, macro) LOOP_UNROLLING_1(idx, step, macro); UNROLL_INCR(idx, step, macro)
160#define LOOP_UNROLLING_3(idx, step, macro) LOOP_UNROLLING_2(idx, step, macro); UNROLL_INCR(idx, step, macro)
161#define LOOP_UNROLLING_4(idx, step, macro) LOOP_UNROLLING_3(idx, step, macro); UNROLL_INCR(idx, step, macro)
162#define LOOP_UNROLLING_5(idx, step, macro) LOOP_UNROLLING_4(idx, step, macro); UNROLL_INCR(idx, step, macro)
163#define LOOP_UNROLLING_6(idx, step, macro) LOOP_UNROLLING_5(idx, step, macro); UNROLL_INCR(idx, step, macro)
164#define LOOP_UNROLLING_7(idx, step, macro) LOOP_UNROLLING_6(idx, step, macro); UNROLL_INCR(idx, step, macro)
165#define LOOP_UNROLLING_8(idx, step, macro) LOOP_UNROLLING_7(idx, step, macro); UNROLL_INCR(idx, step, macro)
166#define LOOP_UNROLLING_9(idx, step, macro) LOOP_UNROLLING_8(idx, step, macro); UNROLL_INCR(idx, step, macro)
167#define LOOP_UNROLLING_10(idx, step, macro) LOOP_UNROLLING_9(idx, step, macro); UNROLL_INCR(idx, step, macro)
168#define LOOP_UNROLLING_11(idx, step, macro) LOOP_UNROLLING_10(idx, step, macro); UNROLL_INCR(idx, step, macro)
169#define LOOP_UNROLLING_12(idx, step, macro) LOOP_UNROLLING_11(idx, step, macro); UNROLL_INCR(idx, step, macro)
170#define LOOP_UNROLLING_13(idx, step, macro) LOOP_UNROLLING_12(idx, step, macro); UNROLL_INCR(idx, step, macro)
171#define LOOP_UNROLLING_14(idx, step, macro) LOOP_UNROLLING_13(idx, step, macro); UNROLL_INCR(idx, step, macro)
172#define LOOP_UNROLLING_15(idx, step, macro) LOOP_UNROLLING_14(idx, step, macro); UNROLL_INCR(idx, step, macro)
173#define LOOP_UNROLLING_16(idx, step, macro) LOOP_UNROLLING_15(idx, step, macro); UNROLL_INCR(idx, step, macro)
174#define LOOP_UNROLLING_17(idx, step, macro) LOOP_UNROLLING_16(idx, step, macro); UNROLL_INCR(idx, step, macro)
175#define LOOP_UNROLLING_18(idx, step, macro) LOOP_UNROLLING_17(idx, step, macro); UNROLL_INCR(idx, step, macro)
176#define LOOP_UNROLLING_19(idx, step, macro) LOOP_UNROLLING_18(idx, step, macro); UNROLL_INCR(idx, step, macro)
177#define LOOP_UNROLLING_20(idx, step, macro) LOOP_UNROLLING_19(idx, step, macro); UNROLL_INCR(idx, step, macro)
178#define LOOP_UNROLLING_21(idx, step, macro) LOOP_UNROLLING_20(idx, step, macro); UNROLL_INCR(idx, step, macro)
179#define LOOP_UNROLLING_22(idx, step, macro) LOOP_UNROLLING_21(idx, step, macro); UNROLL_INCR(idx, step, macro)
180#define LOOP_UNROLLING_23(idx, step, macro) LOOP_UNROLLING_22(idx, step, macro); UNROLL_INCR(idx, step, macro)
181#define LOOP_UNROLLING_24(idx, step, macro) LOOP_UNROLLING_23(idx, step, macro); UNROLL_INCR(idx, step, macro)
182#define LOOP_UNROLLING_25(idx, step, macro) LOOP_UNROLLING_24(idx, step, macro); UNROLL_INCR(idx, step, macro)
183#define LOOP_UNROLLING_26(idx, step, macro) LOOP_UNROLLING_25(idx, step, macro); UNROLL_INCR(idx, step, macro)
184#define LOOP_UNROLLING_27(idx, step, macro) LOOP_UNROLLING_26(idx, step, macro); UNROLL_INCR(idx, step, macro)
185#define LOOP_UNROLLING_28(idx, step, macro) LOOP_UNROLLING_27(idx, step, macro); UNROLL_INCR(idx, step, macro)
186#define LOOP_UNROLLING_29(idx, step, macro) LOOP_UNROLLING_28(idx, step, macro); UNROLL_INCR(idx, step, macro)
187#define LOOP_UNROLLING_30(idx, step, macro) LOOP_UNROLLING_29(idx, step, macro); UNROLL_INCR(idx, step, macro)
188#define LOOP_UNROLLING_31(idx, step, macro) LOOP_UNROLLING_30(idx, step, macro); UNROLL_INCR(idx, step, macro)
189#define LOOP_UNROLLING_32(idx, step, macro) LOOP_UNROLLING_31(idx, step, macro); UNROLL_INCR(idx, step, macro)
190#define LOOP_UNROLLING_33(idx, step, macro) LOOP_UNROLLING_32(idx, step, macro); UNROLL_INCR(idx, step, macro)
191#define LOOP_UNROLLING_34(idx, step, macro) LOOP_UNROLLING_33(idx, step, macro); UNROLL_INCR(idx, step, macro)
192#define LOOP_UNROLLING_35(idx, step, macro) LOOP_UNROLLING_34(idx, step, macro); UNROLL_INCR(idx, step, macro)
193#define LOOP_UNROLLING_36(idx, step, macro) LOOP_UNROLLING_35(idx, step, macro); UNROLL_INCR(idx, step, macro)
194#define LOOP_UNROLLING_37(idx, step, macro) LOOP_UNROLLING_36(idx, step, macro); UNROLL_INCR(idx, step, macro)
195#define LOOP_UNROLLING_38(idx, step, macro) LOOP_UNROLLING_37(idx, step, macro); UNROLL_INCR(idx, step, macro)
196#define LOOP_UNROLLING_39(idx, step, macro) LOOP_UNROLLING_38(idx, step, macro); UNROLL_INCR(idx, step, macro)
197#define LOOP_UNROLLING_40(idx, step, macro) LOOP_UNROLLING_39(idx, step, macro); UNROLL_INCR(idx, step, macro)
198#define LOOP_UNROLLING_41(idx, step, macro) LOOP_UNROLLING_40(idx, step, macro); UNROLL_INCR(idx, step, macro)
199#define LOOP_UNROLLING_42(idx, step, macro) LOOP_UNROLLING_41(idx, step, macro); UNROLL_INCR(idx, step, macro)
200#define LOOP_UNROLLING_43(idx, step, macro) LOOP_UNROLLING_42(idx, step, macro); UNROLL_INCR(idx, step, macro)
201#define LOOP_UNROLLING_44(idx, step, macro) LOOP_UNROLLING_43(idx, step, macro); UNROLL_INCR(idx, step, macro)
202#define LOOP_UNROLLING_45(idx, step, macro) LOOP_UNROLLING_44(idx, step, macro); UNROLL_INCR(idx, step, macro)
203#define LOOP_UNROLLING_46(idx, step, macro) LOOP_UNROLLING_45(idx, step, macro); UNROLL_INCR(idx, step, macro)
204#define LOOP_UNROLLING_47(idx, step, macro) LOOP_UNROLLING_46(idx, step, macro); UNROLL_INCR(idx, step, macro)
205#define LOOP_UNROLLING_48(idx, step, macro) LOOP_UNROLLING_47(idx, step, macro); UNROLL_INCR(idx, step, macro)
206#define LOOP_UNROLLING_49(idx, step, macro) LOOP_UNROLLING_48(idx, step, macro); UNROLL_INCR(idx, step, macro)
207#define LOOP_UNROLLING_50(idx, step, macro) LOOP_UNROLLING_49(idx, step, macro); UNROLL_INCR(idx, step, macro)
208#define LOOP_UNROLLING_51(idx, step, macro) LOOP_UNROLLING_50(idx, step, macro); UNROLL_INCR(idx, step, macro)
209#define LOOP_UNROLLING_52(idx, step, macro) LOOP_UNROLLING_51(idx, step, macro); UNROLL_INCR(idx, step, macro)
210#define LOOP_UNROLLING_53(idx, step, macro) LOOP_UNROLLING_52(idx, step, macro); UNROLL_INCR(idx, step, macro)
211#define LOOP_UNROLLING_54(idx, step, macro) LOOP_UNROLLING_53(idx, step, macro); UNROLL_INCR(idx, step, macro)
212#define LOOP_UNROLLING_55(idx, step, macro) LOOP_UNROLLING_54(idx, step, macro); UNROLL_INCR(idx, step, macro)
213#define LOOP_UNROLLING_56(idx, step, macro) LOOP_UNROLLING_55(idx, step, macro); UNROLL_INCR(idx, step, macro)
214#define LOOP_UNROLLING_57(idx, step, macro) LOOP_UNROLLING_56(idx, step, macro); UNROLL_INCR(idx, step, macro)
215#define LOOP_UNROLLING_58(idx, step, macro) LOOP_UNROLLING_57(idx, step, macro); UNROLL_INCR(idx, step, macro)
216#define LOOP_UNROLLING_59(idx, step, macro) LOOP_UNROLLING_58(idx, step, macro); UNROLL_INCR(idx, step, macro)
217#define LOOP_UNROLLING_60(idx, step, macro) LOOP_UNROLLING_59(idx, step, macro); UNROLL_INCR(idx, step, macro)
218#define LOOP_UNROLLING_61(idx, step, macro) LOOP_UNROLLING_60(idx, step, macro); UNROLL_INCR(idx, step, macro)
219#define LOOP_UNROLLING_62(idx, step, macro) LOOP_UNROLLING_61(idx, step, macro); UNROLL_INCR(idx, step, macro)
220#define LOOP_UNROLLING_63(idx, step, macro) LOOP_UNROLLING_62(idx, step, macro); UNROLL_INCR(idx, step, macro)
221#define LOOP_UNROLLING_64(idx, step, macro) LOOP_UNROLLING_63(idx, step, macro); UNROLL_INCR(idx, step, macro)
222#define LOOP_UNROLLING_65(idx, step, macro) LOOP_UNROLLING_64(idx, step, macro); UNROLL_INCR(idx, step, macro)
223#define LOOP_UNROLLING_66(idx, step, macro) LOOP_UNROLLING_65(idx, step, macro); UNROLL_INCR(idx, step, macro)
224#define LOOP_UNROLLING_67(idx, step, macro) LOOP_UNROLLING_66(idx, step, macro); UNROLL_INCR(idx, step, macro)
225#define LOOP_UNROLLING_68(idx, step, macro) LOOP_UNROLLING_67(idx, step, macro); UNROLL_INCR(idx, step, macro)
226#define LOOP_UNROLLING_69(idx, step, macro) LOOP_UNROLLING_68(idx, step, macro); UNROLL_INCR(idx, step, macro)
227#define LOOP_UNROLLING_70(idx, step, macro) LOOP_UNROLLING_69(idx, step, macro); UNROLL_INCR(idx, step, macro)
228#define LOOP_UNROLLING_71(idx, step, macro) LOOP_UNROLLING_70(idx, step, macro); UNROLL_INCR(idx, step, macro)
229#define LOOP_UNROLLING_72(idx, step, macro) LOOP_UNROLLING_71(idx, step, macro); UNROLL_INCR(idx, step, macro)
230#define LOOP_UNROLLING_73(idx, step, macro) LOOP_UNROLLING_72(idx, step, macro); UNROLL_INCR(idx, step, macro)
231#define LOOP_UNROLLING_74(idx, step, macro) LOOP_UNROLLING_73(idx, step, macro); UNROLL_INCR(idx, step, macro)
232#define LOOP_UNROLLING_75(idx, step, macro) LOOP_UNROLLING_74(idx, step, macro); UNROLL_INCR(idx, step, macro)
233#define LOOP_UNROLLING_76(idx, step, macro) LOOP_UNROLLING_75(idx, step, macro); UNROLL_INCR(idx, step, macro)
234#define LOOP_UNROLLING_77(idx, step, macro) LOOP_UNROLLING_76(idx, step, macro); UNROLL_INCR(idx, step, macro)
235#define LOOP_UNROLLING_78(idx, step, macro) LOOP_UNROLLING_77(idx, step, macro); UNROLL_INCR(idx, step, macro)
236#define LOOP_UNROLLING_79(idx, step, macro) LOOP_UNROLLING_78(idx, step, macro); UNROLL_INCR(idx, step, macro)
237#define LOOP_UNROLLING_80(idx, step, macro) LOOP_UNROLLING_79(idx, step, macro); UNROLL_INCR(idx, step, macro)
238#define LOOP_UNROLLING_81(idx, step, macro) LOOP_UNROLLING_80(idx, step, macro); UNROLL_INCR(idx, step, macro)
239#define LOOP_UNROLLING_82(idx, step, macro) LOOP_UNROLLING_81(idx, step, macro); UNROLL_INCR(idx, step, macro)
240#define LOOP_UNROLLING_83(idx, step, macro) LOOP_UNROLLING_82(idx, step, macro); UNROLL_INCR(idx, step, macro)
241#define LOOP_UNROLLING_84(idx, step, macro) LOOP_UNROLLING_83(idx, step, macro); UNROLL_INCR(idx, step, macro)
242#define LOOP_UNROLLING_85(idx, step, macro) LOOP_UNROLLING_84(idx, step, macro); UNROLL_INCR(idx, step, macro)
243#define LOOP_UNROLLING_86(idx, step, macro) LOOP_UNROLLING_85(idx, step, macro); UNROLL_INCR(idx, step, macro)
244#define LOOP_UNROLLING_87(idx, step, macro) LOOP_UNROLLING_86(idx, step, macro); UNROLL_INCR(idx, step, macro)
245#define LOOP_UNROLLING_88(idx, step, macro) LOOP_UNROLLING_87(idx, step, macro); UNROLL_INCR(idx, step, macro)
246#define LOOP_UNROLLING_89(idx, step, macro) LOOP_UNROLLING_88(idx, step, macro); UNROLL_INCR(idx, step, macro)
247#define LOOP_UNROLLING_90(idx, step, macro) LOOP_UNROLLING_89(idx, step, macro); UNROLL_INCR(idx, step, macro)
248#define LOOP_UNROLLING_91(idx, step, macro) LOOP_UNROLLING_90(idx, step, macro); UNROLL_INCR(idx, step, macro)
249#define LOOP_UNROLLING_92(idx, step, macro) LOOP_UNROLLING_91(idx, step, macro); UNROLL_INCR(idx, step, macro)
250#define LOOP_UNROLLING_93(idx, step, macro) LOOP_UNROLLING_92(idx, step, macro); UNROLL_INCR(idx, step, macro)
251#define LOOP_UNROLLING_94(idx, step, macro) LOOP_UNROLLING_93(idx, step, macro); UNROLL_INCR(idx, step, macro)
252#define LOOP_UNROLLING_95(idx, step, macro) LOOP_UNROLLING_94(idx, step, macro); UNROLL_INCR(idx, step, macro)
253#define LOOP_UNROLLING_96(idx, step, macro) LOOP_UNROLLING_95(idx, step, macro); UNROLL_INCR(idx, step, macro)
254#define LOOP_UNROLLING_97(idx, step, macro) LOOP_UNROLLING_96(idx, step, macro); UNROLL_INCR(idx, step, macro)
255#define LOOP_UNROLLING_98(idx, step, macro) LOOP_UNROLLING_97(idx, step, macro); UNROLL_INCR(idx, step, macro)
256#define LOOP_UNROLLING_99(idx, step, macro) LOOP_UNROLLING_98(idx, step, macro); UNROLL_INCR(idx, step, macro)
257#define LOOP_UNROLLING_100(idx, step, macro) LOOP_UNROLLING_99(idx, step, macro); UNROLL_INCR(idx, step, macro)
258#define LOOP_UNROLLING_101(idx, step, macro) LOOP_UNROLLING_100(idx, step, macro); UNROLL_INCR(idx, step, macro)
259#define LOOP_UNROLLING_102(idx, step, macro) LOOP_UNROLLING_101(idx, step, macro); UNROLL_INCR(idx, step, macro)
260#define LOOP_UNROLLING_103(idx, step, macro) LOOP_UNROLLING_102(idx, step, macro); UNROLL_INCR(idx, step, macro)
261#define LOOP_UNROLLING_104(idx, step, macro) LOOP_UNROLLING_103(idx, step, macro); UNROLL_INCR(idx, step, macro)
262#define LOOP_UNROLLING_105(idx, step, macro) LOOP_UNROLLING_104(idx, step, macro); UNROLL_INCR(idx, step, macro)
263#define LOOP_UNROLLING_106(idx, step, macro) LOOP_UNROLLING_105(idx, step, macro); UNROLL_INCR(idx, step, macro)
264#define LOOP_UNROLLING_107(idx, step, macro) LOOP_UNROLLING_106(idx, step, macro); UNROLL_INCR(idx, step, macro)
265#define LOOP_UNROLLING_108(idx, step, macro) LOOP_UNROLLING_107(idx, step, macro); UNROLL_INCR(idx, step, macro)
266#define LOOP_UNROLLING_109(idx, step, macro) LOOP_UNROLLING_108(idx, step, macro); UNROLL_INCR(idx, step, macro)
267#define LOOP_UNROLLING_110(idx, step, macro) LOOP_UNROLLING_109(idx, step, macro); UNROLL_INCR(idx, step, macro)
268#define LOOP_UNROLLING_111(idx, step, macro) LOOP_UNROLLING_110(idx, step, macro); UNROLL_INCR(idx, step, macro)
269#define LOOP_UNROLLING_112(idx, step, macro) LOOP_UNROLLING_111(idx, step, macro); UNROLL_INCR(idx, step, macro)
270#define LOOP_UNROLLING_113(idx, step, macro) LOOP_UNROLLING_112(idx, step, macro); UNROLL_INCR(idx, step, macro)
271#define LOOP_UNROLLING_114(idx, step, macro) LOOP_UNROLLING_113(idx, step, macro); UNROLL_INCR(idx, step, macro)
272#define LOOP_UNROLLING_115(idx, step, macro) LOOP_UNROLLING_114(idx, step, macro); UNROLL_INCR(idx, step, macro)
273#define LOOP_UNROLLING_116(idx, step, macro) LOOP_UNROLLING_115(idx, step, macro); UNROLL_INCR(idx, step, macro)
274#define LOOP_UNROLLING_117(idx, step, macro) LOOP_UNROLLING_116(idx, step, macro); UNROLL_INCR(idx, step, macro)
275#define LOOP_UNROLLING_118(idx, step, macro) LOOP_UNROLLING_117(idx, step, macro); UNROLL_INCR(idx, step, macro)
276#define LOOP_UNROLLING_119(idx, step, macro) LOOP_UNROLLING_118(idx, step, macro); UNROLL_INCR(idx, step, macro)
277#define LOOP_UNROLLING_120(idx, step, macro) LOOP_UNROLLING_119(idx, step, macro); UNROLL_INCR(idx, step, macro)
278#define LOOP_UNROLLING_121(idx, step, macro) LOOP_UNROLLING_120(idx, step, macro); UNROLL_INCR(idx, step, macro)
279#define LOOP_UNROLLING_122(idx, step, macro) LOOP_UNROLLING_121(idx, step, macro); UNROLL_INCR(idx, step, macro)
280#define LOOP_UNROLLING_123(idx, step, macro) LOOP_UNROLLING_122(idx, step, macro); UNROLL_INCR(idx, step, macro)
281#define LOOP_UNROLLING_124(idx, step, macro) LOOP_UNROLLING_123(idx, step, macro); UNROLL_INCR(idx, step, macro)
282#define LOOP_UNROLLING_125(idx, step, macro) LOOP_UNROLLING_124(idx, step, macro); UNROLL_INCR(idx, step, macro)
283#define LOOP_UNROLLING_126(idx, step, macro) LOOP_UNROLLING_125(idx, step, macro); UNROLL_INCR(idx, step, macro)
284#define LOOP_UNROLLING_127(idx, step, macro) LOOP_UNROLLING_126(idx, step, macro); UNROLL_INCR(idx, step, macro)
285#define LOOP_UNROLLING_128(idx, step, macro) LOOP_UNROLLING_127(idx, step, macro); UNROLL_INCR(idx, step, macro)
286
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100287#define LOOP_UNROLLING_STR(type, idx, start, step, num, macro) \
288 { \
289 type idx = start; \
290 LOOP_UNROLLING_##num(idx, step, macro); \
291 }
Giorgio Arenaea8d2662021-05-20 11:36:56 +0100292#else // !defined(UNROLL_WITH_PRAGMA)
293#define LOOP_UNROLLING_STR(type, idx, start, step, num, macro) \
294 { \
295 _Pragma("unroll") \
296 for(type idx = start; idx < (num * step); idx += step) \
297 { \
298 (macro); \
299 } \
300 }
301#endif // !defined(UNROLL_WITH_PRAGMA)
302#define LOOP_UNROLLING(type, idx, start, step, num, macro) LOOP_UNROLLING_STR(type, idx, start, step, num, macro)
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000303
304/** Get the get_global_id with partial N0. This function is useful when the dimension is not multiple of N0 and we need to use a partial N0
305 * to avoid out-of-bound read/write
306 *
307 * @note PARTIAL_N0 is used for get_global_id(n) = 0.
308 *
309 * @param[in] IDX get_global_id index (0,1 and 2 only)
310 * @param[in] N0 Number of elements read/written on the IDX direction
311 * @param[in] PARTIAL_N0 Number of elements read/written on the IDX direction for get_global_id(IDX) = 0. If zero,
312 * the Number of elements read/written on the IDX direction for get_global_id(IDX) = 0 is N0
313 */
314#define GET_SPATIAL_IDX(IDX, N0, PARTIAL_N0) (max((int)(get_global_id(IDX) * N0 - (N0 - PARTIAL_N0) % N0), 0))
315
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000316/** Dot product integet 8bit function
317 *
318 * @note Performs: c += dot(a, b)
319 *
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100320 * @param[in] A_DATA_TYPE A (lhs) data type
321 * @param[in] B_DATA_TYPE B (rhs) data type
322 * @param[in] C_DATA_TYPE C (accumulator) data type
323 * @param[in] K0 Number of accumulations
324 * @param[in] a OpenCL vector a
325 * @param[in] b OpenCL vector b
326 * @param[in] c Scalar variable c
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000327 */
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100328#define DOT_PRODUCT_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, b, c) DOT_PRODUCT_INTEGER8_STR(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, b, c)
329#define DOT_PRODUCT_INTEGER8_STR(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, b, c) DOT_PRODUCT##K0##_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c)
330#define DOT_PRODUCT1_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000331 ({ \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100332 c += (C_DATA_TYPE)(a) * (C_DATA_TYPE)(b); \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000333 })
334#if defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100335#define DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c = arm_dot_acc((A_DATA_TYPE##4)((a).s01, (A_DATA_TYPE##2)(0)), (B_DATA_TYPE##4)(((b).s01), (B_DATA_TYPE##2)(0)), (c));
336#define DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c = arm_dot_acc((A_DATA_TYPE##4)((a).s012, (A_DATA_TYPE)0), (B_DATA_TYPE##4)(((b).s012), (B_DATA_TYPE)0), (c));
337#define DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c = arm_dot_acc((a), (b), (c));
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000338#elif defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
Michalis Spyrouc38ca382021-07-14 13:30:28 +0100339#define DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c += arm_dot((A_DATA_TYPE##4)((a).s01, (A_DATA_TYPE##2)(0)), (B_DATA_TYPE##4)(((b).s01), (B_DATA_TYPE##2)(0)));
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100340#define DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c += arm_dot((A_DATA_TYPE##4)((a).s012, (A_DATA_TYPE)0), (B_DATA_TYPE##4)(((b).s012), (B_DATA_TYPE)0));
341#define DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) c += arm_dot((a), (b));
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000342#else // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100343#define DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
344 ({ \
345 c += (C_DATA_TYPE)(a).s0 * (C_DATA_TYPE)(b).s0; \
346 c += (C_DATA_TYPE)(a).s1 * (C_DATA_TYPE)(b).s1; \
347 })
348#define DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
349 ({ \
350 DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c); \
351 c += (C_DATA_TYPE)(a).s2 * (C_DATA_TYPE)(b).s2; \
352 })
353#define DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, x, y, val) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000354 ({ \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100355 val += (C_DATA_TYPE)(x).s0 * (C_DATA_TYPE)(y).s0; \
356 val += (C_DATA_TYPE)(x).s1 * (C_DATA_TYPE)(y).s1; \
357 val += (C_DATA_TYPE)(x).s2 * (C_DATA_TYPE)(y).s2; \
358 val += (C_DATA_TYPE)(x).s3 * (C_DATA_TYPE)(y).s3; \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000359 })
360#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100361#define DOT_PRODUCT5_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
362 ({ \
363 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s0123), ((b).s0123), c); \
364 DOT_PRODUCT1_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s4), ((b).s4), c); \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000365 })
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100366#define DOT_PRODUCT6_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
367 ({ \
368 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s0123), ((b).s0123), c); \
369 DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s45), ((b).s45), c); \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000370 })
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100371#define DOT_PRODUCT7_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
372 ({ \
373 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s0123), ((b).s0123), c); \
374 DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s456), ((b).s456), c); \
375 })
376#define DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
377 ({ \
378 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).lo), ((b).lo), c); \
379 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).hi), ((b).hi), c); \
380 })
381#define DOT_PRODUCT9_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
382 ({ \
383 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
384 DOT_PRODUCT1_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s8), ((b).s8), c); \
385 })
386#define DOT_PRODUCT10_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
387 ({ \
388 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
389 DOT_PRODUCT2_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89), ((b).s89), c); \
390 })
391#define DOT_PRODUCT11_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
392 ({ \
393 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
394 DOT_PRODUCT3_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89A), ((b).s89A), c); \
395 })
396#define DOT_PRODUCT12_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
397 ({ \
398 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
399 DOT_PRODUCT4_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89AB), ((b).s89AB), c); \
400 })
401#define DOT_PRODUCT13_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
402 ({ \
403 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
404 DOT_PRODUCT5_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89ABC), ((b).s89ABC), c); \
405 })
406#define DOT_PRODUCT14_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
407 ({ \
408 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
409 DOT_PRODUCT6_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89ABCD), ((b).s89ABCD), c); \
410 })
411#define DOT_PRODUCT15_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
412 ({ \
413 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s01234567), ((b).s01234567), c); \
414 DOT_PRODUCT7_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).s89ABCDE), ((b).s89ABCDE), c); \
415 })
416#define DOT_PRODUCT16_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, a, b, c) \
417 ({ \
418 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).lo), ((b).lo), c); \
419 DOT_PRODUCT8_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, ((a).hi), ((b).hi), c); \
420 })
421
422/** Dot product integet 8bit function
423 *
424 * @note Performs: c += dot(a, b)
425 *
426 * @param[in] A_DATA_TYPE A (lhs) data type
427 * @param[in] B_DATA_TYPE B (rhs) data type
428 * @param[in] C_DATA_TYPE C (accumulator) data type
429 * @param[in] K0 Number of accumulations
430 * @param[in] a OpenCL vector a
431 * @param[in] c Scalar variable c
432 */
433#define REDUCE_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, c) REDUCE_INTEGER8_STR(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, c)
434#define REDUCE_INTEGER8_STR(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, c) DOT_PRODUCT_INTEGER8(A_DATA_TYPE, B_DATA_TYPE, C_DATA_TYPE, K0, a, (TILE_VECTOR_TYPE##K0(B_DATA_TYPE))1, c)
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000435
436/** Load a vector from global memory (tensor)
437 *
438 * @param[in] DATA_TYPE Data type
439 * @param[in] WIDTH Number of dst columns
440 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image).
441 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
442 * @param[in] TENSOR Tensor basename
443 * @param[in] X Starting X position
444 * @param[in] Y Starting Y position
445 * @param[in] STRIDE_Y Stride Y (in bytes)
446 */
447#define V_LOAD(DATA_TYPE, WIDTH, TENSOR_TYPE, TENSOR, X, Y, STRIDE_Y) V_LOAD_STR(DATA_TYPE, WIDTH, TENSOR_TYPE, TENSOR, X, Y, STRIDE_Y)
448#define V_LOAD_STR(DATA_TYPE, WIDTH, TENSOR_TYPE, TENSOR, X, Y, STRIDE_Y) V_LOAD_##TENSOR_TYPE(DATA_TYPE, WIDTH, TENSOR, X, Y, STRIDE_Y)
449#define V_LOAD_BUFFER(DATA_TYPE, WIDTH, TENSOR, X, Y, STRIDE_Y) \
450 VLOAD(WIDTH) \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100451 (0, (__global DATA_TYPE *)(TENSOR##_ptr + TENSOR##_offset_first_element_in_bytes + (X) * sizeof(DATA_TYPE) + (Y) * (STRIDE_Y)))
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000452#define V_LOAD_IMAGE(DATA_TYPE, WIDTH, TENSOR, X, Y, STRIDE_Y) READ_IMAGE2D(DATA_TYPE, CONVERT_VECTOR_SIZE_TO_PIXEL_UNIT(WIDTH), TENSOR##_img, (X) / 4, (Y))
453
454/** Load a tile from global memory (tensor)
455 *
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100456 * @param[in] DATA_TYPE Data type
457 * @param[in] HEIGHT Number of dst rows
458 * @param[in] WIDTH Number of dst columns
459 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image).
460 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
461 * @param[in] TENSOR Tensor basename
462 * @param[in] X Starting X position
463 * @param[in] Y Starting Y position
464 * @param[in] YI_MULTIPLIER Parameter used to multiply the internal row increment (_i).
465 * In common cases should be 1 but it becomes useful when we want to load rows which are multiple of STRIDE_Y. (e.g. loading the weights of convolution layer).
466 * In this case the address calculation is performed as: (Y + _i * Y_MULTIPLIER) * STRIDE_Y
467 * @param[in] STRIDE_Y Stride Y (in bytes) used to load each row.
468 * @param[out] dst Output tile
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000469 */
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100470#define T_LOAD(DATA_TYPE, HEIGHT, WIDTH, TENSOR_TYPE, TENSOR, X, Y, YI_MULTIPLIER, STRIDE_Y, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100471 ({ \
472 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
473 { \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100474 dst[_i].v = V_LOAD(DATA_TYPE, WIDTH, TENSOR_TYPE, TENSOR, X, ((Y) + _i * (int)(YI_MULTIPLIER)), STRIDE_Y); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100475 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000476 })
477
478/** Load a tile from global memory (tensor) using an indirect Y index tile
479 *
480 * @param[in] DATA_TYPE Data type
481 * @param[in] HEIGHT Number of dst rows
482 * @param[in] WIDTH Number of dst columns
483 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image). Currently BUFFER only is supported
484 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
485 * @param[in] TENSOR Tensor basename
486 * @param[in] X Starting X position
487 * @param[in] STRIDE_Y Stride Y (in bytes)
488 * @param[in] indirect_y Indirect Y index tile
489 * @param[out] dst Output tile
490 */
491#define T_LOAD_INDIRECT(DATA_TYPE, HEIGHT, WIDTH, TENSOR_TYPE, TENSOR, X, STRIDE_Y, indirect_y, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100492 ({ \
493 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
494 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000495 dst[_i].v = V_LOAD(DATA_TYPE, WIDTH, TENSOR_TYPE, TENSOR, X, (indirect_y[_i].v), STRIDE_Y); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100496 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000497 })
498
Adnan AlSinan3e155a52021-12-10 12:34:02 +0000499/** Load a tile from global memory (tensor) using an indirect Y index tile and conditionally use a different length for the load
500 *
501 * @note If WIDTH1_CONDITION is true, the load will use the WIDTH1 length for the store
502 * @note The vectors are stored in reverse order so the invalid rows are overwritten by the valid ones
503 *
504 * @param[in] DATA_TYPE Data type
505 * @param[in] HEIGHT Number of dst rows
506 * @param[in] WIDTH0 Store width to use if WIDTH1_CONDITION = false
507 * @param[in] WIDTH1 Store width to use if WIDTH1_CONDITION = true
508 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image).
509 * In case of cl_image, only WIDTH multiples of 4 are supported (4, 8, 16)
510 * @param[in] TENSOR Tensor basename
511 * @param[in] X Starting X position
512 * @param[in] STRIDE_Y Stride Y (in bytes) used to load each row.
513 * @param[in] WIDTH1_CONDITION Condition to select the WIDTH1 store
514 * @param[out] dst Output tile
515 * @param[out] indirect_y Indirect Y index tile
516 */
517#define T_LOAD_INDIRECT_WIDTH_SELECT(DATA_TYPE, HEIGHT, WIDTH0, WIDTH1, TENSOR_TYPE, TENSOR, X, STRIDE_Y, WIDTH1_CONDITION, dst, indirect_y) \
518 ({ \
519 if(WIDTH1_CONDITION) \
520 { \
521 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
522 { \
523 VLOAD_PARTIAL(WIDTH0, WIDTH1) \
524 (dst[HEIGHT - 1 - _i].v, 0, (__global DATA_TYPE *)(TENSOR##_ptr + TENSOR##_offset_first_element_in_bytes + (X) * sizeof(DATA_TYPE) + (indirect_y[HEIGHT - 1 - _i].v) * STRIDE_Y)); \
525 }) \
526 } \
527 else \
528 { \
529 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
530 { \
531 dst[HEIGHT - 1 - _i].v = V_LOAD(DATA_TYPE, WIDTH0, TENSOR_TYPE, TENSOR, X, (indirect_y[HEIGHT - 1 - _i].v), STRIDE_Y); \
532 }) \
533 } \
534 })
Gian Marco Iodice534b8892021-04-01 16:17:16 +0100535/** Load a tile from global memory (tensor) when the tensor is stored using a NHWC layout
536 *
537 * @param[in] DATA_TYPE Data type
538 * @param[in] TILE_HEIGHT Number of elements to load from Y (height) dimension
539 * @param[in] TILE_WIDTH Number of elements to load from X (width) dimension
540 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
541 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image). Currently BUFFER only is supported
542 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
543 * @param[in] TENSOR Tensor basename
544 * @param[in] B Starting batch index
545 * @param[in] Y Starting Y index
546 * @param[in] X Starting X index
547 * @param[in] C Starting C index
548 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
549 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
550 * @param[in] STRIDE_Y Stride Y (in bytes)
551 * @param[out] dst Output tile
552 */
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100553#define T_LOAD_NHWC(DATA_TYPE, TILE_HEIGHT, TILE_WIDTH, TILE_CHANNELS, TENSOR_TYPE, TENSOR, B, Y, X, C, TENSOR_WIDTH, TENSOR_HEIGHT, STRIDE_Y, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100554 ({ \
555 LOOP_UNROLLING(int, _yk, 0, 1, TILE_HEIGHT, \
556 { \
557 LOOP_UNROLLING(int, _xk, 0, 1, TILE_WIDTH, \
558 { \
559 int _src_y = (X) + _xk + ((Y) + _yk) * (TENSOR_WIDTH); \
560 _src_y += (B) * (int)(TENSOR_WIDTH) * (int)(TENSOR_HEIGHT); \
Gian Marco Iodice534b8892021-04-01 16:17:16 +0100561 int _src_valid_y = (((X) + _xk) >= 0 && ((X) + _xk) < (int)(TENSOR_WIDTH) && ((Y) + _yk) >= 0 && ((Y) + _yk) < (int)(TENSOR_HEIGHT)); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100562 if(_src_valid_y != 0) \
563 { \
564 dst[_xk + _yk * (TILE_WIDTH)].v = V_LOAD(DATA_TYPE, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, _src_y, STRIDE_Y); \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100565 } \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100566 }) \
567 }) \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100568 })
569
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100570/** Load a tile from global memory (tensor) when the tensor is stored using a NHWC layout with dilation for the X and Y increments
571 *
572 * @param[in] DATA_TYPE Data type
573 * @param[in] TILE_HEIGHT Number of elements to load from Y (height) dimension
574 * @param[in] TILE_WIDTH Number of elements to load from X (width) dimension
575 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
576 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image). Currently BUFFER only is supported
577 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
578 * @param[in] TENSOR Tensor basename
579 * @param[in] B Starting batch index
580 * @param[in] Y Starting Y index
581 * @param[in] X Starting X index
582 * @param[in] C Starting C index
583 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
584 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
585 * @param[in] DILATION_X Dilation for the X increment
586 * @param[in] DILATION_Y Dilation for the Y increment
587 * @param[in] BOUNDARY_CHECK Boundary check flag. If true, it checks for any out-of-bound reads
588 * @param[out] dst Output tile
589 */
590#define T_LOAD_NHWC_WITH_DILATION(DATA_TYPE, TILE_HEIGHT, TILE_WIDTH, TILE_CHANNELS, TENSOR_TYPE, TENSOR, B, Y, X, C, TENSOR_WIDTH, TENSOR_HEIGHT, DILATION_X, DILATION_Y, BOUNDARY_CHECK, dst) \
591 ({ \
592 LOOP_UNROLLING(int, _yk, 0, 1, TILE_HEIGHT, \
593 { \
594 LOOP_UNROLLING(int, _xk, 0, 1, TILE_WIDTH, \
595 { \
596 int _src_y = (X) + _xk * (DILATION_X); \
597 int _src_z = ((Y) + _yk * (DILATION_Y)); \
598 int _src_w = (B); \
599 bool _src_valid_y = (((X) + _xk * (DILATION_X)) >= 0) && (((X) + _xk * (DILATION_X)) < (int)(TENSOR_WIDTH)) && (((Y) + _yk * (DILATION_Y)) >= 0) && (((Y) + _yk * (DILATION_Y)) < (int)(TENSOR_HEIGHT)); \
600 if(!(BOUNDARY_CHECK)) \
601 { \
602 dst[_xk + _yk * (TILE_WIDTH)].v = VLOAD(TILE_CHANNELS) \
603 (0, (__global DATA_TYPE *)(TENSOR##_ptr + TENSOR##_offset_first_element_in_bytes + (C) * sizeof(DATA_TYPE) + (_src_y) * (TENSOR##_stride_y) + (_src_z) * (TENSOR##_stride_z) + (_src_w) * (TENSOR##_stride_w))); \
604 } \
605 else \
606 { \
607 if(_src_valid_y) \
608 { \
609 dst[_xk + _yk * (TILE_WIDTH)].v = VLOAD(TILE_CHANNELS) \
610 (0, (__global DATA_TYPE *)(TENSOR##_ptr + TENSOR##_offset_first_element_in_bytes + (C) * sizeof(DATA_TYPE) + (_src_y) * (TENSOR##_stride_y) + (_src_z) * (TENSOR##_stride_z) + (_src_w) * (TENSOR##_stride_w))); \
611 } \
612 } \
613 }) \
614 }) \
615 })
616
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100617/** Load a tile from global memory (tensor) when the tensor is stored using a NHWC layout using indirect X and Y coordinates
618 *
619 * @param[in] DATA_TYPE Data type
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100620 * @param[in] TILE_AREA Number of elements to load from Y (height) dimension * Number of elements to load from X (width) dimension
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100621 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
622 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image). Currently BUFFER only is supported
623 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
624 * @param[in] TENSOR Tensor basename
625 * @param[in] B Starting batch index
626 * @param[in] Y Starting Y index
627 * @param[in] X Starting X index
628 * @param[in] C Starting C index
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100629 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100630 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100631 * @param[in] STRIDE_Y Stride Y (in bytes)
632 * @param[out] xi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect X coordinate
633 * @param[out] yi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect Y coordinate
634 * @param[out] dst Output tile
635 */
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100636#define T_LOAD_NHWC_INDIRECT(DATA_TYPE, TILE_AREA, TILE_CHANNELS, TENSOR_TYPE, TENSOR, B, Y, X, C, TENSOR_WIDTH, TENSOR_HEIGHT, STRIDE_Y, xi, yi, dst) \
637 ({ \
638 LOOP_UNROLLING(int, _i, 0, 1, TILE_AREA, \
639 { \
640 int _src_y = (X) + xi[_i].v + ((Y) + yi[_i].v) * (TENSOR_WIDTH); \
641 _src_y += (B) * (int)(TENSOR_WIDTH) * (int)(TENSOR_HEIGHT); \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100642 int _src_valid_y = (((X) + xi[_i].v) >= 0 && ((X) + xi[_i].v) < (int)(TENSOR_WIDTH) && ((Y) + yi[_i].v) >= 0 && ((Y) + yi[_i].v) < (int)(TENSOR_HEIGHT)); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100643 if(_src_valid_y != 0) \
644 { \
645 dst[_i].v = V_LOAD(DATA_TYPE, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, _src_y, STRIDE_Y); \
Gian Marco Iodice0b76f7d2021-04-08 17:20:00 +0100646 } \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100647 }) \
Gian Marco Iodice534b8892021-04-01 16:17:16 +0100648 })
649
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100650/** Load a tile from global memory (tensor) when the tensor is stored using a NDHWC layout using indirect X, Y and Z coordinates
651 *
652 * @param[in] DATA_TYPE Data type
653 * @param[in] TILE_AREA Number of elements to load from Y (height) dimension * Number of elements to load from X (width) dimension
654 * @param[in] TILE_CHANNELS Number of elements to load from C (channel) dimension
655 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image). Currently BUFFER only is supported
656 * In case of cl_image, only TILE_CHANNELS multiples of 4 are supported (4, 8, 16)
657 * @param[in] TENSOR Tensor basename
658 * @param[in] B Starting batch index
659 * @param[in] Z Starting Z index
660 * @param[in] Y Starting Y index
661 * @param[in] X Starting X index
662 * @param[in] C Starting C index
663 * @param[in] TENSOR_WIDTH Number of elements to load from X (width) dimension
664 * @param[in] TENSOR_HEIGHT Number of elements to load from Y (height) dimension
665 * @param[in] TENSOR_DEPTH Number of elements to load from Z (depth) dimension
666 * @param[in] STRIDE_Y Stride Y (in bytes)
667 * @param[out] xi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect X coordinate
668 * @param[out] yi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect Y coordinate
669 * @param[out] zi A tile with (TILE_WIDTH x TILE_HEIGHT) values with the indirect Z coordinate
670 * @param[out] dst Output tile
671 */
672#define T_LOAD_NDHWC_INDIRECT(DATA_TYPE, TILE_AREA, TILE_CHANNELS, TENSOR_TYPE, TENSOR, B, Z, Y, X, C, TENSOR_WIDTH, TENSOR_HEIGHT, TENSOR_DEPTH, STRIDE_Y, xi, yi, zi, dst) \
673 ({ \
674 LOOP_UNROLLING(int, _i, 0, 1, TILE_AREA, \
675 { \
676 int _src_y = (X) + xi[_i].v + ((Y) + yi[_i].v) * (TENSOR_WIDTH) + ((Z) + zi[_i].v) * (TENSOR_WIDTH * TENSOR_HEIGHT); \
677 _src_y += (B) * (int)(TENSOR_WIDTH) * (int)(TENSOR_HEIGHT) * (int)(TENSOR_DEPTH); \
678 int _src_valid_y = (((X) + xi[_i].v) >= 0 && ((X) + xi[_i].v) < (int)(TENSOR_WIDTH) && ((Y) + yi[_i].v) >= 0 && ((Y) + yi[_i].v) < (int)(TENSOR_HEIGHT) \
679 && ((Z) + zi[_i].v) >= 0 && ((Z) + zi[_i].v) < (int)(TENSOR_DEPTH)); \
680 if(_src_valid_y != 0) \
681 { \
682 dst[_i].v = V_LOAD(DATA_TYPE, TILE_CHANNELS, TENSOR_TYPE, TENSOR, C, _src_y, STRIDE_Y); \
683 } \
684 }) \
685 })
686
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000687/** Store a tile to global memory (tensor) using an indirect Y index tile and conditionally use a different length for the store
688 *
689 * @note If WIDTH1_CONDITION is true, the store will use the WIDTH1 length for the store
690 * @note The vectors are stored in reverse order so the invalid rows are overwritten by the valid ones
691 *
692 * @param[in] DATA_TYPE Data type
693 * @param[in] HEIGHT Number of src rows
694 * @param[in] WIDTH0 Store width to use if WIDTH1_CONDITION = false
695 * @param[in] WIDTH1 Store width to use if WIDTH1_CONDITION = true
696 * @param[in] TENSOR_TYPE Type of cl_type used to store the tensor in global memory (BUFFER=cl_buffer, IMAGE=cl_image). Currently BUFFER only is supported
697 * cl_image is not supported.
698 * @param[in] TENSOR Tensor basename
699 * @param[in] X Starting X position
700 * @param[in] STRIDE_Y Stride Y (in bytes)
701 * @param[in] WIDTH1_CONDITION Condition to select the WIDTH1 store
702 * @param[in] src Input tile
703 * @param[in] indirect_y Indirect Y index tile
704 */
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000705#define T_STORE_INDIRECT_WIDTH_SELECT(DATA_TYPE, HEIGHT, WIDTH0, WIDTH1, TENSOR_TYPE, TENSOR, X, STRIDE_Y, WIDTH1_CONDITION, src, indirect_y) \
706 ({ \
707 if(WIDTH1_CONDITION) \
708 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100709 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000710 { \
711 VSTORE_PARTIAL(WIDTH0, WIDTH1) \
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100712 (CONVERT(src[HEIGHT - 1 - _i].v, VEC_DATA_TYPE(DATA_TYPE, WIDTH0)), 0, (__global DATA_TYPE *)(TENSOR##_ptr + TENSOR##_offset_first_element_in_bytes + (X) * sizeof(DATA_TYPE) + (indirect_y[HEIGHT - 1 - _i].v) * STRIDE_Y)); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100713 }) \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000714 } \
715 else \
716 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100717 LOOP_UNROLLING(int, _i, 0, 1, HEIGHT, \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000718 { \
719 VSTORE(WIDTH0) \
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100720 (CONVERT(src[HEIGHT - 1 - _i].v, VEC_DATA_TYPE(DATA_TYPE, WIDTH0)), 0, (__global DATA_TYPE *)(TENSOR##_ptr + TENSOR##_offset_first_element_in_bytes + (X) * sizeof(DATA_TYPE) + (indirect_y[HEIGHT - 1 - _i].v) * STRIDE_Y)); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100721 }) \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000722 } \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000723 })
724
725/** Offset correction for the QASYMM8 computation
726 *
727 * @param[in] ACC_DATA_TYPE Accumulator data type
728 * @param[in] M0 Number of src/dst rows
729 * @param[in] N0 Number of src/dst columns
730 * @param[in] K0 Number of src columns
731 * @param[in] SRC_OFFSET Source quantization offset
732 * @param[in] WEI_OFFSET Weights quantization shift
733 * @param[in] lhs LHS tile
734 * @param[in] rhs RHS tile
735 * @param[out] dst DST tile
736 */
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100737#define T_OFFSET_CORRECTION(ACC_DATA_TYPE, M0, N0, K0, SRC_OFFSET, WEI_OFFSET, lhs, rhs, dst) \
738 ({ \
739 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
740 { \
741 ACC_DATA_TYPE _tm = 0; \
742 LOOP_UNROLLING(int, _k0, 0, 1, K0, \
743 { \
744 _tm += ((ACC_DATA_TYPE)lhs[_m0].s[_k0] * (ACC_DATA_TYPE)WEI_OFFSET); \
745 }) \
746 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
747 { \
748 dst[_m0].s[_n0] += _tm; \
749 LOOP_UNROLLING(int, _k0, 0, 1, K0, \
750 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000751 dst[_m0].s[_n0] += ((ACC_DATA_TYPE)rhs[_n0].s[_k0] * (ACC_DATA_TYPE)SRC_OFFSET); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100752 }) \
753 }) \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100754 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000755 })
756
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100757/** 8-bit quantization with fixed-point scale
758 *
759 * @param[in] SRC_DATA_TYPE SRC data type
760 * @param[in] DST_DATA_TYPE DST data type
761 * @param[in] QUANTIZATION_TYPE Quantization type (PER_TENSOR or PER_CHANNEL)
762 * @param[in] M0 Number of src/dst rows
763 * @param[in] N0 Number of src/dst columns
764 * @param[in] DST_OFFSET Quantization offset used for both the per-tensor and per-channel quantization
765 * @param[in] DST_SHIFT Quantization shift for the per-tensor quantization
766 * @param[in] DST_MULTIPLIER Quantization multiplier for the per-tensor quantization
767 * @param[in] src Input tile
768 * @param[in] dst_multipliers Output multipliers tile for the per-channel quantization
769 * @param[in] dst_shifts Output shift tile for the per-channel quantization
770 * @param[out] dst Output tile
771 */
772#define T_QUANTIZE8(SRC_DATA_TYPE, DST_DATA_TYPE, QUANTIZATION_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst_multipliers, dst_shifts, dst) T_QUANTIZE8_STR(SRC_DATA_TYPE, DST_DATA_TYPE, QUANTIZATION_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst_multipliers, dst_shifts, dst)
773#define T_QUANTIZE8_STR(SRC_DATA_TYPE, DST_DATA_TYPE, QUANTIZATION_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst_multipliers, dst_shifts, dst) T_QUANTIZE8_##QUANTIZATION_TYPE(SRC_DATA_TYPE, DST_DATA_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst_multipliers, dst_shifts, dst)
774
775/** 8-bit per-tensor quantization with fixed-point scale
776 *
777 * @param[in] SRC_DATA_TYPE SRC data type
778 * @param[in] DST_DATA_TYPE DST data type
779 * @param[in] M0 Number of src/dst rows
780 * @param[in] N0 Number of src/dst columns
781 * @param[in] DST_OFFSET Quantization offset
782 * @param[in] DST_SHIFT Quantization shift for the per-tensor quantization
783 * @param[in] DST_MULTIPLIER Quantization multiplier for the per-tensor quantization
784 * @param[in] src Input tile
785 * @param[in] dst_multipliers (unused)
786 * @param[in] dst_shifts (unused)
787 * @param[out] dst Output tile
788 */
789#define T_QUANTIZE8_PER_TENSOR(SRC_DATA_TYPE, DST_DATA_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst_multipliers, dst_shifts, dst) \
790 ({ \
791 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
792 { \
793 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
794 { \
795 SRC_DATA_TYPE _tmp = 0; \
796 SRC_DATA_TYPE _src = src[_m0].s[_n0]; \
797 _src *= select((SRC_DATA_TYPE)1, ((SRC_DATA_TYPE)1 << (SRC_DATA_TYPE)(-DST_SHIFT)), ((SRC_DATA_TYPE)DST_SHIFT < (SRC_DATA_TYPE)0)); \
798 SRC_DATA_TYPE overflow = _src == DST_MULTIPLIER && _src == INT_MIN; \
799 long a_64 = (long)(_src); \
800 long b_64 = (long)(DST_MULTIPLIER); \
801 long ab_64 = a_64 * b_64; \
802 long mask1 = 1 << 30; \
803 long mask2 = 1 - (1 << 30); \
804 long is_positive_or_zero = ab_64 >= 0; \
805 long nudge = select(mask2, mask1, is_positive_or_zero); \
806 SRC_DATA_TYPE ab_x2_high32 = CONVERT((ab_64 + nudge) / (long)(1ll << 31), SRC_DATA_TYPE); \
807 _tmp = select(ab_x2_high32, (SRC_DATA_TYPE)INT_MAX, overflow); \
808 if(DST_SHIFT >= 0) \
809 { \
Freddie Liardet767dbf92021-07-21 16:20:41 +0100810 long mask = ((((int)1) << DST_SHIFT) - (long)1); \
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100811 long threshold = _tmp < (int)0 ? (mask >> 1) + (long)1 : (mask >> 1) + 0; \
812 _tmp = (_tmp & mask) > threshold ? (_tmp >> DST_SHIFT) + (int)1 : (_tmp >> DST_SHIFT); \
813 } \
814 _tmp += DST_OFFSET; \
815 dst[_m0].s[_n0] = CONVERT_SAT(_tmp, DST_DATA_TYPE); \
816 }) \
817 }) \
818 })
819
820/** 8-bit per-channel quantization with fixed-point scale
821 *
822 * @param[in] SRC_DATA_TYPE SRC data type
823 * @param[in] DST_DATA_TYPE DST data type
824 * @param[in] M0 Number of src/dst rows
825 * @param[in] N0 Number of src/dst columns
826 * @param[in] DST_OFFSET Quantization offset
827 * @param[in] DST_SHIFT (unused)
828 * @param[in] DST_MULTIPLIER (unused)
829 * @param[in] src Input tile
830 * @param[in] dst_multipliers Output multipliers tile for the per-channel quantization
831 * @param[in] dst_shifts Output shift tile for the per-channel quantization
832 * @param[out] dst Output tile
833 */
834#define T_QUANTIZE8_PER_CHANNEL(SRC_DATA_TYPE, DST_DATA_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst_multipliers, dst_shifts, dst) \
835 ({ \
836 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
837 { \
838 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
839 { \
840 SRC_DATA_TYPE _tmp = 0; \
841 SRC_DATA_TYPE _src = src[_m0].s[_n0]; \
842 SRC_DATA_TYPE _dst_multiplier = dst_multipliers[0].s[_n0]; \
843 SRC_DATA_TYPE _dst_shift = dst_shifts[0].s[_n0]; \
844 _src *= select((SRC_DATA_TYPE)1, ((SRC_DATA_TYPE)1 << (SRC_DATA_TYPE)(-_dst_shift)), ((SRC_DATA_TYPE)_dst_shift < (SRC_DATA_TYPE)0)); \
845 SRC_DATA_TYPE overflow = _src == _dst_multiplier && _src == INT_MIN; \
846 long a_64 = (long)(_src); \
847 long b_64 = (long)(_dst_multiplier); \
848 long ab_64 = a_64 * b_64; \
849 long mask1 = 1 << 30; \
850 long mask2 = 1 - (1 << 30); \
851 long is_positive_or_zero = ab_64 >= 0; \
852 long nudge = select(mask2, mask1, is_positive_or_zero); \
853 SRC_DATA_TYPE ab_x2_high32 = CONVERT((ab_64 + nudge) / (long)(1ll << 31), SRC_DATA_TYPE); \
854 _tmp = select(ab_x2_high32, (SRC_DATA_TYPE)INT_MAX, overflow); \
855 if(_dst_shift >= 0) \
856 { \
857 long mask = ((((int)1) << _dst_shift) - (int)1); \
858 long threshold = _tmp < (int)0 ? (mask >> 1) + (long)1 : (mask >> 1) + 0; \
859 _tmp = (_tmp & mask) > threshold ? (_tmp >> _dst_shift) + (int)1 : (_tmp >> _dst_shift); \
860 } \
861 _tmp += DST_OFFSET; \
862 dst[_m0].s[_n0] = CONVERT_SAT(_tmp, DST_DATA_TYPE); \
863 }) \
864 }) \
865 })
866
867/** Quantized the 8-bit tile with fixed-point scale for asymmetric
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000868 *
869 * @param[in] SRC_DATA_TYPE SRC data type
870 * @param[in] DST_DATA_TYPE DST data type
871 * @param[in] M0 Number of src/dst rows
872 * @param[in] N0 Number of src/dst columns
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100873 * @param[in] DST_OFFSET Quantization offset used for both the per-tensor and per-channel quantization
874 * @param[in] DST_SHIFT Quantization shift for the per-tensor quantization
875 * @param[in] DST_MULTIPLIER Quantization multiplier for the per-tensor quantization
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000876 * @param[in] src Input tile
877 * @param[out] dst Output tile
878 */
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100879#define T_QUANTIZE8_ASYMMETRIC(SRC_DATA_TYPE, DST_DATA_TYPE, M0, N0, DST_OFFSET, DST_SHIFT, DST_MULTIPLIER, src, dst) \
880 ({ \
881 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
882 { \
883 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
884 { \
885 SRC_DATA_TYPE _tmp = 0; \
886 SRC_DATA_TYPE _src = src[_m0].s[_n0]; \
887 _src *= select((SRC_DATA_TYPE)1, ((SRC_DATA_TYPE)1 << (SRC_DATA_TYPE)(-DST_SHIFT)), ((SRC_DATA_TYPE)DST_SHIFT < (SRC_DATA_TYPE)0)); \
888 SRC_DATA_TYPE overflow = _src == DST_MULTIPLIER && _src == INT_MIN; \
889 long a_64 = (long)(_src); \
890 long b_64 = (long)(DST_MULTIPLIER); \
891 long ab_64 = a_64 * b_64; \
892 long mask1 = 1 << 30; \
893 long mask2 = 1 - (1 << 30); \
894 long is_positive_or_zero = ab_64 >= 0; \
895 long nudge = select(mask2, mask1, is_positive_or_zero); \
896 SRC_DATA_TYPE ab_x2_high32 = CONVERT((ab_64 + nudge) / (long)(1ll << 31), SRC_DATA_TYPE); \
897 _tmp = select(ab_x2_high32, (SRC_DATA_TYPE)INT_MAX, overflow); \
898 if(DST_SHIFT >= 0) \
899 { \
900 long mask = ((((int)1) << DST_SHIFT) - (int)1); \
901 long threshold = _tmp < (int)0 ? (mask >> 1) + (long)1 : (mask >> 1) + 0; \
902 _tmp = (_tmp & mask) > threshold ? (_tmp >> DST_SHIFT) + (int)1 : (_tmp >> DST_SHIFT); \
903 } \
904 _tmp += DST_OFFSET; \
905 dst[_m0].s[_n0] = CONVERT_SAT(_tmp, DST_DATA_TYPE); \
906 }) \
907 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000908 })
909
910/** Conditional rowset (memset by row)
911 *
912 * @note Set the row to VALUE_TO_SET if the corresponding mask == 0
913 *
914 * @param[in] DATA_TYPE Data type
915 * @param[in] M0 Number of LHS rows
916 * @param[in] N0 Number of LHS columns
917 * @param[in] VALUE_TO_SET Value to set the row
918 * @param[in, out] a Input/output tile
919 * @param[out] mask Mask to check for setting the row to VALUE_TO_SET
920 */
921#define T_ROWSET_MASK(DATA_TYPE, M0, N0, VALUE_TO_SET, a, mask) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100922 ({ \
923 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
924 { \
925 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
926 { \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000927 a[_m0].s[_n0] = select((DATA_TYPE)(a[_m0].s[_n0]), (DATA_TYPE)(VALUE_TO_SET), (SELECT_DATA_TYPE(DATA_TYPE))(mask[_m0].v == (DATA_TYPE)0)); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100928 }) \
929 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000930 })
931
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100932/** Element-wise activation for floating point types
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000933 *
934 * @note Performs: activation(LHS) = DST
935 *
936 * @param[in] DATA_TYPE SRC/DST data type
937 * @param[in] M0 Number of SRC/DST rows
938 * @param[in] N0 Number of SRC/DST columns
939 * @param[in] ACTIVATION_TYPE Activation type
940 * @param[in] A_VAL A value used for the activation (e.g. tanh_op, brelu,..)
941 * @param[in] B_VAL B value used for the activation (e.g. tanh_op, brelu,..)
942 * @param[out] src SRC tile
943 * @param[out] dst DST tile
944 */
945#define T_ACTIVATION(DATA_TYPE, M0, N0, ACTIVATION_TYPE, A_VAL, B_VAL, src, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100946 ({ \
947 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
948 { \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000949 dst[_m0].v = ACTIVATION(ACTIVATION_TYPE, DATA_TYPE, N0, src[_m0].v, A_VAL, B_VAL); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +0100950 }) \
Gian Marco Iodicea8903c82021-03-24 14:48:22 +0000951 })
952
Gian Marco Iodice8155c022021-04-16 15:08:59 +0100953// RELU Activation
954#define relu_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x) (max((DATA_TYPE)ZERO_VALUE, x))
955// Bounded RELU Activation
956#define brelu_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x) (min((DATA_TYPE)A_VAL, max((DATA_TYPE)ZERO_VALUE, x)))
957// Lower Upper Bounded RELU Activation
958#define lu_brelu_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x) (min(max(x, (DATA_TYPE)B_VAL), (DATA_TYPE)A_VAL))
959// Hard Swish Activation
960#define hard_swish_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x) (x * ((min(max((DATA_TYPE)(x + (DATA_TYPE)3.f), (DATA_TYPE)0.f), (DATA_TYPE)6.f)) * (DATA_TYPE)0.166666667f))
961// Identity Activation
962#define identity_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x) (x)
963
964#define ACT_OP_QUANTIZED(op, DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x) op##_op_quantized(DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x)
965#define ACTIVATION_QUANTIZED(op, DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x) ACT_OP_QUANTIZED(op, DATA_TYPE, VEC_SIZE, ZERO_VALUE, A_VAL, B_VAL, x)
966
967/** Element-wise activation for quantized types
968 *
969 * @note Performs: activation(LHS) = DST
970 *
971 * @param[in] DATA_TYPE SRC/DST data type
972 * @param[in] M0 Number of SRC/DST rows
973 * @param[in] N0 Number of SRC/DST columns
974 * @param[in] ACTIVATION_TYPE Activation type
975 * @param[in] ZERO_VALUE The zero value to consider in the computation
976 * @param[in] A_VAL A value used for the activation (e.g. tanh_op, brelu,..)
977 * @param[in] B_VAL B value used for the activation (e.g. tanh_op, brelu,..)
978 * @param[out] src SRC tile
979 * @param[out] dst DST tile
980 */
981#define T_ACTIVATION_QUANTIZED(DATA_TYPE, M0, N0, ACTIVATION_TYPE, ZERO_VALUE, A_VAL, B_VAL, src, dst) \
982 ({ \
983 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
984 { \
985 dst[_m0].v = ACTIVATION_QUANTIZED(ACTIVATION_TYPE, DATA_TYPE, N0, ZERO_VALUE, A_VAL, B_VAL, src[_m0].v); \
986 }) \
987 })
988
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +0000989/** Element-wise addition with a constant value
990 *
991 * @note Performs: LHS + constant = DST
992 *
993 * @param[in] DATA_TYPE LHS/RHS/DST data type
994 * @param[in] M0 Number of LHS rows
995 * @param[in] N0 Number of LHS columns
996 * @param[in] lhs LHS tile
997 * @param[in] rhs_constant Constant value
998 * @param[out] dst DST tile
999 */
1000#define T_ADD_CONSTANT(DATA_TYPE, M0, N0, lhs, rhs_constant, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001001 ({ \
1002 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1003 { \
Ramy Elgammal451c3092022-02-01 23:01:27 +00001004 LOOP_UNROLLING(int, _n0, 0, 1, N0, \
1005 { \
1006 dst[_m0].s[_n0] = lhs[_m0].s[_n0] + rhs_constant; \
1007 }) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001008 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001009 })
1010
1011/** Element-wise addition with RHS broadcasted (RHS has the X dimension only)
1012 *
1013 * @note Performs: LHS + RHS[broadcasted] = DST
1014 * @note Both tiles must have same data type
1015 *
Giorgio Arena945ae9e2021-10-13 11:13:04 +01001016 * @param[in] DST_DATA_TYPE DST data type
1017 * @param[in] M0 Number of LHS rows
1018 * @param[in] N0 Number of LHS columns
1019 * @param[in] lhs LHS tile
1020 * @param[in] rhs RHS tile
1021 * @param[out] dst DST tile
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001022 */
Giorgio Arena945ae9e2021-10-13 11:13:04 +01001023#define T_ADD_BROADCAST_X(DST_DATA_TYPE, M0, N0, lhs, rhs, dst) \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001024 ({ \
1025 LOOP_UNROLLING(int, _m0, 0, 1, M0, \
1026 { \
Giorgio Arena945ae9e2021-10-13 11:13:04 +01001027 dst[_m0].v = CONVERT(lhs[_m0].v, VEC_DATA_TYPE(DST_DATA_TYPE, N0)) + CONVERT(rhs[0].v, VEC_DATA_TYPE(DST_DATA_TYPE, N0)); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001028 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001029 })
1030
1031/** Matrix multiplication
1032 *
1033 * @note Performs: LHS X RHS + DST = DST
1034 *
1035 * @param[in] LHS_DATA_TYPE LHS tile data type
1036 * @param[in] RHS_DATA_TYPE RHS tile data type
1037 * @param[in] DST_DATA_TYPE RHS tile data type
1038 * @param[in] M0 Number of LHS rows
1039 * @param[in] N0 Number of RHS columns
1040 * @param[in] K0 Number of LHS columns
1041 * @param[in] LHS_LAYOUT LHS layout (T= transposed, NT= not transposed)
1042 * @param[in] RHS_LAYOUT RHS layout (T= transposed, NT= not transposed)
1043 * @param[in] lhs LHS tile
1044 * @param[in] rhs RHS tile
1045 * @param[in, out] dst DST tile
1046 */
1047#define T_MMUL(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, LHS_LAYOUT, RHS_LAYOUT, lhs, rhs, dst) T_MMUL_##LHS_LAYOUT##_##RHS_LAYOUT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001048#define T_MMUL_NT_T(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_##LHS_DATA_TYPE##_##RHS_DATA_TYPE##_##DST_DATA_TYPE(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1049#define T_MMUL_NT_T_float_float_float(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
Giorgio Arena945ae9e2021-10-13 11:13:04 +01001050#define T_MMUL_NT_T_half_half_float(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001051#define T_MMUL_NT_T_half_half_half(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1052#define T_MMUL_NT_T_char_char_int(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_INTEGER8(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1053#define T_MMUL_NT_T_uchar_uchar_uint(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_INTEGER8(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1054#define T_MMUL_NT_T_uchar_uchar_int(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) T_MMUL_NT_T_INTEGER8(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst)
1055#define T_MMUL_NT_T_FLOAT(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001056 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001057 LOOP_UNROLLING(int, _m, 0, 1, M0, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001058 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001059 LOOP_UNROLLING(int, _n, 0, 1, N0, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001060 { \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001061 LOOP_UNROLLING(int, _k, 0, 1, K0, \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001062 { \
Giorgio Arena945ae9e2021-10-13 11:13:04 +01001063 dst[_m].s[_n] = fma((DST_DATA_TYPE)(lhs[_m].s[_k]), (DST_DATA_TYPE)(rhs[_n].s[_k]), dst[_m].s[_n]); \
Giorgio Arenabdd16d12021-05-13 16:58:51 +01001064 }) \
1065 }) \
1066 }) \
Gian Marco Iodice5c9eed82021-03-19 11:26:20 +00001067 }
Gian Marco Iodice8155c022021-04-16 15:08:59 +01001068
1069#define T_MMUL_NT_T_INTEGER8(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, M0, N0, K0, lhs, rhs, dst) \
1070 ({ \
1071 LOOP_UNROLLING(int, _m, 0, 1, M0, \
1072 { \
1073 LOOP_UNROLLING(int, _n, 0, 1, N0, \
1074 { \
1075 DOT_PRODUCT_INTEGER8(LHS_DATA_TYPE, RHS_DATA_TYPE, DST_DATA_TYPE, K0, (lhs[_m].v), (rhs[_n].v), dst[_m].s[_n]); \
1076 }) \
1077 }) \
Gian Marco Iodice561c1762021-04-16 15:08:59 +01001078 })