Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 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/CLGEMMMatrixVectorMultiplyKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 29 | #include "arm_compute/core/CL/ICLTensor.h" |
| 30 | #include "arm_compute/core/CL/OpenCL.h" |
| 31 | #include "arm_compute/core/Error.h" |
| 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/Types.h" |
| 34 | |
| 35 | using namespace arm_compute; |
| 36 | |
| 37 | CLGEMMMatrixVectorMultiplyKernel::CLGEMMMatrixVectorMultiplyKernel() |
| 38 | : _input0(nullptr), _input1(nullptr), _output(nullptr), _num_rows_read_per_iteration(0), _border_size(0) |
| 39 | { |
| 40 | } |
| 41 | BorderSize CLGEMMMatrixVectorMultiplyKernel::border_size() const |
| 42 | { |
| 43 | return _border_size; |
| 44 | } |
| 45 | |
| 46 | void CLGEMMMatrixVectorMultiplyKernel::configure(const ICLTensor *input0, const ICLTensor *input1, ICLTensor *output) |
| 47 | { |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
| 49 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input0, input1, output); |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_ERROR_ON(is_data_type_quantized_asymmetric(input0->info()->data_type()) && (output->info()->data_type() != DataType::S32)); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_ERROR_ON(input0->info()->dimension(2) != input1->info()->dimension(1)); |
| 53 | |
| 54 | _input0 = input0; |
| 55 | _input1 = input1; |
| 56 | _output = output; |
| 57 | |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 58 | // Check if is a quantized operation |
| 59 | bool is_quantized = is_data_type_quantized_asymmetric(_input0->info()->data_type()); |
| 60 | |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 61 | // Create kernel |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 62 | CLBuildOptions build_opts; |
| 63 | build_opts.add_option_if(!is_quantized, "-DDATA_TYPE=" + get_cl_type_from_data_type(input0->info()->data_type())); |
| 64 | build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input0->info()->dimension(0))); |
| 65 | build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input0->info()->dimension(1))); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 66 | |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 67 | std::string kernel_name = is_quantized ? std::string("gemm_mv_quantized") : std::string("gemm_mv"); |
| 68 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 69 | |
Georgios Pinitas | de5a1cc | 2018-02-02 12:52:07 +0000 | [diff] [blame] | 70 | // Add static arguments |
| 71 | if(is_quantized) |
| 72 | { |
| 73 | unsigned int idx = num_arguments_per_3D_tensor() + num_arguments_per_2D_tensor() + num_arguments_per_1D_tensor(); |
| 74 | _kernel.setArg<int>(idx++, -_input0->info()->quantization_info().offset); |
| 75 | _kernel.setArg<int>(idx++, -_input1->info()->quantization_info().offset); |
| 76 | } |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 77 | |
Anthony Barbier | a2ea753 | 2017-11-28 10:33:22 +0000 | [diff] [blame] | 78 | // Configure the local work size for Bifrost with a value obtained |
| 79 | // via exhaustive autotuning for the MobileNets tensor shapes. |
Michalis Spyrou | a967611 | 2018-02-22 18:07:43 +0000 | [diff] [blame] | 80 | const GPUTarget gpu_target = get_target(); |
| 81 | if(gpu_target_is_in(gpu_target, GPUTarget::G71, GPUTarget::G72)) |
Anthony Barbier | a2ea753 | 2017-11-28 10:33:22 +0000 | [diff] [blame] | 82 | { |
| 83 | _lws_hint = cl::NDRange(1, 1, 1); |
| 84 | } |
| 85 | |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 86 | // Configure kernel window |
| 87 | const unsigned int num_elems_read_per_iteration = 4; |
| 88 | |
| 89 | _num_rows_read_per_iteration = 4; |
| 90 | |
Georgios Pinitas | af9cf00 | 2017-09-27 18:41:07 +0100 | [diff] [blame] | 91 | const unsigned int border_x = ceil_to_multiple(input0->info()->dimension(0), num_elems_read_per_iteration) - input0->info()->dimension(0); |
| 92 | const unsigned int border_y = ceil_to_multiple(input0->info()->dimension(1), _num_rows_read_per_iteration) - input0->info()->dimension(1); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 93 | |
| 94 | _border_size = BorderSize(border_y, border_x); |
| 95 | |
| 96 | Window win = calculate_max_window(*input0->info(), Steps(num_elems_read_per_iteration)); |
| 97 | |
steniu01 | f3aeee1 | 2017-09-21 16:27:56 +0100 | [diff] [blame] | 98 | AccessWindowRectangle input0_access(input0->info(), 0, 0, num_elems_read_per_iteration, _num_rows_read_per_iteration); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 99 | AccessWindowHorizontal input1_access(input1->info(), 0, num_elems_read_per_iteration); |
| 100 | AccessWindowStatic output_access(_output->info(), 0, 0, _output->info()->dimension(0) + border_x, _output->info()->dimension(1) + border_y); |
| 101 | |
| 102 | update_window_and_padding(win, input0_access, input1_access, output_access); |
| 103 | |
| 104 | _output->info()->set_valid_region(ValidRegion(Coordinates(), _output->info()->tensor_shape())); |
| 105 | |
| 106 | ICLKernel::configure(win); |
| 107 | } |
| 108 | |
| 109 | void CLGEMMMatrixVectorMultiplyKernel::run(const Window &window, cl::CommandQueue &queue) |
| 110 | { |
| 111 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 112 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 113 | |
| 114 | Window slice_in = window.first_slice_window_3D(); |
| 115 | Window slice_in2 = window.first_slice_window_3D(); |
| 116 | Window slice_out = window.first_slice_window_3D(); |
| 117 | |
| 118 | // Setup input0 slice |
Georgios Pinitas | af9cf00 | 2017-09-27 18:41:07 +0100 | [diff] [blame] | 119 | slice_in.set(Window::DimX, Window::Dimension(0, _input0->info()->dimension(0), _input0->info()->dimension(0))); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 120 | slice_in.set(Window::DimY, Window::Dimension(0, _input0->info()->dimension(1) + border_size().bottom, _num_rows_read_per_iteration)); |
| 121 | slice_in.set(Window::DimZ, Window::Dimension(0, _input0->info()->dimension(2), 1)); |
| 122 | |
| 123 | // Setup input1 and output slice. Their dimensions are increased in the cl kernel. |
| 124 | slice_in2.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 125 | slice_in2.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 126 | slice_in2.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 127 | |
| 128 | slice_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 129 | slice_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 130 | slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 131 | |
| 132 | unsigned int idx_1 = num_arguments_per_3D_tensor(); |
| 133 | |
| 134 | add_2D_tensor_argument(idx_1, _input1, slice_in2); |
| 135 | |
| 136 | do |
| 137 | { |
| 138 | unsigned int idx_0 = 0; |
| 139 | unsigned int idx_2 = num_arguments_per_3D_tensor() + num_arguments_per_2D_tensor(); |
| 140 | add_3D_tensor_argument(idx_0, _input0, slice_in); |
| 141 | add_1D_tensor_argument(idx_2, _output, slice_out); |
Anthony Barbier | a2ea753 | 2017-11-28 10:33:22 +0000 | [diff] [blame] | 142 | enqueue(queue, *this, slice_in, _lws_hint); |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 143 | } |
| 144 | while(window.slide_window_slice_3D(slice_in) && window.slide_window_slice_3D(slice_out)); |
| 145 | } |