Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +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_ACTIVATION_LAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_ACTIVATION_LAYER_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 34 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 35 | #include "tests/validation/reference/ActivationLayer.h" |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 36 | |
| 37 | #include <random> |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
| 45 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Michel Iwaniec | 66cc12f | 2017-12-07 17:26:40 +0000 | [diff] [blame] | 46 | class ActivationValidationGenericFixture : public framework::Fixture |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 47 | { |
| 48 | public: |
| 49 | template <typename...> |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 50 | void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type, QuantizationInfo quantization_info) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 51 | { |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 52 | ActivationLayerInfo info(function, alpha_beta, alpha_beta); |
| 53 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 54 | _in_place = in_place; |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 55 | _data_type = data_type; |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 56 | _output_quantization_info = calculate_output_quantization_info(_data_type, info, quantization_info); |
| 57 | _input_quantization_info = in_place ? _output_quantization_info : quantization_info; |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 58 | |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 59 | _function = function; |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 60 | _target = compute_target(shape, info); |
| 61 | _reference = compute_reference(shape, info); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | protected: |
| 65 | template <typename U> |
| 66 | void fill(U &&tensor) |
| 67 | { |
| 68 | if(is_data_type_float(_data_type)) |
| 69 | { |
| 70 | float min_bound = 0; |
| 71 | float max_bound = 0; |
| 72 | std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<T>(_function, _data_type); |
| 73 | std::uniform_real_distribution<> distribution(min_bound, max_bound); |
| 74 | library->fill(tensor, distribution, 0); |
| 75 | } |
Manuel Bottini | 30dbeef | 2019-06-26 16:23:03 +0100 | [diff] [blame] | 76 | else if(is_data_type_quantized(tensor.data_type())) |
Michel Iwaniec | 66cc12f | 2017-12-07 17:26:40 +0000 | [diff] [blame] | 77 | { |
| 78 | library->fill_tensor_uniform(tensor, 0); |
| 79 | } |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 80 | else |
| 81 | { |
| 82 | int min_bound = 0; |
| 83 | int max_bound = 0; |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 84 | std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<T>(_function, _data_type); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 85 | std::uniform_int_distribution<> distribution(min_bound, max_bound); |
| 86 | library->fill(tensor, distribution, 0); |
| 87 | } |
| 88 | } |
| 89 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 90 | TensorType compute_target(const TensorShape &shape, ActivationLayerInfo info) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 91 | { |
| 92 | // Create tensors |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 93 | TensorType src = create_tensor<TensorType>(shape, _data_type, 1, _input_quantization_info); |
| 94 | TensorType dst = create_tensor<TensorType>(shape, _data_type, 1, _output_quantization_info); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 95 | |
| 96 | // Create and configure function |
| 97 | FunctionType act_layer; |
| 98 | |
Manuel Bottini | 30dbeef | 2019-06-26 16:23:03 +0100 | [diff] [blame] | 99 | TensorType *dst_ptr = _in_place ? nullptr : &dst; |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 100 | |
| 101 | act_layer.configure(&src, dst_ptr, info); |
| 102 | |
| 103 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 104 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 105 | |
| 106 | // Allocate tensors |
| 107 | src.allocator()->allocate(); |
| 108 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 109 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 110 | if(!_in_place) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 111 | { |
| 112 | dst.allocator()->allocate(); |
| 113 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 114 | } |
| 115 | |
| 116 | // Fill tensors |
| 117 | fill(AccessorType(src)); |
| 118 | |
| 119 | // Compute function |
| 120 | act_layer.run(); |
| 121 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 122 | if(_in_place) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 123 | { |
| 124 | return src; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | return dst; |
| 129 | } |
| 130 | } |
| 131 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 132 | SimpleTensor<T> compute_reference(const TensorShape &shape, ActivationLayerInfo info) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 133 | { |
| 134 | // Create reference |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 135 | SimpleTensor<T> src{ shape, _data_type, 1, _input_quantization_info }; |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 136 | |
| 137 | // Fill reference |
| 138 | fill(src); |
| 139 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 140 | return reference::activation_layer<T>(src, info, _output_quantization_info); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 143 | private: |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 144 | QuantizationInfo calculate_output_quantization_info(DataType dt, const ActivationLayerInfo &act_info, const QuantizationInfo &default_qinfo) |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 145 | { |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 146 | auto qasymm8_max = float(std::numeric_limits<uint8_t>::max()) + 1.f; |
| 147 | auto qsymm16_max = float(std::numeric_limits<int16_t>::max()) + 1.f; |
| 148 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 149 | switch(act_info.activation()) |
| 150 | { |
| 151 | case ActivationLayerInfo::ActivationFunction::TANH: |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 152 | if(dt == DataType::QSYMM16) |
| 153 | { |
| 154 | return QuantizationInfo(1.f / qsymm16_max, 0); |
| 155 | } |
| 156 | else if(dt == DataType::QASYMM8) |
| 157 | { |
| 158 | return QuantizationInfo(1.f / (0.5 * qasymm8_max), int(0.5 * qasymm8_max)); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | return default_qinfo; |
| 163 | } |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 164 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 165 | if(dt == DataType::QSYMM16) |
| 166 | { |
| 167 | return QuantizationInfo(1.f / qsymm16_max, 0); |
| 168 | } |
| 169 | else if(dt == DataType::QASYMM8) |
| 170 | { |
| 171 | return QuantizationInfo(1.f / qasymm8_max, 0); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | return default_qinfo; |
| 176 | } |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 177 | default: |
| 178 | return default_qinfo; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | protected: |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 183 | TensorType _target{}; |
| 184 | SimpleTensor<T> _reference{}; |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 185 | bool _in_place{}; |
| 186 | QuantizationInfo _input_quantization_info{}; |
| 187 | QuantizationInfo _output_quantization_info{}; |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 188 | DataType _data_type{}; |
| 189 | ActivationLayerInfo::ActivationFunction _function{}; |
| 190 | }; |
| 191 | |
| 192 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Michel Iwaniec | 66cc12f | 2017-12-07 17:26:40 +0000 | [diff] [blame] | 193 | class ActivationValidationFixture : public ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 194 | { |
| 195 | public: |
| 196 | template <typename...> |
| 197 | void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type) |
| 198 | { |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 199 | ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, in_place, function, alpha_beta, data_type, QuantizationInfo()); |
Michel Iwaniec | 66cc12f | 2017-12-07 17:26:40 +0000 | [diff] [blame] | 200 | } |
| 201 | }; |
| 202 | |
| 203 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 204 | class ActivationValidationQuantizedFixture : public ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 205 | { |
| 206 | public: |
| 207 | template <typename...> |
| 208 | void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type, QuantizationInfo quantization_info) |
| 209 | { |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 210 | ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, in_place, function, alpha_beta, data_type, quantization_info); |
Michel Iwaniec | 66cc12f | 2017-12-07 17:26:40 +0000 | [diff] [blame] | 211 | } |
| 212 | }; |
| 213 | |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 214 | } // namespace validation |
| 215 | } // namespace test |
| 216 | } // namespace arm_compute |
| 217 | #endif /* ARM_COMPUTE_TEST_ACTIVATION_LAYER_FIXTURE */ |