Added openvino executable network API (#7230)

This commit is contained in:
Anton Pankratv authored and GitHub committed 2021-09-10 11:55:52 +03:00
1 parent c862abae03
commit 305b86fe4a
6 files changed
+293 -6

No files matched your search

@@ -13,7 +13,8 @@
#include <map>
#include <string>
namespace InferenceEngine {};
namespace InferenceEngine {}
namespace ov {
namespace ie = InferenceEngine;
namespace runtime {
@@ -0,0 +1,147 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
/**
* @brief A header file that provides ExecutableNetwork class
*
* @file openvino/runtime/executable_network.hpp
*/
#pragma once
#include <map>
#include <memory>
#include <ostream>
#include <string>
#include <vector>
#include "ie_parameter.hpp"
#include "openvino/core/function.hpp"
#include "openvino/runtime/infer_request.hpp"
#include "openvino/runtime/remote_context.hpp"
namespace InferenceEngine {
class IExecutableNetworkInternal;
class RemoteContext;
} // namespace InferenceEngine
namespace ov {
namespace runtime {
class Core;
/**
* @brief This is an interface of an executable network
*/
class INFERENCE_ENGINE_API_CLASS(ExecutableNetwork) {
std::shared_ptr<SharedObject> _so;
std::shared_ptr<InferenceEngine::IExecutableNetworkInternal> _impl;
/**
* @brief Constructs ExecutableNetwork from the initialized std::shared_ptr
* @param so Plugin to use. This is required to ensure that ExecutableNetwork can work properly even if plugin
* object is destroyed.
* @param impl Initialized shared pointer
*/
ExecutableNetwork(const std::shared_ptr<SharedObject>& so,
const std::shared_ptr<ie::IExecutableNetworkInternal>& impl);
friend class ov::runtime::Core;
public:
/**
* @brief A default constructor.
*/
ExecutableNetwork() = default;
/**
* @brief Get executable graph information from a device
*
* @return Function containing Executable Graph Info
*/
std::shared_ptr<const Function> get_runtime_function() const;
/**
* @brief Get parameters of executeble graph function
*
* @return vector of paramter nodes
*/
ParameterVector get_parameters() const;
/**
* @brief Get results of executeble graph function
*
* @return vector of result nodes
*/
ResultVector get_results() const;
/**
* @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).
*
* @return InferRequest object
*/
InferRequest create_infer_request();
/**
* @brief Exports the current executable network.
*
* @see Core::ImportNetwork
*
* @param networkModel Network model output stream
*/
void export_model(std::ostream& networkModel);
/**
* @brief Sets configuration for current executable network
*
* @param config Map of pairs: (config parameter name, config parameter value)
*/
void set_config(const ie::ParamMap& config);
/** @brief Gets configuration for current executable network.
*
* The method is responsible to extract information
* which affects executable network execution. The list of supported configuration values can be extracted via
* ExecutableNetwork::get_metric with the SUPPORTED_CONFIG_KEYS key, but some of these keys cannot be changed
* dynamically, 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
*/
ie::Parameter get_config(const std::string& name) const;
/**
* @brief Gets general runtime metric for an executable network.
*
* 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
*/
ie::Parameter get_metric(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
*/
std::shared_ptr<ie::RemoteContext> get_context() const;
/**
* @brief Checks if current ExecutableNetwork object is not initialized
* @return true if current ExecutableNetwork object is not initialized, false - otherwise
*/
bool operator!() const noexcept;
/**
* @brief Checks if current ExecutableNetwork object is initialized
* @return true if current ExecutableNetwork object is initialized, false - otherwise
*/
explicit operator bool() const noexcept;
};
} // namespace runtime
} // namespace ov
@@ -24,6 +24,9 @@ class Blob;
namespace ov {
namespace runtime {
class ExecutableNetwork;
/**
* @brief This is an interface of asynchronous infer request
*
@@ -40,7 +43,7 @@ class INFERENCE_ENGINE_API_CLASS(InferRequest) {
* @param impl Initialized shared pointer
*/
InferRequest(const std::shared_ptr<SharedObject>& so, const std::shared_ptr<ie::IInferRequestInternal>& impl);
friend class ExecutableNetwork;
friend class ov::runtime::ExecutableNetwork;
public:
/**
@@ -9,6 +9,7 @@
#include "cpp_interfaces/interface/ie_iremote_context.hpp"
#include "ie_common.h"
#include "ie_executable_network_base.hpp"
#include "openvino/runtime/executable_network.hpp"
namespace InferenceEngine {
@@ -18,7 +19,7 @@ namespace InferenceEngine {
try { \
__VA_ARGS__; \
} catch (...) { \
details::Rethrow(); \
InferenceEngine::details::Rethrow(); \
}
ExecutableNetwork::ExecutableNetwork(const details::SharedObjectLoader& so, const IExecutableNetworkInternal::Ptr& impl)
@@ -55,9 +56,10 @@ ExecutableNetwork::operator IExecutableNetwork::Ptr() {
std::vector<VariableState> ExecutableNetwork::QueryState() {
std::vector<VariableState> controller;
EXEC_NET_CALL_STATEMENT(for (auto&& state
: _impl->QueryState()) {
controller.emplace_back(VariableState{_so, state});
EXEC_NET_CALL_STATEMENT({
for (auto&& state : _impl->QueryState()) {
controller.emplace_back(VariableState{_so, state});
}
});
return controller;
}
@@ -106,3 +108,58 @@ ExecutableNetwork::operator bool() const noexcept {
return !!_impl;
}
} // namespace InferenceEngine
namespace ov {
namespace runtime {
ExecutableNetwork::ExecutableNetwork(const std::shared_ptr<SharedObject>& so,
const std::shared_ptr<ie::IExecutableNetworkInternal>& impl)
: _so{so},
_impl{impl} {
IE_ASSERT(_impl != nullptr);
}
std::shared_ptr<const Function> ExecutableNetwork::get_runtime_function() const {
EXEC_NET_CALL_STATEMENT(return std::const_pointer_cast<const Function>(_impl->GetExecGraphInfo()));
}
ParameterVector ExecutableNetwork::get_parameters() const {
EXEC_NET_CALL_STATEMENT(return _impl->GetExecGraphInfo()->get_parameters());
}
ResultVector ExecutableNetwork::get_results() const {
EXEC_NET_CALL_STATEMENT(return _impl->GetExecGraphInfo()->get_results());
}
InferRequest ExecutableNetwork::create_infer_request() {
EXEC_NET_CALL_STATEMENT(return {_so, _impl->CreateInferRequest()});
}
void ExecutableNetwork::export_model(std::ostream& networkModel) {
EXEC_NET_CALL_STATEMENT(_impl->Export(networkModel));
}
void ExecutableNetwork::set_config(const ie::ParamMap& config) {
EXEC_NET_CALL_STATEMENT(_impl->SetConfig(config));
}
ie::Parameter ExecutableNetwork::get_config(const std::string& name) const {
EXEC_NET_CALL_STATEMENT(return _impl->GetConfig(name));
}
ie::Parameter ExecutableNetwork::get_metric(const std::string& name) const {
EXEC_NET_CALL_STATEMENT(return _impl->GetMetric(name));
}
std::shared_ptr<ie::RemoteContext> ExecutableNetwork::get_context() const {
EXEC_NET_CALL_STATEMENT(return _impl->GetContext());
}
bool ExecutableNetwork::operator!() const noexcept {
return !_impl;
}
ExecutableNetwork::operator bool() const noexcept {
return !!_impl;
}
} // namespace runtime
} // namespace ov
@@ -0,0 +1,49 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <gtest/gtest.h>
#include <openvino/runtime/executable_network.hpp>
using namespace ::testing;
using namespace std;
TEST(ExecutableNetworkOVTests, throwsOnUninitializedExportStream) {
ov::runtime::ExecutableNetwork exec;
ASSERT_THROW(exec.export_model(std::cout), InferenceEngine::NotAllocated);
}
TEST(ExecutableNetworkOVTests, throwsOnUninitializedGetFunction) {
ov::runtime::ExecutableNetwork exec;
ASSERT_THROW(exec.get_runtime_function(), InferenceEngine::NotAllocated);
}
TEST(ExecutableNetworkOVTests, throwsOnUninitializedGetParameters) {
ov::runtime::ExecutableNetwork exec;
ASSERT_THROW(exec.get_parameters(), InferenceEngine::NotAllocated);
}
TEST(ExecutableNetworkOVTests, throwsOnUninitializedGetResults) {
ov::runtime::ExecutableNetwork exec;
ASSERT_THROW(exec.get_results(), InferenceEngine::NotAllocated);
}
TEST(ExecutableNetworkOVTests, throwsOnUninitializedSetConfig) {
ov::runtime::ExecutableNetwork exec;
ASSERT_THROW(exec.set_config({{}}), InferenceEngine::NotAllocated);
}
TEST(ExecutableNetworkOVTests, throwsOnUninitializedGetConfig) {
ov::runtime::ExecutableNetwork exec;
ASSERT_THROW(exec.get_config({}), InferenceEngine::NotAllocated);
}
TEST(ExecutableNetworkOVTests, throwsOnUninitializedGetMetric) {
ov::runtime::ExecutableNetwork exec;
ASSERT_THROW(exec.get_metric({}), InferenceEngine::NotAllocated);
}
TEST(ExecutableNetworkOVTests, throwsOnUninitializedGetContext) {
ov::runtime::ExecutableNetwork exec;
ASSERT_THROW(exec.get_context(), InferenceEngine::NotAllocated);
}
@@ -440,6 +440,36 @@ TEST_P(OVClassNetworkTestP, LoadNetworkActualHeteroDevice2NoThrow) {
ASSERT_NO_THROW(ie.compile_model(actualNetwork, CommonTestUtils::DEVICE_HETERO, {{"TARGET_FALLBACK", deviceName}}));
}
TEST_P(OVClassNetworkTestP, LoadNetworkCreateDefaultExecGraphResult) {
SKIP_IF_CURRENT_TEST_IS_DISABLED()
auto ie = createCoreWithTemplate();
auto net = ie.compile_model(actualNetwork, deviceName);
auto exec_function = net.GetExecGraphInfo().getFunction();
ASSERT_NE(nullptr, exec_function);
auto actual_parameters = exec_function->get_parameters();
auto actual_results = exec_function->get_results();
auto expected_parameters = actualNetwork->get_parameters();
auto expected_results = actualNetwork->get_results();
ASSERT_EQ(expected_parameters.size(), actual_parameters.size());
for (std::size_t i = 0; i < expected_parameters.size(); ++i) {
auto expected_element_type = expected_parameters[i]->get_output_element_type(0);
auto actual_element_type = actual_parameters[i]->get_output_element_type(0);
ASSERT_EQ(expected_element_type, actual_element_type) << "For index: " << i;
auto expected_shape = expected_parameters[i]->get_output_shape(0);
auto actual_shape = actual_parameters[i]->get_output_shape(0);
ASSERT_EQ(expected_shape, actual_shape) << "For index: " << i;
}
ASSERT_EQ(expected_results.size(), actual_results.size());
for (std::size_t i = 0; i < expected_results.size(); ++i) {
auto expected_element_type = expected_results[i]->get_input_element_type(0);
auto actual_element_type = actual_results[i]->get_input_element_type(0);
ASSERT_EQ(expected_element_type, actual_element_type) << "For index: " << i;
auto expected_shape = expected_results[i]->get_input_shape(0);
auto actual_shape = actual_results[i]->get_input_shape(0);
ASSERT_EQ(expected_shape, actual_shape) << "For index: " << i;
}
}
//
// ImportExportNetwork
//