aboutsummaryrefslogtreecommitdiff
path: root/tests/validation/fixtures/UNIT/MemoryManagerFixture.h
blob: 14f22a8d21c78dff8166fa04db166113bac691f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * Copyright (c) 2017-2018 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef ARM_COMPUTE_TEST_UNIT_MEMORY_MANAGER
#define ARM_COMPUTE_TEST_UNIT_MEMORY_MANAGER

#include "arm_compute/core/TensorShape.h"
#include "arm_compute/core/Types.h"
#include "arm_compute/runtime/BlobLifetimeManager.h"
#include "arm_compute/runtime/MemoryManagerOnDemand.h"
#include "arm_compute/runtime/PoolManager.h"
#include "tests/AssetsLibrary.h"
#include "tests/Globals.h"
#include "tests/IAccessor.h"
#include "tests/framework/Asserts.h"
#include "tests/framework/Fixture.h"
#include "tests/validation/Helpers.h"
#include "tests/validation/reference/FullyConnectedLayer.h"
#include "tests/validation/reference/SoftmaxLayer.h"

namespace arm_compute
{
namespace test
{
namespace validation
{
/** Simple test case to run two fully connected layers using a blob affinity memory manager
 *
 * Runs two fully connected layers back to back
 */
template <typename TensorType, typename AccessorType, typename AllocatorType, typename FullyConnectedFunction>
class BlobMemoryManagerSimpleTestCaseFixture : public framework::Fixture
{
    using T = float;

public:
    void setup()
    {
        _target    = compute_target();
        _reference = compute_reference();
    };

protected:
    template <typename U>
    void fill(U &&tensor, int i)
    {
        std::uniform_real_distribution<> distribution(0.5f, 1.f);
        library->fill(tensor, distribution, i);
    }

    TensorType compute_target()
    {
        auto lifetime_mgr = std::make_shared<BlobLifetimeManager>();
        auto pool_mgr     = std::make_shared<PoolManager>();
        auto mm           = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);

        // Create tensors
        TensorType w1  = create_tensor<TensorType>(TensorShape(128U, 128U), DataType::F32, 1);
        TensorType b1  = create_tensor<TensorType>(TensorShape(128U), DataType::F32, 1);
        TensorType w2  = create_tensor<TensorType>(TensorShape(128U, 24U), DataType::F32, 1);
        TensorType b2  = create_tensor<TensorType>(TensorShape(24U), DataType::F32, 1);
        TensorType src = create_tensor<TensorType>(TensorShape(128U), DataType::F32, 1);
        TensorType fc1 = create_tensor<TensorType>(TensorShape(128U), DataType::F32, 1);
        TensorType dst = create_tensor<TensorType>(TensorShape(24U), DataType::F32, 1);

        // Create and configure function
        FullyConnectedFunction fc_layer_1(mm);
        FullyConnectedFunction fc_layer_2(mm);
        fc_layer_1.configure(&src, &w1, &b1, &fc1);
        fc_layer_2.configure(&fc1, &w2, &b2, &dst);

        // Allocate tensors
        w1.allocator()->allocate();
        b1.allocator()->allocate();
        w2.allocator()->allocate();
        b2.allocator()->allocate();
        src.allocator()->allocate();
        fc1.allocator()->allocate();
        dst.allocator()->allocate();

        // Finalize memory manager
        mm->populate(_allocator, 1 /* num_pools */);
        ARM_COMPUTE_EXPECT(mm->lifetime_manager()->are_all_finalized(), framework::LogLevel::ERRORS);
        ARM_COMPUTE_EXPECT(mm->pool_manager()->num_pools() == 1, framework::LogLevel::ERRORS);

        // Fill tensors
        fill(AccessorType(src), 0);
        fill(AccessorType(w1), 1);
        fill(AccessorType(b1), 2);
        fill(AccessorType(w2), 3);
        fill(AccessorType(b2), 4);

        // Compute functions
        fc_layer_1.run();
        fc_layer_2.run();

        return dst;
    }

