Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
David Beck | b4540be | 2018-09-24 13:18:27 +0100 | [diff] [blame] | 7 | #include <armnn/Tensor.hpp> |
| 8 | #include <armnn/Types.hpp> |
| 9 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 10 | #include "Network.hpp" |
| 11 | #include "LayerFwd.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 12 | #include "Profiling.hpp" |
David Beck | b4540be | 2018-09-24 13:18:27 +0100 | [diff] [blame] | 13 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 14 | #include <armnn/backends/IBackendInternal.hpp> |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 15 | #include <backendsCommon/TensorHandleFactoryRegistry.hpp> |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 16 | #include <backendsCommon/Workload.hpp> |
| 17 | #include <backendsCommon/WorkloadFactory.hpp> |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 18 | #include <ProfilingService.hpp> |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 19 | #include <TimelineUtilityMethods.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 20 | |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 21 | #include <mutex> |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 22 | #include <condition_variable> |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 23 | #include <unordered_map> |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 24 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 25 | namespace cl |
| 26 | { |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 27 | class Context; |
| 28 | class CommandQueue; |
| 29 | class Device; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | namespace armnn |
| 33 | { |
| 34 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 35 | class LoadedNetwork |
| 36 | { |
| 37 | public: |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 38 | using WorkloadQueue = std::vector<std::unique_ptr<IWorkload>>; |
| 39 | |
| 40 | using ExecutionTuple = std::tuple<InputTensors, |
| 41 | OutputTensors, |
| 42 | std::shared_ptr<IAsyncExecutionCallback>>; |
| 43 | |
| 44 | using ExecutionQueue = std::queue<std::shared_ptr<ExecutionTuple>>; |
| 45 | |
| 46 | ~LoadedNetwork() |
| 47 | { |
| 48 | FreeWorkingMemory(); |
| 49 | TerminateThreadPool(); |
| 50 | } |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 51 | |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 52 | /// Create a new unique WorkingMemHandle object. Create multiple handles if you wish to have |
| 53 | /// overlapped Execution by calling this function from different threads. |
| 54 | std::unique_ptr<IWorkingMemHandle> CreateWorkingMemHandle(NetworkId networkId); |
| 55 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | TensorInfo GetInputTensorInfo(LayerBindingId layerId) const; |
| 57 | TensorInfo GetOutputTensorInfo(LayerBindingId layerId) const; |
| 58 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 59 | /// Single thread execution of the loaded network |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 60 | Status EnqueueWorkload(const InputTensors& inputTensors, const OutputTensors& outputTensors); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 61 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 62 | /// Thread safe execution of the loaded network |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 63 | Status Execute(const InputTensors& inputTensors, |
| 64 | const OutputTensors& outputTensors, |
| 65 | IWorkingMemHandle& workingMemHandle); |
| 66 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 67 | /// Schedule an asynchronous execution on the loaded network |
| 68 | void Schedule(const InputTensors& inputTensors, |
| 69 | const OutputTensors& outputTensors, |
| 70 | const QosExecPriority priority, |
| 71 | std::shared_ptr<IAsyncExecutionCallback> cb); |
| 72 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 73 | static std::unique_ptr<LoadedNetwork> MakeLoadedNetwork(std::unique_ptr<IOptimizedNetwork> net, |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 74 | std::string& errorMessage, |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 75 | const INetworkProperties& networkProperties, |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 76 | profiling::ProfilingService& profilingService, |
| 77 | const NetworkId networkIdOut); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 78 | |
| 79 | // NOTE we return by reference as the purpose of this method is only to provide |
| 80 | // access to the private m_Profiler and in theory we should not need to increment |
| 81 | // the shared_ptr's reference counter |
Francis Murtagh | 33199c2 | 2021-02-15 10:11:28 +0000 | [diff] [blame] | 82 | const std::shared_ptr<IProfiler>& GetProfiler() const { return m_Profiler; } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 83 | |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 84 | void FreeWorkingMemory(); |
| 85 | |
Nattapat Chaimanowong | 6e94820 | 2019-03-22 14:01:46 +0000 | [diff] [blame] | 86 | void RegisterDebugCallback(const DebugCallbackFunction& func); |
| 87 | |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 88 | void SendNetworkStructure(); |
| 89 | |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 90 | bool IsAsyncEnabled() |
| 91 | { |
| 92 | return m_NetworkProperties.m_AsyncEnabled; |
| 93 | } |
| 94 | |
Jim Flynn | f771321 | 2020-07-14 09:50:59 +0100 | [diff] [blame] | 95 | profiling::ProfilingGuid GetNetworkGuid(); |
| 96 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 97 | private: |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 98 | using WorkloadFactoryWithMemoryManager = |
| 99 | std::pair<IBackendInternal::IWorkloadFactoryPtr, IBackendInternal::IMemoryManagerSharedPtr>; |
| 100 | |
| 101 | using WorkloadFactoryMap = std::unordered_map<BackendId, WorkloadFactoryWithMemoryManager>; |
| 102 | |
Derek Lamberti | a08d29b | 2020-06-19 14:33:05 +0100 | [diff] [blame] | 103 | void AllocateWorkingMemory(std::lock_guard<std::mutex>& lock); |
Finn Williams | 0109794 | 2021-04-26 12:06:34 +0100 | [diff] [blame] | 104 | void AllocateAndExecuteConstantWorkloads(); |
| 105 | |
| 106 | std::unordered_map<LayerGuid, ITensorHandle* > m_ConstantTensorHandles; |
| 107 | std::unordered_map<LayerGuid, std::unique_ptr<IWorkload> > m_ConstantWorkloads; |
Matthew Bentham | 2a326b5 | 2019-03-19 10:11:01 +0000 | [diff] [blame] | 108 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 109 | LoadedNetwork(std::unique_ptr<IOptimizedNetwork> net, |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 110 | const INetworkProperties& networkProperties, |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 111 | profiling::ProfilingService& profilingService, |
| 112 | const NetworkId networkIdOut); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 113 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 114 | void EnqueueInput(const BindableLayer& layer, ITensorHandle* tensorHandle, const TensorInfo& tensorInfo); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 115 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 116 | void EnqueueOutput(const BindableLayer& layer, ITensorHandle* tensorHandle, const TensorInfo& tensorInfo); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 117 | |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 118 | void EnqueueInput(const BindableLayer& layer, const ConstTensor& inputTensor, WorkingMemHandle& handle); |
| 119 | |
| 120 | void EnqueueOutput(const BindableLayer& layer, const Tensor& outputTensor, WorkingMemHandle& handle); |
| 121 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 122 | void ProcessExecPriorities(std::unique_ptr<IWorkingMemHandle> workingMemHandle); |
| 123 | |
David Monahan | 6198fe0 | 2019-12-02 08:35:43 +0000 | [diff] [blame] | 124 | bool Execute(std::unique_ptr<profiling::TimelineUtilityMethods>& timelineUtils, |
| 125 | profiling::ProfilingGuid inferenceGuid); |
| 126 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 127 | void CreateThreadPool(std::size_t numThreads); |
| 128 | |
| 129 | void TerminateThreadPool() noexcept; |
| 130 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 131 | const IWorkloadFactory& GetWorkloadFactory(const Layer& layer) const; |
| 132 | |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 133 | using BackendPtrMap = std::unordered_map<BackendId, IBackendInternalUniquePtr>; |
Aron Virginas-Tar | 5caf907 | 2018-11-14 18:35:18 +0000 | [diff] [blame] | 134 | |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 135 | BackendPtrMap m_Backends; |
| 136 | WorkloadFactoryMap m_WorkloadFactories; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 137 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 138 | std::unique_ptr<IOptimizedNetwork> m_OptimizedNetwork; |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 139 | std::shared_ptr<IProfiler> m_Profiler; |
| 140 | |
| 141 | WorkloadQueue m_InputQueue; |
| 142 | WorkloadQueue m_WorkloadQueue; |
| 143 | WorkloadQueue m_OutputQueue; |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 144 | |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 145 | mutable std::mutex m_WorkingMemMutex; |
Derek Lamberti | 03614f6 | 2018-10-02 15:52:46 +0100 | [diff] [blame] | 146 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 147 | bool m_IsWorkingMemAllocated = false; |
| 148 | |
| 149 | std::vector<std::unique_ptr<std::thread>> m_Threads; |
| 150 | std::stack<IWorkingMemHandle> m_WorkingMemHandles; |
| 151 | |
| 152 | ExecutionQueue m_HighPriorityQueue; |
| 153 | ExecutionQueue m_MediumPriorityQueue; |
| 154 | ExecutionQueue m_LowPriorityQueue; |
| 155 | |
| 156 | // Condition Variables require mutex which will guard the shared state. |
| 157 | // Has an event happened? Stop signal for example |
| 158 | std::condition_variable m_ThreadPoolEvent; |
| 159 | std::mutex m_ThreadPoolMutex; |
| 160 | |
| 161 | // The shared state for conditional variable |
| 162 | bool m_TerminatePool = false; |
| 163 | |
Mike Kelly | 55a8ffd | 2021-04-07 20:10:49 +0100 | [diff] [blame] | 164 | INetworkProperties m_NetworkProperties; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 165 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 166 | const NetworkId m_NetworkId; |
| 167 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 168 | TensorHandleFactoryRegistry m_TensorHandleFactoryRegistry; |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 169 | |
Keith Davis | e813d67 | 2021-04-22 10:10:34 +0100 | [diff] [blame^] | 170 | profiling::ProfilingService& m_ProfilingService; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | } |