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 | // |
| 5 | #include "Network.hpp" |
| 6 | #include "Graph.hpp" |
| 7 | #include "Layer.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 8 | #include "DeviceSpec.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 9 | #include "Optimizer.hpp" |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 10 | #include "optimizations/All.hpp" |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 11 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 12 | #include <backendsCommon/CpuTensorHandle.hpp> |
| 13 | #include <backendsCommon/WorkloadFactory.hpp> |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 14 | #include <backendsCommon/BackendRegistry.hpp> |
| 15 | #include <backendsCommon/IBackendInternal.hpp> |
David Beck | ac42efd | 2018-09-26 17:41:13 +0100 | [diff] [blame] | 16 | |
| 17 | #include <armnn/Exceptions.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 18 | #include <armnn/Utils.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 19 | #include <armnn/TypesUtils.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 20 | |
| 21 | #include <fcntl.h> |
| 22 | #include <algorithm> |
| 23 | #include <fstream> |
| 24 | #include <memory> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 25 | #include <vector> |
| 26 | #include <algorithm> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 27 | |
| 28 | #include <boost/assert.hpp> |
| 29 | #include <boost/format.hpp> |
| 30 | #include <boost/log/trivial.hpp> |
| 31 | #include <boost/numeric/conversion/converter_policies.hpp> |
| 32 | #include <boost/cast.hpp> |
| 33 | |
| 34 | namespace armnn |
| 35 | { |
| 36 | |
| 37 | armnn::INetwork* INetwork::CreateRaw() |
| 38 | { |
| 39 | return new Network(); |
| 40 | } |
| 41 | |
| 42 | armnn::INetworkPtr INetwork::Create() |
| 43 | { |
| 44 | return INetworkPtr(CreateRaw(), &INetwork::Destroy); |
| 45 | } |
| 46 | |
| 47 | void INetwork::Destroy(INetwork* network) |
| 48 | { |
| 49 | delete boost::polymorphic_downcast<Network*>(network); |
| 50 | } |
| 51 | |
| 52 | Status Network::PrintGraph() |
| 53 | { |
| 54 | m_Graph->Print(); |
| 55 | return Status::Success; |
| 56 | } |
| 57 | |
| 58 | void IOptimizedNetwork::Destroy(IOptimizedNetwork* network) |
| 59 | { |
| 60 | delete boost::polymorphic_downcast<OptimizedNetwork*>(network); |
| 61 | } |
| 62 | |
| 63 | Status OptimizedNetwork::PrintGraph() |
| 64 | { |
| 65 | m_Graph->Print(); |
| 66 | return Status::Success; |
| 67 | } |
| 68 | |
surmeh01 | bceff2f | 2018-03-29 16:29:27 +0100 | [diff] [blame] | 69 | Status OptimizedNetwork::SerializeToDot(std::ostream& stream) const |
| 70 | { |
| 71 | return m_Graph->SerializeToDot(stream); |
| 72 | } |
| 73 | |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 74 | bool CheckScaleSetOnQuantizedType(Layer* layer, Optional<std::vector<std::string>&> errMessages) |
| 75 | { |
| 76 | bool noErrors = true; |
| 77 | unsigned int numOutputs = layer->GetNumOutputSlots(); |
| 78 | for (unsigned int i = 0; i < numOutputs; i++) { |
| 79 | const OutputSlot &outputSlot = layer->GetOutputSlot(i); |
| 80 | const TensorInfo &info = outputSlot.GetTensorInfo(); |
| 81 | if (DataType::QuantisedAsymm8 == info.GetDataType()) { |
| 82 | if (0.f == info.GetQuantizationScale()) { |
| 83 | noErrors = false; |
| 84 | std::stringstream ss; |
| 85 | ss << "ERROR: output " << i << " of layer " << GetLayerTypeAsCString(layer->GetType()) |
| 86 | << " (" << layer->GetNameStr() << ") is of type" |
| 87 | << " Quantized 8 bit but its scale parameter has not been set"; |
| 88 | BOOST_LOG_TRIVIAL(warning) << ss.str() ; |
| 89 | if (errMessages) { |
| 90 | errMessages.value().push_back(ss.str()); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | return noErrors; |
| 96 | } |
| 97 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 98 | IOptimizedNetworkPtr Optimize(const INetwork& inNetwork, |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 99 | const std::vector<BackendId>& backendPreferences, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 100 | const IDeviceSpec& deviceSpec, |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 101 | const OptimizerOptions& options, |
| 102 | Optional<std::vector<std::string>&> errMessages) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 103 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 104 | if (backendPreferences.empty()) { |
| 105 | throw armnn::InvalidArgumentException("Invoked Optimize with no backends specified"); |
| 106 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 107 | const Network& network = *boost::polymorphic_downcast<const Network*>(&inNetwork); |
| 108 | std::unique_ptr<Graph> graph = std::make_unique<Graph>(network.GetGraph()); |
| 109 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 110 | auto optNet = IOptimizedNetworkPtr(new OptimizedNetwork(std::move(graph)), &IOptimizedNetwork::Destroy); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 111 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 112 | OptimizedNetwork* optNetObjPtr = boost::polymorphic_downcast<OptimizedNetwork*>(optNet.get()); |
| 113 | |
| 114 | // Perform optimisation passes |
| 115 | using namespace optimizations; |
| 116 | Optimizer::Pass(optNetObjPtr->GetGraph(), MakeOptimizations(SquashEqualPermuteSiblings(), |
| 117 | SquashEqualReshapeSiblings(), |
| 118 | OptimizeInversePermutes(), |
| 119 | MovePermuteUp(), |
| 120 | PermuteAsReshape(), |
| 121 | OptimizeConsecutiveReshapes())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 122 | |
| 123 | // Infer the tensor infos for all output slots. Throws an exception on failure. |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 124 | optNetObjPtr->GetGraph().InferTensorInfos(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 125 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 126 | // if Fp32 to Fp16 optimization is set convert Fp32 network to Fp16 |
| 127 | if (options.m_ReduceFp32ToFp16) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 128 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 129 | Optimizer::Pass(optNetObjPtr->GetGraph(), MakeOptimizations(Fp32NetworkToFp16Converter())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 130 | } |
| 131 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 132 | // We know that DeviceSpec should be the only implementation of IDeviceSpec. |
| 133 | const DeviceSpec& spec = *boost::polymorphic_downcast<const DeviceSpec*>(&deviceSpec); |
David Beck | 056be3c | 2018-10-22 13:16:00 +0100 | [diff] [blame] | 134 | auto const& supportedBackends = spec.GetSupportedBackends(); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 135 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 136 | // determine which of the preferred backends we have available for use |
| 137 | // and whether we have specified CpuRef as one of those backends. |
| 138 | bool cpuRefUsed = false; |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 139 | std::vector<BackendId> availablePreferredBackends; |
| 140 | for (const auto& backend : backendPreferences) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 141 | { |
| 142 | // Check if the backend is in the available backend devices. |
David Beck | 056be3c | 2018-10-22 13:16:00 +0100 | [diff] [blame] | 143 | if (supportedBackends.count(backend) > 0) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 144 | { |
| 145 | availablePreferredBackends.push_back(backend); |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 146 | if (backend == armnn::Compute::CpuRef) { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 147 | cpuRefUsed = true; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | if (availablePreferredBackends.empty()) { |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 152 | std::stringstream failureMsg; |
| 153 | failureMsg << "ERROR: None of the preferred backends " << backendPreferences |
David Beck | 056be3c | 2018-10-22 13:16:00 +0100 | [diff] [blame] | 154 | << " are supported. Current platform provides " << supportedBackends; |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 155 | BOOST_LOG_TRIVIAL(warning) << failureMsg.str(); |
| 156 | if (errMessages) { |
| 157 | errMessages.value().push_back(failureMsg.str()); |
| 158 | } |
| 159 | return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | auto ReturnWithError = [&](Layer* layer) |
| 163 | { |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 164 | std::stringstream failureMsg; |
| 165 | failureMsg << "ERROR: Layer of type " << GetLayerTypeAsCString(layer->GetType()) |
| 166 | << " is not supported on any preferred backend " << backendPreferences; |
| 167 | BOOST_LOG_TRIVIAL(warning) << failureMsg.str(); |
| 168 | if (errMessages) { |
| 169 | errMessages.value().push_back(failureMsg.str()); |
| 170 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 171 | return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy); |
| 172 | }; |
| 173 | |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 174 | // The backends that we choose to run layers on |
| 175 | std::unordered_set<BackendId> chosenBackends; |
| 176 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 177 | // Assign a compute device for all nodes |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 178 | bool bErrorFound = false; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 179 | for (auto&& layer : optNetObjPtr->GetGraph()) |
| 180 | { |
| 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. |
| 188 | bErrorFound = true; |
| 189 | } |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 190 | for (const auto& backend : availablePreferredBackends) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 191 | { |
| 192 | // need to set the compute device on the layer |
| 193 | // before we can check if it is supported |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 194 | layer->SetBackendId(backend); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 195 | if (!IWorkloadFactory::IsLayerSupported(*layer, dataType, reasonIfUnsupported)) |
| 196 | { |
| 197 | if (dataType == DataType::Float16) |
| 198 | { |
| 199 | if (IWorkloadFactory::IsLayerSupported(*layer, DataType::Float32, reasonIfUnsupported) |
| 200 | && layer->GetType() != LayerType::ConvertFp32ToFp16 |
| 201 | && layer->GetType() != LayerType::ConvertFp16ToFp32) |
| 202 | { |
| 203 | // Insert FP16 -> FP32 conversion layer before current layer |
| 204 | std::vector<ConvertFp16ToFp32Layer*> convertFp16ToFp32Layers = |
| 205 | InsertConvertFp16ToFp32LayersBefore(optNetObjPtr->GetGraph(), *layer); |
| 206 | |
| 207 | // Insert FP32 -> FP16 conversion layer after current layer |
| 208 | std::vector<ConvertFp32ToFp16Layer*> convertFp32ToFp16Layers = |
| 209 | InsertConvertFp32ToFp16LayersAfter(optNetObjPtr->GetGraph(), *layer); |
| 210 | |
| 211 | // Assign a supported backend to the newly introduced conversion layers |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 212 | auto AssignFirstSupportedBackend = [&](Layer* layer, BackendId preferredBackend) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 213 | { |
| 214 | bool supportedBackendFound = false; |
| 215 | std::string reasonIfUnsupported; |
| 216 | |
| 217 | // Try preferred backend first |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 218 | layer->SetBackendId(preferredBackend); |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 219 | if (IWorkloadFactory::IsLayerSupported(*layer, |
| 220 | EmptyOptional(), |
| 221 | reasonIfUnsupported)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 222 | { |
| 223 | supportedBackendFound = true; |
| 224 | } |
| 225 | else |
| 226 | { |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 227 | for (const auto& backend : availablePreferredBackends) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 228 | { |
| 229 | // Skip preferred backend (we already determined that it is not supported) |
| 230 | if (backend == preferredBackend) |
| 231 | { |
| 232 | continue; |
| 233 | } |
| 234 | |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 235 | layer->SetBackendId(backend); |
David Beck | 29c75de | 2018-10-23 13:35:58 +0100 | [diff] [blame] | 236 | if (IWorkloadFactory::IsLayerSupported(*layer, |
| 237 | EmptyOptional(), |
| 238 | reasonIfUnsupported)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 239 | { |
| 240 | supportedBackendFound = true; |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return supportedBackendFound; |
| 247 | }; |
| 248 | |
| 249 | for (ConvertFp16ToFp32Layer* convertLayer : convertFp16ToFp32Layers) |
| 250 | { |
| 251 | if (!AssignFirstSupportedBackend(convertLayer, backend)) |
| 252 | { |
| 253 | return ReturnWithError(convertLayer); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | for (ConvertFp32ToFp16Layer* convertLayer : convertFp32ToFp16Layers) |
| 258 | { |
| 259 | if (!AssignFirstSupportedBackend(convertLayer, backend)) |
| 260 | { |
| 261 | return ReturnWithError(convertLayer); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | found = true; |
| 266 | break; |
| 267 | } |
| 268 | } |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 269 | std::stringstream warningMsg; |
| 270 | warningMsg << "WARNING: Layer of type " << GetLayerTypeAsCString(layer->GetType()) |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 271 | << " is not supported on requested backend " << layer->GetBackendId().Get() |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 272 | << " for data type " << GetDataTypeName(dataType) |
| 273 | << " (reason: " << reasonIfUnsupported |
| 274 | << "), falling back to the next backend."; |
| 275 | BOOST_LOG_TRIVIAL(warning) << warningMsg.str(); |
| 276 | if (errMessages) { |
| 277 | errMessages.value().push_back(warningMsg.str()); |
| 278 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 279 | } |
| 280 | else |
| 281 | { |
| 282 | found = true; |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 283 | chosenBackends.insert(backend); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // If the layer is unsupported by any devices, log and return a null network. |
| 289 | if (!found) { |
| 290 | // NOTE: if the layer is not an operation queue type AND we have not got CpuRef as a |
| 291 | // fallback we should set the compute device on the layer to CpuRef (these are not |
| 292 | // available as accelerated operations, or are only available under certain |
| 293 | // conditions, currently they comprise MemCopy, Constant, Permute) |
| 294 | armnn::LayerType layerType = layer->GetType(); |
| 295 | if (!cpuRefUsed && (layerType == armnn::LayerType::MemCopy || |
| 296 | layerType == armnn::LayerType::Constant || |
| 297 | layerType == armnn::LayerType::Permute)) |
| 298 | { |
David Beck | 33f0ae0 | 2018-10-18 15:13:56 +0100 | [diff] [blame] | 299 | layer->SetBackendId(armnn::Compute::CpuRef); |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 300 | chosenBackends.insert(armnn::Compute::CpuRef); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 301 | } |
| 302 | else |
| 303 | { |
| 304 | return ReturnWithError(layer); |
| 305 | } |
| 306 | } |
| 307 | } |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 308 | if (bErrorFound) |
| 309 | { |
| 310 | return IOptimizedNetworkPtr(nullptr, &IOptimizedNetwork::Destroy); |
| 311 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 312 | |
| 313 | Optimizer::Pass(optNetObjPtr->GetGraph(), MakeOptimizations(OptimizeInverseConversionsFp16(), |
| 314 | OptimizeInverseConversionsFp32())); |
| 315 | |
| 316 | optNetObjPtr->GetGraph().AddCopyLayers(); |
| 317 | |
| 318 | // Convert constants |
| 319 | Optimizer::Pass(optNetObjPtr->GetGraph(), MakeOptimizations(ConvertConstantsFloatToHalf())); |
| 320 | Optimizer::Pass(optNetObjPtr->GetGraph(), MakeOptimizations(ConvertConstantsHalfToFloat())); |
| 321 | |
David Beck | 263e349 | 2018-11-09 14:46:40 +0000 | [diff] [blame] | 322 | // Run backend specific optimizations |
| 323 | for (auto&& chosenBackend : chosenBackends) |
| 324 | { |
| 325 | auto factoryFun = BackendRegistryInstance().GetFactory(chosenBackend); |
| 326 | auto backendPtr = factoryFun(); |
| 327 | BOOST_ASSERT(backendPtr.get() != nullptr); |
| 328 | |
| 329 | auto backendSpecificOptimizations = backendPtr->GetOptimizations(); |
| 330 | if (!backendSpecificOptimizations.empty()) |
| 331 | { |
| 332 | Optimizer::Pass(optNetObjPtr->GetGraph(), backendSpecificOptimizations); |
| 333 | } |
| 334 | } |
| 335 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 336 | return optNet; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 337 | } |
| 338 | |
jimfly01 | 6b0b53d | 2018-10-08 14:43:01 +0100 | [diff] [blame] | 339 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 340 | Network::Network() |
| 341 | : m_Graph(std::make_unique<Graph>()) |
| 342 | { |
| 343 | } |
| 344 | |
| 345 | Network::~Network() |
| 346 | { |
| 347 | } |
| 348 | |
| 349 | IConnectableLayer* Network::AddInputLayer(LayerBindingId id, const char* name) |
| 350 | { |
| 351 | return m_Graph->AddLayer<InputLayer>(id, name); |
| 352 | } |
| 353 | |
Éanna Ó Catháin | 4e1e136 | 2018-11-12 11:36:34 +0000 | [diff] [blame] | 354 | IConnectableLayer* Network::AddBatchToSpaceNdLayer(const BatchToSpaceNdDescriptor& batchToSpaceNdDescriptor, |
| 355 | const char* name) |
| 356 | { |
| 357 | return m_Graph->AddLayer<BatchToSpaceNdLayer>(batchToSpaceNdDescriptor, name); |
| 358 | } |
| 359 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 360 | IConnectableLayer* Network::AddFullyConnectedLayerImpl(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 361 | const ConstTensor& weights, |
| 362 | const ConstTensor* biases, |
| 363 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 364 | { |
| 365 | if (fullyConnectedDescriptor.m_BiasEnabled && (biases == nullptr)) |
| 366 | { |
| 367 | throw InvalidArgumentException("AddFullyConnectedLayer: biases cannot be NULL"); |
| 368 | } |
| 369 | |
| 370 | const auto layer = m_Graph->AddLayer<FullyConnectedLayer>(fullyConnectedDescriptor, name); |
| 371 | |
| 372 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 373 | |
| 374 | if (fullyConnectedDescriptor.m_BiasEnabled) |
| 375 | { |
| 376 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(*biases); |
| 377 | } |
| 378 | |
| 379 | return layer; |
| 380 | } |
| 381 | |
| 382 | IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 383 | const ConstTensor& weights, |
| 384 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 385 | { |
| 386 | return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, nullptr, name); |
| 387 | } |
| 388 | |
| 389 | IConnectableLayer* Network::AddFullyConnectedLayer(const FullyConnectedDescriptor& fullyConnectedDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 390 | const ConstTensor& weights, |
| 391 | const ConstTensor& biases, |
| 392 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 393 | { |
| 394 | return AddFullyConnectedLayerImpl(fullyConnectedDescriptor, weights, &biases, name); |
| 395 | } |
| 396 | |
| 397 | IConnectableLayer* Network::AddConvolution2dLayerImpl(const Convolution2dDescriptor& convolution2dDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 398 | const ConstTensor& weights, |
| 399 | const ConstTensor* biases, |
| 400 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 401 | { |
| 402 | if (convolution2dDescriptor.m_BiasEnabled && (biases == nullptr)) |
| 403 | { |
| 404 | throw InvalidArgumentException("AddConvolution2dLayer: biases cannot be NULL"); |
| 405 | } |
| 406 | |
| 407 | const auto layer = m_Graph->AddLayer<Convolution2dLayer>(convolution2dDescriptor, name); |
| 408 | |
| 409 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 410 | |
| 411 | if (convolution2dDescriptor.m_BiasEnabled) |
| 412 | { |
| 413 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(*biases); |
| 414 | } |
| 415 | |
| 416 | return layer; |
| 417 | } |
| 418 | |
| 419 | IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 420 | const ConstTensor& weights, |
| 421 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 422 | { |
| 423 | return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, nullptr, name); |
| 424 | } |
| 425 | IConnectableLayer* Network::AddConvolution2dLayer(const Convolution2dDescriptor& convolution2dDescriptor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 426 | const ConstTensor& weights, |
| 427 | const ConstTensor& biases, |
| 428 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 429 | { |
| 430 | return AddConvolution2dLayerImpl(convolution2dDescriptor, weights, &biases, name); |
| 431 | } |
| 432 | |
| 433 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayerImpl( |
| 434 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 435 | const ConstTensor& weights, |
| 436 | const ConstTensor* biases, |
| 437 | const char* name) |
| 438 | { |
| 439 | if (convolution2dDescriptor.m_BiasEnabled && (biases == nullptr)) |
| 440 | { |
| 441 | throw InvalidArgumentException("AddDepthwiseConvolution2dLayer: biases cannot be NULL"); |
| 442 | } |
| 443 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 444 | const auto layer = m_Graph->AddLayer<DepthwiseConvolution2dLayer>(convolution2dDescriptor, |
| 445 | name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 446 | |
| 447 | layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(weights); |
| 448 | |
| 449 | if (convolution2dDescriptor.m_BiasEnabled) |
| 450 | { |
| 451 | layer->m_Bias = std::make_unique<ScopedCpuTensorHandle>(*biases); |
| 452 | } |
| 453 | |
| 454 | return layer; |
| 455 | } |
| 456 | |
| 457 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayer( |
| 458 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 459 | const ConstTensor& weights, |
| 460 | const char* name) |
| 461 | { |
| 462 | return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, nullptr, name); |
| 463 | } |
| 464 | IConnectableLayer* Network::AddDepthwiseConvolution2dLayer( |
| 465 | const DepthwiseConvolution2dDescriptor& convolution2dDescriptor, |
| 466 | const ConstTensor& weights, |
| 467 | const ConstTensor& biases, |
| 468 | const char* name) |
| 469 | { |
| 470 | return AddDepthwiseConvolution2dLayerImpl(convolution2dDescriptor, weights, &biases, name); |
| 471 | } |
| 472 | |
| 473 | IConnectableLayer* Network::AddPermuteLayer(const PermuteDescriptor& permuteDescriptor, |
| 474 | const char* name) |
| 475 | { |
| 476 | return m_Graph->AddLayer<PermuteLayer>(permuteDescriptor, name); |
| 477 | } |
| 478 | |
| 479 | IConnectableLayer* Network::AddPooling2dLayer(const Pooling2dDescriptor& pooling2dDescriptor, |
| 480 | const char* name) |
| 481 | { |
| 482 | return m_Graph->AddLayer<Pooling2dLayer>(pooling2dDescriptor, name); |
| 483 | } |
| 484 | |
| 485 | IConnectableLayer* Network::AddActivationLayer(const ActivationDescriptor& activationDescriptor, |
| 486 | const char* name) |
| 487 | { |
| 488 | return m_Graph->AddLayer<ActivationLayer>(activationDescriptor, name); |
| 489 | } |
| 490 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 491 | IConnectableLayer* Network::AddNormalizationLayer(const NormalizationDescriptor& |
| 492 | normalizationDescriptor, |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 493 | const char* name) |
| 494 | { |
| 495 | return m_Graph->AddLayer<NormalizationLayer>(normalizationDescriptor, name); |
| 496 | } |
| 497 | |
| 498 | IConnectableLayer* Network::AddSoftmaxLayer(const SoftmaxDescriptor& softmaxDescriptor, |
| 499 | const char* name) |
| 500 | { |
| 501 | return m_Graph->AddLayer<SoftmaxLayer>(softmaxDescriptor, name); |
| 502 | } |
| 503 | |
| 504 | IConnectableLayer* Network::AddSplitterLayer(const ViewsDescriptor& splitterDescriptor, |
| 505 | const char* name) |
| 506 | { |
| 507 | return m_Graph->AddLayer<SplitterLayer>(splitterDescriptor, name); |
| 508 | } |
| 509 | |
| 510 | IConnectableLayer* Network::AddMergerLayer(const OriginsDescriptor& mergerDescriptor, |
| 511 | const char* name) |
| 512 | { |
| 513 | return m_Graph->AddLayer<MergerLayer>(mergerDescriptor, name); |
| 514 | } |
| 515 | |
| 516 | IConnectableLayer* Network::AddAdditionLayer(const char* name) |
| 517 | { |
| 518 | return m_Graph->AddLayer<AdditionLayer>(name); |
| 519 | } |
| 520 | |
| 521 | IConnectableLayer* Network::AddMultiplicationLayer(const char* name) |
| 522 | { |
| 523 | return m_Graph->AddLayer<MultiplicationLayer>(name); |
| 524 | } |
| 525 | |
| 526 | IConnectableLayer* Network::AddOutputLayer(LayerBindingId id, const char* name) |
| 527 | { |
| 528 | return m_Graph->AddLayer<OutputLayer>(id, name); |
| 529 | } |
| 530 | |
| 531 | IConnectableLayer* Network::AddBatchNormalizationLayer(const BatchNormalizationDescriptor& desc, |
| 532 | const ConstTensor& mean, |
| 533 | const ConstTensor& variance, |
| 534 | const ConstTensor& beta, |
| 535 | const ConstTensor& gamma, |
| 536 | const char* name) |
| 537 | { |
| 538 | const auto layer = m_Graph->AddLayer<BatchNormalizationLayer>(desc, name); |
| 539 | |
| 540 | layer->m_Mean = std::make_unique<ScopedCpuTensorHandle>(mean); |
| 541 | layer->m_Variance = std::make_unique<ScopedCpuTensorHandle>(variance); |
| 542 | layer->m_Beta = std::make_unique<ScopedCpuTensorHandle>(beta); |
| 543 | layer->m_Gamma = std::make_unique<ScopedCpuTensorHandle>(gamma); |
| 544 | |
| 545 | return layer; |
| 546 | } |
| 547 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 548 | IConnectableLayer* Network::AddResizeBilinearLayer(const ResizeBilinearDescriptor& |
| 549 | resizeDescriptor, const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 550 | { |
| 551 | return m_Graph->AddLayer<ResizeBilinearLayer>(resizeDescriptor,name); |
| 552 | } |
| 553 | |
Matteo Martincigh | bcd3c85 | 2018-09-28 14:14:12 +0100 | [diff] [blame] | 554 | IConnectableLayer* Network::AddL2NormalizationLayer(const L2NormalizationDescriptor& desc, |
| 555 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 556 | { |
Matteo Martincigh | bcd3c85 | 2018-09-28 14:14:12 +0100 | [diff] [blame] | 557 | return m_Graph->AddLayer<L2NormalizationLayer>(desc, name); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | IConnectableLayer* Network::AddConstantLayer(const ConstTensor& input, const char* name) |
| 561 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 562 | auto layer = m_Graph->AddLayer<ConstantLayer>(name); |
| 563 | |
| 564 | layer->m_LayerOutput = std::make_unique<ScopedCpuTensorHandle>(input); |
| 565 | |
| 566 | return layer; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 567 | } |
| 568 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 569 | IConnectableLayer* Network::AddReshapeLayer(const ReshapeDescriptor& reshapeDescriptor, |
| 570 | const char* name) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 571 | { |
| 572 | return m_Graph->AddLayer<ReshapeLayer>(reshapeDescriptor, name); |
| 573 | } |
| 574 | |
Nattapat Chaimanowong | 207ef9a | 2018-11-02 10:57:25 +0000 | [diff] [blame] | 575 | IConnectableLayer* Network::AddSpaceToBatchNdLayer(const SpaceToBatchNdDescriptor& spaceToBatchNdDescriptor, |
| 576 | const char* name) |
| 577 | { |
| 578 | return m_Graph->AddLayer<SpaceToBatchNdLayer>(spaceToBatchNdDescriptor, name); |
| 579 | } |
| 580 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 581 | IConnectableLayer* Network::AddFloorLayer(const char* name) |
| 582 | { |
| 583 | return m_Graph->AddLayer<FloorLayer>(name); |
| 584 | } |
| 585 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 586 | IConnectableLayer* Network::AddLstmLayer(const LstmDescriptor& descriptor, |
| 587 | const LstmInputParams& params, |
| 588 | const char* name) |
| 589 | { |
| 590 | const auto layer = m_Graph->AddLayer<LstmLayer>(descriptor, name); |
| 591 | |
| 592 | //Lstm Basic Parameters |
| 593 | layer->m_BasicParameters.m_InputToForgetWeights = |
| 594 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToForgetWeights)); |
| 595 | layer->m_BasicParameters.m_InputToCellWeights = |
| 596 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToCellWeights)); |
| 597 | layer->m_BasicParameters.m_InputToOutputWeights = |
| 598 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToOutputWeights)); |
| 599 | layer->m_BasicParameters.m_RecurrentToForgetWeights = |
| 600 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToForgetWeights)); |
| 601 | layer->m_BasicParameters.m_RecurrentToCellWeights = |
| 602 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToCellWeights)); |
| 603 | layer->m_BasicParameters.m_RecurrentToOutputWeights = |
| 604 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToOutputWeights)); |
| 605 | layer->m_BasicParameters.m_ForgetGateBias = |
| 606 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ForgetGateBias)); |
| 607 | layer->m_BasicParameters.m_CellBias = |
| 608 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellBias)); |
| 609 | layer->m_BasicParameters.m_OutputGateBias = |
| 610 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_OutputGateBias)); |
| 611 | |
| 612 | //Lstm Cifg parameters |
| 613 | if(!descriptor.m_CifgEnabled) |
| 614 | { |
| 615 | if(params.m_InputToInputWeights == nullptr) |
| 616 | { |
| 617 | throw InvalidArgumentException("AddLstmLayer: Input To Input Weights cannot be NULL"); |
| 618 | } |
| 619 | if(params.m_RecurrentToInputWeights == nullptr) |
| 620 | { |
| 621 | throw InvalidArgumentException( |
| 622 | "AddLstmLayer: Recurrent To Input Weights cannot be NULL"); |
| 623 | } |
| 624 | if(params.m_InputGateBias == nullptr) |
| 625 | { |
| 626 | throw InvalidArgumentException("AddLstmLayer: Input Gate Bias cannot be NULL"); |
| 627 | } |
| 628 | layer->m_CifgParameters.m_InputToInputWeights = |
| 629 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputToInputWeights)); |
| 630 | layer->m_CifgParameters.m_RecurrentToInputWeights = |
| 631 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_RecurrentToInputWeights)); |
| 632 | // In the VTS tests, cell-to-input weights may be null, even if the other CIFG params are not. |
| 633 | if(params.m_CellToInputWeights != nullptr) |
| 634 | { |
| 635 | layer->m_CifgParameters.m_CellToInputWeights = |
| 636 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToInputWeights)); |
| 637 | } |
| 638 | layer->m_CifgParameters.m_InputGateBias = |
| 639 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_InputGateBias)); |
| 640 | } |
| 641 | |
| 642 | //Lstm projection parameters |
| 643 | if(descriptor.m_ProjectionEnabled) |
| 644 | { |
| 645 | if(params.m_ProjectionWeights == nullptr) |
| 646 | { |
| 647 | throw InvalidArgumentException("AddLstmLayer: Projection Weights cannot be NULL"); |
| 648 | } |
| 649 | layer->m_ProjectionParameters.m_ProjectionWeights = |
| 650 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionWeights)); |
| 651 | if(params.m_ProjectionBias != nullptr) |
| 652 | { |
| 653 | layer->m_ProjectionParameters.m_ProjectionBias = |
| 654 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_ProjectionBias)); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | //Lstm Peephole params |
| 659 | if(descriptor.m_PeepholeEnabled) |
| 660 | { |
| 661 | if(params.m_CellToForgetWeights == nullptr) |
| 662 | { |
| 663 | throw InvalidArgumentException("AddLstmLayer: Cell To Forget Weights cannot be NULL"); |
| 664 | } |
| 665 | if(params.m_CellToOutputWeights == nullptr) |
| 666 | { |
| 667 | throw InvalidArgumentException("AddLstmLayer: Cell To Output Weights cannot be NULL"); |
| 668 | } |
| 669 | layer->m_PeepholeParameters.m_CellToForgetWeights = |
| 670 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToForgetWeights)); |
| 671 | layer->m_PeepholeParameters.m_CellToOutputWeights = |
| 672 | std::make_unique<ScopedCpuTensorHandle>(*(params.m_CellToOutputWeights)); |
| 673 | } |
| 674 | return layer; |
| 675 | } |
| 676 | |
Francis Murtagh | e7a86a4 | 2018-08-29 12:42:10 +0100 | [diff] [blame] | 677 | IConnectableLayer* Network::AddDivisionLayer(const char* name) |
| 678 | { |
| 679 | return m_Graph->AddLayer<DivisionLayer>(name); |
| 680 | } |
| 681 | |
David Beck | 1952622 | 2018-09-12 16:00:08 +0100 | [diff] [blame] | 682 | IConnectableLayer* Network::AddSubtractionLayer(const char* name) |
| 683 | { |
| 684 | return m_Graph->AddLayer<SubtractionLayer>(name); |
| 685 | } |
| 686 | |
narpra01 | 32b9046 | 2018-09-13 11:07:48 +0100 | [diff] [blame] | 687 | IConnectableLayer* Network::AddMeanLayer(const MeanDescriptor& meanDescriptor, const char* name) |
| 688 | { |
| 689 | return m_Graph->AddLayer<MeanLayer>(meanDescriptor,name); |
| 690 | } |
| 691 | |
Mohamed Nour Abouelseoud | 5662c20 | 2018-09-24 13:30:09 +0100 | [diff] [blame] | 692 | IConnectableLayer* Network::AddPadLayer(const PadDescriptor& padDescriptor, const char* name) |
| 693 | { |
| 694 | return m_Graph->AddLayer<PadLayer>(padDescriptor,name); |
| 695 | } |
| 696 | |
Conor Kennedy | 430b5d8 | 2018-11-14 15:28:28 +0000 | [diff] [blame^] | 697 | IConnectableLayer* Network::AddStridedSliceLayer(const StridedSliceDescriptor& stridedSliceDescriptor, |
| 698 | const char* name) |
| 699 | { |
| 700 | return m_Graph->AddLayer<StridedSliceLayer>(stridedSliceDescriptor, name); |
| 701 | } |
| 702 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 703 | OptimizedNetwork::OptimizedNetwork(std::unique_ptr<Graph> graph) |
| 704 | : m_Graph(std::move(graph)) |
| 705 | { |
| 706 | } |
| 707 | |
| 708 | OptimizedNetwork::~OptimizedNetwork() |
| 709 | { |
| 710 | } |
| 711 | |
| 712 | } // namespace armnn |