Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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_POOLING_LAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/runtime/Tensor.h" |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +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" |
| 35 | #include "tests/validation/CPP/PoolingLayer.h" |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +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> |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 46 | class PoolingLayerValidationGenericFixture : public framework::Fixture |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 47 | { |
| 48 | public: |
| 49 | template <typename...> |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 50 | void setup(TensorShape shape, PoolingLayerInfo pool_info, DataType data_type, int fractional_bits, QuantizationInfo quantization_info) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 51 | { |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 52 | _fractional_bits = fractional_bits; |
| 53 | _quantization_info = quantization_info; |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 54 | _pool_info = pool_info; |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 55 | |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 56 | _target = compute_target(shape, pool_info, data_type, fractional_bits, quantization_info); |
| 57 | _reference = compute_reference(shape, pool_info, data_type, fractional_bits, quantization_info); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | protected: |
| 61 | template <typename U> |
| 62 | void fill(U &&tensor) |
| 63 | { |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 64 | if(!is_data_type_quantized(tensor.data_type())) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 65 | { |
| 66 | std::uniform_real_distribution<> distribution(-1.f, 1.f); |
| 67 | library->fill(tensor, distribution, 0); |
| 68 | } |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 69 | else if(is_data_type_quantized_asymmetric(tensor.data_type())) |
| 70 | { |
| 71 | library->fill_tensor_uniform(tensor, 0); |
| 72 | } |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 73 | else |
| 74 | { |
| 75 | const int one_fixed = 1 << _fractional_bits; |
| 76 | std::uniform_int_distribution<> distribution(-one_fixed, one_fixed); |
| 77 | library->fill(tensor, distribution, 0); |
| 78 | } |
| 79 | } |
| 80 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 81 | TensorType compute_target(const TensorShape &shape, PoolingLayerInfo info, |
| 82 | DataType data_type, int fixed_point_position, QuantizationInfo quantization_info) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 83 | { |
| 84 | // Create tensors |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 85 | TensorType src = create_tensor<TensorType>(shape, data_type, 1, fixed_point_position, quantization_info); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 86 | TensorType dst; |
| 87 | |
| 88 | // Create and configure function |
| 89 | FunctionType pool_layer; |
| 90 | pool_layer.configure(&src, &dst, info); |
| 91 | |
| 92 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 93 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 94 | |
| 95 | // Allocate tensors |
| 96 | src.allocator()->allocate(); |
| 97 | dst.allocator()->allocate(); |
| 98 | |
| 99 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 100 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 101 | |
| 102 | // Fill tensors |
| 103 | fill(AccessorType(src)); |
| 104 | |
| 105 | // Compute function |
| 106 | pool_layer.run(); |
| 107 | |
| 108 | return dst; |
| 109 | } |
| 110 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 111 | SimpleTensor<T> compute_reference(const TensorShape &shape, PoolingLayerInfo info, |
| 112 | DataType data_type, int fixed_point_position, QuantizationInfo quantization_info) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 113 | { |
| 114 | // Create reference |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 115 | SimpleTensor<T> src{ shape, data_type, 1, fixed_point_position, quantization_info }; |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 116 | |
| 117 | // Fill reference |
| 118 | fill(src); |
| 119 | |
| 120 | return reference::pooling_layer<T>(src, info); |
| 121 | } |
| 122 | |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 123 | TensorType _target{}; |
| 124 | SimpleTensor<T> _reference{}; |
| 125 | int _fractional_bits{}; |
| 126 | QuantizationInfo _quantization_info{}; |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 127 | PoolingLayerInfo _pool_info{}; |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 131 | class PoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 132 | { |
| 133 | public: |
| 134 | template <typename...> |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 135 | void setup(TensorShape shape, PoolingType pool_type, int pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type) |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 136 | { |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 137 | PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, pad_stride_info, exclude_padding), |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 138 | data_type, 0, QuantizationInfo()); |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 139 | } |
| 140 | }; |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 141 | |
| 142 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 143 | class PoolingLayerValidationFixedPointFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 144 | { |
| 145 | public: |
| 146 | template <typename...> |
| 147 | void setup(TensorShape shape, PoolingType pool_type, int pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type, int fractional_bits) |
| 148 | { |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 149 | PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, pad_stride_info, exclude_padding), |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 150 | data_type, fractional_bits, QuantizationInfo()); |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 155 | class PoolingLayerValidationQuantizedFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 156 | { |
| 157 | public: |
| 158 | template <typename...> |
| 159 | void setup(TensorShape shape, PoolingType pool_type, int pool_size, PadStrideInfo pad_stride_info, bool exclude_padding, DataType data_type, QuantizationInfo quantization_info) |
| 160 | { |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 161 | PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type, pool_size, pad_stride_info, exclude_padding), |
Anton Lokhmotov | af6204c | 2017-11-08 09:34:19 +0000 | [diff] [blame] | 162 | data_type, 0, quantization_info); |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 167 | class GlobalPoolingLayerValidationFixture : public PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 168 | { |
| 169 | public: |
| 170 | template <typename...> |
| 171 | void setup(TensorShape shape, PoolingType pool_type, DataType data_type) |
| 172 | { |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 173 | PoolingLayerValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, PoolingLayerInfo(pool_type), data_type, 0, QuantizationInfo()); |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 174 | } |
| 175 | }; |
Georgios Pinitas | dc460f1 | 2017-08-24 19:02:44 +0100 | [diff] [blame] | 176 | } // namespace validation |
| 177 | } // namespace test |
| 178 | } // namespace arm_compute |
| 179 | #endif /* ARM_COMPUTE_TEST_POOLING_LAYER_FIXTURE */ |