blob: af30e9ee543fb6acec3c09bda5412a50280c37df [file] [log] [blame]
Moritz Pflanzer69d33412017-08-09 11:45:15 +01001/*
ramelg01b2eba7f2021-12-23 08:32:08 +00002 * Copyright (c) 2017-2021 Arm Limited.
Moritz Pflanzer69d33412017-08-09 11:45:15 +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 */
24#include "FullyConnectedLayer.h"
25
Georgios Pinitas583137c2017-08-31 18:12:42 +010026#include "arm_compute/core/Types.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000027#include "tests/validation/reference/UtilsQuantizedAsymm.h"
Moritz Pflanzer69d33412017-08-09 11:45:15 +010028
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000029#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
30
Moritz Pflanzer69d33412017-08-09 11:45:15 +010031#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
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000044template < 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,
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010046 int rows_weights)
Moritz Pflanzer69d33412017-08-09 11:45:15 +010047{
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +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;
Michalis Spyroud1d77222020-04-08 14:10:15 +010052#if defined(_OPENMP)
53 #pragma omp parallel for
54#endif /* _OPENMP */
Moritz Pflanzer69d33412017-08-09 11:45:15 +010055 for(int y = 0; y < rows_weights; ++y)
56 {
Michalis Spyroud1d77222020-04-08 14:10:15 +010057 dst_ptr[y] = std::inner_product(src_ptr, src_ptr + cols_weights, &weights_ptr[cols_weights * y], static_cast<T>(0)) + bias_ptr[y];
Moritz Pflanzer69d33412017-08-09 11:45:15 +010058 }
59}
60
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010061// Vector matrix multiply for quantized type
Michele Di Giorgio9c700372020-01-08 11:33:44 +000062template < 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 >
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010063void vector_matrix_multiply(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, SimpleTensor<T> &dst, int offset_src, int offset_dst,
64 int cols_weights, int rows_weights)
Moritz Pflanzer69d33412017-08-09 11:45:15 +010065{
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000066 const T *src_ptr = src.data() + offset_src;
67 const T *weights_ptr = weights.data();
68 const TB *bias_ptr = bias.data();
69 T *dst_ptr = dst.data() + offset_dst;
70
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010071 const UniformQuantizationInfo iq_info = src.quantization_info().uniform();
72 const UniformQuantizationInfo wq_info = weights.quantization_info().uniform();
73 const UniformQuantizationInfo oq_info = dst.quantization_info().uniform();
74
75 const int input_offset = -iq_info.offset;
76 const float input_scale = iq_info.scale;
77 const int weights_offset = -wq_info.offset;
78 const float weights_scale = wq_info.scale;
79 const int output_offset = oq_info.offset;
80 const float output_scale = oq_info.scale;
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000081
82 int output_multiplier = 0;
83 int output_shift = 0;
84 const float multiplier = input_scale * weights_scale / output_scale;
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010085 arm_compute::quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000086
Michele Di Giorgio9c700372020-01-08 11:33:44 +000087 const int min = std::numeric_limits<T>::lowest();
88 const int max = std::numeric_limits<T>::max();
Michalis Spyroud1d77222020-04-08 14:10:15 +010089#if defined(_OPENMP)
90 #pragma omp parallel for
91#endif /* _OPENMP */
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000092 for(int y = 0; y < rows_weights; ++y)
93 {
94 // Reset accumulator
95 int32_t acc = 0;
96
97 for(int x = 0; x < cols_weights; ++x)
98 {
Michalis Spyroud1d77222020-04-08 14:10:15 +010099 acc += (src_ptr[x] + input_offset) * (weights_ptr[x + y * cols_weights] + weights_offset);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000100 }
101
102 // Accumulate the bias
103 acc += bias_ptr[y];
104
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100105 // Quantize down
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000106 acc = quantize_down_scale_by_fixedpoint(acc, output_multiplier, output_shift, output_offset, min, max);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000107
108 // Store the result
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100109 dst_ptr[y] = static_cast<T>(acc);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100110 }
111}
112} // namespace
113
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000114template <typename T, typename TB>
John Kesapides341b2182019-02-22 10:05:29 +0000115SimpleTensor<T> fully_connected_layer(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, const TensorShape &dst_shape, QuantizationInfo out_quant_info)
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100116{
John Kesapides341b2182019-02-22 10:05:29 +0000117 // if no explicit quantization has been set you the same as src
118 if(out_quant_info == QuantizationInfo())
119 {
120 out_quant_info = src.quantization_info();
121 }
122
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100123 // Create reference
John Kesapides341b2182019-02-22 10:05:29 +0000124 SimpleTensor<T> dst{ TensorShape{ dst_shape }, src.data_type(), 1, out_quant_info };
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100125
ramelg01b2eba7f2021-12-23 08:32:08 +0000126 // Health checks
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100127 const int num_batch_dimensions = std::max(0, static_cast<int>(dst_shape.num_dimensions()) - 1);
128 const int num_input_dimensions = src.shape().num_dimensions() - num_batch_dimensions;
129 const unsigned int linear_input_size = src.shape().total_size_lower(num_input_dimensions);
130
131 ARM_COMPUTE_UNUSED(num_batch_dimensions);
132 ARM_COMPUTE_UNUSED(num_input_dimensions);
133 ARM_COMPUTE_UNUSED(linear_input_size);
134 ARM_COMPUTE_ERROR_ON(weights.shape().x() != linear_input_size);
135 ARM_COMPUTE_ERROR_ON(weights.shape().y() != bias.shape().x());
136 ARM_COMPUTE_ERROR_ON(weights.shape().y() != dst.shape().x());
137
138 // Compute reference
139 const int cols_weights = weights.shape().x();
140 const int rows_weights = weights.shape().y();
141 const int num_batches = dst_shape.total_size_upper(1);
142
143 for(int k = 0; k < num_batches; ++k)
144 {
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000145 const int offset_in = k * cols_weights;
146 const int offset_out = k * rows_weights;
147
148 vector_matrix_multiply<T>(src,
149 weights,
150 bias,
151 dst,
152 offset_in,
153 offset_out,
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100154 cols_weights,
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100155 rows_weights);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100156 }
157
158 return dst;
159}
160
John Kesapides341b2182019-02-22 10:05:29 +0000161template SimpleTensor<float> fully_connected_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &dst_shape,
162 QuantizationInfo out_quant_info);
163template SimpleTensor<half> fully_connected_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &dst_shape,
164 QuantizationInfo out_quant_info);
165template 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,
166 QuantizationInfo out_quant_info);
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000167template 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,
168 QuantizationInfo out_quant_info);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100169} // namespace reference
170} // namespace validation
171} // namespace test
172} // namespace arm_compute