    SimpleTensor<T> compute_reference()
    {
        // Create reference
        SimpleTensor<T> w1{ TensorShape(128U, 128U), DataType::F32 };
        SimpleTensor<T> b1{ TensorShape(128U), DataType::F32 };
        SimpleTensor<T> w2{ TensorShape(128U, 24U), DataType::F32 };
        SimpleTensor<T> b2{ TensorShape(24U), DataType::F32 };
        SimpleTensor<T> src{ TensorShape(128U), DataType::F32 };

        // Fill reference
        fill(src, 0);
        fill(w1, 1);
        fill(b1, 2);
        fill(w2, 3);
        fill(b2, 4);

        auto fc1 = reference::fully_connected_layer(src, w1, b1, TensorShape(128U));
        return reference::fully_connected_layer(fc1, w2, b2, TensorShape(24U));
    }

protected:
    TensorType      _target{};
    SimpleTensor<T> _reference{};
    AllocatorType   _allocator{};
};

/** Test case to run two fully connected layers using a blob affinity memory manager,
 *  reconfigure with different shapes and rerun
 *
 * Runs two fully connected layers back to back then reconfigures with different batch size and reruns
 * Shapes of the reconfigure step are smaller that the initial configured step
 */
template <typename TensorType, typename AccessorType, typename AllocatorType, typename FullyConnectedFunction>
class BlobMemoryManagerReconfigureTestCaseFixture : public framework::Fixture
{
    using T = float;

public:
    void setup()
    {
        _max_batches = 8;
        _cur_batches = 6;
        _target      = compute_target();
        _reference   = compute_reference();
    };

protected:
    template <typename U>
    void fill(U &&tensor, int i)
    {
        std::uniform_real_distribution<> distribution(0.5f, 1.f);
        library->fill(tensor, distribution, i);
    }

    TensorType compute_target()
    {
        AllocatorType allocator{};
        auto          lifetime_mgr = std::make_shared<BlobLifetimeManager>();
        auto          pool_mgr     = std::make_shared<PoolManager>();
        auto          mm           = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);

        // Create tensors
        TensorType w1  = create_tensor<TensorType>(TensorShape(128U, 128U), DataType::F32, 1);
        TensorType b1  = create_tensor<TensorType>(TensorShape(128U), DataType::F32, 1);
        TensorType w2  = create_tensor<TensorType>(TensorShape(128U, 24U), DataType::F32, 1);
        TensorType b2  = create_tensor<TensorType>(TensorShape(24U), DataType::F32, 1);
        TensorType src = create_tensor<TensorType>(TensorShape(128U, _max_batches), DataType::F32, 1);
        TensorType fc1 = create_tensor<TensorType>(TensorShape(128U, _max_batches), DataType::F32, 1);
        TensorType dst = create_tensor<TensorType>(TensorShape(24U, _max_batches), DataType::F32, 1);

        // Create and configure function
        FullyConnectedFunction fc_layer_1(mm);
        FullyConnectedFunction fc_layer_2(mm);
        fc_layer_1.configure(&src, &w1, &b1, &fc1);
        fc_layer_2.configure(&fc1, &w2, &b2, &dst);

        // Allocate persistent tensors
        w1.allocator()->allocate();
        b1.allocator()->allocate();
        w2.allocator()->allocate();
        b2.allocator()->allocate();

        // Allocate tensors (1st iteration)
        src.allocator()->allocate();
        fc1.allocator()->allocate();
        dst.allocator()->allocate();

        // Finalize memory manager
        mm->populate(_allocator, 1 /* num_pools */);
        ARM_COMPUTE_EXPECT(mm->lifetime_manager()->are_all_finalized(), framework::LogLevel::ERRORS);
        ARM_COMPUTE_EXPECT(mm->pool_manager()->num_pools() == 1, framework::LogLevel::ERRORS);

        // Fill tensors (1st iteration)
        fill(AccessorType(src), 0);
        fill(AccessorType(w1), 1);
        fill(AccessorType(b1), 2);
        fill(AccessorType(w2), 3);
        fill(AccessorType(b2), 4);

