blob: d35825c4280981c77997751a0382f76f22d47d54 [file] [log] [blame]
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001/*
Gunes Bayiref637392024-02-12 21:32:51 +00002 * Copyright (c) 2019-2021, 2024 Arm Limited.
Georgios Pinitascfa2bba2019-06-27 17:00:52 +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 */
24
25#pragma once
26
27#include "arm_gemm.hpp"
28
29#include "barrier.hpp"
30#include "gemm_implementation.hpp"
31#include "quantized.hpp"
32
33namespace arm_gemm {
34
35/* Quantized wrapper - do an integer GEMM and wrap around the quantization. */
36
37template<typename To, typename Tr, typename Tgemm>
38class QuantizeWrapper : public GemmCommon<To, Tr> {
39private:
40 UniqueGemmCommon<To, Tgemm> _subgemm = nullptr;
41 int32_t *_row_sums = nullptr;
42 int32_t *_col_sums = nullptr;
Michalis Spyrou71ac9032019-11-14 14:31:44 +000043 Requantize32 _params;
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010044 GemmArgs _args;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010045 barrier _barrier;
46
47 void *working_space = nullptr;
48 bool arrays_set = false;
49
50 /* We need a subgemm which outputs the 32-bit intermediates - how much space is needed for that? */
51 size_t subgemm_output_size() const {
52 return (_args._Msize * _args._Nsize * _args._nbatches * _args._nmulti * sizeof(int32_t));
53 }
54
55 size_t col_sum_size() const {
56 return (_args._Nsize * _args._nmulti * sizeof(int32_t));
57 }
58
59 size_t row_sum_size() const {
60 return (_args._Msize * _args._nbatches * _args._nmulti * sizeof(int32_t));
61 }
62
63 /* Local working space: We need space for the subgemm output (above) and
Georgios Pinitas0cc50ed2020-07-06 19:10:38 +010064 * the row sums. */
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010065 size_t local_working_size() const {
Georgios Pinitas0cc50ed2020-07-06 19:10:38 +010066 return subgemm_output_size() + row_sum_size();
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010067 }
68
69 void set_child_arrays() {
70 if (working_space == nullptr || arrays_set == false)
71 return;
72
73 /* Use the first part of our working space for the subgemm result, pass the operand details straight through. */
74 _subgemm->set_arrays(this->_Aptr, this->_lda, this->_A_batch_stride, this->_A_multi_stride,
75 this->_Bptr, this->_ldb, this->_B_multi_stride,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010076 reinterpret_cast<Tgemm *>(working_space), _args._Nsize, (_args._Nsize * _args._Msize), (_args._Nsize * _args._Msize * _args._nbatches),
77 nullptr, 0);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010078 }
79
80 void col_sums_pretransposed(const To *B, const int ldb, const int B_multi_stride) {
81 for (unsigned int multi=0; multi<_args._nmulti; multi++) {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010082 compute_col_sums(_params, _args._Nsize, _args._Ksize, B + (multi * B_multi_stride), ldb, _col_sums + (multi * _args._Nsize), _args._Ksize, multi, 0);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010083 }
84 }
85
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010086 void requantize_runtime(unsigned int threadid) {
87 unsigned int first_row = (threadid * _args._Msize) / _args._maxthreads;
88 unsigned int last_row = ((threadid+1) * _args._Msize) / _args._maxthreads;
89
90 for (unsigned int multi=0; multi<_args._nmulti; multi++) {
91 for (unsigned int batch=0; batch<_args._nbatches; batch++) {
92 /* Compute row sums now */
93 compute_row_sums(_params, _args._Ksize, (last_row - first_row), this->_Aptr + (multi * this->_A_multi_stride) + (batch * this->_A_batch_stride) + (first_row * this->_lda),
94 this->_lda, _row_sums + (multi * _args._nbatches * _args._Msize) + (batch * _args._Msize) + first_row);
95 // If we don't care about negative values, call the version of this function that doesn't correct before shifting.
96 // 'c_offset' represents zero, so if the lowest possible quantized output value is the same or more than that we will not output negative numbers.
97 requantize_block_32(_params, _args._Nsize, (last_row - first_row),
98 reinterpret_cast<Tgemm *>(working_space) + (multi * (_args._Msize * _args._Nsize * _args._nbatches)) + (batch * (_args._Msize * _args._Nsize)) + (first_row * _args._Nsize),
99 _args._Nsize,
100 this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (first_row * this->_ldc), this->_ldc,
101 _row_sums + (multi * _args._nbatches * _args._Msize) + (batch * _args._Msize) + first_row,
Georgios Pinitasaf56d522020-07-01 12:35:30 +0100102 _col_sums + (multi * _args._Nsize), 0);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100103 }
104 }
105 }
106
107
108public:
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100109 QuantizeWrapper(const QuantizeWrapper &) = delete;
110 QuantizeWrapper operator=(const QuantizeWrapper &) = delete;
111
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000112 QuantizeWrapper(const GemmArgs &args, const Requantize32 &qp) : _params(qp), _args(args), _barrier(args._maxthreads) {
Georgios Pinitasc0b6f762020-11-02 01:37:17 +0000113 GemmArgs newargs = GemmArgs(args._ci, args._Msize, args._Nsize, args._Ksize, args._Ksections, args._nbatches, args._nmulti, args._indirect_input, Activation(), args._maxthreads);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100114 _subgemm = gemm<To, Tgemm>(newargs);
115
116 if (_subgemm == nullptr) {
117 return;
118 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100119 }
120
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100121 void set_arrays(const To *A, const int lda, const int A_batch_stride, const int A_multi_stride,
122 const To *B, const int ldb, const int B_multi_stride,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100123 Tr *C, const int ldc, const int C_batch_stride, const int C_multi_stride,
124 const Tr *bias, const int bias_multi_stride) override {
125 GemmCommon<To, Tr>::set_arrays(A, lda, A_batch_stride, A_multi_stride, B, ldb, B_multi_stride, C, ldc, C_batch_stride, C_multi_stride, bias, bias_multi_stride);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100126
127 arrays_set = true;
128 set_child_arrays();
129 }
130
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000131 ndrange_t get_window_size() const override {
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100132 return { _subgemm->get_window_size() };
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100133 }
134
135 void set_nthreads(int nthreads) override {
136 _subgemm->set_nthreads(nthreads);
137 _barrier.set_nthreads(nthreads);
138 _args._maxthreads = nthreads;
139 }
140
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100141 void execute(const ndcoord_t &work_range, const ndcoord_t &thread_locator, int threadid) override {
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000142 _subgemm->execute(work_range, thread_locator, threadid);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100143
144 _barrier.arrive_and_wait();
145
146 requantize_runtime(threadid);
147 }
148
149 size_t get_working_size() const override {
150 return _subgemm->get_working_size() + local_working_size();
151 }
152
153 // Space arrangement:
154
155 // ptr
156 // V
Georgios Pinitas0cc50ed2020-07-06 19:10:38 +0100157 // | subgemm output | row_sums | subgemm working space |
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100158 void set_working_space(void *space) override {
159 uintptr_t space_int = reinterpret_cast<uintptr_t>(space);
160
161 working_space = space;
162 _subgemm->set_working_space(reinterpret_cast<void *>(space_int + local_working_size()));
163
164 _row_sums = reinterpret_cast<int32_t *>(space_int + subgemm_output_size());
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100165
166 set_child_arrays();
167 }
168
169 bool B_is_pretransposed() const override {
170 /* We clear this flag if the subgemm isn't pretransposed, so just return its value */
Georgios Pinitas0cc50ed2020-07-06 19:10:38 +0100171 return _subgemm->B_is_pretransposed();
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100172 }
173
174 bool B_pretranspose_required() const override {
175 return _subgemm->B_pretranspose_required();
176 }
177
178 size_t get_B_pretransposed_array_size() const override {
Georgios Pinitas0cc50ed2020-07-06 19:10:38 +0100179 return _subgemm->get_B_pretransposed_array_size() + col_sum_size();
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100180 }
181
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100182 void requantize_bias(void *in_buffer, const To *B, const int ldb, const int B_multi_stride) override {
183 _col_sums = reinterpret_cast<int32_t *>(in_buffer);
184 col_sums_pretransposed(B, ldb, B_multi_stride);
185 }
186
Gunes Bayiref637392024-02-12 21:32:51 +0000187 void pretranspose_B_array(void *buffer, const To *B, const int ldb, const int B_multi_stride, bool transposed) override {
188 assert(!transposed);
189
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100190 uintptr_t buffer_int = reinterpret_cast<uintptr_t>(buffer);
Gunes Bayiref637392024-02-12 21:32:51 +0000191 _subgemm->pretranspose_B_array(reinterpret_cast<void *>(buffer_int + col_sum_size()), B, ldb, B_multi_stride, transposed);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100192
Giorgio Arena63e0beb2021-09-24 14:04:27 +0100193 requantize_bias(buffer, B, ldb, B_multi_stride);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100194 }
195
196 void set_pretransposed_B_data(void *buffer) override {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100197 uintptr_t buffer_int = reinterpret_cast<uintptr_t>(buffer);
198 _subgemm->set_pretransposed_B_data(reinterpret_cast<void *>(buffer_int + col_sum_size()));
199 _col_sums = reinterpret_cast<int32_t *>(buffer);
200 }
201
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100202 void set_quantized_bias(const int32_t *bias, size_t bias_multi_stride) override {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100203 _params.bias = bias;
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100204 _params.bias_multi_stride = bias_multi_stride;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100205 }
Georgios Pinitas4ee8b152021-07-16 16:16:43 +0100206
207 GemmConfig get_config() override {
208 GemmConfig c = _subgemm->get_config();
209
210 std::string n = "quantize_wrapper[";
211 n.append(c.filter);
212 n.append("]");
213
214 c.method = GemmMethod::QUANTIZE_WRAPPER;
215 c.filter = n;
216
217 return c;
218 }
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100219};
220
221} // namespace arm_gemm