Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Tianle Cheng | 2828818 | 2024-02-23 17:56:54 +0000 | [diff] [blame^] | 2 | // Copyright © 2017-2024 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 5 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 6 | #include "Network.hpp" |
| 7 | #include "Graph.hpp" |
| 8 | #include "Layer.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 9 | #include "DeviceSpec.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 10 | #include "Optimizer.hpp" |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 11 | #include "SubgraphViewSelector.hpp" |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 12 | #include "BackendSettings.hpp" |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 13 | #include "optimizations/All.hpp" |
Keith Davis | f63b457 | 2022-10-19 14:53:05 +0100 | [diff] [blame] | 14 | #include "armnnUtils/Filesystem.hpp" |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 15 | #include "armnn/utility/Timer.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 16 | |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 17 | #include <armnn/backends/TensorHandle.hpp> |
| 18 | #include <armnn/backends/WorkloadFactory.hpp> |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 19 | #include <armnn/backends/IBackendInternal.hpp> |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 20 | #include <backendsCommon/TensorHandleFactoryRegistry.hpp> |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 21 | |
| 22 | #include <armnn/Exceptions.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 23 | #include <armnn/TypesUtils.hpp> |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 24 | #include <armnn/BackendRegistry.hpp> |
Matthew Bentham | f48afc6 | 2020-01-15 17:55:08 +0000 | [diff] [blame] | 25 | #include <armnn/Logging.hpp> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 26 | #include <armnn/utility/Assert.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 27 | #include <armnn/utility/IgnoreUnused.hpp> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 28 | #include <armnn/utility/PolymorphicDowncast.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 29 | |
Jim Flynn | 2776183 | 2022-03-20 21:52:17 +0000 | [diff] [blame] | 30 | #include <client/include/IProfilingService.hpp> |
Jan Eilers | 99d9d4a | 2019-11-06 10:02:16 +0000 | [diff] [blame] | 31 | |
Nikhil Raj | 77fe76b | 2021-06-09 14:55:32 +0100 | [diff] [blame] | 32 | #include <common/include/ProfilingGuid.hpp> |
| 33 | |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 34 | #include <fmt/format.h> |
| 35 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 36 | #include <fcntl.h> |
| 37 | #include <algorithm> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 38 | #include <memory> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 39 | #include <vector> |
Ryan OShea | b4c4934 | 2023-07-25 14:28:27 +0100 | [diff] [blame] | 40 | #include <armnn/ArmNN.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 41 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 42 | namespace armnn |
| 43 | { |
| 44 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 45 | INetwork::INetwork(NetworkOptions networkOptions) : pNetworkImpl(new NetworkImpl(networkOptions)) {} |
| 46 | |
| 47 | INetwork::~INetwork() = default; |
| 48 | |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 49 | OptimizerOptionsOpaque::OptimizerOptionsOpaque() |
| 50 | : p_OptimizerOptionsImpl(std::make_unique<OptimizerOptionsOpaqueImpl>()) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | OptimizerOptionsOpaque::OptimizerOptionsOpaque(OptimizerOptionsOpaque const &other) |
| 55 | : p_OptimizerOptionsImpl(std::make_unique<OptimizerOptionsOpaqueImpl>(*other.p_OptimizerOptionsImpl)) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | OptimizerOptionsOpaque::~OptimizerOptionsOpaque() = default; |
| 60 | |
| 61 | OptimizerOptionsOpaque::OptimizerOptionsOpaque(bool reduceFp32ToFp16, bool debug, bool reduceFp32ToBf16, |
| 62 | bool importEnabled, ModelOptions modelOptions, bool exportEnabled, |
| 63 | bool debugToFile) |
| 64 | : p_OptimizerOptionsImpl(std::make_unique<OptimizerOptionsOpaqueImpl>(reduceFp32ToFp16, debug, reduceFp32ToBf16, |
| 65 | importEnabled, modelOptions, |
| 66 | exportEnabled, debugToFile)) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | OptimizerOptionsOpaque::OptimizerOptionsOpaque(bool reduceFp32ToFp16, bool debug, bool reduceFp32ToBf16, |
| 71 | ShapeInferenceMethod shapeInferenceMethod, |
| 72 | bool importEnabled, ModelOptions modelOptions, bool exportEnabled, |
| 73 | bool debugToFile, bool allowExpandedDims) |
| 74 | : p_OptimizerOptionsImpl(std::make_unique<OptimizerOptionsOpaqueImpl>(reduceFp32ToFp16, debug, reduceFp32ToBf16, |
| 75 | shapeInferenceMethod, importEnabled, |
| 76 | modelOptions, exportEnabled, |
| 77 | debugToFile, allowExpandedDims)) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | OptimizerOptionsOpaque::OptimizerOptionsOpaque(const OptimizerOptions& OptimizerStruct) |
| 82 | : p_OptimizerOptionsImpl(std::make_unique<OptimizerOptionsOpaqueImpl>()) |
| 83 | { |
| 84 | p_OptimizerOptionsImpl->m_ImportEnabled = OptimizerStruct.m_ImportEnabled; |
| 85 | p_OptimizerOptionsImpl->m_shapeInferenceMethod = OptimizerStruct.m_shapeInferenceMethod; |
| 86 | p_OptimizerOptionsImpl->m_ModelOptions = OptimizerStruct.m_ModelOptions; |
| 87 | p_OptimizerOptionsImpl->m_ProfilingEnabled = OptimizerStruct.m_ProfilingEnabled; |
| 88 | p_OptimizerOptionsImpl->m_DebugToFile = OptimizerStruct.m_DebugToFile; |
| 89 | p_OptimizerOptionsImpl->m_Debug = OptimizerStruct.m_Debug; |
| 90 | p_OptimizerOptionsImpl->m_ReduceFp32ToFp16 = OptimizerStruct.m_ReduceFp32ToFp16; |
| 91 | p_OptimizerOptionsImpl->m_ExportEnabled = OptimizerStruct.m_ExportEnabled; |
| 92 | p_OptimizerOptionsImpl->m_AllowExpandedDims = OptimizerStruct.m_AllowExpandedDims; |
| 93 | p_OptimizerOptionsImpl->m_ReduceFp32ToBf16 = OptimizerStruct.m_ReduceFp32ToBf16; |
| 94 | } |
| 95 | |
| 96 | OptimizerOptionsOpaque& OptimizerOptionsOpaque::operator= (OptimizerOptionsOpaque other) |
| 97 | { |
| 98 | p_OptimizerOptionsImpl->m_ImportEnabled = other.GetImportEnabled(); |
| 99 | p_OptimizerOptionsImpl->m_shapeInferenceMethod = other.GetShapeInferenceMethod(); |
| 100 | p_OptimizerOptionsImpl->m_ModelOptions = other.GetModelOptions(); |
| 101 | p_OptimizerOptionsImpl->m_ProfilingEnabled = other.GetProfilingEnabled(); |
| 102 | p_OptimizerOptionsImpl->m_DebugToFile = other.GetDebugToFileEnabled(); |
| 103 | p_OptimizerOptionsImpl->m_Debug = other.GetDebugEnabled(); |
| 104 | p_OptimizerOptionsImpl->m_ReduceFp32ToFp16 = other.GetReduceFp32ToFp16(); |
| 105 | p_OptimizerOptionsImpl->m_ExportEnabled = other.GetExportEnabled(); |
| 106 | p_OptimizerOptionsImpl->m_AllowExpandedDims = other.GetAllowExpandedDims(); |
| 107 | p_OptimizerOptionsImpl->m_ReduceFp32ToBf16 = other.GetReduceFp32ToBf16(); |
| 108 | return *this; |
| 109 | } |
| 110 | |
| 111 | void OptimizerOptionsOpaque::SetImportEnabled(bool ImportState) |
| 112 | { |
| 113 | p_OptimizerOptionsImpl->m_ImportEnabled = ImportState; |
| 114 | } |
| 115 | |
| 116 | void OptimizerOptionsOpaque::SetExportEnabled(bool ExportState) |
| 117 | { |
| 118 | p_OptimizerOptionsImpl->m_ExportEnabled = ExportState; |
| 119 | } |
| 120 | |
| 121 | void OptimizerOptionsOpaque::SetProfilingEnabled(bool ProfilingState) |
| 122 | { |
| 123 | p_OptimizerOptionsImpl->m_ProfilingEnabled = ProfilingState; |
| 124 | } |
| 125 | |
| 126 | void OptimizerOptionsOpaque::SetDebugEnabled(bool DebugState) |
| 127 | { |
| 128 | p_OptimizerOptionsImpl->m_Debug = DebugState; |
| 129 | } |
| 130 | |
| 131 | void OptimizerOptionsOpaque::SetDebugToFileEnabled(bool DebugFileState) |
| 132 | { |
| 133 | p_OptimizerOptionsImpl->m_DebugToFile = DebugFileState; |
| 134 | } |
| 135 | |
| 136 | void OptimizerOptionsOpaque::SetReduceFp32ToFp16(bool ReduceFp32ToFp16State) |
| 137 | { |
| 138 | p_OptimizerOptionsImpl->m_ReduceFp32ToFp16 = ReduceFp32ToFp16State; |
| 139 | } |
| 140 | |
| 141 | void OptimizerOptionsOpaque::SetShapeInferenceMethod(armnn::ShapeInferenceMethod ShapeInferenceMethodType) |
| 142 | { |
| 143 | p_OptimizerOptionsImpl->m_shapeInferenceMethod = ShapeInferenceMethodType; |
| 144 | } |
| 145 | |
| 146 | void OptimizerOptionsOpaque::SetAllowExpandedDims(bool ExpandedDimsAllowed) |
| 147 | { |
| 148 | p_OptimizerOptionsImpl->m_AllowExpandedDims = ExpandedDimsAllowed; |
| 149 | } |
| 150 | |
| 151 | void OptimizerOptionsOpaque::AddModelOption(armnn::BackendOptions NewModelOption) |
| 152 | { |
| 153 | p_OptimizerOptionsImpl->m_ModelOptions.push_back(NewModelOption); |
| 154 | } |
| 155 | |
| 156 | bool OptimizerOptionsOpaque::GetProfilingEnabled() const |
| 157 | { |
| 158 | return p_OptimizerOptionsImpl->m_ProfilingEnabled; |
| 159 | }; |
| 160 | |
| 161 | bool OptimizerOptionsOpaque::GetImportEnabled() const |
| 162 | { |
| 163 | return p_OptimizerOptionsImpl->m_ImportEnabled; |
| 164 | }; |
| 165 | |
| 166 | bool OptimizerOptionsOpaque::GetExportEnabled() const |
| 167 | { |
| 168 | return p_OptimizerOptionsImpl->m_ExportEnabled; |
| 169 | }; |
| 170 | |
| 171 | bool OptimizerOptionsOpaque::GetReduceFp32ToFp16() const |
| 172 | { |
| 173 | return p_OptimizerOptionsImpl->m_ReduceFp32ToFp16; |
| 174 | }; |
| 175 | |
| 176 | bool OptimizerOptionsOpaque::GetReduceFp32ToBf16() const |
| 177 | { |
| 178 | return p_OptimizerOptionsImpl->m_ReduceFp32ToBf16; |
| 179 | } |
| 180 | |
| 181 | bool OptimizerOptionsOpaque::GetDebugEnabled() const |
| 182 | { |
| 183 | return p_OptimizerOptionsImpl->m_Debug; |
| 184 | } |
| 185 | |
| 186 | bool OptimizerOptionsOpaque::GetDebugToFileEnabled() const |
| 187 | { |
| 188 | return p_OptimizerOptionsImpl->m_DebugToFile; |
| 189 | } |
| 190 | |
| 191 | bool OptimizerOptionsOpaque::GetAllowExpandedDims() const |
| 192 | { |
| 193 | return p_OptimizerOptionsImpl->m_AllowExpandedDims; |
| 194 | } |
| 195 | |
| 196 | armnn::ModelOptions OptimizerOptionsOpaque::GetModelOptions() const |
| 197 | { |
| 198 | return p_OptimizerOptionsImpl->m_ModelOptions; |
| 199 | } |
| 200 | |
| 201 | armnn::ShapeInferenceMethod OptimizerOptionsOpaque::GetShapeInferenceMethod() const |
| 202 | { |
| 203 | return p_OptimizerOptionsImpl->m_shapeInferenceMethod; |
| 204 | } |
| 205 | |
| 206 | const std::string OptimizerOptionsOpaque::ToString() const |
| 207 | { |
| 208 | std::stringstream stream; |
| 209 | stream << "OptimizerOptions: \n"; |
| 210 | stream << "\tReduceFp32ToFp16: " << p_OptimizerOptionsImpl->m_ReduceFp32ToFp16 << "\n"; |
| 211 | stream << "\tReduceFp32ToBf16: " << p_OptimizerOptionsImpl->m_ReduceFp32ToBf16 << "\n"; |
| 212 | stream << "\tDebug: " << p_OptimizerOptionsImpl->m_Debug << "\n"; |
| 213 | stream << "\tDebug to file: " << p_OptimizerOptionsImpl->m_DebugToFile << "\n"; |
| 214 | stream << "\tShapeInferenceMethod: " << |
| 215 | (p_OptimizerOptionsImpl->m_shapeInferenceMethod == ShapeInferenceMethod::ValidateOnly ? |
| 216 | "ValidateOnly" : "InferAndValidate") << "\n"; |
| 217 | stream << "\tImportEnabled: " << p_OptimizerOptionsImpl->m_ImportEnabled << "\n"; |
| 218 | stream << "\tExportEnabled: " << p_OptimizerOptionsImpl->m_ExportEnabled << "\n"; |
| 219 | stream << "\tProfilingEnabled: " << p_OptimizerOptionsImpl->m_ProfilingEnabled << "\n"; |
| 220 | stream << "\tAllowExpandedDims: " << p_OptimizerOptionsImpl->m_AllowExpandedDims << "\n"; |
| 221 | |
| 222 | stream << "\tModelOptions: \n"; |
| 223 | for (auto optionsGroup : p_OptimizerOptionsImpl->m_ModelOptions) |
| 224 | { |
| 225 | for (size_t i=0; i < optionsGroup.GetOptionCount(); i++) |
| 226 | { |
| 227 | const armnn::BackendOptions::BackendOption option = optionsGroup.GetOption(i); |
| 228 | stream << "\t\tBackend: " << optionsGroup.GetBackendId() << "\n" |
| 229 | << "\t\t\tOption: " << option.GetName() << "\n" |
| 230 | << "\t\t\tValue: " << std::string(option.GetValue().ToString()) << "\n"; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return stream.str(); |
| 235 | } |
| 236 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 237 | Status INetwork::PrintGraph() |
| 238 | { |
| 239 | return pNetworkImpl->PrintGraph(); |
| 240 | } |
| 241 | |
| 242 | IConnectableLayer* INetwork::AddInputLayer(LayerBindingId id, const char* name) |
| 243 | { |
| 244 | return pNetworkImpl->AddInputLayer(id, name); |
| 245 | } |
| 246 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 247 | IConnectableLayer* INetwork::AddArgMinMaxLayer(const ArgMinMaxDescriptor& desc, |
| 248 | const char* name) |
| 249 | { |
| 250 | return pNetworkImpl->AddArgMinMaxLayer(desc, name); |
| 251 | } |
| 252 | |
mathad01 | b392e98 | 2021-04-07 12:07:30 +0100 | [diff] [blame] | 253 | IConnectableLayer* INetwork::AddCastLayer(const char* name) |
| 254 | { |
| 255 | return pNetworkImpl->AddCastLayer(name); |
| 256 | } |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 257 | |
| 258 | IConnectableLayer* INetwork::AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor, |
| 259 | const char* name) |
| 260 | { |
| 261 | return pNetworkImpl->AddComparisonLayer(comparisonDescriptor, name); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | IConnectableLayer* INetwork::AddConcatLayer(const ConcatDescriptor& concatDescriptor, |
| 266 | const char* name) |
| 267 | { |
| 268 | return pNetworkImpl->AddConcatLayer(concatDescriptor, name); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | IConnectableLayer* INetwork::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 273 | const char* name) |
| 274 | { |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 275 | return pNetworkImpl->AddConvolution2dLayer(convolution2dDescriptor, name); |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 278 | IConnectableLayer* INetwork::AddConvolution3dLayer(const Convolution3dDescriptor& convolution3dDescriptor, |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 279 | const char* name) |
| 280 | { |
Matthew Sloyan | 5d7b0a3 | 2021-10-18 13:07:49 +0100 | [diff] [blame] | 281 | return pNetworkImpl->AddConvolution3dLayer(convolution3dDescriptor, name); |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 285 | IConnectableLayer* INetwork::AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor, |
| 286 | const char* name) |
| 287 | { |
| 288 | return pNetworkImpl->AddDepthToSpaceLayer(depthToSpaceDescriptor, name); |
| 289 | } |
| 290 | |
| 291 | |
| 292 | IConnectableLayer* INetwork::AddDepthwiseConvolution2dLayer( |
| 293 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
Cathal Corbett | 0690265 | 2022-04-14 17:55:11 +0100 | [diff] [blame] | 294 | const char* name) |
| 295 | { |
| 296 | return pNetworkImpl->AddDepthwiseConvolution2dLayer(convolution2dDescriptor, name); |
| 297 | } |
| 298 | |
| 299 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 300 | IConnectableLayer* INetwork::AddDequantizeLayer(const char* name) |
| 301 | { |
| 302 | return pNetworkImpl->AddDequantizeLayer(name); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | IConnectableLayer* INetwork::AddDetectionPostProcessLayer( |
| 307 | const DetectionPostProcessDescriptor& descriptor, |
| 308 | const ConstTensor& anchors, |
| 309 | const char* name) |
| 310 | { |
| 311 | return pNetworkImpl->AddDetectionPostProcessLayer(descriptor, anchors, name); |
| 312 | } |
| 313 | |
Mike Kelly | 3ec3077 | 2023-03-08 13:47:17 +0000 | [diff] [blame] | 314 | IConnectableLayer* INetwork::AddElementwiseBinaryLayer(const ElementwiseBinaryDescriptor& elementwiseBinaryDescriptor, |
| 315 | const char* name) |
| 316 | { |
| 317 | return pNetworkImpl->AddElementwiseBinaryLayer(elementwiseBinaryDescriptor, name); |
| 318 | } |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 319 | |
| 320 | IConnectableLayer* INetwork::AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor, |
| 321 | const char* name) |
| 322 | { |
| 323 | return pNetworkImpl->AddElementwiseUnaryLayer(elementwiseUnaryDescriptor, name); |
| 324 | } |
| 325 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 326 | IConnectableLayer* INetwork::AddFillLayer(const FillDescriptor& fillDescriptor, |
| 327 | const char* name) |
| 328 | { |
| 329 | return pNetworkImpl->AddFillLayer(fillDescriptor, name); |
| 330 | } |
| 331 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 332 | IConnectableLayer* INetwork::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 333 | const char* name) |
| 334 | { |
| 335 | return pNetworkImpl->AddFullyConnectedLayer(fullyConnectedDescriptor, name); |
| 336 | } |
| 337 | |
Teresa Charlin | 9145e38 | 2023-08-17 18:44:58 +0100 | [diff] [blame] | 338 | IConnectableLayer* INetwork::AddFusedLayer(const FusedDescriptor& fusedDescriptor, |
| 339 | const char* name) |
| 340 | { |
| 341 | return pNetworkImpl->AddFusedLayer(fusedDescriptor, name); |
| 342 | } |
| 343 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 344 | IConnectableLayer* INetwork::AddPermuteLayer(const PermuteDescriptor& permuteDescriptor, |
| 345 | const char* name) |
| 346 | { |
| 347 | return pNetworkImpl->AddPermuteLayer(permuteDescriptor, name); |
| 348 | } |
| 349 | |
| 350 | IConnectableLayer* INetwork::AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor, |
| 351 | const char* name) |
| 352 | { |
| 353 | return pNetworkImpl->AddBatchToSpaceNdLayer(batchToSpaceNdDescriptor, name); |
| 354 | } |
| 355 | |
| 356 | IConnectableLayer* INetwork::AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor, |
| 357 | const char* name) |
| 358 | { |
| 359 | return pNetworkImpl->AddPooling2dLayer(pooling2dDescriptor, name); |
| 360 | } |
| 361 | |
Tamás Nyíri | 7b885b3 | 2021-10-26 14:47:57 +0100 | [diff] [blame] | 362 | IConnectableLayer* INetwork::AddPooling3dLayer(const Pooling3dDescriptor& pooling3dDescriptor, |
| 363 | const char* name) |
| 364 | { |
| 365 | return pNetworkImpl->AddPooling3dLayer(pooling3dDescriptor, name); |
| 366 | } |
| 367 | |
Cathal Corbett | 18655b8 | 2021-12-13 13:03:22 +0000 | [diff] [blame] | 368 | IConnectableLayer* INetwork::AddPrecompiledLayer(const PreCompiledDescriptor& preCompiledDescriptor, |
Cathal Corbett | 3ea0107 | 2022-01-06 10:29:43 +0000 | [diff] [blame] | 369 | CompiledBlobPtr compiledBlobPtr, |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 370 | const Optional<BackendId>& backend, |
| 371 | const char* name) |
Cathal Corbett | 18655b8 | 2021-12-13 13:03:22 +0000 | [diff] [blame] | 372 | { |
Cathal Corbett | 3ea0107 | 2022-01-06 10:29:43 +0000 | [diff] [blame] | 373 | return pNetworkImpl->AddPrecompiledLayer(preCompiledDescriptor, std::move(compiledBlobPtr), backend, name); |
Cathal Corbett | 18655b8 | 2021-12-13 13:03:22 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 376 | IConnectableLayer* INetwork::AddActivationLayer(const ActivationDescriptor& activationDescriptor, |
| 377 | const char* name) |
| 378 | { |
| 379 | return pNetworkImpl->AddActivationLayer(activationDescriptor, name); |
| 380 | } |
| 381 | |
| 382 | IConnectableLayer* INetwork::AddNormalizationLayer(const NormalizationDescriptor& normalizationDescriptor, |
| 383 | const char* name) |
| 384 | { |
| 385 | return pNetworkImpl->AddNormalizationLayer(normalizationDescriptor, name); |
| 386 | } |
| 387 | |
| 388 | IConnectableLayer* INetwork::AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name) |
| 389 | { |
| 390 | return pNetworkImpl->AddSliceLayer(sliceDescriptor, name); |
| 391 | } |
| 392 | IConnectableLayer* INetwork::AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor, |
| 393 | const char* name) |
| 394 | { |
| 395 | return pNetworkImpl->AddSoftmaxLayer(softmaxDescriptor, name); |
| 396 | } |
| 397 | |
| 398 | IConnectableLayer* INetwork::AddSplitterLayer(const ViewsDescriptor& splitterDescriptor, |
| 399 | const char* name) |
| 400 | { |
| 401 | return pNetworkImpl->AddSplitterLayer(splitterDescriptor, name); |
| 402 | } |
| 403 | |
| 404 | IConnectableLayer* INetwork::AddMergeLayer(const char* name) |
| 405 | { |
| 406 | return pNetworkImpl->AddMergeLayer(name); |
| 407 | } |
| 408 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 409 | IConnectableLayer* INetwork::AddAdditionLayer(const char* name) |
| 410 | { |
Mike Kelly | 2c14db6 | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 411 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 412 | return pNetworkImpl->AddAdditionLayer(name); |
Mike Kelly | 2c14db6 | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 413 | ARMNN_NO_DEPRECATE_WARN_END |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | IConnectableLayer* INetwork::AddMultiplicationLayer(const char* name) |
| 417 | { |
Mike Kelly | 2c14db6 | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 418 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 419 | return pNetworkImpl->AddMultiplicationLayer(name); |
Mike Kelly | 2c14db6 | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 420 | ARMNN_NO_DEPRECATE_WARN_END |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | IConnectableLayer* INetwork::AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc, |
| 424 | const ConstTensor& mean, |
| 425 | const ConstTensor& variance, |
| 426 | const ConstTensor& beta, |
| 427 | const ConstTensor& gamma, |
| 428 | const char* name) |
| 429 | { |
| 430 | return pNetworkImpl->AddBatchNormalizationLayer(desc, mean, variance, beta, gamma, name); |
| 431 | } |
| 432 | |
| 433 | IConnectableLayer* INetwork::AddRankLayer(const char* name) |
| 434 | { |
| 435 | return pNetworkImpl->AddRankLayer(name); |
| 436 | } |
| 437 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 438 | IConnectableLayer* INetwork::AddResizeLayer(const ResizeDescriptor& resizeDescriptor, |
| 439 | const char* name) |
| 440 | { |
| 441 | return pNetworkImpl->AddResizeLayer(resizeDescriptor, name); |
| 442 | } |
| 443 | |
| 444 | IConnectableLayer* INetwork::AddReduceLayer(const ReduceDescriptor& reduceDescriptor, |
| 445 | const char* name) |
| 446 | { |
| 447 | return pNetworkImpl->AddReduceLayer(reduceDescriptor, name); |
| 448 | } |
| 449 | |
| 450 | IConnectableLayer* INetwork::AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc, |
| 451 | const char* name) |
| 452 | { |
| 453 | return pNetworkImpl->AddInstanceNormalizationLayer(desc, name); |
| 454 | } |
| 455 | |
| 456 | IConnectableLayer* INetwork::AddL2NormalizationLayer(const L2NormalizationDescriptor& desc, |
| 457 | const char* name) |
| 458 | { |
| 459 | return pNetworkImpl->AddL2NormalizationLayer(desc, name); |
| 460 | } |
| 461 | |
| 462 | IConnectableLayer* INetwork::AddLogSoftmaxLayer(const LogSoftmaxDescriptor& logSoftmaxDescriptor, |
| 463 | const char* name) |
| 464 | { |
| 465 | return pNetworkImpl->AddLogSoftmaxLayer(logSoftmaxDescriptor, name); |
| 466 | } |
| 467 | |
| 468 | IConnectableLayer* INetwork::AddConstantLayer(const ConstTensor& input, |
| 469 | const char* name) |
| 470 | { |
| 471 | return pNetworkImpl->AddConstantLayer(input, name); |
| 472 | } |
| 473 | |
| 474 | IConnectableLayer* INetwork::AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor, |
| 475 | const char* name) |
| 476 | { |
| 477 | return pNetworkImpl->AddReshapeLayer(reshapeDescriptor, name); |
| 478 | } |
| 479 | |
| 480 | IConnectableLayer* INetwork::AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor, |
| 481 | const char* name) |
| 482 | { |
| 483 | return pNetworkImpl->AddSpaceToBatchNdLayer(spaceToBatchNdDescriptor, name); |
| 484 | } |
| 485 | |
| 486 | IConnectableLayer* INetwork::AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor, |
| 487 | const char* name) |
| 488 | { |
| 489 | return pNetworkImpl->AddSpaceToDepthLayer(spaceToDepthDescriptor, name); |
| 490 | } |
| 491 | |
| 492 | IConnectableLayer* INetwork::AddFloorLayer(const char* name) |
| 493 | { |
| 494 | return pNetworkImpl->AddFloorLayer(name); |
| 495 | } |
| 496 | IConnectableLayer* INetwork::AddOutputLayer(LayerBindingId id, const char* name) |
| 497 | { |
| 498 | return pNetworkImpl->AddOutputLayer(id, name); |
| 499 | } |
| 500 | |
| 501 | IConnectableLayer* INetwork::AddLstmLayer(const LstmDescriptor& descriptor, |
| 502 | const LstmInputParams& params, |
| 503 | const char* name) |
| 504 | { |
| 505 | return pNetworkImpl->AddLstmLayer(descriptor, params, name); |
| 506 | } |
| 507 | |
| 508 | IConnectableLayer* INetwork::AddDivisionLayer(const char* name) |
| 509 | { |
Mike Kelly | 2c14db6 | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 510 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 511 | return pNetworkImpl->AddDivisionLayer(name); |
Mike Kelly | 2c14db6 | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 512 | ARMNN_NO_DEPRECATE_WARN_END |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | IConnectableLayer* INetwork::AddSubtractionLayer(const char* name) |
| 516 | { |
Mike Kelly | 2c14db6 | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 517 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 518 | return pNetworkImpl->AddSubtractionLayer(name); |
Mike Kelly | 2c14db6 | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 519 | ARMNN_NO_DEPRECATE_WARN_END |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | IConnectableLayer* INetwork::AddMaximumLayer(const char* name) |
| 523 | { |
Mike Kelly | 2c14db6 | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 524 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 525 | return pNetworkImpl->AddMaximumLayer(name); |
Mike Kelly | 2c14db6 | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 526 | ARMNN_NO_DEPRECATE_WARN_END |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | IConnectableLayer* INetwork::AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name) |
| 530 | { |
| 531 | return pNetworkImpl->AddMeanLayer(meanDescriptor, name); |
| 532 | } |
| 533 | |
| 534 | IConnectableLayer* INetwork::AddPadLayer(const PadDescriptor& padDescriptor, |
| 535 | const char* name) |
| 536 | { |
| 537 | return pNetworkImpl->AddPadLayer(padDescriptor, name); |
| 538 | } |
| 539 | |
| 540 | IConnectableLayer* INetwork::AddQuantizeLayer(const char* name) |
| 541 | { |
| 542 | return pNetworkImpl->AddQuantizeLayer(name); |
| 543 | } |
| 544 | |
| 545 | IConnectableLayer* INetwork::AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor, |
| 546 | const char* name) |
| 547 | { |
| 548 | return pNetworkImpl->AddStridedSliceLayer(stridedSliceDescriptor, name); |
| 549 | } |
| 550 | |
| 551 | IConnectableLayer* INetwork::AddMinimumLayer(const char* name) |
| 552 | { |
Mike Kelly | 2c14db6 | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 553 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 554 | return pNetworkImpl->AddMinimumLayer(name); |
Mike Kelly | 2c14db6 | 2023-03-15 15:06:23 +0000 | [diff] [blame] | 555 | ARMNN_NO_DEPRECATE_WARN_END |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 558 | IConnectableLayer* INetwork::AddGatherLayer(const GatherDescriptor& descriptor, |
| 559 | const char* name) |
| 560 | { |
| 561 | return pNetworkImpl->AddGatherLayer(descriptor, name); |
| 562 | } |
| 563 | |
Teresa Charlin | b2d3ec5 | 2022-04-12 22:07:09 +0100 | [diff] [blame] | 564 | IConnectableLayer* INetwork::AddGatherNdLayer(const char* name) |
| 565 | { |
| 566 | return pNetworkImpl->AddGatherNdLayer(name); |
| 567 | } |
| 568 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 569 | IConnectableLayer* INetwork::AddSwitchLayer(const char* name) |
| 570 | { |
| 571 | return pNetworkImpl->AddSwitchLayer(name); |
| 572 | } |
| 573 | |
| 574 | IConnectableLayer* INetwork::AddPreluLayer(const char* name) |
| 575 | { |
| 576 | return pNetworkImpl->AddPreluLayer(name); |
| 577 | } |
| 578 | |
| 579 | IConnectableLayer* INetwork::AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor, |
| 580 | const ConstTensor& weights, |
| 581 | const Optional<ConstTensor>& biases, |
| 582 | const char* name) |
| 583 | { |
| 584 | return pNetworkImpl->AddTransposeConvolution2dLayer(descriptor, weights, biases, name); |
| 585 | } |
| 586 | |
| 587 | IConnectableLayer* INetwork::AddTransposeLayer(const TransposeDescriptor& transposeDescriptor, |
| 588 | const char* name) |
| 589 | { |
| 590 | return pNetworkImpl->AddTransposeLayer(transposeDescriptor, name); |
| 591 | } |
| 592 | |
Keith Davis | 3ae3f97 | 2021-05-21 16:33:48 +0100 | [diff] [blame] | 593 | IConnectableLayer* INetwork::AddShapeLayer(const char* name) |
| 594 | { |
| 595 | return pNetworkImpl->AddShapeLayer(name); |
| 596 | } |
| 597 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 598 | IConnectableLayer* INetwork::AddStackLayer(const StackDescriptor& descriptor, |
| 599 | const char* name) |
| 600 | { |
| 601 | return pNetworkImpl->AddStackLayer(descriptor, name); |
| 602 | } |
| 603 | |
| 604 | IConnectableLayer* INetwork::AddStandInLayer(const StandInDescriptor& descriptor, |
| 605 | const char* name) |
| 606 | { |
| 607 | return pNetworkImpl->AddStandInLayer(descriptor, name); |
| 608 | } |
| 609 | |
| 610 | IConnectableLayer* INetwork::AddQuantizedLstmLayer(const QuantizedLstmInputParams& params, |
| 611 | const char* name) |
| 612 | { |
| 613 | return pNetworkImpl->AddQuantizedLstmLayer(params, name); |
| 614 | } |
| 615 | |
| 616 | IConnectableLayer* INetwork::AddQLstmLayer(const QLstmDescriptor& descriptor, |
| 617 | const LstmInputParams& params, |
| 618 | const char* name) |
| 619 | { |
| 620 | return pNetworkImpl->AddQLstmLayer(descriptor, params, name); |
| 621 | } |
| 622 | |
| 623 | IConnectableLayer* INetwork::AddLogicalBinaryLayer(const LogicalBinaryDescriptor& descriptor, |
| 624 | const char* name) |
| 625 | { |
| 626 | return pNetworkImpl->AddLogicalBinaryLayer(descriptor, name); |
| 627 | } |
| 628 | |
Narumol Prangnawarat | 8ed39ae | 2021-07-15 16:16:25 +0100 | [diff] [blame] | 629 | IConnectableLayer* INetwork::AddUnidirectionalSequenceLstmLayer( |
| 630 | const UnidirectionalSequenceLstmDescriptor& descriptor, |
| 631 | const LstmInputParams& params, |
| 632 | const char* name) |
| 633 | { |
| 634 | return pNetworkImpl->AddUnidirectionalSequenceLstmLayer(descriptor, params, name); |
| 635 | } |
| 636 | |
Simon Obute | 51f6777 | 2021-09-03 15:50:13 +0100 | [diff] [blame] | 637 | IConnectableLayer* INetwork::AddChannelShuffleLayer(const ChannelShuffleDescriptor &descriptor, |
| 638 | const char* name) |
| 639 | { |
| 640 | return pNetworkImpl->AddChannelShuffleLayer(descriptor, name); |
| 641 | } |
| 642 | |
Samuel Yap | 6b47809 | 2022-07-06 15:36:03 +0100 | [diff] [blame] | 643 | IConnectableLayer* INetwork::AddBatchMatMulLayer(const BatchMatMulDescriptor &descriptor, |
| 644 | const char* name) |
| 645 | { |
| 646 | return pNetworkImpl->AddBatchMatMulLayer(descriptor, name); |
| 647 | } |
| 648 | |
Tracy Narine | bb8d759 | 2023-07-13 16:50:54 +0100 | [diff] [blame] | 649 | IConnectableLayer* INetwork::AddReverseV2Layer(const char *name) |
Tianle Cheng | 988354d | 2023-06-28 13:20:47 +0100 | [diff] [blame] | 650 | { |
Tracy Narine | bb8d759 | 2023-07-13 16:50:54 +0100 | [diff] [blame] | 651 | return pNetworkImpl->AddReverseV2Layer(name); |
Tianle Cheng | 988354d | 2023-06-28 13:20:47 +0100 | [diff] [blame] | 652 | } |
| 653 | |
Teresa Charlin | 79a06a5 | 2023-07-13 17:16:45 +0100 | [diff] [blame] | 654 | IConnectableLayer* INetwork::AddTileLayer(const TileDescriptor &descriptor, |
| 655 | const char *name) |
| 656 | { |
| 657 | return pNetworkImpl->AddTileLayer(descriptor, name); |
| 658 | } |
| 659 | |
Idriss Chaouch | 98e383e | 2023-08-28 14:28:31 +0100 | [diff] [blame] | 660 | IConnectableLayer* INetwork::AddBroadcastToLayer(const BroadcastToDescriptor& descriptor, |
| 661 | const char* name) |
| 662 | { |
| 663 | return pNetworkImpl->AddBroadcastToLayer(descriptor, name); |
| 664 | } |
| 665 | |
Tianle Cheng | 2828818 | 2024-02-23 17:56:54 +0000 | [diff] [blame^] | 666 | IConnectableLayer* INetwork::AddScatterNdLayer(const ScatterNdDescriptor &descriptor, |
| 667 | const char *name) |
| 668 | { |
| 669 | return pNetworkImpl->AddScatterNdLayer(descriptor, name); |
| 670 | } |
| 671 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 672 | void INetwork::ExecuteStrategy(IStrategy& strategy) const |
| 673 | { |
| 674 | return pNetworkImpl->ExecuteStrategy(strategy); |
| 675 | } |
| 676 | |
Jim Flynn | fcc72f5 | 2022-10-14 11:20:07 +0100 | [diff] [blame] | 677 | armnn::INetwork* INetwork::CreateRaw(const NetworkOptions& networkOptions) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 678 | { |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 679 | return new INetwork(networkOptions); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Jim Flynn | fcc72f5 | 2022-10-14 11:20:07 +0100 | [diff] [blame] | 682 | armnn::INetworkPtr INetwork::Create(const NetworkOptions& networkOptions) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 683 | { |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 684 | return INetworkPtr(CreateRaw(networkOptions), &INetwork::Destroy); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | void INetwork::Destroy(INetwork* network) |
| 688 | { |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 689 | delete network; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Mike Kelly | 0d677db | 2021-06-27 22:39:21 +0100 | [diff] [blame] | 692 | IOptimizedNetwork::IOptimizedNetwork(const IOptimizedNetwork& other, const ModelOptions& modelOptions) |
| 693 | : pOptimizedNetworkImpl(new OptimizedNetworkImpl(*other.pOptimizedNetworkImpl.get(), modelOptions)) {} |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 694 | |
| 695 | IOptimizedNetwork::IOptimizedNetwork(std::unique_ptr<Graph> graph) |
| 696 | : pOptimizedNetworkImpl(new OptimizedNetworkImpl(std::move(graph))) {} |
| 697 | |
| 698 | IOptimizedNetwork::IOptimizedNetwork(std::unique_ptr<OptimizedNetworkImpl> impl) |
| 699 | : pOptimizedNetworkImpl(std::move(impl)) {} |
| 700 | |
| 701 | IOptimizedNetwork::IOptimizedNetwork(std::unique_ptr<Graph> graph, const ModelOptions& modelOptions) |
| 702 | : pOptimizedNetworkImpl(new OptimizedNetworkImpl(std::move(graph), modelOptions)) {} |
| 703 | |
| 704 | IOptimizedNetwork::~IOptimizedNetwork() = default; |
| 705 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 706 | void IOptimizedNetwork::Destroy(IOptimizedNetwork* network) |
| 707 | { |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 708 | delete network; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 711 | Status IOptimizedNetwork::PrintGraph() |
| 712 | { |
| 713 | return pOptimizedNetworkImpl->PrintGraph(); |
| 714 | } |
| 715 | |
| 716 | Status IOptimizedNetwork::SerializeToDot(std::ostream& stream) const |
| 717 | { |
| 718 | return pOptimizedNetworkImpl->SerializeToDot(stream); |
| 719 | } |
| 720 | |
Derek Lamberti | e155bbf | 2021-10-13 14:32:12 +0100 | [diff] [blame] | 721 | const std::shared_ptr<IProfiler>& IOptimizedNetwork::GetProfiler() const |
| 722 | { |
| 723 | return pOptimizedNetworkImpl->GetGraph().GetProfiler(); |
| 724 | } |
| 725 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 726 | arm::pipe::ProfilingGuid IOptimizedNetwork::GetGuid() const |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 727 | { |
| 728 | return pOptimizedNetworkImpl->GetGuid(); |
| 729 | } |
| 730 | |
Sadik Armagan | b7851f9 | 2021-10-06 16:37:02 +0100 | [diff] [blame] | 731 | size_t IOptimizedNetwork::GetNumInputs() const |
| 732 | { |
| 733 | return pOptimizedNetworkImpl->GetNumInputs(); |
| 734 | } |
| 735 | |
| 736 | size_t IOptimizedNetwork::GetNumOutputs() const |
| 737 | { |
| 738 | return pOptimizedNetworkImpl->GetNumOutputs(); |
| 739 | } |
| 740 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 741 | Status OptimizedNetworkImpl::PrintGraph() |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 742 | { |
| 743 | m_Graph->Print(); |
| 744 | return Status::Success; |
| 745 | } |
| 746 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 747 | Status OptimizedNetworkImpl::SerializeToDot(std::ostream& stream) const |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 748 | { |
| 749 | return m_Graph->SerializeToDot(stream); |
| 750 | } |
| 751 | |
Sadik Armagan | b7851f9 | 2021-10-06 16:37:02 +0100 | [diff] [blame] | 752 | size_t OptimizedNetworkImpl::GetNumInputs() const |
| 753 | { |
| 754 | return m_Graph->GetNumInputs(); |
| 755 | } |
| 756 | |
| 757 | size_t OptimizedNetworkImpl::GetNumOutputs() const |
| 758 | { |
| 759 | return m_Graph->GetNumOutputs(); |
| 760 | } |
| 761 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 762 | void ReportError(const std::string& errorMessage, |
| 763 | Optional<std::vector<std::string>&> errorMessages) |
| 764 | { |
| 765 | std::stringstream fullErrorMessage; |
| 766 | fullErrorMessage << "ERROR: " << errorMessage; |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 767 | ARMNN_LOG(warning) << fullErrorMessage.str(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 768 | if (errorMessages) |
| 769 | { |
| 770 | errorMessages.value().push_back(fullErrorMessage.str()); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | void ReportWarning(const std::string& warningMessage, |
| 775 | Optional<std::vector<std::string>&> warningMessages) |
| 776 | { |
| 777 | std::stringstream fullWarningMessage; |
| 778 | fullWarningMessage << "WARNING: " << warningMessage; |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 779 | ARMNN_LOG(warning) << fullWarningMessage.str(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 780 | if (warningMessages) |
| 781 | { |
| 782 | warningMessages.value().push_back(fullWarningMessage.str()); |
| 783 | } |
| 784 | } |
| 785 | |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 786 | OptimizationResult ReturnWithError(OptimizationResult res, |
| 787 | const Layer* layer, |
| 788 | const BackendSettings& backendSettings, |
| 789 | Optional<std::vector<std::string>&> errMessages) |
| 790 | { |
| 791 | std::stringstream failureMsg; |
| 792 | failureMsg << "Layer of type " << GetLayerTypeAsCString(layer->GetType()) |
| 793 | << " is not supported on any preferred backend " << backendSettings.m_PreferredBackends; |
| 794 | ReportError(failureMsg.str(), errMessages); |
| 795 | |
| 796 | res.m_Error = true; |
| 797 | return res; |
| 798 | } |
| 799 | |
| 800 | |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 801 | bool CheckScaleSetOnQuantizedType(Layer* layer, Optional<std::vector<std::string>&> errMessages) |
| 802 | { |
| 803 | bool noErrors = true; |
| 804 | unsigned int numOutputs = layer->GetNumOutputSlots(); |
| 805 | for (unsigned int i = 0; i < numOutputs; i++) { |
David Monahan | b855470 | 2019-04-25 16:03:38 +0100 | [diff] [blame] | 806 | OutputSlot& outputSlot = layer->GetOutputSlot(i); |
| 807 | TensorInfo info = outputSlot.GetTensorInfo(); |
John Mcloughlin | b41793a | 2023-10-16 10:28:40 +0100 | [diff] [blame] | 808 | auto quantizationDataType = info.GetDataType(); |
| 809 | auto quantizationScales = info.GetQuantizationScales(); |
| 810 | // For any Quantized Tensor ensure scale(s) are set |
| 811 | switch(quantizationDataType) { |
| 812 | case DataType::QAsymmU8: |
| 813 | case DataType::QSymmS16: |
| 814 | case DataType::QSymmS8: |
| 815 | case DataType::QAsymmS8: |
| 816 | if ((quantizationDataType == DataType::QAsymmU8 || quantizationDataType == DataType::QAsymmS8) |
| 817 | && info.HasPerAxisQuantization()) { |
| 818 | throw InvalidArgumentException("Per Axis Quantization is not supported in " |
| 819 | "Asymmetric Quantization Datatype."); |
| 820 | } |
| 821 | if ((!info.HasPerAxisQuantization() && info.GetQuantizationScale() == 0.f) |
| 822 | || (info.HasPerAxisQuantization() && (quantizationScales.end() != |
| 823 | std::find(quantizationScales.begin(), quantizationScales.end(), 0.f)))) { |
| 824 | noErrors = false; |
| 825 | std::stringstream ss; |
| 826 | ss << "output " << i << " of layer " << GetLayerTypeAsCString(layer->GetType()) |
| 827 | << " (" << layer->GetNameStr() << ") is of type" |
| 828 | << " Quantized value but the scale parameter has not been set"; |
| 829 | ReportError(ss.str(), errMessages); |
| 830 | } |
| 831 | // Softmax under QuantisedAsymm8 must always be scale (1.0f/256.0f) and offset 0 |
| 832 | if (!info.HasPerAxisQuantization() && quantizationDataType == DataType::QAsymmU8 && |
| 833 | (info.GetQuantizationScale() != (1.0f / 256.0f) || |
| 834 | info.GetQuantizationOffset() != 0) && |
| 835 | layer->GetType() == armnn::LayerType::Softmax) { |
| 836 | std::stringstream ss; |
| 837 | ss << "Quantization parameters for Softmax layer (Scale: " << |
| 838 | info.GetQuantizationScale() << " and Offset: " << info.GetQuantizationOffset() << |
| 839 | ") are incorrect and have been updated to Scale: 0.00390625 and Offset: 0"; |
| 840 | ARMNN_LOG(warning) << ss.str(); |
| 841 | info.SetQuantizationScale((1.0f / 256.0f)); |
| 842 | info.SetQuantizationOffset(0); |
| 843 | outputSlot.SetTensorInfo(info); |
| 844 | } |
| 845 | break; |
| 846 | default: |
| 847 | break; |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | return noErrors; |
| 851 | } |
| 852 | |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 853 | OptimizationResult AttemptBackendAssignment(BackendSettings& backendSettings, |
| 854 | Graph& graph, |
| 855 | Layer* layer, |
| 856 | BackendId backend, |
| 857 | DataType dataTypeIn, |
| 858 | DataType dataTypeOut, |
| 859 | const std::vector<BackendId>& availablePreferredBackends, |
| 860 | std::string& reasonIfUnsupported, |
| 861 | Optional<std::vector<std::string>&> errMessages) |
| 862 | { |
| 863 | OptimizationResult result; |
| 864 | |
| 865 | // Helper lambda to compose meaningful error message before returning with error |
| 866 | auto ReturnError = [&](const Layer* layer) |
| 867 | { |
| 868 | return ReturnWithError(result, layer, backendSettings, errMessages); |
| 869 | }; |
| 870 | |
| 871 | // need to set the compute device on the layer |
| 872 | // before we can check if it is supported |
| 873 | layer->SetBackendId(backend); |
Ryan OShea | b4c4934 | 2023-07-25 14:28:27 +0100 | [diff] [blame] | 874 | std::string currentReasonIfUnsupported; |
Cathal Corbett | 80f71a8 | 2022-12-20 18:25:40 +0000 | [diff] [blame] | 875 | |
| 876 | // To run FP16 operations on CpuAcc we need at least v8.2 architecture. If the available architecture |
| 877 | // is older than v8.2, we can check if the operator is supported by changing operator inputs & outputs |
| 878 | // to be FP32 and inserting convert layers around the FP32 operator. |
Ryan OShea | b4c4934 | 2023-07-25 14:28:27 +0100 | [diff] [blame] | 879 | bool isLayerSupported = IWorkloadFactory::IsLayerSupported(*layer, EmptyOptional(), currentReasonIfUnsupported); |
| 880 | reasonIfUnsupported += currentReasonIfUnsupported; |
| 881 | // This string matches the error message that is produced by acl when attempting to run FP16 kernels on |
| 882 | // a cpu or build that does not have fp16 support. We use this to check if we should add |
| 883 | // conversion layers or not. |
Cathal Corbett | 80f71a8 | 2022-12-20 18:25:40 +0000 | [diff] [blame] | 884 | std::string checkStr = "This CPU architecture does not support F16 data type, you need v8.2 or above"; |
Ryan OShea | b4c4934 | 2023-07-25 14:28:27 +0100 | [diff] [blame] | 885 | if (!isLayerSupported || currentReasonIfUnsupported.find(checkStr) != std::string::npos) |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 886 | { |
| 887 | if (dataTypeIn == DataType::Float16 || dataTypeOut == DataType::Float16) |
| 888 | { |
| 889 | if (IWorkloadFactory::IsLayerSupported(*layer, DataType::Float32, reasonIfUnsupported) |
| 890 | && layer->GetType() != LayerType::ConvertFp32ToFp16 |
| 891 | && layer->GetType() != LayerType::ConvertFp16ToFp32) |
| 892 | { |
Jan Eilers | 0c0019c | 2021-08-20 16:42:58 +0100 | [diff] [blame] | 893 | auto ConstantLayerFromFp16ToFp32 = [](Layer& layer) |
| 894 | { |
| 895 | if (layer.GetType() == LayerType::Constant) |
| 896 | { |
| 897 | ConstantLayer* constantLayer = PolymorphicDowncast<ConstantLayer*>(&layer); |
| 898 | |
| 899 | auto& info = constantLayer->m_LayerOutput->GetTensorInfo(); |
| 900 | |
| 901 | if (info.GetDataType() == DataType::Float16) |
| 902 | { |
| 903 | std::vector<float> newValues(info.GetNumElements()); |
| 904 | |
| 905 | armnnUtils::FloatingPointConverter::ConvertFloat16To32( |
| 906 | constantLayer->m_LayerOutput->GetConstTensor<Half>(), |
| 907 | info.GetNumElements(), |
| 908 | newValues.data()); |
| 909 | |
| 910 | TensorInfo newInfo(info); |
| 911 | newInfo.SetDataType(DataType::Float32); |
| 912 | ConstTensor newInput(newInfo, newValues); |
| 913 | constantLayer->m_LayerOutput.reset(new ScopedTensorHandle(newInput)); |
| 914 | |
| 915 | layer.GetOutputSlot(0).SetTensorInfo(newInfo); |
| 916 | } |
| 917 | } |
| 918 | }; |
| 919 | |
| 920 | bool checkType = false; |
| 921 | |
| 922 | for (auto inputSlot : layer->GetInputSlots()) |
| 923 | { |
| 924 | auto connectedOutputSlot = inputSlot.GetConnectedOutputSlot(); |
| 925 | if (connectedOutputSlot->GetOwningLayer().GetType() == LayerType::Constant) |
| 926 | { |
| 927 | if (connectedOutputSlot->GetNumConnections() == 1) |
| 928 | { |
| 929 | checkType = true; |
| 930 | ConstantLayerFromFp16ToFp32(connectedOutputSlot->GetOwningLayer()); |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 935 | // Insert FP16 -> FP32 conversion layer before current layer |
| 936 | std::vector<ConvertFp16ToFp32Layer*> convertFp16ToFp32Layers; |
| 937 | if (dataTypeIn == DataType::Float16) |
| 938 | { |
| 939 | convertFp16ToFp32Layers = |
Jan Eilers | 0c0019c | 2021-08-20 16:42:58 +0100 | [diff] [blame] | 940 | InsertConvertFp16ToFp32LayersBefore(graph, *layer, checkType); |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | // Insert FP32 -> FP16 conversion layer after current layer |
| 944 | std::vector<ConvertFp32ToFp16Layer*> convertFp32ToFp16Layers; |
| 945 | if (dataTypeOut == DataType::Float16) |
| 946 | { |
| 947 | convertFp32ToFp16Layers = |
| 948 | InsertConvertFp32ToFp16LayersAfter(graph, *layer); |
| 949 | } |
| 950 | |
| 951 | // Assign a supported backend to the newly introduced conversion layers |
| 952 | auto AssignFirstSupportedBackend = [&](Layer* layer, BackendId preferredBackend) |
| 953 | { |
| 954 | bool supportedBackendFound = false; |
| 955 | std::string reasonIfUnsupported; |
| 956 | |
| 957 | // Try preferred backend first |
| 958 | layer->SetBackendId(preferredBackend); |
| 959 | if (IWorkloadFactory::IsLayerSupported(*layer, |
| 960 | EmptyOptional(), |
| 961 | reasonIfUnsupported)) |
| 962 | { |
| 963 | supportedBackendFound = true; |
| 964 | } |
| 965 | else |
| 966 | { |
| 967 | for (const auto& backend : availablePreferredBackends) |
| 968 | { |
| 969 | // Skip preferred backend (we already determined that it is not supported) |
| 970 | if (backend == preferredBackend) |
| 971 | { |
| 972 | continue; |
| 973 | } |
| 974 | |
| 975 | layer->SetBackendId(backend); |
| 976 | if (IWorkloadFactory::IsLayerSupported(*layer, |
| 977 | EmptyOptional(), |
| 978 | reasonIfUnsupported)) |
| 979 | { |
| 980 | supportedBackendFound = true; |
| 981 | break; |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | return supportedBackendFound; |
| 987 | }; |
| 988 | |
| 989 | for (ConvertFp16ToFp32Layer* convertLayer : convertFp16ToFp32Layers) |
| 990 | { |
| 991 | if (!AssignFirstSupportedBackend(convertLayer, backend)) |
| 992 | { |
| 993 | return ReturnError(convertLayer); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | for (ConvertFp32ToFp16Layer* convertLayer : convertFp32ToFp16Layers) |
| 998 | { |
| 999 | if (!AssignFirstSupportedBackend(convertLayer, backend)) |
| 1000 | { |
| 1001 | return ReturnError(convertLayer); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | return result; |
| 1006 | } |
| 1007 | } |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 1008 | |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 1009 | std::stringstream warningMsg; |
| 1010 | warningMsg << "Layer of type " << GetLayerTypeAsCString(layer->GetType()) |
| 1011 | << " is not supported on requested backend " << layer->GetBackendId().Get() |
| 1012 | << " for input data type " << GetDataTypeName(dataTypeIn) |
| 1013 | << " and output data type " << GetDataTypeName(dataTypeOut) |
| 1014 | << " (reason: " << reasonIfUnsupported |
| 1015 | << "), falling back to the next backend."; |
| 1016 | ReportWarning(warningMsg.str(), errMessages); |
| 1017 | |
| 1018 | return OptimizationResult(true, false); |
| 1019 | } |
| 1020 | else |
| 1021 | { |
| 1022 | return result; |
| 1023 | } |
| 1024 | } |
| 1025 | |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 1026 | inline std::vector<DataType> GetLayerInOutDatatype(const Layer* layer) |
| 1027 | { |
| 1028 | DataType dataTypeIn = layer->GetNumInputSlots() == 0 ? DataType::Float32 : |
| 1029 | layer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo().GetDataType(); |
| 1030 | DataType dataTypeOut = layer->GetNumOutputSlots() == 0 ? DataType::Float32 : |
| 1031 | layer->GetOutputSlot(0).GetTensorInfo().GetDataType(); |
| 1032 | return {dataTypeIn, dataTypeOut}; |
| 1033 | } |
| 1034 | |
Ryan OShea | b4c4934 | 2023-07-25 14:28:27 +0100 | [diff] [blame] | 1035 | bool CheckFp16Support(BackendsMap& backends, |
| 1036 | const std::vector<BackendId>& availablePreferredBackends) |
| 1037 | { |
| 1038 | bool hasFp16 = false; |
| 1039 | // Check if the first preferred backend has FP16 support |
| 1040 | auto firstBackend = availablePreferredBackends[0]; |
| 1041 | auto backendObjPtr = backends.find(firstBackend)->second.get(); |
| 1042 | ARMNN_ASSERT(backendObjPtr); |
| 1043 | auto hasFp16Capability = BackendOptions::BackendOption{"HasFp16", true}; |
| 1044 | auto backendCapabilities = backendObjPtr->GetCapabilities(); |
| 1045 | |
| 1046 | if (HasMatchingCapability(hasFp16Capability, backendCapabilities)) |
| 1047 | { |
| 1048 | // First preferred backend has FP16 support. Enable reduce FP32 to FP16 when fp16-turbo-mode is enabled. |
| 1049 | hasFp16 = true; |
| 1050 | ARMNN_LOG(debug) << "The first available preferred backend: " << firstBackend |
| 1051 | << ", has FP16 support."; |
| 1052 | } |
| 1053 | else |
| 1054 | { |
| 1055 | ARMNN_LOG(warning) << "The first available preferred backend: " << firstBackend |
| 1056 | << ", does not have FP16 support. " |
| 1057 | << "The FP16 turbo mode option will be disable. It will run using FP32."; |
| 1058 | } |
| 1059 | |
| 1060 | // Check if the rest of the available preferred backends have FP16 support |
| 1061 | for (size_t i = 1; i < availablePreferredBackends.size(); ++i) |
| 1062 | { |
| 1063 | auto backend = availablePreferredBackends[i]; |
| 1064 | backendObjPtr = backends.find(backend)->second.get(); |
| 1065 | backendCapabilities = backendObjPtr->GetCapabilities(); |
| 1066 | if (!HasMatchingCapability(hasFp16Capability, backendCapabilities)) |
| 1067 | { |
| 1068 | ARMNN_LOG(warning) << "Next preferred backend: " << backend << ", does not have FP16 support. " |
| 1069 | << "It will run using FP32 when falling back to this backend."; |
| 1070 | } |
| 1071 | else |
| 1072 | { |
| 1073 | ARMNN_LOG(debug) << "Next preferred backend: " << backend << ", has FP16 support."; |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | return hasFp16; |
| 1078 | } |
| 1079 | |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1080 | // Refactor to allow passing the IConnectableLayer* rather than Layer Iterator |
| 1081 | // on Graph and SubgraphView which are different types. |
| 1082 | void AssignBackendsIConnectable(OptimizedNetworkImpl* optNetObjPtr, |
| 1083 | IConnectableLayer* it, |
| 1084 | Optional<std::vector<std::string>&> errMessages, |
| 1085 | OptimizationResult& result, |
| 1086 | BackendSettings& backendSettings, |
| 1087 | std::vector<BackendId>& availablePreferredBackends) |
| 1088 | { |
| 1089 | auto ReturnError = [&](const Layer* layer) |
| 1090 | { |
| 1091 | return ReturnWithError(result, layer, backendSettings, errMessages); |
| 1092 | }; |
| 1093 | |
| 1094 | auto layer = PolymorphicDowncast<Layer*>(it); |
| 1095 | |
| 1096 | if (layer->GetType() == LayerType::Input) |
| 1097 | { |
| 1098 | return; |
| 1099 | } |
| 1100 | |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 1101 | std::vector<DataType> inOutDataType = GetLayerInOutDatatype(layer); |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1102 | |
| 1103 | std::string reasonIfUnsupported; |
| 1104 | bool found = false; |
| 1105 | if (!CheckScaleSetOnQuantizedType(layer, errMessages)) |
| 1106 | { |
| 1107 | // don't bomb immediately, find all the quantized outputs |
| 1108 | // which haven't had a scale set and report them all back. |
| 1109 | result.m_Error = true; |
| 1110 | } |
| 1111 | |
| 1112 | // First try assign layer to hint backend |
| 1113 | if (layer->GetBackendHint().has_value() && |
| 1114 | backendSettings.IsBackendSupported(layer->GetBackendHint().value()) && |
| 1115 | AttemptBackendAssignment(backendSettings, |
| 1116 | optNetObjPtr->GetGraph(), |
| 1117 | layer, |
| 1118 | layer->GetBackendHint().value(), |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 1119 | inOutDataType[0], |
| 1120 | inOutDataType[1], |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1121 | availablePreferredBackends, |
| 1122 | reasonIfUnsupported, |
| 1123 | errMessages).IsOk()) |
| 1124 | { |
| 1125 | found = true; |
| 1126 | backendSettings.m_SelectedBackends.insert(layer->GetBackendHint().value()); |
| 1127 | } |
| 1128 | else |
| 1129 | { |
| 1130 | // Try assign layer to prefered list of backends |
| 1131 | for (const auto& backend : availablePreferredBackends) |
| 1132 | { |
| 1133 | if (layer->GetBackendHint().has_value() && |
| 1134 | layer->GetBackendHint().value() == backend) |
| 1135 | { |
| 1136 | continue; //Don't re-test the backend hint |
| 1137 | } |
| 1138 | |
| 1139 | OptimizationResult res = AttemptBackendAssignment(backendSettings, |
| 1140 | optNetObjPtr->GetGraph(), |
| 1141 | layer, |
| 1142 | backend, |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 1143 | inOutDataType[0], |
| 1144 | inOutDataType[1], |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1145 | availablePreferredBackends, |
| 1146 | reasonIfUnsupported, |
| 1147 | errMessages); |
| 1148 | |
| 1149 | if (res.IsOk()) |
| 1150 | { |
| 1151 | found = true; |
| 1152 | backendSettings.m_SelectedBackends.insert(backend); |
| 1153 | break; |
| 1154 | } |
| 1155 | else if (res.IsError()) |
| 1156 | { |
| 1157 | result = res; // Cannot continue. |
| 1158 | // Note: we don't need to log the error as it would already |
| 1159 | // be logged in AttemptBackendAssignment(). |
| 1160 | } |
| 1161 | else |
| 1162 | { |
| 1163 | ARMNN_ASSERT_MSG(res.IsWarningOnly(), "OptimizationResult in unexpected state."); |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | // If the layer is unsupported by any devices, log and return a null network. |
| 1169 | if (!found) |
| 1170 | { |
| 1171 | // NOTE: if the layer is not an operation queue type AND we have not got CpuRef as a |
| 1172 | // fallback we should set the compute device on the layer to CpuRef (these are not |
| 1173 | // available as accelerated operations, or are only available under certain |
| 1174 | // conditions, currently they comprise MemCopy, Constant, Permute) |
| 1175 | armnn::LayerType layerType = layer->GetType(); |
| 1176 | if (!backendSettings.IsCpuRefUsed() && (layerType == armnn::LayerType::MemCopy || |
| 1177 | layerType == armnn::LayerType::Constant || |
| 1178 | layerType == armnn::LayerType::Permute)) |
| 1179 | { |
| 1180 | BackendId cpuBackendId(armnn::Compute::CpuRef); |
| 1181 | layer->SetBackendId(cpuBackendId); |
| 1182 | backendSettings.m_SelectedBackends.insert(cpuBackendId); |
| 1183 | } |
| 1184 | else |
| 1185 | { |
| 1186 | result = ReturnError(layer); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | } |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 1191 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 1192 | OptimizationResult AssignBackends(OptimizedNetworkImpl* optNetObjPtr, |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1193 | BackendSettings& backendSettings, |
| 1194 | Graph::Iterator& firstLayer, |
| 1195 | Graph::Iterator& lastLayer, |
| 1196 | Optional<std::vector<std::string>&> errMessages) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1197 | { |
Derek Lamberti | f1e0ad3 | 2021-10-13 18:02:25 +0100 | [diff] [blame] | 1198 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Optimizer_AssignBackends"); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1199 | OptimizationResult result; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1200 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1201 | auto availablePreferredBackends = backendSettings.GetAvailablePreferredBackends(); |
| 1202 | if (availablePreferredBackends.empty()) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1203 | { |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1204 | std::stringstream failureMsg; |
| 1205 | failureMsg << "No preferred backends are available"; |
| 1206 | ReportError(failureMsg.str(), errMessages); |
| 1207 | |
| 1208 | result.m_Error = true; |
| 1209 | return result; |
| 1210 | } |
| 1211 | |
| 1212 | for (auto it = firstLayer; it != lastLayer; ++it) |
| 1213 | { |
Cathal Corbett | 5383767 | 2022-09-01 11:34:37 +0100 | [diff] [blame] | 1214 | auto layer = PolymorphicDowncast<Layer*>(*it); |
| 1215 | std::vector<DataType> inOutDataType = GetLayerInOutDatatype(layer); |
| 1216 | |
| 1217 | // In AttemptBackendAssignment() we check: |
| 1218 | // - if input/output datatypes of the layer are float16 |
| 1219 | // - if the layer is supported with these datatypes |
| 1220 | // If the layer is not supported (failing on ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED() in clframework), |
| 1221 | // we attempt to insert convertion layers either side of the new fp32 layer. |
| 1222 | bool isFloat16 = false; |
| 1223 | for (auto type : inOutDataType) |
| 1224 | { |
| 1225 | if (type == DataType::Float16) |
| 1226 | { |
| 1227 | isFloat16 = true; |
| 1228 | break; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | if (layer->GetBackendId() == "Unknown" || isFloat16) |
| 1233 | { |
| 1234 | AssignBackendsIConnectable(optNetObjPtr, |
| 1235 | *it, |
| 1236 | errMessages, |
| 1237 | result, |
| 1238 | backendSettings, |
| 1239 | availablePreferredBackends); |
| 1240 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1241 | } |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1242 | |
Finn Williams | b1aad42 | 2021-10-28 19:07:32 +0100 | [diff] [blame] | 1243 | for (auto it = firstLayer; it != lastLayer; ++it) |
| 1244 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1245 | auto layer = PolymorphicDowncast<Layer*>(*it); |
| 1246 | |
| 1247 | if(layer->GetType() == LayerType::Input) |
| 1248 | { |
| 1249 | BackendId connectedBackendId = layer->GetOutputSlot(0).GetConnection(0)->GetOwningLayer().GetBackendId(); |
| 1250 | layer->SetBackendId(connectedBackendId); |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | return result; |
| 1255 | } |
| 1256 | |
| 1257 | OptimizationResult AssignBackends(OptimizedNetworkImpl* optNetObjPtr, |
| 1258 | BackendSettings& backendSettings, |
| 1259 | SubgraphView::IConnectableLayerIterator& firstLayer, |
| 1260 | SubgraphView::IConnectableLayerIterator& lastLayer, |
| 1261 | Optional<std::vector<std::string>&> errMessages) |
| 1262 | { |
| 1263 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Optimizer_AssignBackends"); |
| 1264 | OptimizationResult result; |
| 1265 | |
| 1266 | auto availablePreferredBackends = backendSettings.GetAvailablePreferredBackends(); |
| 1267 | if (availablePreferredBackends.empty()) |
| 1268 | { |
| 1269 | std::stringstream failureMsg; |
| 1270 | failureMsg << "No preferred backends are available"; |
| 1271 | ReportError(failureMsg.str(), errMessages); |
| 1272 | |
| 1273 | result.m_Error = true; |
| 1274 | return result; |
| 1275 | } |
| 1276 | |
| 1277 | for (auto it = firstLayer; it != lastLayer; ++it) |
| 1278 | { |
| 1279 | AssignBackendsIConnectable(optNetObjPtr, |
| 1280 | *it, |
| 1281 | errMessages, |
| 1282 | result, |
| 1283 | backendSettings, |
| 1284 | availablePreferredBackends); |
| 1285 | } |
| 1286 | |
| 1287 | for (auto it = firstLayer; it != lastLayer; ++it) |
| 1288 | { |
| 1289 | auto layer = PolymorphicDowncast<Layer*>(*it); |
Finn Williams | b1aad42 | 2021-10-28 19:07:32 +0100 | [diff] [blame] | 1290 | |
| 1291 | if(layer->GetType() == LayerType::Input) |
| 1292 | { |
| 1293 | BackendId connectedBackendId = layer->GetOutputSlot(0).GetConnection(0)->GetOwningLayer().GetBackendId(); |
| 1294 | layer->SetBackendId(connectedBackendId); |
| 1295 | } |
| 1296 | } |
| 1297 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1298 | return result; |
| 1299 | } |
| 1300 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 1301 | OptimizationResult AssignBackends(OptimizedNetworkImpl* optNetObjPtr, |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1302 | BackendSettings& backendSettings, |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 1303 | SubgraphView& subgraph, |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1304 | Optional<std::vector<std::string>&> errMessages) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1305 | { |
Francis Murtagh | 0f3e9a0 | 2023-07-28 14:29:46 +0100 | [diff] [blame] | 1306 | SubgraphView::IConnectableLayerIterator firstLayer = subgraph.begin(); |
| 1307 | SubgraphView::IConnectableLayerIterator lastLayer = subgraph.end(); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1308 | return AssignBackends(optNetObjPtr, |
| 1309 | backendSettings, |
| 1310 | firstLayer, |
| 1311 | lastLayer, |
| 1312 | errMessages); |
| 1313 | } |
| 1314 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1315 | BackendsMap CreateSupportedBackends(TensorHandleFactoryRegistry& handleFactoryRegistry, |
| 1316 | BackendSettings& backendSettings) |
| 1317 | { |
| 1318 | BackendsMap backends; |
| 1319 | auto const& backendRegistry = BackendRegistryInstance(); |
| 1320 | for (auto&& selectedBackend : backendSettings.m_SupportedBackends) |
| 1321 | { |
| 1322 | auto backendFactory = backendRegistry.GetFactory(selectedBackend); |
| 1323 | auto backendObjPtr = backendFactory(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1324 | ARMNN_ASSERT(backendObjPtr); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1325 | |
| 1326 | backendObjPtr->RegisterTensorHandleFactories(handleFactoryRegistry); |
| 1327 | |
| 1328 | backends[backendObjPtr->GetId()] = std::move(backendObjPtr); |
| 1329 | } |
| 1330 | |
| 1331 | return backends; |
| 1332 | } |
| 1333 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 1334 | OptimizationResult ApplyBackendOptimizations(OptimizedNetworkImpl* optNetObjPtr, |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1335 | BackendSettings& backendSettings, |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1336 | BackendsMap& backends, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 1337 | const ModelOptions& modelOptions, |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1338 | Optional<std::vector<std::string>&> errMessages) |
| 1339 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1340 | ARMNN_ASSERT(optNetObjPtr); |
Derek Lamberti | f1e0ad3 | 2021-10-13 18:02:25 +0100 | [diff] [blame] | 1341 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Optimizer_ApplyBackendOptimizations") |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1342 | OptimizationResult result; |
| 1343 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1344 | // Get the optimized graph |
| 1345 | Graph& optGraph = optNetObjPtr->GetGraph(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1346 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1347 | // Run backend specific optimizations |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1348 | for (auto&& selectedBackend : backendSettings.m_SelectedBackends) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1349 | { |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1350 | auto backendObjPtr = backends.find(selectedBackend)->second.get(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1351 | ARMNN_ASSERT(backendObjPtr); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1352 | |
Francis Murtagh | d97db7e | 2023-01-16 13:11:29 +0000 | [diff] [blame] | 1353 | if (selectedBackend == armnn::Compute::GpuAcc || selectedBackend == armnn::Compute::CpuAcc) |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 1354 | { |
| 1355 | Optimizer::Pass(optGraph, MakeOptimizations(optimizations::PermuteDepthwiseConv2dWeights())); |
Cathal Corbett | 541880f | 2022-05-16 15:20:56 +0100 | [diff] [blame] | 1356 | Optimizer::Pass(optGraph, MakeOptimizations(optimizations::FusePermuteIntoConstLayer())); |
Cathal Corbett | 4b19d22 | 2022-05-11 20:12:17 +0100 | [diff] [blame] | 1357 | } |
| 1358 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1359 | // Select sub-graphs based on backend |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 1360 | SubgraphViewSelector::Subgraphs subgraphs = |
Rob Hughes | 65c3226 | 2019-07-23 15:33:39 +0100 | [diff] [blame] | 1361 | SubgraphViewSelector::SelectSubgraphs(optGraph, |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 1362 | // Select layers assigned to the requested backend |
| 1363 | [&backendObjPtr](const Layer& layer) |
| 1364 | { |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1365 | |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 1366 | return layer.GetType() != LayerType::Input && |
| 1367 | layer.GetType() != LayerType::Output && |
| 1368 | layer.GetBackendId() == backendObjPtr->GetId(); |
| 1369 | }); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 1370 | if (subgraphs.empty()) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1371 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1372 | // No sub-graphs found, try with next selected backend |
| 1373 | continue; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1374 | } |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1375 | |
| 1376 | // Try to optimize each sub-graph |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 1377 | for (auto& subgraph : subgraphs) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1378 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1379 | // Try to optimize the current sub-graph |
Derek Lamberti | f1e0ad3 | 2021-10-13 18:02:25 +0100 | [diff] [blame] | 1380 | ARMNN_SCOPED_PROFILING_EVENT(backendObjPtr->GetId(), "Optimizer_OptimizeSubgraph"); |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 1381 | OptimizationViews optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraph, modelOptions); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1382 | ARMNN_ASSERT(optimizationViews.Validate(*subgraph)); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1383 | |
| 1384 | // Optimization attempted, check the resulting optimized sub-graph |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 1385 | for (auto& substitution : optimizationViews.GetSubstitutions()) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1386 | { |
| 1387 | // Sub-graph optimized, substitute the sub-graph with the new optimized one in the main optimized graph |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 1388 | SubgraphView& replacementSubgraph = substitution.m_ReplacementSubgraph; |
| 1389 | SubgraphView& substitutableSubgraph = substitution.m_SubstitutableSubgraph; |
| 1390 | optGraph.SubstituteSubgraph(substitutableSubgraph, replacementSubgraph); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1391 | |
| 1392 | // Assign the current backend to the optimized sub-graph |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1393 | const SubgraphView::IConnectableLayers& subgraphLayers = replacementSubgraph.GetIConnectableLayers(); |
| 1394 | std::for_each(subgraphLayers.begin(), subgraphLayers.end(), [&selectedBackend](IConnectableLayer* l) |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 1395 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1396 | ARMNN_ASSERT(l); |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1397 | PolymorphicDowncast<Layer*>(l)->SetBackendId(selectedBackend); |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 1398 | }); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1399 | } |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 1400 | |
Mike Kelly | 4cc341c | 2023-07-07 15:43:06 +0100 | [diff] [blame] | 1401 | // Remove deleted sub-graphs |
| 1402 | for (auto& deletedSubgraph : optimizationViews.GetDeletedSubgraphs()) |
| 1403 | { |
| 1404 | for (auto& l : deletedSubgraph.GetIConnectableLayers()) |
| 1405 | { |
| 1406 | Layer* deletedLayer = PolymorphicDowncast<Layer*>(l); |
| 1407 | for (unsigned int in = deletedLayer->GetNumInputSlots(); in > 0; --in) |
| 1408 | { |
| 1409 | auto inputSlot = deletedLayer->GetInputSlot(in -1); |
| 1410 | OutputSlot* parentOut = inputSlot.GetConnectedOutputSlot(); |
| 1411 | parentOut->Disconnect(inputSlot); |
| 1412 | for (unsigned int out = deletedLayer->GetOutputSlot(in -1).GetNumConnections(); out > 0; --out) |
| 1413 | { |
Mike Kelly | b6de7a1 | 2023-07-18 12:03:41 +0100 | [diff] [blame] | 1414 | InputSlot* childIn = deletedLayer->GetOutputSlot(in - 1).GetConnection(out -1); |
Mike Kelly | 4cc341c | 2023-07-07 15:43:06 +0100 | [diff] [blame] | 1415 | deletedLayer->GetOutputSlot(in - 1).Disconnect(*childIn); |
| 1416 | parentOut->Connect(*childIn); |
| 1417 | } |
| 1418 | } |
| 1419 | optGraph.EraseLayer(deletedLayer); |
| 1420 | } |
| 1421 | } |
| 1422 | |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 1423 | if (!optimizationViews.GetFailedSubgraphs().empty()) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1424 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1425 | std::stringstream warningMsg; |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 1426 | warningMsg << "Some sub-graph(s) failed to optimized on " << backendObjPtr->GetId() << " backend."; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1427 | ReportWarning(warningMsg.str(), errMessages); |
| 1428 | |
| 1429 | // Failed to optimize the given sub-graph, re-assign the sub-graph layers to other available backends |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 1430 | BackendSettings settingsCopy(backendSettings); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1431 | if (!backendObjPtr->GetId().IsCpuRef()) |
| 1432 | { |
| 1433 | // Add the current backend to the list of backends to ignore |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 1434 | settingsCopy.m_IgnoredBackends.insert(backendObjPtr->GetId()); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1435 | } |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 1436 | |
| 1437 | int count=0; |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 1438 | for (auto& failedSubgraph : optimizationViews.GetFailedSubgraphs()) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1439 | { |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 1440 | // An error occurred: the optimization was attempted but not performed, try different backends |
| 1441 | std::stringstream subgraphMsg; |
Francis Murtagh | 56ccf68 | 2021-12-13 18:48:12 +0000 | [diff] [blame] | 1442 | subgraphMsg << "Re-assigning backends to " << failedSubgraph.GetIConnectableLayers().size() |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 1443 | << " layers inside sub-graph " << count++; |
Matteo Martincigh | 328d92b | 2019-07-04 17:52:55 +0100 | [diff] [blame] | 1444 | ReportWarning(subgraphMsg.str(), errMessages); |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 1445 | |
| 1446 | OptimizationResult reassignmentResult = AssignBackends(optNetObjPtr, |
| 1447 | settingsCopy, |
| 1448 | *subgraph, |
| 1449 | errMessages); |
| 1450 | if (reassignmentResult.m_Error) |
| 1451 | { |
| 1452 | // Failed to re-assign one of the remaining backends to each layer of the sub-graph |
| 1453 | result.m_Error = true; |
| 1454 | return result; |
| 1455 | } |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1456 | } |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | return result; |
| 1462 | } |
| 1463 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1464 | bool RequiresCopy(ITensorHandleFactory::FactoryId src, |
| 1465 | ITensorHandleFactory::FactoryId dst, |
| 1466 | TensorHandleFactoryRegistry& registry) |
| 1467 | { |
| 1468 | if (src != dst) |
| 1469 | { |
| 1470 | ITensorHandleFactory* srcFactory = registry.GetFactory(src); |
| 1471 | ITensorHandleFactory* dstFactory = registry.GetFactory(dst); |
| 1472 | |
Matteo Martincigh | a6539ed | 2019-08-27 13:43:32 +0100 | [diff] [blame] | 1473 | if (srcFactory && dstFactory && |
| 1474 | (srcFactory->GetExportFlags() & dstFactory->GetImportFlags()) != 0) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1475 | { |
| 1476 | return false; |
| 1477 | } |
| 1478 | return true; |
| 1479 | } |
| 1480 | return false; |
| 1481 | } |
| 1482 | |
| 1483 | // Find the handle factory for the input layer which results in fewest required copies. |
| 1484 | ITensorHandleFactory::FactoryId CalculateSlotOptionForInput(BackendsMap& backends, |
| 1485 | OutputSlot& slot, |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 1486 | TensorHandleFactoryRegistry& registry, |
| 1487 | bool importEnabled) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1488 | { |
| 1489 | Layer& layer = slot.GetOwningLayer(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1490 | ARMNN_ASSERT(layer.GetType() == LayerType::Input); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1491 | |
| 1492 | // Explicitly select the tensorhandle factory for InputLayer because the rules for it are slightly different. It |
| 1493 | // doesn't matter which backend it is assigned to because they all use the same implementation, which |
| 1494 | // requires Map/Unmap support. This means that, so long as the handle type supports map/unmap semantics, we can |
| 1495 | // select a factory with maximum compatibility with the layers connected to the InputLayer. |
| 1496 | |
| 1497 | // First ensure the from backends can support the TensorHandeAPI |
| 1498 | auto frmBackend = backends.find(layer.GetBackendId()); |
| 1499 | if (frmBackend == backends.end() || |
| 1500 | !frmBackend->second->SupportsTensorAllocatorAPI()) |
| 1501 | { |
| 1502 | return ITensorHandleFactory::LegacyFactoryId; |
| 1503 | } |
| 1504 | |
| 1505 | // Go through all connections to the output slot and determine the TensorHandleFactory which results in the |
| 1506 | // fewest copies. |
| 1507 | std::map<ITensorHandleFactory::FactoryId, int> factoryScores; |
| 1508 | int topScore = 0; |
| 1509 | ITensorHandleFactory::FactoryId topChoice = ITensorHandleFactory::LegacyFactoryId; |
| 1510 | |
| 1511 | for (auto&& connection : slot.GetConnections()) |
| 1512 | { |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 1513 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1514 | const Layer& connectedLayer = connection->GetOwningLayer(); |
| 1515 | |
| 1516 | auto toBackend = backends.find(connectedLayer.GetBackendId()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1517 | ARMNN_ASSERT_MSG(toBackend != backends.end(), "Backend id not found for the connected layer"); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1518 | |
| 1519 | if (!toBackend->second.get()->SupportsTensorAllocatorAPI()) |
| 1520 | { |
| 1521 | // The destination backend does not support the tensor allocator API, move to the next one |
| 1522 | continue; |
| 1523 | } |
| 1524 | |
| 1525 | auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences(); |
| 1526 | for (auto&& dst : dstPrefs) |
| 1527 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1528 | // Input layers use the mem copy workload or import, so the selected factory must |
| 1529 | // support either the map/unmap API or Import API |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1530 | ITensorHandleFactory* factory = registry.GetFactory(dst); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 1531 | if (importEnabled && factory->GetImportFlags() == 0) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1532 | { |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 1533 | continue; |
| 1534 | } |
| 1535 | else if (!importEnabled && !factory->SupportsMapUnmap()) |
| 1536 | { |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1537 | continue; |
| 1538 | } |
| 1539 | |
| 1540 | auto it = factoryScores.find(dst); |
| 1541 | if (it == factoryScores.end()) |
| 1542 | { |
| 1543 | // Add new score to the table |
| 1544 | factoryScores[dst] = 0; |
| 1545 | if (topChoice == ITensorHandleFactory::LegacyFactoryId) |
| 1546 | { |
| 1547 | topChoice = dst; |
| 1548 | } |
| 1549 | } |
| 1550 | else |
| 1551 | { |
| 1552 | // Increase the score |
| 1553 | factoryScores[dst]++; |
| 1554 | |
| 1555 | // Track the best option |
| 1556 | if (factoryScores[dst] > topScore) |
| 1557 | { |
| 1558 | topScore = factoryScores[dst]; |
| 1559 | topChoice = dst; |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | return topChoice; |
| 1566 | } |
| 1567 | |
| 1568 | // Find the handle factory for the output layer which results in fewest required copies. |
| 1569 | ITensorHandleFactory::FactoryId CalculateSlotOptionForOutput(BackendsMap& backends, |
| 1570 | OutputSlot& slot, |
| 1571 | TensorHandleFactoryRegistry& registry) |
| 1572 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 1573 | IgnoreUnused(backends, slot, registry); |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 1574 | return ITensorHandleFactory::DeferredFactoryId; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | // For all handle factories supported on the source backend, we wish to find the one which requires the fewest copies |
| 1578 | // when considering all connections. |
| 1579 | ITensorHandleFactory::FactoryId CalculateSlotOption(BackendsMap& backends, |
| 1580 | OutputSlot& outputSlot, |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 1581 | TensorHandleFactoryRegistry& registry, |
Francis Murtagh | 626bd90 | 2022-06-21 13:16:23 +0000 | [diff] [blame] | 1582 | bool exportEnabled) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1583 | { |
| 1584 | // First ensure the from backends can support the TensorHandeAPI |
| 1585 | Layer& layer = outputSlot.GetOwningLayer(); |
| 1586 | auto frmBackend = backends.find(layer.GetBackendId()); |
| 1587 | if (frmBackend == backends.end() || |
| 1588 | !frmBackend->second->SupportsTensorAllocatorAPI()) |
| 1589 | { |
| 1590 | return ITensorHandleFactory::LegacyFactoryId; |
| 1591 | } |
| 1592 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 1593 | bool outputConnection = false; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1594 | for (auto&& connection : outputSlot.GetConnections()) |
| 1595 | { |
| 1596 | const Layer& connectedLayer = connection->GetOwningLayer(); |
| 1597 | if (connectedLayer.GetType() == LayerType::Output) |
| 1598 | { |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 1599 | outputConnection = true; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | IBackendInternal* srcBackend = frmBackend->second.get(); |
| 1604 | auto srcPrefs = srcBackend->GetHandleFactoryPreferences(); |
| 1605 | |
| 1606 | // Initialize the scores |
| 1607 | std::map<ITensorHandleFactory::FactoryId, int> factoryScores; |
| 1608 | for (auto&& pref : srcPrefs) |
| 1609 | { |
Francis Murtagh | 626bd90 | 2022-06-21 13:16:23 +0000 | [diff] [blame] | 1610 | if (exportEnabled) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1611 | { |
| 1612 | ITensorHandleFactory* factory = registry.GetFactory(pref); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 1613 | if (outputConnection) |
| 1614 | { |
| 1615 | // Check if this is fallback case |
| 1616 | bool fallbackConnection = false; |
| 1617 | for (auto&& inputSlot : layer.GetInputSlots()) |
| 1618 | { |
| 1619 | if (inputSlot.GetConnectedOutputSlot()->GetOwningLayer().GetBackendId() != layer.GetBackendId()) |
| 1620 | { |
| 1621 | fallbackConnection = true; |
| 1622 | } |
| 1623 | } |
| 1624 | if (fallbackConnection) |
| 1625 | { |
| 1626 | auto factoryCap = factory->GetCapabilities(&layer, &layer, CapabilityClass::FallbackImportDisabled); |
| 1627 | // Cannot use factory import if fallback import is not supported. |
| 1628 | if (!factoryCap.empty()) |
| 1629 | { |
| 1630 | continue; |
| 1631 | } |
| 1632 | } |
| 1633 | else if (factory->GetExportFlags() == 0) |
| 1634 | { |
| 1635 | continue; |
| 1636 | } |
| 1637 | } |
| 1638 | if (!outputConnection) |
| 1639 | { |
| 1640 | auto factoryCap = factory->GetCapabilities(&layer, &layer, CapabilityClass::FallbackImportDisabled); |
| 1641 | // Cannot use factory import if fallback import is not supported. |
| 1642 | if (!factoryCap.empty()) |
| 1643 | { |
| 1644 | continue; |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | } |
| 1649 | else |
| 1650 | { |
| 1651 | // Only consider factories that support map/unmap |
| 1652 | ITensorHandleFactory* factory = registry.GetFactory(pref); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1653 | if (!factory->SupportsMapUnmap()) |
| 1654 | { |
| 1655 | // The current tensor handle factory does not support the map/unmap strategy, move to the next one |
| 1656 | continue; |
| 1657 | } |
| 1658 | } |
| 1659 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 1660 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1661 | auto it = factoryScores.find(pref); |
| 1662 | if (it == factoryScores.end()) |
| 1663 | { |
| 1664 | // Add new score to the table |
| 1665 | factoryScores[pref] = 0; |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | // Score each handle factory based on how many times it requires copies on the slot connections |
| 1670 | for (auto&& connection : outputSlot.GetConnections()) |
| 1671 | { |
| 1672 | const Layer& connectedLayer = connection->GetOwningLayer(); |
| 1673 | |
| 1674 | auto toBackend = backends.find(connectedLayer.GetBackendId()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1675 | ARMNN_ASSERT_MSG(toBackend != backends.end(), "Backend id not found for the connected layer"); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1676 | |
| 1677 | auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences(); |
| 1678 | for (auto&& src : srcPrefs) |
| 1679 | { |
| 1680 | if (factoryScores.find(src) == factoryScores.end()) // Don't consider excluded factories |
| 1681 | { |
| 1682 | continue; |
| 1683 | } |
| 1684 | |
| 1685 | for (auto&& dst : dstPrefs) |
| 1686 | { |
| 1687 | if (RequiresCopy(src, dst, registry)) |
| 1688 | { |
| 1689 | // Copy avoided, increase the score |
| 1690 | factoryScores[src]++; |
| 1691 | break; |
| 1692 | } |
| 1693 | } |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | // Find the lowest score |
| 1698 | int minScore = std::numeric_limits<int>::max(); |
| 1699 | for (auto it : factoryScores) |
| 1700 | { |
| 1701 | minScore = std::min(minScore, it.second); |
| 1702 | } |
| 1703 | |
| 1704 | // Collect factories matching the best(lowest) score |
| 1705 | std::vector<ITensorHandleFactory::FactoryId> optimalFactories; |
| 1706 | for (auto it : factoryScores) |
| 1707 | { |
| 1708 | if (it.second == minScore) |
| 1709 | { |
| 1710 | optimalFactories.push_back(it.first); |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | // For all compatible Factories matching the best score, find the preferred one for the current layer. |
| 1715 | for (auto&& srcPref : srcPrefs) |
| 1716 | { |
| 1717 | for (auto&& comp : optimalFactories) |
| 1718 | { |
| 1719 | if (comp == srcPref) |
| 1720 | { |
| 1721 | return comp; |
| 1722 | } |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | return ITensorHandleFactory::LegacyFactoryId; |
| 1727 | } |
| 1728 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1729 | EdgeStrategy CalculateEdgeStrategy(BackendsMap& backends, |
| 1730 | ITensorHandleFactory::FactoryId srcFactoryId, |
| 1731 | const Layer& layer, |
| 1732 | const Layer& connectedLayer, |
Narumol Prangnawarat | a2493a0 | 2020-08-19 14:39:07 +0100 | [diff] [blame] | 1733 | TensorHandleFactoryRegistry& registry, |
| 1734 | bool importEnabled) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1735 | { |
| 1736 | auto toBackend = backends.find(connectedLayer.GetBackendId()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1737 | ARMNN_ASSERT_MSG(toBackend != backends.end(), "Backend id not found for the connected layer"); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1738 | |
| 1739 | auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences(); |
| 1740 | |
| 1741 | // Legacy API check for backward compatibility |
| 1742 | if (srcFactoryId == ITensorHandleFactory::LegacyFactoryId || dstPrefs.empty()) |
| 1743 | { |
| 1744 | if (layer.GetBackendId() != connectedLayer.GetBackendId()) |
| 1745 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1746 | return EdgeStrategy::CopyToTarget; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1747 | } |
| 1748 | else |
| 1749 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1750 | return EdgeStrategy::DirectCompatibility; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | // TensorHandleFactory API present, so perform more sophisticated strategies. |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1755 | // Dst Output layers don't require copy because they use import or map/unmap |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1756 | if (connectedLayer.GetType() == LayerType::Output) |
| 1757 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1758 | return EdgeStrategy::DirectCompatibility; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | // Search for direct match in prefs |
| 1762 | for (auto&& pref : dstPrefs) |
| 1763 | { |
| 1764 | if (pref == srcFactoryId) |
| 1765 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1766 | return EdgeStrategy::DirectCompatibility; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | // Search for export/import options |
| 1771 | ITensorHandleFactory* srcFactory = registry.GetFactory(srcFactoryId); |
Narumol Prangnawarat | a2493a0 | 2020-08-19 14:39:07 +0100 | [diff] [blame] | 1772 | if (srcFactory->GetExportFlags() != 0 && importEnabled) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1773 | { |
| 1774 | for (auto&& pref : dstPrefs) |
| 1775 | { |
| 1776 | ITensorHandleFactory* dstFactory = registry.GetFactory(pref); |
James Conroy | ffab16f | 2019-11-07 14:37:09 +0000 | [diff] [blame] | 1777 | |
James Conroy | 47e863d | 2019-11-18 17:07:43 +0000 | [diff] [blame] | 1778 | // Handles cases when a destPref is not listed in TensorHandleFactoryRegistry |
James Conroy | ffab16f | 2019-11-07 14:37:09 +0000 | [diff] [blame] | 1779 | if (!dstFactory) { |
James Conroy | 47e863d | 2019-11-18 17:07:43 +0000 | [diff] [blame] | 1780 | continue; |
James Conroy | ffab16f | 2019-11-07 14:37:09 +0000 | [diff] [blame] | 1781 | } |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1782 | if ((dstFactory->GetImportFlags() & srcFactory->GetExportFlags()) != 0) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1783 | { |
Narumol Prangnawarat | b8d771a | 2020-08-14 11:51:12 +0100 | [diff] [blame] | 1784 | auto srcCapability = srcFactory->GetCapabilities(&layer, &layer, CapabilityClass::PaddingRequired); |
| 1785 | auto dstCapability = dstFactory->GetCapabilities(&connectedLayer, |
| 1786 | &connectedLayer, |
| 1787 | CapabilityClass::PaddingRequired); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 1788 | auto srcFallback = srcFactory->GetCapabilities(&layer, &layer, CapabilityClass::FallbackImportDisabled); |
| 1789 | auto dstFallback = dstFactory->GetCapabilities(&connectedLayer, |
| 1790 | &connectedLayer, |
| 1791 | CapabilityClass::FallbackImportDisabled); |
Narumol Prangnawarat | b8d771a | 2020-08-14 11:51:12 +0100 | [diff] [blame] | 1792 | // Do not require memory copy if the source and destination do not require padding. |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 1793 | if (srcCapability.empty() && dstCapability.empty() && srcFallback.empty() && dstFallback.empty()) |
Narumol Prangnawarat | b8d771a | 2020-08-14 11:51:12 +0100 | [diff] [blame] | 1794 | { |
| 1795 | return EdgeStrategy::ExportToTarget; |
| 1796 | } |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1797 | } |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | // Search for copy options via map/unmap |
| 1802 | if (srcFactory->SupportsMapUnmap()) |
| 1803 | { |
| 1804 | for (auto&& pref : dstPrefs) |
| 1805 | { |
| 1806 | ITensorHandleFactory* dstFactory = registry.GetFactory(pref); |
James Conroy | 47e863d | 2019-11-18 17:07:43 +0000 | [diff] [blame] | 1807 | if (dstFactory && dstFactory->SupportsMapUnmap()) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1808 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1809 | return EdgeStrategy::CopyToTarget; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1810 | } |
| 1811 | } |
| 1812 | } |
| 1813 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1814 | return EdgeStrategy::Undefined; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | // Select the TensorHandleFactories and the corresponding memory strategy |
| 1818 | OptimizationResult SelectTensorHandleStrategy(Graph& optGraph, |
| 1819 | BackendsMap& backends, |
| 1820 | TensorHandleFactoryRegistry& registry, |
Narumol Prangnawarat | a2493a0 | 2020-08-19 14:39:07 +0100 | [diff] [blame] | 1821 | bool importEnabled, |
Francis Murtagh | 626bd90 | 2022-06-21 13:16:23 +0000 | [diff] [blame] | 1822 | bool exportEnabled, |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1823 | Optional<std::vector<std::string>&> errMessages) |
| 1824 | { |
Derek Lamberti | f1e0ad3 | 2021-10-13 18:02:25 +0100 | [diff] [blame] | 1825 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Optimizer_SelectTensorHandleStrategy"); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1826 | OptimizationResult result; |
| 1827 | |
Francis Murtagh | 626bd90 | 2022-06-21 13:16:23 +0000 | [diff] [blame] | 1828 | optGraph.ForEachLayer([&backends, ®istry, &result, &errMessages, importEnabled, exportEnabled](Layer* layer) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1829 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1830 | ARMNN_ASSERT(layer); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1831 | |
| 1832 | // Lets make sure the backend is in our list of supported backends. Something went wrong during backend |
| 1833 | // assignment if this check fails |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1834 | ARMNN_ASSERT(backends.find(layer->GetBackendId()) != backends.end()); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1835 | |
| 1836 | // Check each output separately |
| 1837 | for (unsigned int slotIdx = 0; slotIdx < layer->GetNumOutputSlots(); slotIdx++) |
| 1838 | { |
| 1839 | OutputSlot& outputSlot = layer->GetOutputSlot(slotIdx); |
| 1840 | |
| 1841 | ITensorHandleFactory::FactoryId slotOption = ITensorHandleFactory::LegacyFactoryId; |
| 1842 | |
| 1843 | // Calculate the factory to use which results in the fewest copies being made. |
| 1844 | switch(layer->GetType()) |
| 1845 | { |
| 1846 | case LayerType::Input: |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 1847 | slotOption = CalculateSlotOptionForInput(backends, outputSlot, registry, importEnabled); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1848 | break; |
| 1849 | case LayerType::Output: |
| 1850 | slotOption = CalculateSlotOptionForOutput(backends, outputSlot, registry); |
| 1851 | break; |
| 1852 | default: |
Francis Murtagh | 626bd90 | 2022-06-21 13:16:23 +0000 | [diff] [blame] | 1853 | slotOption = CalculateSlotOption(backends, outputSlot, registry, exportEnabled); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1854 | break; |
| 1855 | } |
| 1856 | outputSlot.SetTensorHandleFactory(slotOption); |
| 1857 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1858 | // Now determine the "best" edge strategy for each connection given the slotOption. |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1859 | unsigned int connectionIdx = 0; |
| 1860 | for (auto&& connection : outputSlot.GetConnections()) |
| 1861 | { |
| 1862 | const Layer& connectedLayer = connection->GetOwningLayer(); |
| 1863 | |
Narumol Prangnawarat | a2493a0 | 2020-08-19 14:39:07 +0100 | [diff] [blame] | 1864 | EdgeStrategy strategy = CalculateEdgeStrategy(backends, slotOption, *layer, connectedLayer, |
| 1865 | registry, importEnabled); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1866 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1867 | if (strategy == EdgeStrategy::Undefined) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1868 | { |
| 1869 | result.m_Error = true; |
| 1870 | if (errMessages) |
| 1871 | { |
| 1872 | errMessages.value().emplace_back("Could not find valid strategy required for compatibility" |
| 1873 | " between backends."); |
| 1874 | } |
| 1875 | return; |
| 1876 | } |
| 1877 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1878 | outputSlot.SetEdgeStrategy(connectionIdx, strategy); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1879 | |
| 1880 | connectionIdx++; |
| 1881 | } |
| 1882 | } |
| 1883 | }); |
| 1884 | |
| 1885 | return result; |
| 1886 | } |
| 1887 | |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 1888 | // Forwarding function to remain backward compatible with legacy OptimizerOptions |
Cathal Corbett | a3f4fba | 2022-03-21 09:27:08 +0000 | [diff] [blame] | 1889 | IOptimizedNetworkPtr Optimize(const Graph& inGraph, |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1890 | const std::vector<BackendId>& backendPreferences, |
| 1891 | const IDeviceSpec& deviceSpec, |
| 1892 | const OptimizerOptions& options, |
Rob Hughes | 2321443 | 2019-11-05 11:27:36 +0000 | [diff] [blame] | 1893 | Optional<std::vector<std::string>&> messages) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1894 | { |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 1895 | return Optimize(inGraph, |
| 1896 | backendPreferences, |
| 1897 | deviceSpec, |
| 1898 | OptimizerOptionsOpaque(options), |
| 1899 | messages); |
| 1900 | } |
| 1901 | |
| 1902 | IOptimizedNetworkPtr Optimize(const Graph& inGraph, |
| 1903 | const std::vector<BackendId>& backendPreferences, |
| 1904 | const IDeviceSpec& deviceSpec, |
| 1905 | const OptimizerOptionsOpaque& options, |
| 1906 | Optional<std::vector<std::string>&> messages) |
| 1907 | { |
Jan Eilers | 17d34da | 2021-12-08 16:15:12 +0000 | [diff] [blame] | 1908 | ARMNN_LOG(debug) << options.ToString(); |
Jan Eilers | 6a71bb5 | 2021-10-26 17:41:18 +0100 | [diff] [blame] | 1909 | |
Derek Lamberti | f1e0ad3 | 2021-10-13 18:02:25 +0100 | [diff] [blame] | 1910 | // Enable profiling |
Cathal Corbett | a3f4fba | 2022-03-21 09:27:08 +0000 | [diff] [blame] | 1911 | auto profiler = inGraph.GetProfiler(); |
Derek Lamberti | f1e0ad3 | 2021-10-13 18:02:25 +0100 | [diff] [blame] | 1912 | ProfilerManager::GetInstance().RegisterProfiler(profiler.get()); |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 1913 | profiler->EnableProfiling(options.GetProfilingEnabled()); |
Derek Lamberti | f1e0ad3 | 2021-10-13 18:02:25 +0100 | [diff] [blame] | 1914 | |
David Monahan | 8a57046 | 2023-11-22 13:24:25 +0000 | [diff] [blame] | 1915 | // Some backends don't play well together. Check here before continuing. |
| 1916 | { |
| 1917 | std::set<BackendId> backendSet(backendPreferences.begin(), backendPreferences.end()); |
| 1918 | // GpuFsa cannot co-exist with GpuAcc. |
| 1919 | if (backendSet.find("GpuFsa") != backendSet.end() && |
| 1920 | backendSet.find("GpuAcc") != backendSet.end()) |
| 1921 | { |
| 1922 | throw InvalidArgumentException("The backends \"GpuAcc\" and \"GpuFsa\" cannot be specified " |
| 1923 | "for the same optimized network."); |
| 1924 | } |
| 1925 | } |
| 1926 | |
Derek Lamberti | f1e0ad3 | 2021-10-13 18:02:25 +0100 | [diff] [blame] | 1927 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Optimizer"); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1928 | if (backendPreferences.empty()) |
| 1929 | { |
Mike Kelly | 3a613cc | 2020-09-29 20:50:35 +0100 | [diff] [blame] | 1930 | throw InvalidArgumentException("Invoked Optimize with no backends specified"); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 1933 | if (options.GetReduceFp32ToBf16()) |
Ryan OShea | 3144159 | 2022-11-07 16:20:48 +0000 | [diff] [blame] | 1934 | { |
| 1935 | throw InvalidArgumentException("BFloat16 optimization is currently ignored. In order to use Bf16 optimization " |
| 1936 | "Please use the FastMathEnabled backend option for CpuAcc or GpuAcc."); |
| 1937 | } |
| 1938 | |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 1939 | if (options.GetReduceFp32ToFp16() && options.GetReduceFp32ToBf16()) |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 1940 | { |
| 1941 | throw InvalidArgumentException("BFloat16 and Float16 optimization cannot be enabled at the same time."); |
| 1942 | } |
| 1943 | |
Cathal Corbett | 521032f | 2021-10-07 11:46:40 +0100 | [diff] [blame] | 1944 | // Ensure TensorInfo is set on all output slots of ConstantLayers in the graph |
Cathal Corbett | a3f4fba | 2022-03-21 09:27:08 +0000 | [diff] [blame] | 1945 | inGraph.VerifyConstantLayerSetTensorInfo(); |
Cathal Corbett | 521032f | 2021-10-07 11:46:40 +0100 | [diff] [blame] | 1946 | |
Cathal Corbett | a3f4fba | 2022-03-21 09:27:08 +0000 | [diff] [blame] | 1947 | std::unique_ptr<Graph> graph = std::make_unique<Graph>(inGraph); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1948 | |
Francis Murtagh | 626bd90 | 2022-06-21 13:16:23 +0000 | [diff] [blame] | 1949 | // We need to pass on the information about whether import and export is enabled to the LoadNetwork phase. |
| 1950 | // The mechanism to do that is to add model options to the optimized network. |
| 1951 | armnn::BackendOptions importExport("Global", |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 1952 | {{"ImportEnabled", options.GetImportEnabled()}, |
| 1953 | {"ExportEnabled", options.GetExportEnabled()}}); |
| 1954 | ModelOptions optimizedOptions(options.GetModelOptions()); |
Francis Murtagh | 626bd90 | 2022-06-21 13:16:23 +0000 | [diff] [blame] | 1955 | optimizedOptions.push_back(importExport); |
| 1956 | |
| 1957 | auto optNet = IOptimizedNetworkPtr(new IOptimizedNetwork(std::move(graph), optimizedOptions), |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 1958 | &IOptimizedNetwork::Destroy); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1959 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 1960 | IOptimizedNetwork* optNetObjPtr = optNet.get(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1961 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1962 | // Get the optimized graph |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 1963 | Graph& optGraph = optNetObjPtr->pOptimizedNetworkImpl->GetGraph(); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1964 | |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 1965 | if(options.GetShapeInferenceMethod() == ShapeInferenceMethod::InferAndValidate) |
Finn Williams | d218d98 | 2021-08-09 13:00:08 +0100 | [diff] [blame] | 1966 | { |
| 1967 | // Infer the tensor infos for all output slots. Throws an exception on failure |
| 1968 | optGraph.InferTensorInfos(); |
| 1969 | } |
Finn Williams | 84e025a | 2021-08-05 17:29:32 +0100 | [diff] [blame] | 1970 | |
Idriss Chaouch | 98e383e | 2023-08-28 14:28:31 +0100 | [diff] [blame] | 1971 | // Perform BroadcastToOptimizationLayer and then AddBroadcastReshapeLayer optimisation |
Narumol Prangnawarat | 16f82f9 | 2020-09-14 16:12:44 +0100 | [diff] [blame] | 1972 | using namespace optimizations; |
Idriss Chaouch | 98e383e | 2023-08-28 14:28:31 +0100 | [diff] [blame] | 1973 | Optimizer::Pass(optGraph, MakeOptimizations(BroadcastToOptimizationLayer())); |
| 1974 | |
Narumol Prangnawarat | 16f82f9 | 2020-09-14 16:12:44 +0100 | [diff] [blame] | 1975 | Optimizer::Pass(optGraph, MakeOptimizations(AddBroadcastReshapeLayer())); |
| 1976 | |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 1977 | if(options.GetShapeInferenceMethod() == ShapeInferenceMethod::ValidateOnly) |
Finn Williams | d218d98 | 2021-08-09 13:00:08 +0100 | [diff] [blame] | 1978 | { |
| 1979 | // Validate the tensor infos for all output slots. Throws an exception on failure |
| 1980 | optGraph.InferTensorInfos(); |
| 1981 | } |
| 1982 | |
Cathal Corbett | 541880f | 2022-05-16 15:20:56 +0100 | [diff] [blame] | 1983 | |
Francis Murtagh | d97db7e | 2023-01-16 13:11:29 +0000 | [diff] [blame] | 1984 | // Group Constant Layer optimizations together where possible. |
| 1985 | // This is important as: |
| 1986 | // FusePermuteIntoConstantLayer must happen before FoldPadIntoDepthwiseConvolution2d and |
| 1987 | // FuseBatchNormIntoDepthwiseConvolution2D. |
| 1988 | // ConvertConstDequantisationLayersToConstLayers must happen before FoldPadIntoConvolution2d |
| 1989 | Optimizer::Pass(optGraph, MakeOptimizations(FusePermuteIntoConstLayer(), |
| 1990 | ConvertConstDequantisationLayersToConstLayers())); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1991 | // Perform optimisation passes |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1992 | Optimizer::Pass(optGraph, MakeOptimizations(SquashEqualPermuteSiblings(), |
Mike Kelly | 490b7be | 2020-03-03 12:39:09 +0000 | [diff] [blame] | 1993 | SquashEqualTransposeSiblings(), |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1994 | SquashEqualReshapeSiblings(), |
| 1995 | OptimizeInversePermutes(), |
Mike Kelly | 490b7be | 2020-03-03 12:39:09 +0000 | [diff] [blame] | 1996 | OptimizeInverseTransposes(), |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1997 | MovePermuteUp(), |
Mike Kelly | 490b7be | 2020-03-03 12:39:09 +0000 | [diff] [blame] | 1998 | MoveTransposeUp(), |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1999 | PermuteAsReshape(), |
Mike Kelly | 490b7be | 2020-03-03 12:39:09 +0000 | [diff] [blame] | 2000 | TransposeAsReshape(), |
Nina Drozd | 861985f | 2019-04-18 14:48:51 +0100 | [diff] [blame] | 2001 | OptimizeConsecutiveReshapes(), |
Rob Hughes | 3a7d3a7 | 2019-09-24 16:59:56 +0100 | [diff] [blame] | 2002 | FoldPadIntoConvolution2d(), |
Teresa Charlin | 5786eb7 | 2021-05-21 16:29:45 +0100 | [diff] [blame] | 2003 | FoldPadIntoDepthwiseConvolution2d(), |
Diego Lopez Recas | fe95d72 | 2021-03-19 12:40:16 +0000 | [diff] [blame] | 2004 | FoldPadIntoPooling2d(), |
Idriss Chaouch | 98e383e | 2023-08-28 14:28:31 +0100 | [diff] [blame] | 2005 | BroadcastToOptimizationLayer(), |
Mike Kelly | 490b7be | 2020-03-03 12:39:09 +0000 | [diff] [blame] | 2006 | PermuteAndBatchToSpaceAsDepthToSpace(), |
Teresa Charlin | 06e0300 | 2020-10-15 13:16:07 +0100 | [diff] [blame] | 2007 | TransposeAndBatchToSpaceAsDepthToSpace(), |
Mike Kelly | 90231b8 | 2020-11-05 15:44:56 +0000 | [diff] [blame] | 2008 | FuseBatchNormIntoConvolution2DFloat32(), |
| 2009 | FuseBatchNormIntoConvolution2DFloat16(), |
| 2010 | FuseBatchNormIntoDepthwiseConvolution2DFloat32(), |
Francis Murtagh | d97db7e | 2023-01-16 13:11:29 +0000 | [diff] [blame] | 2011 | FuseBatchNormIntoDepthwiseConvolution2DFloat16())); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 2012 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 2013 | // Initialize backend settings |
| 2014 | BackendSettings backendSettings(backendPreferences, deviceSpec); |
Ryan OShea | b4c4934 | 2023-07-25 14:28:27 +0100 | [diff] [blame] | 2015 | auto availablePreferredBackends = backendSettings.GetAvailablePreferredBackends(); |
| 2016 | if (availablePreferredBackends.empty()) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 2017 | { |
| 2018 | std::stringstream failureMsg; |
| 2019 | failureMsg << "None of the preferred backends " << backendPreferences |
| 2020 | << " are supported. Current platform provides " << backendSettings.m_SupportedBackends; |
Rob Hughes | 2321443 | 2019-11-05 11:27:36 +0000 | [diff] [blame] | 2021 | ReportError(failureMsg.str(), messages); |
Mike Kelly | 3a613cc | 2020-09-29 20:50:35 +0100 | [diff] [blame] | 2022 | throw InvalidArgumentException(failureMsg.str()); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 2025 | // Create a map to temporarily hold initialized backend objects |
| 2026 | TensorHandleFactoryRegistry tensorHandleFactoryRegistry; |
| 2027 | BackendsMap backends = CreateSupportedBackends(tensorHandleFactoryRegistry, backendSettings); |
| 2028 | |
Ryan OShea | b4c4934 | 2023-07-25 14:28:27 +0100 | [diff] [blame] | 2029 | if (options.GetReduceFp32ToFp16()) |
| 2030 | { |
| 2031 | bool hasFp16 = CheckFp16Support(backends, availablePreferredBackends); |
| 2032 | if (hasFp16) |
| 2033 | { |
| 2034 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Optimizer_ReduceFp32ToFp16"); |
| 2035 | Optimizer::Pass(optGraph, MakeOptimizations(Fp32NetworkToFp16Converter())); |
| 2036 | Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsFloatToHalf())); |
| 2037 | } |
| 2038 | } |
| 2039 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 2040 | // Assign an available backend to each layer |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 2041 | Graph::Iterator firstLayer = optGraph.begin(); |
| 2042 | Graph::Iterator lastLayer = optGraph.end(); |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2043 | OptimizationResult assignBackendsResult = AssignBackends(optNetObjPtr->pOptimizedNetworkImpl.get(), |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 2044 | backendSettings, |
| 2045 | firstLayer, |
| 2046 | lastLayer, |
Rob Hughes | 2321443 | 2019-11-05 11:27:36 +0000 | [diff] [blame] | 2047 | messages); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 2048 | if (assignBackendsResult.m_Error) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 2049 | { |
| 2050 | // Failed to assign a backend to each layer |
Mike Kelly | 3a613cc | 2020-09-29 20:50:35 +0100 | [diff] [blame] | 2051 | throw InvalidArgumentException("Failed to assign a backend to each layer"); |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 2052 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2053 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 2054 | Optimizer::Pass(optGraph, MakeOptimizations(OptimizeInverseConversionsFp16(), |
| 2055 | OptimizeInverseConversionsFp32())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2056 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 2057 | // Apply the backend-specific optimizations |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2058 | OptimizationResult backendOptimizationResult = ApplyBackendOptimizations(optNetObjPtr->pOptimizedNetworkImpl.get(), |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 2059 | backendSettings, |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 2060 | backends, |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 2061 | options.GetModelOptions(), |
Rob Hughes | 2321443 | 2019-11-05 11:27:36 +0000 | [diff] [blame] | 2062 | messages); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 2063 | if (backendOptimizationResult.m_Error) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 2064 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 2065 | // Failed to apply the backend-specific optimizations |
Mike Kelly | 3a613cc | 2020-09-29 20:50:35 +0100 | [diff] [blame] | 2066 | throw InvalidArgumentException("Failed to apply the backend-specific optimizations"); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Keith Davis | 71ebf5f | 2022-09-28 17:13:20 +0100 | [diff] [blame] | 2069 | // Convert constants |
| 2070 | { |
| 2071 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Optimizer_ConvertConstants"); |
| 2072 | Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsFloatToHalf())); |
| 2073 | Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsHalfToFloat())); |
Keith Davis | 71ebf5f | 2022-09-28 17:13:20 +0100 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | // This must occur after all topological changes to the graph and any redirection of variables |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 2077 | // If the debug flag is set, then insert a DebugLayer after each layer |
| 2078 | // Doing this after applying the backend optimizations as they might have changed some layers |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 2079 | if (options.GetDebugEnabled() && !options.GetDebugToFileEnabled()) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 2080 | { |
| 2081 | Optimizer::Pass(optGraph, MakeOptimizations(InsertDebugLayer())); |
| 2082 | } |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 2083 | else if (options.GetDebugToFileEnabled()) |
Keith Davis | 15f9c68 | 2022-10-14 15:50:33 +0100 | [diff] [blame] | 2084 | { |
Keith Davis | f63b457 | 2022-10-19 14:53:05 +0100 | [diff] [blame] | 2085 | // Setup the output file path |
Colm Donelan | e27983c | 2023-01-16 16:45:08 +0000 | [diff] [blame] | 2086 | try |
| 2087 | { |
Ryan OShea | a3dc95e | 2023-03-20 11:10:40 +0000 | [diff] [blame] | 2088 | #if !defined(ARMNN_DISABLE_FILESYSTEM) |
Colm Donelan | e27983c | 2023-01-16 16:45:08 +0000 | [diff] [blame] | 2089 | auto result = armnnUtils::Filesystem::CreateDirectory("/ArmNNIntermediateLayerOutputs"); |
| 2090 | ARMNN_LOG(info) << "Intermediate tensors will be written to: " << result; |
Ryan OShea | a3dc95e | 2023-03-20 11:10:40 +0000 | [diff] [blame] | 2091 | #endif |
Colm Donelan | e27983c | 2023-01-16 16:45:08 +0000 | [diff] [blame] | 2092 | Optimizer::Pass(optGraph, MakeOptimizations(InsertDebugToFileLayer())); |
| 2093 | } |
| 2094 | catch (const armnn::RuntimeException& e) |
| 2095 | { |
| 2096 | // If we cannot create the output directory then we'll issue a warning and continue. |
| 2097 | ARMNN_LOG(warning) << "Unable to print intermediate layer outputs : " << e.what(); |
| 2098 | } |
Keith Davis | 15f9c68 | 2022-10-14 15:50:33 +0100 | [diff] [blame] | 2099 | } |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 2100 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 2101 | // Calculate the compatibility strategies for tensor handles |
| 2102 | OptimizationResult strategyResult = SelectTensorHandleStrategy(optGraph, |
| 2103 | backends, |
| 2104 | tensorHandleFactoryRegistry, |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 2105 | options.GetImportEnabled(), |
| 2106 | options.GetExportEnabled(), |
Rob Hughes | 2321443 | 2019-11-05 11:27:36 +0000 | [diff] [blame] | 2107 | messages); |
Francis Murtagh | 626bd90 | 2022-06-21 13:16:23 +0000 | [diff] [blame] | 2108 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 2109 | if (strategyResult.m_Error) |
| 2110 | { |
| 2111 | // Failed to apply the backend-specific optimizations |
| 2112 | return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy); |
| 2113 | } |
| 2114 | |
| 2115 | // Based on the tensor handle strategy determined above, insert copy layers where required. |
Derek Lamberti | f1e0ad3 | 2021-10-13 18:02:25 +0100 | [diff] [blame] | 2116 | { |
| 2117 | ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Optimizer_AddCompatibilityLayers"); |
| 2118 | optGraph.AddCompatibilityLayers(backends, tensorHandleFactoryRegistry); |
| 2119 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2120 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2121 | return optNet; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2122 | } |
Cathal Corbett | a3f4fba | 2022-03-21 09:27:08 +0000 | [diff] [blame] | 2123 | |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 2124 | // Forwarding function to remain backward compatible with legacy OptimizerOptions |
Cathal Corbett | a3f4fba | 2022-03-21 09:27:08 +0000 | [diff] [blame] | 2125 | IOptimizedNetworkPtr Optimize(const INetwork& inNetwork, |
| 2126 | const std::vector<BackendId>& backendPreferences, |
| 2127 | const IDeviceSpec& deviceSpec, |
| 2128 | const OptimizerOptions& options, |
| 2129 | Optional<std::vector<std::string>&> messages) |
| 2130 | { |
John Mcloughlin | c5ee0d7 | 2023-03-24 12:07:25 +0000 | [diff] [blame] | 2131 | return Optimize(inNetwork, |
| 2132 | backendPreferences, |
| 2133 | deviceSpec, |
| 2134 | OptimizerOptionsOpaque(options), |
| 2135 | messages); |
| 2136 | } |
| 2137 | |
| 2138 | IOptimizedNetworkPtr Optimize(const INetwork& inNetwork, |
| 2139 | const std::vector<BackendId>& backendPreferences, |
| 2140 | const IDeviceSpec& deviceSpec, |
| 2141 | const OptimizerOptionsOpaque& options, |
| 2142 | Optional<std::vector<std::string>&> messages) |
| 2143 | { |
Cathal Corbett | a3f4fba | 2022-03-21 09:27:08 +0000 | [diff] [blame] | 2144 | return Optimize(inNetwork.pNetworkImpl->GetGraph(), |
| 2145 | backendPreferences, |
| 2146 | deviceSpec, |
| 2147 | options, |
| 2148 | messages); |
| 2149 | } |
| 2150 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2151 | bool NetworkImpl::GetShapeInferenceMethod() |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2152 | { |
Mike Kelly | 80512b0 | 2022-05-16 23:10:42 +0100 | [diff] [blame] | 2153 | bool shapeInferenceMethod = false; |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 2154 | |
Mike Kelly | 80512b0 | 2022-05-16 23:10:42 +0100 | [diff] [blame] | 2155 | ParseOptions(m_NetworkOptions, "ShapeInferenceMethod", [&](std::string name, const BackendOptions::Var& value) |
| 2156 | { |
| 2157 | if (name == "InferAndValidate") |
| 2158 | { |
| 2159 | shapeInferenceMethod |= value.AsBool(); |
| 2160 | } |
| 2161 | }); |
| 2162 | return shapeInferenceMethod; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2163 | } |
Mike Kelly | 80512b0 | 2022-05-16 23:10:42 +0100 | [diff] [blame] | 2164 | |
| 2165 | bool NetworkImpl::GetAllowExpandedDims() |
| 2166 | { |
| 2167 | bool allowExpandedDims = false; |
| 2168 | |
| 2169 | ParseOptions(m_NetworkOptions, "AllowExpandedDims", [&](std::string name, const BackendOptions::Var& value) |
| 2170 | { |
| 2171 | if (name == "AllowExpandedDims") |
| 2172 | { |
| 2173 | allowExpandedDims |= value.AsBool(); |
| 2174 | } |
| 2175 | }); |
| 2176 | return allowExpandedDims; |
| 2177 | } |
| 2178 | |
Jim Flynn | fcc72f5 | 2022-10-14 11:20:07 +0100 | [diff] [blame] | 2179 | NetworkImpl::NetworkImpl(const NetworkOptions& networkOptions) |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 2180 | : m_NetworkOptions(networkOptions), |
Mike Kelly | 80512b0 | 2022-05-16 23:10:42 +0100 | [diff] [blame] | 2181 | m_Graph(std::make_unique<Graph>(GetShapeInferenceMethod(), GetAllowExpandedDims())) |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 2182 | {} |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2183 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2184 | NetworkImpl::~NetworkImpl() |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2185 | { |
| 2186 | } |
| 2187 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2188 | Status NetworkImpl::PrintGraph() |
Jan Eilers | 99d9d4a | 2019-11-06 10:02:16 +0000 | [diff] [blame] | 2189 | { |
| 2190 | m_Graph->Print(); |
| 2191 | return Status::Success; |
| 2192 | } |
| 2193 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2194 | IConnectableLayer* NetworkImpl::AddInputLayer(LayerBindingId id, const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2195 | { |
| 2196 | return m_Graph->AddLayer<InputLayer>(id, name); |
| 2197 | } |
| 2198 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2199 | IConnectableLayer* NetworkImpl::AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor, |
Éanna Ó Catháin | 4e1e136 | 2018-11-12 11:36:34 +0000 | [diff] [blame] | 2200 | const char* name) |
| 2201 | { |
| 2202 | return m_Graph->AddLayer<BatchToSpaceNdLayer>(batchToSpaceNdDescriptor, name); |
| 2203 | } |
| 2204 | |
mathad01 | b392e98 | 2021-04-07 12:07:30 +0100 | [diff] [blame] | 2205 | IConnectableLayer* NetworkImpl::AddCastLayer(const char* name) |
| 2206 | { |
| 2207 | return m_Graph->AddLayer<CastLayer>(name); |
| 2208 | } |
Simon Obute | 51f6777 | 2021-09-03 15:50:13 +0100 | [diff] [blame] | 2209 | IConnectableLayer* NetworkImpl::AddChannelShuffleLayer(const ChannelShuffleDescriptor& channelShuffleDescriptor, |
| 2210 | const char* name) |
| 2211 | { |
| 2212 | return m_Graph->AddLayer<ChannelShuffleLayer>(channelShuffleDescriptor, name); |
| 2213 | } |
mathad01 | b392e98 | 2021-04-07 12:07:30 +0100 | [diff] [blame] | 2214 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2215 | IConnectableLayer* NetworkImpl::AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor, |
Aron Virginas-Tar | 77bfb5e | 2019-10-16 17:45:38 +0100 | [diff] [blame] | 2216 | const char* name) |
| 2217 | { |
| 2218 | return m_Graph->AddLayer<ComparisonLayer>(comparisonDescriptor, name); |
| 2219 | } |
| 2220 | |
Mike Kelly | 3ec3077 | 2023-03-08 13:47:17 +0000 | [diff] [blame] | 2221 | IConnectableLayer* NetworkImpl::AddElementwiseBinaryLayer(const ElementwiseBinaryDescriptor& elementwiseBinaryDesc, |
| 2222 | const char* name) |
| 2223 | { |
| 2224 | return m_Graph->AddLayer<ElementwiseBinaryLayer>(elementwiseBinaryDesc, name); |
| 2225 | } |
| 2226 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2227 | IConnectableLayer* NetworkImpl::AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor, |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 2228 | const char* name) |
| 2229 | { |
| 2230 | return m_Graph->AddLayer<ElementwiseUnaryLayer>(elementwiseUnaryDescriptor, name); |
| 2231 | } |
| 2232 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2233 | IConnectableLayer* NetworkImpl::AddFillLayer(const FillDescriptor& fillDescriptor, |
Ryan OShea | ec6c680 | 2020-06-05 17:17:06 +0100 | [diff] [blame] | 2234 | const char* name) |
| 2235 | { |
| 2236 | return m_Graph->AddLayer<FillLayer>(fillDescriptor, name); |
| 2237 | } |
| 2238 | |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 2239 | IConnectableLayer* NetworkImpl::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
| 2240 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2241 | { |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 2242 | return m_Graph->AddLayer<FullyConnectedLayer>(fullyConnectedDescriptor, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
Teresa Charlin | 9145e38 | 2023-08-17 18:44:58 +0100 | [diff] [blame] | 2245 | IConnectableLayer* NetworkImpl::AddFusedLayer(const FusedDescriptor& fusedDescriptor, |
| 2246 | const char* name) |
| 2247 | { |
| 2248 | return m_Graph->AddLayer<FusedLayer>(fusedDescriptor, name); |
| 2249 | } |
| 2250 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2251 | IConnectableLayer* NetworkImpl::AddConcatLayer(const ConcatDescriptor& concatDescriptor, |
Jim Flynn | 906f946 | 2019-05-10 13:55:21 +0100 | [diff] [blame] | 2252 | const char* name) |
| 2253 | { |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 2254 | return m_Graph->AddLayer<ConcatLayer>(concatDescriptor, name); |
Jim Flynn | 906f946 | 2019-05-10 13:55:21 +0100 | [diff] [blame] | 2255 | } |
| 2256 | |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 2257 | IConnectableLayer* NetworkImpl::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, |
| 2258 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2259 | { |
Keith Davis | b4dd5cc | 2022-04-07 11:32:00 +0100 | [diff] [blame] | 2260 | return m_Graph->AddLayer<Convolution2dLayer>(convolution2dDescriptor, name); |
| 2261 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2262 | |
Cathal Corbett | a3f4fba | 2022-03-21 09:27:08 +0000 | [diff] [blame] | 2263 | IConnectableLayer* NetworkImpl::AddConvertFp16ToFp32Layer(const char* name) |
| 2264 | { |
| 2265 | return m_Graph->AddLayer<ConvertFp16ToFp32Layer>(name); |
| 2266 | } |
| 2267 | |
| 2268 | IConnectableLayer* NetworkImpl::AddConvertFp32ToFp16Layer(const char* name) |
| 2269 | { |
| 2270 | return m_Graph->AddLayer<ConvertFp32ToFp16Layer>(name); |
| 2271 | } |
| 2272 | |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 2273 | IConnectableLayer* NetworkImpl::AddConvolution3dLayer(const Convolution3dDescriptor& convolution3dDescriptor, |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 2274 | const char* name) |
| 2275 | { |
Matthew Sloyan | 5d7b0a3 | 2021-10-18 13:07:49 +0100 | [diff] [blame] | 2276 | return m_Graph->AddLayer<Convolution3dLayer>(convolution3dDescriptor, name); |
Matthew Sloyan | b63a311 | 2021-09-08 13:05:51 +0100 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | IConnectableLayer* NetworkImpl::AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor, |
| 2280 | const char* name) |
| 2281 | { |
| 2282 | return m_Graph->AddLayer<DepthToSpaceLayer>(depthToSpaceDescriptor, name); |
| 2283 | } |
| 2284 | |
Cathal Corbett | 0690265 | 2022-04-14 17:55:11 +0100 | [diff] [blame] | 2285 | IConnectableLayer* NetworkImpl::AddDepthwiseConvolution2dLayer( |
| 2286 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 2287 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2288 | { |
Cathal Corbett | 0690265 | 2022-04-14 17:55:11 +0100 | [diff] [blame] | 2289 | return m_Graph->AddLayer<DepthwiseConvolution2dLayer>(convolution2dDescriptor, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2292 | IConnectableLayer* NetworkImpl::AddDetectionPostProcessLayer(const armnn::DetectionPostProcessDescriptor& descriptor, |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 2293 | const ConstTensor& anchors, const char* name) |
Narumol Prangnawarat | 94dd5d8 | 2019-01-23 18:06:26 +0000 | [diff] [blame] | 2294 | { |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 2295 | const auto layer = m_Graph->AddLayer<DetectionPostProcessLayer>(descriptor, name); |
| 2296 | |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2297 | layer->m_Anchors = std::make_shared<ScopedTensorHandle>(anchors); |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 2298 | |
| 2299 | return layer; |
Narumol Prangnawarat | 94dd5d8 | 2019-01-23 18:06:26 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2302 | IConnectableLayer* NetworkImpl::AddPermuteLayer(const PermuteDescriptor& permuteDescriptor, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2303 | const char* name) |
| 2304 | { |
| 2305 | return m_Graph->AddLayer<PermuteLayer>(permuteDescriptor, name); |
| 2306 | } |
| 2307 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2308 | IConnectableLayer* NetworkImpl::AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2309 | const char* name) |
| 2310 | { |
| 2311 | return m_Graph->AddLayer<Pooling2dLayer>(pooling2dDescriptor, name); |
| 2312 | } |
| 2313 | |
Tamás Nyíri | 7b885b3 | 2021-10-26 14:47:57 +0100 | [diff] [blame] | 2314 | IConnectableLayer* NetworkImpl::AddPooling3dLayer(const Pooling3dDescriptor& pooling3dDescriptor, |
| 2315 | const char* name) |
| 2316 | { |
| 2317 | return m_Graph->AddLayer<Pooling3dLayer>(pooling3dDescriptor, name); |
| 2318 | } |
| 2319 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2320 | IConnectableLayer* NetworkImpl::AddActivationLayer(const ActivationDescriptor& activationDescriptor, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2321 | const char* name) |
| 2322 | { |
| 2323 | return m_Graph->AddLayer<ActivationLayer>(activationDescriptor, name); |
| 2324 | } |
| 2325 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2326 | IConnectableLayer* NetworkImpl::AddArgMinMaxLayer(const ArgMinMaxDescriptor& argMinMaxDescriptor, |
Nikhil Raj | ee391d5 | 2019-09-05 17:50:44 +0100 | [diff] [blame] | 2327 | const char* name) |
| 2328 | { |
| 2329 | return m_Graph->AddLayer<ArgMinMaxLayer>(argMinMaxDescriptor, name); |
| 2330 | } |
| 2331 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2332 | IConnectableLayer* NetworkImpl::AddNormalizationLayer(const NormalizationDescriptor& |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2333 | normalizationDescriptor, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2334 | const char* name) |
| 2335 | { |
| 2336 | return m_Graph->AddLayer<NormalizationLayer>(normalizationDescriptor, name); |
| 2337 | } |
| 2338 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2339 | IConnectableLayer* NetworkImpl::AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name) |
Aron Virginas-Tar | 636ab40 | 2019-09-16 14:27:45 +0100 | [diff] [blame] | 2340 | { |
| 2341 | return m_Graph->AddLayer<SliceLayer>(sliceDescriptor, name); |
| 2342 | } |
| 2343 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2344 | IConnectableLayer* NetworkImpl::AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2345 | const char* name) |
| 2346 | { |
| 2347 | return m_Graph->AddLayer<SoftmaxLayer>(softmaxDescriptor, name); |
| 2348 | } |
| 2349 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2350 | IConnectableLayer* NetworkImpl::AddSplitterLayer(const ViewsDescriptor& splitterDescriptor, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2351 | const char* name) |
| 2352 | { |
| 2353 | return m_Graph->AddLayer<SplitterLayer>(splitterDescriptor, name); |
| 2354 | } |
| 2355 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2356 | IConnectableLayer* NetworkImpl::AddMaximumLayer(const char* name) |
Nattapat Chaimanowong | 5a4304a | 2018-11-28 10:44:37 +0000 | [diff] [blame] | 2357 | { |
| 2358 | return m_Graph->AddLayer<MaximumLayer>(name); |
| 2359 | } |
| 2360 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2361 | IConnectableLayer* NetworkImpl::AddMinimumLayer(const char* name) |
Éanna Ó Catháin | 20e5880 | 2018-12-04 10:29:06 +0000 | [diff] [blame] | 2362 | { |
| 2363 | return m_Graph->AddLayer<MinimumLayer>(name); |
| 2364 | } |
| 2365 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2366 | IConnectableLayer* NetworkImpl::AddAdditionLayer(const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2367 | { |
| 2368 | return m_Graph->AddLayer<AdditionLayer>(name); |
| 2369 | } |
| 2370 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2371 | IConnectableLayer* NetworkImpl::AddMultiplicationLayer(const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2372 | { |
| 2373 | return m_Graph->AddLayer<MultiplicationLayer>(name); |
| 2374 | } |
| 2375 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2376 | IConnectableLayer* NetworkImpl::AddOutputLayer(LayerBindingId id, const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2377 | { |
| 2378 | return m_Graph->AddLayer<OutputLayer>(id, name); |
| 2379 | } |
| 2380 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2381 | IConnectableLayer* NetworkImpl::AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2382 | const ConstTensor& mean, |
| 2383 | const ConstTensor& variance, |
| 2384 | const ConstTensor& beta, |
| 2385 | const ConstTensor& gamma, |
| 2386 | const char* name) |
| 2387 | { |
| 2388 | const auto layer = m_Graph->AddLayer<BatchNormalizationLayer>(desc, name); |
| 2389 | |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2390 | layer->m_Mean = std::make_shared<ScopedTensorHandle>(mean); |
| 2391 | layer->m_Variance = std::make_shared<ScopedTensorHandle>(variance); |
| 2392 | layer->m_Beta = std::make_shared<ScopedTensorHandle>(beta); |
| 2393 | layer->m_Gamma = std::make_shared<ScopedTensorHandle>(gamma); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2394 | |
| 2395 | return layer; |
| 2396 | } |
| 2397 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2398 | IConnectableLayer* NetworkImpl::AddRankLayer(const char* name) |
Finn Williams | 2605b23 | 2020-06-10 15:53:46 +0100 | [diff] [blame] | 2399 | { |
| 2400 | return m_Graph->AddLayer<RankLayer>(name); |
| 2401 | } |
| 2402 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2403 | IConnectableLayer* NetworkImpl::AddReduceLayer(const ReduceDescriptor& reduceDescriptor, |
| 2404 | const char* name) |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 2405 | { |
| 2406 | return m_Graph->AddLayer<ReduceLayer>(reduceDescriptor, name); |
| 2407 | } |
| 2408 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2409 | IConnectableLayer* NetworkImpl::AddResizeLayer(const ResizeDescriptor& resizeDescriptor, const char* name) |
Teresa Charlin | a9075df | 2019-06-27 15:41:57 +0100 | [diff] [blame] | 2410 | { |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 2411 | return m_Graph->AddLayer<ResizeLayer>(resizeDescriptor, name); |
Teresa Charlin | a9075df | 2019-06-27 15:41:57 +0100 | [diff] [blame] | 2412 | } |
| 2413 | |
Keith Davis | 3ae3f97 | 2021-05-21 16:33:48 +0100 | [diff] [blame] | 2414 | IConnectableLayer* NetworkImpl::AddShapeLayer(const char* name) |
| 2415 | { |
| 2416 | return m_Graph->AddLayer<ShapeLayer>(name); |
| 2417 | } |
| 2418 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2419 | IConnectableLayer* NetworkImpl::AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc, |
| 2420 | const char* name) |
Kevin May | ce5045a | 2019-10-02 14:07:47 +0100 | [diff] [blame] | 2421 | { |
| 2422 | return m_Graph->AddLayer<InstanceNormalizationLayer>(desc, name); |
| 2423 | } |
| 2424 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2425 | IConnectableLayer* NetworkImpl::AddL2NormalizationLayer(const L2NormalizationDescriptor& desc, |
| 2426 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2427 | { |
Matteo Martincigh | bcd3c85 | 2018-09-28 14:14:12 +0100 | [diff] [blame] | 2428 | return m_Graph->AddLayer<L2NormalizationLayer>(desc, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2431 | IConnectableLayer* NetworkImpl::AddLogSoftmaxLayer(const LogSoftmaxDescriptor& desc, |
Aron Virginas-Tar | f982dea | 2019-10-11 14:07:53 +0100 | [diff] [blame] | 2432 | const char* name) |
| 2433 | { |
| 2434 | return m_Graph->AddLayer<LogSoftmaxLayer>(desc, name); |
| 2435 | } |
| 2436 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2437 | IConnectableLayer* NetworkImpl::AddConstantLayer(const ConstTensor& input, const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2438 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2439 | auto layer = m_Graph->AddLayer<ConstantLayer>(name); |
| 2440 | |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2441 | layer->m_LayerOutput = std::make_shared<ScopedTensorHandle>(input); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2442 | |
| 2443 | return layer; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2446 | IConnectableLayer* NetworkImpl::AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2447 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2448 | { |
| 2449 | return m_Graph->AddLayer<ReshapeLayer>(reshapeDescriptor, name); |
| 2450 | } |
| 2451 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2452 | IConnectableLayer* NetworkImpl::AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor, |
Nattapat Chaimanowong | 207ef9a | 2018-11-02 10:57:25 +0000 | [diff] [blame] | 2453 | const char* name) |
| 2454 | { |
| 2455 | return m_Graph->AddLayer<SpaceToBatchNdLayer>(spaceToBatchNdDescriptor, name); |
| 2456 | } |
| 2457 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2458 | IConnectableLayer* NetworkImpl::AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor, |
Aron Virginas-Tar | 972af15 | 2019-06-11 14:14:03 +0100 | [diff] [blame] | 2459 | const char* name) |
| 2460 | { |
| 2461 | return m_Graph->AddLayer<SpaceToDepthLayer>(spaceToDepthDescriptor, name); |
| 2462 | } |
| 2463 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2464 | IConnectableLayer* NetworkImpl::AddFloorLayer(const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2465 | { |
| 2466 | return m_Graph->AddLayer<FloorLayer>(name); |
| 2467 | } |
| 2468 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2469 | IConnectableLayer* NetworkImpl::AddLstmLayer(const LstmDescriptor& descriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2470 | const LstmInputParams& params, |
| 2471 | const char* name) |
| 2472 | { |
| 2473 | const auto layer = m_Graph->AddLayer<LstmLayer>(descriptor, name); |
| 2474 | |
| 2475 | //Lstm Basic Parameters |
| 2476 | layer->m_BasicParameters.m_InputToForgetWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2477 | std::make_shared<ScopedTensorHandle>(*(params.m_InputToForgetWeights)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2478 | layer->m_BasicParameters.m_InputToCellWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2479 | std::make_shared<ScopedTensorHandle>(*(params.m_InputToCellWeights)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2480 | layer->m_BasicParameters.m_InputToOutputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2481 | std::make_shared<ScopedTensorHandle>(*(params.m_InputToOutputWeights)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2482 | layer->m_BasicParameters.m_RecurrentToForgetWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2483 | std::make_shared<ScopedTensorHandle>(*(params.m_RecurrentToForgetWeights)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2484 | layer->m_BasicParameters.m_RecurrentToCellWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2485 | std::make_shared<ScopedTensorHandle>(*(params.m_RecurrentToCellWeights)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2486 | layer->m_BasicParameters.m_RecurrentToOutputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2487 | std::make_shared<ScopedTensorHandle>(*(params.m_RecurrentToOutputWeights)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2488 | layer->m_BasicParameters.m_ForgetGateBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2489 | std::make_shared<ScopedTensorHandle>(*(params.m_ForgetGateBias)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2490 | layer->m_BasicParameters.m_CellBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2491 | std::make_shared<ScopedTensorHandle>(*(params.m_CellBias)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2492 | layer->m_BasicParameters.m_OutputGateBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2493 | std::make_shared<ScopedTensorHandle>(*(params.m_OutputGateBias)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2494 | |
| 2495 | //Lstm Cifg parameters |
| 2496 | if(!descriptor.m_CifgEnabled) |
| 2497 | { |
| 2498 | if(params.m_InputToInputWeights == nullptr) |
| 2499 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 2500 | throw InvalidArgumentException("AddLstmLayer: Input To Input Weights cannot be NULL " |
| 2501 | "when CIFG is disabled."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2502 | } |
| 2503 | if(params.m_RecurrentToInputWeights == nullptr) |
| 2504 | { |
| 2505 | throw InvalidArgumentException( |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 2506 | "AddLstmLayer: Recurrent To Input Weights cannot be NULL " |
| 2507 | "when CIFG is disabled."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2508 | } |
| 2509 | if(params.m_InputGateBias == nullptr) |
| 2510 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 2511 | throw InvalidArgumentException("AddLstmLayer: Input Gate Bias cannot be NULL " |
| 2512 | "when CIFG is disabled."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2513 | } |
| 2514 | layer->m_CifgParameters.m_InputToInputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2515 | std::make_shared<ScopedTensorHandle>(*(params.m_InputToInputWeights)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2516 | layer->m_CifgParameters.m_RecurrentToInputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2517 | std::make_shared<ScopedTensorHandle>(*(params.m_RecurrentToInputWeights)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2518 | layer->m_CifgParameters.m_InputGateBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2519 | std::make_shared<ScopedTensorHandle>(*(params.m_InputGateBias)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | //Lstm projection parameters |
| 2523 | if(descriptor.m_ProjectionEnabled) |
| 2524 | { |
| 2525 | if(params.m_ProjectionWeights == nullptr) |
| 2526 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 2527 | throw InvalidArgumentException("AddLstmLayer: Projection Weights cannot be NULL " |
| 2528 | "when projection is enabled."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2529 | } |
| 2530 | layer->m_ProjectionParameters.m_ProjectionWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2531 | std::make_shared<ScopedTensorHandle>(*(params.m_ProjectionWeights)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2532 | if(params.m_ProjectionBias != nullptr) |
| 2533 | { |
| 2534 | layer->m_ProjectionParameters.m_ProjectionBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2535 | std::make_shared<ScopedTensorHandle>(*(params.m_ProjectionBias)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | //Lstm Peephole params |
| 2540 | if(descriptor.m_PeepholeEnabled) |
| 2541 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 2542 | if(!descriptor.m_CifgEnabled) |
| 2543 | { |
| 2544 | if(params.m_CellToInputWeights == nullptr) |
| 2545 | { |
| 2546 | throw InvalidArgumentException("AddLstmLayer: Cell To Input Weights cannot be NULL " |
| 2547 | "when Peephole is enabled and CIFG disabled."); |
| 2548 | } |
| 2549 | |
| 2550 | layer->m_PeepholeParameters.m_CellToInputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2551 | std::make_shared<ScopedTensorHandle>(*(params.m_CellToInputWeights)); |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 2552 | } |
| 2553 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2554 | if(params.m_CellToForgetWeights == nullptr) |
| 2555 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 2556 | throw InvalidArgumentException("AddLstmLayer: Cell To Forget Weights cannot be NULL " |
| 2557 | "when Peephole is enabled."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2558 | } |
| 2559 | if(params.m_CellToOutputWeights == nullptr) |
| 2560 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 2561 | throw InvalidArgumentException("AddLstmLayer: Cell To Output Weights cannot be NULL " |
| 2562 | "when Peephole is enabled."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2563 | } |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 2564 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2565 | layer->m_PeepholeParameters.m_CellToForgetWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2566 | std::make_shared<ScopedTensorHandle>(*(params.m_CellToForgetWeights)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2567 | layer->m_PeepholeParameters.m_CellToOutputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2568 | std::make_shared<ScopedTensorHandle>(*(params.m_CellToOutputWeights)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2569 | } |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 2570 | |
| 2571 | //Lstm Layer Normalization params |
| 2572 | if(descriptor.m_LayerNormEnabled) |
| 2573 | { |
| 2574 | if(!descriptor.m_CifgEnabled) |
| 2575 | { |
| 2576 | if(params.m_InputLayerNormWeights == nullptr) |
| 2577 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 2578 | throw InvalidArgumentException("AddLstmLayer: Input layer normalization weights cannot be NULL " |
| 2579 | "when layer normalization is enabled and CIFG disabled."); |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 2580 | } |
| 2581 | layer->m_LayerNormParameters.m_InputLayerNormWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2582 | std::make_shared<ScopedTensorHandle>(*(params.m_InputLayerNormWeights)); |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | if(params.m_ForgetLayerNormWeights == nullptr) |
| 2586 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 2587 | throw InvalidArgumentException("AddLstmLayer: Forget layer normalization weights cannot be NULL " |
| 2588 | "when layer normalization is enabled."); |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 2589 | } |
| 2590 | if(params.m_CellLayerNormWeights == nullptr) |
| 2591 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 2592 | throw InvalidArgumentException("AddLstmLayer: Cell layer normalization weights cannot be NULL " |
| 2593 | "when layer normalization is enabled."); |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 2594 | } |
| 2595 | if(params.m_OutputLayerNormWeights == nullptr) |
| 2596 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 2597 | throw InvalidArgumentException("AddLstmLayer: Output layer normalization weights cannot be NULL " |
| 2598 | "when layer normalization is enabled."); |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 2599 | } |
| 2600 | layer->m_LayerNormParameters.m_ForgetLayerNormWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2601 | std::make_shared<ScopedTensorHandle>(*(params.m_ForgetLayerNormWeights)); |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 2602 | layer->m_LayerNormParameters.m_CellLayerNormWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2603 | std::make_shared<ScopedTensorHandle>(*(params.m_CellLayerNormWeights)); |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 2604 | layer->m_LayerNormParameters.m_OutputLayerNormWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2605 | std::make_shared<ScopedTensorHandle>(*(params.m_OutputLayerNormWeights)); |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 2606 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2607 | return layer; |
| 2608 | } |
| 2609 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2610 | IConnectableLayer* NetworkImpl::AddDivisionLayer(const char* name) |
Francis Murtagh | e7a86a4 | 2018-08-29 12:42:10 +0100 | [diff] [blame] | 2611 | { |
| 2612 | return m_Graph->AddLayer<DivisionLayer>(name); |
| 2613 | } |
| 2614 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2615 | IConnectableLayer* NetworkImpl::AddSubtractionLayer(const char* name) |
David Beck | 1952622 | 2018-09-12 16:00:08 +0100 | [diff] [blame] | 2616 | { |
| 2617 | return m_Graph->AddLayer<SubtractionLayer>(name); |
| 2618 | } |
| 2619 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2620 | IConnectableLayer* NetworkImpl::AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name) |
narpra01 | 32b9046 | 2018-09-13 11:07:48 +0100 | [diff] [blame] | 2621 | { |
| 2622 | return m_Graph->AddLayer<MeanLayer>(meanDescriptor,name); |
| 2623 | } |
| 2624 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2625 | IConnectableLayer* NetworkImpl::AddPadLayer(const PadDescriptor& padDescriptor, const char* name) |
Mohamed Nour Abouelseoud | 5662c20 | 2018-09-24 13:30:09 +0100 | [diff] [blame] | 2626 | { |
| 2627 | return m_Graph->AddLayer<PadLayer>(padDescriptor,name); |
| 2628 | } |
| 2629 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2630 | IConnectableLayer *NetworkImpl::AddQuantizeLayer(const char *name) |
Derek Lamberti | a9cca6a | 2019-03-25 15:41:58 +0000 | [diff] [blame] | 2631 | { |
| 2632 | return m_Graph->AddLayer<QuantizeLayer>(name); |
| 2633 | } |
| 2634 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2635 | IConnectableLayer* NetworkImpl::AddDequantizeLayer(const char* name) |
Nattapat Chaimanowong | e4294fd | 2019-03-28 09:56:53 +0000 | [diff] [blame] | 2636 | { |
| 2637 | return m_Graph->AddLayer<DequantizeLayer>(name); |
| 2638 | } |
| 2639 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2640 | IConnectableLayer* NetworkImpl::AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor, |
Teresa Charlin | b2d3ec5 | 2022-04-12 22:07:09 +0100 | [diff] [blame] | 2641 | const char* name) |
Conor Kennedy | 430b5d8 | 2018-11-14 15:28:28 +0000 | [diff] [blame] | 2642 | { |
| 2643 | return m_Graph->AddLayer<StridedSliceLayer>(stridedSliceDescriptor, name); |
| 2644 | } |
| 2645 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2646 | IConnectableLayer* NetworkImpl::AddGatherLayer(const GatherDescriptor& gatherDescriptor, |
Teresa Charlin | b2d3ec5 | 2022-04-12 22:07:09 +0100 | [diff] [blame] | 2647 | const char* name) |
Teresa Charlin | 5266473 | 2020-06-29 16:27:03 +0100 | [diff] [blame] | 2648 | { |
| 2649 | return m_Graph->AddLayer<GatherLayer>(gatherDescriptor, name); |
narpra01 | b89b05f | 2019-01-16 09:53:09 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Teresa Charlin | b2d3ec5 | 2022-04-12 22:07:09 +0100 | [diff] [blame] | 2652 | IConnectableLayer* NetworkImpl::AddGatherNdLayer(const char* name) |
| 2653 | { |
| 2654 | return m_Graph->AddLayer<GatherNdLayer>(name); |
| 2655 | } |
| 2656 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2657 | IConnectableLayer* NetworkImpl::AddMergeLayer(const char* name) |
Nattapat Chaimanowong | 1f88630 | 2019-04-05 13:37:19 +0100 | [diff] [blame] | 2658 | { |
| 2659 | return m_Graph->AddLayer<MergeLayer>(name); |
| 2660 | } |
| 2661 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2662 | IConnectableLayer* NetworkImpl::AddSwitchLayer(const char* name) |
Sadik Armagan | eff363d | 2019-04-05 15:25:46 +0100 | [diff] [blame] | 2663 | { |
| 2664 | return m_Graph->AddLayer<SwitchLayer>(name); |
| 2665 | } |
| 2666 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2667 | IConnectableLayer* NetworkImpl::AddPreluLayer(const char* name) |
Matteo Martincigh | 0e406ee | 2019-06-12 15:42:18 +0100 | [diff] [blame] | 2668 | { |
| 2669 | return m_Graph->AddLayer<PreluLayer>(name); |
| 2670 | } |
| 2671 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2672 | IConnectableLayer* NetworkImpl::AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor, |
Aron Virginas-Tar | 639fb04 | 2019-06-20 14:28:19 +0100 | [diff] [blame] | 2673 | const ConstTensor& weights, |
| 2674 | const Optional<ConstTensor>& biases, |
| 2675 | const char* name) |
| 2676 | { |
| 2677 | if (descriptor.m_BiasEnabled && !biases.has_value()) |
| 2678 | { |
| 2679 | throw InvalidArgumentException("AddTransposeConvolution2dLayer: Biases cannot be empty"); |
| 2680 | } |
| 2681 | |
| 2682 | const auto layer = m_Graph->AddLayer<TransposeConvolution2dLayer>(descriptor, name); |
| 2683 | |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2684 | layer->m_Weight = std::make_shared<ScopedTensorHandle>(weights); |
Aron Virginas-Tar | 639fb04 | 2019-06-20 14:28:19 +0100 | [diff] [blame] | 2685 | |
| 2686 | if (descriptor.m_BiasEnabled) |
| 2687 | { |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2688 | layer->m_Bias = std::make_shared<ScopedTensorHandle>(biases.value()); |
Aron Virginas-Tar | 639fb04 | 2019-06-20 14:28:19 +0100 | [diff] [blame] | 2689 | } |
| 2690 | |
| 2691 | return layer; |
| 2692 | } |
| 2693 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2694 | IConnectableLayer* NetworkImpl::AddTransposeLayer(const TransposeDescriptor& transposeDescriptor, |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 2695 | const char* name) |
| 2696 | { |
| 2697 | return m_Graph->AddLayer<TransposeLayer>(transposeDescriptor, name); |
| 2698 | } |
| 2699 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2700 | IConnectableLayer* NetworkImpl::AddStackLayer(const StackDescriptor& stackDescriptor, |
Matthew Jackson | 2b8c1da | 2019-07-04 14:59:16 +0100 | [diff] [blame] | 2701 | const char* name) |
| 2702 | { |
| 2703 | return m_Graph->AddLayer<StackLayer>(stackDescriptor, name); |
| 2704 | } |
| 2705 | |
Derek Lamberti | 013c390 | 2019-10-21 10:46:16 +0100 | [diff] [blame] | 2706 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2707 | IConnectableLayer* NetworkImpl::AddStandInLayer(const StandInDescriptor& desc, |
Derek Lamberti | 013c390 | 2019-10-21 10:46:16 +0100 | [diff] [blame] | 2708 | const char* name) |
| 2709 | { |
| 2710 | return m_Graph->AddLayer<StandInLayer>(desc, name); |
| 2711 | } |
| 2712 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2713 | IConnectableLayer* NetworkImpl::AddQuantizedLstmLayer(const QuantizedLstmInputParams& params, |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 2714 | const char* name) |
| 2715 | { |
| 2716 | const auto layer = m_Graph->AddLayer<QuantizedLstmLayer>(name); |
| 2717 | |
| 2718 | // InputToX weights |
| 2719 | layer->m_QuantizedLstmParameters.m_InputToInputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2720 | std::make_shared<ScopedTensorHandle>(params.GetInputToInputWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 2721 | layer->m_QuantizedLstmParameters.m_InputToForgetWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2722 | std::make_shared<ScopedTensorHandle>(params.GetInputToForgetWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 2723 | layer->m_QuantizedLstmParameters.m_InputToCellWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2724 | std::make_shared<ScopedTensorHandle>(params.GetInputToCellWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 2725 | layer->m_QuantizedLstmParameters.m_InputToOutputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2726 | std::make_shared<ScopedTensorHandle>(params.GetInputToOutputWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 2727 | |
| 2728 | // RecurrentToX weights |
| 2729 | layer->m_QuantizedLstmParameters.m_RecurrentToInputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2730 | std::make_shared<ScopedTensorHandle>(params.GetRecurrentToInputWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 2731 | layer->m_QuantizedLstmParameters.m_RecurrentToForgetWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2732 | std::make_shared<ScopedTensorHandle>(params.GetRecurrentToForgetWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 2733 | layer->m_QuantizedLstmParameters.m_RecurrentToCellWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2734 | std::make_shared<ScopedTensorHandle>(params.GetRecurrentToCellWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 2735 | layer->m_QuantizedLstmParameters.m_RecurrentToOutputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2736 | std::make_shared<ScopedTensorHandle>(params.GetRecurrentToOutputWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 2737 | |
| 2738 | // Bias |
| 2739 | layer->m_QuantizedLstmParameters.m_InputGateBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2740 | std::make_shared<ScopedTensorHandle>(params.GetInputGateBias()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 2741 | layer->m_QuantizedLstmParameters.m_ForgetGateBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2742 | std::make_shared<ScopedTensorHandle>(params.GetForgetGateBias()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 2743 | layer->m_QuantizedLstmParameters.m_CellBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2744 | std::make_shared<ScopedTensorHandle>(params.GetCellBias()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 2745 | layer->m_QuantizedLstmParameters.m_OutputGateBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2746 | std::make_shared<ScopedTensorHandle>(params.GetOutputGateBias()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 2747 | |
| 2748 | return layer; |
| 2749 | } |
| 2750 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2751 | IConnectableLayer* NetworkImpl::AddQLstmLayer(const QLstmDescriptor& descriptor, |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2752 | const LstmInputParams& params, |
| 2753 | const char* name) |
| 2754 | { |
| 2755 | const auto layer = m_Graph->AddLayer<QLstmLayer>(descriptor, name); |
| 2756 | |
| 2757 | // QLstm Basic Parameters |
| 2758 | layer->m_BasicParameters.m_InputToForgetWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2759 | std::make_shared<ScopedTensorHandle>(*(params.m_InputToForgetWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2760 | layer->m_BasicParameters.m_InputToCellWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2761 | std::make_shared<ScopedTensorHandle>(*(params.m_InputToCellWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2762 | layer->m_BasicParameters.m_InputToOutputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2763 | std::make_shared<ScopedTensorHandle>(*(params.m_InputToOutputWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2764 | layer->m_BasicParameters.m_RecurrentToForgetWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2765 | std::make_shared<ScopedTensorHandle>(*(params.m_RecurrentToForgetWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2766 | layer->m_BasicParameters.m_RecurrentToCellWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2767 | std::make_shared<ScopedTensorHandle>(*(params.m_RecurrentToCellWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2768 | layer->m_BasicParameters.m_RecurrentToOutputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2769 | std::make_shared<ScopedTensorHandle>(*(params.m_RecurrentToOutputWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2770 | layer->m_BasicParameters.m_ForgetGateBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2771 | std::make_shared<ScopedTensorHandle>(*(params.m_ForgetGateBias)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2772 | layer->m_BasicParameters.m_CellBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2773 | std::make_shared<ScopedTensorHandle>(*(params.m_CellBias)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2774 | layer->m_BasicParameters.m_OutputGateBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2775 | std::make_shared<ScopedTensorHandle>(*(params.m_OutputGateBias)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2776 | |
| 2777 | // QLstm Cifg parameters |
| 2778 | if(!descriptor.m_CifgEnabled) |
| 2779 | { |
| 2780 | if(params.m_InputToInputWeights == nullptr) |
| 2781 | { |
| 2782 | throw InvalidArgumentException("AddQLstmLayer: Input To Input Weights cannot be NULL"); |
| 2783 | } |
| 2784 | |
| 2785 | if(params.m_RecurrentToInputWeights == nullptr) |
| 2786 | { |
| 2787 | throw InvalidArgumentException( |
| 2788 | "AddQLstmLayer: Recurrent To Input Weights cannot be NULL"); |
| 2789 | } |
| 2790 | |
| 2791 | if(params.m_InputGateBias == nullptr) |
| 2792 | { |
| 2793 | throw InvalidArgumentException("AddQLstmLayer: Input Gate Bias cannot be NULL"); |
| 2794 | } |
| 2795 | |
| 2796 | layer->m_CifgParameters.m_InputToInputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2797 | std::make_shared<ScopedTensorHandle>(*(params.m_InputToInputWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2798 | layer->m_CifgParameters.m_RecurrentToInputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2799 | std::make_shared<ScopedTensorHandle>(*(params.m_RecurrentToInputWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2800 | layer->m_CifgParameters.m_InputGateBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2801 | std::make_shared<ScopedTensorHandle>(*(params.m_InputGateBias)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | // QLstm Projection parameters |
| 2805 | if(descriptor.m_ProjectionEnabled) |
| 2806 | { |
| 2807 | if(params.m_ProjectionWeights == nullptr) |
| 2808 | { |
| 2809 | throw InvalidArgumentException("AddQLstmLayer: Projection Weights cannot be NULL"); |
| 2810 | } |
| 2811 | |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2812 | layer->m_ProjectionParameters.m_ProjectionWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2813 | std::make_shared<ScopedTensorHandle>(*(params.m_ProjectionWeights)); |
James Conroy | ed32405 | 2020-05-18 15:16:42 +0100 | [diff] [blame] | 2814 | |
| 2815 | // Projection bias is optional even if projection is enabled |
Cathal Corbett | 727c2b5 | 2022-05-06 12:11:37 +0100 | [diff] [blame] | 2816 | if(params.m_ProjectionBias != nullptr) |
James Conroy | ed32405 | 2020-05-18 15:16:42 +0100 | [diff] [blame] | 2817 | { |
| 2818 | layer->m_ProjectionParameters.m_ProjectionBias = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2819 | std::make_shared<ScopedTensorHandle>(*(params.m_ProjectionBias)); |
James Conroy | ed32405 | 2020-05-18 15:16:42 +0100 | [diff] [blame] | 2820 | } |
| 2821 | |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
| 2824 | // QLstm Peephole params |
| 2825 | if(descriptor.m_PeepholeEnabled) |
| 2826 | { |
| 2827 | if(params.m_CellToForgetWeights == nullptr) |
| 2828 | { |
| 2829 | throw InvalidArgumentException("AddQLstmLayer: Cell To Forget Weights cannot be NULL"); |
| 2830 | } |
| 2831 | |
| 2832 | if(params.m_CellToOutputWeights == nullptr) |
| 2833 | { |
| 2834 | throw InvalidArgumentException("AddQLstmLayer: Cell To Output Weights cannot be NULL"); |
| 2835 | } |
| 2836 | |
| 2837 | if(!descriptor.m_CifgEnabled) |
| 2838 | { |
| 2839 | if(params.m_CellToInputWeights == nullptr) |
| 2840 | { |
| 2841 | throw InvalidArgumentException("AddQLstmLayer: Cell To Input Weights cannot be NULL"); |
| 2842 | } |
| 2843 | |
| 2844 | layer->m_PeepholeParameters.m_CellToInputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2845 | std::make_shared<ScopedTensorHandle>(*(params.m_CellToInputWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | layer->m_PeepholeParameters.m_CellToForgetWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2849 | std::make_shared<ScopedTensorHandle>(*(params.m_CellToForgetWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2850 | layer->m_PeepholeParameters.m_CellToOutputWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2851 | std::make_shared<ScopedTensorHandle>(*(params.m_CellToOutputWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | // QLstm Layer Normalization params |
| 2855 | if(descriptor.m_LayerNormEnabled) |
| 2856 | { |
| 2857 | if(params.m_ForgetLayerNormWeights == nullptr) |
| 2858 | { |
| 2859 | throw InvalidArgumentException("AddQLstmLayer: Forget layer normalization weights cannot be NULL"); |
| 2860 | } |
| 2861 | |
| 2862 | if(params.m_CellLayerNormWeights == nullptr) |
| 2863 | { |
| 2864 | throw InvalidArgumentException("AddQLstmLayer: Cell layer normalization weights cannot be NULL"); |
| 2865 | } |
| 2866 | |
| 2867 | if(params.m_OutputLayerNormWeights == nullptr) |
| 2868 | { |
| 2869 | throw InvalidArgumentException("AddQLstmLayer: Output layer normalization weights cannot be NULL"); |
| 2870 | } |
| 2871 | |
| 2872 | if(!descriptor.m_CifgEnabled) |
| 2873 | { |
| 2874 | if(params.m_InputLayerNormWeights == nullptr) |
| 2875 | { |
| 2876 | throw InvalidArgumentException("AddQLstmLayer: Input layer normalization weights cannot be NULL"); |
| 2877 | } |
| 2878 | |
| 2879 | layer->m_LayerNormParameters.m_InputLayerNormWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2880 | std::make_shared<ScopedTensorHandle>(*(params.m_InputLayerNormWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | layer->m_LayerNormParameters.m_ForgetLayerNormWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2884 | std::make_shared<ScopedTensorHandle>(*(params.m_ForgetLayerNormWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2885 | layer->m_LayerNormParameters.m_CellLayerNormWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2886 | std::make_shared<ScopedTensorHandle>(*(params.m_CellLayerNormWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2887 | layer->m_LayerNormParameters.m_OutputLayerNormWeights = |
James Conroy | 1f58f03 | 2021-04-27 17:13:27 +0100 | [diff] [blame] | 2888 | std::make_shared<ScopedTensorHandle>(*(params.m_OutputLayerNormWeights)); |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 2889 | } |
| 2890 | return layer; |
| 2891 | } |
| 2892 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 2893 | IConnectableLayer* NetworkImpl::AddLogicalBinaryLayer(const LogicalBinaryDescriptor& logicalBinaryDescriptor, |
Narumol Prangnawarat | 8ed39ae | 2021-07-15 16:16:25 +0100 | [diff] [blame] | 2894 | const char* name) |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 2895 | { |
| 2896 | return m_Graph->AddLayer<LogicalBinaryLayer>(logicalBinaryDescriptor, name); |
| 2897 | } |
| 2898 | |
Narumol Prangnawarat | 8ed39ae | 2021-07-15 16:16:25 +0100 | [diff] [blame] | 2899 | IConnectableLayer* NetworkImpl::AddUnidirectionalSequenceLstmLayer( |
| 2900 | const UnidirectionalSequenceLstmDescriptor& descriptor, |
| 2901 | const LstmInputParams& params, |
| 2902 | const char* name) |
| 2903 | { |
| 2904 | const auto layer = m_Graph->AddLayer<UnidirectionalSequenceLstmLayer>(descriptor, name); |
| 2905 | |
| 2906 | //Lstm Basic Parameters |
| 2907 | layer->m_BasicParameters.m_InputToForgetWeights = |
| 2908 | std::make_shared<ScopedTensorHandle>(*(params.m_InputToForgetWeights)); |
| 2909 | layer->m_BasicParameters.m_InputToCellWeights = |
| 2910 | std::make_shared<ScopedTensorHandle>(*(params.m_InputToCellWeights)); |
| 2911 | layer->m_BasicParameters.m_InputToOutputWeights = |
| 2912 | std::make_shared<ScopedTensorHandle>(*(params.m_InputToOutputWeights)); |
| 2913 | layer->m_BasicParameters.m_RecurrentToForgetWeights = |
| 2914 | std::make_shared<ScopedTensorHandle>(*(params.m_RecurrentToForgetWeights)); |
| 2915 | layer->m_BasicParameters.m_RecurrentToCellWeights = |
| 2916 | std::make_shared<ScopedTensorHandle>(*(params.m_RecurrentToCellWeights)); |
| 2917 | layer->m_BasicParameters.m_RecurrentToOutputWeights = |
| 2918 | std::make_shared<ScopedTensorHandle>(*(params.m_RecurrentToOutputWeights)); |
| 2919 | layer->m_BasicParameters.m_ForgetGateBias = |
| 2920 | std::make_shared<ScopedTensorHandle>(*(params.m_ForgetGateBias)); |
| 2921 | layer->m_BasicParameters.m_CellBias = |
| 2922 | std::make_shared<ScopedTensorHandle>(*(params.m_CellBias)); |
| 2923 | layer->m_BasicParameters.m_OutputGateBias = |
| 2924 | std::make_shared<ScopedTensorHandle>(*(params.m_OutputGateBias)); |
| 2925 | |
| 2926 | //Lstm Cifg parameters |
| 2927 | if(!descriptor.m_CifgEnabled) |
| 2928 | { |
| 2929 | if(params.m_InputToInputWeights == nullptr) |
| 2930 | { |
| 2931 | throw InvalidArgumentException("AddUnidirectionalSequenceLstmLayer: Input To Input Weights cannot be NULL " |
| 2932 | "when CIFG is disabled."); |
| 2933 | } |
| 2934 | if(params.m_RecurrentToInputWeights == nullptr) |
| 2935 | { |
| 2936 | throw InvalidArgumentException( |
| 2937 | "AddUnidirectionalSequenceLstmLayer: Recurrent To Input Weights cannot be NULL " |
| 2938 | "when CIFG is disabled."); |
| 2939 | } |
| 2940 | if(params.m_InputGateBias == nullptr) |
| 2941 | { |
| 2942 | throw InvalidArgumentException("AddUnidirectionalSequenceLstmLayer: Input Gate Bias cannot be NULL " |
| 2943 | "when CIFG is disabled."); |
| 2944 | } |
| 2945 | layer->m_CifgParameters.m_InputToInputWeights = |
| 2946 | std::make_shared<ScopedTensorHandle>(*(params.m_InputToInputWeights)); |
| 2947 | layer->m_CifgParameters.m_RecurrentToInputWeights = |
| 2948 | std::make_shared<ScopedTensorHandle>(*(params.m_RecurrentToInputWeights)); |
| 2949 | layer->m_CifgParameters.m_InputGateBias = |
| 2950 | std::make_shared<ScopedTensorHandle>(*(params.m_InputGateBias)); |
| 2951 | } |
| 2952 | |
| 2953 | //Lstm projection parameters |
| 2954 | if(descriptor.m_ProjectionEnabled) |
| 2955 | { |
| 2956 | if(params.m_ProjectionWeights == nullptr) |
| 2957 | { |
| 2958 | throw InvalidArgumentException("AddUnidirectionalSequenceLstmLayer: Projection Weights cannot be NULL " |
| 2959 | "when projection is enabled."); |
| 2960 | } |
| 2961 | layer->m_ProjectionParameters.m_ProjectionWeights = |
| 2962 | std::make_shared<ScopedTensorHandle>(*(params.m_ProjectionWeights)); |
| 2963 | if(params.m_ProjectionBias != nullptr) |
| 2964 | { |
| 2965 | layer->m_ProjectionParameters.m_ProjectionBias = |
| 2966 | std::make_shared<ScopedTensorHandle>(*(params.m_ProjectionBias)); |
| 2967 | } |
| 2968 | } |
| 2969 | |
| 2970 | //Lstm Peephole params |
| 2971 | if(descriptor.m_PeepholeEnabled) |
| 2972 | { |
| 2973 | if(!descriptor.m_CifgEnabled) |
| 2974 | { |
| 2975 | if(params.m_CellToInputWeights == nullptr) |
| 2976 | { |
| 2977 | throw InvalidArgumentException("AddUnidirectionalSequenceLstmLayer: Cell To Input Weights " |
| 2978 | "cannot be NULL when Peephole is enabled and CIFG disabled."); |
| 2979 | } |
| 2980 | |
| 2981 | layer->m_PeepholeParameters.m_CellToInputWeights = |
| 2982 | std::make_shared<ScopedTensorHandle>(*(params.m_CellToInputWeights)); |
| 2983 | } |
| 2984 | |
| 2985 | if(params.m_CellToForgetWeights == nullptr) |
| 2986 | { |
| 2987 | throw InvalidArgumentException("AddUnidirectionalSequenceLstmLayer: Cell To Forget Weights cannot be NULL " |
| 2988 | "when Peephole is enabled."); |
| 2989 | } |
| 2990 | if(params.m_CellToOutputWeights == nullptr) |
| 2991 | { |
| 2992 | throw InvalidArgumentException("AddUnidirectionalSequenceLstmLayer: Cell To Output Weights cannot be NULL " |
| 2993 | "when Peephole is enabled."); |
| 2994 | } |
| 2995 | |
| 2996 | layer->m_PeepholeParameters.m_CellToForgetWeights = |
| 2997 | std::make_shared<ScopedTensorHandle>(*(params.m_CellToForgetWeights)); |
| 2998 | layer->m_PeepholeParameters.m_CellToOutputWeights = |
| 2999 | std::make_shared<ScopedTensorHandle>(*(params.m_CellToOutputWeights)); |
| 3000 | } |
| 3001 | |
| 3002 | //Lstm Layer Normalization params |
| 3003 | if(descriptor.m_LayerNormEnabled) |
| 3004 | { |
| 3005 | if(!descriptor.m_CifgEnabled) |
| 3006 | { |
| 3007 | if(params.m_InputLayerNormWeights == nullptr) |
| 3008 | { |
| 3009 | throw InvalidArgumentException("AddUnidirectionalSequenceLstmLayer: Input layer normalization weights " |
| 3010 | "cannot be NULL when layer normalization is enabled and CIFG disabled."); |
| 3011 | } |
| 3012 | layer->m_LayerNormParameters.m_InputLayerNormWeights = |
| 3013 | std::make_shared<ScopedTensorHandle>(*(params.m_InputLayerNormWeights)); |
| 3014 | } |
| 3015 | |
| 3016 | if(params.m_ForgetLayerNormWeights == nullptr) |
| 3017 | { |
| 3018 | throw InvalidArgumentException("AddUnidirectionalSequenceLstmLayer: Forget layer normalization weights " |
| 3019 | "cannot be NULL when layer normalization is enabled."); |
| 3020 | } |
| 3021 | if(params.m_CellLayerNormWeights == nullptr) |
| 3022 | { |
| 3023 | throw InvalidArgumentException("AddUnidirectionalSequenceLstmLayer: Cell layer normalization weights " |
| 3024 | "cannot be NULL when layer normalization is enabled."); |
| 3025 | } |
| 3026 | if(params.m_OutputLayerNormWeights == nullptr) |
| 3027 | { |
| 3028 | throw InvalidArgumentException("AddUnidirectionalSequenceLstmLayer: Output layer normalization weights " |
| 3029 | "cannot be NULL when layer normalization is enabled."); |
| 3030 | } |
| 3031 | layer->m_LayerNormParameters.m_ForgetLayerNormWeights = |
| 3032 | std::make_shared<ScopedTensorHandle>(*(params.m_ForgetLayerNormWeights)); |
| 3033 | layer->m_LayerNormParameters.m_CellLayerNormWeights = |
| 3034 | std::make_shared<ScopedTensorHandle>(*(params.m_CellLayerNormWeights)); |
| 3035 | layer->m_LayerNormParameters.m_OutputLayerNormWeights = |
| 3036 | std::make_shared<ScopedTensorHandle>(*(params.m_OutputLayerNormWeights)); |
| 3037 | } |
| 3038 | return layer; |
| 3039 | } |
| 3040 | |
Samuel Yap | 6b47809 | 2022-07-06 15:36:03 +0100 | [diff] [blame] | 3041 | IConnectableLayer* NetworkImpl::AddBatchMatMulLayer(const BatchMatMulDescriptor& desc, const char* name) |
| 3042 | { |
| 3043 | return m_Graph->AddLayer<BatchMatMulLayer>(desc, name); |
| 3044 | } |
| 3045 | |
Tracy Narine | bb8d759 | 2023-07-13 16:50:54 +0100 | [diff] [blame] | 3046 | IConnectableLayer* NetworkImpl::AddReverseV2Layer(const char *name) |
Tianle Cheng | 988354d | 2023-06-28 13:20:47 +0100 | [diff] [blame] | 3047 | { |
Tracy Narine | bb8d759 | 2023-07-13 16:50:54 +0100 | [diff] [blame] | 3048 | return m_Graph->AddLayer<ReverseV2Layer>(name); |
Tianle Cheng | 988354d | 2023-06-28 13:20:47 +0100 | [diff] [blame] | 3049 | } |
| 3050 | |
Teresa Charlin | 79a06a5 | 2023-07-13 17:16:45 +0100 | [diff] [blame] | 3051 | IConnectableLayer* NetworkImpl::AddTileLayer(const TileDescriptor &desc, const char *name) |
| 3052 | { |
| 3053 | return m_Graph->AddLayer<TileLayer>(desc, name); |
| 3054 | } |
| 3055 | |
Cathal Corbett | 18655b8 | 2021-12-13 13:03:22 +0000 | [diff] [blame] | 3056 | IConnectableLayer* NetworkImpl::AddPrecompiledLayer(const PreCompiledDescriptor& preCompiledDescriptor, |
Cathal Corbett | 3ea0107 | 2022-01-06 10:29:43 +0000 | [diff] [blame] | 3057 | CompiledBlobPtr compiledBlobPtr, |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 3058 | const Optional<BackendId>& backend, |
| 3059 | const char* name) |
Cathal Corbett | 18655b8 | 2021-12-13 13:03:22 +0000 | [diff] [blame] | 3060 | { |
| 3061 | // Method use is for backend users. |
Cathal Corbett | cbfd718 | 2021-12-15 17:12:59 +0000 | [diff] [blame] | 3062 | PreCompiledLayer* layer; |
| 3063 | if (name) |
| 3064 | { |
| 3065 | layer = m_Graph->AddLayer<PreCompiledLayer>(preCompiledDescriptor, name); |
| 3066 | } |
| 3067 | else |
| 3068 | { |
| 3069 | layer = m_Graph->AddLayer<PreCompiledLayer>(preCompiledDescriptor, "pre-compiled"); |
| 3070 | } |
Cathal Corbett | 18655b8 | 2021-12-13 13:03:22 +0000 | [diff] [blame] | 3071 | |
| 3072 | // Assign the pre-compiled object to layer |
| 3073 | // Pass only one compiled network, Arm NN does not handle multiple |
| 3074 | // pre-compiled objects in a single pre-compiled layer currently |
| 3075 | layer->SetPreCompiledObject(std::move(compiledBlobPtr)); |
| 3076 | |
| 3077 | if (backend.has_value()) |
| 3078 | { |
| 3079 | layer->SetBackendId(backend.value()); |
| 3080 | } |
Francis Murtagh | 9d74ba6 | 2022-01-19 16:31:58 +0000 | [diff] [blame] | 3081 | else if (layer->GetBackendHint().has_value()) |
Cathal Corbett | 18655b8 | 2021-12-13 13:03:22 +0000 | [diff] [blame] | 3082 | { |
| 3083 | layer->SetBackendId(layer->GetBackendHint().value()); |
| 3084 | } |
| 3085 | |
| 3086 | return layer; |
| 3087 | } |
| 3088 | |
Idriss Chaouch | 98e383e | 2023-08-28 14:28:31 +0100 | [diff] [blame] | 3089 | IConnectableLayer* NetworkImpl::AddBroadcastToLayer(const BroadcastToDescriptor &desc, const char *name) |
| 3090 | { |
| 3091 | return m_Graph->AddLayer<BroadcastToLayer>(desc, name); |
| 3092 | } |
| 3093 | |
Tianle Cheng | 2828818 | 2024-02-23 17:56:54 +0000 | [diff] [blame^] | 3094 | IConnectableLayer* NetworkImpl::AddScatterNdLayer(const ScatterNdDescriptor &desc, const char *name) |
| 3095 | { |
| 3096 | return m_Graph->AddLayer<ScatterNdLayer>(desc, name); |
| 3097 | } |
| 3098 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 3099 | void NetworkImpl::ExecuteStrategy(IStrategy& strategy) const |
Finn Williams | b454c5c | 2021-02-09 15:56:23 +0000 | [diff] [blame] | 3100 | { |
| 3101 | for (auto layer : GetGraph()) |
| 3102 | { |
| 3103 | layer->ExecuteStrategy(strategy); |
| 3104 | }; |
| 3105 | } |
| 3106 | |
Mike Kelly | 0d677db | 2021-06-27 22:39:21 +0100 | [diff] [blame] | 3107 | OptimizedNetworkImpl::OptimizedNetworkImpl(const OptimizedNetworkImpl& other, const ModelOptions& modelOptions) |
| 3108 | : m_Graph(new Graph(*other.m_Graph.get())) |
Jim Flynn | af94772 | 2022-03-02 11:04:47 +0000 | [diff] [blame] | 3109 | , m_Guid(arm::pipe::IProfilingService::GetNextGuid()) |
Mike Kelly | 0d677db | 2021-06-27 22:39:21 +0100 | [diff] [blame] | 3110 | , m_ModelOptions(modelOptions) |
| 3111 | { |
| 3112 | } |
| 3113 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 3114 | OptimizedNetworkImpl::OptimizedNetworkImpl(std::unique_ptr<Graph> graph) |
Jim Flynn | af94772 | 2022-03-02 11:04:47 +0000 | [diff] [blame] | 3115 | : m_Graph(std::move(graph)), m_Guid(arm::pipe::IProfilingService::GetNextGuid()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 3116 | { |
| 3117 | } |
| 3118 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 3119 | OptimizedNetworkImpl::OptimizedNetworkImpl(std::unique_ptr<Graph> graph, const ModelOptions& modelOptions) |
Jim Flynn | af94772 | 2022-03-02 11:04:47 +0000 | [diff] [blame] | 3120 | : m_Graph(std::move(graph)), m_Guid(arm::pipe::IProfilingService::GetNextGuid()), m_ModelOptions(modelOptions) |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 3121 | { |
| 3122 | } |
| 3123 | |
Francis Murtagh | 3d2b4b2 | 2021-02-15 18:23:17 +0000 | [diff] [blame] | 3124 | OptimizedNetworkImpl::~OptimizedNetworkImpl() |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 3125 | { |
| 3126 | } |
| 3127 | |
Teresa Charlin | 83b4291 | 2022-07-07 14:24:59 +0100 | [diff] [blame] | 3128 | void IOptimizedNetwork::ExecuteStrategy(IStrategy &strategy) const |
| 3129 | { |
| 3130 | pOptimizedNetworkImpl->ExecuteStrategy(strategy); |
| 3131 | } |
| 3132 | |
| 3133 | void OptimizedNetworkImpl::ExecuteStrategy(IStrategy &strategy) const |
| 3134 | { |
| 3135 | for (auto layer : GetGraph()) |
| 3136 | { |
| 3137 | layer->ExecuteStrategy(strategy); |
| 3138 | }; |
| 3139 | } |
| 3140 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 3141 | } // namespace armnn |