telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame^] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #include <boost/test/unit_test.hpp> |
| 6 | |
| 7 | #include "armnn/TypesUtils.hpp" |
| 8 | |
| 9 | #include "armnn/IRuntime.hpp" |
| 10 | #include "armnn/INetwork.hpp" |
| 11 | #include "armnn/Descriptors.hpp" |
| 12 | #include "Runtime.hpp" |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 13 | #include "HeapProfiling.hpp" |
| 14 | #include "LeakChecking.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 15 | |
| 16 | #ifdef WITH_VALGRIND |
| 17 | #include "valgrind/memcheck.h" |
| 18 | #endif |
| 19 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 20 | namespace armnn |
| 21 | { |
| 22 | |
| 23 | void RuntimeLoadedNetworksReserve(armnn::Runtime* runtime) |
| 24 | { |
| 25 | runtime->m_LoadedNetworks.reserve(1); |
| 26 | } |
| 27 | |
| 28 | } |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(Runtime) |
| 31 | |
| 32 | BOOST_AUTO_TEST_CASE(RuntimeUnloadNetwork) |
| 33 | { |
| 34 | // build 2 mock-networks and load them into the runtime |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 35 | armnn::IRuntime::CreationOptions options; |
| 36 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 37 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 38 | // Mock network 1. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 39 | armnn::NetworkId networkIdentifier1 = 1; |
| 40 | armnn::INetworkPtr mockNetwork1(armnn::INetwork::Create()); |
| 41 | mockNetwork1->AddInputLayer(0, "test layer"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 42 | std::vector<armnn::Compute> backends = {armnn::Compute::CpuRef}; |
| 43 | runtime->LoadNetwork(networkIdentifier1, Optimize(*mockNetwork1, backends, runtime->GetDeviceSpec())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 44 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 45 | // Mock network 2. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 46 | armnn::NetworkId networkIdentifier2 = 2; |
| 47 | armnn::INetworkPtr mockNetwork2(armnn::INetwork::Create()); |
| 48 | mockNetwork2->AddInputLayer(0, "test layer"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 49 | runtime->LoadNetwork(networkIdentifier2, Optimize(*mockNetwork2, backends, runtime->GetDeviceSpec())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 50 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 51 | // Unloads one by its networkID. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 52 | BOOST_TEST(runtime->UnloadNetwork(networkIdentifier1) == armnn::Status::Success); |
| 53 | |
| 54 | BOOST_TEST(runtime->UnloadNetwork(networkIdentifier1) == armnn::Status::Failure); |
| 55 | } |
| 56 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 57 | // Note: the current builds we don't do valgrind and gperftools based leak checking at the same |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | // time, so in practice WITH_VALGRIND and ARMNN_LEAK_CHECKING_ENABLED are exclusive. The |
| 59 | // valgrind tests can stay for x86 builds, but on hikey Valgrind is just way too slow |
| 60 | // to be integrated into the CI system. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 61 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 62 | #ifdef ARMNN_LEAK_CHECKING_ENABLED |
| 63 | |
| 64 | struct DisableGlobalLeakChecking |
| 65 | { |
| 66 | DisableGlobalLeakChecking() |
| 67 | { |
| 68 | ARMNN_LOCAL_LEAK_CHECKING_ONLY(); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | BOOST_GLOBAL_FIXTURE(DisableGlobalLeakChecking); |
| 73 | |
| 74 | void CreateAndDropDummyNetwork(const std::vector<armnn::Compute>& backends, armnn::Runtime& runtime) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 75 | { |
| 76 | armnn::NetworkId networkIdentifier; |
| 77 | { |
| 78 | armnn::TensorInfo inputTensorInfo(armnn::TensorShape({ 7, 7 }), armnn::DataType::Float32); |
| 79 | armnn::TensorInfo outputTensorInfo(armnn::TensorShape({ 7, 7 }), armnn::DataType::Float32); |
| 80 | |
| 81 | armnn::INetworkPtr network(armnn::INetwork::Create()); |
| 82 | |
| 83 | armnn::IConnectableLayer* input = network->AddInputLayer(0, "input"); |
| 84 | armnn::IConnectableLayer* layer = network->AddActivationLayer(armnn::ActivationDescriptor(), "test"); |
| 85 | armnn::IConnectableLayer* output = network->AddOutputLayer(0, "output"); |
| 86 | |
| 87 | input->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 88 | layer->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 89 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 90 | // Sets the tensors in the network. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 91 | input->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
| 92 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 93 | |
| 94 | // optimize the network |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 95 | armnn::IOptimizedNetworkPtr optNet = Optimize(*network, backends, runtime.GetDeviceSpec()); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 96 | |
| 97 | runtime.LoadNetwork(networkIdentifier, std::move(optNet)); |
| 98 | } |
| 99 | |
| 100 | runtime.UnloadNetwork(networkIdentifier); |
| 101 | } |
| 102 | |
| 103 | BOOST_AUTO_TEST_CASE(RuntimeHeapMemoryUsageSanityChecks) |
| 104 | { |
| 105 | BOOST_TEST(ARMNN_LEAK_CHECKER_IS_ACTIVE()); |
| 106 | { |
| 107 | ARMNN_SCOPED_LEAK_CHECKER("Sanity_Check_Outer"); |
| 108 | { |
| 109 | ARMNN_SCOPED_LEAK_CHECKER("Sanity_Check_Inner"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 110 | BOOST_TEST(ARMNN_NO_LEAKS_IN_SCOPE() == true); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 111 | std::unique_ptr<char[]> dummyAllocation(new char[1000]); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 112 | BOOST_CHECK_MESSAGE(ARMNN_NO_LEAKS_IN_SCOPE() == false, |
| 113 | "A leak of 1000 bytes is expected here. " |
| 114 | "Please make sure environment variable: HEAPCHECK=draconian is set!"); |
| 115 | BOOST_TEST(ARMNN_BYTES_LEAKED_IN_SCOPE() == 1000); |
| 116 | BOOST_TEST(ARMNN_OBJECTS_LEAKED_IN_SCOPE() == 1); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 117 | } |
| 118 | BOOST_TEST(ARMNN_NO_LEAKS_IN_SCOPE()); |
| 119 | BOOST_TEST(ARMNN_BYTES_LEAKED_IN_SCOPE() == 0); |
| 120 | BOOST_TEST(ARMNN_OBJECTS_LEAKED_IN_SCOPE() == 0); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #ifdef ARMCOMPUTECL_ENABLED |
| 125 | BOOST_AUTO_TEST_CASE(RuntimeMemoryLeaksGpuAcc) |
| 126 | { |
| 127 | BOOST_TEST(ARMNN_LEAK_CHECKER_IS_ACTIVE()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 128 | armnn::IRuntime::CreationOptions options; |
| 129 | armnn::Runtime runtime(options); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 130 | armnn::RuntimeLoadedNetworksReserve(&runtime); |
| 131 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 132 | std::vector<armnn::Compute> backends = {armnn::Compute::GpuAcc}; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 133 | { |
| 134 | // Do a warmup of this so we make sure that all one-time |
| 135 | // initialization happens before we do the leak checking. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 136 | CreateAndDropDummyNetwork(backends, runtime); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | { |
| 140 | ARMNN_SCOPED_LEAK_CHECKER("LoadAndUnloadNetworkGpuAcc"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 141 | BOOST_TEST(ARMNN_NO_LEAKS_IN_SCOPE()); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 142 | // In the second run we check for all remaining memory |
| 143 | // in use after the network was unloaded. If there is any |
| 144 | // then it will be treated as a memory leak. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 145 | CreateAndDropDummyNetwork(backends, runtime); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 146 | BOOST_TEST(ARMNN_NO_LEAKS_IN_SCOPE()); |
| 147 | BOOST_TEST(ARMNN_BYTES_LEAKED_IN_SCOPE() == 0); |
| 148 | BOOST_TEST(ARMNN_OBJECTS_LEAKED_IN_SCOPE() == 0); |
| 149 | } |
| 150 | } |
| 151 | #endif // ARMCOMPUTECL_ENABLED |
| 152 | |
| 153 | #ifdef ARMCOMPUTENEON_ENABLED |
| 154 | BOOST_AUTO_TEST_CASE(RuntimeMemoryLeaksCpuAcc) |
| 155 | { |
| 156 | BOOST_TEST(ARMNN_LEAK_CHECKER_IS_ACTIVE()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 157 | armnn::IRuntime::CreationOptions options; |
| 158 | armnn::Runtime runtime(options); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 159 | armnn::RuntimeLoadedNetworksReserve(&runtime); |
| 160 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 161 | std::vector<armnn::Compute> backends = {armnn::Compute::CpuAcc}; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 162 | { |
| 163 | // Do a warmup of this so we make sure that all one-time |
| 164 | // initialization happens before we do the leak checking. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 165 | CreateAndDropDummyNetwork(backends, runtime); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | { |
| 169 | ARMNN_SCOPED_LEAK_CHECKER("LoadAndUnloadNetworkCpuAcc"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 170 | BOOST_TEST(ARMNN_NO_LEAKS_IN_SCOPE()); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 171 | // In the second run we check for all remaining memory |
| 172 | // in use after the network was unloaded. If there is any |
| 173 | // then it will be treated as a memory leak. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 174 | CreateAndDropDummyNetwork(backends, runtime); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 175 | BOOST_TEST(ARMNN_NO_LEAKS_IN_SCOPE()); |
| 176 | BOOST_TEST(ARMNN_BYTES_LEAKED_IN_SCOPE() == 0); |
| 177 | BOOST_TEST(ARMNN_OBJECTS_LEAKED_IN_SCOPE() == 0); |
| 178 | } |
| 179 | } |
| 180 | #endif // ARMCOMPUTENEON_ENABLED |
| 181 | |
| 182 | BOOST_AUTO_TEST_CASE(RuntimeMemoryLeaksCpuRef) |
| 183 | { |
| 184 | BOOST_TEST(ARMNN_LEAK_CHECKER_IS_ACTIVE()); |
| 185 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 186 | armnn::IRuntime::CreationOptions options; |
| 187 | armnn::Runtime runtime(options); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 188 | armnn::RuntimeLoadedNetworksReserve(&runtime); |
| 189 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 190 | std::vector<armnn::Compute> backends = {armnn::Compute::CpuRef}; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 191 | { |
| 192 | // Do a warmup of this so we make sure that all one-time |
| 193 | // initialization happens before we do the leak checking. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 194 | CreateAndDropDummyNetwork(backends, runtime); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | { |
| 198 | ARMNN_SCOPED_LEAK_CHECKER("LoadAndUnloadNetworkCpuRef"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 199 | BOOST_TEST(ARMNN_NO_LEAKS_IN_SCOPE()); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 200 | // In the second run we check for all remaining memory |
| 201 | // in use after the network was unloaded. If there is any |
| 202 | // then it will be treated as a memory leak. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 203 | CreateAndDropDummyNetwork(backends, runtime); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 204 | BOOST_TEST(ARMNN_NO_LEAKS_IN_SCOPE()); |
| 205 | BOOST_TEST(ARMNN_BYTES_LEAKED_IN_SCOPE() == 0); |
| 206 | BOOST_TEST(ARMNN_OBJECTS_LEAKED_IN_SCOPE() == 0); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | #endif // ARMNN_LEAK_CHECKING_ENABLED |
| 211 | |
| 212 | // Note: this part of the code is due to be removed when we fully trust the gperftools based results. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 213 | #if defined(ARMCOMPUTECL_ENABLED) && defined(WITH_VALGRIND) |
| 214 | BOOST_AUTO_TEST_CASE(RuntimeMemoryUsage) |
| 215 | { |
| 216 | // From documentation: |
| 217 | |
| 218 | // This means that no pointer to the block can be found. The block is classified as "lost", |
| 219 | // because the programmer could not possibly have freed it at program exit, since no pointer to it exists. |
| 220 | unsigned long leakedBefore = 0; |
| 221 | unsigned long leakedAfter = 0; |
| 222 | |
| 223 | // A start-pointer or chain of start-pointers to the block is found. Since the block is still pointed at, |
| 224 | // the programmer could, at least in principle, have freed it before program exit. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 225 | // We want to test this in case memory is not freed as early as it could have been. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 226 | unsigned long reachableBefore = 0; |
| 227 | unsigned long reachableAfter = 0; |
| 228 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 229 | // Needed as out params but we don't test them. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 230 | unsigned long dubious = 0; |
| 231 | unsigned long suppressed = 0; |
| 232 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 233 | // Ensure that runtime is large enough before checking for memory leaks. |
| 234 | // Otherwise, when loading the network, it will automatically reserve memory that won't be released |
| 235 | // until destruction. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 236 | armnn::NetworkId networkIdentifier; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 237 | armnn::IRuntime::CreationOptions options; |
| 238 | armnn::Runtime runtime(options); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 239 | armnn::RuntimeLoadedNetworksReserve(&runtime); |
| 240 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 241 | // Checks for leaks before we load the network and record them so that we can see the delta after unloading. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 242 | VALGRIND_DO_QUICK_LEAK_CHECK; |
| 243 | VALGRIND_COUNT_LEAKS(leakedBefore, dubious, reachableBefore, suppressed); |
| 244 | |
| 245 | // build a mock-network and load it into the runtime |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 246 | std::vector<armnn::Compute> backends = {armnn::Compute::GpuAcc}; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 247 | { |
| 248 | armnn::TensorInfo inputTensorInfo(armnn::TensorShape({ 7, 7 }), armnn::DataType::Float32); |
| 249 | armnn::TensorInfo outputTensorInfo(armnn::TensorShape({ 7, 7 }), armnn::DataType::Float32); |
| 250 | |
| 251 | armnn::INetworkPtr mockNetwork(armnn::INetwork::Create()); |
| 252 | |
| 253 | armnn::IConnectableLayer* input = mockNetwork->AddInputLayer(0, "input"); |
| 254 | armnn::IConnectableLayer* layer = mockNetwork->AddActivationLayer(armnn::ActivationDescriptor(), "test"); |
| 255 | armnn::IConnectableLayer* output = mockNetwork->AddOutputLayer(0, "output"); |
| 256 | |
| 257 | input->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 258 | layer->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 259 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 260 | // Sets the tensors in the network. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 261 | input->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
| 262 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 263 | |
| 264 | // optimize the network |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 265 | armnn::IOptimizedNetworkPtr optNet = Optimize(*mockNetwork, backends, runtime.GetDeviceSpec()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 266 | |
| 267 | runtime.LoadNetwork(networkIdentifier, std::move(optNet)); |
| 268 | } |
| 269 | |
| 270 | runtime.UnloadNetwork(networkIdentifier); |
| 271 | |
| 272 | VALGRIND_DO_ADDED_LEAK_CHECK; |
| 273 | VALGRIND_COUNT_LEAKS(leakedAfter, dubious, reachableAfter, suppressed); |
| 274 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 275 | // If we're not running under Valgrind, these vars will have been initialised to 0, so this will always pass. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 276 | BOOST_TEST(leakedBefore == leakedAfter); |
| 277 | |
| 278 | // Add resonable threshold after and before running valgrind with the ACL clear cache function. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 279 | // TODO Threshold set to 80k until the root cause of the memory leakage is found and fixed. Revert threshold |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 280 | // value to 1024 when fixed. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 281 | BOOST_TEST(static_cast<long>(reachableAfter) - static_cast<long>(reachableBefore) < 81920); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 282 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 283 | // These are needed because VALGRIND_COUNT_LEAKS is a macro that assigns to the parameters |
| 284 | // so they are assigned to, but still considered unused, causing a warning. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 285 | boost::ignore_unused(dubious); |
| 286 | boost::ignore_unused(suppressed); |
| 287 | } |
| 288 | #endif |
| 289 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 290 | // Note: this part of the code is due to be removed when we fully trust the gperftools based results. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 291 | #ifdef WITH_VALGRIND |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 292 | // Run with the following command to get all the amazing output (in the devenv/build folder) :) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 293 | // valgrind --leak-check=full --show-leak-kinds=all --log-file=Valgrind_Memcheck_Leak_Report.txt armnn/test/UnitTests |
| 294 | BOOST_AUTO_TEST_CASE(RuntimeMemoryLeak) |
| 295 | { |
| 296 | // From documentation: |
| 297 | |
| 298 | // This means that no pointer to the block can be found. The block is classified as "lost", |
| 299 | // because the programmer could not possibly have freed it at program exit, since no pointer to it exists. |
| 300 | unsigned long leakedBefore = 0; |
| 301 | unsigned long leakedAfter = 0; |
| 302 | |
| 303 | // A start-pointer or chain of start-pointers to the block is found. Since the block is still pointed at, |
| 304 | // the programmer could, at least in principle, have freed it before program exit. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 305 | // We want to test this in case memory is not freed as early as it could have been. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 306 | unsigned long reachableBefore = 0; |
| 307 | unsigned long reachableAfter = 0; |
| 308 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 309 | // Needed as out params but we don't test them. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 310 | unsigned long dubious = 0; |
| 311 | unsigned long suppressed = 0; |
| 312 | |
| 313 | armnn::NetworkId networkIdentifier1 = 1; |
| 314 | |
| 315 | // ensure that runtime is large enough before checking for memory leaks |
| 316 | // otherwise when loading the network it will automatically reserve memory that won't be released until destruction |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 317 | armnn::IRuntime::CreationOptions options; |
| 318 | armnn::Runtime runtime(options); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 319 | armnn::RuntimeLoadedNetworksReserve(&runtime); |
| 320 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 321 | // Checks for leaks before we load the network and record them so that we can see the delta after unloading. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 322 | VALGRIND_DO_QUICK_LEAK_CHECK; |
| 323 | VALGRIND_COUNT_LEAKS(leakedBefore, dubious, reachableBefore, suppressed); |
| 324 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 325 | // Builds a mock-network and load it into the runtime. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 326 | { |
| 327 | unsigned int inputShape[] = {1, 7, 1, 1}; |
| 328 | armnn::TensorInfo inputTensorInfo(4, inputShape, armnn::DataType::Float32); |
| 329 | |
| 330 | std::unique_ptr<armnn::Network> mockNetwork1 = std::make_unique<armnn::Network>(); |
| 331 | mockNetwork1->AddInputLayer(0, "test layer"); |
| 332 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 333 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 334 | std::vector<armnn::Compute> backends = {armnn::Compute::CpuRef}; |
| 335 | runtime.LoadNetwork(networkIdentifier1, Optimize(*mockNetwork1, backends, runtime.GetDeviceSpec())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | runtime.UnloadNetwork(networkIdentifier1); |
| 339 | |
| 340 | VALGRIND_DO_ADDED_LEAK_CHECK; |
| 341 | VALGRIND_COUNT_LEAKS(leakedAfter, dubious, reachableAfter, suppressed); |
| 342 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 343 | // If we're not running under Valgrind, these vars will have been initialised to 0, so this will always pass. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 344 | BOOST_TEST(leakedBefore == leakedAfter); |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 345 | |
| 346 | #if defined(ARMCOMPUTECL_ENABLED) |
| 347 | // reachableBefore == reachableAfter should hold, but on OpenCL with Android we are still |
| 348 | // not entirely able to control the memory in the OpenCL driver. Testing is showing that |
| 349 | // after this test (which clears all OpenCL memory) we are clearing a little bit more than |
| 350 | // we expect, probably depending on the order in which other tests are run. |
| 351 | BOOST_TEST(reachableBefore - reachableAfter <= 24); |
| 352 | #else |
| 353 | BOOST_TEST(reachableBefore == reachableAfter); |
| 354 | #endif |
| 355 | |
| 356 | BOOST_TEST(reachableBefore >= reachableAfter); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 357 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 358 | // These are needed because VALGRIND_COUNT_LEAKS is a macro that assigns to the parameters |
| 359 | // so they are assigned to, but still considered unused, causing a warning. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 360 | boost::ignore_unused(dubious); |
| 361 | boost::ignore_unused(suppressed); |
| 362 | } |
| 363 | #endif |
| 364 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 365 | #if ARMCOMPUTENEON_ENABLED |
| 366 | BOOST_AUTO_TEST_CASE(RuntimeValidateCpuAccDeviceSupportLayerNoFallback) |
| 367 | { |
| 368 | // build up the structure of the network |
| 369 | armnn::INetworkPtr net(armnn::INetwork::Create()); |
| 370 | |
| 371 | armnn::IConnectableLayer* input = net->AddInputLayer(0); |
| 372 | |
| 373 | armnn::IConnectableLayer* output = net->AddOutputLayer(0); |
| 374 | |
| 375 | input->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 376 | |
| 377 | input->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo({ 1, 1, 4, 4 }, armnn::DataType::Float32)); |
| 378 | |
| 379 | armnn::IRuntime::CreationOptions options; |
| 380 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 381 | |
| 382 | std::vector<armnn::Compute> backends = { armnn::Compute::CpuAcc }; |
| 383 | armnn::IOptimizedNetworkPtr optNet = armnn::Optimize(*net, backends, runtime->GetDeviceSpec()); |
| 384 | BOOST_CHECK(optNet); |
| 385 | |
| 386 | // Load it into the runtime. It should success. |
| 387 | armnn::NetworkId netId; |
| 388 | BOOST_TEST(runtime->LoadNetwork(netId, std::move(optNet)) == armnn::Status::Success); |
| 389 | } |
| 390 | #endif // ARMCOMPUTENEON_ENABLED |
| 391 | |
| 392 | #if ARMCOMPUTECL_ENABLED |
| 393 | BOOST_AUTO_TEST_CASE(RuntimeValidateGpuDeviceSupportLayerNoFallback) |
| 394 | { |
| 395 | // build up the structure of the network |
| 396 | armnn::INetworkPtr net(armnn::INetwork::Create()); |
| 397 | |
| 398 | armnn::IConnectableLayer* input = net->AddInputLayer(0); |
| 399 | |
| 400 | armnn::IConnectableLayer* output = net->AddOutputLayer(0); |
| 401 | |
| 402 | input->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 403 | |
| 404 | input->GetOutputSlot(0).SetTensorInfo(armnn::TensorInfo({ 1, 1, 4, 4 }, armnn::DataType::Float32)); |
| 405 | |
| 406 | armnn::IRuntime::CreationOptions options; |
| 407 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 408 | |
| 409 | std::vector<armnn::Compute> backends = { armnn::Compute::GpuAcc }; |
| 410 | armnn::IOptimizedNetworkPtr optNet = armnn::Optimize(*net, backends, runtime->GetDeviceSpec()); |
| 411 | BOOST_CHECK(optNet); |
| 412 | |
| 413 | // Load it into the runtime. It should success. |
| 414 | armnn::NetworkId netId; |
| 415 | BOOST_TEST(runtime->LoadNetwork(netId, std::move(optNet)) == armnn::Status::Success); |
| 416 | } |
| 417 | #endif // ARMCOMPUTECL_ENABLED |
| 418 | |
| 419 | BOOST_AUTO_TEST_CASE(RuntimeCpuRef) |
| 420 | { |
| 421 | using namespace armnn; |
| 422 | |
| 423 | // Create runtime in which test will run |
| 424 | armnn::IRuntime::CreationOptions options; |
| 425 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 426 | |
| 427 | // build up the structure of the network |
| 428 | INetworkPtr net(INetwork::Create()); |
| 429 | |
| 430 | IConnectableLayer* input = net->AddInputLayer(0); |
| 431 | |
| 432 | // This layer configuration isn't supported by CpuAcc, should be fall back to CpuRef. |
| 433 | NormalizationDescriptor descriptor; |
| 434 | IConnectableLayer* normalize = net->AddNormalizationLayer(descriptor); |
| 435 | |
| 436 | IConnectableLayer* output = net->AddOutputLayer(0); |
| 437 | |
| 438 | input->GetOutputSlot(0).Connect(normalize->GetInputSlot(0)); |
| 439 | normalize->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 440 | |
| 441 | input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 442 | normalize->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 443 | |
| 444 | // optimize the network |
| 445 | std::vector<armnn::Compute> backends = { armnn::Compute::CpuRef }; |
| 446 | IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime->GetDeviceSpec()); |
| 447 | |
| 448 | // Load it into the runtime. It should success. |
| 449 | armnn::NetworkId netId; |
| 450 | BOOST_TEST(runtime->LoadNetwork(netId, std::move(optNet)) == Status::Success); |
| 451 | } |
| 452 | |
| 453 | BOOST_AUTO_TEST_CASE(RuntimeFallbackToCpuRef) |
| 454 | { |
| 455 | using namespace armnn; |
| 456 | |
| 457 | // Create runtime in which test will run |
| 458 | armnn::IRuntime::CreationOptions options; |
| 459 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 460 | |
| 461 | // build up the structure of the network |
| 462 | INetworkPtr net(INetwork::Create()); |
| 463 | |
| 464 | IConnectableLayer* input = net->AddInputLayer(0); |
| 465 | |
| 466 | // This layer configuration isn't supported by CpuAcc, should be fall back to CpuRef. |
| 467 | NormalizationDescriptor descriptor; |
| 468 | IConnectableLayer* normalize = net->AddNormalizationLayer(descriptor); |
| 469 | |
| 470 | IConnectableLayer* output = net->AddOutputLayer(0); |
| 471 | |
| 472 | input->GetOutputSlot(0).Connect(normalize->GetInputSlot(0)); |
| 473 | normalize->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 474 | |
| 475 | input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 476 | normalize->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 477 | |
| 478 | // Allow fallback to CpuRef. |
| 479 | std::vector<armnn::Compute> backends = { armnn::Compute::CpuAcc, armnn::Compute::CpuRef }; |
| 480 | // optimize the network |
| 481 | IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime->GetDeviceSpec()); |
| 482 | |
| 483 | // Load it into the runtime. It should succeed. |
| 484 | armnn::NetworkId netId; |
| 485 | BOOST_TEST(runtime->LoadNetwork(netId, std::move(optNet)) == Status::Success); |
| 486 | } |
| 487 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 488 | BOOST_AUTO_TEST_SUITE_END() |