Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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/runtime/CL/functions/CLGEMM.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
| 27 | #include "arm_compute/core/CL/kernels/CLGEMMInterleave4x4Kernel.h" |
| 28 | #include "arm_compute/core/CL/kernels/CLGEMMMatrixAdditionKernel.h" |
| 29 | #include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h" |
| 30 | #include "arm_compute/core/CL/kernels/CLGEMMTranspose1xWKernel.h" |
| 31 | #include "arm_compute/core/Error.h" |
| 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/TensorInfo.h" |
| 34 | #include "arm_compute/core/Types.h" |
| 35 | #include "arm_compute/core/Validate.h" |
| 36 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 37 | #include "arm_compute/runtime/ITensorAllocator.h" |
| 38 | |
| 39 | using namespace arm_compute; |
| 40 | |
| 41 | CLGEMM::CLGEMM() |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 42 | : _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _ma_kernel(), _tmp_a(), _tmp_b(), _is_interleaved_transposed(false), _run_addition(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
| 46 | void CLGEMM::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta) |
| 47 | { |
Gian Marco Iodice | 8a38369 | 2017-07-03 17:41:47 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | |
| 51 | if(c != nullptr) |
| 52 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, c); |
| 54 | ARM_COMPUTE_ERROR_ON_MSG(a->info()->dimension(1) != c->info()->dimension(1), "The C matrix must have the same number of rows as the matrix A"); |
| 55 | ARM_COMPUTE_ERROR_ON_MSG(b->info()->dimension(0) != c->info()->dimension(0), "The C matrix must have the same number of columns as the matrix C"); |
| 56 | ARM_COMPUTE_ERROR_ON_MSG(c->info()->dimension(0) != output->info()->dimension(0), "The C matrix must have the same number of rows as the output matrix"); |
| 57 | ARM_COMPUTE_ERROR_ON_MSG(c->info()->dimension(1) != output->info()->dimension(1), "The C matrix must have the same number of columns as the output matrix"); |
| 58 | } |
| 59 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_ERROR_ON_MSG(a->info()->dimension(0) != b->info()->dimension(1), "The product AB is defined only if the number of columns in A is equal to the number of rows in B"); |
| 61 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 62 | // If the input tensor has less than 16 rows, we run a special version of GEMM without reshaping the input tensors |
| 63 | _is_interleaved_transposed = a->info()->dimension(1) > 16; |
Gian Marco Iodice | 1246b63 | 2017-08-16 18:38:32 +0100 | [diff] [blame] | 64 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 65 | const ICLTensor *matrix_a = a; |
| 66 | const ICLTensor *matrix_b = b; |
| 67 | |
| 68 | if(_is_interleaved_transposed) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 70 | matrix_a = &_tmp_a; |
| 71 | matrix_b = &_tmp_b; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | |
| 73 | TensorShape shape_tmp_a = a->info()->tensor_shape(); |
| 74 | TensorShape shape_tmp_b = b->info()->tensor_shape(); |
| 75 | |
| 76 | shape_tmp_a.set(0, a->info()->dimension(0) * 4); |
| 77 | shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.0f)); |
| 78 | |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 79 | const unsigned int transpose_w = max_cl_vector_width / data_size_from_type(b->info()->data_type()); |
| 80 | shape_tmp_b.set(0, b->info()->dimension(1) * transpose_w); |
| 81 | shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / static_cast<float>(transpose_w))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 82 | |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 83 | TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type(), a->info()->fixed_point_position()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | _tmp_a.allocator()->init(info_a); |
| 85 | |
Gian Marco Iodice | 3a3066b | 2017-06-23 13:38:14 +0100 | [diff] [blame] | 86 | TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type(), b->info()->fixed_point_position()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | _tmp_b.allocator()->init(info_b); |
| 88 | |
| 89 | // Configure interleave kernel |
| 90 | _interleave_kernel.configure(a, &_tmp_a); |
| 91 | |
| 92 | // Configure transpose kernel |
| 93 | _transpose_kernel.configure(b, &_tmp_b); |
| 94 | |
| 95 | // Configure matrix multiply kernel |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 96 | _mm_kernel.set_target(CLScheduler::get().target()); |
| 97 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 99 | _mm_kernel.configure(matrix_a, matrix_b, output, alpha, _is_interleaved_transposed); |
| 100 | |
| 101 | if(_is_interleaved_transposed) |
| 102 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | // Allocate intermediate tensors |
| 104 | _tmp_a.allocator()->allocate(); |
| 105 | _tmp_b.allocator()->allocate(); |
| 106 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | |
| 108 | // Configure matrix addition kernel |
| 109 | if(beta != 0 && c != nullptr) |
| 110 | { |
| 111 | _ma_kernel.configure(c, output, beta); |
| 112 | _run_addition = true; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void CLGEMM::run() |
| 117 | { |
Gian Marco Iodice | edfa9f4 | 2017-08-15 11:45:22 +0100 | [diff] [blame] | 118 | if(_is_interleaved_transposed) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | { |
| 120 | // Run interleave kernel |
| 121 | CLScheduler::get().enqueue(_interleave_kernel, false); |
| 122 | |
| 123 | // Run transpose kernel |
| 124 | CLScheduler::get().enqueue(_transpose_kernel, false); |
| 125 | } |
| 126 | |
| 127 | // Run matrix multiply kernel |
| 128 | CLScheduler::get().enqueue(_mm_kernel, !_run_addition); |
| 129 | |
| 130 | // Run matrix addition kernel |
| 131 | if(_run_addition) |
| 132 | { |
| 133 | CLScheduler::get().enqueue(_ma_kernel); |
| 134 | } |
| 135 | } |