Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 1 | /* |
| 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" |
| 33 | #include "support/ToolchainSupport.h" |
| 34 | |
| 35 | using namespace arm_compute; |
| 36 | |
| 37 | CLPermuteKernel::CLPermuteKernel() |
| 38 | : _input(nullptr), _output(nullptr), _perm() |
| 39 | { |
| 40 | } |
| 41 | namespace |
| 42 | { |
| 43 | TensorShape get_output_shape(const ITensorInfo *input, const PermutationVector &perm) |
| 44 | { |
| 45 | TensorShape output_shape = input->tensor_shape(); |
| 46 | permute(output_shape, perm); |
| 47 | return output_shape; |
| 48 | } |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 49 | } // namespace |
| 50 | |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 51 | void CLPermuteKernel::configure(const ICLTensor *input, ICLTensor *output, const PermutationVector &perm) |
| 52 | { |
| 53 | ARM_COMPUTE_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); |
| 57 | ARM_COMPUTE_ERROR_ON_MSG(input->info()->num_dimensions() < 3, "Invalid input size!"); |
| 58 | ARM_COMPUTE_ERROR_ON_MSG( |
| 59 | (perm.num_dimensions() != 3 && ((perm[0] != 2 && perm[1] != 0 && perm[2] != 1) || (perm[0] != 1 && perm[1] != 2 && perm[2] != 0))) && (perm.num_dimensions() != 4 && ((perm[0] != 2 && perm[1] != 0 |
| 60 | && perm[2] != 1) |
| 61 | || (perm[0] != 1 && perm[1] != 2 && perm[2] != 0))), |
| 62 | "Only [2, 0, 1],[1, 2, 0] and [3, 2, 0, 1] permutation is supported"); |
| 63 | |
| 64 | _input = input; |
| 65 | _output = output; |
| 66 | _perm = perm; |
| 67 | |
| 68 | const TensorShape output_shape = get_output_shape(input->info(), perm); |
| 69 | // Output auto inizialitation if not yet initialized |
| 70 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
| 71 | |
| 72 | // Create kernel |
| 73 | std::set<std::string> build_opts; |
| 74 | |
| 75 | build_opts.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 76 | build_opts.emplace("-DDEPTH_IN=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 77 | |
| 78 | // Run [2, 0, 1] permute |
| 79 | if(_perm[0] == 2 && _perm[1] == 0 && _perm[2] == 1) |
| 80 | { |
| 81 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("permute_201", build_opts)); |
| 82 | } |
| 83 | // Run [1, 2, 0] permute |
| 84 | else if(_perm[0] == 1 && _perm[1] == 2 && _perm[2] == 0) |
| 85 | { |
| 86 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("permute_120", build_opts)); |
| 87 | } |
| 88 | // Run [3, 2, 0, 1] permute |
| 89 | else if(_perm[0] == 3 && _perm[1] == 2 && _perm[2] == 0 && _perm[3] == 1) |
| 90 | { |
| 91 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("permute_3201", build_opts)); |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | ARM_COMPUTE_ERROR("Not supported."); |
| 96 | } |
| 97 | |
| 98 | // Configure kernel window |
| 99 | Window win = calculate_max_window(*input->info(), Steps()); |
| 100 | |
| 101 | ICLKernel::configure(win); |
| 102 | } |
| 103 | |
| 104 | void CLPermuteKernel::run(const Window &window, cl::CommandQueue &queue) |
| 105 | { |
| 106 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 107 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 108 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame^] | 109 | 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] | 110 | |
| 111 | // Setup output slice |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame^] | 112 | Window slice_out(slice_in); |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 113 | slice_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 114 | slice_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 115 | slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 116 | slice_out.set(3, Window::Dimension(0, 0, 0)); |
| 117 | |
| 118 | do |
| 119 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame^] | 120 | unsigned int idx = 0; |
| 121 | add_4D_tensor_argument(idx, _input, slice_in); |
| 122 | add_4D_tensor_argument(idx, _output, slice_out); |
| 123 | enqueue(queue, *this, slice_in); |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 124 | } |
| 125 | while(window.slide_window_slice_4D(slice_in) && window.slide_window_slice_4D(slice_out)); |
| 126 | } |