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