blob: 4ef108d4306024651cbe042ae4b1a4043403e463 [file] [log] [blame]
Anthony Barbier71d9b572018-07-06 17:05:59 +01001/*
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +00002 * Copyright (c) 2018-2022 Arm Limited.
Anthony Barbier71d9b572018-07-06 17:05:59 +01003 *
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 */
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010024#ifndef ARM_COMPUTE_CPU_INTERNAL_CPU_GEMM_ASSEMBLY_DISPATCH_H
25#define ARM_COMPUTE_CPU_INTERNAL_CPU_GEMM_ASSEMBLY_DISPATCH_H
Anthony Barbier71d9b572018-07-06 17:05:59 +010026
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010027#include "src/core/common/Macros.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010028#include "src/cpu/ICpuOperator.h"
Anthony Barbier71d9b572018-07-06 17:05:59 +010029
Anthony Barbier71d9b572018-07-06 17:05:59 +010030namespace arm_compute
31{
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010032namespace cpu
33{
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000034/* Convolution method supported by the assembly gemm interface */
35enum class AsmConvMethod
36{
37 Im2Col,
38 Indirect,
39 Conv
40};
41
42struct AsmGemmInfo
43{
44 AsmConvMethod method{ AsmConvMethod::Im2Col };
45 PadStrideInfo ps_info{};
46 ActivationLayerInfo activation_info{};
47 GEMMLowpOutputStageInfo output_stage{};
48 bool negated_offsets{ true };
49 bool reinterpret_input_as_3d{ false };
50 bool depth_output_gemm3d{ false };
51 int64_t padding_top{ 0 };
52 int64_t padding_left{ 0 };
53 float padding_value{ 0.f };
Georgios Pinitas4ee8b152021-07-16 16:16:43 +010054 bool fast_mode{ false };
Francesco.Petrogalli@arm.com5fcf22d2022-04-05 10:31:08 +000055 bool fixed_format{ false };
Francesco Petrogalli553f6952022-06-30 10:22:01 +000056 arm_gemm::WeightFormat weight_format{ arm_gemm::WeightFormat::UNSPECIFIED };
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000057};
58
Anthony Barbier71d9b572018-07-06 17:05:59 +010059/** Assembly kernel glue */
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010060class CpuGemmAssemblyDispatch : public ICpuOperator
Anthony Barbier71d9b572018-07-06 17:05:59 +010061{
62public:
Michalis Spyrou1a569a32019-09-10 17:20:34 +010063 /** Constructor */
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010064 CpuGemmAssemblyDispatch();
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +010065 /** Defautl destructor */
66 ~CpuGemmAssemblyDispatch() = default;
Anthony Barbiereaefd002018-07-20 17:49:35 +010067
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010068 ARM_COMPUTE_DISALLOW_COPY_ALLOW_MOVE(CpuGemmAssemblyDispatch);
69
Anthony Barbiereaefd002018-07-20 17:49:35 +010070 class IFallback
71 {
72 public:
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +000073 virtual void run(ITensorPack &tensors) = 0;
74 virtual void prepare(ITensorPack &tensors) = 0;
75 virtual experimental::MemoryRequirements workspace() const = 0;
76 virtual bool is_configured() const = 0;
Francesco Petrogalli553f6952022-06-30 10:22:01 +000077 virtual bool isVarWeightsKernel() const = 0;
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +000078 virtual ~IFallback() = default;
Anthony Barbiereaefd002018-07-20 17:49:35 +010079 };
Anthony Barbier71d9b572018-07-06 17:05:59 +010080
Anthony Barbier71d9b572018-07-06 17:05:59 +010081public:
Michele Di Giorgio57f30a92020-09-08 14:03:51 +010082 /** If supported create a Compute Library function else fallback to the arm_gemm function.
Anthony Barbierc8e84b52018-07-17 16:48:42 +010083 *
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000084 * @param[in] a Input tensor (Matrix A)
85 * @param[in] b Input tensor (Matrix B)
86 * @param[in] c Input tensor (Matrix C) used to pass the bias for quantized calculations
87 * @param[out] d Output tensor to store the result of matrix multiplication. Data type supported: same as @p input0.
88 * @param[in] info GEMM meta-data
Anthony Barbierc8e84b52018-07-17 16:48:42 +010089 */
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +010090 void configure(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, ITensorInfo *d, const AsmGemmInfo &info);
Anthony Barbiereaefd002018-07-20 17:49:35 +010091
92 /** Indicates whether or not this function can be used to process the given parameters.
93 *
Georgios Pinitasc0b6f762020-11-02 01:37:17 +000094 * @param[in] a Input tensor info (Matrix A)
95 * @param[in] b Input tensor info (Matrix B)
96 * @param[in] c Input tensor info (Matrix C) used to pass the bias for quantized calculations
97 * @param[in] d Output tensor to store the result of matrix multiplication. Data type supported: same as @p input0.
98 * @param[in] info GEMM meta-data
Anthony Barbiereaefd002018-07-20 17:49:35 +010099 *
100 * @return a status.
101 */
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000102 static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, const AsmGemmInfo &info);
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000103
104 /** Indicates whether or not there is an optimal assembly implementation that can be used to process the given parameters.
105 *
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000106 * This method has the same use of @ref
107 * NEGEMMConvolutionLayer::has_opt_impl, with the only caveat that
108 * the value of arm_gemm::WeightFormat need to be passed via the
109 * parameter info.
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000110 *
111 * @return a status.
112 */
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000113 static Status has_opt_impl(arm_gemm::WeightFormat &weight_format, const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *d, const AsmGemmInfo &info);
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100114 /** Checks if activation is supported by the gemm assembly dispatcher
115 *
116 * @param[in] activation Activation to check
117 *
118 * @return True if activation is supported else false
119 */
120 static bool is_activation_supported(const ActivationLayerInfo &activation);
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100121 /** Was the function successfully configured ?
122 *
123 * @return True if the function is configured and ready to run
124 */
Anthony Barbier71d9b572018-07-06 17:05:59 +0100125 bool is_configured() const;
Francesco Petrogalli553f6952022-06-30 10:22:01 +0000126 /** Indicates if the convolution executes in variable weights mode.
127 *
128 * Similar to @ref CpuGemm::isVarWeightsKernel
129 */
130 bool isVarWeightsKernel() const
131 {
132 return _arm_gemm && _arm_gemm->isVarWeightsKernel();
133 }
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000134
Anthony Barbier71d9b572018-07-06 17:05:59 +0100135 // Inherited methods overridden:
Francesco.Petrogalli@arm.come33c5562022-03-31 17:55:35 +0000136 void prepare(ITensorPack &tensors) override;
137 void run(ITensorPack &tensors) override;
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100138 experimental::MemoryRequirements workspace() const override;
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000139
140private:
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +0100141 std::unique_ptr<IFallback> _arm_gemm; /**< Interface for the arm_gemm fallback */
Anthony Barbier71d9b572018-07-06 17:05:59 +0100142};
Sang-Hoon Park4f7693d2021-05-12 13:59:10 +0100143} // namespace cpu
Anthony Barbierc8e84b52018-07-17 16:48:42 +0100144} // namespace arm_compute
Sang-Hoon Parkd89e2fa2021-05-17 17:04:50 +0100145#endif /* ARM_COMPUTE_CPU_INTERNAL_CPU_GEMM_ASSEMBLY_DISPATCH_H */