blob: 225c358b205875e0ea195269facdb0fc9bff9b1b [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
Anthony Barbierb6eb3532018-08-08 13:20:04 +01002 * Copyright (c) 2017-2018 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
41using namespace arm_compute;
42
43namespace arm_compute
44{
45class Coordinates;
46} // namespace arm_compute
47
Georgios Pinitas358ca202017-12-07 16:47:52 +000048namespace
49{
50Status validate_arguments_matrix_a_reduction(const ITensorInfo *input, const ITensorInfo *output)
51{
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8);
53 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
54
55 return Status{};
56}
57std::pair<Status, Window> validate_and_configure_window_matrix_a_reduction(ITensorInfo *input, ITensorInfo *output)
58{
59 const unsigned int num_elems_processed_per_iteration = 1;
60
61 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
62
Gian Marco Iodice4b908652018-10-18 10:21:02 +010063 AccessWindowStatic input_access(input, 0, 0, input->dimension(0), input->dimension(1));
Georgios Pinitas358ca202017-12-07 16:47:52 +000064 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
65
66 bool window_changed = update_window_and_padding(win, input_access, output_access);
67
68 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
69
70 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
71 return std::make_pair(err, win);
72}
73
74Status validate_arguments_matrix_b_reduction(const ITensorInfo *input, const ITensorInfo *output)
75{
76 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8);
77 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
78
79 return Status{};
80}
81
82std::pair<Status, Window> validate_and_configure_window_matrix_b_reduction(ITensorInfo *input, ITensorInfo *output)
83{
84 constexpr unsigned int num_elems_processed_per_iteration = 16;
85
86 // Configure kernel window
87 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
88
89 AccessWindowStatic input_access(input, 0, 0, ceil_to_multiple(input->dimension(0), num_elems_processed_per_iteration), input->dimension(1));
90 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
91
92 bool window_changed = update_window_and_padding(win, input_access, output_access);
93
94 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
95
96 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
97 return std::make_pair(err, win);
98}
99} // namespace
100
Gian Marco05288a22017-11-21 10:57:50 +0000101ICLGEMMLowpReductionKernel::ICLGEMMLowpReductionKernel()
102 : _input(), _output()
103{
104}
105
106void CLGEMMLowpMatrixAReductionKernel::configure(const ICLTensor *mtx_a, ICLTensor *vector_sum_row)
107{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000108 // Perform validate step
109 ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_a, vector_sum_row);
110 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_a_reduction(mtx_a->info(), vector_sum_row->info()));
Gian Marco05288a22017-11-21 10:57:50 +0000111
112 _input = mtx_a;
113 _output = vector_sum_row;
114
115 // Set the arguments to pass at compile time
116 CLBuildOptions build_opts;
117 build_opts.add_option("-DCOLS_A=" + support::cpp11::to_string(mtx_a->info()->dimension(0)));
118
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100119 const bool is_dot8_supported = dot8_supported(CLKernelLibrary::get().get_device());
120
121 std::string kernel_name = "gemmlowp_matrix_a_reduction" + std::string(is_dot8_supported ? "_dot8" : "");
122
Gian Marco05288a22017-11-21 10:57:50 +0000123 // Create kernel
Gian Marco Iodice4b908652018-10-18 10:21:02 +0100124 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
Gian Marco05288a22017-11-21 10:57:50 +0000125
Gian Marco05288a22017-11-21 10:57:50 +0000126 // Configure kernel window
Georgios Pinitas358ca202017-12-07 16:47:52 +0000127 auto win_config = validate_and_configure_window_matrix_a_reduction(_input->info(), _output->info());
128 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100129 ICLKernel::configure_internal(win_config.second);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000130}
Gian Marco05288a22017-11-21 10:57:50 +0000131
Georgios Pinitas358ca202017-12-07 16:47:52 +0000132Status CLGEMMLowpMatrixAReductionKernel::validate(const ITensorInfo *mtx_a, const ITensorInfo *vector_sum_row)
133{
134 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_a_reduction(mtx_a, vector_sum_row));
135 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 +0000136
Georgios Pinitas358ca202017-12-07 16:47:52 +0000137 return Status{};
Gian Marco05288a22017-11-21 10:57:50 +0000138}
139
140void CLGEMMLowpMatrixAReductionKernel::run(const Window &window, cl::CommandQueue &queue)
141{
142 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
143 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
144
145 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimY);
146 Window slice_in = collapsed.first_slice_window_2D();
147 Window slice_out = collapsed.first_slice_window_2D();
148
149 // Setup input slice. Its dimensions are increased in the cl kernel.
150 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
151 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
152 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
153
154 do
155 {
156 unsigned int idx = 0;
157 add_3D_tensor_argument(idx, _input, slice_in);
158 add_2D_tensor_argument(idx, _output, slice_out);
159 enqueue(queue, *this, slice_out);
160 }
161 while(collapsed.slide_window_slice_2D(slice_out));
162}
163
164void CLGEMMLowpMatrixBReductionKernel::configure(const ICLTensor *mtx_b, ICLTensor *vector_sum_col)
165{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000166 ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_b, vector_sum_col);
167 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_b_reduction(mtx_b->info(), vector_sum_col->info()));
Gian Marco05288a22017-11-21 10:57:50 +0000168
169 _input = mtx_b;
170 _output = vector_sum_col;
171
172 // Set the arguments to pass at compile time
173 CLBuildOptions build_opts;
174 build_opts.add_option("-DCOLS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(0)));
175 build_opts.add_option("-DROWS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(1)));
176
177 // Create kernel
178 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemmlowp_matrix_b_reduction", build_opts.options()));
179
Gian Marco05288a22017-11-21 10:57:50 +0000180 // Configure kernel window
Georgios Pinitas358ca202017-12-07 16:47:52 +0000181 auto win_config = validate_and_configure_window_matrix_b_reduction(_input->info(), _output->info());
182 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100183 ICLKernel::configure_internal(win_config.second);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000184}
Gian Marco05288a22017-11-21 10:57:50 +0000185
Georgios Pinitas358ca202017-12-07 16:47:52 +0000186Status CLGEMMLowpMatrixBReductionKernel::validate(const ITensorInfo *mtx_b, const ITensorInfo *vector_sum_col)
187{
188 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_b_reduction(mtx_b, vector_sum_col));
189 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 +0000190
Georgios Pinitas358ca202017-12-07 16:47:52 +0000191 return Status{};
Gian Marco05288a22017-11-21 10:57:50 +0000192}
193
194void CLGEMMLowpMatrixBReductionKernel::run(const Window &window, cl::CommandQueue &queue)
195{
196 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
197 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
198
199 Window collapsed = window.collapse_if_possible(IKernel::window(), Window::DimY);
200
201 Window slice_out = collapsed.first_slice_window_2D();
202 Window slice_in = slice_out;
203
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100204 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
205 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
Gian Marco05288a22017-11-21 10:57:50 +0000206
207 do
208 {
209 unsigned int idx = 0;
210 add_3D_tensor_argument(idx, _input, slice_in);
211 add_2D_tensor_argument(idx, _output, slice_out);
212 enqueue(queue, *this, slice_out);
213 }
214 while(collapsed.slide_window_slice_2D(slice_out));
215}