Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +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_FLATTEN_LAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_FLATTEN_LAYER_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/core/Utils.h" |
| 30 | #include "arm_compute/runtime/Tensor.h" |
| 31 | #include "tests/AssetsLibrary.h" |
| 32 | #include "tests/Globals.h" |
| 33 | #include "tests/IAccessor.h" |
| 34 | #include "tests/framework/Asserts.h" |
| 35 | #include "tests/framework/Fixture.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 36 | #include "tests/validation/reference/FlattenLayer.h" |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +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> |
| 47 | class FlattenLayerValidationFixture : public framework::Fixture |
| 48 | { |
| 49 | public: |
| 50 | template <typename...> |
| 51 | void setup(TensorShape shape, DataType data_type) |
| 52 | { |
| 53 | _fractional_bits = is_data_type_fixed_point(data_type) ? 4 : 0; |
| 54 | _target = compute_target(shape, data_type); |
| 55 | _reference = compute_reference(shape, data_type); |
| 56 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(_target.info()->tensor_shape(), _reference.shape()); |
| 57 | } |
| 58 | |
| 59 | protected: |
| 60 | template <typename U> |
| 61 | void fill(U &&tensor) |
| 62 | { |
| 63 | if(_fractional_bits == 0) |
| 64 | { |
| 65 | std::uniform_real_distribution<> distribution(-1.f, 1.f); |
| 66 | library->fill(tensor, distribution, 0); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | const int one_fixed = 1 << _fractional_bits; |
| 71 | std::uniform_int_distribution<> distribution(-one_fixed, one_fixed); |
| 72 | library->fill(tensor, distribution, 0); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | TensorType compute_target(const TensorShape &shape, DataType data_type) |
| 77 | { |
| 78 | TensorShape shape_flatten(shape); |
Michalis Spyrou | 3b8a5f5 | 2017-12-18 14:14:05 +0000 | [diff] [blame] | 79 | shape_flatten.collapse(3); |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 80 | |
| 81 | // Create tensors |
| 82 | TensorType src = create_tensor<TensorType>(shape, data_type, 1, _fractional_bits); |
| 83 | TensorType dst = create_tensor<TensorType>(shape_flatten, data_type, 1, _fractional_bits); |
| 84 | |
| 85 | // Create and configure function |
| 86 | FunctionType flatten_layer; |
| 87 | flatten_layer.configure(&src, &dst); |
| 88 | |
| 89 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 90 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 91 | |
| 92 | // Allocate tensors |
| 93 | src.allocator()->allocate(); |
| 94 | dst.allocator()->allocate(); |
| 95 | |
| 96 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 97 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 98 | |
| 99 | // Fill tensors |
| 100 | fill(AccessorType(src)); |
| 101 | |
| 102 | // Compute function |
| 103 | flatten_layer.run(); |
| 104 | |
| 105 | return dst; |
| 106 | } |
| 107 | |
| 108 | SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type) |
| 109 | { |
| 110 | // Create reference |
| 111 | SimpleTensor<T> src{ shape, data_type, 1, _fractional_bits }; |
| 112 | |
| 113 | // Fill reference |
| 114 | fill(src); |
| 115 | |
| 116 | return reference::flatten_layer<T>(src); |
| 117 | } |
| 118 | |
| 119 | TensorType _target{}; |
| 120 | SimpleTensor<T> _reference{}; |
| 121 | int _fractional_bits{}; |
| 122 | }; |
| 123 | } // namespace validation |
| 124 | } // namespace test |
| 125 | } // namespace arm_compute |
| 126 | #endif /* ARM_COMPUTE_TEST_FLATTEN_LAYER_FIXTURE */ |