blob: c6da261c157f86624244c21563c02abb510934c8 [file] [log] [blame]
Aron Virginas-Tarc26ba752018-10-22 13:32:01 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00006#include <test/RuntimeTests.hpp>
Aron Virginas-Tarc26ba752018-10-22 13:32:01 +01007
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00008#include <LeakChecking.hpp>
Aron Virginas-Tarc26ba752018-10-22 13:32:01 +01009
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +000010#include <backendsCommon/test/RuntimeTestImpl.hpp>
Aron Virginas-Tarc26ba752018-10-22 13:32:01 +010011
12#include <boost/core/ignore_unused.hpp>
13#include <boost/test/unit_test.hpp>
14
15#ifdef WITH_VALGRIND
16#include <valgrind/memcheck.h>
17#endif
18
19BOOST_AUTO_TEST_SUITE(ClRuntime)
20
21BOOST_AUTO_TEST_CASE(RuntimeValidateGpuDeviceSupportLayerNoFallback)
22{
23 // build up the structure of the network
24 armnn::INetworkPtr net(armnn::INetwork::Create());
25
26 armnn::IConnectableLayer* input = net->AddInputLayer(0);
27 armnn::IConnectableLayer* output = net->AddOutputLayer(0);
28
29 input->GetOutputSlot(0).Connect(output->GetInputSlot(0));
30 input->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo({ 1, 1, 4, 4 }, armnn::DataType::Float32));
31
32 armnn::IRuntime::CreationOptions options;
33 armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options));
34
35 std::vector<armnn::BackendId> backends = { armnn::Compute::GpuAcc };
36 armnn::IOptimizedNetworkPtr optNet = armnn::Optimize(*net, backends, runtime->GetDeviceSpec());
37 BOOST_CHECK(optNet);
38
39 // Load it into the runtime. It should success.
40 armnn::NetworkId netId;
41 BOOST_TEST(runtime->LoadNetwork(netId, std::move(optNet)) == armnn::Status::Success);
42}
43
44#ifdef ARMNN_LEAK_CHECKING_ENABLED
45BOOST_AUTO_TEST_CASE(RuntimeMemoryLeaksGpuAcc)
46{
47 BOOST_TEST(ARMNN_LEAK_CHECKER_IS_ACTIVE());
48 armnn::IRuntime::CreationOptions options;
49 armnn::Runtime runtime(options);
50 armnn::RuntimeLoadedNetworksReserve(&runtime);
51
52 std::vector<armnn::BackendId> backends = {armnn::Compute::GpuAcc};
53 {
54 // Do a warmup of this so we make sure that all one-time
55 // initialization happens before we do the leak checking.
56 CreateAndDropDummyNetwork(backends, runtime);
57 }
58
59 {
60 ARMNN_SCOPED_LEAK_CHECKER("LoadAndUnloadNetworkGpuAcc");
61 BOOST_TEST(ARMNN_NO_LEAKS_IN_SCOPE());
62 // In the second run we check for all remaining memory
63 // in use after the network was unloaded. If there is any
64 // then it will be treated as a memory leak.
65 CreateAndDropDummyNetwork(backends, runtime);
66 BOOST_TEST(ARMNN_NO_LEAKS_IN_SCOPE());
67 BOOST_TEST(ARMNN_BYTES_LEAKED_IN_SCOPE() == 0);
68 BOOST_TEST(ARMNN_OBJECTS_LEAKED_IN_SCOPE() == 0);
69 }
70}
71#endif
72
73// Note: this part of the code is due to be removed when we fully trust the gperftools based results.
74#if defined(WITH_VALGRIND)
75BOOST_AUTO_TEST_CASE(RuntimeMemoryUsage)
76{
77 // From documentation:
78
79 // This means that no pointer to the block can be found. The block is classified as "lost",
80 // because the programmer could not possibly have freed it at program exit, since no pointer to it exists.
81 unsigned long leakedBefore = 0;
82 unsigned long leakedAfter = 0;
83
84 // A start-pointer or chain of start-pointers to the block is found. Since the block is still pointed at,
85 // the programmer could, at least in principle, have freed it before program exit.
86 // We want to test this in case memory is not freed as early as it could have been.
87 unsigned long reachableBefore = 0;
88 unsigned long reachableAfter = 0;
89
90 // Needed as out params but we don't test them.
91 unsigned long dubious = 0;
92 unsigned long suppressed = 0;
93
94 // Ensure that runtime is large enough before checking for memory leaks.
95 // Otherwise, when loading the network, it will automatically reserve memory that won't be released
96 // until destruction.
97 armnn::NetworkId networkIdentifier;
98 armnn::IRuntime::CreationOptions options;
99 armnn::Runtime runtime(options);
100 armnn::RuntimeLoadedNetworksReserve(&runtime);
101
102 // Checks for leaks before we load the network and record them so that we can see the delta after unloading.
103 VALGRIND_DO_QUICK_LEAK_CHECK;
104 VALGRIND_COUNT_LEAKS(leakedBefore, dubious, reachableBefore, suppressed);
105
106 // build a mock-network and load it into the runtime
107 std::vector<armnn::BackendId> backends = {armnn::Compute::GpuAcc};
108 {
109 armnn::TensorInfo inputTensorInfo(armnn::TensorShape({ 7, 7 }), armnn::DataType::Float32);
110 armnn::TensorInfo outputTensorInfo(armnn::TensorShape({ 7, 7 }), armnn::DataType::Float32);
111
112 armnn::INetworkPtr mockNetwork(armnn::INetwork::Create());
113
114 armnn::IConnectableLayer* input = mockNetwork->AddInputLayer(0, "input");
115 armnn::IConnectableLayer* layer = mockNetwork->AddActivationLayer(armnn::ActivationDescriptor(), "test");
116 armnn::IConnectableLayer* output = mockNetwork->AddOutputLayer(0, "output");
117
118 input->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
119 layer->GetOutputSlot(0).Connect(output->GetInputSlot(0));
120
121 // Sets the tensors in the network.
122 input->GetOutputSlot(0).SetTensorInfo(inputTensorInfo);
123 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
124
125 // optimize the network
126 armnn::IOptimizedNetworkPtr optNet = Optimize(*mockNetwork, backends, runtime.GetDeviceSpec());
127
128 runtime.LoadNetwork(networkIdentifier, std::move(optNet));
129 }
130
131 runtime.UnloadNetwork(networkIdentifier);
132
133 VALGRIND_DO_ADDED_LEAK_CHECK;
134 VALGRIND_COUNT_LEAKS(leakedAfter, dubious, reachableAfter, suppressed);
135
136 // If we're not running under Valgrind, these vars will have been initialised to 0, so this will always pass.
137 BOOST_TEST(leakedBefore == leakedAfter);
138
139 // Add resonable threshold after and before running valgrind with the ACL clear cache function.
140 // TODO Threshold set to 80k until the root cause of the memory leakage is found and fixed. Revert threshold
141 // value to 1024 when fixed.
142 BOOST_TEST(static_cast<long>(reachableAfter) - static_cast<long>(reachableBefore) < 81920);
143
144 // These are needed because VALGRIND_COUNT_LEAKS is a macro that assigns to the parameters
145 // so they are assigned to, but still considered unused, causing a warning.
146 boost::ignore_unused(dubious);
147 boost::ignore_unused(suppressed);
148}
149#endif
150
151BOOST_AUTO_TEST_SUITE_END()