ArmNN
 21.11
RuntimeImpl Struct Referencefinal

#include <Runtime.hpp>

Inheritance diagram for RuntimeImpl:
IReportStructure

Public Member Functions

Status LoadNetwork (NetworkId &networkIdOut, IOptimizedNetworkPtr network)
 Loads a complete network into the Runtime. More...
 
Status LoadNetwork (NetworkId &networkIdOut, IOptimizedNetworkPtr network, std::string &errorMessage)
 Load a complete network into the IRuntime. More...
 
Status LoadNetwork (NetworkId &networkIdOut, IOptimizedNetworkPtr network, std::string &errorMessage, const INetworkProperties &networkProperties)
 
armnn::TensorInfo GetInputTensorInfo (NetworkId networkId, LayerBindingId layerId) const
 
armnn::TensorInfo GetOutputTensorInfo (NetworkId networkId, LayerBindingId layerId) const
 
std::vector< ImportedInputIdImportInputs (NetworkId networkId, const InputTensors &inputTensors)
 
std::vector< ImportedOutputIdImportOutputs (NetworkId networkId, const OutputTensors &outputTensors)
 
void ClearImportedInputs (NetworkId networkId, const std::vector< ImportedInputId > inputIds)
 
void ClearImportedOutputs (NetworkId networkId, const std::vector< ImportedOutputId > outputIds)
 
Status EnqueueWorkload (NetworkId networkId, const InputTensors &inputTensors, const OutputTensors &outputTensors)
 
Status Execute (IWorkingMemHandle &workingMemHandle, const InputTensors &inputTensors, const OutputTensors &outputTensors, std::vector< ImportedInputId > preImportedInputs, std::vector< ImportedOutputId > preImportedOutputs)
 This is an experimental function. More...
 
Status UnloadNetwork (NetworkId networkId)
 Unloads a network from the Runtime. More...
 
const IDeviceSpecGetDeviceSpec () const
 
const std::shared_ptr< IProfilerGetProfiler (NetworkId networkId) const
 Gets the profiler corresponding to the given network id. More...
 
std::unique_ptr< IWorkingMemHandleCreateWorkingMemHandle (NetworkId networkId)
 Create a new unique WorkingMemHandle object. More...
 
void RegisterDebugCallback (NetworkId networkId, const DebugCallbackFunction &func)
 Registers a callback function to debug layers performing custom computations on intermediate tensors. More...
 
 RuntimeImpl (const IRuntime::CreationOptions &options)
 Creates a runtime for workload execution. More...
 
 ~RuntimeImpl ()
 
void ReportStructure ()
 
- Public Member Functions inherited from IReportStructure
virtual ~IReportStructure ()
 

Friends

void RuntimeLoadedNetworksReserve (RuntimeImpl *runtime)
 
profiling::ProfilingServiceGetProfilingService (RuntimeImpl *runtime)
 

Detailed Description

Definition at line 30 of file Runtime.hpp.

Constructor & Destructor Documentation

◆ RuntimeImpl()

RuntimeImpl ( const IRuntime::CreationOptions options)

Creates a runtime for workload execution.

Definition at line 291 of file Runtime.cpp.

References ProfilingService::AddBackendProfilingContext(), DeviceSpec::AddSupportedBackends(), ARMNN_ASSERT, ARMNN_LOG, ARMNN_VERSION, armnn::BackendRegistryInstance(), DeviceSpec::ClearDynamicBackends(), ProfilingService::ConfigureProfilingService(), DynamicBackendUtils::DeregisterDynamicBackends(), armnn::DmaBufProtected, armnn::error, DeviceSpec::GetDynamicBackends(), BackendRegistry::GetFactory(), armnn::GetMemBlockStrategyTypeName(), armnn::GetMemoryOptimizerStrategy(), armnn::GetTimeDuration(), armnn::GetTimeNow(), armnn::HasCapability(), armnn::info, IRuntime::CreationOptions::m_BackendOptions, IRuntime::CreationOptions::m_CustomAllocatorMap, IRuntime::CreationOptions::m_DynamicBackendsPath, IRuntime::CreationOptions::ExternalProfilingOptions::m_EnableProfiling, IRuntime::CreationOptions::m_MemoryOptimizerStrategyMap, IRuntime::CreationOptions::m_ProfilingOptions, IRuntime::CreationOptions::m_ProtectedMode, IRuntime::CreationOptions::ExternalProfilingOptions::m_TimelineEnabled, armnn::ParseOptions(), BackendRegistry::RegisterAllocator(), BackendRegistry::RegisterMemoryOptimizerStrategy(), BackendRegistry::SetProfilingService(), ProfilingService::WaitForProfilingServiceActivation(), and armnn::warning.

Referenced by RuntimeImpl::GetDeviceSpec().

