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" |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 11 | #include "SubGraphSelector.hpp" |
| 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> |
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> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 23 | |
| 24 | #include <fcntl.h> |
| 25 | #include <algorithm> |
| 26 | #include <fstream> |
| 27 | #include <memory> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 28 | #include <vector> |
| 29 | #include <algorithm> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 30 | |
| 31 | #include <boost/assert.hpp> |
| 32 | #include <boost/format.hpp> |
| 33 | #include <boost/log/trivial.hpp> |
| 34 | #include <boost/numeric/conversion/converter_policies.hpp> |
| 35 | #include <boost/cast.hpp> |
| 36 | |
| 37 | namespace armnn |
| 38 | { |
| 39 | |
| 40 | armnn::INetwork* INetwork::CreateRaw() |
| 41 | { |
| 42 | return new Network(); |
| 43 | } |
| 44 | |
| 45 | armnn::INetworkPtr INetwork::Create() |
| 46 | { |
| 47 | return INetworkPtr(CreateRaw(), &INetwork::Destroy); |
| 48 | } |
| 49 | |
| 50 | void INetwork::Destroy(INetwork* network) |
| 51 | { |
| 52 | delete boost::polymorphic_downcast<Network*>(network); |
| 53 | } |
| 54 | |
| 55 | Status Network::PrintGraph() |
| 56 | { |
| 57 | m_Graph->Print(); |
| 58 | return Status::Success; |
| 59 | } |
| 60 | |
| 61 | void IOptimizedNetwork::Destroy(IOptimizedNetwork* network) |
| 62 | { |
| 63 | delete boost::polymorphic_downcast<OptimizedNetwork*>(network); |
| 64 | } |
| 65 | |
| 66 | Status OptimizedNetwork::PrintGraph() |
| 67 | { |
| 68 | m_Graph->Print(); |
| 69 | return Status::Success; |
| 70 | } |
| 71 | |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 72 | Status OptimizedNetwork::SerializeToDot(std::ostream& stream) const |
| 73 | { |
| 74 | return m_Graph->SerializeToDot(stream); |
| 75 | } |
| 76 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 77 | struct OptimizationResult |
| 78 | { |
| 79 | bool m_Warning; |
| 80 | bool m_Error; |
| 81 | |
| 82 | OptimizationResult() |
| 83 | : m_Warning(false) |
| 84 | , m_Error(false) |
| 85 | {} |
| 86 | }; |
| 87 | |
| 88 | void ReportError(const std::string& errorMessage, |
| 89 | Optional<std::vector<std::string>&> errorMessages) |
| 90 | { |
| 91 | std::stringstream fullErrorMessage; |
| 92 | fullErrorMessage << "ERROR: " << errorMessage; |
| 93 | BOOST_LOG_TRIVIAL(warning) << fullErrorMessage.str(); |
| 94 | if (errorMessages) |
| 95 | { |
| 96 | errorMessages.value().push_back(fullErrorMessage.str()); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void ReportWarning(const std::string& warningMessage, |
| 101 | Optional<std::vector<std::string>&> warningMessages) |
| 102 | { |
| 103 | std::stringstream fullWarningMessage; |
| 104 | fullWarningMessage << "WARNING: " << warningMessage; |
| 105 | BOOST_LOG_TRIVIAL(warning) << fullWarningMessage.str(); |
| 106 | if (warningMessages) |
| 107 | { |
| 108 | warningMessages.value().push_back(fullWarningMessage.str()); |
| 109 | } |
| 110 | } |
| 111 | |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 112 | bool CheckScaleSetOnQuantizedType(Layer* layer, Optional<std::vector<std::string>&> errMessages) |
| 113 | { |
| 114 | bool noErrors = true; |
| 115 | unsigned int numOutputs = layer->GetNumOutputSlots(); |
| 116 | for (unsigned int i = 0; i < numOutputs; i++) { |
| 117 | const OutputSlot &outputSlot = layer->GetOutputSlot(i); |
| 118 | const TensorInfo &info = outputSlot.GetTensorInfo(); |
| 119 | if (DataType::QuantisedAsymm8 == info.GetDataType()) { |
| 120 | if (0.f == info.GetQuantizationScale()) { |
| 121 | noErrors = false; |
| 122 | std::stringstream ss; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 123 | ss << "output " << i << " of layer " << GetLayerTypeAsCString(layer->GetType()) |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 124 | << " (" << layer->GetNameStr() << ") is of type" |
| 125 | << " Quantized 8 bit but its scale parameter has not been set"; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 126 | ReportError(ss.str(), errMessages); |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |
| 130 | return noErrors; |
| 131 | } |
| 132 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 133 | OptimizationResult AssignBackends(OptimizedNetwork* optNetObjPtr, |
| 134 | BackendSettings& backendSettings, |
| 135 | Graph::Iterator& firstLayer, |
| 136 | Graph::Iterator& lastLayer, |
| 137 | Optional<std::vector<std::string>&> errMessages) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 138 | { |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 139 | OptimizationResult result; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 140 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 141 | // Helper lambda to compose meaningful error message before returning with error |
| 142 | auto ReturnWithError = [&](const Layer* layer) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 143 | { |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 144 | std::stringstream failureMsg; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 145 | failureMsg << "Layer of type " << GetLayerTypeAsCString(layer->GetType()) |
| 146 | << " is not supported on any preferred backend " << backendSettings.m_PreferredBackends; |
| 147 | ReportError(failureMsg.str(), errMessages); |
| 148 | |
| 149 | result.m_Error = true; |
| 150 | return result; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 151 | }; |
| 152 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 153 | auto availablePreferredBackends = backendSettings.GetAvailablePreferredBackends(); |
| 154 | if (availablePreferredBackends.empty()) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 155 | { |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 156 | std::stringstream failureMsg; |
| 157 | failureMsg << "No preferred backends are available"; |
| 158 | ReportError(failureMsg.str(), errMessages); |
| 159 | |
| 160 | result.m_Error = true; |
| 161 | return result; |
| 162 | } |
| 163 | |
| 164 | for (auto it = firstLayer; it != lastLayer; ++it) |
| 165 | { |
| 166 | auto layer = *it; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 167 | DataType dataType = layer->GetDataType(); |
| 168 | std::string reasonIfUnsupported; |
| 169 | bool found = false; |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 170 | if (!CheckScaleSetOnQuantizedType(layer, errMessages)) |
| 171 | { |
| 172 | // don't bomb immediately, find all the quantized outputs |
| 173 | // which haven't had a scale set and report them all back. |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 174 | result.m_Error = true; |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 175 | } |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 176 | |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 177 | for (const auto& backend : availablePreferredBackends) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 178 | { |
| 179 | // need to set the compute device on the layer |
| 180 | // before we can check if it is supported |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 181 | layer->SetBackendId(backend); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 182 | if (!IWorkloadFactory::IsLayerSupported(*layer, dataType, reasonIfUnsupported)) |
| 183 | { |
| 184 | if (dataType == DataType::Float16) |
| 185 | { |
| 186 | if (IWorkloadFactory::IsLayerSupported(*layer, DataType::Float32, reasonIfUnsupported) |
| 187 | && layer->GetType() != LayerType::ConvertFp32ToFp16 |
| 188 | && layer->GetType() != LayerType::ConvertFp16ToFp32) |
| 189 | { |
| 190 | // Insert FP16 -> FP32 conversion layer before current layer |
| 191 | std::vector<ConvertFp16ToFp32Layer*> convertFp16ToFp32Layers = |
| 192 | InsertConvertFp16ToFp32LayersBefore(optNetObjPtr->GetGraph(), *layer); |
| 193 | |
| 194 | // Insert FP32 -> FP16 conversion layer after current layer |
| 195 | std::vector<ConvertFp32ToFp16Layer*> convertFp32ToFp16Layers = |
| 196 | InsertConvertFp32ToFp16LayersAfter(optNetObjPtr->GetGraph(), *layer); |
| 197 | |
| 198 | // Assign a supported backend to the newly introduced conversion layers |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 199 | auto AssignFirstSupportedBackend = [&](Layer* layer, BackendId preferredBackend) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 200 | { |
| 201 | bool supportedBackendFound = false; |
| 202 | std::string reasonIfUnsupported; |
| 203 | |
| 204 | // Try preferred backend first |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 205 | layer->SetBackendId(preferredBackend); |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 206 | if (IWorkloadFactory::IsLayerSupported(*layer, |
| 207 | EmptyOptional(), |
| 208 | reasonIfUnsupported)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 209 | { |
| 210 | supportedBackendFound = true; |
| 211 | } |
| 212 | else |
| 213 | { |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 214 | for (const auto& backend : availablePreferredBackends) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 215 | { |
| 216 | // Skip preferred backend (we already determined that it is not supported) |
| 217 | if (backend == preferredBackend) |
| 218 | { |
| 219 | continue; |
| 220 | } |
| 221 | |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 222 | layer->SetBackendId(backend); |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 223 | if (IWorkloadFactory::IsLayerSupported(*layer, |
| 224 | EmptyOptional(), |
| 225 | reasonIfUnsupported)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 226 | { |
| 227 | supportedBackendFound = true; |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | return supportedBackendFound; |
| 234 | }; |
| 235 | |
| 236 | for (ConvertFp16ToFp32Layer* convertLayer : convertFp16ToFp32Layers) |
| 237 | { |
| 238 | if (!AssignFirstSupportedBackend(convertLayer, backend)) |
| 239 | { |
| 240 | return ReturnWithError(convertLayer); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | for (ConvertFp32ToFp16Layer* convertLayer : convertFp32ToFp16Layers) |
| 245 | { |
| 246 | if (!AssignFirstSupportedBackend(convertLayer, backend)) |
| 247 | { |
| 248 | return ReturnWithError(convertLayer); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | found = true; |
| 253 | break; |
| 254 | } |
| 255 | } |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 256 | std::stringstream warningMsg; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 257 | warningMsg << "Layer of type " << GetLayerTypeAsCString(layer->GetType()) |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 258 | << " is not supported on requested backend " << layer->GetBackendId().Get() |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 259 | << " for data type " << GetDataTypeName(dataType) |
| 260 | << " (reason: " << reasonIfUnsupported |
| 261 | << "), falling back to the next backend."; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 262 | ReportWarning(warningMsg.str(), errMessages); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 263 | } |
| 264 | else |
| 265 | { |
| 266 | found = true; |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 267 | backendSettings.m_SelectedBackends.insert(backend); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 268 | break; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // 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] | 273 | if (!found) |
| 274 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 275 | // NOTE: if the layer is not an operation queue type AND we have not got CpuRef as a |
| 276 | // fallback we should set the compute device on the layer to CpuRef (these are not |
| 277 | // available as accelerated operations, or are only available under certain |
| 278 | // conditions, currently they comprise MemCopy, Constant, Permute) |
| 279 | armnn::LayerType layerType = layer->GetType(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 280 | if (!backendSettings.IsCpuRefUsed() && (layerType == armnn::LayerType::MemCopy || |
| 281 | layerType == armnn::LayerType::Constant || |
| 282 | layerType == armnn::LayerType::Permute)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 283 | { |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 284 | BackendId cpuBackendId(armnn::Compute::CpuRef); |
| 285 | layer->SetBackendId(cpuBackendId); |
| 286 | backendSettings.m_SelectedBackends.insert(cpuBackendId); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 287 | } |
| 288 | else |
| 289 | { |
| 290 | return ReturnWithError(layer); |
| 291 | } |
| 292 | } |
| 293 | } |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 294 | |
| 295 | return result; |
| 296 | } |
| 297 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 298 | OptimizationResult AssignBackends(OptimizedNetwork* optNetObjPtr, |
| 299 | BackendSettings& backendSettings, |
| 300 | SubGraph& subGraph, |
| 301 | Optional<std::vector<std::string>&> errMessages) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 302 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 303 | Graph::Iterator firstLayer = subGraph.begin(); |
| 304 | Graph::Iterator lastLayer = subGraph.end(); |
| 305 | return AssignBackends(optNetObjPtr, |
| 306 | backendSettings, |
| 307 | firstLayer, |
| 308 | lastLayer, |
| 309 | errMessages); |
| 310 | } |
| 311 | |
| 312 | OptimizationResult ApplyBackendOptimizations(OptimizedNetwork* optNetObjPtr, |
| 313 | BackendSettings& backendSettings, |
| 314 | Optional<std::vector<std::string>&> errMessages) |
| 315 | { |
| 316 | BOOST_ASSERT(optNetObjPtr); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 317 | |
| 318 | OptimizationResult result; |
| 319 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 320 | // Get the optimized graph |
| 321 | Graph& optGraph = optNetObjPtr->GetGraph(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 322 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 323 | // Get the entire graph as a sub-graph |
| 324 | SubGraph mainSubGraph(optGraph); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 325 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 326 | // Run backend specific optimizations |
| 327 | auto const& backendRegistry = BackendRegistryInstance(); |
| 328 | for (auto&& selectedBackend : backendSettings.m_SelectedBackends) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 329 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 330 | auto backendFactory = backendRegistry.GetFactory(selectedBackend); |
| 331 | auto backendObjPtr = backendFactory(); |
| 332 | BOOST_ASSERT(backendObjPtr); |
| 333 | |
| 334 | // Select sub-graphs based on backend |
| 335 | SubGraphSelector::SubGraphs subGraphs = |
| 336 | SubGraphSelector::SelectSubGraphs(mainSubGraph, |
| 337 | // Select layers assigned to the requested backend |
| 338 | [&backendObjPtr](const Layer& layer) |
| 339 | { |
| 340 | return layer.GetType() != LayerType::Input && |
| 341 | layer.GetType() != LayerType::Output && |
| 342 | layer.GetBackendId() == backendObjPtr->GetId(); |
| 343 | }); |
| 344 | if (subGraphs.empty()) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 345 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 346 | // No sub-graphs found, try with next selected backend |
| 347 | continue; |
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 | |
| 350 | // Try to optimize each sub-graph |
| 351 | for (auto& subGraph : subGraphs) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 352 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 353 | // Try to optimize the current sub-graph |
| 354 | bool optimizationAttempted = false; |
| 355 | SubGraph::SubGraphPtr optSubGraph = backendObjPtr->OptimizeSubGraph(*subGraph, optimizationAttempted); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 356 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 357 | // Check if the optimization has been attempted |
| 358 | if (!optimizationAttempted) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 359 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 360 | // No optimization attempted, keep the current sub-graph as it is and move to the next one |
| 361 | continue; |
| 362 | } |
| 363 | |
| 364 | // Optimization attempted, check the resulting optimized sub-graph |
| 365 | if (optSubGraph) |
| 366 | { |
| 367 | // Sub-graph optimized, substitute the sub-graph with the new optimized one in the main optimized graph |
| 368 | optGraph.SubstituteSubGraph(std::move(subGraph), *optSubGraph); |
| 369 | |
| 370 | // Assign the current backend to the optimized sub-graph |
| 371 | std::for_each(optSubGraph->begin(), optSubGraph->end(), [&selectedBackend](Layer* l) |
| 372 | { |
| 373 | BOOST_ASSERT(l); |
| 374 | l->SetBackendId(selectedBackend); |
| 375 | }); |
| 376 | |
| 377 | // Recreate the sub-graph representing the entire graph |
| 378 | mainSubGraph.Update(optGraph); |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | // An error occurred: the optimization was attempted but not performed, try different backends |
| 383 | std::stringstream warningMsg; |
| 384 | warningMsg << "Sub-graph failed to get optimized on " << backendObjPtr->GetId() << ". " |
| 385 | << "Re-assigning backends to " << subGraph->GetLayers().size() << " layers inside sub-graph"; |
| 386 | ReportWarning(warningMsg.str(), errMessages); |
| 387 | |
| 388 | // Failed to optimize the given sub-graph, re-assign the sub-graph layers to other available backends |
| 389 | if (!backendObjPtr->GetId().IsCpuRef()) |
| 390 | { |
| 391 | // Add the current backend to the list of backends to ignore |
| 392 | backendSettings.m_IgnoredBackends.insert(backendObjPtr->GetId()); |
| 393 | } |
| 394 | OptimizationResult reassignmentResult = AssignBackends(optNetObjPtr, |
| 395 | backendSettings, |
| 396 | *subGraph, |
| 397 | errMessages); |
| 398 | if (reassignmentResult.m_Error) |
| 399 | { |
| 400 | // Failed to re-assign one of the remaining backends to each layer of the sub-graph |
| 401 | result.m_Error = true; |
| 402 | return result; |
| 403 | } |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | return result; |
| 409 | } |
| 410 | |
| 411 | IOptimizedNetworkPtr Optimize(const INetwork& inNetwork, |
| 412 | const std::vector<BackendId>& backendPreferences, |
| 413 | const IDeviceSpec& deviceSpec, |
| 414 | const OptimizerOptions& options, |
| 415 | Optional<std::vector<std::string>&> errMessages) |
| 416 | { |
| 417 | if (backendPreferences.empty()) |
| 418 | { |
| 419 | throw armnn::InvalidArgumentException("Invoked Optimize with no backends specified"); |
| 420 | } |
| 421 | |
| 422 | const Network& network = *boost::polymorphic_downcast<const Network*>(&inNetwork); |
| 423 | std::unique_ptr<Graph> graph = std::make_unique<Graph>(network.GetGraph()); |
| 424 | |
| 425 | auto optNet = IOptimizedNetworkPtr(new OptimizedNetwork(std::move(graph)), &IOptimizedNetwork::Destroy); |
| 426 | |
| 427 | OptimizedNetwork* optNetObjPtr = boost::polymorphic_downcast<OptimizedNetwork*>(optNet.get()); |
| 428 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 429 | // Get the optimized graph |
| 430 | Graph& optGraph = optNetObjPtr->GetGraph(); |
| 431 | |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 432 | // Perform optimisation passes |
| 433 | using namespace optimizations; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 434 | Optimizer::Pass(optGraph, MakeOptimizations(SquashEqualPermuteSiblings(), |
| 435 | SquashEqualReshapeSiblings(), |
| 436 | OptimizeInversePermutes(), |
| 437 | MovePermuteUp(), |
| 438 | PermuteAsReshape(), |
| 439 | OptimizeConsecutiveReshapes())); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 440 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 441 | // Infer the tensor infos for all output slots. Throws an exception on failure |
| 442 | optGraph.InferTensorInfos(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 443 | |
| 444 | // If Fp32 to Fp16 optimization is set convert Fp32 network to Fp16 |
| 445 | if (options.m_ReduceFp32ToFp16) |
| 446 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 447 | Optimizer::Pass(optGraph, MakeOptimizations(Fp32NetworkToFp16Converter())); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // Initialize backend settings |
| 451 | BackendSettings backendSettings(backendPreferences, deviceSpec); |
| 452 | if (backendSettings.GetAvailablePreferredBackends().empty()) |
| 453 | { |
| 454 | std::stringstream failureMsg; |
| 455 | failureMsg << "None of the preferred backends " << backendPreferences |
| 456 | << " are supported. Current platform provides " << backendSettings.m_SupportedBackends; |
| 457 | ReportError(failureMsg.str(), errMessages); |
| 458 | return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy); |
| 459 | } |
| 460 | |
| 461 | // Assign an available backend to each layer |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 462 | Graph::Iterator firstLayer = optGraph.begin(); |
| 463 | Graph::Iterator lastLayer = optGraph.end(); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 464 | OptimizationResult assigBackendsResult = AssignBackends(optNetObjPtr, |
| 465 | backendSettings, |
| 466 | firstLayer, |
| 467 | lastLayer, |
| 468 | errMessages); |
| 469 | if (assigBackendsResult.m_Error) |
| 470 | { |
| 471 | // Failed to assign a backend to each layer |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 472 | return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy); |
| 473 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 474 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 475 | Optimizer::Pass(optGraph, MakeOptimizations(OptimizeInverseConversionsFp16(), |
| 476 | OptimizeInverseConversionsFp32())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 477 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 478 | // Apply the backend-specific optimizations |
| 479 | OptimizationResult backendOptimizationResult = ApplyBackendOptimizations(optNetObjPtr, |
| 480 | backendSettings, |
| 481 | errMessages); |
| 482 | if (backendOptimizationResult.m_Error) |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 483 | { |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 484 | // Failed to apply the backend-specific optimizations |
| 485 | return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy); |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 488 | // If the debug flag is set, then insert a DebugLayer after each layer |
| 489 | // Doing this after applying the backend optimizations as they might have changed some layers |
| 490 | if (options.m_Debug) |
| 491 | { |
| 492 | Optimizer::Pass(optGraph, MakeOptimizations(InsertDebugLayer())); |
| 493 | } |
| 494 | |
| 495 | optGraph.AddCopyLayers(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 496 | |
| 497 | // Convert constants |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 498 | Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsFloatToHalf())); |
| 499 | Optimizer::Pass(optGraph, MakeOptimizations(ConvertConstantsHalfToFloat())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 500 | |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 501 | // Run backend specific optimizations |
Matteo Martincigh | 4912402 | 2019-01-11 13:25:59 +0000 | [diff] [blame] | 502 | for (auto&& chosenBackend : backendSettings.m_SelectedBackends) |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 503 | { |
| 504 | auto factoryFun = BackendRegistryInstance().GetFactory(chosenBackend); |
| 505 | auto backendPtr = factoryFun(); |
| 506 | BOOST_ASSERT(backendPtr.get() != nullptr); |
| 507 | |
| 508 | auto backendSpecificOptimizations = backendPtr->GetOptimizations(); |
| 509 | if (!backendSpecificOptimizations.empty()) |
| 510 | { |
| 511 | Optimizer::Pass(optNetObjPtr->GetGraph(), backendSpecificOptimizations); |
| 512 | } |
| 513 | } |
| 514 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 515 | return optNet; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | Network::Network() |
| 519 | : m_Graph(std::make_unique<Graph>()) |
| 520 | { |
| 521 | } |
| 522 | |
| 523 | Network::~Network() |
| 524 | { |
| 525 | } |
| 526 | |
| 527 | IConnectableLayer* Network::AddInputLayer(LayerBindingId id, const char* name) |
| 528 | { |
| 529 | return m_Graph->AddLayer<InputLayer>(id, name); |
| 530 | } |
| 531 | |
Éanna Ó Catháin | 4e1e136 | 2018-11-12 11:36:34 +0000 | [diff] [blame] | 532 | IConnectableLayer* Network::AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor, |
| 533 | const char* name) |
| 534 | { |
| 535 | return m_Graph->AddLayer<BatchToSpaceNdLayer>(batchToSpaceNdDescriptor, name); |
| 536 | } |
| 537 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 538 | IConnectableLayer* Network::AddFullyConnectedLayerImpl(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 539 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 540 | const Optional<ConstTensor>& biases, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 541 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 542 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 543 | if (fullyConnectedDescriptor.m_BiasEnabled && !biases.has_value()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 544 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 545 | throw InvalidArgumentException("AddFullyConnectedLayer: biases cannot be empty"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | const auto layer = m_Graph->AddLayer<FullyConnectedLayer>(fullyConnectedDescriptor, name); |
| 549 | |
| 550 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 551 | |
| 552 | if (fullyConnectedDescriptor.m_BiasEnabled) |
| 553 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 554 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | return layer; |
| 558 | } |
| 559 | |
| 560 | IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 561 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 562 | const Optional<ConstTensor>& biases, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 563 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 564 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 565 | return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, biases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 568 | /// @deprecated |
| 569 | IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
| 570 | const ConstTensor& weights, |
| 571 | const char* name) |
| 572 | { |
| 573 | Optional<ConstTensor> biases = EmptyOptional(); |
| 574 | return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, biases, name); |
| 575 | } |
| 576 | |
| 577 | /// @deprecated |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 578 | IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 579 | const ConstTensor& weights, |
| 580 | const ConstTensor& biases, |
| 581 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 582 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 583 | Optional<ConstTensor> optionalBiases(biases); |
| 584 | return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, optionalBiases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | IConnectableLayer* Network::AddConvolution2dLayerImpl(const Convolution2dDescriptor& convolution2dDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 588 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 589 | const Optional<ConstTensor>& biases, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 590 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 591 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 592 | if (convolution2dDescriptor.m_BiasEnabled && !biases.has_value()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 593 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 594 | throw InvalidArgumentException("AddConvolution2dLayer: biases cannot be empty"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | const auto layer = m_Graph->AddLayer<Convolution2dLayer>(convolution2dDescriptor, name); |
| 598 | |
| 599 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 600 | |
| 601 | if (convolution2dDescriptor.m_BiasEnabled) |
| 602 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 603 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | return layer; |
| 607 | } |
| 608 | |
| 609 | IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 610 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 611 | const Optional<ConstTensor>& biases, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 612 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 613 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 614 | return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 615 | } |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 616 | |
| 617 | /// @deprecated |
| 618 | IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, |
| 619 | const ConstTensor& weights, |
| 620 | const char* name) |
| 621 | { |
| 622 | Optional<ConstTensor> biases = EmptyOptional(); |
| 623 | return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name); |
| 624 | } |
| 625 | |
| 626 | /// @deprecated |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 627 | IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 628 | const ConstTensor& weights, |
| 629 | const ConstTensor& biases, |
| 630 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 631 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 632 | Optional<ConstTensor> optionalBiases(biases); |
| 633 | return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, optionalBiases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayerImpl( |
| 637 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 638 | const ConstTensor& weights, |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 639 | const Optional<ConstTensor>& biases, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 640 | const char* name) |
| 641 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 642 | if (convolution2dDescriptor.m_BiasEnabled && !biases.has_value()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 643 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 644 | throw InvalidArgumentException("AddDepthwiseConvolution2dLayer: biases cannot be empty"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Matteo Martincigh | 3d6898c | 2019-01-15 16:11:44 +0000 | [diff] [blame] | 647 | const auto layer = m_Graph->AddLayer<DepthwiseConvolution2dLayer>(convolution2dDescriptor, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 648 | |
| 649 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 650 | |
| 651 | if (convolution2dDescriptor.m_BiasEnabled) |
| 652 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 653 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(biases.value()); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | return layer; |
| 657 | } |
| 658 | |
| 659 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayer( |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 660 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 661 | const ConstTensor& weights, |
| 662 | const Optional<ConstTensor>& biases, |
| 663 | const char* name) |
| 664 | { |
| 665 | return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name); |
| 666 | } |
| 667 | |
| 668 | /// @deprecated |
| 669 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayer( |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 670 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 671 | const ConstTensor& weights, |
| 672 | const char* name) |
| 673 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 674 | Optional<ConstTensor> biases = EmptyOptional(); |
| 675 | return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, biases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 676 | } |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 677 | |
| 678 | /// @deprecated |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 679 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayer( |
| 680 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 681 | const ConstTensor& weights, |
| 682 | const ConstTensor& biases, |
| 683 | const char* name) |
| 684 | { |
Aron Virginas-Tar | ad40270 | 2019-02-22 17:03:44 +0000 | [diff] [blame^] | 685 | Optional<ConstTensor> optionalBiases(biases); |
| 686 | return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, optionalBiases, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Narumol Prangnawarat | 94dd5d8 | 2019-01-23 18:06:26 +0000 | [diff] [blame] | 689 | IConnectableLayer* Network::AddDetectionPostProcessLayer(const armnn::DetectionPostProcessDescriptor& descriptor, |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 690 | const ConstTensor& anchors, const char* name) |
Narumol Prangnawarat | 94dd5d8 | 2019-01-23 18:06:26 +0000 | [diff] [blame] | 691 | { |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 692 | const auto layer = m_Graph->AddLayer<DetectionPostProcessLayer>(descriptor, name); |
| 693 | |
| 694 | layer->m_Anchors = std::make_unique<ScopedCpuTensorHandle>(anchors); |
| 695 | |
| 696 | return layer; |
Narumol Prangnawarat | 94dd5d8 | 2019-01-23 18:06:26 +0000 | [diff] [blame] | 697 | } |
| 698 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 699 | IConnectableLayer* Network::AddPermuteLayer(const PermuteDescriptor& permuteDescriptor, |
| 700 | const char* name) |
| 701 | { |
| 702 | return m_Graph->AddLayer<PermuteLayer>(permuteDescriptor, name); |
| 703 | } |
| 704 | |
| 705 | IConnectableLayer* Network::AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor, |
| 706 | const char* name) |
| 707 | { |
| 708 | return m_Graph->AddLayer<Pooling2dLayer>(pooling2dDescriptor, name); |
| 709 | } |
| 710 | |
| 711 | IConnectableLayer* Network::AddActivationLayer(const ActivationDescriptor& activationDescriptor, |
| 712 | const char* name) |
| 713 | { |
| 714 | return m_Graph->AddLayer<ActivationLayer>(activationDescriptor, name); |
| 715 | } |
| 716 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 717 | IConnectableLayer* Network::AddNormalizationLayer(const NormalizationDescriptor& |
| 718 | normalizationDescriptor, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 719 | const char* name) |
| 720 | { |
| 721 | return m_Graph->AddLayer<NormalizationLayer>(normalizationDescriptor, name); |
| 722 | } |
| 723 | |
| 724 | IConnectableLayer* Network::AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor, |
| 725 | const char* name) |
| 726 | { |
| 727 | return m_Graph->AddLayer<SoftmaxLayer>(softmaxDescriptor, name); |
| 728 | } |
| 729 | |
| 730 | IConnectableLayer* Network::AddSplitterLayer(const ViewsDescriptor& splitterDescriptor, |
| 731 | const char* name) |
| 732 | { |
| 733 | return m_Graph->AddLayer<SplitterLayer>(splitterDescriptor, name); |
| 734 | } |
| 735 | |
Nattapat Chaimanowong | 5a4304a | 2018-11-28 10:44:37 +0000 | [diff] [blame] | 736 | IConnectableLayer* Network::AddMaximumLayer(const char* name) |
| 737 | { |
| 738 | return m_Graph->AddLayer<MaximumLayer>(name); |
| 739 | } |
| 740 | |
Éanna Ó Catháin | 20e5880 | 2018-12-04 10:29:06 +0000 | [diff] [blame] | 741 | IConnectableLayer* Network::AddMinimumLayer(const char* name) |
| 742 | { |
| 743 | return m_Graph->AddLayer<MinimumLayer>(name); |
| 744 | } |
| 745 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 746 | IConnectableLayer* Network::AddMergerLayer(const OriginsDescriptor& mergerDescriptor, |
| 747 | const char* name) |
| 748 | { |
| 749 | return m_Graph->AddLayer<MergerLayer>(mergerDescriptor, name); |
| 750 | } |
| 751 | |
| 752 | IConnectableLayer* Network::AddAdditionLayer(const char* name) |
| 753 | { |
| 754 | return m_Graph->AddLayer<AdditionLayer>(name); |
| 755 | } |
| 756 | |
| 757 | IConnectableLayer* Network::AddMultiplicationLayer(const char* name) |
| 758 | { |
| 759 | return m_Graph->AddLayer<MultiplicationLayer>(name); |
| 760 | } |
| 761 | |
| 762 | IConnectableLayer* Network::AddOutputLayer(LayerBindingId id, const char* name) |
| 763 | { |
| 764 | return m_Graph->AddLayer<OutputLayer>(id, name); |
| 765 | } |
| 766 | |
| 767 | IConnectableLayer* Network::AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc, |
| 768 | const ConstTensor& mean, |
| 769 | const ConstTensor& variance, |
| 770 | const ConstTensor& beta, |
| 771 | const ConstTensor& gamma, |
| 772 | const char* name) |
| 773 | { |
| 774 | const auto layer = m_Graph->AddLayer<BatchNormalizationLayer>(desc, name); |
| 775 | |
| 776 | layer->m_Mean = std::make_unique<ScopedCpuTensorHandle>(mean); |
| 777 | layer->m_Variance = std::make_unique<ScopedCpuTensorHandle>(variance); |
| 778 | layer->m_Beta = std::make_unique<ScopedCpuTensorHandle>(beta); |
| 779 | layer->m_Gamma = std::make_unique<ScopedCpuTensorHandle>(gamma); |
| 780 | |
| 781 | return layer; |
| 782 | } |
| 783 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 784 | IConnectableLayer* Network::AddResizeBilinearLayer(const ResizeBilinearDescriptor& |
| 785 | resizeDescriptor, const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 786 | { |
| 787 | return m_Graph->AddLayer<ResizeBilinearLayer>(resizeDescriptor,name); |
| 788 | } |
| 789 | |
Matteo Martincigh | bcd3c85 | 2018-09-28 14:14:12 +0100 | [diff] [blame] | 790 | IConnectableLayer* Network::AddL2NormalizationLayer(const L2NormalizationDescriptor& desc, |
| 791 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 792 | { |
Matteo Martincigh | bcd3c85 | 2018-09-28 14:14:12 +0100 | [diff] [blame] | 793 | return m_Graph->AddLayer<L2NormalizationLayer>(desc, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | IConnectableLayer* Network::AddConstantLayer(const ConstTensor& input, const char* name) |
| 797 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 798 | auto layer = m_Graph->AddLayer<ConstantLayer>(name); |
| 799 | |
| 800 | layer->m_LayerOutput = std::make_unique<ScopedCpuTensorHandle>(input); |
| 801 | |
| 802 | return layer; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 803 | } |
| 804 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 805 | IConnectableLayer* Network::AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor, |
| 806 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 807 | { |
| 808 | return m_Graph->AddLayer<ReshapeLayer>(reshapeDescriptor, name); |
| 809 | } |
| 810 | |
Nattapat Chaimanowong | 207ef9a | 2018-11-02 10:57:25 +0000 | [diff] [blame] | 811 | IConnectableLayer* Network::AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor, |
| 812 | const char* name) |
| 813 | { |
| 814 | return m_Graph->AddLayer<SpaceToBatchNdLayer>(spaceToBatchNdDescriptor, name); |
| 815 | } |
| 816 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 817 | IConnectableLayer* Network::AddFloorLayer(const char* name) |
| 818 | { |
| 819 | return m_Graph->AddLayer<FloorLayer>(name); |
| 820 | } |
| 821 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 822 | IConnectableLayer* Network::AddLstmLayer(const LstmDescriptor& descriptor, |
| 823 | const LstmInputParams& params, |
| 824 | const char* name) |
| 825 | { |
| 826 | const auto layer = m_Graph->AddLayer<LstmLayer>(descriptor, name); |
| 827 | |
| 828 | //Lstm Basic Parameters |
| 829 | layer->m_BasicParameters.m_InputToForgetWeights = |
| 830 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToForgetWeights)); |
| 831 | layer->m_BasicParameters.m_InputToCellWeights = |
| 832 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToCellWeights)); |
| 833 | layer->m_BasicParameters.m_InputToOutputWeights = |
| 834 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToOutputWeights)); |
| 835 | layer->m_BasicParameters.m_RecurrentToForgetWeights = |
| 836 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToForgetWeights)); |
| 837 | layer->m_BasicParameters.m_RecurrentToCellWeights = |
| 838 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToCellWeights)); |
| 839 | layer->m_BasicParameters.m_RecurrentToOutputWeights = |
| 840 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToOutputWeights)); |
| 841 | layer->m_BasicParameters.m_ForgetGateBias = |
| 842 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetGateBias)); |
| 843 | layer->m_BasicParameters.m_CellBias = |
| 844 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellBias)); |
| 845 | layer->m_BasicParameters.m_OutputGateBias = |
| 846 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputGateBias)); |
| 847 | |
| 848 | //Lstm Cifg parameters |
| 849 | if(!descriptor.m_CifgEnabled) |
| 850 | { |
| 851 | if(params.m_InputToInputWeights == nullptr) |
| 852 | { |
| 853 | throw InvalidArgumentException("AddLstmLayer: Input To Input Weights cannot be NULL"); |
| 854 | } |
| 855 | if(params.m_RecurrentToInputWeights == nullptr) |
| 856 | { |
| 857 | throw InvalidArgumentException( |
| 858 | "AddLstmLayer: Recurrent To Input Weights cannot be NULL"); |
| 859 | } |
| 860 | if(params.m_InputGateBias == nullptr) |
| 861 | { |
| 862 | throw InvalidArgumentException("AddLstmLayer: Input Gate Bias cannot be NULL"); |
| 863 | } |
| 864 | layer->m_CifgParameters.m_InputToInputWeights = |
| 865 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToInputWeights)); |
| 866 | layer->m_CifgParameters.m_RecurrentToInputWeights = |
| 867 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToInputWeights)); |
| 868 | // In the VTS tests, cell-to-input weights may be null, even if the other CIFG params are not. |
| 869 | if(params.m_CellToInputWeights != nullptr) |
| 870 | { |
| 871 | layer->m_CifgParameters.m_CellToInputWeights = |
| 872 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToInputWeights)); |
| 873 | } |
| 874 | layer->m_CifgParameters.m_InputGateBias = |
| 875 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputGateBias)); |
| 876 | } |
| 877 | |
| 878 | //Lstm projection parameters |
| 879 | if(descriptor.m_ProjectionEnabled) |
| 880 | { |
| 881 | if(params.m_ProjectionWeights == nullptr) |
| 882 | { |
| 883 | throw InvalidArgumentException("AddLstmLayer: Projection Weights cannot be NULL"); |
| 884 | } |
| 885 | layer->m_ProjectionParameters.m_ProjectionWeights = |
| 886 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionWeights)); |
| 887 | if(params.m_ProjectionBias != nullptr) |
| 888 | { |
| 889 | layer->m_ProjectionParameters.m_ProjectionBias = |
| 890 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionBias)); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | //Lstm Peephole params |
| 895 | if(descriptor.m_PeepholeEnabled) |
| 896 | { |
| 897 | if(params.m_CellToForgetWeights == nullptr) |
| 898 | { |
| 899 | throw InvalidArgumentException("AddLstmLayer: Cell To Forget Weights cannot be NULL"); |
| 900 | } |
| 901 | if(params.m_CellToOutputWeights == nullptr) |
| 902 | { |
| 903 | throw InvalidArgumentException("AddLstmLayer: Cell To Output Weights cannot be NULL"); |
| 904 | } |
| 905 | layer->m_PeepholeParameters.m_CellToForgetWeights = |
| 906 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToForgetWeights)); |
| 907 | layer->m_PeepholeParameters.m_CellToOutputWeights = |
| 908 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToOutputWeights)); |
| 909 | } |
| 910 | return layer; |
| 911 | } |
| 912 | |
Francis Murtagh | e7a86a4 | 2018-08-29 12:42:10 +0100 | [diff] [blame] | 913 | IConnectableLayer* Network::AddDivisionLayer(const char* name) |
| 914 | { |
| 915 | return m_Graph->AddLayer<DivisionLayer>(name); |
| 916 | } |
| 917 | |
David Beck | 1952622 | 2018-09-12 16:00:08 +0100 | [diff] [blame] | 918 | IConnectableLayer* Network::AddSubtractionLayer(const char* name) |
| 919 | { |
| 920 | return m_Graph->AddLayer<SubtractionLayer>(name); |
| 921 | } |
| 922 | |
narpra01 | 32b9046 | 2018-09-13 11:07:48 +0100 | [diff] [blame] | 923 | IConnectableLayer* Network::AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name) |
| 924 | { |
| 925 | return m_Graph->AddLayer<MeanLayer>(meanDescriptor,name); |
| 926 | } |
| 927 | |
Mohamed Nour Abouelseoud | 5662c20 | 2018-09-24 13:30:09 +0100 | [diff] [blame] | 928 | IConnectableLayer* Network::AddPadLayer(const PadDescriptor& padDescriptor, const char* name) |
| 929 | { |
| 930 | return m_Graph->AddLayer<PadLayer>(padDescriptor,name); |
| 931 | } |
| 932 | |
Conor Kennedy | 430b5d8 | 2018-11-14 15:28:28 +0000 | [diff] [blame] | 933 | IConnectableLayer* Network::AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor, |
| 934 | const char* name) |
| 935 | { |
| 936 | return m_Graph->AddLayer<StridedSliceLayer>(stridedSliceDescriptor, name); |
| 937 | } |
| 938 | |
Matteo Martincigh | 59a950c | 2018-12-13 12:48:25 +0000 | [diff] [blame] | 939 | IConnectableLayer* Network::AddGreaterLayer(const char* name) |
| 940 | { |
| 941 | return m_Graph->AddLayer<GreaterLayer>(name); |
| 942 | } |
| 943 | |
FrancisMurtagh | 2099595 | 2018-12-17 12:11:36 +0000 | [diff] [blame] | 944 | IConnectableLayer* Network::AddEqualLayer(const char* name) |
| 945 | { |
jimfly01 | 84c70e6 | 2018-12-19 13:14:46 +0000 | [diff] [blame] | 946 | return m_Graph->AddLayer<EqualLayer>(name); |
FrancisMurtagh | 2099595 | 2018-12-17 12:11:36 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Mohamed Nour Abouelseoud | a1d3c6a | 2018-12-27 12:39:16 +0000 | [diff] [blame] | 949 | IConnectableLayer* Network::AddRsqrtLayer(const char * name) |
| 950 | { |
| 951 | return m_Graph->AddLayer<RsqrtLayer>(name); |
| 952 | } |
| 953 | |
narpra01 | b89b05f | 2019-01-16 09:53:09 +0000 | [diff] [blame] | 954 | IConnectableLayer* Network::AddGatherLayer(const char* name) |
| 955 | { |
| 956 | return m_Graph->AddLayer<GatherLayer>(name); |
| 957 | } |
| 958 | |
Mike Kelly | 8c1701a | 2019-02-11 17:01:27 +0000 | [diff] [blame] | 959 | void Network::Accept(ILayerVisitor& visitor) const |
| 960 | { |
| 961 | for (auto layer : GetGraph()) |
| 962 | { |
| 963 | layer->Accept(visitor); |
| 964 | }; |
| 965 | } |
| 966 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 967 | OptimizedNetwork::OptimizedNetwork(std::unique_ptr<Graph> graph) |
| 968 | : m_Graph(std::move(graph)) |
| 969 | { |
| 970 | } |
| 971 | |
| 972 | OptimizedNetwork::~OptimizedNetwork() |
| 973 | { |
| 974 | } |
| 975 | |
| 976 | } // namespace armnn |