Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 1 | /* |
ramelg01 | b2eba7f | 2021-12-23 08:32:08 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 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 Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Types.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 27 | #include "tests/validation/reference/UtilsQuantizedAsymm.h" |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 28 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 30 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 31 | #include <numeric> |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace test |
| 36 | { |
| 37 | namespace validation |
| 38 | { |
| 39 | namespace reference |
| 40 | { |
| 41 | namespace |
| 42 | { |
| 43 | // Vector matrix multiply for floating point |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 44 | template < typename T, typename TB, typename std::enable_if < is_floating_point<T>::value &&is_floating_point<TB>::value, int >::type = 0 > |
| 45 | 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, |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 46 | int rows_weights) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 47 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 48 | 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; |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 52 | #if defined(_OPENMP) |
| 53 | #pragma omp parallel for |
| 54 | #endif /* _OPENMP */ |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 55 | for(int y = 0; y < rows_weights; ++y) |
| 56 | { |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 57 | dst_ptr[y] = std::inner_product(src_ptr, src_ptr + cols_weights, &weights_ptr[cols_weights * y], static_cast<T>(0)) + bias_ptr[y]; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 61 | // Vector matrix multiply for quantized type |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 62 | 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 > |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 63 | 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 | int cols_weights, int rows_weights) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 65 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 66 | const T *src_ptr = src.data() + offset_src; |
| 67 | const T *weights_ptr = weights.data(); |
| 68 | const TB *bias_ptr = bias.data(); |
| 69 | T *dst_ptr = dst.data() + offset_dst; |
| 70 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 71 | const UniformQuantizationInfo iq_info = src.quantization_info().uniform(); |
| 72 | const UniformQuantizationInfo wq_info = weights.quantization_info().uniform(); |
| 73 | const UniformQuantizationInfo oq_info = dst.quantization_info().uniform(); |
| 74 | |
| 75 | const int input_offset = -iq_info.offset; |
| 76 | const float input_scale = iq_info.scale; |
| 77 | const int weights_offset = -wq_info.offset; |
| 78 | const float weights_scale = wq_info.scale; |
| 79 | const int output_offset = oq_info.offset; |
| 80 | const float output_scale = oq_info.scale; |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 81 | |
| 82 | int output_multiplier = 0; |
| 83 | int output_shift = 0; |
| 84 | const float multiplier = input_scale * weights_scale / output_scale; |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 85 | arm_compute::quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 86 | |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 87 | const int min = std::numeric_limits<T>::lowest(); |
| 88 | const int max = std::numeric_limits<T>::max(); |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 89 | #if defined(_OPENMP) |
| 90 | #pragma omp parallel for |
| 91 | #endif /* _OPENMP */ |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 92 | for(int y = 0; y < rows_weights; ++y) |
| 93 | { |
| 94 | // Reset accumulator |
| 95 | int32_t acc = 0; |
| 96 | |
| 97 | for(int x = 0; x < cols_weights; ++x) |
| 98 | { |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 99 | acc += (src_ptr[x] + input_offset) * (weights_ptr[x + y * cols_weights] + weights_offset); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Accumulate the bias |
| 103 | acc += bias_ptr[y]; |
| 104 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 105 | // Quantize down |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 106 | acc = quantize_down_scale_by_fixedpoint(acc, output_multiplier, output_shift, output_offset, min, max); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 107 | |
| 108 | // Store the result |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 109 | dst_ptr[y] = static_cast<T>(acc); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } // namespace |
| 113 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 114 | template <typename T, typename TB> |
John Kesapides | 341b218 | 2019-02-22 10:05:29 +0000 | [diff] [blame] | 115 | 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) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 116 | { |
John Kesapides | 341b218 | 2019-02-22 10:05:29 +0000 | [diff] [blame] | 117 | // if no explicit quantization has been set you the same as src |
| 118 | if(out_quant_info == QuantizationInfo()) |
| 119 | { |
| 120 | out_quant_info = src.quantization_info(); |
| 121 | } |
| 122 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 123 | // Create reference |
John Kesapides | 341b218 | 2019-02-22 10:05:29 +0000 | [diff] [blame] | 124 | SimpleTensor<T> dst{ TensorShape{ dst_shape }, src.data_type(), 1, out_quant_info }; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 125 | |
ramelg01 | b2eba7f | 2021-12-23 08:32:08 +0000 | [diff] [blame] | 126 | // Health checks |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 127 | const int num_batch_dimensions = std::max(0, static_cast<int>(dst_shape.num_dimensions()) - 1); |
| 128 | const int num_input_dimensions = src.shape().num_dimensions() - num_batch_dimensions; |
| 129 | const unsigned int linear_input_size = src.shape().total_size_lower(num_input_dimensions); |
| 130 | |
| 131 | ARM_COMPUTE_UNUSED(num_batch_dimensions); |
| 132 | ARM_COMPUTE_UNUSED(num_input_dimensions); |
| 133 | ARM_COMPUTE_UNUSED(linear_input_size); |
| 134 | ARM_COMPUTE_ERROR_ON(weights.shape().x() != linear_input_size); |
| 135 | ARM_COMPUTE_ERROR_ON(weights.shape().y() != bias.shape().x()); |
| 136 | ARM_COMPUTE_ERROR_ON(weights.shape().y() != dst.shape().x()); |
| 137 | |
| 138 | // Compute reference |
| 139 | const int cols_weights = weights.shape().x(); |
| 140 | const int rows_weights = weights.shape().y(); |
| 141 | const int num_batches = dst_shape.total_size_upper(1); |
| 142 | |
| 143 | for(int k = 0; k < num_batches; ++k) |
| 144 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 145 | const int offset_in = k * cols_weights; |
| 146 | const int offset_out = k * rows_weights; |
| 147 | |
| 148 | vector_matrix_multiply<T>(src, |
| 149 | weights, |
| 150 | bias, |
| 151 | dst, |
| 152 | offset_in, |
| 153 | offset_out, |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 154 | cols_weights, |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 155 | rows_weights); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return dst; |
| 159 | } |
| 160 | |
John Kesapides | 341b218 | 2019-02-22 10:05:29 +0000 | [diff] [blame] | 161 | template SimpleTensor<float> fully_connected_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &dst_shape, |
| 162 | QuantizationInfo out_quant_info); |
| 163 | template SimpleTensor<half> fully_connected_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &dst_shape, |
| 164 | QuantizationInfo out_quant_info); |
| 165 | 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 | QuantizationInfo out_quant_info); |
Michele Di Giorgio | 9c70037 | 2020-01-08 11:33:44 +0000 | [diff] [blame] | 167 | 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 | QuantizationInfo out_quant_info); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 169 | } // namespace reference |
| 170 | } // namespace validation |
| 171 | } // namespace test |
| 172 | } // namespace arm_compute |