Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +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_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" |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 31 | #include "arm_compute/runtime/Tensor.h" |
| 32 | #include "tests/AssetsLibrary.h" |
| 33 | #include "tests/Globals.h" |
| 34 | #include "tests/IAccessor.h" |
| 35 | #include "tests/framework/Asserts.h" |
| 36 | #include "tests/framework/Fixture.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 37 | #include "tests/validation/reference/FlattenLayer.h" |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 38 | |
| 39 | #include <random> |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace test |
| 44 | { |
| 45 | namespace validation |
| 46 | { |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 47 | using namespace arm_compute::misc::shape_calculator; |
| 48 | |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 49 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 50 | class FlattenLayerValidationFixture : public framework::Fixture |
| 51 | { |
| 52 | public: |
| 53 | template <typename...> |
| 54 | void setup(TensorShape shape, DataType data_type) |
| 55 | { |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 56 | TensorShape shape_flatten; |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 57 | TensorInfo input_info(shape, 1, data_type); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 58 | shape_flatten = compute_flatten_shape(&input_info); |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 59 | |
| 60 | _target = compute_target(shape, shape_flatten, data_type); |
| 61 | _reference = compute_reference(shape, shape_flatten, data_type); |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 62 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(_target.info()->tensor_shape(), _reference.shape()); |
| 63 | } |
| 64 | |
| 65 | protected: |
| 66 | template <typename U> |
| 67 | void fill(U &&tensor) |
| 68 | { |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 69 | std::uniform_real_distribution<> distribution(-1.f, 1.f); |
| 70 | library->fill(tensor, distribution, 0); |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 73 | TensorType compute_target(const TensorShape &shape, const TensorShape &shape_flatten, DataType data_type) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 74 | { |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 75 | // Create tensors |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 76 | TensorType src = create_tensor<TensorType>(shape, data_type, 1); |
| 77 | TensorType dst = create_tensor<TensorType>(shape_flatten, data_type, 1); |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 78 | |
| 79 | // Create and configure function |
| 80 | FunctionType flatten_layer; |
| 81 | flatten_layer.configure(&src, &dst); |
| 82 | |
| 83 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 84 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 85 | |
| 86 | // Allocate tensors |
| 87 | src.allocator()->allocate(); |
| 88 | dst.allocator()->allocate(); |
| 89 | |
| 90 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 91 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 92 | |
| 93 | // Fill tensors |
| 94 | fill(AccessorType(src)); |
| 95 | |
| 96 | // Compute function |
| 97 | flatten_layer.run(); |
| 98 | |
| 99 | return dst; |
| 100 | } |
| 101 | |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 102 | SimpleTensor<T> compute_reference(const TensorShape &shape, const TensorShape &shape_flatten, DataType data_type) |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 103 | { |
| 104 | // Create reference |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 105 | SimpleTensor<T> src{ shape, data_type, 1 }; |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 106 | |
| 107 | // Fill reference |
| 108 | fill(src); |
| 109 | |
Giorgio Arena | 156fcf3 | 2018-03-09 15:30:43 +0000 | [diff] [blame] | 110 | return reference::flatten_layer<T>(src, shape_flatten); |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TensorType _target{}; |
| 114 | SimpleTensor<T> _reference{}; |
Gian Marco Iodice | bf17955 | 2017-09-05 13:51:21 +0100 | [diff] [blame] | 115 | }; |
| 116 | } // namespace validation |
| 117 | } // namespace test |
| 118 | } // namespace arm_compute |
| 119 | #endif /* ARM_COMPUTE_TEST_FLATTEN_LAYER_FIXTURE */ |