blob: 35b8a6486e259ef0b37a7cafc0d435f1009a3eda [file] [log] [blame]
Pablo Tello299025a2017-09-29 11:30:12 +01001/*
2 * Copyright (c) 2017 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 */
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"
Pablo Tello299025a2017-09-29 11:30:12 +010027
28namespace arm_compute
29{
30namespace test
31{
32namespace validation
33{
34namespace reference
35{
Gian Marco6b77e912017-11-17 09:27:57 +000036namespace
37{
38template <typename T>
39void quantize_down_int32_to_uint8_scale(const SimpleTensor<T> *in, const SimpleTensor<T> *bias, SimpleTensor<uint8_t> *dst, int32_t result_offset, int32_t result_mult_int, int32_t result_shift,
40 int32_t min, int32_t max)
41{
42 const int cols_in = in->shape().x();
43
44 for(int i = 0; i < in->num_elements(); ++i)
45 {
46 int32_t result = ((*in)[i] + result_offset) * result_mult_int;
47
48 if(bias != nullptr)
49 {
50 result += (*bias)[i % cols_in];
51 }
52
53 result >>= result_shift;
54
55 // Bounded ReLu
56 if(min != max)
57 {
58 result = std::max(min, std::min(max, result));
59 }
60
61 (*dst)[i] = static_cast<uint8_t>(std::max(0, std::min(255, result)));
62 }
63}
64} // namespace
65
Michalis Spyrouf3dfa272017-11-21 17:52:12 +000066template <typename T_out, typename T_in>
67SimpleTensor<T_out> gemmlowp_matrix_multiply_core(const SimpleTensor<T_in> &a, const SimpleTensor<T_in> &b, int32_t a_offset, int32_t b_offset)
Pablo Tello299025a2017-09-29 11:30:12 +010068{
Michalis Spyrouf3dfa272017-11-21 17:52:12 +000069 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 +000070
Michalis Spyrouf3dfa272017-11-21 17:52:12 +000071 TensorShape shape(b.shape()[0], a.shape()[1]);
72 DataType dt = std::is_same<T_out, int32_t>::value ? DataType::S32 : DataType::U32;
73 SimpleTensor<T_out> c(shape, dt);
Gian Marcoe75a02b2017-11-08 12:24:09 +000074
75 const int K = a.shape().x();
76 const int b_width = b.shape().x();
77 const int rows = c.shape().y(); //M
78 const int cols = c.shape().x(); //N
79
Michalis Spyrouf3dfa272017-11-21 17:52:12 +000080 std::vector<T_out> acc;
Pablo Tello299025a2017-09-29 11:30:12 +010081 acc.resize(cols);
Gian Marcoe75a02b2017-11-08 12:24:09 +000082
Pablo Tello299025a2017-09-29 11:30:12 +010083 for(int i = 0; i < rows; ++i)
84 {
85 for(int j = 0; j < cols; ++j)
86 {
87 acc[j] = 0;
88 }
89 for(int k = 0; k < K; ++k)
90 {
Michalis Spyrouf3dfa272017-11-21 17:52:12 +000091 const T_out tmp_a = a_offset + static_cast<T_out>(a[k + i * K]);
Pablo Tello299025a2017-09-29 11:30:12 +010092 for(int j = 0; j < b_width; ++j)
93 {
Michalis Spyrouf3dfa272017-11-21 17:52:12 +000094 const T_out tmp_b = b_offset + static_cast<T_out>(b[j + k * b_width]);
95 const T_out mult_as_int = tmp_a * tmp_b;
Pablo Tello299025a2017-09-29 11:30:12 +010096 acc[j] += mult_as_int;
97 }
98 }
99 for(int j = 0; j < cols; ++j)
100 {
Gian Marcoe75a02b2017-11-08 12:24:09 +0000101 c[j + i * cols] = acc[j];
Pablo Tello299025a2017-09-29 11:30:12 +0100102 }
103 }
104
105 return c;
106}
107
Pablo Tello181e6512017-11-15 13:28:27 +0000108// used to validate assembly kernels which don't know anything about offsets
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000109template <typename T1, typename T2>
110SimpleTensor<T1> gemmlowp(const SimpleTensor<T2> &a, const SimpleTensor<T2> &b)
Pablo Tello181e6512017-11-15 13:28:27 +0000111{
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000112 return gemmlowp_matrix_multiply_core<T1, T2>(a, b, 0, 0);
Pablo Tello181e6512017-11-15 13:28:27 +0000113}
114
Gian Marcoe75a02b2017-11-08 12:24:09 +0000115template <typename T>
Gian Marco6b77e912017-11-17 09:27:57 +0000116SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale(const SimpleTensor<T> &in, int32_t result_offset, int32_t result_mult_int, int32_t result_shift, int32_t min, int32_t max)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000117{
118 SimpleTensor<uint8_t> dst(in.shape(), DataType::QASYMM8);
119
Gian Marco6b77e912017-11-17 09:27:57 +0000120 quantize_down_int32_to_uint8_scale<T>(&in, nullptr, &dst, result_offset, result_mult_int, result_shift, min, max);
121
122 return dst;
123}
124
125template <typename T>
126SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale(const SimpleTensor<T> &in, const SimpleTensor<T> &bias, int32_t result_offset, int32_t result_mult_int, int32_t result_shift,
127 int32_t min, int32_t max)
128{
129 SimpleTensor<uint8_t> dst(in.shape(), DataType::QASYMM8);
130
131 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 +0000132
133 return dst;
134}
135
Gian Marco6b77e912017-11-17 09:27:57 +0000136template SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale(const SimpleTensor<int32_t> &a, int32_t result_offset, int32_t result_mult_int, int32_t result_shift, int32_t min,
137 int32_t max);
138template SimpleTensor<uint8_t> gemmlowp_quantize_down_int32_to_uint8_scale(const SimpleTensor<int32_t> &a, const SimpleTensor<int32_t> &b, int32_t result_offset, int32_t result_mult_int,
139 int32_t result_shift, int32_t min, int32_t max);
Michalis Spyrouf3dfa272017-11-21 17:52:12 +0000140template SimpleTensor<int32_t> gemmlowp_matrix_multiply_core(const SimpleTensor<int8_t> &a, const SimpleTensor<int8_t> &b, int32_t a_offset, int32_t b_offset);
141template SimpleTensor<int32_t> gemmlowp_matrix_multiply_core(const SimpleTensor<uint8_t> &a, const SimpleTensor<uint8_t> &b, int32_t a_offset, int32_t b_offset);
142template SimpleTensor<int32_t> gemmlowp(const SimpleTensor<int8_t> &a, const SimpleTensor<int8_t> &b);
143template SimpleTensor<int32_t> gemmlowp(const SimpleTensor<uint8_t> &a, const SimpleTensor<uint8_t> &b);
Pablo Tello299025a2017-09-29 11:30:12 +0100144} // namespace reference
145} // namespace validation
146} // namespace test
147} // namespace arm_compute