ArmNN
 21.02
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)
 
TensorInfo GetInputTensorInfo (NetworkId networkId, LayerBindingId layerId) const
 
TensorInfo GetOutputTensorInfo (NetworkId networkId, LayerBindingId layerId) const
 
Status EnqueueWorkload (NetworkId networkId, const InputTensors &inputTensors, const OutputTensors &outputTensors)
 
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...
 
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 247 of file Runtime.cpp.

References ProfilingService::AddBackendProfilingContext(), DeviceSpec::AddSupportedBackends(), ARMNN_ASSERT, ARMNN_LOG, ARMNN_VERSION, armnn::BackendRegistryInstance(), ProfilingService::ConfigureProfilingService(), BackendRegistry::GetFactory(), armnn::GetTimeDuration(), armnn::GetTimeNow(), armnn::info, IRuntime::CreationOptions::m_DynamicBackendsPath, IRuntime::CreationOptions::ExternalProfilingOptions::m_EnableProfiling, IRuntime::CreationOptions::m_ProfilingOptions, IRuntime::CreationOptions::ExternalProfilingOptions::m_TimelineEnabled, BackendRegistry::SetProfilingService(), and ProfilingService::WaitForProfilingServiceActivation().

Referenced by RuntimeImpl::GetDeviceSpec().

248  : m_NetworkIdCounter(0),
249  m_ProfilingService(*this)
250 {
251  const auto start_time = armnn::GetTimeNow();
252  ARMNN_LOG(info) << "ArmNN v" << ARMNN_VERSION << "\n";
253 
255  {
256  throw RuntimeException("It is not possible to enable timeline reporting without profiling being enabled");
257  }
258 
259  // Load any available/compatible dynamic backend before the runtime
260  // goes through the backend registry
261  LoadDynamicBackends(options.m_DynamicBackendsPath);
262 
263  BackendIdSet supportedBackends;
264  for (const auto& id : BackendRegistryInstance().GetBackendIds())
265  {
266  // Store backend contexts for the supported ones
267  try {
268  auto factoryFun = BackendRegistryInstance().GetFactory(id);
269  auto backend = factoryFun();
270  ARMNN_ASSERT(backend.get() != nullptr);
271 
272  auto context = backend->CreateBackendContext(options);
273 
274  // backends are allowed to return nullptrs if they
275  // don't wish to create a backend specific context
276  if (context)
277  {
278  m_BackendContexts.emplace(std::make_pair(id, std::move(context)));
279  }
280  supportedBackends.emplace(id);
281 
282  unique_ptr<armnn::profiling::IBackendProfiling> profilingIface =
283  std::make_unique<armnn::profiling::BackendProfiling>(armnn::profiling::BackendProfiling(
284  options, m_ProfilingService, id));
285 
286  // Backends may also provide a profiling context. Ask for it now.
287  auto profilingContext = backend->CreateBackendProfilingContext(options, profilingIface);
288  // Backends that don't support profiling will return a null profiling context.
289  if (profilingContext)
290  {
291  // Pass the context onto the profiling service.
292  m_ProfilingService.AddBackendProfilingContext(id, profilingContext);
293  }
294  }
295  catch (const BackendUnavailableException&)
296  {
297  // Ignore backends which are unavailable
298  }
299  }
300 
301  BackendRegistryInstance().SetProfilingService(m_ProfilingService);
302  // pass configuration info to the profiling service
303  m_ProfilingService.ConfigureProfilingService(options.m_ProfilingOptions);
305  {
306  // try to wait for the profiling service to initialise
307  m_ProfilingService.WaitForProfilingServiceActivation(3000);
308  }
309 
310  m_DeviceSpec.AddSupportedBackends(supportedBackends);
311 
312  ARMNN_LOG(info) << "Initialization time: " << std::setprecision(2)
313  << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n";
314 }
void AddSupportedBackends(const BackendIdSet &backendIds, bool isDynamic=false)
Definition: DeviceSpec.hpp:30
void WaitForProfilingServiceActivation(unsigned int timeout) override
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
std::unordered_set< BackendId > BackendIdSet
Definition: BackendId.hpp:191
#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
#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)
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
std::string m_DynamicBackendsPath
Setting this value will override the paths set by the DYNAMIC_BACKEND_PATHS compiler directive Only a...
Definition: IRuntime.hpp:60
void AddBackendProfilingContext(const BackendId backendId, std::shared_ptr< armnn::profiling::IBackendProfilingContext > profilingContext)
ExternalProfilingOptions m_ProfilingOptions
Definition: IRuntime.hpp:84
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 316 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().

