Moved executable network implementation from header (#4049)

This commit is contained in:
Anton Pankratv
2021-03-19 11:58:16 +03:00
committed by GitHub
parent 93d2bae95a
commit 35bff0d8a1
4 changed files with 181 additions and 127 deletions

View File

@@ -9,7 +9,6 @@
*/
#pragma once
#include <algorithm>
#include <map>
#include <memory>
#include <string>
@@ -24,9 +23,9 @@
namespace InferenceEngine {
/**
* @brief wrapper over IExecutableNetwork
* @brief This is an interface of an executable network
*/
class ExecutableNetwork {
class INFERENCE_ENGINE_API_CLASS(ExecutableNetwork) {
IExecutableNetwork::Ptr actual;
details::SharedObjectLoader::Ptr plg;
@@ -37,11 +36,9 @@ public:
ExecutableNetwork() = default;
/**
* @brief Destructor
* @brief Default destructor
*/
~ExecutableNetwork() {
actual = nullptr;
}
~ExecutableNetwork();
/**
* @brief Constructs ExecutableNetwork from the initialized shared_pointer
@@ -49,37 +46,30 @@ public:
* @param actual Initialized shared pointer
* @param plg Plugin to use
*/
explicit ExecutableNetwork(IExecutableNetwork::Ptr actual, details::SharedObjectLoader::Ptr plg = {})
: actual(actual), plg(plg) {
// plg can be null, but not the actual
if (actual == nullptr) {
THROW_IE_EXCEPTION << "ExecutableNetwork wrapper was not initialized.";
}
}
explicit ExecutableNetwork(IExecutableNetwork::Ptr actual, details::SharedObjectLoader::Ptr plg = {});
/**
* @copybrief IExecutableNetwork::GetOutputsInfo
* @brief Gets the Executable network output Data node information.
*
* The received info is stored in the given InferenceEngine::ConstOutputsDataMap node.
* This method need to be called to find output names for using them later
* when calling InferenceEngine::InferRequest::GetBlob or InferenceEngine::InferRequest::SetBlob
*
* Wraps IExecutableNetwork::GetOutputsInfo.
* @return A collection that contains string as key, and const Data smart pointer as value
*/
ConstOutputsDataMap GetOutputsInfo() const {
ConstOutputsDataMap data;
CALL_STATUS_FNC(GetOutputsInfo, data);
return data;
}
ConstOutputsDataMap GetOutputsInfo() const;
/**
* @copybrief IExecutableNetwork::GetInputsInfo
* @brief Gets the executable network input Data node information.
*
* Wraps IExecutableNetwork::GetInputsInfo
* The received info is stored in the given InferenceEngine::ConstInputsDataMap object.
* This method need to be called to find out input names for using them later
* when calling InferenceEngine::InferRequest::SetBlob
*
* @param inputs Reference to InferenceEngine::ConstInputsDataMap object.
* @return A collection that contains string as key, and const InputInfo smart pointer as value
*/
ConstInputsDataMap GetInputsInfo() const {
ConstInputsDataMap info;
CALL_STATUS_FNC(GetInputsInfo, info);
return info;
}
ConstInputsDataMap GetInputsInfo() const;
/**
* @brief reset owned object to new pointer.
@@ -87,28 +77,16 @@ public:
* Eessential for cases when simultaneously loaded networks not expected.
* @param newActual actual pointed object
*/
void reset(IExecutableNetwork::Ptr newActual) {
if (actual == nullptr) {
THROW_IE_EXCEPTION << "ExecutableNetwork wrapper was not initialized.";
}
if (newActual == nullptr) {
THROW_IE_EXCEPTION << "ExecutableNetwork wrapper used for reset was not initialized.";
}
this->actual.swap(newActual);
}
void reset(IExecutableNetwork::Ptr newActual);
/**
* @copybrief IExecutableNetwork::CreateInferRequest
* @brief Creates an inference request object used to infer the network.
*
* The created request has allocated input and output blobs (that can be changed later).
*
* Wraps IExecutableNetwork::CreateInferRequest.
* @return InferRequest object
*/
InferRequest CreateInferRequest() {
IInferRequest::Ptr req;
CALL_STATUS_FNC(CreateInferRequest, req);
if (req.get() == nullptr) THROW_IE_EXCEPTION << "Internal error: pointer to infer request is null";
return InferRequest(req, plg);
}
InferRequest CreateInferRequest();
/**
* @copybrief IExecutableNetwork::CreateInferRequest
@@ -116,45 +94,31 @@ public:
* Wraps IExecutableNetwork::CreateInferRequest.
* @return shared pointer on InferenceEngine::InferRequest object
*/
InferRequest::Ptr CreateInferRequestPtr() {
IInferRequest::Ptr req;
CALL_STATUS_FNC(CreateInferRequest, req);
return std::make_shared<InferRequest>(req, plg);
}
InferRequest::Ptr CreateInferRequestPtr();
/**
* @copybrief IExecutableNetwork::Export
*
* Wraps IExecutableNetwork::Export.
* @brief Exports the current executable network.
*
* @see Core::ImportNetwork
*
* @param modelFileName Full path to the location of the exported file
*/
void Export(const std::string& modelFileName) {
CALL_STATUS_FNC(Export, modelFileName);
}
void Export(const std::string& modelFileName);
/**
* @copybrief IExecutableNetwork::Export
*
* Wraps IExecutableNetwork::Export.
* @brief Exports the current executable network.
*
* @see Core::ImportNetwork
*
* @param networkModel network model output stream
* @param networkModel Network model output stream
*/
void Export(std::ostream& networkModel) {
CALL_STATUS_FNC(Export, networkModel);
}
void Export(std::ostream& networkModel);
/**
* @brief cast operator is used when this wrapper initialized by LoadNetwork
* @return A shared pointer to IExecutableNetwork interface.
* @return A shared pointer to IExecutableNetwork interface.
*/
operator IExecutableNetwork::Ptr&() {
return actual;
}
operator IExecutableNetwork::Ptr&();
/**
* @copybrief IExecutableNetwork::GetExecGraphInfo
@@ -162,88 +126,56 @@ public:
* Wraps IExecutableNetwork::GetExecGraphInfo.
* @return CNNetwork containing Executable Graph Info
*/
CNNNetwork GetExecGraphInfo() {
IE_SUPPRESS_DEPRECATED_START
ICNNNetwork::Ptr ptr = nullptr;
CALL_STATUS_FNC(GetExecGraphInfo, ptr);
return CNNNetwork(ptr);
IE_SUPPRESS_DEPRECATED_END
}
CNNNetwork GetExecGraphInfo();
/**
* @copybrief IExecutableNetwork::QueryState
* @deprecated Use InferRequest::QueryState instead
* @brief Gets state control interface for given executable network.
*
* State control essential for recurrent networks
*
* Wraps IExecutableNetwork::QueryState
* @return A vector of Memory State objects
*/
INFERENCE_ENGINE_DEPRECATED("Use InferRequest::QueryState instead")
std::vector<VariableState> QueryState() {
if (actual == nullptr) THROW_IE_EXCEPTION << "ExecutableNetwork was not initialized.";
IVariableState::Ptr pState = nullptr;
auto res = OK;
std::vector<VariableState> controller;
for (size_t idx = 0; res == OK; ++idx) {
ResponseDesc resp;
IE_SUPPRESS_DEPRECATED_START
res = actual->QueryState(pState, idx, &resp);
IE_SUPPRESS_DEPRECATED_END
if (res != OK && res != OUT_OF_BOUNDS) {
THROW_IE_EXCEPTION << resp.msg;
}
if (res != OUT_OF_BOUNDS) {
controller.push_back(VariableState(pState, plg));
}
}
return controller;
}
std::vector<VariableState> QueryState();
/**
* @copybrief IExecutableNetwork::SetConfig
* @brief Sets configuration for current executable network
*
* Wraps IExecutableNetwork::SetConfig.
* @param config Map of pairs: (config parameter name, config parameter value)
*/
void SetConfig(const std::map<std::string, Parameter>& config) {
CALL_STATUS_FNC(SetConfig, config);
}
void SetConfig(const std::map<std::string, Parameter>& config);
/**
* @copybrief IExecutableNetwork::GetConfig
/** @brief Gets configuration for current executable network.
*
* Wraps IExecutableNetwork::GetConfig
* @param name - config key, can be found in ie_plugin_config.hpp
* The method is responsible to extract information
* which affects executable network execution. The list of supported configuration values can be extracted via
* ExecutableNetwork::GetMetric with the SUPPORTED_CONFIG_KEYS key, but some of these keys cannot be changed
* dymanically, e.g. DEVICE_ID cannot changed if an executable network has already been compiled for particular
* device.
*
* @param name config key, can be found in ie_plugin_config.hpp
* @return Configuration parameter value
*/
Parameter GetConfig(const std::string& name) const {
Parameter configValue;
CALL_STATUS_FNC(GetConfig, name, configValue);
return configValue;
}
Parameter GetConfig(const std::string& name) const;
/**
* @copybrief IExecutableNetwork::GetMetric
* @brief Gets general runtime metric for an executable network.
*
* Wraps IExecutableNetwork::GetMetric
* @param name - metric name to request
* It can be network name, actual device ID on
* which executable network is running or all other properties which cannot be changed dynamically.
*
* @param name metric name to request
* @return Metric parameter value
*/
Parameter GetMetric(const std::string& name) const {
Parameter metricValue;
CALL_STATUS_FNC(GetMetric, name, metricValue);
return metricValue;
}
Parameter GetMetric(const std::string& name) const;
/**
* @brief Returns pointer to plugin-specific shared context
* on remote accelerator device that was used to create this ExecutableNetwork
* @return A context
*/
RemoteContext::Ptr GetContext() const {
RemoteContext::Ptr pContext;
CALL_STATUS_FNC(GetContext, pContext);
return pContext;
}
RemoteContext::Ptr GetContext() const;
/**
* @brief A smart pointer to the ExecutableNetwork object

View File

@@ -0,0 +1,120 @@
// Copyright (C) 2018-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "cpp/ie_executable_network.hpp"
#include "ie_common.h"
namespace InferenceEngine {
ExecutableNetwork::ExecutableNetwork(IExecutableNetwork::Ptr actual_, details::SharedObjectLoader::Ptr plg)
: actual(actual_), plg(plg) {
// plg can be null, but not the actual
if (actual == nullptr) {
THROW_IE_EXCEPTION << "ExecutableNetwork wrapper was not initialized.";
}
}
ExecutableNetwork::~ExecutableNetwork() {
actual = {};
}
ConstOutputsDataMap ExecutableNetwork::GetOutputsInfo() const {
ConstOutputsDataMap data;
CALL_STATUS_FNC(GetOutputsInfo, data);
return data;
}
ConstInputsDataMap ExecutableNetwork::GetInputsInfo() const {
ConstInputsDataMap info;
CALL_STATUS_FNC(GetInputsInfo, info);
return info;
}
void ExecutableNetwork::reset(IExecutableNetwork::Ptr newActual) {
if (actual == nullptr) {
THROW_IE_EXCEPTION << "ExecutableNetwork wrapper was not initialized.";
}
if (newActual == nullptr) {
THROW_IE_EXCEPTION << "ExecutableNetwork wrapper used for reset was not initialized.";
}
this->actual.swap(newActual);
}
InferRequest ExecutableNetwork::CreateInferRequest() {
IInferRequest::Ptr req;
CALL_STATUS_FNC(CreateInferRequest, req);
if (req.get() == nullptr) THROW_IE_EXCEPTION << "Internal error: pointer to infer request is null";
return InferRequest(req, plg);
}
InferRequest::Ptr ExecutableNetwork::CreateInferRequestPtr() {
IInferRequest::Ptr req;
CALL_STATUS_FNC(CreateInferRequest, req);
return std::make_shared<InferRequest>(req, plg);
}
void ExecutableNetwork::Export(const std::string& modelFileName) {
CALL_STATUS_FNC(Export, modelFileName);
}
void ExecutableNetwork::Export(std::ostream& networkModel) {
CALL_STATUS_FNC(Export, networkModel);
}
ExecutableNetwork::operator IExecutableNetwork::Ptr&() {
return actual;
}
CNNNetwork ExecutableNetwork::GetExecGraphInfo() {
IE_SUPPRESS_DEPRECATED_START
ICNNNetwork::Ptr ptr = nullptr;
CALL_STATUS_FNC(GetExecGraphInfo, ptr);
return CNNNetwork(ptr);
IE_SUPPRESS_DEPRECATED_END
}
std::vector<VariableState> ExecutableNetwork::QueryState() {
if (actual == nullptr) THROW_IE_EXCEPTION << "ExecutableNetwork was not initialized.";
IVariableState::Ptr pState = nullptr;
auto res = OK;
std::vector<VariableState> controller;
for (size_t idx = 0; res == OK; ++idx) {
ResponseDesc resp;
IE_SUPPRESS_DEPRECATED_START
res = actual->QueryState(pState, idx, &resp);
IE_SUPPRESS_DEPRECATED_END
if (res != OK && res != OUT_OF_BOUNDS) {
THROW_IE_EXCEPTION << resp.msg;
}
if (res != OUT_OF_BOUNDS) {
controller.push_back(VariableState(pState, plg));
}
}
return controller;
}
void ExecutableNetwork::SetConfig(const std::map<std::string, Parameter>& config) {
CALL_STATUS_FNC(SetConfig, config);
}
Parameter ExecutableNetwork::GetConfig(const std::string& name) const {
Parameter configValue;
CALL_STATUS_FNC(GetConfig, name, configValue);
return configValue;
}
Parameter ExecutableNetwork::GetMetric(const std::string& name) const {
Parameter metricValue;
CALL_STATUS_FNC(GetMetric, name, metricValue);
return metricValue;
}
RemoteContext::Ptr ExecutableNetwork::GetContext() const {
RemoteContext::Ptr pContext;
CALL_STATUS_FNC(GetContext, pContext);
return pContext;
}
} // namespace InferenceEngine

View File

@@ -137,8 +137,9 @@ TEST_P(ExecGraphTests, CheckExecGraphInfoBeforeExecution) {
ASSERT_GE(layer.second, 0);
}
} else {
ASSERT_THROW(ie->LoadNetwork(cnnNet, targetDevice, configuration).GetExecGraphInfo(),
InferenceEngine::NotImplemented);
InferenceEngine::ExecutableNetwork network;
ASSERT_NO_THROW(network = ie->LoadNetwork(cnnNet, targetDevice, configuration));
ASSERT_THROW(network.GetExecGraphInfo(), InferenceEngine::NotImplemented);
}
}

View File

@@ -45,8 +45,9 @@ TEST(ExecutableNetworkConstructorTests, CanConstruct) {
TEST(ExecutableNetworkDestructorTests, Destruct) {
std::shared_ptr<MockIExecutableNetwork> mockIExeNet_p = std::make_shared<MockIExecutableNetwork>();
InferenceEngine::ExecutableNetwork exeNet{mockIExeNet_p};
exeNet.~ExecutableNetwork();
{
InferenceEngine::ExecutableNetwork exeNet{mockIExeNet_p};
}
// Call of destructor should decrease counter of shared_ptr
ASSERT_EQ(mockIExeNet_p.use_count(), 1);
}