blob: 08be4a5182affc832b48caca7312c754bd4c9ab8 [file] [log] [blame]
Pablo Tello299025a2017-09-29 11:30:12 +01001/*
Gian Marco Iodicebc415af2019-06-13 15:58:32 +01002 * Copyright (c) 2017-2019 ARM Limited.
Pablo Tello299025a2017-09-29 11:30:12 +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 */
Gian Marcoe75a02b2017-11-08 12:24:09 +000024#include "GEMMLowp.h"
Pablo Tello299025a2017-09-29 11:30:12 +010025
26#include "arm_compute/core/Types.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000027#include "tests/validation/reference/UtilsQuantizedAsymm.h"
Gian Marco58c57942017-11-28 09:10:03 +000028
29#include <limits>
Pablo Tello299025a2017-09-29 11:30:12 +010030
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace reference
38{
Gian Marco6b77e912017-11-17 09:27:57 +000039namespace
40{
41template <typename T>
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000042void quantize_down_int32_to_uint8_scale(const SimpleTensor<T> *in, const SimpleTensor<T> *bias, SimpleTensor<uint8_t> *dst, int32_t result_offset, std::vector<int32_t> result_mult_int,
43 std::vector<int32_t> result_shift, int32_t min, int32_t max)
Gian Marco6b77e912017-11-17 09:27:57 +000044{
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000045 const int cols_in = in->shape().x();
46 const bool is_per_channel = result_mult_int.size() > 1;
Gian Marco6b77e912017-11-17 09:27:57 +000047
48 for(int i = 0; i < in->num_elements(); ++i)
49 {
Gian Marco58c57942017-11-28 09:10:03 +000050 int32_t result = ((*in)[i] + result_offset);
Gian Marco6b77e912017-11-17 09:27:57 +000051
52 if(bias != nullptr)
53 {
54 result += (*bias)[i % cols_in];
55 }
56
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000057 result *= (is_per_channel) ? result_mult_int[i % cols_in] : result_mult_int[0];
Gian Marco58c57942017-11-28 09:10:03 +000058
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000059 result >>= (is_per_channel) ? result_shift[i % cols_in] : result_shift[0];
Gian Marco6b77e912017-11-17 09:27:57 +000060
61 // Bounded ReLu
62 if(min != max)
63 {
64 result = std::max(min, std::min(max, result));
65 }
66
67 (*dst)[i] = static_cast<uint8_t>(std::max(0, std::min(255, result)));
68 }
69}
Gian Marco58c57942017-11-28 09:10:03 +000070
71template <typename T>
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000072void quantize_down_int32_to_uint8_scale_by_fixedpoint(const SimpleTensor<T> *in, const SimpleTensor<T> *bias, SimpleTensor<uint8_t> *dst, std::vector<int32_t> result_fixedpoint_multiplier,
73 std::vector<int32_t> result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max)
Gian Marco58c57942017-11-28 09:10:03 +000074{
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000075 const int cols_in = in->shape().x();
76 const bool is_per_channel = result_fixedpoint_multiplier.size() > 1;
Gian Marco58c57942017-11-28 09:10:03 +000077
78 for(int i = 0; i < in->num_elements(); ++i)
79 {
80 int32_t result = (*in)[i];
81
82 if(bias != nullptr)
83 {
84 result += (*bias)[i % cols_in];
85 }
86
87 // Fixed point multiplication
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000088 const int32_t multiplier = (is_per_channel) ? result_fixedpoint_multiplier[i % cols_in] : result_fixedpoint_multiplier[0];
89 const int32_t shift = (is_per_channel) ? result_shift[i % cols_in] : result_shift[0];
90
91 result = asymm_rounding_divide_by_pow2(asymm_int_mult(result, multiplier), shift);
Gian Marco58c57942017-11-28 09:10:03 +000092 result += result_offset_after_shift;
93
94 // Bounded ReLu
95 if(min != max)
96 {
97 result = std::max(min, std::min(max, result));
98 }
99
100 (*dst)[i] = static_cast<uint8_t>(std::max(0, std::min(255, result)));
101 }
102}
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100103
104template <typename T>
105void quantize_down_int32_to_int16_scale_by_fixedpoint(const SimpleTensor<T> *in, const SimpleTensor<T> *bias, SimpleTensor<int16_t> *dst, int32_t result_fixedpoint_multiplier, int32_t result_shift,
106 int32_t min, int32_t max)
107{
108 const int cols_in = in->shape().x();
109
110 for(int i = 0; i < in->num_elements(); ++i)
111 {
112 int32_t result = (*in)[i];
113
114 if(bias != nullptr)
115 {
116 result += (*bias)[i % cols_in];
117 }
118
119 // Fixed point multiplication
Manuel Bottini07263982019-10-17 18:37:26 +0100120 if(result_shift < 0)
121 {
122 result = asymm_int_mult(result * (1 << (-result_shift)), result_fixedpoint_multiplier);
123 }
124 else
125 {
126 result = asymm_rounding_divide_by_pow2(asymm_int_mult(result, result_fixedpoint_multiplier), result_shift);
127 }
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100128
129 // Bounded ReLu
130 if(min != max)
131 {
132 result = std::max(min, std::min(max, result));
133 }
134
135 (*dst)[i] = static_cast<int16_t>(std::max(-32768, std::min(32767, result)));
136 }
137}
Gian Marco6b77e912017-11-17 09:27:57 +0000138} // namespace
139
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000140template <typename T_out, typename T_in, typename T_in_1>
141SimpleTensor<T_out> gemmlowp_matrix_multiply_core(const SimpleTensor<T_in> &a, const SimpleTensor<T_in_1> &b, TensorShape shape_c, int32_t a_offset, int32_t b_offset)
Pablo Tello299025a2017-09-29 11:30:12 +0100142{
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000143 static_assert(std::is_same<typename std::decay<T_out>::type, int32_t>::value, "Only int32_t is allowed for the output");
Gian Marcoe75a02b2017-11-08 12:24:09 +0000144
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000145 DataType dt = std::is_same<T_out, int32_t>::value ? DataType::S32 : DataType::U32;
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100146 SimpleTensor<T_out> c(shape_c, dt);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000147
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100148 const int K = a.shape().x();
149 const int M = a.shape().y();
150 const int N = b.shape().x();
151 const int D = a.shape().z(); // Number of matrices in a batch
152
153 const int a_stride_z = K * M;
154 // Do not slide the matrix B along the 3rd dimension in case matrix B has less than 3 dimensions
155 const int b_stride_z = b.shape().num_dimensions() > 2 ? N * K : 0;
156 const int c_stride_z = N * M;
Gian Marcoe75a02b2017-11-08 12:24:09 +0000157
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000158 std::vector<T_out> acc;
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100159 acc.resize(N);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000160
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100161 for(int depth = 0; depth < D; ++depth)
Pablo Tello299025a2017-09-29 11:30:12 +0100162 {
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100163 const int base_addr_a = depth * a_stride_z;
164 const int base_addr_b = depth * b_stride_z;
165 const int base_addr_c = depth * c_stride_z;
166
167 for(int i = 0; i < M; ++i)
Pablo Tello299025a2017-09-29 11:30:12 +0100168 {
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100169 for(int j = 0; j < N; ++j)
Pablo Tello299025a2017-09-29 11:30:12 +0100170 {
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100171 acc[j] = 0;
Pablo Tello299025a2017-09-29 11:30:12 +0100172 }
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100173 for(int k = 0; k < K; ++k)
174 {
175 const T_out tmp_a = a_offset + static_cast<T_out>(a[base_addr_a + k + i * K]);
176 for(int j = 0; j < N; ++j)
177 {
178 const T_out tmp_b = b_offset + static_cast<T_out>(b[base_addr_b + j + k * N]);
179 const T_out mult_as_int = tmp_a * tmp_b;
180 acc[j] += mult_as_int;
181 }
182 }
183 for(int j = 0; j < N; ++j)
184 {
185 c[base_addr_c + j + i * N] = acc[j];
186 }
Pablo Tello299025a2017-09-29 11:30:12 +0100187 }
188 }
189
190 return c;
191}
192
Pablo Tello181e6512017-11-15 13:28:27 +0000193// used to validate assembly kernels which don't know anything about offsets
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000194template <typename T1, typename T2, typename T3>
195SimpleTensor<T1> gemmlowp(const SimpleTensor<T2> &a, const SimpleTensor<T3> &b, TensorShape shape_c)
Pablo Tello181e6512017-11-15 13:28:27 +0000196{
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000197 return gemmlowp_matrix_multiply_core<T1, T2, T3>(a, b, shape_c, 0, 0);
Pablo Tello181e6512017-11-15 13:28:27 +0000198}
199
Gian Marcoe75a02b2017-11-08 12:24:09 +0000200template <typename T>
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000201SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale(const SimpleTensor<T> &in, int32_t result_offset, std::vector<int32_t> result_mult_int, std::vector<int32_t> result_shift,
202 int32_t min, int32_t max)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000203{
204 SimpleTensor<uint8_t> dst(in.shape(), DataType::QASYMM8);
205
Gian Marco6b77e912017-11-17 09:27:57 +0000206 quantize_down_int32_to_uint8_scale<T>(&in, nullptr, &dst, result_offset, result_mult_int, result_shift, min, max);
207
208 return dst;
209}
210
211template <typename T>
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000212SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale(const SimpleTensor<T> &in, const SimpleTensor<T> &bias, int32_t result_offset, std::vector<int32_t> result_mult_int,
213 std::vector<int32_t> result_shift, int32_t min, int32_t max)
Gian Marco6b77e912017-11-17 09:27:57 +0000214{
215 SimpleTensor<uint8_t> dst(in.shape(), DataType::QASYMM8);
216
217 quantize_down_int32_to_uint8_scale<T>(&in, &bias, &dst, result_offset, result_mult_int, result_shift, min, max);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000218
219 return dst;
220}
221
Gian Marco58c57942017-11-28 09:10:03 +0000222template <typename T>
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000223SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale_by_fixedpoint(const SimpleTensor<T> &in, std::vector<int32_t> result_fixedpoint_multiplier, std::vector<int32_t> result_shift,
224 int32_t result_offset_after_shift, int32_t min, int32_t max)
Gian Marco58c57942017-11-28 09:10:03 +0000225{
226 SimpleTensor<uint8_t> dst(in.shape(), DataType::QASYMM8);
227
228 quantize_down_int32_to_uint8_scale_by_fixedpoint<T>(&in, nullptr, &dst, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
229
230 return dst;
231}
232
233template <typename T>
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000234SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale_by_fixedpoint(const SimpleTensor<T> &in, const SimpleTensor<T> &bias, std::vector<int32_t> result_fixedpoint_multiplier,
235 std::vector<int32_t> result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max)
Gian Marco58c57942017-11-28 09:10:03 +0000236{
237 SimpleTensor<uint8_t> dst(in.shape(), DataType::QASYMM8);
238
239 quantize_down_int32_to_uint8_scale_by_fixedpoint<T>(&in, &bias, &dst, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
240
241 return dst;
242}
243
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100244template <typename T>
245SimpleTensor<int16_t> gemmlowp_quantize_down_int32_to_int16_scale_by_fixedpoint(const SimpleTensor<T> &in, int32_t result_fixedpoint_multiplier, int32_t result_shift, int32_t min,
246 int32_t max)
247{
248 SimpleTensor<int16_t> dst(in.shape(), DataType::QSYMM16);
249
250 quantize_down_int32_to_int16_scale_by_fixedpoint<T>(&in, nullptr, &dst, result_fixedpoint_multiplier, result_shift, min, max);
251
252 return dst;
253}
254
255template <typename T>
256SimpleTensor<int16_t> gemmlowp_quantize_down_int32_to_int16_scale_by_fixedpoint(const SimpleTensor<T> &in, const SimpleTensor<T> &bias, int32_t result_fixedpoint_multiplier, int32_t result_shift,
257 int32_t min, int32_t max)
258{
259 SimpleTensor<int16_t> dst(in.shape(), DataType::QSYMM16);
260
261 quantize_down_int32_to_int16_scale_by_fixedpoint<T>(&in, &bias, &dst, result_fixedpoint_multiplier, result_shift, min, max);
262
263 return dst;
264}
265
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000266template SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale_by_fixedpoint(const SimpleTensor<int32_t> &a, std::vector<int32_t> result_fixedpoint_multiplier,
267 std::vector<int32_t> result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max);
268template SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale_by_fixedpoint(const SimpleTensor<int32_t> &a, const SimpleTensor<int32_t> &b,
269 std::vector<int32_t> result_fixedpoint_multiplier,
270 std::vector<int32_t> result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max);
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100271template SimpleTensor<int16_t> gemmlowp_quantize_down_int32_to_int16_scale_by_fixedpoint(const SimpleTensor<int32_t> &a, int32_t result_fixedpoint_multiplier, int32_t result_shift,
272 int32_t min, int32_t max);
273template SimpleTensor<int16_t> gemmlowp_quantize_down_int32_to_int16_scale_by_fixedpoint(const SimpleTensor<int32_t> &a, const SimpleTensor<int32_t> &b, int32_t result_fixedpoint_multiplier,
274 int32_t result_shift, int32_t min, int32_t max);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000275template SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale(const SimpleTensor<int32_t> &a, int32_t result_offset, std::vector<int32_t> result_mult_int,
276 std::vector<int32_t> result_shift, int32_t min, int32_t max);
277template SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale(const SimpleTensor<int32_t> &a, const SimpleTensor<int32_t> &b, int32_t result_offset, std::vector<int32_t> result_mult_int,
278 std::vector<int32_t> result_shift, int32_t min, int32_t max);
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100279template SimpleTensor<int32_t> gemmlowp_matrix_multiply_core(const SimpleTensor<int8_t> &a, const SimpleTensor<int8_t> &b, TensorShape shape_c, int32_t a_offset, int32_t b_offset);
280template SimpleTensor<int32_t> gemmlowp_matrix_multiply_core(const SimpleTensor<uint8_t> &a, const SimpleTensor<uint8_t> &b, TensorShape shape_c, int32_t a_offset, int32_t b_offset);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000281template SimpleTensor<int32_t> gemmlowp<int32_t, int8_t, int8_t>(const SimpleTensor<int8_t> &a, const SimpleTensor<int8_t> &b, TensorShape shape_c);
282template SimpleTensor<int32_t> gemmlowp<int32_t, uint8_t, uint8_t>(const SimpleTensor<uint8_t> &a, const SimpleTensor<uint8_t> &b, TensorShape shape_c);
283template SimpleTensor<int32_t> gemmlowp<int32_t, uint8_t, int8_t>(const SimpleTensor<uint8_t> &a, const SimpleTensor<int8_t> &b, TensorShape shape_c);
Pablo Tello299025a2017-09-29 11:30:12 +0100284} // namespace reference
285} // namespace validation
286} // namespace test
287} // namespace arm_compute