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