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