blob: 6f410d3b14a9fbfae8dfbfcb720e55651ed094ec [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
47ICLGEMMLowpReductionKernel::ICLGEMMLowpReductionKernel()
48 : _input(), _output()
49{
50}
51
52void CLGEMMLowpMatrixAReductionKernel::configure(const ICLTensor *mtx_a, ICLTensor *vector_sum_row)
53{
54 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(mtx_a, 1, DataType::QASYMM8);
55 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_row, 1, DataType::S32);
56
57 _input = mtx_a;
58 _output = vector_sum_row;
59
60 // Set the arguments to pass at compile time
61 CLBuildOptions build_opts;
62 build_opts.add_option("-DCOLS_A=" + support::cpp11::to_string(mtx_a->info()->dimension(0)));
63
64 // Create kernel
65 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemmlowp_matrix_a_reduction", build_opts.options()));
66
67 const unsigned int num_elems_processed_per_iteration = 1;
68
69 // Configure kernel window
70 Window win = calculate_max_window(*_output->info(), Steps(num_elems_processed_per_iteration));
71
72 AccessWindowStatic input_access(_input->info(), 0, 0, ceil_to_multiple(_input->info()->dimension(0), 16), _input->info()->dimension(1));
73 AccessWindowHorizontal output_access(_output->info(), 0, num_elems_processed_per_iteration);
74
75 update_window_and_padding(win,
76 input_access,
77 output_access);
78
79 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), _output->info()->tensor_shape()));
80
81 ICLKernel::configure(win);
82}
83
84void CLGEMMLowpMatrixAReductionKernel::run(const Window &window, cl::CommandQueue &queue)
85{
86 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
87 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
88
89 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimY);
90 Window slice_in = collapsed.first_slice_window_2D();
91 Window slice_out = collapsed.first_slice_window_2D();
92
93 // Setup input slice. Its dimensions are increased in the cl kernel.
94 slice_in.set(Window::DimX, Window::Dimension(0, 0, 0));
95 slice_in.set(Window::DimY, Window::Dimension(0, 0, 0));
96 slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0));
97
98 do
99 {
100 unsigned int idx = 0;
101 add_3D_tensor_argument(idx, _input, slice_in);
102 add_2D_tensor_argument(idx, _output, slice_out);
103 enqueue(queue, *this, slice_out);
104 }
105 while(collapsed.slide_window_slice_2D(slice_out));
106}
107
108void CLGEMMLowpMatrixBReductionKernel::configure(const ICLTensor *mtx_b, ICLTensor *vector_sum_col)
109{
110 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(mtx_b, 1, DataType::QASYMM8);
111 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_col, 1, DataType::S32);
112
113 _input = mtx_b;
114 _output = vector_sum_col;
115
116 // Set the arguments to pass at compile time
117 CLBuildOptions build_opts;
118 build_opts.add_option("-DCOLS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(0)));
119 build_opts.add_option("-DROWS_B=" + support::cpp11::to_string(mtx_b->info()->dimension(1)));
120
121 // Create kernel
122 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemmlowp_matrix_b_reduction", build_opts.options()));
123
124 constexpr unsigned int num_elems_processed_per_iteration = 16;
125
126 // Configure kernel window
127 Window win = calculate_max_window(*vector_sum_col->info(), Steps(num_elems_processed_per_iteration));
128
129 AccessWindowStatic input_access(_input->info(), 0, 0, ceil_to_multiple(_input->info()->dimension(0), 16), _input->info()->dimension(1));
130 AccessWindowHorizontal output_access(_output->info(), 0, num_elems_processed_per_iteration);
131
132 update_window_and_padding(win,
133 input_access,
134 output_access);
135
136 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), _output->info()->tensor_shape()));
137
138 ICLKernel::configure(win);
139}
140
141void CLGEMMLowpMatrixBReductionKernel::run(const Window &window, cl::CommandQueue &queue)
142{
143 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
144 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
145
146 Window collapsed = window.collapse_if_possible(IKernel::window(), Window::DimY);
147
148 Window slice_out = collapsed.first_slice_window_2D();
149 Window slice_in = slice_out;
150
151 slice_in.set(Window::DimY, Window::Dimension(0, 1, 1));
152 slice_in.set(Window::DimZ, Window::Dimension(0, 1, 1));
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}