ArmNN
 22.05.01
WorkingMemHandle.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "Layer.hpp"
9 #include "Network.hpp"
10 #include "WorkingMemDescriptor.hpp"
11 
13 #include <armnn/Tensor.hpp>
14 
15 #include <unordered_map>
16 #include <mutex>
18 
19 namespace armnn
20 {
21 
22 namespace experimental
23 {
24 
25 
26 class WorkingMemHandle final : public IWorkingMemHandle
27 {
28 
29 public:
31  {
33 
34  std::vector<std::pair<unsigned int, unsigned int>> m_InputSlotCoords;
35  };
36 
38  {
39  std::vector<LayerBindingId> m_LayerBindingIds;
40 
41  std::pair<unsigned int, unsigned int> m_OutputSlotCoords;
42  std::vector<std::pair<unsigned int, unsigned int>> m_InputSlotCoords;
43  };
44 
45  WorkingMemHandle(NetworkId networkId) : m_NetworkId(networkId){}
46 
47  WorkingMemHandle(NetworkId networkId,
48  std::vector<InputMemDescriptorCoords> inputLayerInfo,
49  std::vector<OutputMemDescriptorCoords> outputLayerInfo,
50  std::vector<WorkingMemDescriptor> workingMemDescriptors,
51  std::unordered_map<LayerGuid, WorkingMemDescriptor> workingMemDescriptorMap,
52  std::unique_ptr<MemoryManager> memoryManager,
53  std::vector<std::pair<std::shared_ptr<TensorMemory>, MemorySource>> tensorMemory,
54  std::vector<std::unique_ptr<ITensorHandle>> managedTensorHandles,
55  std::vector<std::unique_ptr<ITensorHandle>> unmanagedTensorHandles);
56 
58  { Free(); }
59 
61  {
62  return m_NetworkId;
63  }
64 
65  /// Allocate the backing memory required for execution. If this is not called, then allocation will be
66  /// deferred to execution time.
67  void Allocate() override;
68 
69  /// Free the backing memory required for execution.
70  void Free() override;
71 
72  /// IsAllocated returns true if the backing memory is currently allocated.
73  bool IsAllocated() override
74  {
75  return m_IsAllocated;
76  }
77 
78  /// Get the WorkingMemDescriptor for a Layer.
80  {
81  auto result = m_WorkingMemDescriptorMap.find(id);
82  ARMNN_ASSERT(result != m_WorkingMemDescriptorMap.end());
83  return result->second;
84  }
85 
86  /// Get the WorkingMemDescriptor at an index. The WorkingMemDescriptors are stored in the same order as
87  /// the Workloads in a topologically sorted graph.
89  {
90  return m_WorkingMemDescriptors[id];
91  }
92 
94  {
95  return m_InputHandleMap.at(layerBindingId);
96  };
97 
99  {
100  return m_OutputHandleMap.at(layerBindingId);
101  };
102 
103  const std::vector<std::vector<ITensorHandle*>::iterator>& GetInputConnections(LayerBindingId layerBindingId) const
104  {
105  return m_InputConnectionMap.at(layerBindingId);
106  };
107 
108  const std::vector<std::vector<ITensorHandle*>::iterator>& GetOutputConnection(LayerBindingId layerBindingId) const
109  {
110  return m_OutputConnectionMap.at(layerBindingId);
111  };
112 
113  void MemSyncOutputs();
114 
115  std::vector<LayerBindingId>& GetBindingIdVector()
116  {
117  return m_BindingIdVec;
118  };
119 
120  void ValidateBindingIds();
121 
122 private:
123  using DifferenceType = std::vector<ITensorHandle*>::difference_type;
124  NetworkId m_NetworkId;
125 
126  std::unordered_map<LayerBindingId, ITensorHandle*> m_InputHandleMap;
127  std::unordered_map<LayerBindingId, ITensorHandle*> m_OutputHandleMap;
128  std::unordered_map<LayerBindingId, std::vector<std::vector<ITensorHandle*>::iterator>> m_InputConnectionMap;
129  std::unordered_map<LayerBindingId, std::vector<std::vector<ITensorHandle*>::iterator>> m_OutputConnectionMap;
130 
131  std::vector<WorkingMemDescriptor> m_WorkingMemDescriptors;
132  std::unordered_map<LayerGuid, WorkingMemDescriptor> m_WorkingMemDescriptorMap;
133 
134  std::unique_ptr<MemoryManager> m_MemoryManager;
135 
136  // Memory to be imported into the tensorHandles after allocation
137  std::vector<std::pair<std::shared_ptr<TensorMemory>, MemorySource>> m_TensorMemory;
138 
139 
140  // Tensors that will need to be allocated internally within armnn
141  std::vector<std::unique_ptr<ITensorHandle>> m_ManagedTensorHandles;
142 
143  // Tensors that will be allocated externally by the user
144  std::vector<std::unique_ptr<ITensorHandle>> m_UnmanagedTensorHandles;
145 
146  std::unordered_map<LayerBindingId, bool> m_InputValidationMap;
147  std::unordered_map<LayerBindingId, bool> m_OutputValidationMap;
148 
149  std::vector<LayerBindingId> m_BindingIdVec;
150 
151  DifferenceType m_InputSize;
152 
153  bool m_IsAllocated;
154 };
155 
156 } // end experimental namespace
157 
158 } // end armnn namespace
std::vector< std::pair< unsigned int, unsigned int > > m_InputSlotCoords
WorkingMemDescriptor & GetWorkingMemDescriptorAt(unsigned int id) override
Get the WorkingMemDescriptor at an index.
ITensorHandle * GetOutputHandle(LayerBindingId layerBindingId) const
std::vector< std::pair< unsigned int, unsigned int > > m_InputSlotCoords
Copyright (c) 2021 ARM Limited and Contributors.
NetworkId GetNetworkId() override
Returns the NetworkId of the Network that this IWorkingMemHandle works with.
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:290
const std::vector< std::vector< ITensorHandle * >::iterator > & GetOutputConnection(LayerBindingId layerBindingId) const
WorkingMemDescriptor & GetWorkingMemDescriptor(LayerGuid id) override
Get the WorkingMemDescriptor for a Layer.
int NetworkId
Definition: IRuntime.hpp:27
const std::vector< std::vector< ITensorHandle * >::iterator > & GetInputConnections(LayerBindingId layerBindingId) const
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
void Allocate() override
Allocate the backing memory required for execution.
arm::pipe::ProfilingGuid LayerGuid
Define LayerGuid type.
Definition: Types.hpp:26
std::vector< LayerBindingId > & GetBindingIdVector()
bool IsAllocated() override
IsAllocated returns true if the backing memory is currently allocated.
MemorySource
Define the Memory Source to reduce copies.
Definition: Types.hpp:230
ITensorHandle * GetInputHandle(LayerBindingId layerBindingId) const
void Free() override
Free the backing memory required for execution.