Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

Fix set_property of ModelDistributionPolicy with set type in python (#29124)

Sun Xiaoxia34b3367928ff
diff --git a/src/bindings/python/src/pyopenvino/graph/attribute_visitor.cpp b/src/bindings/python/src/pyopenvino/graph/attribute_visitor.cpp
index 82699434d80..adb33797bfb 100644
--- a/src/bindings/python/src/pyopenvino/graph/attribute_visitor.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/attribute_visitor.cpp
@@ -71,7 +71,7 @@ void regclass_graph_AttributeVisitor(py::module m) {
OPENVINO_ASSERT(!_list.empty(), "Attributes list is empty.");
- PY_TYPE detected_type = Common::utils::check_list_element_type(_list);
+ PY_TYPE detected_type = Common::utils::check_container_element_type<py::list>(_list);
switch (detected_type) {
case PY_TYPE::STR:
diff --git a/src/bindings/python/src/pyopenvino/utils/utils.cpp b/src/bindings/python/src/pyopenvino/utils/utils.cpp
index f19405df814..8520a5781a8 100644
--- a/src/bindings/python/src/pyopenvino/utils/utils.cpp
+++ b/src/bindings/python/src/pyopenvino/utils/utils.cpp
@@ -24,7 +24,8 @@ using Version = ov::pass::Serialize::Version;
namespace Common {
namespace utils {
-PY_TYPE check_list_element_type(const py::list& list) {
+template <typename T>
+PY_TYPE check_container_element_type(const T& container) {
PY_TYPE detected_type = PY_TYPE::UNKNOWN;
auto check_type = [&](PY_TYPE type) {
@@ -32,11 +33,11 @@ PY_TYPE check_list_element_type(const py::list& list) {
detected_type = type;
return;
}
- OPENVINO_THROW("Incorrect attribute. Mixed types in the list are not allowed.");
+ OPENVINO_THROW("Incorrect attribute. Mixed types in the container are not allowed.");
};
- for (const auto& it : list) {
- // Check the type of elements in the list
+ for (const auto& it : container) {
+ // Check the type of elements in the container
if (py::isinstance<py::str>(it)) {
check_type(PY_TYPE::STR);
} else if (py::isinstance<py::int_>(it)) {
@@ -47,6 +48,8 @@ PY_TYPE check_list_element_type(const py::list& list) {
check_type(PY_TYPE::BOOL);
} else if (py::isinstance<ov::PartialShape>(it)) {
check_type(PY_TYPE::PARTIAL_SHAPE);
+ } else if (py::isinstance<ov::hint::ModelDistributionPolicy>(it)) {
+ check_type(PY_TYPE::MODEL_DISTRIBUTION_POLICY);
}
}
@@ -449,7 +452,7 @@ ov::Any py_object_to_any(const py::object& py_obj) {
} else if (py::isinstance<py::list>(py_obj)) {
auto _list = py_obj.cast<py::list>();
- PY_TYPE detected_type = check_list_element_type(_list);
+ PY_TYPE detected_type = check_container_element_type<py::list>(_list);
if (_list.empty())
return ov::Any(EmptyList());
@@ -494,8 +497,28 @@ ov::Any py_object_to_any(const py::object& py_obj) {
return py::cast<ov::intel_auto::SchedulePolicy>(py_obj);
} else if (py::isinstance<ov::hint::SchedulingCoreType>(py_obj)) {
return py::cast<ov::hint::SchedulingCoreType>(py_obj);
- } else if (py::isinstance<std::set<ov::hint::ModelDistributionPolicy>>(py_obj)) {
- return py::cast<std::set<ov::hint::ModelDistributionPolicy>>(py_obj);
+ } else if (py::isinstance<py::set>(py_obj)) {
+ auto _set = py_obj.cast<py::set>();
+
+ PY_TYPE detected_type = check_container_element_type<py::set>(_set);
+
+ if (_set.empty())
+ return ov::Any(EmptyList());
+
+ switch (detected_type) {
+ case PY_TYPE::MODEL_DISTRIBUTION_POLICY:
+ return _set.cast<std::set<ov::hint::ModelDistributionPolicy>>();
+ case PY_TYPE::STR:
+ return _set.cast<std::set<std::string>>();
+ case PY_TYPE::FLOAT:
+ return _set.cast<std::set<double>>();
+ case PY_TYPE::INT:
+ return _set.cast<std::set<int64_t>>();
+ case PY_TYPE::BOOL:
+ return _set.cast<std::set<bool>>();
+ default:
+ OPENVINO_ASSERT(false, "Unsupported attribute type.");
+ }
} else if (py::isinstance<ov::hint::ExecutionMode>(py_obj)) {
return py::cast<ov::hint::ExecutionMode>(py_obj);
} else if (py::isinstance<ov::log::Level>(py_obj)) {
diff --git a/src/bindings/python/src/pyopenvino/utils/utils.hpp b/src/bindings/python/src/pyopenvino/utils/utils.hpp
index 80b92be43d2..fc1dbb646ea 100644
--- a/src/bindings/python/src/pyopenvino/utils/utils.hpp
+++ b/src/bindings/python/src/pyopenvino/utils/utils.hpp
@@ -110,11 +110,13 @@ protected:
};
- enum class PY_TYPE : int { UNKNOWN = 0, STR, INT, FLOAT, BOOL, PARTIAL_SHAPE };
+ enum class PY_TYPE : int { UNKNOWN = 0, STR, INT, FLOAT, BOOL, PARTIAL_SHAPE, MODEL_DISTRIBUTION_POLICY };
struct EmptyList {};
- PY_TYPE check_list_element_type(const py::list& list);
+ template <typename T>
+ // Checks element type of list and set
+ PY_TYPE check_container_element_type(const T& container);
py::object from_ov_any_no_leaves(const ov::Any& any);
diff --git a/src/bindings/python/tests/test_runtime/test_compiled_model.py b/src/bindings/python/tests/test_runtime/test_compiled_model.py
index d8eae65ddb1..bee43090ac9 100644
--- a/src/bindings/python/tests/test_runtime/test_compiled_model.py
+++ b/src/bindings/python/tests/test_runtime/test_compiled_model.py
@@ -21,6 +21,7 @@ from openvino import Model, Shape, Core, Tensor, serialize
from openvino import ConstOutput
import openvino.properties as props
+import openvino.properties.hint as hints
def test_get_property(device):
@@ -328,3 +329,16 @@ def test_memory_release(device):
# Release memory and perform inference again
compiled_model.release_memory()
request.infer({0: input_tensor, 1: input_tensor})
+
+
+def test_set_property_set_type():
+ model = get_relu_model([1, 3, 32, 32])
+ core = Core()
+ set_model = set()
+ set_model.add(hints.ModelDistributionPolicy.TENSOR_PARALLEL)
+ core.set_property("CPU", {hints.model_distribution_policy: set_model})
+ core.compile_model(model, "CPU")
+ out_set_model = core.get_property("CPU", hints.model_distribution_policy)
+ assert len(out_set_model) > 0
+ for item in out_set_model:
+ assert item == hints.ModelDistributionPolicy.TENSOR_PARALLEL