From 3e42ddbde5e3a8d2c149bc40bb4bb2711f5d6208 Mon Sep 17 00:00:00 2001 From: Anastasia Kuporosova Date: Tue, 12 Dec 2023 18:21:47 +0100 Subject: [PATCH] [PyOV] support Type as arg in convert op (#21603) * [PyOV] support Type as arg in convert op * add test * fix linter --- .../python/src/openvino/runtime/opset1/ops.py | 13 ++++++++----- src/bindings/python/tests/test_graph/test_basic.py | 2 ++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/bindings/python/src/openvino/runtime/opset1/ops.py b/src/bindings/python/src/openvino/runtime/opset1/ops.py index 4f3c9629534..b5e8b63ec21 100644 --- a/src/bindings/python/src/openvino/runtime/opset1/ops.py +++ b/src/bindings/python/src/openvino/runtime/opset1/ops.py @@ -3,7 +3,7 @@ # SPDX-License-Identifier: Apache-2.0 """Factory functions for all openvino ops.""" -from typing import List, Optional, Union +from typing import List, Optional, Union, get_args import numpy as np from functools import partial @@ -352,7 +352,7 @@ def constant( @nameable_op def convert( data: NodeInput, - destination_type: Union[str, NumericType], + destination_type: Union[str, NumericType, Type], name: Optional[str] = None, ) -> Node: """Return node which casts input node values to specified type. @@ -362,12 +362,15 @@ def convert( :param name: Optional name for the output node. :return: New node performing the conversion operation. """ - if not isinstance(destination_type, str): - destination_type = get_element_type_str(destination_type) + _destination_type = None # type: Union[str, Type] + if isinstance(destination_type, get_args(NumericType)): + _destination_type = get_element_type_str(destination_type).lower() + else: + _destination_type = destination_type return _get_node_factory_opset1().create( "Convert", [as_node(data)], - {"destination_type": destination_type.lower()}, + {"destination_type": _destination_type}, ) diff --git a/src/bindings/python/tests/test_graph/test_basic.py b/src/bindings/python/tests/test_graph/test_basic.py index 45723c6db7b..259a8c1d73b 100644 --- a/src/bindings/python/tests/test_graph/test_basic.py +++ b/src/bindings/python/tests/test_graph/test_basic.py @@ -144,6 +144,8 @@ def test_convert_to_bool(destination_type, input_data): pytest.param(np.float64, (-16383, 16383), np.int64, np.float64), pytest.param("f32", (-8, 8), np.int32, np.float32), pytest.param("f64", (-16383, 16383), np.int64, np.float64), + pytest.param(Type.f32, (-8, 8), np.int32, np.float32), + pytest.param(Type.f64, (-16383, 16383), np.int64, np.float64), ], ) def test_convert_to_float(destination_type, rand_range, in_dtype, expected_type):