Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 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 | #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 Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 30 | #include "tests/AssetsLibrary.h" |
| 31 | #include "tests/Globals.h" |
| 32 | #include "tests/IAccessor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 33 | #include "tests/framework/Asserts.h" |
| 34 | #include "tests/framework/Fixture.h" |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 35 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 36 | #include "tests/validation/reference/DequantizationLayer.h" |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 37 | |
| 38 | #include <random> |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace test |
| 43 | { |
| 44 | namespace validation |
| 45 | { |
| 46 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Giorgio Arena | c3b2e07 | 2018-08-10 16:27:11 +0100 | [diff] [blame] | 47 | class DequantizationValidationFixture : public framework::Fixture |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 48 | { |
| 49 | public: |
| 50 | template <typename...> |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 51 | void setup(TensorShape shape, DataType src_data_type, DataType dst_datatype, DataLayout data_layout) |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 52 | { |
Michalis Spyrou | 29a01c9 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 53 | _quantization_info = generate_quantization_info(src_data_type, shape.z()); |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 54 | _target = compute_target(shape, src_data_type, dst_datatype, data_layout); |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 55 | _reference = compute_reference(shape, src_data_type); |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | protected: |
| 59 | template <typename U> |
| 60 | void fill(U &&tensor) |
| 61 | { |
| 62 | library->fill_tensor_uniform(tensor, 0); |
| 63 | } |
| 64 | |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 65 | TensorType compute_target(TensorShape shape, DataType src_data_type, DataType dst_datatype, DataLayout data_layout) |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 66 | { |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 67 | if(data_layout == DataLayout::NHWC) |
| 68 | { |
| 69 | permute(shape, PermutationVector(2U, 0U, 1U)); |
| 70 | } |
| 71 | |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 72 | // Create tensors |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 73 | 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 Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 75 | |
| 76 | // Create and configure function |
| 77 | FunctionType dequantization_layer; |
Georgios Pinitas | 574775c | 2019-02-18 20:08:02 +0000 | [diff] [blame] | 78 | dequantization_layer.configure(&src, &dst); |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 79 | |
| 80 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 81 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 82 | |
| 83 | // Allocate tensors |
| 84 | src.allocator()->allocate(); |
| 85 | dst.allocator()->allocate(); |
| 86 | |
| 87 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 88 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 89 | |
| 90 | // Fill tensors |
| 91 | fill(AccessorType(src)); |
| 92 | |
| 93 | // Compute function |
| 94 | dequantization_layer.run(); |
| 95 | |
| 96 | return dst; |
| 97 | } |
| 98 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 99 | SimpleTensor<T> compute_reference(const TensorShape &shape, DataType src_data_type) |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 100 | { |
Michalis Spyrou | 29a01c9 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 101 | switch(src_data_type) |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 102 | { |
Michalis Spyrou | 29a01c9 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 103 | case DataType::QASYMM8: |
Michalis Spyrou | 29a01c9 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 104 | { |
| 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 Park | d817647 | 2019-12-04 09:46:28 +0000 | [diff] [blame] | 109 | case DataType::QASYMM8_SIGNED: |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 110 | case DataType::QSYMM8_PER_CHANNEL: |
Michalis Spyrou | 29a01c9 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 111 | 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 Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 125 | } |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 128 | protected: |
Michalis Spyrou | 29a01c9 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 129 | QuantizationInfo generate_quantization_info(DataType data_type, int32_t num_channels) |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 130 | { |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 131 | std::mt19937 gen(library.get()->seed()); |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 132 | 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 Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 135 | |
| 136 | switch(data_type) |
| 137 | { |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 138 | case DataType::QSYMM16: |
| 139 | return QuantizationInfo(1.f / distribution_scale_q16(gen)); |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 140 | case DataType::QSYMM8: |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 141 | return QuantizationInfo(1.f / distribution_scale_q8(gen)); |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 142 | case DataType::QSYMM8_PER_CHANNEL: |
Michalis Spyrou | 29a01c9 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 143 | { |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 144 | std::vector<float> scale(num_channels); |
Michalis Spyrou | 29a01c9 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 145 | for(int32_t i = 0; i < num_channels; ++i) |
| 146 | { |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 147 | scale[i] = 1.f / distribution_offset_q8(gen); |
Michalis Spyrou | 29a01c9 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 148 | } |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 149 | return QuantizationInfo(scale); |
Michalis Spyrou | 29a01c9 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 150 | } |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 151 | case DataType::QASYMM8: |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 152 | return QuantizationInfo(1.f / distribution_scale_q8(gen), distribution_offset_q8(gen)); |
Sang-Hoon Park | d817647 | 2019-12-04 09:46:28 +0000 | [diff] [blame] | 153 | case DataType::QASYMM8_SIGNED: |
| 154 | return QuantizationInfo(1.f / distribution_scale_q8(gen), -distribution_offset_q8(gen)); |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 155 | default: |
| 156 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | protected: |
| 161 | TensorType _target{}; |
| 162 | SimpleTensor<T> _reference{}; |
| 163 | QuantizationInfo _quantization_info{}; |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 164 | }; |
Michele Di Giorgio | 32982d8 | 2017-07-07 14:44:43 +0100 | [diff] [blame] | 165 | } // namespace validation |
| 166 | } // namespace test |
| 167 | } // namespace arm_compute |
| 168 | #endif /* ARM_COMPUTE_TEST_DEQUANTIZATION_LAYER_FIXTURE */ |