Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python API] move frontend bindings #49

2 changes: 1 addition & 1 deletion model-optimizer/mo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from mo.utils.versions_checker import check_requirements # pylint: disable=no-name-in-module

# pylint: disable=no-name-in-module,import-error
from ngraph.frontend import FrontEndManager, FrontEnd
from openvino.frontend import FrontEndManager, FrontEnd


def replace_ext(name: str, old: str, new: str):
Expand Down
2 changes: 1 addition & 1 deletion model-optimizer/mo/main_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

if __name__ == "__main__":
from mo.main import main
from ngraph.frontend import FrontEndManager # pylint: disable=no-name-in-module,import-error
from openvino.frontend import FrontEndManager # pylint: disable=no-name-in-module,import-error

sys.exit(main(get_onnx_cli_parser(), FrontEndManager(), 'onnx'))
2 changes: 1 addition & 1 deletion model-optimizer/mo/main_paddle.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from mo.utils.cli_parser import get_all_cli_parser

from ngraph.frontend import FrontEndManager # pylint: disable=no-name-in-module,import-error
from openvino.frontend import FrontEndManager # pylint: disable=no-name-in-module,import-error


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion model-optimizer/mo/moc_frontend/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from mo.front.extractor import raise_no_node, raise_node_name_collision
from mo.utils.error import Error

from ngraph.frontend import InputModel # pylint: disable=no-name-in-module,import-error
from openvino.frontend import InputModel # pylint: disable=no-name-in-module,import-error

import numpy as np

Expand Down
2 changes: 1 addition & 1 deletion model-optimizer/mo/moc_frontend/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from mo.middle.passes.infer import validate_batch_in_shape

from ngraph import Dimension, PartialShape # pylint: disable=no-name-in-module,import-error
from ngraph.frontend import FrontEnd, Place # pylint: disable=no-name-in-module,import-error
from openvino.frontend import FrontEnd, Place # pylint: disable=no-name-in-module,import-error
from ngraph.utils.types import get_element_type # pylint: disable=no-name-in-module,import-error


Expand Down
2 changes: 1 addition & 1 deletion model-optimizer/mo/utils/check_ie_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def import_core_modules(silent: bool, path_to_module: str):

import openvino # pylint: disable=import-error,no-name-in-module
import ngraph # pylint: disable=import-error,no-name-in-module
import ngraph.frontend # pylint: disable=import-error,no-name-in-module
import openvino.frontend # pylint: disable=import-error,no-name-in-module

if silent:
return True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

# pylint: disable=no-name-in-module,import-error
from ngraph import PartialShape
from ngraph.frontend import FrontEndManager
from openvino.frontend import FrontEndManager
from ngraph.utils.types import get_element_type
akuporos marked this conversation as resolved.
Show resolved Hide resolved

except Exception:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
clear_setup, set_equal_data, set_max_port_counts

# pylint: disable=no-name-in-module,import-error
from ngraph.frontend import FrontEndManager
from openvino.frontend import FrontEndManager

