Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 0f3d597 | 2021-01-05 11:36:16 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLTransposeKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Helpers.h" |
Isabella Gottardi | d56e770 | 2018-02-28 14:29:36 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/TensorInfo.h" |
Gian Marco | 5420b28 | 2017-11-29 10:41:38 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/Utils.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/CL/CLValidate.h" |
| 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Manuel Bottini | 0f3d597 | 2021-01-05 11:36:16 +0000 | [diff] [blame] | 36 | #include "support/StringSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 40 | namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 41 | { |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 42 | TensorShape transposed_tensor_shape(const TensorShape &in) |
| 43 | { |
| 44 | TensorShape output_shape{ in }; |
| 45 | const size_t w_out = in[1]; |
| 46 | const size_t h_out = in[0]; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | output_shape.set(0, w_out); |
| 48 | output_shape.set(1, h_out); |
| 49 | |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 50 | return output_shape; |
| 51 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 53 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 54 | { |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 56 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 58 | |
| 59 | if(output->total_size() != 0) |
| 60 | { |
| 61 | const TensorInfo tensor_info = input->clone()->set_tensor_shape(transposed_tensor_shape(input->tensor_shape())); |
| 62 | |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 68 | return Status{}; |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 69 | } |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 70 | } // namespace |
| 71 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 72 | Status CLTransposeKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 73 | { |
| 74 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 75 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 76 | return Status{}; |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void CLTransposeKernel::configure(const ICLTensor *input, ICLTensor *output) |
| 80 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 81 | configure(CLKernelLibrary::get().get_compile_context(), input, output); |
| 82 | } |
| 83 | |
Manuel Bottini | 2803f70 | 2020-04-21 16:20:03 +0100 | [diff] [blame] | 84 | void CLTransposeKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 85 | { |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 86 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 87 | |
| 88 | // Output tensor auto inizialitation if not yet initialized |
| 89 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(transposed_tensor_shape(input->info()->tensor_shape()))); |
| 90 | |
| 91 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); |
Manuel Bottini | 0f3d597 | 2021-01-05 11:36:16 +0000 | [diff] [blame] | 92 | auto padding_info = get_padding_info({ input, output }); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 94 | _input = input; |
| 95 | _output = output; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | |
Manuel Bottini | 0f3d597 | 2021-01-05 11:36:16 +0000 | [diff] [blame] | 97 | const unsigned int vec_size_x = adjust_vec_size(max_cl_vector_width / input->info()->element_size(), input->info()->dimension(0)); |
| 98 | const int vec_size_x_leftovers = input->info()->dimension(0) % vec_size_x; |
| 99 | const unsigned int vec_size_y = adjust_vec_size(max_cl_vector_width / input->info()->element_size(), input->info()->dimension(1)); |
| 100 | const int vec_size_y_leftovers = input->info()->dimension(1) % vec_size_y; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | |
Manuel Bottini | 0f3d597 | 2021-01-05 11:36:16 +0000 | [diff] [blame] | 102 | CLBuildOptions build_opts; |
| 103 | build_opts.add_option("-DDATA_TYPE_IN_BYTES=" + support::cpp11::to_string(input->info()->element_size())); |
| 104 | build_opts.add_option("-DVEC_SIZE_X=" + support::cpp11::to_string(vec_size_x)); |
| 105 | build_opts.add_option("-DVEC_SIZE_LEFTOVER_X=" + support::cpp11::to_string(vec_size_x_leftovers)); |
| 106 | build_opts.add_option("-DVEC_SIZE_Y=" + support::cpp11::to_string(vec_size_y)); |
| 107 | build_opts.add_option("-DVEC_SIZE_LEFTOVER_Y=" + support::cpp11::to_string(vec_size_y_leftovers)); |
| 108 | |
| 109 | _kernel = create_kernel(compile_context, "transpose", build_opts.options()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | |
| 111 | // Configure kernel window |
Manuel Bottini | 0f3d597 | 2021-01-05 11:36:16 +0000 | [diff] [blame] | 112 | Window win = calculate_max_window(*input->info(), Steps(vec_size_x, vec_size_y)); |
| 113 | ICLKernel::configure_internal(win, cl::NDRange(2, 8)); |
| 114 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | } |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 116 | } // namespace arm_compute |