blob: 649fba30d4b954ee906241b82d2929a9c5f90255 [file] [log] [blame]
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +01001/*
Isabella Gottardi0a1090a2019-02-14 18:07:36 +00002 * Copyright (c) 2017-2019 ARM Limited.
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +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 "arm_compute/core/NEON/kernels/NEReshapeLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
Anthony Barbiereaefd002018-07-20 17:49:35 +010027#include "arm_compute/core/CPP/Validate.h"
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010028#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/IAccessWindow.h"
31#include "arm_compute/core/ITensor.h"
32#include "arm_compute/core/NEON/INEKernel.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Validate.h"
35
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010036#include <cstdint>
37
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000038/** [NEReshapeLayerKernel Kernel] **/
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010039using namespace arm_compute;
40
41namespace
42{
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010043Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000046 // Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010047 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, DataType::U16, DataType::S16,
48 DataType::U32, DataType::S32, DataType::F16, DataType::F32);
49
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
51 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().total_size() != output->tensor_shape().total_size());
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000052 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010053
54 return Status{};
55}
56
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010057template <typename T>
58inline void reshape_tensor(const Window &window, const ITensor *input, ITensor *output)
59{
60 const TensorShape &input_shape = input->info()->tensor_shape();
61 const TensorShape &output_shape = output->info()->tensor_shape();
62 Coordinates output_coord{};
63
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010064 Iterator in(input, window);
65
66 execute_window_loop(window, [&](const Coordinates & id)
67 {
68 output_coord = index2coords(output_shape, coords2index(input_shape, id));
69 *reinterpret_cast<T *>(output->ptr_to_element(output_coord)) = *reinterpret_cast<T *>(in.ptr());
70 },
71 in);
72}
73} // namespace
74
75void NEReshapeLayerKernel::configure(const ITensor *input, ITensor *output)
76{
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010077 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
78 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010079
80 _input = input;
81 _output = output;
82
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010083 // Configure kernel window
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010084 Window win = calculate_max_window(*input->info());
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010085
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010086 // Set the output valid region
87 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010088
89 INEKernel::configure(win);
90}
91
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010092Status NEReshapeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
93{
94 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
95
96 return Status{};
97}
98
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010099void NEReshapeLayerKernel::run(const Window &window, const ThreadInfo &info)
100{
101 ARM_COMPUTE_UNUSED(info);
102 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
103 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
104
105 switch(_input->info()->data_type())
106 {
107 case DataType::U8:
108 case DataType::S8:
Les Bella5e6d672017-11-29 10:48:23 +0000109 case DataType::QASYMM8:
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100110 reshape_tensor<uint8_t>(window, _input, _output);
111 break;
112 case DataType::U16:
113 case DataType::S16:
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100114 case DataType::F16:
115 reshape_tensor<uint16_t>(window, _input, _output);
116 break;
117 case DataType::U32:
118 case DataType::S32:
119 case DataType::F32:
120 reshape_tensor<uint32_t>(window, _input, _output);
121 break;
122 default:
123 ARM_COMPUTE_ERROR("Unsupported data type!");
124 }
125}
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000126/** [NEReshapeLayerKernel Kernel] **/