2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
7 #include <armnn/Version.hpp>
8 #include <backendsCommon/BackendRegistry.hpp>
9 #include <backendsCommon/IBackendContext.hpp>
13 #include <boost/log/trivial.hpp>
14 #include <boost/polymorphic_cast.hpp>
16 using namespace armnn;
22 IRuntime* IRuntime::CreateRaw(const CreationOptions& options)
24 return new Runtime(options);
27 IRuntimePtr IRuntime::Create(const CreationOptions& options)
29 return IRuntimePtr(CreateRaw(options), &IRuntime::Destroy);
32 void IRuntime::Destroy(IRuntime* runtime)
34 delete boost::polymorphic_downcast<Runtime*>(runtime);
37 int Runtime::GenerateNetworkId()
39 return m_NetworkIdCounter++;
42 Status Runtime::LoadNetwork(NetworkId& networkIdOut, IOptimizedNetworkPtr inNetwork)
44 std::string ignoredErrorMessage;
45 return LoadNetwork(networkIdOut, std::move(inNetwork), ignoredErrorMessage);
48 Status Runtime::LoadNetwork(NetworkId& networkIdOut,
49 IOptimizedNetworkPtr inNetwork,
50 std::string & errorMessage)
52 IOptimizedNetwork* rawNetwork = inNetwork.release();
54 networkIdOut = GenerateNetworkId();
56 for (auto&& context : m_BackendContexts)
58 context.second->BeforeLoadNetwork(networkIdOut);
61 unique_ptr<LoadedNetwork> loadedNetwork = LoadedNetwork::MakeLoadedNetwork(
62 std::unique_ptr<OptimizedNetwork>(boost::polymorphic_downcast<OptimizedNetwork*>(rawNetwork)),
67 return Status::Failure;
71 std::lock_guard<std::mutex> lockGuard(m_Mutex);
74 m_LoadedNetworks[networkIdOut] = std::move(loadedNetwork);
77 for (auto&& context : m_BackendContexts)
79 context.second->AfterLoadNetwork(networkIdOut);
82 return Status::Success;
85 Status Runtime::UnloadNetwork(NetworkId networkId)
88 for (auto&& context : m_BackendContexts)
90 unloadOk &= context.second->BeforeUnloadNetwork(networkId);
95 BOOST_LOG_TRIVIAL(warning) << "Runtime::UnloadNetwork(): failed to unload "
96 "network with ID:" << networkId << " because BeforeUnloadNetwork failed";
97 return Status::Failure;
101 std::lock_guard<std::mutex> lockGuard(m_Mutex);
103 if (m_LoadedNetworks.erase(networkId) == 0)
105 BOOST_LOG_TRIVIAL(warning) << "WARNING: Runtime::UnloadNetwork(): " << networkId << " not found!";
106 return Status::Failure;
110 for (auto&& context : m_BackendContexts)
112 context.second->AfterUnloadNetwork(networkId);
115 BOOST_LOG_TRIVIAL(debug) << "Runtime::UnloadNetwork(): Unloaded network with ID: " << networkId;
116 return Status::Success;
119 const std::shared_ptr<IProfiler> Runtime::GetProfiler(NetworkId networkId) const
121 auto it = m_LoadedNetworks.find(networkId);
122 if (it != m_LoadedNetworks.end())
124 auto& loadedNetwork = it->second;
125 return loadedNetwork->GetProfiler();
131 Runtime::Runtime(const CreationOptions& options)
132 : m_NetworkIdCounter(0)
133 , m_DeviceSpec{BackendRegistryInstance().GetBackendIds()}
135 BOOST_LOG_TRIVIAL(info) << "ArmNN v" << ARMNN_VERSION << "\n";
137 for (const auto& id : BackendRegistryInstance().GetBackendIds())
139 // Store backend contexts for the supported ones
140 if (m_DeviceSpec.GetSupportedBackends().count(id) > 0)
142 auto factoryFun = BackendRegistryInstance().GetFactory(id);
143 auto backend = factoryFun();
144 BOOST_ASSERT(backend.get() != nullptr);
146 auto context = backend->CreateBackendContext(options);
148 // backends are allowed to return nullptrs if they
149 // don't wish to create a backend specific context
152 m_BackendContexts.emplace(std::make_pair(id, std::move(context)));
160 std::vector<int> networkIDs;
163 // Coverity fix: The following code may throw an exception of type std::length_error.
164 std::transform(m_LoadedNetworks.begin(), m_LoadedNetworks.end(),
165 std::back_inserter(networkIDs),
166 [](const auto &pair) { return pair.first; });
168 catch (const std::exception& e)
170 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
171 // exception of type std::length_error.
172 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
173 std::cerr << "WARNING: An error has occurred when getting the IDs of the networks to unload: " << e.what()
174 << "\nSome of the loaded networks may not be unloaded" << std::endl;
176 // We then proceed to unload all the networks which IDs have been appended to the list
177 // up to the point the exception was thrown (if any).
179 for (auto networkID : networkIDs)
183 // Coverity fix: UnloadNetwork() may throw an exception of type std::length_error,
184 // boost::log::v2s_mt_posix::odr_violation or boost::log::v2s_mt_posix::system_error
185 UnloadNetwork(networkID);
187 catch (const std::exception& e)
189 // Coverity fix: BOOST_LOG_TRIVIAL (typically used to report errors) may throw an
190 // exception of type std::length_error.
191 // Using stderr instead in this context as there is no point in nesting try-catch blocks here.
192 std::cerr << "WARNING: An error has occurred when unloading network " << networkID << ": " << e.what()
198 LoadedNetwork* Runtime::GetLoadedNetworkPtr(NetworkId networkId) const
200 std::lock_guard<std::mutex> lockGuard(m_Mutex);
201 return m_LoadedNetworks.at(networkId).get();
204 TensorInfo Runtime::GetInputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
206 return GetLoadedNetworkPtr(networkId)->GetInputTensorInfo(layerId);
209 TensorInfo Runtime::GetOutputTensorInfo(NetworkId networkId, LayerBindingId layerId) const
211 return GetLoadedNetworkPtr(networkId)->GetOutputTensorInfo(layerId);
215 Status Runtime::EnqueueWorkload(NetworkId networkId,
216 const InputTensors& inputTensors,
217 const OutputTensors& outputTensors)
219 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
221 static thread_local NetworkId lastId = networkId;
222 if (lastId != networkId)
224 LoadedNetworkFuncSafe(lastId, [](LoadedNetwork* network)
226 network->FreeWorkingMemory();
231 return loadedNetwork->EnqueueWorkload(inputTensors, outputTensors);
234 void Runtime::RegisterDebugCallback(NetworkId networkId, const DebugCallbackFunction& func)
236 LoadedNetwork* loadedNetwork = GetLoadedNetworkPtr(networkId);
237 loadedNetwork->RegisterDebugCallback(func);