blob: 56ff48be1f1635cade9d3a2883d5e1c100eced2c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottini0f3d5972021-01-05 11:36:16 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLTransposeKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#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 Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/Helpers.h"
Isabella Gottardid56e7702018-02-28 14:29:36 +000031#include "arm_compute/core/TensorInfo.h"
Gian Marco5420b282017-11-29 10:41:38 +000032#include "arm_compute/core/Utils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/CL/CLValidate.h"
34#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Manuel Bottini0f3d5972021-01-05 11:36:16 +000036#include "support/StringSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
Manuel Bottini8481d832019-12-10 15:28:40 +000038namespace arm_compute
39{
Gian Marco7c435f22017-12-05 16:17:23 +000040namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041{
Gian Marco7c435f22017-12-05 16:17:23 +000042TensorShape 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 Barbier6ff3b192017-09-04 18:44:23 +010047 output_shape.set(0, w_out);
48 output_shape.set(1, h_out);
49
Gian Marco7c435f22017-12-05 16:17:23 +000050 return output_shape;
51}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052
Georgios Pinitas631c41a2017-12-06 11:53:03 +000053Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
Gian Marco7c435f22017-12-05 16:17:23 +000054{
Manuel Bottini8481d832019-12-10 15:28:40 +000055 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010056 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini8481d832019-12-10 15:28:40 +000057 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Gian Marco7c435f22017-12-05 16:17:23 +000058
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 Gottardi0a1090a2019-02-14 18:07:36 +000065 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Gian Marco7c435f22017-12-05 16:17:23 +000066 }
67
Georgios Pinitas631c41a2017-12-06 11:53:03 +000068 return Status{};
Gian Marco7c435f22017-12-05 16:17:23 +000069}
Gian Marco7c435f22017-12-05 16:17:23 +000070} // namespace
71
Georgios Pinitas631c41a2017-12-06 11:53:03 +000072Status CLTransposeKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
Gian Marco7c435f22017-12-05 16:17:23 +000073{
74 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
75 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
Georgios Pinitas631c41a2017-12-06 11:53:03 +000076 return Status{};
Gian Marco7c435f22017-12-05 16:17:23 +000077}
78
79void CLTransposeKernel::configure(const ICLTensor *input, ICLTensor *output)
80{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010081 configure(CLKernelLibrary::get().get_compile_context(), input, output);
82}
83
Manuel Bottini2803f702020-04-21 16:20:03 +010084void CLTransposeKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010085{
Gian Marco7c435f22017-12-05 16:17:23 +000086 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 Bottini0f3d5972021-01-05 11:36:16 +000092 auto padding_info = get_padding_info({ input, output });
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093
Anthony Barbierb6eb3532018-08-08 13:20:04 +010094 _input = input;
95 _output = output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096
Manuel Bottini0f3d5972021-01-05 11:36:16 +000097 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 Barbier6ff3b192017-09-04 18:44:23 +0100101
Manuel Bottini0f3d5972021-01-05 11:36:16 +0000102 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 Barbier6ff3b192017-09-04 18:44:23 +0100110
111 // Configure kernel window
Manuel Bottini0f3d5972021-01-05 11:36:16 +0000112 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 Barbier6ff3b192017-09-04 18:44:23 +0100115}
Manuel Bottini8481d832019-12-10 15:28:40 +0000116} // namespace arm_compute