blob: ef572cfc7e885897de3533679fc57061b2ce8d72 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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/CLGEMMLowpMatrixMultiplyKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.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"
Moritz Pflanzer05da6dd2017-07-04 12:08:41 +010036#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38#include <cstddef>
39#include <cstdint>
40#include <tuple>
41
42using namespace arm_compute;
43
44namespace arm_compute
45{
46class Coordinates;
47} // namespace arm_compute
48
49CLGEMMLowpMatrixMultiplyKernel::CLGEMMLowpMatrixMultiplyKernel()
50 : _input0(nullptr), _input1(nullptr), _output(nullptr)
51{
52}
53
54void CLGEMMLowpMatrixMultiplyKernel::configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output,
55 int32_t a_offset, int32_t b_offset, int32_t output_offset, int32_t output_mult_int, int32_t shift)
56{
57 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::U8);
58 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8);
59 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
60 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1, output);
61
62 _input0 = input0;
63 _input1 = input1;
64 _output = output;
65
66 // Create kernel and set static arguments
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010067 std::set<std::string> build_opts = { ("-DCOLS_B=" + support::cpp11::to_string(input1->info()->dimension(0))) };
68 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("gemm_mm_interleaved_transposed_u8", build_opts));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069 unsigned int idx = 3 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
70 _kernel.setArg<int32_t>(idx++, a_offset);
71 _kernel.setArg<int32_t>(idx++, b_offset);
72 _kernel.setArg<int32_t>(idx++, output_offset);
73 _kernel.setArg<int32_t>(idx++, output_mult_int);
74 _kernel.setArg<int32_t>(idx++, shift);
75
76 // Configure window
77 constexpr unsigned int num_elems_processed_per_iteration_x = 16;
78 constexpr unsigned int num_elems_processed_per_iteration_y = 4;
79 constexpr unsigned int num_elems_read_per_iteration_input0 = 4;
80 constexpr unsigned int num_elems_read_per_iteration_input1 = 16;
81
82 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
83
84 AccessWindowRectangle input0_access(input0->info(), 0, 0, num_elems_read_per_iteration_input0, 1);
85 AccessWindowRectangle input1_access(input1->info(), 0, 0, num_elems_read_per_iteration_input1, 1);
86 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
87
88 update_window_and_padding(win, input0_access, input1_access, output_access);
89
90 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->info()->tensor_shape()));
91
92 ICLKernel::configure(win);
93}
94
95void CLGEMMLowpMatrixMultiplyKernel::run(const Window &window, cl::CommandQueue &queue)
96{
97 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
98 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
99
100 Window slice = window.first_slice_window_2D();
101 Window slice_matrix_b = slice;
102 slice_matrix_b.set(Window::DimX, Window::Dimension(0, _input1->info()->dimension(0), 1));
103 slice_matrix_b.set(Window::DimY, Window::Dimension(0, _input1->info()->dimension(1), 1));
104 slice_matrix_b.set(Window::DimZ, Window::Dimension(0, 1, 1));
105
106 do
107 {
108 Window slice_b = slice;
109 // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2
110 // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
111 if(_input1->info()->num_dimensions() < 3)
112 {
113 slice_b = slice_matrix_b;
114 }
115
116 unsigned int idx = 0;
117 add_2D_tensor_argument(idx, _input0, slice);
118 add_2D_tensor_argument(idx, _input1, slice_b);
119 add_2D_tensor_argument(idx, _output, slice);
120 enqueue(queue, *this, slice);
121 }
122 while(window.slide_window_slice_2D(slice));
123}