blob: 6951512167e53e78e0ff9c1d32246aa6f0adbd5f [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
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/CLGEMMLowpReductionKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
35#include "support/ToolchainSupport.h"
36
37#include <cstddef>
38#include <cstdint>
39
40using namespace arm_compute;
41
42namespace arm_compute
43{
44class Coordinates;
45} // namespace arm_compute
46
Georgios Pinitas358ca202017-12-07 16:47:52 +000047namespace
48{
49Status validate_arguments_matrix_a_reduction(const ITensorInfo *input, const ITensorInfo *output)
50{
51 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8);
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
53
54 return Status{};
55}
56std::pair<Status, Window> validate_and_configure_window_matrix_a_reduction(ITensorInfo *input, ITensorInfo *output)
57{
58 const unsigned int num_elems_processed_per_iteration = 1;
59
60 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
61
62 AccessWindowStatic input_access(input, 0, 0, ceil_to_multiple(input->dimension(0), 16), input->dimension(1));
63 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
64
65 bool window_changed = update_window_and_padding(win, input_access, output_access);
66
67 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
68
69 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
70 return std::make_pair(err, win);
71}
72
73Status validate_arguments_matrix_b_reduction(const ITensorInfo *input, const ITensorInfo *output)
74{
75 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8);
76 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
77
78 return Status{};
79}
80
81std::pair<Status, Window> validate_and_configure_window_matrix_b_reduction(ITensorInfo *input, ITensorInfo *output)
82{
83 constexpr unsigned int num_elems_processed_per_iteration = 16;
84
85 // Configure kernel window
86 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
87
88 AccessWindowStatic input_access(input, 0, 0, ceil_to_multiple(input->dimension(0), num_elems_processed_per_iteration), input->dimension(1));
89 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
90
91 bool window_changed = update_window_and_padding(win, input_access, output_access);
92
93 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape()));
94
95 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
96 return std::make_pair(err, win);
97}
98} // namespace
99
Gian Marco05288a22017-11-21 10:57:50 +0000100ICLGEMMLowpReductionKernel::ICLGEMMLowpReductionKernel()
101 : _input(), _output()
102{
103}
104
105void CLGEMMLowpMatrixAReductionKernel::configure(const ICLTensor *mtx_a, ICLTensor *vector_sum_row)
106{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000107 // Perform validate step
108 ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_a, vector_sum_row);
109 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_a_reduction(mtx_a->info(), vector_sum_row->info()));
Gian Marco05288a22017-11-21 10:57:50 +0000110
111 _input = mtx_a;
112 _output = vector_sum_row;
113
114 // Set the arguments to pass at compile time
115 CLBuildOptions build_opts;
116 build_opts.add_option("-DCOLS_A=" + support::cpp11::to_string(mtx_a->info()->dimension(0)));
117
118 // Create kernel
119 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemmlowp_matrix_a_reduction", build_opts.options()));
120
Gian Marco05288a22017-11-21 10:57:50 +0000121 // Configure kernel window
Georgios Pinitas358ca202017-12-07 16:47:52 +0000122 auto win_config = validate_and_configure_window_matrix_a_reduction(_input->info(), _output->info());
123 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
124 ICLKernel::configure(win_config.second);
125}
Gian Marco05288a22017-11-21 10:57:50 +0000126
Georgios Pinitas358ca202017-12-07 16:47:52 +0000127Status CLGEMMLowpMatrixAReductionKernel::validate(const ITensorInfo *mtx_a, const ITensorInfo *vector_sum_row)
128{
129 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_a_reduction(mtx_a, vector_sum_row));
130 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 +0000131
Georgios Pinitas358ca202017-12-07 16:47:52 +0000132 return Status{};
Gian Marco05288a22017-11-21 10:57:50 +0000133}
134
135void CLGEMMLowpMatrixAReductionKernel::run(const Window &window, cl::CommandQueue &queue)
136{
137 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
138 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
139
140 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimY);
141 Window slice_in = collapsed.first_slice_window_2D();
142 Window slice_out = collapsed.first_slice_window_2D();
143
144 // Setup input slice. Its dimensions are increased in the cl kernel.
145 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
146 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
147 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
148
149 do
150 {
151 unsigned int idx = 0;
152 add_3D_tensor_argument(idx, _input, slice_in);
153 add_2D_tensor_argument(idx, _output, slice_out);
154 enqueue(queue, *this, slice_out);
155 }
156 while(collapsed.slide_window_slice_2D(slice_out));
157}
158
159void CLGEMMLowpMatrixBReductionKernel::configure(const ICLTensor *mtx_b, ICLTensor *vector_sum_col)
160{
Georgios Pinitas358ca202017-12-07 16:47:52 +0000161 ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_b, vector_sum_col);
162 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_b_reduction(mtx_b->info(), vector_sum_col->info()));
Gian Marco05288a22017-11-21 10:57:50 +0000163
164 _input = mtx_b;
165 _output = vector_sum_col;
166
167 // Set the arguments to pass at compile time
168 CLBuildOptions build_opts;
169 build_opts.add_option("-DCOLS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(0)));
170 build_opts.add_option("-DROWS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(1)));
171
172 // Create kernel
173 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemmlowp_matrix_b_reduction", build_opts.options()));
174
Gian Marco05288a22017-11-21 10:57:50 +0000175 // Configure kernel window
Georgios Pinitas358ca202017-12-07 16:47:52 +0000176 auto win_config = validate_and_configure_window_matrix_b_reduction(_input->info(), _output->info());
177 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
178 ICLKernel::configure(win_config.second);
179}
Gian Marco05288a22017-11-21 10:57:50 +0000180
Georgios Pinitas358ca202017-12-07 16:47:52 +0000181Status CLGEMMLowpMatrixBReductionKernel::validate(const ITensorInfo *mtx_b, const ITensorInfo *vector_sum_col)
182{
183 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_b_reduction(mtx_b, vector_sum_col));
184 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 +0000185
Georgios Pinitas358ca202017-12-07 16:47:52 +0000186 return Status{};
Gian Marco05288a22017-11-21 10:57:50 +0000187}
188
189void CLGEMMLowpMatrixBReductionKernel::run(const Window &window, cl::CommandQueue &queue)
190{
191 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
192 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
193
194 Window collapsed = window.collapse_if_possible(IKernel::window(), Window::DimY);
195
196 Window slice_out = collapsed.first_slice_window_2D();
197 Window slice_in = slice_out;
198
199 slice_in.set(Window::DimY, Window::Dimension(0, 1, 1));
200 slice_in.set(Window::DimZ, Window::Dimension(0, 1, 1));
201
202 do
203 {
204 unsigned int idx = 0;
205 add_3D_tensor_argument(idx, _input, slice_in);
206 add_2D_tensor_argument(idx, _output, slice_out);
207 enqueue(queue, *this, slice_out);
208 }
209 while(collapsed.slide_window_slice_2D(slice_out));
210}