ArmNN
 20.02
Runtime Class Referencefinal

#include <Runtime.hpp>

Inheritance diagram for Runtime:
IRuntime

Public Member Functions

virtual Status LoadNetwork (NetworkId &networkIdOut, IOptimizedNetworkPtr network) override
 Loads a complete network into the Runtime. More...
 
virtual Status LoadNetwork (NetworkId &networkIdOut, IOptimizedNetworkPtr network, std::string &errorMessage) override
 Load a complete network into the IRuntime. More...
 
virtual Status LoadNetwork (NetworkId &networkIdOut, IOptimizedNetworkPtr network, std::string &errorMessage, const INetworkProperties &networkProperties) override
 
virtual TensorInfo GetInputTensorInfo (NetworkId networkId, LayerBindingId layerId) const override
 
virtual TensorInfo GetOutputTensorInfo (NetworkId networkId, LayerBindingId layerId) const override
 
virtual Status EnqueueWorkload (NetworkId networkId, const InputTensors &inputTensors, const OutputTensors &outputTensors) override
 Evaluates a network using input in inputTensors and outputs filled into outputTensors. More...
 
virtual Status UnloadNetwork (NetworkId networkId) override
 Unloads a network from the Runtime. More...
 
virtual const IDeviceSpecGetDeviceSpec () const override
 
virtual const std::shared_ptr< IProfilerGetProfiler (NetworkId networkId) const override
 Gets the profiler corresponding to the given network id. More...
 
virtual void RegisterDebugCallback (NetworkId networkId, const DebugCallbackFunction &func) override
 Registers a callback function to debug layers performing custom computations on intermediate tensors. More...
 
 Runtime (const CreationOptions &options)
 Creates a runtime for workload execution. More...
 
 ~Runtime ()
 

Friends

void RuntimeLoadedNetworksReserve (armnn::Runtime *runtime)
 

Additional Inherited Members

- Static Public Member Functions inherited from IRuntime
static IRuntimeCreateRaw (const CreationOptions &options)
 
static IRuntimePtr Create (const CreationOptions &options)
 
static void Destroy (IRuntime *runtime)
 
- Protected Member Functions inherited from IRuntime
 ~IRuntime ()
 

Detailed Description

Definition at line 23 of file Runtime.hpp.

Constructor & Destructor Documentation

◆ Runtime()

Runtime ( const CreationOptions options)

Creates a runtime for workload execution.

Definition at line 155 of file Runtime.cpp.

References ProfilingService::AddBackendProfilingContext(), DeviceSpec::AddSupportedBackends(), ARMNN_LOG, ARMNN_VERSION, armnn::BackendRegistryInstance(), ProfilingService::ConfigureProfilingService(), BackendRegistry::GetFactory(), armnn::info, ProfilingService::Instance(), IRuntime::CreationOptions::m_DynamicBackendsPath, and IRuntime::CreationOptions::m_ProfilingOptions.

Referenced by Runtime::GetDeviceSpec().

