1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "FullyConnectedLayer.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/Types.h"
27*c217d954SCole Faust #include "tests/validation/reference/UtilsQuantizedAsymm.h"
28*c217d954SCole Faust
29*c217d954SCole Faust #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
30*c217d954SCole Faust
31*c217d954SCole Faust #include <numeric>
32*c217d954SCole Faust
33*c217d954SCole Faust namespace arm_compute
34*c217d954SCole Faust {
35*c217d954SCole Faust namespace test
36*c217d954SCole Faust {
37*c217d954SCole Faust namespace validation
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace reference
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace
42*c217d954SCole Faust {
43*c217d954SCole Faust // Vector matrix multiply for floating point
44*c217d954SCole Faust template < typename T, typename TB, typename std::enable_if < is_floating_point<T>::value &&is_floating_point<TB>::value, int >::type = 0 >
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,int rows_weights)45*c217d954SCole Faust void 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,
46*c217d954SCole Faust int rows_weights)
47*c217d954SCole Faust {
48*c217d954SCole Faust const T *src_ptr = src.data() + offset_src;
49*c217d954SCole Faust const T *weights_ptr = weights.data();
50*c217d954SCole Faust const TB *bias_ptr = bias.data();
51*c217d954SCole Faust T *dst_ptr = dst.data() + offset_dst;
52*c217d954SCole Faust #if defined(_OPENMP)
53*c217d954SCole Faust #pragma omp parallel for
54*c217d954SCole Faust #endif /* _OPENMP */
55*c217d954SCole Faust for(int y = 0; y < rows_weights; ++y)
56*c217d954SCole Faust {
57*c217d954SCole Faust dst_ptr[y] = std::inner_product(src_ptr, src_ptr + cols_weights, &weights_ptr[cols_weights * y], static_cast<T>(0)) + bias_ptr[y];
58*c217d954SCole Faust }
59*c217d954SCole Faust }
60*c217d954SCole Faust
61*c217d954SCole Faust // Vector matrix multiply for quantized type
62*c217d954SCole Faust template < 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 >
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,int rows_weights)63*c217d954SCole Faust void 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*c217d954SCole Faust int cols_weights, int rows_weights)
65*c217d954SCole Faust {
66*c217d954SCole Faust const T *src_ptr = src.data() + offset_src;
67*c217d954SCole Faust const T *weights_ptr = weights.data();
68*c217d954SCole Faust const TB *bias_ptr = bias.data();
69*c217d954SCole Faust T *dst_ptr = dst.data() + offset_dst;
70*c217d954SCole Faust
71*c217d954SCole Faust const UniformQuantizationInfo iq_info = src.quantization_info().uniform();
72*c217d954SCole Faust const UniformQuantizationInfo wq_info = weights.quantization_info().uniform();
73*c217d954SCole Faust const UniformQuantizationInfo oq_info = dst.quantization_info().uniform();
74*c217d954SCole Faust
75*c217d954SCole Faust const int input_offset = -iq_info.offset;
76*c217d954SCole Faust const float input_scale = iq_info.scale;
77*c217d954SCole Faust const int weights_offset = -wq_info.offset;
78*c217d954SCole Faust const float weights_scale = wq_info.scale;
79*c217d954SCole Faust const int output_offset = oq_info.offset;
80*c217d954SCole Faust const float output_scale = oq_info.scale;
81*c217d954SCole Faust
82*c217d954SCole Faust int output_multiplier = 0;
83*c217d954SCole Faust int output_shift = 0;
84*c217d954SCole Faust const float multiplier = input_scale * weights_scale / output_scale;
85*c217d954SCole Faust arm_compute::quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift);
86*c217d954SCole Faust
87*c217d954SCole Faust const int min = std::numeric_limits<T>::lowest();
88*c217d954SCole Faust const int max = std::numeric_limits<T>::max();
89*c217d954SCole Faust #if defined(_OPENMP)
90*c217d954SCole Faust #pragma omp parallel for
91*c217d954SCole Faust #endif /* _OPENMP */
92*c217d954SCole Faust for(int y = 0; y < rows_weights; ++y)
93*c217d954SCole Faust {
94*c217d954SCole Faust // Reset accumulator
95*c217d954SCole Faust int32_t acc = 0;
96*c217d954SCole Faust
97*c217d954SCole Faust for(int x = 0; x < cols_weights; ++x)
98*c217d954SCole Faust {
99*c217d954SCole Faust acc += (src_ptr[x] + input_offset) * (weights_ptr[x + y * cols_weights] + weights_offset);
100*c217d954SCole Faust }
101*c217d954SCole Faust
102*c217d954SCole Faust // Accumulate the bias
103*c217d954SCole Faust acc += bias_ptr[y];
104*c217d954SCole Faust
105*c217d954SCole Faust // Quantize down
106*c217d954SCole Faust acc = quantize_down_scale_by_fixedpoint(acc, output_multiplier, output_shift, output_offset, min, max);
107*c217d954SCole Faust
108*c217d954SCole Faust // Store the result
109*c217d954SCole Faust dst_ptr[y] = static_cast<T>(acc);
110*c217d954SCole Faust }
111*c217d954SCole Faust }
112*c217d954SCole Faust } // namespace
113*c217d954SCole Faust
114*c217d954SCole Faust template <typename T, typename TB>
fully_connected_layer(const SimpleTensor<T> & src,const SimpleTensor<T> & weights,const SimpleTensor<TB> & bias,const TensorShape & dst_shape,QuantizationInfo out_quant_info)115*c217d954SCole Faust SimpleTensor<T> fully_connected_layer(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, const TensorShape &dst_shape, QuantizationInfo out_quant_info)
116*c217d954SCole Faust {
117*c217d954SCole Faust // if no explicit quantization has been set you the same as src
118*c217d954SCole Faust if(out_quant_info == QuantizationInfo())
119*c217d954SCole Faust {
120*c217d954SCole Faust out_quant_info = src.quantization_info();
121*c217d954SCole Faust }
122*c217d954SCole Faust
123*c217d954SCole Faust // Create reference
124*c217d954SCole Faust SimpleTensor<T> dst{ TensorShape{ dst_shape }, src.data_type(), 1, out_quant_info };
125*c217d954SCole Faust
126*c217d954SCole Faust // Health checks
127*c217d954SCole Faust const int num_batch_dimensions = std::max(0, static_cast<int>(dst_shape.num_dimensions()) - 1);
128*c217d954SCole Faust const int num_input_dimensions = src.shape().num_dimensions() - num_batch_dimensions;
129*c217d954SCole Faust const unsigned int linear_input_size = src.shape().total_size_lower(num_input_dimensions);
130*c217d954SCole Faust
131*c217d954SCole Faust ARM_COMPUTE_UNUSED(num_batch_dimensions);
132*c217d954SCole Faust ARM_COMPUTE_UNUSED(num_input_dimensions);
133*c217d954SCole Faust ARM_COMPUTE_UNUSED(linear_input_size);
134*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(weights.shape().x() != linear_input_size);
135*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(weights.shape().y() != bias.shape().x());
136*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(weights.shape().y() != dst.shape().x());
137*c217d954SCole Faust
138*c217d954SCole Faust // Compute reference
139*c217d954SCole Faust const int cols_weights = weights.shape().x();
140*c217d954SCole Faust const int rows_weights = weights.shape().y();
141*c217d954SCole Faust const int num_batches = dst_shape.total_size_upper(1);
142*c217d954SCole Faust
143*c217d954SCole Faust for(int k = 0; k < num_batches; ++k)
144*c217d954SCole Faust {
145*c217d954SCole Faust const int offset_in = k * cols_weights;
146*c217d954SCole Faust const int offset_out = k * rows_weights;
147*c217d954SCole Faust
148*c217d954SCole Faust vector_matrix_multiply<T>(src,
149*c217d954SCole Faust weights,
150*c217d954SCole Faust bias,
151*c217d954SCole Faust dst,
152*c217d954SCole Faust offset_in,
153*c217d954SCole Faust offset_out,
154*c217d954SCole Faust cols_weights,
155*c217d954SCole Faust rows_weights);
156*c217d954SCole Faust }
157*c217d954SCole Faust
158*c217d954SCole Faust return dst;
159*c217d954SCole Faust }
160*c217d954SCole Faust
161*c217d954SCole Faust template SimpleTensor<float> fully_connected_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &dst_shape,
162*c217d954SCole Faust QuantizationInfo out_quant_info);
163*c217d954SCole Faust template SimpleTensor<half> fully_connected_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &dst_shape,
164*c217d954SCole Faust QuantizationInfo out_quant_info);
165*c217d954SCole Faust template 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*c217d954SCole Faust QuantizationInfo out_quant_info);
167*c217d954SCole Faust template 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*c217d954SCole Faust QuantizationInfo out_quant_info);
169*c217d954SCole Faust } // namespace reference
170*c217d954SCole Faust } // namespace validation
171*c217d954SCole Faust } // namespace test
172*c217d954SCole Faust } // namespace arm_compute
173