Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 1 | /* |
| 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 Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Types.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 27 | #include "tests/validation/FixedPoint.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 28 | #include "tests/validation/reference/UtilsQuantizedAsymm.h" |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 29 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 31 | |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 32 | #include <numeric> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace test |
| 37 | { |
| 38 | namespace validation |
| 39 | { |
| 40 | namespace reference |
| 41 | { |
| 42 | namespace |
| 43 | { |
| 44 | // Vector matrix multiply for floating point |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 45 | template < typename T, typename TB, typename std::enable_if < is_floating_point<T>::value &&is_floating_point<TB>::value, int >::type = 0 > |
| 46 | 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, |
| 47 | int rows_weights, uint8_t fixed_point_position) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 48 | { |
| 49 | ARM_COMPUTE_UNUSED(fixed_point_position); |
| 50 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 51 | 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 Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 56 | for(int y = 0; y < rows_weights; ++y) |
| 57 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 58 | 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 Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | // Vector matrix multiply for fixed point type |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 64 | template < typename T, typename TB, typename std::enable_if < std::is_integral<T>::value &&std::is_integral<TB>::value, int >::type = 0 > |
| 65 | 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, |
| 66 | int rows_weights, uint8_t fixed_point_position) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 67 | { |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 68 | 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 Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 73 | 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 Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 83 | 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 Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 85 | acc = acc + i_value * w_value; |
| 86 | } |
| 87 | |
| 88 | // Get the bias |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 89 | const fixed_point<T> b(bias_ptr[y], fixed_point_position, true); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 90 | |
| 91 | // Convert back and accumulate the bias |
| 92 | fixed_point<T> res(acc); |
| 93 | res = res + b; |
| 94 | |
| 95 | // Store the result |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 96 | dst_ptr[y] = res.raw(); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 97 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 98 | weights_ptr += cols_weights; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Vector matrix multiply for quantized type |
| 103 | template <> |
| 104 | void 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 Recas | 490b3d8 | 2017-12-19 15:42:25 +0000 | [diff] [blame] | 141 | acc = utility::clamp<int32_t>(acc, 0, 255); |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 142 | |
| 143 | // Store the result |
| 144 | dst_ptr[y] = static_cast<uint8_t>(acc); |
| 145 | |
| 146 | weights_ptr += cols_weights; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | } // namespace |
| 150 | |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 151 | template <typename T, typename TB> |
| 152 | SimpleTensor<T> fully_connected_layer(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, const TensorShape &dst_shape) |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 153 | { |
| 154 | // Create reference |
Georgios Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 155 | SimpleTensor<T> dst{ TensorShape{ dst_shape }, src.data_type(), 1, src.fixed_point_position(), src.quantization_info() }; |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 156 | |
| 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 Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 176 | 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 Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 185 | cols_weights, |
| 186 | rows_weights, |
| 187 | src.fixed_point_position()); |
| 188 | } |
| 189 | |
| 190 | return dst; |
| 191 | } |
| 192 | |
| 193 | template SimpleTensor<float> fully_connected_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &dst_shape); |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 194 | template SimpleTensor<half> fully_connected_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &dst_shape); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 195 | template 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); |
| 196 | template 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 Pinitas | 45bcc3a | 2017-11-29 11:06:49 +0000 | [diff] [blame] | 197 | 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); |
Moritz Pflanzer | 69d3341 | 2017-08-09 11:45:15 +0100 | [diff] [blame] | 198 | } // namespace reference |
| 199 | } // namespace validation |
| 200 | } // namespace test |
| 201 | } // namespace arm_compute |