292  : m_NetworkIdCounter(0),
293  m_ProfilingService(*this)
294 {
295  const auto start_time = armnn::GetTimeNow();
296  ARMNN_LOG(info) << "ArmNN v" << ARMNN_VERSION << "\n";
298  {
299  throw RuntimeException(
300  "It is not possible to enable timeline reporting without profiling being enabled");
301  }
302 
303  // Load any available/compatible dynamic backend before the runtime
304  // goes through the backend registry
305  LoadDynamicBackends(options.m_DynamicBackendsPath);
306 
307  BackendIdSet supportedBackends;
308  for (const auto& id : BackendRegistryInstance().GetBackendIds())
309  {
310  // Store backend contexts for the supported ones
311  try {
312  auto factoryFun = BackendRegistryInstance().GetFactory(id);
313  ARMNN_ASSERT(factoryFun != nullptr);
314  auto backend = factoryFun();
315  ARMNN_ASSERT(backend != nullptr);
316  ARMNN_ASSERT(backend.get() != nullptr);
317 
318  auto customAllocatorMapIterator = options.m_CustomAllocatorMap.find(id);
319  if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end() &&
320  customAllocatorMapIterator->second == nullptr)
321  {
322  // We need to manually clean up the dynamic backends before throwing an exception.
324  m_DeviceSpec.ClearDynamicBackends();
325  throw armnn::Exception("Allocator associated with id " + id.Get() + " is null");
326  }
327 
328  // If the runtime is created in protected mode only add backends that support this mode
329  if (options.m_ProtectedMode)
330  {
331  // check if backend supports ProtectedMode
333  BackendCapability protectedContentCapability {"ProtectedContentAllocation", true};
334  if (!HasCapability(protectedContentCapability, id))
335  {
336  // Protected Content Allocation is not supported by the backend
337  // backend should not be registered
338  ARMNN_LOG(warning) << "Backend "
339  << id
340  << " is not registered as does not support protected content allocation \n";
341  continue;
342  }
343  // The user is responsible to provide a custom memory allocator which allows to allocate
344  // protected memory
345  if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end())
346  {
347  std::string err;
348  if (customAllocatorMapIterator->second->GetMemorySourceType()
350  {
351  if (!backend->UseCustomMemoryAllocator(customAllocatorMapIterator->second, err))
352  {
353  ARMNN_LOG(error) << "The backend "
354  << id
355  << " reported an error when entering protected mode. Backend won't be"
356  << " used. ErrorMsg: " << err;
357  continue;
358  }
359  // No errors so register the Custom Allocator with the BackendRegistry
360  BackendRegistryInstance().RegisterAllocator(id, customAllocatorMapIterator->second);
361  }
362  else
363  {
364  ARMNN_LOG(error) << "The CustomAllocator provided with the runtime options doesn't support "
365  "protected memory. Protected mode can't be activated. The backend "
366  << id
367  << " is not going to be used. MemorySource must be MemorySource::DmaBufProtected";
368  continue;
369  }
370  }
371  else
372  {
373  ARMNN_LOG(error) << "Protected mode can't be activated for backend: "
374  << id
375  << " no custom allocator was provided to the runtime options.";
376  continue;
377  }
378  }
379  else
380  {
381  // If a custom memory allocator is provided make the backend use that instead of the default
382  if (customAllocatorMapIterator != options.m_CustomAllocatorMap.end())
383  {
384  std::string err;
385  if (!backend->UseCustomMemoryAllocator(customAllocatorMapIterator->second, err))
386  {
387  ARMNN_LOG(error) << "The backend "
388  << id
389  << " reported an error when trying to use the provided custom allocator."
390  " Backend won't be used."
391  << " ErrorMsg: " << err;
392  continue;
393  }
394  // No errors so register the Custom Allocator with the BackendRegistry
395  BackendRegistryInstance().RegisterAllocator(id, customAllocatorMapIterator->second);
396  }
397  }
398 
399  // check if custom memory optimizer strategy map is set
400  if (!options.m_MemoryOptimizerStrategyMap.empty())
401  {
402  auto customMemoryOptimizerStrategyMapIterator = options.m_MemoryOptimizerStrategyMap.find(id);
403  // if a memory optimizer strategy is provided make the backend use that instead of the default
404  if (customMemoryOptimizerStrategyMapIterator != options.m_MemoryOptimizerStrategyMap.end())
405  {
406  // no errors.. register the memory optimizer strategy with the BackendRegistry
408  id, customMemoryOptimizerStrategyMapIterator->second);
409 
410  ARMNN_LOG(info) << "MemoryOptimizerStrategy "
411  << customMemoryOptimizerStrategyMapIterator->second->GetName()
412  << " set for the backend " << id << ".";
413  }
414  }
415  else
416  {
417  // check if to use one of the existing memory optimizer strategies is set
418  std::string memoryOptimizerStrategyName = "";
419  ParseOptions(options.m_BackendOptions, id, [&](std::string name, const BackendOptions::Var& value)
420  {
421  if (name == "MemoryOptimizerStrategy")
422  {
423  memoryOptimizerStrategyName = ParseStringBackendOption(value, "");
424  }
425  });
426  if (memoryOptimizerStrategyName != "")
427  {
428  std::shared_ptr<IMemoryOptimizerStrategy> strategy =
429  GetMemoryOptimizerStrategy(memoryOptimizerStrategyName);
430 
431  if (!strategy)
432  {
433  ARMNN_LOG(warning) << "MemoryOptimizerStrategy: " << memoryOptimizerStrategyName
434  << " was not found \n";
435  }
436  else
437  {
439  auto strategyType = GetMemBlockStrategyTypeName(strategy->GetMemBlockStrategyType());
440  BackendCapability memOptimizeStrategyCapability {strategyType, true};
441  if (HasCapability(memOptimizeStrategyCapability, id))
442  {
444 
445  ARMNN_LOG(info) << "MemoryOptimizerStrategy: "
446  << memoryOptimizerStrategyName << " set for the backend " << id << ".";
447  }
448  else
449  {
450  ARMNN_LOG(warning) << "Backend "
451  << id
452  << " does not have multi-axis packing capability and cannot support"
453  << "MemoryOptimizerStrategy: " << memoryOptimizerStrategyName << "\n";
454  }
455  }
456  }
457  }
458 
459  auto context = backend->CreateBackendContext(options);
460 
461  // backends are allowed to return nullptrs if they
462  // don't wish to create a backend specific context
463  if (context)
464  {
465  m_BackendContexts.emplace(std::make_pair(id, std::move(context)));
466  }
467  supportedBackends.emplace(id);
468 
469  unique_ptr<armnn::profiling::IBackendProfiling> profilingIface =
470  std::make_unique<armnn::profiling::BackendProfiling>(armnn::profiling::BackendProfiling(
471  options, m_ProfilingService, id));
472 
473  // Backends may also provide a profiling context. Ask for it now.
474  auto profilingContext = backend->CreateBackendProfilingContext(options, profilingIface);
475  // Backends that don't support profiling will return a null profiling context.
476  if (profilingContext)
477  {
478  // Pass the context onto the profiling service.
479  m_ProfilingService.AddBackendProfilingContext(id, profilingContext);
480  }
481  }
482  catch (const BackendUnavailableException&)
483  {
484  // Ignore backends which are unavailable
485  }
486  }
487 
488  BackendRegistryInstance().SetProfilingService(m_ProfilingService);
489  // pass configuration info to the profiling service
490  m_ProfilingService.ConfigureProfilingService(options.m_ProfilingOptions);
492  {
493  // try to wait for the profiling service to initialise
494  m_ProfilingService.WaitForProfilingServiceActivation(3000);
495  }
496 
497  m_DeviceSpec.AddSupportedBackends(supportedBackends);
498 
499  ARMNN_LOG(info) << "Initialization time: " << std::setprecision(2)
500  << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n";
501 }
void AddSupportedBackends(const BackendIdSet &backendIds, bool isDynamic=false)
Definition: DeviceSpec.hpp:30
void WaitForProfilingServiceActivation(unsigned int timeout) override
bool HasCapability(const std::string &name, const BackendCapabilities &capabilities)
Convenience function to check if a capability exists in a BackendCapabilites struct.
FactoryFunction GetFactory(const BackendId &id) const
std::chrono::duration< double, std::milli > GetTimeDuration(std::chrono::high_resolution_clock::time_point start_time)
Definition: Timer.hpp:19
Very basic type safe variant.
static void DeregisterDynamicBackends(const BackendIdSet &dynamicBackends)
std::unordered_set< BackendId > BackendIdSet
Definition: BackendId.hpp:193
void ParseOptions(const std::vector< BackendOptions > &options, BackendId backend, F f)
#define ARMNN_VERSION
ARMNN_VERSION: "X.Y.Z" where: X = Major version number Y = Minor version number Z = Patch version num...
Definition: Version.hpp:22
void RegisterMemoryOptimizerStrategy(const BackendId &id, std::shared_ptr< IMemoryOptimizerStrategy > strategy)
void RegisterAllocator(const BackendId &id, std::shared_ptr< ICustomAllocator > alloc)
#define ARMNN_LOG(severity)
Definition: Logging.hpp:202
BackendRegistry & BackendRegistryInstance()
std::chrono::high_resolution_clock::time_point GetTimeNow()
Definition: Timer.hpp:14
std::map< BackendId, std::shared_ptr< IMemoryOptimizerStrategy > > m_MemoryOptimizerStrategyMap
A map to define a custom memory optimizer strategy for specific backend Ids.
Definition: IRuntime.hpp:153
bool m_EnableProfiling
Indicates whether external profiling is enabled or not.
Definition: IRuntime.hpp:169
std::vector< BackendOptions > m_BackendOptions
Pass backend specific options.
Definition: IRuntime.hpp:220
BackendCapability
BackendCapability class.
Definition: Types.hpp:254
void SetProfilingService(armnn::Optional< profiling::ProfilingService &> profilingService)
std::map< BackendId, std::shared_ptr< ICustomAllocator > > m_CustomAllocatorMap
A map to define a custom memory allocator for specific backend Ids.
Definition: IRuntime.hpp:145
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
std::unique_ptr< IMemoryOptimizerStrategy > GetMemoryOptimizerStrategy(const std::string &strategyName)
bool m_ProtectedMode
Setting this flag will allow the user to create the Runtime in protected mode.
Definition: IRuntime.hpp:136
std::string m_DynamicBackendsPath
Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive Only a...
Definition: IRuntime.hpp:129
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
constexpr const char * GetMemBlockStrategyTypeName(MemBlockStrategyType memBlockStrategyType)
Definition: TypesUtils.hpp:264
const BackendIdSet & GetDynamicBackends() const
Definition: DeviceSpec.hpp:48
bool m_TimelineEnabled
Indicates whether external timeline profiling is enabled or not.
Definition: IRuntime.hpp:171
void ClearDynamicBackends()
Definition: DeviceSpec.hpp:39
void AddBackendProfilingContext(const BackendId backendId, std::shared_ptr< armnn::profiling::IBackendProfilingContext > profilingContext)
ExternalProfilingOptions m_ProfilingOptions
Definition: IRuntime.hpp:184
ProfilingState ConfigureProfilingService(const ExternalProfilingOptions &options, bool resetProfilingService=false)
Class for non-fatal exceptions raised while initialising a backend.
Definition: Exceptions.hpp:68