        // Compute functions (1st iteration)
        fc_layer_1.run();
        fc_layer_2.run();

        // Update tensor shapes (2nd iteration)
        auto src_padding     = src.allocator()->info().padding();
        auto fc1_padding     = fc1.allocator()->info().padding();
        auto dst_padding     = dst.allocator()->info().padding();
        int  diff            = _max_batches - _cur_batches;
        auto new_src_padding = PaddingSize(src_padding.top, src_padding.right, src_padding.bottom + diff, src_padding.left);
        auto new_fc1_padding = PaddingSize(fc1_padding.top, fc1_padding.right, fc1_padding.bottom + diff, fc1_padding.left);
        auto new_dst_padding = PaddingSize(dst_padding.top, dst_padding.right, dst_padding.bottom + diff, dst_padding.left);
        src.allocator()->info().set_tensor_shape(TensorShape(128U, _cur_batches)).set_is_resizable(true).extend_padding(new_src_padding);
        src.allocator()->info().set_is_resizable(false);
        fc1.allocator()->info().set_tensor_shape(TensorShape(128U, _cur_batches)).set_is_resizable(true).extend_padding(new_fc1_padding);
        fc1.allocator()->info().set_is_resizable(false);
        dst.allocator()->info().set_tensor_shape(TensorShape(24U, _cur_batches)).set_is_resizable(true).extend_padding(new_dst_padding);
        dst.allocator()->info().set_is_resizable(false);

        // Configure FC info
        FullyConnectedLayerInfo fc_info;
        fc_info.retain_internal_weights = true;

        // Configure functions (2nd iteration)
        fc_layer_1.configure(&src, &w1, &b1, &fc1, fc_info);
        fc_layer_2.configure(&fc1, &w2, &b2, &dst, fc_info);

        // Fill tensors (2nd iteration)
        fill(AccessorType(src), 5);

        // Compute functions (2nd iteration)
        fc_layer_1.run();
        fc_layer_2.run();

        return dst;
    }

    SimpleTensor<T> compute_reference()
    {
        // Create reference
        SimpleTensor<T> w1{ TensorShape(128U, 128U), DataType::F32 };
        SimpleTensor<T> b1{ TensorShape(128U), DataType::F32 };
        SimpleTensor<T> w2{ TensorShape(128U, 24U), DataType::F32 };
        SimpleTensor<T> b2{ TensorShape(24U), DataType::F32 };
        SimpleTensor<T> src{ TensorShape(128U, _cur_batches), DataType::F32 };

        // Fill reference
        fill(src, 5);
        fill(w1, 1);
        fill(b1, 2);
        fill(w2, 3);
        fill(b2, 4);

        auto fc1 = reference::fully_connected_layer(src, w1, b1, TensorShape(128U, _cur_batches));
        return reference::fully_connected_layer(fc1, w2, b2, TensorShape(24U, _cur_batches));
    }

protected:
    TensorType      _target{};
    SimpleTensor<T> _reference{};
    AllocatorType   _allocator{};
    unsigned int    _max_batches{};
    unsigned int    _cur_batches{};
};

/** Test case to run a fully connected layer followed by a softmax layer using a blob affinity memory manager,
 *  reconfigure with different shapes and rerun
 *
 * Runs a fully connected convolution layer followed by a softmax layer then reconfigures with different batch size and reruns
 * Shapes of the reconfigure step are smaller that the initial configured step
 */
template <typename TensorType, typename AccessorType, typename AllocatorType, typename FullyConnectedFunction, typename SoftmaxFunction>
class BlobMemoryManagerReconfigure2TestCaseFixture : public framework::Fixture
{
    using T = float;

public:
    void setup()
    {
        _max_batches = 30;
        _cur_batches = 3;
        _target      = compute_target();
        _reference   = compute_reference();
    };

protected:
    template <typename U>
    void fill(U &&tensor, int i)
    {
        std::uniform_real_distribution<> distribution(0.5f, 1.f);
        library->fill(tensor, distribution, i);
    }

