From 3e364ca52901475335c36a9cbb3a8360287a467c Mon Sep 17 00:00:00 2001 From: Francis Murtagh Date: Thu, 12 Aug 2021 11:55:21 +0100 Subject: IVGCVSW-6077 Add Custom Allocator tests * Add test for invalid backend and nullptr * Small refactor * Throw exception on nullptr allocator instead of assert Signed-off-by: Francis Murtagh Change-Id: I94ce4f61d7cb3123831f1acd98165ae14c40033a --- src/armnn/Runtime.cpp | 7 +- src/backends/cl/test/ClCustomAllocatorTests.cpp | 106 +++++++++++++++++++----- 2 files changed, 91 insertions(+), 22 deletions(-) diff --git a/src/armnn/Runtime.cpp b/src/armnn/Runtime.cpp index 5a52888639..824a2b077c 100644 --- a/src/armnn/Runtime.cpp +++ b/src/armnn/Runtime.cpp @@ -286,6 +286,11 @@ RuntimeImpl::RuntimeImpl(const IRuntime::CreationOptions& options) ARMNN_ASSERT(backend.get() != nullptr); auto customAllocatorMapIterator = options.m_CustomAllocatorMap.find(id); + if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end() && + customAllocatorMapIterator->second == nullptr) + { + throw armnn::Exception("Allocator associated with id " + id.Get() + " is null"); + } // If the runtime is created in protected mode only add backends that support this mode if (options.m_ProtectedMode) @@ -307,8 +312,6 @@ RuntimeImpl::RuntimeImpl(const IRuntime::CreationOptions& options) if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end()) { std::string err; - // Check we have actually been given an allocator. - ARMNN_ASSERT(customAllocatorMapIterator->second != nullptr); if (customAllocatorMapIterator->second->GetMemorySourceType() == armnn::MemorySource::DmaBufProtected) { diff --git a/src/backends/cl/test/ClCustomAllocatorTests.cpp b/src/backends/cl/test/ClCustomAllocatorTests.cpp index 4d1a0e1cfb..86e1182703 100644 --- a/src/backends/cl/test/ClCustomAllocatorTests.cpp +++ b/src/backends/cl/test/ClCustomAllocatorTests.cpp @@ -20,7 +20,6 @@ #include #include - /** Sample implementation of ICustomAllocator for use with the ClBackend. * Note: any memory allocated must be host accessible with write access to allow for weights and biases * to be passed in. Read access is not required.. */ @@ -58,20 +57,9 @@ public: } }; -TEST_SUITE("ClCustomAllocatorTests") -{ - -// This is a copy of the SimpleSample app modified to use a custom -// allocator for the clbackend. It creates a FullyConnected network with a single layer -// taking a single number as an input -TEST_CASE("ClCustomAllocatorTest") +armnn::INetworkPtr CreateTestNetwork(armnn::TensorInfo& inputTensorInfo) { using namespace armnn; - - float number = 3; - - // Construct ArmNN network - armnn::NetworkId networkIdentifier; INetworkPtr myNetwork = INetwork::Create(); armnn::FullyConnectedDescriptor fullyConnectedDesc; @@ -91,6 +79,34 @@ TEST_CASE("ClCustomAllocatorTest") InputLayer->GetOutputSlot(0).Connect(fullyConnected->GetInputSlot(0)); fullyConnected->GetOutputSlot(0).Connect(OutputLayer->GetInputSlot(0)); + //Set the tensors in the network. + + InputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); + + TensorInfo outputTensorInfo(TensorShape({1, 1}), DataType::Float32); + fullyConnected->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); + + return myNetwork; +} + +TEST_SUITE("ClCustomAllocatorTests") +{ + +// This is a copy of the SimpleSample app modified to use a custom +// allocator for the clbackend. It creates a FullyConnected network with a single layer +// taking a single number as an input +TEST_CASE("ClCustomAllocatorTest") +{ + using namespace armnn; + + float number = 3; + + // Construct ArmNN network + armnn::NetworkId networkIdentifier; + + TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32); + + INetworkPtr myNetwork = CreateTestNetwork(inputTensorInfo); // Create ArmNN runtime IRuntime::CreationOptions options; // default options @@ -98,13 +114,6 @@ TEST_CASE("ClCustomAllocatorTest") options.m_CustomAllocatorMap = {{"GpuAcc", std::move(customAllocator)}}; IRuntimePtr run = IRuntime::Create(options); - //Set the tensors in the network. - TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32); - InputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); - - TensorInfo outputTensorInfo(TensorShape({1, 1}), DataType::Float32); - fullyConnected->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); - // Optimise ArmNN network OptimizerOptions optOptions; optOptions.m_ImportEnabled = true; @@ -157,4 +166,61 @@ TEST_CASE("ClCustomAllocatorTest") backendRegistry.DeregisterAllocator(ClBackend::GetIdStatic()); } +TEST_CASE("ClCustomAllocatorCpuAccNegativeTest") +{ + using namespace armnn; + + // Create ArmNN runtime + IRuntime::CreationOptions options; // default options + auto customAllocator = std::make_shared(); + options.m_CustomAllocatorMap = {{"CpuAcc", std::move(customAllocator)}}; + IRuntimePtr run = IRuntime::Create(options); + + TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32); + INetworkPtr myNetwork = CreateTestNetwork(inputTensorInfo); + + // Optimise ArmNN network + OptimizerOptions optOptions; + optOptions.m_ImportEnabled = true; + IOptimizedNetworkPtr optNet(nullptr, nullptr); + std::vector errMessages; + + try + { + optNet = Optimize(*myNetwork, {"CpuAcc"}, run->GetDeviceSpec(), optOptions, errMessages); + FAIL("Should have thrown an exception as GetAvailablePreferredBackends() should be empty in Optimize()."); + } + catch (const armnn::InvalidArgumentException& e) + { + // Different exceptions are thrown on different backends + } + CHECK(errMessages.size() > 0); + + auto& backendRegistry = armnn::BackendRegistryInstance(); + backendRegistry.DeregisterAllocator(ClBackend::GetIdStatic()); +} + +TEST_CASE("ClCustomAllocatorGpuAccNullptrTest") +{ + using namespace armnn; + + // Create ArmNN runtime + IRuntime::CreationOptions options; // default options + auto customAllocator = std::make_shared(); + options.m_CustomAllocatorMap = {{"GpuAcc", nullptr}}; + + try + { + IRuntimePtr run = IRuntime::Create(options); + FAIL("Should have thrown an exception in RuntimeImpl::RuntimeImpl()."); + } + catch (const armnn::Exception& e) + { + // Caught successfully + } + + auto& backendRegistry = armnn::BackendRegistryInstance(); + backendRegistry.DeregisterAllocator(ClBackend::GetIdStatic()); +} + } // test suite ClCustomAllocatorTests \ No newline at end of file -- cgit v1.2.1