◆ ~RuntimeImpl()

Definition at line 503 of file Runtime.cpp.

References ARMNN_LOG, armnn::BackendRegistryInstance(), DeviceSpec::ClearDynamicBackends(), DynamicBackendUtils::DeregisterDynamicBackends(), DeviceSpec::GetDynamicBackends(), armnn::GetTimeDuration(), armnn::GetTimeNow(), armnn::info, BackendRegistry::SetProfilingService(), and RuntimeImpl::UnloadNetwork().

Referenced by RuntimeImpl::GetDeviceSpec().

504 {
505  const auto start_time = armnn::GetTimeNow();
506  std::vector<int> networkIDs;
507  try
508  {
509  // Coverity fix: The following code may throw an exception of type std::length_error.
510  std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(),
511  std::back_inserter(networkIDs),
512  [](const auto &pair) { return pair.first; });
513  }
514  catch (const std::exception& e)
515  {
516  // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
517  // exception of type std::length_error.
518  // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
519  std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what()
520  << "\nSome of the loaded networks may not be unloaded" << std::endl;
521  }
522  // We then proceed to unload all the networks which IDs have been appended to the list
523  // up to the point the exception was thrown (if any).
524 
525  for (auto networkID : networkIDs)
526  {
527  try
528  {
529  // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error,
530  // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error
531  UnloadNetwork(networkID);
532  }
533  catch (const std::exception& e)
534  {
535  // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
536  // exception of type std::length_error.
537  // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
538  std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what()
539  << std::endl;
540  }
541  }
542 
543  // Clear all dynamic backends.
545  m_DeviceSpec.ClearDynamicBackends();
546  m_BackendContexts.clear();
547 
549  ARMNN_LOG(info) << "Shutdown time: " << std::setprecision(2)
550  << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n";
551 }
std::chrono::duration< double, std::milli > GetTimeDuration(std::chrono::high_resolution_clock::time_point start_time)
Definition: Timer.hpp:19
static void DeregisterDynamicBackends(const BackendIdSet &dynamicBackends)
Status UnloadNetwork(NetworkId networkId)
Unloads a network from the Runtime.
Definition: Runtime.cpp:209
#define ARMNN_LOG(severity)
Definition: Logging.hpp:202
BackendRegistry & BackendRegistryInstance()
std::chrono::high_resolution_clock::time_point GetTimeNow()
Definition: Timer.hpp:14
void SetProfilingService(armnn::Optional< profiling::ProfilingService &> profilingService)
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
Definition: Optional.hpp:32
const BackendIdSet & GetDynamicBackends() const
Definition: DeviceSpec.hpp:48
void ClearDynamicBackends()
Definition: DeviceSpec.hpp:39

