blob: 7852dab27ba782a350c431784fd37b7ab94ebec6 [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
26#include "tests/validation_new/FixedPoint.h"
27#include "tests/validation_new/half.h"
28
29#include <numeric>
30
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace reference
38{
39namespace
40{
41// Vector matrix multiply for floating point
42template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type = 0>
43void vector_matrix_multiply(const T *src, const T *weights, const T *bias, T *dst, int cols_weights, int rows_weights, uint8_t fixed_point_position)
44{
45 ARM_COMPUTE_UNUSED(fixed_point_position);
46
47 for(int y = 0; y < rows_weights; ++y)
48 {
49 dst[y] = std::inner_product(src, src + cols_weights, weights, static_cast<T>(0)) + bias[y];
50 weights += cols_weights;
51 }
52}
53
54// Vector matrix multiply for fixed point type
55template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
56void vector_matrix_multiply(const T *src, const T *weights, const T *bias, T *dst, int cols_weights, int rows_weights, uint8_t fixed_point_position)
57{
58 using namespace fixed_point_arithmetic;
59 using promoted_type = fixed_point_arithmetic::traits::promote_t<T>;
60
61 for(int y = 0; y < rows_weights; ++y)
62 {
63 // Reset accumulator
64 fixed_point<promoted_type> acc(0, fixed_point_position);
65
66 for(int x = 0; x < cols_weights; ++x)
67 {
68 const fixed_point<promoted_type> i_value(src[x], fixed_point_position, true);
69 const fixed_point<promoted_type> w_value(weights[x], fixed_point_position, true);
70 acc = acc + i_value * w_value;
71 }
72
73 // Get the bias
74 const fixed_point<T> b(bias[y], fixed_point_position, true);
75
76 // Convert back and accumulate the bias
77 fixed_point<T> res(acc);
78 res = res + b;
79
80 // Store the result
81 dst[y] = res.raw();
82
83 weights += cols_weights;
84 }
85}
86} // namespace
87
88template <typename T>
89SimpleTensor<T> fully_connected_layer(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<T> &bias, const TensorShape &dst_shape)
90{
91 // Create reference
92 SimpleTensor<T> dst{ TensorShape{ dst_shape }, src.data_type(), 1, src.fixed_point_position() };
93
94 // Sanity checks
95 const int num_batch_dimensions = std::max(0, static_cast<int>(dst_shape.num_dimensions()) - 1);
96 const int num_input_dimensions = src.shape().num_dimensions() - num_batch_dimensions;
97 const unsigned int linear_input_size = src.shape().total_size_lower(num_input_dimensions);
98
99 ARM_COMPUTE_UNUSED(num_batch_dimensions);
100 ARM_COMPUTE_UNUSED(num_input_dimensions);
101 ARM_COMPUTE_UNUSED(linear_input_size);
102 ARM_COMPUTE_ERROR_ON(weights.shape().x() != linear_input_size);
103 ARM_COMPUTE_ERROR_ON(weights.shape().y() != bias.shape().x());
104 ARM_COMPUTE_ERROR_ON(weights.shape().y() != dst.shape().x());
105
106 // Compute reference
107 const int cols_weights = weights.shape().x();
108 const int rows_weights = weights.shape().y();
109 const int num_batches = dst_shape.total_size_upper(1);
110
111 for(int k = 0; k < num_batches; ++k)
112 {
113 vector_matrix_multiply<T>(src.data() + k * cols_weights,
114 weights.data(),
115 bias.data(),
116 dst.data() + k * rows_weights,
117 cols_weights,
118 rows_weights,
119 src.fixed_point_position());
120 }
121
122 return dst;
123}
124
125template SimpleTensor<float> fully_connected_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &dst_shape);
126template SimpleTensor<half_float::half> fully_connected_layer(const SimpleTensor<half_float::half> &src, const SimpleTensor<half_float::half> &weights, const SimpleTensor<half_float::half> &bias,
127 const TensorShape &dst_shape);
128template 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);
129template 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);
130} // namespace reference
131} // namespace validation
132} // namespace test
133} // namespace arm_compute