blob: 5fdfacbb761068f28da06dac8989b32e97542d0a [file] [log] [blame]
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +01001/*
Michalis Spyrou898db6f2018-03-02 11:34:05 +00002 * Copyright (c) 2017-2018 ARM Limited.
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +01003 *
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"
Georgios Pinitase29acf12018-07-16 14:40:09 +010029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +010030#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010035#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000036#include "tests/validation/reference/DepthConcatenateLayer.h"
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +010037
38#include <random>
39
40namespace arm_compute
41{
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +010042namespace test
43{
44namespace validation
45{
Moritz Pflanzerfb205682017-09-12 09:28:56 +010046template <typename TensorType, typename ITensorType, typename AccessorType, typename FunctionType, typename T>
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000047class DepthConcatenateLayerValidationFixture : public framework::Fixture
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +010048{
49public:
50 template <typename...>
51 void setup(TensorShape shape, DataType data_type)
52 {
53 // Create input shapes
54 std::mt19937 gen(library->seed());
Michalis Spyrou898db6f2018-03-02 11:34:05 +000055 std::uniform_int_distribution<> num_dis(2, 4);
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +010056 const int num_tensors = num_dis(gen);
57
58 std::vector<TensorShape> shapes(num_tensors, shape);
Michalis Spyrou898db6f2018-03-02 11:34:05 +000059 std::uniform_int_distribution<> depth_dis(1, 3);
60 std::bernoulli_distribution mutate_dis(0.5f);
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +010061 std::uniform_real_distribution<> change_dis(-0.25f, 0.f);
62
63 // Generate more shapes based on the input
64 for(auto &s : shapes)
65 {
66 // Set the depth of the tensor
67 s.set(2, depth_dis(gen));
68
69 // Randomly change the first dimension
70 if(mutate_dis(gen))
71 {
72 // Decrease the dimension by a small percentage. Don't increase
73 // as that could make tensor too large. Also the change must be
74 // an even number. Otherwise out depth concatenate fails.
75 s.set(0, s[0] + 2 * static_cast<int>(s[0] * change_dis(gen)));
76 }
77
78 // Repeat the same as above for the second dimension
79 if(mutate_dis(gen))
80 {
81 s.set(1, s[1] + 2 * static_cast<int>(s[1] * change_dis(gen)));
82 }
83 }
84
85 _target = compute_target(shapes, data_type);
86 _reference = compute_reference(shapes, data_type);
87 }
88
89protected:
90 template <typename U>
91 void fill(U &&tensor, int i)
92 {
93 library->fill_tensor_uniform(tensor, i);
94 }
95
96 TensorType compute_target(std::vector<TensorShape> shapes, DataType data_type)
97 {
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +010098 std::vector<TensorType> srcs;
99 std::vector<ITensorType *> src_ptrs;
100
101 // Create tensors
102 srcs.reserve(shapes.size());
103
104 for(const auto &shape : shapes)
105 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100106 srcs.emplace_back(create_tensor<TensorType>(shape, data_type, 1));
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +0100107 src_ptrs.emplace_back(&srcs.back());
108 }
109
Georgios Pinitase29acf12018-07-16 14:40:09 +0100110 TensorShape dst_shape = misc::shape_calculator::calculate_depth_concatenate_shape(src_ptrs);
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100111 TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1);
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +0100112
113 // Create and configure function
114 FunctionType depth_concat;
115 depth_concat.configure(src_ptrs, &dst);
116
117 for(auto &src : srcs)
118 {
119 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
120 }
121
122 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
123
124 // Allocate tensors
125 for(auto &src : srcs)
126 {
127 src.allocator()->allocate();
128 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
129 }
130
131 dst.allocator()->allocate();
132 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
133
134 // Fill tensors
135 int i = 0;
136 for(auto &src : srcs)
137 {
138 fill(AccessorType(src), i++);
139 }
140
141 // Compute function
142 depth_concat.run();
143
144 return dst;
145 }
146
147 SimpleTensor<T> compute_reference(std::vector<TensorShape> shapes, DataType data_type)
148 {
149 std::vector<SimpleTensor<T>> srcs;
150
151 // Create and fill tensors
152 int i = 0;
153 for(const auto &shape : shapes)
154 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100155 srcs.emplace_back(shape, data_type, 1);
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +0100156 fill(srcs.back(), i++);
157 }
158
159 return reference::depthconcatenate_layer<T>(srcs);
160 }
161
162 TensorType _target{};
163 SimpleTensor<T> _reference{};
Moritz Pflanzer3ce3ff42017-07-21 17:41:02 +0100164};
165} // namespace validation
166} // namespace test
167} // namespace arm_compute
168#endif /* ARM_COMPUTE_TEST_DEPTHCONCATENATE_LAYER_FIXTURE */