    TensorType compute_target()
    {
        AllocatorType allocator{};
        auto          lifetime_mgr = std::make_shared<BlobLifetimeManager>();
        auto          pool_mgr     = std::make_shared<PoolManager>();
        auto          mm           = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);

        // Create tensors
        TensorType w   = create_tensor<TensorType>(TensorShape(112U, 8U), DataType::F32, 1);
        TensorType b   = create_tensor<TensorType>(TensorShape(8U), DataType::F32, 1);
        TensorType src = create_tensor<TensorType>(TensorShape(1U, 1U, 112U, _max_batches), DataType::F32, 1);
        TensorType fc  = create_tensor<TensorType>(TensorShape(8U, _max_batches), DataType::F32, 1);
        TensorType dst = create_tensor<TensorType>(TensorShape(8U, _max_batches), DataType::F32, 1);

        // Create and configure function
        FullyConnectedFunction fc_layer(mm);
        SoftmaxFunction        smx_layer(mm);
        fc_layer.configure(&src, &w, &b, &fc);
        smx_layer.configure(&fc, &dst);

        // Allocate persistent tensors
        w.allocator()->allocate();
        b.allocator()->allocate();

        // Allocate tensors (1st iteration)
        src.allocator()->allocate();
        fc.allocator()->allocate();
        dst.allocator()->allocate();

        // Finalize memory manager
        mm->populate(_allocator, 1 /* num_pools */);
        ARM_COMPUTE_EXPECT(mm->lifetime_manager()->are_all_finalized(), framework::LogLevel::ERRORS);
        ARM_COMPUTE_EXPECT(mm->pool_manager()->num_pools() == 1, framework::LogLevel::ERRORS);

        // Fill tensors (1st iteration)
        fill(AccessorType(src), 0);
        fill(AccessorType(w), 1);
        fill(AccessorType(b), 2);

        // Compute functions (1st iteration)
        fc_layer.run();
        smx_layer.run();

        // Get padding requirements
        auto fc_padding = fc.allocator()->info().padding();

        // Configure FC info
        FullyConnectedLayerInfo fc_info;
        fc_info.retain_internal_weights = true;

        // Run rest iterations
        for(int i = _max_batches; i >= static_cast<int>(_cur_batches); --i)
        {
            int  diff           = _max_batches - i;
            auto new_fc_padding = PaddingSize(fc_padding.top, fc_padding.right, fc_padding.bottom + diff, fc_padding.left);
            src.allocator()->info().set_tensor_shape(TensorShape(1U, 1U, 112U, i));
            fc.allocator()->info().set_tensor_shape(TensorShape(8U, i)).set_is_resizable(true).extend_padding(new_fc_padding);
            fc.allocator()->info().set_is_resizable(false);
            dst.allocator()->info().set_tensor_shape(TensorShape(8U, i));

            // Configure functions
            fc_layer.configure(&src, &w, &b, &fc, fc_info);
            smx_layer.configure(&fc, &dst);

            // Fill tensors
            fill(AccessorType(src), 3);

            // Compute functions
            fc_layer.run();
            smx_layer.run();
        }

        return dst;
    }

    SimpleTensor<T> compute_reference()
    {
        // Create reference
        SimpleTensor<T> w{ TensorShape(112U, 8U), DataType::F32 };
        SimpleTensor<T> b{ TensorShape(8U), DataType::F32 };
        SimpleTensor<T> src{ TensorShape(1U, 1U, 112U, _cur_batches), DataType::F32 };

        // Fill reference
        fill(src, 3);
        fill(w, 1);
        fill(b, 2);

        auto fc = reference::fully_connected_layer(src, w, b, TensorShape(8U, _cur_batches));
        return reference::softmax_layer(fc, 1.f);
    }

protected:
    TensorType      _target{};
    SimpleTensor<T> _reference{};
    AllocatorType   _allocator{};
    unsigned int    _max_batches{};
    unsigned int    _cur_batches{};
};
} // namespace validation
} // namespace test
} // namespace arm_compute
#endif /* ARM_COMPUTE_TEST_UNIT_MEMORY_MANAGER */