blob: 054c7bf05a900cb6a7788e6db733f6bfc011101c [file] [log] [blame]
Georgios Pinitas8795ffb2017-12-01 16:13:40 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Georgios Pinitas8795ffb2017-12-01 16:13:40 +00003 *
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/CPP/kernels/CPPPermuteKernel.h"
25
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000026#include "arm_compute/core/Helpers.h"
Pablo Tello00afd112018-01-04 10:34:24 +000027#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010028#include "src/core/helpers/AutoConfiguration.h"
29#include "src/core/helpers/WindowHelpers.h"
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000030
31#include <cstddef>
32#include <cstdint>
33
34using namespace arm_compute;
35
36namespace
37{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000038Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000039{
Sheri Zhanga0352d32020-03-16 14:20:56 +000040 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000041 ARM_COMPUTE_RETURN_ERROR_ON_MSG(perm.num_dimensions() > 4, "Only up to 4D permutation vectors are supported");
Pablo Tello02541fb2017-12-15 09:48:59 +000042
Pablo Tello00afd112018-01-04 10:34:24 +000043 const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input, perm);
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000044
45 // Validate configured output
46 if(output->total_size() != 0)
47 {
Pablo Tello02541fb2017-12-15 09:48:59 +000048 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000049 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000050 }
51
Georgios Pinitas631c41a2017-12-06 11:53:03 +000052 return Status{};
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000053}
Pablo Tello00afd112018-01-04 10:34:24 +000054
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000055} // namespace
56
57template <typename T>
58void CPPPermuteKernel::run_permute(const Window &window)
59{
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000060 // Permute strides
Pablo Tello6c6e77a2018-01-23 10:03:27 +000061 Strides strides = _output->info()->strides_in_bytes();
Pablo Tello00afd112018-01-04 10:34:24 +000062 Strides perm_strides = strides;
Pablo Tello6c6e77a2018-01-23 10:03:27 +000063 permute_strides(perm_strides, _perm);
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000064
65 // Create output window
Pablo Tello00afd112018-01-04 10:34:24 +000066 Window window_out(window);
67 const Window::Dimension zero_window = Window::Dimension(0, 0, 0);
68 for(size_t d = 0; d <= _perm.num_dimensions(); ++d)
69 {
70 window_out.set(d, zero_window);
71 }
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000072
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000073 // Create iterators
74 Iterator in(_input, window);
75 Iterator out(_output, window_out);
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000076
Pablo Tello00afd112018-01-04 10:34:24 +000077 if(_input->info()->num_dimensions() <= 3)
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000078 {
79 execute_window_loop(window, [&](const Coordinates & id)
80 {
Pablo Tello00afd112018-01-04 10:34:24 +000081 const int idx = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2];
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000082 *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
83 },
84 in, out);
85 }
Pablo Tello00afd112018-01-04 10:34:24 +000086 else if(_input->info()->num_dimensions() >= 4)
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000087 {
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000088 execute_window_loop(window, [&](const Coordinates & id)
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000089 {
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000090 const int idx = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2] + id[3] * perm_strides[3];
91 *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
92 },
93 in, out);
Pablo Tello02541fb2017-12-15 09:48:59 +000094 }
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000095}
96
97CPPPermuteKernel::CPPPermuteKernel()
98 : _func(), _input(nullptr), _output(nullptr), _perm()
99{
100}
101
102void CPPPermuteKernel::configure(const ITensor *input, ITensor *output, const PermutationVector &perm)
103{
104 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Pablo Tello00afd112018-01-04 10:34:24 +0000105 const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input->info(), perm);
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000106 // Output auto inizialitation if not yet initialized
Pablo Tello02541fb2017-12-15 09:48:59 +0000107 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000108
109 // Perform validation step
110 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm));
111
112 _input = input;
113 _output = output;
114 _perm = perm;
115
116 switch(input->info()->element_size())
117 {
118 case 1:
119 _func = &CPPPermuteKernel::run_permute<uint8_t>;
120 break;
121 case 2:
122 _func = &CPPPermuteKernel::run_permute<uint16_t>;
123 break;
124 case 4:
125 _func = &CPPPermuteKernel::run_permute<uint32_t>;
126 break;
127 default:
128 ARM_COMPUTE_ERROR("Element size not supported");
129 break;
130 }
131
132 // Configure kernel window
133 Window win = calculate_max_window(*input->info(), Steps());
134
135 // The CPPPermute doesn't need padding so update_window_and_padding() can be skipped
136 Coordinates coord;
137 coord.set_num_dimensions(output->info()->num_dimensions());
138 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
139
140 ICPPKernel::configure(win);
141}
142
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000143Status CPPPermuteKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000144{
145 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, perm));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000146 return Status{};
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000147}
148
149void CPPPermuteKernel::run(const Window &window, const ThreadInfo &info)
150{
151 ARM_COMPUTE_UNUSED(info);
152 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
153 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
154
155 if(_func != nullptr)
156 {
157 (this->*_func)(window);
158 }
159}