/* * Copyright (c) 2017-2020 Arm Limited. * * SPDX-License-Identifier: MIT * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include "GEMMLowp.h" #include "arm_compute/core/Types.h" #include "tests/validation/reference/UtilsQuantizedAsymm.h" #include "support/ToolchainSupport.h" #include namespace arm_compute { namespace test { namespace validation { namespace reference { namespace { template struct DataTypeExtractor { static DataType data_type() { DataType data_type = DataType::UNKNOWN; if(std::is_same::value) { data_type = DataType::QASYMM8_SIGNED; } else if(std::is_same::value) { data_type = DataType::QASYMM8; } else if(std::is_same::value) { data_type = DataType::QSYMM16; } return data_type; } }; template void quantize_down_scale(const SimpleTensor *in, const SimpleTensor *bias, SimpleTensor *dst, int32_t result_offset, std::vector result_mult_int, std::vector result_shift, int32_t min, int32_t max) { const int cols_in = in->shape().x(); const bool is_per_channel = result_mult_int.size() > 1; #if defined(_OPENMP) #pragma omp parallel for #endif /* _OPENMP */ for(int i = 0; i < in->num_elements(); ++i) { int32_t result = ((*in)[i] + result_offset); if(bias != nullptr) { result += (*bias)[i % cols_in]; } result *= (is_per_channel) ? result_mult_int[i % cols_in] : result_mult_int[0]; result >>= (is_per_channel) ? result_shift[i % cols_in] : result_shift[0]; // Bounded ReLu if(min != max) { result = std::max(min, std::min(max, result)); } (*dst)[i] = static_cast(std::max(std::numeric_limits::lowest(), std::min(std::numeric_limits::max(), result))); } } template void quantize_down_scale_by_fixedpoint(const SimpleTensor *in, const SimpleTensor *bias, SimpleTensor *dst, std::vector result_fixedpoint_multiplier, std::vector result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max) { const int cols_in = in->shape().x(); const bool is_per_channel = result_fixedpoint_multiplier.size() > 1; #if defined(_OPENMP) #pragma omp parallel for #endif /* _OPENMP */ for(int i = 0; i < in->num_elements(); ++i) { TIn result = (*in)[i]; if(bias != nullptr) { result += (*bias)[i % cols_in]; } // Fixed point multiplication const int32_t multiplier = (is_per_channel) ? result_fixedpoint_multiplier[i % cols_in] : result_fixedpoint_multiplier[0]; const int32_t shift = (is_per_channel) ? result_shift[i % cols_in] : result_shift[0]; if(shift < 0) { result = asymm_int_mult(result * (1 << (-shift)), multiplier); } else { result = asymm_rounding_divide_by_pow2(asymm_int_mult(result, multiplier), shift); } result += result_offset_after_shift; // Bounded ReLu if(min != max) { result = std::max(min, std::min(max, result)); } (*dst)[i] = static_cast(std::max(std::numeric_limits::lowest(), std::min(std::numeric_limits::max(), result))); } } template void quantize_down_scale_by_float(const SimpleTensor *in, const SimpleTensor *bias, SimpleTensor *dst, std::vector result_real_multiplier, int32_t result_offset, int32_t min, int32_t max) { const int cols_in = in->shape().x(); const bool is_per_channel = result_real_multiplier.size() > 1; #if defined(_OPENMP) #pragma omp parallel for #endif /* _OPENMP */ for(int i = 0; i < in->num_elements(); ++i) { TIn result = (*in)[i]; if(bias != nullptr) { result += (*bias)[i % cols_in]; } // Float multiplication const float_t multiplier = (is_per_channel) ? result_real_multiplier[i % cols_in] : result_real_multiplier[0]; float_t result_f = static_cast(result) * multiplier + static_cast(result_offset); result = static_cast(support::cpp11::round(result_f)); // Bounded ReLu if(min != max) { result = std::max(min, std::min(max, result)); } (*dst)[i] = static_cast(std::max(std::numeric_limits::lowest(), std::min(std::numeric_limits::max(), result))); } } } // namespace template SimpleTensor gemmlowp_matrix_multiply_core(const SimpleTensor &a, const SimpleTensor &b, TensorShape shape_c, int32_t a_offset, int32_t b_offset) { static_assert(std::is_same::type, int32_t>::value, "Only int32_t is allowed for the output"); DataType dt = std::is_same::value ? DataType::S32 : DataType::U32; SimpleTensor c(shape_c, dt); const int K = a.shape().x(); const int M = a.shape().y(); const int N = b.shape().x(); const int D = a.shape().z(); // Number of matrices in a batch const int a_stride_z = K * M; // Do not slide the matrix B along the 3rd dimension in case matrix B has less than 3 dimensions const int b_stride_z = b.shape().num_dimensions() > 2 ? N * K : 0; const int c_stride_z = N * M; std::vector acc; acc.resize(N); for(int depth = 0; depth < D; ++depth) { const int base_addr_a = depth * a_stride_z; const int base_addr_b = depth * b_stride_z; const int base_addr_c = depth * c_stride_z; for(int i = 0; i < M; ++i) { for(int j = 0; j < N; ++j) { acc[j] = 0; } for(int k = 0; k < K; ++k) { const T_out tmp_a = a_offset + static_cast(a[base_addr_a + k + i * K]); for(int j = 0; j < N; ++j) { const T_out tmp_b = b_offset + static_cast(b[base_addr_b + j + k * N]); const T_out mult_as_int = tmp_a * tmp_b; acc[j] += mult_as_int; } } for(int j = 0; j < N; ++j) { c[base_addr_c + j + i * N] = acc[j]; } } } return c; } // used to validate assembly kernels which don't know anything about offsets template SimpleTensor gemmlowp(const SimpleTensor &a, const SimpleTensor &b, TensorShape shape_c) { return gemmlowp_matrix_multiply_core(a, b, shape_c, 0, 0); } template SimpleTensor gemmlowp_quantize_down_scale(const SimpleTensor &in, int32_t result_offset, std::vector result_mult_int, std::vector result_shift, int32_t min, int32_t max) { SimpleTensor dst(in.shape(), DataTypeExtractor::data_type()); quantize_down_scale(&in, nullptr, &dst, result_offset, result_mult_int, result_shift, min, max); return dst; } template SimpleTensor gemmlowp_quantize_down_scale(const SimpleTensor &in, const SimpleTensor &bias, int32_t result_offset, std::vector result_mult_int, std::vector result_shift, int32_t min, int32_t max) { SimpleTensor dst(in.shape(), DataTypeExtractor::data_type()); quantize_down_scale(&in, &bias, &dst, result_offset, result_mult_int, result_shift, min, max); return dst; } template SimpleTensor gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor &in, std::vector result_fixedpoint_multiplier, std::vector result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max) { SimpleTensor dst(in.shape(), DataTypeExtractor::data_type()); quantize_down_scale_by_fixedpoint(&in, nullptr, &dst, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max); return dst; } template SimpleTensor gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor &in, const SimpleTensor &bias, std::vector result_fixedpoint_multiplier, std::vector result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max) { SimpleTensor dst(in.shape(), DataTypeExtractor::data_type()); quantize_down_scale_by_fixedpoint(&in, &bias, &dst, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, min, max); return dst; } template SimpleTensor gemmlowp_quantize_down_scale_by_float(const SimpleTensor &in, const SimpleTensor &bias, std::vector result_real_multiplier, int32_t result_offset, int32_t min, int32_t max) { SimpleTensor dst(in.shape(), DataTypeExtractor::data_type()); quantize_down_scale_by_float(&in, &bias, &dst, result_real_multiplier, result_offset, min, max); return dst; } template SimpleTensor gemmlowp_quantize_down_scale_by_float(const SimpleTensor &in, std::vector result_real_multiplier, int32_t result_offset, int32_t min, int32_t max) { SimpleTensor dst(in.shape(), DataTypeExtractor::data_type()); quantize_down_scale_by_float(&in, nullptr, &dst, result_real_multiplier, result_offset, min, max); return dst; } template SimpleTensor gemmlowp_quantize_down_scale_by_float(const SimpleTensor &a, const SimpleTensor &b, std::vector result_real_multiplier, int32_t result_offset, int32_t min, int32_t max); template SimpleTensor gemmlowp_quantize_down_scale_by_float(const SimpleTensor &a, std::vector result_real_multiplier, int32_t result_offset, int32_t min, int32_t max); template SimpleTensor gemmlowp_quantize_down_scale_by_float(const SimpleTensor &a, const SimpleTensor &b, std::vector result_real_multiplier, int32_t result_offset, int32_t min, int32_t max); template SimpleTensor gemmlowp_quantize_down_scale_by_float(const SimpleTensor &a, std::vector result_real_multiplier, int32_t result_offset, int32_t min, int32_t max); template SimpleTensor gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor &a, std::vector result_fixedpoint_multiplier, std::vector result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max); template SimpleTensor gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor &a, const SimpleTensor &b, std::vector result_fixedpoint_multiplier, std::vector result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max); template SimpleTensor gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor &a, std::vector result_fixedpoint_multiplier, std::vector result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max); template SimpleTensor gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor &a, const SimpleTensor &b, std::vector result_fixedpoint_multiplier, std::vector result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max); template SimpleTensor gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor &a, std::vector result_fixedpoint_multiplier, std::vector result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max); template SimpleTensor gemmlowp_quantize_down_scale_by_fixedpoint(const SimpleTensor &a, const SimpleTensor &b, std::vector result_fixedpoint_multiplier, std::vector result_shift, int32_t result_offset_after_shift, int32_t min, int32_t max); template SimpleTensor gemmlowp_quantize_down_scale(const SimpleTensor &a, int32_t result_offset, std::vector result_mult_int, std::vector result_shift, int32_t min, int32_t max); template SimpleTensor gemmlowp_quantize_down_scale(const SimpleTensor &a, const SimpleTensor &b, int32_t result_offset, std::vector result_mult_int, std::vector result_shift, int32_t min, int32_t max); template SimpleTensor gemmlowp_quantize_down_scale(const SimpleTensor &a, int32_t result_offset, std::vector result_mult_int, std::vector result_shift, int32_t min, int32_t max); template SimpleTensor gemmlowp_quantize_down_scale(const SimpleTensor &a, const SimpleTensor &b, int32_t result_offset, std::vector result_mult_int, std::vector result_shift, int32_t min, int32_t max); template SimpleTensor gemmlowp_matrix_multiply_core(const SimpleTensor &a, const SimpleTensor &b, TensorShape shape_c, int32_t a_offset, int32_t b_offset); template SimpleTensor gemmlowp_matrix_multiply_core(const SimpleTensor &a, const SimpleTensor &b, TensorShape shape_c, int32_t a_offset, int32_t b_offset); template SimpleTensor gemmlowp(const SimpleTensor &a, const SimpleTensor &b, TensorShape shape_c); template SimpleTensor gemmlowp(const SimpleTensor &a, const SimpleTensor &b, TensorShape shape_c); template SimpleTensor gemmlowp(const SimpleTensor &a, const SimpleTensor &b, TensorShape shape_c); } // namespace reference } // namespace validation } // namespace test } // namespace arm_compute