blob: 5384715ace1b3a46b85d668f7998f40dfb86b03c [file] [log] [blame]
Moritz Pflanzer69d33412017-08-09 11:45:15 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010027#include "tests/validation/FixedPoint.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000028#include "tests/validation/reference/UtilsQuantizedAsymm.h"
Moritz Pflanzer69d33412017-08-09 11:45:15 +010029
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000030#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
31
Moritz Pflanzer69d33412017-08-09 11:45:15 +010032#include <numeric>
33
34namespace arm_compute
35{
36namespace test
37{
38namespace validation
39{
40namespace reference
41{
42namespace
43{
44// Vector matrix multiply for floating point
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000045template < typename T, typename TB, typename std::enable_if < is_floating_point<T>::value &&is_floating_point<TB>::value, int >::type = 0 >
46void 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,
47 int rows_weights, uint8_t fixed_point_position)
Moritz Pflanzer69d33412017-08-09 11:45:15 +010048{
49 ARM_COMPUTE_UNUSED(fixed_point_position);
50
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000051 const T *src_ptr = src.data() + offset_src;
52 const T *weights_ptr = weights.data();
53 const TB *bias_ptr = bias.data();
54 T *dst_ptr = dst.data() + offset_dst;
55
Moritz Pflanzer69d33412017-08-09 11:45:15 +010056 for(int y = 0; y < rows_weights; ++y)
57 {
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000058 dst_ptr[y] = std::inner_product(src_ptr, src_ptr + cols_weights, weights_ptr, static_cast<T>(0)) + bias_ptr[y];
59 weights_ptr += cols_weights;
Moritz Pflanzer69d33412017-08-09 11:45:15 +010060 }
61}
62
63// Vector matrix multiply for fixed point type
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000064template < typename T, typename TB, typename std::enable_if < std::is_integral<T>::value &&std::is_integral<TB>::value, int >::type = 0 >
65void 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,
66 int rows_weights, uint8_t fixed_point_position)
Moritz Pflanzer69d33412017-08-09 11:45:15 +010067{
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000068 const T *src_ptr = src.data() + offset_src;
69 const T *weights_ptr = weights.data();
70 const TB *bias_ptr = bias.data();
71 T *dst_ptr = dst.data() + offset_dst;
72
Moritz Pflanzer69d33412017-08-09 11:45:15 +010073 using namespace fixed_point_arithmetic;
74 using promoted_type = fixed_point_arithmetic::traits::promote_t<T>;
75
76 for(int y = 0; y < rows_weights; ++y)
77 {
78 // Reset accumulator
79 fixed_point<promoted_type> acc(0, fixed_point_position);
80
81 for(int x = 0; x < cols_weights; ++x)
82 {
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000083 const fixed_point<promoted_type> i_value(src_ptr[x], fixed_point_position, true);
84 const fixed_point<promoted_type> w_value(weights_ptr[x], fixed_point_position, true);
Moritz Pflanzer69d33412017-08-09 11:45:15 +010085 acc = acc + i_value * w_value;
86 }
87
88 // Get the bias
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000089 const fixed_point<T> b(bias_ptr[y], fixed_point_position, true);
Moritz Pflanzer69d33412017-08-09 11:45:15 +010090
91 // Convert back and accumulate the bias
92 fixed_point<T> res(acc);
93 res = res + b;
94
95 // Store the result
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000096 dst_ptr[y] = res.raw();
Moritz Pflanzer69d33412017-08-09 11:45:15 +010097
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000098 weights_ptr += cols_weights;
99 }
100}
101
102// Vector matrix multiply for quantized type
103template <>
104void vector_matrix_multiply(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &bias, SimpleTensor<uint8_t> &dst, int offset_src, int offset_dst,
105 int cols_weights, int rows_weights, uint8_t fixed_point_position)
106{
107 ARM_COMPUTE_UNUSED(fixed_point_position);
108
109 const uint8_t *src_ptr = src.data() + offset_src;
110 const uint8_t *weights_ptr = weights.data();
111 const int32_t *bias_ptr = bias.data();
112 uint8_t *dst_ptr = dst.data() + offset_dst;
113
114 const int input_offset = -src.quantization_info().offset;
115 const float input_scale = src.quantization_info().scale;
116 const int weights_offset = -weights.quantization_info().offset;
117 const float weights_scale = weights.quantization_info().scale;
118 const int output_offset = dst.quantization_info().offset;
119 const float output_scale = dst.quantization_info().scale;
120
121 int output_multiplier = 0;
122 int output_shift = 0;
123 const float multiplier = input_scale * weights_scale / output_scale;
124 arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
125
126 for(int y = 0; y < rows_weights; ++y)
127 {
128 // Reset accumulator
129 int32_t acc = 0;
130
131 for(int x = 0; x < cols_weights; ++x)
132 {
133 acc += (src_ptr[x] + input_offset) * (weights_ptr[x] + weights_offset);
134 }
135
136 // Accumulate the bias
137 acc += bias_ptr[y];
138
139 acc = asymm_rounding_divide_by_pow2(asymm_int_mult(acc, output_multiplier), output_shift);
140 acc += output_offset;
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000141 acc = utility::clamp<int32_t>(acc, 0, 255);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000142
143 // Store the result
144 dst_ptr[y] = static_cast<uint8_t>(acc);
145
146 weights_ptr += cols_weights;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100147 }
148}
149} // namespace
150
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000151template <typename T, typename TB>
152SimpleTensor<T> fully_connected_layer(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, const TensorShape &dst_shape)
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100153{
154 // Create reference
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000155 SimpleTensor<T> dst{ TensorShape{ dst_shape }, src.data_type(), 1, src.fixed_point_position(), src.quantization_info() };
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100156
157 // Sanity checks
158 const int num_batch_dimensions = std::max(0, static_cast<int>(dst_shape.num_dimensions()) - 1);
159 const int num_input_dimensions = src.shape().num_dimensions() - num_batch_dimensions;
160 const unsigned int linear_input_size = src.shape().total_size_lower(num_input_dimensions);
161
162 ARM_COMPUTE_UNUSED(num_batch_dimensions);
163 ARM_COMPUTE_UNUSED(num_input_dimensions);
164 ARM_COMPUTE_UNUSED(linear_input_size);
165 ARM_COMPUTE_ERROR_ON(weights.shape().x() != linear_input_size);
166 ARM_COMPUTE_ERROR_ON(weights.shape().y() != bias.shape().x());
167 ARM_COMPUTE_ERROR_ON(weights.shape().y() != dst.shape().x());
168
169 // Compute reference
170 const int cols_weights = weights.shape().x();
171 const int rows_weights = weights.shape().y();
172 const int num_batches = dst_shape.total_size_upper(1);
173
174 for(int k = 0; k < num_batches; ++k)
175 {
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000176 const int offset_in = k * cols_weights;
177 const int offset_out = k * rows_weights;
178
179 vector_matrix_multiply<T>(src,
180 weights,
181 bias,
182 dst,
183 offset_in,
184 offset_out,
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100185 cols_weights,
186 rows_weights,
187 src.fixed_point_position());
188 }
189
190 return dst;
191}
192
193template SimpleTensor<float> fully_connected_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &dst_shape);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100194template SimpleTensor<half> fully_connected_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &dst_shape);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100195template SimpleTensor<qint8_t> fully_connected_layer(const SimpleTensor<qint8_t> &src, const SimpleTensor<qint8_t> &weights, const SimpleTensor<qint8_t> &bias, const TensorShape &dst_shape);
196template SimpleTensor<qint16_t> fully_connected_layer(const SimpleTensor<qint16_t> &src, const SimpleTensor<qint16_t> &weights, const SimpleTensor<qint16_t> &bias, const TensorShape &dst_shape);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000197template 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);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100198} // namespace reference
199} // namespace validation
200} // namespace test
201} // namespace arm_compute