Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 1 | /* |
Joel Liang | abd03cf | 2018-01-08 15:20:48 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017, 2018 ARM Limited. |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +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/GLES_COMPUTE/kernels/GCGEMMMatrixMultiplyKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/AccessWindowTranspose.h" |
| 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/GLES_COMPUTE/GCHelpers.h" |
| 30 | #include "arm_compute/core/GLES_COMPUTE/GCKernelLibrary.h" |
| 31 | #include "arm_compute/core/GLES_COMPUTE/IGCTensor.h" |
| 32 | #include "arm_compute/core/GLES_COMPUTE/OpenGLES.h" |
| 33 | #include "arm_compute/core/Helpers.h" |
| 34 | #include "arm_compute/core/Types.h" |
| 35 | #include "arm_compute/core/Utils.h" |
| 36 | #include "arm_compute/core/Validate.h" |
| 37 | #include "arm_compute/core/Window.h" |
| 38 | |
| 39 | #include <set> |
| 40 | #include <string> |
| 41 | |
| 42 | using namespace arm_compute; |
Joel Liang | 1c5ffd6 | 2017-12-28 10:09:51 +0800 | [diff] [blame] | 43 | using namespace arm_compute::gles_compute; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 44 | |
| 45 | GCGEMMMatrixMultiplyKernel::GCGEMMMatrixMultiplyKernel() |
| 46 | : _input0(nullptr), _input1(nullptr), _output(nullptr) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | void GCGEMMMatrixMultiplyKernel::configure(const IGCTensor *input0, const IGCTensor *input1, IGCTensor *output, float alpha, bool is_interleaved_transposed) |
| 51 | { |
| 52 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::F32, DataType::F16); |
| 53 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1, output); |
| 54 | |
| 55 | if(!is_interleaved_transposed) |
| 56 | { |
| 57 | ARM_COMPUTE_ERROR_ON(input0->info()->dimension(0) != input1->info()->dimension(1)); |
| 58 | } |
| 59 | |
| 60 | _input0 = input0; |
| 61 | _input1 = input1; |
| 62 | _output = output; |
| 63 | |
| 64 | std::set<std::string> build_opts; |
| 65 | Window win; |
| 66 | |
| 67 | build_opts.emplace("#define LOCAL_SIZE_X " + support::cpp11::to_string(1)); |
| 68 | build_opts.emplace("#define LOCAL_SIZE_Y " + support::cpp11::to_string(1)); |
| 69 | build_opts.emplace("#define LOCAL_SIZE_Z " + support::cpp11::to_string(1)); |
| 70 | build_opts.emplace("#define COLS_A " + support::cpp11::to_string(input0->info()->dimension(0))); |
| 71 | build_opts.emplace("#define COLS_B " + support::cpp11::to_string(input1->info()->dimension(0))); |
| 72 | build_opts.emplace("#define ALPHA " + float_to_string_with_full_precision(alpha)); |
| 73 | |
| 74 | // Check if the output tensor is a vector. If so,the kernel runs the vector-matrix multiplication |
| 75 | if(is_interleaved_transposed) |
| 76 | { |
| 77 | switch(input0->info()->data_type()) |
| 78 | { |
| 79 | case DataType::F16: |
| 80 | build_opts.emplace("#define DATA_TYPE_FP16"); |
| 81 | break; |
| 82 | |
| 83 | case DataType::F32: |
| 84 | build_opts.emplace("#define DATA_TYPE_FP32"); |
| 85 | break; |
| 86 | |
| 87 | default: |
| 88 | ARM_COMPUTE_ERROR("Current data type is not supported"); |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | build_opts.emplace("#define GEMM_MM_INTERLEAVED_TRANSPOSED"); |
| 93 | |
| 94 | // Create kernel |
| 95 | _kernel = GCKernelLibrary::get().create_kernel(("gemm_mm_interleaved_transposed"), build_opts); |
| 96 | |
| 97 | // Configure window kernel |
| 98 | const unsigned int num_elems_processed_per_iteration_x = max_gc_vector_width / data_size_from_type(input0->info()->data_type()); |
| 99 | constexpr unsigned int num_elems_processed_per_iteration_y = 4; |
| 100 | |
| 101 | win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 102 | |
| 103 | AccessWindowRectangle input0_access(input0->info(), 0, 0, num_elems_processed_per_iteration_y, 1, 1.f, 0.25f); |
| 104 | AccessWindowTranspose input1_access(input1->info(), 0, 0, num_elems_processed_per_iteration_x, 1, 0.f, 0.25f); |
| 105 | AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); |
| 106 | |
| 107 | update_window_and_padding(win, input0_access, input1_access, output_access); |
| 108 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame^] | 109 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 110 | } |
| 111 | else |
| 112 | { |
| 113 | ARM_COMPUTE_ERROR_ON(input0->info()->dimension(0) != input1->info()->dimension(1)); |
| 114 | |
| 115 | // Special case for 1xN, 2xN, 3xN and 4xN input0 tensor |
| 116 | unsigned int num_elems_processed_per_iteration_x; |
| 117 | unsigned int num_elems_processed_per_iteration_y; |
| 118 | |
| 119 | switch(input0->info()->data_type()) |
| 120 | { |
| 121 | case DataType::F16: |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 122 | build_opts.emplace("#define DATA_TYPE_FP16"); |
Frank Lei | b9d38ee | 2017-12-05 10:43:33 +0800 | [diff] [blame] | 123 | |
| 124 | #define MM_PROCESS_4X_OPTIMIZED |
| 125 | |
| 126 | #if defined(MM_PROCESS_4X) |
| 127 | num_elems_processed_per_iteration_x = 4; |
| 128 | num_elems_processed_per_iteration_y = std::min(static_cast<int>(output->info()->dimension(1)), 4); |
| 129 | build_opts.emplace("#define MM_PROCESS_4X"); |
| 130 | #elif defined(MM_PROCESS_4X_OPTIMIZED) /* MM_PROCESS_4X */ |
| 131 | num_elems_processed_per_iteration_x = 4; |
| 132 | num_elems_processed_per_iteration_y = std::min(static_cast<int>(output->info()->dimension(1)), 4); |
| 133 | build_opts.emplace("#define MM_PROCESS_4X_OPTIMIZED"); |
| 134 | #elif defined(MM_PROCESS_8X) /* MM_PROCESS_4X */ |
| 135 | num_elems_processed_per_iteration_x = 8; |
| 136 | num_elems_processed_per_iteration_y = 1; |
| 137 | build_opts.emplace("#define MM_PROCESS_8X"); |
| 138 | #endif /* MM_PROCESS_4X */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 139 | break; |
| 140 | |
| 141 | case DataType::F32: |
| 142 | num_elems_processed_per_iteration_x = max_gc_vector_width / data_size_from_type(input0->info()->data_type()); |
| 143 | num_elems_processed_per_iteration_y = std::min(static_cast<int>(output->info()->dimension(1)), 4); |
| 144 | build_opts.emplace("#define DATA_TYPE_FP32"); |
| 145 | break; |
| 146 | |
| 147 | default: |
| 148 | ARM_COMPUTE_ERROR("Current data type is not supported"); |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | build_opts.emplace("#define GEMM_MM_FLOATING_POINT"); |
| 153 | build_opts.emplace("#define NUM_ELEMS_PROCESSED_PER_THREAD_X " + support::cpp11::to_string(num_elems_processed_per_iteration_x)); |
| 154 | build_opts.emplace("#define NUM_ELEMS_PROCESSED_PER_THREAD_Y " + support::cpp11::to_string(num_elems_processed_per_iteration_y)); |
| 155 | |
| 156 | // Create kernel |
| 157 | _kernel = GCKernelLibrary::get().create_kernel("gemm_mm_floating_point", build_opts); |
| 158 | |
| 159 | win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 160 | |
Frank Lei | b9d38ee | 2017-12-05 10:43:33 +0800 | [diff] [blame] | 161 | #if defined(MM_PROCESS_4X_OPTIMIZED) |
| 162 | AccessWindowStatic input0_access(input0->info(), 0, 0, ceil_to_multiple(input0->info()->dimension(0), 8), ceil_to_multiple(input0->info()->dimension(1), num_elems_processed_per_iteration_y)); |
| 163 | #else /* MM_PROCESS_4X_OPTIMIZED */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 164 | AccessWindowStatic input0_access(input0->info(), 0, 0, ceil_to_multiple(input0->info()->dimension(0), num_elems_processed_per_iteration_x), ceil_to_multiple(input0->info()->dimension(1), |
| 165 | num_elems_processed_per_iteration_y)); |
Frank Lei | b9d38ee | 2017-12-05 10:43:33 +0800 | [diff] [blame] | 166 | #endif /* MM_PROCESS_4X_OPTIMIZED */ |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 167 | AccessWindowStatic input1_access(input1->info(), 0, 0, ceil_to_multiple(input1->info()->dimension(0), num_elems_processed_per_iteration_x), input1->info()->dimension(1)); |
| 168 | AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); |
| 169 | |
| 170 | update_window_and_padding(win, input0_access, input1_access, output_access); |
| 171 | |
| 172 | Coordinates coord; |
| 173 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 174 | output_access.set_valid_region(win, ValidRegion(coord, output->info()->tensor_shape())); |
| 175 | } |
| 176 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 177 | IGCKernel::configure(win); |
| 178 | } |
| 179 | |
| 180 | void GCGEMMMatrixMultiplyKernel::run(const Window &window) |
| 181 | { |
| 182 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 183 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IGCKernel::window(), window); |
| 184 | |
| 185 | _kernel.use(); |
| 186 | |
| 187 | Window slice = window.first_slice_window_2D(); |
| 188 | Window slice_matrix_b = slice; |
| 189 | |
| 190 | slice_matrix_b.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 191 | slice_matrix_b.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 192 | |
| 193 | do |
| 194 | { |
| 195 | Window slice_b = slice; |
| 196 | // Don't slice matrix B along the z dimension if matrix B has just 2 dimensions and matrix A more than 2 |
| 197 | // This scenario can happen when the the matrix multiplication is used to perform a convolution operation |
| 198 | if(_input1->info()->num_dimensions() < 3) |
| 199 | { |
| 200 | slice_b = slice_matrix_b; |
| 201 | } |
| 202 | |
| 203 | unsigned int idx = 0; |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 204 | |
Joel Liang | abd03cf | 2018-01-08 15:20:48 +0800 | [diff] [blame] | 205 | add_2D_tensor_argument(idx, _input0, 1, slice); |
| 206 | add_2D_tensor_argument(idx, _input1, 2, slice_b); |
| 207 | add_2D_tensor_argument(idx, _output, 3, slice); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 208 | _kernel.update_shader_params(); |
| 209 | enqueue(*this, slice); |
| 210 | } |
| 211 | while(window.slide_window_slice_2D(slice)); |
| 212 | } |