blob: 0c0b0ec817e5880698f0bf397adae5028dfeeafd [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Gian Marco Iodice2b52add2019-06-13 15:58:32 +01002 * Copyright (c) 2017-2019 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"
29#include "arm_compute/core/Error.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.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#include "support/ToolchainSupport.h"
37
38#include <cstddef>
39#include <cstdint>
40
Gian Marco05288a22017-11-21 10:57:50 +000041namespace arm_compute
42{
43class Coordinates;
Gian Marco05288a22017-11-21 10:57:50 +000044
Georgios Pinitas358ca202017-12-07 16:47:52 +000045namespace
46{
47Status validate_arguments_matrix_a_reduction(const ITensorInfo *input, const ITensorInfo *output)
48{
49 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8);
50 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
51
52 return Status{};
53}
54std::pair<Status, Window> validate_and_configure_window_matrix_a_reduction(ITensorInfo *input, ITensorInfo *output)
55{
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010056 constexpr unsigned int num_elems_processed_per_iteration = 1;
Georgios Pinitas358ca202017-12-07 16:47:52 +000057
58 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
59
Gian Marco Iodice4b908652018-10-18 10:21:02 +010060 AccessWindowStatic input_access(input, 0, 0, input->dimension(0), input->dimension(1));
Georgios Pinitas358ca202017-12-07 16:47:52 +000061 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
62
63 bool window_changed = update_window_and_padding(win, input_access, output_access);
64
65 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
66
67 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
68 return std::make_pair(err, win);
69}
70
71Status validate_arguments_matrix_b_reduction(const ITensorInfo *input, const ITensorInfo *output)
72{
73 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8);
74 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
75
76 return Status{};
77}
78
79std::pair<Status, Window> validate_and_configure_window_matrix_b_reduction(ITensorInfo *input, ITensorInfo *output)
80{
81 constexpr unsigned int num_elems_processed_per_iteration = 16;
82
83 // Configure kernel window
84 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
85
86 AccessWindowStatic input_access(input, 0, 0, ceil_to_multiple(input->dimension(0), num_elems_processed_per_iteration), input->dimension(1));
87 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
88
89 bool window_changed = update_window_and_padding(win, input_access, output_access);
90
91 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
92
93 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
94 return std::make_pair(err, win);
95}
96} // namespace
97
Gian Marco05288a22017-11-21 10:57:50 +000098ICLGEMMLowpReductionKernel::ICLGEMMLowpReductionKernel()
99 : _input(), _output()
100{
101}
102
103void CLGEMMLowpMatrixAReductionKernel::configure(const ICLTensor *mtx_a, ICLTensor *vector_sum_row)
104{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000105 // Perform validate step
106 ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_a, vector_sum_row);
107 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_a_reduction(mtx_a->info(), vector_sum_row->info()));
Gian Marco05288a22017-11-21 10:57:50 +0000108
109 _input = mtx_a;
110 _output = vector_sum_row;
111
112 // Set the arguments to pass at compile time
113 CLBuildOptions build_opts;
114 build_opts.add_option("-DCOLS_A=" + support::cpp11::to_string(mtx_a->info()->dimension(0)));
115
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100116 const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device());
117
118 std::string kernel_name = "gemmlowp_matrix_a_reduction" + std::string(is_dot8_supported ? "_dot8" : "");
119
Gian Marco05288a22017-11-21 10:57:50 +0000120 // Create kernel
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100121 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Gian Marco05288a22017-11-21 10:57:50 +0000122
Gian Marco05288a22017-11-21 10:57:50 +0000123 // Configure kernel window
Georgios Pinitas358ca202017-12-07 16:47:52 +0000124 auto win_config = validate_and_configure_window_matrix_a_reduction(_input->info(), _output->info());
125 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100126 ICLKernel::configure_internal(win_config.second);
Gian Marco Iodice2b52add2019-06-13 15:58:32 +0100127
128 _config_id = kernel_name;
129 _config_id += "_";
130 _config_id += support::cpp11::to_string(_input->info()->dimension(0));
131 _config_id += "_";
132 _config_id += support::cpp11::to_string(_input->info()->dimension(1));
133 _config_id += "_";
134 _config_id += support::cpp11::to_string(_input->info()->dimension(2));
Georgios Pinitas358ca202017-12-07 16:47:52 +0000135}
Gian Marco05288a22017-11-21 10:57:50 +0000136
Georgios Pinitas358ca202017-12-07 16:47:52 +0000137Status CLGEMMLowpMatrixAReductionKernel::validate(const ITensorInfo *mtx_a, const ITensorInfo *vector_sum_row)
138{
139 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_a_reduction(mtx_a, vector_sum_row));
140 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_matrix_a_reduction(mtx_a->clone().get(), vector_sum_row->clone().get()).first);
Gian Marco05288a22017-11-21 10:57:50 +0000141
Georgios Pinitas358ca202017-12-07 16:47:52 +0000142 return Status{};
Gian Marco05288a22017-11-21 10:57:50 +0000143}
144
145void CLGEMMLowpMatrixAReductionKernel::run(const Window &window, cl::CommandQueue &queue)
146{
147 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
148 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
149
150 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimY);
151 Window slice_in = collapsed.first_slice_window_2D();
152 Window slice_out = collapsed.first_slice_window_2D();
153
154 // Setup input slice. Its dimensions are increased in the cl kernel.
155 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
156 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
157 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
158
159 do
160 {
161 unsigned int idx = 0;
162 add_3D_tensor_argument(idx, _input, slice_in);
163 add_2D_tensor_argument(idx, _output, slice_out);
Gian Marco Iodice2b52add2019-06-13 15:58:32 +0100164 enqueue(queue, *this, slice_out, lws_hint());
Gian Marco05288a22017-11-21 10:57:50 +0000165 }
166 while(collapsed.slide_window_slice_2D(slice_out));
167}
168
169void CLGEMMLowpMatrixBReductionKernel::configure(const ICLTensor *mtx_b, ICLTensor *vector_sum_col)
170{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000171 ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_b, vector_sum_col);
172 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_b_reduction(mtx_b->info(), vector_sum_col->info()));
Gian Marco05288a22017-11-21 10:57:50 +0000173
174 _input = mtx_b;
175 _output = vector_sum_col;
176
177 // Set the arguments to pass at compile time
178 CLBuildOptions build_opts;
179 build_opts.add_option("-DCOLS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(0)));
180 build_opts.add_option("-DROWS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(1)));
181
182 // Create kernel
183 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemmlowp_matrix_b_reduction", build_opts.options()));
184
Gian Marco05288a22017-11-21 10:57:50 +0000185 // Configure kernel window
Georgios Pinitas358ca202017-12-07 16:47:52 +0000186 auto win_config = validate_and_configure_window_matrix_b_reduction(_input->info(), _output->info());
187 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100188 ICLKernel::configure_internal(win_config.second);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000189}
Gian Marco05288a22017-11-21 10:57:50 +0000190
Georgios Pinitas358ca202017-12-07 16:47:52 +0000191Status CLGEMMLowpMatrixBReductionKernel::validate(const ITensorInfo *mtx_b, const ITensorInfo *vector_sum_col)
192{
193 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_b_reduction(mtx_b, vector_sum_col));
194 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 +0000195
Georgios Pinitas358ca202017-12-07 16:47:52 +0000196 return Status{};
Gian Marco05288a22017-11-21 10:57:50 +0000197}
198
199void CLGEMMLowpMatrixBReductionKernel::run(const Window &window, cl::CommandQueue &queue)
200{
201 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
202 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
203
204 Window collapsed = window.collapse_if_possible(IKernel::window(), Window::DimY);
205
206 Window slice_out = collapsed.first_slice_window_2D();
207 Window slice_in = slice_out;
208
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100209 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
210 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
Gian Marco05288a22017-11-21 10:57:50 +0000211
212 do
213 {
214 unsigned int idx = 0;
215 add_3D_tensor_argument(idx, _input, slice_in);
216 add_2D_tensor_argument(idx, _output, slice_out);
217 enqueue(queue, *this, slice_out);
218 }
219 while(collapsed.slide_window_slice_2D(slice_out));
220}
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +0100221} // namespace arm_compute