blob: 7a8a1e529d984fceac59da7200f375dbf8f8b475 [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);
Georgios Pinitas358ca202017-12-07 16:47:52 +000050 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::U8, DataType::S8,
51 DataType::QS16, DataType::U16, DataType::S16, DataType::U32, DataType::S32,
52 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);
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
60 }
61
62 return Status{};
63}
64
Gian Marco36a0a462018-01-12 10:21:40 +000065std::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 +000066{
67 num_elems_processed_per_iteration = 16 / input->element_size();
68
Gian Marco36a0a462018-01-12 10:21:40 +000069 const int scale_x = num_elems_processed_per_iteration * mult_transpose1xW_width;
Georgios Pinitas358ca202017-12-07 16:47:52 +000070 bool window_changed = false;
71
72 // Configure kernel window
73 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
74
Georgios Pinitas358ca202017-12-07 16:47:52 +000075 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
Georgios Pinitas358ca202017-12-07 16:47:52 +000076
Gian Marco Iodice750641d2018-05-08 12:01:57 +010077 // Output tensor auto inizialitation if not yet initialized
78 auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_transpose1xW_with_element_size_shape(*input, mult_transpose1xW_width)));
79
Georgios Pinitas358ca202017-12-07 16:47:52 +000080 // Configure window in case of configured output
Gian Marco Iodice750641d2018-05-08 12:01:57 +010081 AccessWindowStatic output_access(output, 0, 0, ceil_to_multiple(output->dimension(0), scale_x), output->dimension(1));
82 window_changed = window_changed || update_window_and_padding(win, input_access, output_access);
83 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), input->tensor_shape()));
Georgios Pinitas358ca202017-12-07 16:47:52 +000084
Gian Marcoae2af742018-02-15 12:35:44 +000085 // Collapse along the Z direction
86 Window collapsed = win.collapse(win, Window::DimZ);
87
Georgios Pinitas358ca202017-12-07 16:47:52 +000088 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Gian Marcoae2af742018-02-15 12:35:44 +000089 return std::make_pair(err, collapsed);
Georgios Pinitas358ca202017-12-07 16:47:52 +000090}
91} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092
Gian Marco36a0a462018-01-12 10:21:40 +000093void CLGEMMTranspose1xWKernel::configure(const ICLTensor *input, ICLTensor *output, int mult_transpose1xW_width)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094{
Georgios Pinitas358ca202017-12-07 16:47:52 +000095 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096
97 // Output tensor auto inizialitation if not yet initialized
Gian Marco36a0a462018-01-12 10:21:40 +000098 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 +010099
Georgios Pinitas358ca202017-12-07 16:47:52 +0000100 // Perform validate step
Gian Marco36a0a462018-01-12 10:21:40 +0000101 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), mult_transpose1xW_width));
Georgios Pinitas4cbee6e2017-06-19 13:02:56 +0100102
103 _input = input;
104 _output = output;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
Georgios Pinitas358ca202017-12-07 16:47:52 +0000106 // Configure kernel window
Gian Marco36a0a462018-01-12 10:21:40 +0000107 // Note: num_elems_processed_per_iteration will be set in validate_and_configure_window()
Georgios Pinitas358ca202017-12-07 16:47:52 +0000108 unsigned int num_elems_processed_per_iteration = 1;
Gian Marco36a0a462018-01-12 10:21:40 +0000109 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 +0000110 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
111 ICLKernel::configure(win_config.second);
112
Gian Marco36a0a462018-01-12 10:21:40 +0000113 // Create build options
114 CLBuildOptions build_opts;
Gian Marco19835e52018-01-30 13:35:54 +0000115 build_opts.add_option("-DELEMENT_SIZE=" + support::cpp11::to_string(input->info()->element_size()));
Gian Marco36a0a462018-01-12 10:21:40 +0000116 build_opts.add_option("-DTRANSPOSE_W=" + support::cpp11::to_string(num_elems_processed_per_iteration));
117 build_opts.add_option("-DMULT_TRANSPOSE1XW_WIDTH=" + support::cpp11::to_string(mult_transpose1xW_width));
118
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 /*
120 * Following an example of how the transposition1xW works when the input data type is F32
121 *
122 * |a00 a01 a02 a03|
123 * |a10 a11 a12 a13|
124 * |a20 a21 a22 a23| = | a00 a01 a02 a03 || a10 a11 a12 a13 || a20 a21 a22 a23 || a30 a31 a32 a33 |
125 * |a30 a31 a32 a33|
126 *
Gian Marco36a0a462018-01-12 10:21:40 +0000127 * 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 +0100128 */
129 // Create kernel
Gian Marco36a0a462018-01-12 10:21:40 +0000130 std::string kernel_name = "gemm_transpose1xW";
131 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000132}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133
Gian Marco36a0a462018-01-12 10:21:40 +0000134Status CLGEMMTranspose1xWKernel::validate(const ITensorInfo *input, const ITensorInfo *output, int mult_transpose1xW_width)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000135{
136 unsigned int num_elems_processed_per_iteration = 1;
Gian Marco36a0a462018-01-12 10:21:40 +0000137 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, mult_transpose1xW_width));
138 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 +0100139
Georgios Pinitas358ca202017-12-07 16:47:52 +0000140 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100141}
142
143void CLGEMMTranspose1xWKernel::run(const Window &window, cl::CommandQueue &queue)
144{
145 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
146 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
147
148 // Output is transposed
149 Window out_window(window);
150 out_window.set(Window::DimX, window.y());
151 out_window.set(Window::DimY, window.x());
152
Gian Marcoae2af742018-02-15 12:35:44 +0000153 Window in_slice = window.first_slice_window_3D();
154 Window out_slice = out_window.first_slice_window_3D();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155
156 do
157 {
158 unsigned int idx = 0;
Gian Marcoae2af742018-02-15 12:35:44 +0000159 add_3D_tensor_argument(idx, _input, in_slice);
160 add_3D_tensor_argument(idx, _output, out_slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 enqueue(queue, *this, in_slice, _lws_hint);
162 }
Gian Marcoae2af742018-02-15 12:35:44 +0000163 while(window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_3D(out_slice));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164}