From cd4f4f287083093c03243a7d6e78507713f2ba35 Mon Sep 17 00:00:00 2001 From: Alexander Dokuchaev Date: Mon, 11 Mar 2024 08:37:38 +0200 Subject: [PATCH] [PT] check only major and minor version (#2422) ### Changes - Use `SpecifierSet` to check version of PT and TF - Remove unused `BKC_TORCHVISION_VERSION` - Fix strict check version of TF that never raised before --------- Co-authored-by: Alexander Suslov --- nncf/common/logging/logger.py | 4 ++-- nncf/tensorflow/__init__.py | 20 ++++++++++---------- nncf/tensorflow/tf_internals.py | 2 +- nncf/torch/__init__.py | 13 ++++++------- nncf/version.py | 6 +++--- ruff.toml | 2 +- 6 files changed, 23 insertions(+), 24 deletions(-) diff --git a/nncf/common/logging/logger.py b/nncf/common/logging/logger.py index 26284dae1f3..5ba4b9a257c 100644 --- a/nncf/common/logging/logger.py +++ b/nncf/common/logging/logger.py @@ -82,7 +82,7 @@ def extension_is_loading_info_log(extension_name: str): def warn_bkc_version_mismatch(backend: str, bkc_version: str, current_version: str): nncf_logger.warning( - f"NNCF provides best results with {backend}=={bkc_version}, " + f"NNCF provides best results with {backend}{bkc_version}, " f"while current {backend} version is {current_version}. " - f"If you encounter issues, consider switching to {backend}=={bkc_version}" + f"If you encounter issues, consider switching to {backend}{bkc_version}" ) diff --git a/nncf/tensorflow/__init__.py b/nncf/tensorflow/__init__.py index 80bc64c2211..d3801ae239e 100644 --- a/nncf/tensorflow/__init__.py +++ b/nncf/tensorflow/__init__.py @@ -14,26 +14,26 @@ import tensorflow from packaging import version +from packaging.specifiers import SpecifierSet import nncf from nncf import nncf_logger from nncf.common.logging.logger import warn_bkc_version_mismatch -from nncf.version import BKC_TF_VERSION +from nncf.version import BKC_TF_SPEC +from nncf.version import STRICT_TF_SPEC try: - _tf_version = tensorflow.__version__ - tensorflow_version = version.parse(_tf_version).base_version + tensorflow_version = version.parse(version.parse(tensorflow.__version__).base_version) except: nncf_logger.debug("Could not parse tensorflow version") - _tf_version = "0.0.0" - tensorflow_version = version.parse(_tf_version).base_version -tensorflow_version_major, tensorflow_version_minor = tuple(map(int, tensorflow_version.split(".")))[:2] -if not tensorflow_version.startswith(BKC_TF_VERSION[:-2]): - warn_bkc_version_mismatch("tensorflow", BKC_TF_VERSION, _tf_version) -elif not (tensorflow_version_major == 2 and 8 <= tensorflow_version_minor <= 13): + tensorflow_version = version.parse("0.0.0") + +if tensorflow_version not in SpecifierSet(STRICT_TF_SPEC): raise nncf.UnsupportedVersionError( - f"NNCF only supports 2.8.4 <= tensorflow <= 2.13.*, while current tensorflow version is {_tf_version}" + f"NNCF only supports tensorflow{STRICT_TF_SPEC}, while current tensorflow version is {tensorflow_version}" ) +if tensorflow_version not in SpecifierSet(BKC_TF_SPEC): + warn_bkc_version_mismatch("torch", BKC_TF_SPEC, tensorflow.__version__) from nncf.common.accuracy_aware_training.training_loop import ( diff --git a/nncf/tensorflow/tf_internals.py b/nncf/tensorflow/tf_internals.py index b8521513149..2892c0e99cf 100644 --- a/nncf/tensorflow/tf_internals.py +++ b/nncf/tensorflow/tf_internals.py @@ -18,7 +18,7 @@ from nncf.tensorflow import tensorflow_version -if version.parse(tensorflow_version) < version.parse("2.13"): +if tensorflow_version < version.parse("2.13"): from keras import engine as keras_engine # noqa: F401 from keras.applications import imagenet_utils as imagenet_utils from keras.engine.keras_tensor import KerasTensor as KerasTensor diff --git a/nncf/torch/__init__.py b/nncf/torch/__init__.py index fe01f433541..59c2047e224 100644 --- a/nncf/torch/__init__.py +++ b/nncf/torch/__init__.py @@ -17,21 +17,20 @@ from nncf import nncf_logger from nncf.common.logging.logger import warn_bkc_version_mismatch -from nncf.version import BKC_TORCH_VERSION +from nncf.version import BKC_TORCH_SPEC import torch from packaging import version +from packaging.specifiers import SpecifierSet try: - _torch_version = torch.__version__ - torch_version = version.parse(_torch_version).base_version + _torch_version = version.parse(version.parse(torch.__version__).base_version) except: # noqa: E722 nncf_logger.debug("Could not parse torch version") - _torch_version = "0.0.0" - torch_version = version.parse(_torch_version).base_version + _torch_version = version.parse("0.0.0") -if version.parse(BKC_TORCH_VERSION).base_version != torch_version: - warn_bkc_version_mismatch("torch", BKC_TORCH_VERSION, torch.__version__) +if _torch_version not in SpecifierSet(BKC_TORCH_SPEC): + warn_bkc_version_mismatch("torch", BKC_TORCH_SPEC, torch.__version__) # Required for correct COMPRESSION_ALGORITHMS registry functioning diff --git a/nncf/version.py b/nncf/version.py index 87dd9df1d2d..8797db09c41 100644 --- a/nncf/version.py +++ b/nncf/version.py @@ -11,6 +11,6 @@ __version__ = "2.10.0" -BKC_TORCH_VERSION = "2.2.1" -BKC_TORCHVISION_VERSION = "0.17.1" -BKC_TF_VERSION = "2.12.*" +BKC_TORCH_SPEC = "==2.2.*" +BKC_TF_SPEC = "==2.12.*" +STRICT_TF_SPEC = ">=2.8.4,<2.14.0" diff --git a/ruff.toml b/ruff.toml index a29a1d4156d..53940dc8dcb 100644 --- a/ruff.toml +++ b/ruff.toml @@ -18,7 +18,7 @@ exclude = ["nncf/tensorflow/__init__.py"] [per-file-ignores] "nncf/experimental/torch/nas/bootstrapNAS/__init__.py" = ["F401"] -"nncf/torch/__init__.py" = ["F401"] +"nncf/torch/__init__.py" = ["F401", "E402"] "tests/**/*.py" = ["F403"] "tests/**/__init__.py" = ["F401"] "examples/**/*.py" = ["F403"]