blob: 07ddf6d308b5c2b141433a7c0d83a019c3d87db7 [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 Pinitas45bcc3a2017-11-29 11:06:49 +000070 const int input_offset = -src.quantization_info().offset;
71 const float input_scale = src.quantization_info().scale;
72 const int weights_offset = -weights.quantization_info().offset;
73 const float weights_scale = weights.quantization_info().scale;
74 const int output_offset = dst.quantization_info().offset;
75 const float output_scale = dst.quantization_info().scale;
76
77 int output_multiplier = 0;
78 int output_shift = 0;
79 const float multiplier = input_scale * weights_scale / output_scale;
80 arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
81
82 for(int y = 0; y < rows_weights; ++y)
83 {
84 // Reset accumulator
85 int32_t acc = 0;
86
87 for(int x = 0; x < cols_weights; ++x)
88 {
89 acc += (src_ptr[x] + input_offset) * (weights_ptr[x] + weights_offset);
90 }
91
92 // Accumulate the bias
93 acc += bias_ptr[y];
94
95 acc = asymm_rounding_divide_by_pow2(asymm_int_mult(acc, output_multiplier), output_shift);
96 acc += output_offset;
Diego Lopez Recas490b3d82017-12-19 15:42:25 +000097 acc = utility::clamp<int32_t>(acc, 0, 255);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000098
99 // Store the result
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100100 dst_ptr[y] = static_cast<T>(acc);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000101
102 weights_ptr += cols_weights;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100103 }
104}
105} // namespace
106
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000107template <typename T, typename TB>
John Kesapides341b2182019-02-22 10:05:29 +0000108SimpleTensor<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 +0100109{
John Kesapides341b2182019-02-22 10:05:29 +0000110 // if no explicit quantization has been set you the same as src
111 if(out_quant_info == QuantizationInfo())
112 {
113 out_quant_info = src.quantization_info();
114 }
115
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100116 // Create reference
John Kesapides341b2182019-02-22 10:05:29 +0000117 SimpleTensor<T> dst{ TensorShape{ dst_shape }, src.data_type(), 1, out_quant_info };
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100118
119 // Sanity checks
120 const int num_batch_dimensions = std::max(0, static_cast<int>(dst_shape.num_dimensions()) - 1);
121 const int num_input_dimensions = src.shape().num_dimensions() - num_batch_dimensions;
122 const unsigned int linear_input_size = src.shape().total_size_lower(num_input_dimensions);
123
124 ARM_COMPUTE_UNUSED(num_batch_dimensions);
125 ARM_COMPUTE_UNUSED(num_input_dimensions);
126 ARM_COMPUTE_UNUSED(linear_input_size);
127 ARM_COMPUTE_ERROR_ON(weights.shape().x() != linear_input_size);
128 ARM_COMPUTE_ERROR_ON(weights.shape().y() != bias.shape().x());
129 ARM_COMPUTE_ERROR_ON(weights.shape().y() != dst.shape().x());
130
131 // Compute reference
132 const int cols_weights = weights.shape().x();
133 const int rows_weights = weights.shape().y();
134 const int num_batches = dst_shape.total_size_upper(1);
135
136 for(int k = 0; k < num_batches; ++k)
137 {
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000138 const int offset_in = k * cols_weights;
139 const int offset_out = k * rows_weights;
140
141 vector_matrix_multiply<T>(src,
142 weights,
143 bias,
144 dst,
145 offset_in,
146 offset_out,
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100147 cols_weights,
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100148 rows_weights);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100149 }
150
151 return dst;
152}
153
John Kesapides341b2182019-02-22 10:05:29 +0000154template SimpleTensor<float> fully_connected_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &dst_shape,
155 QuantizationInfo out_quant_info);
156template SimpleTensor<half> fully_connected_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &dst_shape,
157 QuantizationInfo out_quant_info);
158template 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,
159 QuantizationInfo out_quant_info);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100160} // namespace reference
161} // namespace validation
162} // namespace test
163} // namespace arm_compute