blob: cceee0421cdd11b0c2b5d0b1ea28ad8305af9b60 [file] [log] [blame]
Michele Di Giorgio32982d82017-07-07 14:44:43 +01001/*
Georgios Pinitas574775c2019-02-18 20:08:02 +00002 * Copyright (c) 2017-2019 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>
39TOut dequantize(int8_t val, const UniformQuantizationInfo qinfo)
40{
41 return static_cast<TOut>(dequantize_qsymm8(val, qinfo));
42}
43template <typename TOut>
44TOut dequantize(uint8_t val, const UniformQuantizationInfo qinfo)
45{
46 return static_cast<TOut>(dequantize_qasymm8(val, qinfo));
47}
Manuel Bottini10c53f12019-07-17 16:11:53 +010048template <typename TOut>
49TOut dequantize(int16_t val, const UniformQuantizationInfo qinfo)
50{
51 return static_cast<TOut>(dequantize_qsymm16(val, qinfo));
52}
Michele Di Giorgio32982d82017-07-07 14:44:43 +010053
Georgios Pinitas3d13af82019-06-04 13:04:16 +010054template <typename TOut, typename TIn>
55SimpleTensor<TOut> dequantization_layer_nchw(const SimpleTensor<TIn> &src)
56{
57 const DataType src_data_type = src.data_type();
58 const DataType dst_data_type = std::is_same<TOut, float>::value ? DataType::F32 : DataType::F16;
Michele Di Giorgio32982d82017-07-07 14:44:43 +010059
Georgios Pinitas3d13af82019-06-04 13:04:16 +010060 SimpleTensor<TOut> dst{ src.shape(), dst_data_type };
61
62 if(src_data_type == DataType::QSYMM8_PER_CHANNEL)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010063 {
Georgios Pinitas3d13af82019-06-04 13:04:16 +010064 const int WH = src.shape().x() * src.shape().y();
65 const int C = src.shape().z();
66 const int N = src.shape().total_size() / (WH * C);
67
68 const std::vector<float> qscales = src.quantization_info().scale();
69
70 for(int n = 0; n < N; ++n)
71 {
72 for(int c = 0; c < C; ++c)
73 {
74 const size_t idx = n * C * WH + c * WH;
75 const UniformQuantizationInfo channel_qinfo = { qscales[c], 0 };
76
77 // Dequantize slice
78 for(int s = 0; s < WH; ++s)
79 {
Manuel Bottini10c53f12019-07-17 16:11:53 +010080 dst[idx + s] = dequantize<TOut>(static_cast<TIn>(src[idx + s]), channel_qinfo);
Georgios Pinitas3d13af82019-06-04 13:04:16 +010081 }
82 }
83 }
84 }
85 else
86 {
87 const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform();
88 ARM_COMPUTE_ERROR_ON(quantization_info.offset != 0 && src_data_type == DataType::QSYMM8);
89
90 for(int i = 0; i < src.num_elements(); ++i)
91 {
Manuel Bottini10c53f12019-07-17 16:11:53 +010092 dst[i] = static_cast<TOut>(dequantize<TOut>(static_cast<TIn>(src[i]), quantization_info));
Georgios Pinitas3d13af82019-06-04 13:04:16 +010093 }
Michele Di Giorgio32982d82017-07-07 14:44:43 +010094 }
95
96 return dst;
97}
Georgios Pinitas3d13af82019-06-04 13:04:16 +010098} // namespace
99template <typename TOut, typename TIn>
100SimpleTensor<TOut> dequantization_layer(const SimpleTensor<TIn> &src)
101{
102 if(src.data_layout() == DataLayout::NHWC && src.data_type() == DataType::QSYMM8_PER_CHANNEL)
103 {
104 SimpleTensor<TIn> src_nchw = reference::permute<TIn>(src, PermutationVector(1U, 2U, 0U));
105 return reference::permute<TOut>(dequantization_layer_nchw<TOut>(src_nchw), PermutationVector(2U, 0U, 1U));
106 }
107 else
108 {
109 return dequantization_layer_nchw<TOut>(src);
110 }
111}
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100112
Georgios Pinitas574775c2019-02-18 20:08:02 +0000113template SimpleTensor<half> dequantization_layer(const SimpleTensor<uint8_t> &src);
114template SimpleTensor<float> dequantization_layer(const SimpleTensor<uint8_t> &src);
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100115template SimpleTensor<half> dequantization_layer(const SimpleTensor<int8_t> &src);
116template SimpleTensor<float> dequantization_layer(const SimpleTensor<int8_t> &src);
Manuel Bottini10c53f12019-07-17 16:11:53 +0100117template SimpleTensor<half> dequantization_layer(const SimpleTensor<int16_t> &src);
118template SimpleTensor<float> dequantization_layer(const SimpleTensor<int16_t> &src);
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100119} // namespace reference
120} // namespace validation
121} // namespace test
122} // namespace arm_compute