blob: a5cbe3d5c4e460aca0c0bffa5afe6f864ebdd9ab [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 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
26/** This kernel reshapes the tensor's low three dimensions to single column
27 *
28 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=short
29 *
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010030 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
32 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
33 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
34 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
35 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
36 * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes)
37 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010038 * @param[out] dst_ptr Pointer to the destination tensor. Same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
40 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
41 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
42 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
43 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
Gian Marco Iodice13edbff2017-06-26 17:20:16 +010044 * @param[in] bias_ptr Pointer to the bias tensor. Same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045 * @param[in] bias_stride_x Stride of the bias tensor in X dimension (in bytes)
46 * @param[in] bias_step_x bias_stride_x * number of elements along X processed per workitem(in bytes)
47 * @param[in] bias_offset_first_element_in_bytes The offset of the first element in the source tensor
48 * @param[in] width The width of the input tensor
49 * @param[in] height The height of the input tensor
50 * @param[in] depth The depth of the input tensor
51 * @param[in] total_filters Total number of filters. 4th dimension of the weights matrix
52 */
53__kernel void reshape_to_columns(
54 TENSOR3D_DECLARATION(src),
55 IMAGE_DECLARATION(dst),
Anthony Barbierac69aa12017-07-03 17:39:37 +010056#ifdef HAS_BIAS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057 VECTOR_DECLARATION(bias),
Anthony Barbierac69aa12017-07-03 17:39:37 +010058#endif /* HAS_BIAS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 uint width, uint height, uint depth, uint total_filters)
60{
61 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
62 bool is_last_thread = (get_global_id(0) == (get_global_size(0) - 1) && get_global_id(1) == (get_global_size(1) - 1) && get_global_id(2) == (get_global_size(2) - 1));
63
64 __global uchar *tmp_src_ptr = src.ptr;
65 __global uchar *tmp_dst_ptr = dst_ptr + dst_offset_first_element_in_bytes + get_global_id(0) * dst_stride_y + get_global_id(1) * width * dst_stride_y + get_global_id(
66 2) * width * height * dst_stride_y;
Anthony Barbierac69aa12017-07-03 17:39:37 +010067#ifdef HAS_BIAS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068 __global uchar *tmp_bias_ptr = bias_ptr + bias_offset_first_element_in_bytes;
Anthony Barbierac69aa12017-07-03 17:39:37 +010069#endif /* HAS_BIAS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070
71 if(is_last_thread)
72 {
73 for(uint i = 0; i < total_filters; ++i)
74 {
75 *((__global DATA_TYPE *)tmp_dst_ptr) = *((__global DATA_TYPE *)tmp_src_ptr);
76
Anthony Barbierac69aa12017-07-03 17:39:37 +010077#ifdef HAS_BIAS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 *((__global DATA_TYPE *)(tmp_dst_ptr + dst_stride_y)) = *((__global DATA_TYPE *)(tmp_bias_ptr));
79 tmp_bias_ptr += bias_stride_x;
Anthony Barbierac69aa12017-07-03 17:39:37 +010080#endif /* HAS_BIAS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081 tmp_src_ptr += depth * src_stride_z;
82 tmp_dst_ptr += dst_stride_x;
83 }
84 }
85 else
86 {
87 for(uint i = 0; i < total_filters; ++i)
88 {
89 *((__global DATA_TYPE *)tmp_dst_ptr) = *((__global DATA_TYPE *)tmp_src_ptr);
90 tmp_src_ptr += depth * src_stride_z;
91 tmp_dst_ptr += dst_stride_x;
92 }
93 }
94}
95
Anthony Barbierac69aa12017-07-03 17:39:37 +010096#if defined(CONVOLVED_WIDTH) && defined(STRIDE_X) && defined(STRIDE_Y) && defined(PAD_X) && defined(PAD_Y) && defined(KERNEL_WIDTH) && defined(KERNEL_HEIGHT) && defined(KERNEL_DEPTH) && defined(SRC_WIDTH) && defined(SRC_HEIGHT)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097/** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM.
98 *
99 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
100 * @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.
101 *
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100102 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
104 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
105 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
106 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
107 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
108 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
109 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100110 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
112 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
113 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
114 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
115 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116 */
117__kernel void im2col_generic(
118 TENSOR3D_DECLARATION(src),
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100119 IMAGE_DECLARATION(dst))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120{
121 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
122 Image dst = CONVERT_TO_IMAGE_STRUCT_NO_STEP(dst);
123
124 // Determine output index
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100125 uint idx = (get_global_id(1) * CONVOLVED_WIDTH + get_global_id(0)) * dst.stride_y;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126 __global uchar *output_ptr = dst.ptr + idx;
127
128 // Determine current input index
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100129 const int top_left_x = get_global_id(0) * STRIDE_X - PAD_X;
130 const int top_left_y = get_global_id(1) * STRIDE_Y - PAD_Y;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131
132 // Linearize convolution elements
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100133 for(int d = 0; d < KERNEL_DEPTH; ++d)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 {
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100135 for(int y = top_left_y, y_e = top_left_y + KERNEL_HEIGHT; y < y_e; ++y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 {
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100137 for(int x = top_left_x, x_e = top_left_x + KERNEL_WIDTH; x < x_e; ++x, output_ptr += dst.stride_x)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138 {
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100139 if(x < 0 || x >= SRC_WIDTH || y < 0 || y >= SRC_HEIGHT)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 {
141 *((__global DATA_TYPE *)output_ptr) = 0;
142 }
143 else
144 {
145 *((__global DATA_TYPE *)output_ptr) = *((__global DATA_TYPE *)(tensor3D_offset(&src, x, y, d)));
146 }
147 }
148 }
149 }
150
Anthony Barbierac69aa12017-07-03 17:39:37 +0100151#if defined(HAS_BIAS)
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100152 *((__global DATA_TYPE *)output_ptr) = (DATA_TYPE)1;
Anthony Barbierac69aa12017-07-03 17:39:37 +0100153#endif /* HAS_BIAS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154}
Anthony Barbierac69aa12017-07-03 17:39:37 +0100155#endif //(CONVOLVED_WIDTH && STRIDE_X && STRIDE_Y && PAD_X && PAD_Y && KERNEL_WIDTH && KERNEL_HEIGHT && KERNEL_DEPTH && SRC_WIDTH && SRC_HEIGHT)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156
157/** This kernel performs a reshaping of the output of the convolution layer.
158 *
159 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
160 *
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100161 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
163 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
164 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
165 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
166 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100167 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
169 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
170 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
171 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
172 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
173 * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
174 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
175 * @param[in] width The output tensor width
176 */
177__kernel void col2im(
178 IMAGE_DECLARATION(src),
179 TENSOR3D_DECLARATION(dst),
180 uint width)
181{
182 Image src = CONVERT_TO_IMAGE_STRUCT(src);
183 Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(dst);
184
185 int idx = get_global_id(0) * dst.stride_z + (get_global_id(1) / width) * dst.stride_y + (get_global_id(1) % width) * dst.stride_x;
186 __global uchar *tmp_out_ptr = dst.ptr + idx;
187 *((__global DATA_TYPE *)tmp_out_ptr) = *((__global DATA_TYPE *)(src.ptr));
188}
189
190/** This kernel reshapes the tensor's low three dimensions to single row for GEMM operation
191 *
192 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=float
193 * @note In case biases will be added in late stage, -DHAS_BIAS has to be passed to append the final matrix with 1 in each row.
194 *
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100195 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
197 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
198 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
199 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
200 * @param[in] src_stride_z Stride of the source tensor in Z dimension (in bytes)
201 * @param[in] src_step_z src_stride_z * number of elements along Y processed per workitem(in bytes)
202 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100203 * @param[out] dst_ptr Pointer to the destination tensor. Same as @p src_ptr
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
205 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
206 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
207 * @param[in] width The width of the input tensor
208 * @param[in] height The height of the input tensor
209 */
210__kernel void im2col_reduced(
211 TENSOR3D_DECLARATION(src),
212 VECTOR_DECLARATION(dst),
213 uint width, uint height)
214{
215 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
216
217 const uint image_size = width * height;
218
219 __global uchar *tmp_out_ptr = dst_ptr + dst_offset_first_element_in_bytes + (get_global_id(0) + get_global_id(1) * width + get_global_id(2) * image_size) * dst_stride_x;
220
221 *((__global DATA_TYPE *)tmp_out_ptr) = *((__global DATA_TYPE *)src.ptr);
222
Anthony Barbierac69aa12017-07-03 17:39:37 +0100223#ifdef HAS_BIAS
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224 // If it is the last thread in the 3 dimensional workgroup
225 if(get_global_id(0) == (get_global_size(0) - 1) && get_global_id(1) == (get_global_size(1) - 1) && get_global_id(2) == (get_global_size(2) - 1))
226 {
227 tmp_out_ptr += dst_stride_x;
228 *((__global DATA_TYPE *)tmp_out_ptr) = (DATA_TYPE)1;
229 }
Anthony Barbierac69aa12017-07-03 17:39:37 +0100230#endif /* HAS_BIAS */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100231}