blob: 64a89aa6a0557353461688810e1f8941019df551 [file] [log] [blame]
Michele Di Giorgio32982d82017-07-07 14:44:43 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Michele Di Giorgio32982d82017-07-07 14:44:43 +01003 *
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 "DequantizationLayer.h"
25
Georgios Pinitas3d13af82019-06-04 13:04:16 +010026#include "Permute.h"
27
Michele Di Giorgio32982d82017-07-07 14:44:43 +010028namespace arm_compute
29{
30namespace test
31{
32namespace validation
33{
34namespace reference
35{
Georgios Pinitas3d13af82019-06-04 13:04:16 +010036namespace
Michele Di Giorgio32982d82017-07-07 14:44:43 +010037{
Georgios Pinitas3d13af82019-06-04 13:04:16 +010038template <typename TOut>
Sang-Hoon Parkd8176472019-12-04 09:46:28 +000039TOut dequantize(int8_t val, const UniformQuantizationInfo qinfo, DataType dt)
Georgios Pinitas3d13af82019-06-04 13:04:16 +010040{
Sang-Hoon Parkd8176472019-12-04 09:46:28 +000041 if(dt == DataType::QSYMM8 || dt == DataType::QSYMM8_PER_CHANNEL)
42 {
43 return static_cast<TOut>(dequantize_qsymm8(val, qinfo));
44 }
45 else
46 {
47 return static_cast<TOut>(dequantize_qasymm8_signed(val, qinfo));
48 }
Georgios Pinitas3d13af82019-06-04 13:04:16 +010049}
50template <typename TOut>
Sang-Hoon Parkd8176472019-12-04 09:46:28 +000051TOut dequantize(uint8_t val, const UniformQuantizationInfo qinfo, DataType dt)
Georgios Pinitas3d13af82019-06-04 13:04:16 +010052{
Sang-Hoon Parkd8176472019-12-04 09:46:28 +000053 ARM_COMPUTE_UNUSED(dt);
Georgios Pinitas3d13af82019-06-04 13:04:16 +010054 return static_cast<TOut>(dequantize_qasymm8(val, qinfo));
55}
Manuel Bottini10c53f12019-07-17 16:11:53 +010056template <typename TOut>
Sang-Hoon Parkd8176472019-12-04 09:46:28 +000057TOut dequantize(int16_t val, const UniformQuantizationInfo qinfo, DataType dt)
Manuel Bottini10c53f12019-07-17 16:11:53 +010058{
Sang-Hoon Parkd8176472019-12-04 09:46:28 +000059 ARM_COMPUTE_UNUSED(dt);
Manuel Bottini10c53f12019-07-17 16:11:53 +010060 return static_cast<TOut>(dequantize_qsymm16(val, qinfo));
61}
Michalis Spyrou3f632f32019-08-22 16:52:00 +010062} // namespace
Georgios Pinitas3d13af82019-06-04 13:04:16 +010063template <typename TOut, typename TIn>
Michalis Spyrou3f632f32019-08-22 16:52:00 +010064SimpleTensor<TOut> dequantization_layer(const SimpleTensor<TIn> &src)
Georgios Pinitas3d13af82019-06-04 13:04:16 +010065{
66 const DataType src_data_type = src.data_type();
67 const DataType dst_data_type = std::is_same<TOut, float>::value ? DataType::F32 : DataType::F16;
Michele Di Giorgio32982d82017-07-07 14:44:43 +010068
Georgios Pinitas3d13af82019-06-04 13:04:16 +010069 SimpleTensor<TOut> dst{ src.shape(), dst_data_type };
70
Michalis Spyrou29a01c92019-08-22 11:44:04 +010071 if(is_data_type_quantized_per_channel(src_data_type))
Michele Di Giorgio32982d82017-07-07 14:44:43 +010072 {
Georgios Pinitas3d13af82019-06-04 13:04:16 +010073 const int WH = src.shape().x() * src.shape().y();
74 const int C = src.shape().z();
75 const int N = src.shape().total_size() / (WH * C);
76
Georgios Pinitas8217c8e2019-11-11 18:24:22 +000077 const std::vector<float> qscales = src.quantization_info().scale();
Michalis Spyroud1d77222020-04-08 14:10:15 +010078#if defined(_OPENMP)
79 #pragma omp parallel for collapse(2)
80#endif /* _OPENMP */
Georgios Pinitas3d13af82019-06-04 13:04:16 +010081 for(int n = 0; n < N; ++n)
82 {
83 for(int c = 0; c < C; ++c)
84 {
85 const size_t idx = n * C * WH + c * WH;
Georgios Pinitas8217c8e2019-11-11 18:24:22 +000086 const UniformQuantizationInfo channel_qinfo = { qscales[c], 0 };
Georgios Pinitas3d13af82019-06-04 13:04:16 +010087
88 // Dequantize slice
89 for(int s = 0; s < WH; ++s)
90 {
Sang-Hoon Parkd8176472019-12-04 09:46:28 +000091 dst[idx + s] = dequantize<TOut>(static_cast<TIn>(src[idx + s]), channel_qinfo, src_data_type);
Georgios Pinitas3d13af82019-06-04 13:04:16 +010092 }
93 }
94 }
95 }
96 else
97 {
98 const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform();
99 ARM_COMPUTE_ERROR_ON(quantization_info.offset != 0 && src_data_type == DataType::QSYMM8);
Michalis Spyroud1d77222020-04-08 14:10:15 +0100100#if defined(_OPENMP)
101 #pragma omp parallel for
102#endif /* _OPENMP */
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100103 for(int i = 0; i < src.num_elements(); ++i)
104 {
Sang-Hoon Parkd8176472019-12-04 09:46:28 +0000105 dst[i] = static_cast<TOut>(dequantize<TOut>(static_cast<TIn>(src[i]), quantization_info, src_data_type));
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100106 }
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100107 }
108
109 return dst;
110}
111
Georgios Pinitas574775c2019-02-18 20:08:02 +0000112template SimpleTensor<half> dequantization_layer(const SimpleTensor<uint8_t> &src);
113template SimpleTensor<float> dequantization_layer(const SimpleTensor<uint8_t> &src);
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100114template SimpleTensor<half> dequantization_layer(const SimpleTensor<int8_t> &src);
115template SimpleTensor<float> dequantization_layer(const SimpleTensor<int8_t> &src);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100116template SimpleTensor<half> dequantization_layer(const SimpleTensor<int16_t> &src);
117template SimpleTensor<float> dequantization_layer(const SimpleTensor<int16_t> &src);
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100118} // namespace reference
119} // namespace validation
120} // namespace test
121} // namespace arm_compute