OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[PyOV] Accept a type name string for read_value variable_type (#39174)
Somay kousisf9d408abdaee
diff --git a/src/bindings/python/src/openvino/opset6/ops.py b/src/bindings/python/src/openvino/opset6/ops.pyindex 363973c17af..a9a67a74699 100644--- a/src/bindings/python/src/openvino/opset6/ops.py+++ b/src/bindings/python/src/openvino/opset6/ops.py@@ -10,6 +10,7 @@ from typing import Optional, Unionfrom functools import partial, singledispatchfrom openvino import Node, Type, PartialShape, Output, Shape+from openvino.exceptions import OVTypeErrorfrom openvino.op import assign, Constant, Parameterfrom openvino.op import read_value as _read_valuefrom openvino.op.util import VariableInfo, Variable@@ -127,7 +128,7 @@ def read_value(init_value: Union[Node, Output, int, float, np.ndarray],:param init_value: Node producing a value to be returned instead of an unassigned variable.:param variable_id: Id of a variable to be read.- :param variable_type: Optional type to be set into Variable.+ :param variable_type: Optional type to be set into Variable, e.g. Type.f32, np.float32 or "f32".:param variable_shape: Optional shape to be set into Variable.:param name: Optional name for output node.:return: ReadValue node@@ -136,7 +137,12 @@ def read_value(init_value: Union[Node, Output, int, float, np.ndarray],info.variable_id = variable_idif variable_type is not None:- if not isinstance(variable_type, Type) and not isinstance(variable_type, str):+ if isinstance(variable_type, str):+ element_type = getattr(Type, variable_type, None)+ if not isinstance(element_type, Type):+ raise OVTypeError(f"Unidentified data type {variable_type}")+ info.data_type = element_type+ elif not isinstance(variable_type, Type):info.data_type = get_element_type(variable_type)else:info.data_type = variable_type@@ -168,7 +174,7 @@ def read_value( # noqa: F811"""Return a node which produces the Assign operation.:param variable_id: Id of a variable to be read.- :param variable_type: Optional type to be set into Variable.+ :param variable_type: Optional type to be set into Variable, e.g. Type.f32, np.float32 or "f32".:param variable_shape: Optional shape to be set into Variable.:param name: Optional name for output node.:return: ReadValue node@@ -177,7 +183,12 @@ def read_value( # noqa: F811info.variable_id = variable_idif variable_type is not None:- if not isinstance(variable_type, Type) and not isinstance(variable_type, str):+ if isinstance(variable_type, str):+ element_type = getattr(Type, variable_type, None)+ if not isinstance(element_type, Type):+ raise OVTypeError(f"Unidentified data type {variable_type}")+ info.data_type = element_type+ elif not isinstance(variable_type, Type):info.data_type = get_element_type(variable_type)else:info.data_type = variable_typediff --git a/src/bindings/python/tests/test_graph/test_create_op.py b/src/bindings/python/tests/test_graph/test_create_op.pyindex 0e5672a9c14..dd79a92e4e4 100644--- a/src/bindings/python/tests/test_graph/test_create_op.py+++ b/src/bindings/python/tests/test_graph/test_create_op.py@@ -6,7 +6,7 @@ import numpy as npimport pytestfrom openvino import PartialShape, Dimension, Model, Type-from openvino.exceptions import UserInputError+from openvino.exceptions import OVTypeError, UserInputErrorfrom openvino.utils.types import make_constant_nodeimport openvino.opset1 as ov_opset1@@ -1010,6 +1010,33 @@ def test_read_value():assert read_value_attributes["variable_shape"] == [2, 2][email protected](("variable_type", "expected_type"), [+ ("i32", Type.i32),+ ("f32", Type.f32),+ ("boolean", Type.boolean),+])+def test_read_value_with_type_name(variable_type, expected_type):+ init_value = ov.parameter([2, 2], name="init_value", dtype=expected_type)++ node = ov.read_value(init_value, "var_id_667", variable_type, [2, 2])+ assert node.get_output_element_type(0) == expected_type+ assert node.get_attributes()["variable_type"] == variable_type++ node = ov.read_value("var_id_668", variable_type, [2, 2])+ assert node.get_output_element_type(0) == expected_type+ assert node.get_attributes()["variable_type"] == variable_type++[email protected]("variable_type", ["nonsense", "real", "to_string"])+def test_read_value_with_unknown_type_name(variable_type):+ init_value = ov.parameter([2, 2], name="init_value", dtype=np.float32)++ with pytest.raises(OVTypeError, match=f"Unidentified data type {variable_type}"):+ ov.read_value(init_value, "var_id_667", variable_type, [2, 2])+ with pytest.raises(OVTypeError, match=f"Unidentified data type {variable_type}"):+ ov.read_value("var_id_668", variable_type, [2, 2])++def test_read_value_ctors():data = np.ones((1, 64), dtype=np.float32)# check mixed args&kwargs creation