ArmNN
 21.02
Runtime.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include "LoadedNetwork.hpp"
8 #include "DeviceSpec.hpp"
9 
10 #include <armnn/INetwork.hpp>
11 #include <armnn/IRuntime.hpp>
12 #include <armnn/Tensor.hpp>
13 #include <armnn/BackendId.hpp>
14 
16 
17 #include <ProfilingService.hpp>
18 
19 #include <IProfilingService.hpp>
20 #include <IReportStructure.hpp>
21 
22 #include <mutex>
23 #include <unordered_map>
24 
25 namespace armnn
26 {
27 using LoadedNetworks = std::unordered_map<NetworkId, std::unique_ptr<LoadedNetwork>>;
29 
30 struct RuntimeImpl final : public IReportStructure
31 {
32 public:
33  /// Loads a complete network into the Runtime.
34  /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
35  /// @param [in] network - Complete network to load into the Runtime.
36  /// The runtime takes ownership of the network once passed in.
37  /// @return armnn::Status
38  Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network);
39 
40  /// Load a complete network into the IRuntime.
41  /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
42  /// @param [in] network Complete network to load into the IRuntime.
43  /// @param [out] errorMessage Error message if there were any errors.
44  /// The runtime takes ownership of the network once passed in.
45  /// @return armnn::Status
46  Status LoadNetwork(NetworkId& networkIdOut,
47  IOptimizedNetworkPtr network,
48  std::string& errorMessage);
49 
50  Status LoadNetwork(NetworkId& networkIdOut,
51  IOptimizedNetworkPtr network,
52  std::string& errorMessage,
53  const INetworkProperties& networkProperties);
54 
55  TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
56  TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const;
57 
58  // Evaluates network using input in inputTensors, outputs filled into outputTensors.
60  const InputTensors& inputTensors,
61  const OutputTensors& outputTensors);
62 
63  /// Unloads a network from the Runtime.
64  /// At the moment this only removes the network from the m_Impl->m_Network.
65  /// This might need more work in the future to be AndroidNN compliant.
66  /// @param [in] networkId Unique identifier for the network to be unloaded. Generated in LoadNetwork().
67  /// @return armnn::Status
68  Status UnloadNetwork(NetworkId networkId);
69 
70  const IDeviceSpec& GetDeviceSpec() const { return m_DeviceSpec; }
71 
72  /// Gets the profiler corresponding to the given network id.
73  /// @param networkId The id of the network for which to get the profile.
74  /// @return A pointer to the requested profiler, or nullptr if not found.
75  const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const;
76 
77  /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
78  /// @param networkId The id of the network to register the callback.
79  /// @param func callback function to pass to the debug layer.
80  void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func);
81 
82  /// Creates a runtime for workload execution.
83  RuntimeImpl(const IRuntime::CreationOptions& options);
84 
85  ~RuntimeImpl();
86 
87  //NOTE: we won't need the profiling service reference but it is good to pass the service
88  // in this way to facilitate other implementations down the road
89  void ReportStructure();
90 
91 private:
92  friend void RuntimeLoadedNetworksReserve(RuntimeImpl* runtime); // See RuntimeTests.cpp
93 
94  friend profiling::ProfilingService& GetProfilingService(RuntimeImpl* runtime); // See RuntimeTests.cpp
95 
96  int GenerateNetworkId();
97 
98  LoadedNetwork* GetLoadedNetworkPtr(NetworkId networkId) const;
99 
100  template<typename Func>
101  void LoadedNetworkFuncSafe(NetworkId networkId, Func f)
102  {
103  std::lock_guard<std::mutex> lockGuard(m_Mutex);
104  auto iter = m_LoadedNetworks.find(networkId);
105  if (iter != m_LoadedNetworks.end())
106  {
107  f(iter->second.get());
108  }
109  }
110 
111  /// Loads any available/compatible dynamic backend in the runtime.
112  void LoadDynamicBackends(const std::string& overrideBackendPath);
113 
114  mutable std::mutex m_Mutex;
115 
116  /// Map of Loaded Networks with associated GUID as key
117  LoadedNetworks m_LoadedNetworks;
118 
119  std::unordered_map<BackendId, IBackendInternal::IBackendContextPtr> m_BackendContexts;
120 
121  int m_NetworkIdCounter;
122 
123  DeviceSpec m_DeviceSpec;
124 
125  /// List of dynamic backends loaded in the runtime
126  std::vector<DynamicBackendPtr> m_DynamicBackends;
127 
128  /// Profiling Service Instance
129  profiling::ProfilingService m_ProfilingService;
130 };
131 
132 } // namespace armnn
TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
Definition: Runtime.cpp:372
Status UnloadNetwork(NetworkId networkId)
Unloads a network from the Runtime.
Definition: Runtime.cpp:168
Status LoadNetwork(NetworkId &networkIdOut, IOptimizedNetworkPtr network)
Loads a complete network into the Runtime.
Definition: Runtime.cpp:109
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:340
TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
Definition: Runtime.cpp:377
int NetworkId
Definition: IRuntime.hpp:20
Copyright (c) 2021 ARM Limited and Contributors.
Status EnqueueWorkload(NetworkId networkId, const InputTensors &inputTensors, const OutputTensors &outputTensors)
Definition: Runtime.cpp:383
profiling::IReportStructure IReportStructure
Definition: Runtime.hpp:28
void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction &func)
Registers a callback function to debug layers performing custom computations on intermediate tensors...
Definition: Runtime.cpp:405
const std::shared_ptr< IProfiler > GetProfiler(NetworkId networkId) const
Gets the profiler corresponding to the given network id.
Definition: Runtime.cpp:220
std::function< void(LayerGuid guid, unsigned int slotIndex, ITensorHandle *tensorHandle)> DebugCallbackFunction
Define the type of callback for the Debug layer to call.
Definition: Types.hpp:283
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:210
friend profiling::ProfilingService & GetProfilingService(RuntimeImpl *runtime)
Definition: TestUtils.cpp:35
void ReportStructure()
Definition: Runtime.cpp:232
const IDeviceSpec & GetDeviceSpec() const
Definition: Runtime.hpp:70
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:341
Status
enumeration
Definition: Types.hpp:26
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:174
Device specific knowledge to be passed to the optimizer.
Definition: Types.hpp:200
friend void RuntimeLoadedNetworksReserve(RuntimeImpl *runtime)
RuntimeImpl(const IRuntime::CreationOptions &options)
Creates a runtime for workload execution.
Definition: Runtime.cpp:247
std::unordered_map< NetworkId, std::unique_ptr< LoadedNetwork > > LoadedNetworks
Definition: Runtime.hpp:27