blob: aa1b92a685eec3e4cd273f093eaf461a9bc93f54 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco36a0a462018-01-12 10:21:40 +00002 * Copyright (c) 2017-2018 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 */
24#include "arm_compute/core/CL/kernels/CLGEMMTranspose1xWKernel.h"
25
Georgios Pinitas535fedd2018-05-04 18:52:25 +010026#include "arm_compute/core/AccessWindowStatic.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010029#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/CL/OpenCL.h"
32#include "arm_compute/core/Error.h"
33#include "arm_compute/core/Helpers.h"
34#include "arm_compute/core/Types.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035#include "arm_compute/core/Window.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000036#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38#include <cmath>
39
40using namespace arm_compute;
Georgios Pinitas358ca202017-12-07 16:47:52 +000041using namespace arm_compute::misc::shape_calculator;
42
43namespace
44{
Gian Marco36a0a462018-01-12 10:21:40 +000045Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int mult_transpose1xW_width)
Georgios Pinitas358ca202017-12-07 16:47:52 +000046{
Gian Marco36a0a462018-01-12 10:21:40 +000047 ARM_COMPUTE_RETURN_ERROR_ON(mult_transpose1xW_width < 1);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010048 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::U8, DataType::S8,
50 DataType::U16, DataType::S16, DataType::U32, DataType::S32,
Georgios Pinitas358ca202017-12-07 16:47:52 +000051 DataType::F16, DataType::F32);
52
53 if(output->total_size() != 0)
54 {
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(),
Gian Marco36a0a462018-01-12 10:21:40 +000056 compute_transpose1xW_with_element_size_shape(*input, mult_transpose1xW_width));
Georgios Pinitas358ca202017-12-07 16:47:52 +000057 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitas358ca202017-12-07 16:47:52 +000058 }
59
60 return Status{};
61}
62
Gian Marco36a0a462018-01-12 10:21:40 +000063std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, unsigned int &num_elems_processed_per_iteration, int mult_transpose1xW_width)
Georgios Pinitas358ca202017-12-07 16:47:52 +000064{
65 num_elems_processed_per_iteration = 16 / input->element_size();
66
Gian Marco36a0a462018-01-12 10:21:40 +000067 const int scale_x = num_elems_processed_per_iteration * mult_transpose1xW_width;
Georgios Pinitas358ca202017-12-07 16:47:52 +000068 bool window_changed = false;
69
70 // Configure kernel window
71 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
72
Georgios Pinitas358ca202017-12-07 16:47:52 +000073 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
Georgios Pinitas358ca202017-12-07 16:47:52 +000074
Gian Marco Iodice750641d2018-05-08 12:01:57 +010075 // Output tensor auto inizialitation if not yet initialized
76 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*input, mult_transpose1xW_width)));
77
Georgios Pinitas358ca202017-12-07 16:47:52 +000078 // Configure window in case of configured output
Gian Marco Iodice750641d2018-05-08 12:01:57 +010079 AccessWindowStatic output_access(output, 0, 0, ceil_to_multiple(output->dimension(0), scale_x), output->dimension(1));
80 window_changed = window_changed || update_window_and_padding(win, input_access, output_access);
81 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), input->tensor_shape()));
Georgios Pinitas358ca202017-12-07 16:47:52 +000082
Gian Marcoae2af742018-02-15 12:35:44 +000083 // Collapse along the Z direction
84 Window collapsed = win.collapse(win, Window::DimZ);
85
Georgios Pinitas358ca202017-12-07 16:47:52 +000086 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Gian Marcoae2af742018-02-15 12:35:44 +000087 return std::make_pair(err, collapsed);
Georgios Pinitas358ca202017-12-07 16:47:52 +000088}
89} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
Gian Marco36a0a462018-01-12 10:21:40 +000091void CLGEMMTranspose1xWKernel::configure(const ICLTensor *input, ICLTensor *output, int mult_transpose1xW_width)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092{
Georgios Pinitas358ca202017-12-07 16:47:52 +000093 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094
95 // Output tensor auto inizialitation if not yet initialized
Gian Marco36a0a462018-01-12 10:21:40 +000096 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*input->info(), mult_transpose1xW_width)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097
Georgios Pinitas358ca202017-12-07 16:47:52 +000098 // Perform validate step
Gian Marco36a0a462018-01-12 10:21:40 +000099 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mult_transpose1xW_width));
Georgios Pinitas4cbee6e2017-06-19 13:02:56 +0100100
101 _input = input;
102 _output = output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
Georgios Pinitas358ca202017-12-07 16:47:52 +0000104 // Configure kernel window
Gian Marco36a0a462018-01-12 10:21:40 +0000105 // Note: num_elems_processed_per_iteration will be set in validate_and_configure_window()
Georgios Pinitas358ca202017-12-07 16:47:52 +0000106 unsigned int num_elems_processed_per_iteration = 1;
Gian Marco36a0a462018-01-12 10:21:40 +0000107 auto win_config = validate_and_configure_window(input->info(), output->info(), num_elems_processed_per_iteration, mult_transpose1xW_width);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000108 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100109 ICLKernel::configure_internal(win_config.second);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000110
Gian Marco36a0a462018-01-12 10:21:40 +0000111 // Create build options
112 CLBuildOptions build_opts;
Gian Marco19835e52018-01-30 13:35:54 +0000113 build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->info()->element_size()));
Gian Marco36a0a462018-01-12 10:21:40 +0000114 build_opts.add_option("-DTRANSPOSE_W=" + support::cpp11::to_string(num_elems_processed_per_iteration));
115 build_opts.add_option("-DMULT_TRANSPOSE1XW_WIDTH=" + support::cpp11::to_string(mult_transpose1xW_width));
116
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117 /*
118 * Following an example of how the transposition1xW works when the input data type is F32
119 *
120 * |a00 a01 a02 a03|
121 * |a10 a11 a12 a13|
122 * |a20 a21 a22 a23| = | a00 a01 a02 a03 || a10 a11 a12 a13 || a20 a21 a22 a23 || a30 a31 a32 a33 |
123 * |a30 a31 a32 a33|
124 *
Gian Marco36a0a462018-01-12 10:21:40 +0000125 * The output matrix will have the following shape: [ height * W, ceil(width / W) ], where W = (16 / element size of the tensor) * mult_transpose1xW_width
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126 */
127 // Create kernel
Gian Marco36a0a462018-01-12 10:21:40 +0000128 std::string kernel_name = "gemm_transpose1xW";
129 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000130}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131
Gian Marco36a0a462018-01-12 10:21:40 +0000132Status CLGEMMTranspose1xWKernel::validate(const ITensorInfo *input, const ITensorInfo *output, int mult_transpose1xW_width)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000133{
134 unsigned int num_elems_processed_per_iteration = 1;
Gian Marco36a0a462018-01-12 10:21:40 +0000135 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mult_transpose1xW_width));
136 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), num_elems_processed_per_iteration, mult_transpose1xW_width).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137
Georgios Pinitas358ca202017-12-07 16:47:52 +0000138 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139}
140
141void CLGEMMTranspose1xWKernel::run(const Window &window, cl::CommandQueue &queue)
142{
143 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
144 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
145
146 // Output is transposed
147 Window out_window(window);
148 out_window.set(Window::DimX, window.y());
149 out_window.set(Window::DimY, window.x());
150
Gian Marcoae2af742018-02-15 12:35:44 +0000151 Window in_slice = window.first_slice_window_3D();
152 Window out_slice = out_window.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153
154 do
155 {
156 unsigned int idx = 0;
Gian Marcoae2af742018-02-15 12:35:44 +0000157 add_3D_tensor_argument(idx, _input, in_slice);
158 add_3D_tensor_argument(idx, _output, out_slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100159 enqueue(queue, *this, in_slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 }
Gian Marcoae2af742018-02-15 12:35:44 +0000161 while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_3D(out_slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162}