156  : m_NetworkIdCounter(0)
157 {
158  ARMNN_LOG(info) << "ArmNN v" << ARMNN_VERSION << "\n";
159 
160  // pass configuration info to the profiling service
162 
163  // Load any available/compatible dynamic backend before the runtime
164  // goes through the backend registry
165  LoadDynamicBackends(options.m_DynamicBackendsPath);
166 
167  BackendIdSet supportedBackends;
168  for (const auto& id : BackendRegistryInstance().GetBackendIds())
169  {
170  // Store backend contexts for the supported ones
171  try {
172  auto factoryFun = BackendRegistryInstance().GetFactory(id);
173  auto backend = factoryFun();
174  BOOST_ASSERT(backend.get() != nullptr);
175 
176  auto context = backend->CreateBackendContext(options);
177 
178  // backends are allowed to return nullptrs if they
179  // don't wish to create a backend specific context
180  if (context)
181  {
182  m_BackendContexts.emplace(std::make_pair(id, std::move(context)));
183  }
184  supportedBackends.emplace(id);
185 
186  unique_ptr<armnn::profiling::IBackendProfiling> profilingIface =
187  std::make_unique<armnn::profiling::BackendProfiling>(armnn::profiling::BackendProfiling(
189 
190  // Backends may also provide a profiling context. Ask for it now.
191  auto profilingContext = backend->CreateBackendProfilingContext(options, profilingIface);
192  // Backends that don't support profiling will return a null profiling context.
193  if (profilingContext)
194  {
195  // Enable profiling on the backend and assert that it returns true
196  if(profilingContext->EnableProfiling(true))
197  {
198  // Pass the context onto the profiling service.
200  }
201  else
202  {
203  throw BackendProfilingException("Unable to enable profiling on Backend Id: " + id.Get());
204  }
205  }
206  }
207  catch (const BackendUnavailableException&)
208  {
209  // Ignore backends which are unavailable
210  }
211 
212  }
213  m_DeviceSpec.AddSupportedBackends(supportedBackends);
214 }
void AddSupportedBackends(const BackendIdSet &backendIds, bool isDynamic=false)
Definition: DeviceSpec.hpp:30
#define ARMNN_VERSION
ARMNN_VERSION: "YYYYMMPP" where: YYYY = 4-digit year number MM = 2-digit month number PP = 2-digit pa...
Definition: Version.hpp:24
static ProfilingService & Instance()
FactoryFunction GetFactory(const BackendId &id) const
std::unordered_set< BackendId > BackendIdSet
Definition: BackendId.hpp:191
#define ARMNN_LOG(severity)
Definition: Logging.hpp:163
BackendRegistry & BackendRegistryInstance()
void AddBackendProfilingContext(const BackendId backendId, std::shared_ptr< armnn::profiling::IBackendProfilingContext > profilingContext)
armnn::Runtime::CreationOptions::ExternalProfilingOptions options
ProfilingState ConfigureProfilingService(const ExternalProfilingOptions &options, bool resetProfilingService=false)
Class for non-fatal exceptions raised while initialising a backend.
Definition: Exceptions.hpp:68

◆ ~Runtime()

~Runtime ( )

Definition at line 216 of file Runtime.cpp.

References DeviceSpec::ClearDynamicBackends(), DynamicBackendUtils::DeregisterDynamicBackends(), DeviceSpec::GetDynamicBackends(), and Runtime::UnloadNetwork().

Referenced by Runtime::GetDeviceSpec().

217 {
218  std::vector<int> networkIDs;
219  try
220  {
221  // Coverity fix: The following code may throw an exception of type std::length_error.
222  std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(),
223  std::back_inserter(networkIDs),
224  [](const auto &pair) { return pair.first; });
225  }
226  catch (const std::exception& e)
227  {
228  // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
229  // exception of type std::length_error.
230  // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
231  std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what()
232  << "\nSome of the loaded networks may not be unloaded" << std::endl;
233  }
234  // We then proceed to unload all the networks which IDs have been appended to the list
235  // up to the point the exception was thrown (if any).
236 
237  for (auto networkID : networkIDs)
238  {
239  try
240  {
241  // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error,
242  // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error
243  UnloadNetwork(networkID);
244  }
245  catch (const std::exception& e)
246  {
247  // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
248  // exception of type std::length_error.
249  // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
250  std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what()
251  << std::endl;
252  }
253  }
254 
255 
256  // Clear all dynamic backends.
258  m_DeviceSpec.ClearDynamicBackends();
259  m_BackendContexts.clear();
260 }
static void DeregisterDynamicBackends(const BackendIdSet &dynamicBackends)
const BackendIdSet & GetDynamicBackends() const
Definition: DeviceSpec.hpp:48
void ClearDynamicBackends()
Definition: DeviceSpec.hpp:39
virtual Status UnloadNetwork(NetworkId networkId) override
Unloads a network from the Runtime.
Definition: Runtime.cpp:105

Member Function Documentation

◆ EnqueueWorkload()

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

Evaluates a network using input in inputTensors and outputs filled into outputTensors.

Implements IRuntime.

Definition at line 279 of file Runtime.cpp.

References LoadedNetwork::EnqueueWorkload(), and LoadedNetwork::FreeWorkingMemory().

282 {
283  LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
284 
285  static thread_local NetworkId lastId = networkId;
286  if (lastId != networkId)
287  {
288  LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
289  {
290  network->FreeWorkingMemory();
291  });
292  }
293  lastId=networkId;
294 
295  return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors);
296 }
int NetworkId
Definition: IRuntime.hpp:19
Status EnqueueWorkload(const InputTensors &inputTensors, const OutputTensors &outputTensors)

