blob: 824306f2ba13287806c27c162dc30631ec5b42ca [file] [log] [blame]
steniu0127b386c2017-07-18 17:37:43 +01001/*
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +01002 * Copyright (c) 2016-2018 ARM Limited.
steniu0127b386c2017-07-18 17:37:43 +01003 *
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#include "helpers.h"
25
Moritz Pflanzer54f366a2017-09-25 15:36:14 +010026#undef CONVERT_SAT
27
Michalis Spyroudef665a2017-08-14 11:26:37 +010028#define ADD_OP(a, b) ((a) + (b))
29#define MUL_OP(a, b) ((a) * (b))
30#define CONVERT_SAT(a, b) ((a))
31
Gian Marco Iodice1246b632017-08-16 18:38:32 +010032#if defined(DATA_TYPE) && defined(STRIDE_X) && defined(WEIGHTS_DEPTH)
33
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010034#if STRIDE_X == 1
35#define CONVOLUTION1x3(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x3_STRIDE1(acc, src_row_ptr, weights_row_ptr)
36#elif STRIDE_X == 2 /* STRIDE_X == 1 */
37#define CONVOLUTION1x3(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x3_STRIDE2(acc, src_row_ptr, weights_row_ptr)
steniu0127b386c2017-07-18 17:37:43 +010038#else /* STRIDE_X not equals 1 or 2 */
39#error "STRIDE_X larger than 2 is not supported"
40#endif /* STRIDE_X == 2 */
41
Michalis Spyroudef665a2017-08-14 11:26:37 +010042#define CONVOLUTION1x3_STRIDE1(acc, src_row_ptr, weights_row_ptr) \
43 ({ \
steniu01db006682017-08-09 16:26:22 +010044 VEC_DATA_TYPE(DATA_TYPE, 3) \
45 weights_values0 = vload3(0, weights_row_ptr); \
Michalis Spyroudef665a2017-08-14 11:26:37 +010046 VEC_DATA_TYPE(DATA_TYPE, 8) \
47 src0 = vload8(0, src_row_ptr); \
48 VEC_DATA_TYPE(DATA_TYPE, 2) \
49 src1 = vload2(0, src_row_ptr + 8); \
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010050 \
Michalis Spyroudef665a2017-08-14 11:26:37 +010051 acc = ADD_OP(acc, MUL_OP(src0, (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s0)); \
52 acc = ADD_OP(acc, MUL_OP((VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s1234, src0.s567, src1.s0), (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s1)); \
53 acc = ADD_OP(acc, MUL_OP((VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s234, src0.s567, src1.s01), (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s2)); \
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010054 })
steniu0127b386c2017-07-18 17:37:43 +010055
Michalis Spyroudef665a2017-08-14 11:26:37 +010056#define CONVOLUTION1x3_STRIDE2(acc, src_row_ptr, weights_row_ptr) \
57 ({ \
steniu01db006682017-08-09 16:26:22 +010058 VEC_DATA_TYPE(DATA_TYPE, 3) \
59 weights_values0 = vload3(0, weights_row_ptr); \
Michalis Spyroudef665a2017-08-14 11:26:37 +010060 VEC_DATA_TYPE(DATA_TYPE, 16) \
61 src0 = vload16(0, src_row_ptr); \
62 DATA_TYPE src1 = *(src_row_ptr + 16); \
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010063 \
Michalis Spyroudef665a2017-08-14 11:26:37 +010064 acc = ADD_OP(acc, MUL_OP(src0.even, (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s0)); \
65 acc = ADD_OP(acc, MUL_OP((VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s1357, src0.s9BDF), (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s1)); \
66 acc = ADD_OP(acc, MUL_OP((VEC_DATA_TYPE(DATA_TYPE, 8))(src0.s2468, src0.sACE, src1), (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s2)); \
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010067 })
steniu0127b386c2017-07-18 17:37:43 +010068
69/** This kernel performs a direct convolution to convolve the low three dimensions.
70 *
Gian Marco Iodice1246b632017-08-16 18:38:32 +010071 * @note This OpenCL kernel works with stride_x = 1 and 2
steniu0127b386c2017-07-18 17:37:43 +010072 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010073 * @note The third dimensions of the weights tensors must be passed at compile time using -DWEIGHTS_DEPTH
Gian Marco Iodice1246b632017-08-16 18:38:32 +010074 * @note If biases are used then -DHAS_BIAS has to be passed at compile time
steniu0127b386c2017-07-18 17:37:43 +010075 *
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010076 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
steniu0127b386c2017-07-18 17:37:43 +010077 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
78 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
79 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
80 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
81 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
82 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
83 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
84 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
85 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
86 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
87 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
88 * @param[in] dst_step_y dst_stride_y * number of elements along Z processed per workitem(in bytes)
89 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
90 * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
91 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
Joel Liangf1f3ebd2017-11-10 09:59:19 +080092 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr
steniu0127b386c2017-07-18 17:37:43 +010093 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
94 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
95 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
96 * @param[in] weights_step_y weights_stride_y * number of elements along y processed per workitem(in bytes)
97 * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes)
98 * @param[in] weights_step_z weights_stride_z * number of elements along Z processed per workitem(in bytes)
99 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
100 * @param[in] biases_ptr Pointer to the biases tensor. Same as @p src_ptr
101 * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes)
102 * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes)
103 * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100104 * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension
steniu0127b386c2017-07-18 17:37:43 +0100105 */
106__kernel void direct_convolution3x3(
107 TENSOR3D_DECLARATION(src),
108 TENSOR3D_DECLARATION(dst),
109 TENSOR3D_DECLARATION(weights),
110#ifdef HAS_BIAS
111 VECTOR_DECLARATION(biases),
112#endif /* defined(HAS_BIAS) */
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100113 unsigned int weights_stride_w)
steniu0127b386c2017-07-18 17:37:43 +0100114{
115 Image src = CONVERT_TO_IMAGE_STRUCT(src);
116 Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(weights);
117 Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst);
118
Michalis Spyroudef665a2017-08-14 11:26:37 +0100119 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 8)
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100120 pixels0 = 0;
steniu0127b386c2017-07-18 17:37:43 +0100121
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100122 __global uchar *weights_addr = (__global uchar *)tensor3D_offset(&weights, 0, 0, 0);
123 __global uchar *src_addr = (__global uchar *)offset(&src, 0, 0);
steniu0127b386c2017-07-18 17:37:43 +0100124
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100125 const int kernel_index = get_global_id(2);
126 weights_addr += kernel_index * weights_stride_w;
steniu0127b386c2017-07-18 17:37:43 +0100127
Gian Marco Iodice744b5ed2017-10-06 15:44:27 +0100128 for(volatile int d = 0; d < WEIGHTS_DEPTH; ++d)
steniu0127b386c2017-07-18 17:37:43 +0100129 {
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100130 CONVOLUTION1x3(pixels0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_y));
131 CONVOLUTION1x3(pixels0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_y));
132 CONVOLUTION1x3(pixels0, (__global DATA_TYPE *)(src_addr + 2 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 2 * weights_stride_y));
steniu0127b386c2017-07-18 17:37:43 +0100133
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100134 src_addr += src_stride_z;
135 weights_addr += weights_stride_z;
steniu0127b386c2017-07-18 17:37:43 +0100136 }
137
138#ifdef HAS_BIAS
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100139 Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases);
140
Michalis Spyroudef665a2017-08-14 11:26:37 +0100141 pixels0 = ADD_OP(pixels0, (VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 8)) * ((__global DATA_TYPE *)(vector_offset(&biases, kernel_index))));
steniu0127b386c2017-07-18 17:37:43 +0100142#endif /* defined(HAS_BIAS) */
143
Michalis Spyroudef665a2017-08-14 11:26:37 +0100144 vstore8(CONVERT_SAT(pixels0, VEC_DATA_TYPE(DATA_TYPE, 8)), 0, (__global DATA_TYPE *)dst.ptr);
steniu0127b386c2017-07-18 17:37:43 +0100145}
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100146#endif //defined(DATA_TYPE) && defined(STRIDE_X) && defined(WEIGHTS_DEPTH)
147
148#if defined(WEIGHTS_DEPTH)
149
150#define CONVOLUTION1x3_BIFROST(acc, src0, src1, weights_row0) \
151 ({ \
152 acc.s0 = mad(src0.s0, weights_row0.s0, acc.s0); \
153 acc.s1 = mad(src0.s1, weights_row0.s0, acc.s1); \
154 acc.s2 = mad(src0.s2, weights_row0.s0, acc.s2); \
155 acc.s3 = mad(src0.s3, weights_row0.s0, acc.s3); \
156 acc.s0 = mad(src0.s1, weights_row0.s1, acc.s0); \
157 acc.s1 = mad(src0.s2, weights_row0.s1, acc.s1); \
158 acc.s2 = mad(src0.s3, weights_row0.s1, acc.s2); \
159 acc.s3 = mad(src1.s0, weights_row0.s1, acc.s3); \
160 acc.s0 = mad(src0.s2, weights_row0.s2, acc.s0); \
161 acc.s1 = mad(src0.s3, weights_row0.s2, acc.s1); \
162 acc.s2 = mad(src1.s0, weights_row0.s2, acc.s2); \
163 acc.s3 = mad(src1.s1, weights_row0.s2, acc.s3); \
164 })
165
166/** An optimized direct convolution 3x3 OpenCL kernel for Bifrost architectures when the data type is F32
167 *
168 * @note This OpenCL kernel works only with stride_x and stride_y equal to 1
169 * @note The third dimensions of the weights tensors must be passed at compile time using -DWEIGHTS_DEPTH
170 * @note In case biases, -DHAS_BIAS must to be passed at compile
171 *
172 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F32
173 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
174 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
175 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
176 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
177 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
178 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
179 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
180 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
181 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
182 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
183 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
184 * @param[in] dst_step_y dst_stride_y * number of elements along Z processed per workitem(in bytes)
185 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
186 * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
187 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
Joel Liangf1f3ebd2017-11-10 09:59:19 +0800188 * @param[in] weights_ptr Pointer to the weights tensor. Supported data types: same as @p src_ptr
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100189 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
190 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
191 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
192 * @param[in] weights_step_y weights_stride_y * number of elements along y processed per workitem(in bytes)
193 * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes)
194 * @param[in] weights_step_z weights_stride_z * number of elements along Z processed per workitem(in bytes)
195 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
196 * @param[in] biases_ptr Pointer to the biases tensor. Same as @p src_ptr
197 * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes)
198 * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes)
199 * @param[in] biases_offset_first_element_in_bytes The offset of the first element in the biases tensor
200 * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension
201 */
202__kernel void direct_convolution3x3_f32_bifrost(
203 TENSOR3D_DECLARATION(src),
204 TENSOR3D_DECLARATION(dst),
205 TENSOR3D_DECLARATION(weights),
206#ifdef HAS_BIAS
207 VECTOR_DECLARATION(biases),
208#endif /* defined(HAS_BIAS) */
209 unsigned int weights_stride_w)
210{
211 // Get the kernel index
212 const int kernel_index = get_global_id(2);
213
214 Image src = CONVERT_TO_IMAGE_STRUCT(src);
215 Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst);
216
217 float4 pixels0 = 0;
218 float4 pixels1 = 0;
219 float4 pixels2 = 0;
220
221 __global uchar *weights_addr = (__global uchar *)(weights_ptr + weights_offset_first_element_in_bytes + kernel_index * weights_stride_w);
222 __global uchar *src_addr = (__global uchar *)offset(&src, 0, 0);
223
224 // Note: Since each work-item computes 4x3 elements, we need to load 5 rows from the input tensor
225
226 for(ushort d = 0; d < (ushort)WEIGHTS_DEPTH; ++d)
227 {
228 // Load the weights
229 float3 weights_row0 = vload3(0, (__global float *)(weights_addr + 0 * weights_stride_y));
230 float3 weights_row1 = vload3(0, (__global float *)(weights_addr + 1 * weights_stride_y));
231 float3 weights_row2 = vload3(0, (__global float *)(weights_addr + 2 * weights_stride_y));
232 float4 src0;
233 float2 src1;
234
235 // Load values from row0 of input tensor
236 src0 = vload4(0, (__global float *)(src_addr + 0 * src_stride_y));
237 src1 = vload2(0, (__global float *)(src_addr + 0 * src_stride_y) + 4);
238
239 CONVOLUTION1x3_BIFROST(pixels0, src0, src1, weights_row0);
240
241 // Load values from row1 of input tensor
242 src0 = vload4(0, (__global float *)(src_addr + 1 * src_stride_y));
243 src1 = vload2(0, (__global float *)(src_addr + 1 * src_stride_y) + 4);
244
245 // Accumulate
246 CONVOLUTION1x3_BIFROST(pixels0, src0, src1, weights_row1);
247 CONVOLUTION1x3_BIFROST(pixels1, src0, src1, weights_row0);
248
249 // Load values from row2 of input tensor
250 src0 = vload4(0, (__global float *)(src_addr + 2 * src_stride_y));
251 src1 = vload2(0, (__global float *)(src_addr + 2 * src_stride_y) + 4);
252
253 // Accumulate
254 CONVOLUTION1x3_BIFROST(pixels0, src0, src1, weights_row2);
255 CONVOLUTION1x3_BIFROST(pixels1, src0, src1, weights_row1);
256 CONVOLUTION1x3_BIFROST(pixels2, src0, src1, weights_row0);
257
258 // Load values from row3 of input tensor
259 src0 = vload4(0, (__global float *)(src_addr + 3 * src_stride_y));
260 src1 = vload2(0, (__global float *)(src_addr + 3 * src_stride_y) + 4);
261
262 // Accumulate
263 CONVOLUTION1x3_BIFROST(pixels1, src0, src1, weights_row2);
264 CONVOLUTION1x3_BIFROST(pixels2, src0, src1, weights_row1);
265
266 // Row4
267 src0 = vload4(0, (__global float *)(src_addr + 4 * src_stride_y));
268 src1 = vload2(0, (__global float *)(src_addr + 4 * src_stride_y) + 4);
269
270 // Accumulate
271 CONVOLUTION1x3_BIFROST(pixels2, src0, src1, weights_row2);
272
273 src_addr += src_stride_z;
274 weights_addr += weights_stride_z;
275 }
276
277#ifdef HAS_BIAS
278 Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases);
279
Gian Marco Iodice1c8409d2017-09-06 17:24:25 +0100280 float bias = (float) * ((__global float *)(vector_offset(&biases, kernel_index)));
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100281
Gian Marco Iodice1c8409d2017-09-06 17:24:25 +0100282 pixels0 += (float4)bias;
283 pixels1 += (float4)bias;
284 pixels2 += (float4)bias;
Gian Marco Iodice1246b632017-08-16 18:38:32 +0100285#endif /* defined(HAS_BIAS) */
286
287 vstore4(pixels0, 0, (__global float *)(dst.ptr + 0 * dst_stride_y));
288 vstore4(pixels1, 0, (__global float *)(dst.ptr + 1 * dst_stride_y));
289 vstore4(pixels2, 0, (__global float *)(dst.ptr + 2 * dst_stride_y));
290}
291#endif // defined(WEIGHTS_DEPTH)