ArmNN
 26.07
RefBackend.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022-2025 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "RefBackend.hpp"
7 #include "RefBackendId.hpp"
8 #include "RefWorkloadFactory.hpp"
9 #include "RefLayerSupport.hpp"
11 
18 
19 namespace armnn
20 {
21 
23 {
24  static const BackendId s_Id{RefBackendId()};
25  return s_Id;
26 }
27 
29  const IBackendInternal::IMemoryManagerSharedPtr& memoryManager) const
30 {
31  return std::make_unique<RefWorkloadFactory>(PolymorphicPointerDowncast<RefMemoryManager>(memoryManager));
32 }
33 
35  class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry) const
36 {
37  auto memoryManager = std::make_shared<RefMemoryManager>();
38 
39  tensorHandleFactoryRegistry.RegisterMemoryManager(memoryManager);
40 
41  std::unique_ptr<RefTensorHandleFactory> factory = std::make_unique<RefTensorHandleFactory>(memoryManager);
42  // Register copy and import factory pair
43  tensorHandleFactoryRegistry.RegisterCopyAndImportFactoryPair(factory->GetId(), factory->GetId());
44  // Register the factory
45  tensorHandleFactoryRegistry.RegisterFactory(std::move(factory));
46 
47  return std::make_unique<RefWorkloadFactory>(PolymorphicPointerDowncast<RefMemoryManager>(memoryManager));
48 }
49 
51 {
52  return IBackendContextPtr{};
53 }
54 
57 {
59 }
60 
62 {
63  return std::make_unique<RefMemoryManager>();
64 }
65 
67 {
68  static ILayerSupportSharedPtr layerSupport{new RefLayerSupport};
69  return layerSupport;
70 }
71 
73  const ModelOptions& modelOptions) const
74 {
75  OptimizationViews optimizationViews(modelOptions);
76 
77  auto it = subgraph.end();
78  std::map<LayerGuid, Layer*> untouched;
79 
80  while (it != subgraph.begin())
81  {
82  --it;
83  Layer& base = *(PolymorphicDowncast<Layer*>(*it));
84  untouched.insert({base.GetGuid(), &base});
85  }
86 
87  it = subgraph.end();
88  while (it != subgraph.begin())
89  {
90  --it;
91  Layer& base = *(PolymorphicDowncast<Layer*>(*it));
92 
93  // Special case to fuse padding into average pooling 2d for quantized datatype.
94  // Required to be done as a backend specific optimization as Neon does not support this special case.
95  if (base.GetType() == LayerType::Pooling2d)
96  {
97  Pooling2dLayer* baseLayer = PolymorphicDowncast<Pooling2dLayer*>(&base);
98  Pooling2dDescriptor poolingDescriptor = baseLayer->GetParameters();
99 
101  {
102  PadLayer* padLayer = PolymorphicDowncast<PadLayer*>(
104  if (padLayer->GetOutputSlot(0).GetNumConnections() == 1 &&
106  poolingDescriptor,
107  padLayer->GetOutputSlot().GetTensorInfo(),
108  true))
109  {
110  FoldPadLayer2d<Pooling2dLayer, Pooling2dDescriptor>(optimizationViews, baseLayer,
111  poolingDescriptor, padLayer);
112  untouched.erase(baseLayer->GetGuid());
113  untouched.erase(padLayer->GetGuid());
114  }
115  }
116  }
117 
118  if (base.GetType() == LayerType::Convolution2d)
119  {
120  Convolution2dLayer* baseLayer = PolymorphicDowncast<Convolution2dLayer*>(&base);
121  Convolution2dDescriptor convDescriptor = baseLayer->GetParameters();
123  {
124  // perform fold pad into conv2d if possible
125  PadLayer* padLayer = PolymorphicDowncast<PadLayer*>(
127  if (padLayer->GetOutputSlot(0).GetNumConnections() == 1 &&
128  optimizations::pad_fold::TryFoldPadIntoLayer2d<Convolution2dDescriptor>(padLayer->GetParameters(),
129  convDescriptor,
130  padLayer->GetOutputSlot().GetTensorInfo()))
131  {
132  FoldPadLayer2d<Convolution2dLayer, Convolution2dDescriptor>(optimizationViews, baseLayer,
133  convDescriptor, padLayer);
134 
135  untouched.erase(baseLayer->GetGuid());
136  untouched.erase(padLayer->GetGuid());
137  }
138  }
139  }
141  {
142  DepthwiseConvolution2dLayer* baseLayer = PolymorphicDowncast<DepthwiseConvolution2dLayer*>(&base);
143  DepthwiseConvolution2dDescriptor convDescriptor = baseLayer->GetParameters();
145  {
146  // perform fold pad into depthwiseconv2d if possible
147  PadLayer* padLayer = PolymorphicDowncast<PadLayer*>(
149  if (padLayer->GetOutputSlot(0).GetNumConnections() == 1 &&
150  optimizations::pad_fold::TryFoldPadIntoLayer2d<DepthwiseConvolution2dDescriptor>(
151  padLayer->GetParameters(),
152  convDescriptor,
153  padLayer->GetOutputSlot().GetTensorInfo()))
154  {
155  FoldPadLayer2d<DepthwiseConvolution2dLayer, DepthwiseConvolution2dDescriptor>(optimizationViews,
156  baseLayer,
157  convDescriptor,
158  padLayer);
159 
160  untouched.erase(baseLayer->GetGuid());
161  untouched.erase(padLayer->GetGuid());
162  }
163  }
164  }
165 
166  // Remove Reshape where possible
167  if (base.GetType() == LayerType::Reshape)
168  {
169  ReshapeLayer* baseLayer = PolymorphicDowncast<ReshapeLayer*>(&base);
170  RemoveReshapeLayer(baseLayer, untouched, optimizationViews);
171  }
172  }
173 
174  if (optimizationViews.GetSubstitutions().empty() && optimizationViews.GetDeletedSubgraphs().empty())
175  {
176  optimizationViews.AddUntouchedSubgraph(SubgraphView(subgraph));
177  }
178  else
179  {
180  ReportUntouchedLayers(optimizationViews, untouched);
181  }
182 
183  return optimizationViews;
184 }
185 
186 std::vector<ITensorHandleFactory::FactoryId> RefBackend::GetHandleFactoryPreferences() const
187 {
188  return std::vector<ITensorHandleFactory::FactoryId> { RefTensorHandleFactory::GetIdStatic() };
189 }
190 
192 {
193  auto memoryManager = std::make_shared<RefMemoryManager>();
194 
195  registry.RegisterMemoryManager(memoryManager);
196 
197  std::unique_ptr<RefTensorHandleFactory> factory = std::make_unique<RefTensorHandleFactory>(memoryManager);
198 
199  // Register copy and import factory pair
200  registry.RegisterCopyAndImportFactoryPair(factory->GetId(), factory->GetId());
201  // Register the factory
202  registry.RegisterFactory(std::move(factory));
203 }
204 
205 std::unique_ptr<ICustomAllocator> RefBackend::GetDefaultAllocator() const
206 {
207  return std::make_unique<DefaultAllocator>();
208 }
209 
210 } // namespace armnn
This layer represents a convolution 2d operation.
This layer represents a depthwise convolution 2d operation.
std::shared_ptr< ILayerSupport > ILayerSupportSharedPtr
std::unique_ptr< IMemoryManager > IMemoryManagerUniquePtr
std::unique_ptr< arm::pipe::IBackendProfiling > IBackendProfilingPtr
std::shared_ptr< IMemoryManager > IMemoryManagerSharedPtr
std::unique_ptr< IWorkloadFactory > IWorkloadFactoryPtr
std::unique_ptr< IBackendContext > IBackendContextPtr
std::shared_ptr< arm::pipe::IBackendProfilingContext > IBackendProfilingContextPtr
This is the bridge between backend and backend profiling we'll keep it in the backend namespace.
const OutputSlot * GetConnectedOutputSlot() const
Definition: Layer.hpp:56
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:339
LayerGuid GetGuid() const final
Returns the unique id of the layer.
Definition: Layer.hpp:343
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition: Layer.hpp:286
const Parameters & GetParameters() const override
If the layer has a descriptor return it.
void AddUntouchedSubgraph(SubgraphView &&subgraph)
const Substitutions & GetSubstitutions() const
const Subgraphs & GetDeletedSubgraphs() const
unsigned int GetNumConnections() const override
Definition: Layer.hpp:158
Layer & GetOwningLayer() const
Definition: Layer.hpp:132
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:100
This layer represents a pad operation.
Definition: PadLayer.hpp:15
This layer represents a pooling 2d operation.
static const BackendId & GetIdStatic()
Definition: RefBackend.cpp:22
OptimizationViews OptimizeSubgraphView(const SubgraphView &subgraph, const ModelOptions &modelOptions) const override
Definition: RefBackend.cpp:72
void RegisterTensorHandleFactories(class TensorHandleFactoryRegistry &registry) override
(Optional) Register TensorHandleFactories Either this method or CreateMemoryManager() and IWorkloadFa...
Definition: RefBackend.cpp:191
std::vector< ITensorHandleFactory::FactoryId > GetHandleFactoryPreferences() const override
(Optional) Returns a vector of supported TensorHandleFactory ids in preference order.
Definition: RefBackend.cpp:186
IBackendInternal::IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions &) const override
Create the runtime context of the backend.
Definition: RefBackend.cpp:50
IBackendInternal::IWorkloadFactoryPtr CreateWorkloadFactory(const IBackendInternal::IMemoryManagerSharedPtr &memoryManager=nullptr) const override
Definition: RefBackend.cpp:28
IBackendInternal::ILayerSupportSharedPtr GetLayerSupport() const override
Definition: RefBackend.cpp:66
IBackendInternal::IMemoryManagerUniquePtr CreateMemoryManager() const override
Definition: RefBackend.cpp:61
IBackendInternal::IBackendProfilingContextPtr CreateBackendProfilingContext(const IRuntime::CreationOptions &creationOptions, IBackendProfilingPtr &backendProfiling) override
Create context specifically used for profiling interaction from backends.
Definition: RefBackend.cpp:55
std::unique_ptr< ICustomAllocator > GetDefaultAllocator() const override
Returns the default memory allocator for the backend.
Definition: RefBackend.cpp:205
static const FactoryId & GetIdStatic()
This layer represents a reshape operation.
The SubgraphView class represents a subgraph of a Graph.
IConnectableLayerIterator begin()
IConnectableLayerIterator end()
void RegisterFactory(std::unique_ptr< ITensorHandleFactory > allocator)
Register a TensorHandleFactory and transfer ownership.
void RegisterMemoryManager(std::shared_ptr< IMemoryManager > memoryManger)
Register a memory manager with shared ownership.
void RegisterCopyAndImportFactoryPair(ITensorHandleFactory::FactoryId copyFactoryId, ITensorHandleFactory::FactoryId importFactoryId)
Register a pair of TensorHandleFactory Id for Memory Copy and TensorHandleFactory Id for Memory Impor...
bool TryFoldPadIntoLayer2d(const PadDescriptor &padDescriptor, Descriptor &layerDescriptor, const TensorInfo &tensorInfo)
Copyright (c) 2021 ARM Limited and Contributors.
void ReportUntouchedLayers(OptimizationViews &optimizationViews, std::map< LayerGuid, Layer * > untouched)
std::vector< BackendOptions > ModelOptions
void RemoveReshapeLayer(ReshapeLayer *baseLayer, std::map< LayerGuid, Layer * > &untouched, OptimizationViews &optimizationViews)
constexpr const char * RefBackendId()
A Convolution2dDescriptor for the Convolution2dLayer.
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
A Pooling2dDescriptor for the Pooling2dLayer.