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