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