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