ArmNN
 20.05
Runtime Class Referencefinal

#include <Runtime.hpp>

Inheritance diagram for Runtime:
IRuntime IReportStructure

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 ()
 
virtual void ReportStructure () override
 
- Public Member Functions inherited from IReportStructure
virtual ~IReportStructure ()
 

Friends

void RuntimeLoadedNetworksReserve (armnn::Runtime *runtime)
 
profiling::ProfilingServiceGetProfilingService (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 30 of file Runtime.hpp.

Constructor & Destructor Documentation

◆ Runtime()

Runtime ( const CreationOptions options)

Creates a runtime for workload execution.

Definition at line 171 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, and BackendRegistry::SetProfilingService().

Referenced by Runtime::GetDeviceSpec().

172  : m_NetworkIdCounter(0),
173  m_ProfilingService(*this)
174 {
175  const auto start_time = armnn::GetTimeNow();
176  ARMNN_LOG(info) << "ArmNN v" << ARMNN_VERSION << "\n";
177 
178  if ( options.m_ProfilingOptions.m_TimelineEnabled && !options.m_ProfilingOptions.m_EnableProfiling )
179  {
180  throw RuntimeException("It is not possible to enable timeline reporting without profiling being enabled");
181  }
182 
183  // Load any available/compatible dynamic backend before the runtime
184  // goes through the backend registry
185  LoadDynamicBackends(options.m_DynamicBackendsPath);
186 
187  BackendIdSet supportedBackends;
188  for (const auto& id : BackendRegistryInstance().GetBackendIds())
189  {
190  // Store backend contexts for the supported ones
191  try {
192  auto factoryFun = BackendRegistryInstance().GetFactory(id);
193  auto backend = factoryFun();
194  ARMNN_ASSERT(backend.get() != nullptr);
195 
196  auto context = backend->CreateBackendContext(options);
197 
198  // backends are allowed to return nullptrs if they
199  // don't wish to create a backend specific context
200  if (context)
201  {
202  m_BackendContexts.emplace(std::make_pair(id, std::move(context)));
203  }
204  supportedBackends.emplace(id);
205 
206  unique_ptr<armnn::profiling::IBackendProfiling> profilingIface =
207  std::make_unique<armnn::profiling::BackendProfiling>(armnn::profiling::BackendProfiling(
208  options, m_ProfilingService, id));
209 
210  // Backends may also provide a profiling context. Ask for it now.
211  auto profilingContext = backend->CreateBackendProfilingContext(options, profilingIface);
212  // Backends that don't support profiling will return a null profiling context.
213  if (profilingContext)
214  {
215  // Pass the context onto the profiling service.
216  m_ProfilingService.AddBackendProfilingContext(id, profilingContext);
217  }
218  }
219  catch (const BackendUnavailableException&)
220  {
221  // Ignore backends which are unavailable
222  }
223  }
224 
225  BackendRegistryInstance().SetProfilingService(m_ProfilingService);
226  // pass configuration info to the profiling service
227  m_ProfilingService.ConfigureProfilingService(options.m_ProfilingOptions);
228 
229  m_DeviceSpec.AddSupportedBackends(supportedBackends);
230 
231  ARMNN_LOG(info) << "Initialization time: " << std::setprecision(2)
232  << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n";
233 }
void AddSupportedBackends(const BackendIdSet &backendIds, bool isDynamic=false)
Definition: DeviceSpec.hpp:30
#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
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_LOG(severity)
Definition: Logging.hpp:163
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
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 235 of file Runtime.cpp.

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

Referenced by Runtime::GetDeviceSpec().

236 {
237  const auto start_time = armnn::GetTimeNow();
238  std::vector<int> networkIDs;
239  try
240  {
241  // Coverity fix: The following code may throw an exception of type std::length_error.
242  std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(),
243  std::back_inserter(networkIDs),
244  [](const auto &pair) { return pair.first; });
245  }
246  catch (const std::exception& e)
247  {
248  // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
249  // exception of type std::length_error.
250  // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
251  std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what()
252  << "\nSome of the loaded networks may not be unloaded" << std::endl;
253  }
254  // We then proceed to unload all the networks which IDs have been appended to the list
255  // up to the point the exception was thrown (if any).
256 
257  for (auto networkID : networkIDs)
258  {
259  try
260  {
261  // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error,
262  // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error
263  UnloadNetwork(networkID);
264  }
265  catch (const std::exception& e)
266  {
267  // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
268  // exception of type std::length_error.
269  // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
270  std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what()
271  << std::endl;
272  }
273  }
274 
275  // Clear all dynamic backends.
277  m_DeviceSpec.ClearDynamicBackends();
278  m_BackendContexts.clear();
279 
281  ARMNN_LOG(info) << "Shutdown time: " << std::setprecision(2)
282  << std::fixed << armnn::GetTimeDuration(start_time).count() << " ms\n";
283 }
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)
#define ARMNN_LOG(severity)
Definition: Logging.hpp:163
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
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 302 of file Runtime.cpp.

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

Referenced by BOOST_AUTO_TEST_CASE(), and VerifyPostOptimisationStructureTestImpl().

