blob: cd84b9cfd1710e82f71bb215ff76c5ca316bfb89 [file] [log] [blame]
Moritz Pflanzer69d33412017-08-09 11:45:15 +01001/*
John Kesapides341b2182019-02-22 10:05:29 +00002 * Copyright (c) 2017-2019 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;
52
Moritz Pflanzer69d33412017-08-09 11:45:15 +010053 for(int y = 0; y < rows_weights; ++y)
54 {
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000055 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;
Moritz Pflanzer69d33412017-08-09 11:45:15 +010057 }
58}
59
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010060// Vector matrix multiply for quantized type
61template < typename T, typename TB, typename std::enable_if < std::is_same<T, uint8_t>::value &&std::is_same<TB, int32_t>::value, int >::type = 0 >
62void 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)
Moritz Pflanzer69d33412017-08-09 11:45:15 +010064{
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000065 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
Georgios Pinitas4c5469b2019-05-21 13:32:43 +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;
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000080
81 int output_multiplier = 0;
82 int output_shift = 0;
83 const float multiplier = input_scale * weights_scale / output_scale;
84 arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
85
86 for(int y = 0; y < rows_weights; ++y)
87 {
88 // Reset accumulator
89 int32_t acc = 0;
90
91 for(int x = 0; x < cols_weights; ++x)
92 {
93 acc += (src_ptr[x] + input_offset) * (weights_ptr[x] + weights_offset);
94 }
95
96 // Accumulate the bias
97 acc += bias_ptr[y];
98
99 acc = asymm_rounding_divide_by_pow2(asymm_int_mult(acc, output_multiplier), output_shift);
100 acc += output_offset;
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000101 acc = utility::clamp<int32_t>(acc, 0, 255);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000102
103 // Store the result
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100104 dst_ptr[y] = static_cast<T>(acc);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000105
106 weights_ptr += cols_weights;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100107 }
108}
109} // namespace
110
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000111template <typename T, typename TB>
John Kesapides341b2182019-02-22 10:05:29 +0000112SimpleTensor<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 +0100113{
John Kesapides341b2182019-02-22 10:05:29 +0000114 // if no explicit quantization has been set you the same as src
115 if(out_quant_info == QuantizationInfo())
116 {
117 out_quant_info = src.quantization_info();
118 }
119
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100120 // Create reference
John Kesapides341b2182019-02-22 10:05:29 +0000121 SimpleTensor<T> dst{ TensorShape{ dst_shape }, src.data_type(), 1, out_quant_info };
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100122
123 // Sanity checks
124 const int num_batch_dimensions = std::max(0, static_cast<int>(dst_shape.num_dimensions()) - 1);
125 const int num_input_dimensions = src.shape().num_dimensions() - num_batch_dimensions;
126 const unsigned int linear_input_size = src.shape().total_size_lower(num_input_dimensions);
127
128 ARM_COMPUTE_UNUSED(num_batch_dimensions);
129 ARM_COMPUTE_UNUSED(num_input_dimensions);
130 ARM_COMPUTE_UNUSED(linear_input_size);
131 ARM_COMPUTE_ERROR_ON(weights.shape().x() != linear_input_size);
132 ARM_COMPUTE_ERROR_ON(weights.shape().y() != bias.shape().x());
133 ARM_COMPUTE_ERROR_ON(weights.shape().y() != dst.shape().x());
134
135 // Compute reference
136 const int cols_weights = weights.shape().x();
137 const int rows_weights = weights.shape().y();
138 const int num_batches = dst_shape.total_size_upper(1);
139
140 for(int k = 0; k < num_batches; ++k)
141 {
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000142 const int offset_in = k * cols_weights;
143 const int offset_out = k * rows_weights;
144
145 vector_matrix_multiply<T>(src,
146 weights,
147 bias,
148 dst,
149 offset_in,
150 offset_out,
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100151 cols_weights,
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100152 rows_weights);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100153 }
154
155 return dst;
156}
157
John Kesapides341b2182019-02-22 10:05:29 +0000158template SimpleTensor<float> fully_connected_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &dst_shape,
159 QuantizationInfo out_quant_info);
160template SimpleTensor<half> fully_connected_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &dst_shape,
161 QuantizationInfo out_quant_info);
162template 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,
163 QuantizationInfo out_quant_info);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100164} // namespace reference
165} // namespace validation
166} // namespace test
167} // namespace arm_compute