blob: d11be5bbb3884979c31c2bd091c3eb1c2a93a472 [file] [log] [blame]
Giorgio Arena945ae9e2021-10-13 11:13:04 +01001/*
2 * Copyright (c) 2021 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#include "helpers.h"
26#include "tile_helpers.h"
27
28//! @cond Doxygen_Suppress
Giorgio Arena273c28c2021-10-14 15:59:15 +010029/** OpenCL kernel to compute the direct convolution 3d.
Giorgio Arena945ae9e2021-10-13 11:13:04 +010030 *
31 * @note Data layout supported: NDHWC
32 * @note Data type supported: F32/F16
33 * @note The accumulation data type must be passed at compile time using -DDATA_TYPE (e.g. -DDATA_TYPE_PROMOTED=half)
34 * @note The convolution padding (left, top and front) must be passed at compile time using -DPAD_LEFT, -DPAD_TOP and -DPAD_FRONT (e.g. -DPAD_LEFT=2, -DPAD_TOP=2, -DPAD_FRONT=2)
35 * @note The convolution strides must be passed at compile time using -DSTRIDE_X, -DSTRIDE_Y and -DSTRIDE_Z (e.g. -DSTRIDE_X=2, -DSTRIDE_Y=2, -DSTRIDE_Z=2)
36 * @note The spatial dimensions of the weights must be passed at compile time using -DWEI_WIDTH, -DWEI_HEIGHT and -DWEI_DEPTH (e.g. -DWEI_WIDTH=9, -DWEI_HEIGHT=9, -DWEI_DEPTH=9)
37 * @note The spatial dimensions of the source tensor must be passed at compile time using -DSRC_WIDTH, -DSRC_HEIGHT and -DSRC_DEPTH (e.g. -DSRC_WIDTH=96, -DSRC_HEIGHT=64, -DSRC_DEPTH=32)
38 * @note The spatial dimensions of the destination tensor must be passed at compile time using -DDST_WIDTH, -DDST_HEIGHT and -DDST_DEPTH (e.g. -DDST_WIDTH=96, -DDST_HEIGHT=64, -DDST_DEPTH=32)
39 * @note The channels of the source tensor must be passed at compile time using -DSRC_CHANNELS (e.g. -DSRC_CHANNELS=64)
Giorgio Arena273c28c2021-10-14 15:59:15 +010040 * @note The channels of the destination tensor must be passed at compile time using -DDST_CHANNELS (e.g. -DDST_CHANNELS=64)
41 * @note The data type must be passed at compile time using -DDATA_TYPE (e.g. -DDATA_TYPE=half)
42 * @note The data type of the accumulators must be passed at compile time using -DACC_DATA_TYPE (e.g. -DACC_DATA_TYPE=float)
Giorgio Arena945ae9e2021-10-13 11:13:04 +010043 * @note The number of M0 rows (width*height) to process must be passed at compile time using -DM0 (e.g. -DM0=2)
44 * @note The number of N0 output channels to process must be passed at compile time using -DN0 (e.g. -DN0=2)
45 * @note The number of K0 inner accumulations must be passed at compile time using -DK0 (e.g. -DK0=2)
46 * @note The size of the partial store block in x must be passed at compile time using -DPARTIAL_N0 (e.g. -DPARTIAL_N0=1)
47 * @note Only the following configurations of M0, N0 and K0 are currently supported:
48 * - M0 = 1, 2, 3, 4, 5, .... n
49 * - N0 = 2, 3, 4, 8, 16
50 * - K0 = 2, 3, 4, 8, 16
51 *
52 * @note If biases are used then -DHAS_BIAS has to be passed at compile time
53 *
54 * @param[in] src_ptr Pointer to the source tensor. Supported data type: F16/F32
55 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
56 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
57 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
58 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
59 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
60 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
61 * @param[in] src_stride_w Stride of the source tensor in W dimension (in bytes)
62 * @param[in] src_step_w src_stride_w * number of elements along W processed per workitem(in bytes)
63 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
64 * @param[out] dst_ptr Pointer to the destination tensor. Supported data type: same as @p src_ptr
65 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
66 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
67 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
68 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
69 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
70 * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
71 * @param[in] dst_stride_w Stride of the destination tensor in W dimension (in bytes)
72 * @param[in] dst_step_w dst_stride_w * number of elements along W processed per workitem(in bytes)
73 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
74 * @param[in] wei_ptr Pointer to the weights tensor. Supported data type: same as @p src_ptr
75 * @param[in] wei_stride_x Stride of the weights tensor in X dimension (in bytes)
76 * @param[in] wei_step_x wei_stride_x * number of elements along X processed per workitem(in bytes)
77 * @param[in] wei_stride_y Stride of the weights tensor in Y dimension (in bytes)
78 * @param[in] wei_step_y wei_stride_y * number of elements along Y processed per workitem(in bytes)
79 * @param[in] wei_stride_z Stride of the weights tensor in Z dimension (in bytes)
80 * @param[in] wei_step_z wei_stride_z * number of elements along Z processed per workitem(in bytes)
81 * @param[in] wei_stride_w Stride of the weights tensor in W dimension (in bytes)
82 * @param[in] wei_step_w wei_stride_w * number of elements along W processed per workitem(in bytes)
83 * @param[in] wei_offset_first_element_in_bytes The offset of the first element in the weights matrix
84 * @param[in] bia_ptr (Optional) Pointer to the bias tensor Supported data type: same as @p src_ptr
85 * @param[in] bia_stride_x (Optional) Stride of the bias tensor in X dimension (in bytes)
86 * @param[in] bia_step_x (Optional) bia_stride_x * number of elements along X processed per workitem(in bytes)
87 * @param[in] bia_offset_first_element_in_bytes (Optional) The offset of the first element in the bias matrix
88 */
89//! @endcond
90__kernel void direct_convolution3d_ndhwc(
91 TENSOR4D(src, BUFFER),
92 TENSOR4D(dst, BUFFER),
93 TENSOR4D(wei, BUFFER)
94#if defined(HAS_BIAS)
95 ,
96 VECTOR_DECLARATION(bia)
97#endif // defined(HAS_BIAS)
98)
99{
100#define _IWEI_WIDTH WEI_WIDTH
101#define _IWEI_HEIGHT WEI_HEIGHT
102#define _IWEI_DEPTH WEI_DEPTH
103#define _ISRC_WIDTH SRC_WIDTH
104#define _ISRC_HEIGHT SRC_HEIGHT
105#define _ISRC_DEPTH SRC_DEPTH
106#define _ISRC_CHANNELS SRC_CHANNELS
107#define _IDST_WIDTH DST_WIDTH
108#define _IDST_HEIGHT DST_HEIGHT
109#define _IDST_DEPTH DST_DEPTH
110#define _IDST_CHANNELS DST_CHANNELS
111#define _IY_MULTIPLIER (_IWEI_WIDTH * _IWEI_HEIGHT * _IWEI_DEPTH)
112
113 const int cout = GET_SPATIAL_IDX(0, N0, PARTIAL_N0); // OFM
114 const int mout = GET_SPATIAL_IDX(1, M0, 0); // WIDTH x HEIGHT x DEPTH
115 const int bout = GET_SPATIAL_IDX(2, 1, 0); // BATCH SIZE IDX
116
117 TILE(int, M0, 1, xi);
118 TILE(int, M0, 1, yi);
119 TILE(int, M0, 1, zi);
120
121 // Convert the linear index to coordinate
122 LOOP_UNROLLING(int, i, 0, 1, M0,
123 {
124 xi[i].v = ((mout + i) % _IDST_WIDTH) * STRIDE_X;
125 yi[i].v = (((mout + i) / _IDST_WIDTH) % _IDST_HEIGHT) * STRIDE_Y;
126 zi[i].v = (((mout + i) / (_IDST_WIDTH * _IDST_HEIGHT)) % _IDST_DEPTH) * STRIDE_Z;
127
128 xi[i].v -= PAD_LEFT;
129 yi[i].v -= PAD_TOP;
130 zi[i].v -= PAD_FRONT;
131 })
132
133 // Initialize the accumulators
134 TILE(ACC_DATA_TYPE, M0, N0, c);
135
136 LOOP_UNROLLING(int, i, 0, 1, M0,
137 {
138 c[i].v = (ACC_DATA_TYPE)0;
139 })
140
141 for(int i = 0; i < _IY_MULTIPLIER; ++i)
142 {
143 int ck = 0;
144 int xk = i % _IWEI_WIDTH;
145 int yk = (i / _IWEI_WIDTH) % _IWEI_HEIGHT;
146 int zk = i / (_IWEI_WIDTH * _IWEI_HEIGHT);
147
Giorgio Arena945ae9e2021-10-13 11:13:04 +0100148 int k = 0;
149 for(; k <= (_ISRC_CHANNELS - K0); k += K0)
150 {
151 TILE(DATA_TYPE, M0, K0, a);
152 TILE(DATA_TYPE, N0, K0, b);
153
154 LOOP_UNROLLING(int, i, 0, 1, M0,
155 {
156 a[i].v = (DATA_TYPE)0;
157 })
158
159 // Load tile from the src tensor
160 T_LOAD_NDHWC_INDIRECT(DATA_TYPE, M0, K0, BUFFER, src, bout, zk, yk, xk, ck, _ISRC_WIDTH, _ISRC_HEIGHT, _ISRC_DEPTH, src_stride_y, xi, yi, zi, a);
161
162 // Load tile from the weights tensor
163 const int b_offs = k + (xk * _ISRC_CHANNELS) + (yk * _ISRC_CHANNELS * _IWEI_WIDTH) + (zk * _ISRC_CHANNELS * _IWEI_WIDTH * _IWEI_HEIGHT);
164 LOOP_UNROLLING(int, i, 0, 1, N0,
165 {
166 if((cout + i) < _IDST_CHANNELS)
167 {
168 LOOP_UNROLLING(int, j, 0, 1, K0,
169 {
170 b[i].s[j] = *(__global DATA_TYPE *)(wei_ptr + wei_offset_first_element_in_bytes + (cout + i) * sizeof(DATA_TYPE) + j * wei_stride_y + b_offs * wei_stride_y);
171 })
172 }
173 })
174
175 // Compute the matrix multiplication between two tiles
176 T_MMUL(DATA_TYPE, DATA_TYPE, ACC_DATA_TYPE, M0, N0, K0, NT, T, a, b, c);
177
178 ck += K0;
179 }
180
181#if((_ISRC_CHANNELS % K0) != 0)
182 // Left-over accumulations
183 for(; k < _ISRC_CHANNELS; ++k)
184 {
185 TILE(DATA_TYPE, M0, 1, a);
186 TILE(DATA_TYPE, N0, 1, b);
187
188 LOOP_UNROLLING(int, i, 0, 1, M0,
189 {
190 a[i].v = (DATA_TYPE)0;
191 })
192
193 // Load tile from the src tensor
194 T_LOAD_NDHWC_INDIRECT(DATA_TYPE, M0, 1, BUFFER, src, bout, zk, yk, xk, ck, _ISRC_WIDTH, _ISRC_HEIGHT, _ISRC_DEPTH, src_stride_y, xi, yi, zi, a);
195
196 // Load tile from the weights tensor
197 const int b_offs = k + (xk * _ISRC_CHANNELS) + (yk * _ISRC_CHANNELS * _IWEI_WIDTH) + (zk * _ISRC_CHANNELS * _IWEI_WIDTH * _IWEI_HEIGHT);
198 LOOP_UNROLLING(int, i, 0, 1, N0,
199 {
200 if((cout + i) < _IDST_CHANNELS)
201 {
202 b[i].v = *(__global DATA_TYPE *)(wei_ptr + wei_offset_first_element_in_bytes + (cout + i) * sizeof(DATA_TYPE) + b_offs * wei_stride_y);
203 }
204 })
205
206 // // Compute the matrix multiplication between two tiles
207 T_MMUL(DATA_TYPE, DATA_TYPE, ACC_DATA_TYPE, M0, N0, 1, NT, T, a, b, c);
208
209 ++ck;
210 }
211#endif // ((_ISRC_CHANNELS % K0) != 0)
212 }
213
214#if defined(HAS_BIAS)
215 TILE(DATA_TYPE, 1, N0, bias0);
216
217 if((cout + N0) <= _IDST_CHANNELS)
218 {
219 bias0[0].v = VLOAD(N0)(0, (__global DATA_TYPE *)(bia_ptr + bia_offset_first_element_in_bytes + cout * sizeof(DATA_TYPE)));
220 }
221 else
222 {
223 VLOAD_PARTIAL(N0, PARTIAL_N0)
224 (bias0[0].v, 0, (__global DATA_TYPE *)(bia_ptr + bia_offset_first_element_in_bytes + cout * sizeof(DATA_TYPE)));
225 }
226
227 // c = c + bias[broadcasted]
228 T_ADD_BROADCAST_X(ACC_DATA_TYPE, M0, N0, c, bias0, c);
229
230#endif // HAS_BIAS
231
232 TILE(uint, M0, 1, dst_indirect_y);
233
234 // Calculate the destination indirect Y
235 LOOP_UNROLLING(int, i, 0, 1, M0,
236 {
237 dst_indirect_y[i].v = (uint)min(mout + i, (int)(_IDST_WIDTH *_IDST_HEIGHT * _IDST_DEPTH) - 1);
238 dst_indirect_y[i].v += bout * (int)(_IDST_WIDTH *_IDST_HEIGHT * _IDST_DEPTH);
239 })
240
241 bool x_cond = PARTIAL_N0 != 0 && get_global_id(0) == 0;
242
243 // Store the tile in reverse order so the invalid values are overwritten with the valid ones
244 T_STORE_INDIRECT_WIDTH_SELECT(DATA_TYPE, M0, N0, PARTIAL_N0, BUFFER, dst, cout, dst_stride_y, x_cond, c, dst_indirect_y);
245}