Skip to content

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

[Py OV] Add ov.experimental set_tensor_type/set_element_type (#26178)

Alicja Miloszewska345115eeb7be
diff --git a/src/bindings/python/src/openvino/experimental/__init__.py b/src/bindings/python/src/openvino/experimental/__init__.py
index ed445a816eb..6376fafd958 100644
--- a/src/bindings/python/src/openvino/experimental/__init__.py
+++ b/src/bindings/python/src/openvino/experimental/__init__.py
@@ -10,3 +10,5 @@ This module provides access to experimental functionality that is subject to cha
from openvino._pyopenvino.experimental import evaluate_as_partial_shape
from openvino._pyopenvino.experimental import evaluate_both_bounds
+from openvino._pyopenvino.experimental import set_element_type
+from openvino._pyopenvino.experimental import set_tensor_type
diff --git a/src/bindings/python/src/pyopenvino/experimental/experimental.cpp b/src/bindings/python/src/pyopenvino/experimental/experimental.cpp
index e44f945f9e2..7c695fefecf 100644
--- a/src/bindings/python/src/pyopenvino/experimental/experimental.cpp
+++ b/src/bindings/python/src/pyopenvino/experimental/experimental.cpp
@@ -5,6 +5,7 @@
#include "pyopenvino/experimental/experimental.hpp"
#include "openvino/core/bound_evaluation_util.hpp"
+#include "openvino/core/descriptor_tensor.hpp"
#include "openvino/core/validation_util.hpp"
namespace py = pybind11;
@@ -40,4 +41,31 @@ void regmodule_experimental(py::module m) {
:return: Tensors representing the lower and upper bound value estimations.
:rtype: Tuple[openvino.Tensor, openvino.Tensor]
)");
+ m_dev.def("set_element_type",
+ &ov::descriptor::set_element_type,
+ py::arg("tensor"),
+ py::arg("element_type"),
+ R"(
+ Sets element type for a tensor descriptor in the OV model graph.
+
+ :param tensor: The tensor descriptor whose element type is to be set.
+ :type tensor: openvino.Tensor
+ :param element_type: A new element type of the tensor descriptor.
+ :type element_type: openvino.Type
+ )");
+ m_dev.def("set_tensor_type",
+ &ov::descriptor::set_tensor_type,
+ py::arg("tensor"),
+ py::arg("element_type"),
+ py::arg("partial_shape"),
+ R"(
+ Changes element type and partial shape of a tensor descriptor in the OV model graph.
+
+ :param tensor: The tensor descriptor whose element type is to be set.
+ :type tensor: openvino.Tensor
+ :param element_type: A new element type of the tensor descriptor.
+ :type element_type: openvino.Type
+ :param partial_shape: A new partial shape of the tensor desriptor.
+ :type partial_shape: openvino.PartialShape
+ )");
}
diff --git a/src/bindings/python/tests/test_runtime/test_experimental.py b/src/bindings/python/tests/test_runtime/test_experimental.py
index 66144e0b0f5..ddda7c7b37d 100644
--- a/src/bindings/python/tests/test_runtime/test_experimental.py
+++ b/src/bindings/python/tests/test_runtime/test_experimental.py
@@ -2,7 +2,7 @@
# Copyright (C) 2018-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
-from openvino.experimental import evaluate_as_partial_shape, evaluate_both_bounds
+from openvino.experimental import evaluate_as_partial_shape, evaluate_both_bounds, set_element_type, set_tensor_type
import pytest
from openvino.runtime import Shape, PartialShape, Dimension, Type
@@ -51,3 +51,20 @@ def test_evaluate_as_partial_shape(graph_with_partial_value):
node = ops.abs(graph_with_partial_value)
output_value = PartialShape([])
assert evaluate_as_partial_shape(node.output(0), output_value)
+
+
+def test_tensor_descriptor():
+ data1 = np.array([[1, 2, 3], [1, 2, 3]], dtype=np.int64)
+ data2 = np.array([3, 2, 1], dtype=np.int64)
+
+ node = ops.add(data1, data2)
+ input_tensor1 = node.get_input_tensor(0)
+
+ assert input_tensor1.get_element_type() == Type.i64
+ set_element_type(input_tensor1, Type.f64)
+ assert input_tensor1.get_element_type() == Type.f64
+
+ partial_shape = PartialShape([-1, 6])
+ set_tensor_type(input_tensor1, Type.i64, partial_shape)
+ assert input_tensor1.get_partial_shape() == partial_shape
+ assert input_tensor1.get_element_type() == Type.i64
diff --git a/src/core/dev_api/openvino/core/descriptor_tensor.hpp b/src/core/dev_api/openvino/core/descriptor_tensor.hpp
index 6643c0814f9..4c7c0ad8c3c 100644
--- a/src/core/dev_api/openvino/core/descriptor_tensor.hpp
+++ b/src/core/dev_api/openvino/core/descriptor_tensor.hpp
@@ -8,9 +8,11 @@ namespace ov {
namespace descriptor {
// To change Tensor element type please change the Parameter type.
+OPENVINO_API
void set_element_type(Tensor& tensor, const element::Type& elemenet_type);
// To change Tensor type please change the Parameter type.
+OPENVINO_API
void set_tensor_type(Tensor& tensor, const element::Type& element_type, const PartialShape& pshape);
OPENVINO_DEPRECATED("get_ov_tensor_legacy_name() is deprecated. Please don't use this function.")
diff --git a/src/core/include/openvino/core/descriptor/tensor.hpp b/src/core/include/openvino/core/descriptor/tensor.hpp
index 25eeea950f8..edf2e690e8c 100644
--- a/src/core/include/openvino/core/descriptor/tensor.hpp
+++ b/src/core/include/openvino/core/descriptor/tensor.hpp
@@ -106,8 +106,10 @@ protected:
friend OPENVINO_API std::string get_ov_tensor_legacy_name(const Tensor& tensor);
friend OPENVINO_API void set_ov_tensor_legacy_name(Tensor& tensor, const std::string& tensor_name);
- friend void set_element_type(Tensor& tensor, const element::Type& elemenet_type);
- friend void set_tensor_type(Tensor& tensor, const element::Type& element_type, const PartialShape& pshape);
+ friend OPENVINO_API void set_element_type(Tensor& tensor, const element::Type& elemenet_type);
+ friend OPENVINO_API void set_tensor_type(Tensor& tensor,
+ const element::Type& element_type,
+ const PartialShape& pshape);
friend class pass::ReverseShapeAndTypeInfer;
private: