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 | |
Mike Kelly | 80512b0 | 2022-05-16 23:10:42 +0100 | [diff] [blame] | 142 | OptimizationViews NeonBackend::OptimizeSubgraphView(const SubgraphView& subgraph, |
| 143 | const ModelOptions& modelOptions) const |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 144 | { |
Mike Kelly | 80512b0 | 2022-05-16 23:10:42 +0100 | [diff] [blame] | 145 | OptimizationViews optimizationViews(modelOptions); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 146 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 147 | auto it = subgraph.endIConnectable(); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 148 | std::map<LayerGuid, Layer*> untouched; |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 149 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 150 | while (it != subgraph.beginIConnectable()) |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 151 | { |
| 152 | --it; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 153 | Layer& base = *(PolymorphicDowncast<Layer*>(*it)); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 154 | untouched.insert({base.GetGuid(), &base}); |
| 155 | } |
| 156 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 157 | it = subgraph.endIConnectable(); |
| 158 | while (it != subgraph.beginIConnectable()) |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 159 | { |
| 160 | --it; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 161 | Layer& base = *(PolymorphicDowncast<Layer*>(*it)); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 162 | |
Matthew Sloyan | 5fc0fd6 | 2021-05-03 12:22:03 +0100 | [diff] [blame] | 163 | // Fuse activation into previous layer if supported by backend |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 164 | if ((base.GetType() == LayerType::DepthwiseConvolution2d || base.GetType() == LayerType::Convolution2d |
| 165 | || base.GetType() == LayerType::BatchNormalization || base.GetType() == LayerType::FullyConnected |
| 166 | || base.GetType() == LayerType::Addition || base.GetType() == LayerType::Multiplication |
Matthew Sloyan | ae12306 | 2021-05-07 14:18:01 +0000 | [diff] [blame] | 167 | || base.GetType() == LayerType::Subtraction || base.GetType() == LayerType::Division) |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 168 | && (base.GetAdditionalInformation<ActivationDescriptor>() == nullptr)) |
| 169 | { |
| 170 | for (auto output = base.BeginOutputSlots(); output != base.EndOutputSlots(); ++output) |
| 171 | { |
| 172 | if (output->GetNumConnections() == 1) |
| 173 | { |
| 174 | for (auto&& childInput : output->GetConnections()) |
| 175 | { |
Teresa Charlin | d672f5d | 2021-01-18 18:07:57 +0000 | [diff] [blame] | 176 | if ((childInput->GetOwningLayer().GetType() == LayerType::Activation) && |
| 177 | (checkDataTypeInputandOutput(childInput->GetOwningLayer()))) |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 178 | { |
| 179 | Layer& child = childInput->GetOwningLayer(); |
| 180 | |
| 181 | auto* activationLayer = PolymorphicDowncast<ActivationLayer*>(&child); |
| 182 | |
| 183 | const std::string name = std::string("fused-") + child.GetName() + std::string("-into-") + |
| 184 | base.GetName(); |
| 185 | |
| 186 | // Get params from activation layer |
| 187 | ActivationDescriptor activationDesc = activationLayer->GetParameters(); |
| 188 | |
| 189 | if (base.GetType() == LayerType::Convolution2d) |
| 190 | { |
| 191 | Convolution2dLayer* baseLayer = PolymorphicDowncast<Convolution2dLayer*>(&base); |
| 192 | |
| 193 | Optional<TensorInfo> biases; |
| 194 | |
| 195 | if (baseLayer->GetParameters().m_BiasEnabled) |
| 196 | { |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 197 | biases = baseLayer->GetInputSlot(2).GetConnectedOutputSlot()->GetTensorInfo(); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | arm_compute::Status status = NeonConvolution2dWorkloadValidate( |
| 201 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 202 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 203 | baseLayer->GetParameters(), |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 204 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 205 | biases, |
| 206 | false, |
| 207 | &activationDesc); |
| 208 | |
| 209 | if (status) |
| 210 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 211 | FuseConvolution2dLayer<Convolution2dLayer>(optimizationViews, |
| 212 | baseLayer, |
| 213 | activationLayer, |
| 214 | activationDesc, |
| 215 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 216 | untouched.erase(baseLayer->GetGuid()); |
| 217 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | else if (base.GetType() == LayerType::DepthwiseConvolution2d) |
| 221 | { |
| 222 | DepthwiseConvolution2dLayer* baseLayer = |
| 223 | PolymorphicDowncast<DepthwiseConvolution2dLayer*>(&base); |
| 224 | |
| 225 | Optional<TensorInfo> biases; |
| 226 | |
| 227 | if (baseLayer->GetParameters().m_BiasEnabled) |
| 228 | { |
Cathal Corbett | 0690265 | 2022-04-14 17:55:11 +0100 | [diff] [blame] | 229 | biases = baseLayer->GetInputSlot(2).GetConnectedOutputSlot()->GetTensorInfo(); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | arm_compute::Status status = NeonDepthwiseConvolutionWorkloadValidate( |
| 233 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 234 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 235 | baseLayer->GetParameters(), |
Cathal Corbett | 0690265 | 2022-04-14 17:55:11 +0100 | [diff] [blame] | 236 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 237 | biases, |
| 238 | &activationDesc); |
| 239 | |
| 240 | if (status) |
| 241 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 242 | FuseDepthwiseConvolution2dLayer<DepthwiseConvolution2dLayer>(optimizationViews, |
| 243 | baseLayer, |
| 244 | activationLayer, |
| 245 | activationDesc, |
| 246 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 247 | untouched.erase(baseLayer->GetGuid()); |
| 248 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | else if (base.GetType() == LayerType::FullyConnected) |
| 252 | { |
| 253 | FullyConnectedLayer* baseLayer = PolymorphicDowncast<FullyConnectedLayer*>(&base); |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 254 | FullyConnectedDescriptor descriptor = baseLayer->GetParameters(); |
Matthew Bentham | 67d6390 | 2022-02-08 15:03:07 +0000 | [diff] [blame] | 255 | |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 256 | // As bias is optional only try to get TensorInfo from input if bias is enabled. |
| 257 | Optional<TensorInfo> biases; |
| 258 | if (descriptor.m_BiasEnabled) |
Matthew Bentham | 67d6390 | 2022-02-08 15:03:07 +0000 | [diff] [blame] | 259 | { |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 260 | biases = baseLayer->GetInputSlot(2).GetConnectedOutputSlot()->GetTensorInfo(); |
Matthew Bentham | 67d6390 | 2022-02-08 15:03:07 +0000 | [diff] [blame] | 261 | } |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 262 | |
| 263 | arm_compute::Status status = NeonFullyConnectedWorkloadValidate( |
| 264 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 265 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 266 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
Matthew Bentham | 67d6390 | 2022-02-08 15:03:07 +0000 | [diff] [blame] | 267 | biases, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 268 | baseLayer->GetParameters(), |
| 269 | &activationDesc); |
| 270 | |
| 271 | if (status) |
| 272 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 273 | FuseFullyConnectedLayer<FullyConnectedLayer>(optimizationViews, |
| 274 | baseLayer, |
| 275 | activationLayer, |
| 276 | activationDesc, |
| 277 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 278 | untouched.erase(baseLayer->GetGuid()); |
| 279 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | else if (base.GetType() == LayerType::BatchNormalization) |
| 283 | { |
| 284 | BatchNormalizationLayer* baseLayer = |
| 285 | PolymorphicDowncast<BatchNormalizationLayer*>(&base); |
| 286 | |
| 287 | arm_compute::Status status = NeonBatchNormalizationValidate( |
| 288 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 289 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 290 | baseLayer->m_Mean->GetTensorInfo(), |
| 291 | baseLayer->m_Variance->GetTensorInfo(), |
| 292 | baseLayer->m_Beta->GetTensorInfo(), |
| 293 | baseLayer->m_Gamma->GetTensorInfo(), |
| 294 | baseLayer->GetParameters(), |
| 295 | &activationDesc); |
| 296 | |
| 297 | if (status) |
| 298 | { |
| 299 | BatchNormalizationLayer* replacementLayer = |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 300 | FuseBatchNormalizationLayer<BatchNormalizationLayer>(optimizationViews, |
| 301 | baseLayer, |
| 302 | activationLayer, |
| 303 | activationDesc, |
| 304 | name); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 305 | |
| 306 | replacementLayer->m_Beta = std::move(baseLayer->m_Beta); |
| 307 | replacementLayer->m_Gamma = std::move(baseLayer->m_Gamma); |
| 308 | replacementLayer->m_Mean = std::move(baseLayer->m_Mean); |
| 309 | replacementLayer->m_Variance = std::move(baseLayer->m_Variance); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 310 | untouched.erase(baseLayer->GetGuid()); |
| 311 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | else if (base.GetType() == LayerType::Addition) |
| 315 | { |
| 316 | AdditionLayer* baseLayer = PolymorphicDowncast<AdditionLayer*>(&base); |
| 317 | |
| 318 | arm_compute::Status status = NeonAdditionWorkloadValidate( |
| 319 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 320 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 321 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 322 | &activationDesc); |
| 323 | |
| 324 | if (status) |
| 325 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 326 | FuseAdditionLayer<AdditionLayer>(optimizationViews, |
| 327 | baseLayer, |
| 328 | activationLayer, |
| 329 | activationDesc, |
| 330 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 331 | untouched.erase(baseLayer->GetGuid()); |
| 332 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | else if (base.GetType() == LayerType::Division) |
| 336 | { |
| 337 | DivisionLayer* baseLayer = PolymorphicDowncast<DivisionLayer*>(&base); |
| 338 | |
| 339 | arm_compute::Status status = NeonDivisionWorkloadValidate( |
| 340 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 341 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 342 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 343 | &activationDesc); |
| 344 | |
| 345 | if (status) |
| 346 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 347 | FuseDivisionLayer<DivisionLayer>(optimizationViews, |
| 348 | baseLayer, |
| 349 | activationLayer, |
| 350 | activationDesc, |
| 351 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 352 | untouched.erase(baseLayer->GetGuid()); |
| 353 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | else if (base.GetType() == LayerType::Multiplication) |
| 357 | { |
| 358 | MultiplicationLayer* baseLayer = PolymorphicDowncast<MultiplicationLayer*>(&base); |
| 359 | |
| 360 | arm_compute::Status status = NeonMultiplicationWorkloadValidate( |
| 361 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 362 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 363 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 364 | &activationDesc); |
| 365 | |
| 366 | if (status) |
| 367 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 368 | FuseMultiplicationLayer<MultiplicationLayer>(optimizationViews, |
| 369 | baseLayer, |
| 370 | activationLayer, |
| 371 | activationDesc, |
| 372 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 373 | untouched.erase(baseLayer->GetGuid()); |
| 374 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | else if (base.GetType() == LayerType::Subtraction) |
| 378 | { |
| 379 | SubtractionLayer* baseLayer = PolymorphicDowncast<SubtractionLayer*>(&base); |
| 380 | |
| 381 | arm_compute::Status status = NeonSubtractionWorkloadValidate( |
| 382 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 383 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 384 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 385 | &activationDesc); |
| 386 | |
| 387 | if (status) |
| 388 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 389 | FuseSubtractionLayer<SubtractionLayer>(optimizationViews, |
| 390 | baseLayer, |
| 391 | activationLayer, |
| 392 | activationDesc, |
| 393 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 394 | untouched.erase(baseLayer->GetGuid()); |
| 395 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
Matthew Sloyan | 5fc0fd6 | 2021-05-03 12:22:03 +0100 | [diff] [blame] | 403 | |
| 404 | // Separate reduce layer with multiple axes into multiple reduce layers with 1 axis. |
| 405 | if (base.GetType() == LayerType::Reduce) |
| 406 | { |
| 407 | ReduceLayer* baseLayer = PolymorphicDowncast<ReduceLayer*>(&base); |
| 408 | ReduceDescriptor reduceDescriptor = baseLayer->GetParameters(); |
| 409 | |
| 410 | if (!reduceDescriptor.m_vAxis.empty() && reduceDescriptor.m_vAxis.size() > 1) |
| 411 | { |
| 412 | // Add new layers to the graph and connect them. |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 413 | std::vector<IConnectableLayer*> layers = ChainReduceLayers<ReduceLayer>(optimizationViews, |
| 414 | baseLayer, |
| 415 | reduceDescriptor); |
Matthew Sloyan | 5fc0fd6 | 2021-05-03 12:22:03 +0100 | [diff] [blame] | 416 | |
| 417 | // Replace existing baselayer with new subgraph. |
| 418 | ReplaceLayers<ReduceLayer>(optimizationViews, baseLayer, layers); |
| 419 | untouched.erase(baseLayer->GetGuid()); |
| 420 | } |
| 421 | } |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | if (optimizationViews.GetSubstitutions().empty()) |
| 425 | { |
| 426 | optimizationViews.AddUntouchedSubgraph(SubgraphView(subgraph)); |
| 427 | } |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 428 | else |
| 429 | { |
| 430 | ReportUntouchedLayers(optimizationViews, untouched); |
| 431 | } |
Matteo Martincigh | c3ba50e | 2019-05-22 14:28:16 +0100 | [diff] [blame] | 432 | |
| 433 | return optimizationViews; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 436 | std::vector<ITensorHandleFactory::FactoryId> NeonBackend::GetHandleFactoryPreferences() const |
| 437 | { |
Narumol Prangnawarat | 265e53e | 2020-10-30 16:06:55 +0000 | [diff] [blame] | 438 | return std::vector<ITensorHandleFactory::FactoryId>() = { NeonTensorHandleFactory::GetIdStatic() }; |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void NeonBackend::RegisterTensorHandleFactories(class TensorHandleFactoryRegistry& registry) |
| 442 | { |
| 443 | auto memoryManager = std::make_shared<NeonMemoryManager>(std::make_unique<arm_compute::Allocator>(), |
| 444 | BaseMemoryManager::MemoryAffinity::Offset); |
| 445 | |
| 446 | registry.RegisterMemoryManager(memoryManager); |
Narumol Prangnawarat | 7740045 | 2022-01-13 17:43:41 +0000 | [diff] [blame] | 447 | |
| 448 | auto factory = std::make_unique<NeonTensorHandleFactory>(memoryManager); |
| 449 | // Register copy and import factory pair |
| 450 | registry.RegisterCopyAndImportFactoryPair(factory->GetId(), factory->GetId()); |
| 451 | // Register the factory |
| 452 | registry.RegisterFactory(std::move(factory)); |
Narumol Prangnawarat | 4e3e818 | 2019-08-14 12:25:50 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Francis Murtagh | e8d7ccb | 2021-10-14 17:30:24 +0100 | [diff] [blame] | 455 | std::unique_ptr<ICustomAllocator> NeonBackend::GetDefaultAllocator() const |
| 456 | { |
| 457 | return std::make_unique<DefaultAllocator>(); |
| 458 | } |
| 459 | |
| 460 | |
Matthew Bentham | 42bad95 | 2018-12-17 09:23:36 +0000 | [diff] [blame] | 461 | } // namespace armnn |