317 {
318  const auto start_time = armnn::GetTimeNow();
319  std::vector<int> networkIDs;
320  try
321  {
322  // Coverity fix: The following code may throw an exception of type std::length_error.
323  std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(),
324  std::back_inserter(networkIDs),
325  [](const auto &pair) { return pair.first; });
326  }
327  catch (const std::exception& e)
328  {
329  // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
330  // exception of type std::length_error.
331  // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
332  std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what()
333  << "\nSome of the loaded networks may not be unloaded" << std::endl;
334  }
335  // We then proceed to unload all the networks which IDs have been appended to the list
336  // up to the point the exception was thrown (if any).
337 
338  for (auto networkID : networkIDs)
339  {
340  try
341  {
342  // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error,
343  // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error
344  UnloadNetwork(networkID);
345  }
346  catch (const std::exception& e)
347  {
348  // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
349  // exception of type std::length_error.
350  // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
351  std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what()
352  << std::endl;
353  }
354  }
355 
356  // Clear all dynamic backends.
358  m_DeviceSpec.ClearDynamicBackends();
359  m_BackendContexts.clear();
360 
362  ARMNN_LOG(info) << "Shutdown time: " << std::setprecision(2)
363  << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n";
364 }
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:168
#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

◆ EnqueueWorkload()

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

Definition at line 383 of file Runtime.cpp.

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

Referenced by BOOST_AUTO_TEST_CASE(), and VerifyPostOptimisationStructureTestImpl().

386 {
387  LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
389 
391 
392  static thread_local NetworkId lastId = networkId;
393  if (lastId != networkId)
394  {
395  LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
396  {
397  network->FreeWorkingMemory();
398  });
399  }
400  lastId=networkId;
401 
402  return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors);
403 }
static ProfilerManager & GetInstance()
Definition: Profiling.cpp:489
int NetworkId
Definition: IRuntime.hpp:20
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
Definition: Profiling.hpp:173
Status EnqueueWorkload(const InputTensors &inputTensors, const OutputTensors &outputTensors)
void RegisterProfiler(IProfiler *profiler)
Definition: Profiling.cpp:496
const std::shared_ptr< IProfiler > & GetProfiler() const

◆ GetDeviceSpec()

◆ GetInputTensorInfo()

TensorInfo GetInputTensorInfo ( NetworkId  networkId,
LayerBindingId  layerId 
) const

Definition at line 372 of file Runtime.cpp.

References LoadedNetwork::GetInputTensorInfo().

Referenced by BOOST_AUTO_TEST_CASE(), and VerifyPostOptimisationStructureTestImpl().

373 {
374  return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId);
375 }
TensorInfo GetInputTensorInfo(LayerBindingId layerId) const

◆ GetOutputTensorInfo()

TensorInfo GetOutputTensorInfo ( NetworkId  networkId,
LayerBindingId  layerId 
) const

Definition at line 377 of file Runtime.cpp.

References LoadedNetwork::GetOutputTensorInfo().

Referenced by BOOST_AUTO_TEST_CASE(), and VerifyPostOptimisationStructureTestImpl().

378 {
379  return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId);
380 }
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 220 of file Runtime.cpp.

Referenced by RuntimeImpl::GetDeviceSpec().

221 {
222  auto it = m_LoadedNetworks.find(networkId);
223  if (it != m_LoadedNetworks.end())
224  {
225  auto& loadedNetwork = it->second;
226  return loadedNetwork->GetProfiler();
227  }
228 
229  return nullptr;
230 }

◆ 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 109 of file Runtime.cpp.

References IRuntime::LoadNetwork().

Referenced by BOOST_AUTO_TEST_CASE(), and VerifyPostOptimisationStructureTestImpl().

110 {
111  std::string ignoredErrorMessage;
112  return LoadNetwork(networkIdOut, std::move(inNetwork), ignoredErrorMessage);
113 }
Status LoadNetwork(NetworkId &networkIdOut, IOptimizedNetworkPtr network)
Loads a complete network into the Runtime.
Definition: Runtime.cpp:109

◆ 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 115 of file Runtime.cpp.

References IRuntime::LoadNetwork().

118 {
119  INetworkProperties networkProperties;
120  return LoadNetwork(networkIdOut, std::move(inNetwork), errorMessage, networkProperties);
121 }
Status LoadNetwork(NetworkId &networkIdOut, IOptimizedNetworkPtr network)
Loads a complete network into the Runtime.
Definition: Runtime.cpp:109

◆ LoadNetwork() [3/3]

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

Definition at line 123 of file Runtime.cpp.

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

