blob: 7827bc1ccf6c0b063bc294a2290b2ac75ee4e93c [file] [log] [blame]
Pablo Tellobf2fb952017-09-29 16:43:25 +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/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{
105 GemmInterleaved<strategy, typename strategy::operand_type, typename strategy::result_type> gemm(&info.cpu_info, M, N, K, is_transposed_0, is_transposed_1);
106 void *workspace = align_workspace(gemm, info, ws);
107 execute_window_loop(win, [&](const Coordinates & id)
108 {
109 gemm.execute(reinterpret_cast<const typename strategy::operand_type *>(in0.ptr()), lda,
110 reinterpret_cast<const typename strategy::operand_type *>(in1.ptr()), ldb,
111 reinterpret_cast<typename strategy::result_type *>(out.ptr()), ldc,
112 alpha, beta, workspace);
113 },
114 in0, out);
115}
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000116} // namespace
117
Pablo Tellobf2fb952017-09-29 16:43:25 +0100118namespace arm_compute
119{
Georgios Pinitas08c5a062017-12-14 17:53:39 +0000120void NEGEMMLowpAArch64V8P4Kernel::internal_configure(const ITensor *input0, const ITensor *input1, ITensor *output, ITensor *workspace, float alpha, float beta, bool is_transposed_0,
121 bool is_transposed_1)
Pablo Tellobf2fb952017-09-29 16:43:25 +0100122{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000123 // Perform validate step
124 ARM_COMPUTE_ERROR_ON_NULLPTR(input0, input1, output);
125 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input0->info(), input1->info(), output->info()));
Pablo Tellobf2fb952017-09-29 16:43:25 +0100126
Georgios Pinitas08c5a062017-12-14 17:53:39 +0000127 _input0 = input0;
128 _input1 = input1;
129 _output = output;
130 _workspace = workspace;
131 _alpha = alpha;
132 _beta = beta;
133 _is_transposed_0 = is_transposed_0;
134 _is_transposed_1 = is_transposed_1;
Pablo Tellobf2fb952017-09-29 16:43:25 +0100135
136 // Configure kernel window
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000137 auto win_config = validate_and_configure_window(input0->info(), input1->info(), output->info());
138 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
139 INEKernel::configure(win_config.second);
140}
Pablo Tellobf2fb952017-09-29 16:43:25 +0100141
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000142Status NEGEMMLowpAArch64V8P4Kernel::validate(const ITensorInfo *input0, const ITensorInfo *input1, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000143{
144 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input0, input1, output));
145 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 +0100146
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000147 return Status{};
Pablo Tellobf2fb952017-09-29 16:43:25 +0100148}
149
Pablo Tellobf2fb952017-09-29 16:43:25 +0100150void NEGEMMLowpAArch64V8P4Kernel::run(const Window &window, const ThreadInfo &info)
151{
Pablo Tellobf2fb952017-09-29 16:43:25 +0100152 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
153 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
154
Pablo Tello6ff12a02017-11-02 16:09:35 +0000155 const int lda = _input0->info()->strides_in_bytes().y();
156 const int ldb = _input1->info()->strides_in_bytes().y();
Pablo Tello6681d242017-11-13 16:44:08 +0000157 const int ldc = _output->info()->strides_in_bytes().y() / sizeof(uint32_t);
Pablo Tellobf2fb952017-09-29 16:43:25 +0100158
Pablo Tello6ff12a02017-11-02 16:09:35 +0000159 const int M = std::min(_output->info()->tensor_shape().y(), static_cast<size_t>(window.y().end())) - window.y().start();
160 const int N = _output->info()->tensor_shape().x();
161 const int K = _input0->info()->tensor_shape().x();
162
163 // Only iterate over batches
164 Window win(window);
165 win.set(0, Window::Dimension(0, 1, 1));
166 win.set(1, Window::Dimension(0, 1, 1));
167
168 Iterator in0(_input0, window);
Pablo Tellofef6dae2017-12-15 10:36:21 +0000169 Iterator in1(_input1, window);
Pablo Tello6ff12a02017-11-02 16:09:35 +0000170 Iterator out(_output, window);
171
Pablo Tellofef6dae2017-12-15 10:36:21 +0000172 switch(_input0->info()->data_type())
Pablo Tellobf2fb952017-09-29 16:43:25 +0100173 {
Pablo Tellofef6dae2017-12-15 10:36:21 +0000174 case DataType::QASYMM8:
175 case DataType::U8:
176 {
177 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);
178 break;
179 }
180 case DataType::S8:
181 {
182 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);
183 break;
184 }
185 default:
186 {
187 ARM_COMPUTE_ERROR("Not supported.");
188 break;
189 }
Pablo Tellobf2fb952017-09-29 16:43:25 +0100190 }
Pablo Tellobf2fb952017-09-29 16:43:25 +0100191}
192} // namespace arm_compute
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100193#endif /* ARM_COMPUTE_AARCH64_V8_2 */