Member Function Documentation

◆ ClearImportedInputs()

void ClearImportedInputs ( NetworkId  networkId,
const std::vector< ImportedInputId inputIds 
)

Definition at line 579 of file Runtime.cpp.

References LoadedNetwork::ClearImportedInputs().

580 {
581  return GetLoadedNetworkPtr(networkId)->ClearImportedInputs(inputIds);
582 }
void ClearImportedInputs(const std::vector< ImportedInputId > inputIds)

◆ ClearImportedOutputs()

void ClearImportedOutputs ( NetworkId  networkId,
const std::vector< ImportedOutputId outputIds 
)

Definition at line 583 of file Runtime.cpp.

References LoadedNetwork::ClearImportedOutputs().

584 {
585  return GetLoadedNetworkPtr(networkId)->ClearImportedOutputs(outputIds);
586 }
void ClearImportedOutputs(const std::vector< ImportedOutputId > outputIds)

◆ CreateWorkingMemHandle()

std::unique_ptr< IWorkingMemHandle > CreateWorkingMemHandle ( NetworkId  networkId)

Create a new unique WorkingMemHandle object.

Create multiple handles if you wish to have overlapped Execution by calling this function from different threads.

Definition at line 653 of file Runtime.cpp.

References ARMNN_LOG, ARMNN_SCOPED_PROFILING_EVENT, LoadedNetwork::CreateWorkingMemHandle(), armnn::error, LoadedNetwork::FreeWorkingMemory(), ProfilerManager::GetInstance(), LoadedNetwork::GetProfiler(), LoadedNetwork::IsAsyncEnabled(), ProfilerManager::RegisterProfiler(), and armnn::Undefined.

