blob: 5c58c585d7827b55c22d538dccf501dc4f2e1a29 [file] [log] [blame]
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001/*
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +01002 * Copyright (c) 2019-2020 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
64 * the row sums. If the GEMM is not pretransposed we need to store the
65 * column sums here too. */
66 size_t local_working_size() const {
67 size_t sz = subgemm_output_size() + row_sum_size();
68
69 if (_args._pretransposed_hint) {
70 return sz;
71 }
72
73 return sz + col_sum_size();
74 }
75
76 void set_child_arrays() {
77 if (working_space == nullptr || arrays_set == false)
78 return;
79
80 /* Use the first part of our working space for the subgemm result, pass the operand details straight through. */
81 _subgemm->set_arrays(this->_Aptr, this->_lda, this->_A_batch_stride, this->_A_multi_stride,
82 this->_Bptr, this->_ldb, this->_B_multi_stride,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010083 reinterpret_cast<Tgemm *>(working_space), _args._Nsize, (_args._Nsize * _args._Msize), (_args._Nsize * _args._Msize * _args._nbatches),
84 nullptr, 0);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010085 }
86
87 void col_sums_pretransposed(const To *B, const int ldb, const int B_multi_stride) {
88 for (unsigned int multi=0; multi<_args._nmulti; multi++) {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010089 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 +010090 }
91 }
92
93 void col_sums_runtime(unsigned int threadid) {
94 unsigned int first_col = (threadid * _args._Nsize) / _args._maxthreads;
95 unsigned int last_col = ((threadid + 1) * _args._Nsize) / _args._maxthreads;
96
97 for (unsigned int multi=0; multi<_args._nmulti; multi++) {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +010098 compute_col_sums(_params, (last_col - first_col), _args._Ksize, this->_Bptr + (multi * this->_B_multi_stride) + first_col, this->_ldb, _col_sums + (multi * _args._Nsize) + first_col, _args._Ksize, multi, first_col);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +010099 }
100 }
101
102 void requantize_runtime(unsigned int threadid) {
103 unsigned int first_row = (threadid * _args._Msize) / _args._maxthreads;
104 unsigned int last_row = ((threadid+1) * _args._Msize) / _args._maxthreads;
105
106 for (unsigned int multi=0; multi<_args._nmulti; multi++) {
107 for (unsigned int batch=0; batch<_args._nbatches; batch++) {
108 /* Compute row sums now */
109 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),
110 this->_lda, _row_sums + (multi * _args._nbatches * _args._Msize) + (batch * _args._Msize) + first_row);
111 // If we don't care about negative values, call the version of this function that doesn't correct before shifting.
112 // '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.
113 requantize_block_32(_params, _args._Nsize, (last_row - first_row),
114 reinterpret_cast<Tgemm *>(working_space) + (multi * (_args._Msize * _args._Nsize * _args._nbatches)) + (batch * (_args._Msize * _args._Nsize)) + (first_row * _args._Nsize),
115 _args._Nsize,
116 this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (first_row * this->_ldc), this->_ldc,
117 _row_sums + (multi * _args._nbatches * _args._Msize) + (batch * _args._Msize) + first_row,
Georgios Pinitasaf56d522020-07-01 12:35:30 +0100118 _col_sums + (multi * _args._Nsize), 0);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100119 }
120 }
121 }
122
123
124public:
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100125 QuantizeWrapper(const QuantizeWrapper &) = delete;
126 QuantizeWrapper operator=(const QuantizeWrapper &) = delete;
127
Michalis Spyrou71ac9032019-11-14 14:31:44 +0000128 QuantizeWrapper(const GemmArgs &args, const Requantize32 &qp) : _params(qp), _args(args), _barrier(args._maxthreads) {
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100129 GemmArgs newargs = GemmArgs(args._ci, args._Msize, args._Nsize, args._Ksize, args._nbatches, args._nmulti, args._trA, args._trB, Activation(), args._maxthreads, args._pretransposed_hint, nullptr);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100130 _subgemm = gemm<To, Tgemm>(newargs);
131
132 if (_subgemm == nullptr) {
133 return;
134 }
135
136 if (!_subgemm->B_is_pretransposed()) {
137 _args._pretransposed_hint = false;
138 }
139 }
140
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100141 void set_arrays(const To *A, const int lda, const int A_batch_stride, const int A_multi_stride,
142 const To *B, const int ldb, const int B_multi_stride,
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100143 Tr *C, const int ldc, const int C_batch_stride, const int C_multi_stride,
144 const Tr *bias, const int bias_multi_stride) override {
145 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 +0100146
147 arrays_set = true;
148 set_child_arrays();
149 }
150
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000151 ndrange_t get_window_size() const override {
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100152 return { _subgemm->get_window_size() };
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100153 }
154
155 void set_nthreads(int nthreads) override {
156 _subgemm->set_nthreads(nthreads);
157 _barrier.set_nthreads(nthreads);
158 _args._maxthreads = nthreads;
159 }
160
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +0100161 void execute(const ndcoord_t &work_range, const ndcoord_t &thread_locator, int threadid) override {
Joseph Dobson6f8b17d2020-02-11 19:32:11 +0000162 _subgemm->execute(work_range, thread_locator, threadid);
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100163 if (!_args._pretransposed_hint) {
164 col_sums_runtime(threadid);
165 }
166
167 _barrier.arrive_and_wait();
168
169 requantize_runtime(threadid);
170 }
171
172 size_t get_working_size() const override {
173 return _subgemm->get_working_size() + local_working_size();
174 }
175
176 // Space arrangement:
177
178 // ptr
179 // V
180 // | subgemm output | row_sums | col_sums (if not pretransposed | subgemm working space |
181 void set_working_space(void *space) override {
182 uintptr_t space_int = reinterpret_cast<uintptr_t>(space);
183
184 working_space = space;
185 _subgemm->set_working_space(reinterpret_cast<void *>(space_int + local_working_size()));
186
187 _row_sums = reinterpret_cast<int32_t *>(space_int + subgemm_output_size());
188 if (!_args._pretransposed_hint) {
189 _col_sums = reinterpret_cast<int32_t *>(space_int + subgemm_output_size() + row_sum_size());
190 }
191
192 set_child_arrays();
193 }
194
195 bool B_is_pretransposed() const override {
196 /* We clear this flag if the subgemm isn't pretransposed, so just return its value */
197 return _args._pretransposed_hint;
198 }
199
200 bool B_pretranspose_required() const override {
201 return _subgemm->B_pretranspose_required();
202 }
203
204 size_t get_B_pretransposed_array_size() const override {
205 if (_args._pretransposed_hint) {
206 return _subgemm->get_B_pretransposed_array_size() + col_sum_size();
207 }
208
209 return 0;
210 }
211
212 void pretranspose_B_array(void *buffer, const To *B, const int ldb, const int B_multi_stride) override {
213 if (!_args._pretransposed_hint) {
214 return;
215 }
216
217 uintptr_t buffer_int = reinterpret_cast<uintptr_t>(buffer);
218 _subgemm->pretranspose_B_array(reinterpret_cast<void *>(buffer_int + col_sum_size()), B, ldb, B_multi_stride);
219
220 _col_sums = reinterpret_cast<int32_t *>(buffer);
221
222 col_sums_pretransposed(B, ldb, B_multi_stride);
223 }
224
225 void set_pretransposed_B_data(void *buffer) override {
226 if (!_args._pretransposed_hint) {
227 return;
228 }
229
230 uintptr_t buffer_int = reinterpret_cast<uintptr_t>(buffer);
231 _subgemm->set_pretransposed_B_data(reinterpret_cast<void *>(buffer_int + col_sum_size()));
232 _col_sums = reinterpret_cast<int32_t *>(buffer);
233 }
234
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100235 void set_quantized_bias(const int32_t *bias, size_t bias_multi_stride) override {
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100236 _params.bias = bias;
Georgios Pinitas48b3ef82019-10-14 19:03:09 +0100237 _params.bias_multi_stride = bias_multi_stride;
Georgios Pinitascfa2bba2019-06-27 17:00:52 +0100238 }
239};
240
241} // namespace arm_gemm