blob: 9aecd6cf14e7183cc871a7027c06d237ecdd0598 [file] [log] [blame]
Anthony Barbier8140e1e2017-12-14 23:48:46 +00001/*
Jenkins36ccc902020-02-21 11:10:48 +00002 * Copyright (c) 2017-2020 ARM Limited.
Anthony Barbier8140e1e2017-12-14 23:48:46 +00003 *
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#include "FullyConnectedLayer.h"
25
26#include "arm_compute/core/Types.h"
Anthony Barbier8140e1e2017-12-14 23:48:46 +000027#include "tests/validation/reference/UtilsQuantizedAsymm.h"
28
29#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
30
31#include <numeric>
32
33namespace arm_compute
34{
35namespace test
36{
37namespace validation
38{
39namespace reference
40{
41namespace
42{
43// Vector matrix multiply for floating point
44template < typename T, typename TB, typename std::enable_if < is_floating_point<T>::value &&is_floating_point<TB>::value, int >::type = 0 >
45void vector_matrix_multiply(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, SimpleTensor<T> &dst, int offset_src, int offset_dst, int cols_weights,
Jenkins52ba29e2018-08-29 15:32:11 +000046 int rows_weights)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000047{
Anthony Barbier8140e1e2017-12-14 23:48:46 +000048 const T *src_ptr = src.data() + offset_src;
49 const T *weights_ptr = weights.data();
50 const TB *bias_ptr = bias.data();
51 T *dst_ptr = dst.data() + offset_dst;
52
53 for(int y = 0; y < rows_weights; ++y)
54 {
55 dst_ptr[y] = std::inner_product(src_ptr, src_ptr + cols_weights, weights_ptr, static_cast<T>(0)) + bias_ptr[y];
56 weights_ptr += cols_weights;
57 }
58}
59
Jenkins52ba29e2018-08-29 15:32:11 +000060// Vector matrix multiply for quantized type
Jenkins36ccc902020-02-21 11:10:48 +000061template < typename T, typename TB, typename std::enable_if < (std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value) &&std::is_same<TB, int32_t>::value, int >::type = 0 >
Jenkins52ba29e2018-08-29 15:32:11 +000062void vector_matrix_multiply(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, SimpleTensor<T> &dst, int offset_src, int offset_dst,
63 int cols_weights, int rows_weights)
Anthony Barbier8140e1e2017-12-14 23:48:46 +000064{
65 const T *src_ptr = src.data() + offset_src;
66 const T *weights_ptr = weights.data();
67 const TB *bias_ptr = bias.data();
68 T *dst_ptr = dst.data() + offset_dst;
69
Jenkins975dfe12019-09-02 11:47:54 +010070 const UniformQuantizationInfo iq_info = src.quantization_info().uniform();
71 const UniformQuantizationInfo wq_info = weights.quantization_info().uniform();
72 const UniformQuantizationInfo oq_info = dst.quantization_info().uniform();
73
74 const int input_offset = -iq_info.offset;
75 const float input_scale = iq_info.scale;
76 const int weights_offset = -wq_info.offset;
77 const float weights_scale = wq_info.scale;
78 const int output_offset = oq_info.offset;
79 const float output_scale = oq_info.scale;
Anthony Barbier8140e1e2017-12-14 23:48:46 +000080
81 int output_multiplier = 0;
82 int output_shift = 0;
83 const float multiplier = input_scale * weights_scale / output_scale;
Jenkins36ccc902020-02-21 11:10:48 +000084 arm_compute::quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
85
86 const int min = std::numeric_limits<T>::lowest();
87 const int max = std::numeric_limits<T>::max();
Anthony Barbier8140e1e2017-12-14 23:48:46 +000088
89 for(int y = 0; y < rows_weights; ++y)
90 {
91 // Reset accumulator
92 int32_t acc = 0;
93
94 for(int x = 0; x < cols_weights; ++x)
95 {
96 acc += (src_ptr[x] + input_offset) * (weights_ptr[x] + weights_offset);
97 }
98
99 // Accumulate the bias
100 acc += bias_ptr[y];
101
Jenkins36ccc902020-02-21 11:10:48 +0000102 // Quantize down
103 acc = quantize_down_scale_by_fixedpoint(acc, output_multiplier, output_shift, output_offset, min, max);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000104
105 // Store the result
Jenkins52ba29e2018-08-29 15:32:11 +0000106 dst_ptr[y] = static_cast<T>(acc);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000107
108 weights_ptr += cols_weights;
109 }
110}
111} // namespace
112
113template <typename T, typename TB>
Jenkins4ba87db2019-05-23 17:11:51 +0100114SimpleTensor<T> fully_connected_layer(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, const TensorShape &dst_shape, QuantizationInfo out_quant_info)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000115{
Jenkins4ba87db2019-05-23 17:11:51 +0100116 // if no explicit quantization has been set you the same as src
117 if(out_quant_info == QuantizationInfo())
118 {
119 out_quant_info = src.quantization_info();
120 }
121
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000122 // Create reference
Jenkins4ba87db2019-05-23 17:11:51 +0100123 SimpleTensor<T> dst{ TensorShape{ dst_shape }, src.data_type(), 1, out_quant_info };
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000124
125 // Sanity checks
126 const int num_batch_dimensions = std::max(0, static_cast<int>(dst_shape.num_dimensions()) - 1);
127 const int num_input_dimensions = src.shape().num_dimensions() - num_batch_dimensions;
128 const unsigned int linear_input_size = src.shape().total_size_lower(num_input_dimensions);
129
130 ARM_COMPUTE_UNUSED(num_batch_dimensions);
131 ARM_COMPUTE_UNUSED(num_input_dimensions);
132 ARM_COMPUTE_UNUSED(linear_input_size);
133 ARM_COMPUTE_ERROR_ON(weights.shape().x() != linear_input_size);
134 ARM_COMPUTE_ERROR_ON(weights.shape().y() != bias.shape().x());
135 ARM_COMPUTE_ERROR_ON(weights.shape().y() != dst.shape().x());
136
137 // Compute reference
138 const int cols_weights = weights.shape().x();
139 const int rows_weights = weights.shape().y();
140 const int num_batches = dst_shape.total_size_upper(1);
141
142 for(int k = 0; k < num_batches; ++k)
143 {
144 const int offset_in = k * cols_weights;
145 const int offset_out = k * rows_weights;
146
147 vector_matrix_multiply<T>(src,
148 weights,
149 bias,
150 dst,
151 offset_in,
152 offset_out,
153 cols_weights,
Jenkins52ba29e2018-08-29 15:32:11 +0000154 rows_weights);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000155 }
156
157 return dst;
158}
159
Jenkins4ba87db2019-05-23 17:11:51 +0100160template SimpleTensor<float> fully_connected_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &dst_shape,
161 QuantizationInfo out_quant_info);
162template SimpleTensor<half> fully_connected_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &dst_shape,
163 QuantizationInfo out_quant_info);
164template SimpleTensor<uint8_t> fully_connected_layer(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &bias, const TensorShape &dst_shape,
165 QuantizationInfo out_quant_info);
Jenkins36ccc902020-02-21 11:10:48 +0000166template SimpleTensor<int8_t> fully_connected_layer(const SimpleTensor<int8_t> &src, const SimpleTensor<int8_t> &weights, const SimpleTensor<int32_t> &bias, const TensorShape &dst_shape,
167 QuantizationInfo out_quant_info);
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000168} // namespace reference
169} // namespace validation
170} // namespace test
171} // namespace arm_compute