blob: 53fcfd724d617106bb1e488ed7286941f4a4925e [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.
Georgios Pinitas33843562019-12-10 13:33:18 +000047 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010048
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
50 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().total_size() != output->tensor_shape().total_size());
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000051 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010052
53 return Status{};
54}
55
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010056template <typename T>
57inline void reshape_tensor(const Window &window, const ITensor *input, ITensor *output)
58{
59 const TensorShape &input_shape = input->info()->tensor_shape();
60 const TensorShape &output_shape = output->info()->tensor_shape();
61 Coordinates output_coord{};
62
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010063 Iterator in(input, window);
64
65 execute_window_loop(window, [&](const Coordinates & id)
66 {
67 output_coord = index2coords(output_shape, coords2index(input_shape, id));
68 *reinterpret_cast<T *>(output->ptr_to_element(output_coord)) = *reinterpret_cast<T *>(in.ptr());
69 },
70 in);
71}
72} // namespace
73
74void NEReshapeLayerKernel::configure(const ITensor *input, ITensor *output)
75{
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010076 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
77 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010078
79 _input = input;
80 _output = output;
81
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010082 // Configure kernel window
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010083 Window win = calculate_max_window(*input->info());
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010084
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010085 // Set the output valid region
86 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010087
88 INEKernel::configure(win);
89}
90
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010091Status NEReshapeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
92{
93 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
94
95 return Status{};
96}
97
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010098void NEReshapeLayerKernel::run(const Window &window, const ThreadInfo &info)
99{
100 ARM_COMPUTE_UNUSED(info);
101 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
102 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
103
104 switch(_input->info()->data_type())
105 {
106 case DataType::U8:
107 case DataType::S8:
Les Bella5e6d672017-11-29 10:48:23 +0000108 case DataType::QASYMM8:
Georgios Pinitas33843562019-12-10 13:33:18 +0000109 case DataType::QASYMM8_SIGNED:
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] **/