ArmNN
 22.05.01
TensorHandleStrategyTest.cpp File Reference

Go to the source code of this file.

Functions

 TEST_SUITE ("TensorHandle")
 

Function Documentation

◆ TEST_SUITE()

TEST_SUITE ( "TensorHandle"  )

Definition at line 274 of file TensorHandleStrategyTest.cpp.

References Graph::AddCompatibilityLayers(), Graph::AddLayer(), OutputSlot::Connect(), armnn::CopyToTarget, armnn::DirectCompatibility, armnn::ExportToTarget, Graph::ForEachLayer(), OutputSlot::GetEdgeStrategyForConnection(), Layer::GetInputSlot(), TensorHandleFactoryRegistry::GetMatchingImportFactoryId(), Layer::GetOutputSlot(), OutputSlot::GetTensorHandleFactoryId(), Layer::GetType(), armnn::MemCopy, armnn::MemImport, TensorHandleFactoryRegistry::RegisterCopyAndImportFactoryPair(), armnn::SelectTensorHandleStrategy(), Layer::SetBackendId(), and Graph::TopologicalSort().

275 {
276 TEST_CASE("RegisterFactories")
277 {
278  TestBackendA backendA;
279  TestBackendB backendB;
280 
281  CHECK(backendA.GetHandleFactoryPreferences()[0] == "TestHandleFactoryA1");
282  CHECK(backendA.GetHandleFactoryPreferences()[1] == "TestHandleFactoryA2");
283  CHECK(backendA.GetHandleFactoryPreferences()[2] == "TestHandleFactoryB1");
284  CHECK(backendA.GetHandleFactoryPreferences()[3] == "TestHandleFactoryD1");
285 
287  backendA.RegisterTensorHandleFactories(registry);
288  backendB.RegisterTensorHandleFactories(registry);
289 
290  CHECK((registry.GetFactory("Non-existing Backend") == nullptr));
291  CHECK((registry.GetFactory("TestHandleFactoryA1") != nullptr));
292  CHECK((registry.GetFactory("TestHandleFactoryA2") != nullptr));
293  CHECK((registry.GetFactory("TestHandleFactoryB1") != nullptr));
294 }
295 
296 TEST_CASE("TensorHandleSelectionStrategy")
297 {
298  auto backendA = std::make_unique<TestBackendA>();
299  auto backendB = std::make_unique<TestBackendB>();
300  auto backendC = std::make_unique<TestBackendC>();
301  auto backendD = std::make_unique<TestBackendD>();
302 
304  backendA->RegisterTensorHandleFactories(registry);
305  backendB->RegisterTensorHandleFactories(registry);
306  backendC->RegisterTensorHandleFactories(registry);
307  backendD->RegisterTensorHandleFactories(registry);
308 
309  BackendsMap backends;
310  backends["BackendA"] = std::move(backendA);
311  backends["BackendB"] = std::move(backendB);
312  backends["BackendC"] = std::move(backendC);
313  backends["BackendD"] = std::move(backendD);
314 
315  armnn::Graph graph;
316 
317  armnn::InputLayer* const inputLayer = graph.AddLayer<armnn::InputLayer>(0, "input");
318  inputLayer->SetBackendId("BackendA");
319 
321  armnn::SoftmaxLayer* const softmaxLayer1 = graph.AddLayer<armnn::SoftmaxLayer>(smDesc, "softmax1");
322  softmaxLayer1->SetBackendId("BackendA");
323 
324  armnn::SoftmaxLayer* const softmaxLayer2 = graph.AddLayer<armnn::SoftmaxLayer>(smDesc, "softmax2");
325  softmaxLayer2->SetBackendId("BackendB");
326 
327  armnn::SoftmaxLayer* const softmaxLayer3 = graph.AddLayer<armnn::SoftmaxLayer>(smDesc, "softmax3");
328  softmaxLayer3->SetBackendId("BackendC");
329 
330  armnn::SoftmaxLayer* const softmaxLayer4 = graph.AddLayer<armnn::SoftmaxLayer>(smDesc, "softmax4");
331  softmaxLayer4->SetBackendId("BackendD");
332 
333  armnn::OutputLayer* const outputLayer = graph.AddLayer<armnn::OutputLayer>(0, "output");
334  outputLayer->SetBackendId("BackendA");
335 
336  inputLayer->GetOutputSlot(0).Connect(softmaxLayer1->GetInputSlot(0));
337  softmaxLayer1->GetOutputSlot(0).Connect(softmaxLayer2->GetInputSlot(0));
338  softmaxLayer2->GetOutputSlot(0).Connect(softmaxLayer3->GetInputSlot(0));
339  softmaxLayer3->GetOutputSlot(0).Connect(softmaxLayer4->GetInputSlot(0));
340  softmaxLayer4->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
341 
342  graph.TopologicalSort();
343 
344  std::vector<std::string> errors;
345  auto result = SelectTensorHandleStrategy(graph, backends, registry, true, errors);
346 
347  CHECK(result.m_Error == false);
348  CHECK(result.m_Warning == false);
349 
350  OutputSlot& inputLayerOut = inputLayer->GetOutputSlot(0);
351  OutputSlot& softmaxLayer1Out = softmaxLayer1->GetOutputSlot(0);
352  OutputSlot& softmaxLayer2Out = softmaxLayer2->GetOutputSlot(0);
353  OutputSlot& softmaxLayer3Out = softmaxLayer3->GetOutputSlot(0);
354  OutputSlot& softmaxLayer4Out = softmaxLayer4->GetOutputSlot(0);
355 
356  // Check that the correct factory was selected
357  CHECK(inputLayerOut.GetTensorHandleFactoryId() == "TestHandleFactoryD1");
358  CHECK(softmaxLayer1Out.GetTensorHandleFactoryId() == "TestHandleFactoryB1");
359  CHECK(softmaxLayer2Out.GetTensorHandleFactoryId() == "TestHandleFactoryB1");
360  CHECK(softmaxLayer3Out.GetTensorHandleFactoryId() == "TestHandleFactoryC1");
361  CHECK(softmaxLayer4Out.GetTensorHandleFactoryId() == "TestHandleFactoryD1");
362 
363  // Check that the correct strategy was selected
364  CHECK((inputLayerOut.GetEdgeStrategyForConnection(0) == EdgeStrategy::DirectCompatibility));
365  CHECK((softmaxLayer1Out.GetEdgeStrategyForConnection(0) == EdgeStrategy::DirectCompatibility));
366  CHECK((softmaxLayer2Out.GetEdgeStrategyForConnection(0) == EdgeStrategy::CopyToTarget));
367  CHECK((softmaxLayer3Out.GetEdgeStrategyForConnection(0) == EdgeStrategy::ExportToTarget));
368  CHECK((softmaxLayer4Out.GetEdgeStrategyForConnection(0) == EdgeStrategy::DirectCompatibility));
369 
370  graph.AddCompatibilityLayers(backends, registry);
371 
372  // Test for copy layers
373  int copyCount= 0;
374  graph.ForEachLayer([&copyCount](Layer* layer)
375  {
376  if (layer->GetType() == LayerType::MemCopy)
377  {
378  copyCount++;
379  }
380  });
381  CHECK(copyCount == 1);
382 
383  // Test for import layers
384  int importCount= 0;
385  graph.ForEachLayer([&importCount](Layer *layer)
386  {
387  if (layer->GetType() == LayerType::MemImport)
388  {
389  importCount++;
390  }
391  });
392  CHECK(importCount == 1);
393 }
394 
395 TEST_CASE("RegisterCopyAndImportFactoryPairTest")
396 {
398  ITensorHandleFactory::FactoryId copyId = "CopyFactoryId";
399  ITensorHandleFactory::FactoryId importId = "ImportFactoryId";
400  registry.RegisterCopyAndImportFactoryPair(copyId, importId);
401 
402  // Get mathing import factory id correctly
403  CHECK((registry.GetMatchingImportFactoryId(copyId) == importId));
404 
405  // Return empty id when Invalid Id is given
406  CHECK((registry.GetMatchingImportFactoryId("InvalidFactoryId") == ""));
407 }
408 
409 }
LayerT * AddLayer(Args &&... args)
Adds a new layer, of type LayerType, to the graph constructed with the arguments passed.
Definition: Graph.hpp:425
int Connect(InputSlot &destination)
Definition: Layer.cpp:112
EdgeStrategy GetEdgeStrategyForConnection(unsigned int connectionIdx) const
Definition: Layer.cpp:215
void SetBackendId(const BackendId &id)
Definition: Layer.hpp:276
void RegisterCopyAndImportFactoryPair(ITensorHandleFactory::FactoryId copyFactoryId, ITensorHandleFactory::FactoryId importFactoryId)
Register a pair of TensorHandleFactory Id for Memory Copy and TensorHandleFactory Id for Memory Impor...
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:322
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: OutputLayer.hpp:13
void ForEachLayer(Func func) const
Definition: Graph.hpp:40
This layer represents a softmax operation.
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition: Layer.hpp:271
ITensorHandleFactory::FactoryId GetMatchingImportFactoryId(ITensorHandleFactory::FactoryId copyFactoryId)
Get a matching TensorHandleFatory Id for Memory Import given TensorHandleFactory Id for Memory Copy...
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: InputLayer.hpp:13
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:324
OptimizationResult SelectTensorHandleStrategy(Graph &optGraph, BackendsMap &backends, TensorHandleFactoryRegistry &registry, bool importEnabled, Optional< std::vector< std::string > &> errMessages)
Definition: Network.cpp:1601
ITensorHandleFactory::FactoryId GetTensorHandleFactoryId() const
Definition: Layer.cpp:205
Graph & TopologicalSort()
Sorts layers in topological order and return this.
Definition: Graph.hpp:184
A SoftmaxDescriptor for the SoftmaxLayer.
void AddCompatibilityLayers(std::map< BackendId, std::unique_ptr< class IBackendInternal >> &backends, TensorHandleFactoryRegistry &registry)
Modifies the graph in-place, removing edges connecting layers using different compute devices...
Definition: Graph.cpp:303
std::map< BackendId, std::unique_ptr< class IBackendInternal > > BackendsMap
Definition: Network.hpp:294