blob: 4ac6a3cae284addc6e214dad7df177b0ac4c2a99 [file] [log] [blame]
Anthony Barbier71d9b572018-07-06 17:05:59 +01001/*
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_COMPUTE_NEGEMMASSEMBLYDISPATCH_H__
25#define __ARM_COMPUTE_NEGEMMASSEMBLYDISPATCH_H__
26
27#include "arm_compute/core/NEON/kernels/assembly/NEGEMMAssemblyWrapperKernel.h"
28#include "arm_compute/runtime/IFunction.h"
29#include "arm_compute/runtime/IMemoryManager.h"
30#include "arm_compute/runtime/MemoryGroup.h"
31#include "arm_compute/runtime/Tensor.h"
32
33#include "arm_compute/core/NEON/kernels/assembly/arm_gemm.hpp"
34
35namespace arm_compute
36{
37/** Assembly kernel glue */
38template <typename TypeInput, typename TypeOutput>
39class NEGEMMAssemblyDispatch : public IFunction
40{
41public:
42 /** Default constructor */
43 NEGEMMAssemblyDispatch(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
44
45 /** Prevent instances of this class from being copy constructed */
46 NEGEMMAssemblyDispatch(const NEGEMMAssemblyDispatch<TypeInput, TypeOutput> &) = delete;
47 /** Prevent instances of this class from being copied */
48 NEGEMMAssemblyDispatch<TypeInput, TypeOutput> &operator=(const NEGEMMAssemblyDispatch<TypeInput, TypeOutput> &) = delete;
49 NEGEMMAssemblyDispatch(NEGEMMAssemblyDispatch<TypeInput, TypeOutput> &&) = default;
50 NEGEMMAssemblyDispatch<TypeInput, TypeOutput> &operator=(NEGEMMAssemblyDispatch<TypeInput, TypeOutput> &&) = default;
51 ~NEGEMMAssemblyDispatch() = default;
52
53private:
54 /** ACL Function */
55 std::unique_ptr<IFunction> _function;
56
57 //Fallback: use arm_gemm's AssemblyGemm:
58 class Fallback
59 {
60#ifndef DOXYGEN_SKIP_THIS
61 public:
62 /** Configures the arrays pointers and strides in the assembly kernel and executes the assembly kernel.
63 * The call to set_arrays is needed to deal with the input sizes containing batches (dims > 2)
64 */
65 void run();
66 void configure(const ITensor *a, const ITensor *b, ITensor *d, float alpha, float beta, bool pretranspose_hint, MemoryGroup &memory_group);
67 void prepare();
68 bool is_configured() const;
69#endif /* DOXYGEN_SKIP_THIS */
70
71 private:
72 /** Allocate a workspace tensor.
73 *
74 * @param[in] workspace_size Size to allocate.
75 * @param[in] memory_group Tensor memory group.
76 * @param[in] alignment Workspace memory alignment.
77 */
78 void allocate_workspace(size_t workspace_size, MemoryGroup *memory_group, size_t alignment);
79
80 /** Assembly Gemm kernel */
81 std::unique_ptr<arm_gemm::GemmCommon<TypeInput, TypeOutput>> _gemm_kernel_asm{ nullptr };
82 /** Optimised NEON kernel */
83 std::unique_ptr<INEKernel> _optimised_kernel{ nullptr };
84 /** Input A */
85 const ITensor *_a
86 {
87 nullptr
88 };
89 /** Input B */
90 const ITensor *_b
91 {
92 nullptr
93 };
94 /** Output */
95 ITensor *_d{ nullptr };
96 /** GEMM workspace */
97 Tensor _workspace{};
98 /** Pre-transpose tensor */
99 Tensor _pretranspose{};
100 /** Prepared flag */
101 bool _is_prepared{ false };
102 } _arm_gemm; /**< Fallback in case ACL doesn't have a function */
103 MemoryGroup _memory_group; /**< Function memory group */
104public:
105 void configure(const ITensor *a, const ITensor *b, ITensor *d, float alpha, float beta, bool pretranspose_hint);
106 bool is_configured() const;
107 // Inherited methods overridden:
108 /** Runs a preparation step, usually for pre-transposing matrix b */
109 void prepare() override;
110 void run() override;
111};
112
113/** Float 32 assembly kernel glue */
114using NEGEMMAssemblyDispatchF32 = NEGEMMAssemblyDispatch<float, float>;
115/** Uint 8 to Uint 32 kernel glue */
116using NEGEMMAssemblyDispatchU8U32 = NEGEMMAssemblyDispatch<uint8_t, uint32_t>;
117/** Int 8 to Int 32 kernel glue */
118using NEGEMMAssemblyDispatchS8S32 = NEGEMMAssemblyDispatch<int8_t, int32_t>;
119}
120#endif /* __ARM_COMPUTE_NEGEMMASSEMBLYDISPATCH_H__ */