blob: 984121c5bc10b09d231b7bd829a1fbac3618fce9 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
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 */
24#include "arm_compute/core/CL/kernels/CLGEMMInterleave4x4Kernel.h"
25
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"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/Types.h"
33#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
36
37using namespace arm_compute;
38
39CLGEMMInterleave4x4Kernel::CLGEMMInterleave4x4Kernel()
40 : _input(nullptr), _output(nullptr)
41{
42}
43
44void CLGEMMInterleave4x4Kernel::configure(const ICLTensor *input, ICLTensor *output)
45{
Gian Marcoc8279902017-11-27 16:35:28 +000046 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QS8, DataType::QASYMM8, DataType::U16, DataType::S16, DataType::QS16, DataType::U32, DataType::S32,
47 DataType::F16,
Gian Marco Iodice8a383692017-07-03 17:41:47 +010048 DataType::F32);
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +010049 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
50
51 TensorShape output_shape = input->info()->tensor_shape();
52 output_shape.set(0, input->info()->dimension(0) * 4);
53 output_shape.set(1, std::ceil(input->info()->dimension(1) / 4.0f));
54
55 // Output auto inizialitation if not yet initialized
Gian Marcoc8279902017-11-27 16:35:28 +000056 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position(), input->info()->quantization_info());
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +010057
58 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Gian Marco Iodice9f89bae2017-06-22 12:09:49 +010060 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061
62 _input = input;
63 _output = output;
64
65 // Create kernel
66 std::string data_type_name;
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +010067 data_type_name = support::cpp11::to_string(input->info()->element_size() * 8) + "bit";
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemm_interleave4x4_" + data_type_name));
69
70 // Configure kernel window
71 const unsigned int num_elems_processed_per_iteration_x = max_cl_vector_width / data_size_from_type(input->info()->data_type());
72 constexpr unsigned int num_elems_processed_per_iteration_y = 4;
73 const unsigned int num_elems_written_per_iteration = num_elems_processed_per_iteration_x * num_elems_processed_per_iteration_y;
74
75 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
76
77 AccessWindowRectangle input_access(input->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
78 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration, 1, 4.f, 0.25f);
79
80 update_window_and_padding(win, input_access, output_access);
81
82 output_access.set_valid_region(win, input->info()->valid_region());
83
84 ICLKernel::configure(win);
Gian Marcode691f02017-09-08 16:13:11 +010085
86 // Set config_id for enabling LWS tuning
87 _config_id = "interleave4x4_";
88 _config_id += lower_string(string_from_data_type(input->info()->data_type()));
89 _config_id += "_";
90 _config_id += support::cpp11::to_string(output->info()->dimension(0));
91 _config_id += "_";
92 _config_id += support::cpp11::to_string(output->info()->dimension(1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093}
94
95void CLGEMMInterleave4x4Kernel::run(const Window &window, cl::CommandQueue &queue)
96{
97 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
98 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
99
100 /*
101 * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
102 * |a00 a01 a02 a03|
103 * |a10 a11 a12 a13|
104 * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
105 * |a30 a31 a32 a33|
106 *
107 * After this operation, the output matrix will have the following shape: [ height * 4, width / 4 ]
108 */
109 Window in_slice = window.first_slice_window_2D();
110 Window out_slice = window.first_slice_window_2D();
111
112 // Change x and y steps for the slide of output tensor
113 out_slice.scale(Window::DimX, 4.f);
114 out_slice.scale(Window::DimY, 0.25f);
115
116 do
117 {
118 unsigned int idx = 0;
119 add_2D_tensor_argument(idx, _input, in_slice);
120 add_2D_tensor_argument(idx, _output, out_slice);
Gian Marcod7779da2017-11-22 14:46:28 +0000121 enqueue(queue, *this, in_slice, _lws_hint);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122 }
123 while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
124}