blob: ee09ef531e4201fec610bd69a72f7f1faa5c8a97 [file] [log] [blame]
Pablo Telloeb82fd22018-02-23 13:43:50 +00001/*
2 * Copyright (c) 2018 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#ifndef __ARM_ASSEMBLY_HELPER_H__
25#define __ARM_ASSEMBLY_HELPER_H__
26
27#include "arm_compute/core/ITensor.h"
28#include "support/ToolchainSupport.h"
29
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/IAccessWindow.h"
32#include "arm_compute/core/Log.h"
33#include "arm_compute/core/NEON/kernels/assembly/NEGEMMAssemblyWrapper.h"
34#include "arm_compute/core/NEON/kernels/assembly/arm_gemm.hpp"
35#include "arm_compute/core/TensorInfo.h"
36#include "arm_compute/core/Types.h"
37#include "arm_compute/core/Validate.h"
38#include "arm_compute/core/Window.h"
39#include "arm_compute/runtime/NEON/NEScheduler.h"
40
41namespace arm_compute
42{
Alex Gildayc357c472018-03-21 13:54:09 +000043/** Assembly kernel glue */
Pablo Telloeb82fd22018-02-23 13:43:50 +000044template <typename TypeInput, typename TypeOutput>
45class AssemblyKernelGlue final
46{
47public:
Alex Gildayc357c472018-03-21 13:54:09 +000048 /** Operator type */
Pablo Telloeb82fd22018-02-23 13:43:50 +000049 using TypeOperator = TypeInput;
Alex Gildayc357c472018-03-21 13:54:09 +000050 /** Result type */
51 using TypeResult = TypeOutput;
52 /** Default constructor. */
Pablo Telloeb82fd22018-02-23 13:43:50 +000053 AssemblyKernelGlue()
Georgios Pinitas932b5612018-05-03 13:44:35 +010054 : _gemm_kernel_asm(nullptr), _optimised_kernel(nullptr), _a(nullptr), _b(nullptr), _d(nullptr), _pretranspose(nullptr)
Pablo Telloeb82fd22018-02-23 13:43:50 +000055 {
56 }
Alex Gildayc357c472018-03-21 13:54:09 +000057 /** Assembly Gemm */
Pablo Telloeb82fd22018-02-23 13:43:50 +000058 using AssemblyGemm = arm_gemm::GemmCommon<TypeInput, TypeOutput>;
59
Alex Gildayc357c472018-03-21 13:54:09 +000060 /** Prevent instances of this class from being copy constructed */
Pablo Telloeb82fd22018-02-23 13:43:50 +000061 const AssemblyKernelGlue<TypeInput, TypeOutput> &operator=(const AssemblyKernelGlue<TypeInput, TypeOutput> &) = delete;
Alex Gildayc357c472018-03-21 13:54:09 +000062 /** Prevent instances of this class from being copied */
Pablo Telloeb82fd22018-02-23 13:43:50 +000063 AssemblyKernelGlue(const AssemblyKernelGlue<TypeInput, TypeOutput> &) = delete;
64
Alex Gildayc357c472018-03-21 13:54:09 +000065 /** Assembly Gemm kernel */
Pablo Telloeb82fd22018-02-23 13:43:50 +000066 std::unique_ptr<AssemblyGemm> _gemm_kernel_asm;
Alex Gildayc357c472018-03-21 13:54:09 +000067 /** Optimised NEON kernel */
68 std::unique_ptr<INEKernel> _optimised_kernel;
69 /** Input A */
70 const ITensor *_a;
71 /** Input B */
72 const ITensor *_b;
73 /** Output */
74 ITensor *_d;
Georgios Pinitas932b5612018-05-03 13:44:35 +010075 /** Pre-transpose tensor */
76 ITensor *_pretranspose;
Pablo Telloeb82fd22018-02-23 13:43:50 +000077
78 /** Configures the arrays pointers and strides in the assembly kernel and executes the assembly kernel.
79 * The call to set_arrays is needed to deal with the input sizes containing batches (dims > 2)
80 */
81 inline void run()
82 {
83 const int lda = _a->info()->strides_in_bytes().y() / sizeof(TypeInput);
84 const int ldb = _b->info()->strides_in_bytes().y() / sizeof(TypeInput);
85 const int ldd = _d->info()->strides_in_bytes().y() / sizeof(TypeOutput);
86
Michalis Spyroue7e96e02018-04-13 13:44:10 +010087 const int batch_stride_a = _a->info()->strides_in_bytes().z() / sizeof(TypeInput);
88 const int batch_stride_d = _d->info()->strides_in_bytes().z() / sizeof(TypeOutput);
Pablo Telloeb82fd22018-02-23 13:43:50 +000089
Michalis Spyroue7e96e02018-04-13 13:44:10 +010090 const int multi_stride_a = _a->info()->strides_in_bytes()[3] / sizeof(TypeInput);
91 const int multi_stride_b = _b->info()->strides_in_bytes().z() / sizeof(TypeInput);
92 const int multi_stride_d = _d->info()->strides_in_bytes()[3] / sizeof(TypeOutput);
93
94 const auto in0_ptr = reinterpret_cast<const TypeInput *>(_a->buffer());
95 const auto in1_ptr = reinterpret_cast<const TypeInput *>(_b->buffer());
96 auto out_ptr = reinterpret_cast<TypeOutput *>(_d->buffer());
97
98 _gemm_kernel_asm->set_arrays(in0_ptr, lda, batch_stride_a, multi_stride_a, in1_ptr, ldb, multi_stride_b, out_ptr, ldd, batch_stride_d, multi_stride_d);
Georgios Pinitas932b5612018-05-03 13:44:35 +010099 if(_gemm_kernel_asm->B_pretranspose_required())
100 {
101 ARM_COMPUTE_ERROR_ON(_pretranspose == nullptr || _pretranspose->buffer() == nullptr);
102 _gemm_kernel_asm->pretranspose_B_array(reinterpret_cast<void *>(_pretranspose->buffer()), in1_ptr, ldb, multi_stride_b);
103 }
104
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100105 NEScheduler::get().schedule(_optimised_kernel.get(), Window::DimX);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000106 }
107};
108
Alex Gildayc357c472018-03-21 13:54:09 +0000109/** Float 32 assembly kernel glue */
110using AssemblyKernelGlueF32 = AssemblyKernelGlue<float, float>;
111/** Uint 8 to Uint 32 kernel glue */
Pablo Telloeb82fd22018-02-23 13:43:50 +0000112using AssemblyKernelGlueU8U32 = AssemblyKernelGlue<uint8_t, uint32_t>;
Alex Gildayc357c472018-03-21 13:54:09 +0000113/** Int 8 to Int 32 kernel glue */
Pablo Telloeb82fd22018-02-23 13:43:50 +0000114using AssemblyKernelGlueS8S32 = AssemblyKernelGlue<int8_t, int32_t>;
115
Alex Gildayc357c472018-03-21 13:54:09 +0000116/** Allocate a workspace tensor.
117 *
118 * @param[in] workspace_size Size to allocate.
119 * @param[out] workspace Tensor to allocate.
120 * @param[in] memory_group Tensor memory group.
121 * @param[in] alignment Workspace memory alignment.
122 * @param[in] num_threads Number of workspace threads.
123 */
Georgios Pinitas932b5612018-05-03 13:44:35 +0100124inline void allocate_workspace(size_t workspace_size, Tensor &workspace, MemoryGroup *memory_group, size_t alignment, unsigned int num_threads)
Pablo Telloeb82fd22018-02-23 13:43:50 +0000125{
Georgios Pinitas932b5612018-05-03 13:44:35 +0100126 ARM_COMPUTE_UNUSED(memory_group);
Pablo Telloeb82fd22018-02-23 13:43:50 +0000127 ARM_COMPUTE_ERROR_ON_MSG(workspace_size == 0, "size cannot be 0");
128 workspace.allocator()->init(TensorInfo(TensorShape{ (workspace_size + alignment - 1) * num_threads }, 1, DataType::S8));
129 workspace.allocator()->allocate();
130}
131
Alex Gildayc357c472018-03-21 13:54:09 +0000132/** Create a wrapper kernel.
133 *
Georgios Pinitas932b5612018-05-03 13:44:35 +0100134 * @param[in] a Input tensor A.
135 * @param[in] b Input tensor B.
136 * @param[out] d Output tensor.
137 * @param[in] alpha Alpha value.
138 * @param[in] beta Beta value.
139 * @param[in] pretranspose_hint Pre-transpose hint in case matrix b should be pre-transposed
140 * @param[out] workspace Workspace tensor
141 * @param[out] B_pretranspose Tensor to hold the pre-transposed B
142 * @param[in] memory_group Tensor memory group.
143 * @param[out] asm_glue Assembly glue kernel.
Alex Gildayc357c472018-03-21 13:54:09 +0000144 *
Pablo Tello7fad9b12018-03-14 17:55:27 +0000145 * @return the wrapper kernel.
Alex Gildayc357c472018-03-21 13:54:09 +0000146 */
Pablo Telloeb82fd22018-02-23 13:43:50 +0000147template <typename T>
Georgios Pinitas932b5612018-05-03 13:44:35 +0100148inline bool setup_assembly_kernel(const ITensor *a, const ITensor *b, ITensor *d, float alpha, float beta, bool pretranspose_hint,
149 Tensor &workspace, Tensor &B_pretranspose, MemoryGroup &memory_group, T &asm_glue)
Pablo Telloeb82fd22018-02-23 13:43:50 +0000150{
Pablo Tello7fad9b12018-03-14 17:55:27 +0000151 const CPUInfo &ci = NEScheduler::get().cpu_info();
152 const int M = d->info()->tensor_shape().y();
153 const int N = d->info()->tensor_shape().x();
154 const int K = a->info()->tensor_shape().x();
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100155 const int batches = a->info()->tensor_shape().total_size_upper(2);
156 const int multis = b->info()->tensor_shape().z();
Pablo Tello7fad9b12018-03-14 17:55:27 +0000157 unsigned int num_threads = NEScheduler::get().num_threads();
Michalis Spyroue7e96e02018-04-13 13:44:10 +0100158
Pablo Telloeb82fd22018-02-23 13:43:50 +0000159 // unique_ptr to a Gemm object
Pablo Tello7fad9b12018-03-14 17:55:27 +0000160 std::unique_ptr<typename T::AssemblyGemm>
Georgios Pinitas932b5612018-05-03 13:44:35 +0100161 asm_gemm(arm_gemm::gemm<typename T::TypeOperator, typename T::TypeResult>(ci, M, N, K, batches, multis, false, false, alpha, beta, num_threads, pretranspose_hint));
Pablo Telloeb82fd22018-02-23 13:43:50 +0000162 // arm_compute wrapper for the Gemm object (see above)
Pablo Tello7fad9b12018-03-14 17:55:27 +0000163 std::unique_ptr<NEGEMMAssemblyWrapper<typename T::AssemblyGemm>>
164 acl_gemm_wrapper = support::cpp14::make_unique<NEGEMMAssemblyWrapper<typename T::AssemblyGemm>>();
Pablo Telloeb82fd22018-02-23 13:43:50 +0000165 if(acl_gemm_wrapper != nullptr && asm_gemm != nullptr)
166 {
167 acl_gemm_wrapper->configure(asm_gemm.get());
168 const size_t workspace_size = asm_gemm->get_working_size();
169 if(workspace_size)
170 {
171 // Allocate workspace
Pablo Tello7fad9b12018-03-14 17:55:27 +0000172 const unsigned int alignment = 4096;
Georgios Pinitas932b5612018-05-03 13:44:35 +0100173 allocate_workspace(workspace_size, workspace, &memory_group, alignment, num_threads);
Pablo Tello7fad9b12018-03-14 17:55:27 +0000174 ARM_COMPUTE_ERROR_ON_NULLPTR(workspace.buffer());
Pablo Telloeb82fd22018-02-23 13:43:50 +0000175 asm_gemm->set_working_space(reinterpret_cast<typename T::TypeResult *>(workspace.buffer()));
176 }
Pablo Tello7fad9b12018-03-14 17:55:27 +0000177
178 //if we disable this code below in brackets then ConvLayer deadlocks when threads > 1 and
179 //the shapes are In=1x1x1024 Weights=1x1x1024x1001 Biases=1001 Out=1x1x1001
Pablo Telloeb82fd22018-02-23 13:43:50 +0000180 {
Pablo Tello7fad9b12018-03-14 17:55:27 +0000181 const unsigned int window_size = asm_gemm->get_window_size();
182 if(window_size < num_threads)
183 {
184 num_threads = window_size;
185 asm_gemm->set_nthreads(num_threads);
186 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000187 }
Pablo Tello7fad9b12018-03-14 17:55:27 +0000188
Georgios Pinitas932b5612018-05-03 13:44:35 +0100189 // Check for pre-transposed support
190 if(asm_gemm->B_pretranspose_required())
191 {
192 const size_t B_pretranspose_size = asm_gemm->get_B_pretransposed_array_size();
193 allocate_workspace(B_pretranspose_size, B_pretranspose, nullptr, 1, 1);
194 ARM_COMPUTE_ERROR_ON_NULLPTR(B_pretranspose.buffer());
195 asm_glue._pretranspose = &B_pretranspose;
196 }
197
Pablo Telloeb82fd22018-02-23 13:43:50 +0000198 asm_glue._gemm_kernel_asm = std::move(asm_gemm);
199 asm_glue._optimised_kernel = std::move(acl_gemm_wrapper);
200 // We need to setup the ptrs in the run() method
201 asm_glue._a = a;
202 asm_glue._b = b;
203 asm_glue._d = d;
204 return true;
205 }
206 return false;
207}
208}
209#endif /* __ARM_ASSEMBLY_HELPER_H__ */