305 {
306  LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
307 
308  static thread_local NetworkId lastId = networkId;
309  if (lastId != networkId)
310  {
311  LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
312  {
313  network->FreeWorkingMemory();
314  });
315  }
316  lastId=networkId;
317 
318  return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors);
319 }
int NetworkId
Definition: IRuntime.hpp:20
Status EnqueueWorkload(const InputTensors &inputTensors, const OutputTensors &outputTensors)

◆ GetDeviceSpec()

◆ GetInputTensorInfo()

TensorInfo GetInputTensorInfo ( NetworkId  networkId,
LayerBindingId  layerId 
) const
overridevirtual

Implements IRuntime.

Definition at line 291 of file Runtime.cpp.

References LoadedNetwork::GetInputTensorInfo().

Referenced by BOOST_AUTO_TEST_CASE(), and VerifyPostOptimisationStructureTestImpl().

292 {
293  return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId);
294 }
TensorInfo GetInputTensorInfo(LayerBindingId layerId) const

◆ GetOutputTensorInfo()

TensorInfo GetOutputTensorInfo ( NetworkId  networkId,
LayerBindingId  layerId 
) const
overridevirtual

Implements IRuntime.

Definition at line 296 of file Runtime.cpp.

References LoadedNetwork::GetOutputTensorInfo().

Referenced by BOOST_AUTO_TEST_CASE(), and VerifyPostOptimisationStructureTestImpl().

297 {
298  return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId);
299 }
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 144 of file Runtime.cpp.

Referenced by Runtime::GetDeviceSpec().

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

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

Referenced by BOOST_AUTO_TEST_CASE(), and VerifyPostOptimisationStructureTestImpl().

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

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

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

◆ LoadNetwork() [3/3]

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

Implements IRuntime.

Definition at line 60 of file Runtime.cpp.

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

64 {
65  IOptimizedNetwork* rawNetwork = inNetwork.release();
66 
67  networkIdOut = GenerateNetworkId();
68 
69  for (auto&& context : m_BackendContexts)
70  {
71  context.second->BeforeLoadNetwork(networkIdOut);
72  }
73 
74  unique_ptr<LoadedNetwork> loadedNetwork = LoadedNetwork::MakeLoadedNetwork(
75  std::unique_ptr<OptimizedNetwork>(PolymorphicDowncast<OptimizedNetwork*>(rawNetwork)),
76  errorMessage,
77  networkProperties,
78  m_ProfilingService);
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 (m_ProfilingService.IsProfilingEnabled())
98  {
99  m_ProfilingService.IncrementCounterValue(armnn::profiling::NETWORK_LOADS);
100  }
101 
102  return Status::Success;
103 }
uint32_t IncrementCounterValue(uint16_t counterUid) override
bool IsProfilingEnabled() const override
static std::unique_ptr< LoadedNetwork > MakeLoadedNetwork(std::unique_ptr< OptimizedNetwork > net, std::string &errorMessage, const INetworkProperties &networkProperties, profiling::ProfilingService &profilingService)

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

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

Referenced by Runtime::GetDeviceSpec().

322 {
323  LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
324  loadedNetwork->RegisterDebugCallback(func);
325 }
void RegisterDebugCallback(const DebugCallbackFunction &func)

◆ ReportStructure()

void ReportStructure ( )
overridevirtual

Implements IReportStructure.

Definition at line 156 of file Runtime.cpp.

Referenced by Runtime::GetDeviceSpec().

157 {
158  // No-op for the time being, but this may be useful in future to have the profilingService available
159  // if (profilingService.IsProfilingEnabled()){}
160 
161  LoadedNetworks::iterator it = m_LoadedNetworks.begin();
162  while (it != m_LoadedNetworks.end())
163  {
164  auto& loadedNetwork = it->second;
165  loadedNetwork->SendNetworkStructure();
166  // Increment the Iterator to point to next entry
167  it++;
168  }
169 }

◆ 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, 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 
129  if (m_ProfilingService.IsProfilingEnabled())
130  {
131  m_ProfilingService.IncrementCounterValue(armnn::profiling::NETWORK_UNLOADS);
132  }
133  }
134 
135  for (auto&& context : m_BackendContexts)
136  {
137  context.second->AfterUnloadNetwork(networkId);
138  }
139 
140  ARMNN_LOG(debug) << "Runtime::UnloadNetwork(): Unloaded network with ID: " << networkId;
141  return Status::Success;
142 }
#define ARMNN_LOG(severity)
Definition: Logging.hpp:163
uint32_t IncrementCounterValue(uint16_t counterUid) override
bool IsProfilingEnabled() const override

Friends And Related Function Documentation

◆ GetProfilingService

profiling::ProfilingService& GetProfilingService ( armnn::Runtime runtime)
friend

Definition at line 25 of file TestUtils.cpp.

Referenced by Runtime::GetDeviceSpec().

26 {
27  return runtime->m_ProfilingService;
28 }

◆ RuntimeLoadedNetworksReserve

void RuntimeLoadedNetworksReserve ( armnn::Runtime runtime)
friend

Definition at line 29 of file RuntimeTests.cpp.

Referenced by Runtime::GetDeviceSpec().

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

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