blob: 5fe198f4553b71bdd1f9f18ae75970eca9a81e61 [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"
41#include "arm_compute/core/NEON/kernels/assembly/kernels/a64_gemm_s8_12x8.hpp"
42} // namespace arm_compute
43
Pablo Tellobf2fb952017-09-29 16:43:25 +010044#include <arm_neon.h>
45#include <cstddef>
46#include <cstdint>
47
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +010048// Enable only if compiled for AArch64-V8.2-A targets
49#ifdef ARM_COMPUTE_AARCH64_V8_2
50
Pablo Tellobf2fb952017-09-29 16:43:25 +010051namespace arm_compute
52{
Pablo Tello6ff12a02017-11-02 16:09:35 +000053void NEGEMMLowpAArch64V8P4Kernel::internal_configure(const ITensor *input0, const ITensor *input1, ITensor *output, ITensor *workspace, float alpha, float beta, bool transform_0, bool transform_1)
Pablo Tellobf2fb952017-09-29 16:43:25 +010054{
Pablo Tello6ff12a02017-11-02 16:09:35 +000055 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::S8);
56 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32);
Pablo Tellobf2fb952017-09-29 16:43:25 +010057 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1);
58
Pablo Tello6ff12a02017-11-02 16:09:35 +000059 _input0 = input0;
60 _input1 = input1;
61 _output = output;
62 _workspace = workspace;
63 _alpha = alpha;
64 _beta = beta;
65 _transform_0 = transform_0;
66 _transform_1 = transform_1;
Pablo Tellobf2fb952017-09-29 16:43:25 +010067
68 // Configure kernel window
69 Window win = calculate_max_window(*output->info());
70
71 AccessWindowRectangle output_access(output->info(), 0, 0, 12, 8);
72
73 const int input0_access_end = ceil_to_multiple(input0->info()->tensor_shape().x(), 8);
74 const int input1_access_end = ceil_to_multiple(input1->info()->tensor_shape().x(), 12);
75
76 update_window_and_padding(win,
77 AccessWindowStatic(input0->info(), 0, 0, input0_access_end, input0->info()->tensor_shape().y()),
78 AccessWindowStatic(input1->info(), 0, 0, input1_access_end, input1->info()->tensor_shape().y()),
79 output_access);
80
81 INEKernel::configure(win);
82}
83
84bool NEGEMMLowpAArch64V8P4Kernel::is_parallelisable() const
85{
86 return false;
87}
88
Pablo Tellobf2fb952017-09-29 16:43:25 +010089void NEGEMMLowpAArch64V8P4Kernel::run(const Window &window, const ThreadInfo &info)
90{
Pablo Tellobf2fb952017-09-29 16:43:25 +010091 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
92 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
93
Pablo Tello6ff12a02017-11-02 16:09:35 +000094 const int lda = _input0->info()->strides_in_bytes().y();
95 const int ldb = _input1->info()->strides_in_bytes().y();
96 const int ldc = _output->info()->strides_in_bytes().y() / sizeof(int32_t);
Pablo Tellobf2fb952017-09-29 16:43:25 +010097
Pablo Tello6ff12a02017-11-02 16:09:35 +000098 const auto in1_ptr = reinterpret_cast<const int8_t *>(_input1->buffer());
Pablo Tellobf2fb952017-09-29 16:43:25 +010099
Pablo Tello6ff12a02017-11-02 16:09:35 +0000100 const int M = std::min(_output->info()->tensor_shape().y(), static_cast<size_t>(window.y().end())) - window.y().start();
101 const int N = _output->info()->tensor_shape().x();
102 const int K = _input0->info()->tensor_shape().x();
103
104 // Only iterate over batches
105 Window win(window);
106 win.set(0, Window::Dimension(0, 1, 1));
107 win.set(1, Window::Dimension(0, 1, 1));
108
109 Iterator in0(_input0, window);
110 Iterator out(_output, window);
111
112 GemmInterleaved<gemm_s8_12x8, int8_t, int32_t> gemm(&info.cpu_info, M, N, K, !_transform_1, !_transform_1);
113
114 constexpr size_t alignment = 4096;
115 const size_t offset = (gemm.get_working_size() + alignment - 1) * info.thread_id;
116 void *workspace = _workspace->buffer() + offset;
117 size_t workspace_size = _workspace->info()->total_size();
118
119 if(support::cpp11::align(alignment, gemm.get_working_size(), workspace, workspace_size) == nullptr)
Pablo Tellobf2fb952017-09-29 16:43:25 +0100120 {
Pablo Tello6ff12a02017-11-02 16:09:35 +0000121 ARM_COMPUTE_ERROR("Not enough space to align buffer!");
Pablo Tellobf2fb952017-09-29 16:43:25 +0100122 }
123
Pablo Tello6ff12a02017-11-02 16:09:35 +0000124 execute_window_loop(win, [&](const Coordinates & id)
Pablo Tellobf2fb952017-09-29 16:43:25 +0100125 {
Pablo Tello6ff12a02017-11-02 16:09:35 +0000126 gemm.execute(reinterpret_cast<const int8_t *>(in0.ptr()), lda,
127 reinterpret_cast<const int8_t *>(in1_ptr), ldb,
128 reinterpret_cast<int32_t *>(out.ptr()), ldc,
129 _alpha, _beta, workspace);
130 },
131 in0, out);
Pablo Tellobf2fb952017-09-29 16:43:25 +0100132}
133} // namespace arm_compute
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100134#endif /* ARM_COMPUTE_AARCH64_V8_2 */