Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 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/NEGEMMAssemblyDispatch.h" |
| 25 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CPP/Validate.h" |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/NEON/kernels/assembly/NEGEMMNativeWrapperKernel.h" |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 28 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/NEON/functions/NESimpleAssemblyFunction.h" |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/NEON/functions/assembly/NEGEMMInterleavedWrapper.h" |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 31 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 32 | #include <arm_neon.h> |
| 33 | |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 36 | namespace |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 37 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 38 | std::unique_ptr<IFunction> create_function_all_types(const arm_gemm::KernelDescription &gemm_kernel_info, |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 39 | const ITensor *a, const ITensor *b, ITensor *d, |
| 40 | float alpha, float beta, const GEMMInfo &gemm_info, |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 41 | std::shared_ptr<IMemoryManager> memory_manager) |
| 42 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 43 | { |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 44 | // Note: It's safe to not check for FP16 support because this was already checked in NEGEMMAssemblyDispatch::configure() |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 45 | switch(gemm_kernel_info.method) |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 46 | { |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 47 | case arm_gemm::GemmMethod::GEMM_INTERLEAVED: |
| 48 | { |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 49 | if(!gemm_info.pretranpose_B()) |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 50 | { |
| 51 | return nullptr; |
| 52 | } |
| 53 | auto function = support::cpp14::make_unique<NEGEMMInterleavedWrapper>(memory_manager); |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 54 | function->configure(a, b, d, alpha, beta, gemm_info); |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 55 | return std::move(function); |
| 56 | } |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 57 | #if defined(__aarch64__) |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 58 | case arm_gemm::GemmMethod::GEMM_NATIVE: |
| 59 | { |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 60 | if(gemm_kernel_info.name.find("sgemm_native_16x4") != std::string::npos) |
| 61 | { |
| 62 | auto kernel = support::cpp14::make_unique<NEGEMMNativeWrapperKernel<float, float>>(); |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 63 | kernel->configure(a, b, d, alpha, beta, gemm_info); |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 64 | auto function = support::cpp14::make_unique<NESimpleAssemblyFunction>(); |
| 65 | function->configure(std::move(kernel)); |
| 66 | return std::move(function); |
| 67 | } |
| 68 | return nullptr; |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 69 | } |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 70 | #endif // defined(__aarch64__) |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 71 | default: |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 72 | return nullptr; |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 76 | /** Fallback in case ACL doesn't have a function */ |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 77 | template <typename TypeInput, typename TypeOutput, class OutputStage = arm_gemm::Nothing> |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 78 | class Fallback : public NEGEMMAssemblyDispatch::IFallback |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 79 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 80 | public: |
Georgios Pinitas | 3dbfd23 | 2019-01-30 17:17:16 +0000 | [diff] [blame] | 81 | /** Initialise the functions's input and output. |
| 82 | * |
| 83 | * @param[in] a Input tensor containing the Matrix A. |
| 84 | * @param[in] b Input tensor containing the Matrix B. |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 85 | * @param[in] c Input tensor containing the Matrix C. |
Georgios Pinitas | 3dbfd23 | 2019-01-30 17:17:16 +0000 | [diff] [blame] | 86 | * @param[out] d Output tensor to store the result of matrix multiplication. |
| 87 | * @param[in] args Matrix multiplication information. |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 88 | * @param[in] gemm_info GEMM meta-data |
Georgios Pinitas | 3dbfd23 | 2019-01-30 17:17:16 +0000 | [diff] [blame] | 89 | * @param[in] memory_group Memory group to be used by the function. |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 90 | * @param[in] os Output stage meta-data. |
Georgios Pinitas | 3dbfd23 | 2019-01-30 17:17:16 +0000 | [diff] [blame] | 91 | */ |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 92 | void configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, |
| 93 | arm_gemm::GemmArgs<TypeOutput> args, const GEMMInfo &gemm_info, |
| 94 | MemoryGroup &memory_group, const OutputStage &os = {}); |
Georgios Pinitas | 3dbfd23 | 2019-01-30 17:17:16 +0000 | [diff] [blame] | 95 | |
| 96 | // Inherited methods overridden: |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 97 | void run() override; |
| 98 | void prepare() override; |
| 99 | bool is_configured() const override; |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 100 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 101 | private: |
| 102 | /** Allocate a workspace tensor. |
| 103 | * |
| 104 | * @param[in] workspace_size Size to allocate. |
| 105 | * @param[in] memory_group Tensor memory group. |
| 106 | * @param[in] alignment Workspace memory alignment. |
| 107 | */ |
Anthony Barbier | 20394d5 | 2018-08-02 11:29:09 +0100 | [diff] [blame] | 108 | void allocate_workspace(size_t workspace_size, MemoryGroup &memory_group, size_t alignment); |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 109 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 110 | /** Assembly Gemm kernel */ |
| 111 | std::unique_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{ nullptr }; |
| 112 | /** Optimised NEON kernel */ |
| 113 | std::unique_ptr<INEKernel> _optimised_kernel{ nullptr }; |
| 114 | /** Input A */ |
| 115 | const ITensor *_a |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 116 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 117 | nullptr |
| 118 | }; |
| 119 | /** Input B */ |
| 120 | const ITensor *_b |
| 121 | { |
| 122 | nullptr |
| 123 | }; |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 124 | const ITensor *_c |
| 125 | { |
| 126 | nullptr |
| 127 | }; |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 128 | /** Output */ |
| 129 | ITensor *_d{ nullptr }; |
| 130 | /** GEMM workspace */ |
| 131 | Tensor _workspace{}; |
| 132 | /** Pre-transpose tensor */ |
| 133 | Tensor _pretranspose{}; |
| 134 | /** Prepared flag */ |
| 135 | bool _is_prepared{ false }; |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 136 | /** GEMM meta-data */ |
| 137 | GEMMInfo _gemm_info{}; |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 138 | }; |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 139 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 140 | template <typename TypeInput, typename TypeOutput, class OutputStage> |
| 141 | void Fallback<TypeInput, TypeOutput, OutputStage>::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, |
| 142 | arm_gemm::GemmArgs<TypeOutput> args, const GEMMInfo &gemm_info, |
| 143 | MemoryGroup &memory_group, const OutputStage &os) |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 144 | { |
Georgios Pinitas | 3dbfd23 | 2019-01-30 17:17:16 +0000 | [diff] [blame] | 145 | arm_gemm::GemmConfig gemm_cfg; |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 146 | const arm_gemm::KernelDescription gemm_kernel_info = arm_gemm::get_gemm_method<TypeInput, TypeOutput, OutputStage>(args, os); |
Georgios Pinitas | 3dbfd23 | 2019-01-30 17:17:16 +0000 | [diff] [blame] | 147 | if(gemm_kernel_info.method != arm_gemm::GemmMethod::GEMV_BATCHED) |
| 148 | { |
| 149 | gemm_cfg.filter = gemm_kernel_info.name; |
| 150 | args._cfg = &gemm_cfg; |
| 151 | } |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 152 | _gemm_kernel_asm = arm_gemm::gemm<TypeInput, TypeOutput, OutputStage>(args, os); |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 153 | if(_gemm_kernel_asm == nullptr) |
| 154 | { |
| 155 | //configuration not supported: Leave function unconfigured: |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | // arm_compute wrapper for the Gemm object (see above) |
| 160 | std::unique_ptr<NEGEMMAssemblyWrapperKernel<TypeInput, TypeOutput>> acl_gemm_wrapper = support::cpp14::make_unique<NEGEMMAssemblyWrapperKernel<TypeInput, TypeOutput>>(); |
| 161 | ARM_COMPUTE_ERROR_ON(acl_gemm_wrapper == nullptr); |
Georgios Pinitas | 3dbfd23 | 2019-01-30 17:17:16 +0000 | [diff] [blame] | 162 | acl_gemm_wrapper->configure(_gemm_kernel_asm.get(), gemm_cfg.filter); |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 163 | const size_t workspace_size = _gemm_kernel_asm->get_working_size(); |
| 164 | if(workspace_size > 0) |
| 165 | { |
| 166 | // Allocate workspace |
| 167 | const unsigned int alignment = 4096; |
Anthony Barbier | 20394d5 | 2018-08-02 11:29:09 +0100 | [diff] [blame] | 168 | allocate_workspace(workspace_size, memory_group, alignment); |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | //if we disable this code below in brackets then ConvLayer deadlocks when threads > 1 and |
| 172 | //the shapes are In=1x1x1024 Weights=1x1x1024x1001 Biases=1001 Out=1x1x1001 |
| 173 | { |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 174 | const int window_size = _gemm_kernel_asm->get_window_size(); |
| 175 | if(window_size < args._maxthreads) |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 176 | { |
Anthony Barbier | c8e84b5 | 2018-07-17 16:48:42 +0100 | [diff] [blame] | 177 | _gemm_kernel_asm->set_nthreads(window_size); |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | _optimised_kernel = std::move(acl_gemm_wrapper); |
| 182 | _a = a; |
| 183 | _b = b; |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 184 | _c = c; |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 185 | _d = d; |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 186 | _gemm_info = gemm_info; |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 187 | // Check for pre-transposed support |
| 188 | if(_gemm_kernel_asm->B_pretranspose_required()) |
| 189 | { |
| 190 | // Forcing 128-byte alignment (required by 32-bit kernels) |
| 191 | const unsigned int alignment = 128; |
| 192 | const size_t B_pretranspose_size = _gemm_kernel_asm->get_B_pretransposed_array_size(); |
| 193 | _pretranspose.allocator()->init(TensorInfo(TensorShape{ (B_pretranspose_size + alignment /* FIXME: remove alignment after COMPMID-1088 */) }, 1, DataType::S8), alignment); |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 197 | template <typename TypeInput, typename TypeOutput, class OutputStage> |
| 198 | void Fallback<TypeInput, TypeOutput, OutputStage>::prepare() |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 199 | { |
| 200 | if(!_is_prepared) |
| 201 | { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 202 | // Setup up matrix bias in the assembly kernel, it's just a pointer to matrix C. |
| 203 | if(_c && _c->info()->data_type() == DataType::S32) |
| 204 | { |
| 205 | _gemm_kernel_asm->set_quantized_bias(reinterpret_cast<const int32_t *>(_c->buffer() + _c->info()->offset_first_element_in_bytes())); |
| 206 | } |
| 207 | |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 208 | // Pretranspose B if required |
| 209 | if(_gemm_kernel_asm->B_pretranspose_required()) |
| 210 | { |
Georgios Pinitas | ca1250d | 2018-11-22 19:38:27 +0000 | [diff] [blame] | 211 | _pretranspose.allocator()->allocate(); |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 212 | ARM_COMPUTE_ERROR_ON(_pretranspose.buffer() == nullptr); |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 213 | const int ldb = _b->info()->strides_in_bytes().y() / sizeof(TypeInput); |
Georgios Pinitas | eb84d6b | 2018-07-27 18:28:10 +0100 | [diff] [blame] | 214 | const auto in1_ptr = reinterpret_cast<const TypeInput *>(_b->buffer() + _b->info()->offset_first_element_in_bytes()); |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 215 | const int multi_stride_b = _b->info()->strides_in_bytes().z() / sizeof(TypeInput); |
| 216 | |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 217 | _gemm_kernel_asm->pretranspose_B_array(_pretranspose.buffer(), in1_ptr, ldb, multi_stride_b); |
| 218 | _b->mark_as_unused(); |
| 219 | } |
| 220 | |
| 221 | _is_prepared = true; |
| 222 | } |
| 223 | } |
| 224 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 225 | template <typename TypeInput, typename TypeOutput, class OutputStage> |
| 226 | void Fallback<TypeInput, TypeOutput, OutputStage>::allocate_workspace(size_t workspace_size, MemoryGroup &memory_group, size_t alignment) |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 227 | { |
| 228 | ARM_COMPUTE_ERROR_ON_MSG(workspace_size == 0, "size cannot be 0"); |
| 229 | _workspace.allocator()->init(TensorInfo(TensorShape{ (workspace_size + alignment /* FIXME: remove alignment after COMPMID-1088 */) }, 1, DataType::S8), alignment); |
Anthony Barbier | 20394d5 | 2018-08-02 11:29:09 +0100 | [diff] [blame] | 230 | memory_group.manage(&_workspace); |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 231 | _workspace.allocator()->allocate(); |
| 232 | } |
| 233 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 234 | template <typename TypeInput, typename TypeOutput, class OutputStage> |
| 235 | bool Fallback<TypeInput, TypeOutput, OutputStage>::is_configured() const |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 236 | { |
| 237 | return _optimised_kernel != nullptr; |
| 238 | } |
| 239 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 240 | template <typename TypeInput, typename TypeOutput, class OutputStage> |
| 241 | void Fallback<TypeInput, TypeOutput, OutputStage>::run() |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 242 | { |
| 243 | const int lda = _a->info()->strides_in_bytes().y() / sizeof(TypeInput); |
Georgios Pinitas | 40ed6d8 | 2018-07-31 17:22:11 +0100 | [diff] [blame] | 244 | int ldb = 0; |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 245 | const int ldd = _d->info()->strides_in_bytes().y() / sizeof(TypeOutput); |
| 246 | |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 247 | const size_t a_batch_idx = _gemm_info.reinterpret_input_as_3d() != 0 ? 3 : 2; |
| 248 | const size_t a_multi_idx = a_batch_idx + 1; |
| 249 | const size_t d_batch_idx = _gemm_info.depth_output_gemm3d() != 0 ? 3 : 2; |
| 250 | const size_t d_multi_idx = d_batch_idx + 1; |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 251 | |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 252 | const int batch_stride_a = _a->info()->strides_in_bytes()[a_batch_idx] / sizeof(TypeInput); |
| 253 | const int batch_stride_d = _d->info()->strides_in_bytes()[d_batch_idx] / sizeof(TypeOutput); |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 254 | |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 255 | const int multi_stride_a = _a->info()->strides_in_bytes()[a_multi_idx] / sizeof(TypeInput); |
Georgios Pinitas | 40ed6d8 | 2018-07-31 17:22:11 +0100 | [diff] [blame] | 256 | int multi_stride_b = 0; |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 257 | const int multi_stride_d = _d->info()->strides_in_bytes()[d_multi_idx] / sizeof(TypeOutput); |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 258 | |
Georgios Pinitas | 40ed6d8 | 2018-07-31 17:22:11 +0100 | [diff] [blame] | 259 | const auto in0_ptr = reinterpret_cast<const TypeInput *>(_a->buffer() + _a->info()->offset_first_element_in_bytes()); |
| 260 | const TypeInput *in1_ptr = nullptr; |
| 261 | auto out_ptr = reinterpret_cast<TypeOutput *>(_d->buffer() + _d->info()->offset_first_element_in_bytes()); |
| 262 | |
| 263 | // Check if B is pre-tranposed and de-reference if not |
| 264 | if(!_gemm_kernel_asm->B_is_pretransposed()) |
| 265 | { |
| 266 | ldb = _b->info()->strides_in_bytes().y() / sizeof(TypeInput); |
| 267 | multi_stride_b = _b->info()->strides_in_bytes().z() / sizeof(TypeInput); |
| 268 | in1_ptr = reinterpret_cast<const TypeInput *>(_b->buffer() + _b->info()->offset_first_element_in_bytes()); |
| 269 | } |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 270 | |
| 271 | // Set workspace if needed and reset number of threads as buffer manager gets re-created with max_threads |
| 272 | if(_workspace.buffer() != nullptr) |
| 273 | { |
| 274 | _gemm_kernel_asm->set_working_space(reinterpret_cast<void *>(_workspace.buffer())); |
| 275 | const unsigned int window_size = _gemm_kernel_asm->get_window_size(); |
| 276 | unsigned int num_threads = NEScheduler::get().num_threads(); |
| 277 | if(window_size < num_threads) |
| 278 | { |
| 279 | num_threads = window_size; |
| 280 | _gemm_kernel_asm->set_nthreads(num_threads); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // Prepare assembly kernel |
| 285 | prepare(); |
| 286 | |
| 287 | // Set gemm parameters |
| 288 | _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); |
| 289 | |
| 290 | // Schedule assembly kernel |
| 291 | NEScheduler::get().schedule(_optimised_kernel.get(), Window::DimX); |
| 292 | } |
| 293 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 294 | template <typename TypeInput, typename TypeOutput> |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 295 | void create_function_or_arm_gemm(std::unique_ptr<IFunction> &acl_function, std::unique_ptr<NEGEMMAssemblyDispatch::IFallback> &arm_gemm, MemoryGroup &memory_group, |
| 296 | const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info, |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 297 | std::shared_ptr<IMemoryManager> memory_manager) |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 298 | { |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 299 | INEGEMMWrapperKernel::Params p = INEGEMMWrapperKernel::extract_parameters(a, b, d, gemm_info); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 300 | const CPUInfo &ci = NEScheduler::get().cpu_info(); |
| 301 | unsigned int num_threads = NEScheduler::get().num_threads(); |
| 302 | |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 303 | arm_gemm::GemmArgs<TypeOutput> args(&ci, p.M, p.N, p.K, p.batches, p.multis, false, false, alpha, beta, num_threads, gemm_info.pretranpose_B()); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 304 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 305 | // Try to create an ACL function: |
| 306 | const arm_gemm::KernelDescription gemm_kernel_info = arm_gemm::get_gemm_method<TypeInput, TypeOutput>(args); |
| 307 | acl_function = create_function_all_types(gemm_kernel_info, a, b, d, alpha, beta, gemm_info, std::move(memory_manager)); |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 308 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 309 | // If we still don't have an ACL function: |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 310 | if(acl_function == nullptr) |
| 311 | { |
| 312 | //Fallback onto arm_gemm function if ACL doesn't support this method. |
| 313 | auto fallback = support::cpp14::make_unique<Fallback<TypeInput, TypeOutput>>(); |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 314 | fallback->configure(a, b, c, d, args, gemm_info, memory_group); |
| 315 | arm_gemm = std::move(fallback); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | template <typename TypeInput, typename TypeOutput> |
| 320 | void create_function_or_arm_gemm_quant(std::unique_ptr<IFunction> &acl_function, std::unique_ptr<NEGEMMAssemblyDispatch::IFallback> &arm_gemm, MemoryGroup &memory_group, |
| 321 | const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info, |
| 322 | std::shared_ptr<IMemoryManager> memory_manager) |
| 323 | { |
| 324 | INEGEMMWrapperKernel::Params p = INEGEMMWrapperKernel::extract_parameters(a, b, d, gemm_info); |
| 325 | const CPUInfo &ci = NEScheduler::get().cpu_info(); |
| 326 | unsigned int num_threads = NEScheduler::get().num_threads(); |
| 327 | |
| 328 | arm_gemm::GemmArgs<TypeOutput> args(&ci, p.M, p.N, p.K, p.batches, p.multis, false, false, alpha, beta, num_threads, gemm_info.pretranpose_B()); |
| 329 | |
| 330 | // Configure requantization info |
| 331 | const int32_t a_offset = -a->info()->quantization_info().uniform().offset; |
| 332 | const int32_t b_offset = -b->info()->quantization_info().uniform().offset; |
| 333 | const GEMMLowpOutputStageInfo os_info = gemm_info.gemmlowp_output_stage(); |
| 334 | |
| 335 | const arm_gemm::ARequantizeLayer32 gemm_requant_info(nullptr, |
| 336 | a_offset, b_offset, os_info.gemmlowp_offset, |
| 337 | -os_info.gemmlowp_shift, os_info.gemmlowp_multiplier, |
| 338 | os_info.gemmlowp_min_bound, os_info.gemmlowp_max_bound); |
| 339 | |
| 340 | // Try to create an ACL function: |
| 341 | const arm_gemm::KernelDescription gemm_kernel_info = arm_gemm::get_gemm_method<TypeInput, TypeOutput>(args, gemm_requant_info); |
| 342 | acl_function = create_function_all_types(gemm_kernel_info, a, b, d, alpha, beta, gemm_info, std::move(memory_manager)); |
| 343 | |
| 344 | // If we still don't have an ACL function: |
| 345 | if(acl_function == nullptr) |
| 346 | { |
| 347 | // Fallback onto arm_gemm function if ACL doesn't support this method. |
| 348 | auto fallback = support::cpp14::make_unique<Fallback<TypeInput, TypeOutput, arm_gemm::ARequantizeLayer32>>(); |
| 349 | fallback->configure(a, b, c, d, args, gemm_info, memory_group, gemm_requant_info); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 350 | arm_gemm = std::move(fallback); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | } //namespace |
| 355 | |
| 356 | NEGEMMAssemblyDispatch::NEGEMMAssemblyDispatch(std::shared_ptr<IMemoryManager> memory_manager) |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 357 | : _function(nullptr), _arm_gemm(nullptr), _memory_group(memory_manager), _memory_manager(memory_manager) |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 358 | { |
| 359 | } |
| 360 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 361 | Status NEGEMMAssemblyDispatch::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, float alpha, float beta, const GEMMInfo &gemm_info) |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 362 | { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 363 | ARM_COMPUTE_UNUSED(alpha, beta, gemm_info); |
| 364 | ARM_COMPUTE_UNUSED(c); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 365 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(a, b, d); |
| 366 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(a); |
| 367 | #ifndef __aarch64__ |
| 368 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::U8 || a->data_type() == DataType::S8 || a->data_type() == DataType::QASYMM8, "8bit integer types only supported for aarch64"); |
| 369 | #endif /* __aarch64__ */ |
| 370 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(a, 1, DataType::F32, DataType::U8, DataType::QASYMM8, DataType::S8, DataType::F16); |
| 371 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(a, b); |
| 372 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F32 && d->data_type() != DataType::F32, "Only F32 output supported for F32 input"); |
| 373 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::F16 && d->data_type() != DataType::F16, "Only F16 output supported for F16 input"); |
Anthony Barbier | 9036749 | 2018-08-01 13:56:08 +0100 | [diff] [blame] | 374 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::U8 && d->data_type() != DataType::U32, "Only U32 output supported for U8 input"); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 375 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::S8 && d->data_type() != DataType::S32, "Only S32 output supported for S8 input"); |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 376 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(a->data_type() == DataType::QASYMM8 && d->data_type() != DataType::QASYMM8, "Only QASYMM8 output supported for QASYMM8 input"); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 377 | return Status{}; |
| 378 | } |
| 379 | |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 380 | void NEGEMMAssemblyDispatch::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info) |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 381 | { |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 382 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 383 | |
| 384 | //If we don't support a combination of data types, silently return: it is the caller's responsibility to check if configure() was successful via is_configured() |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 385 | if(!NEGEMMAssemblyDispatch::validate(a->info(), b->info(), c != nullptr ? c->info() : nullptr, d->info(), alpha, beta, gemm_info)) |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 386 | { |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | switch(a->info()->data_type()) |
| 391 | { |
| 392 | case DataType::F32: |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 393 | create_function_or_arm_gemm<float, float>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 394 | break; |
| 395 | #ifdef __aarch64__ |
| 396 | case DataType::U8: |
| 397 | case DataType::QASYMM8: |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 398 | if(d->info()->data_type() == DataType::S32) |
| 399 | { |
| 400 | create_function_or_arm_gemm<uint8_t, uint32_t>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager); |
| 401 | } |
| 402 | else |
| 403 | { |
| 404 | create_function_or_arm_gemm_quant<uint8_t, uint8_t>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager); |
| 405 | } |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 406 | break; |
| 407 | case DataType::S8: |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 408 | create_function_or_arm_gemm<int8_t, int32_t>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 409 | break; |
| 410 | #endif /* __aarch64__ */ |
| 411 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 412 | case DataType::F16: |
Georgios Pinitas | cfa2bba | 2019-06-27 17:00:52 +0100 | [diff] [blame^] | 413 | create_function_or_arm_gemm<float16_t, float16_t>(_function, _arm_gemm, _memory_group, a, b, c, d, alpha, beta, gemm_info, _memory_manager); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 414 | break; |
| 415 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 416 | default: |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | void NEGEMMAssemblyDispatch::prepare() |
| 422 | { |
| 423 | if(_function != nullptr) |
| 424 | { |
| 425 | _function->prepare(); |
| 426 | } |
| 427 | else |
| 428 | { |
| 429 | ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr); |
| 430 | _arm_gemm->prepare(); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | bool NEGEMMAssemblyDispatch::is_configured() const |
| 435 | { |
| 436 | return (_arm_gemm != nullptr && _arm_gemm->is_configured()) || _function != nullptr; |
| 437 | } |
| 438 | |
| 439 | void NEGEMMAssemblyDispatch::run() |
| 440 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 441 | MemoryGroupResourceScope scope_mg(_memory_group); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 442 | if(_function != nullptr) |
| 443 | { |
| 444 | _function->run(); |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | ARM_COMPUTE_ERROR_ON(_arm_gemm == nullptr); |
| 449 | _arm_gemm->run(); |
| 450 | } |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 451 | } |
Anthony Barbier | 71d9b57 | 2018-07-06 17:05:59 +0100 | [diff] [blame] | 452 | } //namespace arm_compute |