Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +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_NORMALIZATION_LAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_NORMALIZATION_LAYER_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/runtime/Tensor.h" |
Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +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" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 35 | #include "tests/validation/reference/NormalizationLayer.h" |
Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +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> |
| 46 | class NormalizationValidationFixedPointFixture : public framework::Fixture |
| 47 | { |
| 48 | public: |
| 49 | template <typename...> |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 50 | void setup(TensorShape shape, NormType norm_type, int norm_size, float beta, bool is_scaled, DataType data_type, int fractional_bits) |
Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +0100 | [diff] [blame] | 51 | { |
| 52 | _fractional_bits = fractional_bits; |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 53 | NormalizationLayerInfo info(norm_type, norm_size, 5, beta, 1.f, is_scaled); |
Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +0100 | [diff] [blame] | 54 | |
| 55 | _target = compute_target(shape, info, data_type, fractional_bits); |
| 56 | _reference = compute_reference(shape, info, data_type, fractional_bits); |
| 57 | } |
| 58 | |
| 59 | protected: |
| 60 | template <typename U> |
| 61 | void fill(U &&tensor) |
| 62 | { |
| 63 | if(_fractional_bits == 0) |
| 64 | { |
| 65 | library->fill_tensor_uniform(tensor, 0); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | const int one_fixed = 1 << _fractional_bits; |
| 70 | std::uniform_int_distribution<> distribution(-one_fixed, one_fixed); |
| 71 | library->fill(tensor, distribution, 0); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | TensorType compute_target(const TensorShape &shape, NormalizationLayerInfo info, DataType data_type, int fixed_point_position = 0) |
| 76 | { |
| 77 | // Create tensors |
| 78 | TensorType src = create_tensor<TensorType>(shape, data_type, 1, fixed_point_position); |
| 79 | TensorType dst = create_tensor<TensorType>(shape, data_type, 1, fixed_point_position); |
| 80 | |
| 81 | // Create and configure function |
| 82 | FunctionType norm_layer; |
| 83 | norm_layer.configure(&src, &dst, info); |
| 84 | |
| 85 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 86 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 87 | |
| 88 | // Allocate tensors |
| 89 | src.allocator()->allocate(); |
| 90 | dst.allocator()->allocate(); |
| 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 | // Fill tensors |
| 96 | fill(AccessorType(src)); |
| 97 | |
| 98 | // Compute function |
| 99 | norm_layer.run(); |
| 100 | |
| 101 | return dst; |
| 102 | } |
| 103 | |
| 104 | SimpleTensor<T> compute_reference(const TensorShape &shape, NormalizationLayerInfo info, DataType data_type, int fixed_point_position = 0) |
| 105 | { |
| 106 | // Create reference |
| 107 | SimpleTensor<T> src{ shape, data_type, 1, fixed_point_position }; |
| 108 | |
| 109 | // Fill reference |
| 110 | fill(src); |
| 111 | |
| 112 | return reference::normalization_layer<T>(src, info); |
| 113 | } |
| 114 | |
| 115 | TensorType _target{}; |
| 116 | SimpleTensor<T> _reference{}; |
| 117 | int _fractional_bits{}; |
| 118 | }; |
| 119 | |
| 120 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 121 | class NormalizationValidationFixture : public NormalizationValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T> |
| 122 | { |
| 123 | public: |
| 124 | template <typename...> |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 125 | void setup(TensorShape shape, NormType norm_type, int norm_size, float beta, bool is_scaled, DataType data_type) |
Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +0100 | [diff] [blame] | 126 | { |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 127 | NormalizationValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, norm_type, norm_size, beta, is_scaled, data_type, 0); |
Moritz Pflanzer | 6db73ce | 2017-07-19 10:18:42 +0100 | [diff] [blame] | 128 | } |
| 129 | }; |
| 130 | } // namespace validation |
| 131 | } // namespace test |
| 132 | } // namespace arm_compute |
| 133 | #endif /* ARM_COMPUTE_TEST_NORMALIZATION_LAYER_FIXTURE */ |