blob: e3e15478fba2b62e20ea31729a474e737f1f02b1 [file] [log] [blame]
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +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 */
Pablo Telloeb82fd22018-02-23 13:43:50 +000024#pragma once
25
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000026#include <cstring>
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010027#include <memory>
Pablo Telloeb82fd22018-02-23 13:43:50 +000028
29#include "arm_gemm_local.hpp"
30#include "gemm_common.hpp"
31
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010032namespace arm_gemm
33{
David Manselle39334c2018-07-06 17:53:35 +010034enum class GemmMethod
35{
36 DEFAULT,
37 GEMV_BATCHED,
38 GEMV_PRETRANSPOSED,
39 GEMV_NATIVE_TRANSPOSED,
40 GEMM_NATIVE,
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000041 GEMM_HYBRID,
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010042 GEMM_INTERLEAVED,
Joseph Dobson6f8b17d2020-02-11 19:32:11 +000043 GEMM_INTERLEAVED_2D,
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010044 QUANTIZE_WRAPPER,
Aleksandr Nikolaeva084b462020-06-25 12:25:52 +010045 QUANTIZE_WRAPPER_2D,
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010046 GEMM_HYBRID_QUANTIZED
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000047};
48
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000049struct KernelDescription
50{
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010051 GemmMethod method = GemmMethod::DEFAULT;
52 std::string name = "";
53 bool is_default = false;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000054
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010055 KernelDescription(GemmMethod m, std::string n, bool d = false)
56 : method(m), name(n), is_default(d)
57 {
58 }
59 KernelDescription() noexcept
60 {
61 }
David Manselle39334c2018-07-06 17:53:35 +010062};
63
64struct GemmConfig
65{
Georgios Pinitas7cd26d42019-01-09 18:35:17 +000066 GemmMethod method = GemmMethod::DEFAULT;
67 std::string filter = "";
David Manselle39334c2018-07-06 17:53:35 +010068 unsigned int inner_block_size = 0;
69 unsigned int outer_block_size = 0;
70
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010071 GemmConfig(GemmMethod method)
72 : method(method)
73 {
74 }
75 GemmConfig()
76 {
77 }
David Manselle39334c2018-07-06 17:53:35 +010078};
79
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010080struct Activation
81{
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010082 enum class Type
83 {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010084 None,
85 ReLU,
86 BoundedReLU
87 };
88
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010089 Type type;
90 float param1;
91 float param2;
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010092
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010093 Activation(Type type = Type::None, float p1 = 0.0f, float p2 = 0.0f)
94 : type(type), param1(p1), param2(p2)
95 {
96 }
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010097};
98
David Manselle39334c2018-07-06 17:53:35 +010099struct GemmArgs
100{
101public:
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000102 const CPUInfo *_ci;
103 unsigned int _Msize;
104 unsigned int _Nsize;
105 unsigned int _Ksize;
106 unsigned int _nbatches;
107 unsigned int _nmulti;
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100108 Activation _act;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000109 int _maxthreads;
Georgios Pinitas7cd26d42019-01-09 18:35:17 +0000110 const GemmConfig *_cfg;
David Manselle39334c2018-07-06 17:53:35 +0100111
112 GemmArgs(const CPUInfo *ci, const unsigned int M, const unsigned int N,
113 const unsigned int K, const unsigned int nbatches,
Georgios Pinitas0cc50ed2020-07-06 19:10:38 +0100114 const unsigned int nmulti, Activation act, const int maxthreads,
115 const GemmConfig *cfg = nullptr)
116 : _ci(ci), _Msize(M), _Nsize(N), _Ksize(K), _nbatches(nbatches), _nmulti(nmulti), _act(act), _maxthreads(maxthreads), _cfg(cfg)
David Manselle39334c2018-07-06 17:53:35 +0100117 {
118 }
119};
120
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000121struct Requantize32
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100122{
123public:
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100124 const int32_t *bias = nullptr;
125 size_t bias_multi_stride = 0;
126 int32_t a_offset = 0;
127 int32_t b_offset = 0;
128 int32_t c_offset = 0;
129 bool per_channel_requant = false;
130 int32_t per_layer_shift = 0;
131 int32_t per_layer_mul = 0;
132 const int32_t *per_channel_shifts = nullptr;
133 const int32_t *per_channel_muls = nullptr;
134 int32_t minval = 0;
135 int32_t maxval = 0;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100136
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000137 Requantize32() = default;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100138
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000139 // Constructor for per-tensor quantization
140 Requantize32(const int32_t *bias, size_t bias_multi_stride,
141 int32_t a_offset, int32_t b_offset, int32_t c_offset,
142 int32_t requant_shift, int32_t requant_mul,
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100143 int32_t minv, int32_t maxv)
144 : bias(bias), bias_multi_stride(bias_multi_stride), a_offset(a_offset), b_offset(b_offset), c_offset(c_offset), per_channel_requant(false), per_layer_shift(requant_shift), per_layer_mul(requant_mul),
145 minval(minv), maxval(maxv)
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000146 {
147 }
148
149 // Constructor for per-channel quantization
150 Requantize32(const int32_t *bias, size_t bias_multi_stride,
151 int32_t a_offset, int32_t b_offset, int32_t c_offset,
152 const int32_t *requant_shifts, const int32_t *requant_muls,
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100153 int32_t minv, int32_t maxv)
154 : bias(bias), bias_multi_stride(bias_multi_stride), a_offset(a_offset), b_offset(b_offset), c_offset(c_offset), per_channel_requant(true), per_channel_shifts(requant_shifts),
155 per_channel_muls(requant_muls), minval(minv), maxval(maxv)
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100156 {
157 }
158};
159
160struct Nothing
161{
162};
163
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100164template <typename Top, typename Tret>
165using UniqueGemmCommon = std::unique_ptr<GemmCommon<Top, Tret>>;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000166
David Manselle39334c2018-07-06 17:53:35 +0100167/* Low level API calls.
168 * These are implemented as 'GemmArgs' versions, or with the arguments explicitly listed. */
169
David Manselle39334c2018-07-06 17:53:35 +0100170/* get_gemm_method(): Given the templated types and provided parameters,
171 * which is the preferred method to implement this GEMM? */
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100172template <typename Top, typename Tret, class OutputStage = Nothing>
173KernelDescription get_gemm_method(const GemmArgs &args, const OutputStage & = {});
David Manselle39334c2018-07-06 17:53:35 +0100174
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100175template <typename Top, typename Tret, class OutputStage = Nothing>
176UniqueGemmCommon<Top, Tret> gemm(const GemmArgs &args, const OutputStage & = {});
David Manselle39334c2018-07-06 17:53:35 +0100177
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100178template <typename Top, typename Tret, class OutputStage = Nothing>
179std::vector<KernelDescription> get_compatible_kernels(const GemmArgs &args, const OutputStage & = {});
Anthony Barbier5f707732018-07-03 16:22:02 +0100180
Pablo Telloeb82fd22018-02-23 13:43:50 +0000181} // namespace arm_gemm