Laurent Carlier | 749294b | 2020-06-01 09:03:17 +0100 | [diff] [blame] | 1 | // |
Teresa Charlin | 5266473 | 2020-06-29 16:27:03 +0100 | [diff] [blame] | 2 | // Copyright © 2017 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" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 14 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 15 | #include <backendsCommon/CpuTensorHandle.hpp> |
| 16 | #include <backendsCommon/WorkloadFactory.hpp> |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 17 | #include <armnn/backends/IBackendInternal.hpp> |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 18 | #include <backendsCommon/TensorHandleFactoryRegistry.hpp> |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 19 | |
| 20 | #include <armnn/Exceptions.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 21 | #include <armnn/Utils.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 22 | #include <armnn/TypesUtils.hpp> |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 23 | #include <armnn/BackendRegistry.hpp> |
Matthew Bentham | f48afc6 | 2020-01-15 17:55:08 +0000 | [diff] [blame] | 24 | #include <armnn/Logging.hpp> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 25 | #include <armnn/utility/Assert.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 26 | #include <armnn/utility/IgnoreUnused.hpp> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 27 | #include <armnn/utility/PolymorphicDowncast.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 28 | |
Jan Eilers | 99d9d4a | 2019-11-06 10:02:16 +0000 | [diff] [blame] | 29 | #include <ProfilingService.hpp> |
| 30 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 31 | #include <fcntl.h> |
| 32 | #include <algorithm> |
| 33 | #include <fstream> |
| 34 | #include <memory> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 35 | #include <vector> |
| 36 | #include <algorithm> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 37 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 38 | namespace armnn |
| 39 | { |
| 40 | |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 41 | armnn::INetwork* INetwork::CreateRaw(NetworkOptions networkOptions) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 42 | { |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 43 | return new Network(networkOptions); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 46 | armnn::INetworkPtr INetwork::Create(NetworkOptions networkOptions) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 47 | { |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 48 | return INetworkPtr(CreateRaw(networkOptions), &INetwork::Destroy); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void INetwork::Destroy(INetwork* network) |
| 52 | { |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 53 | delete PolymorphicDowncast<Network*>(network); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 54 | } |
| 55 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 56 | void IOptimizedNetwork::Destroy(IOptimizedNetwork* network) |
| 57 | { |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 58 | delete PolymorphicDowncast<OptimizedNetwork*>(network); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | Status OptimizedNetwork::PrintGraph() |
| 62 | { |
| 63 | m_Graph->Print(); |
| 64 | return Status::Success; |
| 65 | } |
| 66 | |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 67 | Status OptimizedNetwork::SerializeToDot(std::ostream& stream) const |
| 68 | { |
| 69 | return m_Graph->SerializeToDot(stream); |
| 70 | } |
| 71 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 72 | void ReportError(const std::string& errorMessage, |
| 73 | Optional<std::vector<std::string>&> errorMessages) |
| 74 | { |
| 75 | std::stringstream fullErrorMessage; |
| 76 | fullErrorMessage << "ERROR: " << errorMessage; |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 77 | ARMNN_LOG(warning) << fullErrorMessage.str(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 78 | if (errorMessages) |
| 79 | { |
| 80 | errorMessages.value().push_back(fullErrorMessage.str()); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void ReportWarning(const std::string& warningMessage, |
| 85 | Optional<std::vector<std::string>&> warningMessages) |
| 86 | { |
| 87 | std::stringstream fullWarningMessage; |
| 88 | fullWarningMessage << "WARNING: " << warningMessage; |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 89 | ARMNN_LOG(warning) << fullWarningMessage.str(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 90 | if (warningMessages) |
| 91 | { |
| 92 | warningMessages.value().push_back(fullWarningMessage.str()); |
| 93 | } |
| 94 | } |
| 95 | |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 96 | OptimizationResult ReturnWithError(OptimizationResult res, |
| 97 | const Layer* layer, |
| 98 | const BackendSettings& backendSettings, |
| 99 | Optional<std::vector<std::string>&> errMessages) |
| 100 | { |
| 101 | std::stringstream failureMsg; |
| 102 | failureMsg << "Layer of type " << GetLayerTypeAsCString(layer->GetType()) |
| 103 | << " is not supported on any preferred backend " << backendSettings.m_PreferredBackends; |
| 104 | ReportError(failureMsg.str(), errMessages); |
| 105 | |
| 106 | res.m_Error = true; |
| 107 | return res; |
| 108 | } |
| 109 | |
| 110 | |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 111 | bool CheckScaleSetOnQuantizedType(Layer* layer, Optional<std::vector<std::string>&> errMessages) |
| 112 | { |
| 113 | bool noErrors = true; |
| 114 | unsigned int numOutputs = layer->GetNumOutputSlots(); |
| 115 | for (unsigned int i = 0; i < numOutputs; i++) { |
David Monahan | b855470 | 2019-04-25 16:03:38 +0100 | [diff] [blame] | 116 | OutputSlot& outputSlot = layer->GetOutputSlot(i); |
| 117 | TensorInfo info = outputSlot.GetTensorInfo(); |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 118 | if (DataType::QAsymmU8 == info.GetDataType()) { |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 119 | if (0.f == info.GetQuantizationScale()) { |
| 120 | noErrors = false; |
| 121 | std::stringstream ss; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 122 | ss << "output " << i << " of layer " << GetLayerTypeAsCString(layer->GetType()) |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 123 | << " (" << layer->GetNameStr() << ") is of type" |
| 124 | << " Quantized 8 bit but its scale parameter has not been set"; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 125 | ReportError(ss.str(), errMessages); |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 126 | } |
David Monahan | b855470 | 2019-04-25 16:03:38 +0100 | [diff] [blame] | 127 | // Softmax under QuantisedAsymm8 must always be scale (1.0f/256.0f) and offset 0 |
| 128 | if ((info.GetQuantizationScale() != (1.0f / 256.0f) || |
| 129 | info.GetQuantizationOffset() != 0) && |
| 130 | layer->GetType() == armnn::LayerType::Softmax) |
| 131 | { |
| 132 | std::stringstream ss; |
| 133 | ss << "Quantization parameters for Softmax layer (Scale: " << |
| 134 | info.GetQuantizationScale() << " and Offset: " << info.GetQuantizationOffset() << |
| 135 | ") are incorrect and have been updated to Scale: 0.00390625 and Offset: 0"; |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 136 | ARMNN_LOG(warning) << ss.str(); |
David Monahan | b855470 | 2019-04-25 16:03:38 +0100 | [diff] [blame] | 137 | info.SetQuantizationScale((1.0f /256.0f)); |
| 138 | info.SetQuantizationOffset(0); |
| 139 | outputSlot.SetTensorInfo(info); |
| 140 | } |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | return noErrors; |
| 144 | } |
| 145 | |
Narumol Prangnawarat | 250d392 | 2020-03-30 16:11:04 +0100 | [diff] [blame] | 146 | template <typename LayerT> |
| 147 | LayerT* ConvertBf16ToFp32Weight(Layer* l) |
| 148 | { |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 149 | LayerT* layer = PolymorphicDowncast<LayerT*>(l); |
Narumol Prangnawarat | 250d392 | 2020-03-30 16:11:04 +0100 | [diff] [blame] | 150 | if ((layer->GetType() == LayerType::Convolution2d || layer->GetType() == LayerType::FullyConnected) |
| 151 | && layer->m_Weight) |
| 152 | { |
| 153 | const TensorInfo& info = layer->m_Weight->GetTensorInfo(); |
| 154 | |
| 155 | if (info.GetDataType() == DataType::BFloat16) |
| 156 | { |
| 157 | std::vector<float> newValues(info.GetNumElements()); |
| 158 | |
| 159 | armnnUtils::FloatingPointConverter::ConvertBFloat16ToFloat32( |
| 160 | layer->m_Weight->template GetTensor<armnn::BFloat16>(), info.GetNumElements(), newValues.data()); |
| 161 | |
| 162 | TensorInfo newInfo(info.GetShape(), DataType::Float32); |
| 163 | ConstTensor newInput(newInfo, newValues); |
| 164 | layer->m_Weight.reset(new ScopedCpuTensorHandle(newInput)); |
| 165 | } |
| 166 | } |
| 167 | return layer; |
| 168 | } |
| 169 | |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 170 | OptimizationResult AttemptBackendAssignment(BackendSettings& backendSettings, |
| 171 | Graph& graph, |
| 172 | Layer* layer, |
| 173 | BackendId backend, |
| 174 | DataType dataTypeIn, |
| 175 | DataType dataTypeOut, |
| 176 | const std::vector<BackendId>& availablePreferredBackends, |
| 177 | std::string& reasonIfUnsupported, |
| 178 | Optional<std::vector<std::string>&> errMessages) |
| 179 | { |
| 180 | OptimizationResult result; |
| 181 | |
| 182 | // Helper lambda to compose meaningful error message before returning with error |
| 183 | auto ReturnError = [&](const Layer* layer) |
| 184 | { |
| 185 | return ReturnWithError(result, layer, backendSettings, errMessages); |
| 186 | }; |
| 187 | |
| 188 | // need to set the compute device on the layer |
| 189 | // before we can check if it is supported |
| 190 | layer->SetBackendId(backend); |
| 191 | if (!IWorkloadFactory::IsLayerSupported(*layer, EmptyOptional(), reasonIfUnsupported)) |
| 192 | { |
| 193 | if (dataTypeIn == DataType::Float16 || dataTypeOut == DataType::Float16) |
| 194 | { |
| 195 | if (IWorkloadFactory::IsLayerSupported(*layer, DataType::Float32, reasonIfUnsupported) |
| 196 | && layer->GetType() != LayerType::ConvertFp32ToFp16 |
| 197 | && layer->GetType() != LayerType::ConvertFp16ToFp32) |
| 198 | { |
| 199 | // Insert FP16 -> FP32 conversion layer before current layer |
| 200 | std::vector<ConvertFp16ToFp32Layer*> convertFp16ToFp32Layers; |
| 201 | if (dataTypeIn == DataType::Float16) |
| 202 | { |
| 203 | convertFp16ToFp32Layers = |
| 204 | InsertConvertFp16ToFp32LayersBefore(graph, *layer); |
| 205 | } |
| 206 | |
| 207 | // Insert FP32 -> FP16 conversion layer after current layer |
| 208 | std::vector<ConvertFp32ToFp16Layer*> convertFp32ToFp16Layers; |
| 209 | if (dataTypeOut == DataType::Float16) |
| 210 | { |
| 211 | convertFp32ToFp16Layers = |
| 212 | InsertConvertFp32ToFp16LayersAfter(graph, *layer); |
| 213 | } |
| 214 | |
| 215 | // Assign a supported backend to the newly introduced conversion layers |
| 216 | auto AssignFirstSupportedBackend = [&](Layer* layer, BackendId preferredBackend) |
| 217 | { |
| 218 | bool supportedBackendFound = false; |
| 219 | std::string reasonIfUnsupported; |
| 220 | |
| 221 | // Try preferred backend first |
| 222 | layer->SetBackendId(preferredBackend); |
| 223 | if (IWorkloadFactory::IsLayerSupported(*layer, |
| 224 | EmptyOptional(), |
| 225 | reasonIfUnsupported)) |
| 226 | { |
| 227 | supportedBackendFound = true; |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | for (const auto& backend : availablePreferredBackends) |
| 232 | { |
| 233 | // Skip preferred backend (we already determined that it is not supported) |
| 234 | if (backend == preferredBackend) |
| 235 | { |
| 236 | continue; |
| 237 | } |
| 238 | |
| 239 | layer->SetBackendId(backend); |
| 240 | if (IWorkloadFactory::IsLayerSupported(*layer, |
| 241 | EmptyOptional(), |
| 242 | reasonIfUnsupported)) |
| 243 | { |
| 244 | supportedBackendFound = true; |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return supportedBackendFound; |
| 251 | }; |
| 252 | |
| 253 | for (ConvertFp16ToFp32Layer* convertLayer : convertFp16ToFp32Layers) |
| 254 | { |
| 255 | if (!AssignFirstSupportedBackend(convertLayer, backend)) |
| 256 | { |
| 257 | return ReturnError(convertLayer); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | for (ConvertFp32ToFp16Layer* convertLayer : convertFp32ToFp16Layers) |
| 262 | { |
| 263 | if (!AssignFirstSupportedBackend(convertLayer, backend)) |
| 264 | { |
| 265 | return ReturnError(convertLayer); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return result; |
| 270 | } |
| 271 | } |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 272 | else if (dataTypeIn == DataType::BFloat16 || dataTypeOut == DataType::BFloat16) |
| 273 | { |
| 274 | if (IWorkloadFactory::IsLayerSupported(*layer, DataType::Float32, reasonIfUnsupported) |
| 275 | && layer->GetType() != LayerType::ConvertFp32ToBf16 |
| 276 | && layer->GetType() != LayerType::ConvertBf16ToFp32) |
| 277 | { |
| 278 | // Insert BF16 -> FP32 conversion layer before current layer |
| 279 | std::vector<ConvertBf16ToFp32Layer*> convertBf16ToFp32Layers; |
| 280 | if (dataTypeIn == DataType::BFloat16) |
| 281 | { |
| 282 | convertBf16ToFp32Layers = |
| 283 | InsertConvertBf16ToFp32LayersBefore(graph, *layer); |
Narumol Prangnawarat | 250d392 | 2020-03-30 16:11:04 +0100 | [diff] [blame] | 284 | if (layer->GetType() == LayerType::Convolution2d) |
| 285 | { |
| 286 | ConvertBf16ToFp32Weight<Convolution2dLayer>(layer); |
| 287 | } |
| 288 | else if (layer->GetType() == LayerType::FullyConnected) |
| 289 | { |
| 290 | ConvertBf16ToFp32Weight<FullyConnectedLayer>(layer); |
| 291 | } |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Insert FP32 -> BF16 conversion layer after current layer |
| 295 | std::vector<ConvertFp32ToBf16Layer*> convertFp32ToBf16Layers; |
| 296 | if (dataTypeOut == DataType::BFloat16) |
| 297 | { |
| 298 | convertFp32ToBf16Layers = |
| 299 | InsertConvertFp32ToBf16LayersAfter(graph, *layer); |
| 300 | } |
| 301 | |
| 302 | // Assign a supported backend to the newly introduced conversion layers |
| 303 | auto AssignFirstSupportedBackend = [&](Layer* layer, BackendId preferredBackend) |
| 304 | { |
| 305 | bool supportedBackendFound = false; |
| 306 | std::string reasonIfUnsupported; |
| 307 | |
| 308 | // Try preferred backend first |
| 309 | layer->SetBackendId(preferredBackend); |
| 310 | if (IWorkloadFactory::IsLayerSupported(*layer, |
| 311 | EmptyOptional(), |
| 312 | reasonIfUnsupported)) |
| 313 | { |
| 314 | supportedBackendFound = true; |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | for (const auto& backend : availablePreferredBackends) |
| 319 | { |
| 320 | // Skip preferred backend (we already determined that it is not supported) |
| 321 | if (backend == preferredBackend) |
| 322 | { |
| 323 | continue; |
| 324 | } |
| 325 | |
| 326 | layer->SetBackendId(backend); |
| 327 | if (IWorkloadFactory::IsLayerSupported(*layer, |
| 328 | EmptyOptional(), |
| 329 | reasonIfUnsupported)) |
| 330 | { |
| 331 | supportedBackendFound = true; |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | return supportedBackendFound; |
| 338 | }; |
| 339 | |
| 340 | for (ConvertBf16ToFp32Layer* convertLayer : convertBf16ToFp32Layers) |
| 341 | { |
| 342 | if (!AssignFirstSupportedBackend(convertLayer, backend)) |
| 343 | { |
| 344 | return ReturnError(convertLayer); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | for (ConvertFp32ToBf16Layer* convertLayer : convertFp32ToBf16Layers) |
| 349 | { |
| 350 | if (!AssignFirstSupportedBackend(convertLayer, backend)) |
| 351 | { |
| 352 | return ReturnError(convertLayer); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return result; |
| 357 | } |
| 358 | } |
| 359 | |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 360 | std::stringstream warningMsg; |
| 361 | warningMsg << "Layer of type " << GetLayerTypeAsCString(layer->GetType()) |
| 362 | << " is not supported on requested backend " << layer->GetBackendId().Get() |
| 363 | << " for input data type " << GetDataTypeName(dataTypeIn) |
| 364 | << " and output data type " << GetDataTypeName(dataTypeOut) |
| 365 | << " (reason: " << reasonIfUnsupported |
| 366 | << "), falling back to the next backend."; |
| 367 | ReportWarning(warningMsg.str(), errMessages); |
| 368 | |
| 369 | return OptimizationResult(true, false); |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | return result; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 378 | OptimizationResult AssignBackends(OptimizedNetwork* optNetObjPtr, |
| 379 | BackendSettings& backendSettings, |
| 380 | Graph::Iterator& firstLayer, |
| 381 | Graph::Iterator& lastLayer, |
| 382 | Optional<std::vector<std::string>&> errMessages) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 383 | { |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 384 | OptimizationResult result; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 385 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 386 | // Helper lambda to compose meaningful error message before returning with error |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 387 | auto ReturnError = [&](const Layer* layer) |
| 388 | { |
| 389 | return ReturnWithError(result, layer, backendSettings, errMessages); |
| 390 | }; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 391 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 392 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 393 | auto availablePreferredBackends = backendSettings.GetAvailablePreferredBackends(); |
| 394 | if (availablePreferredBackends.empty()) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 395 | { |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 396 | std::stringstream failureMsg; |
| 397 | failureMsg << "No preferred backends are available"; |
| 398 | ReportError(failureMsg.str(), errMessages); |
| 399 | |
| 400 | result.m_Error = true; |
| 401 | return result; |
| 402 | } |
| 403 | |
| 404 | for (auto it = firstLayer; it != lastLayer; ++it) |
| 405 | { |
| 406 | auto layer = *it; |
Aron Virginas-Tar | 87972be | 2019-11-13 15:16:28 +0000 | [diff] [blame] | 407 | |
| 408 | DataType dataTypeIn = layer->GetNumInputSlots() == 0 ? DataType::Float32 : |
| 409 | layer->GetInputSlot(0).GetConnectedOutputSlot()->GetTensorInfo().GetDataType(); |
| 410 | DataType dataTypeOut = layer->GetNumOutputSlots() == 0 ? DataType::Float32 : |
| 411 | layer->GetOutputSlot(0).GetTensorInfo().GetDataType(); |
| 412 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 413 | std::string reasonIfUnsupported; |
| 414 | bool found = false; |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 415 | if (!CheckScaleSetOnQuantizedType(layer, errMessages)) |
| 416 | { |
| 417 | // don't bomb immediately, find all the quantized outputs |
| 418 | // which haven't had a scale set and report them all back. |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 419 | result.m_Error = true; |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 420 | } |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 421 | |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 422 | // First try assign layer to hint backend |
| 423 | if (layer->GetBackendHint().has_value() && |
| 424 | backendSettings.IsBackendSupported(layer->GetBackendHint().value()) && |
| 425 | AttemptBackendAssignment(backendSettings, |
| 426 | optNetObjPtr->GetGraph(), |
| 427 | layer, |
| 428 | layer->GetBackendHint().value(), |
| 429 | dataTypeIn, |
| 430 | dataTypeOut, |
| 431 | availablePreferredBackends, |
| 432 | reasonIfUnsupported, |
| 433 | errMessages).IsOk()) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 434 | { |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 435 | found = true; |
| 436 | backendSettings.m_SelectedBackends.insert(layer->GetBackendHint().value()); |
| 437 | } |
| 438 | else |
| 439 | { |
| 440 | // Try assign layer to prefered list of backends |
| 441 | for (const auto& backend : availablePreferredBackends) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 442 | { |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 443 | if (layer->GetBackendHint().has_value() && |
| 444 | layer->GetBackendHint().value() == backend) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 445 | { |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 446 | continue; //Don't re-test the backend hint |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 447 | } |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 448 | |
| 449 | OptimizationResult res = AttemptBackendAssignment(backendSettings, |
| 450 | optNetObjPtr->GetGraph(), |
| 451 | layer, |
| 452 | backend, |
| 453 | dataTypeIn, |
| 454 | dataTypeOut, |
| 455 | availablePreferredBackends, |
| 456 | reasonIfUnsupported, |
| 457 | errMessages); |
| 458 | |
| 459 | if (res.IsOk()) |
| 460 | { |
| 461 | found = true; |
| 462 | backendSettings.m_SelectedBackends.insert(backend); |
| 463 | break; |
| 464 | } |
| 465 | else if (res.IsError()) |
| 466 | { |
| 467 | return res; // Cannot continue. |
| 468 | // Note: we don't need to log the error as it would already |
| 469 | // be logged in AttemptBackendAssignment(). |
| 470 | } |
| 471 | else |
| 472 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 473 | ARMNN_ASSERT_MSG(res.IsWarningOnly(), "OptimizationResult in unexpected state."); |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 474 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
| 478 | // If the layer is unsupported by any devices, log and return a null network. |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 479 | if (!found) |
| 480 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 481 | // NOTE: if the layer is not an operation queue type AND we have not got CpuRef as a |
| 482 | // fallback we should set the compute device on the layer to CpuRef (these are not |
| 483 | // available as accelerated operations, or are only available under certain |
| 484 | // conditions, currently they comprise MemCopy, Constant, Permute) |
| 485 | armnn::LayerType layerType = layer->GetType(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 486 | if (!backendSettings.IsCpuRefUsed() && (layerType == armnn::LayerType::MemCopy || |
| 487 | layerType == armnn::LayerType::Constant || |
| 488 | layerType == armnn::LayerType::Permute)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 489 | { |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 490 | BackendId cpuBackendId(armnn::Compute::CpuRef); |
| 491 | layer->SetBackendId(cpuBackendId); |
| 492 | backendSettings.m_SelectedBackends.insert(cpuBackendId); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 493 | } |
| 494 | else |
| 495 | { |
Derek Lamberti | 4a9e24b | 2020-01-03 16:53:38 +0000 | [diff] [blame] | 496 | return ReturnError(layer); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | } |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 500 | |
| 501 | return result; |
| 502 | } |
| 503 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 504 | OptimizationResult AssignBackends(OptimizedNetwork* optNetObjPtr, |
| 505 | BackendSettings& backendSettings, |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 506 | SubgraphView& subgraph, |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 507 | Optional<std::vector<std::string>&> errMessages) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 508 | { |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 509 | Graph::Iterator firstLayer = subgraph.begin(); |
| 510 | Graph::Iterator lastLayer = subgraph.end(); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 511 | return AssignBackends(optNetObjPtr, |
| 512 | backendSettings, |
| 513 | firstLayer, |
| 514 | lastLayer, |
| 515 | errMessages); |
| 516 | } |
| 517 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 518 | BackendsMap CreateSupportedBackends(TensorHandleFactoryRegistry& handleFactoryRegistry, |
| 519 | BackendSettings& backendSettings) |
| 520 | { |
| 521 | BackendsMap backends; |
| 522 | auto const& backendRegistry = BackendRegistryInstance(); |
| 523 | for (auto&& selectedBackend : backendSettings.m_SupportedBackends) |
| 524 | { |
| 525 | auto backendFactory = backendRegistry.GetFactory(selectedBackend); |
| 526 | auto backendObjPtr = backendFactory(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 527 | ARMNN_ASSERT(backendObjPtr); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 528 | |
| 529 | backendObjPtr->RegisterTensorHandleFactories(handleFactoryRegistry); |
| 530 | |
| 531 | backends[backendObjPtr->GetId()] = std::move(backendObjPtr); |
| 532 | } |
| 533 | |
| 534 | return backends; |
| 535 | } |
| 536 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 537 | OptimizationResult ApplyBackendOptimizations(OptimizedNetwork* optNetObjPtr, |
| 538 | BackendSettings& backendSettings, |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 539 | BackendsMap& backends, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame^] | 540 | const ModelOptions& modelOptions, |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 541 | Optional<std::vector<std::string>&> errMessages) |
| 542 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 543 | ARMNN_ASSERT(optNetObjPtr); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 544 | |
| 545 | OptimizationResult result; |
| 546 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 547 | // Get the optimized graph |
| 548 | Graph& optGraph = optNetObjPtr->GetGraph(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 549 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 550 | // Run backend specific optimizations |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 551 | for (auto&& selectedBackend : backendSettings.m_SelectedBackends) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 552 | { |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 553 | auto backendObjPtr = backends.find(selectedBackend)->second.get(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 554 | ARMNN_ASSERT(backendObjPtr); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 555 | |
| 556 | // Select sub-graphs based on backend |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 557 | SubgraphViewSelector::Subgraphs subgraphs = |
Rob Hughes | 65c3226 | 2019-07-23 15:33:39 +0100 | [diff] [blame] | 558 | SubgraphViewSelector::SelectSubgraphs(optGraph, |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 559 | // Select layers assigned to the requested backend |
| 560 | [&backendObjPtr](const Layer& layer) |
| 561 | { |
| 562 | return layer.GetType() != LayerType::Input && |
| 563 | layer.GetType() != LayerType::Output && |
| 564 | layer.GetBackendId() == backendObjPtr->GetId(); |
| 565 | }); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 566 | if (subgraphs.empty()) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 567 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 568 | // No sub-graphs found, try with next selected backend |
| 569 | continue; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 570 | } |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 571 | |
| 572 | // Try to optimize each sub-graph |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 573 | for (auto& subgraph : subgraphs) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 574 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 575 | // Try to optimize the current sub-graph |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame^] | 576 | OptimizationViews optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraph, modelOptions); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 577 | ARMNN_ASSERT(optimizationViews.Validate(*subgraph)); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 578 | |
| 579 | // Optimization attempted, check the resulting optimized sub-graph |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 580 | for (auto& substitution : optimizationViews.GetSubstitutions()) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 581 | { |
| 582 | // 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] | 583 | SubgraphView& replacementSubgraph = substitution.m_ReplacementSubgraph; |
| 584 | SubgraphView& substitutableSubgraph = substitution.m_SubstitutableSubgraph; |
| 585 | optGraph.SubstituteSubgraph(substitutableSubgraph, replacementSubgraph); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 586 | |
| 587 | // Assign the current backend to the optimized sub-graph |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 588 | std::for_each(replacementSubgraph.begin(), replacementSubgraph.end(), [&selectedBackend](Layer* l) |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 589 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 590 | ARMNN_ASSERT(l); |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 591 | l->SetBackendId(selectedBackend); |
| 592 | }); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 593 | } |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 594 | |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 595 | if (!optimizationViews.GetFailedSubgraphs().empty()) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 596 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 597 | std::stringstream warningMsg; |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 598 | warningMsg << "Some sub-graph(s) failed to optimized on " << backendObjPtr->GetId() << " backend."; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 599 | ReportWarning(warningMsg.str(), errMessages); |
| 600 | |
| 601 | // 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] | 602 | BackendSettings settingsCopy(backendSettings); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 603 | if (!backendObjPtr->GetId().IsCpuRef()) |
| 604 | { |
| 605 | // Add the current backend to the list of backends to ignore |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 606 | settingsCopy.m_IgnoredBackends.insert(backendObjPtr->GetId()); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 607 | } |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 608 | |
| 609 | int count=0; |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 610 | for (auto& failedSubgraph : optimizationViews.GetFailedSubgraphs()) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 611 | { |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 612 | // An error occurred: the optimization was attempted but not performed, try different backends |
| 613 | std::stringstream subgraphMsg; |
| 614 | subgraphMsg << "Re-assigning backends to " << failedSubgraph.GetLayers().size() |
| 615 | << " layers inside sub-graph " << count++; |
Matteo Martincigh | 328d92b | 2019-07-04 17:52:55 +0100 | [diff] [blame] | 616 | ReportWarning(subgraphMsg.str(), errMessages); |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 617 | |
| 618 | OptimizationResult reassignmentResult = AssignBackends(optNetObjPtr, |
| 619 | settingsCopy, |
| 620 | *subgraph, |
| 621 | errMessages); |
| 622 | if (reassignmentResult.m_Error) |
| 623 | { |
| 624 | // Failed to re-assign one of the remaining backends to each layer of the sub-graph |
| 625 | result.m_Error = true; |
| 626 | return result; |
| 627 | } |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 628 | } |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | return result; |
| 634 | } |
| 635 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 636 | bool RequiresCopy(ITensorHandleFactory::FactoryId src, |
| 637 | ITensorHandleFactory::FactoryId dst, |
| 638 | TensorHandleFactoryRegistry& registry) |
| 639 | { |
| 640 | if (src != dst) |
| 641 | { |
| 642 | ITensorHandleFactory* srcFactory = registry.GetFactory(src); |
| 643 | ITensorHandleFactory* dstFactory = registry.GetFactory(dst); |
| 644 | |
Matteo Martincigh | a6539ed | 2019-08-27 13:43:32 +0100 | [diff] [blame] | 645 | if (srcFactory && dstFactory && |
| 646 | (srcFactory->GetExportFlags() & dstFactory->GetImportFlags()) != 0) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 647 | { |
| 648 | return false; |
| 649 | } |
| 650 | return true; |
| 651 | } |
| 652 | return false; |
| 653 | } |
| 654 | |
| 655 | // Find the handle factory for the input layer which results in fewest required copies. |
| 656 | ITensorHandleFactory::FactoryId CalculateSlotOptionForInput(BackendsMap& backends, |
| 657 | OutputSlot& slot, |
| 658 | TensorHandleFactoryRegistry& registry) |
| 659 | { |
| 660 | Layer& layer = slot.GetOwningLayer(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 661 | ARMNN_ASSERT(layer.GetType() == LayerType::Input); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 662 | |
| 663 | // Explicitly select the tensorhandle factory for InputLayer because the rules for it are slightly different. It |
| 664 | // doesn't matter which backend it is assigned to because they all use the same implementation, which |
| 665 | // requires Map/Unmap support. This means that, so long as the handle type supports map/unmap semantics, we can |
| 666 | // select a factory with maximum compatibility with the layers connected to the InputLayer. |
| 667 | |
| 668 | // First ensure the from backends can support the TensorHandeAPI |
| 669 | auto frmBackend = backends.find(layer.GetBackendId()); |
| 670 | if (frmBackend == backends.end() || |
| 671 | !frmBackend->second->SupportsTensorAllocatorAPI()) |
| 672 | { |
| 673 | return ITensorHandleFactory::LegacyFactoryId; |
| 674 | } |
| 675 | |
| 676 | // Go through all connections to the output slot and determine the TensorHandleFactory which results in the |
| 677 | // fewest copies. |
| 678 | std::map<ITensorHandleFactory::FactoryId, int> factoryScores; |
| 679 | int topScore = 0; |
| 680 | ITensorHandleFactory::FactoryId topChoice = ITensorHandleFactory::LegacyFactoryId; |
| 681 | |
| 682 | for (auto&& connection : slot.GetConnections()) |
| 683 | { |
| 684 | const Layer& connectedLayer = connection->GetOwningLayer(); |
| 685 | |
| 686 | auto toBackend = backends.find(connectedLayer.GetBackendId()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 687 | 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] | 688 | |
| 689 | if (!toBackend->second.get()->SupportsTensorAllocatorAPI()) |
| 690 | { |
| 691 | // The destination backend does not support the tensor allocator API, move to the next one |
| 692 | continue; |
| 693 | } |
| 694 | |
| 695 | auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences(); |
| 696 | for (auto&& dst : dstPrefs) |
| 697 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 698 | // Input layers use the mem copy workload or import, so the selected factory must |
| 699 | // support either the map/unmap API or Import API |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 700 | ITensorHandleFactory* factory = registry.GetFactory(dst); |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 701 | if (!factory->SupportsMapUnmap() && |
| 702 | !CheckFlag(factory->GetImportFlags(), MemorySource::Malloc)) // Just support cpu mem imports for now |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 703 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 704 | // The current tensor handle factory does not support the map/unmap or import |
| 705 | // strategy, move to the next one |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 706 | continue; |
| 707 | } |
| 708 | |
| 709 | auto it = factoryScores.find(dst); |
| 710 | if (it == factoryScores.end()) |
| 711 | { |
| 712 | // Add new score to the table |
| 713 | factoryScores[dst] = 0; |
| 714 | if (topChoice == ITensorHandleFactory::LegacyFactoryId) |
| 715 | { |
| 716 | topChoice = dst; |
| 717 | } |
| 718 | } |
| 719 | else |
| 720 | { |
| 721 | // Increase the score |
| 722 | factoryScores[dst]++; |
| 723 | |
| 724 | // Track the best option |
| 725 | if (factoryScores[dst] > topScore) |
| 726 | { |
| 727 | topScore = factoryScores[dst]; |
| 728 | topChoice = dst; |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | return topChoice; |
| 735 | } |
| 736 | |
| 737 | // Find the handle factory for the output layer which results in fewest required copies. |
| 738 | ITensorHandleFactory::FactoryId CalculateSlotOptionForOutput(BackendsMap& backends, |
| 739 | OutputSlot& slot, |
| 740 | TensorHandleFactoryRegistry& registry) |
| 741 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 742 | IgnoreUnused(backends, slot, registry); |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 743 | return ITensorHandleFactory::DeferredFactoryId; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | // For all handle factories supported on the source backend, we wish to find the one which requires the fewest copies |
| 747 | // when considering all connections. |
| 748 | ITensorHandleFactory::FactoryId CalculateSlotOption(BackendsMap& backends, |
| 749 | OutputSlot& outputSlot, |
| 750 | TensorHandleFactoryRegistry& registry) |
| 751 | { |
| 752 | // First ensure the from backends can support the TensorHandeAPI |
| 753 | Layer& layer = outputSlot.GetOwningLayer(); |
| 754 | auto frmBackend = backends.find(layer.GetBackendId()); |
| 755 | if (frmBackend == backends.end() || |
| 756 | !frmBackend->second->SupportsTensorAllocatorAPI()) |
| 757 | { |
| 758 | return ITensorHandleFactory::LegacyFactoryId; |
| 759 | } |
| 760 | |
| 761 | // Connections to Output Layers requires support for map/unmap on the TensorHandle. |
| 762 | bool requiresMapUnmap = false; |
| 763 | for (auto&& connection : outputSlot.GetConnections()) |
| 764 | { |
| 765 | const Layer& connectedLayer = connection->GetOwningLayer(); |
| 766 | if (connectedLayer.GetType() == LayerType::Output) |
| 767 | { |
| 768 | requiresMapUnmap = true; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | IBackendInternal* srcBackend = frmBackend->second.get(); |
| 773 | auto srcPrefs = srcBackend->GetHandleFactoryPreferences(); |
| 774 | |
| 775 | // Initialize the scores |
| 776 | std::map<ITensorHandleFactory::FactoryId, int> factoryScores; |
| 777 | for (auto&& pref : srcPrefs) |
| 778 | { |
| 779 | if (requiresMapUnmap) // Only consider factories that support map/unmap if required |
| 780 | { |
| 781 | ITensorHandleFactory* factory = registry.GetFactory(pref); |
| 782 | if (!factory->SupportsMapUnmap()) |
| 783 | { |
| 784 | // The current tensor handle factory does not support the map/unmap strategy, move to the next one |
| 785 | continue; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | auto it = factoryScores.find(pref); |
| 790 | if (it == factoryScores.end()) |
| 791 | { |
| 792 | // Add new score to the table |
| 793 | factoryScores[pref] = 0; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | // Score each handle factory based on how many times it requires copies on the slot connections |
| 798 | for (auto&& connection : outputSlot.GetConnections()) |
| 799 | { |
| 800 | const Layer& connectedLayer = connection->GetOwningLayer(); |
| 801 | |
| 802 | auto toBackend = backends.find(connectedLayer.GetBackendId()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 803 | 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] | 804 | |
| 805 | auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences(); |
| 806 | for (auto&& src : srcPrefs) |
| 807 | { |
| 808 | if (factoryScores.find(src) == factoryScores.end()) // Don't consider excluded factories |
| 809 | { |
| 810 | continue; |
| 811 | } |
| 812 | |
| 813 | for (auto&& dst : dstPrefs) |
| 814 | { |
| 815 | if (RequiresCopy(src, dst, registry)) |
| 816 | { |
| 817 | // Copy avoided, increase the score |
| 818 | factoryScores[src]++; |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | // Find the lowest score |
| 826 | int minScore = std::numeric_limits<int>::max(); |
| 827 | for (auto it : factoryScores) |
| 828 | { |
| 829 | minScore = std::min(minScore, it.second); |
| 830 | } |
| 831 | |
| 832 | // Collect factories matching the best(lowest) score |
| 833 | std::vector<ITensorHandleFactory::FactoryId> optimalFactories; |
| 834 | for (auto it : factoryScores) |
| 835 | { |
| 836 | if (it.second == minScore) |
| 837 | { |
| 838 | optimalFactories.push_back(it.first); |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | // For all compatible Factories matching the best score, find the preferred one for the current layer. |
| 843 | for (auto&& srcPref : srcPrefs) |
| 844 | { |
| 845 | for (auto&& comp : optimalFactories) |
| 846 | { |
| 847 | if (comp == srcPref) |
| 848 | { |
| 849 | return comp; |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | return ITensorHandleFactory::LegacyFactoryId; |
| 855 | } |
| 856 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 857 | EdgeStrategy CalculateEdgeStrategy(BackendsMap& backends, |
| 858 | ITensorHandleFactory::FactoryId srcFactoryId, |
| 859 | const Layer& layer, |
| 860 | const Layer& connectedLayer, |
Narumol Prangnawarat | a2493a0 | 2020-08-19 14:39:07 +0100 | [diff] [blame] | 861 | TensorHandleFactoryRegistry& registry, |
| 862 | bool importEnabled) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 863 | { |
| 864 | auto toBackend = backends.find(connectedLayer.GetBackendId()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 865 | 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] | 866 | |
| 867 | auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences(); |
| 868 | |
| 869 | // Legacy API check for backward compatibility |
| 870 | if (srcFactoryId == ITensorHandleFactory::LegacyFactoryId || dstPrefs.empty()) |
| 871 | { |
| 872 | if (layer.GetBackendId() != connectedLayer.GetBackendId()) |
| 873 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 874 | return EdgeStrategy::CopyToTarget; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 875 | } |
| 876 | else |
| 877 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 878 | return EdgeStrategy::DirectCompatibility; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
| 882 | // TensorHandleFactory API present, so perform more sophisticated strategies. |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 883 | // 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] | 884 | if (connectedLayer.GetType() == LayerType::Output) |
| 885 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 886 | return EdgeStrategy::DirectCompatibility; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | // Search for direct match in prefs |
| 890 | for (auto&& pref : dstPrefs) |
| 891 | { |
| 892 | if (pref == srcFactoryId) |
| 893 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 894 | return EdgeStrategy::DirectCompatibility; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
| 898 | // Search for export/import options |
| 899 | ITensorHandleFactory* srcFactory = registry.GetFactory(srcFactoryId); |
Narumol Prangnawarat | a2493a0 | 2020-08-19 14:39:07 +0100 | [diff] [blame] | 900 | if (srcFactory->GetExportFlags() != 0 && importEnabled) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 901 | { |
| 902 | for (auto&& pref : dstPrefs) |
| 903 | { |
| 904 | ITensorHandleFactory* dstFactory = registry.GetFactory(pref); |
James Conroy | ffab16f | 2019-11-07 14:37:09 +0000 | [diff] [blame] | 905 | |
James Conroy | 47e863d | 2019-11-18 17:07:43 +0000 | [diff] [blame] | 906 | // Handles cases when a destPref is not listed in TensorHandleFactoryRegistry |
James Conroy | ffab16f | 2019-11-07 14:37:09 +0000 | [diff] [blame] | 907 | if (!dstFactory) { |
James Conroy | 47e863d | 2019-11-18 17:07:43 +0000 | [diff] [blame] | 908 | continue; |
James Conroy | ffab16f | 2019-11-07 14:37:09 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 911 | if ((dstFactory->GetImportFlags() & srcFactory->GetExportFlags()) != 0) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 912 | { |
Narumol Prangnawarat | b8d771a | 2020-08-14 11:51:12 +0100 | [diff] [blame] | 913 | auto srcCapability = srcFactory->GetCapabilities(&layer, &layer, CapabilityClass::PaddingRequired); |
| 914 | auto dstCapability = dstFactory->GetCapabilities(&connectedLayer, |
| 915 | &connectedLayer, |
| 916 | CapabilityClass::PaddingRequired); |
| 917 | // Do not require memory copy if the source and destination do not require padding. |
| 918 | if (srcCapability.empty() && dstCapability.empty()) |
| 919 | { |
| 920 | return EdgeStrategy::ExportToTarget; |
| 921 | } |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | // Search for copy options via map/unmap |
| 927 | if (srcFactory->SupportsMapUnmap()) |
| 928 | { |
| 929 | for (auto&& pref : dstPrefs) |
| 930 | { |
| 931 | ITensorHandleFactory* dstFactory = registry.GetFactory(pref); |
James Conroy | 47e863d | 2019-11-18 17:07:43 +0000 | [diff] [blame] | 932 | if (dstFactory && dstFactory->SupportsMapUnmap()) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 933 | { |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 934 | return EdgeStrategy::CopyToTarget; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 939 | return EdgeStrategy::Undefined; |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | // Select the TensorHandleFactories and the corresponding memory strategy |
| 943 | OptimizationResult SelectTensorHandleStrategy(Graph& optGraph, |
| 944 | BackendsMap& backends, |
| 945 | TensorHandleFactoryRegistry& registry, |
Narumol Prangnawarat | a2493a0 | 2020-08-19 14:39:07 +0100 | [diff] [blame] | 946 | bool importEnabled, |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 947 | Optional<std::vector<std::string>&> errMessages) |
| 948 | { |
| 949 | OptimizationResult result; |
| 950 | |
Narumol Prangnawarat | a2493a0 | 2020-08-19 14:39:07 +0100 | [diff] [blame] | 951 | optGraph.ForEachLayer([&backends, ®istry, &result, &errMessages, importEnabled](Layer* layer) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 952 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 953 | ARMNN_ASSERT(layer); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 954 | |
| 955 | // Lets make sure the backend is in our list of supported backends. Something went wrong during backend |
| 956 | // assignment if this check fails |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 957 | ARMNN_ASSERT(backends.find(layer->GetBackendId()) != backends.end()); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 958 | |
| 959 | // Check each output separately |
| 960 | for (unsigned int slotIdx = 0; slotIdx < layer->GetNumOutputSlots(); slotIdx++) |
| 961 | { |
| 962 | OutputSlot& outputSlot = layer->GetOutputSlot(slotIdx); |
| 963 | |
| 964 | ITensorHandleFactory::FactoryId slotOption = ITensorHandleFactory::LegacyFactoryId; |
| 965 | |
| 966 | // Calculate the factory to use which results in the fewest copies being made. |
| 967 | switch(layer->GetType()) |
| 968 | { |
| 969 | case LayerType::Input: |
| 970 | slotOption = CalculateSlotOptionForInput(backends, outputSlot, registry); |
| 971 | break; |
| 972 | case LayerType::Output: |
| 973 | slotOption = CalculateSlotOptionForOutput(backends, outputSlot, registry); |
| 974 | break; |
| 975 | default: |
| 976 | slotOption = CalculateSlotOption(backends, outputSlot, registry); |
| 977 | break; |
| 978 | } |
| 979 | outputSlot.SetTensorHandleFactory(slotOption); |
| 980 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 981 | // Now determine the "best" edge strategy for each connection given the slotOption. |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 982 | unsigned int connectionIdx = 0; |
| 983 | for (auto&& connection : outputSlot.GetConnections()) |
| 984 | { |
| 985 | const Layer& connectedLayer = connection->GetOwningLayer(); |
| 986 | |
Narumol Prangnawarat | a2493a0 | 2020-08-19 14:39:07 +0100 | [diff] [blame] | 987 | EdgeStrategy strategy = CalculateEdgeStrategy(backends, slotOption, *layer, connectedLayer, |
| 988 | registry, importEnabled); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 989 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 990 | if (strategy == EdgeStrategy::Undefined) |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 991 | { |
| 992 | result.m_Error = true; |
| 993 | if (errMessages) |
| 994 | { |
| 995 | errMessages.value().emplace_back("Could not find valid strategy required for compatibility" |
| 996 | " between backends."); |
| 997 | } |
| 998 | return; |
| 999 | } |
| 1000 | |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1001 | outputSlot.SetEdgeStrategy(connectionIdx, strategy); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1002 | |
| 1003 | connectionIdx++; |
| 1004 | } |
| 1005 | } |
| 1006 | }); |
| 1007 | |
| 1008 | return result; |
| 1009 | } |
| 1010 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1011 | IOptimizedNetworkPtr Optimize(const INetwork& inNetwork, |
| 1012 | const std::vector<BackendId>& backendPreferences, |
| 1013 | const IDeviceSpec& deviceSpec, |
| 1014 | const OptimizerOptions& options, |
Rob Hughes | 2321443 | 2019-11-05 11:27:36 +0000 | [diff] [blame] | 1015 | Optional<std::vector<std::string>&> messages) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1016 | { |
| 1017 | if (backendPreferences.empty()) |
| 1018 | { |
Mike Kelly | 3a613cc | 2020-09-29 20:50:35 +0100 | [diff] [blame] | 1019 | throw InvalidArgumentException("Invoked Optimize with no backends specified"); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 1022 | if (options.m_ReduceFp32ToFp16 && options.m_ReduceFp32ToBf16) |
| 1023 | { |
| 1024 | throw InvalidArgumentException("BFloat16 and Float16 optimization cannot be enabled at the same time."); |
| 1025 | } |
| 1026 | |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 1027 | const Network& network = *PolymorphicDowncast<const Network*>(&inNetwork); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1028 | std::unique_ptr<Graph> graph = std::make_unique<Graph>(network.GetGraph()); |
| 1029 | |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 1030 | auto optNet = IOptimizedNetworkPtr(new OptimizedNetwork(std::move(graph), options.m_ModelOptions), |
| 1031 | &IOptimizedNetwork::Destroy); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1032 | |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 1033 | OptimizedNetwork* optNetObjPtr = PolymorphicDowncast<OptimizedNetwork*>(optNet.get()); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1034 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1035 | // Get the optimized graph |
| 1036 | Graph& optGraph = optNetObjPtr->GetGraph(); |
| 1037 | |
Narumol Prangnawarat | 16f82f9 | 2020-09-14 16:12:44 +0100 | [diff] [blame] | 1038 | // Perform AddBroadcastReshapeLayer optimisation |
| 1039 | using namespace optimizations; |
| 1040 | Optimizer::Pass(optGraph, MakeOptimizations(AddBroadcastReshapeLayer())); |
| 1041 | |
Narumol Prangnawarat | bbf71a6 | 2020-09-07 14:05:22 +0100 | [diff] [blame] | 1042 | // Infer the tensor infos for all output slots. Throws an exception on failure |
| 1043 | optGraph.InferTensorInfos(); |
| 1044 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1045 | // Perform optimisation passes |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1046 | Optimizer::Pass(optGraph, MakeOptimizations(SquashEqualPermuteSiblings(), |
Mike Kelly | 490b7be | 2020-03-03 12:39:09 +0000 | [diff] [blame] | 1047 | SquashEqualTransposeSiblings(), |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1048 | SquashEqualReshapeSiblings(), |
| 1049 | OptimizeInversePermutes(), |
Mike Kelly | 490b7be | 2020-03-03 12:39:09 +0000 | [diff] [blame] | 1050 | OptimizeInverseTransposes(), |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1051 | MovePermuteUp(), |
Mike Kelly | 490b7be | 2020-03-03 12:39:09 +0000 | [diff] [blame] | 1052 | MoveTransposeUp(), |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1053 | PermuteAsReshape(), |
Mike Kelly | 490b7be | 2020-03-03 12:39:09 +0000 | [diff] [blame] | 1054 | TransposeAsReshape(), |
Nina Drozd | 861985f | 2019-04-18 14:48:51 +0100 | [diff] [blame] | 1055 | OptimizeConsecutiveReshapes(), |
Rob Hughes | 3a7d3a7 | 2019-09-24 16:59:56 +0100 | [diff] [blame] | 1056 | FoldPadIntoConvolution2d(), |
Mike Kelly | 490b7be | 2020-03-03 12:39:09 +0000 | [diff] [blame] | 1057 | PermuteAndBatchToSpaceAsDepthToSpace(), |
Teresa Charlin | 06e0300 | 2020-10-15 13:16:07 +0100 | [diff] [blame] | 1058 | TransposeAndBatchToSpaceAsDepthToSpace(), |
Mike Kelly | 90231b8 | 2020-11-05 15:44:56 +0000 | [diff] [blame] | 1059 | FuseBatchNormIntoConvolution2DFloat32(), |
| 1060 | FuseBatchNormIntoConvolution2DFloat16(), |
| 1061 | FuseBatchNormIntoDepthwiseConvolution2DFloat32(), |
| 1062 | FuseBatchNormIntoDepthwiseConvolution2DFloat16())); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1063 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1064 | // If Fp32 to Fp16 optimization is set convert Fp32 network to Fp16 |
| 1065 | if (options.m_ReduceFp32ToFp16) |
| 1066 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1067 | Optimizer::Pass(optGraph, MakeOptimizations(Fp32NetworkToFp16Converter())); |
Derek Lamberti | dd6804b | 2019-11-27 09:29:57 +0000 | [diff] [blame] | 1068 | Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsFloatToHalf())); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 1071 | // If Fp32 to Bf16 optimization is set convert Fp32 network to Bf16 |
Narumol Prangnawarat | 57ef008 | 2020-03-26 09:20:43 +0000 | [diff] [blame] | 1072 | // Convert input of Convolution2d and FullyConnected from Fp32 to Bf16 |
| 1073 | // Only Constant weight of Convolution2d and FullyConnected are converted from Fp32 to Bf16 |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 1074 | if (options.m_ReduceFp32ToBf16) |
| 1075 | { |
| 1076 | Optimizer::Pass(optGraph, MakeOptimizations(Fp32NetworkToBf16Converter())); |
Narumol Prangnawarat | bc7ffb5 | 2020-03-20 15:01:01 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1079 | // Initialize backend settings |
| 1080 | BackendSettings backendSettings(backendPreferences, deviceSpec); |
| 1081 | if (backendSettings.GetAvailablePreferredBackends().empty()) |
| 1082 | { |
| 1083 | std::stringstream failureMsg; |
| 1084 | failureMsg << "None of the preferred backends " << backendPreferences |
| 1085 | << " are supported. Current platform provides " << backendSettings.m_SupportedBackends; |
Rob Hughes | 2321443 | 2019-11-05 11:27:36 +0000 | [diff] [blame] | 1086 | ReportError(failureMsg.str(), messages); |
Mike Kelly | 3a613cc | 2020-09-29 20:50:35 +0100 | [diff] [blame] | 1087 | throw InvalidArgumentException(failureMsg.str()); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1090 | // Create a map to temporarily hold initialized backend objects |
| 1091 | TensorHandleFactoryRegistry tensorHandleFactoryRegistry; |
| 1092 | BackendsMap backends = CreateSupportedBackends(tensorHandleFactoryRegistry, backendSettings); |
| 1093 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1094 | // Assign an available backend to each layer |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1095 | Graph::Iterator firstLayer = optGraph.begin(); |
| 1096 | Graph::Iterator lastLayer = optGraph.end(); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1097 | OptimizationResult assignBackendsResult = AssignBackends(optNetObjPtr, |
| 1098 | backendSettings, |
| 1099 | firstLayer, |
| 1100 | lastLayer, |
Rob Hughes | 2321443 | 2019-11-05 11:27:36 +0000 | [diff] [blame] | 1101 | messages); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1102 | if (assignBackendsResult.m_Error) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1103 | { |
| 1104 | // Failed to assign a backend to each layer |
Mike Kelly | 3a613cc | 2020-09-29 20:50:35 +0100 | [diff] [blame] | 1105 | throw InvalidArgumentException("Failed to assign a backend to each layer"); |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 1106 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1107 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1108 | Optimizer::Pass(optGraph, MakeOptimizations(OptimizeInverseConversionsFp16(), |
| 1109 | OptimizeInverseConversionsFp32())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1110 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1111 | // Apply the backend-specific optimizations |
| 1112 | OptimizationResult backendOptimizationResult = ApplyBackendOptimizations(optNetObjPtr, |
| 1113 | backendSettings, |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1114 | backends, |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame^] | 1115 | options.m_ModelOptions, |
Rob Hughes | 2321443 | 2019-11-05 11:27:36 +0000 | [diff] [blame] | 1116 | messages); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1117 | if (backendOptimizationResult.m_Error) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1118 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1119 | // Failed to apply the backend-specific optimizations |
Mike Kelly | 3a613cc | 2020-09-29 20:50:35 +0100 | [diff] [blame] | 1120 | throw InvalidArgumentException("Failed to apply the backend-specific optimizations"); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1123 | // If the debug flag is set, then insert a DebugLayer after each layer |
| 1124 | // Doing this after applying the backend optimizations as they might have changed some layers |
| 1125 | if (options.m_Debug) |
| 1126 | { |
| 1127 | Optimizer::Pass(optGraph, MakeOptimizations(InsertDebugLayer())); |
| 1128 | } |
| 1129 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1130 | // Calculate the compatibility strategies for tensor handles |
| 1131 | OptimizationResult strategyResult = SelectTensorHandleStrategy(optGraph, |
| 1132 | backends, |
| 1133 | tensorHandleFactoryRegistry, |
Narumol Prangnawarat | a2493a0 | 2020-08-19 14:39:07 +0100 | [diff] [blame] | 1134 | options.m_ImportEnabled, |
Rob Hughes | 2321443 | 2019-11-05 11:27:36 +0000 | [diff] [blame] | 1135 | messages); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1136 | if (strategyResult.m_Error) |
| 1137 | { |
| 1138 | // Failed to apply the backend-specific optimizations |
| 1139 | return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy); |
| 1140 | } |
| 1141 | |
| 1142 | // Based on the tensor handle strategy determined above, insert copy layers where required. |
Derek Lamberti | f674aa0 | 2019-08-01 15:56:25 +0100 | [diff] [blame] | 1143 | optGraph.AddCompatibilityLayers(backends, tensorHandleFactoryRegistry); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1144 | |
| 1145 | // Convert constants |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 1146 | Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsFloatToHalf())); |
| 1147 | Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsHalfToFloat())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1148 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 1149 | // Run backend specific optimizations (deprecated) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 1150 | for (auto&& chosenBackend : backendSettings.m_SelectedBackends) |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 1151 | { |
| 1152 | auto factoryFun = BackendRegistryInstance().GetFactory(chosenBackend); |
| 1153 | auto backendPtr = factoryFun(); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1154 | ARMNN_ASSERT(backendPtr.get() != nullptr); |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 1155 | |
Matteo Martincigh | ed73504 | 2019-05-22 09:42:43 +0100 | [diff] [blame] | 1156 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 1157 | auto backendSpecificOptimizations = backendPtr->GetOptimizations(); |
Matteo Martincigh | ed73504 | 2019-05-22 09:42:43 +0100 | [diff] [blame] | 1158 | ARMNN_NO_DEPRECATE_WARN_END |
| 1159 | |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 1160 | if (!backendSpecificOptimizations.empty()) |
| 1161 | { |
| 1162 | Optimizer::Pass(optNetObjPtr->GetGraph(), backendSpecificOptimizations); |
| 1163 | } |
| 1164 | } |
| 1165 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1166 | return optNet; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1167 | } |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 1168 | bool Network::GetShapeInferenceMethod() |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1169 | { |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 1170 | if (m_NetworkOptions.size() > 0 && m_NetworkOptions[0].GetBackendId().Get() == "ShapeInferenceMethod") |
| 1171 | { |
| 1172 | return m_NetworkOptions[0].GetOption(0).GetValue().AsBool(); |
| 1173 | } |
| 1174 | |
| 1175 | return false; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1176 | } |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 1177 | Network::Network(NetworkOptions networkOptions) |
| 1178 | : m_NetworkOptions(networkOptions), |
| 1179 | m_Graph(std::make_unique<Graph>(GetShapeInferenceMethod())) |
| 1180 | {} |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1181 | |
| 1182 | Network::~Network() |
| 1183 | { |
| 1184 | } |
| 1185 | |
Jan Eilers | 99d9d4a | 2019-11-06 10:02:16 +0000 | [diff] [blame] | 1186 | Status Network::PrintGraph() |
| 1187 | { |
| 1188 | m_Graph->Print(); |
| 1189 | return Status::Success; |
| 1190 | } |
| 1191 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1192 | IConnectableLayer* Network::AddInputLayer(LayerBindingId id, const char* name) |
| 1193 | { |
| 1194 | return m_Graph->AddLayer<InputLayer>(id, name); |
| 1195 | } |
| 1196 | |
Éanna Ó Catháin | 4e1e136 | 2018-11-12 11:36:34 +0000 | [diff] [blame] | 1197 | IConnectableLayer* Network::AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor, |
| 1198 | const char* name) |
| 1199 | { |
| 1200 | return m_Graph->AddLayer<BatchToSpaceNdLayer>(batchToSpaceNdDescriptor, name); |
| 1201 | } |
| 1202 | |
Aron Virginas-Tar | 77bfb5e | 2019-10-16 17:45:38 +0100 | [diff] [blame] | 1203 | IConnectableLayer* Network::AddComparisonLayer(const ComparisonDescriptor& comparisonDescriptor, |
| 1204 | const char* name) |
| 1205 | { |
| 1206 | return m_Graph->AddLayer<ComparisonLayer>(comparisonDescriptor, name); |
| 1207 | } |
| 1208 | |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 1209 | IConnectableLayer* Network::AddElementwiseUnaryLayer(const ElementwiseUnaryDescriptor& elementwiseUnaryDescriptor, |
| 1210 | const char* name) |
| 1211 | { |
| 1212 | return m_Graph->AddLayer<ElementwiseUnaryLayer>(elementwiseUnaryDescriptor, name); |
| 1213 | } |
| 1214 | |
Ryan OShea | ec6c680 | 2020-06-05 17:17:06 +0100 | [diff] [blame] | 1215 | IConnectableLayer* Network::AddFillLayer(const FillDescriptor& fillDescriptor, |
| 1216 | const char* name) |
| 1217 | { |
| 1218 | return m_Graph->AddLayer<FillLayer>(fillDescriptor, name); |
| 1219 | } |
| 1220 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1221 | IConnectableLayer* Network::AddFullyConnectedLayerImpl(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1222 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1223 | const Optional<ConstTensor>& biases, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1224 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1225 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1226 | if (fullyConnectedDescriptor.m_BiasEnabled && !biases.has_value()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1227 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1228 | throw InvalidArgumentException("AddFullyConnectedLayer: biases cannot be empty"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | const auto layer = m_Graph->AddLayer<FullyConnectedLayer>(fullyConnectedDescriptor, name); |
| 1232 | |
| 1233 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 1234 | |
| 1235 | if (fullyConnectedDescriptor.m_BiasEnabled) |
| 1236 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1237 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | return layer; |
| 1241 | } |
| 1242 | |
| 1243 | IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1244 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1245 | const Optional<ConstTensor>& biases, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1246 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1247 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1248 | return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, biases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1251 | IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
| 1252 | const ConstTensor& weights, |
| 1253 | const char* name) |
| 1254 | { |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1255 | Optional<ConstTensor> biases; |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1256 | return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, biases, name); |
| 1257 | } |
| 1258 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1259 | IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1260 | const ConstTensor& weights, |
| 1261 | const ConstTensor& biases, |
| 1262 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1263 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1264 | Optional<ConstTensor> optionalBiases(biases); |
| 1265 | return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, optionalBiases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 1268 | IConnectableLayer* Network::AddConcatLayer(const ConcatDescriptor& concatDescriptor, |
Jim Flynn | 906f946 | 2019-05-10 13:55:21 +0100 | [diff] [blame] | 1269 | const char* name) |
| 1270 | { |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 1271 | return m_Graph->AddLayer<ConcatLayer>(concatDescriptor, name); |
Jim Flynn | 906f946 | 2019-05-10 13:55:21 +0100 | [diff] [blame] | 1272 | } |
| 1273 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1274 | IConnectableLayer* Network::AddConvolution2dLayerImpl(const Convolution2dDescriptor& convolution2dDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1275 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1276 | const Optional<ConstTensor>& biases, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1277 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1278 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1279 | if (convolution2dDescriptor.m_BiasEnabled && !biases.has_value()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1280 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1281 | throw InvalidArgumentException("AddConvolution2dLayer: biases cannot be empty"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | const auto layer = m_Graph->AddLayer<Convolution2dLayer>(convolution2dDescriptor, name); |
| 1285 | |
| 1286 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 1287 | |
| 1288 | if (convolution2dDescriptor.m_BiasEnabled) |
| 1289 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1290 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | return layer; |
| 1294 | } |
| 1295 | |
| 1296 | IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1297 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1298 | const Optional<ConstTensor>& biases, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1299 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1300 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1301 | return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1302 | } |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1303 | |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1304 | IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, |
| 1305 | const ConstTensor& weights, |
| 1306 | const char* name) |
| 1307 | { |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1308 | Optional<ConstTensor> biases; |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1309 | return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name); |
| 1310 | } |
| 1311 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1312 | IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1313 | const ConstTensor& weights, |
| 1314 | const ConstTensor& biases, |
| 1315 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1316 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1317 | Optional<ConstTensor> optionalBiases(biases); |
| 1318 | return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, optionalBiases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayerImpl( |
| 1322 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 1323 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1324 | const Optional<ConstTensor>& biases, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1325 | const char* name) |
| 1326 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1327 | if (convolution2dDescriptor.m_BiasEnabled && !biases.has_value()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1328 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1329 | throw InvalidArgumentException("AddDepthwiseConvolution2dLayer: biases cannot be empty"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Matteo Martincigh | 3d6898c | 2019-01-15 16:11:44 +0000 | [diff] [blame] | 1332 | const auto layer = m_Graph->AddLayer<DepthwiseConvolution2dLayer>(convolution2dDescriptor, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1333 | |
| 1334 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 1335 | |
| 1336 | if (convolution2dDescriptor.m_BiasEnabled) |
| 1337 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1338 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | return layer; |
| 1342 | } |
| 1343 | |
Aron Virginas-Tar | dd6247f | 2019-09-19 14:31:17 +0100 | [diff] [blame] | 1344 | IConnectableLayer* Network::AddDepthToSpaceLayer(const DepthToSpaceDescriptor& depthToSpaceDescriptor, |
| 1345 | const char* name) |
| 1346 | { |
| 1347 | return m_Graph->AddLayer<DepthToSpaceLayer>(depthToSpaceDescriptor, name); |
| 1348 | } |
| 1349 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1350 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayer( |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1351 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 1352 | const ConstTensor& weights, |
| 1353 | const Optional<ConstTensor>& biases, |
| 1354 | const char* name) |
| 1355 | { |
| 1356 | return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name); |
| 1357 | } |
| 1358 | |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1359 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayer( |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1360 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 1361 | const ConstTensor& weights, |
| 1362 | const char* name) |
| 1363 | { |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1364 | Optional<ConstTensor> biases; |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1365 | return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1366 | } |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1367 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1368 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayer( |
| 1369 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 1370 | const ConstTensor& weights, |
| 1371 | const ConstTensor& biases, |
| 1372 | const char* name) |
| 1373 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1374 | Optional<ConstTensor> optionalBiases(biases); |
| 1375 | return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, optionalBiases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
Narumol Prangnawarat | 94dd5d8 | 2019-01-23 18:06:26 +0000 | [diff] [blame] | 1378 | IConnectableLayer* Network::AddDetectionPostProcessLayer(const armnn::DetectionPostProcessDescriptor& descriptor, |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 1379 | const ConstTensor& anchors, const char* name) |
Narumol Prangnawarat | 94dd5d8 | 2019-01-23 18:06:26 +0000 | [diff] [blame] | 1380 | { |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 1381 | const auto layer = m_Graph->AddLayer<DetectionPostProcessLayer>(descriptor, name); |
| 1382 | |
| 1383 | layer->m_Anchors = std::make_unique<ScopedCpuTensorHandle>(anchors); |
| 1384 | |
| 1385 | return layer; |
Narumol Prangnawarat | 94dd5d8 | 2019-01-23 18:06:26 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1388 | IConnectableLayer* Network::AddPermuteLayer(const PermuteDescriptor& permuteDescriptor, |
| 1389 | const char* name) |
| 1390 | { |
| 1391 | return m_Graph->AddLayer<PermuteLayer>(permuteDescriptor, name); |
| 1392 | } |
| 1393 | |
| 1394 | IConnectableLayer* Network::AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor, |
| 1395 | const char* name) |
| 1396 | { |
| 1397 | return m_Graph->AddLayer<Pooling2dLayer>(pooling2dDescriptor, name); |
| 1398 | } |
| 1399 | |
| 1400 | IConnectableLayer* Network::AddActivationLayer(const ActivationDescriptor& activationDescriptor, |
| 1401 | const char* name) |
| 1402 | { |
| 1403 | return m_Graph->AddLayer<ActivationLayer>(activationDescriptor, name); |
| 1404 | } |
| 1405 | |
Nikhil Raj | ee391d5 | 2019-09-05 17:50:44 +0100 | [diff] [blame] | 1406 | IConnectableLayer* Network::AddArgMinMaxLayer(const ArgMinMaxDescriptor& argMinMaxDescriptor, |
| 1407 | const char* name) |
| 1408 | { |
| 1409 | return m_Graph->AddLayer<ArgMinMaxLayer>(argMinMaxDescriptor, name); |
| 1410 | } |
| 1411 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1412 | IConnectableLayer* Network::AddNormalizationLayer(const NormalizationDescriptor& |
| 1413 | normalizationDescriptor, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1414 | const char* name) |
| 1415 | { |
| 1416 | return m_Graph->AddLayer<NormalizationLayer>(normalizationDescriptor, name); |
| 1417 | } |
| 1418 | |
Aron Virginas-Tar | 636ab40 | 2019-09-16 14:27:45 +0100 | [diff] [blame] | 1419 | IConnectableLayer* Network::AddSliceLayer(const SliceDescriptor& sliceDescriptor, const char* name) |
| 1420 | { |
| 1421 | return m_Graph->AddLayer<SliceLayer>(sliceDescriptor, name); |
| 1422 | } |
| 1423 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1424 | IConnectableLayer* Network::AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor, |
| 1425 | const char* name) |
| 1426 | { |
| 1427 | return m_Graph->AddLayer<SoftmaxLayer>(softmaxDescriptor, name); |
| 1428 | } |
| 1429 | |
| 1430 | IConnectableLayer* Network::AddSplitterLayer(const ViewsDescriptor& splitterDescriptor, |
| 1431 | const char* name) |
| 1432 | { |
| 1433 | return m_Graph->AddLayer<SplitterLayer>(splitterDescriptor, name); |
| 1434 | } |
| 1435 | |
Nattapat Chaimanowong | 5a4304a | 2018-11-28 10:44:37 +0000 | [diff] [blame] | 1436 | IConnectableLayer* Network::AddMaximumLayer(const char* name) |
| 1437 | { |
| 1438 | return m_Graph->AddLayer<MaximumLayer>(name); |
| 1439 | } |
| 1440 | |
Éanna Ó Catháin | 20e5880 | 2018-12-04 10:29:06 +0000 | [diff] [blame] | 1441 | IConnectableLayer* Network::AddMinimumLayer(const char* name) |
| 1442 | { |
| 1443 | return m_Graph->AddLayer<MinimumLayer>(name); |
| 1444 | } |
| 1445 | |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 1446 | IConnectableLayer* Network::AddMergerLayer(const MergerDescriptor& mergerDescriptor, |
Jim Flynn | 906f946 | 2019-05-10 13:55:21 +0100 | [diff] [blame] | 1447 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1448 | { |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 1449 | return AddConcatLayer(mergerDescriptor, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Kevin May | 868eb14 | 2019-09-04 17:29:31 +0100 | [diff] [blame] | 1452 | IConnectableLayer* Network::AddAbsLayer(const char * name) |
| 1453 | { |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 1454 | return AddElementwiseUnaryLayer(ElementwiseUnaryDescriptor(UnaryOperation::Abs), name); |
Kevin May | 868eb14 | 2019-09-04 17:29:31 +0100 | [diff] [blame] | 1455 | } |
| 1456 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1457 | IConnectableLayer* Network::AddAdditionLayer(const char* name) |
| 1458 | { |
| 1459 | return m_Graph->AddLayer<AdditionLayer>(name); |
| 1460 | } |
| 1461 | |
| 1462 | IConnectableLayer* Network::AddMultiplicationLayer(const char* name) |
| 1463 | { |
| 1464 | return m_Graph->AddLayer<MultiplicationLayer>(name); |
| 1465 | } |
| 1466 | |
| 1467 | IConnectableLayer* Network::AddOutputLayer(LayerBindingId id, const char* name) |
| 1468 | { |
| 1469 | return m_Graph->AddLayer<OutputLayer>(id, name); |
| 1470 | } |
| 1471 | |
| 1472 | IConnectableLayer* Network::AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc, |
| 1473 | const ConstTensor& mean, |
| 1474 | const ConstTensor& variance, |
| 1475 | const ConstTensor& beta, |
| 1476 | const ConstTensor& gamma, |
| 1477 | const char* name) |
| 1478 | { |
| 1479 | const auto layer = m_Graph->AddLayer<BatchNormalizationLayer>(desc, name); |
| 1480 | |
| 1481 | layer->m_Mean = std::make_unique<ScopedCpuTensorHandle>(mean); |
| 1482 | layer->m_Variance = std::make_unique<ScopedCpuTensorHandle>(variance); |
| 1483 | layer->m_Beta = std::make_unique<ScopedCpuTensorHandle>(beta); |
| 1484 | layer->m_Gamma = std::make_unique<ScopedCpuTensorHandle>(gamma); |
| 1485 | |
| 1486 | return layer; |
| 1487 | } |
| 1488 | |
Finn Williams | 2605b23 | 2020-06-10 15:53:46 +0100 | [diff] [blame] | 1489 | IConnectableLayer* Network::AddRankLayer(const char* name) |
| 1490 | { |
| 1491 | return m_Graph->AddLayer<RankLayer>(name); |
| 1492 | } |
| 1493 | |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 1494 | IConnectableLayer* Network::AddResizeBilinearLayer(const ResizeBilinearDescriptor& descriptor, |
| 1495 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1496 | { |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 1497 | ResizeDescriptor resizeDescriptor; |
David Monahan | 4a0c9b9 | 2020-05-30 09:48:39 +0100 | [diff] [blame] | 1498 | resizeDescriptor.m_Method = ResizeMethod::Bilinear; |
| 1499 | resizeDescriptor.m_DataLayout = descriptor.m_DataLayout; |
| 1500 | resizeDescriptor.m_TargetWidth = descriptor.m_TargetWidth; |
| 1501 | resizeDescriptor.m_TargetHeight = descriptor.m_TargetHeight; |
| 1502 | resizeDescriptor.m_AlignCorners = descriptor.m_AlignCorners; |
| 1503 | resizeDescriptor.m_HalfPixelCenters = descriptor.m_HalfPixelCenters; |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 1504 | |
| 1505 | return m_Graph->AddLayer<ResizeLayer>(resizeDescriptor, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Teresa Charlin | a9075df | 2019-06-27 15:41:57 +0100 | [diff] [blame] | 1508 | IConnectableLayer* Network::AddResizeLayer(const ResizeDescriptor& |
| 1509 | resizeDescriptor, const char* name) |
| 1510 | { |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 1511 | return m_Graph->AddLayer<ResizeLayer>(resizeDescriptor, name); |
Teresa Charlin | a9075df | 2019-06-27 15:41:57 +0100 | [diff] [blame] | 1512 | } |
| 1513 | |
Kevin May | ce5045a | 2019-10-02 14:07:47 +0100 | [diff] [blame] | 1514 | IConnectableLayer* Network::AddInstanceNormalizationLayer(const InstanceNormalizationDescriptor& desc, |
| 1515 | const char* name) |
| 1516 | { |
| 1517 | return m_Graph->AddLayer<InstanceNormalizationLayer>(desc, name); |
| 1518 | } |
| 1519 | |
Matteo Martincigh | bcd3c85 | 2018-09-28 14:14:12 +0100 | [diff] [blame] | 1520 | IConnectableLayer* Network::AddL2NormalizationLayer(const L2NormalizationDescriptor& desc, |
| 1521 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1522 | { |
Matteo Martincigh | bcd3c85 | 2018-09-28 14:14:12 +0100 | [diff] [blame] | 1523 | return m_Graph->AddLayer<L2NormalizationLayer>(desc, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
Aron Virginas-Tar | f982dea | 2019-10-11 14:07:53 +0100 | [diff] [blame] | 1526 | IConnectableLayer* Network::AddLogSoftmaxLayer(const LogSoftmaxDescriptor& desc, |
| 1527 | const char* name) |
| 1528 | { |
| 1529 | return m_Graph->AddLayer<LogSoftmaxLayer>(desc, name); |
| 1530 | } |
| 1531 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1532 | IConnectableLayer* Network::AddConstantLayer(const ConstTensor& input, const char* name) |
| 1533 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1534 | auto layer = m_Graph->AddLayer<ConstantLayer>(name); |
| 1535 | |
| 1536 | layer->m_LayerOutput = std::make_unique<ScopedCpuTensorHandle>(input); |
| 1537 | |
| 1538 | return layer; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1541 | IConnectableLayer* Network::AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor, |
| 1542 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1543 | { |
| 1544 | return m_Graph->AddLayer<ReshapeLayer>(reshapeDescriptor, name); |
| 1545 | } |
| 1546 | |
Nattapat Chaimanowong | 207ef9a | 2018-11-02 10:57:25 +0000 | [diff] [blame] | 1547 | IConnectableLayer* Network::AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor, |
| 1548 | const char* name) |
| 1549 | { |
| 1550 | return m_Graph->AddLayer<SpaceToBatchNdLayer>(spaceToBatchNdDescriptor, name); |
| 1551 | } |
| 1552 | |
Aron Virginas-Tar | 972af15 | 2019-06-11 14:14:03 +0100 | [diff] [blame] | 1553 | IConnectableLayer* Network::AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor, |
| 1554 | const char* name) |
| 1555 | { |
| 1556 | return m_Graph->AddLayer<SpaceToDepthLayer>(spaceToDepthDescriptor, name); |
| 1557 | } |
| 1558 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1559 | IConnectableLayer* Network::AddFloorLayer(const char* name) |
| 1560 | { |
| 1561 | return m_Graph->AddLayer<FloorLayer>(name); |
| 1562 | } |
| 1563 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1564 | IConnectableLayer* Network::AddLstmLayer(const LstmDescriptor& descriptor, |
| 1565 | const LstmInputParams& params, |
| 1566 | const char* name) |
| 1567 | { |
| 1568 | const auto layer = m_Graph->AddLayer<LstmLayer>(descriptor, name); |
| 1569 | |
| 1570 | //Lstm Basic Parameters |
| 1571 | layer->m_BasicParameters.m_InputToForgetWeights = |
| 1572 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToForgetWeights)); |
| 1573 | layer->m_BasicParameters.m_InputToCellWeights = |
| 1574 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToCellWeights)); |
| 1575 | layer->m_BasicParameters.m_InputToOutputWeights = |
| 1576 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToOutputWeights)); |
| 1577 | layer->m_BasicParameters.m_RecurrentToForgetWeights = |
| 1578 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToForgetWeights)); |
| 1579 | layer->m_BasicParameters.m_RecurrentToCellWeights = |
| 1580 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToCellWeights)); |
| 1581 | layer->m_BasicParameters.m_RecurrentToOutputWeights = |
| 1582 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToOutputWeights)); |
| 1583 | layer->m_BasicParameters.m_ForgetGateBias = |
| 1584 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetGateBias)); |
| 1585 | layer->m_BasicParameters.m_CellBias = |
| 1586 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellBias)); |
| 1587 | layer->m_BasicParameters.m_OutputGateBias = |
| 1588 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputGateBias)); |
| 1589 | |
| 1590 | //Lstm Cifg parameters |
| 1591 | if(!descriptor.m_CifgEnabled) |
| 1592 | { |
| 1593 | if(params.m_InputToInputWeights == nullptr) |
| 1594 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 1595 | throw InvalidArgumentException("AddLstmLayer: Input To Input Weights cannot be NULL " |
| 1596 | "when CIFG is disabled."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1597 | } |
| 1598 | if(params.m_RecurrentToInputWeights == nullptr) |
| 1599 | { |
| 1600 | throw InvalidArgumentException( |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 1601 | "AddLstmLayer: Recurrent To Input Weights cannot be NULL " |
| 1602 | "when CIFG is disabled."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1603 | } |
| 1604 | if(params.m_InputGateBias == nullptr) |
| 1605 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 1606 | throw InvalidArgumentException("AddLstmLayer: Input Gate Bias cannot be NULL " |
| 1607 | "when CIFG is disabled."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1608 | } |
| 1609 | layer->m_CifgParameters.m_InputToInputWeights = |
| 1610 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToInputWeights)); |
| 1611 | layer->m_CifgParameters.m_RecurrentToInputWeights = |
| 1612 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToInputWeights)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1613 | layer->m_CifgParameters.m_InputGateBias = |
| 1614 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputGateBias)); |
| 1615 | } |
| 1616 | |
| 1617 | //Lstm projection parameters |
| 1618 | if(descriptor.m_ProjectionEnabled) |
| 1619 | { |
| 1620 | if(params.m_ProjectionWeights == nullptr) |
| 1621 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 1622 | throw InvalidArgumentException("AddLstmLayer: Projection Weights cannot be NULL " |
| 1623 | "when projection is enabled."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1624 | } |
| 1625 | layer->m_ProjectionParameters.m_ProjectionWeights = |
| 1626 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionWeights)); |
| 1627 | if(params.m_ProjectionBias != nullptr) |
| 1628 | { |
| 1629 | layer->m_ProjectionParameters.m_ProjectionBias = |
| 1630 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionBias)); |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | //Lstm Peephole params |
| 1635 | if(descriptor.m_PeepholeEnabled) |
| 1636 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 1637 | if(!descriptor.m_CifgEnabled) |
| 1638 | { |
| 1639 | if(params.m_CellToInputWeights == nullptr) |
| 1640 | { |
| 1641 | throw InvalidArgumentException("AddLstmLayer: Cell To Input Weights cannot be NULL " |
| 1642 | "when Peephole is enabled and CIFG disabled."); |
| 1643 | } |
| 1644 | |
| 1645 | layer->m_PeepholeParameters.m_CellToInputWeights = |
| 1646 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToInputWeights)); |
| 1647 | } |
| 1648 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1649 | if(params.m_CellToForgetWeights == nullptr) |
| 1650 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 1651 | throw InvalidArgumentException("AddLstmLayer: Cell To Forget Weights cannot be NULL " |
| 1652 | "when Peephole is enabled."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1653 | } |
| 1654 | if(params.m_CellToOutputWeights == nullptr) |
| 1655 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 1656 | throw InvalidArgumentException("AddLstmLayer: Cell To Output Weights cannot be NULL " |
| 1657 | "when Peephole is enabled."); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1658 | } |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 1659 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1660 | layer->m_PeepholeParameters.m_CellToForgetWeights = |
| 1661 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToForgetWeights)); |
| 1662 | layer->m_PeepholeParameters.m_CellToOutputWeights = |
| 1663 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToOutputWeights)); |
| 1664 | } |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 1665 | |
| 1666 | //Lstm Layer Normalization params |
| 1667 | if(descriptor.m_LayerNormEnabled) |
| 1668 | { |
| 1669 | if(!descriptor.m_CifgEnabled) |
| 1670 | { |
| 1671 | if(params.m_InputLayerNormWeights == nullptr) |
| 1672 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 1673 | throw InvalidArgumentException("AddLstmLayer: Input layer normalization weights cannot be NULL " |
| 1674 | "when layer normalization is enabled and CIFG disabled."); |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 1675 | } |
| 1676 | layer->m_LayerNormParameters.m_InputLayerNormWeights = |
| 1677 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputLayerNormWeights)); |
| 1678 | } |
| 1679 | |
| 1680 | if(params.m_ForgetLayerNormWeights == nullptr) |
| 1681 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 1682 | throw InvalidArgumentException("AddLstmLayer: Forget layer normalization weights cannot be NULL " |
| 1683 | "when layer normalization is enabled."); |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 1684 | } |
| 1685 | if(params.m_CellLayerNormWeights == nullptr) |
| 1686 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 1687 | throw InvalidArgumentException("AddLstmLayer: Cell layer normalization weights cannot be NULL " |
| 1688 | "when layer normalization is enabled."); |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 1689 | } |
| 1690 | if(params.m_OutputLayerNormWeights == nullptr) |
| 1691 | { |
Jan Eilers | e2062cd | 2020-03-30 15:07:45 +0100 | [diff] [blame] | 1692 | throw InvalidArgumentException("AddLstmLayer: Output layer normalization weights cannot be NULL " |
| 1693 | "when layer normalization is enabled."); |
Jan Eilers | f8c6297 | 2019-07-17 11:07:49 +0100 | [diff] [blame] | 1694 | } |
| 1695 | layer->m_LayerNormParameters.m_ForgetLayerNormWeights = |
| 1696 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetLayerNormWeights)); |
| 1697 | layer->m_LayerNormParameters.m_CellLayerNormWeights = |
| 1698 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellLayerNormWeights)); |
| 1699 | layer->m_LayerNormParameters.m_OutputLayerNormWeights = |
| 1700 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputLayerNormWeights)); |
| 1701 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1702 | return layer; |
| 1703 | } |
| 1704 | |
Francis Murtagh | e7a86a4 | 2018-08-29 12:42:10 +0100 | [diff] [blame] | 1705 | IConnectableLayer* Network::AddDivisionLayer(const char* name) |
| 1706 | { |
| 1707 | return m_Graph->AddLayer<DivisionLayer>(name); |
| 1708 | } |
| 1709 | |
David Beck | 1952622 | 2018-09-12 16:00:08 +0100 | [diff] [blame] | 1710 | IConnectableLayer* Network::AddSubtractionLayer(const char* name) |
| 1711 | { |
| 1712 | return m_Graph->AddLayer<SubtractionLayer>(name); |
| 1713 | } |
| 1714 | |
narpra01 | 32b9046 | 2018-09-13 11:07:48 +0100 | [diff] [blame] | 1715 | IConnectableLayer* Network::AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name) |
| 1716 | { |
| 1717 | return m_Graph->AddLayer<MeanLayer>(meanDescriptor,name); |
| 1718 | } |
| 1719 | |
Mohamed Nour Abouelseoud | 5662c20 | 2018-09-24 13:30:09 +0100 | [diff] [blame] | 1720 | IConnectableLayer* Network::AddPadLayer(const PadDescriptor& padDescriptor, const char* name) |
| 1721 | { |
| 1722 | return m_Graph->AddLayer<PadLayer>(padDescriptor,name); |
| 1723 | } |
| 1724 | |
Derek Lamberti | a9cca6a | 2019-03-25 15:41:58 +0000 | [diff] [blame] | 1725 | IConnectableLayer *Network::AddQuantizeLayer(const char *name) |
| 1726 | { |
| 1727 | return m_Graph->AddLayer<QuantizeLayer>(name); |
| 1728 | } |
| 1729 | |
Nattapat Chaimanowong | e4294fd | 2019-03-28 09:56:53 +0000 | [diff] [blame] | 1730 | IConnectableLayer* Network::AddDequantizeLayer(const char* name) |
| 1731 | { |
| 1732 | return m_Graph->AddLayer<DequantizeLayer>(name); |
| 1733 | } |
| 1734 | |
Conor Kennedy | 430b5d8 | 2018-11-14 15:28:28 +0000 | [diff] [blame] | 1735 | IConnectableLayer* Network::AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor, |
| 1736 | const char* name) |
| 1737 | { |
| 1738 | return m_Graph->AddLayer<StridedSliceLayer>(stridedSliceDescriptor, name); |
| 1739 | } |
| 1740 | |
Matteo Martincigh | 59a950c | 2018-12-13 12:48:25 +0000 | [diff] [blame] | 1741 | IConnectableLayer* Network::AddGreaterLayer(const char* name) |
| 1742 | { |
Aron Virginas-Tar | 77bfb5e | 2019-10-16 17:45:38 +0100 | [diff] [blame] | 1743 | return AddComparisonLayer(ComparisonDescriptor(ComparisonOperation::Greater), name); |
Matteo Martincigh | 59a950c | 2018-12-13 12:48:25 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
FrancisMurtagh | 2099595 | 2018-12-17 12:11:36 +0000 | [diff] [blame] | 1746 | IConnectableLayer* Network::AddEqualLayer(const char* name) |
| 1747 | { |
Aron Virginas-Tar | 77bfb5e | 2019-10-16 17:45:38 +0100 | [diff] [blame] | 1748 | return AddComparisonLayer(ComparisonDescriptor(ComparisonOperation::Equal), name); |
FrancisMurtagh | 2099595 | 2018-12-17 12:11:36 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Mohamed Nour Abouelseoud | a1d3c6a | 2018-12-27 12:39:16 +0000 | [diff] [blame] | 1751 | IConnectableLayer* Network::AddRsqrtLayer(const char * name) |
| 1752 | { |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 1753 | return AddElementwiseUnaryLayer(ElementwiseUnaryDescriptor(UnaryOperation::Rsqrt), name); |
Mohamed Nour Abouelseoud | a1d3c6a | 2018-12-27 12:39:16 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
narpra01 | b89b05f | 2019-01-16 09:53:09 +0000 | [diff] [blame] | 1756 | IConnectableLayer* Network::AddGatherLayer(const char* name) |
| 1757 | { |
Teresa Charlin | 5266473 | 2020-06-29 16:27:03 +0100 | [diff] [blame] | 1758 | GatherDescriptor gatherDescriptor{}; |
| 1759 | return AddGatherLayer(gatherDescriptor, name); |
| 1760 | } |
| 1761 | |
| 1762 | IConnectableLayer* Network::AddGatherLayer(const GatherDescriptor& gatherDescriptor, |
| 1763 | const char* name) |
| 1764 | { |
| 1765 | return m_Graph->AddLayer<GatherLayer>(gatherDescriptor, name); |
narpra01 | b89b05f | 2019-01-16 09:53:09 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Nattapat Chaimanowong | 1f88630 | 2019-04-05 13:37:19 +0100 | [diff] [blame] | 1768 | IConnectableLayer* Network::AddMergeLayer(const char* name) |
| 1769 | { |
| 1770 | return m_Graph->AddLayer<MergeLayer>(name); |
| 1771 | } |
| 1772 | |
Sadik Armagan | eff363d | 2019-04-05 15:25:46 +0100 | [diff] [blame] | 1773 | IConnectableLayer* Network::AddSwitchLayer(const char* name) |
| 1774 | { |
| 1775 | return m_Graph->AddLayer<SwitchLayer>(name); |
| 1776 | } |
| 1777 | |
Matteo Martincigh | 0e406ee | 2019-06-12 15:42:18 +0100 | [diff] [blame] | 1778 | IConnectableLayer* Network::AddPreluLayer(const char* name) |
| 1779 | { |
| 1780 | return m_Graph->AddLayer<PreluLayer>(name); |
| 1781 | } |
| 1782 | |
Aron Virginas-Tar | 639fb04 | 2019-06-20 14:28:19 +0100 | [diff] [blame] | 1783 | IConnectableLayer* Network::AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor, |
| 1784 | const ConstTensor& weights, |
| 1785 | const Optional<ConstTensor>& biases, |
| 1786 | const char* name) |
| 1787 | { |
| 1788 | if (descriptor.m_BiasEnabled && !biases.has_value()) |
| 1789 | { |
| 1790 | throw InvalidArgumentException("AddTransposeConvolution2dLayer: Biases cannot be empty"); |
| 1791 | } |
| 1792 | |
| 1793 | const auto layer = m_Graph->AddLayer<TransposeConvolution2dLayer>(descriptor, name); |
| 1794 | |
| 1795 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 1796 | |
| 1797 | if (descriptor.m_BiasEnabled) |
| 1798 | { |
| 1799 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value()); |
| 1800 | } |
| 1801 | |
| 1802 | return layer; |
| 1803 | } |
| 1804 | |
Mike Kelly | c9ea45a | 2020-02-28 18:11:58 +0000 | [diff] [blame] | 1805 | IConnectableLayer* Network::AddTransposeLayer(const TransposeDescriptor& transposeDescriptor, |
| 1806 | const char* name) |
| 1807 | { |
| 1808 | return m_Graph->AddLayer<TransposeLayer>(transposeDescriptor, name); |
| 1809 | } |
| 1810 | |
Matthew Jackson | 2b8c1da | 2019-07-04 14:59:16 +0100 | [diff] [blame] | 1811 | IConnectableLayer* Network::AddStackLayer(const StackDescriptor& stackDescriptor, |
| 1812 | const char* name) |
| 1813 | { |
| 1814 | return m_Graph->AddLayer<StackLayer>(stackDescriptor, name); |
| 1815 | } |
| 1816 | |
Derek Lamberti | 013c390 | 2019-10-21 10:46:16 +0100 | [diff] [blame] | 1817 | |
| 1818 | IConnectableLayer* Network::AddStandInLayer(const StandInDescriptor& desc, |
| 1819 | const char* name) |
| 1820 | { |
| 1821 | return m_Graph->AddLayer<StandInLayer>(desc, name); |
| 1822 | } |
| 1823 | |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 1824 | IConnectableLayer* Network::AddQuantizedLstmLayer(const QuantizedLstmInputParams& params, |
| 1825 | const char* name) |
| 1826 | { |
| 1827 | const auto layer = m_Graph->AddLayer<QuantizedLstmLayer>(name); |
| 1828 | |
| 1829 | // InputToX weights |
| 1830 | layer->m_QuantizedLstmParameters.m_InputToInputWeights = |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 1831 | std::make_unique<ScopedCpuTensorHandle>(params.GetInputToInputWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 1832 | layer->m_QuantizedLstmParameters.m_InputToForgetWeights = |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 1833 | std::make_unique<ScopedCpuTensorHandle>(params.GetInputToForgetWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 1834 | layer->m_QuantizedLstmParameters.m_InputToCellWeights = |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 1835 | std::make_unique<ScopedCpuTensorHandle>(params.GetInputToCellWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 1836 | layer->m_QuantizedLstmParameters.m_InputToOutputWeights = |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 1837 | std::make_unique<ScopedCpuTensorHandle>(params.GetInputToOutputWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 1838 | |
| 1839 | // RecurrentToX weights |
| 1840 | layer->m_QuantizedLstmParameters.m_RecurrentToInputWeights = |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 1841 | std::make_unique<ScopedCpuTensorHandle>(params.GetRecurrentToInputWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 1842 | layer->m_QuantizedLstmParameters.m_RecurrentToForgetWeights = |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 1843 | std::make_unique<ScopedCpuTensorHandle>(params.GetRecurrentToForgetWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 1844 | layer->m_QuantizedLstmParameters.m_RecurrentToCellWeights = |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 1845 | std::make_unique<ScopedCpuTensorHandle>(params.GetRecurrentToCellWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 1846 | layer->m_QuantizedLstmParameters.m_RecurrentToOutputWeights = |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 1847 | std::make_unique<ScopedCpuTensorHandle>(params.GetRecurrentToOutputWeights()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 1848 | |
| 1849 | // Bias |
| 1850 | layer->m_QuantizedLstmParameters.m_InputGateBias = |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 1851 | std::make_unique<ScopedCpuTensorHandle>(params.GetInputGateBias()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 1852 | layer->m_QuantizedLstmParameters.m_ForgetGateBias = |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 1853 | std::make_unique<ScopedCpuTensorHandle>(params.GetForgetGateBias()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 1854 | layer->m_QuantizedLstmParameters.m_CellBias = |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 1855 | std::make_unique<ScopedCpuTensorHandle>(params.GetCellBias()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 1856 | layer->m_QuantizedLstmParameters.m_OutputGateBias = |
Francis Murtagh | bb590b4 | 2019-08-14 09:51:36 +0100 | [diff] [blame] | 1857 | std::make_unique<ScopedCpuTensorHandle>(params.GetOutputGateBias()); |
James Conroy | ee18dc8 | 2019-07-17 11:27:46 +0100 | [diff] [blame] | 1858 | |
| 1859 | return layer; |
| 1860 | } |
| 1861 | |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 1862 | IConnectableLayer* Network::AddQLstmLayer(const QLstmDescriptor& descriptor, |
| 1863 | const LstmInputParams& params, |
| 1864 | const char* name) |
| 1865 | { |
| 1866 | const auto layer = m_Graph->AddLayer<QLstmLayer>(descriptor, name); |
| 1867 | |
| 1868 | // QLstm Basic Parameters |
| 1869 | layer->m_BasicParameters.m_InputToForgetWeights = |
| 1870 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToForgetWeights)); |
| 1871 | layer->m_BasicParameters.m_InputToCellWeights = |
| 1872 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToCellWeights)); |
| 1873 | layer->m_BasicParameters.m_InputToOutputWeights = |
| 1874 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToOutputWeights)); |
| 1875 | layer->m_BasicParameters.m_RecurrentToForgetWeights = |
| 1876 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToForgetWeights)); |
| 1877 | layer->m_BasicParameters.m_RecurrentToCellWeights = |
| 1878 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToCellWeights)); |
| 1879 | layer->m_BasicParameters.m_RecurrentToOutputWeights = |
| 1880 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToOutputWeights)); |
| 1881 | layer->m_BasicParameters.m_ForgetGateBias = |
| 1882 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetGateBias)); |
| 1883 | layer->m_BasicParameters.m_CellBias = |
| 1884 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellBias)); |
| 1885 | layer->m_BasicParameters.m_OutputGateBias = |
| 1886 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputGateBias)); |
| 1887 | |
| 1888 | // QLstm Cifg parameters |
| 1889 | if(!descriptor.m_CifgEnabled) |
| 1890 | { |
| 1891 | if(params.m_InputToInputWeights == nullptr) |
| 1892 | { |
| 1893 | throw InvalidArgumentException("AddQLstmLayer: Input To Input Weights cannot be NULL"); |
| 1894 | } |
| 1895 | |
| 1896 | if(params.m_RecurrentToInputWeights == nullptr) |
| 1897 | { |
| 1898 | throw InvalidArgumentException( |
| 1899 | "AddQLstmLayer: Recurrent To Input Weights cannot be NULL"); |
| 1900 | } |
| 1901 | |
| 1902 | if(params.m_InputGateBias == nullptr) |
| 1903 | { |
| 1904 | throw InvalidArgumentException("AddQLstmLayer: Input Gate Bias cannot be NULL"); |
| 1905 | } |
| 1906 | |
| 1907 | layer->m_CifgParameters.m_InputToInputWeights = |
| 1908 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToInputWeights)); |
| 1909 | layer->m_CifgParameters.m_RecurrentToInputWeights = |
| 1910 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToInputWeights)); |
| 1911 | layer->m_CifgParameters.m_InputGateBias = |
| 1912 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputGateBias)); |
| 1913 | } |
| 1914 | |
| 1915 | // QLstm Projection parameters |
| 1916 | if(descriptor.m_ProjectionEnabled) |
| 1917 | { |
| 1918 | if(params.m_ProjectionWeights == nullptr) |
| 1919 | { |
| 1920 | throw InvalidArgumentException("AddQLstmLayer: Projection Weights cannot be NULL"); |
| 1921 | } |
| 1922 | |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 1923 | layer->m_ProjectionParameters.m_ProjectionWeights = |
| 1924 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionWeights)); |
James Conroy | ed32405 | 2020-05-18 15:16:42 +0100 | [diff] [blame] | 1925 | |
| 1926 | // Projection bias is optional even if projection is enabled |
| 1927 | if(params.m_ProjectionWeights != nullptr) |
| 1928 | { |
| 1929 | layer->m_ProjectionParameters.m_ProjectionBias = |
| 1930 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionBias)); |
| 1931 | } |
| 1932 | |
James Conroy | 586a9aa | 2020-03-20 08:49:33 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | // QLstm Peephole params |
| 1936 | if(descriptor.m_PeepholeEnabled) |
| 1937 | { |
| 1938 | if(params.m_CellToForgetWeights == nullptr) |
| 1939 | { |
| 1940 | throw InvalidArgumentException("AddQLstmLayer: Cell To Forget Weights cannot be NULL"); |
| 1941 | } |
| 1942 | |
| 1943 | if(params.m_CellToOutputWeights == nullptr) |
| 1944 | { |
| 1945 | throw InvalidArgumentException("AddQLstmLayer: Cell To Output Weights cannot be NULL"); |
| 1946 | } |
| 1947 | |
| 1948 | if(!descriptor.m_CifgEnabled) |
| 1949 | { |
| 1950 | if(params.m_CellToInputWeights == nullptr) |
| 1951 | { |
| 1952 | throw InvalidArgumentException("AddQLstmLayer: Cell To Input Weights cannot be NULL"); |
| 1953 | } |
| 1954 | |
| 1955 | layer->m_PeepholeParameters.m_CellToInputWeights = |
| 1956 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToInputWeights)); |
| 1957 | } |
| 1958 | |
| 1959 | layer->m_PeepholeParameters.m_CellToForgetWeights = |
| 1960 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToForgetWeights)); |
| 1961 | layer->m_PeepholeParameters.m_CellToOutputWeights = |
| 1962 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToOutputWeights)); |
| 1963 | } |
| 1964 | |
| 1965 | // QLstm Layer Normalization params |
| 1966 | if(descriptor.m_LayerNormEnabled) |
| 1967 | { |
| 1968 | if(params.m_ForgetLayerNormWeights == nullptr) |
| 1969 | { |
| 1970 | throw InvalidArgumentException("AddQLstmLayer: Forget layer normalization weights cannot be NULL"); |
| 1971 | } |
| 1972 | |
| 1973 | if(params.m_CellLayerNormWeights == nullptr) |
| 1974 | { |
| 1975 | throw InvalidArgumentException("AddQLstmLayer: Cell layer normalization weights cannot be NULL"); |
| 1976 | } |
| 1977 | |
| 1978 | if(params.m_OutputLayerNormWeights == nullptr) |
| 1979 | { |
| 1980 | throw InvalidArgumentException("AddQLstmLayer: Output layer normalization weights cannot be NULL"); |
| 1981 | } |
| 1982 | |
| 1983 | if(!descriptor.m_CifgEnabled) |
| 1984 | { |
| 1985 | if(params.m_InputLayerNormWeights == nullptr) |
| 1986 | { |
| 1987 | throw InvalidArgumentException("AddQLstmLayer: Input layer normalization weights cannot be NULL"); |
| 1988 | } |
| 1989 | |
| 1990 | layer->m_LayerNormParameters.m_InputLayerNormWeights = |
| 1991 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputLayerNormWeights)); |
| 1992 | } |
| 1993 | |
| 1994 | layer->m_LayerNormParameters.m_ForgetLayerNormWeights = |
| 1995 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetLayerNormWeights)); |
| 1996 | layer->m_LayerNormParameters.m_CellLayerNormWeights = |
| 1997 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellLayerNormWeights)); |
| 1998 | layer->m_LayerNormParameters.m_OutputLayerNormWeights = |
| 1999 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputLayerNormWeights)); |
| 2000 | } |
| 2001 | return layer; |
| 2002 | } |
| 2003 | |
James Conroy | aba90cd | 2020-11-06 16:28:18 +0000 | [diff] [blame] | 2004 | IConnectableLayer* Network::AddLogicalBinaryLayer(const LogicalBinaryDescriptor& logicalBinaryDescriptor, |
| 2005 | const char* name) |
| 2006 | { |
| 2007 | return m_Graph->AddLayer<LogicalBinaryLayer>(logicalBinaryDescriptor, name); |
| 2008 | } |
| 2009 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 2010 | void Network::Accept(ILayerVisitor& visitor) const |
| 2011 | { |
| 2012 | for (auto layer : GetGraph()) |
| 2013 | { |
| 2014 | layer->Accept(visitor); |
| 2015 | }; |
| 2016 | } |
| 2017 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2018 | OptimizedNetwork::OptimizedNetwork(std::unique_ptr<Graph> graph) |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 2019 | : m_Graph(std::move(graph)), m_Guid(profiling::ProfilingService::GetNextGuid()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2020 | { |
| 2021 | } |
| 2022 | |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 2023 | OptimizedNetwork::OptimizedNetwork(std::unique_ptr<Graph> graph, const ModelOptions& modelOptions) |
| 2024 | : m_Graph(std::move(graph)), m_Guid(profiling::ProfilingService::GetNextGuid()), m_ModelOptions(modelOptions) |
| 2025 | { |
| 2026 | } |
| 2027 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 2028 | OptimizedNetwork::~OptimizedNetwork() |
| 2029 | { |
| 2030 | } |
| 2031 | |
| 2032 | } // namespace armnn |