ArmNN
 20.11
LoadedNetwork Class Reference

#include <LoadedNetwork.hpp>

Public Types

using WorkloadQueue = std::vector< std::unique_ptr< IWorkload > >
 

Public Member Functions

 ~LoadedNetwork ()
 
TensorInfo GetInputTensorInfo (LayerBindingId layerId) const
 
TensorInfo GetOutputTensorInfo (LayerBindingId layerId) const
 
Status EnqueueWorkload (const InputTensors &inputTensors, const OutputTensors &outputTensors)
 
const std::shared_ptr< Profiler > & GetProfiler () const
 
void FreeWorkingMemory ()
 
void RegisterDebugCallback (const DebugCallbackFunction &func)
 
void SendNetworkStructure ()
 
profiling::ProfilingGuid GetNetworkGuid ()
 

Static Public Member Functions

static std::unique_ptr< LoadedNetworkMakeLoadedNetwork (std::unique_ptr< OptimizedNetwork > net, std::string &errorMessage, const INetworkProperties &networkProperties, profiling::ProfilingService &profilingService)
 

Detailed Description

Definition at line 34 of file LoadedNetwork.hpp.

Member Typedef Documentation

◆ WorkloadQueue

using WorkloadQueue = std::vector< std::unique_ptr<IWorkload> >

Definition at line 37 of file LoadedNetwork.hpp.

Constructor & Destructor Documentation

◆ ~LoadedNetwork()

~LoadedNetwork ( )
inline

Definition at line 38 of file LoadedNetwork.hpp.

References armnn::GetInputTensorInfo().

Member Function Documentation

◆ EnqueueWorkload()

Status EnqueueWorkload ( const InputTensors inputTensors,
const OutputTensors outputTensors 
)

Definition at line 465 of file LoadedNetwork.cpp.

References ARMNN_ASSERT_MSG, ARMNN_LOG, LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS, LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS, ARMNN_SCOPED_HEAP_PROFILING, ARMNN_SCOPED_PROFILING_EVENT, armnn::CheckFlag(), LabelsAndEventClasses::EXECUTION_OF_GUID, armnn::Failure, ITensorHandle::GetImportFlags(), Graph::GetInputLayers(), Layer::GetInputSlots(), Graph::GetNumInputs(), Layer::GetNumInputSlots(), Graph::GetNumLayers(), Graph::GetNumOutputs(), Layer::GetNumOutputSlots(), Layer::GetOutputHandler(), Graph::GetOutputLayers(), TimelineUtilityMethods::GetTimelineUtils(), Layer::GetType(), armnn::IgnoreUnused(), ITensorHandle::Import(), LabelsAndEventClasses::INFERENCE_GUID, armnn::info, armnn::Input, QueueDescriptor::m_Inputs, WorkloadInfo::m_InputTensorInfos, QueueDescriptor::m_Outputs, WorkloadInfo::m_OutputTensorInfos, armnn::Malloc, ITensorHandle::Map(), armnn::Output, armnn::Success, armnn::Undefined, ITensorHandle::Unmap(), and armnn::warning.

Referenced by Runtime::EnqueueWorkload().

