blob: 28da544f89a151606b857bee0b826f59e8d3aaf9 [file] [log] [blame]
steniu0127b386c2017-07-18 17:37:43 +01001/*
2 * Copyright (c) 2016, 2017 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#include "helpers.h"
25
Michalis Spyroudef665a2017-08-14 11:26:37 +010026#if defined(FIXED_POINT_POSITION)
27#include "fixed_point.h"
28
29#define ADD_OP(a, b) ADD_SAT_OP_EXPAND((a), (b), DATA_TYPE_PROMOTED, 8)
30#define MUL_OP(a, b) MUL_SAT_OP_EXPAND(CONVERT((a), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 8)), CONVERT((b), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 8)), DATA_TYPE_PROMOTED, 8, FIXED_POINT_POSITION)
31
32// There is no need to have a larger intermediate type for qs32 because all the arguments are already promoted
33MULQ_SAT_IMPL(qs32x8, qs32x8)
34
35#else /* FIXED_POINT_POSITION */
36
37#define ADD_OP(a, b) ((a) + (b))
38#define MUL_OP(a, b) ((a) * (b))
39#define CONVERT_SAT(a, b) ((a))
40
41#endif /* FIXED_POINT_POSITION */
42
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010043#if STRIDE_X == 1
44#define CONVOLUTION1x3(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x3_STRIDE1(acc, src_row_ptr, weights_row_ptr)
45#elif STRIDE_X == 2 /* STRIDE_X == 1 */
46#define CONVOLUTION1x3(acc, src_row_ptr, weights_row_ptr) CONVOLUTION1x3_STRIDE2(acc, src_row_ptr, weights_row_ptr)
steniu0127b386c2017-07-18 17:37:43 +010047#else /* STRIDE_X not equals 1 or 2 */
48#error "STRIDE_X larger than 2 is not supported"
49#endif /* STRIDE_X == 2 */
50
Michalis Spyroudef665a2017-08-14 11:26:37 +010051#define CONVOLUTION1x3_STRIDE1(acc, src_row_ptr, weights_row_ptr) \
52 ({ \
steniu01db006682017-08-09 16:26:22 +010053 VEC_DATA_TYPE(DATA_TYPE, 3) \
54 weights_values0 = vload3(0, weights_row_ptr); \
Michalis Spyroudef665a2017-08-14 11:26:37 +010055 VEC_DATA_TYPE(DATA_TYPE, 8) \
56 src0 = vload8(0, src_row_ptr); \
57 VEC_DATA_TYPE(DATA_TYPE, 2) \
58 src1 = vload2(0, src_row_ptr + 8); \
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010059 \
Michalis Spyroudef665a2017-08-14 11:26:37 +010060 acc = ADD_OP(acc, MUL_OP(src0, (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s0)); \
61 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)); \
62 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 +010063 })
steniu0127b386c2017-07-18 17:37:43 +010064
Michalis Spyroudef665a2017-08-14 11:26:37 +010065#define CONVOLUTION1x3_STRIDE2(acc, src_row_ptr, weights_row_ptr) \
66 ({ \
steniu01db006682017-08-09 16:26:22 +010067 VEC_DATA_TYPE(DATA_TYPE, 3) \
68 weights_values0 = vload3(0, weights_row_ptr); \
Michalis Spyroudef665a2017-08-14 11:26:37 +010069 VEC_DATA_TYPE(DATA_TYPE, 16) \
70 src0 = vload16(0, src_row_ptr); \
71 DATA_TYPE src1 = *(src_row_ptr + 16); \
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010072 \
Michalis Spyroudef665a2017-08-14 11:26:37 +010073 acc = ADD_OP(acc, MUL_OP(src0.even, (VEC_DATA_TYPE(DATA_TYPE, 8))weights_values0.s0)); \
74 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)); \
75 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 +010076 })
steniu0127b386c2017-07-18 17:37:43 +010077
78/** This kernel performs a direct convolution to convolve the low three dimensions.
79 *
80 * @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 +010081 * @note The third dimensions of the weights tensors must be passed at compile time using -DWEIGHTS_DEPTH
steniu0127b386c2017-07-18 17:37:43 +010082 * @note In case biases will be added to the convolution -DHAS_BIAS has to be passed to append the final matrix with 1 in each row.
83 *
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +010084 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
steniu0127b386c2017-07-18 17:37:43 +010085 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
86 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
87 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
88 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
89 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
90 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
91 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
92 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
93 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
94 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
95 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
96 * @param[in] dst_step_y dst_stride_y * number of elements along Z processed per workitem(in bytes)
97 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
98 * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
99 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
100 * @param[out] weights_ptr Pointer to the weights tensor. Supported data types: same as @p weights_ptr
101 * @param[in] weights_stride_x Stride of the weights tensor in X dimension (in bytes)
102 * @param[in] weights_step_x weights_stride_x * number of elements along X processed per workitem(in bytes)
103 * @param[in] weights_stride_y Stride of the weights tensor in Y dimension (in bytes)
104 * @param[in] weights_step_y weights_stride_y * number of elements along y processed per workitem(in bytes)
105 * @param[in] weights_stride_z Stride of the weights tensor in Z dimension (in bytes)
106 * @param[in] weights_step_z weights_stride_z * number of elements along Z processed per workitem(in bytes)
107 * @param[in] weights_offset_first_element_in_bytes The offset of the first element in the weights tensor
108 * @param[in] biases_ptr Pointer to the biases tensor. Same as @p src_ptr
109 * @param[in] biases_stride_x Stride of the biases tensor in X dimension (in bytes)
110 * @param[in] biases_step_x biases_stride_x * number of elements along X processed per workitem(in bytes)
111 * @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 +0100112 * @param[in] weights_stride_w Stride of the weights tensor in the 4th dimension
steniu0127b386c2017-07-18 17:37:43 +0100113 */
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100114#if defined(DATA_TYPE) && defined(STRIDE_X) && defined(WEIGHTS_DEPTH)
steniu0127b386c2017-07-18 17:37:43 +0100115__kernel void direct_convolution3x3(
116 TENSOR3D_DECLARATION(src),
117 TENSOR3D_DECLARATION(dst),
118 TENSOR3D_DECLARATION(weights),
119#ifdef HAS_BIAS
120 VECTOR_DECLARATION(biases),
121#endif /* defined(HAS_BIAS) */
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100122 unsigned int weights_stride_w)
steniu0127b386c2017-07-18 17:37:43 +0100123{
124 Image src = CONVERT_TO_IMAGE_STRUCT(src);
125 Tensor3D weights = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(weights);
126 Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst);
127
Michalis Spyroudef665a2017-08-14 11:26:37 +0100128 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 8)
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100129 pixels0 = 0;
steniu0127b386c2017-07-18 17:37:43 +0100130
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100131 __global uchar *weights_addr = (__global uchar *)tensor3D_offset(&weights, 0, 0, 0);
132 __global uchar *src_addr = (__global uchar *)offset(&src, 0, 0);
steniu0127b386c2017-07-18 17:37:43 +0100133
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100134 const int kernel_index = get_global_id(2);
135 weights_addr += kernel_index * weights_stride_w;
steniu0127b386c2017-07-18 17:37:43 +0100136
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100137 for(int d = 0; d < WEIGHTS_DEPTH; ++d)
steniu0127b386c2017-07-18 17:37:43 +0100138 {
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100139 CONVOLUTION1x3(pixels0, (__global DATA_TYPE *)(src_addr + 0 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 0 * weights_stride_y));
140 CONVOLUTION1x3(pixels0, (__global DATA_TYPE *)(src_addr + 1 * src_stride_y), (__global DATA_TYPE *)(weights_addr + 1 * weights_stride_y));
141 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 +0100142
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100143 src_addr += src_stride_z;
144 weights_addr += weights_stride_z;
steniu0127b386c2017-07-18 17:37:43 +0100145 }
146
147#ifdef HAS_BIAS
Gian Marco Iodice5cb4d6a2017-08-08 10:53:00 +0100148 Vector biases = CONVERT_TO_VECTOR_STRUCT_NO_STEP(biases);
149
Michalis Spyroudef665a2017-08-14 11:26:37 +0100150 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 +0100151#endif /* defined(HAS_BIAS) */
152
Michalis Spyroudef665a2017-08-14 11:26:37 +0100153 vstore8(CONVERT_SAT(pixels0, VEC_DATA_TYPE(DATA_TYPE, 8)), 0, (__global DATA_TYPE *)dst.ptr);
steniu0127b386c2017-07-18 17:37:43 +0100154}
steniu01db006682017-08-09 16:26:22 +0100155#endif // defined(DATA_TYPE) && defined(STRIDE_X) && defined(WEIGHTS_DEPTH)