aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancis Murtagh <francis.murtagh@arm.com>2021-08-12 11:55:21 +0100
committerMatthew Sloyan <matthew.sloyan@arm.com>2021-08-12 17:52:08 +0100
commit3e364ca52901475335c36a9cbb3a8360287a467c (patch)
tree21cc7d8a23e9fd0df6bc226293a9d5f0316d2a5b
parent80bd5511a3c6633fcad2c2754cd788c7b414f3e1 (diff)
downloadarmnn-3e364ca52901475335c36a9cbb3a8360287a467c.tar.gz
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 <francis.murtagh@arm.com> Change-Id: I94ce4f61d7cb3123831f1acd98165ae14c40033a
-rw-r--r--src/armnn/Runtime.cpp7
-rw-r--r--src/backends/cl/test/ClCustomAllocatorTests.cpp106
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 <CL/cl_ext.h>
#include <arm_compute/runtime/CL/CLScheduler.h>
-
/** 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<SampleClBackendCustomAllocator>();
+ 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<std::string> 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<SampleClBackendCustomAllocator>();
+ 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