◆ GetDeviceSpec()

virtual const IDeviceSpec& GetDeviceSpec ( ) const
inlineoverridevirtual

◆ GetInputTensorInfo()

TensorInfo GetInputTensorInfo ( NetworkId  networkId,
LayerBindingId  layerId 
) const
overridevirtual

Implements IRuntime.

Definition at line 268 of file Runtime.cpp.

References LoadedNetwork::GetInputTensorInfo().

269 {
270  return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId);
271 }
TensorInfo GetInputTensorInfo(LayerBindingId layerId) const

◆ GetOutputTensorInfo()

TensorInfo GetOutputTensorInfo ( NetworkId  networkId,
LayerBindingId  layerId 
) const
overridevirtual

Implements IRuntime.

Definition at line 273 of file Runtime.cpp.

References LoadedNetwork::GetOutputTensorInfo().

274 {
275  return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId);
276 }
TensorInfo GetOutputTensorInfo(LayerBindingId layerId) const

◆ GetProfiler()

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

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.

Implements IRuntime.

Definition at line 143 of file Runtime.cpp.

Referenced by Runtime::GetDeviceSpec().

144 {
145  auto it = m_LoadedNetworks.find(networkId);
146  if (it != m_LoadedNetworks.end())
147  {
148  auto& loadedNetwork = it->second;
149  return loadedNetwork->GetProfiler();
150  }
151 
152  return nullptr;
153 }

◆ LoadNetwork() [1/3]

Status LoadNetwork ( NetworkId networkIdOut,
IOptimizedNetworkPtr  network 
)
overridevirtual

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

Implements IRuntime.

Definition at line 47 of file Runtime.cpp.

Referenced by BOOST_AUTO_TEST_CASE().

48 {
49  std::string ignoredErrorMessage;
50  return LoadNetwork(networkIdOut, std::move(inNetwork), ignoredErrorMessage);
51 }
virtual Status LoadNetwork(NetworkId &networkIdOut, IOptimizedNetworkPtr network) override
Loads a complete network into the Runtime.
Definition: Runtime.cpp:47

◆ LoadNetwork() [2/3]

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

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

Implements IRuntime.

Definition at line 53 of file Runtime.cpp.

56 {
57  INetworkProperties networkProperties;
58  return LoadNetwork(networkIdOut, std::move(inNetwork), errorMessage, networkProperties);
59 }
virtual Status LoadNetwork(NetworkId &networkIdOut, IOptimizedNetworkPtr network) override
Loads a complete network into the Runtime.
Definition: Runtime.cpp:47

◆ LoadNetwork() [3/3]

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

Implements IRuntime.

Definition at line 61 of file Runtime.cpp.

References armnn::Failure, ProfilingService::IncrementCounterValue(), ProfilingService::Instance(), LoadedNetwork::MakeLoadedNetwork(), and armnn::Success.