except Exception:
print("No mock frontend API available, "
Expand Down
2 changes: 1 addition & 1 deletion runtime/bindings/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"ngraph.impl.op",
"ngraph.impl.op.util",
"ngraph.impl.passes",
"ngraph.frontend",
"openvino.frontend",
"openvino",
"openvino.opset1",
"openvino.opset2",
Expand Down
9 changes: 0 additions & 9 deletions runtime/bindings/python/src/compatibility/ngraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@
from ngraph.impl import Function
from ngraph.impl import Node
from ngraph.impl import PartialShape
from ngraph.frontend import FrontEnd
from ngraph.frontend import FrontEndManager
from ngraph.frontend import GeneralFailure
from ngraph.frontend import NotImplementedFailure
from ngraph.frontend import InitializationFailure
from ngraph.frontend import InputModel
from ngraph.frontend import OpConversionFailure
from ngraph.frontend import OpValidationFailure
from ngraph.frontend import Place
from ngraph.helpers import function_from_cnn
from ngraph.helpers import function_to_cnn
from ngraph.helpers import partial_shape_from_data
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pybind11_add_module(_${PROJECT_NAME} MODULE ${SOURCES})

target_include_directories(_${PROJECT_NAME} PRIVATE "../")

target_link_libraries(_${PROJECT_NAME} PRIVATE openvino::core openvino::frontend::manager)
target_link_libraries(_${PROJECT_NAME} PRIVATE openvino::core)

if(TARGET onnx_ngraph_frontend)
add_dependencies(_${PROJECT_NAME} onnx_ngraph_frontend)
Expand Down

This file was deleted.

13 changes: 0 additions & 13 deletions runtime/bindings/python/src/compatibility/pyngraph/pyngraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
#endif
#include "pyngraph/dimension.hpp"
#include "pyngraph/discrete_type_info.hpp"
#include "pyngraph/frontend/frontend.hpp"
#include "pyngraph/frontend/frontend_manager.hpp"
#include "pyngraph/frontend/inputmodel.hpp"
#include "pyngraph/frontend/place.hpp"
#include "pyngraph/ops/constant.hpp"
#include "pyngraph/ops/parameter.hpp"
#include "pyngraph/ops/result.hpp"
Expand All @@ -45,15 +41,6 @@ PYBIND11_MODULE(_pyngraph, m) {
regclass_pyngraph_Shape(m);
regclass_pyngraph_PartialShape(m);
regclass_pyngraph_Node(m);
regclass_pyngraph_Place(m);
regclass_pyngraph_InitializationFailureFrontEnd(m);
regclass_pyngraph_GeneralFailureFrontEnd(m);
regclass_pyngraph_OpConversionFailureFrontEnd(m);
regclass_pyngraph_OpValidationFailureFrontEnd(m);
regclass_pyngraph_NotImplementedFailureFrontEnd(m);
regclass_pyngraph_FrontEndManager(m);
regclass_pyngraph_FrontEnd(m);
regclass_pyngraph_InputModel(m);
regclass_pyngraph_Input(m);
regclass_pyngraph_Output(m);
regclass_pyngraph_NodeFactory(m);
Expand Down
1 change: 1 addition & 0 deletions runtime/bindings/python/src/openvino/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from openvino.ie_api import async_infer
from openvino.ie_api import get_result
from openvino.ie_api import blob_from_file
from openvino.ie_api import tensor_from_file

from openvino.impl import Dimension
from openvino.impl import Function
Expand Down
22 changes: 22 additions & 0 deletions runtime/bindings/python/src/openvino/frontend/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (C) 2018-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

"""
Package: ngraph
akuporos marked this conversation as resolved.
Show resolved Hide resolved
Low level wrappers for the FrontEnd c++ api.
"""

# flake8: noqa

# main classes
from openvino.pyopenvino import FrontEndManager
from openvino.pyopenvino import FrontEnd
from openvino.pyopenvino import InputModel
from openvino.pyopenvino import Place

# exceptions
from openvino.pyopenvino import NotImplementedFailure
from openvino.pyopenvino import InitializationFailure
from openvino.pyopenvino import OpConversionFailure
from openvino.pyopenvino import OpValidationFailure
from openvino.pyopenvino import GeneralFailure
6 changes: 6 additions & 0 deletions runtime/bindings/python/src/openvino/ie_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from openvino.pyopenvino import TBlobUint8
from openvino.pyopenvino import TensorDesc
from openvino.pyopenvino import InferRequest
from openvino.pyopenvino import Tensor

import numpy as np

Expand Down Expand Up @@ -112,3 +113,8 @@ def blob_from_file(path_to_bin_file: str) -> BlobWrapper:
array = np.fromfile(path_to_bin_file, dtype=np.uint8)
tensor_desc = TensorDesc("U8", array.shape, "C")
return BlobWrapper(tensor_desc, array)

# flake8: noqa: D102
def tensor_from_file(path: str) -> Tensor:
"""The data will be read with dtype of unit8"""
return Tensor(np.fromfile(path, dtype=np.uint8))
2 changes: 1 addition & 1 deletion runtime/bindings/python/src/openvino/impl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@
from openvino.pyopenvino import AxisSet
from openvino.pyopenvino import AxisVector
from openvino.pyopenvino import Coordinate
from openvino.pyopenvino import Output
from openvino.pyopenvino import ConstOutput

from openvino.pyopenvino import util
22 changes: 21 additions & 1 deletion runtime/bindings/python/src/pyopenvino/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,34 @@ if(OpenVINO_SOURCE_DIR)
set(CMAKE_PDB_OUTPUT_DIRECTORY ${PYTHON_BRIDGE_OUTPUT_DIRECTORY})
endif()

# compile options

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# disable warning: This operator was deprecated and will be removed with v0 operation.
add_compile_options(/wd4996)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options(-Wno-deprecated-register -Wno-range-loop-analysis)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
add_link_options(-stdlib=libc++)
add_compile_options(-Wno-unused-value -Wno-range-loop-analysis)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# WA for GCC 7.5 "PYBIND11_NOINLINE inline" warning
add_compile_options(-Wno-error=attributes)
endif()

if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# for proper fix need to update pybind to version which does not use PyEval_InitThreads()
add_compile_options(-Wno-deprecated-declarations -Wno-undef)
endif()

# create target

file(GLOB_RECURSE SOURCES *.cpp)

pybind11_add_module(${PROJECT_NAME} MODULE ${SOURCES})

target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/..")
target_link_libraries(${PROJECT_NAME} PRIVATE openvino::runtime)
target_link_libraries(${PROJECT_NAME} PRIVATE openvino::runtime openvino::frontend::manager)

# perform copy
if(OpenVINO_SOURCE_DIR)
Expand Down
114 changes: 114 additions & 0 deletions runtime/bindings/python/src/pyopenvino/core/core.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "pyopenvino/core/core.hpp"

#include <ie_extension.h>
#include <pybind11/stl.h>

#include <openvino/runtime/core.hpp>
#include <pyopenvino/core/tensor.hpp>

#include "common.hpp"

namespace py = pybind11;

using ConfigMap = std::map<std::string, std::string>;

std::string to_string(py::handle handle) {
auto encodedString = PyUnicode_AsUTF8String(handle.ptr());
return PyBytes_AsString(encodedString);
}

void regclass_Core(py::module m) {
py::class_<ov::runtime::Core, std::shared_ptr<ov::runtime::Core>> cls(m, "Core");
cls.def(py::init<const std::string&>(), py::arg("xml_config_file") = "");

cls.def("set_config",
(void (ov::runtime::Core::*)(const ConfigMap&, const std::string&)) & ov::runtime::Core::set_config,
py::arg("config"),
py::arg("device_name") = "");

cls.def(
"compile_model",
(ov::runtime::ExecutableNetwork(
ov::runtime::Core::*)(const std::shared_ptr<const ov::Function>&, const std::string&, const ConfigMap&)) &
ov::runtime::Core::compile_model,
py::arg("network"),
py::arg("device_name"),
py::arg("config") = py::dict());

cls.def(
"add_extension",
[](ov::runtime::Core& self, const std::string& extension_path) {
auto extension_ptr = InferenceEngine::make_so_pointer<InferenceEngine::IExtension>(extension_path);
auto extension = std::dynamic_pointer_cast<InferenceEngine::IExtension>(extension_ptr);
self.add_extension(extension);
},
py::arg("extension_path"));

cls.def("get_versions", &ov::runtime::Core::get_versions);

cls.def("read_model",
(std::shared_ptr<ov::Function>(ov::runtime::Core::*)(const std::string&, const std::string&) const) &
ov::runtime::Core::read_model,
py::arg("model"),
py::arg("weights") = "");

cls.def(
"read_model",
(std::shared_ptr<ov::Function>(ov::runtime::Core::*)(const std::string&, const ov::runtime::Tensor&) const) &
ov::runtime::Core::read_model,
py::arg("model"),
py::arg("weights"));

cls.def(
"read_model",
[](ov::runtime::Core& self, py::object model, py::object weights) {
return self.read_model(py::str(model), py::str(weights));
},
py::arg("model"),
py::arg("weights") = "");

cls.def(
"import_model",
(ov::runtime::ExecutableNetwork(ov::runtime::Core::*)(std::istream&, const std::string&, const ConfigMap&)) &
ov::runtime::Core::import_model,
py::arg("model_file"),
py::arg("device_name"),
py::arg("config") = py::none());

cls.def(
"get_config",
[](ov::runtime::Core& self, const std::string& device_name, const std::string& name) -> py::handle {
return Common::parse_parameter(self.get_config(device_name, name));
},
py::arg("device_name"),
py::arg("name"));

cls.def(
"get_metric",
[](ov::runtime::Core& self, const std::string device_name, const std::string name) -> py::handle {
return Common::parse_parameter(self.get_metric(device_name, name));
},
py::arg("device_name"),
py::arg("name"));

cls.def("register_plugin", &ov::runtime::Core::register_plugin, py::arg("plugin_name"), py::arg("device_name"));

cls.def("register_plugins", &ov::runtime::Core::register_plugins, py::arg("xml_config_file"));

cls.def("unload_plugin", &ov::runtime::Core::unload_plugin, py::arg("device_name"));

cls.def(
"query_model",
(ov::runtime::SupportedOpsMap(
ov::runtime::Core::*)(const std::shared_ptr<const ov::Function>&, const std::string&, const ConfigMap&)) &
ov::runtime::Core::query_model,
py::arg("model"),
py::arg("device_name"),
py::arg("config") = py::dict());

cls.def_property_readonly("available_devices", &ov::runtime::Core::get_available_devices);
}
Loading