467 {
468  const Graph& graph = m_OptimizedNetwork->GetGraph();
469 
470  // Walk graph to determine the order of execution.
471  if (graph.GetNumLayers() < 2)
472  {
473  ARMNN_LOG(warning) << "IRuntime::EnqueueWorkload()::Less than two nodes in graph";
474  return Status::Failure;
475  }
476 
477  // Data that must be kept alive for the entire execution of the workload.
478  WorkloadData workloadData(inputTensors, outputTensors);
479 
480  if (graph.GetNumInputs() != inputTensors.size())
481  {
482  throw InvalidArgumentException("Number of inputs provided does not match network.");
483  }
484 
485  // For each input to the network, call EnqueueInput with the data passed by the user.
486  {
488  m_InputQueue.clear();
489  m_InputQueue.reserve(graph.GetNumInputs());
490  for (const BindableLayer* inputLayer : graph.GetInputLayers())
491  {
492  const TensorPin& pin = workloadData.GetInputTensorPin(inputLayer->GetBindingId());
493  EnqueueInput(*inputLayer, pin.GetTensorHandle(), pin.GetTensorInfo());
494  }
495  }
496 
497  // For each output to the network, call EnqueueOutput with the data passed by the user.
498  {
500  m_OutputQueue.clear();
501  m_OutputQueue.reserve(graph.GetNumOutputs());
502  for (const BindableLayer* outputLayer : graph.GetOutputLayers())
503  {
504  const TensorPin& pin = workloadData.GetOutputTensorPin(outputLayer->GetBindingId());
505  EnqueueOutput(*outputLayer, pin.GetTensorHandle(), pin.GetTensorInfo());
506  }
507  }
508 
509  std::unique_ptr<TimelineUtilityMethods> timelineUtils =
510  TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService);
511  ProfilingGuid inferenceGuid = m_ProfilingService.GetNextGuid();
512  if (timelineUtils)
513  {
514  // Add inference timeline trace if profiling is enabled.
515  ProfilingGuid networkGuid = m_OptimizedNetwork->GetGuid();
516  timelineUtils->CreateTypedEntity(inferenceGuid, LabelsAndEventClasses::INFERENCE_GUID);
517  timelineUtils->CreateRelationship(ProfilingRelationshipType::RetentionLink,
518  networkGuid,
519  inferenceGuid,
521  timelineUtils->RecordEvent(inferenceGuid, LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS);
522  }
523 
524  bool executionSucceeded = true;
525 
526  {
527  if (m_ProfilingService.IsProfilingEnabled())
528  {
529  m_ProfilingService.IncrementCounterValue(armnn::profiling::INFERENCES_RUN);
530  }
532  ARMNN_SCOPED_HEAP_PROFILING("Executing");
533  executionSucceeded = Execute(timelineUtils, inferenceGuid);
534  }
535 
536  if (timelineUtils)
537  {
538  // Add end of life of the inference timeline if profiling is enabled.
539  timelineUtils->RecordEvent(inferenceGuid, LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS);
540  timelineUtils->Commit();
541  }
542  return executionSucceeded ? Status::Success : Status::Failure;
543 }
static ARMNN_DLLEXPORT ProfilingStaticGuid INFERENCE_GUID
static std::unique_ptr< TimelineUtilityMethods > GetTimelineUtils(ProfilingService &profilingService)
#define ARMNN_LOG(severity)
Definition: Logging.hpp:163
uint32_t IncrementCounterValue(uint16_t counterUid) override
static ARMNN_DLLEXPORT ProfilingStaticGuid ARMNN_PROFILING_EOL_EVENT_CLASS
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
Definition: Profiling.hpp:175
static ARMNN_DLLEXPORT ProfilingStaticGuid ARMNN_PROFILING_SOL_EVENT_CLASS
#define ARMNN_SCOPED_HEAP_PROFILING(TAG)
static ARMNN_DLLEXPORT ProfilingStaticGuid EXECUTION_OF_GUID
bool IsProfilingEnabled() const override
static ProfilingDynamicGuid GetNextGuid()

◆ FreeWorkingMemory()

void FreeWorkingMemory ( )

Definition at line 720 of file LoadedNetwork.cpp.

References ARMNN_LOG, and armnn::error.

Referenced by Runtime::EnqueueWorkload().

721 {
722  std::lock_guard<std::mutex> lockGuard(m_WorkingMemMutex);
723  if (!m_IsWorkingMemAllocated)
724  {
725  return;
726  }
727  // Informs the memory managers to release memory in it's respective memory group
728  for (auto&& workloadFactory : m_WorkloadFactories)
729  {
730  IBackendInternal::IMemoryManagerSharedPtr memoryManager = workloadFactory.second.second;
731  if (memoryManager)
732  {
733  memoryManager->Release();
734  }
735  }
736  m_TensorHandleFactoryRegistry.ReleaseMemory();
737  m_IsWorkingMemAllocated = false;
738 }
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
void ReleaseMemory()
Release memory required for inference.

◆ GetInputTensorInfo()

TensorInfo GetInputTensorInfo ( LayerBindingId  layerId) const

Definition at line 316 of file LoadedNetwork.cpp.

References ARMNN_ASSERT_MSG.

Referenced by Runtime::GetInputTensorInfo().

317 {
318  for (auto&& inputLayer : m_OptimizedNetwork->GetGraph().GetInputLayers())
319  {
320  ARMNN_ASSERT_MSG(inputLayer->GetNumOutputSlots() == 1, "Input layer should have exactly 1 output slot");
321  if (inputLayer->GetBindingId() == layerId)
322  {
323  return inputLayer->GetOutputSlot(0).GetTensorInfo();
324  }
325  }
326 
327  throw InvalidArgumentException(fmt::format("No input layer is associated with id {}", layerId));
328 }
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15

◆ GetNetworkGuid()

profiling::ProfilingGuid GetNetworkGuid ( )

Definition at line 311 of file LoadedNetwork.cpp.

312 {
313  return m_OptimizedNetwork->GetGuid();
314 }

◆ GetOutputTensorInfo()

TensorInfo GetOutputTensorInfo ( LayerBindingId  layerId) const

Definition at line 330 of file LoadedNetwork.cpp.

References ARMNN_ASSERT_MSG, CHECK_LOCATION, BackendId::Get(), Layer::GetBackendId(), Layer::GetNameStr(), armnn::IgnoreUnused(), armnn::info, and IWorkloadFactory::IsLayerSupported().

Referenced by Runtime::GetOutputTensorInfo().

