blob: 298f50b93d63321794e2ac59da5ec0cdb990c3e4 [file] [log] [blame]
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +01001/*
Michalis Spyrou995f5522018-01-29 13:43:35 +00002 * Copyright (c) 2017-2018 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
38using namespace arm_compute;
39
40namespace
41{
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010042Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
43{
44 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
45 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, DataType::U16, DataType::S16,
46 DataType::U32, DataType::S32, DataType::F16, DataType::F32);
47
48 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
49 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().total_size() != output->tensor_shape().total_size());
50
51 return Status{};
52}
53
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010054template <typename T>
55inline void reshape_tensor(const Window &window, const ITensor *input, ITensor *output)
56{
57 const TensorShape &input_shape = input->info()->tensor_shape();
58 const TensorShape &output_shape = output->info()->tensor_shape();
59 Coordinates output_coord{};
60
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010061 Iterator in(input, window);
62
63 execute_window_loop(window, [&](const Coordinates & id)
64 {
65 output_coord = index2coords(output_shape, coords2index(input_shape, id));
66 *reinterpret_cast<T *>(output->ptr_to_element(output_coord)) = *reinterpret_cast<T *>(in.ptr());
67 },
68 in);
69}
70} // namespace
71
72void NEReshapeLayerKernel::configure(const ITensor *input, ITensor *output)
73{
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010074 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
75 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010076
77 _input = input;
78 _output = output;
79
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010080 // Configure kernel window
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010081 Window win = calculate_max_window(*input->info());
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010082
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010083 // Set the output valid region
84 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010085
86 INEKernel::configure(win);
87}
88
Giuseppe Rossini6a90b692018-08-23 11:29:59 +010089Status NEReshapeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
90{
91 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
92
93 return Status{};
94}
95
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010096void NEReshapeLayerKernel::run(const Window &window, const ThreadInfo &info)
97{
98 ARM_COMPUTE_UNUSED(info);
99 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
100 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
101
102 switch(_input->info()->data_type())
103 {
104 case DataType::U8:
105 case DataType::S8:
Les Bella5e6d672017-11-29 10:48:23 +0000106 case DataType::QASYMM8:
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100107 reshape_tensor<uint8_t>(window, _input, _output);
108 break;
109 case DataType::U16:
110 case DataType::S16:
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100111 case DataType::F16:
112 reshape_tensor<uint16_t>(window, _input, _output);
113 break;
114 case DataType::U32:
115 case DataType::S32:
116 case DataType::F32:
117 reshape_tensor<uint32_t>(window, _input, _output);
118 break;
119 default:
120 ARM_COMPUTE_ERROR("Unsupported data type!");
121 }
122}