arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ClBackend.hpp" |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 7 | #include "ClBackendContext.hpp" |
David Monahan | 6642b8a | 2021-11-04 16:31:46 +0000 | [diff] [blame] | 8 | #include "ClBackendDefaultAllocator.hpp" |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 9 | #include "ClBackendId.hpp" |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 10 | #include "ClBackendModelContext.hpp" |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 11 | #include "ClImportTensorHandleFactory.hpp" |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 12 | #include "ClLayerSupport.hpp" |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 13 | #include "ClTensorHandleFactory.hpp" |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 14 | #include "ClWorkloadFactory.hpp" |
arovir01 | a094479 | 2018-10-11 15:00:58 +0100 | [diff] [blame] | 15 | |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 16 | #include <armnn/BackendRegistry.hpp> |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 17 | #include <armnn/Descriptors.hpp> |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 18 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 19 | #include <aclCommon/ArmComputeSubgraphUtils.hpp> |
| 20 | #include <aclCommon/ArmComputeUtils.hpp> |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 21 | #include <aclCommon/BaseMemoryManager.hpp> |
| 22 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 23 | #include <armnn/backends/IBackendContext.hpp> |
| 24 | #include <armnn/backends/IMemoryManager.hpp> |
Jan Eilers | 3c9e045 | 2020-04-10 13:00:44 +0100 | [diff] [blame] | 25 | #include <armnn/utility/PolymorphicDowncast.hpp> |
| 26 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 27 | #include "workloads/ClAdditionWorkload.hpp" |
| 28 | #include "workloads/ClBatchNormalizationFloatWorkload.hpp" |
| 29 | #include "workloads/ClConvolution2dWorkload.hpp" |
| 30 | #include "workloads/ClDepthwiseConvolutionWorkload.hpp" |
Teresa Charlin | e11e63d | 2021-04-21 12:56:45 +0100 | [diff] [blame] | 31 | #include "workloads/ClDivisionWorkload.hpp" |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 32 | #include "workloads/ClFullyConnectedWorkload.hpp" |
| 33 | #include "workloads/ClMultiplicationWorkload.hpp" |
Matthew Sloyan | 5fc0fd6 | 2021-05-03 12:22:03 +0100 | [diff] [blame] | 34 | #include "workloads/ClReduceWorkload.hpp" |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 35 | #include "workloads/ClSubtractionWorkload.hpp" |
| 36 | |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 37 | #include <Optimizer.hpp> |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 38 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 39 | #include <arm_compute/core/Types.h> |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 40 | #include <arm_compute/runtime/CL/CLBufferAllocator.h> |
| 41 | |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 42 | namespace armnn |
| 43 | { |
| 44 | |
David Beck | 3cc9a62 | 2018-10-12 10:38:31 +0100 | [diff] [blame] | 45 | const BackendId& ClBackend::GetIdStatic() |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 46 | { |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 47 | static const BackendId s_Id{ClBackendId()}; |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 48 | return s_Id; |
| 49 | } |
| 50 | |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 51 | IBackendInternal::IMemoryManagerUniquePtr ClBackend::CreateMemoryManager() const |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 52 | { |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 53 | if (m_UsingCustomAllocator) |
| 54 | { |
| 55 | return std::make_unique<ClMemoryManager>(m_CustomAllocator); |
| 56 | } |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 57 | return std::make_unique<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>()); |
| 58 | } |
| 59 | |
| 60 | IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory( |
| 61 | const IBackendInternal::IMemoryManagerSharedPtr& memoryManager) const |
| 62 | { |
| 63 | return std::make_unique<ClWorkloadFactory>( |
Jan Eilers | 3c9e045 | 2020-04-10 13:00:44 +0100 | [diff] [blame] | 64 | PolymorphicPointerDowncast<ClMemoryManager>(memoryManager)); |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 67 | IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory( |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 68 | const IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const ModelOptions& modelOptions) const |
| 69 | { |
| 70 | return std::make_unique<ClWorkloadFactory>( |
| 71 | PolymorphicPointerDowncast<ClMemoryManager>(memoryManager), CreateBackendSpecificModelContext(modelOptions)); |
| 72 | } |
| 73 | |
| 74 | IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory( |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 75 | TensorHandleFactoryRegistry& registry) const |
| 76 | { |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 77 | 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 Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 86 | |
| 87 | registry.RegisterMemoryManager(memoryManager); |
Narumol Prangnawarat | 549cb7a | 2020-07-10 17:50:53 +0100 | [diff] [blame] | 88 | registry.RegisterFactory(std::make_unique<ClTensorHandleFactory>(memoryManager)); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 89 | registry.RegisterFactory(std::make_unique<ClImportTensorHandleFactory>( |
| 90 | static_cast<MemorySourceFlags>(MemorySource::Malloc), static_cast<MemorySourceFlags>(MemorySource::Malloc))); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 91 | |
| 92 | return std::make_unique<ClWorkloadFactory>( |
Jan Eilers | 3c9e045 | 2020-04-10 13:00:44 +0100 | [diff] [blame] | 93 | PolymorphicPointerDowncast<ClMemoryManager>(memoryManager)); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 96 | IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory( |
| 97 | TensorHandleFactoryRegistry& registry, const ModelOptions& modelOptions) const |
| 98 | { |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 99 | std::shared_ptr<ClMemoryManager> memoryManager; |
| 100 | if (m_UsingCustomAllocator) |
| 101 | { |
| 102 | memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>()); |
| 107 | } |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 108 | |
| 109 | registry.RegisterMemoryManager(memoryManager); |
| 110 | registry.RegisterFactory(std::make_unique<ClTensorHandleFactory>(memoryManager)); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 111 | registry.RegisterFactory(std::make_unique<ClImportTensorHandleFactory>( |
| 112 | static_cast<MemorySourceFlags>(MemorySource::Malloc), static_cast<MemorySourceFlags>(MemorySource::Malloc))); |
| 113 | |
| 114 | return std::make_unique<ClWorkloadFactory>( |
| 115 | PolymorphicPointerDowncast<ClMemoryManager>(memoryManager), CreateBackendSpecificModelContext(modelOptions)); |
| 116 | } |
| 117 | |
| 118 | IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory( |
| 119 | TensorHandleFactoryRegistry& registry, |
| 120 | const ModelOptions& modelOptions, |
| 121 | MemorySourceFlags inputFlags, |
| 122 | MemorySourceFlags outputFlags) const |
| 123 | { |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 124 | std::shared_ptr<ClMemoryManager> memoryManager; |
| 125 | if (m_UsingCustomAllocator) |
| 126 | { |
| 127 | memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>()); |
| 132 | } |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 133 | |
| 134 | registry.RegisterMemoryManager(memoryManager); |
| 135 | registry.RegisterFactory(std::make_unique<ClTensorHandleFactory>(memoryManager)); |
| 136 | registry.RegisterFactory(std::make_unique<ClImportTensorHandleFactory>(inputFlags, outputFlags)); |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 137 | |
| 138 | return std::make_unique<ClWorkloadFactory>( |
| 139 | PolymorphicPointerDowncast<ClMemoryManager>(memoryManager), CreateBackendSpecificModelContext(modelOptions)); |
| 140 | } |
| 141 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 142 | std::vector<ITensorHandleFactory::FactoryId> ClBackend::GetHandleFactoryPreferences() const |
| 143 | { |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 144 | return std::vector<ITensorHandleFactory::FactoryId> {ClTensorHandleFactory::GetIdStatic(), |
| 145 | ClImportTensorHandleFactory::GetIdStatic()}; |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void ClBackend::RegisterTensorHandleFactories(TensorHandleFactoryRegistry& registry) |
| 149 | { |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 150 | std::shared_ptr<ClMemoryManager> memoryManager; |
| 151 | if (m_UsingCustomAllocator) |
| 152 | { |
| 153 | memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>()); |
| 158 | } |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 159 | |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 160 | registry.RegisterMemoryManager(memoryManager); |
| 161 | registry.RegisterFactory(std::make_unique<ClTensorHandleFactory>(memoryManager)); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 162 | registry.RegisterFactory(std::make_unique<ClImportTensorHandleFactory>( |
| 163 | static_cast<MemorySourceFlags>(MemorySource::Malloc), static_cast<MemorySourceFlags>(MemorySource::Malloc))); |
| 164 | } |
| 165 | |
| 166 | void ClBackend::RegisterTensorHandleFactories(TensorHandleFactoryRegistry& registry, |
| 167 | MemorySourceFlags inputFlags, |
| 168 | MemorySourceFlags outputFlags) |
| 169 | { |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 170 | std::shared_ptr<ClMemoryManager> memoryManager; |
| 171 | if (m_UsingCustomAllocator) |
| 172 | { |
| 173 | memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>()); |
| 178 | } |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 179 | |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 180 | registry.RegisterMemoryManager(memoryManager); |
| 181 | registry.RegisterFactory(std::make_unique<ClTensorHandleFactory>(memoryManager)); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 182 | registry.RegisterFactory(std::make_unique<ClImportTensorHandleFactory>(inputFlags, outputFlags)); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 185 | IBackendInternal::IBackendContextPtr ClBackend::CreateBackendContext(const IRuntime::CreationOptions& options) const |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 186 | { |
| 187 | return IBackendContextPtr{new ClBackendContext{options}}; |
| 188 | } |
| 189 | |
Colm Donelan | e49755b | 2020-01-29 15:22:43 +0000 | [diff] [blame] | 190 | IBackendInternal::IBackendProfilingContextPtr ClBackend::CreateBackendProfilingContext( |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 191 | const IRuntime::CreationOptions&, IBackendProfilingPtr&) |
Colm Donelan | e49755b | 2020-01-29 15:22:43 +0000 | [diff] [blame] | 192 | { |
| 193 | return IBackendProfilingContextPtr{}; |
| 194 | } |
| 195 | |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 196 | IBackendInternal::IBackendSpecificModelContextPtr ClBackend::CreateBackendSpecificModelContext( |
| 197 | const ModelOptions& modelOptions) const |
| 198 | { |
| 199 | return IBackendSpecificModelContextPtr{new ClBackendModelContext{modelOptions}}; |
| 200 | } |
| 201 | |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 202 | IBackendInternal::ILayerSupportSharedPtr ClBackend::GetLayerSupport() const |
| 203 | { |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 204 | static ILayerSupportSharedPtr layerSupport |
| 205 | { |
| 206 | new ClLayerSupport(IBackendInternal::IBackendSpecificModelContextPtr{}) |
| 207 | }; |
| 208 | return layerSupport; |
| 209 | } |
| 210 | |
| 211 | IBackendInternal::ILayerSupportSharedPtr ClBackend::GetLayerSupport(const ModelOptions& modelOptions) const |
| 212 | { |
| 213 | static ILayerSupportSharedPtr layerSupport |
| 214 | { |
| 215 | new ClLayerSupport(CreateBackendSpecificModelContext(modelOptions)) |
| 216 | }; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 217 | return layerSupport; |
| 218 | } |
| 219 | |
David Monahan | 6642b8a | 2021-11-04 16:31:46 +0000 | [diff] [blame] | 220 | std::unique_ptr<ICustomAllocator> ClBackend::GetDefaultAllocator() const |
| 221 | { |
| 222 | return std::make_unique<ClBackendDefaultAllocator>(); |
| 223 | } |
| 224 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 225 | OptimizationViews ClBackend::OptimizeSubgraphView(const SubgraphView& subgraph, |
| 226 | const ModelOptions& modelOptions) const |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 227 | { |
Matteo Martincigh | c3ba50e | 2019-05-22 14:28:16 +0100 | [diff] [blame] | 228 | OptimizationViews optimizationViews; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 229 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 230 | auto it = subgraph.endIConnectable(); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 231 | bool isFastMathEnabled = false; |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 232 | std::map<LayerGuid, Layer*> untouched; |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 233 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 234 | while (it != subgraph.beginIConnectable()) |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 235 | { |
| 236 | --it; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 237 | Layer& base = *(PolymorphicDowncast<Layer*>(*it)); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 238 | untouched.insert({base.GetGuid(), &base}); |
| 239 | } |
| 240 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 241 | it = subgraph.endIConnectable(); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 242 | #if defined(ARMCOMPUTECL_ENABLED) |
| 243 | IBackendInternal::IBackendSpecificModelContextPtr modelContextPtr = CreateBackendSpecificModelContext(modelOptions); |
| 244 | |
| 245 | if (modelContextPtr) |
| 246 | { |
| 247 | auto clModelOptions = dynamic_cast<ClBackendModelContext*>(modelContextPtr.get()); |
| 248 | if (clModelOptions) |
| 249 | { |
| 250 | isFastMathEnabled = clModelOptions->IsFastMathEnabled(); |
| 251 | } |
| 252 | } |
| 253 | #endif |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 254 | while (it != subgraph.beginIConnectable()) |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 255 | { |
| 256 | --it; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 257 | Layer& base = *(PolymorphicDowncast<Layer*>(*it)); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 258 | |
Matthew Sloyan | 5fc0fd6 | 2021-05-03 12:22:03 +0100 | [diff] [blame] | 259 | // Fuse activation into previous layer if supported by backend |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 260 | if ((base.GetType() == LayerType::DepthwiseConvolution2d || base.GetType() == LayerType::Convolution2d |
| 261 | || base.GetType() == LayerType::BatchNormalization || base.GetType() == LayerType::FullyConnected |
| 262 | || base.GetType() == LayerType::Addition || base.GetType() == LayerType::Multiplication |
Matthew Sloyan | ae12306 | 2021-05-07 14:18:01 +0000 | [diff] [blame] | 263 | || base.GetType() == LayerType::Subtraction || base.GetType() == LayerType::Division) |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 264 | && (base.GetAdditionalInformation<ActivationDescriptor>() == nullptr)) |
| 265 | { |
| 266 | for (auto output = base.BeginOutputSlots(); output != base.EndOutputSlots(); ++output) |
| 267 | { |
| 268 | if (output->GetNumConnections() == 1) |
| 269 | { |
| 270 | for (auto&& childInput : output->GetConnections()) |
| 271 | { |
Teresa Charlin | d672f5d | 2021-01-18 18:07:57 +0000 | [diff] [blame] | 272 | if ((childInput->GetOwningLayer().GetType() == LayerType::Activation) && |
| 273 | (checkDataTypeInputandOutput(childInput->GetOwningLayer()))) |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 274 | { |
| 275 | Layer& child = childInput->GetOwningLayer(); |
| 276 | |
| 277 | auto* activationLayer = PolymorphicDowncast<ActivationLayer*>(&child); |
| 278 | |
| 279 | const std::string name = std::string("fused-") + child.GetName() + std::string("-into-") + |
| 280 | base.GetName(); |
| 281 | |
| 282 | // Get params from activation layer |
| 283 | ActivationDescriptor activationDesc = activationLayer->GetParameters(); |
| 284 | |
| 285 | if (base.GetType() == LayerType::Convolution2d) |
| 286 | { |
| 287 | Convolution2dLayer* baseLayer = PolymorphicDowncast<Convolution2dLayer*>(&base); |
| 288 | |
| 289 | Optional<TensorInfo> biases; |
| 290 | |
| 291 | if (baseLayer->GetParameters().m_BiasEnabled) |
| 292 | { |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 293 | biases = baseLayer->m_Bias->GetTensorInfo(); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | arm_compute::Status status = ClConvolution2dWorkloadValidate( |
| 297 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 298 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 299 | baseLayer->GetParameters(), |
| 300 | baseLayer->m_Weight->GetTensorInfo(), |
| 301 | biases, |
| 302 | isFastMathEnabled, |
| 303 | &activationDesc); |
| 304 | |
| 305 | if (status) |
| 306 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 307 | FuseConvolution2dLayer<Convolution2dLayer>(optimizationViews, |
| 308 | baseLayer, |
| 309 | activationLayer, |
| 310 | activationDesc, |
| 311 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 312 | untouched.erase(baseLayer->GetGuid()); |
| 313 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | else if (base.GetType() == LayerType::DepthwiseConvolution2d) |
| 317 | { |
| 318 | DepthwiseConvolution2dLayer* baseLayer = |
| 319 | PolymorphicDowncast<DepthwiseConvolution2dLayer*>(&base); |
| 320 | |
| 321 | Optional<TensorInfo> biases; |
| 322 | |
| 323 | if (baseLayer->GetParameters().m_BiasEnabled) |
| 324 | { |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 325 | biases = baseLayer->m_Bias->GetTensorInfo(); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | arm_compute::Status status = ClDepthwiseConvolutionWorkloadValidate( |
| 329 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 330 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 331 | baseLayer->GetParameters(), |
| 332 | baseLayer->m_Weight->GetTensorInfo(), |
| 333 | biases, |
| 334 | &activationDesc); |
| 335 | |
| 336 | if (status) |
| 337 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 338 | FuseDepthwiseConvolution2dLayer<DepthwiseConvolution2dLayer>(optimizationViews, |
| 339 | baseLayer, |
| 340 | activationLayer, |
| 341 | activationDesc, |
| 342 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 343 | untouched.erase(baseLayer->GetGuid()); |
| 344 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | else if (base.GetType() == LayerType::FullyConnected) |
| 348 | { |
| 349 | FullyConnectedLayer* baseLayer = PolymorphicDowncast<FullyConnectedLayer*>(&base); |
| 350 | |
| 351 | arm_compute::Status status = ClFullyConnectedWorkloadValidate( |
| 352 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 353 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 354 | baseLayer->m_Weight->GetTensorInfo(), |
| 355 | baseLayer->m_Bias->GetTensorInfo(), |
| 356 | baseLayer->GetParameters(), |
| 357 | &activationDesc); |
| 358 | |
| 359 | if (status) |
| 360 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 361 | FuseFullyConnectedLayer<FullyConnectedLayer>(optimizationViews, |
| 362 | baseLayer, |
| 363 | activationLayer, |
| 364 | activationDesc, |
| 365 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 366 | untouched.erase(baseLayer->GetGuid()); |
| 367 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | else if (base.GetType() == LayerType::BatchNormalization) |
| 371 | { |
| 372 | BatchNormalizationLayer* baseLayer = |
| 373 | PolymorphicDowncast<BatchNormalizationLayer*>(&base); |
| 374 | |
| 375 | arm_compute::Status status = ClBatchNormalizationValidate( |
| 376 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 377 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 378 | baseLayer->m_Mean->GetTensorInfo(), |
| 379 | baseLayer->m_Variance->GetTensorInfo(), |
| 380 | baseLayer->m_Beta->GetTensorInfo(), |
| 381 | baseLayer->m_Gamma->GetTensorInfo(), |
| 382 | baseLayer->GetParameters(), |
| 383 | &activationDesc); |
| 384 | |
| 385 | if (status) |
| 386 | { |
| 387 | BatchNormalizationLayer* replacementLayer = |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 388 | FuseBatchNormalizationLayer<BatchNormalizationLayer>(optimizationViews, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 389 | baseLayer, |
| 390 | activationLayer, |
| 391 | activationDesc, |
| 392 | name); |
| 393 | |
| 394 | replacementLayer->m_Beta = std::move(baseLayer->m_Beta); |
| 395 | replacementLayer->m_Gamma = std::move(baseLayer->m_Gamma); |
| 396 | replacementLayer->m_Mean = std::move(baseLayer->m_Mean); |
| 397 | replacementLayer->m_Variance = std::move(baseLayer->m_Variance); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 398 | untouched.erase(baseLayer->GetGuid()); |
| 399 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | else if (base.GetType() == LayerType::Addition) |
| 403 | { |
| 404 | AdditionLayer* baseLayer = PolymorphicDowncast<AdditionLayer*>(&base); |
| 405 | |
| 406 | arm_compute::Status status = ClAdditionValidate( |
| 407 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 408 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 409 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 410 | &activationDesc); |
| 411 | |
| 412 | if (status) |
| 413 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 414 | FuseAdditionLayer<AdditionLayer>(optimizationViews, |
| 415 | baseLayer, |
| 416 | activationLayer, |
| 417 | activationDesc, |
| 418 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 419 | untouched.erase(baseLayer->GetGuid()); |
| 420 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | else if (base.GetType() == LayerType::Division) |
| 424 | { |
| 425 | DivisionLayer* baseLayer = PolymorphicDowncast<DivisionLayer*>(&base); |
| 426 | |
| 427 | arm_compute::Status status = ClDivisionWorkloadValidate( |
| 428 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 429 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 430 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 431 | &activationDesc); |
| 432 | |
| 433 | if (status) |
| 434 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 435 | FuseDivisionLayer<DivisionLayer>(optimizationViews, |
| 436 | baseLayer, |
| 437 | activationLayer, |
| 438 | activationDesc, |
| 439 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 440 | untouched.erase(baseLayer->GetGuid()); |
| 441 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | else if (base.GetType() == LayerType::Multiplication) |
| 445 | { |
| 446 | MultiplicationLayer* baseLayer = PolymorphicDowncast<MultiplicationLayer*>(&base); |
| 447 | |
| 448 | arm_compute::Status status = ClMultiplicationWorkloadValidate( |
| 449 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 450 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 451 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 452 | &activationDesc); |
| 453 | |
| 454 | if (status) |
| 455 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 456 | FuseMultiplicationLayer<MultiplicationLayer>(optimizationViews, |
| 457 | baseLayer, |
| 458 | activationLayer, |
| 459 | activationDesc, |
| 460 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 461 | untouched.erase(baseLayer->GetGuid()); |
| 462 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | else if (base.GetType() == LayerType::Subtraction) |
| 466 | { |
| 467 | SubtractionLayer* baseLayer = PolymorphicDowncast<SubtractionLayer*>(&base); |
| 468 | |
| 469 | arm_compute::Status status = ClSubtractionValidate( |
| 470 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 471 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 472 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 473 | &activationDesc); |
| 474 | |
| 475 | if (status) |
| 476 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 477 | FuseSubtractionLayer<SubtractionLayer>(optimizationViews, |
| 478 | baseLayer, |
| 479 | activationLayer, |
| 480 | activationDesc, |
| 481 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 482 | untouched.erase(baseLayer->GetGuid()); |
| 483 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | } |
Matthew Sloyan | 5fc0fd6 | 2021-05-03 12:22:03 +0100 | [diff] [blame] | 491 | |
| 492 | // Separate reduce layer with multiple axes into multiple reduce layers with 1 axis. |
| 493 | if (base.GetType() == LayerType::Reduce) |
| 494 | { |
| 495 | ReduceLayer* baseLayer = PolymorphicDowncast<ReduceLayer*>(&base); |
| 496 | ReduceDescriptor reduceDescriptor = baseLayer->GetParameters(); |
| 497 | |
| 498 | if (!reduceDescriptor.m_vAxis.empty() && reduceDescriptor.m_vAxis.size() > 1) |
| 499 | { |
| 500 | // Add new layers to the graph and connect them. |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame^] | 501 | std::vector<IConnectableLayer*> layers = ChainReduceLayers<ReduceLayer>(optimizationViews, |
| 502 | baseLayer, |
| 503 | reduceDescriptor); |
Matthew Sloyan | 5fc0fd6 | 2021-05-03 12:22:03 +0100 | [diff] [blame] | 504 | |
| 505 | // Replace existing baselayer with new subgraph. |
| 506 | ReplaceLayers<ReduceLayer>(optimizationViews, baseLayer, layers); |
| 507 | untouched.erase(baseLayer->GetGuid()); |
| 508 | } |
| 509 | } |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 510 | } |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 511 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 512 | if (optimizationViews.GetSubstitutions().empty()) |
| 513 | { |
| 514 | optimizationViews.AddUntouchedSubgraph(SubgraphView(subgraph)); |
| 515 | } |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 516 | else |
| 517 | { |
| 518 | ReportUntouchedLayers(optimizationViews, untouched); |
| 519 | } |
Matteo Martincigh | c3ba50e | 2019-05-22 14:28:16 +0100 | [diff] [blame] | 520 | |
| 521 | return optimizationViews; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 522 | } |
| 523 | |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 524 | } // namespace armnn |