331 {
332  for (auto&& outputLayer : m_OptimizedNetwork->GetGraph().GetOutputLayers())
333  {
334  ARMNN_ASSERT_MSG(outputLayer->GetNumInputSlots() == 1, "Output layer should have exactly 1 input slot");
335  ARMNN_ASSERT_MSG(outputLayer->GetInputSlot(0).GetConnection(), "Input slot on Output layer must be connected");
336  if (outputLayer->GetBindingId() == layerId)
337  {
338  return outputLayer->GetInputSlot(0).GetConnection()->GetTensorInfo();
339  }
340  }
341 
342  throw InvalidArgumentException(fmt::format("No output layer is associated with id {}", layerId));
343 }
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15

◆ GetProfiler()

const std::shared_ptr<Profiler>& GetProfiler ( ) const
inline

Definition at line 53 of file LoadedNetwork.hpp.

Referenced by Runtime::EnqueueWorkload().

53 { return m_Profiler; }

◆ MakeLoadedNetwork()

std::unique_ptr< LoadedNetwork > MakeLoadedNetwork ( std::unique_ptr< OptimizedNetwork net,
std::string &  errorMessage,
const INetworkProperties networkProperties,
profiling::ProfilingService profilingService 
)
static

Definition at line 83 of file LoadedNetwork.cpp.

References ARMNN_LOG, LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS, armnn::BackendRegistryInstance(), IBackendInternal::CreateMemoryManager(), IBackendInternal::CreateWorkloadFactory(), armnn::error, armnnUtils::Processes::GetCurrentId(), BackendRegistry::GetFactory(), ProfilerManager::GetInstance(), TimelineUtilityMethods::GetTimelineUtils(), armnn::Input, INetworkProperties::m_ExportEnabled, INetworkProperties::m_ImportEnabled, armnn::MemImport, LabelsAndEventClasses::NETWORK_GUID, armnn::Output, LabelsAndEventClasses::PROCESS_ID_GUID, ProfilerManager::RegisterProfiler(), IBackendInternal::SupportsTensorAllocatorAPI(), and Graph::TopologicalSort().

Referenced by Runtime::LoadNetwork().

87 {
88  std::unique_ptr<LoadedNetwork> loadedNetwork;
89 
90  auto Fail = [&](const std::exception& error) -> std::unique_ptr<LoadedNetwork>
91  {
92  errorMessage = ToErrorMessage("An error occurred when preparing the network workloads: ", error);
93  ARMNN_LOG(error) << errorMessage;
94 
95  return std::unique_ptr<LoadedNetwork>();
96  };
97 
98  try
99  {
100  loadedNetwork.reset(new LoadedNetwork(std::move(net), networkProperties, profilingService));
101  }
102  catch (const armnn::RuntimeException& error)
103  {
104  return Fail(error);
105  }
106  catch (const armnn::Exception& error)
107  {
108  return Fail(error);
109  }
110  catch (const std::runtime_error& error)
111  {
112  return Fail(error);
113  }
114 
115  return loadedNetwork;
116 }
#define ARMNN_LOG(severity)
Definition: Logging.hpp:163
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46

◆ RegisterDebugCallback()

void RegisterDebugCallback ( const DebugCallbackFunction func)

Definition at line 790 of file LoadedNetwork.cpp.

Referenced by Runtime::RegisterDebugCallback().

791 {
792  for (auto&& workloadPtr: m_WorkloadQueue)
793  {
794  workloadPtr.get()->RegisterDebugCallback(func);
795  }
796 }

◆ SendNetworkStructure()

void SendNetworkStructure ( )

Definition at line 274 of file LoadedNetwork.cpp.

References TimelineUtilityMethods::GetTimelineUtils(), armnn::Input, LabelsAndEventClasses::NETWORK_GUID, armnn::Output, and Graph::TopologicalSort().

275 {
276  Graph& order = m_OptimizedNetwork->GetGraph().TopologicalSort();
277  ProfilingGuid networkGuid = m_OptimizedNetwork->GetGuid();
278 
279  std::unique_ptr<TimelineUtilityMethods> timelineUtils =
280  TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService);
281 
282  timelineUtils->CreateTypedEntity(networkGuid, LabelsAndEventClasses::NETWORK_GUID);
283 
284  for (auto&& layer : order)
285  {
286  // Add layer to the post-optimisation network structure
287  AddLayerStructure(timelineUtils, *layer, networkGuid);
288  switch (layer->GetType())
289  {
290  case LayerType::Input:
291  case LayerType::Output:
292  {
293  // Inputs and outputs are treated in a special way - see EnqueueInput() and EnqueueOutput().
294  break;
295  }
296  default:
297  {
298  for (auto& workload : m_WorkloadQueue)
299  {
300  // Add workload to the post-optimisation network structure
301  AddWorkloadStructure(timelineUtils, workload, *layer);
302  }
303  break;
304  }
305  }
306  }
307  // Commit to send the post-optimisation network structure
308  timelineUtils->Commit();
309 }
static std::unique_ptr< TimelineUtilityMethods > GetTimelineUtils(ProfilingService &profilingService)
static ARMNN_DLLEXPORT ProfilingStaticGuid NETWORK_GUID

The documentation for this class was generated from the following files: