Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 898db6f | 2018-03-02 11:34:05 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +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_DEPTHCONCATENATE_LAYER_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_DEPTHCONCATENATE_LAYER_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +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" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 34 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 35 | #include "tests/validation/reference/DepthConcatenateLayer.h" |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 36 | |
| 37 | #include <random> |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
Moritz Pflanzer | fb20568 | 2017-09-12 09:28:56 +0100 | [diff] [blame] | 45 | template <typename TensorType, typename ITensorType, typename AccessorType, typename FunctionType, typename T> |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 46 | class DepthConcatenateLayerValidationFixture : public framework::Fixture |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 47 | { |
| 48 | public: |
| 49 | template <typename...> |
| 50 | void setup(TensorShape shape, DataType data_type) |
| 51 | { |
| 52 | // Create input shapes |
| 53 | std::mt19937 gen(library->seed()); |
Michalis Spyrou | 898db6f | 2018-03-02 11:34:05 +0000 | [diff] [blame] | 54 | std::uniform_int_distribution<> num_dis(2, 4); |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 55 | const int num_tensors = num_dis(gen); |
| 56 | |
| 57 | std::vector<TensorShape> shapes(num_tensors, shape); |
Michalis Spyrou | 898db6f | 2018-03-02 11:34:05 +0000 | [diff] [blame] | 58 | std::uniform_int_distribution<> depth_dis(1, 3); |
| 59 | std::bernoulli_distribution mutate_dis(0.5f); |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 60 | std::uniform_real_distribution<> change_dis(-0.25f, 0.f); |
| 61 | |
| 62 | // Generate more shapes based on the input |
| 63 | for(auto &s : shapes) |
| 64 | { |
| 65 | // Set the depth of the tensor |
| 66 | s.set(2, depth_dis(gen)); |
| 67 | |
| 68 | // Randomly change the first dimension |
| 69 | if(mutate_dis(gen)) |
| 70 | { |
| 71 | // Decrease the dimension by a small percentage. Don't increase |
| 72 | // as that could make tensor too large. Also the change must be |
| 73 | // an even number. Otherwise out depth concatenate fails. |
| 74 | s.set(0, s[0] + 2 * static_cast<int>(s[0] * change_dis(gen))); |
| 75 | } |
| 76 | |
| 77 | // Repeat the same as above for the second dimension |
| 78 | if(mutate_dis(gen)) |
| 79 | { |
| 80 | s.set(1, s[1] + 2 * static_cast<int>(s[1] * change_dis(gen))); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | _target = compute_target(shapes, data_type); |
| 85 | _reference = compute_reference(shapes, data_type); |
| 86 | } |
| 87 | |
| 88 | protected: |
| 89 | template <typename U> |
| 90 | void fill(U &&tensor, int i) |
| 91 | { |
| 92 | library->fill_tensor_uniform(tensor, i); |
| 93 | } |
| 94 | |
| 95 | TensorType compute_target(std::vector<TensorShape> shapes, DataType data_type) |
| 96 | { |
Moritz Pflanzer | 3ce3ff4 | 2017-07-21 17:41:02 +0100 | [diff] [blame] | 97 | std::vector<TensorType> srcs; |
| 98 | std::vector<ITensorType *> src_ptrs; |
| 99 | |
| 100 | // Create tensors |
| 101 | srcs.reserve(shapes.size()); |
| 102 | |
| 103 | for(const auto &shape : shapes) |
| 104 | { |
| 105 | srcs.emplace_back(create_tensor<TensorType>(shape, data_type, 1, _fractional_bits)); |
| 106 | src_ptrs.emplace_back(&srcs.back()); |
| 107 | } |
| 108 | |
| 109 | TensorShape dst_shape = calculate_depth_concatenate_shape(shapes); |
| 110 | TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, _fractional_bits); |
| 111 | |
| 112 | // Create and configure function |
| 113 | FunctionType depth_concat; |
| 114 | depth_concat.configure(src_ptrs, &dst); |
| 115 | |
| 116 | for(auto &src : srcs) |
| 117 | { |
| 118 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 119 | } |
| 120 | |
| 121 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 122 | |
| 123 | // Allocate tensors |
| 124 | for(auto &src : srcs) |
| 125 | { |
| 126 | src.allocator()->allocate(); |
| 127 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 128 | } |
| 129 | |
| 130 | dst.allocator()->allocate(); |
| 131 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 132 | |
| 133 | // Fill tensors |
| 134 | int i = 0; |
| 135 | for(auto &src : srcs) |
| 136 | { |
| 137 | fill(AccessorType(src), i++); |
| 138 | } |
| 139 | |
| 140 | // Compute function |
| 141 | depth_concat.run(); |
| 142 | |
| 143 | return dst; |
| 144 | } |
| 145 | |
| 146 | SimpleTensor<T> compute_reference(std::vector<TensorShape> shapes, DataType data_type) |
| 147 | { |
| 148 | std::vector<SimpleTensor<T>> srcs; |
| 149 | |
| 150 | // Create and fill tensors |
| 151 | int i = 0; |
| 152 | for(const auto &shape : shapes) |
| 153 | { |
| 154 | srcs.emplace_back(shape, data_type, 1, _fractional_bits); |
| 155 | fill(srcs.back(), i++); |
| 156 | } |
| 157 | |
| 158 | return reference::depthconcatenate_layer<T>(srcs); |
| 159 | } |
| 160 | |
| 161 | TensorType _target{}; |
| 162 | SimpleTensor<T> _reference{}; |
| 163 | |
| 164 | private: |
| 165 | int _fractional_bits{ 1 }; |
| 166 | }; |
| 167 | } // namespace validation |
| 168 | } // namespace test |
| 169 | } // namespace arm_compute |
| 170 | #endif /* ARM_COMPUTE_TEST_DEPTHCONCATENATE_LAYER_FIXTURE */ |