From 45bcc3a1c287a208098ae99288273a5129ddd5eb Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Wed, 29 Nov 2017 11:06:49 +0000 Subject: COMPMID-661: QASYMM8 support for fully connected layer. Change-Id: I70e04d3a175ba366432ada98e9ca893c9f81b260 Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/111094 Reviewed-by: Gian Marco Iodice Tested-by: BSG Visual Compute Jenkins server to access repositories on http://mpd-gerrit.cambridge.arm.com Reviewed-by: Anthony Barbier --- tests/validation/CPP/FullyConnectedLayer.cpp | 105 ++++++++++++++++++++++----- tests/validation/CPP/FullyConnectedLayer.h | 4 +- 2 files changed, 89 insertions(+), 20 deletions(-) (limited to 'tests/validation/CPP') diff --git a/tests/validation/CPP/FullyConnectedLayer.cpp b/tests/validation/CPP/FullyConnectedLayer.cpp index 2b32c4b161..6b618a955c 100644 --- a/tests/validation/CPP/FullyConnectedLayer.cpp +++ b/tests/validation/CPP/FullyConnectedLayer.cpp @@ -24,8 +24,11 @@ #include "FullyConnectedLayer.h" #include "arm_compute/core/Types.h" +#include "tests/validation/CPP/UtilsQuantizedAsymm.h" #include "tests/validation/FixedPoint.h" +#include "arm_compute/core/utils/quantization/AsymmHelpers.h" + #include namespace arm_compute @@ -39,22 +42,34 @@ namespace reference namespace { // Vector matrix multiply for floating point -template ::value, int>::type = 0> -void 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) +template < typename T, typename TB, typename std::enable_if < is_floating_point::value &&is_floating_point::value, int >::type = 0 > +void vector_matrix_multiply(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, SimpleTensor &dst, int offset_src, int offset_dst, int cols_weights, + int rows_weights, uint8_t fixed_point_position) { ARM_COMPUTE_UNUSED(fixed_point_position); + const T *src_ptr = src.data() + offset_src; + const T *weights_ptr = weights.data(); + const TB *bias_ptr = bias.data(); + T *dst_ptr = dst.data() + offset_dst; + for(int y = 0; y < rows_weights; ++y) { - dst[y] = std::inner_product(src, src + cols_weights, weights, static_cast(0)) + bias[y]; - weights += cols_weights; + dst_ptr[y] = std::inner_product(src_ptr, src_ptr + cols_weights, weights_ptr, static_cast(0)) + bias_ptr[y]; + weights_ptr += cols_weights; } } // Vector matrix multiply for fixed point type -template ::value, int>::type = 0> -void 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) +template < typename T, typename TB, typename std::enable_if < std::is_integral::value &&std::is_integral::value, int >::type = 0 > +void vector_matrix_multiply(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, SimpleTensor &dst, int offset_src, int offset_dst, int cols_weights, + int rows_weights, uint8_t fixed_point_position) { + const T *src_ptr = src.data() + offset_src; + const T *weights_ptr = weights.data(); + const TB *bias_ptr = bias.data(); + T *dst_ptr = dst.data() + offset_dst; + using namespace fixed_point_arithmetic; using promoted_type = fixed_point_arithmetic::traits::promote_t; @@ -65,31 +80,79 @@ void vector_matrix_multiply(const T *src, const T *weights, const T *bias, T *ds for(int x = 0; x < cols_weights; ++x) { - const fixed_point i_value(src[x], fixed_point_position, true); - const fixed_point w_value(weights[x], fixed_point_position, true); + const fixed_point i_value(src_ptr[x], fixed_point_position, true); + const fixed_point w_value(weights_ptr[x], fixed_point_position, true); acc = acc + i_value * w_value; } // Get the bias - const fixed_point b(bias[y], fixed_point_position, true); + const fixed_point b(bias_ptr[y], fixed_point_position, true); // Convert back and accumulate the bias fixed_point res(acc); res = res + b; // Store the result - dst[y] = res.raw(); + dst_ptr[y] = res.raw(); + + weights_ptr += cols_weights; + } +} + +// Vector matrix multiply for quantized type +template <> +void vector_matrix_multiply(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, SimpleTensor &dst, int offset_src, int offset_dst, + int cols_weights, int rows_weights, uint8_t fixed_point_position) +{ + ARM_COMPUTE_UNUSED(fixed_point_position); + + const uint8_t *src_ptr = src.data() + offset_src; + const uint8_t *weights_ptr = weights.data(); + const int32_t *bias_ptr = bias.data(); + uint8_t *dst_ptr = dst.data() + offset_dst; + + const int input_offset = -src.quantization_info().offset; + const float input_scale = src.quantization_info().scale; + const int weights_offset = -weights.quantization_info().offset; + const float weights_scale = weights.quantization_info().scale; + const int output_offset = dst.quantization_info().offset; + const float output_scale = dst.quantization_info().scale; + + int output_multiplier = 0; + int output_shift = 0; + const float multiplier = input_scale * weights_scale / output_scale; + arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift); + + for(int y = 0; y < rows_weights; ++y) + { + // Reset accumulator + int32_t acc = 0; + + for(int x = 0; x < cols_weights; ++x) + { + acc += (src_ptr[x] + input_offset) * (weights_ptr[x] + weights_offset); + } + + // Accumulate the bias + acc += bias_ptr[y]; + + acc = asymm_rounding_divide_by_pow2(asymm_int_mult(acc, output_multiplier), output_shift); + acc += output_offset; + acc = clamp(acc, 0, 255); + + // Store the result + dst_ptr[y] = static_cast(acc); - weights += cols_weights; + weights_ptr += cols_weights; } } } // namespace -template -SimpleTensor fully_connected_layer(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, const TensorShape &dst_shape) +template +SimpleTensor fully_connected_layer(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, const TensorShape &dst_shape) { // Create reference - SimpleTensor dst{ TensorShape{ dst_shape }, src.data_type(), 1, src.fixed_point_position() }; + SimpleTensor dst{ TensorShape{ dst_shape }, src.data_type(), 1, src.fixed_point_position(), src.quantization_info() }; // Sanity checks const int num_batch_dimensions = std::max(0, static_cast(dst_shape.num_dimensions()) - 1); @@ -110,10 +173,15 @@ SimpleTensor fully_connected_layer(const SimpleTensor &src, const SimpleTe for(int k = 0; k < num_batches; ++k) { - vector_matrix_multiply(src.data() + k * cols_weights, - weights.data(), - bias.data(), - dst.data() + k * rows_weights, + const int offset_in = k * cols_weights; + const int offset_out = k * rows_weights; + + vector_matrix_multiply(src, + weights, + bias, + dst, + offset_in, + offset_out, cols_weights, rows_weights, src.fixed_point_position()); @@ -126,6 +194,7 @@ template SimpleTensor fully_connected_layer(const SimpleTensor &sr template SimpleTensor fully_connected_layer(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, const TensorShape &dst_shape); template SimpleTensor fully_connected_layer(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, const TensorShape &dst_shape); template SimpleTensor fully_connected_layer(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, const TensorShape &dst_shape); +template SimpleTensor fully_connected_layer(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, const TensorShape &dst_shape); } // namespace reference } // namespace validation } // namespace test diff --git a/tests/validation/CPP/FullyConnectedLayer.h b/tests/validation/CPP/FullyConnectedLayer.h index 05c570a2c0..1dfb496924 100644 --- a/tests/validation/CPP/FullyConnectedLayer.h +++ b/tests/validation/CPP/FullyConnectedLayer.h @@ -35,8 +35,8 @@ namespace validation { namespace reference { -template -SimpleTensor fully_connected_layer(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, const TensorShape &dst_shape); +template +SimpleTensor fully_connected_layer(const SimpleTensor &src, const SimpleTensor &weights, const SimpleTensor &bias, const TensorShape &dst_shape); } // namespace reference } // namespace validation } // namespace test -- cgit v1.2.1