From e11e8ede1b24f90338482c7befc0a893024257e9 Mon Sep 17 00:00:00 2001 From: Maxim Vafin Date: Wed, 23 Aug 2023 21:16:26 +0200 Subject: [PATCH] [MO] Fix issue in nncf version verification (#19347) * Return deleted nncf import * Remove try-except, it hides exception * Get version visout importing nncf module --- .../mo/moc_frontend/pytorch_frontend_utils.py | 21 +++++++++++-------- .../moc_frontend/pytorch_frontend_utils.py | 21 +++++++++++-------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/tools/mo/openvino/tools/mo/moc_frontend/pytorch_frontend_utils.py b/tools/mo/openvino/tools/mo/moc_frontend/pytorch_frontend_utils.py index aaf8b9fd89efcc..5b11f8c6998b66 100644 --- a/tools/mo/openvino/tools/mo/moc_frontend/pytorch_frontend_utils.py +++ b/tools/mo/openvino/tools/mo/moc_frontend/pytorch_frontend_utils.py @@ -17,17 +17,20 @@ def get_pytorch_decoder(model, input_shape, example_inputs, args): except Exception as e: log.error("PyTorch frontend loading failed") raise e - try: - if 'nncf' in sys.modules: - from nncf.torch.nncf_network import NNCFNetwork # pylint: disable=undefined-variable - from packaging import version + if 'nncf' in sys.modules: + is_good_version = True + try: + from nncf.torch.nncf_network import NNCFNetwork if isinstance(model, NNCFNetwork): - if version.parse(nncf.__version__) < version.parse("2.6"): # pylint: disable=undefined-variable - raise RuntimeError( - "NNCF models produced by nncf<2.6 are not supported directly. Please upgrade nncf or export to ONNX first.") - except: - pass + from packaging import version + if version.parse(sys.modules['nncf'].__version__) < version.parse("2.6"): + is_good_version = False + except: + pass + if not is_good_version: + raise RuntimeError( + "NNCF models produced by nncf<2.6 are not supported directly. Please upgrade nncf or export to ONNX first.") inputs = prepare_torch_inputs(example_inputs) decoder = TorchScriptPythonDecoder(model, example_input=inputs, shared_memory=args.get("share_weights", True)) args['input_model'] = decoder diff --git a/tools/ovc/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py b/tools/ovc/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py index 9004bacc4bf7a3..2703af43d8b192 100644 --- a/tools/ovc/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py +++ b/tools/ovc/openvino/tools/ovc/moc_frontend/pytorch_frontend_utils.py @@ -17,17 +17,20 @@ def get_pytorch_decoder(model, example_inputs, args): except Exception as e: log.error("PyTorch frontend loading failed") raise e - try: - if 'nncf' in sys.modules: - from nncf.torch.nncf_network import NNCFNetwork # pylint: disable=undefined-variable - from packaging import version + if 'nncf' in sys.modules: + is_good_version = True + try: + from nncf.torch.nncf_network import NNCFNetwork if isinstance(model, NNCFNetwork): - if version.parse(nncf.__version__) <= version.parse("2.6"): # pylint: disable=undefined-variable - raise RuntimeError( - "NNCF models produced by nncf<2.6 are not supported directly. Please upgrade nncf or export to ONNX first.") - except: - pass + from packaging import version + if version.parse(sys.modules['nncf'].__version__) < version.parse("2.6"): + is_good_version = False + except: + pass + if not is_good_version: + raise RuntimeError( + "NNCF models produced by nncf<2.6 are not supported directly. Please upgrade nncf or export to ONNX first.") inputs = prepare_torch_inputs(example_inputs) decoder = TorchScriptPythonDecoder(model, example_input=inputs, shared_memory=args.get("share_weights", True)) args['input_model'] = decoder