Skip to content

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

[PyOV] Consts ops from Tensor (#26812)

Anastasia Kuporosova0eaddb9d465e
diff --git a/src/bindings/python/src/openvino/runtime/opset13/ops.py b/src/bindings/python/src/openvino/runtime/opset13/ops.py
index ecd16fac797..cb201d3d426 100644
--- a/src/bindings/python/src/openvino/runtime/opset13/ops.py
+++ b/src/bindings/python/src/openvino/runtime/opset13/ops.py
@@ -3,7 +3,7 @@
# SPDX-License-Identifier: Apache-2.0
"""Factory functions for ops added to openvino opset13."""
-from functools import partial
+from functools import partial, singledispatch
from typing import Literal, Optional, Union
import logging
@@ -11,7 +11,7 @@ import numpy as np
log = logging.getLogger(__name__)
-from openvino.runtime import Node, Shape, Type, Output
+from openvino.runtime import Node, Shape, Type, Output, Tensor
from openvino.runtime.op import Constant, Result
from openvino.runtime.opset1 import convert_like
from openvino.runtime.opset_utils import _get_node_factory
@@ -271,6 +271,7 @@ def scaled_dot_product_attention(
return _get_node_factory_opset13().create("ScaledDotProductAttention", inputs, attributes)
+@singledispatch
@nameable_op
def constant(
value: Union[NumericData, np.number, bool, np.bool_, list],
@@ -338,6 +339,16 @@ def constant(
return Constant(_value, shared_memory=_shared_memory)
+@nameable_op
+def _(
+ tensor: Tensor,
+ shared_memory: bool = False,
+ name: Optional[str] = None,
+) -> Constant:
+ return Constant(tensor, shared_memory=shared_memory)
+
+
@unary_op
def result(data: Union[Node, Output, NumericData], name: Optional[str] = None) -> Node:
"""Return a node which represents an output of a graph (Model).
diff --git a/src/bindings/python/tests/test_graph/test_constant.py b/src/bindings/python/tests/test_graph/test_constant.py
index b1c1a6f9668..e28a4ad0551 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, compile_model
+from openvino import Type, PartialShape, Model, Tensor, compile_model
from openvino.runtime.op import Constant
from openvino.helpers import pack_data, unpack_data
@@ -854,3 +854,28 @@ def test_get_data_casting_packed(src_dtype, ov_type, dst_dtype, copy_flag):
else:
assert arr.flags["OWNDATA"] is True
assert np.array_equal(arr, data)
+
+
+ ("shared_flag"),
+ [
+ (True),
+ (False),
+ ],
+)
+def test_const_from_tensor(shared_flag):
+ shape = [1, 3, 32, 32]
+ arr = np.ones(shape).astype(np.float32)
+ ov_tensor = Tensor(arr, shape, Type.f32)
+ ov_const = ops.constant(ov_tensor, shared_memory=shared_flag)
+
+ assert isinstance(ov_const, Constant)
+ assert np.all(list(ov_const.shape) == shape)
+ arr += 1
+
+ if shared_flag is True:
+ assert np.array_equal(ov_const.data, arr)
+ assert np.shares_memory(arr, ov_const.data)
+ else:
+ assert not np.array_equal(ov_const.data, arr)
+ assert not np.shares_memory(arr, ov_const.data)