telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. 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> |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 17 | #include <backendsCommon/BackendRegistry.hpp> |
| 18 | #include <backendsCommon/IBackendInternal.hpp> |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 19 | #include <backendsCommon/TensorHandleFactoryRegistry.hpp> |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 20 | |
| 21 | #include <armnn/Exceptions.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 22 | #include <armnn/Utils.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 23 | #include <armnn/TypesUtils.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 24 | |
| 25 | #include <fcntl.h> |
| 26 | #include <algorithm> |
| 27 | #include <fstream> |
| 28 | #include <memory> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 29 | #include <vector> |
| 30 | #include <algorithm> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 31 | |
| 32 | #include <boost/assert.hpp> |
| 33 | #include <boost/format.hpp> |
| 34 | #include <boost/log/trivial.hpp> |
| 35 | #include <boost/numeric/conversion/converter_policies.hpp> |
| 36 | #include <boost/cast.hpp> |
| 37 | |
| 38 | namespace armnn |
| 39 | { |
| 40 | |
| 41 | armnn::INetwork* INetwork::CreateRaw() |
| 42 | { |
| 43 | return new Network(); |
| 44 | } |
| 45 | |
| 46 | armnn::INetworkPtr INetwork::Create() |
| 47 | { |
| 48 | return INetworkPtr(CreateRaw(), &INetwork::Destroy); |
| 49 | } |
| 50 | |
| 51 | void INetwork::Destroy(INetwork* network) |
| 52 | { |
| 53 | delete boost::polymorphic_downcast<Network*>(network); |
| 54 | } |
| 55 | |
| 56 | Status Network::PrintGraph() |
| 57 | { |
| 58 | m_Graph->Print(); |
| 59 | return Status::Success; |
| 60 | } |
| 61 | |
| 62 | void IOptimizedNetwork::Destroy(IOptimizedNetwork* network) |
| 63 | { |
| 64 | delete boost::polymorphic_downcast<OptimizedNetwork*>(network); |
| 65 | } |
| 66 | |
| 67 | Status OptimizedNetwork::PrintGraph() |
| 68 | { |
| 69 | m_Graph->Print(); |
| 70 | return Status::Success; |
| 71 | } |
| 72 | |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 73 | Status OptimizedNetwork::SerializeToDot(std::ostream& stream) const |
| 74 | { |
| 75 | return m_Graph->SerializeToDot(stream); |
| 76 | } |
| 77 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 78 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 79 | |
| 80 | void ReportError(const std::string& errorMessage, |
| 81 | Optional<std::vector<std::string>&> errorMessages) |
| 82 | { |
| 83 | std::stringstream fullErrorMessage; |
| 84 | fullErrorMessage << "ERROR: " << errorMessage; |
| 85 | BOOST_LOG_TRIVIAL(warning) << fullErrorMessage.str(); |
| 86 | if (errorMessages) |
| 87 | { |
| 88 | errorMessages.value().push_back(fullErrorMessage.str()); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void ReportWarning(const std::string& warningMessage, |
| 93 | Optional<std::vector<std::string>&> warningMessages) |
| 94 | { |
| 95 | std::stringstream fullWarningMessage; |
| 96 | fullWarningMessage << "WARNING: " << warningMessage; |
| 97 | BOOST_LOG_TRIVIAL(warning) << fullWarningMessage.str(); |
| 98 | if (warningMessages) |
| 99 | { |
| 100 | warningMessages.value().push_back(fullWarningMessage.str()); |
| 101 | } |
| 102 | } |
| 103 | |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 104 | bool CheckScaleSetOnQuantizedType(Layer* layer, Optional<std::vector<std::string>&> errMessages) |
| 105 | { |
| 106 | bool noErrors = true; |
| 107 | unsigned int numOutputs = layer->GetNumOutputSlots(); |
| 108 | for (unsigned int i = 0; i < numOutputs; i++) { |
David Monahan | b855470 | 2019-04-25 16:03:38 +0100 | [diff] [blame] | 109 | OutputSlot& outputSlot = layer->GetOutputSlot(i); |
| 110 | TensorInfo info = outputSlot.GetTensorInfo(); |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 111 | if (DataType::QuantisedAsymm8 == info.GetDataType()) { |
| 112 | if (0.f == info.GetQuantizationScale()) { |
| 113 | noErrors = false; |
| 114 | std::stringstream ss; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 115 | ss << "output " << i << " of layer " << GetLayerTypeAsCString(layer->GetType()) |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 116 | << " (" << layer->GetNameStr() << ") is of type" |
| 117 | << " Quantized 8 bit but its scale parameter has not been set"; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 118 | ReportError(ss.str(), errMessages); |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 119 | } |
David Monahan | b855470 | 2019-04-25 16:03:38 +0100 | [diff] [blame] | 120 | // Softmax under QuantisedAsymm8 must always be scale (1.0f/256.0f) and offset 0 |
| 121 | if ((info.GetQuantizationScale() != (1.0f / 256.0f) || |
| 122 | info.GetQuantizationOffset() != 0) && |
| 123 | layer->GetType() == armnn::LayerType::Softmax) |
| 124 | { |
| 125 | std::stringstream ss; |
| 126 | ss << "Quantization parameters for Softmax layer (Scale: " << |
| 127 | info.GetQuantizationScale() << " and Offset: " << info.GetQuantizationOffset() << |
| 128 | ") are incorrect and have been updated to Scale: 0.00390625 and Offset: 0"; |
| 129 | BOOST_LOG_TRIVIAL(warning) << ss.str(); |
| 130 | info.SetQuantizationScale((1.0f /256.0f)); |
| 131 | info.SetQuantizationOffset(0); |
| 132 | outputSlot.SetTensorInfo(info); |
| 133 | } |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | return noErrors; |
| 137 | } |
| 138 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 139 | OptimizationResult AssignBackends(OptimizedNetwork* optNetObjPtr, |
| 140 | BackendSettings& backendSettings, |
| 141 | Graph::Iterator& firstLayer, |
| 142 | Graph::Iterator& lastLayer, |
| 143 | Optional<std::vector<std::string>&> errMessages) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 144 | { |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 145 | OptimizationResult result; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 146 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 147 | // Helper lambda to compose meaningful error message before returning with error |
| 148 | auto ReturnWithError = [&](const Layer* layer) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 149 | { |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 150 | std::stringstream failureMsg; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 151 | failureMsg << "Layer of type " << GetLayerTypeAsCString(layer->GetType()) |
| 152 | << " is not supported on any preferred backend " << backendSettings.m_PreferredBackends; |
| 153 | ReportError(failureMsg.str(), errMessages); |
| 154 | |
| 155 | result.m_Error = true; |
| 156 | return result; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 157 | }; |
| 158 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 159 | auto availablePreferredBackends = backendSettings.GetAvailablePreferredBackends(); |
| 160 | if (availablePreferredBackends.empty()) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 161 | { |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 162 | std::stringstream failureMsg; |
| 163 | failureMsg << "No preferred backends are available"; |
| 164 | ReportError(failureMsg.str(), errMessages); |
| 165 | |
| 166 | result.m_Error = true; |
| 167 | return result; |
| 168 | } |
| 169 | |
| 170 | for (auto it = firstLayer; it != lastLayer; ++it) |
| 171 | { |
| 172 | auto layer = *it; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 173 | DataType dataType = layer->GetDataType(); |
| 174 | std::string reasonIfUnsupported; |
| 175 | bool found = false; |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 176 | if (!CheckScaleSetOnQuantizedType(layer, errMessages)) |
| 177 | { |
| 178 | // don't bomb immediately, find all the quantized outputs |
| 179 | // which haven't had a scale set and report them all back. |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 180 | result.m_Error = true; |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 181 | } |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 182 | |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 183 | for (const auto& backend : availablePreferredBackends) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 184 | { |
| 185 | // need to set the compute device on the layer |
| 186 | // before we can check if it is supported |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 187 | layer->SetBackendId(backend); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 188 | if (!IWorkloadFactory::IsLayerSupported(*layer, dataType, reasonIfUnsupported)) |
| 189 | { |
| 190 | if (dataType == DataType::Float16) |
| 191 | { |
| 192 | if (IWorkloadFactory::IsLayerSupported(*layer, DataType::Float32, reasonIfUnsupported) |
| 193 | && layer->GetType() != LayerType::ConvertFp32ToFp16 |
| 194 | && layer->GetType() != LayerType::ConvertFp16ToFp32) |
| 195 | { |
| 196 | // Insert FP16 -> FP32 conversion layer before current layer |
| 197 | std::vector<ConvertFp16ToFp32Layer*> convertFp16ToFp32Layers = |
| 198 | InsertConvertFp16ToFp32LayersBefore(optNetObjPtr->GetGraph(), *layer); |
| 199 | |
| 200 | // Insert FP32 -> FP16 conversion layer after current layer |
| 201 | std::vector<ConvertFp32ToFp16Layer*> convertFp32ToFp16Layers = |
| 202 | InsertConvertFp32ToFp16LayersAfter(optNetObjPtr->GetGraph(), *layer); |
| 203 | |
| 204 | // Assign a supported backend to the newly introduced conversion layers |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 205 | auto AssignFirstSupportedBackend = [&](Layer* layer, BackendId preferredBackend) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 206 | { |
| 207 | bool supportedBackendFound = false; |
| 208 | std::string reasonIfUnsupported; |
| 209 | |
| 210 | // Try preferred backend first |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 211 | layer->SetBackendId(preferredBackend); |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 212 | if (IWorkloadFactory::IsLayerSupported(*layer, |
| 213 | EmptyOptional(), |
| 214 | reasonIfUnsupported)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 215 | { |
| 216 | supportedBackendFound = true; |
| 217 | } |
| 218 | else |
| 219 | { |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 220 | for (const auto& backend : availablePreferredBackends) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 221 | { |
| 222 | // Skip preferred backend (we already determined that it is not supported) |
| 223 | if (backend == preferredBackend) |
| 224 | { |
| 225 | continue; |
| 226 | } |
| 227 | |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 228 | layer->SetBackendId(backend); |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 229 | if (IWorkloadFactory::IsLayerSupported(*layer, |
| 230 | EmptyOptional(), |
| 231 | reasonIfUnsupported)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 232 | { |
| 233 | supportedBackendFound = true; |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | return supportedBackendFound; |
| 240 | }; |
| 241 | |
| 242 | for (ConvertFp16ToFp32Layer* convertLayer : convertFp16ToFp32Layers) |
| 243 | { |
| 244 | if (!AssignFirstSupportedBackend(convertLayer, backend)) |
| 245 | { |
| 246 | return ReturnWithError(convertLayer); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | for (ConvertFp32ToFp16Layer* convertLayer : convertFp32ToFp16Layers) |
| 251 | { |
| 252 | if (!AssignFirstSupportedBackend(convertLayer, backend)) |
| 253 | { |
| 254 | return ReturnWithError(convertLayer); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | found = true; |
| 259 | break; |
| 260 | } |
| 261 | } |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 262 | std::stringstream warningMsg; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 263 | warningMsg << "Layer of type " << GetLayerTypeAsCString(layer->GetType()) |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 264 | << " is not supported on requested backend " << layer->GetBackendId().Get() |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 265 | << " for data type " << GetDataTypeName(dataType) |
| 266 | << " (reason: " << reasonIfUnsupported |
| 267 | << "), falling back to the next backend."; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 268 | ReportWarning(warningMsg.str(), errMessages); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 269 | } |
| 270 | else |
| 271 | { |
| 272 | found = true; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 273 | backendSettings.m_SelectedBackends.insert(backend); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 274 | break; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // 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] | 279 | if (!found) |
| 280 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 281 | // NOTE: if the layer is not an operation queue type AND we have not got CpuRef as a |
| 282 | // fallback we should set the compute device on the layer to CpuRef (these are not |
| 283 | // available as accelerated operations, or are only available under certain |
| 284 | // conditions, currently they comprise MemCopy, Constant, Permute) |
| 285 | armnn::LayerType layerType = layer->GetType(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 286 | if (!backendSettings.IsCpuRefUsed() && (layerType == armnn::LayerType::MemCopy || |
| 287 | layerType == armnn::LayerType::Constant || |
| 288 | layerType == armnn::LayerType::Permute)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 289 | { |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 290 | BackendId cpuBackendId(armnn::Compute::CpuRef); |
| 291 | layer->SetBackendId(cpuBackendId); |
| 292 | backendSettings.m_SelectedBackends.insert(cpuBackendId); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 293 | } |
| 294 | else |
| 295 | { |
| 296 | return ReturnWithError(layer); |
| 297 | } |
| 298 | } |
| 299 | } |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 300 | |
| 301 | return result; |
| 302 | } |
| 303 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 304 | OptimizationResult AssignBackends(OptimizedNetwork* optNetObjPtr, |
| 305 | BackendSettings& backendSettings, |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 306 | SubgraphView& subgraph, |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 307 | Optional<std::vector<std::string>&> errMessages) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 308 | { |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 309 | Graph::Iterator firstLayer = subgraph.begin(); |
| 310 | Graph::Iterator lastLayer = subgraph.end(); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 311 | return AssignBackends(optNetObjPtr, |
| 312 | backendSettings, |
| 313 | firstLayer, |
| 314 | lastLayer, |
| 315 | errMessages); |
| 316 | } |
| 317 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 318 | BackendsMap CreateSupportedBackends(TensorHandleFactoryRegistry& handleFactoryRegistry, |
| 319 | BackendSettings& backendSettings) |
| 320 | { |
| 321 | BackendsMap backends; |
| 322 | auto const& backendRegistry = BackendRegistryInstance(); |
| 323 | for (auto&& selectedBackend : backendSettings.m_SupportedBackends) |
| 324 | { |
| 325 | auto backendFactory = backendRegistry.GetFactory(selectedBackend); |
| 326 | auto backendObjPtr = backendFactory(); |
| 327 | BOOST_ASSERT(backendObjPtr); |
| 328 | |
| 329 | backendObjPtr->RegisterTensorHandleFactories(handleFactoryRegistry); |
| 330 | |
| 331 | backends[backendObjPtr->GetId()] = std::move(backendObjPtr); |
| 332 | } |
| 333 | |
| 334 | return backends; |
| 335 | } |
| 336 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 337 | OptimizationResult ApplyBackendOptimizations(OptimizedNetwork* optNetObjPtr, |
| 338 | BackendSettings& backendSettings, |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 339 | BackendsMap& backends, |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 340 | Optional<std::vector<std::string>&> errMessages) |
| 341 | { |
| 342 | BOOST_ASSERT(optNetObjPtr); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 343 | |
| 344 | OptimizationResult result; |
| 345 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 346 | // Get the optimized graph |
| 347 | Graph& optGraph = optNetObjPtr->GetGraph(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 348 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 349 | // Get the entire graph as a sub-graph |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 350 | SubgraphView mainSubgraph(optGraph); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 351 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 352 | // Run backend specific optimizations |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 353 | for (auto&& selectedBackend : backendSettings.m_SelectedBackends) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 354 | { |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 355 | auto backendObjPtr = backends.find(selectedBackend)->second.get(); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 356 | BOOST_ASSERT(backendObjPtr); |
| 357 | |
| 358 | // Select sub-graphs based on backend |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 359 | SubgraphViewSelector::Subgraphs subgraphs = |
| 360 | SubgraphViewSelector::SelectSubgraphs(mainSubgraph, |
Matteo Martincigh | 602af09 | 2019-05-01 10:31:27 +0100 | [diff] [blame] | 361 | // Select layers assigned to the requested backend |
| 362 | [&backendObjPtr](const Layer& layer) |
| 363 | { |
| 364 | return layer.GetType() != LayerType::Input && |
| 365 | layer.GetType() != LayerType::Output && |
| 366 | layer.GetBackendId() == backendObjPtr->GetId(); |
| 367 | }); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 368 | if (subgraphs.empty()) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 369 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 370 | // No sub-graphs found, try with next selected backend |
| 371 | continue; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 372 | } |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 373 | |
| 374 | // Try to optimize each sub-graph |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 375 | for (auto& subgraph : subgraphs) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 376 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 377 | // Try to optimize the current sub-graph |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 378 | OptimizationViews optimizationViews = backendObjPtr->OptimizeSubgraphView(*subgraph); |
| 379 | BOOST_ASSERT(optimizationViews.Validate(*subgraph)); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 380 | |
| 381 | // Optimization attempted, check the resulting optimized sub-graph |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 382 | for (auto& substitution : optimizationViews.GetSubstitutions()) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 383 | { |
| 384 | // 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] | 385 | SubgraphView& replacementSubgraph = substitution.m_ReplacementSubgraph; |
| 386 | SubgraphView& substitutableSubgraph = substitution.m_SubstitutableSubgraph; |
| 387 | optGraph.SubstituteSubgraph(substitutableSubgraph, replacementSubgraph); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 388 | |
| 389 | // Assign the current backend to the optimized sub-graph |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 390 | std::for_each(replacementSubgraph.begin(), replacementSubgraph.end(), [&selectedBackend](Layer* l) |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 391 | { |
| 392 | BOOST_ASSERT(l); |
| 393 | l->SetBackendId(selectedBackend); |
| 394 | }); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 395 | } |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 396 | |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 397 | if (!optimizationViews.GetFailedSubgraphs().empty()) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 398 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 399 | std::stringstream warningMsg; |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 400 | warningMsg << "Some sub-graph(s) failed to optimized on " << backendObjPtr->GetId() << " backend."; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 401 | ReportWarning(warningMsg.str(), errMessages); |
| 402 | |
| 403 | // 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] | 404 | BackendSettings settingsCopy(backendSettings); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 405 | if (!backendObjPtr->GetId().IsCpuRef()) |
| 406 | { |
| 407 | // Add the current backend to the list of backends to ignore |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 408 | settingsCopy.m_IgnoredBackends.insert(backendObjPtr->GetId()); |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 409 | } |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 410 | |
| 411 | int count=0; |
Matteo Martincigh | 8492433 | 2019-05-09 12:46:16 +0100 | [diff] [blame] | 412 | for (auto& failedSubgraph : optimizationViews.GetFailedSubgraphs()) |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 413 | { |
Derek Lamberti | c2fe5fb | 2019-05-08 10:23:08 +0100 | [diff] [blame] | 414 | // An error occurred: the optimization was attempted but not performed, try different backends |
| 415 | std::stringstream subgraphMsg; |
| 416 | subgraphMsg << "Re-assigning backends to " << failedSubgraph.GetLayers().size() |
| 417 | << " layers inside sub-graph " << count++; |
| 418 | ReportWarning(warningMsg.str(), errMessages); |
| 419 | |
| 420 | OptimizationResult reassignmentResult = AssignBackends(optNetObjPtr, |
| 421 | settingsCopy, |
| 422 | *subgraph, |
| 423 | errMessages); |
| 424 | if (reassignmentResult.m_Error) |
| 425 | { |
| 426 | // Failed to re-assign one of the remaining backends to each layer of the sub-graph |
| 427 | result.m_Error = true; |
| 428 | return result; |
| 429 | } |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 430 | } |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | return result; |
| 436 | } |
| 437 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 438 | bool RequiresCopy(ITensorHandleFactory::FactoryId src, |
| 439 | ITensorHandleFactory::FactoryId dst, |
| 440 | TensorHandleFactoryRegistry& registry) |
| 441 | { |
| 442 | if (src != dst) |
| 443 | { |
| 444 | ITensorHandleFactory* srcFactory = registry.GetFactory(src); |
| 445 | ITensorHandleFactory* dstFactory = registry.GetFactory(dst); |
| 446 | |
| 447 | if (srcFactory->SupportsExport() && dstFactory->SupportsImport()) |
| 448 | { |
| 449 | return false; |
| 450 | } |
| 451 | return true; |
| 452 | } |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | // Find the handle factory for the input layer which results in fewest required copies. |
| 457 | ITensorHandleFactory::FactoryId CalculateSlotOptionForInput(BackendsMap& backends, |
| 458 | OutputSlot& slot, |
| 459 | TensorHandleFactoryRegistry& registry) |
| 460 | { |
| 461 | Layer& layer = slot.GetOwningLayer(); |
| 462 | BOOST_ASSERT(layer.GetType() == LayerType::Input); |
| 463 | |
| 464 | // Explicitly select the tensorhandle factory for InputLayer because the rules for it are slightly different. It |
| 465 | // doesn't matter which backend it is assigned to because they all use the same implementation, which |
| 466 | // requires Map/Unmap support. This means that, so long as the handle type supports map/unmap semantics, we can |
| 467 | // select a factory with maximum compatibility with the layers connected to the InputLayer. |
| 468 | |
| 469 | // First ensure the from backends can support the TensorHandeAPI |
| 470 | auto frmBackend = backends.find(layer.GetBackendId()); |
| 471 | if (frmBackend == backends.end() || |
| 472 | !frmBackend->second->SupportsTensorAllocatorAPI()) |
| 473 | { |
| 474 | return ITensorHandleFactory::LegacyFactoryId; |
| 475 | } |
| 476 | |
| 477 | // Go through all connections to the output slot and determine the TensorHandleFactory which results in the |
| 478 | // fewest copies. |
| 479 | std::map<ITensorHandleFactory::FactoryId, int> factoryScores; |
| 480 | int topScore = 0; |
| 481 | ITensorHandleFactory::FactoryId topChoice = ITensorHandleFactory::LegacyFactoryId; |
| 482 | |
| 483 | for (auto&& connection : slot.GetConnections()) |
| 484 | { |
| 485 | const Layer& connectedLayer = connection->GetOwningLayer(); |
| 486 | |
| 487 | auto toBackend = backends.find(connectedLayer.GetBackendId()); |
| 488 | BOOST_ASSERT_MSG(toBackend != backends.end(), "Backend id not found for the connected layer"); |
| 489 | |
| 490 | if (!toBackend->second.get()->SupportsTensorAllocatorAPI()) |
| 491 | { |
| 492 | // The destination backend does not support the tensor allocator API, move to the next one |
| 493 | continue; |
| 494 | } |
| 495 | |
| 496 | auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences(); |
| 497 | for (auto&& dst : dstPrefs) |
| 498 | { |
| 499 | // Input layers use the mem copy workload, so the selected factory must support map/unmap API |
| 500 | ITensorHandleFactory* factory = registry.GetFactory(dst); |
| 501 | if (!factory->SupportsMapUnmap()) |
| 502 | { |
| 503 | // The current tensor handle factory does not support the map/unmap strategy, move to the next one |
| 504 | continue; |
| 505 | } |
| 506 | |
| 507 | auto it = factoryScores.find(dst); |
| 508 | if (it == factoryScores.end()) |
| 509 | { |
| 510 | // Add new score to the table |
| 511 | factoryScores[dst] = 0; |
| 512 | if (topChoice == ITensorHandleFactory::LegacyFactoryId) |
| 513 | { |
| 514 | topChoice = dst; |
| 515 | } |
| 516 | } |
| 517 | else |
| 518 | { |
| 519 | // Increase the score |
| 520 | factoryScores[dst]++; |
| 521 | |
| 522 | // Track the best option |
| 523 | if (factoryScores[dst] > topScore) |
| 524 | { |
| 525 | topScore = factoryScores[dst]; |
| 526 | topChoice = dst; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | return topChoice; |
| 533 | } |
| 534 | |
| 535 | // Find the handle factory for the output layer which results in fewest required copies. |
| 536 | ITensorHandleFactory::FactoryId CalculateSlotOptionForOutput(BackendsMap& backends, |
| 537 | OutputSlot& slot, |
| 538 | TensorHandleFactoryRegistry& registry) |
| 539 | { |
| 540 | return ITensorHandleFactory::DeferredFactoryId; |
| 541 | } |
| 542 | |
| 543 | // For all handle factories supported on the source backend, we wish to find the one which requires the fewest copies |
| 544 | // when considering all connections. |
| 545 | ITensorHandleFactory::FactoryId CalculateSlotOption(BackendsMap& backends, |
| 546 | OutputSlot& outputSlot, |
| 547 | TensorHandleFactoryRegistry& registry) |
| 548 | { |
| 549 | // First ensure the from backends can support the TensorHandeAPI |
| 550 | Layer& layer = outputSlot.GetOwningLayer(); |
| 551 | auto frmBackend = backends.find(layer.GetBackendId()); |
| 552 | if (frmBackend == backends.end() || |
| 553 | !frmBackend->second->SupportsTensorAllocatorAPI()) |
| 554 | { |
| 555 | return ITensorHandleFactory::LegacyFactoryId; |
| 556 | } |
| 557 | |
| 558 | // Connections to Output Layers requires support for map/unmap on the TensorHandle. |
| 559 | bool requiresMapUnmap = false; |
| 560 | for (auto&& connection : outputSlot.GetConnections()) |
| 561 | { |
| 562 | const Layer& connectedLayer = connection->GetOwningLayer(); |
| 563 | if (connectedLayer.GetType() == LayerType::Output) |
| 564 | { |
| 565 | requiresMapUnmap = true; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | IBackendInternal* srcBackend = frmBackend->second.get(); |
| 570 | auto srcPrefs = srcBackend->GetHandleFactoryPreferences(); |
| 571 | |
| 572 | // Initialize the scores |
| 573 | std::map<ITensorHandleFactory::FactoryId, int> factoryScores; |
| 574 | for (auto&& pref : srcPrefs) |
| 575 | { |
| 576 | if (requiresMapUnmap) // Only consider factories that support map/unmap if required |
| 577 | { |
| 578 | ITensorHandleFactory* factory = registry.GetFactory(pref); |
| 579 | if (!factory->SupportsMapUnmap()) |
| 580 | { |
| 581 | // The current tensor handle factory does not support the map/unmap strategy, move to the next one |
| 582 | continue; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | auto it = factoryScores.find(pref); |
| 587 | if (it == factoryScores.end()) |
| 588 | { |
| 589 | // Add new score to the table |
| 590 | factoryScores[pref] = 0; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | // Score each handle factory based on how many times it requires copies on the slot connections |
| 595 | for (auto&& connection : outputSlot.GetConnections()) |
| 596 | { |
| 597 | const Layer& connectedLayer = connection->GetOwningLayer(); |
| 598 | |
| 599 | auto toBackend = backends.find(connectedLayer.GetBackendId()); |
| 600 | BOOST_ASSERT_MSG(toBackend != backends.end(), "Backend id not found for the connected layer"); |
| 601 | |
| 602 | auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences(); |
| 603 | for (auto&& src : srcPrefs) |
| 604 | { |
| 605 | if (factoryScores.find(src) == factoryScores.end()) // Don't consider excluded factories |
| 606 | { |
| 607 | continue; |
| 608 | } |
| 609 | |
| 610 | for (auto&& dst : dstPrefs) |
| 611 | { |
| 612 | if (RequiresCopy(src, dst, registry)) |
| 613 | { |
| 614 | // Copy avoided, increase the score |
| 615 | factoryScores[src]++; |
| 616 | break; |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | // Find the lowest score |
| 623 | int minScore = std::numeric_limits<int>::max(); |
| 624 | for (auto it : factoryScores) |
| 625 | { |
| 626 | minScore = std::min(minScore, it.second); |
| 627 | } |
| 628 | |
| 629 | // Collect factories matching the best(lowest) score |
| 630 | std::vector<ITensorHandleFactory::FactoryId> optimalFactories; |
| 631 | for (auto it : factoryScores) |
| 632 | { |
| 633 | if (it.second == minScore) |
| 634 | { |
| 635 | optimalFactories.push_back(it.first); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | // For all compatible Factories matching the best score, find the preferred one for the current layer. |
| 640 | for (auto&& srcPref : srcPrefs) |
| 641 | { |
| 642 | for (auto&& comp : optimalFactories) |
| 643 | { |
| 644 | if (comp == srcPref) |
| 645 | { |
| 646 | return comp; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | return ITensorHandleFactory::LegacyFactoryId; |
| 652 | } |
| 653 | |
| 654 | MemoryStrategy CalculateStrategy(BackendsMap& backends, |
| 655 | ITensorHandleFactory::FactoryId srcFactoryId, |
| 656 | const Layer& layer, |
| 657 | const Layer& connectedLayer, |
| 658 | TensorHandleFactoryRegistry& registry) |
| 659 | { |
| 660 | auto toBackend = backends.find(connectedLayer.GetBackendId()); |
| 661 | BOOST_ASSERT_MSG(toBackend != backends.end(), "Backend id not found for the connected layer"); |
| 662 | |
| 663 | auto dstPrefs = toBackend->second.get()->GetHandleFactoryPreferences(); |
| 664 | |
| 665 | // Legacy API check for backward compatibility |
| 666 | if (srcFactoryId == ITensorHandleFactory::LegacyFactoryId || dstPrefs.empty()) |
| 667 | { |
| 668 | if (layer.GetBackendId() != connectedLayer.GetBackendId()) |
| 669 | { |
| 670 | return MemoryStrategy::CopyToTarget; |
| 671 | } |
| 672 | else |
| 673 | { |
| 674 | return MemoryStrategy::DirectCompatibility; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | // TensorHandleFactory API present, so perform more sophisticated strategies. |
| 679 | // Dst Output layers don't require copy because they use map/unmap |
| 680 | if (connectedLayer.GetType() == LayerType::Output) |
| 681 | { |
| 682 | return MemoryStrategy::DirectCompatibility; |
| 683 | } |
| 684 | |
| 685 | // Search for direct match in prefs |
| 686 | for (auto&& pref : dstPrefs) |
| 687 | { |
| 688 | if (pref == srcFactoryId) |
| 689 | { |
| 690 | return MemoryStrategy::DirectCompatibility; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | // Search for export/import options |
| 695 | ITensorHandleFactory* srcFactory = registry.GetFactory(srcFactoryId); |
| 696 | if (srcFactory->SupportsExport()) |
| 697 | { |
| 698 | for (auto&& pref : dstPrefs) |
| 699 | { |
| 700 | ITensorHandleFactory* dstFactory = registry.GetFactory(pref); |
| 701 | if (dstFactory->SupportsImport()) |
| 702 | { |
| 703 | return MemoryStrategy::ExportToTarget; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | // Search for copy options via map/unmap |
| 709 | if (srcFactory->SupportsMapUnmap()) |
| 710 | { |
| 711 | for (auto&& pref : dstPrefs) |
| 712 | { |
| 713 | ITensorHandleFactory* dstFactory = registry.GetFactory(pref); |
| 714 | if (dstFactory->SupportsMapUnmap()) |
| 715 | { |
| 716 | return MemoryStrategy::CopyToTarget; |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | return MemoryStrategy::Undefined; |
| 722 | } |
| 723 | |
| 724 | // Select the TensorHandleFactories and the corresponding memory strategy |
| 725 | OptimizationResult SelectTensorHandleStrategy(Graph& optGraph, |
| 726 | BackendsMap& backends, |
| 727 | TensorHandleFactoryRegistry& registry, |
| 728 | Optional<std::vector<std::string>&> errMessages) |
| 729 | { |
| 730 | OptimizationResult result; |
| 731 | |
| 732 | optGraph.ForEachLayer([&backends, ®istry, &result, &errMessages](Layer* layer) |
| 733 | { |
| 734 | BOOST_ASSERT(layer); |
| 735 | |
| 736 | // Lets make sure the backend is in our list of supported backends. Something went wrong during backend |
| 737 | // assignment if this check fails |
| 738 | BOOST_ASSERT(backends.find(layer->GetBackendId()) != backends.end()); |
| 739 | |
| 740 | // Check each output separately |
| 741 | for (unsigned int slotIdx = 0; slotIdx < layer->GetNumOutputSlots(); slotIdx++) |
| 742 | { |
| 743 | OutputSlot& outputSlot = layer->GetOutputSlot(slotIdx); |
| 744 | |
| 745 | ITensorHandleFactory::FactoryId slotOption = ITensorHandleFactory::LegacyFactoryId; |
| 746 | |
| 747 | // Calculate the factory to use which results in the fewest copies being made. |
| 748 | switch(layer->GetType()) |
| 749 | { |
| 750 | case LayerType::Input: |
| 751 | slotOption = CalculateSlotOptionForInput(backends, outputSlot, registry); |
| 752 | break; |
| 753 | case LayerType::Output: |
| 754 | slotOption = CalculateSlotOptionForOutput(backends, outputSlot, registry); |
| 755 | break; |
| 756 | default: |
| 757 | slotOption = CalculateSlotOption(backends, outputSlot, registry); |
| 758 | break; |
| 759 | } |
| 760 | outputSlot.SetTensorHandleFactory(slotOption); |
| 761 | |
| 762 | // Now determine the "best" memory strategy for each connection given the slotOption. |
| 763 | unsigned int connectionIdx = 0; |
| 764 | for (auto&& connection : outputSlot.GetConnections()) |
| 765 | { |
| 766 | const Layer& connectedLayer = connection->GetOwningLayer(); |
| 767 | |
| 768 | MemoryStrategy strategy = CalculateStrategy(backends, slotOption, *layer, connectedLayer, registry); |
| 769 | |
| 770 | if (strategy == MemoryStrategy::Undefined) |
| 771 | { |
| 772 | result.m_Error = true; |
| 773 | if (errMessages) |
| 774 | { |
| 775 | errMessages.value().emplace_back("Could not find valid strategy required for compatibility" |
| 776 | " between backends."); |
| 777 | } |
| 778 | return; |
| 779 | } |
| 780 | |
| 781 | outputSlot.SetMemoryStrategy(connectionIdx, strategy); |
| 782 | |
| 783 | connectionIdx++; |
| 784 | } |
| 785 | } |
| 786 | }); |
| 787 | |
| 788 | return result; |
| 789 | } |
| 790 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 791 | IOptimizedNetworkPtr Optimize(const INetwork& inNetwork, |
| 792 | const std::vector<BackendId>& backendPreferences, |
| 793 | const IDeviceSpec& deviceSpec, |
| 794 | const OptimizerOptions& options, |
| 795 | Optional<std::vector<std::string>&> errMessages) |
| 796 | { |
| 797 | if (backendPreferences.empty()) |
| 798 | { |
| 799 | throw armnn::InvalidArgumentException("Invoked Optimize with no backends specified"); |
| 800 | } |
| 801 | |
| 802 | const Network& network = *boost::polymorphic_downcast<const Network*>(&inNetwork); |
| 803 | std::unique_ptr<Graph> graph = std::make_unique<Graph>(network.GetGraph()); |
| 804 | |
| 805 | auto optNet = IOptimizedNetworkPtr(new OptimizedNetwork(std::move(graph)), &IOptimizedNetwork::Destroy); |
| 806 | |
| 807 | OptimizedNetwork* optNetObjPtr = boost::polymorphic_downcast<OptimizedNetwork*>(optNet.get()); |
| 808 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 809 | // Get the optimized graph |
| 810 | Graph& optGraph = optNetObjPtr->GetGraph(); |
| 811 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 812 | // Perform optimisation passes |
| 813 | using namespace optimizations; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 814 | Optimizer::Pass(optGraph, MakeOptimizations(SquashEqualPermuteSiblings(), |
| 815 | SquashEqualReshapeSiblings(), |
| 816 | OptimizeInversePermutes(), |
| 817 | MovePermuteUp(), |
| 818 | PermuteAsReshape(), |
Nina Drozd | 861985f | 2019-04-18 14:48:51 +0100 | [diff] [blame] | 819 | OptimizeConsecutiveReshapes(), |
| 820 | FoldPadIntoConvolution2d())); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 821 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 822 | // Infer the tensor infos for all output slots. Throws an exception on failure |
| 823 | optGraph.InferTensorInfos(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 824 | |
| 825 | // If Fp32 to Fp16 optimization is set convert Fp32 network to Fp16 |
| 826 | if (options.m_ReduceFp32ToFp16) |
| 827 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 828 | Optimizer::Pass(optGraph, MakeOptimizations(Fp32NetworkToFp16Converter())); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | // Initialize backend settings |
| 832 | BackendSettings backendSettings(backendPreferences, deviceSpec); |
| 833 | if (backendSettings.GetAvailablePreferredBackends().empty()) |
| 834 | { |
| 835 | std::stringstream failureMsg; |
| 836 | failureMsg << "None of the preferred backends " << backendPreferences |
| 837 | << " are supported. Current platform provides " << backendSettings.m_SupportedBackends; |
| 838 | ReportError(failureMsg.str(), errMessages); |
| 839 | return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy); |
| 840 | } |
| 841 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 842 | // Create a map to temporarily hold initialized backend objects |
| 843 | TensorHandleFactoryRegistry tensorHandleFactoryRegistry; |
| 844 | BackendsMap backends = CreateSupportedBackends(tensorHandleFactoryRegistry, backendSettings); |
| 845 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 846 | // Assign an available backend to each layer |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 847 | Graph::Iterator firstLayer = optGraph.begin(); |
| 848 | Graph::Iterator lastLayer = optGraph.end(); |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 849 | OptimizationResult assignBackendsResult = AssignBackends(optNetObjPtr, |
| 850 | backendSettings, |
| 851 | firstLayer, |
| 852 | lastLayer, |
| 853 | errMessages); |
| 854 | if (assignBackendsResult.m_Error) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 855 | { |
| 856 | // Failed to assign a backend to each layer |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 857 | return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy); |
| 858 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 859 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 860 | Optimizer::Pass(optGraph, MakeOptimizations(OptimizeInverseConversionsFp16(), |
| 861 | OptimizeInverseConversionsFp32())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 862 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 863 | // Apply the backend-specific optimizations |
| 864 | OptimizationResult backendOptimizationResult = ApplyBackendOptimizations(optNetObjPtr, |
| 865 | backendSettings, |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 866 | backends, |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 867 | errMessages); |
| 868 | if (backendOptimizationResult.m_Error) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 869 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 870 | // Failed to apply the backend-specific optimizations |
| 871 | return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 874 | // If the debug flag is set, then insert a DebugLayer after each layer |
| 875 | // Doing this after applying the backend optimizations as they might have changed some layers |
| 876 | if (options.m_Debug) |
| 877 | { |
| 878 | Optimizer::Pass(optGraph, MakeOptimizations(InsertDebugLayer())); |
| 879 | } |
| 880 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 881 | // Calculate the compatibility strategies for tensor handles |
| 882 | OptimizationResult strategyResult = SelectTensorHandleStrategy(optGraph, |
| 883 | backends, |
| 884 | tensorHandleFactoryRegistry, |
| 885 | errMessages); |
| 886 | if (strategyResult.m_Error) |
| 887 | { |
| 888 | // Failed to apply the backend-specific optimizations |
| 889 | return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy); |
| 890 | } |
| 891 | |
| 892 | // Based on the tensor handle strategy determined above, insert copy layers where required. |
| 893 | optGraph.AddCopyLayers(backends, tensorHandleFactoryRegistry); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 894 | |
| 895 | // Convert constants |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 896 | Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsFloatToHalf())); |
| 897 | Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsHalfToFloat())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 898 | |
Derek Lamberti | 84da38b | 2019-06-13 11:40:08 +0100 | [diff] [blame] | 899 | // Run backend specific optimizations (deprecated) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 900 | for (auto&& chosenBackend : backendSettings.m_SelectedBackends) |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 901 | { |
| 902 | auto factoryFun = BackendRegistryInstance().GetFactory(chosenBackend); |
| 903 | auto backendPtr = factoryFun(); |
| 904 | BOOST_ASSERT(backendPtr.get() != nullptr); |
| 905 | |
Matteo Martincigh | ed73504 | 2019-05-22 09:42:43 +0100 | [diff] [blame] | 906 | ARMNN_NO_DEPRECATE_WARN_BEGIN |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 907 | auto backendSpecificOptimizations = backendPtr->GetOptimizations(); |
Matteo Martincigh | ed73504 | 2019-05-22 09:42:43 +0100 | [diff] [blame] | 908 | ARMNN_NO_DEPRECATE_WARN_END |
| 909 | |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 910 | if (!backendSpecificOptimizations.empty()) |
| 911 | { |
| 912 | Optimizer::Pass(optNetObjPtr->GetGraph(), backendSpecificOptimizations); |
| 913 | } |
| 914 | } |
| 915 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 916 | return optNet; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | Network::Network() |
| 920 | : m_Graph(std::make_unique<Graph>()) |
| 921 | { |
| 922 | } |
| 923 | |
| 924 | Network::~Network() |
| 925 | { |
| 926 | } |
| 927 | |
| 928 | IConnectableLayer* Network::AddInputLayer(LayerBindingId id, const char* name) |
| 929 | { |
| 930 | return m_Graph->AddLayer<InputLayer>(id, name); |
| 931 | } |
| 932 | |
Éanna Ó Catháin | 4e1e136 | 2018-11-12 11:36:34 +0000 | [diff] [blame] | 933 | IConnectableLayer* Network::AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor, |
| 934 | const char* name) |
| 935 | { |
| 936 | return m_Graph->AddLayer<BatchToSpaceNdLayer>(batchToSpaceNdDescriptor, name); |
| 937 | } |
| 938 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 939 | IConnectableLayer* Network::AddFullyConnectedLayerImpl(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 940 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 941 | const Optional<ConstTensor>& biases, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 942 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 943 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 944 | if (fullyConnectedDescriptor.m_BiasEnabled && !biases.has_value()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 945 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 946 | throw InvalidArgumentException("AddFullyConnectedLayer: biases cannot be empty"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | const auto layer = m_Graph->AddLayer<FullyConnectedLayer>(fullyConnectedDescriptor, name); |
| 950 | |
| 951 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 952 | |
| 953 | if (fullyConnectedDescriptor.m_BiasEnabled) |
| 954 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 955 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | return layer; |
| 959 | } |
| 960 | |
| 961 | IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 962 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 963 | const Optional<ConstTensor>& biases, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 964 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 965 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 966 | return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, biases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 969 | IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
| 970 | const ConstTensor& weights, |
| 971 | const char* name) |
| 972 | { |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 973 | Optional<ConstTensor> biases; |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 974 | return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, biases, name); |
| 975 | } |
| 976 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 977 | IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 978 | const ConstTensor& weights, |
| 979 | const ConstTensor& biases, |
| 980 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 981 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 982 | Optional<ConstTensor> optionalBiases(biases); |
| 983 | return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, optionalBiases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 986 | IConnectableLayer* Network::AddConcatLayer(const ConcatDescriptor& concatDescriptor, |
Jim Flynn | 906f946 | 2019-05-10 13:55:21 +0100 | [diff] [blame] | 987 | const char* name) |
| 988 | { |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 989 | return m_Graph->AddLayer<ConcatLayer>(concatDescriptor, name); |
Jim Flynn | 906f946 | 2019-05-10 13:55:21 +0100 | [diff] [blame] | 990 | } |
| 991 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 992 | IConnectableLayer* Network::AddConvolution2dLayerImpl(const Convolution2dDescriptor& convolution2dDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 993 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 994 | const Optional<ConstTensor>& biases, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 995 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 996 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 997 | if (convolution2dDescriptor.m_BiasEnabled && !biases.has_value()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 998 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 999 | throw InvalidArgumentException("AddConvolution2dLayer: biases cannot be empty"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | const auto layer = m_Graph->AddLayer<Convolution2dLayer>(convolution2dDescriptor, name); |
| 1003 | |
| 1004 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 1005 | |
| 1006 | if (convolution2dDescriptor.m_BiasEnabled) |
| 1007 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1008 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | return layer; |
| 1012 | } |
| 1013 | |
| 1014 | IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1015 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1016 | const Optional<ConstTensor>& biases, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1017 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1018 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1019 | return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1020 | } |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1021 | |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1022 | IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, |
| 1023 | const ConstTensor& weights, |
| 1024 | const char* name) |
| 1025 | { |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1026 | Optional<ConstTensor> biases; |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1027 | return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name); |
| 1028 | } |
| 1029 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1030 | IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1031 | const ConstTensor& weights, |
| 1032 | const ConstTensor& biases, |
| 1033 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1034 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1035 | Optional<ConstTensor> optionalBiases(biases); |
| 1036 | return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, optionalBiases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayerImpl( |
| 1040 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 1041 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1042 | const Optional<ConstTensor>& biases, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1043 | const char* name) |
| 1044 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1045 | if (convolution2dDescriptor.m_BiasEnabled && !biases.has_value()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1046 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1047 | throw InvalidArgumentException("AddDepthwiseConvolution2dLayer: biases cannot be empty"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Matteo Martincigh | 3d6898c | 2019-01-15 16:11:44 +0000 | [diff] [blame] | 1050 | const auto layer = m_Graph->AddLayer<DepthwiseConvolution2dLayer>(convolution2dDescriptor, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1051 | |
| 1052 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 1053 | |
| 1054 | if (convolution2dDescriptor.m_BiasEnabled) |
| 1055 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1056 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | return layer; |
| 1060 | } |
| 1061 | |
| 1062 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayer( |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1063 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 1064 | const ConstTensor& weights, |
| 1065 | const Optional<ConstTensor>& biases, |
| 1066 | const char* name) |
| 1067 | { |
| 1068 | return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name); |
| 1069 | } |
| 1070 | |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1071 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayer( |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1072 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 1073 | const ConstTensor& weights, |
| 1074 | const char* name) |
| 1075 | { |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1076 | Optional<ConstTensor> biases; |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1077 | return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1078 | } |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1079 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1080 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayer( |
| 1081 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 1082 | const ConstTensor& weights, |
| 1083 | const ConstTensor& biases, |
| 1084 | const char* name) |
| 1085 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame] | 1086 | Optional<ConstTensor> optionalBiases(biases); |
| 1087 | return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, optionalBiases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Narumol Prangnawarat | 94dd5d8 | 2019-01-23 18:06:26 +0000 | [diff] [blame] | 1090 | IConnectableLayer* Network::AddDetectionPostProcessLayer(const armnn::DetectionPostProcessDescriptor& descriptor, |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 1091 | const ConstTensor& anchors, const char* name) |
Narumol Prangnawarat | 94dd5d8 | 2019-01-23 18:06:26 +0000 | [diff] [blame] | 1092 | { |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 1093 | const auto layer = m_Graph->AddLayer<DetectionPostProcessLayer>(descriptor, name); |
| 1094 | |
| 1095 | layer->m_Anchors = std::make_unique<ScopedCpuTensorHandle>(anchors); |
| 1096 | |
| 1097 | return layer; |
Narumol Prangnawarat | 94dd5d8 | 2019-01-23 18:06:26 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1100 | IConnectableLayer* Network::AddPermuteLayer(const PermuteDescriptor& permuteDescriptor, |
| 1101 | const char* name) |
| 1102 | { |
| 1103 | return m_Graph->AddLayer<PermuteLayer>(permuteDescriptor, name); |
| 1104 | } |
| 1105 | |
| 1106 | IConnectableLayer* Network::AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor, |
| 1107 | const char* name) |
| 1108 | { |
| 1109 | return m_Graph->AddLayer<Pooling2dLayer>(pooling2dDescriptor, name); |
| 1110 | } |
| 1111 | |
| 1112 | IConnectableLayer* Network::AddActivationLayer(const ActivationDescriptor& activationDescriptor, |
| 1113 | const char* name) |
| 1114 | { |
| 1115 | return m_Graph->AddLayer<ActivationLayer>(activationDescriptor, name); |
| 1116 | } |
| 1117 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1118 | IConnectableLayer* Network::AddNormalizationLayer(const NormalizationDescriptor& |
| 1119 | normalizationDescriptor, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1120 | const char* name) |
| 1121 | { |
| 1122 | return m_Graph->AddLayer<NormalizationLayer>(normalizationDescriptor, name); |
| 1123 | } |
| 1124 | |
| 1125 | IConnectableLayer* Network::AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor, |
| 1126 | const char* name) |
| 1127 | { |
| 1128 | return m_Graph->AddLayer<SoftmaxLayer>(softmaxDescriptor, name); |
| 1129 | } |
| 1130 | |
| 1131 | IConnectableLayer* Network::AddSplitterLayer(const ViewsDescriptor& splitterDescriptor, |
| 1132 | const char* name) |
| 1133 | { |
| 1134 | return m_Graph->AddLayer<SplitterLayer>(splitterDescriptor, name); |
| 1135 | } |
| 1136 | |
Nattapat Chaimanowong | 5a4304a | 2018-11-28 10:44:37 +0000 | [diff] [blame] | 1137 | IConnectableLayer* Network::AddMaximumLayer(const char* name) |
| 1138 | { |
| 1139 | return m_Graph->AddLayer<MaximumLayer>(name); |
| 1140 | } |
| 1141 | |
Éanna Ó Catháin | 20e5880 | 2018-12-04 10:29:06 +0000 | [diff] [blame] | 1142 | IConnectableLayer* Network::AddMinimumLayer(const char* name) |
| 1143 | { |
| 1144 | return m_Graph->AddLayer<MinimumLayer>(name); |
| 1145 | } |
| 1146 | |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 1147 | IConnectableLayer* Network::AddMergerLayer(const MergerDescriptor& mergerDescriptor, |
Jim Flynn | 906f946 | 2019-05-10 13:55:21 +0100 | [diff] [blame] | 1148 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1149 | { |
Jim Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 1150 | return AddConcatLayer(mergerDescriptor, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | IConnectableLayer* Network::AddAdditionLayer(const char* name) |
| 1154 | { |
| 1155 | return m_Graph->AddLayer<AdditionLayer>(name); |
| 1156 | } |
| 1157 | |
| 1158 | IConnectableLayer* Network::AddMultiplicationLayer(const char* name) |
| 1159 | { |
| 1160 | return m_Graph->AddLayer<MultiplicationLayer>(name); |
| 1161 | } |
| 1162 | |
| 1163 | IConnectableLayer* Network::AddOutputLayer(LayerBindingId id, const char* name) |
| 1164 | { |
| 1165 | return m_Graph->AddLayer<OutputLayer>(id, name); |
| 1166 | } |
| 1167 | |
| 1168 | IConnectableLayer* Network::AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc, |
| 1169 | const ConstTensor& mean, |
| 1170 | const ConstTensor& variance, |
| 1171 | const ConstTensor& beta, |
| 1172 | const ConstTensor& gamma, |
| 1173 | const char* name) |
| 1174 | { |
| 1175 | const auto layer = m_Graph->AddLayer<BatchNormalizationLayer>(desc, name); |
| 1176 | |
| 1177 | layer->m_Mean = std::make_unique<ScopedCpuTensorHandle>(mean); |
| 1178 | layer->m_Variance = std::make_unique<ScopedCpuTensorHandle>(variance); |
| 1179 | layer->m_Beta = std::make_unique<ScopedCpuTensorHandle>(beta); |
| 1180 | layer->m_Gamma = std::make_unique<ScopedCpuTensorHandle>(gamma); |
| 1181 | |
| 1182 | return layer; |
| 1183 | } |
| 1184 | |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame^] | 1185 | IConnectableLayer* Network::AddResizeBilinearLayer(const ResizeBilinearDescriptor& descriptor, |
| 1186 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1187 | { |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame^] | 1188 | ResizeDescriptor resizeDescriptor; |
| 1189 | resizeDescriptor.m_Method = ResizeMethod::Bilinear; |
| 1190 | resizeDescriptor.m_DataLayout = descriptor.m_DataLayout; |
| 1191 | resizeDescriptor.m_TargetWidth = descriptor.m_TargetWidth; |
| 1192 | resizeDescriptor.m_TargetHeight = descriptor.m_TargetHeight; |
| 1193 | |
| 1194 | return m_Graph->AddLayer<ResizeLayer>(resizeDescriptor, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Teresa Charlin | a9075df | 2019-06-27 15:41:57 +0100 | [diff] [blame] | 1197 | IConnectableLayer* Network::AddResizeLayer(const ResizeDescriptor& |
| 1198 | resizeDescriptor, const char* name) |
| 1199 | { |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame^] | 1200 | return m_Graph->AddLayer<ResizeLayer>(resizeDescriptor, name); |
Teresa Charlin | a9075df | 2019-06-27 15:41:57 +0100 | [diff] [blame] | 1201 | } |
| 1202 | |
Matteo Martincigh | bcd3c85 | 2018-09-28 14:14:12 +0100 | [diff] [blame] | 1203 | IConnectableLayer* Network::AddL2NormalizationLayer(const L2NormalizationDescriptor& desc, |
| 1204 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1205 | { |
Matteo Martincigh | bcd3c85 | 2018-09-28 14:14:12 +0100 | [diff] [blame] | 1206 | return m_Graph->AddLayer<L2NormalizationLayer>(desc, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | IConnectableLayer* Network::AddConstantLayer(const ConstTensor& input, const char* name) |
| 1210 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1211 | auto layer = m_Graph->AddLayer<ConstantLayer>(name); |
| 1212 | |
| 1213 | layer->m_LayerOutput = std::make_unique<ScopedCpuTensorHandle>(input); |
| 1214 | |
| 1215 | return layer; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1218 | IConnectableLayer* Network::AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor, |
| 1219 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1220 | { |
| 1221 | return m_Graph->AddLayer<ReshapeLayer>(reshapeDescriptor, name); |
| 1222 | } |
| 1223 | |
Nattapat Chaimanowong | 207ef9a | 2018-11-02 10:57:25 +0000 | [diff] [blame] | 1224 | IConnectableLayer* Network::AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor, |
| 1225 | const char* name) |
| 1226 | { |
| 1227 | return m_Graph->AddLayer<SpaceToBatchNdLayer>(spaceToBatchNdDescriptor, name); |
| 1228 | } |
| 1229 | |
Aron Virginas-Tar | 972af15 | 2019-06-11 14:14:03 +0100 | [diff] [blame] | 1230 | IConnectableLayer* Network::AddSpaceToDepthLayer(const SpaceToDepthDescriptor& spaceToDepthDescriptor, |
| 1231 | const char* name) |
| 1232 | { |
| 1233 | return m_Graph->AddLayer<SpaceToDepthLayer>(spaceToDepthDescriptor, name); |
| 1234 | } |
| 1235 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1236 | IConnectableLayer* Network::AddFloorLayer(const char* name) |
| 1237 | { |
| 1238 | return m_Graph->AddLayer<FloorLayer>(name); |
| 1239 | } |
| 1240 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1241 | IConnectableLayer* Network::AddLstmLayer(const LstmDescriptor& descriptor, |
| 1242 | const LstmInputParams& params, |
| 1243 | const char* name) |
| 1244 | { |
| 1245 | const auto layer = m_Graph->AddLayer<LstmLayer>(descriptor, name); |
| 1246 | |
| 1247 | //Lstm Basic Parameters |
| 1248 | layer->m_BasicParameters.m_InputToForgetWeights = |
| 1249 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToForgetWeights)); |
| 1250 | layer->m_BasicParameters.m_InputToCellWeights = |
| 1251 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToCellWeights)); |
| 1252 | layer->m_BasicParameters.m_InputToOutputWeights = |
| 1253 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToOutputWeights)); |
| 1254 | layer->m_BasicParameters.m_RecurrentToForgetWeights = |
| 1255 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToForgetWeights)); |
| 1256 | layer->m_BasicParameters.m_RecurrentToCellWeights = |
| 1257 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToCellWeights)); |
| 1258 | layer->m_BasicParameters.m_RecurrentToOutputWeights = |
| 1259 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToOutputWeights)); |
| 1260 | layer->m_BasicParameters.m_ForgetGateBias = |
| 1261 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetGateBias)); |
| 1262 | layer->m_BasicParameters.m_CellBias = |
| 1263 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellBias)); |
| 1264 | layer->m_BasicParameters.m_OutputGateBias = |
| 1265 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputGateBias)); |
| 1266 | |
| 1267 | //Lstm Cifg parameters |
| 1268 | if(!descriptor.m_CifgEnabled) |
| 1269 | { |
| 1270 | if(params.m_InputToInputWeights == nullptr) |
| 1271 | { |
| 1272 | throw InvalidArgumentException("AddLstmLayer: Input To Input Weights cannot be NULL"); |
| 1273 | } |
| 1274 | if(params.m_RecurrentToInputWeights == nullptr) |
| 1275 | { |
| 1276 | throw InvalidArgumentException( |
| 1277 | "AddLstmLayer: Recurrent To Input Weights cannot be NULL"); |
| 1278 | } |
| 1279 | if(params.m_InputGateBias == nullptr) |
| 1280 | { |
| 1281 | throw InvalidArgumentException("AddLstmLayer: Input Gate Bias cannot be NULL"); |
| 1282 | } |
| 1283 | layer->m_CifgParameters.m_InputToInputWeights = |
| 1284 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToInputWeights)); |
| 1285 | layer->m_CifgParameters.m_RecurrentToInputWeights = |
| 1286 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToInputWeights)); |
| 1287 | // In the VTS tests, cell-to-input weights may be null, even if the other CIFG params are not. |
| 1288 | if(params.m_CellToInputWeights != nullptr) |
| 1289 | { |
| 1290 | layer->m_CifgParameters.m_CellToInputWeights = |
| 1291 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToInputWeights)); |
| 1292 | } |
| 1293 | layer->m_CifgParameters.m_InputGateBias = |
| 1294 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputGateBias)); |
| 1295 | } |
| 1296 | |
| 1297 | //Lstm projection parameters |
| 1298 | if(descriptor.m_ProjectionEnabled) |
| 1299 | { |
| 1300 | if(params.m_ProjectionWeights == nullptr) |
| 1301 | { |
| 1302 | throw InvalidArgumentException("AddLstmLayer: Projection Weights cannot be NULL"); |
| 1303 | } |
| 1304 | layer->m_ProjectionParameters.m_ProjectionWeights = |
| 1305 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionWeights)); |
| 1306 | if(params.m_ProjectionBias != nullptr) |
| 1307 | { |
| 1308 | layer->m_ProjectionParameters.m_ProjectionBias = |
| 1309 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionBias)); |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | //Lstm Peephole params |
| 1314 | if(descriptor.m_PeepholeEnabled) |
| 1315 | { |
| 1316 | if(params.m_CellToForgetWeights == nullptr) |
| 1317 | { |
| 1318 | throw InvalidArgumentException("AddLstmLayer: Cell To Forget Weights cannot be NULL"); |
| 1319 | } |
| 1320 | if(params.m_CellToOutputWeights == nullptr) |
| 1321 | { |
| 1322 | throw InvalidArgumentException("AddLstmLayer: Cell To Output Weights cannot be NULL"); |
| 1323 | } |
| 1324 | layer->m_PeepholeParameters.m_CellToForgetWeights = |
| 1325 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToForgetWeights)); |
| 1326 | layer->m_PeepholeParameters.m_CellToOutputWeights = |
| 1327 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToOutputWeights)); |
| 1328 | } |
| 1329 | return layer; |
| 1330 | } |
| 1331 | |
Francis Murtagh | e7a86a4 | 2018-08-29 12:42:10 +0100 | [diff] [blame] | 1332 | IConnectableLayer* Network::AddDivisionLayer(const char* name) |
| 1333 | { |
| 1334 | return m_Graph->AddLayer<DivisionLayer>(name); |
| 1335 | } |
| 1336 | |
David Beck | 1952622 | 2018-09-12 16:00:08 +0100 | [diff] [blame] | 1337 | IConnectableLayer* Network::AddSubtractionLayer(const char* name) |
| 1338 | { |
| 1339 | return m_Graph->AddLayer<SubtractionLayer>(name); |
| 1340 | } |
| 1341 | |
narpra01 | 32b9046 | 2018-09-13 11:07:48 +0100 | [diff] [blame] | 1342 | IConnectableLayer* Network::AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name) |
| 1343 | { |
| 1344 | return m_Graph->AddLayer<MeanLayer>(meanDescriptor,name); |
| 1345 | } |
| 1346 | |
Mohamed Nour Abouelseoud | 5662c20 | 2018-09-24 13:30:09 +0100 | [diff] [blame] | 1347 | IConnectableLayer* Network::AddPadLayer(const PadDescriptor& padDescriptor, const char* name) |
| 1348 | { |
| 1349 | return m_Graph->AddLayer<PadLayer>(padDescriptor,name); |
| 1350 | } |
| 1351 | |
Derek Lamberti | a9cca6a | 2019-03-25 15:41:58 +0000 | [diff] [blame] | 1352 | IConnectableLayer *Network::AddQuantizeLayer(const char *name) |
| 1353 | { |
| 1354 | return m_Graph->AddLayer<QuantizeLayer>(name); |
| 1355 | } |
| 1356 | |
Nattapat Chaimanowong | e4294fd | 2019-03-28 09:56:53 +0000 | [diff] [blame] | 1357 | IConnectableLayer* Network::AddDequantizeLayer(const char* name) |
| 1358 | { |
| 1359 | return m_Graph->AddLayer<DequantizeLayer>(name); |
| 1360 | } |
| 1361 | |
Conor Kennedy | 430b5d8 | 2018-11-14 15:28:28 +0000 | [diff] [blame] | 1362 | IConnectableLayer* Network::AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor, |
| 1363 | const char* name) |
| 1364 | { |
| 1365 | return m_Graph->AddLayer<StridedSliceLayer>(stridedSliceDescriptor, name); |
| 1366 | } |
| 1367 | |
Matteo Martincigh | 59a950c | 2018-12-13 12:48:25 +0000 | [diff] [blame] | 1368 | IConnectableLayer* Network::AddGreaterLayer(const char* name) |
| 1369 | { |
| 1370 | return m_Graph->AddLayer<GreaterLayer>(name); |
| 1371 | } |
| 1372 | |
FrancisMurtagh | 2099595 | 2018-12-17 12:11:36 +0000 | [diff] [blame] | 1373 | IConnectableLayer* Network::AddEqualLayer(const char* name) |
| 1374 | { |
jimfly01 | 84c70e6 | 2018-12-19 13:14:46 +0000 | [diff] [blame] | 1375 | return m_Graph->AddLayer<EqualLayer>(name); |
FrancisMurtagh | 2099595 | 2018-12-17 12:11:36 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
Mohamed Nour Abouelseoud | a1d3c6a | 2018-12-27 12:39:16 +0000 | [diff] [blame] | 1378 | IConnectableLayer* Network::AddRsqrtLayer(const char * name) |
| 1379 | { |
| 1380 | return m_Graph->AddLayer<RsqrtLayer>(name); |
| 1381 | } |
| 1382 | |
narpra01 | b89b05f | 2019-01-16 09:53:09 +0000 | [diff] [blame] | 1383 | IConnectableLayer* Network::AddGatherLayer(const char* name) |
| 1384 | { |
| 1385 | return m_Graph->AddLayer<GatherLayer>(name); |
| 1386 | } |
| 1387 | |
Nattapat Chaimanowong | 1f88630 | 2019-04-05 13:37:19 +0100 | [diff] [blame] | 1388 | IConnectableLayer* Network::AddMergeLayer(const char* name) |
| 1389 | { |
| 1390 | return m_Graph->AddLayer<MergeLayer>(name); |
| 1391 | } |
| 1392 | |
Sadik Armagan | eff363d | 2019-04-05 15:25:46 +0100 | [diff] [blame] | 1393 | IConnectableLayer* Network::AddSwitchLayer(const char* name) |
| 1394 | { |
| 1395 | return m_Graph->AddLayer<SwitchLayer>(name); |
| 1396 | } |
| 1397 | |
Matteo Martincigh | 0e406ee | 2019-06-12 15:42:18 +0100 | [diff] [blame] | 1398 | IConnectableLayer* Network::AddPreluLayer(const char* name) |
| 1399 | { |
| 1400 | return m_Graph->AddLayer<PreluLayer>(name); |
| 1401 | } |
| 1402 | |
Aron Virginas-Tar | 639fb04 | 2019-06-20 14:28:19 +0100 | [diff] [blame] | 1403 | IConnectableLayer* Network::AddTransposeConvolution2dLayer(const TransposeConvolution2dDescriptor& descriptor, |
| 1404 | const ConstTensor& weights, |
| 1405 | const Optional<ConstTensor>& biases, |
| 1406 | const char* name) |
| 1407 | { |
| 1408 | if (descriptor.m_BiasEnabled && !biases.has_value()) |
| 1409 | { |
| 1410 | throw InvalidArgumentException("AddTransposeConvolution2dLayer: Biases cannot be empty"); |
| 1411 | } |
| 1412 | |
| 1413 | const auto layer = m_Graph->AddLayer<TransposeConvolution2dLayer>(descriptor, name); |
| 1414 | |
| 1415 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 1416 | |
| 1417 | if (descriptor.m_BiasEnabled) |
| 1418 | { |
| 1419 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value()); |
| 1420 | } |
| 1421 | |
| 1422 | return layer; |
| 1423 | } |
| 1424 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 1425 | void Network::Accept(ILayerVisitor& visitor) const |
| 1426 | { |
| 1427 | for (auto layer : GetGraph()) |
| 1428 | { |
| 1429 | layer->Accept(visitor); |
| 1430 | }; |
| 1431 | } |
| 1432 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1433 | OptimizedNetwork::OptimizedNetwork(std::unique_ptr<Graph> graph) |
| 1434 | : m_Graph(std::move(graph)) |
| 1435 | { |
| 1436 | } |
| 1437 | |
| 1438 | OptimizedNetwork::~OptimizedNetwork() |
| 1439 | { |
| 1440 | } |
| 1441 | |
| 1442 | } // namespace armnn |