Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018-2021 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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/gpu/cl/kernels/ClPermuteKernel.h" |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 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/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Utils.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 34 | #include "src/core/CL/CLValidate.h" |
| 35 | #include "src/core/helpers/AutoConfiguration.h" |
| 36 | #include "src/core/helpers/WindowHelpers.h" |
| 37 | #include "support/Cast.h" |
| 38 | #include "support/StringSupport.h" |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace opencl |
| 43 | { |
| 44 | namespace kernels |
| 45 | { |
| 46 | namespace |
| 47 | { |
| 48 | TensorShape get_dst_shape(const ITensorInfo *src, const PermutationVector &perm) |
| 49 | { |
| 50 | TensorShape dst_shape = src->tensor_shape(); |
| 51 | permute(dst_shape, perm); |
| 52 | return dst_shape; |
| 53 | } |
| 54 | |
| 55 | Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const PermutationVector &perm) |
| 56 | { |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->num_dimensions() < 1 || src->num_dimensions() > 4, |
Teresa Charlin | 2788609 | 2021-02-25 20:15:01 +0000 | [diff] [blame] | 60 | "Permutation up to 4-D src tensor is supported"); |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(perm.num_dimensions() < 1 || perm.num_dimensions() > 4, |
| 62 | "Permutation vector size should be less than or equal to 4"); |
| 63 | for(const auto &p : perm) |
| 64 | { |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(p >= perm.num_dimensions(), "Permutation vector has invalid values"); |
| 66 | } |
| 67 | |
| 68 | // Validate configured dst |
| 69 | if(dst->total_size() != 0) |
| 70 | { |
| 71 | const TensorShape dst_shape = misc::shape_calculator::compute_permutation_output_shape(*src, perm); |
| 72 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), dst_shape); |
| 73 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst); |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 75 | } |
| 76 | return Status{}; |
| 77 | } |
| 78 | } // namespace |
| 79 | |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 80 | ClPermuteKernel::ClPermuteKernel() |
| 81 | { |
| 82 | _type = CLKernelType::ELEMENTWISE; |
| 83 | } |
| 84 | |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 85 | void ClPermuteKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *src, ITensorInfo *dst, const PermutationVector &perm) |
| 86 | { |
| 87 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 88 | auto padding_info = get_padding_info({ src, dst }); |
| 89 | const TensorShape dst_shape = get_dst_shape(src, perm); |
Teresa Charlin | 2788609 | 2021-02-25 20:15:01 +0000 | [diff] [blame] | 90 | // Output auto initialization if not yet initialized |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 91 | auto_init_if_empty(*dst, src->clone()->set_tensor_shape(dst_shape)); |
| 92 | |
| 93 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, perm)); |
| 94 | |
| 95 | _perm = perm; |
| 96 | |
| 97 | // Create kernel |
| 98 | CLBuildOptions build_opts; |
| 99 | build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(src->data_type()))); |
| 100 | build_opts.add_option("-DDEPTH_IN=" + support::cpp11::to_string(src->dimension(2))); |
| 101 | // New positions of width(W), height(H), channel(C) and batch(D) based on permutation vector |
| 102 | build_opts.add_option("-DP1=" + support::cpp11::to_string((_perm.num_dimensions() >= 1) ? perm[0] : 0)); |
| 103 | build_opts.add_option("-DP2=" + support::cpp11::to_string((_perm.num_dimensions() >= 2) ? perm[1] : 1)); |
| 104 | build_opts.add_option("-DP3=" + support::cpp11::to_string((_perm.num_dimensions() >= 3) ? perm[2] : 2)); |
| 105 | build_opts.add_option("-DP4=" + support::cpp11::to_string((_perm.num_dimensions() >= 4) ? perm[3] : 3)); |
| 106 | |
| 107 | _kernel = create_kernel(compile_context, "permute", build_opts.options()); |
| 108 | |
| 109 | // Configure kernel window |
| 110 | Window win = calculate_max_window(*src, Steps()); |
| 111 | |
Sheri Zhang | 7e20e29 | 2021-02-02 11:49:34 +0000 | [diff] [blame] | 112 | ICLKernel::configure_internal(win); |
| 113 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
| 114 | } |
| 115 | |
| 116 | Status ClPermuteKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const PermutationVector &perm) |
| 117 | { |
| 118 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 119 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, perm)); |
| 120 | |
| 121 | return Status{}; |
| 122 | } |
| 123 | |
| 124 | void ClPermuteKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue) |
| 125 | { |
| 126 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 127 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 128 | |
| 129 | const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC)); |
| 130 | auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST)); |
| 131 | |
| 132 | Window slice_in = window.first_slice_window_4D().collapse(ICLKernel::window(), 2, 4); |
| 133 | |
| 134 | // Setup dst slice |
| 135 | Window slice_out(slice_in); |
| 136 | slice_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 137 | slice_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 138 | slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 139 | slice_out.set(3, Window::Dimension(0, 0, 0)); |
| 140 | |
| 141 | do |
| 142 | { |
| 143 | unsigned int idx = 0; |
| 144 | add_4D_tensor_argument(idx, src, slice_in); |
| 145 | add_4D_tensor_argument(idx, dst, slice_out); |
| 146 | enqueue(queue, *this, slice_in, lws_hint()); |
| 147 | } |
| 148 | while(window.slide_window_slice_4D(slice_in) && window.slide_window_slice_4D(slice_out)); |
| 149 | } |
| 150 | } // namespace kernels |
| 151 | } // namespace opencl |
| 152 | } // namespace arm_compute |