blob: 9a4d72db88d4dade6c415a7edfd4caaf1d5bd5d1 [file] [log] [blame]
Michalis Spyrou5237e012018-01-17 09:40:27 +00001/*
Manuel Bottini8481d832019-12-10 15:28:40 +00002 * Copyright (c) 2018-2020 ARM Limited.
Michalis Spyrou5237e012018-01-17 09:40:27 +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/CL/kernels/CLPermuteKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010028#include "arm_compute/core/CL/CLValidate.h"
Michalis Spyrou5237e012018-01-17 09:40:27 +000029#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/CL/OpenCL.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/Types.h"
Isabella Gottardiaad9f2c2018-02-21 11:51:23 +000034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/StringSupport.h"
Michalis Spyrou5237e012018-01-17 09:40:27 +000036
37using namespace arm_compute;
38
39CLPermuteKernel::CLPermuteKernel()
40 : _input(nullptr), _output(nullptr), _perm()
41{
42}
43namespace
44{
45TensorShape get_output_shape(const ITensorInfo *input, const PermutationVector &perm)
46{
47 TensorShape output_shape = input->tensor_shape();
48 permute(output_shape, perm);
49 return output_shape;
50}
Pablo Tello6c6e77a2018-01-23 10:03:27 +000051
Michele Di Giorgiob28f3292018-02-02 15:58:01 +000052Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
Michalis Spyrou5237e012018-01-17 09:40:27 +000053{
Matthew Bentham758b5ba2020-03-05 23:37:48 +000054 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Manuel Bottini8481d832019-12-10 15:28:40 +000055 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010056 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
shubhame1a4e372019-01-07 21:37:55 +053057 ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() < 1 || input->num_dimensions() > 4,
58 "Permutation upto 4-D input tensor is supported");
59 ARM_COMPUTE_RETURN_ERROR_ON_MSG(perm.num_dimensions() < 1 || perm.num_dimensions() > 4,
60 "Permutation vector size should be less than or equal to 4");
61 for(const auto &p : perm)
62 {
63 ARM_COMPUTE_RETURN_ERROR_ON_MSG(p >= perm.num_dimensions(), "Permutation vector has invalid values");
64 }
Isabella Gottardiaad9f2c2018-02-21 11:51:23 +000065
66 // Validate configured output
67 if(output->total_size() != 0)
68 {
shubhame1a4e372019-01-07 21:37:55 +053069 const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input, perm);
Isabella Gottardiaad9f2c2018-02-21 11:51:23 +000070 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000071 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Isabella Gottardiaad9f2c2018-02-21 11:51:23 +000072 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardiaad9f2c2018-02-21 11:51:23 +000073 }
Michele Di Giorgiob28f3292018-02-02 15:58:01 +000074 return Status{};
75}
76} // namespace
77
78void CLPermuteKernel::configure(const ICLTensor *input, ICLTensor *output, const PermutationVector &perm)
79{
80 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
81 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm));
82
Michalis Spyrou5237e012018-01-17 09:40:27 +000083 _input = input;
84 _output = output;
85 _perm = perm;
86
87 const TensorShape output_shape = get_output_shape(input->info(), perm);
88 // Output auto inizialitation if not yet initialized
89 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
90
91 // Create kernel
shubhame1a4e372019-01-07 21:37:55 +053092 CLBuildOptions build_opts;
93 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
94 build_opts.add_option("-DDEPTH_IN=" + support::cpp11::to_string(input->info()->dimension(2)));
95 // New positions of width(W), height(H), channel(C) and batch(D) based on permutation vector
96 build_opts.add_option("-DP1=" + support::cpp11::to_string((_perm.num_dimensions() >= 1) ? perm[0] : 0));
97 build_opts.add_option("-DP2=" + support::cpp11::to_string((_perm.num_dimensions() >= 2) ? perm[1] : 1));
98 build_opts.add_option("-DP3=" + support::cpp11::to_string((_perm.num_dimensions() >= 3) ? perm[2] : 2));
99 build_opts.add_option("-DP4=" + support::cpp11::to_string((_perm.num_dimensions() >= 4) ? perm[3] : 3));
Michalis Spyrou5237e012018-01-17 09:40:27 +0000100
shubhame1a4e372019-01-07 21:37:55 +0530101 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("permute", build_opts.options()));
Michalis Spyrou5237e012018-01-17 09:40:27 +0000102
103 // Configure kernel window
104 Window win = calculate_max_window(*input->info(), Steps());
105
Georgios Pinitas6528aa22018-03-15 18:02:21 +0000106 // The CLPermute doesn't need padding so update_window_and_padding() can be skipped
107 Coordinates coord;
108 coord.set_num_dimensions(output->info()->num_dimensions());
109 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
110
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100111 ICLKernel::configure_internal(win);
Michalis Spyrou5237e012018-01-17 09:40:27 +0000112}
113
Michele Di Giorgiob28f3292018-02-02 15:58:01 +0000114Status CLPermuteKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
115{
Isabella Gottardiaad9f2c2018-02-21 11:51:23 +0000116 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Anthony Barbiera8a4d9e2018-05-14 14:43:29 +0100117 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, perm));
Michele Di Giorgiob28f3292018-02-02 15:58:01 +0000118
119 return Status{};
120}
121
Michalis Spyrou5237e012018-01-17 09:40:27 +0000122void CLPermuteKernel::run(const Window &window, cl::CommandQueue &queue)
123{
124 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
125 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
126
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000127 Window slice_in = window.first_slice_window_4D().collapse(ICLKernel::window(), 2, 4);
Michalis Spyrou5237e012018-01-17 09:40:27 +0000128
129 // Setup output slice
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000130 Window slice_out(slice_in);
Michalis Spyrou5237e012018-01-17 09:40:27 +0000131 slice_out.set(Window::DimX, Window::Dimension(0, 0, 0));
132 slice_out.set(Window::DimY, Window::Dimension(0, 0, 0));
133 slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
134 slice_out.set(3, Window::Dimension(0, 0, 0));
135
136 do
137 {
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000138 unsigned int idx = 0;
139 add_4D_tensor_argument(idx, _input, slice_in);
140 add_4D_tensor_argument(idx, _output, slice_out);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100141 enqueue(queue, *this, slice_in, lws_hint());
Michalis Spyrou5237e012018-01-17 09:40:27 +0000142 }
143 while(window.slide_window_slice_4D(slice_in) && window.slide_window_slice_4D(slice_out));
144}