blob: 27dd6c51d70f323968221fe0aac7610b79579dec [file] [log] [blame]
Pablo Telloeb82fd22018-02-23 13:43:50 +00001/* Copyright (c) 2017-2018 ARM Limited.
Pablo Tello181e6512017-11-15 13:28:27 +00002 *
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23#include "arm_compute/runtime/NEON/functions/NEGEMMLowpAssemblyMatrixMultiplyCore.h"
24
25#include "arm_compute/core/Error.h"
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/ITensor.h"
Pablo Tello181e6512017-11-15 13:28:27 +000028#include "arm_compute/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
29#include "arm_compute/core/NEON/kernels/NEGEMMLowpMatrixMultiplyKernel.h"
30#include "arm_compute/core/NEON/kernels/NEGEMMTranspose1xWKernel.h"
Pablo Tello181e6512017-11-15 13:28:27 +000031#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Types.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/runtime/NEON/NEScheduler.h"
35#include "arm_compute/runtime/TensorAllocator.h"
36#include "support/ToolchainSupport.h"
37
Pablo Tello181e6512017-11-15 13:28:27 +000038using namespace arm_compute;
39
40NEGEMMLowpAssemblyMatrixMultiplyCore::NEGEMMLowpAssemblyMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)
Pablo Telloeb82fd22018-02-23 13:43:50 +000041 : _memory_group(std::move(memory_manager)), _asm_glue_unsigned(), _asm_glue_signed(), _mm_kernel(nullptr), _mtx_a_reshape_kernel(nullptr), _mtx_b_reshape_kernel(nullptr), _tmp_a(), _tmp_b(),
42 _workspace()
Pablo Tello181e6512017-11-15 13:28:27 +000043{
44}
45
46void NEGEMMLowpAssemblyMatrixMultiplyCore::configure(const ITensor *a, const ITensor *b, ITensor *output)
47{
Michalis Spyrouf3dfa272017-11-21 17:52:12 +000048 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::U8, DataType::S8);
49 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32, DataType::S32);
Pablo Tello181e6512017-11-15 13:28:27 +000050 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b);
51 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");
52 ARM_COMPUTE_ERROR_ON_MSG((a)->info()->dimension(1) != (output)->info()->dimension(1), "The output matrix must have the same number of rows as the matrix A");
53 ARM_COMPUTE_ERROR_ON_MSG((b)->info()->dimension(0) != (output)->info()->dimension(0), "The output matrix must have the same number of columns as the matrix B");
54
Pablo Telloeb82fd22018-02-23 13:43:50 +000055 bool run_optimised = false;
Pablo Tello181e6512017-11-15 13:28:27 +000056#ifdef __aarch64__
Pablo Telloeb82fd22018-02-23 13:43:50 +000057 switch(a->info()->data_type())
58 {
59 case DataType::S8:
60 {
Pablo Tello7fad9b12018-03-14 17:55:27 +000061 run_optimised = setup_assembly_kernel(a, b, output, 1.f, 1.f, _workspace, _memory_group, _asm_glue_signed);
Pablo Telloeb82fd22018-02-23 13:43:50 +000062 break;
63 }
Pablo Tello66c656a2018-03-15 10:34:58 +000064 case DataType::QASYMM8:
Pablo Telloeb82fd22018-02-23 13:43:50 +000065 case DataType::U8:
66 {
Pablo Tello7fad9b12018-03-14 17:55:27 +000067 run_optimised = setup_assembly_kernel(a, b, output, 1.f, 1.f, _workspace, _memory_group, _asm_glue_unsigned);
Pablo Telloeb82fd22018-02-23 13:43:50 +000068 break;
69 }
70 default:
71 {
72 ARM_COMPUTE_ERROR("Datatype not supported");
73 break;
74 }
75 }
Pablo Tello181e6512017-11-15 13:28:27 +000076#endif /* __aarch64__ */
Pablo Telloeb82fd22018-02-23 13:43:50 +000077 if(!run_optimised)
Pablo Tello181e6512017-11-15 13:28:27 +000078 {
79 // The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ]
80 TensorShape shape_tmp_a = a->info()->tensor_shape();
81 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
82 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.f));
83
84 // The transpose1xW output matrix will have the following shape: [ b_height * 16, ceil(b_width / 16.0f) ]
85 TensorShape shape_tmp_b = b->info()->tensor_shape();
86 shape_tmp_b.set(0, b->info()->dimension(1) * 16);
87 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / 16.f));
88
89 TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type());
90 TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type());
91 _tmp_a.allocator()->init(info_a);
92 _tmp_b.allocator()->init(info_b);
93 _memory_group.manage(&_tmp_a);
94 _memory_group.manage(&_tmp_b);
95
96 // Configure interleave kernel
97 {
98 auto k = arm_compute::support::cpp14::make_unique<NEGEMMInterleave4x4Kernel>();
99 k->configure(a, &_tmp_a);
100 _mtx_a_reshape_kernel = std::move(k);
101 }
102
103 // Configure transpose kernel
104 {
105 auto k = arm_compute::support::cpp14::make_unique<NEGEMMTranspose1xWKernel>();
106 k->configure(b, &_tmp_b);
107 _mtx_b_reshape_kernel = std::move(k);
108 }
109
110 // Configure matrix multiply kernel
111 {
112 auto k = arm_compute::support::cpp14::make_unique<NEGEMMLowpMatrixMultiplyKernel>();
113 k->configure(&_tmp_a, &_tmp_b, output);
114 _mm_kernel = std::move(k);
115 }
116
117 // Allocate tensors
118 _tmp_a.allocator()->allocate();
119 _tmp_b.allocator()->allocate();
120 }
121}
122
123void NEGEMMLowpAssemblyMatrixMultiplyCore::run()
124{
125 _memory_group.acquire();
126 if(_mtx_a_reshape_kernel)
127 {
128 NEScheduler::get().schedule(_mtx_a_reshape_kernel.get(), Window::DimY);
129 }
130
131 if(_mtx_b_reshape_kernel)
132 {
133 NEScheduler::get().schedule(_mtx_b_reshape_kernel.get(), Window::DimY);
134 }
135
Pablo Telloeb82fd22018-02-23 13:43:50 +0000136 if(_asm_glue_unsigned._optimised_kernel != nullptr)
137 {
138 _asm_glue_unsigned.run();
139 }
140 else if(_asm_glue_signed._optimised_kernel != nullptr)
141 {
142 _asm_glue_signed.run();
143 }
144 else
145 {
146 NEScheduler::get().schedule(_mm_kernel.get(), Window::DimY);
147 }
Pablo Tello181e6512017-11-15 13:28:27 +0000148
149 _memory_group.release();
150}