Compute Library
 21.08
DequantizationLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 Arm Limited.
3  *
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 
26 #include "Permute.h"
27 
28 namespace arm_compute
29 {
30 namespace test
31 {
32 namespace validation
33 {
34 namespace reference
35 {
36 namespace
37 {
38 template <typename TOut>
39 TOut dequantize(int8_t val, const UniformQuantizationInfo qinfo, DataType dt)
40 {
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  }
49 }
50 template <typename TOut>
51 TOut dequantize(uint8_t val, const UniformQuantizationInfo qinfo, DataType dt)
52 {
54  return static_cast<TOut>(dequantize_qasymm8(val, qinfo));
55 }
56 template <typename TOut>
57 TOut dequantize(int16_t val, const UniformQuantizationInfo qinfo, DataType dt)
58 {
60  return static_cast<TOut>(dequantize_qsymm16(val, qinfo));
61 }
62 } // namespace
63 template <typename TOut, typename TIn>
65 {
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;
68 
69  SimpleTensor<TOut> dst{ src.shape(), dst_data_type };
70 
71  if(is_data_type_quantized_per_channel(src_data_type))
72  {
73  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 
77  const std::vector<float> qscales = src.quantization_info().scale();
78 #if defined(_OPENMP)
79  #pragma omp parallel for collapse(2)
80 #endif /* _OPENMP */
81  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;
86  const UniformQuantizationInfo channel_qinfo = { qscales[c], 0 };
87 
88  // Dequantize slice
89  for(int s = 0; s < WH; ++s)
90  {
91  dst[idx + s] = dequantize<TOut>(static_cast<TIn>(src[idx + s]), channel_qinfo, src_data_type);
92  }
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);
100 #if defined(_OPENMP)
101  #pragma omp parallel for
102 #endif /* _OPENMP */
103  for(int i = 0; i < src.num_elements(); ++i)
104  {
105  dst[i] = static_cast<TOut>(dequantize<TOut>(static_cast<TIn>(src[i]), quantization_info, src_data_type));
106  }
107  }
108 
109  return dst;
110 }
111 
118 } // namespace reference
119 } // namespace validation
120 } // namespace test
121 } // namespace arm_compute
float dequantize_qasymm8(uint8_t value, const INFO_TYPE &qinfo)
Dequantize a value given an unsigned 8-bit asymmetric quantization scheme.
SimpleTensor< TOut > dequantization_layer(const SimpleTensor< TIn > &src)
1 channel, 1 F32 per channel
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Quantization info when assuming per layer quantization.
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
DataType dt
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
bool is_data_type_quantized_per_channel(DataType dt)
Check if a given data type is of per channel type.
Definition: Utils.h:1058
float dequantize_qsymm16(int16_t value, const UniformQuantizationInfo &qinfo)
Dequantize a value given a 16-bit symmetric quantization scheme.
unsigned int N
quantized, symmetric fixed-point 8-bit number
quantized, symmetric per channel fixed-point 8-bit number
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
const QuantizationInfo qinfo
Definition: Im2Col.cpp:155
int num_elements() const override
Number of elements of the tensor.
Definition: SimpleTensor.h:406
float dequantize_qasymm8_signed(int8_t value, const INFO_TYPE &qinfo)
Dequantize a value given a signed 8-bit asymmetric quantization scheme.
float dequantize_qsymm8(int8_t value, const UniformQuantizationInfo &qinfo)
Dequantize a value given a 8-bit symmetric quantization scheme.
float dequantize(uint8_t value, float scale, int32_t offset)
Dequantize a value given an 8-bit asymmetric quantization scheme.
QuantizationInfo quantization_info() const override
Quantization info in case of asymmetric quantized type.
Definition: SimpleTensor.h:332
DataType
Available data types.
Definition: Types.h:77