Referenced by RuntimeImpl::GetDeviceSpec().

654 {
655  LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
656 
657  if (!loadedNetwork)
658  {
659  ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n";
660  return nullptr;
661  }
662  if (!loadedNetwork->IsAsyncEnabled())
663  {
664  ARMNN_LOG(error) << "Network " << networkId << " is not async enabled.\n";
665  return nullptr;
666  }
668 
669  ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "CreateWorkingMemHandle");
670 
671  static thread_local NetworkId lastId = networkId;
672  if (lastId != networkId)
673  {
674  LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
675  {
676  network->FreeWorkingMemory();
677  });
678  }
679  lastId=networkId;
680 
681  return loadedNetwork->CreateWorkingMemHandle(networkId);
682 }
std::unique_ptr< IWorkingMemHandle > CreateWorkingMemHandle(NetworkId networkId)
Create a new unique WorkingMemHandle object.
static ProfilerManager & GetInstance()
Definition: Profiling.cpp:568
#define ARMNN_LOG(severity)
Definition: Logging.hpp:202
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
Definition: Profiling.hpp:220
int NetworkId
Definition: IRuntime.hpp:25
void RegisterProfiler(IProfiler *profiler)
Definition: Profiling.cpp:575
const std::shared_ptr< IProfiler > & GetProfiler() const

◆ EnqueueWorkload()

Status EnqueueWorkload ( NetworkId  networkId,
const InputTensors inputTensors,
const OutputTensors outputTensors 
)

Definition at line 588 of file Runtime.cpp.

References ARMNN_LOG, ARMNN_SCOPED_PROFILING_EVENT, LoadedNetwork::EnqueueWorkload(), armnn::error, armnn::Failure, LoadedNetwork::FreeWorkingMemory(), ProfilerManager::GetInstance(), LoadedNetwork::GetProfiler(), LoadedNetwork::IsAsyncEnabled(), ProfilerManager::RegisterProfiler(), and armnn::Undefined.

Referenced by TEST_SUITE(), and VerifyPostOptimisationStructureTestImpl().

591 {
592  LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
593 
594  if (!loadedNetwork)
595  {
596  ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n";
597  return Status::Failure;
598  }
599  if (loadedNetwork->IsAsyncEnabled())
600  {
601  ARMNN_LOG(error) << "Network " << networkId << " is async enabled.\n";
602  return Status::Failure;
603  }
605 
607 
608  static thread_local NetworkId lastId = networkId;
609  if (lastId != networkId)
610  {
611  LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
612  {
613  network->FreeWorkingMemory();
614  });
615  }
616  lastId=networkId;
617 
618  return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors);
619 }
static ProfilerManager & GetInstance()
Definition: Profiling.cpp:568
#define ARMNN_LOG(severity)
Definition: Logging.hpp:202
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
Definition: Profiling.hpp:220
int NetworkId
Definition: IRuntime.hpp:25
Status EnqueueWorkload(const InputTensors &inputTensors, const OutputTensors &outputTensors)
Single thread execution of the loaded network.
void RegisterProfiler(IProfiler *profiler)
Definition: Profiling.cpp:575
const std::shared_ptr< IProfiler > & GetProfiler() const

◆ Execute()

