Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 1 | /* |
Pablo Marquez Tello | 9469058 | 2024-02-15 11:51:30 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2017-2021, 2023-2024 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 | */ |
Pablo Marquez Tello | 9469058 | 2024-02-15 11:51:30 +0000 | [diff] [blame^] | 24 | #ifndef ACL_TESTS_VALIDATION_FIXTURES_ACTIVATIONLAYERFIXTURE_H |
| 25 | #define ACL_TESTS_VALIDATION_FIXTURES_ACTIVATIONLAYERFIXTURE_H |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 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" |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 34 | #include "tests/framework/ParametersLibrary.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +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/ActivationLayer.h" |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +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> |
Michel Iwaniec | 66cc12f | 2017-12-07 17:26:40 +0000 | [diff] [blame] | 47 | class ActivationValidationGenericFixture : public framework::Fixture |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 48 | { |
| 49 | public: |
Pablo Tello | db8485a | 2019-09-24 11:03:47 +0100 | [diff] [blame] | 50 | |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 51 | 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] | 52 | { |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 53 | ActivationLayerInfo info(function, alpha_beta, alpha_beta); |
| 54 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 55 | _in_place = in_place; |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 56 | _data_type = data_type; |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 57 | _output_quantization_info = calculate_output_quantization_info(_data_type, info, quantization_info); |
| 58 | _input_quantization_info = in_place ? _output_quantization_info : quantization_info; |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 59 | |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 60 | _function = function; |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 61 | _target = compute_target(shape, info); |
| 62 | _reference = compute_reference(shape, info); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | protected: |
Sang-Hoon Park | a8a7c1d | 2020-05-12 22:01:23 +0100 | [diff] [blame] | 66 | std::vector<T> get_boundary_values(T min, T max) |
| 67 | { |
| 68 | // This function will return a vector filled with the following values that can |
| 69 | // represent two partitions derived from equivalent partitioning. |
| 70 | // * Lower parition: min, min + delta, lower quarter (nominal), center - delta |
| 71 | // * Upper partition: center, center + delta, upper quarter (nominal), max - delta, max |
| 72 | const auto delta = is_data_type_float(_data_type) ? T(0.1f) : T(1); |
| 73 | const auto center_value = (min + max) / 2; |
| 74 | const auto lower_quarter = (min + center_value) / 2; |
| 75 | const auto upper_quarter = (center_value + max) / 2; |
| 76 | |
| 77 | std::vector<T> boundary_values{}; |
| 78 | |
| 79 | // To ensure all the inserted values are within the given range after subtracing/adding delta |
| 80 | auto insert_values = [&boundary_values, &min, &max](const std::initializer_list<T> &new_values) |
| 81 | { |
| 82 | for(auto &v : new_values) |
| 83 | { |
| 84 | if(v >= min && v <= max) |
| 85 | { |
| 86 | boundary_values.emplace_back(v); |
| 87 | } |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | insert_values({ min, static_cast<T>(min + delta), static_cast<T>(lower_quarter), static_cast<T>(center_value - delta) }); // lower partition |
| 92 | insert_values({ static_cast<T>(center_value), static_cast<T>(center_value + delta), static_cast<T>(upper_quarter), static_cast<T>(max - delta), max }); // upper partition |
| 93 | |
| 94 | return boundary_values; |
| 95 | } |
| 96 | |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 97 | template <typename U> |
| 98 | void fill(U &&tensor) |
| 99 | { |
| 100 | if(is_data_type_float(_data_type)) |
| 101 | { |
| 102 | float min_bound = 0; |
| 103 | float max_bound = 0; |
| 104 | std::tie(min_bound, max_bound) = get_activation_layer_test_bounds<T>(_function, _data_type); |
Sang-Hoon Park | a8a7c1d | 2020-05-12 22:01:23 +0100 | [diff] [blame] | 105 | library->fill_static_values(tensor, get_boundary_values(static_cast<T>(min_bound), static_cast<T>(max_bound))); |
Michel Iwaniec | 66cc12f | 2017-12-07 17:26:40 +0000 | [diff] [blame] | 106 | } |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 107 | else |
| 108 | { |
Sang-Hoon Park | a8a7c1d | 2020-05-12 22:01:23 +0100 | [diff] [blame] | 109 | PixelValue min{}; |
| 110 | PixelValue max{}; |
| 111 | std::tie(min, max) = get_min_max(tensor.data_type()); |
| 112 | library->fill_static_values(tensor, get_boundary_values(min.get<T>(), max.get<T>())); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 116 | TensorType compute_target(const TensorShape &shape, ActivationLayerInfo info) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 117 | { |
| 118 | // Create tensors |
Pablo Marquez Tello | 9469058 | 2024-02-15 11:51:30 +0000 | [diff] [blame^] | 119 | TensorType src = create_tensor<TensorType>(shape, _data_type, 1, _input_quantization_info, DataLayout::NCHW); |
| 120 | TensorType dst = create_tensor<TensorType>(shape, _data_type, 1, _output_quantization_info, DataLayout::NCHW); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 121 | |
| 122 | // Create and configure function |
Pablo Marquez Tello | 9469058 | 2024-02-15 11:51:30 +0000 | [diff] [blame^] | 123 | FunctionType act_layer; |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 124 | |
Manuel Bottini | 30dbeef | 2019-06-26 16:23:03 +0100 | [diff] [blame] | 125 | TensorType *dst_ptr = _in_place ? nullptr : &dst; |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 126 | |
| 127 | act_layer.configure(&src, dst_ptr, info); |
| 128 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 129 | ARM_COMPUTE_ASSERT(src.info()->is_resizable()); |
| 130 | ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 131 | |
| 132 | // Allocate tensors |
| 133 | src.allocator()->allocate(); |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 134 | ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 135 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 136 | if(!_in_place) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 137 | { |
| 138 | dst.allocator()->allocate(); |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 139 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Fill tensors |
| 143 | fill(AccessorType(src)); |
| 144 | |
| 145 | // Compute function |
| 146 | act_layer.run(); |
| 147 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 148 | if(_in_place) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 149 | { |
| 150 | return src; |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | return dst; |
| 155 | } |
| 156 | } |
| 157 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 158 | SimpleTensor<T> compute_reference(const TensorShape &shape, ActivationLayerInfo info) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 159 | { |
| 160 | // Create reference |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 161 | SimpleTensor<T> src{ shape, _data_type, 1, _input_quantization_info }; |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 162 | |
| 163 | // Fill reference |
| 164 | fill(src); |
| 165 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 166 | return reference::activation_layer<T>(src, info, _output_quantization_info); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 169 | private: |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 170 | 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] | 171 | { |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 172 | auto qasymm8_max = float(std::numeric_limits<uint8_t>::max()) + 1.f; |
| 173 | auto qasymm8_signed_max = float(std::numeric_limits<int8_t>::max()) + 1.f; |
| 174 | auto qsymm16_max = float(std::numeric_limits<int16_t>::max()) + 1.f; |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 175 | |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 176 | switch(act_info.activation()) |
| 177 | { |
| 178 | case ActivationLayerInfo::ActivationFunction::TANH: |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 179 | if(dt == DataType::QSYMM16) |
| 180 | { |
| 181 | return QuantizationInfo(1.f / qsymm16_max, 0); |
| 182 | } |
| 183 | else if(dt == DataType::QASYMM8) |
| 184 | { |
| 185 | return QuantizationInfo(1.f / (0.5 * qasymm8_max), int(0.5 * qasymm8_max)); |
| 186 | } |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 187 | else if(dt == DataType::QASYMM8_SIGNED) |
| 188 | { |
| 189 | return QuantizationInfo(1.f / qasymm8_signed_max, 0); |
| 190 | } |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 191 | else |
| 192 | { |
| 193 | return default_qinfo; |
| 194 | } |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 195 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 196 | if(dt == DataType::QSYMM16) |
| 197 | { |
| 198 | return QuantizationInfo(1.f / qsymm16_max, 0); |
| 199 | } |
| 200 | else if(dt == DataType::QASYMM8) |
| 201 | { |
| 202 | return QuantizationInfo(1.f / qasymm8_max, 0); |
| 203 | } |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 204 | else if(dt == DataType::QASYMM8_SIGNED) |
| 205 | { |
| 206 | return QuantizationInfo(1.f / (2.f * qasymm8_signed_max), -int(qasymm8_signed_max)); |
| 207 | } |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 208 | else |
| 209 | { |
| 210 | return default_qinfo; |
| 211 | } |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 212 | default: |
| 213 | return default_qinfo; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | protected: |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 218 | TensorType _target{}; |
| 219 | SimpleTensor<T> _reference{}; |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 220 | bool _in_place{}; |
| 221 | QuantizationInfo _input_quantization_info{}; |
| 222 | QuantizationInfo _output_quantization_info{}; |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 223 | DataType _data_type{}; |
| 224 | ActivationLayerInfo::ActivationFunction _function{}; |
| 225 | }; |
| 226 | |
| 227 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Michel Iwaniec | 66cc12f | 2017-12-07 17:26:40 +0000 | [diff] [blame] | 228 | class ActivationValidationFixture : public ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 229 | { |
| 230 | public: |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 231 | void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type) |
| 232 | { |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 233 | 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] | 234 | } |
| 235 | }; |
| 236 | |
| 237 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 238 | class ActivationValidationQuantizedFixture : public ActivationValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 239 | { |
| 240 | public: |
Michel Iwaniec | 66cc12f | 2017-12-07 17:26:40 +0000 | [diff] [blame] | 241 | void setup(TensorShape shape, bool in_place, ActivationLayerInfo::ActivationFunction function, float alpha_beta, DataType data_type, QuantizationInfo quantization_info) |
| 242 | { |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 243 | 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] | 244 | } |
| 245 | }; |
| 246 | |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 247 | } // namespace validation |
| 248 | } // namespace test |
| 249 | } // namespace arm_compute |
Pablo Marquez Tello | 9469058 | 2024-02-15 11:51:30 +0000 | [diff] [blame^] | 250 | #endif // ACL_TESTS_VALIDATION_FIXTURES_ACTIVATIONLAYERFIXTURE_H |