Compute Library
 22.05
QuantizationLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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 "arm_compute/core/Types.h"
28 #include "tests/NEON/Accessor.h"
30 #include "tests/datasets/ShapeDatasets.h"
32 #include "tests/framework/Macros.h"
35 #include "tests/validation/fixtures/QuantizationLayerFixture.h"
36 
37 namespace arm_compute
38 {
39 namespace test
40 {
41 namespace validation
42 {
43 namespace
44 {
45 /** Tolerance for quantization */
46 constexpr AbsoluteTolerance<uint8_t> tolerance_u8(1); /**< Tolerance value for comparing reference's output against implementation's output for QASYMM8 data types */
47 constexpr AbsoluteTolerance<int8_t> tolerance_s8(1); /**< Tolerance value for comparing reference's output against implementation's output for QASYMM8_SIGNED data types */
48 constexpr AbsoluteTolerance<uint16_t> tolerance_u16(1); /**< Tolerance value for comparing reference's output against implementation's output for QASYMM16 data types */
49 const auto QuantizationSmallShapes = concat(datasets::Small3DShapes(), datasets::Small4DShapes());
50 const auto QuantizationLargeShapes = concat(datasets::Large3DShapes(), datasets::Large4DShapes());
51 } // namespace
52 
53 TEST_SUITE(NEON)
54 TEST_SUITE(QuantizationLayer)
55 
56 // *INDENT-OFF*
57 // clang-format off
59  framework::dataset::make("InputInfo", { TensorInfo(TensorShape(16U, 16U, 16U, 5U), 1, DataType::QASYMM8), // Wrong output data type
60  TensorInfo(TensorShape(16U, 16U, 16U, 5U), 1, DataType::F32), // Wrong output data type
61  TensorInfo(TensorShape(16U, 16U, 2U, 5U), 1, DataType::F32), // Missmatching shapes
62  TensorInfo(TensorShape(16U, 16U, 16U, 5U), 1, DataType::F32), // Valid
63  }),
64  framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(16U, 16U, 16U, 5U), 1, DataType::F32),
65  TensorInfo(TensorShape(16U, 16U, 16U, 5U), 1, DataType::U16),
66  TensorInfo(TensorShape(16U, 16U, 16U, 5U), 1, DataType::QASYMM8),
67  TensorInfo(TensorShape(16U, 16U, 16U, 5U), 1, DataType::QASYMM8),
68  })),
69  framework::dataset::make("Expected", { false, false, false, true})),
71 {
72  ARM_COMPUTE_EXPECT(bool(NEQuantizationLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false))) == expected, framework::LogLevel::ERRORS);
73 }
74 // clang-format on
75 // *INDENT-ON*
76 
77 template <typename T>
78 using NEQuantizationLayerQASYMM8Fixture = QuantizationValidationFixture<Tensor, Accessor, NEQuantizationLayer, T, uint8_t>;
79 template <typename T>
80 using NEQuantizationLayerQASYMM8SignedFixture = QuantizationValidationFixture<Tensor, Accessor, NEQuantizationLayer, T, int8_t>;
81 template <typename T>
82 using NEQuantizationLayerQASYMM16Fixture = QuantizationValidationFixture<Tensor, Accessor, NEQuantizationLayer, T, uint16_t>;
83 
84 TEST_SUITE(Float)
85 TEST_SUITE(FP32)
86 FIXTURE_DATA_TEST_CASE(RunSmallQASYMM8, NEQuantizationLayerQASYMM8Fixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(QuantizationSmallShapes,
87  framework::dataset::make("DataType", DataType::F32)),
88  framework::dataset::make("DataTypeOut", { DataType::QASYMM8 })),
89  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10) })))
90 {
91  // Validate output
92  validate(Accessor(_target), _reference, tolerance_u8);
93 }
97  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10) })))
98 {
99  // Validate output
100  validate(Accessor(_target), _reference, tolerance_s8);
101 }
104  framework::dataset::make("DataTypeOut", { DataType::QASYMM16 })),
105  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10) })))
106 {
107  // Validate output
108  validate(Accessor(_target), _reference, tolerance_u16);
109 }
112  framework::dataset::make("DataTypeOut", { DataType::QASYMM8 })),
113  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10) })))
114 {
115  // Validate output
116  validate(Accessor(_target), _reference, tolerance_u8);
117 }
120  framework::dataset::make("DataTypeOut", { DataType::QASYMM16 })),
121  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10) })))
122 {
123  // Validate output
124  validate(Accessor(_target), _reference, tolerance_u16);
125 }
126 TEST_SUITE_END() // FP32
127 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
128 TEST_SUITE(FP16)
131  framework::dataset::make("DataTypeOut", { DataType::QASYMM8 })),
132  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10) })))
133 {
134  // Validate output
135  validate(Accessor(_target), _reference, tolerance_u8);
136 }
140  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10) })))
141 {
142  // Validate output
143  validate(Accessor(_target), _reference, tolerance_s8);
144 }
147  framework::dataset::make("DataTypeOut", { DataType::QASYMM16 })),
148  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10) })))
149 {
150  // Validate output
151  validate(Accessor(_target), _reference, tolerance_u16);
152 }
155  framework::dataset::make("DataTypeOut", { DataType::QASYMM8 })),
156  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10) })))
157 {
158  // Validate output
159  validate(Accessor(_target), _reference, tolerance_u8);
160 }
163  framework::dataset::make("DataTypeOut", { DataType::QASYMM16 })),
164  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10) })))
165 {
166  // Validate output
167  validate(Accessor(_target), _reference, tolerance_u16);
168 }
169 TEST_SUITE_END() // FP16
170 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
171 TEST_SUITE_END() // Float
172 
173 TEST_SUITE(Quantized)
174 template <typename T>
175 using NEQuantizationLayerQASYMM8GenFixture = QuantizationValidationGenericFixture<Tensor, Accessor, NEQuantizationLayer, T, uint8_t>;
176 template <typename T>
177 using NEQuantizationLayerQASYMM8_SIGNEDGenFixture = QuantizationValidationGenericFixture<Tensor, Accessor, NEQuantizationLayer, T, int8_t>;
178 template <typename T>
179 using NEQuantizationLayerQASYMM16GenFixture = QuantizationValidationGenericFixture<Tensor, Accessor, NEQuantizationLayer, T, uint16_t>;
180 TEST_SUITE(QASYMM8)
181 FIXTURE_DATA_TEST_CASE(RunSmallQASYMM8, NEQuantizationLayerQASYMM8GenFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(combine(QuantizationSmallShapes,
182  framework::dataset::make("DataType", DataType::QASYMM8)),
183  framework::dataset::make("DataTypeOut", { DataType::QASYMM8 })),
184  framework::dataset::make("QuantizationInfoOutput", { QuantizationInfo(0.5f, 10) })),
185  framework::dataset::make("QuantizationInfoInput", { QuantizationInfo(2.0f, 15) })))
186 {
187  // Validate output
188  validate(Accessor(_target), _reference, tolerance_u8);
189 }
193  framework::dataset::make("QuantizationInfoOutput", { QuantizationInfo(1.0f, 10), QuantizationInfo(2.0f, -25) })),
194  framework::dataset::make("QuantizationInfoInput", { QuantizationInfo(1.0f, 15) })))
195 {
196  // Validate output
197  validate(Accessor(_target), _reference, tolerance_s8);
198 }
201  framework::dataset::make("DataTypeOut", { DataType::QASYMM16 })),
202  framework::dataset::make("QuantizationInfoOutput", { QuantizationInfo(1.0f, 10) })),
203  framework::dataset::make("QuantizationInfoInput", { QuantizationInfo(4.0f, 23) })))
204 {
205  // Validate output
206  validate(Accessor(_target), _reference, tolerance_u16);
207 }
208 TEST_SUITE_END() // QASYMM8
210 FIXTURE_DATA_TEST_CASE(RunSmallQASYMM8_SIGNED, NEQuantizationLayerQASYMM8_SIGNEDGenFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(combine(QuantizationSmallShapes,
211  framework::dataset::make("DataTypeIn", DataType::QASYMM8_SIGNED)),
212  framework::dataset::make("DataTypeOut", { DataType::QASYMM8_SIGNED })),
213  framework::dataset::make("QuantizationInfoOutput", { QuantizationInfo(1.0f, 10) })),
214  framework::dataset::make("QuantizationInfoInput", { QuantizationInfo(2.0f, -5) })))
215 {
216  // Validate output
217  validate(Accessor(_target), _reference, tolerance_s8);
218 }
219 FIXTURE_DATA_TEST_CASE(RunSmallQASYMM8, NEQuantizationLayerQASYMM8GenFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(combine(QuantizationSmallShapes,
221  framework::dataset::make("DataTypeOut", { DataType::QASYMM8 })),
222  framework::dataset::make("QuantizationInfoOutput", { QuantizationInfo(2.0f, 10), QuantizationInfo(2.0f, -25) })),
223  framework::dataset::make("QuantizationInfoInput", { QuantizationInfo(1.0f, 30) })))
224 {
225  // Validate output
226  validate(Accessor(_target), _reference, tolerance_u8);
227 }
228 TEST_SUITE_END() // QASYMM8_SIGNED
229 TEST_SUITE_END() // Quantized
230 
231 TEST_SUITE_END() // QuantizationLayer
232 TEST_SUITE_END() // Neon
233 } // namespace validation
234 } // namespace test
235 } // namespace arm_compute
1 channel, 1 F32 per channel
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
quantized, asymmetric fixed-point 16-bit number
1 channel, 1 U16 per channel
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
Copyright (c) 2017-2022 Arm Limited.
1 channel, 1 F16 per channel
Quantization information.
DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), }), framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), })), framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SQRT), })), framework::dataset::make("Expected", { false, true, true, true, false, false, true, true, false })), input_info, output_info, act_info, expected)
Accessor implementation for Tensor objects.
Definition: Accessor.h:35
DatasetMode
Possible dataset modes.
Definition: DatasetModes.h:40
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
[CLActivationLayer Test snippet]
quantized, asymmetric fixed-point 8-bit number unsigned
QuantizationValidationFixture< Tensor, Accessor, NEQuantizationLayer, T, uint16_t > NEQuantizationLayerQASYMM16Fixture
validate(CLAccessor(output_state), expected_output)
QuantizationValidationFixture< Tensor, Accessor, NEQuantizationLayer, T, int8_t > NEQuantizationLayerQASYMM8SignedFixture
QuantizationValidationFixture< Tensor, Accessor, NEQuantizationLayer, T, uint8_t > NEQuantizationLayerQASYMM8Fixture
FIXTURE_DATA_TEST_CASE(RunSmall, CLAbsLayerFixture< half >, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)))
Definition: AbsLayer.cpp:50
QuantizationValidationGenericFixture< Tensor, Accessor, NEQuantizationLayer, T, uint16_t > NEQuantizationLayerQASYMM16GenFixture
JoinDataset< T, U > concat(T &&dataset1, U &&dataset2)
Helper function to create a JoinDataset.
Definition: JoinDataset.h:160
quantized, asymmetric fixed-point 8-bit number signed
static Status validate(const ITensorInfo *input, const ITensorInfo *output)
Static function to check if given info will lead to a valid configuration of NEQuantizationLayer.
zip(zip(framework::dataset::make("Weights", { TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 1U), 1, DataType::F32), }), framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), TensorInfo(TensorShape(2U), 1, DataType::F16), TensorInfo(TensorShape(5U), 1, DataType::F32), })), framework::dataset::make("Expected", { true, false, false}))
QuantizationValidationGenericFixture< Tensor, Accessor, NEQuantizationLayer, T, int8_t > NEQuantizationLayerQASYMM8_SIGNEDGenFixture
TEST_SUITE(QASYMM8_to_F32) FIXTURE_DATA_TEST_CASE(RunSmall
DataType
Available data types.
Definition: Types.h:79
combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
Definition: AbsLayer.cpp:65