blob: b018654288d742ec0a71696b1217299acdecbd6d [file] [log] [blame]
arovir014424b0a2018-10-04 10:46:04 +01001//
Mike Kelly3ec30772023-03-08 13:47:17 +00002// Copyright © 2017-2023 Arm Ltd. All rights reserved.
arovir014424b0a2018-10-04 10:46:04 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "ClBackend.hpp"
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +01007#include "ClBackendContext.hpp"
David Monahan6642b8a2021-11-04 16:31:46 +00008#include "ClBackendDefaultAllocator.hpp"
David Beck3e9e1152018-10-17 14:17:50 +01009#include "ClBackendId.hpp"
Sadik Armagan045f6be2020-09-10 13:37:32 +010010#include "ClBackendModelContext.hpp"
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +010011#include "ClImportTensorHandleFactory.hpp"
David Beck111b5d92018-11-12 14:59:37 +000012#include "ClLayerSupport.hpp"
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010013#include "ClTensorHandleFactory.hpp"
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +010014#include "ClWorkloadFactory.hpp"
arovir01a0944792018-10-11 15:00:58 +010015
Matteo Martincighc601aa62019-10-29 15:03:22 +000016#include <armnn/BackendRegistry.hpp>
Mike Kelly07810fc2020-11-12 10:58:48 +000017#include <armnn/Descriptors.hpp>
Matteo Martincighc601aa62019-10-29 15:03:22 +000018
Mike Kelly07810fc2020-11-12 10:58:48 +000019#include <aclCommon/ArmComputeSubgraphUtils.hpp>
20#include <aclCommon/ArmComputeUtils.hpp>
Aron Virginas-Tar56055192018-11-12 18:10:43 +000021#include <aclCommon/BaseMemoryManager.hpp>
22
Matteo Martincighe5b8eb92019-11-28 15:45:42 +000023#include <armnn/backends/IBackendContext.hpp>
24#include <armnn/backends/IMemoryManager.hpp>
Jan Eilers3c9e0452020-04-10 13:00:44 +010025#include <armnn/utility/PolymorphicDowncast.hpp>
26
Mike Kelly07810fc2020-11-12 10:58:48 +000027#include "workloads/ClAdditionWorkload.hpp"
28#include "workloads/ClBatchNormalizationFloatWorkload.hpp"
29#include "workloads/ClConvolution2dWorkload.hpp"
30#include "workloads/ClDepthwiseConvolutionWorkload.hpp"
Teresa Charline11e63d2021-04-21 12:56:45 +010031#include "workloads/ClDivisionWorkload.hpp"
Mike Kelly07810fc2020-11-12 10:58:48 +000032#include "workloads/ClFullyConnectedWorkload.hpp"
33#include "workloads/ClMultiplicationWorkload.hpp"
Matthew Sloyan5fc0fd62021-05-03 12:22:03 +010034#include "workloads/ClReduceWorkload.hpp"
Mike Kelly07810fc2020-11-12 10:58:48 +000035#include "workloads/ClSubtractionWorkload.hpp"
36
David Beck263e3492018-11-09 14:46:40 +000037#include <Optimizer.hpp>
arovir014424b0a2018-10-04 10:46:04 +010038
Mike Kelly07810fc2020-11-12 10:58:48 +000039#include <arm_compute/core/Types.h>
Aron Virginas-Tar56055192018-11-12 18:10:43 +000040#include <arm_compute/runtime/CL/CLBufferAllocator.h>
41
arovir014424b0a2018-10-04 10:46:04 +010042namespace armnn
43{
44
David Beck3cc9a622018-10-12 10:38:31 +010045const BackendId& ClBackend::GetIdStatic()
arovir014424b0a2018-10-04 10:46:04 +010046{
David Beck3e9e1152018-10-17 14:17:50 +010047 static const BackendId s_Id{ClBackendId()};
arovir014424b0a2018-10-04 10:46:04 +010048 return s_Id;
49}
50
Aron Virginas-Tar56055192018-11-12 18:10:43 +000051IBackendInternal::IMemoryManagerUniquePtr ClBackend::CreateMemoryManager() const
arovir014424b0a2018-10-04 10:46:04 +010052{
Jan Eilersc1c872f2021-07-22 13:17:04 +010053 if (m_UsingCustomAllocator)
54 {
55 return std::make_unique<ClMemoryManager>(m_CustomAllocator);
56 }
Aron Virginas-Tar56055192018-11-12 18:10:43 +000057 return std::make_unique<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>());
58}
59
60IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory(
61 const IBackendInternal::IMemoryManagerSharedPtr& memoryManager) const
62{
63 return std::make_unique<ClWorkloadFactory>(
Jan Eilers3c9e0452020-04-10 13:00:44 +010064 PolymorphicPointerDowncast<ClMemoryManager>(memoryManager));
arovir014424b0a2018-10-04 10:46:04 +010065}
66
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010067IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory(
Sadik Armagan04a72972020-09-14 15:44:18 +010068 const IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const ModelOptions& modelOptions) const
69{
70 return std::make_unique<ClWorkloadFactory>(
71 PolymorphicPointerDowncast<ClMemoryManager>(memoryManager), CreateBackendSpecificModelContext(modelOptions));
72}
73
74IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory(
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010075 TensorHandleFactoryRegistry& registry) const
76{
Jan Eilersc1c872f2021-07-22 13:17:04 +010077 std::shared_ptr<ClMemoryManager> memoryManager;
78 if (m_UsingCustomAllocator)
79 {
80 memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator);
81 }
82 else
83 {
84 memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>());
85 }
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010086
Narumol Prangnawaratd12b4072022-01-17 18:03:14 +000087 std::unique_ptr<ITensorHandleFactory> factory = std::make_unique<ClTensorHandleFactory>(memoryManager);
88 std::unique_ptr<ITensorHandleFactory> importFactory = std::make_unique<ClImportTensorHandleFactory>(
89 static_cast<MemorySourceFlags>(MemorySource::Malloc), static_cast<MemorySourceFlags>(MemorySource::Malloc));
90
91 registry.RegisterCopyAndImportFactoryPair(factory->GetId(), importFactory->GetId());
92 registry.RegisterCopyAndImportFactoryPair(importFactory->GetId(), factory->GetId());
93
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010094 registry.RegisterMemoryManager(memoryManager);
Narumol Prangnawaratd12b4072022-01-17 18:03:14 +000095 registry.RegisterFactory(std::move(factory));
96 registry.RegisterFactory(std::move(importFactory));
Jan Eilerse9f0f0f2019-08-16 10:28:37 +010097
98 return std::make_unique<ClWorkloadFactory>(
Jan Eilers3c9e0452020-04-10 13:00:44 +010099 PolymorphicPointerDowncast<ClMemoryManager>(memoryManager));
Jan Eilerse9f0f0f2019-08-16 10:28:37 +0100100}
101
Sadik Armagan04a72972020-09-14 15:44:18 +0100102IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory(
103 TensorHandleFactoryRegistry& registry, const ModelOptions& modelOptions) const
104{
Jan Eilersc1c872f2021-07-22 13:17:04 +0100105 std::shared_ptr<ClMemoryManager> memoryManager;
106 if (m_UsingCustomAllocator)
107 {
108 memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator);
109 }
110 else
111 {
112 memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>());
113 }
Sadik Armagan04a72972020-09-14 15:44:18 +0100114
Narumol Prangnawaratd12b4072022-01-17 18:03:14 +0000115 std::unique_ptr<ITensorHandleFactory> factory = std::make_unique<ClTensorHandleFactory>(memoryManager);
116 std::unique_ptr<ITensorHandleFactory> importFactory = std::make_unique<ClImportTensorHandleFactory>(
117 static_cast<MemorySourceFlags>(MemorySource::Malloc), static_cast<MemorySourceFlags>(MemorySource::Malloc));
118
119 registry.RegisterCopyAndImportFactoryPair(factory->GetId(), importFactory->GetId());
120 registry.RegisterCopyAndImportFactoryPair(importFactory->GetId(), factory->GetId());
121
Sadik Armagan04a72972020-09-14 15:44:18 +0100122 registry.RegisterMemoryManager(memoryManager);
Narumol Prangnawaratd12b4072022-01-17 18:03:14 +0000123 registry.RegisterFactory(std::move(factory));
124 registry.RegisterFactory(std::move(importFactory));
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +0100125
126 return std::make_unique<ClWorkloadFactory>(
127 PolymorphicPointerDowncast<ClMemoryManager>(memoryManager), CreateBackendSpecificModelContext(modelOptions));
128}
129
130IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory(
131 TensorHandleFactoryRegistry& registry,
132 const ModelOptions& modelOptions,
133 MemorySourceFlags inputFlags,
134 MemorySourceFlags outputFlags) const
135{
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000136 // To allow force import if inputFlags/outputFlags are Undefined, set it as Malloc
137 if (inputFlags == static_cast<MemorySourceFlags>(MemorySource::Undefined))
138 {
139 inputFlags = static_cast<MemorySourceFlags>(MemorySource::Malloc);
140 }
141 if (outputFlags == static_cast<MemorySourceFlags>(MemorySource::Undefined))
142 {
143 outputFlags = static_cast<MemorySourceFlags>(MemorySource::Malloc);
144 }
Jan Eilersc1c872f2021-07-22 13:17:04 +0100145 std::shared_ptr<ClMemoryManager> memoryManager;
146 if (m_UsingCustomAllocator)
147 {
148 memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator);
149 }
150 else
151 {
152 memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>());
153 }
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +0100154
Narumol Prangnawaratd12b4072022-01-17 18:03:14 +0000155 std::unique_ptr<ITensorHandleFactory> factory = std::make_unique<ClTensorHandleFactory>(memoryManager);
156 std::unique_ptr<ITensorHandleFactory> importFactory = std::make_unique<ClImportTensorHandleFactory>(
157 inputFlags, outputFlags);
158
159 registry.RegisterCopyAndImportFactoryPair(factory->GetId(), importFactory->GetId());
160 registry.RegisterCopyAndImportFactoryPair(importFactory->GetId(), factory->GetId());
161
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +0100162 registry.RegisterMemoryManager(memoryManager);
Narumol Prangnawaratd12b4072022-01-17 18:03:14 +0000163 registry.RegisterFactory(std::move(factory));
164 registry.RegisterFactory(std::move(importFactory));
Sadik Armagan04a72972020-09-14 15:44:18 +0100165
166 return std::make_unique<ClWorkloadFactory>(
167 PolymorphicPointerDowncast<ClMemoryManager>(memoryManager), CreateBackendSpecificModelContext(modelOptions));
168}
169
Jan Eilerse9f0f0f2019-08-16 10:28:37 +0100170std::vector<ITensorHandleFactory::FactoryId> ClBackend::GetHandleFactoryPreferences() const
171{
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +0100172 return std::vector<ITensorHandleFactory::FactoryId> {ClTensorHandleFactory::GetIdStatic(),
173 ClImportTensorHandleFactory::GetIdStatic()};
Jan Eilerse9f0f0f2019-08-16 10:28:37 +0100174}
175
176void ClBackend::RegisterTensorHandleFactories(TensorHandleFactoryRegistry& registry)
177{
Jan Eilersc1c872f2021-07-22 13:17:04 +0100178 std::shared_ptr<ClMemoryManager> memoryManager;
179 if (m_UsingCustomAllocator)
180 {
181 memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator);
182 }
183 else
184 {
185 memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>());
186 }
Jan Eilerse9f0f0f2019-08-16 10:28:37 +0100187
Narumol Prangnawaratd12b4072022-01-17 18:03:14 +0000188 std::unique_ptr<ITensorHandleFactory> factory = std::make_unique<ClTensorHandleFactory>(memoryManager);
189 std::unique_ptr<ITensorHandleFactory> importFactory = std::make_unique<ClImportTensorHandleFactory>(
190 static_cast<MemorySourceFlags>(MemorySource::Malloc), static_cast<MemorySourceFlags>(MemorySource::Malloc));
191
192 registry.RegisterCopyAndImportFactoryPair(factory->GetId(), importFactory->GetId());
193 registry.RegisterCopyAndImportFactoryPair(importFactory->GetId(), factory->GetId());
194
Jan Eilersc1c872f2021-07-22 13:17:04 +0100195 registry.RegisterMemoryManager(memoryManager);
Narumol Prangnawaratd12b4072022-01-17 18:03:14 +0000196 registry.RegisterFactory(std::move(factory));
197 registry.RegisterFactory(std::move(importFactory));
198
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +0100199}
200
201void ClBackend::RegisterTensorHandleFactories(TensorHandleFactoryRegistry& registry,
202 MemorySourceFlags inputFlags,
203 MemorySourceFlags outputFlags)
204{
Narumol Prangnawarate2af6f42022-01-28 17:59:18 +0000205 // To allow force import if inputFlags/outputFlags are Undefined, set it as Malloc
206 if (inputFlags == static_cast<MemorySourceFlags>(MemorySource::Undefined))
207 {
208 inputFlags = static_cast<MemorySourceFlags>(MemorySource::Malloc);
209 }
210 if (outputFlags == static_cast<MemorySourceFlags>(MemorySource::Undefined))
211 {
212 outputFlags = static_cast<MemorySourceFlags>(MemorySource::Malloc);
213 }
Jan Eilersc1c872f2021-07-22 13:17:04 +0100214 std::shared_ptr<ClMemoryManager> memoryManager;
215 if (m_UsingCustomAllocator)
216 {
217 memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator);
218 }
219 else
220 {
221 memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>());
222 }
Narumol Prangnawarate5f0b242021-05-07 17:52:36 +0100223
Narumol Prangnawaratd12b4072022-01-17 18:03:14 +0000224 std::unique_ptr<ITensorHandleFactory> factory = std::make_unique<ClTensorHandleFactory>(memoryManager);
225 std::unique_ptr<ITensorHandleFactory> importFactory = std::make_unique<ClImportTensorHandleFactory>(
226 inputFlags, outputFlags);
227
228 registry.RegisterCopyAndImportFactoryPair(factory->GetId(), importFactory->GetId());
229 registry.RegisterCopyAndImportFactoryPair(importFactory->GetId(), factory->GetId());
230
Jan Eilersc1c872f2021-07-22 13:17:04 +0100231 registry.RegisterMemoryManager(memoryManager);
Narumol Prangnawaratd12b4072022-01-17 18:03:14 +0000232 registry.RegisterFactory(std::move(factory));
233 registry.RegisterFactory(std::move(importFactory));
Jan Eilerse9f0f0f2019-08-16 10:28:37 +0100234}
235
Sadik Armagan045f6be2020-09-10 13:37:32 +0100236IBackendInternal::IBackendContextPtr ClBackend::CreateBackendContext(const IRuntime::CreationOptions& options) const
David Beck1b61be52018-11-08 09:19:14 +0000237{
238 return IBackendContextPtr{new ClBackendContext{options}};
239}
240
Colm Donelane49755b2020-01-29 15:22:43 +0000241IBackendInternal::IBackendProfilingContextPtr ClBackend::CreateBackendProfilingContext(
Colm Donelan1aff3932020-02-05 17:48:59 +0000242 const IRuntime::CreationOptions&, IBackendProfilingPtr&)
Colm Donelane49755b2020-01-29 15:22:43 +0000243{
244 return IBackendProfilingContextPtr{};
245}
246
Sadik Armagan045f6be2020-09-10 13:37:32 +0100247IBackendInternal::IBackendSpecificModelContextPtr ClBackend::CreateBackendSpecificModelContext(
248 const ModelOptions& modelOptions) const
249{
250 return IBackendSpecificModelContextPtr{new ClBackendModelContext{modelOptions}};
251}
252
David Beck111b5d92018-11-12 14:59:37 +0000253IBackendInternal::ILayerSupportSharedPtr ClBackend::GetLayerSupport() const
254{
Sadik Armagan045f6be2020-09-10 13:37:32 +0100255 static ILayerSupportSharedPtr layerSupport
256 {
257 new ClLayerSupport(IBackendInternal::IBackendSpecificModelContextPtr{})
258 };
259 return layerSupport;
260}
261
262IBackendInternal::ILayerSupportSharedPtr ClBackend::GetLayerSupport(const ModelOptions& modelOptions) const
263{
264 static ILayerSupportSharedPtr layerSupport
265 {
266 new ClLayerSupport(CreateBackendSpecificModelContext(modelOptions))
267 };
David Beck111b5d92018-11-12 14:59:37 +0000268 return layerSupport;
269}
270
David Monahan6642b8a2021-11-04 16:31:46 +0000271std::unique_ptr<ICustomAllocator> ClBackend::GetDefaultAllocator() const
272{
273 return std::make_unique<ClBackendDefaultAllocator>();
274}
275
Mike Kelly07810fc2020-11-12 10:58:48 +0000276OptimizationViews ClBackend::OptimizeSubgraphView(const SubgraphView& subgraph,
277 const ModelOptions& modelOptions) const
Matteo Martincighadddddb2019-01-24 14:06:23 +0000278{
Mike Kelly80512b02022-05-16 23:10:42 +0100279 OptimizationViews optimizationViews(modelOptions);
Matteo Martincighadddddb2019-01-24 14:06:23 +0000280
Francis Murtagh56ccf682021-12-13 18:48:12 +0000281 auto it = subgraph.endIConnectable();
Mike Kelly07810fc2020-11-12 10:58:48 +0000282 bool isFastMathEnabled = false;
Mike Kelly1ac690a2020-11-17 11:41:38 +0000283 std::map<LayerGuid, Layer*> untouched;
Mike Kelly07810fc2020-11-12 10:58:48 +0000284
Francis Murtagh56ccf682021-12-13 18:48:12 +0000285 while (it != subgraph.beginIConnectable())
Mike Kelly1ac690a2020-11-17 11:41:38 +0000286 {
287 --it;
Francis Murtagh56ccf682021-12-13 18:48:12 +0000288 Layer& base = *(PolymorphicDowncast<Layer*>(*it));
Mike Kelly1ac690a2020-11-17 11:41:38 +0000289 untouched.insert({base.GetGuid(), &base});
290 }
291
Francis Murtagh56ccf682021-12-13 18:48:12 +0000292 it = subgraph.endIConnectable();
Mike Kelly07810fc2020-11-12 10:58:48 +0000293#if defined(ARMCOMPUTECL_ENABLED)
294 IBackendInternal::IBackendSpecificModelContextPtr modelContextPtr = CreateBackendSpecificModelContext(modelOptions);
295
296 if (modelContextPtr)
297 {
298 auto clModelOptions = dynamic_cast<ClBackendModelContext*>(modelContextPtr.get());
299 if (clModelOptions)
300 {
301 isFastMathEnabled = clModelOptions->IsFastMathEnabled();
302 }
303 }
304#endif
Francis Murtagh56ccf682021-12-13 18:48:12 +0000305 while (it != subgraph.beginIConnectable())
Mike Kelly07810fc2020-11-12 10:58:48 +0000306 {
307 --it;
Francis Murtagh56ccf682021-12-13 18:48:12 +0000308 Layer& base = *(PolymorphicDowncast<Layer*>(*it));
Mike Kelly07810fc2020-11-12 10:58:48 +0000309
Matthew Sloyan5fc0fd62021-05-03 12:22:03 +0100310 // Fuse activation into previous layer if supported by backend
Mike Kelly07810fc2020-11-12 10:58:48 +0000311 if ((base.GetType() == LayerType::DepthwiseConvolution2d || base.GetType() == LayerType::Convolution2d
312 || base.GetType() == LayerType::BatchNormalization || base.GetType() == LayerType::FullyConnected
313 || base.GetType() == LayerType::Addition || base.GetType() == LayerType::Multiplication
Mike Kelly3ec30772023-03-08 13:47:17 +0000314 || base.GetType() == LayerType::Subtraction || base.GetType() == LayerType::Division
315 || base.GetType() == LayerType::ElementwiseBinary)
Mike Kelly07810fc2020-11-12 10:58:48 +0000316 && (base.GetAdditionalInformation<ActivationDescriptor>() == nullptr))
317 {
318 for (auto output = base.BeginOutputSlots(); output != base.EndOutputSlots(); ++output)
319 {
320 if (output->GetNumConnections() == 1)
321 {
322 for (auto&& childInput : output->GetConnections())
323 {
Teresa Charlind672f5d2021-01-18 18:07:57 +0000324 if ((childInput->GetOwningLayer().GetType() == LayerType::Activation) &&
325 (checkDataTypeInputandOutput(childInput->GetOwningLayer())))
Mike Kelly07810fc2020-11-12 10:58:48 +0000326 {
327 Layer& child = childInput->GetOwningLayer();
328
329 auto* activationLayer = PolymorphicDowncast<ActivationLayer*>(&child);
330
331 const std::string name = std::string("fused-") + child.GetName() + std::string("-into-") +
332 base.GetName();
333
334 // Get params from activation layer
335 ActivationDescriptor activationDesc = activationLayer->GetParameters();
336
337 if (base.GetType() == LayerType::Convolution2d)
338 {
339 Convolution2dLayer* baseLayer = PolymorphicDowncast<Convolution2dLayer*>(&base);
340
341 Optional<TensorInfo> biases;
342
343 if (baseLayer->GetParameters().m_BiasEnabled)
344 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +0100345 biases = baseLayer->GetInputSlot(2).GetConnectedOutputSlot()->GetTensorInfo();
Mike Kelly07810fc2020-11-12 10:58:48 +0000346 }
347
348 arm_compute::Status status = ClConvolution2dWorkloadValidate(
349 baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
350 activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
351 baseLayer->GetParameters(),
Keith Davisb4dd5cc2022-04-07 11:32:00 +0100352 baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(),
Mike Kelly07810fc2020-11-12 10:58:48 +0000353 biases,
354 isFastMathEnabled,
355 &activationDesc);
356
357 if (status)
358 {
Cathal Corbettcbfd7182021-12-15 17:12:59 +0000359 FuseConvolution2dLayer<Convolution2dLayer>(optimizationViews,
360 baseLayer,
361 activationLayer,
362 activationDesc,
363 name);
Mike Kelly1ac690a2020-11-17 11:41:38 +0000364 untouched.erase(baseLayer->GetGuid());
365 untouched.erase(activationLayer->GetGuid());
Mike Kelly07810fc2020-11-12 10:58:48 +0000366 }
367 }
368 else if (base.GetType() == LayerType::DepthwiseConvolution2d)
369 {
370 DepthwiseConvolution2dLayer* baseLayer =
371 PolymorphicDowncast<DepthwiseConvolution2dLayer*>(&base);
372
373 Optional<TensorInfo> biases;
374
375 if (baseLayer->GetParameters().m_BiasEnabled)
376 {
Cathal Corbett06902652022-04-14 17:55:11 +0100377 biases = baseLayer->GetInputSlot(2).GetConnectedOutputSlot()->GetTensorInfo();
Mike Kelly07810fc2020-11-12 10:58:48 +0000378 }
379
380 arm_compute::Status status = ClDepthwiseConvolutionWorkloadValidate(
381 baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
382 activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
383 baseLayer->GetParameters(),
Cathal Corbett06902652022-04-14 17:55:11 +0100384 baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(),
Mike Kelly07810fc2020-11-12 10:58:48 +0000385 biases,
386 &activationDesc);
387
388 if (status)
389 {
Cathal Corbettcbfd7182021-12-15 17:12:59 +0000390 FuseDepthwiseConvolution2dLayer<DepthwiseConvolution2dLayer>(optimizationViews,
391 baseLayer,
392 activationLayer,
393 activationDesc,
394 name);
Mike Kelly1ac690a2020-11-17 11:41:38 +0000395 untouched.erase(baseLayer->GetGuid());
396 untouched.erase(activationLayer->GetGuid());
Mike Kelly07810fc2020-11-12 10:58:48 +0000397 }
398 }
399 else if (base.GetType() == LayerType::FullyConnected)
400 {
401 FullyConnectedLayer* baseLayer = PolymorphicDowncast<FullyConnectedLayer*>(&base);
Cathal Corbett4452baf2022-05-13 09:55:59 +0100402 FullyConnectedDescriptor descriptor = baseLayer->GetParameters();
Mike Kelly07810fc2020-11-12 10:58:48 +0000403
Cathal Corbett4452baf2022-05-13 09:55:59 +0100404 // As bias is optional only try to get TensorInfo from input if bias is enabled.
Matthew Bentham67d63902022-02-08 15:03:07 +0000405 Optional<TensorInfo> biases;
Cathal Corbett4452baf2022-05-13 09:55:59 +0100406 if (descriptor.m_BiasEnabled)
Matthew Bentham67d63902022-02-08 15:03:07 +0000407 {
Cathal Corbett4452baf2022-05-13 09:55:59 +0100408 biases = baseLayer->GetInputSlot(2).GetConnectedOutputSlot()->GetTensorInfo();
Matthew Bentham67d63902022-02-08 15:03:07 +0000409 }
410
Mike Kelly07810fc2020-11-12 10:58:48 +0000411 arm_compute::Status status = ClFullyConnectedWorkloadValidate(
412 baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
413 activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
Cathal Corbett4452baf2022-05-13 09:55:59 +0100414 baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(),
Matthew Bentham67d63902022-02-08 15:03:07 +0000415 biases,
Mike Kelly07810fc2020-11-12 10:58:48 +0000416 baseLayer->GetParameters(),
417 &activationDesc);
418
419 if (status)
420 {
Cathal Corbettcbfd7182021-12-15 17:12:59 +0000421 FuseFullyConnectedLayer<FullyConnectedLayer>(optimizationViews,
422 baseLayer,
423 activationLayer,
424 activationDesc,
425 name);
Mike Kelly1ac690a2020-11-17 11:41:38 +0000426 untouched.erase(baseLayer->GetGuid());
427 untouched.erase(activationLayer->GetGuid());
Mike Kelly07810fc2020-11-12 10:58:48 +0000428 }
429 }
430 else if (base.GetType() == LayerType::BatchNormalization)
431 {
432 BatchNormalizationLayer* baseLayer =
433 PolymorphicDowncast<BatchNormalizationLayer*>(&base);
434
435 arm_compute::Status status = ClBatchNormalizationValidate(
436 baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
437 activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
438 baseLayer->m_Mean->GetTensorInfo(),
439 baseLayer->m_Variance->GetTensorInfo(),
440 baseLayer->m_Beta->GetTensorInfo(),
441 baseLayer->m_Gamma->GetTensorInfo(),
442 baseLayer->GetParameters(),
443 &activationDesc);
444
445 if (status)
446 {
447 BatchNormalizationLayer* replacementLayer =
Cathal Corbettcbfd7182021-12-15 17:12:59 +0000448 FuseBatchNormalizationLayer<BatchNormalizationLayer>(optimizationViews,
Mike Kelly07810fc2020-11-12 10:58:48 +0000449 baseLayer,
450 activationLayer,
451 activationDesc,
452 name);
453
454 replacementLayer->m_Beta = std::move(baseLayer->m_Beta);
455 replacementLayer->m_Gamma = std::move(baseLayer->m_Gamma);
456 replacementLayer->m_Mean = std::move(baseLayer->m_Mean);
457 replacementLayer->m_Variance = std::move(baseLayer->m_Variance);
Mike Kelly4cc341c2023-07-07 15:43:06 +0100458
Mike Kelly1ac690a2020-11-17 11:41:38 +0000459 untouched.erase(baseLayer->GetGuid());
460 untouched.erase(activationLayer->GetGuid());
Mike Kelly07810fc2020-11-12 10:58:48 +0000461 }
462 }
463 else if (base.GetType() == LayerType::Addition)
464 {
465 AdditionLayer* baseLayer = PolymorphicDowncast<AdditionLayer*>(&base);
466
467 arm_compute::Status status = ClAdditionValidate(
468 baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
469 baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(),
470 activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
471 &activationDesc);
472
473 if (status)
474 {
Cathal Corbettcbfd7182021-12-15 17:12:59 +0000475 FuseAdditionLayer<AdditionLayer>(optimizationViews,
476 baseLayer,
477 activationLayer,
478 activationDesc,
479 name);
Mike Kelly4cc341c2023-07-07 15:43:06 +0100480
Mike Kelly1ac690a2020-11-17 11:41:38 +0000481 untouched.erase(baseLayer->GetGuid());
482 untouched.erase(activationLayer->GetGuid());
Mike Kelly07810fc2020-11-12 10:58:48 +0000483 }
484 }
485 else if (base.GetType() == LayerType::Division)
486 {
487 DivisionLayer* baseLayer = PolymorphicDowncast<DivisionLayer*>(&base);
488
489 arm_compute::Status status = ClDivisionWorkloadValidate(
490 baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
491 baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(),
492 activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
493 &activationDesc);
494
495 if (status)
496 {
Cathal Corbettcbfd7182021-12-15 17:12:59 +0000497 FuseDivisionLayer<DivisionLayer>(optimizationViews,
498 baseLayer,
499 activationLayer,
500 activationDesc,
501 name);
Mike Kelly1ac690a2020-11-17 11:41:38 +0000502 untouched.erase(baseLayer->GetGuid());
503 untouched.erase(activationLayer->GetGuid());
Mike Kelly07810fc2020-11-12 10:58:48 +0000504 }
505 }
506 else if (base.GetType() == LayerType::Multiplication)
507 {
508 MultiplicationLayer* baseLayer = PolymorphicDowncast<MultiplicationLayer*>(&base);
509
510 arm_compute::Status status = ClMultiplicationWorkloadValidate(
511 baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
512 baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(),
513 activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
514 &activationDesc);
515
516 if (status)
517 {
Cathal Corbettcbfd7182021-12-15 17:12:59 +0000518 FuseMultiplicationLayer<MultiplicationLayer>(optimizationViews,
519 baseLayer,
520 activationLayer,
521 activationDesc,
522 name);
Mike Kelly1ac690a2020-11-17 11:41:38 +0000523 untouched.erase(baseLayer->GetGuid());
524 untouched.erase(activationLayer->GetGuid());
Mike Kelly07810fc2020-11-12 10:58:48 +0000525 }
526 }
527 else if (base.GetType() == LayerType::Subtraction)
528 {
529 SubtractionLayer* baseLayer = PolymorphicDowncast<SubtractionLayer*>(&base);
530
531 arm_compute::Status status = ClSubtractionValidate(
532 baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
533 baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(),
534 activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
535 &activationDesc);
536
537 if (status)
538 {
Cathal Corbettcbfd7182021-12-15 17:12:59 +0000539 FuseSubtractionLayer<SubtractionLayer>(optimizationViews,
540 baseLayer,
541 activationLayer,
542 activationDesc,
543 name);
Mike Kelly1ac690a2020-11-17 11:41:38 +0000544 untouched.erase(baseLayer->GetGuid());
545 untouched.erase(activationLayer->GetGuid());
Mike Kelly07810fc2020-11-12 10:58:48 +0000546 }
547 }
Mike Kelly3ec30772023-03-08 13:47:17 +0000548 else if (base.GetType() == LayerType::ElementwiseBinary)
549 {
550 ElementwiseBinaryLayer* baseLayer = PolymorphicDowncast<ElementwiseBinaryLayer*>(&base);
551
552 if (baseLayer->GetParameters().m_Operation == BinaryOperation::Add)
553 {
554 arm_compute::Status status = ClAdditionValidate(
555 baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
556 baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(),
557 activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
558 &activationDesc);
559
560 if (status)
561 {
562 FuseElementwiseBinaryLayer<ElementwiseBinaryLayer>(optimizationViews,
563 baseLayer,
564 activationLayer,
565 activationDesc,
566 BinaryOperation::Add,
567 name);
568 untouched.erase(baseLayer->GetGuid());
569 untouched.erase(activationLayer->GetGuid());
570 }
571 }
572 else if (baseLayer->GetParameters().m_Operation == BinaryOperation::Div)
573 {
574 arm_compute::Status status = ClDivisionWorkloadValidate(
575 baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
576 baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(),
577 activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
578 &activationDesc);
579
580 if (status)
581 {
582 FuseElementwiseBinaryLayer<ElementwiseBinaryLayer>(optimizationViews,
583 baseLayer,
584 activationLayer,
585 activationDesc,
586 BinaryOperation::Div,
587 name);
588 untouched.erase(baseLayer->GetGuid());
589 untouched.erase(activationLayer->GetGuid());
590 }
591 }
592 else if (baseLayer->GetParameters().m_Operation == BinaryOperation::Mul)
593 {
594 arm_compute::Status status = ClMultiplicationWorkloadValidate(
595 baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
596 baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(),
597 activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
598 &activationDesc);
599
600 if (status)
601 {
602 FuseElementwiseBinaryLayer<ElementwiseBinaryLayer>(optimizationViews,
603 baseLayer,
604 activationLayer,
605 activationDesc,
606 BinaryOperation::Mul,
607 name);
608 untouched.erase(baseLayer->GetGuid());
609 untouched.erase(activationLayer->GetGuid());
610 }
611 }
612 else if (baseLayer->GetParameters().m_Operation == BinaryOperation::Sub)
613 {
614 arm_compute::Status status = ClSubtractionValidate(
615 baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
616 baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(),
617 activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(),
618 &activationDesc);
619
620 if (status)
621 {
622 FuseElementwiseBinaryLayer<ElementwiseBinaryLayer>(optimizationViews,
623 baseLayer,
624 activationLayer,
625 activationDesc,
626 BinaryOperation::Sub,
627 name);
Mike Kelly4cc341c2023-07-07 15:43:06 +0100628 untouched.erase(baseLayer->GetGuid());
629 untouched.erase(activationLayer->GetGuid());
Mike Kelly3ec30772023-03-08 13:47:17 +0000630 }
631 }
632 // No fusion available for other BinaryOperations
633 }
Mike Kelly07810fc2020-11-12 10:58:48 +0000634 }
635 }
636 }
637 }
638 }
Matthew Sloyan5fc0fd62021-05-03 12:22:03 +0100639
640 // Separate reduce layer with multiple axes into multiple reduce layers with 1 axis.
641 if (base.GetType() == LayerType::Reduce)
642 {
643 ReduceLayer* baseLayer = PolymorphicDowncast<ReduceLayer*>(&base);
644 ReduceDescriptor reduceDescriptor = baseLayer->GetParameters();
645
646 if (!reduceDescriptor.m_vAxis.empty() && reduceDescriptor.m_vAxis.size() > 1)
647 {
648 // Add new layers to the graph and connect them.
Francis Murtagh56ccf682021-12-13 18:48:12 +0000649 std::vector<IConnectableLayer*> layers = ChainReduceLayers<ReduceLayer>(optimizationViews,
650 baseLayer,
651 reduceDescriptor);
Matthew Sloyan5fc0fd62021-05-03 12:22:03 +0100652
653 // Replace existing baselayer with new subgraph.
654 ReplaceLayers<ReduceLayer>(optimizationViews, baseLayer, layers);
655 untouched.erase(baseLayer->GetGuid());
656 }
657 }
Cathal Corbett3883b272022-07-22 16:03:36 +0100658
659 // Special case to fuse padding into average pooling 2d for quantized datatype.
660 // Required to be done as a backend specific optimization as Neon does not support this special case.
661 if (base.GetType() == LayerType::Pooling2d)
662 {
663 Pooling2dLayer* baseLayer = PolymorphicDowncast<Pooling2dLayer*>(&base);
664 Pooling2dDescriptor poolingDescriptor = baseLayer->GetParameters();
665
666 if (baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer().GetType() == LayerType::Pad)
667 {
668 PadLayer* padLayer = PolymorphicDowncast<PadLayer*>(
669 &baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer());
670 if (padLayer->GetOutputSlot(0).GetNumConnections() == 1 &&
671 optimizations::pad_fold::TryFoldPadIntoLayer2d(padLayer->GetParameters(),
672 poolingDescriptor,
673 padLayer->GetOutputSlot().GetTensorInfo(),
674 true))
675 {
676 FoldPadIntoAveragePool2d<Pooling2dLayer>(optimizationViews, baseLayer,
677 poolingDescriptor, padLayer);
678 untouched.erase(baseLayer->GetGuid());
679 untouched.erase(padLayer->GetGuid());
680 }
681 }
682 }
Mike Kelly07810fc2020-11-12 10:58:48 +0000683 }
Mike Kelly1ac690a2020-11-17 11:41:38 +0000684
Mike Kelly4cc341c2023-07-07 15:43:06 +0100685 if (optimizationViews.GetSubstitutions().empty() && optimizationViews.GetDeletedSubgraphs().empty())
Mike Kelly07810fc2020-11-12 10:58:48 +0000686 {
687 optimizationViews.AddUntouchedSubgraph(SubgraphView(subgraph));
688 }
Mike Kelly1ac690a2020-11-17 11:41:38 +0000689 else
690 {
691 ReportUntouchedLayers(optimizationViews, untouched);
692 }
Matteo Martincighc3ba50e2019-05-22 14:28:16 +0100693
694 return optimizationViews;
Matteo Martincighadddddb2019-01-24 14:06:23 +0000695}
696
David Beck9efb57d2018-11-05 13:40:33 +0000697} // namespace armnn