blob: d4fcf5e3cb45d7a72efaf891e5f4c49a422ce72a [file] [log] [blame]
Pablo Tellobf2fb952017-09-29 16:43:25 +01001/*
Pablo Tellof6169d82018-02-02 10:05:50 +00002 * Copyright (c) 2017-2018 ARM Limited.
Pablo Tellobf2fb952017-09-29 16:43:25 +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/core/NEON/kernels/arm64/NEGEMMLowpAArch64V8P4Kernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/IAccessWindow.h"
30#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Types.h"
33#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
36#include "support/ToolchainSupport.h"
37
Pablo Tello6ff12a02017-11-02 16:09:35 +000038namespace arm_compute
39{
40#include "arm_compute/core/NEON/kernels/assembly/gemm_interleaved.hpp"
Pablo Tellofef6dae2017-12-15 10:36:21 +000041#include "arm_compute/core/NEON/kernels/assembly/kernels/a64_gemm_s8_12x8.hpp"
Pablo Tello6681d242017-11-13 16:44:08 +000042#include "arm_compute/core/NEON/kernels/assembly/kernels/a64_gemm_u8_12x8.hpp"
Pablo Tello6ff12a02017-11-02 16:09:35 +000043} // namespace arm_compute
44
Pablo Tellobf2fb952017-09-29 16:43:25 +010045#include <arm_neon.h>
46#include <cstddef>
47#include <cstdint>
48
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +010049// Enable only if compiled for AArch64-V8.2-A targets
50#ifdef ARM_COMPUTE_AARCH64_V8_2
51
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000052namespace
53{
54using namespace arm_compute;
55
Georgios Pinitas631c41a2017-12-06 11:53:03 +000056Status validate_arguments(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000057{
Pablo Tellofef6dae2017-12-15 10:36:21 +000058 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::QASYMM8, DataType::U8, DataType::S8);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000059 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
60 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1);
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1);
62
Georgios Pinitas631c41a2017-12-06 11:53:03 +000063 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000064}
65
Georgios Pinitas631c41a2017-12-06 11:53:03 +000066std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input0, ITensorInfo *input1, ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000067{
68 // Configure kernel window
69 Window win = calculate_max_window(*output);
70
71 AccessWindowRectangle output_access(output, 0, 0, 12, 8);
72
73 const int input0_access_end = ceil_to_multiple(input0->tensor_shape().x(), 8);
74 const int input1_access_end = ceil_to_multiple(input1->tensor_shape().x(), 12);
75
76 bool window_changed = update_window_and_padding(win,
77 AccessWindowStatic(input0, 0, 0, input0_access_end, input0->tensor_shape().y()),
78 AccessWindowStatic(input1, 0, 0, input1_access_end, input1->tensor_shape().y()),
79 output_access);
80
Georgios Pinitas631c41a2017-12-06 11:53:03 +000081 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000082 return std::make_pair(err, win);
83}
Pablo Tellofef6dae2017-12-15 10:36:21 +000084
85template <typename strategy, typename To, typename Tr>
86void *align_workspace(GemmInterleaved<strategy, To, Tr> &gemm, const ThreadInfo &info, ITensor *ws)
87{
88 constexpr size_t alignment = 4096;
89 const size_t offset = (gemm.get_working_size() + alignment - 1) * info.thread_id;
90 void *workspace = ws->buffer() + offset;
91 size_t workspace_size = ws->info()->total_size();
92
93 if(support::cpp11::align(alignment, gemm.get_working_size(), workspace, workspace_size) == nullptr)
94 {
95 ARM_COMPUTE_ERROR("Not enough space to align buffer!");
96 }
97 return workspace;
98}
99
100template <typename strategy>
101void execute_gemm(const Window &win, Iterator &in0, Iterator &in1, Iterator &out,
102 const ThreadInfo &info, ITensor *ws, int M, int N, int K, bool is_transposed_0, bool is_transposed_1,
103 int lda, int ldb, int ldc, float alpha, float beta)
104{
Pablo Tellof6169d82018-02-02 10:05:50 +0000105 ARM_COMPUTE_UNUSED(M);
106 ARM_COMPUTE_UNUSED(N);
107 ARM_COMPUTE_UNUSED(K);
108 ARM_COMPUTE_UNUSED(is_transposed_0);
109 ARM_COMPUTE_UNUSED(is_transposed_1);
Pablo Tellofef6dae2017-12-15 10:36:21 +0000110 GemmInterleaved<strategy, typename strategy::operand_type, typename strategy::result_type> gemm(&info.cpu_info, M, N, K, is_transposed_0, is_transposed_1);
111 void *workspace = align_workspace(gemm, info, ws);
112 execute_window_loop(win, [&](const Coordinates & id)
113 {
114 gemm.execute(reinterpret_cast<const typename strategy::operand_type *>(in0.ptr()), lda,
115 reinterpret_cast<const typename strategy::operand_type *>(in1.ptr()), ldb,
116 reinterpret_cast<typename strategy::result_type *>(out.ptr()), ldc,
117 alpha, beta, workspace);
118 },
119 in0, out);
120}
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000121} // namespace
122
Pablo Tellobf2fb952017-09-29 16:43:25 +0100123namespace arm_compute
124{
Georgios Pinitas08c5a062017-12-14 17:53:39 +0000125void NEGEMMLowpAArch64V8P4Kernel::internal_configure(const ITensor *input0, const ITensor *input1, ITensor *output, ITensor *workspace, float alpha, float beta, bool is_transposed_0,
126 bool is_transposed_1)
Pablo Tellobf2fb952017-09-29 16:43:25 +0100127{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000128 // Perform validate step
129 ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
130 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(), input1->info(), output->info()));
Pablo Tellobf2fb952017-09-29 16:43:25 +0100131
Georgios Pinitas08c5a062017-12-14 17:53:39 +0000132 _input0 = input0;
133 _input1 = input1;
134 _output = output;
135 _workspace = workspace;
136 _alpha = alpha;
137 _beta = beta;
138 _is_transposed_0 = is_transposed_0;
139 _is_transposed_1 = is_transposed_1;
Pablo Tellobf2fb952017-09-29 16:43:25 +0100140
141 // Configure kernel window
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000142 auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info());
143 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
144 INEKernel::configure(win_config.second);
145}
Pablo Tellobf2fb952017-09-29 16:43:25 +0100146
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000147Status NEGEMMLowpAArch64V8P4Kernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000148{
149 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output));
150 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input0->clone().get(), input1->clone().get(), output->clone().get()).first);
Pablo Tellobf2fb952017-09-29 16:43:25 +0100151
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000152 return Status{};
Pablo Tellobf2fb952017-09-29 16:43:25 +0100153}
154
Pablo Tellobf2fb952017-09-29 16:43:25 +0100155void NEGEMMLowpAArch64V8P4Kernel::run(const Window &window, const ThreadInfo &info)
156{
Pablo Tellobf2fb952017-09-29 16:43:25 +0100157 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
158 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
159
Pablo Tello6ff12a02017-11-02 16:09:35 +0000160 const int lda = _input0->info()->strides_in_bytes().y();
161 const int ldb = _input1->info()->strides_in_bytes().y();
Pablo Tello6681d242017-11-13 16:44:08 +0000162 const int ldc = _output->info()->strides_in_bytes().y() / sizeof(uint32_t);
Pablo Tellobf2fb952017-09-29 16:43:25 +0100163
Pablo Tello6ff12a02017-11-02 16:09:35 +0000164 const int M = std::min(_output->info()->tensor_shape().y(), static_cast<size_t>(window.y().end())) - window.y().start();
165 const int N = _output->info()->tensor_shape().x();
166 const int K = _input0->info()->tensor_shape().x();
167
168 // Only iterate over batches
169 Window win(window);
170 win.set(0, Window::Dimension(0, 1, 1));
171 win.set(1, Window::Dimension(0, 1, 1));
172
173 Iterator in0(_input0, window);
Pablo Tellofef6dae2017-12-15 10:36:21 +0000174 Iterator in1(_input1, window);
Pablo Tello6ff12a02017-11-02 16:09:35 +0000175 Iterator out(_output, window);
176
Pablo Tellofef6dae2017-12-15 10:36:21 +0000177 switch(_input0->info()->data_type())
Pablo Tellobf2fb952017-09-29 16:43:25 +0100178 {
Pablo Tellofef6dae2017-12-15 10:36:21 +0000179 case DataType::QASYMM8:
180 case DataType::U8:
181 {
182 execute_gemm<gemm_u8_12x8>(win, in0, in1, out, info, _workspace, M, N, K, _is_transposed_0, _is_transposed_1, lda, ldb, ldc, _alpha, _beta);
183 break;
184 }
185 case DataType::S8:
186 {
187 execute_gemm<gemm_s8_12x8>(win, in0, in1, out, info, _workspace, M, N, K, _is_transposed_0, _is_transposed_1, lda, ldb, ldc, _alpha, _beta);
188 break;
189 }
190 default:
191 {
192 ARM_COMPUTE_ERROR("Not supported.");
193 break;
194 }
Pablo Tellobf2fb952017-09-29 16:43:25 +0100195 }
Pablo Tellobf2fb952017-09-29 16:43:25 +0100196}
197} // namespace arm_compute
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100198#endif /* ARM_COMPUTE_AARCH64_V8_2 */