blob: 2d6b83a97d3cc89f7bbc14e6fe18ff57191487ca [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/CLGEMMMatrixMultiplyKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/AccessWindowTranspose.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/CL/CLHelpers.h"
29#include "arm_compute/core/CL/CLKernelLibrary.h"
30#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/CL/OpenCL.h"
32#include "arm_compute/core/Error.h"
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +010033#include "arm_compute/core/FixedPoint.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/Helpers.h"
35#include "arm_compute/core/Types.h"
36#include "arm_compute/core/Utils.h"
37#include "arm_compute/core/Validate.h"
38#include "arm_compute/core/Window.h"
39
40#include <set>
41#include <sstream>
42#include <string>
43
44using namespace arm_compute;
45
46CLGEMMMatrixMultiplyKernel::CLGEMMMatrixMultiplyKernel()
47 : _input0(nullptr), _input1(nullptr), _output(nullptr)
48{
49}
50
51void CLGEMMMatrixMultiplyKernel::configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output, float alpha)
52{
Gian Marco Iodice8a383692017-07-03 17:41:47 +010053 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1, output);
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +010055 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input0, input1, output);
56
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057 if(output->info()->dimension(1) == 1)
58 {
59 ARM_COMPUTE_ERROR_ON(input0->info()->dimension(0) != input1->info()->dimension(1));
60 }
61
62 _input0 = input0;
63 _input1 = input1;
64 _output = output;
65
66 if(output->info()->dimension(1) == 196)
67 {
68 _lws_hint = cl::NDRange(1, 7);
69 }
70 else
71 {
72 _lws_hint = cl::NDRange(8, 8);
73 }
74
75 std::ostringstream mm_arguments;
76 mm_arguments << "-DWIDTH_MATRIX_B=" << input1->info()->dimension(0) << " ";
Gian Marco Iodice8a383692017-07-03 17:41:47 +010077 if(is_data_type_fixed_point(input0->info()->data_type()))
78 {
79 mm_arguments << "-DALPHA=" << (input0->info()->data_type() == DataType::QS8 ?
80 scvt_qs8_f32(alpha, input0->info()->fixed_point_position()) :
81 scvt_qs16_f32(alpha, input0->info()->fixed_point_position()))
82 << " ";
83 mm_arguments << "-DFIXED_POINT_POSITION=" << input0->info()->fixed_point_position() << " ";
84 }
85 else
86 {
87 mm_arguments << "-DALPHA=" << alpha << " ";
88 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 std::set<std::string> build_opts;
90
91 // Check if the output tensor is a vector. If so,the kernel runs the vector-matrix multiplication
92 if(output->info()->dimension(1) == 1)
93 {
94 mm_arguments << "-DWIDTH_VECTOR_A=" << input0->info()->dimension(0) << " ";
95 build_opts.emplace(mm_arguments.str());
96
97 // Create kernel
98 std::string data_type_name = lower_string(string_from_data_type(input0->info()->data_type()));
99 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(("gemm_vm_" + data_type_name), build_opts));
100
101 // Configure window kernel
102 const unsigned int num_elems_processed_per_iteration_x = max_cl_vector_width / data_size_from_type(input0->info()->data_type());
103
104 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration_x));
105
106 AccessWindowRectangle input0_access(input0->info(), 0, 0, num_elems_processed_per_iteration_x, 1);
107 AccessWindowRectangle input1_access(input1->info(), 0, 0, num_elems_processed_per_iteration_x, 1);
108 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_processed_per_iteration_x, 1);
109
110 update_window_and_padding(win, input0_access, input1_access, output_access);
111
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +0100112 Coordinates coord;
113 coord.set_num_dimensions(output->info()->num_dimensions());
114 output_access.set_valid_region(win, ValidRegion(coord, output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115
116 ICLKernel::configure(win);
117 }
118 else
119 {
120 build_opts.emplace(mm_arguments.str());
121
122 // Create kernel
123 std::string data_type_name = lower_string(string_from_data_type(input0->info()->data_type()));
124
125 if(data_type_name == "f32")
126 {
127 GPUTarget arch_target = get_arch_from_target(get_target());
128 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemm_mm_f32_" + string_from_target(arch_target), build_opts));
129 }
130 else
131 {
132 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemm_mm_" + data_type_name, build_opts));
133 }
134
135 // Configure window kernel
136 const unsigned int num_elems_processed_per_iteration_x = max_cl_vector_width / data_size_from_type(input0->info()->data_type());
137 constexpr unsigned int num_elems_processed_per_iteration_y = 4;
138
139 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
140
141 AccessWindowRectangle input0_access(input0->info(), 0, 0, num_elems_processed_per_iteration_y, 1, 1.f, 0.25f);
142 AccessWindowTranspose input1_access(input1->info(), 0, 0, num_elems_processed_per_iteration_x, 1, 0.f, 0.25f);
143 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
144
145 update_window_and_padding(win, input0_access, input1_access, output_access);
146
147 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->info()->tensor_shape()));
148
149 ICLKernel::configure(win);
150 }
151}
152
153void CLGEMMMatrixMultiplyKernel::run(const Window &window, cl::CommandQueue &queue)
154{
155 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
156 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
157
158 Window slice = window.first_slice_window_2D();
159 Window slice_matrix_b = slice;
160 slice_matrix_b.set(Window::DimX, Window::Dimension(0, _input1->info()->dimension(0), 1));
161 slice_matrix_b.set(Window::DimY, Window::Dimension(0, _input1->info()->dimension(1), 1));
162 slice_matrix_b.set(Window::DimZ, Window::Dimension(0, 1, 1));
163
164 do
165 {
166 Window slice_b = slice;
167 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
168 // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
169 if(_input1->info()->num_dimensions() < 3)
170 {
171 slice_b = slice_matrix_b;
172 }
173
174 unsigned int idx = 0;
175 add_2D_tensor_argument(idx, _input0, slice);
176 add_2D_tensor_argument(idx, _input1, slice_b);
177 add_2D_tensor_argument(idx, _output, slice);
178 enqueue(queue, *this, slice, _lws_hint);
179 }
180 while(window.slide_window_slice_2D(slice));
181}