ArmNN
 21.08
LoadedNetwork.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 "Network.hpp"
8 #include "LayerFwd.hpp"
9 #include "Profiling.hpp"
10 
11 #include <armnn/Tensor.hpp>
16 #include <ProfilingService.hpp>
18 
19 #include <common/include/LabelsAndEventClasses.hpp>
20 
21 #include <mutex>
22 #include <condition_variable>
23 #include <unordered_map>
24 
25 namespace cl
26 {
27 class Context;
28 class CommandQueue;
29 class Device;
30 }
31 
32 namespace armnn
33 {
34 
36 {
37 public:
38  using WorkloadQueue = std::vector<std::unique_ptr<IWorkload>>;
39 
41  {
42  FreeWorkingMemory();
43  }
44 
45  /// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have
46  /// overlapped Execution by calling this function from different threads.
47  std::unique_ptr<IWorkingMemHandle> CreateWorkingMemHandle(NetworkId networkId);
48 
49  TensorInfo GetInputTensorInfo(LayerBindingId layerId) const;
50  TensorInfo GetOutputTensorInfo(LayerBindingId layerId) const;
51 
52  /// Single thread execution of the loaded network
53  Status EnqueueWorkload(const InputTensors& inputTensors, const OutputTensors& outputTensors);
54 
55  /// Thread safe execution of the loaded network
56  Status Execute(const InputTensors& inputTensors,
57  const OutputTensors& outputTensors,
58  IWorkingMemHandle& workingMemHandle);
59 
60  static std::unique_ptr<LoadedNetwork> MakeLoadedNetwork(std::unique_ptr<IOptimizedNetwork> net,
61  std::string& errorMessage,
62  const INetworkProperties& networkProperties,
63  profiling::ProfilingService& profilingService);
64 
65  // NOTE we return by reference as the purpose of this method is only to provide
66  // access to the private m_Profiler and in theory we should not need to increment
67  // the shared_ptr's reference counter
68  const std::shared_ptr<IProfiler>& GetProfiler() const { return m_Profiler; }
69 
70  void FreeWorkingMemory();
71 
72  void RegisterDebugCallback(const DebugCallbackFunction& func);
73 
74  void SendNetworkStructure();
75 
77  {
78  return m_NetworkProperties.m_AsyncEnabled;
79  }
80 
81  profiling::ProfilingGuid GetNetworkGuid();
82 
83 private:
84  using WorkloadFactoryWithMemoryManager =
85  std::pair<IBackendInternal::IWorkloadFactoryPtr, IBackendInternal::IMemoryManagerSharedPtr>;
86 
87  using WorkloadFactoryMap = std::unordered_map<BackendId, WorkloadFactoryWithMemoryManager>;
88 
89  void AllocateWorkingMemory(std::lock_guard<std::mutex>& lock);
90  void AllocateAndExecuteConstantWorkloads();
91 
92  std::unordered_map<LayerGuid, ITensorHandle* > m_ConstantTensorHandles;
93  std::unordered_map<LayerGuid, std::unique_ptr<IWorkload> > m_ConstantWorkloads;
94 
95  LoadedNetwork(std::unique_ptr<IOptimizedNetwork> net,
96  const INetworkProperties& networkProperties,
97  profiling::ProfilingService& profilingService);
98 
99  void EnqueueInput(const BindableLayer& layer, ITensorHandle* tensorHandle, const TensorInfo& tensorInfo);
100 
101  void EnqueueOutput(const BindableLayer& layer, ITensorHandle* tensorHandle, const TensorInfo& tensorInfo);
102 
103  void EnqueueInput(const BindableLayer& layer, const ConstTensor& inputTensor, WorkingMemHandle& handle);
104 
105  void EnqueueOutput(const BindableLayer& layer, const Tensor& outputTensor, WorkingMemHandle& handle);
106 
107  bool Execute(std::unique_ptr<profiling::TimelineUtilityMethods>& timelineUtils,
108  profiling::ProfilingGuid inferenceGuid);
109 
110  const IWorkloadFactory& GetWorkloadFactory(const Layer& layer) const;
111 
112  using BackendPtrMap = std::unordered_map<BackendId, IBackendInternalUniquePtr>;
113 
114  BackendPtrMap m_Backends;
115  WorkloadFactoryMap m_WorkloadFactories;
116 
117  std::unique_ptr<IOptimizedNetwork> m_OptimizedNetwork;
118  std::shared_ptr<IProfiler> m_Profiler;
119 
120  WorkloadQueue m_InputQueue;
121  WorkloadQueue m_WorkloadQueue;
122  WorkloadQueue m_OutputQueue;
123 
124  mutable std::mutex m_WorkingMemMutex;
125 
126  bool m_IsWorkingMemAllocated = false;
127 
128  INetworkProperties m_NetworkProperties;
129 
130  TensorHandleFactoryRegistry m_TensorHandleFactoryRegistry;
131 
132  profiling::ProfilingService& m_ProfilingService;
133 };
134 
135 }
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:360
Copyright (c) 2021 ARM Limited and Contributors.
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:321
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:244
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Definition: Tensor.hpp:319
int NetworkId
Definition: IRuntime.hpp:24
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:361
Status
enumeration
Definition: Types.hpp:29
std::vector< std::unique_ptr< IWorkload > > WorkloadQueue
const std::shared_ptr< IProfiler > & GetProfiler() const