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" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/CLValidate.h" |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 29 | #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 Gottardi | aad9f2c | 2018-02-21 11:51:23 +0000 | [diff] [blame] | 34 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 35 | #include "support/ToolchainSupport.h" |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
| 39 | CLPermuteKernel::CLPermuteKernel() |
| 40 | : _input(nullptr), _output(nullptr), _perm() |
| 41 | { |
| 42 | } |
| 43 | namespace |
| 44 | { |
| 45 | TensorShape 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 Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 51 | |
Michele Di Giorgio | b28f329 | 2018-02-02 15:58:01 +0000 | [diff] [blame] | 52 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm) |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 53 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Michele Di Giorgio | b28f329 | 2018-02-02 15:58:01 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QS8, DataType::QASYMM8, |
| 56 | DataType::U16, DataType::S16, DataType::QS16, |
| 57 | DataType::U32, DataType::S32, |
| 58 | DataType::F16, DataType::F32); |
Diego Lopez Recas | 3db5eef | 2018-02-22 16:10:17 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((perm != PermutationVector{ 2U, 0U, 1U }) |
| 60 | && (perm != PermutationVector{ 1U, 2U, 0U }) |
| 61 | && (perm != PermutationVector{ 3U, 2U, 0U, 1U }), |
| 62 | "Only [2, 0, 1], [1, 2, 0] and [3, 2, 0, 1] permutation is supported"); |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 63 | |
Isabella Gottardi | aad9f2c | 2018-02-21 11:51:23 +0000 | [diff] [blame] | 64 | const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input, perm); |
| 65 | |
| 66 | // Validate configured output |
| 67 | if(output->total_size() != 0) |
| 68 | { |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 72 | } |
Michele Di Giorgio | b28f329 | 2018-02-02 15:58:01 +0000 | [diff] [blame] | 73 | return Status{}; |
| 74 | } |
| 75 | } // namespace |
| 76 | |
| 77 | void CLPermuteKernel::configure(const ICLTensor *input, ICLTensor *output, const PermutationVector &perm) |
| 78 | { |
| 79 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 80 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm)); |
| 81 | |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 82 | _input = input; |
| 83 | _output = output; |
| 84 | _perm = perm; |
| 85 | |
| 86 | const TensorShape output_shape = get_output_shape(input->info(), perm); |
| 87 | // Output auto inizialitation if not yet initialized |
| 88 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
| 89 | |
| 90 | // Create kernel |
| 91 | std::set<std::string> build_opts; |
| 92 | |
| 93 | build_opts.emplace("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 94 | build_opts.emplace("-DDEPTH_IN=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 95 | |
| 96 | // Run [2, 0, 1] permute |
| 97 | if(_perm[0] == 2 && _perm[1] == 0 && _perm[2] == 1) |
| 98 | { |
| 99 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("permute_201", build_opts)); |
| 100 | } |
| 101 | // Run [1, 2, 0] permute |
| 102 | else if(_perm[0] == 1 && _perm[1] == 2 && _perm[2] == 0) |
| 103 | { |
| 104 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("permute_120", build_opts)); |
| 105 | } |
| 106 | // Run [3, 2, 0, 1] permute |
| 107 | else if(_perm[0] == 3 && _perm[1] == 2 && _perm[2] == 0 && _perm[3] == 1) |
| 108 | { |
| 109 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("permute_3201", build_opts)); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | ARM_COMPUTE_ERROR("Not supported."); |
| 114 | } |
| 115 | |
| 116 | // Configure kernel window |
| 117 | Window win = calculate_max_window(*input->info(), Steps()); |
| 118 | |
Georgios Pinitas | 6528aa2 | 2018-03-15 18:02:21 +0000 | [diff] [blame] | 119 | // The CLPermute doesn't need padding so update_window_and_padding() can be skipped |
| 120 | Coordinates coord; |
| 121 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 122 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 123 | |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 124 | ICLKernel::configure(win); |
| 125 | } |
| 126 | |
Michele Di Giorgio | b28f329 | 2018-02-02 15:58:01 +0000 | [diff] [blame] | 127 | Status CLPermuteKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm) |
| 128 | { |
Isabella Gottardi | aad9f2c | 2018-02-21 11:51:23 +0000 | [diff] [blame] | 129 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | a8a4d9e | 2018-05-14 14:43:29 +0100 | [diff] [blame] | 130 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, perm)); |
Michele Di Giorgio | b28f329 | 2018-02-02 15:58:01 +0000 | [diff] [blame] | 131 | |
| 132 | return Status{}; |
| 133 | } |
| 134 | |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 135 | void CLPermuteKernel::run(const Window &window, cl::CommandQueue &queue) |
| 136 | { |
| 137 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 138 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 139 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 140 | 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] | 141 | |
| 142 | // Setup output slice |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 143 | Window slice_out(slice_in); |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 144 | slice_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 145 | slice_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 146 | slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 147 | slice_out.set(3, Window::Dimension(0, 0, 0)); |
| 148 | |
| 149 | do |
| 150 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 151 | unsigned int idx = 0; |
| 152 | add_4D_tensor_argument(idx, _input, slice_in); |
| 153 | add_4D_tensor_argument(idx, _output, slice_out); |
| 154 | enqueue(queue, *this, slice_in); |
Michalis Spyrou | 5237e01 | 2018-01-17 09:40:27 +0000 | [diff] [blame] | 155 | } |
| 156 | while(window.slide_window_slice_4D(slice_in) && window.slide_window_slice_4D(slice_out)); |
| 157 | } |