Status Execute ( IWorkingMemHandle workingMemHandle,
const InputTensors inputTensors,
const OutputTensors outputTensors,
std::vector< ImportedInputId preImportedInputs,
std::vector< ImportedOutputId preImportedOutputs 
)

This is an experimental function.

Evaluates a network using input in inputTensors and outputs filled into outputTensors. This function performs a thread safe execution of the network. Returns once execution is complete. Will block until this and any other thread using the same workingMem object completes.

Definition at line 621 of file Runtime.cpp.

References ARMNN_LOG, ARMNN_SCOPED_PROFILING_EVENT, armnn::error, LoadedNetwork::Execute(), armnn::Failure, ProfilerManager::GetInstance(), IWorkingMemHandle::GetNetworkId(), LoadedNetwork::GetProfiler(), LoadedNetwork::IsAsyncEnabled(), ProfilerManager::RegisterProfiler(), and armnn::Undefined.

626 {
627  NetworkId networkId = iWorkingMemHandle.GetNetworkId();
628  LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
629 
630  if (!loadedNetwork)
631  {
632  ARMNN_LOG(error) << "A Network with an id of " << networkId << " does not exist.\n";
633  return Status::Failure;
634  }
635  if (!loadedNetwork->IsAsyncEnabled())
636  {
637  ARMNN_LOG(error) << "Attempting execute " << networkId << " when it is not async enabled.\n";
638  return Status::Failure;
639  }
641 
643 
644  return loadedNetwork->Execute(inputTensors,
645  outputTensors,
646  iWorkingMemHandle,
647  preImportedInputs,
648  preImportedOutputs);
649 }
Status Execute(const InputTensors &inputTensors, const OutputTensors &outputTensors, IWorkingMemHandle &workingMemHandle, std::vector< ImportedInputId > preImportedInputs={}, std::vector< ImportedOutputId > preImportedOutputs={})
Thread safe execution of the loaded network.
static ProfilerManager & GetInstance()
Definition: Profiling.cpp:568
#define ARMNN_LOG(severity)
Definition: Logging.hpp:202
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
Definition: Profiling.hpp:220
int NetworkId
Definition: IRuntime.hpp:25
void RegisterProfiler(IProfiler *profiler)
Definition: Profiling.cpp:575
const std::shared_ptr< IProfiler > & GetProfiler() const

◆ GetDeviceSpec()

◆ GetInputTensorInfo()

TensorInfo GetInputTensorInfo ( NetworkId  networkId,
LayerBindingId  layerId 
) const

Definition at line 559 of file Runtime.cpp.

References LoadedNetwork::GetInputTensorInfo().

Referenced by TEST_SUITE(), and VerifyPostOptimisationStructureTestImpl().

560 {
561  return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId);
562 }
TensorInfo GetInputTensorInfo(LayerBindingId layerId) const

◆ GetOutputTensorInfo()

TensorInfo GetOutputTensorInfo ( NetworkId  networkId,
LayerBindingId  layerId 
) const

Definition at line 564 of file Runtime.cpp.

References LoadedNetwork::GetOutputTensorInfo().

Referenced by TEST_SUITE(), and VerifyPostOptimisationStructureTestImpl().

565 {
566  return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId);
567 }
TensorInfo GetOutputTensorInfo(LayerBindingId layerId) const

◆ GetProfiler()

const std::shared_ptr< IProfiler > GetProfiler ( NetworkId  networkId) const

Gets the profiler corresponding to the given network id.

Parameters
networkIdThe id of the network for which to get the profile.
Returns
A pointer to the requested profiler, or nullptr if not found.

Definition at line 264 of file Runtime.cpp.

Referenced by RuntimeImpl::GetDeviceSpec().

265 {
266  auto it = m_LoadedNetworks.find(networkId);
267  if (it != m_LoadedNetworks.end())
268  {
269  auto& loadedNetwork = it->second;
270  return loadedNetwork->GetProfiler();
271  }
272 
273  return nullptr;
274 }

◆ ImportInputs()

std::vector< ImportedInputId > ImportInputs ( NetworkId  networkId,
const InputTensors inputTensors 
)

Definition at line 569 of file Runtime.cpp.

References LoadedNetwork::ImportInputs().

570 {
571  return GetLoadedNetworkPtr(networkId)->ImportInputs(inputTensors);
572 }
std::vector< ImportedInputId > ImportInputs(const InputTensors &inputTensors)

◆ ImportOutputs()

std::vector< ImportedOutputId > ImportOutputs ( NetworkId  networkId,
const OutputTensors outputTensors 
)

Definition at line 574 of file Runtime.cpp.

References LoadedNetwork::ImportOutputs().

575 {
576  return GetLoadedNetworkPtr(networkId)->ImportOutputs(outputTensors);
577 }
std::vector< ImportedOutputId > ImportOutputs(const OutputTensors &outputTensors)

