blob: 9168ed432717c1c0bddc4faf1be38429b8ebf801 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Anthony Barbierf1df3462018-01-31 09:13:37 +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/NEON/functions/NEGEMM.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
Pablo Telloeb82fd22018-02-23 13:43:50 +000032#include "arm_compute/runtime/NEON/AssemblyHelper.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/runtime/NEON/NEScheduler.h"
34#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010035#include "support/ToolchainSupport.h"
36
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037#include <cmath>
38
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010039namespace arm_compute
40{
Georgios Pinitas658039b2017-09-15 16:30:50 +010041NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitas932b5612018-05-03 13:44:35 +010042 : _memory_group(std::move(memory_manager)), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _asm_glue(), _ma_kernel(), _tmp_a(), _tmp_b(), _workspace(), _B_pretransposed(),
Gian Marco1d25ed52017-12-16 19:33:50 +000043 _run_vector_matrix_multiplication(false), _run_addition(false), _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 NEGEMM::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048{
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +010049 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::F32, DataType::F16, DataType::QS8, DataType::QS16);
Gian Marco Iodice3a3066b2017-06-23 13:38:14 +010050 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, d);
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +010051 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");
Gian Marco1d25ed52017-12-16 19:33:50 +000052 ARM_COMPUTE_ERROR_ON_MSG(gemm_info.is_a_reshaped(), "Matrix A already reshaped is not supported");
53 ARM_COMPUTE_ERROR_ON_MSG(gemm_info.is_b_reshaped(), "Matrix B already reshaped is not supported");
Anthony Barbier6ff3b192017-09-04 18:44:23 +010054
55 if(c != nullptr)
56 {
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +010057 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(c, 1, DataType::F32, DataType::F16, DataType::QS8, DataType::QS16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, c);
59 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");
60 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");
61 ARM_COMPUTE_ERROR_ON_MSG(c->info()->dimension(0) != d->info()->dimension(0), "The C matrix must have the same number of rows as the output matrix");
62 ARM_COMPUTE_ERROR_ON_MSG(c->info()->dimension(1) != d->info()->dimension(1), "The C matrix must have the same number of columns as the output matrix");
63 }
64
Gian Marco1d25ed52017-12-16 19:33:50 +000065 // Check if we need to reshape the matrix B only on the first run
66 _reshape_b_only_on_first_run = gemm_info.reshape_b_only_on_first_run();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010067 _run_vector_matrix_multiplication = a->info()->dimension(1) < 2;
Pablo Tello7fad9b12018-03-14 17:55:27 +000068
Georgios Pinitas932b5612018-05-03 13:44:35 +010069 const bool run_optimised = a->info()->data_type() == DataType::F32 && (c == nullptr || beta == 0.f)
70 && setup_assembly_kernel(a, b, d, alpha, beta, _reshape_b_only_on_first_run, _workspace, _B_pretransposed, _memory_group, _asm_glue);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010072 // Check if the first input tensor is a vector.
73 // If so, all the kernels for reshaping the tensors can be skipped
74 if(_run_vector_matrix_multiplication)
75 {
Pablo Telloeb82fd22018-02-23 13:43:50 +000076 if(!run_optimised)
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +000077 {
78 // Configure the matrix multiply kernel
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000079 _mm_kernel.configure(a, b, d, alpha, false);
Michele Di Giorgio5b6904b2018-01-29 12:24:14 +000080 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010081
82 // Configure matrix addition kernel
83 if(beta != 0 && c != nullptr)
84 {
85 _ma_kernel.configure(c, d, beta);
86 _run_addition = true;
87 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 }
89 else
90 {
Pablo Telloeb82fd22018-02-23 13:43:50 +000091 if(!run_optimised)
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010092 {
93 TensorShape shape_tmp_a = a->info()->tensor_shape();
94 TensorShape shape_tmp_b = b->info()->tensor_shape();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010096 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
97 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.0f));
Georgios Pinitas658039b2017-09-15 16:30:50 +010098
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +010099 const unsigned int transpose_w = 16 / data_size_from_type(b->info()->data_type());
100 shape_tmp_b.set(0, b->info()->dimension(1) * transpose_w);
101 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / static_cast<float>(transpose_w)));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100103 TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type(), a->info()->fixed_point_position());
104 TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type(), a->info()->fixed_point_position());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100106 _tmp_a.allocator()->init(info_a);
107 _tmp_b.allocator()->init(info_b);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100109 // Manage intermediate buffers
110 _memory_group.manage(&_tmp_a);
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100111 if(!_reshape_b_only_on_first_run)
112 {
113 _memory_group.manage(&_tmp_b);
114 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000116 int m = a->info()->dimension(1);
117 int n = b->info()->dimension(0);
118 int k = a->info()->dimension(0);
119
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100120 // Configure interleave kernel
121 _interleave_kernel.configure(a, &_tmp_a);
122
123 // Configure transpose kernel
124 _transpose_kernel.configure(b, &_tmp_b);
125
126 // Configure matrix multiplication kernel
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000127 _mm_kernel.configure(&_tmp_a, &_tmp_b, d, alpha, true, GEMMReshapeInfo(m, n, k));
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100128
129 // Allocate once the all configure methods have been called
130 _tmp_a.allocator()->allocate();
131 _tmp_b.allocator()->allocate();
132
133 // Configure matrix addition kernel
134 if(beta != 0 && c != nullptr)
135 {
136 _ma_kernel.configure(c, d, beta);
137 _run_addition = true;
138 }
139 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 }
141}
142
143void NEGEMM::run()
144{
Georgios Pinitas658039b2017-09-15 16:30:50 +0100145 _memory_group.acquire();
146
Pablo Telloeb82fd22018-02-23 13:43:50 +0000147 if(_asm_glue._optimised_kernel != nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 {
Pablo Telloeb82fd22018-02-23 13:43:50 +0000149 _asm_glue.run();
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100150 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100152 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100154 if(!_run_vector_matrix_multiplication)
155 {
156 // Run interleave kernel
157 NEScheduler::get().schedule(&_interleave_kernel, Window::DimY);
158
Gian Marco1d25ed52017-12-16 19:33:50 +0000159 if(_is_first_run)
160 {
161 // Run transpose kernel
162 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
163
164 _is_first_run = false;
165 }
166 else if(!_reshape_b_only_on_first_run)
167 {
168 // Run transpose kernel
169 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
170 }
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100171 }
172
173 NEScheduler::get().schedule(&_mm_kernel, _run_vector_matrix_multiplication ? Window::DimX : Window::DimY);
174
175 _memory_group.release();
176
177 // Run matrix addition kernel
178 if(_run_addition)
179 {
180 NEScheduler::get().schedule(&_ma_kernel, Window::DimY);
181 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182 }
183}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100184} // namespace arm_compute