blob: 5e67bc9c0e9f4794ded6a9eaec870dfd7b8de9bd [file] [log] [blame]
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001/*
2 * Copyright (c) 2019 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
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;
43 ARequantizeLayer32 _params;
44 GemmArgs<Tr> _args;
45 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,
83 reinterpret_cast<Tgemm *>(working_space), _args._Nsize, (_args._Nsize * _args._Msize), (_args._Nsize * _args._Msize * _args._nbatches));
84 }
85
86 void col_sums_pretransposed(const To *B, const int ldb, const int B_multi_stride) {
87 for (unsigned int multi=0; multi<_args._nmulti; multi++) {
88 compute_col_sums(_params, _args._Nsize, _args._Ksize, B + (multi * B_multi_stride), ldb, _col_sums + (multi * _args._Nsize), _args._Ksize, 0);
89 }
90 }
91
92 void col_sums_runtime(unsigned int threadid) {
93 unsigned int first_col = (threadid * _args._Nsize) / _args._maxthreads;
94 unsigned int last_col = ((threadid + 1) * _args._Nsize) / _args._maxthreads;
95
96 for (unsigned int multi=0; multi<_args._nmulti; multi++) {
97 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, first_col);
98 }
99 }
100
101 void requantize_runtime(unsigned int threadid) {
102 unsigned int first_row = (threadid * _args._Msize) / _args._maxthreads;
103 unsigned int last_row = ((threadid+1) * _args._Msize) / _args._maxthreads;
104
105 for (unsigned int multi=0; multi<_args._nmulti; multi++) {
106 for (unsigned int batch=0; batch<_args._nbatches; batch++) {
107 /* Compute row sums now */
108 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),
109 this->_lda, _row_sums + (multi * _args._nbatches * _args._Msize) + (batch * _args._Msize) + first_row);
110 // If we don't care about negative values, call the version of this function that doesn't correct before shifting.
111 // '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.
112 requantize_block_32(_params, _args._Nsize, (last_row - first_row),
113 reinterpret_cast<Tgemm *>(working_space) + (multi * (_args._Msize * _args._Nsize * _args._nbatches)) + (batch * (_args._Msize * _args._Nsize)) + (first_row * _args._Nsize),
114 _args._Nsize,
115 this->_Cptr + (multi * this->_C_multi_stride) + (batch * this->_C_batch_stride) + (first_row * this->_ldc), this->_ldc,
116 _row_sums + (multi * _args._nbatches * _args._Msize) + (batch * _args._Msize) + first_row,
117 _col_sums + (multi * _args._Nsize));
118 }
119 }
120 }
121
122
123public:
124 QuantizeWrapper(const GemmArgs<Tr> &args, const ARequantizeLayer32 &qp) : _params(qp), _args(args), _barrier(args._maxthreads) {
125 GemmArgs<Tgemm> newargs = GemmArgs<Tgemm>(args._ci, args._Msize, args._Nsize, args._Ksize, args._nbatches, args._nmulti, args._trA, args._trB, 1, 0, args._maxthreads, args._pretransposed_hint, nullptr);
126 _subgemm = gemm<To, Tgemm>(newargs);
127
128 if (_subgemm == nullptr) {
129 return;
130 }
131
132 if (!_subgemm->B_is_pretransposed()) {
133 _args._pretransposed_hint = false;
134 }
135 }
136
137 QuantizeWrapper(const QuantizeWrapper &) = delete;
138 QuantizeWrapper &operator=(const QuantizeWrapper &) = delete;
139 QuantizeWrapper(QuantizeWrapper &&) = default;
140 QuantizeWrapper &operator=(QuantizeWrapper &&) = default;
141
142 void set_arrays(const To *A, const int lda, const int A_batch_stride, const int A_multi_stride,
143 const To *B, const int ldb, const int B_multi_stride,
144 Tr *C, const int ldc, const int C_batch_stride, const int C_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);
146
147 arrays_set = true;
148 set_child_arrays();
149 }
150
151 unsigned int get_window_size() const override {
152 return _subgemm->get_window_size();
153 }
154
155 void set_nthreads(int nthreads) override {
156 _subgemm->set_nthreads(nthreads);
157 _barrier.set_nthreads(nthreads);
158 _args._maxthreads = nthreads;
159 }
160
161 void execute(unsigned int start, unsigned int end, int threadid) override {
162 _subgemm->execute(start, end, threadid);
163 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
235 void set_quantized_bias(const int32_t *bias) override {
236 _params.bias = bias;
237 }
238};
239
240} // namespace arm_gemm