blob: cb11786ac4e30f15984b322634203588427b9b38 [file] [log] [blame]
Michalis Spyrou16934a52018-08-21 18:03:58 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Michalis Spyrou16934a52018-08-21 18:03:58 +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
Sheri Zhang0de45d02020-04-17 14:59:13 +01008 * deal in the Software without restriction, including without limitation the
Michalis Spyrou16934a52018-08-21 18:03:58 +01009 * 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 *
Sheri Zhang0de45d02020-04-17 14:59:13 +010016 * 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
Michalis Spyrou16934a52018-08-21 18:03:58 +010019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Sheri Zhang0de45d02020-04-17 14:59:13 +010020 * 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
Michalis Spyrou16934a52018-08-21 18:03:58 +010022 * SOFTWARE.
23 */
24#include "helpers.h"
25
Isabella Gottardicc6129c2018-12-14 11:40:40 +000026#if defined(BATCH_SIZE) && defined(DATA_TYPE) && defined(WIDTH_IN) && defined(HEIGHT_IN)
Michalis Spyrou16934a52018-08-21 18:03:58 +010027/** Calculate the space to batch conversion.
28 *
29 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=float
30 * @note The block shape tensor rank must be passed at compile time using -DBLOCK_SHAPE_DIM. e.g. -DBLOCK_SHAPE_DIM=2
31 *
Sheri Zhang0de45d02020-04-17 14:59:13 +010032 * @param[in] input_ptr Pointer to the source tensor. Supported data types: All
Michalis Spyrou16934a52018-08-21 18:03:58 +010033 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
34 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
35 * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes)
36 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
37 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
38 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
39 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source image
40 * @param[in] paddings_ptr Pointer to the second source image. Supported data types: S32
41 * @param[in] paddings_stride_x Stride of the paddinds tensor in X dimension (in bytes)
42 * @param[in] paddings_step_x paddings_stride_x * number of elements along X processed per workitem(in bytes)
43 * @param[in] paddings_stride_y Stride of the paddinds tensor in Y dimension (in bytes)
44 * @param[in] paddings_step_y paddings_stride_y * number of elements along Y processed per workitem(in bytes)
45 * @param[in] paddingse_offset_first_element_in_bytes The offset of the first element in the second source image
46 * @param[in] block_shape_ptr Pointer to the block shape tensor. Supported data types: S32
47 * @param[in] block_shape_stride_x Stride of the block shape tensor in X dimension (in bytes)
48 * @param[in] block_shape_step_x block_shape_stride_x * number of elements along X processed per workitem(in bytes)
Michalis Spyrou16934a52018-08-21 18:03:58 +010049 * @param[in] block_shape_offset_first_element_in_bytes The offset of the first element in the block shapetensor
50 * @param[in] batch_id The output tensor batch id
51 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
52 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
53 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
54 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
55 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
56 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
57 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
58 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image
59 */
Michalis Spyrou13a51e12018-09-18 13:09:30 +010060__kernel void space_to_batch_nchw(
Michalis Spyrou16934a52018-08-21 18:03:58 +010061 TENSOR4D_DECLARATION(input),
62 IMAGE_DECLARATION(paddings),
63 VECTOR_DECLARATION(block_shape),
64 const int batch_id,
65 TENSOR3D_DECLARATION(output))
66{
67 Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(input, 0);
68 Image pad = CONVERT_TO_IMAGE_STRUCT_NO_STEP(paddings);
69 Vector block = CONVERT_TO_VECTOR_STRUCT_NO_STEP(block_shape);
70 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
71
Michalis Spyrouedf26ea2018-11-21 14:17:42 +000072 const int pad_left_x = *((__global int *)offset(&pad, 0, 0));
73 const int pad_right_x = *((__global int *)offset(&pad, 1, 0));
74 const int pad_left_y = *((__global int *)offset(&pad, 0, 1));
75 const int pad_right_y = *((__global int *)offset(&pad, 1, 1));
Michalis Spyrou16934a52018-08-21 18:03:58 +010076
77 int block_x = *((__global int *)vector_offset(&block, 0));
78 int block_y = *((__global int *)vector_offset(&block, 1));
79
80 const int out_x = get_global_id(0);
81 const int out_y = get_global_id(1);
82 const int z = get_global_id(2);
83
Isabella Gottardicc6129c2018-12-14 11:40:40 +000084 const int pos_x = out_x * block_x + ((batch_id / BATCH_IN) % block_x);
85 const int pos_y = out_y * block_y + ((batch_id / BATCH_IN) / block_x);
86
87 if(((pos_y >= pad_left_y) && (pos_y < pad_left_y + HEIGHT_IN) && (pos_x >= pad_left_x) && (pos_x < pad_left_x + WIDTH_IN)))
Michalis Spyrou16934a52018-08-21 18:03:58 +010088 {
Isabella Gottardicc6129c2018-12-14 11:40:40 +000089 const int w = batch_id % BATCH_IN;
90 const int in_x = pos_x - pad_left_x;
91 const int in_y = pos_y - pad_left_y;
92
Michalis Spyrou16934a52018-08-21 18:03:58 +010093 *((__global DATA_TYPE *)out.ptr) = *((__global DATA_TYPE *)tensor4D_offset(&in, in_x, in_y, z, w));
94 }
95}
Michalis Spyrou13a51e12018-09-18 13:09:30 +010096/** Calculate the space to batch conversion. (NHWC)
97 *
98 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=float
99 * @note The block shape tensor rank must be passed at compile time using -DBLOCK_SHAPE_DIM. e.g. -DBLOCK_SHAPE_DIM=2
100 *
Sheri Zhang0de45d02020-04-17 14:59:13 +0100101 * @param[in] input_ptr Pointer to the source tensor. Supported data types: All
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100102 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
103 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
104 * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes)
105 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
106 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
107 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
108 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source image
109 * @param[in] paddings_ptr Pointer to the second source image. Supported data types: S32
110 * @param[in] paddings_stride_x Stride of the paddinds tensor in X dimension (in bytes)
111 * @param[in] paddings_step_x paddings_stride_x * number of elements along X processed per workitem(in bytes)
112 * @param[in] paddings_stride_y Stride of the paddinds tensor in Y dimension (in bytes)
113 * @param[in] paddings_step_y paddings_stride_y * number of elements along Y processed per workitem(in bytes)
114 * @param[in] paddingse_offset_first_element_in_bytes The offset of the first element in the second source image
115 * @param[in] block_shape_ptr Pointer to the block shape tensor. Supported data types: S32
116 * @param[in] block_shape_stride_x Stride of the block shape tensor in X dimension (in bytes)
117 * @param[in] block_shape_step_x block_shape_stride_x * number of elements along X processed per workitem(in bytes)
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100118 * @param[in] block_shape_offset_first_element_in_bytes The offset of the first element in the block shapetensor
119 * @param[in] batch_id The output tensor batch id
120 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
121 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
122 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
123 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
124 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
125 * @param[in] output_stride_z Stride of the destination tensor in Z dimension (in bytes)
126 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
127 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image
128 */
129__kernel void space_to_batch_nhwc(
130 TENSOR4D_DECLARATION(input),
131 IMAGE_DECLARATION(paddings),
132 VECTOR_DECLARATION(block_shape),
133 const int batch_id,
134 TENSOR3D_DECLARATION(output))
135{
136 Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(input, 0);
137 Image pad = CONVERT_TO_IMAGE_STRUCT_NO_STEP(paddings);
138 Vector block = CONVERT_TO_VECTOR_STRUCT_NO_STEP(block_shape);
139 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
140
Michalis Spyrouedf26ea2018-11-21 14:17:42 +0000141 const int pad_left_x = *((__global int *)offset(&pad, 0, 0));
142 const int pad_right_x = *((__global int *)offset(&pad, 1, 0));
143 const int pad_left_y = *((__global int *)offset(&pad, 0, 1));
144 const int pad_right_y = *((__global int *)offset(&pad, 1, 1));
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100145
146 int block_x = *((__global int *)vector_offset(&block, 0));
147 int block_y = *((__global int *)vector_offset(&block, 1));
148
149 const int out_x = get_global_id(1);
150 const int out_y = get_global_id(2);
151 const int z = get_global_id(0);
152
Isabella Gottardicc6129c2018-12-14 11:40:40 +0000153 const int pos_x = out_x * block_x + ((batch_id / BATCH_IN) % block_x);
154 const int pos_y = out_y * block_y + ((batch_id / BATCH_IN) / block_x);
155
156 if(((pos_y >= pad_left_y) && (pos_y < pad_left_y + HEIGHT_IN) && (pos_x >= pad_left_x) && (pos_x < pad_left_x + WIDTH_IN)))
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100157 {
Isabella Gottardicc6129c2018-12-14 11:40:40 +0000158 const int w = batch_id % BATCH_IN;
159 const int in_x = pos_x - pad_left_x;
160 const int in_y = pos_y - pad_left_y;
161
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100162 *((__global DATA_TYPE *)out.ptr) = *((__global DATA_TYPE *)tensor4D_offset(&in, z, in_x, in_y, w));
163 }
164}
Isabella Gottardicc6129c2018-12-14 11:40:40 +0000165#endif // defined(BATCH_SIZE) && defined(DATA_TYPE) && defined(WIDTH_IN) && defined(HEIGHT_IN)
Michalis Spyrou16934a52018-08-21 18:03:58 +0100166
Isabella Gottardicc6129c2018-12-14 11:40:40 +0000167#if defined(BATCH_SIZE) && defined(DATA_TYPE) && defined(BLOCK_SHAPE_X) && defined(BLOCK_SHAPE_Y) && defined(PAD_LEFT_X) && defined(PAD_RIGHT_X) && defined(PAD_LEFT_Y) && defined(PAD_RIGHT_Y) && defined(WIDTH_IN) && defined(HEIGHT_IN)
Michalis Spyrou16934a52018-08-21 18:03:58 +0100168/** Calculate the space to batch conversion.
169 *
170 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=float
171 * @note The input tensor batch size must be passed at compile time using -DBATCH_SIZE. e.g. -DBATCH_SIZE=2
172 * @note The block shape x must be passed at compile time using -DBLOCK_SHAPE_X. e.g. -DBLOCK_SHAPE_X=2
173 * @note The block shape y must be passed at compile time using -DBLOCK_SHAPE_Y. e.g. -DBLOCK_SHAPE_Y=2
174 * @note The starting pad value of x must be passed at compile time using -DPAD_LEFT_X. e.g. -DPAD_LEFT_X=2
175 * @note The ending pad value of x must be passed at compile time using -DPAD_RIGHT_X. e.g. -DPAD_RIGHT_X=2
176 * @note The starting pad value of y must be passed at compile time using -DPAD_LEFT_Y. e.g. -DPAD_LEFT_Y=2
177 * @note The ending pad value of y must be passed at compile time using -DPAD_RIGHT_Y. e.g. -DPAD_RIGHT_X=2
178 *
Sheri Zhang0de45d02020-04-17 14:59:13 +0100179 * @param[in] input_ptr Pointer to the source tensor. Supported data types: All
Michalis Spyrou16934a52018-08-21 18:03:58 +0100180 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
181 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
182 * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes)
183 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
184 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
185 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
186 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source image
187 * @param[in] batch_id The output tensor batch id
188 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
189 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
190 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
191 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
192 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
193 * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes)
194 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
195 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image
196 */
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100197__kernel void space_to_batch_static_nchw(
Michalis Spyrou16934a52018-08-21 18:03:58 +0100198 TENSOR4D_DECLARATION(input),
199 const int batch_id,
200 TENSOR3D_DECLARATION(output))
201{
202 Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(input, 0);
203 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
204
205 int block_x = BLOCK_SHAPE_X;
Michalis Spyrouedf26ea2018-11-21 14:17:42 +0000206 int block_y = BLOCK_SHAPE_Y;
Michalis Spyrou16934a52018-08-21 18:03:58 +0100207
208 const int out_x = get_global_id(0);
209 const int out_y = get_global_id(1);
210 const int z = get_global_id(2);
211
Isabella Gottardicc6129c2018-12-14 11:40:40 +0000212 const int pos_x = out_x * block_x + ((batch_id / BATCH_IN) % block_x);
213 const int pos_y = out_y * block_y + ((batch_id / BATCH_IN) / block_x);
214
215 if(pos_y >= PAD_LEFT_Y && pos_y < PAD_LEFT_Y + HEIGHT_IN && pos_x >= PAD_LEFT_X && pos_x < PAD_LEFT_X + WIDTH_IN)
Michalis Spyrou16934a52018-08-21 18:03:58 +0100216 {
Isabella Gottardicc6129c2018-12-14 11:40:40 +0000217 const int w = batch_id % BATCH_IN;
218 const int in_x = pos_x - PAD_LEFT_X;
219 const int in_y = pos_y - PAD_LEFT_Y;
220
Michalis Spyrou16934a52018-08-21 18:03:58 +0100221 *((__global DATA_TYPE *)out.ptr) = *((__global DATA_TYPE *)tensor4D_offset(&in, in_x, in_y, z, w));
222 }
223}
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100224/** Calculate the space to batch conversion. (NHWC)
225 *
226 * @note Datatype should be given as a preprocessor argument using -DDATA_TYPE=type. e.g. -DDATA_TYPE=float
227 * @note The input tensor batch size must be passed at compile time using -DBATCH_SIZE. e.g. -DBATCH_SIZE=2
228 * @note The block shape x must be passed at compile time using -DBLOCK_SHAPE_X. e.g. -DBLOCK_SHAPE_X=2
229 * @note The block shape y must be passed at compile time using -DBLOCK_SHAPE_Y. e.g. -DBLOCK_SHAPE_Y=2
230 * @note The starting pad value of x must be passed at compile time using -DPAD_LEFT_X. e.g. -DPAD_LEFT_X=2
231 * @note The ending pad value of x must be passed at compile time using -DPAD_RIGHT_X. e.g. -DPAD_RIGHT_X=2
232 * @note The starting pad value of y must be passed at compile time using -DPAD_LEFT_Y. e.g. -DPAD_LEFT_Y=2
233 * @note The ending pad value of y must be passed at compile time using -DPAD_RIGHT_Y. e.g. -DPAD_RIGHT_X=2
234 *
Sheri Zhang0de45d02020-04-17 14:59:13 +0100235 * @param[in] input_ptr Pointer to the source tensor. Supported data types: All
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100236 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
237 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
238 * @param[in] input_stride_y Stride of the source image in Y dimension (in bytes)
239 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
240 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
241 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
242 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the first source image
243 * @param[in] batch_id The output tensor batch id
244 * @param[out] output_ptr Pointer to the destination tensor. Supported data types: same as @p input_ptr
245 * @param[in] output_stride_x Stride of the destination tensor in X dimension (in bytes)
246 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
247 * @param[in] output_stride_y Stride of the destination tensor in Y dimension (in bytes)
248 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
249 * @param[in] output_stride_z Stride of the source tensor in Z dimension (in bytes)
250 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
251 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the destination image
252 */
253__kernel void space_to_batch_static_nhwc(
254 TENSOR4D_DECLARATION(input),
255 const int batch_id,
256 TENSOR3D_DECLARATION(output))
257{
258 Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(input, 0);
259 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(output);
260
261 int block_x = BLOCK_SHAPE_X;
Michalis Spyrouedf26ea2018-11-21 14:17:42 +0000262 int block_y = BLOCK_SHAPE_Y;
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100263
264 const int out_x = get_global_id(1);
265 const int out_y = get_global_id(2);
266 const int z = get_global_id(0);
267
Isabella Gottardicc6129c2018-12-14 11:40:40 +0000268 const int pos_x = out_x * block_x + ((batch_id / BATCH_IN) % block_x);
269 const int pos_y = out_y * block_y + ((batch_id / BATCH_IN) / block_x);
270
271 if(pos_y >= PAD_LEFT_Y && pos_y < PAD_LEFT_Y + HEIGHT_IN && pos_x >= PAD_LEFT_X && pos_x < PAD_LEFT_X + WIDTH_IN)
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100272 {
Isabella Gottardicc6129c2018-12-14 11:40:40 +0000273 const int w = batch_id % BATCH_IN;
274 const int in_x = pos_x - PAD_LEFT_X;
275 const int in_y = pos_y - PAD_LEFT_Y;
276
Michalis Spyrou13a51e12018-09-18 13:09:30 +0100277 *((__global DATA_TYPE *)out.ptr) = *((__global DATA_TYPE *)tensor4D_offset(&in, z, in_x, in_y, w));
278 }
279}
Isabella Gottardicc6129c2018-12-14 11:40:40 +0000280#endif // defined(BATCH_SIZE) && defined(DATA_TYPE) && defined(BLOCK_SHAPE_X) && defined(BLOCK_SHAPE_Y) && defined(PAD_LEFT_X) && defined(PAD_RIGHT_X) && defined(PAD_LEFT_Y) && defined(PAD_RIGHT_Y) && defined(WIDTH_IN) && defined(HEIGHT_IN)