blob: 28be710384524013f4e93c45725c0e95333033d4 [file] [log] [blame]
Anthony Barbier7068f992017-10-26 15:23:08 +01001/*
Matthew Bentham758b5ba2020-03-05 23:37:48 +00002 * Copyright (c) 2017-2020 ARM Limited.
Anthony Barbier7068f992017-10-26 15:23:08 +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/GLES_COMPUTE/kernels/GCGEMMInterleave4x4Kernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/GLES_COMPUTE/GCHelpers.h"
28#include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h"
29#include "arm_compute/core/GLES_COMPUTE/IGCTensor.h"
30#include "arm_compute/core/GLES_COMPUTE/OpenGLES.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"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000036#include "support/StringSupport.h"
Anthony Barbier7068f992017-10-26 15:23:08 +010037
38using namespace arm_compute;
39
40GCGEMMInterleave4x4Kernel::GCGEMMInterleave4x4Kernel()
41 : _input(nullptr), _output(nullptr)
42{
43}
44
45void GCGEMMInterleave4x4Kernel::configure(const IGCTensor *input, IGCTensor *output)
46{
Stephen Lie855c232018-01-04 14:13:22 +080047 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Anthony Barbier7068f992017-10-26 15:23:08 +010048 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
49
50 TensorShape output_shape = input->info()->tensor_shape();
51 output_shape.set(0, input->info()->dimension(0) * 4);
52 output_shape.set(1, std::ceil(input->info()->dimension(1) / 4.0f));
53
54 // Output auto inizialitation if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010055 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
Anthony Barbier7068f992017-10-26 15:23:08 +010056
57 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
58 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
59
60 _input = input;
61 _output = output;
62
63 std::set<std::string> build_opts;
64 std::string dt_name = (input->info()->data_type() == DataType::F32) ? "DATA_TYPE_FP32" : "DATA_TYPE_FP16";
65 build_opts.emplace(("#define " + dt_name));
66 build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1));
67 build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1));
68 build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1));
69
70 // Create kernel
71 build_opts.emplace("#define GEMM_INTERLEAVE4x4");
72 _kernel = static_cast<GCKernel>(GCKernelLibrary::get().create_kernel("gemm_interleave4x4", build_opts));
73
74 // Configure kernel window
75 const unsigned int num_elems_processed_per_iteration_x = max_gc_vector_width / data_size_from_type(input->info()->data_type());
76 constexpr unsigned int num_elems_processed_per_iteration_y = 4;
77 const unsigned int num_elems_written_per_iteration = num_elems_processed_per_iteration_x * num_elems_processed_per_iteration_y;
78
79 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
80
81 AccessWindowRectangle input_access(input->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
82 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_written_per_iteration, 1, 4.f, 0.25f);
83
84 update_window_and_padding(win, input_access, output_access);
85
Gian Marco Iodice1e6c9152019-08-20 11:27:20 +010086 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbier7068f992017-10-26 15:23:08 +010087
Anthony Barbier7068f992017-10-26 15:23:08 +010088 IGCKernel::configure(win);
89}
90
91void GCGEMMInterleave4x4Kernel::run(const Window &window)
92{
93 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
94 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window);
95
96 _kernel.use();
97
98 /*
99 * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
100 * |a00 a01 a02 a03|
101 * |a10 a11 a12 a13|
102 * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
103 * |a30 a31 a32 a33|
104 *
105 * After this operation, the output matrix will have the following shape: [ height * 4, width / 4 ]
106 */
107 Window in_slice = window.first_slice_window_2D();
108 Window out_slice = window.first_slice_window_2D();
109
110 // Change x and y steps for the slide of output tensor
111 out_slice.scale(Window::DimX, 4.f);
112 out_slice.scale(Window::DimY, 0.25f);
113
114 do
115 {
116 unsigned int idx = 0;
117 add_2D_tensor_argument(idx, _input, 1, in_slice);
118 add_2D_tensor_argument(idx, _output, 2, out_slice);
119
120 _kernel.update_shader_params();
121
122 enqueue(*this, in_slice);
123 }
124 while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice));
125}