blob: 5b299052d4a362833d3eb58b14f20845b15dcc3f [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/AccessWindowTranspose.h"
28#include "arm_compute/core/CL/CLHelpers.h"
29#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010030#include "arm_compute/core/CL/CLValidate.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/CL/ICLTensor.h"
32#include "arm_compute/core/CL/OpenCL.h"
33#include "arm_compute/core/Error.h"
34#include "arm_compute/core/Helpers.h"
35#include "arm_compute/core/Types.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include "arm_compute/core/Window.h"
Georgios Pinitas358ca202017-12-07 16:47:52 +000037#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
39#include <cmath>
40
41using namespace arm_compute;
Georgios Pinitas358ca202017-12-07 16:47:52 +000042using namespace arm_compute::misc::shape_calculator;
43
44namespace
45{
Gian Marco36a0a462018-01-12 10:21:40 +000046Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int mult_transpose1xW_width)
Georgios Pinitas358ca202017-12-07 16:47:52 +000047{
Gian Marco36a0a462018-01-12 10:21:40 +000048 ARM_COMPUTE_RETURN_ERROR_ON(mult_transpose1xW_width < 1);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010049 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010050 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::U8, DataType::S8,
51 DataType::U16, DataType::S16, DataType::U32, DataType::S32,
Georgios Pinitas358ca202017-12-07 16:47:52 +000052 DataType::F16, DataType::F32);
53
54 if(output->total_size() != 0)
55 {
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(),
Gian Marco36a0a462018-01-12 10:21:40 +000057 compute_transpose1xW_with_element_size_shape(*input, mult_transpose1xW_width));
Georgios Pinitas358ca202017-12-07 16:47:52 +000058 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitas358ca202017-12-07 16:47:52 +000059 }
60
61 return Status{};
62}
63
Gian Marco36a0a462018-01-12 10:21:40 +000064std::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 +000065{
66 num_elems_processed_per_iteration = 16 / input->element_size();
67
Gian Marco36a0a462018-01-12 10:21:40 +000068 const int scale_x = num_elems_processed_per_iteration * mult_transpose1xW_width;
Georgios Pinitas358ca202017-12-07 16:47:52 +000069 bool window_changed = false;
70
71 // Configure kernel window
72 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
73
Georgios Pinitas358ca202017-12-07 16:47:52 +000074 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
Georgios Pinitas358ca202017-12-07 16:47:52 +000075
Gian Marco Iodice750641d2018-05-08 12:01:57 +010076 // Output tensor auto inizialitation if not yet initialized
77 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*input, mult_transpose1xW_width)));
78
Georgios Pinitas358ca202017-12-07 16:47:52 +000079 // Configure window in case of configured output
Gian Marco Iodice750641d2018-05-08 12:01:57 +010080 AccessWindowStatic output_access(output, 0, 0, ceil_to_multiple(output->dimension(0), scale_x), output->dimension(1));
81 window_changed = window_changed || update_window_and_padding(win, input_access, output_access);
82 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), input->tensor_shape()));
Georgios Pinitas358ca202017-12-07 16:47:52 +000083
Gian Marcoae2af742018-02-15 12:35:44 +000084 // Collapse along the Z direction
85 Window collapsed = win.collapse(win, Window::DimZ);
86
Georgios Pinitas358ca202017-12-07 16:47:52 +000087 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Gian Marcoae2af742018-02-15 12:35:44 +000088 return std::make_pair(err, collapsed);
Georgios Pinitas358ca202017-12-07 16:47:52 +000089}
90} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091
Gian Marco36a0a462018-01-12 10:21:40 +000092void CLGEMMTranspose1xWKernel::configure(const ICLTensor *input, ICLTensor *output, int mult_transpose1xW_width)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093{
Georgios Pinitas358ca202017-12-07 16:47:52 +000094 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095
96 // Output tensor auto inizialitation if not yet initialized
Gian Marco36a0a462018-01-12 10:21:40 +000097 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 +010098
Georgios Pinitas358ca202017-12-07 16:47:52 +000099 // Perform validate step
Gian Marco36a0a462018-01-12 10:21:40 +0000100 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mult_transpose1xW_width));
Georgios Pinitas4cbee6e2017-06-19 13:02:56 +0100101
102 _input = input;
103 _output = output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104
Georgios Pinitas358ca202017-12-07 16:47:52 +0000105 // Configure kernel window
Gian Marco36a0a462018-01-12 10:21:40 +0000106 // Note: num_elems_processed_per_iteration will be set in validate_and_configure_window()
Georgios Pinitas358ca202017-12-07 16:47:52 +0000107 unsigned int num_elems_processed_per_iteration = 1;
Gian Marco36a0a462018-01-12 10:21:40 +0000108 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 +0000109 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100110 ICLKernel::configure_internal(win_config.second);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000111
Gian Marco36a0a462018-01-12 10:21:40 +0000112 // Create build options
113 CLBuildOptions build_opts;
Gian Marco19835e52018-01-30 13:35:54 +0000114 build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->info()->element_size()));
Gian Marco36a0a462018-01-12 10:21:40 +0000115 build_opts.add_option("-DTRANSPOSE_W=" + support::cpp11::to_string(num_elems_processed_per_iteration));
116 build_opts.add_option("-DMULT_TRANSPOSE1XW_WIDTH=" + support::cpp11::to_string(mult_transpose1xW_width));
117
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118 /*
119 * Following an example of how the transposition1xW works when the input data type is F32
120 *
121 * |a00 a01 a02 a03|
122 * |a10 a11 a12 a13|
123 * |a20 a21 a22 a23| = | a00 a01 a02 a03 || a10 a11 a12 a13 || a20 a21 a22 a23 || a30 a31 a32 a33 |
124 * |a30 a31 a32 a33|
125 *
Gian Marco36a0a462018-01-12 10:21:40 +0000126 * 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 +0100127 */
128 // Create kernel
Gian Marco36a0a462018-01-12 10:21:40 +0000129 std::string kernel_name = "gemm_transpose1xW";
130 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000131}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132
Gian Marco36a0a462018-01-12 10:21:40 +0000133Status CLGEMMTranspose1xWKernel::validate(const ITensorInfo *input, const ITensorInfo *output, int mult_transpose1xW_width)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000134{
135 unsigned int num_elems_processed_per_iteration = 1;
Gian Marco36a0a462018-01-12 10:21:40 +0000136 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mult_transpose1xW_width));
137 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 +0100138
Georgios Pinitas358ca202017-12-07 16:47:52 +0000139 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140}
141
142void CLGEMMTranspose1xWKernel::run(const Window &window, cl::CommandQueue &queue)
143{
144 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
145 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
146
147 // Output is transposed
148 Window out_window(window);
149 out_window.set(Window::DimX, window.y());
150 out_window.set(Window::DimY, window.x());
151
Gian Marcoae2af742018-02-15 12:35:44 +0000152 Window in_slice = window.first_slice_window_3D();
153 Window out_slice = out_window.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154
155 do
156 {
157 unsigned int idx = 0;
Gian Marcoae2af742018-02-15 12:35:44 +0000158 add_3D_tensor_argument(idx, _input, in_slice);
159 add_3D_tensor_argument(idx, _output, out_slice);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100160 enqueue(queue, *this, in_slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 }
Gian Marcoae2af742018-02-15 12:35:44 +0000162 while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_3D(out_slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163}