ArmNN
 20.02
Runtime.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. 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 <mutex>
18 #include <unordered_map>
19 
20 namespace armnn
21 {
22 
23 class Runtime final : public IRuntime
24 {
25 public:
26  /// Loads a complete network into the Runtime.
27  /// @param [out] networkIdOut - Unique identifier for the network is returned in this reference.
28  /// @param [in] network - Complete network to load into the Runtime.
29  /// The runtime takes ownership of the network once passed in.
30  /// @return armnn::Status
31  virtual Status LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr network) override;
32 
33  /// Load a complete network into the IRuntime.
34  /// @param [out] networkIdOut Unique identifier for the network is returned in this reference.
35  /// @param [in] network Complete network to load into the IRuntime.
36  /// @param [out] errorMessage Error message if there were any errors.
37  /// The runtime takes ownership of the network once passed in.
38  /// @return armnn::Status
39  virtual Status LoadNetwork(NetworkId& networkIdOut,
40  IOptimizedNetworkPtr network,
41  std::string& errorMessage) override;
42 
43  virtual Status LoadNetwork(NetworkId& networkIdOut,
44  IOptimizedNetworkPtr network,
45  std::string& errorMessage,
46  const INetworkProperties& networkProperties) override;
47 
48  virtual TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const override;
49  virtual TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const override;
50 
51  // Evaluates network using input in inputTensors, outputs filled into outputTensors.
52  virtual Status EnqueueWorkload(NetworkId networkId,
53  const InputTensors& inputTensors,
54  const OutputTensors& outputTensors) override;
55 
56  /// Unloads a network from the Runtime.
57  /// At the moment this only removes the network from the m_Impl->m_Network.
58  /// This might need more work in the future to be AndroidNN compliant.
59  /// @param [in] networkId Unique identifier for the network to be unloaded. Generated in LoadNetwork().
60  /// @return armnn::Status
61  virtual Status UnloadNetwork(NetworkId networkId) override;
62 
63  virtual const IDeviceSpec& GetDeviceSpec() const override { return m_DeviceSpec; }
64 
65  /// Gets the profiler corresponding to the given network id.
66  /// @param networkId The id of the network for which to get the profile.
67  /// @return A pointer to the requested profiler, or nullptr if not found.
68  virtual const std::shared_ptr<IProfiler> GetProfiler(NetworkId networkId) const override;
69 
70  /// Registers a callback function to debug layers performing custom computations on intermediate tensors.
71  /// @param networkId The id of the network to register the callback.
72  /// @param func callback function to pass to the debug layer.
73  virtual void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func) override;
74 
75  /// Creates a runtime for workload execution.
77 
78  ~Runtime();
79 
80 private:
81  friend void RuntimeLoadedNetworksReserve(armnn::Runtime* runtime); // See RuntimeTests.cpp
82 
83  int GenerateNetworkId();
84 
85  LoadedNetwork* GetLoadedNetworkPtr(NetworkId networkId) const;
86 
87  template<typename Func>
88  void LoadedNetworkFuncSafe(NetworkId networkId, Func f)
89  {
90  std::lock_guard<std::mutex> lockGuard(m_Mutex);
91  auto iter = m_LoadedNetworks.find(networkId);
92  if (iter != m_LoadedNetworks.end())
93  {
94  f(iter->second.get());
95  }
96  }
97 
98  /// Loads any available/compatible dynamic backend in the runtime.
99  void LoadDynamicBackends(const std::string& overrideBackendPath);
100 
101  mutable std::mutex m_Mutex;
102 
103  std::unordered_map<NetworkId, std::unique_ptr<LoadedNetwork>> m_LoadedNetworks;
104  std::unordered_map<BackendId, IBackendInternal::IBackendContextPtr> m_BackendContexts;
105 
106  int m_NetworkIdCounter;
107 
108  DeviceSpec m_DeviceSpec;
109 
110  /// List of dynamic backends loaded in the runtime
111  std::vector<DynamicBackendPtr> m_DynamicBackends;
112 };
113 
114 } // namespace armnn
virtual TensorInfo GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const override
Definition: Runtime.cpp:273
virtual const IDeviceSpec & GetDeviceSpec() const override
Definition: Runtime.hpp:63
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:225
int NetworkId
Definition: IRuntime.hpp:19
Copyright (c) 2020 ARM Limited.
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:244
virtual const std::shared_ptr< IProfiler > GetProfiler(NetworkId networkId) const override
Gets the profiler corresponding to the given network id.
Definition: Runtime.cpp:143
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:171
virtual void RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction &func) override
Registers a callback function to debug layers performing custom computations on intermediate tensors...
Definition: Runtime.cpp:298
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:226
Status
enumeration
Definition: Types.hpp:26
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:566
Device specific knowledge to be passed to the optimizer.
Definition: Types.hpp:161
friend void RuntimeLoadedNetworksReserve(armnn::Runtime *runtime)
virtual Status LoadNetwork(NetworkId &networkIdOut, IOptimizedNetworkPtr network) override
Loads a complete network into the Runtime.
Definition: Runtime.cpp:47
virtual Status UnloadNetwork(NetworkId networkId) override
Unloads a network from the Runtime.
Definition: Runtime.cpp:105
armnn::Runtime::CreationOptions::ExternalProfilingOptions options
Runtime(const CreationOptions &options)
Creates a runtime for workload execution.
Definition: Runtime.cpp:155
virtual Status EnqueueWorkload(NetworkId networkId, const InputTensors &inputTensors, const OutputTensors &outputTensors) override
Evaluates a network using input in inputTensors and outputs filled into outputTensors.
Definition: Runtime.cpp:279
virtual TensorInfo GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const override
Definition: Runtime.cpp:268