blob: e9bc8effc667273ff35cd2ef17f7e71c426fd8e3 [file] [log] [blame]
Georgios Pinitas284cfe22018-02-13 12:15:13 +00001/*
2 * Copyright (c) 2018 ARM Limited.
3 *
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/NEPermuteKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
33
34namespace
35{
36#include "arm_compute/core/NEON/kernels/convolution/common/shims.hpp"
37} // namespace
38
39#include <cstddef>
40#include <cstdint>
41
42using namespace arm_compute;
43
44namespace
45{
46Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
47{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8,
49 DataType::U16, DataType::S16,
Georgios Pinitas284cfe22018-02-13 12:15:13 +000050 DataType::U32, DataType::S32,
51 DataType::F16, DataType::F32);
Isabella Gottardiaad9f2c2018-02-21 11:51:23 +000052 ARM_COMPUTE_RETURN_ERROR_ON_MSG((perm.num_dimensions() == 3 && !(perm[0] == 2 && perm[1] == 0 && perm[2] == 1) && !(perm[0] == 1 && perm[1] == 2 && perm[2] == 0)),
53 "Only [2, 0, 1] and [1, 2, 0] permutation is supported");
Georgios Pinitas284cfe22018-02-13 12:15:13 +000054
55 const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input, perm);
56
57 // Validate configured output
58 if(output->total_size() != 0)
59 {
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitas284cfe22018-02-13 12:15:13 +000062 }
63
64 return Status{};
65}
66} // namespace
67
68template <typename T>
69void NEPermuteKernel::run_permute(const Window &window)
70{
71 // Input window
72 Window window_in = window;
73 window_in.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().end() - window.x().start()));
74 window_in.set(Window::DimY, Window::Dimension(window.y().start(), window.y().end(), window.y().end() - window.y().start()));
75 window_in.set(Window::DimZ, Window::Dimension(window.z().start(), window.z().end(), window.z().end() - window.z().start()));
76 window_in.set(3, Window::Dimension(window[3].start(), window[3].end(), window[3].end() - window[3].start()));
77
78 // Output window
79 Window window_out(window);
80 const Window::Dimension zero_window = Window::Dimension(0, 0, 0);
81 for(size_t d = 0; d <= _perm.num_dimensions(); ++d)
82 {
83 window_out.set(d, zero_window);
84 }
85
86 // Create iterators
87 Iterator in(_input, window_in);
88 Iterator out(_output, window_out);
89
90 // CHW -> HWC
91 if((_perm.num_dimensions() == 3) && (_perm[0] == 2) && (_perm[1] == 0) && (_perm[2] == 1))
92 {
93 const int in_row_stride = _input->info()->strides_in_bytes().y() / sizeof(T);
94 const int in_channel_stride = _input->info()->strides_in_bytes().z() / sizeof(T);
95 const int in_batch_stride = _input->info()->strides_in_bytes()[3] / sizeof(T);
96
97 const int out_channel_stride = _output->info()->strides_in_bytes().x() / sizeof(T);
98 const int out_col_stride = _output->info()->strides_in_bytes().y() / sizeof(T);
99 const int out_row_stride = _output->info()->strides_in_bytes().z() / sizeof(T);
100 const int out_batch_stride = _output->info()->strides_in_bytes()[3] / sizeof(T);
101
102 const int n_cols = _input->info()->tensor_shape().x();
103 const int n_rows = window_in.y().step();
104 const int n_channels = _input->info()->tensor_shape().z();
105 const int n_batches = _input->info()->tensor_shape()[3];
106
107 execute_window_loop(window_in, [&](const Coordinates & id)
108 {
109 const int idx = id[0] * out_col_stride + id[1] * out_row_stride + id[2] * out_channel_stride;
110 reorder::nchw_to_nhwc(reinterpret_cast<const T *>(in.ptr()), reinterpret_cast<T *>(out.ptr()) + idx,
111 n_batches, n_channels, n_rows, n_cols,
112 in_batch_stride, in_channel_stride, in_row_stride,
113 out_batch_stride, out_row_stride, out_col_stride);
114 },
115 in, out);
116 }
117 // HWC -> CHW
118 else if((_perm.num_dimensions() == 3) && (_perm[0] == 1) && (_perm[1] == 2) && (_perm[2] == 0))
119 {
120 const int in_col_stride = _input->info()->strides_in_bytes().y() / sizeof(T);
121 const int in_row_stride = _input->info()->strides_in_bytes().z() / sizeof(T);
122 const int in_batch_stride = _input->info()->strides_in_bytes()[3] / sizeof(T);
123
124 const int out_col_stride = _output->info()->strides_in_bytes().x() / sizeof(T);
125 const int out_row_stride = _output->info()->strides_in_bytes().y() / sizeof(T);
126 const int out_channel_stride = _output->info()->strides_in_bytes().z() / sizeof(T);
127 const int out_batch_stride = _output->info()->strides_in_bytes()[3] / sizeof(T);
128
129 const int n_channels = _input->info()->tensor_shape().x();
130 const int n_cols = window_in.y().step();
131 const int n_rows = _input->info()->tensor_shape().z();
132 const int n_batches = _input->info()->tensor_shape()[3];
133
134 execute_window_loop(window_in, [&](const Coordinates & id)
135 {
136 const int idx = id[0] * out_channel_stride + id[1] * out_col_stride + id[2] * out_row_stride;
137 reorder::nhwc_to_nchw(reinterpret_cast<const T *>(in.ptr()), reinterpret_cast<T *>(out.ptr()) + idx,
138 n_batches, n_rows, n_cols, n_channels,
139 in_batch_stride, in_row_stride, in_col_stride,
140 out_batch_stride, out_channel_stride, out_row_stride);
141 },
142 in, out);
143 }
144 else
145 {
146 ARM_COMPUTE_ERROR("Unsupported permutation vector");
147 }
148}
149
150NEPermuteKernel::NEPermuteKernel()
151 : _func(), _input(nullptr), _output(nullptr), _perm()
152{
153}
154
155void NEPermuteKernel::configure(const ITensor *input, ITensor *output, const PermutationVector &perm)
156{
157 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
158 const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input->info(), perm);
159 // Output auto inizialitation if not yet initialized
160 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
161
162 // Perform validation step
163 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm));
164
165 _input = input;
166 _output = output;
167 _perm = perm;
168
169 switch(input->info()->element_size())
170 {
171 case 1:
172 _func = &NEPermuteKernel::run_permute<uint8_t>;
173 break;
174 case 2:
175 _func = &NEPermuteKernel::run_permute<uint16_t>;
176 break;
177 case 4:
178 _func = &NEPermuteKernel::run_permute<uint32_t>;
179 break;
180 default:
181 ARM_COMPUTE_ERROR("Element size not supported");
182 break;
183 }
184
185 // Configure kernel window
186 Window win = calculate_max_window(*input->info(), Steps());
187
188 // The NEPermute doesn't need padding so update_window_and_padding() can be skipped
189 Coordinates coord;
190 coord.set_num_dimensions(output->info()->num_dimensions());
191 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
192
193 ICPPKernel::configure(win);
194}
195
196Status NEPermuteKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
197{
198 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, perm));
199 return Status{};
200}
201
202void NEPermuteKernel::run(const Window &window, const ThreadInfo &info)
203{
204 ARM_COMPUTE_UNUSED(info);
205 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
206 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
207
208 if(_func != nullptr)
209 {
210 (this->*_func)(window);
211 }
212}