/* * 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 "DequantizationLayer.h" #include "Permute.h" namespace arm_compute { namespace test { namespace validation { namespace reference { namespace { template TOut dequantize(int8_t val, const UniformQuantizationInfo qinfo, DataType dt) { if(dt == DataType::QSYMM8 || dt == DataType::QSYMM8_PER_CHANNEL) { return static_cast(dequantize_qsymm8(val, qinfo)); } else { return static_cast(dequantize_qasymm8_signed(val, qinfo)); } } template TOut dequantize(uint8_t val, const UniformQuantizationInfo qinfo, DataType dt) { ARM_COMPUTE_UNUSED(dt); return static_cast(dequantize_qasymm8(val, qinfo)); } template TOut dequantize(int16_t val, const UniformQuantizationInfo qinfo, DataType dt) { ARM_COMPUTE_UNUSED(dt); return static_cast(dequantize_qsymm16(val, qinfo)); } } // namespace template SimpleTensor dequantization_layer(const SimpleTensor &src) { const DataType src_data_type = src.data_type(); const DataType dst_data_type = std::is_same::value ? DataType::F32 : DataType::F16; SimpleTensor dst{ src.shape(), dst_data_type }; if(is_data_type_quantized_per_channel(src_data_type)) { const int WH = src.shape().x() * src.shape().y(); const int C = src.shape().z(); const int N = src.shape().total_size() / (WH * C); const std::vector qscales = src.quantization_info().scale(); #if defined(_OPENMP) #pragma omp parallel for collapse(2) #endif /* _OPENMP */ for(int n = 0; n < N; ++n) { for(int c = 0; c < C; ++c) { const size_t idx = n * C * WH + c * WH; const UniformQuantizationInfo channel_qinfo = { qscales[c], 0 }; // Dequantize slice for(int s = 0; s < WH; ++s) { dst[idx + s] = dequantize(static_cast(src[idx + s]), channel_qinfo, src_data_type); } } } } else { const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform(); ARM_COMPUTE_ERROR_ON(quantization_info.offset != 0 && src_data_type == DataType::QSYMM8); #if defined(_OPENMP) #pragma omp parallel for #endif /* _OPENMP */ for(int i = 0; i < src.num_elements(); ++i) { dst[i] = static_cast(dequantize(static_cast(src[i]), quantization_info, src_data_type)); } } return dst; } template SimpleTensor dequantization_layer(const SimpleTensor &src); template SimpleTensor dequantization_layer(const SimpleTensor &src); template SimpleTensor dequantization_layer(const SimpleTensor &src); template SimpleTensor dequantization_layer(const SimpleTensor &src); template SimpleTensor dequantization_layer(const SimpleTensor &src); template SimpleTensor dequantization_layer(const SimpleTensor &src); } // namespace reference } // namespace validation } // namespace test } // namespace arm_compute