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