127 {
128  IOptimizedNetwork* rawNetwork = inNetwork.release();
129 
130  networkIdOut = GenerateNetworkId();
131 
132  for (auto&& context : m_BackendContexts)
133  {
134  context.second->BeforeLoadNetwork(networkIdOut);
135  }
136 
137  unique_ptr<LoadedNetwork> loadedNetwork = LoadedNetwork::MakeLoadedNetwork(
138  std::unique_ptr<IOptimizedNetwork>(rawNetwork),
139  errorMessage,
140  networkProperties,
141  m_ProfilingService);
142 
143  if (!loadedNetwork)
144  {
145  return Status::Failure;
146  }
147 
148  {
149  std::lock_guard<std::mutex> lockGuard(m_Mutex);
150 
151  // Stores the network
152  m_LoadedNetworks[networkIdOut] = std::move(loadedNetwork);
153  }
154 
155  for (auto&& context : m_BackendContexts)
156  {
157  context.second->AfterLoadNetwork(networkIdOut);
158  }
159 
160  if (m_ProfilingService.IsProfilingEnabled())
161  {
162  m_ProfilingService.IncrementCounterValue(armnn::profiling::NETWORK_LOADS);
163  }
164 
165  return Status::Success;
166 }
uint32_t IncrementCounterValue(uint16_t counterUid) override
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 405 of file Runtime.cpp.

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

Referenced by RuntimeImpl::GetDeviceSpec().

406 {
407  LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
408  loadedNetwork->RegisterDebugCallback(func);
409 }
void RegisterDebugCallback(const DebugCallbackFunction &func)

◆ ReportStructure()

void ReportStructure ( )
virtual

Implements IReportStructure.

Definition at line 232 of file Runtime.cpp.

Referenced by RuntimeImpl::GetDeviceSpec().

233 {
234  // No-op for the time being, but this may be useful in future to have the profilingService available
235  // if (profilingService.IsProfilingEnabled()){}
236 
237  LoadedNetworks::iterator it = m_LoadedNetworks.begin();
238  while (it != m_LoadedNetworks.end())
239  {
240  auto& loadedNetwork = it->second;
241  loadedNetwork->SendNetworkStructure();
242  // Increment the Iterator to point to next entry
243  it++;
244  }
245 }

◆ 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 168 of file Runtime.cpp.

References ARMNN_LOG, LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS, armnn::debug, armnn::Failure, TimelineUtilityMethods::GetTimelineUtils(), armnn::Success, and armnn::warning.

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

169 {
170  bool unloadOk = true;
171  for (auto&& context : m_BackendContexts)
172  {
173  unloadOk &= context.second->BeforeUnloadNetwork(networkId);
174  }
175 
176  if (!unloadOk)
177  {
178  ARMNN_LOG(warning) << "RuntimeImpl::UnloadNetwork(): failed to unload "
179  "network with ID:" << networkId << " because BeforeUnloadNetwork failed";
180  return Status::Failure;
181  }
182 
183  std::unique_ptr<profiling::TimelineUtilityMethods> timelineUtils =
185  {
186  std::lock_guard<std::mutex> lockGuard(m_Mutex);
187 
188  // If timeline recording is on mark the Network end of life
189  if (timelineUtils)
190  {
191  auto search = m_LoadedNetworks.find(networkId);
192  if (search != m_LoadedNetworks.end())
193  {
194  profiling::ProfilingGuid networkGuid = search->second->GetNetworkGuid();
195  timelineUtils->RecordEvent(networkGuid,
197  }
198  }
199  if (m_LoadedNetworks.erase(networkId) == 0)
200  {
201  ARMNN_LOG(warning) << "WARNING: RuntimeImpl::UnloadNetwork(): " << networkId << " not found!";
202  return Status::Failure;
203  }
204 
205  if (m_ProfilingService.IsProfilingEnabled())
206  {
207  m_ProfilingService.IncrementCounterValue(armnn::profiling::NETWORK_UNLOADS);
208  }
209  }
210 
211  for (auto&& context : m_BackendContexts)
212  {
213  context.second->AfterUnloadNetwork(networkId);
214  }
215 
216  ARMNN_LOG(debug) << "RuntimeImpl::UnloadNetwork(): Unloaded network with ID: " << networkId;
217  return Status::Success;
218 }
static std::unique_ptr< TimelineUtilityMethods > GetTimelineUtils(ProfilingService &profilingService)
#define ARMNN_LOG(severity)
Definition: Logging.hpp:202
uint32_t IncrementCounterValue(uint16_t counterUid) override
static ARMNN_DLLEXPORT ProfilingStaticGuid ARMNN_PROFILING_EOL_EVENT_CLASS
bool IsProfilingEnabled() const override

Friends And Related Function Documentation

◆ GetProfilingService

profiling::ProfilingService& GetProfilingService ( RuntimeImpl runtime)
friend

Definition at line 35 of file TestUtils.cpp.

Referenced by RuntimeImpl::GetDeviceSpec().

36 {
37  return runtime->m_ProfilingService;
38 }

◆ 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: