blob: 4529b91a48b59b5bc03fd2fd42476e4e8952714b [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>
Georgios Pinitas448a81f2019-11-21 14:10:25 +000042struct DataTypeExtractor
43{
44 static DataType data_type()
45 {
46 DataType data_type = DataType::UNKNOWN;
47 if(std::is_same<T, int8_t>::value)
48 {
49 data_type = DataType::QASYMM8_SIGNED;
50 }
51 else if(std::is_same<T, uint8_t>::value)
52 {
53 data_type = DataType::QASYMM8;
54 }
55 else if(std::is_same<T, int16_t>::value)
56 {
57 data_type = DataType::QSYMM16;
58 }
59 return data_type;
60 }
61};
62
63template <typename T>
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000064void 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,
65 std::vector<int32_t> result_shift, int32_t min, int32_t max)
Gian Marco6b77e912017-11-17 09:27:57 +000066{
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000067 const int cols_in = in->shape().x();
68 const bool is_per_channel = result_mult_int.size() > 1;
Gian Marco6b77e912017-11-17 09:27:57 +000069
70 for(int i = 0; i < in->num_elements(); ++i)
71 {
Gian Marco58c57942017-11-28 09:10:03 +000072 int32_t result = ((*in)[i] + result_offset);
Gian Marco6b77e912017-11-17 09:27:57 +000073
74 if(bias != nullptr)
75 {
76 result += (*bias)[i % cols_in];
77 }
78
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000079 result *= (is_per_channel) ? result_mult_int[i % cols_in] : result_mult_int[0];
Gian Marco58c57942017-11-28 09:10:03 +000080
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000081 result >>= (is_per_channel) ? result_shift[i % cols_in] : result_shift[0];
Gian Marco6b77e912017-11-17 09:27:57 +000082
83 // Bounded ReLu
84 if(min != max)
85 {
86 result = std::max(min, std::min(max, result));
87 }
88
89 (*dst)[i] = static_cast<uint8_t>(std::max(0, std::min(255, result)));
90 }
91}
Gian Marco58c57942017-11-28 09:10:03 +000092
Georgios Pinitas448a81f2019-11-21 14:10:25 +000093template <typename TIn, typename TOut>
94void quantize_down_scale_by_fixedpoint(const SimpleTensor<TIn> *in, const SimpleTensor<TIn> *bias, SimpleTensor<TOut> *dst, std::vector<int32_t> result_fixedpoint_multiplier,
95 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 +000096{
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000097 const int cols_in = in->shape().x();
98 const bool is_per_channel = result_fixedpoint_multiplier.size() > 1;
Gian Marco58c57942017-11-28 09:10:03 +000099
100 for(int i = 0; i < in->num_elements(); ++i)
101 {
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000102 TIn result = (*in)[i];
Gian Marco58c57942017-11-28 09:10:03 +0000103
104 if(bias != nullptr)
105 {
106 result += (*bias)[i % cols_in];
107 }
108
109 // Fixed point multiplication
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000110 const int32_t multiplier = (is_per_channel) ? result_fixedpoint_multiplier[i % cols_in] : result_fixedpoint_multiplier[0];
111 const int32_t shift = (is_per_channel) ? result_shift[i % cols_in] : result_shift[0];
112
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000113 if(shift < 0)
114 {
115 result = asymm_int_mult(result * (1 << (-shift)), multiplier);
116 }
117 else
118 {
119 result = asymm_rounding_divide_by_pow2(asymm_int_mult(result, multiplier), shift);
120 }
Gian Marco58c57942017-11-28 09:10:03 +0000121 result += result_offset_after_shift;
122
123 // Bounded ReLu
124 if(min != max)
125 {
126 result = std::max(min, std::min(max, result));
127 }
128
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000129 (*dst)[i] = static_cast<TOut>(std::max<TIn>(std::numeric_limits<TOut>::lowest(),
130 std::min<TIn>(std::numeric_limits<TOut>::max(), result)));
Gian Marco Iodicebc415af2019-06-13 15:58:32 +0100131 }
132}
Gian Marco6b77e912017-11-17 09:27:57 +0000133} // namespace
134
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000135template <typename T_out, typename T_in, typename T_in_1>
136SimpleTensor<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 +0100137{
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000138 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 +0000139
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000140 DataType dt = std::is_same<T_out, int32_t>::value ? DataType::S32 : DataType::U32;
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100141 SimpleTensor<T_out> c(shape_c, dt);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000142
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100143 const int K = a.shape().x();
144 const int M = a.shape().y();
145 const int N = b.shape().x();
146 const int D = a.shape().z(); // Number of matrices in a batch
147
148 const int a_stride_z = K * M;
149 // Do not slide the matrix B along the 3rd dimension in case matrix B has less than 3 dimensions
150 const int b_stride_z = b.shape().num_dimensions() > 2 ? N * K : 0;
151 const int c_stride_z = N * M;
Gian Marcoe75a02b2017-11-08 12:24:09 +0000152
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000153 std::vector<T_out> acc;
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100154 acc.resize(N);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000155
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100156 for(int depth = 0; depth < D; ++depth)
Pablo Tello299025a2017-09-29 11:30:12 +0100157 {
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100158 const int base_addr_a = depth * a_stride_z;
159 const int base_addr_b = depth * b_stride_z;
160 const int base_addr_c = depth * c_stride_z;
161
162 for(int i = 0; i < M; ++i)
Pablo Tello299025a2017-09-29 11:30:12 +0100163 {
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100164 for(int j = 0; j < N; ++j)
Pablo Tello299025a2017-09-29 11:30:12 +0100165 {
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100166 acc[j] = 0;
Pablo Tello299025a2017-09-29 11:30:12 +0100167 }
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100168 for(int k = 0; k < K; ++k)
169 {
170 const T_out tmp_a = a_offset + static_cast<T_out>(a[base_addr_a + k + i * K]);
171 for(int j = 0; j < N; ++j)
172 {
173 const T_out tmp_b = b_offset + static_cast<T_out>(b[base_addr_b + j + k * N]);
174 const T_out mult_as_int = tmp_a * tmp_b;
175 acc[j] += mult_as_int;
176 }
177 }
178 for(int j = 0; j < N; ++j)
179 {
180 c[base_addr_c + j + i * N] = acc[j];
181 }
Pablo Tello299025a2017-09-29 11:30:12 +0100182 }
183 }
184
185 return c;
186}
187
Pablo Tello181e6512017-11-15 13:28:27 +0000188// used to validate assembly kernels which don't know anything about offsets
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000189template <typename T1, typename T2, typename T3>
190SimpleTensor<T1> gemmlowp(const SimpleTensor<T2> &a, const SimpleTensor<T3> &b, TensorShape shape_c)
Pablo Tello181e6512017-11-15 13:28:27 +0000191{
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000192 return gemmlowp_matrix_multiply_core<T1, T2, T3>(a, b, shape_c, 0, 0);
Pablo Tello181e6512017-11-15 13:28:27 +0000193}
194
Gian Marcoe75a02b2017-11-08 12:24:09 +0000195template <typename T>
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000196SimpleTensor<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,
197 int32_t min, int32_t max)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000198{
199 SimpleTensor<uint8_t> dst(in.shape(), DataType::QASYMM8);
200
Gian Marco6b77e912017-11-17 09:27:57 +0000201 quantize_down_int32_to_uint8_scale<T>(&in, nullptr, &dst, result_offset, result_mult_int, result_shift, min, max);
202
203 return dst;
204}
205
206template <typename T>
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000207SimpleTensor<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,
208 std::vector<int32_t> result_shift, int32_t min, int32_t max)
Gian Marco6b77e912017-11-17 09:27:57 +0000209{
210 SimpleTensor<uint8_t> dst(in.shape(), DataType::QASYMM8);
211
212 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 +0000213
214 return dst;
215}
216
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000217template <typename TIn, typename TOut>
218SimpleTensor<TOut> gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor<TIn> &in, std::vector<int32_t> result_fixedpoint_multiplier, std::vector<int32_t> result_shift,
219 int32_t result_offset_after_shift, int32_t min, int32_t max)
Gian Marco58c57942017-11-28 09:10:03 +0000220{
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000221 SimpleTensor<TOut> dst(in.shape(), DataTypeExtractor<TOut>::data_type());
Gian Marco58c57942017-11-28 09:10:03 +0000222
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000223 quantize_down_scale_by_fixedpoint<TIn, TOut>(&in, nullptr, &dst, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
Gian Marco58c57942017-11-28 09:10:03 +0000224
225 return dst;
226}
227
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000228template <typename TIn, typename TOut>
229SimpleTensor<TOut> gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor<TIn> &in, const SimpleTensor<TIn> &bias, std::vector<int32_t> result_fixedpoint_multiplier,
230 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 +0000231{
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000232 SimpleTensor<TOut> dst(in.shape(), DataTypeExtractor<TOut>::data_type());
Gian Marco58c57942017-11-28 09:10:03 +0000233
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000234 quantize_down_scale_by_fixedpoint<TIn, TOut>(&in, &bias, &dst, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max);
Gian Marco58c57942017-11-28 09:10:03 +0000235
236 return dst;
237}
238
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000239template SimpleTensor<uint8_t> gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor<int32_t> &a, std::vector<int32_t> result_fixedpoint_multiplier,
240 std::vector<int32_t> result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max);
241template SimpleTensor<uint8_t> gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor<int32_t> &a, const SimpleTensor<int32_t> &b,
242 std::vector<int32_t> result_fixedpoint_multiplier,
243 std::vector<int32_t> result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max);
244template SimpleTensor<int8_t> gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor<int32_t> &a, std::vector<int32_t> result_fixedpoint_multiplier,
245 std::vector<int32_t> result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max);
246template SimpleTensor<int8_t> gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor<int32_t> &a, const SimpleTensor<int32_t> &b,
247 std::vector<int32_t> result_fixedpoint_multiplier,
248 std::vector<int32_t> result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max);
249template SimpleTensor<int16_t> gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor<int32_t> &a, std::vector<int32_t> result_fixedpoint_multiplier,
250 std::vector<int32_t> result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max);
251template SimpleTensor<int16_t> gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor<int32_t> &a, const SimpleTensor<int32_t> &b,
252 std::vector<int32_t> result_fixedpoint_multiplier,
253 std::vector<int32_t> result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max);
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +0000254template 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,
255 std::vector<int32_t> result_shift, int32_t min, int32_t max);
256template 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,
257 std::vector<int32_t> result_shift, int32_t min, int32_t max);
Georgios Pinitasebf6b8a2018-09-24 16:31:08 +0100258template 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);
259template 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 +0000260template SimpleTensor<int32_t> gemmlowp<int32_t, int8_t, int8_t>(const SimpleTensor<int8_t> &a, const SimpleTensor<int8_t> &b, TensorShape shape_c);
261template SimpleTensor<int32_t> gemmlowp<int32_t, uint8_t, uint8_t>(const SimpleTensor<uint8_t> &a, const SimpleTensor<uint8_t> &b, TensorShape shape_c);
262template 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 +0100263} // namespace reference
264} // namespace validation
265} // namespace test
266} // namespace arm_compute