blob: 158f4010840a88b5fcd3fbad334f5fa81b36e93d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +01002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000024#include "arm_compute/core/NEON/kernels/NEDepthConvertLayerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Michele Di Giorgio3e570db2018-08-24 18:28:48 +010026#include "arm_compute/core/CPP/Validate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/NEON/NEFixedPoint.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Validate.h"
33
34#include <arm_neon.h>
35
36using namespace arm_compute;
37
Michele Di Giorgio3e570db2018-08-24 18:28:48 +010038namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039{
Michele Di Giorgio3e570db2018-08-24 18:28:48 +010040Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, ConvertPolicy policy, uint32_t shift)
41{
42 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
43 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(output);
44 ARM_COMPUTE_UNUSED(policy);
45 ARM_COMPUTE_RETURN_ERROR_ON(input == output);
46 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::U16, DataType::F16, DataType::F32);
47 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16, DataType::U16, DataType::U32, DataType::S32, DataType::F16, DataType::F32);
48 ARM_COMPUTE_RETURN_ERROR_ON(shift >= 8);
49
50 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::U8 && (output->data_type() != DataType::S16 && output->data_type() != DataType::U16
51 && output->data_type() != DataType::S32),
52 "Only data_types supported [in] U8 -> [out] U16, S16, S32");
53
54 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::U16 && (output->data_type() != DataType::U8 && output->data_type() != DataType::U32),
55 "Only data_types supported [in] U16 -> [out] U8, U32");
56
57 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::S16 && (output->data_type() != DataType::U8 && output->data_type() != DataType::S32),
58 "Only data_types supported [in] S16 -> [out] U8, S32");
59
60 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::F16 && output->data_type() != DataType::F32,
61 "Only data_types supported [in] F16 -> [out] F32");
62
63 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::F32 && output->data_type() != DataType::F16,
64 "Only data_types supported [in] F32 -> [out] F16");
65
66 // Validate in case of configured output
67 if(output->total_size() > 0)
68 {
69 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
70 }
71
72 return Status{};
73}
74
75std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
76{
77 constexpr unsigned int num_elems_processed_per_iteration = 16;
78
79 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
80
81 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
82 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
83 bool window_changed = update_window_and_padding(win, input_access, output_access);
84 output_access.set_valid_region(win, output->valid_region());
85
86 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
87 return std::make_pair(err, win);
88}
89} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000091NEDepthConvertLayerKernel::NEDepthConvertLayerKernel()
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010092 : _input(nullptr), _output(nullptr), _policy(), _shift(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093{
94}
95
Michele Di Giorgio3e570db2018-08-24 18:28:48 +010096void NEDepthConvertLayerKernel::configure(const ITensor *input, ITensor *output, ConvertPolicy policy, uint32_t shift)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097{
Michele Di Giorgio3e570db2018-08-24 18:28:48 +010098 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
99
100 // Auto initialize output shape if not initialized (We can only auto-configure the shape, datatype must be given)
101 set_shape_if_empty(*output->info(), input->info()->tensor_shape());
Georgios Pinitase2229412017-07-12 12:30:40 +0100102
103 _input = input;
Michele Di Giorgio3e570db2018-08-24 18:28:48 +0100104 _output = output;
Georgios Pinitase2229412017-07-12 12:30:40 +0100105 _policy = policy;
106 _shift = shift;
107
Michele Di Giorgio3e570db2018-08-24 18:28:48 +0100108 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), policy, shift));
Georgios Pinitase2229412017-07-12 12:30:40 +0100109
110 // Configure kernel window
Michele Di Giorgio3e570db2018-08-24 18:28:48 +0100111 auto win_config = validate_and_configure_window(input->info(), output->info());
112 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
113 ICPPKernel::configure(win_config.second);
114}
Georgios Pinitase2229412017-07-12 12:30:40 +0100115
Michele Di Giorgio3e570db2018-08-24 18:28:48 +0100116Status NEDepthConvertLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, ConvertPolicy policy, uint32_t shift)
117{
118 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, policy, shift));
119 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
120
121 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122}
123
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000124void NEDepthConvertLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100126 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Georgios Pinitase2229412017-07-12 12:30:40 +0100128 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
Michele Di Giorgio3e570db2018-08-24 18:28:48 +0100129 ARM_COMPUTE_ERROR_ON_NULLPTR(_input, _output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 ARM_COMPUTE_ERROR_ON(_input == _output);
131
132 Iterator input(_input, window);
133 Iterator output(_output, window);
134
135 switch(_input->info()->data_type())
136 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 case DataType::U8:
138 {
139 const int16x8_t b = vdupq_n_s16(_shift);
140
141 switch(_output->info()->data_type())
142 {
143 case DataType::S16:
144 {
145 /* Up-conversion U8 -> S16 */
146 execute_window_loop(window, [&](const Coordinates & id)
147 {
148 const uint8x16_t texels_u8 = vld1q_u8(input.ptr());
149
150 const int16x8x2_t texels =
151 {
152 {
153 vshlq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(texels_u8))), b),
154 vshlq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(texels_u8))), b)
155 }
156 };
157
158 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), texels.val[0]);
159 vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, texels.val[1]);
160 },
161 input, output);
162 break;
163 }
164 case DataType::S32:
165 {
166 /* Up-conversion U8 -> S32 */
167 execute_window_loop(window, [&](const Coordinates & id)
168 {
169 const uint8x16_t texels_u8 = vld1q_u8(input.ptr());
170
171 const int16x8x2_t texels =
172 {
173 {
174 vshlq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(texels_u8))), b),
175 vshlq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(texels_u8))), b)
176 }
177 };
178
179 vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()), vmovl_s16(vget_low_s16(texels.val[0])));
180 vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()) + 4, vmovl_s16(vget_high_s16(texels.val[0])));
181 vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()) + 8, vmovl_s16(vget_low_s16(texels.val[1])));
182 vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()) + 12, vmovl_s16(vget_high_s16(texels.val[1])));
183 },
184 input, output);
185 break;
186 }
187 case DataType::U16:
188 {
189 /* Up-conversion U8 -> U16 */
190 execute_window_loop(window, [&](const Coordinates & id)
191 {
192 const uint8x16_t texels_u8 = vld1q_u8(input.ptr());
193
194 const uint16x8x2_t texels =
195 {
196 {
197 vshlq_u16(vmovl_u8(vget_low_u8(texels_u8)), b),
198 vshlq_u16(vmovl_u8(vget_high_u8(texels_u8)), b)
199 }
200 };
201
202 vst1q_u16(reinterpret_cast<uint16_t *>(output.ptr()), texels.val[0]);
203 vst1q_u16(reinterpret_cast<uint16_t *>(output.ptr()) + 8, texels.val[1]);
204 },
205 input, output);
206 break;
207 }
208 default:
209 ARM_COMPUTE_ERROR("Output data type not supported");
210 }
211 break;
212 }
213 case DataType::S16:
214 {
215 switch(_output->info()->data_type())
216 {
217 case DataType::U8:
218 {
219 const int16x8_t b = vdupq_n_s16(-static_cast<int16_t>(_shift));
220
221 /* Down-conversion S16 -> U8 */
222 if(ConvertPolicy::SATURATE == _policy)
223 {
224 execute_window_loop(window, [&](const Coordinates & id)
225 {
226 const int16x8x2_t texels =
227 {
228 {
229 vqshlq_s16(vld1q_s16(reinterpret_cast<int16_t *>(input.ptr())), b),
230 vqshlq_s16(vld1q_s16(reinterpret_cast<int16_t *>(input.ptr()) + 8), b)
231 }
232 };
233
234 vst1q_u8(output.ptr(), vcombine_u8(vqmovun_s16(texels.val[0]), vqmovun_s16(texels.val[1])));
235 },
236 input, output);
237 }
238 else
239 {
240 execute_window_loop(window, [&](const Coordinates & id)
241 {
242 const int16x8x2_t texels =
243 {
244 {
245 vshlq_s16(vld1q_s16(reinterpret_cast<int16_t *>(input.ptr())), b),
246 vshlq_s16(vld1q_s16(reinterpret_cast<int16_t *>(input.ptr()) + 8), b)
247 }
248 };
249
250 vst1q_u8(output.ptr(), vcombine_u8(vmovn_u16(vreinterpretq_u16_s16(texels.val[0])),
251 vmovn_u16(vreinterpretq_u16_s16(texels.val[1]))));
252 },
253 input, output);
254 }
255 break;
256 }
257 case DataType::S32:
258 {
259 const int32x4_t b = vdupq_n_s32(_shift);
260
261 /* Up-conversion S16 -> S32 */
262 execute_window_loop(window, [&](const Coordinates & id)
263 {
264 const int16x8x2_t texels =
265 {
266 {
267 vld1q_s16(reinterpret_cast<int16_t *>(input.ptr())),
268 vld1q_s16(reinterpret_cast<int16_t *>(input.ptr()) + 8)
269 }
270 };
271
272 const int32x4x4_t texels_s32 =
273 {
274 {
275 vshlq_s32(vmovl_s16(vget_low_s16(texels.val[0])), b),
276 vshlq_s32(vmovl_s16(vget_high_s16(texels.val[0])), b),
277 vshlq_s32(vmovl_s16(vget_low_s16(texels.val[1])), b),
278 vshlq_s32(vmovl_s16(vget_high_s16(texels.val[1])), b)
279 }
280 };
281
282 vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()), texels_s32.val[0]);
283 vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()) + 4, texels_s32.val[1]);
284 vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()) + 8, texels_s32.val[2]);
285 vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()) + 12, texels_s32.val[3]);
286 },
287 input, output);
288 break;
289 }
290 default:
291 ARM_COMPUTE_ERROR("Output data type not supported");
292 }
293 break;
294 }
295 case DataType::U16:
296 {
297 switch(_output->info()->data_type())
298 {
299 case DataType::U8:
300 {
301 const int16x8_t b = vdupq_n_s16(-static_cast<int16_t>(_shift));
302
303 /* Down-conversion U16 -> U8 */
304 if(ConvertPolicy::SATURATE == _policy)
305 {
306 execute_window_loop(window, [&](const Coordinates & id)
307 {
308 const uint16x8x2_t texels =
309 {
310 {
311 vqshlq_u16(vld1q_u16(reinterpret_cast<uint16_t *>(input.ptr())), b),
312 vqshlq_u16(vld1q_u16(reinterpret_cast<uint16_t *>(input.ptr()) + 8), b)
313 }
314 };
315
316 vst1q_u8(output.ptr(), vcombine_u8(vqmovn_u16(texels.val[0]), vqmovn_u16(texels.val[1])));
317 },
318 input, output);
319 }
320 else
321 {
322 execute_window_loop(window, [&](const Coordinates & id)
323 {
324 const uint16x8x2_t texels =
325 {
326 {
327 vshlq_u16(vld1q_u16(reinterpret_cast<uint16_t *>(input.ptr())), b),
328 vshlq_u16(vld1q_u16(reinterpret_cast<uint16_t *>(input.ptr()) + 8), b)
329 }
330 };
331
332 vst1q_u8(output.ptr(), vcombine_u8(vmovn_u16(texels.val[0]), vmovn_u16(texels.val[1])));
333 },
334 input, output);
335 }
336 break;
337 }
338 case DataType::U32:
339 {
340 const int32x4_t b = vdupq_n_s32(_shift);
341
342 /* Up-conversion U16 -> U32 */
343 execute_window_loop(window, [&](const Coordinates & id)
344 {
345 const uint16x8x2_t texels =
346 {
347 {
348 vld1q_u16(reinterpret_cast<uint16_t *>(input.ptr())),
349 vld1q_u16(reinterpret_cast<uint16_t *>(input.ptr()) + 8)
350 }
351 };
352
353 vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr()), vshlq_u32(vmovl_u16(vget_low_u16(texels.val[0])), b));
354 vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr()) + 4, vshlq_u32(vmovl_u16(vget_high_u16(texels.val[0])), b));
355 vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr()) + 8, vshlq_u32(vmovl_u16(vget_low_u16(texels.val[1])), b));
356 vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr()) + 12, vshlq_u32(vmovl_u16(vget_high_u16(texels.val[1])), b));
357 },
358 input, output);
359 break;
360 }
361 default:
362 ARM_COMPUTE_ERROR("Output data type not supported");
363 }
364 break;
365 }
Michele Di Giorgio3e570db2018-08-24 18:28:48 +0100366#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
367 case DataType::F16:
368 switch(_output->info()->data_type())
369 {
370 case DataType::F32:
371 {
372 const float32x4_t scale = vdupq_n_f32(1 << _shift);
373
374 /* Up-conversion F16 -> F32 */
375 execute_window_loop(window, [&](const Coordinates & id)
376 {
377 const float16x8x2_t texels =
378 {
379 {
380 vld1q_f16(reinterpret_cast<float16_t *>(input.ptr())),
381 vld1q_f16(reinterpret_cast<float16_t *>(input.ptr()) + 8)
382 }
383 };
384
385 vst1q_f32(reinterpret_cast<float *>(output.ptr()), vmulq_f32(vcvt_f32_f16(vget_low_f16(texels.val[0])), scale));
386 vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 4, vmulq_f32(vcvt_f32_f16(vget_high_f16(texels.val[0])), scale));
387 vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 8, vmulq_f32(vcvt_f32_f16(vget_low_f16(texels.val[1])), scale));
388 vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 12, vmulq_f32(vcvt_f32_f16(vget_high_f16(texels.val[1])), scale));
389 },
390 input, output);
391 break;
392 }
393 default:
394 ARM_COMPUTE_ERROR("Output data type not supported");
395 }
396 break;
397 case DataType::F32:
398 switch(_output->info()->data_type())
399 {
400 case DataType::F16:
401 {
402 const float32x4_t scale = vdupq_n_f32(1.f / (1 << _shift));
403
404 /* Down-conversion F32 -> F16 */
405 execute_window_loop(window, [&](const Coordinates & id)
406 {
407 const float32x4x4_t texels =
408 {
409 {
410 vmulq_f32(vld1q_f32(reinterpret_cast<float *>(input.ptr())), scale),
411 vmulq_f32(vld1q_f32(reinterpret_cast<float *>(input.ptr()) + 4), scale),
412 vmulq_f32(vld1q_f32(reinterpret_cast<float *>(input.ptr()) + 8), scale),
413 vmulq_f32(vld1q_f32(reinterpret_cast<float *>(input.ptr()) + 12), scale)
414 }
415 };
416
417 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()), vcombine_f16(vcvt_f16_f32(texels.val[0]), vcvt_f16_f32(texels.val[1])));
418 vst1q_f16(reinterpret_cast<float16_t *>(output.ptr()) + 8, vcombine_f16(vcvt_f16_f32(texels.val[2]), vcvt_f16_f32(texels.val[3])));
419 },
420 input, output);
421 break;
422 }
423 default:
424 ARM_COMPUTE_ERROR("Output data type not supported");
425 }
426 break;
427#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100428 default:
429 ARM_COMPUTE_ERROR("Not supported");
430 }
431}