blob: af998bb7408ca2ff0308bbdb5a892ee4eaee810f [file] [log] [blame]
Michele Di Giorgio32982d82017-07-07 14:44:43 +01001/*
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +01002 * Copyright (c) 2017-2021 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#ifndef ARM_COMPUTE_TEST_DEQUANTIZATION_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_DEQUANTIZATION_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/runtime/Tensor.h"
Michele Di Giorgio32982d82017-07-07 14:44:43 +010030#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
Michalis Spyrou3f632f32019-08-22 16:52:00 +010035#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000036#include "tests/validation/reference/DequantizationLayer.h"
Michele Di Giorgio32982d82017-07-07 14:44:43 +010037
38#include <random>
39
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
46template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Giorgio Arenac3b2e072018-08-10 16:27:11 +010047class DequantizationValidationFixture : public framework::Fixture
Michele Di Giorgio32982d82017-07-07 14:44:43 +010048{
49public:
50 template <typename...>
Michalis Spyrou3f632f32019-08-22 16:52:00 +010051 void setup(TensorShape shape, DataType src_data_type, DataType dst_datatype, DataLayout data_layout)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010052 {
Michalis Spyrou29a01c92019-08-22 11:44:04 +010053 _quantization_info = generate_quantization_info(src_data_type, shape.z());
Michalis Spyrou3f632f32019-08-22 16:52:00 +010054 _target = compute_target(shape, src_data_type, dst_datatype, data_layout);
Georgios Pinitas3d13af82019-06-04 13:04:16 +010055 _reference = compute_reference(shape, src_data_type);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010056 }
57
58protected:
59 template <typename U>
60 void fill(U &&tensor)
61 {
62 library->fill_tensor_uniform(tensor, 0);
63 }
64
Michalis Spyrou3f632f32019-08-22 16:52:00 +010065 TensorType compute_target(TensorShape shape, DataType src_data_type, DataType dst_datatype, DataLayout data_layout)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010066 {
Michalis Spyrou3f632f32019-08-22 16:52:00 +010067 if(data_layout == DataLayout::NHWC)
68 {
69 permute(shape, PermutationVector(2U, 0U, 1U));
70 }
71
Michele Di Giorgio32982d82017-07-07 14:44:43 +010072 // Create tensors
Michalis Spyrou3f632f32019-08-22 16:52:00 +010073 TensorType src = create_tensor<TensorType>(shape, src_data_type, 1, _quantization_info, data_layout);
74 TensorType dst = create_tensor<TensorType>(shape, dst_datatype, 1, QuantizationInfo(), data_layout);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010075
76 // Create and configure function
77 FunctionType dequantization_layer;
Georgios Pinitas574775c2019-02-18 20:08:02 +000078 dequantization_layer.configure(&src, &dst);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010079
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010080 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
81 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Michele Di Giorgio32982d82017-07-07 14:44:43 +010082
83 // Allocate tensors
84 src.allocator()->allocate();
85 dst.allocator()->allocate();
86
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010087 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
88 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Michele Di Giorgio32982d82017-07-07 14:44:43 +010089
90 // Fill tensors
91 fill(AccessorType(src));
92
93 // Compute function
94 dequantization_layer.run();
95
96 return dst;
97 }
98
Georgios Pinitas3d13af82019-06-04 13:04:16 +010099 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType src_data_type)
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100100 {
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100101 switch(src_data_type)
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100102 {
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100103 case DataType::QASYMM8:
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100104 {
105 SimpleTensor<uint8_t> src{ shape, src_data_type, 1, _quantization_info };
106 fill(src);
107 return reference::dequantization_layer<T>(src);
108 }
Sang-Hoon Parkd8176472019-12-04 09:46:28 +0000109 case DataType::QASYMM8_SIGNED:
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000110 case DataType::QSYMM8_PER_CHANNEL:
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100111 case DataType::QSYMM8:
112 {
113 SimpleTensor<int8_t> src{ shape, src_data_type, 1, _quantization_info };
114 fill(src);
115 return reference::dequantization_layer<T>(src);
116 }
117 case DataType::QSYMM16:
118 {
119 SimpleTensor<int16_t> src{ shape, src_data_type, 1, _quantization_info };
120 fill(src);
121 return reference::dequantization_layer<T>(src);
122 }
123 default:
124 ARM_COMPUTE_ERROR("Unsupported data type");
Manuel Bottini10c53f12019-07-17 16:11:53 +0100125 }
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100126 }
127
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100128protected:
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100129 QuantizationInfo generate_quantization_info(DataType data_type, int32_t num_channels)
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100130 {
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100131 std::mt19937 gen(library.get()->seed());
Manuel Bottini10c53f12019-07-17 16:11:53 +0100132 std::uniform_int_distribution<> distribution_scale_q8(1, 255);
133 std::uniform_int_distribution<> distribution_offset_q8(1, 127);
134 std::uniform_int_distribution<> distribution_scale_q16(1, 32768);
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100135
136 switch(data_type)
137 {
Manuel Bottini10c53f12019-07-17 16:11:53 +0100138 case DataType::QSYMM16:
139 return QuantizationInfo(1.f / distribution_scale_q16(gen));
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100140 case DataType::QSYMM8:
Manuel Bottini10c53f12019-07-17 16:11:53 +0100141 return QuantizationInfo(1.f / distribution_scale_q8(gen));
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000142 case DataType::QSYMM8_PER_CHANNEL:
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100143 {
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000144 std::vector<float> scale(num_channels);
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100145 for(int32_t i = 0; i < num_channels; ++i)
146 {
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000147 scale[i] = 1.f / distribution_offset_q8(gen);
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100148 }
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000149 return QuantizationInfo(scale);
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100150 }
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100151 case DataType::QASYMM8:
Manuel Bottini10c53f12019-07-17 16:11:53 +0100152 return QuantizationInfo(1.f / distribution_scale_q8(gen), distribution_offset_q8(gen));
Sang-Hoon Parkd8176472019-12-04 09:46:28 +0000153 case DataType::QASYMM8_SIGNED:
154 return QuantizationInfo(1.f / distribution_scale_q8(gen), -distribution_offset_q8(gen));
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100155 default:
156 ARM_COMPUTE_ERROR("Unsupported data type");
157 }
158 }
159
160protected:
161 TensorType _target{};
162 SimpleTensor<T> _reference{};
163 QuantizationInfo _quantization_info{};
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100164};
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100165} // namespace validation
166} // namespace test
167} // namespace arm_compute
168#endif /* ARM_COMPUTE_TEST_DEQUANTIZATION_LAYER_FIXTURE */