Skip to content

Commit

Permalink
return back py::object -> AnyMap
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-esir committed Jul 24, 2024
1 parent f9e45e1 commit 8461652
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/python/py_generate_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,33 @@ OptionalGenerationConfig update_config_from_kwargs(const OptionalGenerationConfi
return res_config;
}

ov::Any py_object_to_any(const py::object& py_obj);

bool py_object_is_any_map(const py::object& py_obj) {
if (!py::isinstance<py::dict>(py_obj)) {
return false;
}
auto dict = py::cast<py::dict>(py_obj);
return std::all_of(dict.begin(), dict.end(), [&](const std::pair<py::object::handle, py::object::handle>& elem) {
return py::isinstance<py::str>(elem.first);
});
}

ov::AnyMap py_object_to_any_map(const py::object& py_obj) {
OPENVINO_ASSERT(py_object_is_any_map(py_obj), "Unsupported attribute type.");
ov::AnyMap return_value = {};
for (auto& item : py::cast<py::dict>(py_obj)) {
std::string key = py::cast<std::string>(item.first);
py::object value = py::cast<py::object>(item.second);
if (py_object_is_any_map(value)) {
return_value[key] = py_object_to_any_map(value);
} else {
return_value[key] = py_object_to_any(value);
}
}
return return_value;
}

ov::Any py_object_to_any(const py::object& py_obj) {
// Python types
py::object float_32_type = py::module_::import("numpy").attr("float32");
Expand Down Expand Up @@ -213,6 +240,8 @@ ov::Any py_object_to_any(const py::object& py_obj) {
}

// OV types
} else if (py_object_is_any_map(py_obj)) {
return py_object_to_any_map(py_obj);
} else if (py::isinstance<ov::Any>(py_obj)) {
return py::cast<ov::Any>(py_obj);
} else if (py::isinstance<ov::element::Type>(py_obj)) {
Expand Down

0 comments on commit 8461652

Please sign in to comment.