2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include <armnn/Descriptors.hpp>
7 #include <armnn/IRuntime.hpp>
8 #include <armnn/INetwork.hpp>
9 #include <armnn/Runtime.hpp>
10 #include <armnn/TypesUtils.hpp>
12 #include <armnnUtils/HeapProfiling.hpp>
13 #include <armnnUtils/LeakChecking.hpp>
16 #include <valgrind/memcheck.h>
19 #include <boost/test/unit_test.hpp>
24 void RuntimeLoadedNetworksReserve(armnn::Runtime* runtime)
26 runtime->m_LoadedNetworks.reserve(1);
31 BOOST_AUTO_TEST_SUITE(Runtime)
33 BOOST_AUTO_TEST_CASE(RuntimeUnloadNetwork)
35 // build 2 mock-networks and load them into the runtime
36 armnn::IRuntime::CreationOptions options;
37 armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options));
40 armnn::NetworkId networkIdentifier1 = 1;
41 armnn::INetworkPtr mockNetwork1(armnn::INetwork::Create());
42 mockNetwork1->AddInputLayer(0, "test layer");
43 std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef};
44 runtime->LoadNetwork(networkIdentifier1, Optimize(*mockNetwork1, backends, runtime->GetDeviceSpec()));
47 armnn::NetworkId networkIdentifier2 = 2;
48 armnn::INetworkPtr mockNetwork2(armnn::INetwork::Create());
49 mockNetwork2->AddInputLayer(0, "test layer");
50 runtime->LoadNetwork(networkIdentifier2, Optimize(*mockNetwork2, backends, runtime->GetDeviceSpec()));
52 // Unloads one by its networkID.
53 BOOST_TEST(runtime->UnloadNetwork(networkIdentifier1) == armnn::Status::Success);
55 BOOST_TEST(runtime->UnloadNetwork(networkIdentifier1) == armnn::Status::Failure);
58 // Note: the current builds we don't do valgrind and gperftools based leak checking at the same
59 // time, so in practice WITH_VALGRIND and ARMNN_LEAK_CHECKING_ENABLED are exclusive. The
60 // valgrind tests can stay for x86 builds, but on hikey Valgrind is just way too slow
61 // to be integrated into the CI system.
63 #ifdef ARMNN_LEAK_CHECKING_ENABLED
65 struct DisableGlobalLeakChecking
67 DisableGlobalLeakChecking()
69 ARMNN_LOCAL_LEAK_CHECKING_ONLY();
73 BOOST_GLOBAL_FIXTURE(DisableGlobalLeakChecking);
75 BOOST_AUTO_TEST_CASE(RuntimeHeapMemoryUsageSanityChecks)
77 BOOST_TEST(ARMNN_LEAK_CHECKER_IS_ACTIVE());
79 ARMNN_SCOPED_LEAK_CHECKER("Sanity_Check_Outer");
81 ARMNN_SCOPED_LEAK_CHECKER("Sanity_Check_Inner");
82 BOOST_TEST(ARMNN_NO_LEAKS_IN_SCOPE() == true);
83 std::unique_ptr<char[]> dummyAllocation(new char[1000]);
84 BOOST_CHECK_MESSAGE(ARMNN_NO_LEAKS_IN_SCOPE() == false,
85 "A leak of 1000 bytes is expected here. "
86 "Please make sure environment variable: HEAPCHECK=draconian is set!");
87 BOOST_TEST(ARMNN_BYTES_LEAKED_IN_SCOPE() == 1000);
88 BOOST_TEST(ARMNN_OBJECTS_LEAKED_IN_SCOPE() == 1);
90 BOOST_TEST(ARMNN_NO_LEAKS_IN_SCOPE());
91 BOOST_TEST(ARMNN_BYTES_LEAKED_IN_SCOPE() == 0);
92 BOOST_TEST(ARMNN_OBJECTS_LEAKED_IN_SCOPE() == 0);
96 #endif // ARMNN_LEAK_CHECKING_ENABLED
98 // Note: this part of the code is due to be removed when we fully trust the gperftools based results.
100 // Run with the following command to get all the amazing output (in the devenv/build folder) :)
101 // valgrind --leak-check=full --show-leak-kinds=all --log-file=Valgrind_Memcheck_Leak_Report.txt armnn/test/UnitTests
102 BOOST_AUTO_TEST_CASE(RuntimeMemoryLeak)
104 // From documentation:
106 // This means that no pointer to the block can be found. The block is classified as "lost",
107 // because the programmer could not possibly have freed it at program exit, since no pointer to it exists.
108 unsigned long leakedBefore = 0;
109 unsigned long leakedAfter = 0;
111 // A start-pointer or chain of start-pointers to the block is found. Since the block is still pointed at,
112 // the programmer could, at least in principle, have freed it before program exit.
113 // We want to test this in case memory is not freed as early as it could have been.
114 unsigned long reachableBefore = 0;
115 unsigned long reachableAfter = 0;
117 // Needed as out params but we don't test them.
118 unsigned long dubious = 0;
119 unsigned long suppressed = 0;
121 armnn::NetworkId networkIdentifier1 = 1;
123 // ensure that runtime is large enough before checking for memory leaks
124 // otherwise when loading the network it will automatically reserve memory that won't be released until destruction
125 armnn::IRuntime::CreationOptions options;
126 armnn::Runtime runtime(options);
127 armnn::RuntimeLoadedNetworksReserve(&runtime);
129 // Checks for leaks before we load the network and record them so that we can see the delta after unloading.
130 VALGRIND_DO_QUICK_LEAK_CHECK;
131 VALGRIND_COUNT_LEAKS(leakedBefore, dubious, reachableBefore, suppressed);
133 // Builds a mock-network and load it into the runtime.
135 unsigned int inputShape[] = {1, 7, 1, 1};
136 armnn::TensorInfo inputTensorInfo(4, inputShape, armnn::DataType::Float32);
138 std::unique_ptr<armnn::Network> mockNetwork1 = std::make_unique<armnn::Network>();
139 mockNetwork1->AddInputLayer(0, "test layer");
142 std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef};
143 runtime.LoadNetwork(networkIdentifier1, Optimize(*mockNetwork1, backends, runtime.GetDeviceSpec()));
146 runtime.UnloadNetwork(networkIdentifier1);
148 VALGRIND_DO_ADDED_LEAK_CHECK;
149 VALGRIND_COUNT_LEAKS(leakedAfter, dubious, reachableAfter, suppressed);
151 // If we're not running under Valgrind, these vars will have been initialised to 0, so this will always pass.
152 BOOST_TEST(leakedBefore == leakedAfter);
153 BOOST_TEST(reachableBefore == reachableAfter);
155 // These are needed because VALGRIND_COUNT_LEAKS is a macro that assigns to the parameters
156 // so they are assigned to, but still considered unused, causing a warning.
157 boost::ignore_unused(dubious);
158 boost::ignore_unused(suppressed);
160 #endif // WITH_VALGRIND
162 BOOST_AUTO_TEST_CASE(RuntimeCpuRef)
164 using namespace armnn;
166 // Create runtime in which test will run
167 armnn::IRuntime::CreationOptions options;
168 armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options));
170 // build up the structure of the network
171 INetworkPtr net(INetwork::Create());
173 IConnectableLayer* input = net->AddInputLayer(0);
175 // This layer configuration isn't supported by CpuAcc, should be fall back to CpuRef.
176 NormalizationDescriptor descriptor;
177 IConnectableLayer* normalize = net->AddNormalizationLayer(descriptor);
179 IConnectableLayer* output = net->AddOutputLayer(0);
181 input->GetOutputSlot(0).Connect(normalize->GetInputSlot(0));
182 normalize->GetOutputSlot(0).Connect(output->GetInputSlot(0));
184 input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
185 normalize->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
187 // optimize the network
188 std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
189 IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime->GetDeviceSpec());
191 // Load it into the runtime. It should success.
192 armnn::NetworkId netId;
193 BOOST_TEST(runtime->LoadNetwork(netId, std::move(optNet)) == Status::Success);
196 BOOST_AUTO_TEST_CASE(RuntimeFallbackToCpuRef)
198 using namespace armnn;
200 // Create runtime in which test will run
201 armnn::IRuntime::CreationOptions options;
202 armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options));
204 // build up the structure of the network
205 INetworkPtr net(INetwork::Create());
207 IConnectableLayer* input = net->AddInputLayer(0);
209 // This layer configuration isn't supported by CpuAcc, should be fall back to CpuRef.
210 NormalizationDescriptor descriptor;
211 IConnectableLayer* normalize = net->AddNormalizationLayer(descriptor);
213 IConnectableLayer* output = net->AddOutputLayer(0);
215 input->GetOutputSlot(0).Connect(normalize->GetInputSlot(0));
216 normalize->GetOutputSlot(0).Connect(output->GetInputSlot(0));
218 input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
219 normalize->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
221 // Allow fallback to CpuRef.
222 std::vector<armnn::BackendId> backends = { armnn::Compute::CpuAcc, armnn::Compute::CpuRef };
223 // optimize the network
224 IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime->GetDeviceSpec());
226 // Load it into the runtime. It should succeed.
227 armnn::NetworkId netId;
228 BOOST_TEST(runtime->LoadNetwork(netId, std::move(optNet)) == Status::Success);
231 BOOST_AUTO_TEST_CASE(IVGCVSW_1929_QuantizedSoftmaxIssue)
233 // Test for issue reported by Chris Nix in https://jira.arm.com/browse/IVGCVSW-1929
234 using namespace armnn;
236 // Create runtime in which test will run
237 armnn::IRuntime::CreationOptions options;
238 armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options));
240 // build up the structure of the network
241 INetworkPtr net(INetwork::Create());
242 armnn::IConnectableLayer* input = net->AddInputLayer(
246 armnn::IConnectableLayer* softmax = net->AddSoftmaxLayer(
247 armnn::SoftmaxDescriptor(),
250 armnn::IConnectableLayer* output = net->AddOutputLayer(
255 input->GetOutputSlot(0).Connect(softmax->GetInputSlot(0));
256 softmax->GetOutputSlot(0).Connect(output->GetInputSlot(0));
258 input->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo(
259 armnn::TensorShape({ 1, 5 }),
260 armnn::DataType::QuantisedAsymm8,
265 softmax->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo(
266 armnn::TensorShape({ 1, 5 }),
267 armnn::DataType::QuantisedAsymm8
270 std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef};
271 std::vector<std::string> errMessages;
272 armnn::IOptimizedNetworkPtr optNet = Optimize(
275 runtime->GetDeviceSpec(),
280 BOOST_TEST(errMessages.size() == 1);
281 BOOST_TEST(errMessages[0] ==
282 "ERROR: output 0 of layer Softmax (softmax) is of type "
283 "Quantized 8 bit but its scale parameter has not been set");
287 BOOST_AUTO_TEST_SUITE_END()