blob: 9fa253a55a943e5238da56d7b08b70acbd555e06 [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Manuel Bottini959c26d2019-12-02 16:22:35 +00002 * Copyright (c) 2017-2020 ARM Limited.
Gian Marco05288a22017-11-21 10:57:50 +00003 *
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/CLGEMMLowpReductionKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
Gian Marco Iodice4b908652018-10-18 10:21:02 +010027#include "arm_compute/core/CL/CLHelpers.h"
Gian Marco05288a22017-11-21 10:57:50 +000028#include "arm_compute/core/CL/ICLTensor.h"
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010029#include "arm_compute/core/KernelDescriptors.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000030#include "support/StringSupport.h"
Gian Marco05288a22017-11-21 10:57:50 +000031
Gian Marco05288a22017-11-21 10:57:50 +000032namespace arm_compute
33{
Georgios Pinitas358ca202017-12-07 16:47:52 +000034namespace
35{
36Status validate_arguments_matrix_a_reduction(const ITensorInfo *input, const ITensorInfo *output)
37{
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010038 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010039 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8);
Georgios Pinitas358ca202017-12-07 16:47:52 +000040
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010041 if(output->total_size() > 0)
42 {
43 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
44 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(0) != input->dimension(1), "Output vector must have length equal to the number of rows of the input matrix");
45 }
Georgios Pinitas358ca202017-12-07 16:47:52 +000046 return Status{};
47}
Georgios Pinitas358ca202017-12-07 16:47:52 +000048
49Status validate_arguments_matrix_b_reduction(const ITensorInfo *input, const ITensorInfo *output)
50{
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010051 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010052 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8);
Georgios Pinitas358ca202017-12-07 16:47:52 +000053
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010054 if(output->total_size() > 0)
55 {
56 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
57 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->dimension(0) != input->dimension(0), "Output vector must have length equal to the number of columns of the input matrix");
58 }
Georgios Pinitas358ca202017-12-07 16:47:52 +000059 return Status{};
60}
61
62std::pair<Status, Window> validate_and_configure_window_matrix_b_reduction(ITensorInfo *input, ITensorInfo *output)
63{
64 constexpr unsigned int num_elems_processed_per_iteration = 16;
65
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +010066 // Output auto initialization if not yet initialized
67 auto_init_if_empty(*output, TensorShape(input->dimension(0)), 1, DataType::S32);
68
Georgios Pinitas358ca202017-12-07 16:47:52 +000069 // Configure kernel window
70 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
71
72 AccessWindowStatic input_access(input, 0, 0, ceil_to_multiple(input->dimension(0), num_elems_processed_per_iteration), input->dimension(1));
73 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
74
75 bool window_changed = update_window_and_padding(win, input_access, output_access);
76
77 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
78
79 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
80 return std::make_pair(err, win);
81}
82} // namespace
83
Gian Marco05288a22017-11-21 10:57:50 +000084ICLGEMMLowpReductionKernel::ICLGEMMLowpReductionKernel()
85 : _input(), _output()
86{
87}
88
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010089void CLGEMMLowpMatrixAReductionKernel::configure(const ICLTensor *mtx_a, ICLTensor *vector_sum_row, const GEMMLowpReductionKernelInfo &info)
Gian Marco05288a22017-11-21 10:57:50 +000090{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010091 configure(CLKernelLibrary::get().get_compile_context(), mtx_a, vector_sum_row, info);
92}
93
94void CLGEMMLowpMatrixAReductionKernel::configure(CLCompileContext &compile_context, const ICLTensor *mtx_a, ICLTensor *vector_sum_row, const GEMMLowpReductionKernelInfo &info)
95{
Georgios Pinitas358ca202017-12-07 16:47:52 +000096 // Perform validate step
97 ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_a, vector_sum_row);
98 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_a_reduction(mtx_a->info(), vector_sum_row->info()));
Gian Marco05288a22017-11-21 10:57:50 +000099
Michele Di Giorgio1c1b3aa2020-04-02 17:35:42 +0100100 // Output auto initialization if not yet initialized
101 auto_init_if_empty(*vector_sum_row->info(), TensorShape(mtx_a->info()->dimension(1)), 1, DataType::S32);
102
Gian Marco05288a22017-11-21 10:57:50 +0000103 _input = mtx_a;
104 _output = vector_sum_row;
105
106 // Set the arguments to pass at compile time
107 CLBuildOptions build_opts;
108 build_opts.add_option("-DCOLS_A=" + support::cpp11::to_string(mtx_a->info()->dimension(0)));
Manuel Bottini959c26d2019-12-02 16:22:35 +0000109 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(mtx_a->info()->data_type()));
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +0000110 build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_dot8_acc_type_from_data_type(mtx_a->info()->data_type()));
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100111 build_opts.add_option_if(info.mul_by_scalar, "-DSCALAR=" + support::cpp11::to_string(info.scalar));
Gian Marco05288a22017-11-21 10:57:50 +0000112
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100113 const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device());
114
115 std::string kernel_name = "gemmlowp_matrix_a_reduction" + std::string(is_dot8_supported ? "_dot8" : "");
116
Gian Marco05288a22017-11-21 10:57:50 +0000117 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100118 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Gian Marco05288a22017-11-21 10:57:50 +0000119
Gian Marco05288a22017-11-21 10:57:50 +0000120 // Configure kernel window
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100121 // This kernel does not need padding
122 Window win = calculate_max_window(*vector_sum_row->info(), Steps());
123 ICLKernel::configure_internal(win);
Gian Marco Iodice2b52add2019-06-13 15:58:32 +0100124
125 _config_id = kernel_name;
126 _config_id += "_";
127 _config_id += support::cpp11::to_string(_input->info()->dimension(0));
128 _config_id += "_";
129 _config_id += support::cpp11::to_string(_input->info()->dimension(1));
130 _config_id += "_";
131 _config_id += support::cpp11::to_string(_input->info()->dimension(2));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000132}
Gian Marco05288a22017-11-21 10:57:50 +0000133
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100134Status CLGEMMLowpMatrixAReductionKernel::validate(const ITensorInfo *mtx_a, const ITensorInfo *vector_sum_row, const GEMMLowpReductionKernelInfo &info)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000135{
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100136 ARM_COMPUTE_UNUSED(info);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000137 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_a_reduction(mtx_a, vector_sum_row));
Gian Marco05288a22017-11-21 10:57:50 +0000138
Georgios Pinitas358ca202017-12-07 16:47:52 +0000139 return Status{};
Gian Marco05288a22017-11-21 10:57:50 +0000140}
141
142void CLGEMMLowpMatrixAReductionKernel::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 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimY);
148 Window slice_in = collapsed.first_slice_window_2D();
149 Window slice_out = collapsed.first_slice_window_2D();
150
151 // Setup input slice. Its dimensions are increased in the cl kernel.
152 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
153 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
154 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
155
156 do
157 {
158 unsigned int idx = 0;
159 add_3D_tensor_argument(idx, _input, slice_in);
160 add_2D_tensor_argument(idx, _output, slice_out);
Gian Marco Iodice2b52add2019-06-13 15:58:32 +0100161 enqueue(queue, *this, slice_out, lws_hint());
Gian Marco05288a22017-11-21 10:57:50 +0000162 }
163 while(collapsed.slide_window_slice_2D(slice_out));
164}
165
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100166void CLGEMMLowpMatrixBReductionKernel::configure(const ICLTensor *mtx_b, ICLTensor *vector_sum_col, const GEMMLowpReductionKernelInfo &info)
Gian Marco05288a22017-11-21 10:57:50 +0000167{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100168 configure(CLKernelLibrary::get().get_compile_context(), mtx_b, vector_sum_col, info);
169}
170
171void CLGEMMLowpMatrixBReductionKernel::configure(CLCompileContext &compile_context, const ICLTensor *mtx_b, ICLTensor *vector_sum_col, const GEMMLowpReductionKernelInfo &info)
172{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000173 ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_b, vector_sum_col);
174 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_b_reduction(mtx_b->info(), vector_sum_col->info()));
Gian Marco05288a22017-11-21 10:57:50 +0000175
176 _input = mtx_b;
177 _output = vector_sum_col;
178
179 // Set the arguments to pass at compile time
180 CLBuildOptions build_opts;
181 build_opts.add_option("-DCOLS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(0)));
182 build_opts.add_option("-DROWS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(1)));
Manuel Bottini959c26d2019-12-02 16:22:35 +0000183 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(mtx_b->info()->data_type()));
Michele Di Giorgioe7b333e2020-01-15 10:30:51 +0000184 build_opts.add_option("-DACC_DATA_TYPE=" + get_cl_dot8_acc_type_from_data_type(mtx_b->info()->data_type()));
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100185 build_opts.add_option_if(info.mul_by_scalar, "-DSCALAR=" + support::cpp11::to_string(info.scalar));
Gian Marco05288a22017-11-21 10:57:50 +0000186
187 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100188 _kernel = create_kernel(compile_context, "gemmlowp_matrix_b_reduction", build_opts.options());
Gian Marco05288a22017-11-21 10:57:50 +0000189
Gian Marco05288a22017-11-21 10:57:50 +0000190 // Configure kernel window
Georgios Pinitas358ca202017-12-07 16:47:52 +0000191 auto win_config = validate_and_configure_window_matrix_b_reduction(_input->info(), _output->info());
192 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100193 ICLKernel::configure_internal(win_config.second);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000194}
Gian Marco05288a22017-11-21 10:57:50 +0000195
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100196Status CLGEMMLowpMatrixBReductionKernel::validate(const ITensorInfo *mtx_b, const ITensorInfo *vector_sum_col, const GEMMLowpReductionKernelInfo &info)
Georgios Pinitas358ca202017-12-07 16:47:52 +0000197{
Michele Di Giorgiof64d3362020-04-03 12:40:10 +0100198 ARM_COMPUTE_UNUSED(info);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000199 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_b_reduction(mtx_b, vector_sum_col));
200 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_matrix_b_reduction(mtx_b->clone().get(), vector_sum_col->clone().get()).first);
Gian Marco05288a22017-11-21 10:57:50 +0000201
Georgios Pinitas358ca202017-12-07 16:47:52 +0000202 return Status{};
Gian Marco05288a22017-11-21 10:57:50 +0000203}
204
205void CLGEMMLowpMatrixBReductionKernel::run(const Window &window, cl::CommandQueue &queue)
206{
207 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
208 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
209
210 Window collapsed = window.collapse_if_possible(IKernel::window(), Window::DimY);
211
212 Window slice_out = collapsed.first_slice_window_2D();
213 Window slice_in = slice_out;
214
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100215 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
216 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
Gian Marco05288a22017-11-21 10:57:50 +0000217
218 do
219 {
220 unsigned int idx = 0;
221 add_3D_tensor_argument(idx, _input, slice_in);
222 add_2D_tensor_argument(idx, _output, slice_out);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100223 enqueue(queue, *this, slice_out, lws_hint());
Gian Marco05288a22017-11-21 10:57:50 +0000224 }
225 while(collapsed.slide_window_slice_2D(slice_out));
226}
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +0100227} // namespace arm_compute