blob: 4eb25a5bc518f12cb23b06b175190de10374765c [file] [log] [blame]
Michele Di Giorgio32982d82017-07-07 14:44:43 +01001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2017-2021, 2023 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:
Michalis Spyrou3f632f32019-08-22 16:52:00 +010050 void setup(TensorShape shape, DataType src_data_type, DataType dst_datatype, DataLayout data_layout)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010051 {
Michalis Spyrou29a01c92019-08-22 11:44:04 +010052 _quantization_info = generate_quantization_info(src_data_type, shape.z());
Michalis Spyrou3f632f32019-08-22 16:52:00 +010053 _target = compute_target(shape, src_data_type, dst_datatype, data_layout);
Georgios Pinitas3d13af82019-06-04 13:04:16 +010054 _reference = compute_reference(shape, src_data_type);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010055 }
56
57protected:
58 template <typename U>
59 void fill(U &&tensor)
60 {
61 library->fill_tensor_uniform(tensor, 0);
62 }
63
Michalis Spyrou3f632f32019-08-22 16:52:00 +010064 TensorType compute_target(TensorShape shape, DataType src_data_type, DataType dst_datatype, DataLayout data_layout)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010065 {
Michalis Spyrou3f632f32019-08-22 16:52:00 +010066 if(data_layout == DataLayout::NHWC)
67 {
68 permute(shape, PermutationVector(2U, 0U, 1U));
69 }
70
Michele Di Giorgio32982d82017-07-07 14:44:43 +010071 // Create tensors
Michalis Spyrou3f632f32019-08-22 16:52:00 +010072 TensorType src = create_tensor<TensorType>(shape, src_data_type, 1, _quantization_info, data_layout);
73 TensorType dst = create_tensor<TensorType>(shape, dst_datatype, 1, QuantizationInfo(), data_layout);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010074
75 // Create and configure function
76 FunctionType dequantization_layer;
Georgios Pinitas574775c2019-02-18 20:08:02 +000077 dequantization_layer.configure(&src, &dst);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010078
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010079 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
80 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Michele Di Giorgio32982d82017-07-07 14:44:43 +010081
82 // Allocate tensors
83 src.allocator()->allocate();
84 dst.allocator()->allocate();
85
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010086 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
87 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Michele Di Giorgio32982d82017-07-07 14:44:43 +010088
89 // Fill tensors
90 fill(AccessorType(src));
91
92 // Compute function
93 dequantization_layer.run();
94
95 return dst;
96 }
97
Georgios Pinitas3d13af82019-06-04 13:04:16 +010098 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType src_data_type)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010099 {
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100100 switch(src_data_type)
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100101 {
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100102 case DataType::QASYMM8:
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100103 {
104 SimpleTensor<uint8_t> src{ shape, src_data_type, 1, _quantization_info };
105 fill(src);
106 return reference::dequantization_layer<T>(src);
107 }
Sang-Hoon Parkd8176472019-12-04 09:46:28 +0000108 case DataType::QASYMM8_SIGNED:
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000109 case DataType::QSYMM8_PER_CHANNEL:
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100110 case DataType::QSYMM8:
111 {
112 SimpleTensor<int8_t> src{ shape, src_data_type, 1, _quantization_info };
113 fill(src);
114 return reference::dequantization_layer<T>(src);
115 }
116 case DataType::QSYMM16:
117 {
118 SimpleTensor<int16_t> src{ shape, src_data_type, 1, _quantization_info };
119 fill(src);
120 return reference::dequantization_layer<T>(src);
121 }
122 default:
123 ARM_COMPUTE_ERROR("Unsupported data type");
Manuel Bottini10c53f12019-07-17 16:11:53 +0100124 }
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100125 }
126
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100127protected:
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100128 QuantizationInfo generate_quantization_info(DataType data_type, int32_t num_channels)
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100129 {
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100130 std::mt19937 gen(library.get()->seed());
Manuel Bottini10c53f12019-07-17 16:11:53 +0100131 std::uniform_int_distribution<> distribution_scale_q8(1, 255);
132 std::uniform_int_distribution<> distribution_offset_q8(1, 127);
133 std::uniform_int_distribution<> distribution_scale_q16(1, 32768);
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100134
135 switch(data_type)
136 {
Manuel Bottini10c53f12019-07-17 16:11:53 +0100137 case DataType::QSYMM16:
138 return QuantizationInfo(1.f / distribution_scale_q16(gen));
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100139 case DataType::QSYMM8:
Manuel Bottini10c53f12019-07-17 16:11:53 +0100140 return QuantizationInfo(1.f / distribution_scale_q8(gen));
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000141 case DataType::QSYMM8_PER_CHANNEL:
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100142 {
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000143 std::vector<float> scale(num_channels);
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100144 for(int32_t i = 0; i < num_channels; ++i)
145 {
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000146 scale[i] = 1.f / distribution_offset_q8(gen);
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100147 }
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000148 return QuantizationInfo(scale);
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100149 }
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100150 case DataType::QASYMM8:
Manuel Bottini10c53f12019-07-17 16:11:53 +0100151 return QuantizationInfo(1.f / distribution_scale_q8(gen), distribution_offset_q8(gen));
Sang-Hoon Parkd8176472019-12-04 09:46:28 +0000152 case DataType::QASYMM8_SIGNED:
153 return QuantizationInfo(1.f / distribution_scale_q8(gen), -distribution_offset_q8(gen));
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100154 default:
155 ARM_COMPUTE_ERROR("Unsupported data type");
156 }
157 }
158
159protected:
160 TensorType _target{};
161 SimpleTensor<T> _reference{};
162 QuantizationInfo _quantization_info{};
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100163};
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100164} // namespace validation
165} // namespace test
166} // namespace arm_compute
167#endif /* ARM_COMPUTE_TEST_DEQUANTIZATION_LAYER_FIXTURE */