Skip to content

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

[PyOV] new methods for Constant (#27732)

Anastasia Kuporosova09927d8dd89d
diff --git a/src/bindings/python/src/pyopenvino/graph/ops/constant.cpp b/src/bindings/python/src/pyopenvino/graph/ops/constant.cpp
index d5b7e5878e0..20361b1fc5c 100644
--- a/src/bindings/python/src/pyopenvino/graph/ops/constant.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/ops/constant.cpp
@@ -121,6 +121,22 @@ void regclass_graph_op_Constant(py::module m) {
}
});
+ constant.def("get_tensor_view",
+ &ov::op::v0::Constant::get_tensor_view,
+ R"(
+ Get view on constant data as tensor.
+
+ :rtype: openvino.Tensor
+ )");
+
+ constant.def("get_strides",
+ &ov::op::v0::Constant::get_strides,
+ R"(
+ Constant's strides in bytes.
+
+ :rtype: openvino.Strides
+ )");
+
// TODO: Remove in future and re-use `get_data`
// Provide buffer access
constant.def_buffer([](const ov::op::v0::Constant& self) -> py::buffer_info {
@@ -236,6 +252,22 @@ void regclass_graph_op_Constant(py::module m) {
:rtype: numpy.array
)");
+ constant.def_property_readonly("tensor_view",
+ &ov::op::v0::Constant::get_tensor_view,
+ R"(
+ Get view on constant data as tensor.
+
+ :rtype: openvino.Tensor
+ )");
+
+ constant.def_property_readonly("strides",
+ &ov::op::v0::Constant::get_strides,
+ R"(
+ Constant's strides in bytes.
+
+ :rtype: openvino.Strides
+ )");
+
constant.def("__repr__", [](const ov::op::v0::Constant& self) {
std::stringstream shapes_ss;
for (size_t i = 0; i < self.get_output_size(); ++i) {
diff --git a/src/bindings/python/src/pyopenvino/graph/strides.cpp b/src/bindings/python/src/pyopenvino/graph/strides.cpp
index daecb6e9e3a..951f4c3e687 100644
--- a/src/bindings/python/src/pyopenvino/graph/strides.cpp
+++ b/src/bindings/python/src/pyopenvino/graph/strides.cpp
@@ -16,6 +16,14 @@
namespace py = pybind11;
+template <typename T>
+bool compare_strides(const ov::Strides& a, const T& b) {
+ return a.size() == b.size() &&
+ std::equal(a.begin(), a.end(), b.begin(), [](const size_t& elem_a, const py::handle& elem_b) {
+ return elem_a == elem_b.cast<size_t>();
+ });
+}
+
void regclass_graph_Strides(py::module m) {
py::class_<ov::Strides, std::shared_ptr<ov::Strides>> strides(m, "Strides");
strides.doc() = "openvino.runtime.Strides wraps ov::Strides";
@@ -48,6 +56,27 @@ void regclass_graph_Strides(py::module m) {
return self.size();
});
+ strides.def(
+ "__eq__",
+ [](const ov::Strides& a, const ov::Strides& b) {
+ return a == b;
+ },
+ py::is_operator());
+
+ strides.def(
+ "__eq__",
+ [](const ov::Strides& a, const py::tuple& b) {
+ return compare_strides<py::tuple>(a, b);
+ },
+ py::is_operator());
+
+ strides.def(
+ "__eq__",
+ [](const ov::Strides& a, const py::list& b) {
+ return compare_strides<py::list>(a, b);
+ },
+ py::is_operator());
+
strides.def(
"__iter__",
[](const ov::Strides& self) {
diff --git a/src/bindings/python/tests/test_graph/test_constant.py b/src/bindings/python/tests/test_graph/test_constant.py
index 7b349ad7cd9..a8b4dfa3e1a 100644
--- a/src/bindings/python/tests/test_graph/test_constant.py
+++ b/src/bindings/python/tests/test_graph/test_constant.py
@@ -6,7 +6,7 @@ import numpy as np
import openvino as ov
import openvino.runtime.opset13 as ops
-from openvino import Type, PartialShape, Model, Tensor, compile_model
+from openvino import Type, PartialShape, Model, Strides, Tensor, compile_model
from openvino.runtime.op import Constant
from openvino.helpers import pack_data, unpack_data
@@ -756,7 +756,7 @@ def test_get_data_casting_packed(src_dtype, ov_type, dst_dtype, copy_flag):
],
)
def test_const_from_tensor(shared_flag):
- shape = [1, 3, 32, 32]
+ shape = [1, 2, 3, 3]
arr = np.ones(shape).astype(np.float32)
ov_tensor = Tensor(arr, shape, Type.f32)
ov_const = ops.constant(tensor=ov_tensor, shared_memory=shared_flag)
@@ -771,3 +771,6 @@ def test_const_from_tensor(shared_flag):
else:
assert not np.array_equal(ov_const.data, arr)
assert not np.shares_memory(arr, ov_const.data)
+
+ assert ov_const.strides == [72, 36, 12, 4]
+ assert ov_const.get_tensor_view().get_strides() == Strides([72, 36, 12, 4])