diff --git a/tests/layer_tests/ovc_python_api_tests/test_paddle.py b/tests/layer_tests/ovc_python_api_tests/test_paddle.py index a91e3f9e723..80b8efefea3 100644 --- a/tests/layer_tests/ovc_python_api_tests/test_paddle.py +++ b/tests/layer_tests/ovc_python_api_tests/test_paddle.py @@ -101,3 +101,35 @@ class TestMoConvertPaddle(CommonMOConvertTest): if mo_params is not None: test_params.update(mo_params) self._test_by_ref_graph(temp_dir, test_params, graph_ref, compare_tensor_names=False) + + +class TestPaddleConversionParams(CommonMOConvertTest): + paddle_is_imported = False + try: + import paddle + paddle_is_imported = True + except ImportError: + pass + + test_data = [ + {'params_test': {'input': paddle.shape(paddle.to_tensor(np.random.rand(2, 3, 4)))}, + 'fw_model': make_pd_hapi_graph_model([1, 2]), + 'ref_model': make_ref_graph_model([2, 3, 4])}, + {'params_test': {'input': paddle.to_tensor(np.random.rand(5, 6)).shape}, + 'fw_model': make_pd_hapi_graph_model([1, 2, 3]), + 'ref_model': make_ref_graph_model([5, 6])}, + {'params_test': {'input': (paddle.to_tensor(np.random.rand(4, 2, 7)).shape, paddle.int32)}, + 'fw_model': make_pd_hapi_graph_model([2, 3]), + 'ref_model': make_ref_graph_model([4, 2, 7], np.int32)}, + ] if paddle_is_imported else [] + + @pytest.mark.parametrize("params", test_data) + @pytest.mark.nightly + def test_conversion_params(self, params, ie_device, precision, ir_version, + temp_dir, use_new_frontend, use_old_api): + fw_model = params['fw_model'] + test_params = params['params_test'] + ref_model = params['ref_model'] + + test_params.update({'input_model': fw_model}) + self._test_by_ref_graph(temp_dir, test_params, ref_model, compare_tensor_names=False) diff --git a/tests/layer_tests/ovc_python_api_tests/test_pytorch.py b/tests/layer_tests/ovc_python_api_tests/test_pytorch.py index 52d66690459..5ae2dcac31c 100644 --- a/tests/layer_tests/ovc_python_api_tests/test_pytorch.py +++ b/tests/layer_tests/ovc_python_api_tests/test_pytorch.py @@ -1145,3 +1145,75 @@ class ConvertRaises(unittest.TestCase): with self.assertRaisesRegex(Exception, ".*Cannot recognize input model.*"): with tempfile.NamedTemporaryFile() as tmpfile: convert_model(tmpfile.name) + + +def create_model_three_inputs(): + from torch import nn + + class NeuralNetwork(nn.Module): + def __init__(self): + super(NeuralNetwork, self).__init__() + self.linear_relu_stack = nn.Sequential( + nn.ReLU(), + nn.Sigmoid(), + ) + + def forward(self, x, y, z): + out = self.linear_relu_stack(x + y + z), + return out + return NeuralNetwork() + + +def make_ref_model_three_inputs(shape, dtype=np.float32): + x = ov.opset8.parameter(PartialShape( + shape), name="x", dtype=dtype) + y = ov.opset8.parameter(PartialShape( + shape), name="y", dtype=dtype) + z = ov.opset8.parameter(PartialShape( + shape), name="z", dtype=dtype) + add1 = ov.opset8.add(x, y) + add2 = ov.opset8.add(add1, z) + + relu = ov.opset8.relu(add2) + + if dtype not in [np.float32, Type.dynamic]: + relu = ov.opset8.convert(relu, np.float32) + + sigm = ov.opset8.sigmoid(relu) + + parameter_list = [x, y, z] + model = Model([sigm], parameter_list, "test") + return model + + +class TestPytorchConversionParams(CommonMOConvertTest): + + test_data = [ + {'params_test': {'input': [(torch.Size([2, 3, 4]), torch.float32), + (torch.empty(2, 3, 4).size(), torch.float32), + (torch.empty(2, 3, 4).shape, torch.float32)]}, + 'fw_model': create_model_three_inputs(), + 'ref_model': make_ref_model_three_inputs([2,3,4], np.float32)}, + {'params_test': {'input': [(torch.Size([5, 2]), torch.int32), + (torch.empty(5, 2).size(), torch.int32), + (torch.empty(5, 2).shape, torch.int32)]}, + 'fw_model': create_model_three_inputs(), + 'ref_model': make_ref_model_three_inputs([5, 2], np.int32)}, + {'params_test': {'input': [(torch.Size([1, 3, 5]), torch.float32)]}, + 'fw_model': make_pt_model_one_input(), + 'ref_model': make_ref_pt_model_one_input([1, 3, 5], np.float32)}, + {'params_test': {'input': [(torch.empty(7, 3).size(), torch.int32)]}, + 'fw_model': make_pt_model_one_input(), + 'ref_model': make_ref_pt_model_one_input([7, 3], np.int32)}, + ] + + @pytest.mark.parametrize("params", test_data) + @pytest.mark.nightly + def test_conversion_params(self, params, ie_device, precision, ir_version, + temp_dir, use_new_frontend, use_old_api): + fw_model = params['fw_model'] + test_params = params['params_test'] + ref_model = params['ref_model'] + + test_params.update({'input_model': fw_model}) + self._test_by_ref_graph(temp_dir, test_params, ref_model, compare_tensor_names=False) diff --git a/tests/layer_tests/ovc_python_api_tests/test_tf.py b/tests/layer_tests/ovc_python_api_tests/test_tf.py index e8c098ff9ca..b894ec7153e 100644 --- a/tests/layer_tests/ovc_python_api_tests/test_tf.py +++ b/tests/layer_tests/ovc_python_api_tests/test_tf.py @@ -10,11 +10,10 @@ from openvino.runtime import PartialShape, Model, Dimension from common.mo_convert_test_class import CommonMOConvertTest from common.layer_test_class import CommonLayerTest +import tensorflow as tf def create_tf_graph_def(tmp_dir): - import tensorflow as tf - tf.compat.v1.reset_default_graph() with tf.compat.v1.Session() as sess: @@ -41,8 +40,6 @@ def create_tf_graph_def(tmp_dir): def create_keras_model(temp_dir): - import tensorflow as tf - tf.keras.backend.clear_session() tf.compat.v1.reset_default_graph() @@ -69,8 +66,6 @@ def create_keras_model(temp_dir): def create_tf1_wrap_function(tmp_dir): - import tensorflow as tf - def f(x, y): return tf.nn.sigmoid(tf.nn.relu(x + y)) @@ -91,7 +86,6 @@ def create_tf1_wrap_function(tmp_dir): def create_tf_session(tmp_dir): - import tensorflow as tf from tensorflow.python.eager.context import graph_mode with graph_mode(): @@ -119,8 +113,6 @@ def create_tf_session(tmp_dir): def create_tf_module(tmp_dir): - import tensorflow as tf - class Net(tf.Module): def __init__(self, name=None): super(Net, self).__init__(name=name) @@ -143,8 +135,6 @@ def create_tf_module(tmp_dir): def create_tf_module_dynamic(tmp_dir): - import tensorflow as tf - class Net(tf.Module): def __init__(self, name=None): super(Net, self).__init__(name=name) @@ -169,7 +159,6 @@ def create_tf_module_dynamic(tmp_dir): def create_keras_layer(tmp_dir): - import tensorflow as tf class LayerModel(tf.keras.layers.Layer): def __init__(self): @@ -193,7 +182,6 @@ def create_keras_layer(tmp_dir): def create_keras_layer_dynamic(tmp_dir): - import tensorflow as tf class LayerModel(tf.keras.layers.Layer): def __init__(self): @@ -219,8 +207,6 @@ def create_keras_layer_dynamic(tmp_dir): def create_tf_checkpoint(tmp_dir): - import tensorflow as tf - input_names = ["Input1", "Input2"] input_shape = [1, 2, 3] @@ -245,8 +231,6 @@ def create_tf_checkpoint(tmp_dir): def create_tf_function(temp_dir): - import tensorflow as tf - @tf.function( input_signature=[tf.TensorSpec(shape=[1, 2, 3], dtype=tf.float32), tf.TensorSpec(shape=[1, 2, 3], dtype=tf.float32)]) @@ -268,8 +252,6 @@ def create_tf_function(temp_dir): def create_tf_graph(temp_dir): - import tensorflow as tf - tf.compat.v1.reset_default_graph() with tf.compat.v1.Session() as sess: @@ -296,8 +278,6 @@ def create_tf_graph(temp_dir): def create_tf_saved_model_dir(temp_dir): - import tensorflow as tf - input_names = ["Input1", "Input2"] input_shape = [1, 2, 3] @@ -322,7 +302,6 @@ def create_tf_saved_model_dir(temp_dir): def create_tf_stateful_partioned_call_net(temp_dir): - import tensorflow as tf tf.compat.v1.reset_default_graph() data_shape = [1, 1, 10, 10] @@ -359,7 +338,6 @@ def create_tf_stateful_partioned_call_net(temp_dir): def create_keras_layer_input_list(): - import tensorflow as tf class LayerModel(tf.keras.layers.Layer): def __init__(self): @@ -386,7 +364,6 @@ def create_keras_layer_input_list(): def create_keras_layer_input_list_one_inp(): - import tensorflow as tf class LayerModel(tf.keras.layers.Layer): def __init__(self): @@ -408,7 +385,6 @@ def create_keras_layer_input_list_one_inp(): def create_keras_layer_input_dict(): - import tensorflow as tf class LayerModel(tf.keras.layers.Layer): def __init__(self): @@ -434,7 +410,6 @@ def create_keras_layer_input_dict(): def create_keras_layer_input_dict_one_inp(): - import tensorflow as tf class LayerModel(tf.keras.layers.Layer): def __init__(self): @@ -522,7 +497,6 @@ def create_keras_layer_with_input_shapes_case4(tmp_dir): def create_keras_layer_with_tf_function_call(tmp_dir): - import tensorflow as tf class LayerModel(tf.Module): def __init__(self): super(LayerModel, self).__init__() @@ -538,7 +512,6 @@ def create_keras_layer_with_tf_function_call(tmp_dir): def create_keras_layer_with_tf_function_call_default_compressed_to_fp16(tmp_dir): - import tensorflow as tf class LayerModel(tf.Module): def __init__(self): super(LayerModel, self).__init__() @@ -554,7 +527,6 @@ def create_keras_layer_with_tf_function_call_default_compressed_to_fp16(tmp_dir) def create_keras_layer_with_tf_function_call_no_signature(tmp_dir): - import tensorflow as tf class LayerModel(tf.Module): def __init__(self): super(LayerModel, self).__init__() @@ -572,7 +544,6 @@ def create_keras_layer_with_tf_function_call_no_signature(tmp_dir): def create_keras_layer_with_tf_function_call_no_signature_single_input(tmp_dir): - import tensorflow as tf class LayerModel(tf.Module): def __init__(self): super(LayerModel, self).__init__() @@ -590,7 +561,6 @@ def create_keras_layer_with_tf_function_call_no_signature_single_input(tmp_dir): def create_keras_layer_with_string_tensor(tmp_dir): - import tensorflow as tf class LayerModel(tf.Module): def __init__(self): super(LayerModel, self).__init__() @@ -612,6 +582,69 @@ def create_keras_layer_with_string_tensor(tmp_dir): return model, model_ref, {} +def create_tf_model_three_inputs(shape=[1, 2, 3, 4], type=tf.float32): + tf.compat.v1.reset_default_graph() + + with tf.compat.v1.Session() as sess: + inp1 = tf.compat.v1.placeholder(type, shape, 'Input1') + inp2 = tf.compat.v1.placeholder(type, shape, 'Input2') + inp3 = tf.compat.v1.placeholder(type, shape, 'Input3') + + relu1 = tf.nn.relu(inp1, name='Relu1') + relu2 = tf.nn.relu(inp2, name='Relu2') + relu3 = tf.nn.relu(inp3, name='Relu3') + + add = relu1 + relu2 + relu3 + + tf.compat.v1.global_variables_initializer() + tf_net = sess.graph + return tf_net + + +def create_ref_model_three_inputs(shape=[1, 2, 3, 4], dtype=np.float32): + inp1 = ov.opset8.parameter(PartialShape( + shape), name="Input1", dtype=dtype) + inp2 = ov.opset8.parameter(PartialShape( + shape), name="Input2", dtype=dtype) + inp3 = ov.opset8.parameter(PartialShape( + shape), name="Input3", dtype=dtype) + + relu1 = ov.opset8.relu(inp1) + relu2 = ov.opset8.relu(inp2) + relu3 = ov.opset8.relu(inp3) + + add1 = ov.opset8.add(relu1, relu2) + add2 = ov.opset8.add(add1, relu3) + + parameter_list = [inp1, inp2, inp3] + model = Model([add2], parameter_list, "test") + return model + + +def create_tf_model_single_input(shape=[1, 2, 3, 4], type=tf.float32): + tf.compat.v1.reset_default_graph() + + with tf.compat.v1.Session() as sess: + inp = tf.compat.v1.placeholder(type, shape, 'Input') + relu = tf.nn.relu(inp, name='Relu') + output = tf.nn.sigmoid(relu, name='Sigmoid') + + tf.compat.v1.global_variables_initializer() + tf_net = sess.graph + + return tf_net + + +def create_ref_model_single_input(shape=[1, 2, 3, 4], dtype=np.float32): + inp = ov.opset8.parameter(PartialShape( + shape), name="Input", dtype=dtype) + relu = ov.opset8.relu(inp) + sigm = ov.opset8.sigmoid(relu) + parameter_list = [inp] + model = Model([sigm], parameter_list, "test") + return model + + class TestMoConvertTF(CommonMOConvertTest): test_data = [ # TF2 @@ -667,7 +700,6 @@ class TestMoConvertTF(CommonMOConvertTest): self._test_by_ref_graph(temp_dir, test_params, graph_ref, compare_tensor_names=False) def test_zero_copy(self, ie_device, precision, ir_version, temp_dir): - import tensorflow as tf from openvino.tools.ovc import convert_model from openvino.runtime import compile_model class LayerModel(tf.Module): @@ -716,7 +748,6 @@ class TestMoConvertTF(CommonMOConvertTest): assert np.array_equal(ov_infer2['Identity:0'], [ 0., 8., 16.]) def test_turn_off_sharing(self, ie_device, precision, ir_version, temp_dir): - import tensorflow as tf from openvino.tools.ovc import convert_model from openvino.runtime import compile_model class LayerModel(tf.Module): @@ -767,7 +798,6 @@ class TestMoConvertTF(CommonMOConvertTest): def test_memory_loss(self, ie_device, precision, ir_version, temp_dir): # This test checks that the memory allocated for constants # is not lost after returning the model from convert_model() method. - import tensorflow as tf tf.compat.v1.reset_default_graph() from openvino.tools.ovc import convert_model @@ -820,7 +850,6 @@ class TestMoConvertTF(CommonMOConvertTest): assert CommonLayerTest().compare_ie_results_with_framework(ov_infer1, {"add:0": [2.6, 9.6, 12.4]}, eps) def test_scalar(self, ie_device, precision, ir_version, temp_dir): - import tensorflow as tf tf.compat.v1.reset_default_graph() from openvino.tools.ovc import convert_model @@ -861,7 +890,6 @@ class TestMoConvertTF(CommonMOConvertTest): assert CommonLayerTest().compare_ie_results_with_framework(ov_infer, {"Identity:0": 3.2}, eps) def test_unnamed_variable(self, ie_device, precision, ir_version, temp_dir): - import tensorflow as tf tf.compat.v1.reset_default_graph() from openvino.tools.ovc import convert_model @@ -905,7 +933,6 @@ class TFConvertTest(unittest.TestCase): @pytest.mark.nightly @pytest.mark.precommit def test_tf_function_no_signature(self): - import tensorflow as tf from openvino.tools.ovc import convert_model @tf.function() @@ -920,8 +947,6 @@ class TFConvertTest(unittest.TestCase): class TestTFLoadByModel(unittest.TestCase): def test_load_by_model_tf_graph_iterator(self): def simple_tf_model(): - import tensorflow as tf - tf.compat.v1.reset_default_graph() with tf.compat.v1.Session() as sess: @@ -955,3 +980,37 @@ class TestTFConvertRaises(unittest.TestCase): # check that it accepts specified names as is without parsing into 2 different inputs with self.assertRaisesRegex(Exception, 'No node with name Input1\[1, 2, 3\],Input2\[1, 2, 3\]'): convert_model(tf_model, input='Input1[1, 2, 3],Input2[1, 2, 3]') + + +class TestTFConversionParams(CommonMOConvertTest): + test_data = [ + {'params_test': {'input': [tf.shape(tf.zeros((2, 3, 4))), tf.zeros((2, 3, 4)).shape, tf.TensorShape((2, 3, 4))]}, + 'fw_model': create_tf_model_three_inputs([1, 2, 3, 4]), + 'ref_model': create_ref_model_three_inputs([2, 3, 4])}, + {'params_test': {'input': [tf.float32, tf.float32, tf.float32]}, + 'fw_model': create_tf_model_three_inputs([2, 3], tf.int32), + 'ref_model': create_ref_model_three_inputs([2, 3], np.float32)}, + {'params_test': {'input': tf.shape(tf.zeros((5, 8, 2)))}, + 'fw_model': create_tf_model_single_input(), + 'ref_model': create_ref_model_single_input([5, 8, 2])}, + {'params_test': {'input': tf.zeros((9, 2)).shape}, + 'fw_model': create_tf_model_single_input(), + 'ref_model': create_ref_model_single_input([9, 2])}, + {'params_test': {'input': tf.TensorShape((4, 8, 3))}, + 'fw_model': create_tf_model_single_input(), + 'ref_model': create_ref_model_single_input([4, 8, 3])}, + {'params_test': {'input': tf.int32}, + 'fw_model': create_tf_model_single_input(), + 'ref_model': create_ref_model_single_input([1, 2, 3, 4], np.int32)} + ] + + @pytest.mark.parametrize("params", test_data) + @pytest.mark.nightly + def test_mo_convert_tf_model(self, params, ie_device, precision, ir_version, + temp_dir, use_new_frontend, use_old_api): + fw_model = params['fw_model'] + test_params = params['params_test'] + ref_model = params['ref_model'] + + test_params.update({'input_model': fw_model}) + self._test_by_ref_graph(temp_dir, test_params, ref_model, compare_tensor_names=False) \ No newline at end of file diff --git a/tools/ovc/openvino/tools/ovc/cli_parser.py b/tools/ovc/openvino/tools/ovc/cli_parser.py index 7bf03f5f548..87fc0225206 100644 --- a/tools/ovc/openvino/tools/ovc/cli_parser.py +++ b/tools/ovc/openvino/tools/ovc/cli_parser.py @@ -14,22 +14,13 @@ from openvino.runtime import PartialShape, Dimension, Shape, Type # pylint: dis import openvino from openvino.tools.ovc.error import Error from openvino.tools.ovc.help import get_convert_model_help_specifics +from openvino.tools.ovc.moc_frontend.shape_utils import to_partial_shape, is_shape_type +from openvino.tools.ovc.moc_frontend.type_utils import to_ov_type, is_type from openvino.tools.ovc.utils import get_mo_root_dir # Helper class for storing input cut information _InputCutInfo = namedtuple("InputCutInfo", ["name", "shape", "type", "value"], defaults=[None, None, None, None]) -def is_shape_type(value): - if isinstance(value, PartialShape): - return True - if isinstance(value, Shape): - return True - if isinstance(value, list) or isinstance(value, tuple): - for dim in value: - if not (isinstance(dim, Dimension) or isinstance(dim, int)): - return False - return True - return False def single_input_to_input_cut_info(input: [str, tuple, list, PartialShape, Type, type]): """ @@ -43,7 +34,7 @@ def single_input_to_input_cut_info(input: [str, tuple, list, PartialShape, Type, if isinstance(input, (tuple, list)) or is_shape_type(input): # If input represents list with shape, wrap it to list. Single PartialShape also goes to this condition. # Check of all dimensions will be in is_shape_type(val) method below - if len(input) > 0 and isinstance(input[0], (int, Dimension)) or isinstance(input, PartialShape): + if is_shape_type(input): input = [input] # Check values of tuple or list and collect to InputCutInfo @@ -55,14 +46,14 @@ def single_input_to_input_cut_info(input: [str, tuple, list, PartialShape, Type, if name is not None: raise Exception("More than one input name provided: {}".format(input)) name = val - elif isinstance(val, (type, Type)): + elif is_type(val): if inp_type is not None: raise Exception("More than one input type provided: {}".format(input)) - inp_type = val + inp_type = to_ov_type(val) elif is_shape_type(val) or val is None: if shape is not None: raise Exception("More than one input shape provided: {}".format(input)) - shape = PartialShape(val) if val is not None else None + shape = to_partial_shape(val) if val is not None else None else: raise Exception("Incorrect input parameters provided. Expected tuple with input name, " "input type or input shape. Got unknown object: {}".format(val)) @@ -72,14 +63,16 @@ def single_input_to_input_cut_info(input: [str, tuple, list, PartialShape, Type, inp_type, None) # Case when only type is set - if isinstance(input, (type, Type)): - return _InputCutInfo(None, None, input, None) # pylint: disable=no-member + if is_type(input): + return _InputCutInfo(None, None, to_ov_type(input), None) # pylint: disable=no-member # We don't expect here single unnamed value. If list of int is set it is considered as shape. # Setting of value is expected only using InputCutInfo or string analog. raise Exception("Unexpected object provided for input. Expected tuple, Shape, PartialShape, Type or str. Got {}".format(type(input))) + + def is_single_input(input: [tuple, list]): """ Checks if input has parameters for single input. @@ -94,14 +87,14 @@ def is_single_input(input: [tuple, list]): if name is not None: return False name = val - elif isinstance(val, (type, Type)): + elif is_type(val): if inp_type is not None: return False - inp_type = val + inp_type = to_ov_type(val) elif is_shape_type(val): if shape is not None: return False - shape = PartialShape(val) + shape = to_partial_shape(val) else: return False return True diff --git a/tools/ovc/openvino/tools/ovc/moc_frontend/shape_utils.py b/tools/ovc/openvino/tools/ovc/moc_frontend/shape_utils.py index defbe552b1d..68599646909 100644 --- a/tools/ovc/openvino/tools/ovc/moc_frontend/shape_utils.py +++ b/tools/ovc/openvino/tools/ovc/moc_frontend/shape_utils.py @@ -1,8 +1,11 @@ # Copyright (C) 2018-2023 Intel Corporation # SPDX-License-Identifier: Apache-2.0 +import sys + import numpy as np -from openvino.runtime import PartialShape, Dimension # pylint: disable=no-name-in-module,import-error +from openvino.runtime import Shape, PartialShape, Dimension # pylint: disable=no-name-in-module,import-error + from openvino.tools.ovc.error import Error @@ -62,3 +65,46 @@ def get_dynamic_dims(shape: [PartialShape, list, tuple]): dynamic_dims.append(idx) return dynamic_dims + + +def tensor_to_int_list(tensor): + assert hasattr(tensor, 'numpy'), "Could not get value of provided tensor: {}".format(tensor) + tensor_numpy = tensor.numpy() + assert tensor_numpy.dtype == np.int32, "Unexpected type of provided tensor. Expected int32, got: {}".format( + tensor_numpy.dtype) + return tensor_numpy.tolist() + +def to_partial_shape(shape): + if 'tensorflow' in sys.modules: + import tensorflow as tf + if isinstance(shape, tf.Tensor): + return PartialShape(tensor_to_int_list(shape)) + if isinstance(shape, tf.TensorShape): + return PartialShape(list(shape)) + if 'paddle' in sys.modules: + import paddle + if isinstance(shape, paddle.Tensor): + return PartialShape(tensor_to_int_list(shape)) + return PartialShape(shape) + + +def is_shape_type(value): + if isinstance(value, PartialShape): + return True + if 'tensorflow' in sys.modules: + import tensorflow as tf + if isinstance(value, (tf.TensorShape, tf.Tensor)): + return True + if 'paddle' in sys.modules: + import paddle + if isinstance(value, paddle.Tensor): + return True + if isinstance(value, Shape): + return True + if isinstance(value, list) or isinstance(value, tuple): + for dim in value: + if not (isinstance(dim, Dimension) or isinstance(dim, int)): + return False + return True + return False + diff --git a/tools/ovc/openvino/tools/ovc/moc_frontend/type_utils.py b/tools/ovc/openvino/tools/ovc/moc_frontend/type_utils.py new file mode 100644 index 00000000000..954143fda9b --- /dev/null +++ b/tools/ovc/openvino/tools/ovc/moc_frontend/type_utils.py @@ -0,0 +1,81 @@ +# Copyright (C) 2018-2023 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +import sys + +from openvino.runtime import Type + +import openvino as ov + + +def is_type(val): + if isinstance(val, (type, Type)): + return True + if 'tensorflow' in sys.modules: + import tensorflow as tf + if isinstance(val, tf.dtypes.DType): + return True + if 'torch' in sys.modules: + import torch + if isinstance(val, torch.dtype): + return True + if 'paddle' in sys.modules: + import paddle + if isinstance(val, paddle.dtype): + return True + return False + + +def to_ov_type(val): + if isinstance(val, Type): + return val + if isinstance(val, type): + return Type(val) + if 'tensorflow' in sys.modules: + import tensorflow as tf + if isinstance(val, tf.dtypes.DType): + return Type(val.as_numpy_dtype()) + if 'torch' in sys.modules: + import torch + + if isinstance(val, torch.dtype): + torch_to_ov_type = { + torch.float32: ov.Type.f32, + torch.float16: ov.Type.f16, + torch.float64: ov.Type.f64, + torch.bfloat16: ov.Type.bf16, + torch.uint8: ov.Type.u8, + torch.int8: ov.Type.i8, + torch.int16: ov.Type.i16, + torch.int32: ov.Type.i32, + torch.int64: ov.Type.i64, + torch.bool: ov.Type.boolean, + } + if val not in torch_to_ov_type: + raise Exception("The provided data time is not supported {}.".format(val)) + + return torch_to_ov_type[val] + + if 'paddle' in sys.modules: + import paddle + + if isinstance(val, paddle.dtype): + paddle_to_ov_type = { + paddle.float32: ov.Type.f32, + paddle.float16: ov.Type.f16, + paddle.float64: ov.Type.f64, + paddle.bfloat16: ov.Type.bf16, + paddle.uint8: ov.Type.u8, + paddle.int8: ov.Type.i8, + paddle.int16: ov.Type.i16, + paddle.int32: ov.Type.i32, + paddle.int64: ov.Type.i64, + paddle.bool: ov.Type.boolean, + } + + if val not in paddle_to_ov_type: + raise Exception("The provided data time is not supported {}.".format(val)) + + return paddle_to_ov_type[val] + raise Exception("Unexpected type object. Expected ov.Type, np.dtype, tf.dtypes.DType. Got {}".format(type(val))) + diff --git a/tools/ovc/unit_tests/ovc/package_BOM.txt b/tools/ovc/unit_tests/ovc/package_BOM.txt index 5c8c73a2ac8..c7b37c2c147 100644 --- a/tools/ovc/unit_tests/ovc/package_BOM.txt +++ b/tools/ovc/unit_tests/ovc/package_BOM.txt @@ -22,6 +22,7 @@ openvino/tools/ovc/moc_frontend/pipeline.py openvino/tools/ovc/moc_frontend/preprocessing.py openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py openvino/tools/ovc/moc_frontend/shape_utils.py +openvino/tools/ovc/moc_frontend/type_utils.py openvino/tools/ovc/ovc.py openvino/tools/ovc/telemetry_params.py openvino/tools/ovc/telemetry_stub.py