blob: 02fc667aee15e5c81c8ce1e1b8e9a4be96363227 [file] [log] [blame]
Michalis Spyrou5237e012018-01-17 09:40:27 +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/CL/kernels/CLPermuteKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/Types.h"
Isabella Gottardiaad9f2c2018-02-21 11:51:23 +000033#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michalis Spyrou5237e012018-01-17 09:40:27 +000034#include "support/ToolchainSupport.h"
35
36using namespace arm_compute;
37
38CLPermuteKernel::CLPermuteKernel()
39 : _input(nullptr), _output(nullptr), _perm()
40{
41}
42namespace
43{
44TensorShape get_output_shape(const ITensorInfo *input, const PermutationVector &perm)
45{
46 TensorShape output_shape = input->tensor_shape();
47 permute(output_shape, perm);
48 return output_shape;
49}
Pablo Tello6c6e77a2018-01-23 10:03:27 +000050
Michele Di Giorgiob28f3292018-02-02 15:58:01 +000051Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
Michalis Spyrou5237e012018-01-17 09:40:27 +000052{
Michele Di Giorgiob28f3292018-02-02 15:58:01 +000053 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QS8, DataType::QASYMM8,
54 DataType::U16, DataType::S16, DataType::QS16,
55 DataType::U32, DataType::S32,
56 DataType::F16, DataType::F32);
Diego Lopez Recas3db5eef2018-02-22 16:10:17 +000057 ARM_COMPUTE_RETURN_ERROR_ON_MSG((perm != PermutationVector{ 2U, 0U, 1U })
58 && (perm != PermutationVector{ 1U, 2U, 0U })
59 && (perm != PermutationVector{ 3U, 2U, 0U, 1U }),
60 "Only [2, 0, 1], [1, 2, 0] and [3, 2, 0, 1] permutation is supported");
Michalis Spyrou5237e012018-01-17 09:40:27 +000061
Isabella Gottardiaad9f2c2018-02-21 11:51:23 +000062 const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input, perm);
63
64 // Validate configured output
65 if(output->total_size() != 0)
66 {
67 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
68 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
69 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
70 }
Michele Di Giorgiob28f3292018-02-02 15:58:01 +000071 return Status{};
72}
73} // namespace
74
75void CLPermuteKernel::configure(const ICLTensor *input, ICLTensor *output, const PermutationVector &perm)
76{
77 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
78 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm));
79
Michalis Spyrou5237e012018-01-17 09:40:27 +000080 _input = input;
81 _output = output;
82 _perm = perm;
83
84 const TensorShape output_shape = get_output_shape(input->info(), perm);
85 // Output auto inizialitation if not yet initialized
86 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
87
88 // Create kernel
89 std::set<std::string> build_opts;
90
91 build_opts.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
92 build_opts.emplace("-DDEPTH_IN=" + support::cpp11::to_string(input->info()->dimension(2)));
93
94 // Run [2, 0, 1] permute
95 if(_perm[0] == 2 && _perm[1] == 0 && _perm[2] == 1)
96 {
97 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("permute_201", build_opts));
98 }
99 // Run [1, 2, 0] permute
100 else if(_perm[0] == 1 && _perm[1] == 2 && _perm[2] == 0)
101 {
102 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("permute_120", build_opts));
103 }
104 // Run [3, 2, 0, 1] permute
105 else if(_perm[0] == 3 && _perm[1] == 2 && _perm[2] == 0 && _perm[3] == 1)
106 {
107 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("permute_3201", build_opts));
108 }
109 else
110 {
111 ARM_COMPUTE_ERROR("Not supported.");
112 }
113
114 // Configure kernel window
115 Window win = calculate_max_window(*input->info(), Steps());
116
Georgios Pinitas6528aa22018-03-15 18:02:21 +0000117 // The CLPermute doesn't need padding so update_window_and_padding() can be skipped
118 Coordinates coord;
119 coord.set_num_dimensions(output->info()->num_dimensions());
120 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
121
Michalis Spyrou5237e012018-01-17 09:40:27 +0000122 ICLKernel::configure(win);
123}
124
Michele Di Giorgiob28f3292018-02-02 15:58:01 +0000125Status CLPermuteKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
126{
Isabella Gottardiaad9f2c2018-02-21 11:51:23 +0000127 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Michele Di Giorgiob28f3292018-02-02 15:58:01 +0000128 ARM_COMPUTE_RETURN_ERROR_ON(validate_arguments(input, output, perm));
129
130 return Status{};
131}
132
Michalis Spyrou5237e012018-01-17 09:40:27 +0000133void CLPermuteKernel::run(const Window &window, cl::CommandQueue &queue)
134{
135 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
136 ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window);
137
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000138 Window slice_in = window.first_slice_window_4D().collapse(ICLKernel::window(), 2, 4);
Michalis Spyrou5237e012018-01-17 09:40:27 +0000139
140 // Setup output slice
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000141 Window slice_out(slice_in);
Michalis Spyrou5237e012018-01-17 09:40:27 +0000142 slice_out.set(Window::DimX, Window::Dimension(0, 0, 0));
143 slice_out.set(Window::DimY, Window::Dimension(0, 0, 0));
144 slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
145 slice_out.set(3, Window::Dimension(0, 0, 0));
146
147 do
148 {
Diego Lopez Recas0021d752017-12-18 14:42:56 +0000149 unsigned int idx = 0;
150 add_4D_tensor_argument(idx, _input, slice_in);
151 add_4D_tensor_argument(idx, _output, slice_out);
152 enqueue(queue, *this, slice_in);
Michalis Spyrou5237e012018-01-17 09:40:27 +0000153 }
154 while(window.slide_window_slice_4D(slice_in) && window.slide_window_slice_4D(slice_out));
155}