◆ LoadNetwork() [1/3]

Status LoadNetwork ( NetworkId networkIdOut,
IOptimizedNetworkPtr  network 
)

Loads a complete network into the Runtime.

Parameters
[out]networkIdOut- Unique identifier for the network is returned in this reference.
[in]network- Complete network to load into the Runtime. The runtime takes ownership of the network once passed in.
Returns
armnn::Status

Definition at line 145 of file Runtime.cpp.

References IRuntime::LoadNetwork().

Referenced by TEST_SUITE(), and VerifyPostOptimisationStructureTestImpl().

146 {
147  std::string ignoredErrorMessage;
148  return LoadNetwork(networkIdOut, std::move(inNetwork), ignoredErrorMessage);
149 }
Status LoadNetwork(NetworkId &networkIdOut, IOptimizedNetworkPtr network)
Loads a complete network into the Runtime.
Definition: Runtime.cpp:145

◆ LoadNetwork() [2/3]

Status LoadNetwork ( NetworkId networkIdOut,
IOptimizedNetworkPtr  network,
std::string &  errorMessage 
)

Load a complete network into the IRuntime.

Parameters
[out]networkIdOutUnique identifier for the network is returned in this reference.
[in]networkComplete network to load into the IRuntime.
[out]errorMessageError message if there were any errors. The runtime takes ownership of the network once passed in.
Returns
armnn::Status

Definition at line 151 of file Runtime.cpp.

References IRuntime::LoadNetwork(), and armnn::Undefined.

