arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 1 | // |
Mike Kelly | 3ec3077 | 2023-03-08 13:47:17 +0000 | [diff] [blame] | 2 | // Copyright © 2017-2023 Arm Ltd. All rights reserved. |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 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 Monahan | 6642b8a | 2021-11-04 16:31:46 +0000 | [diff] [blame] | 8 | #include "ClBackendDefaultAllocator.hpp" |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 9 | #include "ClBackendId.hpp" |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 10 | #include "ClBackendModelContext.hpp" |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 11 | #include "ClImportTensorHandleFactory.hpp" |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 12 | #include "ClLayerSupport.hpp" |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 13 | #include "ClTensorHandleFactory.hpp" |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 14 | #include "ClWorkloadFactory.hpp" |
arovir01 | a094479 | 2018-10-11 15:00:58 +0100 | [diff] [blame] | 15 | |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 16 | #include <armnn/BackendRegistry.hpp> |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 17 | #include <armnn/Descriptors.hpp> |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 18 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 19 | #include <aclCommon/ArmComputeSubgraphUtils.hpp> |
| 20 | #include <aclCommon/ArmComputeUtils.hpp> |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 21 | #include <aclCommon/BaseMemoryManager.hpp> |
| 22 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 23 | #include <armnn/backends/IBackendContext.hpp> |
| 24 | #include <armnn/backends/IMemoryManager.hpp> |
Jan Eilers | 3c9e045 | 2020-04-10 13:00:44 +0100 | [diff] [blame] | 25 | #include <armnn/utility/PolymorphicDowncast.hpp> |
| 26 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 27 | #include "workloads/ClAdditionWorkload.hpp" |
| 28 | #include "workloads/ClBatchNormalizationFloatWorkload.hpp" |
| 29 | #include "workloads/ClConvolution2dWorkload.hpp" |
| 30 | #include "workloads/ClDepthwiseConvolutionWorkload.hpp" |
Teresa Charlin | e11e63d | 2021-04-21 12:56:45 +0100 | [diff] [blame] | 31 | #include "workloads/ClDivisionWorkload.hpp" |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 32 | #include "workloads/ClFullyConnectedWorkload.hpp" |
| 33 | #include "workloads/ClMultiplicationWorkload.hpp" |
Matthew Sloyan | 5fc0fd6 | 2021-05-03 12:22:03 +0100 | [diff] [blame] | 34 | #include "workloads/ClReduceWorkload.hpp" |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 35 | #include "workloads/ClSubtractionWorkload.hpp" |
| 36 | |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 37 | #include <Optimizer.hpp> |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 38 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 39 | #include <arm_compute/core/Types.h> |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 40 | #include <arm_compute/runtime/CL/CLBufferAllocator.h> |
| 41 | |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 42 | namespace armnn |
| 43 | { |
| 44 | |
David Beck | 3cc9a62 | 2018-10-12 10:38:31 +0100 | [diff] [blame] | 45 | const BackendId& ClBackend::GetIdStatic() |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 46 | { |
David Beck | 3e9e115 | 2018-10-17 14:17:50 +0100 | [diff] [blame] | 47 | static const BackendId s_Id{ClBackendId()}; |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 48 | return s_Id; |
| 49 | } |
| 50 | |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 51 | IBackendInternal::IMemoryManagerUniquePtr ClBackend::CreateMemoryManager() const |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 52 | { |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 53 | if (m_UsingCustomAllocator) |
| 54 | { |
| 55 | return std::make_unique<ClMemoryManager>(m_CustomAllocator); |
| 56 | } |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 57 | return std::make_unique<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>()); |
| 58 | } |
| 59 | |
| 60 | IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory( |
| 61 | const IBackendInternal::IMemoryManagerSharedPtr& memoryManager) const |
| 62 | { |
| 63 | return std::make_unique<ClWorkloadFactory>( |
Jan Eilers | 3c9e045 | 2020-04-10 13:00:44 +0100 | [diff] [blame] | 64 | PolymorphicPointerDowncast<ClMemoryManager>(memoryManager)); |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 67 | IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory( |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 68 | const IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const ModelOptions& modelOptions) const |
| 69 | { |
| 70 | return std::make_unique<ClWorkloadFactory>( |
| 71 | PolymorphicPointerDowncast<ClMemoryManager>(memoryManager), CreateBackendSpecificModelContext(modelOptions)); |
| 72 | } |
| 73 | |
| 74 | IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory( |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 75 | TensorHandleFactoryRegistry& registry) const |
| 76 | { |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 77 | std::shared_ptr<ClMemoryManager> memoryManager; |
| 78 | if (m_UsingCustomAllocator) |
| 79 | { |
| 80 | memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>()); |
| 85 | } |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 86 | |
Narumol Prangnawarat | d12b407 | 2022-01-17 18:03:14 +0000 | [diff] [blame] | 87 | std::unique_ptr<ITensorHandleFactory> factory = std::make_unique<ClTensorHandleFactory>(memoryManager); |
| 88 | std::unique_ptr<ITensorHandleFactory> importFactory = std::make_unique<ClImportTensorHandleFactory>( |
| 89 | static_cast<MemorySourceFlags>(MemorySource::Malloc), static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 90 | |
| 91 | registry.RegisterCopyAndImportFactoryPair(factory->GetId(), importFactory->GetId()); |
| 92 | registry.RegisterCopyAndImportFactoryPair(importFactory->GetId(), factory->GetId()); |
| 93 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 94 | registry.RegisterMemoryManager(memoryManager); |
Narumol Prangnawarat | d12b407 | 2022-01-17 18:03:14 +0000 | [diff] [blame] | 95 | registry.RegisterFactory(std::move(factory)); |
| 96 | registry.RegisterFactory(std::move(importFactory)); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 97 | |
| 98 | return std::make_unique<ClWorkloadFactory>( |
Jan Eilers | 3c9e045 | 2020-04-10 13:00:44 +0100 | [diff] [blame] | 99 | PolymorphicPointerDowncast<ClMemoryManager>(memoryManager)); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 102 | IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory( |
| 103 | TensorHandleFactoryRegistry& registry, const ModelOptions& modelOptions) const |
| 104 | { |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 105 | std::shared_ptr<ClMemoryManager> memoryManager; |
| 106 | if (m_UsingCustomAllocator) |
| 107 | { |
| 108 | memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>()); |
| 113 | } |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 114 | |
Narumol Prangnawarat | d12b407 | 2022-01-17 18:03:14 +0000 | [diff] [blame] | 115 | std::unique_ptr<ITensorHandleFactory> factory = std::make_unique<ClTensorHandleFactory>(memoryManager); |
| 116 | std::unique_ptr<ITensorHandleFactory> importFactory = std::make_unique<ClImportTensorHandleFactory>( |
| 117 | static_cast<MemorySourceFlags>(MemorySource::Malloc), static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 118 | |
| 119 | registry.RegisterCopyAndImportFactoryPair(factory->GetId(), importFactory->GetId()); |
| 120 | registry.RegisterCopyAndImportFactoryPair(importFactory->GetId(), factory->GetId()); |
| 121 | |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 122 | registry.RegisterMemoryManager(memoryManager); |
Narumol Prangnawarat | d12b407 | 2022-01-17 18:03:14 +0000 | [diff] [blame] | 123 | registry.RegisterFactory(std::move(factory)); |
| 124 | registry.RegisterFactory(std::move(importFactory)); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 125 | |
| 126 | return std::make_unique<ClWorkloadFactory>( |
| 127 | PolymorphicPointerDowncast<ClMemoryManager>(memoryManager), CreateBackendSpecificModelContext(modelOptions)); |
| 128 | } |
| 129 | |
| 130 | IBackendInternal::IWorkloadFactoryPtr ClBackend::CreateWorkloadFactory( |
| 131 | TensorHandleFactoryRegistry& registry, |
| 132 | const ModelOptions& modelOptions, |
| 133 | MemorySourceFlags inputFlags, |
| 134 | MemorySourceFlags outputFlags) const |
| 135 | { |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 136 | // To allow force import if inputFlags/outputFlags are Undefined, set it as Malloc |
| 137 | if (inputFlags == static_cast<MemorySourceFlags>(MemorySource::Undefined)) |
| 138 | { |
| 139 | inputFlags = static_cast<MemorySourceFlags>(MemorySource::Malloc); |
| 140 | } |
| 141 | if (outputFlags == static_cast<MemorySourceFlags>(MemorySource::Undefined)) |
| 142 | { |
| 143 | outputFlags = static_cast<MemorySourceFlags>(MemorySource::Malloc); |
| 144 | } |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 145 | std::shared_ptr<ClMemoryManager> memoryManager; |
| 146 | if (m_UsingCustomAllocator) |
| 147 | { |
| 148 | memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>()); |
| 153 | } |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 154 | |
Narumol Prangnawarat | d12b407 | 2022-01-17 18:03:14 +0000 | [diff] [blame] | 155 | std::unique_ptr<ITensorHandleFactory> factory = std::make_unique<ClTensorHandleFactory>(memoryManager); |
| 156 | std::unique_ptr<ITensorHandleFactory> importFactory = std::make_unique<ClImportTensorHandleFactory>( |
| 157 | inputFlags, outputFlags); |
| 158 | |
| 159 | registry.RegisterCopyAndImportFactoryPair(factory->GetId(), importFactory->GetId()); |
| 160 | registry.RegisterCopyAndImportFactoryPair(importFactory->GetId(), factory->GetId()); |
| 161 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 162 | registry.RegisterMemoryManager(memoryManager); |
Narumol Prangnawarat | d12b407 | 2022-01-17 18:03:14 +0000 | [diff] [blame] | 163 | registry.RegisterFactory(std::move(factory)); |
| 164 | registry.RegisterFactory(std::move(importFactory)); |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 165 | |
| 166 | return std::make_unique<ClWorkloadFactory>( |
| 167 | PolymorphicPointerDowncast<ClMemoryManager>(memoryManager), CreateBackendSpecificModelContext(modelOptions)); |
| 168 | } |
| 169 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 170 | std::vector<ITensorHandleFactory::FactoryId> ClBackend::GetHandleFactoryPreferences() const |
| 171 | { |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 172 | return std::vector<ITensorHandleFactory::FactoryId> {ClTensorHandleFactory::GetIdStatic(), |
| 173 | ClImportTensorHandleFactory::GetIdStatic()}; |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void ClBackend::RegisterTensorHandleFactories(TensorHandleFactoryRegistry& registry) |
| 177 | { |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 178 | std::shared_ptr<ClMemoryManager> memoryManager; |
| 179 | if (m_UsingCustomAllocator) |
| 180 | { |
| 181 | memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>()); |
| 186 | } |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 187 | |
Narumol Prangnawarat | d12b407 | 2022-01-17 18:03:14 +0000 | [diff] [blame] | 188 | std::unique_ptr<ITensorHandleFactory> factory = std::make_unique<ClTensorHandleFactory>(memoryManager); |
| 189 | std::unique_ptr<ITensorHandleFactory> importFactory = std::make_unique<ClImportTensorHandleFactory>( |
| 190 | static_cast<MemorySourceFlags>(MemorySource::Malloc), static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 191 | |
| 192 | registry.RegisterCopyAndImportFactoryPair(factory->GetId(), importFactory->GetId()); |
| 193 | registry.RegisterCopyAndImportFactoryPair(importFactory->GetId(), factory->GetId()); |
| 194 | |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 195 | registry.RegisterMemoryManager(memoryManager); |
Narumol Prangnawarat | d12b407 | 2022-01-17 18:03:14 +0000 | [diff] [blame] | 196 | registry.RegisterFactory(std::move(factory)); |
| 197 | registry.RegisterFactory(std::move(importFactory)); |
| 198 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void ClBackend::RegisterTensorHandleFactories(TensorHandleFactoryRegistry& registry, |
| 202 | MemorySourceFlags inputFlags, |
| 203 | MemorySourceFlags outputFlags) |
| 204 | { |
Narumol Prangnawarat | e2af6f4 | 2022-01-28 17:59:18 +0000 | [diff] [blame] | 205 | // To allow force import if inputFlags/outputFlags are Undefined, set it as Malloc |
| 206 | if (inputFlags == static_cast<MemorySourceFlags>(MemorySource::Undefined)) |
| 207 | { |
| 208 | inputFlags = static_cast<MemorySourceFlags>(MemorySource::Malloc); |
| 209 | } |
| 210 | if (outputFlags == static_cast<MemorySourceFlags>(MemorySource::Undefined)) |
| 211 | { |
| 212 | outputFlags = static_cast<MemorySourceFlags>(MemorySource::Malloc); |
| 213 | } |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 214 | std::shared_ptr<ClMemoryManager> memoryManager; |
| 215 | if (m_UsingCustomAllocator) |
| 216 | { |
| 217 | memoryManager = std::make_shared<ClMemoryManager>(m_CustomAllocator); |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | memoryManager = std::make_shared<ClMemoryManager>(std::make_unique<arm_compute::CLBufferAllocator>()); |
| 222 | } |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 223 | |
Narumol Prangnawarat | d12b407 | 2022-01-17 18:03:14 +0000 | [diff] [blame] | 224 | std::unique_ptr<ITensorHandleFactory> factory = std::make_unique<ClTensorHandleFactory>(memoryManager); |
| 225 | std::unique_ptr<ITensorHandleFactory> importFactory = std::make_unique<ClImportTensorHandleFactory>( |
| 226 | inputFlags, outputFlags); |
| 227 | |
| 228 | registry.RegisterCopyAndImportFactoryPair(factory->GetId(), importFactory->GetId()); |
| 229 | registry.RegisterCopyAndImportFactoryPair(importFactory->GetId(), factory->GetId()); |
| 230 | |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 231 | registry.RegisterMemoryManager(memoryManager); |
Narumol Prangnawarat | d12b407 | 2022-01-17 18:03:14 +0000 | [diff] [blame] | 232 | registry.RegisterFactory(std::move(factory)); |
| 233 | registry.RegisterFactory(std::move(importFactory)); |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 236 | IBackendInternal::IBackendContextPtr ClBackend::CreateBackendContext(const IRuntime::CreationOptions& options) const |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 237 | { |
| 238 | return IBackendContextPtr{new ClBackendContext{options}}; |
| 239 | } |
| 240 | |
Colm Donelan | e49755b | 2020-01-29 15:22:43 +0000 | [diff] [blame] | 241 | IBackendInternal::IBackendProfilingContextPtr ClBackend::CreateBackendProfilingContext( |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 242 | const IRuntime::CreationOptions&, IBackendProfilingPtr&) |
Colm Donelan | e49755b | 2020-01-29 15:22:43 +0000 | [diff] [blame] | 243 | { |
| 244 | return IBackendProfilingContextPtr{}; |
| 245 | } |
| 246 | |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 247 | IBackendInternal::IBackendSpecificModelContextPtr ClBackend::CreateBackendSpecificModelContext( |
| 248 | const ModelOptions& modelOptions) const |
| 249 | { |
| 250 | return IBackendSpecificModelContextPtr{new ClBackendModelContext{modelOptions}}; |
| 251 | } |
| 252 | |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 253 | IBackendInternal::ILayerSupportSharedPtr ClBackend::GetLayerSupport() const |
| 254 | { |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 255 | static ILayerSupportSharedPtr layerSupport |
| 256 | { |
| 257 | new ClLayerSupport(IBackendInternal::IBackendSpecificModelContextPtr{}) |
| 258 | }; |
| 259 | return layerSupport; |
| 260 | } |
| 261 | |
| 262 | IBackendInternal::ILayerSupportSharedPtr ClBackend::GetLayerSupport(const ModelOptions& modelOptions) const |
| 263 | { |
| 264 | static ILayerSupportSharedPtr layerSupport |
| 265 | { |
| 266 | new ClLayerSupport(CreateBackendSpecificModelContext(modelOptions)) |
| 267 | }; |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 268 | return layerSupport; |
| 269 | } |
| 270 | |
David Monahan | 6642b8a | 2021-11-04 16:31:46 +0000 | [diff] [blame] | 271 | std::unique_ptr<ICustomAllocator> ClBackend::GetDefaultAllocator() const |
| 272 | { |
| 273 | return std::make_unique<ClBackendDefaultAllocator>(); |
| 274 | } |
| 275 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 276 | OptimizationViews ClBackend::OptimizeSubgraphView(const SubgraphView& subgraph, |
| 277 | const ModelOptions& modelOptions) const |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 278 | { |
Mike Kelly | 80512b0 | 2022-05-16 23:10:42 +0100 | [diff] [blame] | 279 | OptimizationViews optimizationViews(modelOptions); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 280 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 281 | auto it = subgraph.endIConnectable(); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 282 | bool isFastMathEnabled = false; |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 283 | std::map<LayerGuid, Layer*> untouched; |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 284 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 285 | while (it != subgraph.beginIConnectable()) |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 286 | { |
| 287 | --it; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 288 | Layer& base = *(PolymorphicDowncast<Layer*>(*it)); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 289 | untouched.insert({base.GetGuid(), &base}); |
| 290 | } |
| 291 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 292 | it = subgraph.endIConnectable(); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 293 | #if defined(ARMCOMPUTECL_ENABLED) |
| 294 | IBackendInternal::IBackendSpecificModelContextPtr modelContextPtr = CreateBackendSpecificModelContext(modelOptions); |
| 295 | |
| 296 | if (modelContextPtr) |
| 297 | { |
| 298 | auto clModelOptions = dynamic_cast<ClBackendModelContext*>(modelContextPtr.get()); |
| 299 | if (clModelOptions) |
| 300 | { |
| 301 | isFastMathEnabled = clModelOptions->IsFastMathEnabled(); |
| 302 | } |
| 303 | } |
| 304 | #endif |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 305 | while (it != subgraph.beginIConnectable()) |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 306 | { |
| 307 | --it; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 308 | Layer& base = *(PolymorphicDowncast<Layer*>(*it)); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 309 | |
Matthew Sloyan | 5fc0fd6 | 2021-05-03 12:22:03 +0100 | [diff] [blame] | 310 | // Fuse activation into previous layer if supported by backend |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 311 | if ((base.GetType() == LayerType::DepthwiseConvolution2d || base.GetType() == LayerType::Convolution2d |
| 312 | || base.GetType() == LayerType::BatchNormalization || base.GetType() == LayerType::FullyConnected |
| 313 | || base.GetType() == LayerType::Addition || base.GetType() == LayerType::Multiplication |
Mike Kelly | 3ec3077 | 2023-03-08 13:47:17 +0000 | [diff] [blame] | 314 | || base.GetType() == LayerType::Subtraction || base.GetType() == LayerType::Division |
| 315 | || base.GetType() == LayerType::ElementwiseBinary) |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 316 | && (base.GetAdditionalInformation<ActivationDescriptor>() == nullptr)) |
| 317 | { |
| 318 | for (auto output = base.BeginOutputSlots(); output != base.EndOutputSlots(); ++output) |
| 319 | { |
| 320 | if (output->GetNumConnections() == 1) |
| 321 | { |
| 322 | for (auto&& childInput : output->GetConnections()) |
| 323 | { |
Teresa Charlin | d672f5d | 2021-01-18 18:07:57 +0000 | [diff] [blame] | 324 | if ((childInput->GetOwningLayer().GetType() == LayerType::Activation) && |
| 325 | (checkDataTypeInputandOutput(childInput->GetOwningLayer()))) |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 326 | { |
| 327 | Layer& child = childInput->GetOwningLayer(); |
| 328 | |
| 329 | auto* activationLayer = PolymorphicDowncast<ActivationLayer*>(&child); |
| 330 | |
| 331 | const std::string name = std::string("fused-") + child.GetName() + std::string("-into-") + |
| 332 | base.GetName(); |
| 333 | |
| 334 | // Get params from activation layer |
| 335 | ActivationDescriptor activationDesc = activationLayer->GetParameters(); |
| 336 | |
| 337 | if (base.GetType() == LayerType::Convolution2d) |
| 338 | { |
| 339 | Convolution2dLayer* baseLayer = PolymorphicDowncast<Convolution2dLayer*>(&base); |
| 340 | |
| 341 | Optional<TensorInfo> biases; |
| 342 | |
| 343 | if (baseLayer->GetParameters().m_BiasEnabled) |
| 344 | { |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 345 | biases = baseLayer->GetInputSlot(2).GetConnectedOutputSlot()->GetTensorInfo(); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | arm_compute::Status status = ClConvolution2dWorkloadValidate( |
| 349 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 350 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 351 | baseLayer->GetParameters(), |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 352 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 353 | biases, |
| 354 | isFastMathEnabled, |
| 355 | &activationDesc); |
| 356 | |
| 357 | if (status) |
| 358 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 359 | FuseConvolution2dLayer<Convolution2dLayer>(optimizationViews, |
| 360 | baseLayer, |
| 361 | activationLayer, |
| 362 | activationDesc, |
| 363 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 364 | untouched.erase(baseLayer->GetGuid()); |
| 365 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | else if (base.GetType() == LayerType::DepthwiseConvolution2d) |
| 369 | { |
| 370 | DepthwiseConvolution2dLayer* baseLayer = |
| 371 | PolymorphicDowncast<DepthwiseConvolution2dLayer*>(&base); |
| 372 | |
| 373 | Optional<TensorInfo> biases; |
| 374 | |
| 375 | if (baseLayer->GetParameters().m_BiasEnabled) |
| 376 | { |
Cathal Corbett | 0690265 | 2022-04-14 17:55:11 +0100 | [diff] [blame] | 377 | biases = baseLayer->GetInputSlot(2).GetConnectedOutputSlot()->GetTensorInfo(); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | arm_compute::Status status = ClDepthwiseConvolutionWorkloadValidate( |
| 381 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 382 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 383 | baseLayer->GetParameters(), |
Cathal Corbett | 0690265 | 2022-04-14 17:55:11 +0100 | [diff] [blame] | 384 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 385 | biases, |
| 386 | &activationDesc); |
| 387 | |
| 388 | if (status) |
| 389 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 390 | FuseDepthwiseConvolution2dLayer<DepthwiseConvolution2dLayer>(optimizationViews, |
| 391 | baseLayer, |
| 392 | activationLayer, |
| 393 | activationDesc, |
| 394 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 395 | untouched.erase(baseLayer->GetGuid()); |
| 396 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | else if (base.GetType() == LayerType::FullyConnected) |
| 400 | { |
| 401 | FullyConnectedLayer* baseLayer = PolymorphicDowncast<FullyConnectedLayer*>(&base); |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 402 | FullyConnectedDescriptor descriptor = baseLayer->GetParameters(); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 403 | |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 404 | // As bias is optional only try to get TensorInfo from input if bias is enabled. |
Matthew Bentham | 67d6390 | 2022-02-08 15:03:07 +0000 | [diff] [blame] | 405 | Optional<TensorInfo> biases; |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 406 | if (descriptor.m_BiasEnabled) |
Matthew Bentham | 67d6390 | 2022-02-08 15:03:07 +0000 | [diff] [blame] | 407 | { |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 408 | biases = baseLayer->GetInputSlot(2).GetConnectedOutputSlot()->GetTensorInfo(); |
Matthew Bentham | 67d6390 | 2022-02-08 15:03:07 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 411 | arm_compute::Status status = ClFullyConnectedWorkloadValidate( |
| 412 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 413 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
Cathal Corbett | 4452baf | 2022-05-13 09:55:59 +0100 | [diff] [blame] | 414 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
Matthew Bentham | 67d6390 | 2022-02-08 15:03:07 +0000 | [diff] [blame] | 415 | biases, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 416 | baseLayer->GetParameters(), |
| 417 | &activationDesc); |
| 418 | |
| 419 | if (status) |
| 420 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 421 | FuseFullyConnectedLayer<FullyConnectedLayer>(optimizationViews, |
| 422 | baseLayer, |
| 423 | activationLayer, |
| 424 | activationDesc, |
| 425 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 426 | untouched.erase(baseLayer->GetGuid()); |
| 427 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | else if (base.GetType() == LayerType::BatchNormalization) |
| 431 | { |
| 432 | BatchNormalizationLayer* baseLayer = |
| 433 | PolymorphicDowncast<BatchNormalizationLayer*>(&base); |
| 434 | |
| 435 | arm_compute::Status status = ClBatchNormalizationValidate( |
| 436 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 437 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 438 | baseLayer->m_Mean->GetTensorInfo(), |
| 439 | baseLayer->m_Variance->GetTensorInfo(), |
| 440 | baseLayer->m_Beta->GetTensorInfo(), |
| 441 | baseLayer->m_Gamma->GetTensorInfo(), |
| 442 | baseLayer->GetParameters(), |
| 443 | &activationDesc); |
| 444 | |
| 445 | if (status) |
| 446 | { |
| 447 | BatchNormalizationLayer* replacementLayer = |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 448 | FuseBatchNormalizationLayer<BatchNormalizationLayer>(optimizationViews, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 449 | baseLayer, |
| 450 | activationLayer, |
| 451 | activationDesc, |
| 452 | name); |
| 453 | |
| 454 | replacementLayer->m_Beta = std::move(baseLayer->m_Beta); |
| 455 | replacementLayer->m_Gamma = std::move(baseLayer->m_Gamma); |
| 456 | replacementLayer->m_Mean = std::move(baseLayer->m_Mean); |
| 457 | replacementLayer->m_Variance = std::move(baseLayer->m_Variance); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 458 | untouched.erase(baseLayer->GetGuid()); |
| 459 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | else if (base.GetType() == LayerType::Addition) |
| 463 | { |
Mike Kelly | 52e90bf | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 464 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 465 | AdditionLayer* baseLayer = PolymorphicDowncast<AdditionLayer*>(&base); |
| 466 | |
| 467 | arm_compute::Status status = ClAdditionValidate( |
| 468 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 469 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 470 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 471 | &activationDesc); |
| 472 | |
| 473 | if (status) |
| 474 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 475 | FuseAdditionLayer<AdditionLayer>(optimizationViews, |
| 476 | baseLayer, |
| 477 | activationLayer, |
| 478 | activationDesc, |
| 479 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 480 | untouched.erase(baseLayer->GetGuid()); |
| 481 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 482 | } |
Mike Kelly | 52e90bf | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 483 | ARMNN_NO_DEPRECATE_WARN_END |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 484 | } |
| 485 | else if (base.GetType() == LayerType::Division) |
| 486 | { |
Mike Kelly | 52e90bf | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 487 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 488 | DivisionLayer* baseLayer = PolymorphicDowncast<DivisionLayer*>(&base); |
| 489 | |
| 490 | arm_compute::Status status = ClDivisionWorkloadValidate( |
| 491 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 492 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 493 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 494 | &activationDesc); |
| 495 | |
| 496 | if (status) |
| 497 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 498 | FuseDivisionLayer<DivisionLayer>(optimizationViews, |
| 499 | baseLayer, |
| 500 | activationLayer, |
| 501 | activationDesc, |
| 502 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 503 | untouched.erase(baseLayer->GetGuid()); |
| 504 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 505 | } |
Mike Kelly | 52e90bf | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 506 | ARMNN_NO_DEPRECATE_WARN_END |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 507 | } |
| 508 | else if (base.GetType() == LayerType::Multiplication) |
| 509 | { |
Mike Kelly | 52e90bf | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 510 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 511 | MultiplicationLayer* baseLayer = PolymorphicDowncast<MultiplicationLayer*>(&base); |
| 512 | |
| 513 | arm_compute::Status status = ClMultiplicationWorkloadValidate( |
| 514 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 515 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 516 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 517 | &activationDesc); |
| 518 | |
| 519 | if (status) |
| 520 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 521 | FuseMultiplicationLayer<MultiplicationLayer>(optimizationViews, |
| 522 | baseLayer, |
| 523 | activationLayer, |
| 524 | activationDesc, |
| 525 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 526 | untouched.erase(baseLayer->GetGuid()); |
| 527 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 528 | } |
Mike Kelly | 52e90bf | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 529 | ARMNN_NO_DEPRECATE_WARN_END |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 530 | } |
| 531 | else if (base.GetType() == LayerType::Subtraction) |
| 532 | { |
Mike Kelly | 52e90bf | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 533 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 534 | SubtractionLayer* baseLayer = PolymorphicDowncast<SubtractionLayer*>(&base); |
| 535 | |
| 536 | arm_compute::Status status = ClSubtractionValidate( |
| 537 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 538 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 539 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 540 | &activationDesc); |
| 541 | |
| 542 | if (status) |
| 543 | { |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 544 | FuseSubtractionLayer<SubtractionLayer>(optimizationViews, |
| 545 | baseLayer, |
| 546 | activationLayer, |
| 547 | activationDesc, |
| 548 | name); |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 549 | untouched.erase(baseLayer->GetGuid()); |
| 550 | untouched.erase(activationLayer->GetGuid()); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 551 | } |
Mike Kelly | 52e90bf | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 552 | ARMNN_NO_DEPRECATE_WARN_END |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 553 | } |
Mike Kelly | 3ec3077 | 2023-03-08 13:47:17 +0000 | [diff] [blame] | 554 | else if (base.GetType() == LayerType::ElementwiseBinary) |
| 555 | { |
| 556 | ElementwiseBinaryLayer* baseLayer = PolymorphicDowncast<ElementwiseBinaryLayer*>(&base); |
| 557 | |
| 558 | if (baseLayer->GetParameters().m_Operation == BinaryOperation::Add) |
| 559 | { |
| 560 | arm_compute::Status status = ClAdditionValidate( |
| 561 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 562 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 563 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 564 | &activationDesc); |
| 565 | |
| 566 | if (status) |
| 567 | { |
| 568 | FuseElementwiseBinaryLayer<ElementwiseBinaryLayer>(optimizationViews, |
| 569 | baseLayer, |
| 570 | activationLayer, |
| 571 | activationDesc, |
| 572 | BinaryOperation::Add, |
| 573 | name); |
| 574 | untouched.erase(baseLayer->GetGuid()); |
| 575 | untouched.erase(activationLayer->GetGuid()); |
| 576 | } |
| 577 | } |
| 578 | else if (baseLayer->GetParameters().m_Operation == BinaryOperation::Div) |
| 579 | { |
| 580 | arm_compute::Status status = ClDivisionWorkloadValidate( |
| 581 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 582 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 583 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 584 | &activationDesc); |
| 585 | |
| 586 | if (status) |
| 587 | { |
| 588 | FuseElementwiseBinaryLayer<ElementwiseBinaryLayer>(optimizationViews, |
| 589 | baseLayer, |
| 590 | activationLayer, |
| 591 | activationDesc, |
| 592 | BinaryOperation::Div, |
| 593 | name); |
| 594 | untouched.erase(baseLayer->GetGuid()); |
| 595 | untouched.erase(activationLayer->GetGuid()); |
| 596 | } |
| 597 | } |
| 598 | else if (baseLayer->GetParameters().m_Operation == BinaryOperation::Mul) |
| 599 | { |
| 600 | arm_compute::Status status = ClMultiplicationWorkloadValidate( |
| 601 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 602 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 603 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 604 | &activationDesc); |
| 605 | |
| 606 | if (status) |
| 607 | { |
| 608 | FuseElementwiseBinaryLayer<ElementwiseBinaryLayer>(optimizationViews, |
| 609 | baseLayer, |
| 610 | activationLayer, |
| 611 | activationDesc, |
| 612 | BinaryOperation::Mul, |
| 613 | name); |
| 614 | untouched.erase(baseLayer->GetGuid()); |
| 615 | untouched.erase(activationLayer->GetGuid()); |
| 616 | } |
| 617 | } |
| 618 | else if (baseLayer->GetParameters().m_Operation == BinaryOperation::Sub) |
| 619 | { |
| 620 | arm_compute::Status status = ClSubtractionValidate( |
| 621 | baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 622 | baseLayer->GetInputSlot(1).GetConnectedOutputSlot()->GetTensorInfo(), |
| 623 | activationLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo(), |
| 624 | &activationDesc); |
| 625 | |
| 626 | if (status) |
| 627 | { |
| 628 | FuseElementwiseBinaryLayer<ElementwiseBinaryLayer>(optimizationViews, |
| 629 | baseLayer, |
| 630 | activationLayer, |
| 631 | activationDesc, |
| 632 | BinaryOperation::Sub, |
| 633 | name); |
| 634 | } |
| 635 | } |
| 636 | // No fusion available for other BinaryOperations |
| 637 | } |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | } |
Matthew Sloyan | 5fc0fd6 | 2021-05-03 12:22:03 +0100 | [diff] [blame] | 643 | |
| 644 | // Separate reduce layer with multiple axes into multiple reduce layers with 1 axis. |
| 645 | if (base.GetType() == LayerType::Reduce) |
| 646 | { |
| 647 | ReduceLayer* baseLayer = PolymorphicDowncast<ReduceLayer*>(&base); |
| 648 | ReduceDescriptor reduceDescriptor = baseLayer->GetParameters(); |
| 649 | |
| 650 | if (!reduceDescriptor.m_vAxis.empty() && reduceDescriptor.m_vAxis.size() > 1) |
| 651 | { |
| 652 | // Add new layers to the graph and connect them. |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 653 | std::vector<IConnectableLayer*> layers = ChainReduceLayers<ReduceLayer>(optimizationViews, |
| 654 | baseLayer, |
| 655 | reduceDescriptor); |
Matthew Sloyan | 5fc0fd6 | 2021-05-03 12:22:03 +0100 | [diff] [blame] | 656 | |
| 657 | // Replace existing baselayer with new subgraph. |
| 658 | ReplaceLayers<ReduceLayer>(optimizationViews, baseLayer, layers); |
| 659 | untouched.erase(baseLayer->GetGuid()); |
| 660 | } |
| 661 | } |
Cathal Corbett | 3883b27 | 2022-07-22 16:03:36 +0100 | [diff] [blame] | 662 | |
| 663 | // Special case to fuse padding into average pooling 2d for quantized datatype. |
| 664 | // Required to be done as a backend specific optimization as Neon does not support this special case. |
| 665 | if (base.GetType() == LayerType::Pooling2d) |
| 666 | { |
| 667 | Pooling2dLayer* baseLayer = PolymorphicDowncast<Pooling2dLayer*>(&base); |
| 668 | Pooling2dDescriptor poolingDescriptor = baseLayer->GetParameters(); |
| 669 | |
| 670 | if (baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer().GetType() == LayerType::Pad) |
| 671 | { |
| 672 | PadLayer* padLayer = PolymorphicDowncast<PadLayer*>( |
| 673 | &baseLayer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer()); |
| 674 | if (padLayer->GetOutputSlot(0).GetNumConnections() == 1 && |
| 675 | optimizations::pad_fold::TryFoldPadIntoLayer2d(padLayer->GetParameters(), |
| 676 | poolingDescriptor, |
| 677 | padLayer->GetOutputSlot().GetTensorInfo(), |
| 678 | true)) |
| 679 | { |
| 680 | FoldPadIntoAveragePool2d<Pooling2dLayer>(optimizationViews, baseLayer, |
| 681 | poolingDescriptor, padLayer); |
| 682 | untouched.erase(baseLayer->GetGuid()); |
| 683 | untouched.erase(padLayer->GetGuid()); |
| 684 | } |
| 685 | } |
| 686 | } |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 687 | } |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 688 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 689 | if (optimizationViews.GetSubstitutions().empty()) |
| 690 | { |
| 691 | optimizationViews.AddUntouchedSubgraph(SubgraphView(subgraph)); |
| 692 | } |
Mike Kelly | 1ac690a | 2020-11-17 11:41:38 +0000 | [diff] [blame] | 693 | else |
| 694 | { |
| 695 | ReportUntouchedLayers(optimizationViews, untouched); |
| 696 | } |
Matteo Martincigh | c3ba50e | 2019-05-22 14:28:16 +0100 | [diff] [blame] | 697 | |
| 698 | return optimizationViews; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 699 | } |
| 700 | |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 701 | } // namespace armnn |