blob: 90e47ceca086d566768a198bde73dbca57d0f2b7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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/NEON/functions/NEGEMMLowp.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Pablo Tellobf2fb952017-09-29 16:43:25 +010029#include "arm_compute/core/NEON/kernels/arm64/NEGEMMLowpAArch64V8P4Kernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/runtime/NEON/NEScheduler.h"
34#include "arm_compute/runtime/TensorAllocator.h"
Pablo Tellobf2fb952017-09-29 16:43:25 +010035#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37using namespace arm_compute;
38
Pablo Tellobf2fb952017-09-29 16:43:25 +010039#define NEGEMMLOWP_VALIDATE_DIMENSIONS(a, b, output) \
40 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN((a), 1, DataType::U8); \
41 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN((b), 1, DataType::U8); \
42 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"); \
43 ARM_COMPUTE_ERROR_ON_MSG((a)->info()->dimension(1) != (output)->info()->dimension(1), "The C matrix must have the same number of rows as the matrix A"); \
44 ARM_COMPUTE_ERROR_ON_MSG((b)->info()->dimension(0) != (output)->info()->dimension(0), "The C matrix must have the same number of columns as the matrix C");
45
Georgios Pinitas658039b2017-09-15 16:30:50 +010046NEGEMMLowp::NEGEMMLowp(std::shared_ptr<IMemoryManager> memory_manager)
Pablo Tellobf2fb952017-09-29 16:43:25 +010047 : _memory_group(std::move(memory_manager)), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _mm_optimised_kernel(nullptr), _interleave_blocked(), _interleave_blocked_transposed(), _tmp_a(),
48 _tmp_b()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049{
50}
51
Pablo Tellobf2fb952017-09-29 16:43:25 +010052void NEGEMMLowp::configure(const ITensor *a, const ITensor *b, ITensor *output)
53{
54 NEGEMMLOWP_VALIDATE_DIMENSIONS(a, b, output);
55 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32);
56
57 const struct CPUInfo ci = NEScheduler::get().cpu_info();
58 const int cpu_has_dotprod = static_cast<int>(ci.CPU) & static_cast<int>(CPUTarget::DOT);
59 if(cpu_has_dotprod != 0)
60 {
61#if defined(__aarch64__)
62 // NEGEMMLowpAArch64V8P4Kernel only compiled in AArch64 targets
63 _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMLowpAArch64V8P4Kernel>();
64 TensorShape shape_a_int = a->info()->tensor_shape();
65 shape_a_int.set(0, a->info()->dimension(0) * 8.f);
66 shape_a_int.set(1, std::ceil(a->info()->dimension(1) / 8.f));
67
68 TensorShape shape_b_int = b->info()->tensor_shape();
69 shape_b_int.set(0, b->info()->dimension(0) * 12.f);
70 shape_b_int.set(1, std::ceil(b->info()->dimension(1) / 12.f));
71
72 TensorInfo info_a_int(shape_a_int, 1, a->info()->data_type());
73 TensorInfo info_b_int(shape_b_int, 1, b->info()->data_type());
74 _tmp_a.allocator()->init(info_a_int);
75 _tmp_b.allocator()->init(info_b_int);
76
77 _memory_group.manage(&_tmp_a);
78 _memory_group.manage(&_tmp_b);
79
80 _interleave_blocked.configure(a, &_tmp_a, 8, 4, false);
81 _interleave_blocked_transposed.configure(b, &_tmp_b, 12, 4, true);
82 _mm_optimised_kernel->configure(&_tmp_a, &_tmp_b, output);
83
84 _tmp_a.allocator()->allocate();
85 _tmp_b.allocator()->allocate();
86#endif /* defined(__aarch64__) */
87 }
88 else
89 {
90 ARM_COMPUTE_ERROR("Not implemented");
91 // This is in the process of being updated, for more info please refer to COMPMID-624.
92 }
93}
94
95void NEGEMMLowp::run()
96{
97 _memory_group.acquire();
98
99 if(_mm_optimised_kernel != nullptr)
100 {
101 NEScheduler::get().schedule(&_interleave_blocked, Window::DimY);
102 NEScheduler::get().schedule(&_interleave_blocked_transposed, Window::DimY);
103 NEScheduler::get().schedule(_mm_optimised_kernel.get(), Window::DimY);
104 }
105 else
106 {
107 /* Run interleave kernel */
108 NEScheduler::get().schedule(&_interleave_kernel, Window::DimY);
109 /* Run transpose kernel */
110 NEScheduler::get().schedule(&_transpose_kernel, Window::DimY);
111 /* Run matrix multiply kernel */
112 NEScheduler::get().schedule(&_mm_kernel, Window::DimY);
113 }
114
115 _memory_group.release();
116}
117
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118void NEGEMMLowp::configure(const ITensor *a, const ITensor *b, ITensor *output, int32_t a_offset, int32_t b_offset, int32_t output_offset, int32_t output_mult_int, int32_t shift)
119{
Pablo Tellobf2fb952017-09-29 16:43:25 +0100120 NEGEMMLOWP_VALIDATE_DIMENSIONS(a, b, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, output);
Pablo Tellobf2fb952017-09-29 16:43:25 +0100122 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123
124 /* The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ] */
125 TensorShape shape_tmp_a = a->info()->tensor_shape();
126 shape_tmp_a.set(0, a->info()->dimension(0) * 4);
127 shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.f));
128
129 TensorShape shape_tmp_b = b->info()->tensor_shape();
130 shape_tmp_b.set(0, b->info()->dimension(1) * 16);
131 shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / 16.f));
132
133 TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type());
134 TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type());
135 _tmp_a.allocator()->init(info_a);
136 _tmp_b.allocator()->init(info_b);
137
Georgios Pinitas658039b2017-09-15 16:30:50 +0100138 // Manage intermediate buffers
139 _memory_group.manage(&_tmp_a);
140 _memory_group.manage(&_tmp_b);
141
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142 _interleave_kernel.configure(a, &_tmp_a);
143 _transpose_kernel.configure(b, &_tmp_b);
144 _mm_kernel.configure(&_tmp_a, &_tmp_b, output, a_offset, b_offset, output_offset, output_mult_int, shift);
145
146 _tmp_a.allocator()->allocate();
147 _tmp_b.allocator()->allocate();
148}
149
Pablo Tellobf2fb952017-09-29 16:43:25 +0100150#undef NEGEMMLOWP_VALIDATE_DIMENSIONS