Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 1 | /* |
John Kesapides | 341b218 | 2019-02-22 10:05:29 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 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; |
| 52 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 53 | for(int y = 0; y < rows_weights; ++y) |
| 54 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 55 | 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 Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 60 | // Vector matrix multiply for quantized type |
| 61 | template < 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 > |
| 62 | 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, |
| 63 | int cols_weights, int rows_weights) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 64 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 65 | 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 Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 70 | const UniformQuantizationInfo iq_info = src.quantization_info().uniform(); |
| 71 | const UniformQuantizationInfo wq_info = weights.quantization_info().uniform(); |
| 72 | const UniformQuantizationInfo oq_info = dst.quantization_info().uniform(); |
| 73 | |
| 74 | const int input_offset = -iq_info.offset; |
| 75 | const float input_scale = iq_info.scale; |
| 76 | const int weights_offset = -wq_info.offset; |
| 77 | const float weights_scale = wq_info.scale; |
| 78 | const int output_offset = oq_info.offset; |
| 79 | const float output_scale = oq_info.scale; |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 80 | |
| 81 | int output_multiplier = 0; |
| 82 | int output_shift = 0; |
| 83 | const float multiplier = input_scale * weights_scale / output_scale; |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 84 | arm_compute::quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 85 | |
| 86 | for(int y = 0; y < rows_weights; ++y) |
| 87 | { |
| 88 | // Reset accumulator |
| 89 | int32_t acc = 0; |
| 90 | |
| 91 | for(int x = 0; x < cols_weights; ++x) |
| 92 | { |
| 93 | acc += (src_ptr[x] + input_offset) * (weights_ptr[x] + weights_offset); |
| 94 | } |
| 95 | |
| 96 | // Accumulate the bias |
| 97 | acc += bias_ptr[y]; |
| 98 | |
Michele Di Giorgio | 14cbfb2 | 2019-10-23 10:53:10 +0100 | [diff] [blame] | 99 | // Quantize down |
| 100 | acc = quantize_down_scale_by_fixedpoint(acc, output_multiplier, output_shift, output_offset, 0, 255); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 101 | |
| 102 | // Store the result |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 103 | dst_ptr[y] = static_cast<T>(acc); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 104 | |
| 105 | weights_ptr += cols_weights; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } // namespace |
| 109 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 110 | template <typename T, typename TB> |
John Kesapides | 341b218 | 2019-02-22 10:05:29 +0000 | [diff] [blame] | 111 | 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] | 112 | { |
John Kesapides | 341b218 | 2019-02-22 10:05:29 +0000 | [diff] [blame] | 113 | // if no explicit quantization has been set you the same as src |
| 114 | if(out_quant_info == QuantizationInfo()) |
| 115 | { |
| 116 | out_quant_info = src.quantization_info(); |
| 117 | } |
| 118 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 119 | // Create reference |
John Kesapides | 341b218 | 2019-02-22 10:05:29 +0000 | [diff] [blame] | 120 | 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] | 121 | |
| 122 | // Sanity checks |
| 123 | const int num_batch_dimensions = std::max(0, static_cast<int>(dst_shape.num_dimensions()) - 1); |
| 124 | const int num_input_dimensions = src.shape().num_dimensions() - num_batch_dimensions; |
| 125 | const unsigned int linear_input_size = src.shape().total_size_lower(num_input_dimensions); |
| 126 | |
| 127 | ARM_COMPUTE_UNUSED(num_batch_dimensions); |
| 128 | ARM_COMPUTE_UNUSED(num_input_dimensions); |
| 129 | ARM_COMPUTE_UNUSED(linear_input_size); |
| 130 | ARM_COMPUTE_ERROR_ON(weights.shape().x() != linear_input_size); |
| 131 | ARM_COMPUTE_ERROR_ON(weights.shape().y() != bias.shape().x()); |
| 132 | ARM_COMPUTE_ERROR_ON(weights.shape().y() != dst.shape().x()); |
| 133 | |
| 134 | // Compute reference |
| 135 | const int cols_weights = weights.shape().x(); |
| 136 | const int rows_weights = weights.shape().y(); |
| 137 | const int num_batches = dst_shape.total_size_upper(1); |
| 138 | |
| 139 | for(int k = 0; k < num_batches; ++k) |
| 140 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 141 | const int offset_in = k * cols_weights; |
| 142 | const int offset_out = k * rows_weights; |
| 143 | |
| 144 | vector_matrix_multiply<T>(src, |
| 145 | weights, |
| 146 | bias, |
| 147 | dst, |
| 148 | offset_in, |
| 149 | offset_out, |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 150 | cols_weights, |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 151 | rows_weights); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | return dst; |
| 155 | } |
| 156 | |
John Kesapides | 341b218 | 2019-02-22 10:05:29 +0000 | [diff] [blame] | 157 | template SimpleTensor<float> fully_connected_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &dst_shape, |
| 158 | QuantizationInfo out_quant_info); |
| 159 | template SimpleTensor<half> fully_connected_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &dst_shape, |
| 160 | QuantizationInfo out_quant_info); |
| 161 | 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, |
| 162 | QuantizationInfo out_quant_info); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 163 | } // namespace reference |
| 164 | } // namespace validation |
| 165 | } // namespace test |
| 166 | } // namespace arm_compute |