Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 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" |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 25 | #include "arm_compute/core/CL/ICLTensor.h" |
Isabella Gottardi | aad9f2c | 2018-02-21 11:51:23 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 27 | #include "src/core/helpers/AutoConfiguration.h" |
| 28 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 29 | #include "support/StringSupport.h" |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace arm_compute; |
| 32 | |
| 33 | CLPermuteKernel::CLPermuteKernel() |
| 34 | : _input(nullptr), _output(nullptr), _perm() |
| 35 | { |
| 36 | } |
| 37 | namespace |
| 38 | { |
| 39 | TensorShape get_output_shape(const ITensorInfo *input, const PermutationVector &perm) |
| 40 | { |
| 41 | TensorShape output_shape = input->tensor_shape(); |
| 42 | permute(output_shape, perm); |
| 43 | return output_shape; |
| 44 | } |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 45 | |
Michele Di Giorgio | b28f329 | 2018-02-02 15:58:01 +0000 | [diff] [blame] | 46 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm) |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 47 | { |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
shubham | e1a4e37 | 2019-01-07 21:37:55 +0530 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() < 1 || input->num_dimensions() > 4, |
| 51 | "Permutation upto 4-D input tensor is supported"); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(perm.num_dimensions() < 1 || perm.num_dimensions() > 4, |
| 53 | "Permutation vector size should be less than or equal to 4"); |
| 54 | for(const auto &p : perm) |
| 55 | { |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(p >= perm.num_dimensions(), "Permutation vector has invalid values"); |
| 57 | } |
Isabella Gottardi | aad9f2c | 2018-02-21 11:51:23 +0000 | [diff] [blame] | 58 | |
| 59 | // Validate configured output |
| 60 | if(output->total_size() != 0) |
| 61 | { |
shubham | e1a4e37 | 2019-01-07 21:37:55 +0530 | [diff] [blame] | 62 | const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input, perm); |
Isabella Gottardi | aad9f2c | 2018-02-21 11:51:23 +0000 | [diff] [blame] | 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Isabella Gottardi | aad9f2c | 2018-02-21 11:51:23 +0000 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | aad9f2c | 2018-02-21 11:51:23 +0000 | [diff] [blame] | 66 | } |
Michele Di Giorgio | b28f329 | 2018-02-02 15:58:01 +0000 | [diff] [blame] | 67 | return Status{}; |
| 68 | } |
| 69 | } // namespace |
| 70 | |
| 71 | void CLPermuteKernel::configure(const ICLTensor *input, ICLTensor *output, const PermutationVector &perm) |
| 72 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 73 | configure(CLKernelLibrary::get().get_compile_context(), input, output, perm); |
| 74 | } |
| 75 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 76 | void CLPermuteKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const PermutationVector &perm) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 77 | { |
Michele Di Giorgio | b28f329 | 2018-02-02 15:58:01 +0000 | [diff] [blame] | 78 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 79 | const TensorShape output_shape = get_output_shape(input->info(), perm); |
| 80 | // Output auto inizialitation if not yet initialized |
| 81 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
| 82 | |
Michele Di Giorgio | b28f329 | 2018-02-02 15:58:01 +0000 | [diff] [blame] | 83 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm)); |
| 84 | |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 85 | _input = input; |
| 86 | _output = output; |
| 87 | _perm = perm; |
| 88 | |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 89 | // Create kernel |
shubham | e1a4e37 | 2019-01-07 21:37:55 +0530 | [diff] [blame] | 90 | CLBuildOptions build_opts; |
Sheri Zhang | 0de45d0 | 2020-04-17 14:59:13 +0100 | [diff] [blame] | 91 | build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(input->info()->data_type()))); |
shubham | e1a4e37 | 2019-01-07 21:37:55 +0530 | [diff] [blame] | 92 | build_opts.add_option("-DDEPTH_IN=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 93 | // New positions of width(W), height(H), channel(C) and batch(D) based on permutation vector |
| 94 | build_opts.add_option("-DP1=" + support::cpp11::to_string((_perm.num_dimensions() >= 1) ? perm[0] : 0)); |
| 95 | build_opts.add_option("-DP2=" + support::cpp11::to_string((_perm.num_dimensions() >= 2) ? perm[1] : 1)); |
| 96 | build_opts.add_option("-DP3=" + support::cpp11::to_string((_perm.num_dimensions() >= 3) ? perm[2] : 2)); |
| 97 | build_opts.add_option("-DP4=" + support::cpp11::to_string((_perm.num_dimensions() >= 4) ? perm[3] : 3)); |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 98 | |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 99 | _kernel = create_kernel(compile_context, "permute", build_opts.options()); |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 100 | |
| 101 | // Configure kernel window |
| 102 | Window win = calculate_max_window(*input->info(), Steps()); |
| 103 | |
Georgios Pinitas | 6528aa2 | 2018-03-15 18:02:21 +0000 | [diff] [blame] | 104 | // The CLPermute doesn't need padding so update_window_and_padding() can be skipped |
| 105 | Coordinates coord; |
| 106 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 107 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 108 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 109 | ICLKernel::configure_internal(win); |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Michele Di Giorgio | b28f329 | 2018-02-02 15:58:01 +0000 | [diff] [blame] | 112 | Status CLPermuteKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm) |
| 113 | { |
Isabella Gottardi | aad9f2c | 2018-02-21 11:51:23 +0000 | [diff] [blame] | 114 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | a8a4d9e | 2018-05-14 14:43:29 +0100 | [diff] [blame] | 115 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, perm)); |
Michele Di Giorgio | b28f329 | 2018-02-02 15:58:01 +0000 | [diff] [blame] | 116 | |
| 117 | return Status{}; |
| 118 | } |
| 119 | |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 120 | void CLPermuteKernel::run(const Window &window, cl::CommandQueue &queue) |
| 121 | { |
| 122 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 123 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 124 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 125 | Window slice_in = window.first_slice_window_4D().collapse(ICLKernel::window(), 2, 4); |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 126 | |
| 127 | // Setup output slice |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 128 | Window slice_out(slice_in); |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 129 | slice_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 130 | slice_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 131 | slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 132 | slice_out.set(3, Window::Dimension(0, 0, 0)); |
| 133 | |
| 134 | do |
| 135 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 136 | unsigned int idx = 0; |
| 137 | add_4D_tensor_argument(idx, _input, slice_in); |
| 138 | add_4D_tensor_argument(idx, _output, slice_out); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 139 | enqueue(queue, *this, slice_in, lws_hint()); |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 140 | } |
| 141 | while(window.slide_window_slice_4D(slice_in) && window.slide_window_slice_4D(slice_out)); |
| 142 | } |