blob: b2600f13f078d02a260c394bf378b9f55834f320 [file] [log] [blame]
Michalis Spyroucaa7dee2019-09-09 19:23:39 +01001/*
2 * Copyright (c) 2019 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_UNIT_DYNAMIC_TENSOR
25#define ARM_COMPUTE_TEST_UNIT_DYNAMIC_TENSOR
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
34#include "tests/validation/Helpers.h"
Georgios Pinitas2ff00092019-09-30 16:50:08 +010035#include "tests/validation/reference/ConvolutionLayer.h"
Michalis Spyroucaa7dee2019-09-09 19:23:39 +010036#include "tests/validation/reference/NormalizationLayer.h"
37
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44namespace
45{
46template <typename AllocatorType,
47 typename LifetimeMgrType,
48 typename PoolMgrType,
49 typename MemoryMgrType>
50struct MemoryManagementService
51{
52public:
Georgios Pinitas2ff00092019-09-30 16:50:08 +010053 using LftMgrType = LifetimeMgrType;
54
55public:
Michalis Spyroucaa7dee2019-09-09 19:23:39 +010056 MemoryManagementService()
57 : allocator(), lifetime_mgr(nullptr), pool_mgr(nullptr), mm(nullptr), mg(), num_pools(0)
58 {
59 lifetime_mgr = std::make_shared<LifetimeMgrType>();
60 pool_mgr = std::make_shared<PoolMgrType>();
61 mm = std::make_shared<MemoryMgrType>(lifetime_mgr, pool_mgr);
62 mg = MemoryGroup(mm);
63 }
64
65 void populate(size_t pools)
66 {
67 mm->populate(allocator, pools);
68 num_pools = pools;
69 }
70
71 void clear()
72 {
73 mm->clear();
74 num_pools = 0;
75 }
76
77 void validate(bool validate_finalized) const
78 {
79 ARM_COMPUTE_EXPECT(mm->pool_manager() != nullptr, framework::LogLevel::ERRORS);
80 ARM_COMPUTE_EXPECT(mm->lifetime_manager() != nullptr, framework::LogLevel::ERRORS);
81
82 if(validate_finalized)
83 {
84 ARM_COMPUTE_EXPECT(mm->lifetime_manager()->are_all_finalized(), framework::LogLevel::ERRORS);
85 }
86 ARM_COMPUTE_EXPECT(mm->pool_manager()->num_pools() == num_pools, framework::LogLevel::ERRORS);
87 }
88
89 AllocatorType allocator;
90 std::shared_ptr<LifetimeMgrType> lifetime_mgr;
91 std::shared_ptr<PoolMgrType> pool_mgr;
92 std::shared_ptr<MemoryMgrType> mm;
93 MemoryGroup mg;
94 size_t num_pools;
95};
Georgios Pinitasb785dd42019-09-19 12:09:32 +010096
97template <typename MemoryMgrType, typename FuncType, typename ITensorType>
98class SimpleFunctionWrapper
99{
100public:
101 SimpleFunctionWrapper(std::shared_ptr<MemoryMgrType> mm)
102 : _func(mm)
103 {
104 }
105 void configure(ITensorType *src, ITensorType *dst)
106 {
107 }
108 void run()
109 {
110 _func.run();
111 }
112
113private:
114 FuncType _func;
115};
Michalis Spyroucaa7dee2019-09-09 19:23:39 +0100116} // namespace
117
118/** Simple test case to run a single function with different shapes twice.
119 *
120 * Runs a specified function twice, where the second time the size of the input/output is different
121 * Internal memory of the function and input/output are managed by different services
122 */
123template <typename TensorType,
124 typename AccessorType,
Georgios Pinitas2ff00092019-09-30 16:50:08 +0100125 typename MemoryManagementServiceType,
Georgios Pinitasb785dd42019-09-19 12:09:32 +0100126 typename SimpleFunctionWrapperType>
Michalis Spyroucaa7dee2019-09-09 19:23:39 +0100127class DynamicTensorType3SingleFunction : public framework::Fixture
128{
Georgios Pinitas2ff00092019-09-30 16:50:08 +0100129 using T = float;
Michalis Spyroucaa7dee2019-09-09 19:23:39 +0100130
131public:
132 template <typename...>
133 void setup(TensorShape input_level0, TensorShape input_level1)
134 {
135 input_l0 = input_level0;
136 input_l1 = input_level1;
137 run();
138 }
139
140protected:
141 void run()
142 {
143 MemoryManagementServiceType serv_internal;
144 MemoryManagementServiceType serv_cross;
145 const size_t num_pools = 1;
146 const bool validate_finalized = true;
147
148 // Create Tensor shapes.
149 TensorShape level_0 = TensorShape(input_l0);
150 TensorShape level_1 = TensorShape(input_l1);
151
152 // Level 0
153 // Create tensors
Georgios Pinitasb785dd42019-09-19 12:09:32 +0100154 TensorType src = create_tensor<TensorType>(level_0, DataType::F32, 1);
155 TensorType dst = create_tensor<TensorType>(level_0, DataType::F32, 1);
Michalis Spyroucaa7dee2019-09-09 19:23:39 +0100156
157 serv_cross.mg.manage(&src);
158 serv_cross.mg.manage(&dst);
159
160 // Create and configure function
Georgios Pinitasb785dd42019-09-19 12:09:32 +0100161 SimpleFunctionWrapperType layer(serv_internal.mm);
162 layer.configure(&src, &dst);
Michalis Spyroucaa7dee2019-09-09 19:23:39 +0100163
164 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
165 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
166
167 // Allocate tensors
168 src.allocator()->allocate();
169 dst.allocator()->allocate();
170
171 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
172 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
173
174 // Populate and validate memory manager
175 serv_cross.populate(num_pools);
176 serv_internal.populate(num_pools);
177 serv_cross.validate(validate_finalized);
178 serv_internal.validate(validate_finalized);
179
180 // Extract lifetime manager meta-data information
181 internal_l0 = serv_internal.lifetime_mgr->info();
182 cross_l0 = serv_cross.lifetime_mgr->info();
183
184 // Acquire memory manager, fill tensors and compute functions
185 serv_cross.mg.acquire();
Georgios Pinitasb785dd42019-09-19 12:09:32 +0100186 arm_compute::test::library->fill_tensor_value(AccessorType(src), 12.f);
187 layer.run();
Michalis Spyroucaa7dee2019-09-09 19:23:39 +0100188 serv_cross.mg.release();
189
190 // Clear manager
191 serv_cross.clear();
192 serv_internal.clear();
193 serv_cross.validate(validate_finalized);
194 serv_internal.validate(validate_finalized);
195
196 // Level 1
197 // Update the tensor shapes
198 src.info()->set_tensor_shape(level_1);
199 dst.info()->set_tensor_shape(level_1);
200 src.info()->set_is_resizable(true);
201 dst.info()->set_is_resizable(true);
202
203 serv_cross.mg.manage(&src);
204 serv_cross.mg.manage(&dst);
205
206 // Re-configure the function
Georgios Pinitasb785dd42019-09-19 12:09:32 +0100207 layer.configure(&src, &dst);
Michalis Spyroucaa7dee2019-09-09 19:23:39 +0100208
209 // Allocate tensors
210 src.allocator()->allocate();
211 dst.allocator()->allocate();
212
213 // Populate and validate memory manager
214 serv_cross.populate(num_pools);
215 serv_internal.populate(num_pools);
216 serv_cross.validate(validate_finalized);
217 serv_internal.validate(validate_finalized);
218
219 // Extract lifetime manager meta-data information
220 internal_l1 = serv_internal.lifetime_mgr->info();
221 cross_l1 = serv_cross.lifetime_mgr->info();
222
223 // Compute functions
224 serv_cross.mg.acquire();
225 arm_compute::test::library->fill_tensor_value(AccessorType(src), 12.f);
Georgios Pinitasb785dd42019-09-19 12:09:32 +0100226 layer.run();
Michalis Spyroucaa7dee2019-09-09 19:23:39 +0100227 serv_cross.mg.release();
228
229 // Clear manager
230 serv_cross.clear();
231 serv_internal.clear();
232 serv_cross.validate(validate_finalized);
233 serv_internal.validate(validate_finalized);
234 }
235
236public:
Georgios Pinitas2ff00092019-09-30 16:50:08 +0100237 TensorShape input_l0{}, input_l1{};
238 typename MemoryManagementServiceType::LftMgrType::info_type internal_l0{}, internal_l1{};
239 typename MemoryManagementServiceType::LftMgrType::info_type cross_l0{}, cross_l1{};
240};
241
242/** Simple test case to run a single function with different shapes twice.
243 *
244 * Runs a specified function twice, where the second time the size of the input/output is different
245 * Internal memory of the function and input/output are managed by different services
246 */
247template <typename TensorType,
248 typename AccessorType,
249 typename MemoryManagementServiceType,
250 typename ComplexFunctionType>
251class DynamicTensorType3ComplexFunction : public framework::Fixture
252{
253 using T = float;
254
255public:
256 template <typename...>
257 void setup(std::vector<TensorShape> input_shapes, TensorShape weights_shape, TensorShape bias_shape, std::vector<TensorShape> output_shapes, PadStrideInfo info)
258 {
259 num_iterations = input_shapes.size();
260 _data_type = DataType::F32;
261 _data_layout = DataLayout::NHWC;
262 _input_shapes = input_shapes;
263 _output_shapes = output_shapes;
264 _weights_shape = weights_shape;
265 _bias_shape = bias_shape;
266 _info = info;
267
268 // Create function
269 _f_target = support::cpp14::make_unique<ComplexFunctionType>(_ms.mm);
270 }
271
272 void run_iteration(unsigned int idx)
273 {
274 auto input_shape = _input_shapes[idx];
275 auto output_shape = _output_shapes[idx];
276
277 dst_ref = run_reference(input_shape, _weights_shape, _bias_shape, output_shape, _info);
278 dst_target = run_target(input_shape, _weights_shape, _bias_shape, output_shape, _info, WeightsInfo());
279 }
280
281protected:
282 template <typename U>
283 void fill(U &&tensor, int i)
284 {
285 switch(tensor.data_type())
286 {
287 case DataType::F32:
288 {
289 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
290 library->fill(tensor, distribution, i);
291 break;
292 }
293 default:
294 library->fill_tensor_uniform(tensor, i);
295 }
296 }
297
298 TensorType run_target(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape,
299 PadStrideInfo info, WeightsInfo weights_info)
300 {
301 if(_data_layout == DataLayout::NHWC)
302 {
303 permute(input_shape, PermutationVector(2U, 0U, 1U));
304 permute(weights_shape, PermutationVector(2U, 0U, 1U));
305 permute(output_shape, PermutationVector(2U, 0U, 1U));
306 }
307
308 _weights_target = create_tensor<TensorType>(weights_shape, _data_type, 1, QuantizationInfo(), _data_layout);
309 _bias_target = create_tensor<TensorType>(bias_shape, _data_type, 1);
310
311 // Create tensors
312 TensorType src = create_tensor<TensorType>(input_shape, _data_type, 1, QuantizationInfo(), _data_layout);
313 TensorType dst = create_tensor<TensorType>(output_shape, _data_type, 1, QuantizationInfo(), _data_layout);
314
315 // Create and configure function
316 _f_target->configure(&src, &_weights_target, &_bias_target, &dst, info, weights_info);
317
318 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
319 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
320
321 // Allocate tensors
322 src.allocator()->allocate();
323 dst.allocator()->allocate();
324 _weights_target.allocator()->allocate();
325 _bias_target.allocator()->allocate();
326
327 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
328 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
329
330 // Fill tensors
331 fill(AccessorType(src), 0);
332 fill(AccessorType(_weights_target), 1);
333 fill(AccessorType(_bias_target), 2);
334
335 // Populate and validate memory manager
336 _ms.clear();
337 _ms.populate(1);
338 _ms.mg.acquire();
339
340 // Compute NEConvolutionLayer function
341 _f_target->run();
342 _ms.mg.release();
343
344 return dst;
345 }
346
347 SimpleTensor<T> run_reference(TensorShape input_shape, TensorShape weights_shape, TensorShape bias_shape, TensorShape output_shape, PadStrideInfo info)
348 {
349 // Create reference
350 SimpleTensor<T> src{ input_shape, _data_type, 1 };
351 SimpleTensor<T> weights{ weights_shape, _data_type, 1 };
352 SimpleTensor<T> bias{ bias_shape, _data_type, 1 };
353
354 // Fill reference
355 fill(src, 0);
356 fill(weights, 1);
357 fill(bias, 2);
358
359 return reference::convolution_layer<T>(src, weights, bias, output_shape, info);
360 }
361
362public:
363 unsigned int num_iterations{ 0 };
364 SimpleTensor<T> dst_ref{};
365 TensorType dst_target{};
366
367private:
368 DataType _data_type{ DataType::UNKNOWN };
369 DataLayout _data_layout{ DataLayout::UNKNOWN };
370 PadStrideInfo _info{};
371 std::vector<TensorShape> _input_shapes{};
372 std::vector<TensorShape> _output_shapes{};
373 TensorShape _weights_shape{};
374 TensorShape _bias_shape{};
375 MemoryManagementServiceType _ms{};
376 TensorType _weights_target{};
377 TensorType _bias_target{};
378 std::unique_ptr<ComplexFunctionType> _f_target{};
Michalis Spyroucaa7dee2019-09-09 19:23:39 +0100379};
380} // namespace validation
381} // namespace test
382} // namespace arm_compute
383#endif /* ARM_COMPUTE_TEST_UNIT_DYNAMIC_TENSOR */