Skip to content

Commit

Permalink
Bind exec core ov (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
bszmelcz authored and akuporos committed Oct 22, 2021
1 parent 80c7070 commit d47f7b1
Show file tree
Hide file tree
Showing 22 changed files with 600 additions and 471 deletions.
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
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
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);
}
76 changes: 76 additions & 0 deletions runtime/bindings/python/src/pyopenvino/core/executable_network.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "openvino/runtime/executable_network.hpp"

#include <pybind11/stl.h>

#include "common.hpp"
#include "pyopenvino/core/containers.hpp"
#include "pyopenvino/core/ie_infer_request.hpp"
#include "pyopenvino/core/ie_input_info.hpp"

namespace py = pybind11;

void regclass_ExecutableNetwork(py::module m) {
py::class_<ov::runtime::ExecutableNetwork, std::shared_ptr<ov::runtime::ExecutableNetwork>> cls(
m,
"ExecutableNetwork");

cls.def("create_infer_request", &ov::runtime::ExecutableNetwork::create_infer_request);

// cls.def("infer_new_request", [](ov::runtime::ExecutableNetwork& self, const py::dict& inputs) {
// TODO: implment after https://github.com/openvinotoolkit/openvino/pull/7962
// will be merged as a seperate ticket
// });

cls.def("export_model", &ov::runtime::ExecutableNetwork::export_model, py::arg("network_model"));

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

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

cls.def("get_runtime_function", &ov::runtime::ExecutableNetwork::get_runtime_function);

cls.def_property_readonly("inputs", &ov::runtime::ExecutableNetwork::inputs);

cls.def("input",
(ov::Output<const ov::Node>(ov::runtime::ExecutableNetwork::*)() const) &
ov::runtime::ExecutableNetwork::input);

cls.def("input",
(ov::Output<const ov::Node>(ov::runtime::ExecutableNetwork::*)(size_t) const) &
ov::runtime::ExecutableNetwork::input,
py::arg("i"));

cls.def("input",
(ov::Output<const ov::Node>(ov::runtime::ExecutableNetwork::*)(const std::string&) const) &
ov::runtime::ExecutableNetwork::input,
py::arg("tensor_name"));

cls.def_property_readonly("outputs", &ov::runtime::ExecutableNetwork::outputs);

cls.def("output",
(ov::Output<const ov::Node>(ov::runtime::ExecutableNetwork::*)() const) &
ov::runtime::ExecutableNetwork::output);

cls.def("output",
(ov::Output<const ov::Node>(ov::runtime::ExecutableNetwork::*)(size_t) const) &
ov::runtime::ExecutableNetwork::output,
py::arg("i"));

cls.def("output",
(ov::Output<const ov::Node>(ov::runtime::ExecutableNetwork::*)(const std::string&) const) &
ov::runtime::ExecutableNetwork::output,
py::arg("tensor_name"));
}
160 changes: 0 additions & 160 deletions runtime/bindings/python/src/pyopenvino/core/ie_core.cpp

This file was deleted.

Loading

0 comments on commit d47f7b1

Please sign in to comment.