[Python API] Update names in Model class (#11348)

* [Python API] Update names in Model class

* fix code-style

* remove from_capsule in the new API
This commit is contained in:
Anastasia Kuporosova 2022-03-31 00:33:57 +03:00 committed by GitHub
parent 4c7050f6a9
commit d107cec39f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 227 additions and 283 deletions

View File

@ -19,8 +19,6 @@
namespace py = pybind11;
static const char* CAPSULE_NAME = "openvino_function";
using PyRTMap = ov::RTMap;
PYBIND11_MAKE_OPAQUE(PyRTMap);
@ -46,22 +44,22 @@ ov::SinkVector cast_to_sink_vector(const std::vector<std::shared_ptr<ov::Node>>&
}
void regclass_graph_Model(py::module m) {
py::class_<ov::Model, std::shared_ptr<ov::Model>> function(m, "Model", py::module_local());
function.doc() = "openvino.runtime.Model wraps ov::Model";
py::class_<ov::Model, std::shared_ptr<ov::Model>> model(m, "Model", py::module_local());
model.doc() = "openvino.runtime.Model wraps ov::Model";
function.def(py::init([](const ov::ResultVector& res,
const std::vector<std::shared_ptr<ov::Node>>& nodes,
const ov::ParameterVector& params,
const std::string& name) {
set_tensor_names(params);
const auto sinks = cast_to_sink_vector(nodes);
return std::make_shared<ov::Model>(res, sinks, params, name);
}),
py::arg("results"),
py::arg("sinks"),
py::arg("parameters"),
py::arg("name"),
R"(
model.def(py::init([](const ov::ResultVector& res,
const std::vector<std::shared_ptr<ov::Node>>& nodes,
const ov::ParameterVector& params,
const std::string& name) {
set_tensor_names(params);
const auto sinks = cast_to_sink_vector(nodes);
return std::make_shared<ov::Model>(res, sinks, params, name);
}),
py::arg("results"),
py::arg("sinks"),
py::arg("parameters"),
py::arg("name"),
R"(
Create user-defined Model which is a representation of a model.
:param results: List of results.
@ -70,51 +68,51 @@ void regclass_graph_Model(py::module m) {
:type sinks: List[openvino.runtime.Node]
:param parameters: List of parameters.
:type parameters: List[op.Parameter]
:param name: String to set as function's friendly name.
:param name: String to set as model's friendly name.
:type name: str
)");
function.def(py::init([](const std::vector<std::shared_ptr<ov::Node>>& results,
const ov::ParameterVector& parameters,
const std::string& name) {
set_tensor_names(parameters);
return std::make_shared<ov::Model>(results, parameters, name);
}),
py::arg("results"),
py::arg("parameters"),
py::arg("name") = "",
R"(
model.def(py::init([](const std::vector<std::shared_ptr<ov::Node>>& results,
const ov::ParameterVector& parameters,
const std::string& name) {
set_tensor_names(parameters);
return std::make_shared<ov::Model>(results, parameters, name);
}),
py::arg("results"),
py::arg("parameters"),
py::arg("name") = "",
R"(
Create user-defined Model which is a representation of a model.
:param results: List of Nodes to be used as results.
:type results: List[openvino.runtime.Node]
:param parameters: List of parameters.
:type parameters: List[op.Parameter]
:param name: String to set as function's friendly name.
:param name: String to set as model's friendly name.
:type name: str
)");
function.def(py::init([](const std::shared_ptr<ov::Node>& result,
const ov::ParameterVector& parameters,
const std::string& name) {
set_tensor_names(parameters);
return std::make_shared<ov::Model>(result, parameters, name);
}),
py::arg("result"),
py::arg("parameters"),
py::arg("name") = "",
R"(
model.def(py::init([](const std::shared_ptr<ov::Node>& result,
const ov::ParameterVector& parameters,
const std::string& name) {
set_tensor_names(parameters);
return std::make_shared<ov::Model>(result, parameters, name);
}),
py::arg("result"),
py::arg("parameters"),
py::arg("name") = "",
R"(
Create user-defined Model which is a representation of a model.
:param result: Node to be used as result.
:type result: openvino.runtime.Node
:param parameters: List of parameters.
:type parameters: List[op.Parameter]
:param name: String to set as function's friendly name.
:param name: String to set as model's friendly name.
:type name: str
)");
function.def(
model.def(
py::init([](const ov::OutputVector& results, const ov::ParameterVector& parameters, const std::string& name) {
set_tensor_names(parameters);
return std::make_shared<ov::Model>(results, parameters, name);
@ -129,47 +127,47 @@ void regclass_graph_Model(py::module m) {
:type results: List[openvino.runtime.Output]
:param parameters: List of parameters.
:type parameters: List[op.Parameter]
:param name: String to set as function's friendly name.
:param name: String to set as model's friendly name.
:type name: str
)");
function.def(py::init([](const ov::OutputVector& results,
const std::vector<std::shared_ptr<ov::Node>>& nodes,
const ov::ParameterVector& parameters,
const std::string& name) {
set_tensor_names(parameters);
const auto sinks = cast_to_sink_vector(nodes);
return std::make_shared<ov::Model>(results, sinks, parameters, name);
}),
py::arg("results"),
py::arg("sinks"),
py::arg("parameters"),
py::arg("name") = "",
R"(
model.def(py::init([](const ov::OutputVector& results,
const std::vector<std::shared_ptr<ov::Node>>& nodes,
const ov::ParameterVector& parameters,
const std::string& name) {
set_tensor_names(parameters);
const auto sinks = cast_to_sink_vector(nodes);
return std::make_shared<ov::Model>(results, sinks, parameters, name);
}),
py::arg("results"),
py::arg("sinks"),
py::arg("parameters"),
py::arg("name") = "",
R"(
Create user-defined Model which is a representation of a model
:param results: List of outputs.
:type results: List[openvino.runtime.Output]
:param sinks: List of Nodes to be used as Sinks (e.g. Assign ops).
:type sinks: List[openvino.runtime.Node]
:param name: String to set as function's friendly name.
:param name: String to set as model's friendly name.
:type name: str
)");
function.def(py::init([](const ov::ResultVector& results,
const std::vector<std::shared_ptr<ov::Node>>& nodes,
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
set_tensor_names(parameters);
const auto sinks = cast_to_sink_vector(nodes);
return std::make_shared<ov::Model>(results, sinks, parameters, variables, name);
}),
py::arg("results"),
py::arg("sinks"),
py::arg("parameters"),
py::arg("variables"),
py::arg("name") = "",
R"(
model.def(py::init([](const ov::ResultVector& results,
const std::vector<std::shared_ptr<ov::Node>>& nodes,
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
set_tensor_names(parameters);
const auto sinks = cast_to_sink_vector(nodes);
return std::make_shared<ov::Model>(results, sinks, parameters, variables, name);
}),
py::arg("results"),
py::arg("sinks"),
py::arg("parameters"),
py::arg("variables"),
py::arg("name") = "",
R"(
Create user-defined Model which is a representation of a model
:param results: List of results.
@ -180,25 +178,25 @@ void regclass_graph_Model(py::module m) {
:type parameters: List[op.Parameter]
:param variables: List of variables.
:type variables: List[op.util.Variable]
:param name: String to set as function's friendly name.
:param name: String to set as model's friendly name.
:type name: str
)");
function.def(py::init([](const ov::OutputVector& results,
const std::vector<std::shared_ptr<ov::Node>>& nodes,
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
set_tensor_names(parameters);
const auto sinks = cast_to_sink_vector(nodes);
return std::make_shared<ov::Model>(results, sinks, parameters, variables, name);
}),
py::arg("results"),
py::arg("sinks"),
py::arg("parameters"),
py::arg("variables"),
py::arg("name") = "",
R"(
model.def(py::init([](const ov::OutputVector& results,
const std::vector<std::shared_ptr<ov::Node>>& nodes,
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
set_tensor_names(parameters);
const auto sinks = cast_to_sink_vector(nodes);
return std::make_shared<ov::Model>(results, sinks, parameters, variables, name);
}),
py::arg("results"),
py::arg("sinks"),
py::arg("parameters"),
py::arg("variables"),
py::arg("name") = "",
R"(
Create user-defined Model which is a representation of a model
:param results: List of results.
@ -207,22 +205,22 @@ void regclass_graph_Model(py::module m) {
:type sinks: List[openvino.runtime.Node]
:param variables: List of variables.
:type variables: List[op.util.Variable]
:param name: String to set as function's friendly name.
:param name: String to set as model's friendly name.
:type name: str
)");
function.def(py::init([](const ov::ResultVector& results,
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
set_tensor_names(parameters);
return std::make_shared<ov::Model>(results, parameters, variables, name);
}),
py::arg("results"),
py::arg("parameters"),
py::arg("variables"),
py::arg("name") = "",
R"(
model.def(py::init([](const ov::ResultVector& results,
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
set_tensor_names(parameters);
return std::make_shared<ov::Model>(results, parameters, variables, name);
}),
py::arg("results"),
py::arg("parameters"),
py::arg("variables"),
py::arg("name") = "",
R"(
Create user-defined Model which is a representation of a model
:param results: List of results.
@ -231,35 +229,35 @@ void regclass_graph_Model(py::module m) {
:type parameters: List[op.Parameter]
:param variables: List of variables.
:type variables: List[op.util.Variable]
:param name: String to set as function's friendly name.
:param name: String to set as model's friendly name.
:type name: str
)");
function.def(py::init([](const ov::OutputVector& results,
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
set_tensor_names(parameters);
return std::make_shared<ov::Model>(results, parameters, variables, name);
}),
py::arg("results"),
py::arg("parameters"),
py::arg("variables"),
py::arg("name") = "",
R"(
model.def(py::init([](const ov::OutputVector& results,
const ov::ParameterVector& parameters,
const ov::op::util::VariableVector& variables,
const std::string& name) {
set_tensor_names(parameters);
return std::make_shared<ov::Model>(results, parameters, variables, name);
}),
py::arg("results"),
py::arg("parameters"),
py::arg("variables"),
py::arg("name") = "",
R"(
Create user-defined Model which is a representation of a model
:param results: List of results.
:type results: List[openvino.runtime.Output]
:param parameters: List of parameters.
:type parameters: List[op.Parameter]
:param name: String to set as function's friendly name.
:param name: String to set as model's friendly name.
:type name: str
)");
function.def("validate_nodes_and_infer_types", &ov::Model::validate_nodes_and_infer_types);
model.def("validate_nodes_and_infer_types", &ov::Model::validate_nodes_and_infer_types);
function.def(
model.def(
"reshape",
[](ov::Model& self, const ov::PartialShape& partial_shape) {
self.reshape(partial_shape);
@ -271,7 +269,7 @@ void regclass_graph_Model(py::module m) {
:return : void
)");
function.def(
model.def(
"reshape",
[](ov::Model& self, const py::list& partial_shape) {
self.reshape(Common::partial_shape_from_list(partial_shape));
@ -283,7 +281,7 @@ void regclass_graph_Model(py::module m) {
:return : void
)");
function.def(
model.def(
"reshape",
[](ov::Model& self, const py::tuple& partial_shape) {
self.reshape(Common::partial_shape_from_list(partial_shape.cast<py::list>()));
@ -295,7 +293,7 @@ void regclass_graph_Model(py::module m) {
:return : void
)");
function.def(
model.def(
"reshape",
[](ov::Model& self, const std::string& partial_shape) {
self.reshape(Common::partial_shape_from_str(partial_shape));
@ -307,7 +305,7 @@ void regclass_graph_Model(py::module m) {
:return : void
)");
function.def(
model.def(
"reshape",
[](ov::Model& self, const py::dict& partial_shapes) {
std::map<ov::Output<ov::Node>, ov::PartialShape> new_shapes;
@ -373,34 +371,34 @@ void regclass_graph_Model(py::module m) {
:type partial_shapes: Dict[keys, values]
)");
function.def("get_output_size",
&ov::Model::get_output_size,
R"(
Return the number of outputs for the function.
model.def("get_output_size",
&ov::Model::get_output_size,
R"(
Return the number of outputs for the model.
:return: Number of outputs.
:rtype: int
)");
function.def("get_ops",
&ov::Model::get_ops,
R"(
Return ops used in the function.
model.def("get_ops",
&ov::Model::get_ops,
R"(
Return ops used in the model.
:return: List of Nodes representing ops used in function.
:return: List of Nodes representing ops used in model.
:rtype: List[openvino.runtime.Node]
)");
function.def("get_ordered_ops",
&ov::Model::get_ordered_ops,
R"(
Return ops used in the function in topological order.
model.def("get_ordered_ops",
&ov::Model::get_ordered_ops,
R"(
Return ops used in the model in topological order.
:return: List of sorted Nodes representing ops used in function.
:return: List of sorted Nodes representing ops used in model.
:rtype: List[openvino.runtime.Node]
)");
function.def("get_output_op",
&ov::Model::get_output_op,
py::arg("index"),
R"(
model.def("get_output_op",
&ov::Model::get_output_op,
py::arg("index"),
R"(
Return the op that generates output i
:param index: output index
@ -408,10 +406,10 @@ void regclass_graph_Model(py::module m) {
:return: Node object that generates output i
:rtype: openvino.runtime.Node
)");
function.def("get_output_element_type",
&ov::Model::get_output_element_type,
py::arg("index"),
R"(
model.def("get_output_element_type",
&ov::Model::get_output_element_type,
py::arg("index"),
R"(
Return the element type of output i
:param index: output index
@ -419,10 +417,10 @@ void regclass_graph_Model(py::module m) {
:return: Type object of output i
:rtype: openvino.runtime.Type
)");
function.def("get_output_shape",
&ov::Model::get_output_shape,
py::arg("index"),
R"(
model.def("get_output_shape",
&ov::Model::get_output_shape,
py::arg("index"),
R"(
Return the shape of element i
:param index: element index
@ -430,10 +428,10 @@ void regclass_graph_Model(py::module m) {
:return: Shape object of element i
:rtype: openvino.runtime.Shape
)");
function.def("get_output_partial_shape",
&ov::Model::get_output_partial_shape,
py::arg("index"),
R"(
model.def("get_output_partial_shape",
&ov::Model::get_output_partial_shape,
py::arg("index"),
R"(
Return the partial shape of element i
:param index: element index
@ -441,34 +439,34 @@ void regclass_graph_Model(py::module m) {
:return: PartialShape object of element i
:rtype: openvino.runtime.PartialShape
)");
function.def("get_parameters",
&ov::Model::get_parameters,
R"(
Return the function parameters.
model.def("get_parameters",
&ov::Model::get_parameters,
R"(
Return the model parameters.
:return: ParameterVector containing function parameters.
:return: ParameterVector containing model parameters.
:rtype: ParameterVector
)");
function.def("get_results",
&ov::Model::get_results,
R"(
Return a list of function outputs.
model.def("get_results",
&ov::Model::get_results,
R"(
Return a list of model outputs.
:return: ResultVector containing function parameters.
:return: ResultVector containing model parameters.
:rtype: ResultVector
)");
function.def("get_result",
&ov::Model::get_result,
R"(
model.def("get_result",
&ov::Model::get_result,
R"(
Return single result.
:return: Node object representing result.
:rtype: openvino.runtime.Node
)");
function.def("get_result_index",
(int64_t(ov::Model::*)(const ov::Output<ov::Node>&) const) & ov::Model::get_result_index,
py::arg("value"),
R"(
model.def("get_result_index",
(int64_t(ov::Model::*)(const ov::Output<ov::Node>&) const) & ov::Model::get_result_index,
py::arg("value"),
R"(
Return index of result.
Return -1 if `value` not matched.
@ -478,10 +476,10 @@ void regclass_graph_Model(py::module m) {
:return: Index for value referencing it.
:rtype: int
)");
function.def("get_result_index",
(int64_t(ov::Model::*)(const ov::Output<const ov::Node>&) const) & ov::Model::get_result_index,
py::arg("value"),
R"(
model.def("get_result_index",
(int64_t(ov::Model::*)(const ov::Output<const ov::Node>&) const) & ov::Model::get_result_index,
py::arg("value"),
R"(
Return index of result.
Return -1 if `value` not matched.
@ -492,81 +490,77 @@ void regclass_graph_Model(py::module m) {
:rtype: int
)");
function.def("get_name",
&ov::Model::get_name,
R"(
Get the unique name of the function.
model.def("get_name",
&ov::Model::get_name,
R"(
Get the unique name of the model.
:return: String with a name of the function.
:return: String with a name of the model.
:rtype: str
)");
function.def("get_friendly_name",
&ov::Model::get_friendly_name,
R"(
Gets the friendly name for a function. If no
model.def("get_friendly_name",
&ov::Model::get_friendly_name,
R"(
Gets the friendly name for a model. If no
friendly name has been set via set_friendly_name
then the function's unique name is returned.
then the model's unique name is returned.
:return: String with a friendly name of the function.
:return: String with a friendly name of the model.
:rtype: str
)");
function.def("set_friendly_name",
&ov::Model::set_friendly_name,
py::arg("name"),
R"(
Sets a friendly name for a function. This does
not overwrite the unique name of the function and
model.def("set_friendly_name",
&ov::Model::set_friendly_name,
py::arg("name"),
R"(
Sets a friendly name for a model. This does
not overwrite the unique name of the model and
is retrieved via get_friendly_name(). Used mainly
for debugging.
:param name: String to set as the friendly name.
:type name: str
)");
function.def("is_dynamic",
&ov::Model::is_dynamic,
R"(
Returns true if any of the op's defined in the function
model.def("is_dynamic",
&ov::Model::is_dynamic,
R"(
Returns true if any of the op's defined in the model
contains partial shape.
:rtype: bool
)");
function.def("input", (ov::Output<ov::Node>(ov::Model::*)()) & ov::Model::input);
model.def("input", (ov::Output<ov::Node>(ov::Model::*)()) & ov::Model::input);
function.def("input", (ov::Output<ov::Node>(ov::Model::*)(size_t)) & ov::Model::input, py::arg("index"));
model.def("input", (ov::Output<ov::Node>(ov::Model::*)(size_t)) & ov::Model::input, py::arg("index"));
function.def("input",
(ov::Output<ov::Node>(ov::Model::*)(const std::string&)) & ov::Model::input,
py::arg("tensor_name"));
model.def("input",
(ov::Output<ov::Node>(ov::Model::*)(const std::string&)) & ov::Model::input,
py::arg("tensor_name"));
function.def("input", (ov::Output<const ov::Node>(ov::Model::*)() const) & ov::Model::input);
model.def("input", (ov::Output<const ov::Node>(ov::Model::*)() const) & ov::Model::input);
function.def("input",
(ov::Output<const ov::Node>(ov::Model::*)(size_t) const) & ov::Model::input,
py::arg("index"));
model.def("input", (ov::Output<const ov::Node>(ov::Model::*)(size_t) const) & ov::Model::input, py::arg("index"));
function.def("input",
(ov::Output<const ov::Node>(ov::Model::*)(const std::string&) const) & ov::Model::input,
py::arg("tensor_name"));
model.def("input",
(ov::Output<const ov::Node>(ov::Model::*)(const std::string&) const) & ov::Model::input,
py::arg("tensor_name"));
function.def("output", (ov::Output<ov::Node>(ov::Model::*)()) & ov::Model::output);
model.def("output", (ov::Output<ov::Node>(ov::Model::*)()) & ov::Model::output);
function.def("output", (ov::Output<ov::Node>(ov::Model::*)(size_t)) & ov::Model::output, py::arg("index"));
model.def("output", (ov::Output<ov::Node>(ov::Model::*)(size_t)) & ov::Model::output, py::arg("index"));
function.def("output",
(ov::Output<ov::Node>(ov::Model::*)(const std::string&)) & ov::Model::output,
py::arg("tensor_name"));
model.def("output",
(ov::Output<ov::Node>(ov::Model::*)(const std::string&)) & ov::Model::output,
py::arg("tensor_name"));
function.def("output", (ov::Output<const ov::Node>(ov::Model::*)() const) & ov::Model::output);
model.def("output", (ov::Output<const ov::Node>(ov::Model::*)() const) & ov::Model::output);
function.def("output",
(ov::Output<const ov::Node>(ov::Model::*)(size_t) const) & ov::Model::output,
py::arg("index"));
model.def("output", (ov::Output<const ov::Node>(ov::Model::*)(size_t) const) & ov::Model::output, py::arg("index"));
function.def("output",
(ov::Output<const ov::Node>(ov::Model::*)(const std::string&) const) & ov::Model::output,
py::arg("tensor_name"));
model.def("output",
(ov::Output<const ov::Node>(ov::Model::*)(const std::string&) const) & ov::Model::output,
py::arg("tensor_name"));
function.def(
model.def(
"add_outputs",
[](ov::Model& self, py::handle& outputs) {
int i = 0;
@ -606,15 +600,15 @@ void regclass_graph_Model(py::module m) {
},
py::arg("outputs"));
function.def("replace_parameter",
&ov::Model::replace_parameter,
py::arg("parameter_index"),
py::arg("parameter"),
R"(
Replace the `parameter_index` parameter of the function with `parameter`
model.def("replace_parameter",
&ov::Model::replace_parameter,
py::arg("parameter_index"),
py::arg("parameter"),
R"(
Replace the `parameter_index` parameter of the model with `parameter`
All users of the `parameter_index` parameter are redirected to `parameter` , and the
`parameter_index` entry in the function parameter list is replaced with `parameter`
`parameter_index` entry in the model parameter list is replaced with `parameter`
:param parameter_index: The index of the parameter to replace.
:type parameter_index: int
@ -622,7 +616,7 @@ void regclass_graph_Model(py::module m) {
:type parameter: op.Parameter
)");
function.def(
model.def(
"get_parameter_index",
(int64_t(ov::Model::*)(const std::shared_ptr<ov::op::v0::Parameter>&) const) & ov::Model::get_parameter_index,
py::arg("parameter"),
@ -637,7 +631,7 @@ void regclass_graph_Model(py::module m) {
:rtype: int
)");
function.def(
model.def(
"evaluate",
[](ov::Model& self,
ov::TensorVector& output_tensors,
@ -649,20 +643,20 @@ void regclass_graph_Model(py::module m) {
py::arg("input_tensors"),
py::arg("evaluation_context") = PyRTMap(),
R"(
Evaluate the function on inputs, putting results in outputs
Evaluate the model on inputs, putting results in outputs
:param output_tensors: Tensors for the outputs to compute. One for each result
:type output_tensors: List[openvino.runtime.Tensor]
:param input_tensors: Tensors for the inputs. One for each inputs.
:type input_tensors: List[openvino.runtime.Tensor]
:param evaluation_context: Storage of additional settings and attributes that can be used
when evaluating the function. This additional information can be
when evaluating the model. This additional information can be
shared across nodes.
:type evaluation_context: openvino.runtime.RTMap
:rtype: bool
)");
function.def("__repr__", [](const ov::Model& self) {
model.def("__repr__", [](const ov::Model& self) {
std::string class_name = py::cast(self).get_type().attr("__name__").cast<std::string>();
auto inputs_str = Common::docs::container_to_string(self.inputs(), ",\n");
@ -672,42 +666,8 @@ void regclass_graph_Model(py::module m) {
outputs_str + "\n]>";
});
function.def_static("from_capsule", [](py::object* capsule) {
// get the underlying PyObject* which is a PyCapsule pointer
auto* pybind_capsule_ptr = capsule->ptr();
// extract the pointer stored in the PyCapsule under the name CAPSULE_NAME
auto* capsule_ptr = PyCapsule_GetPointer(pybind_capsule_ptr, CAPSULE_NAME);
auto* ngraph_function = static_cast<std::shared_ptr<ov::Model>*>(capsule_ptr);
if (ngraph_function && *ngraph_function) {
return *ngraph_function;
} else {
throw std::runtime_error("The provided capsule does not contain an ov::Model");
}
});
function.def_static("to_capsule", [](std::shared_ptr<ov::Model>& ngraph_function) {
// create a shared pointer on the heap before putting it in the capsule
// this secures the lifetime of the object transferred by the capsule
auto* sp_copy = new std::shared_ptr<ov::Model>(ngraph_function);
// a destructor callback that will delete the heap allocated shared_ptr
// when the capsule is destructed
auto sp_deleter = [](PyObject* capsule) {
auto* capsule_ptr = PyCapsule_GetPointer(capsule, CAPSULE_NAME);
auto* function_sp = static_cast<std::shared_ptr<ov::Model>*>(capsule_ptr);
if (function_sp) {
delete function_sp;
}
};
// put the shared_ptr in a new capsule under the same name as in "from_capsule"
auto pybind_capsule = py::capsule(sp_copy, CAPSULE_NAME, sp_deleter);
return pybind_capsule;
});
function.def_property_readonly("inputs", (std::vector<ov::Output<ov::Node>>(ov::Model::*)()) & ov::Model::inputs);
function.def_property_readonly("outputs", (std::vector<ov::Output<ov::Node>>(ov::Model::*)()) & ov::Model::outputs);
function.def_property_readonly("name", &ov::Model::get_name);
function.def_property("friendly_name", &ov::Model::get_friendly_name, &ov::Model::set_friendly_name);
model.def_property_readonly("inputs", (std::vector<ov::Output<ov::Node>>(ov::Model::*)()) & ov::Model::inputs);
model.def_property_readonly("outputs", (std::vector<ov::Output<ov::Node>>(ov::Model::*)()) & ov::Model::outputs);
model.def_property_readonly("name", &ov::Model::get_name);
model.def_property("friendly_name", &ov::Model::get_friendly_name, &ov::Model::set_friendly_name);
}

View File

@ -18,8 +18,6 @@
namespace py = pybind11;
static const char* CAPSULE_NAME = "ngraph_partial_shape";
void regclass_graph_PartialShape(py::module m) {
py::class_<ov::PartialShape, std::shared_ptr<ov::PartialShape>> shape(m, "PartialShape");
shape.doc() = "openvino.runtime.PartialShape wraps ov::PartialShape";
@ -193,18 +191,4 @@ void regclass_graph_PartialShape(py::module m) {
shape.def("__repr__", [](const ov::PartialShape& self) -> std::string {
return "<PartialShape: " + py::cast(self).attr("__str__")().cast<std::string>() + ">";
});
shape.def_static("from_capsule", [](py::object* capsule) {
// get the underlying PyObject* which is a PyCapsule pointer
auto* pybind_capsule_ptr = capsule->ptr();
// extract the pointer stored in the PyCapsule under the name CAPSULE_NAME
auto* capsule_ptr = PyCapsule_GetPointer(pybind_capsule_ptr, CAPSULE_NAME);
auto* ngraph_pShape = static_cast<std::shared_ptr<ov::PartialShape>*>(capsule_ptr);
if (ngraph_pShape && *ngraph_pShape) {
return *ngraph_pShape;
} else {
throw std::runtime_error("The provided capsule does not contain an ov::PartialShape");
}
});
}