ArmNN
 22.08
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 273 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().

274 {
275 TEST_CASE("RegisterFactories")
276 {
277  TestBackendA backendA;
278  TestBackendB backendB;
279 
280  CHECK(backendA.GetHandleFactoryPreferences()[0] == "TestHandleFactoryA1");
281  CHECK(backendA.GetHandleFactoryPreferences()[1] == "TestHandleFactoryA2");
282  CHECK(backendA.GetHandleFactoryPreferences()[2] == "TestHandleFactoryB1");
283  CHECK(backendA.GetHandleFactoryPreferences()[3] == "TestHandleFactoryD1");
284 
286  backendA.RegisterTensorHandleFactories(registry);
287  backendB.RegisterTensorHandleFactories(registry);
288 
289  CHECK((registry.GetFactory("Non-existing Backend") == nullptr));
290  CHECK((registry.GetFactory("TestHandleFactoryA1") != nullptr));
291  CHECK((registry.GetFactory("TestHandleFactoryA2") != nullptr));
292  CHECK((registry.GetFactory("TestHandleFactoryB1") != nullptr));
293 }
294 
295 TEST_CASE("TensorHandleSelectionStrategy")
296 {
297  auto backendA = std::make_unique<TestBackendA>();
298  auto backendB = std::make_unique<TestBackendB>();
299  auto backendC = std::make_unique<TestBackendC>();
300  auto backendD = std::make_unique<TestBackendD>();
301 
303  backendA->RegisterTensorHandleFactories(registry);
304  backendB->RegisterTensorHandleFactories(registry);
305  backendC->RegisterTensorHandleFactories(registry);
306  backendD->RegisterTensorHandleFactories(registry);
307 
308  BackendsMap backends;
309  backends["BackendA"] = std::move(backendA);
310  backends["BackendB"] = std::move(backendB);
311  backends["BackendC"] = std::move(backendC);
312  backends["BackendD"] = std::move(backendD);
313 
314  armnn::Graph graph;
315 
316  armnn::InputLayer* const inputLayer = graph.AddLayer<armnn::InputLayer>(0, "input");
317  inputLayer->SetBackendId("BackendA");
318 
320  armnn::SoftmaxLayer* const softmaxLayer1 = graph.AddLayer<armnn::SoftmaxLayer>(smDesc, "softmax1");
321  softmaxLayer1->SetBackendId("BackendA");
322 
323  armnn::SoftmaxLayer* const softmaxLayer2 = graph.AddLayer<armnn::SoftmaxLayer>(smDesc, "softmax2");
324  softmaxLayer2->SetBackendId("BackendB");
325 
326  armnn::SoftmaxLayer* const softmaxLayer3 = graph.AddLayer<armnn::SoftmaxLayer>(smDesc, "softmax3");
327  softmaxLayer3->SetBackendId("BackendC");
328 
329  armnn::SoftmaxLayer* const softmaxLayer4 = graph.AddLayer<armnn::SoftmaxLayer>(smDesc, "softmax4");
330  softmaxLayer4->SetBackendId("BackendD");
331 
332  armnn::OutputLayer* const outputLayer = graph.AddLayer<armnn::OutputLayer>(0, "output");
333  outputLayer->SetBackendId("BackendA");
334 
335  inputLayer->GetOutputSlot(0).Connect(softmaxLayer1->GetInputSlot(0));
336  softmaxLayer1->GetOutputSlot(0).Connect(softmaxLayer2->GetInputSlot(0));
337  softmaxLayer2->GetOutputSlot(0).Connect(softmaxLayer3->GetInputSlot(0));
338  softmaxLayer3->GetOutputSlot(0).Connect(softmaxLayer4->GetInputSlot(0));
339  softmaxLayer4->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
340 
341  graph.TopologicalSort();
342 
343  std::vector<std::string> errors;
344  auto result = SelectTensorHandleStrategy(graph, backends, registry, true, true, errors);
345 
346  CHECK(result.m_Error == false);
347  CHECK(result.m_Warning == false);
348 
349  OutputSlot& inputLayerOut = inputLayer->GetOutputSlot(0);
350  OutputSlot& softmaxLayer1Out = softmaxLayer1->GetOutputSlot(0);
351  OutputSlot& softmaxLayer2Out = softmaxLayer2->GetOutputSlot(0);
352  OutputSlot& softmaxLayer3Out = softmaxLayer3->GetOutputSlot(0);
353  OutputSlot& softmaxLayer4Out = softmaxLayer4->GetOutputSlot(0);
354 
355  // Check that the correct factory was selected
356  CHECK(inputLayerOut.GetTensorHandleFactoryId() == "TestHandleFactoryD1");
357  CHECK(softmaxLayer1Out.GetTensorHandleFactoryId() == "TestHandleFactoryB1");
358  CHECK(softmaxLayer2Out.GetTensorHandleFactoryId() == "TestHandleFactoryB1");
359  CHECK(softmaxLayer3Out.GetTensorHandleFactoryId() == "TestHandleFactoryC1");
360  CHECK(softmaxLayer4Out.GetTensorHandleFactoryId() == "TestHandleFactoryD1");
361 
362  // Check that the correct strategy was selected
363  CHECK((inputLayerOut.GetEdgeStrategyForConnection(0) == EdgeStrategy::DirectCompatibility));
364  CHECK((softmaxLayer1Out.GetEdgeStrategyForConnection(0) == EdgeStrategy::DirectCompatibility));
365  CHECK((softmaxLayer2Out.GetEdgeStrategyForConnection(0) == EdgeStrategy::CopyToTarget));
366  CHECK((softmaxLayer3Out.GetEdgeStrategyForConnection(0) == EdgeStrategy::ExportToTarget));
367  CHECK((softmaxLayer4Out.GetEdgeStrategyForConnection(0) == EdgeStrategy::DirectCompatibility));
368 
369  graph.AddCompatibilityLayers(backends, registry);
370 
371  // Test for copy layers
372  int copyCount= 0;
373  graph.ForEachLayer([&copyCount](Layer* layer)
374  {
375  if (layer->GetType() == LayerType::MemCopy)
376  {
377  copyCount++;
378  }
379  });
380  CHECK(copyCount == 1);
381 
382  // Test for import layers
383  int importCount= 0;
384  graph.ForEachLayer([&importCount](Layer *layer)
385  {
386  if (layer->GetType() == LayerType::MemImport)
387  {
388  importCount++;
389  }
390  });
391  CHECK(importCount == 1);
392 }
393 
394 TEST_CASE("RegisterCopyAndImportFactoryPairTest")
395 {
397  ITensorHandleFactory::FactoryId copyId = "CopyFactoryId";
398  ITensorHandleFactory::FactoryId importId = "ImportFactoryId";
399  registry.RegisterCopyAndImportFactoryPair(copyId, importId);
400 
401  // Get mathing import factory id correctly
402  CHECK((registry.GetMatchingImportFactoryId(copyId) == importId));
403 
404  // Return empty id when Invalid Id is given
405  CHECK((registry.GetMatchingImportFactoryId("InvalidFactoryId") == ""));
406 }
407 
408 }
LayerT * AddLayer(Args &&... args)
Adds a new layer, of type LayerType, to the graph constructed with the arguments passed.
Definition: Graph.hpp:456
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:278
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:324
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
OptimizationResult SelectTensorHandleStrategy(Graph &optGraph, BackendsMap &backends, TensorHandleFactoryRegistry &registry, bool importEnabled, bool exportEnabled, Optional< std::vector< std::string > &> errMessages)
Definition: Network.cpp:1605
This layer represents a softmax operation.
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition: Layer.hpp:273
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:326
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:293