blob: c676a1097815f1fdd630ebcc2a98f24f9fcbd060 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco20d78482018-01-11 15:10:58 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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
39using namespace arm_compute;
40
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010041CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager)
Gian Marco1d25ed52017-12-16 19:33:50 +000042 : _memory_group(std::move(memory_manager)), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _ma_kernel(), _tmp_a(), _tmp_b(), _is_interleaved_transposed(false), _run_addition(false),
43 _is_first_run(true), _reshape_b_only_on_first_run(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044{
45}
46
Gian Marco1d25ed52017-12-16 19:33:50 +000047void CLGEMM::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048{
Gian Marco Iodice8a383692017-07-03 17:41:47 +010049 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +010050 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, output);
Gian Marco1d25ed52017-12-16 19:33:50 +000051 ARM_COMPUTE_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
52 ARM_COMPUTE_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053
54 if(c != nullptr)
55 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, c);
57 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");
Giorgio Arena093adbc2018-01-10 15:50:03 +000058 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 B");
Anthony Barbier6ff3b192017-09-04 18:44:23 +010059 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");
60 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");
61 }
62
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063 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");
64
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010065 // If the input tensor has less than 16 rows, we run a special version of GEMM without reshaping the input tensors
Gian Marco1d25ed52017-12-16 19:33:50 +000066 // For Bifrost architectures we do not reshape the input matrices
67 _is_interleaved_transposed = (a->info()->dimension(1) > 16 && CLScheduler::get().target() != GPUTarget::BIFROST);
68
69 // Check if we need to reshape the matrix B only on the first run
70 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Gian Marco Iodice1246b632017-08-16 18:38:32 +010071
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010072 const ICLTensor *matrix_a = a;
73 const ICLTensor *matrix_b = b;
74
Gian Marcob5311a62017-12-13 12:48:03 +000075 // Set the target for the matrix multiply kernel
76 _mm_kernel.set_target(CLScheduler::get().target());
77
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010078 if(_is_interleaved_transposed)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 {
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010080 matrix_a = &_tmp_a;
81 matrix_b = &_tmp_b;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082
Gian Marco20d78482018-01-11 15:10:58 +000083 // _tmp_a and _tmp_b will be auto configured in _interleave_kernel and in _transpose_kernel
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +010084
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085 // Configure interleave kernel
86 _interleave_kernel.configure(a, &_tmp_a);
87
88 // Configure transpose kernel
89 _transpose_kernel.configure(b, &_tmp_b);
Gian Marco1d25ed52017-12-16 19:33:50 +000090
91 // Manage intermediate buffers
92 _memory_group.manage(&_tmp_a);
93 _memory_group.manage(&_tmp_b);
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010094 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +010096 _mm_kernel.configure(matrix_a, matrix_b, output, alpha, _is_interleaved_transposed);
97
98 if(_is_interleaved_transposed)
99 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 // Allocate intermediate tensors
101 _tmp_a.allocator()->allocate();
102 _tmp_b.allocator()->allocate();
103 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104
105 // Configure matrix addition kernel
106 if(beta != 0 && c != nullptr)
107 {
108 _ma_kernel.configure(c, output, beta);
109 _run_addition = true;
110 }
111}
112
113void CLGEMM::run()
114{
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100115 _memory_group.acquire();
116
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100117 if(_is_interleaved_transposed)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118 {
119 // Run interleave kernel
120 CLScheduler::get().enqueue(_interleave_kernel, false);
121
Gian Marco1d25ed52017-12-16 19:33:50 +0000122 if(_is_first_run)
123 {
124 // Run transpose kernel
125 CLScheduler::get().enqueue(_transpose_kernel, false);
126
127 _is_first_run = false;
128 }
129 else if(!_reshape_b_only_on_first_run)
130 {
131 // Run transpose kernel
132 CLScheduler::get().enqueue(_transpose_kernel, false);
133 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 }
135
136 // Run matrix multiply kernel
137 CLScheduler::get().enqueue(_mm_kernel, !_run_addition);
138
139 // Run matrix addition kernel
140 if(_run_addition)
141 {
142 CLScheduler::get().enqueue(_ma_kernel);
143 }
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100144
145 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146}