Skip to content

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

[PyOV]: Support direct comparison of PartialShape and Shape with list and tuple (#26261)

Hubert Błaszczykcacb820cfc61
diff --git a/src/bindings/python/src/pyopenvino/graph/partial_shape.cpp b/src/bindings/python/src/pyopenvino/graph/partial_shape.cpp
index 7774c9b6342..efa5df79108 100644
--- a/src/bindings/python/src/pyopenvino/graph/partial_shape.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/partial_shape.cpp
@@ -18,6 +18,17 @@
namespace py = pybind11;
+template <typename T>
+bool compare_shape(const ov::PartialShape& a, const T& b) {
+ if (a.is_dynamic()) {
+ throw py::type_error("Cannot compare dynamic shape with " + std::string(py::str(py::type::of(b))));
+ }
+ return a.size() == b.size() &&
+ std::equal(a.begin(), a.end(), b.begin(), [](const ov::Dimension& elem_a, const py::handle& elem_b) {
+ return elem_a == elem_b.cast<int64_t>();
+ });
+}
+
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";
@@ -179,6 +190,19 @@ void regclass_graph_PartialShape(py::module m) {
return a == b;
},
py::is_operator());
+ shape.def(
+ "__eq__",
+ [](const ov::PartialShape& a, const py::tuple& b) {
+ return compare_shape<py::tuple>(a, b);
+ },
+ py::is_operator());
+
+ shape.def(
+ "__eq__",
+ [](const ov::PartialShape& a, const py::list& b) {
+ return compare_shape<py::list>(a, b);
+ },
+ py::is_operator());
shape.def("__len__", [](const ov::PartialShape& self) {
return self.size();
diff --git a/src/bindings/python/src/pyopenvino/graph/shape.cpp b/src/bindings/python/src/pyopenvino/graph/shape.cpp
index f4951cda79b..ec5a5c77781 100644
--- a/src/bindings/python/src/pyopenvino/graph/shape.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/shape.cpp
@@ -17,6 +17,14 @@
namespace py = pybind11;
+template <typename T>
+bool compare_shape(const ov::Shape& a, const T& b) {
+ return a.size() == b.size() &&
+ std::equal(a.begin(), a.end(), b.begin(), [](const ov::Dimension& elem_a, const py::handle& elem_b) {
+ return elem_a == elem_b.cast<int64_t>();
+ });
+}
+
void regclass_graph_Shape(py::module m) {
py::class_<ov::Shape, std::shared_ptr<ov::Shape>> shape(m, "Shape");
shape.doc() = "openvino.runtime.Shape wraps ov::Shape";
@@ -31,6 +39,18 @@ void regclass_graph_Shape(py::module m) {
return a == b;
},
py::is_operator());
+ shape.def(
+ "__eq__",
+ [](const ov::Shape& a, const py::tuple& b) {
+ return compare_shape<py::tuple>(a, b);
+ },
+ py::is_operator());
+ shape.def(
+ "__eq__",
+ [](const ov::Shape& a, const py::list& b) {
+ return compare_shape<py::list>(a, b);
+ },
+ py::is_operator());
shape.def("__len__", [](const ov::Shape& v) {
return v.size();
});
diff --git a/src/bindings/python/tests/test_graph/test_core.py b/src/bindings/python/tests/test_graph/test_core.py
index c1da69f835f..003df8c56aa 100644
--- a/src/bindings/python/tests/test_graph/test_core.py
+++ b/src/bindings/python/tests/test_graph/test_core.py
@@ -319,6 +319,12 @@ def test_partial_shape_refinement():
assert not ps2.relaxes(ps1)
[email protected]("shape_to_compare", [[1, 2, 3], (1, 2, 3)])
+def test_shape_equals(shape_to_compare):
+ shape = Shape([1, 2, 3])
+ assert shape == shape_to_compare
+
+
def test_partial_shape_equals():
ps1 = PartialShape.dynamic()
ps2 = PartialShape.dynamic()
@@ -337,6 +343,56 @@ def test_partial_shape_equals():
ps2 = PartialShape.dynamic(rank=3)
assert ps1 == ps2
+ ps = PartialShape([1, 2, 3])
+ tuple_ps = (1, 2, 3)
+ list_ps = [1, 2, 3]
+ assert ps == tuple_ps
+ assert ps == list_ps
+
+ ps = PartialShape.dynamic(rank=3)
+ tuple_ps = (0, 0, 0)
+ list_ps = [0, 0, 0]
+ assert ps.get_min_shape() == tuple_ps
+ assert ps.get_min_shape() == list_ps
+
+ ps = PartialShape.dynamic()
+ tuple_ps = ()
+ list_ps = []
+ assert ps.get_min_shape() == tuple_ps
+ assert ps.get_min_shape() == list_ps
+
+ ps = PartialShape([Dimension(1), Dimension(2), Dimension(3), Dimension.dynamic()])
+ tuple_ps = (1, 2, 3, 0)
+ list_ps = [1, 2, 3, 0]
+ assert ps.get_min_shape() == tuple_ps
+ assert ps.get_min_shape() == list_ps
+
+ ps = PartialShape([Dimension(1, 10), Dimension(2), Dimension(3)])
+ tuple_ps_min = (1, 2, 3)
+ tuple_ps_max = (10, 2, 3)
+ list_ps_min = [1, 2, 3]
+ list_ps_max = [10, 2, 3]
+ assert ps.get_min_shape() == tuple_ps_min
+ assert ps.get_max_shape() == tuple_ps_max
+ assert ps.get_min_shape() == list_ps_min
+ assert ps.get_max_shape() == list_ps_max
+
+ with pytest.raises(TypeError) as e:
+ ps = PartialShape.dynamic()
+ tuple_ps = ()
+ assert ps == tuple_ps
+ assert (
+ "Cannot compare dynamic shape with <class 'tuple'>" in str(e.value)
+ )
+
+ with pytest.raises(TypeError) as e:
+ ps = PartialShape.dynamic()
+ list_ps = []
+ assert ps == list_ps
+ assert (
+ "Cannot compare dynamic shape with <class 'list'>" in str(e.value)
+ )
+
def test_input_shape_read_only():
shape = Shape([1, 10])