65 {
66  IOptimizedNetwork* rawNetwork = inNetwork.release();
67 
68  networkIdOut = GenerateNetworkId();
69 
70  for (auto&& context : m_BackendContexts)
71  {
72  context.second->BeforeLoadNetwork(networkIdOut);
73  }
74 
75  unique_ptr<LoadedNetwork> loadedNetwork = LoadedNetwork::MakeLoadedNetwork(
76  std::unique_ptr<OptimizedNetwork>(boost::polymorphic_downcast<OptimizedNetwork*>(rawNetwork)),
77  errorMessage,
78  networkProperties);
79 
80  if (!loadedNetwork)
81  {
82  return Status::Failure;
83  }
84 
85  {
86  std::lock_guard<std::mutex> lockGuard(m_Mutex);
87 
88  // Stores the network
89  m_LoadedNetworks[networkIdOut] = std::move(loadedNetwork);
90  }
91 
92  for (auto&& context : m_BackendContexts)
93  {
94  context.second->AfterLoadNetwork(networkIdOut);
95  }
96 
97  if (profiling::ProfilingService::Instance().IsProfilingEnabled())
98  {
99  profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::NETWORK_LOADS);
100  }
101 
102  return Status::Success;
103 }
static ProfilingService & Instance()
uint32_t IncrementCounterValue(uint16_t counterUid) override
static std::unique_ptr< LoadedNetwork > MakeLoadedNetwork(std::unique_ptr< OptimizedNetwork > net, std::string &errorMessage, const INetworkProperties &networkProperties)

◆ RegisterDebugCallback()

void RegisterDebugCallback ( NetworkId  networkId,
const DebugCallbackFunction func 
)
overridevirtual

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.

Implements IRuntime.

Definition at line 298 of file Runtime.cpp.

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

Referenced by Runtime::GetDeviceSpec().

299 {
300  LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
301  loadedNetwork->RegisterDebugCallback(func);
302 }
void RegisterDebugCallback(const DebugCallbackFunction &func)

◆ UnloadNetwork()

Status UnloadNetwork ( NetworkId  networkId)
overridevirtual

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

Implements IRuntime.

Definition at line 105 of file Runtime.cpp.

References ARMNN_LOG, armnn::debug, armnn::Failure, ProfilingService::IncrementCounterValue(), ProfilingService::Instance(), armnn::Success, and armnn::warning.

Referenced by BOOST_AUTO_TEST_CASE(), and Runtime::~Runtime().

106 {
107  bool unloadOk = true;
108  for (auto&& context : m_BackendContexts)
109  {
110  unloadOk &= context.second->BeforeUnloadNetwork(networkId);
111  }
112 
113  if (!unloadOk)
114  {
115  ARMNN_LOG(warning) << "Runtime::UnloadNetwork(): failed to unload "
116  "network with ID:" << networkId << " because BeforeUnloadNetwork failed";
117  return Status::Failure;
118  }
119 
120  {
121  std::lock_guard<std::mutex> lockGuard(m_Mutex);
122 
123  if (m_LoadedNetworks.erase(networkId) == 0)
124  {
125  ARMNN_LOG(warning) << "WARNING: Runtime::UnloadNetwork(): " << networkId << " not found!";
126  return Status::Failure;
127  }
128  if (profiling::ProfilingService::Instance().IsProfilingEnabled())
129  {
130  profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::NETWORK_UNLOADS);
131  }
132  }
133 
134  for (auto&& context : m_BackendContexts)
135  {
136  context.second->AfterUnloadNetwork(networkId);
137  }
138 
139  ARMNN_LOG(debug) << "Runtime::UnloadNetwork(): Unloaded network with ID: " << networkId;
140  return Status::Success;
141 }
static ProfilingService & Instance()
#define ARMNN_LOG(severity)
Definition: Logging.hpp:163
uint32_t IncrementCounterValue(uint16_t counterUid) override

Friends And Related Function Documentation

◆ RuntimeLoadedNetworksReserve

void RuntimeLoadedNetworksReserve ( armnn::Runtime runtime)
friend

Definition at line 28 of file RuntimeTests.cpp.

Referenced by Runtime::GetDeviceSpec().

29 {
30  runtime->m_LoadedNetworks.reserve(1);
31 }

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