154 {
155  INetworkProperties networkProperties(
157  return LoadNetwork(networkIdOut, std::move(inNetwork), errorMessage, networkProperties);
158 }
Status LoadNetwork(NetworkId &networkIdOut, IOptimizedNetworkPtr network)
Loads a complete network into the Runtime.
Definition: Runtime.cpp:145

◆ LoadNetwork() [3/3]

Status LoadNetwork ( NetworkId networkIdOut,
IOptimizedNetworkPtr  network,
std::string &  errorMessage,
const INetworkProperties networkProperties 
)

Definition at line 160 of file Runtime.cpp.

References armnn::Failure, ProfilerManager::GetInstance(), LoadedNetwork::MakeLoadedNetwork(), ProfilerManager::RegisterProfiler(), and armnn::Success.

164 {
165  // Register the profiler
166  auto profiler = inNetwork->GetProfiler();
168 
169  IOptimizedNetwork* rawNetwork = inNetwork.release();
170 
171  networkIdOut = GenerateNetworkId();
172 
173  for (auto&& context : m_BackendContexts)
174  {
175  context.second->BeforeLoadNetwork(networkIdOut);
176  }
177 
178  unique_ptr<LoadedNetwork> loadedNetwork = LoadedNetwork::MakeLoadedNetwork(
179  std::unique_ptr<IOptimizedNetwork>(rawNetwork),
180  errorMessage,
181  networkProperties,
182  m_ProfilingService);
183 
184  if (!loadedNetwork)
185  {
186  return Status::Failure;
187  }
188 
189  {
190  std::lock_guard<std::mutex> lockGuard(m_Mutex);
191 
192  // Stores the network
193  m_LoadedNetworks[networkIdOut] = std::move(loadedNetwork);
194  }
195 
196  for (auto&& context : m_BackendContexts)
197  {
198  context.second->AfterLoadNetwork(networkIdOut);
199  }
200 
201  if (m_ProfilingService.IsProfilingEnabled())
202  {
203  m_ProfilingService.IncrementCounterValue(armnn::profiling::NETWORK_LOADS);
204  }
205 
206  return Status::Success;
207 }
static ProfilerManager & GetInstance()
Definition: Profiling.cpp:568
uint32_t IncrementCounterValue(uint16_t counterUid) override
void RegisterProfiler(IProfiler *profiler)
Definition: Profiling.cpp:575
bool IsProfilingEnabled() const override
static std::unique_ptr< LoadedNetwork > MakeLoadedNetwork(std::unique_ptr< IOptimizedNetwork > net, std::string &errorMessage, const INetworkProperties &networkProperties, profiling::ProfilingService &profilingService)

◆ RegisterDebugCallback()

void RegisterDebugCallback ( NetworkId  networkId,
const DebugCallbackFunction func 
)

Registers a callback function to debug layers performing custom computations on intermediate tensors.

Parameters
networkIdThe id of the network to register the callback.
funccallback function to pass to the debug layer.

Definition at line 684 of file Runtime.cpp.

References DeviceSpec::AddSupportedBackends(), DynamicBackendUtils::CreateDynamicBackends(), DynamicBackendUtils::GetBackendPaths(), DynamicBackendUtils::GetSharedObjects(), LoadedNetwork::RegisterDebugCallback(), and DynamicBackendUtils::RegisterDynamicBackends().

Referenced by RuntimeImpl::GetDeviceSpec().

685 {
686  LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
687  loadedNetwork->RegisterDebugCallback(func);
688 }
void RegisterDebugCallback(const DebugCallbackFunction &func)

◆ ReportStructure()

void ReportStructure ( )
virtual

Implements IReportStructure.

Definition at line 276 of file Runtime.cpp.

Referenced by RuntimeImpl::GetDeviceSpec().

277 {
278  // No-op for the time being, but this may be useful in future to have the profilingService available
279  // if (profilingService.IsProfilingEnabled()){}
280 
281  LoadedNetworks::iterator it = m_LoadedNetworks.begin();
282  while (it != m_LoadedNetworks.end())
283  {
284  auto& loadedNetwork = it->second;
285  loadedNetwork->SendNetworkStructure();
286  // Increment the Iterator to point to next entry
287  it++;
288  }
289 }

◆ UnloadNetwork()

Status UnloadNetwork ( NetworkId  networkId)

Unloads a network from the Runtime.

At the moment this only removes the network from the m_Impl->m_Network. This might need more work in the future to be AndroidNN compliant.

Parameters
[in]networkIdUnique identifier for the network to be unloaded. Generated in LoadNetwork().
Returns
armnn::Status

Definition at line 209 of file Runtime.cpp.

References ARMNN_LOG, armnn::debug, armnn::Failure, ProfilerManager::GetInstance(), TimelineUtilityMethods::GetTimelineUtils(), ProfilerManager::RegisterProfiler(), armnn::Success, and armnn::warning.

Referenced by TEST_SUITE(), and RuntimeImpl::~RuntimeImpl().

210 {
211  bool unloadOk = true;
212  for (auto&& context : m_BackendContexts)
213  {
214  unloadOk &= context.second->BeforeUnloadNetwork(networkId);
215  }
216 
217  if (!unloadOk)
218  {
219  ARMNN_LOG(warning) << "RuntimeImpl::UnloadNetwork(): failed to unload "
220  "network with ID:" << networkId << " because BeforeUnloadNetwork failed";
221  return Status::Failure;
222  }
223 
224  std::unique_ptr<profiling::TimelineUtilityMethods> timelineUtils =
226  {
227  std::lock_guard<std::mutex> lockGuard(m_Mutex);
228 
229  // If timeline recording is on mark the Network end of life
230  if (timelineUtils)
231  {
232  auto search = m_LoadedNetworks.find(networkId);
233  if (search != m_LoadedNetworks.end())
234  {
235  profiling::ProfilingGuid networkGuid = search->second->GetNetworkGuid();
236  timelineUtils->RecordEvent(networkGuid,
237  profiling::LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS);
238  }
239  }
240  if (m_LoadedNetworks.erase(networkId) == 0)
241  {
242  ARMNN_LOG(warning) << "WARNING: RuntimeImpl::UnloadNetwork(): " << networkId << " not found!";
243  return Status::Failure;
244  }
245 
246  if (m_ProfilingService.IsProfilingEnabled())
247  {
248  m_ProfilingService.IncrementCounterValue(armnn::profiling::NETWORK_UNLOADS);
249  }
250  }
251 
252  for (auto&& context : m_BackendContexts)
253  {
254  context.second->AfterUnloadNetwork(networkId);
255  }
256 
257  // Unregister the profiler
259 
260  ARMNN_LOG(debug) << "RuntimeImpl::UnloadNetwork(): Unloaded network with ID: " << networkId;
261  return Status::Success;
262 }
static std::unique_ptr< TimelineUtilityMethods > GetTimelineUtils(ProfilingService &profilingService)
static ProfilerManager & GetInstance()
Definition: Profiling.cpp:568
#define ARMNN_LOG(severity)
Definition: Logging.hpp:202
uint32_t IncrementCounterValue(uint16_t counterUid) override
void RegisterProfiler(IProfiler *profiler)
Definition: Profiling.cpp:575
bool IsProfilingEnabled() const override

Friends And Related Function Documentation

◆ GetProfilingService

profiling::ProfilingService& GetProfilingService ( RuntimeImpl runtime)
friend

Definition at line 57 of file TestUtils.cpp.

Referenced by RuntimeImpl::GetDeviceSpec().

58 {
59  return runtime->m_ProfilingService;
60 }

◆ RuntimeLoadedNetworksReserve

void RuntimeLoadedNetworksReserve ( RuntimeImpl runtime)
friend

Definition at line 30 of file RuntimeTests.cpp.

Referenced by RuntimeImpl::GetDeviceSpec().

31 {
32  runtime->m_LoadedNetworks.reserve(1);
33 }

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