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