blob: d9fe5b0c0aaa50231c8f883ae59ce72525f25058 [file] [log] [blame]
Georgios Pinitas8795ffb2017-12-01 16:13:40 +00001/*
Pablo Tello6c6e77a2018-01-23 10:03:27 +00002 * Copyright (c) 2017-2018 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
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"
Pablo Tello00afd112018-01-04 10:34:24 +000032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000033
34#include <cstddef>
35#include <cstdint>
36
37using namespace arm_compute;
38
39namespace
40{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000041Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000042{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010043 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8,
44 DataType::U16, DataType::S16,
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000045 DataType::U32, DataType::S32,
46 DataType::F16, DataType::F32);
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000047 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 +000048
Pablo Tello00afd112018-01-04 10:34:24 +000049 const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input, perm);
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000050
51 // Validate configured output
52 if(output->total_size() != 0)
53 {
Pablo Tello02541fb2017-12-15 09:48:59 +000054 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000055 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000056 }
57
Georgios Pinitas631c41a2017-12-06 11:53:03 +000058 return Status{};
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000059}
Pablo Tello00afd112018-01-04 10:34:24 +000060
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000061} // namespace
62
63template <typename T>
64void CPPPermuteKernel::run_permute(const Window &window)
65{
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000066 // Permute strides
Pablo Tello6c6e77a2018-01-23 10:03:27 +000067 Strides strides = _output->info()->strides_in_bytes();
Pablo Tello00afd112018-01-04 10:34:24 +000068 Strides perm_strides = strides;
Pablo Tello6c6e77a2018-01-23 10:03:27 +000069 permute_strides(perm_strides, _perm);
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000070
71 // Create output window
Pablo Tello00afd112018-01-04 10:34:24 +000072 Window window_out(window);
73 const Window::Dimension zero_window = Window::Dimension(0, 0, 0);
74 for(size_t d = 0; d <= _perm.num_dimensions(); ++d)
75 {
76 window_out.set(d, zero_window);
77 }
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000078
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000079 // Create iterators
80 Iterator in(_input, window);
81 Iterator out(_output, window_out);
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000082
Pablo Tello00afd112018-01-04 10:34:24 +000083 if(_input->info()->num_dimensions() <= 3)
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000084 {
85 execute_window_loop(window, [&](const Coordinates & id)
86 {
Pablo Tello00afd112018-01-04 10:34:24 +000087 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 +000088 *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
89 },
90 in, out);
91 }
Pablo Tello00afd112018-01-04 10:34:24 +000092 else if(_input->info()->num_dimensions() >= 4)
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000093 {
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000094 execute_window_loop(window, [&](const Coordinates & id)
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000095 {
Georgios Pinitas69af6cf2018-02-14 19:23:44 +000096 const int idx = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2] + id[3] * perm_strides[3];
97 *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
98 },
99 in, out);
Pablo Tello02541fb2017-12-15 09:48:59 +0000100 }
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000101}
102
103CPPPermuteKernel::CPPPermuteKernel()
104 : _func(), _input(nullptr), _output(nullptr), _perm()
105{
106}
107
108void CPPPermuteKernel::configure(const ITensor *input, ITensor *output, const PermutationVector &perm)
109{
110 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Pablo Tello00afd112018-01-04 10:34:24 +0000111 const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input->info(), perm);
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000112 // Output auto inizialitation if not yet initialized
Pablo Tello02541fb2017-12-15 09:48:59 +0000113 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000114
115 // Perform validation step
116 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm));
117
118 _input = input;
119 _output = output;
120 _perm = perm;
121
122 switch(input->info()->element_size())
123 {
124 case 1:
125 _func = &CPPPermuteKernel::run_permute<uint8_t>;
126 break;
127 case 2:
128 _func = &CPPPermuteKernel::run_permute<uint16_t>;
129 break;
130 case 4:
131 _func = &CPPPermuteKernel::run_permute<uint32_t>;
132 break;
133 default:
134 ARM_COMPUTE_ERROR("Element size not supported");
135 break;
136 }
137
138 // Configure kernel window
139 Window win = calculate_max_window(*input->info(), Steps());
140
141 // The CPPPermute doesn't need padding so update_window_and_padding() can be skipped
142 Coordinates coord;
143 coord.set_num_dimensions(output->info()->num_dimensions());
144 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
145
146 ICPPKernel::configure(win);
147}
148
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000149Status CPPPermuteKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000150{
151 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, perm));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000152 return Status{};
Georgios Pinitas8795ffb2017-12-01 16:13:40 +0000153}
154
155void CPPPermuteKernel::run(const Window &window, const ThreadInfo &info)
156{
157 ARM_COMPUTE_UNUSED(info);
158 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
159 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
160
161 if(_func != nullptr)
162 {
163 (this->*_func)(window);
164 }
165}