From b63dc123c675fe31bb8fd129b6ff26582b0ef924 Mon Sep 17 00:00:00 2001 From: sampathweb <1437573+sampathweb@users.noreply.github.com> Date: Sun, 26 Nov 2023 14:33:00 -0600 Subject: [PATCH 1/4] Fix Keras 3 version check --- keras_nlp/backend/config.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/keras_nlp/backend/config.py b/keras_nlp/backend/config.py index 11b0f24269..06588ab401 100644 --- a/keras_nlp/backend/config.py +++ b/keras_nlp/backend/config.py @@ -63,9 +63,9 @@ _MULTI_BACKEND = True -def detect_if_tensorflow_uses_keras_3(): +def detect_if_keras_3_installed(): # We follow the version of keras that tensorflow is configured to use. - from tensorflow import keras + import keras # Note that only recent versions of keras have a `version()` function. if hasattr(keras, "version") and keras.version().startswith("3."): @@ -75,7 +75,7 @@ def detect_if_tensorflow_uses_keras_3(): return False -_USE_KERAS_3 = detect_if_tensorflow_uses_keras_3() +_USE_KERAS_3 = detect_if_keras_3_installed() if _USE_KERAS_3: _MULTI_BACKEND = True From d9274e9f6847106955c2e3c62ed406157ce61272 Mon Sep 17 00:00:00 2001 From: sampathweb <1437573+sampathweb@users.noreply.github.com> Date: Sun, 26 Nov 2023 16:51:16 -0600 Subject: [PATCH 2/4] Fix Keras 3 version check --- keras_nlp/backend/config.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/keras_nlp/backend/config.py b/keras_nlp/backend/config.py index 06588ab401..a32c132e0a 100644 --- a/keras_nlp/backend/config.py +++ b/keras_nlp/backend/config.py @@ -63,10 +63,21 @@ _MULTI_BACKEND = True -def detect_if_keras_3_installed(): +def detect_if_tensorflow_uses_keras_3(): # We follow the version of keras that tensorflow is configured to use. import keras + # Return False if env variable is set and `tf_keras` is installed. + use_legacy_keras = os.environ.get("TF_USE_LEGACY_KERAS", "") + if use_legacy_keras == "1" or use_legacy_keras.lower() == "true": + try: + import tf_keras # noqa: F401 + + return False + except ImportError: + # `tf_keras` is not installed + pass + # Note that only recent versions of keras have a `version()` function. if hasattr(keras, "version") and keras.version().startswith("3."): return True @@ -75,7 +86,7 @@ def detect_if_keras_3_installed(): return False -_USE_KERAS_3 = detect_if_keras_3_installed() +_USE_KERAS_3 = detect_if_tensorflow_uses_keras_3() if _USE_KERAS_3: _MULTI_BACKEND = True From af9dcb6d0c05e479097171aebc83598861be5add Mon Sep 17 00:00:00 2001 From: sampathweb <1437573+sampathweb@users.noreply.github.com> Date: Mon, 27 Nov 2023 14:59:52 -0600 Subject: [PATCH 3/4] Update version check --- keras_nlp/backend/config.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/keras_nlp/backend/config.py b/keras_nlp/backend/config.py index a32c132e0a..0a90e48390 100644 --- a/keras_nlp/backend/config.py +++ b/keras_nlp/backend/config.py @@ -66,20 +66,25 @@ def detect_if_tensorflow_uses_keras_3(): # We follow the version of keras that tensorflow is configured to use. import keras + import tensorflow as tf + from packaging import version # Return False if env variable is set and `tf_keras` is installed. - use_legacy_keras = os.environ.get("TF_USE_LEGACY_KERAS", "") - if use_legacy_keras == "1" or use_legacy_keras.lower() == "true": - try: - import tf_keras # noqa: F401 - - return False - except ImportError: - # `tf_keras` is not installed - pass - + if os.environ.get("TF_USE_LEGACY_KERAS", None) in ("true", "True", "1"): + raise ValueError( + "`keras-nlp` is not compatible with usage of `tf-keras` " + "via environment variable `TF_USE_LEGACY_KERAS`. " + "Please unset this environment variable to use `keras-nlp`." + ) # Note that only recent versions of keras have a `version()` function. if hasattr(keras, "version") and keras.version().startswith("3."): + if version.parse(tf.__version__) < version.parse("2.15.0"): + raise ValueError( + f"Your `tensorflow` version {tf.__version__} is not compatible " + f"with `keras` version {keras.version()}. Keras 3 requires " + "TensorFlow 2.15 or later. See keras.io/getting_started for " + "more information on installing Keras." + ) return True # No `keras.version()` means we are on an old version of keras. From c666299223da6723bafd2703144356ecb07f94b1 Mon Sep 17 00:00:00 2001 From: sampathweb <1437573+sampathweb@users.noreply.github.com> Date: Mon, 27 Nov 2023 16:48:05 -0600 Subject: [PATCH 4/4] Raise error if Keras is not compatible with TF --- keras_nlp/backend/config.py | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/keras_nlp/backend/config.py b/keras_nlp/backend/config.py index 0a90e48390..9b73ae09cd 100644 --- a/keras_nlp/backend/config.py +++ b/keras_nlp/backend/config.py @@ -65,27 +65,19 @@ def detect_if_tensorflow_uses_keras_3(): # We follow the version of keras that tensorflow is configured to use. - import keras - import tensorflow as tf - from packaging import version + try: + from tensorflow import keras - # Return False if env variable is set and `tf_keras` is installed. - if os.environ.get("TF_USE_LEGACY_KERAS", None) in ("true", "True", "1"): + # Note that only recent versions of keras have a `version()` function. + if hasattr(keras, "version") and keras.version().startswith("3."): + return True + except: raise ValueError( - "`keras-nlp` is not compatible with usage of `tf-keras` " - "via environment variable `TF_USE_LEGACY_KERAS`. " - "Please unset this environment variable to use `keras-nlp`." + "Unable to import `keras` with `tensorflow`. Please check your " + "Keras and Tensorflow version are compatible; Keras 3 requires " + "TensorFlow 2.15 or later. See keras.io/getting_started for more " + "information on installing Keras." ) - # Note that only recent versions of keras have a `version()` function. - if hasattr(keras, "version") and keras.version().startswith("3."): - if version.parse(tf.__version__) < version.parse("2.15.0"): - raise ValueError( - f"Your `tensorflow` version {tf.__version__} is not compatible " - f"with `keras` version {keras.version()}. Keras 3 requires " - "TensorFlow 2.15 or later. See keras.io/getting_started for " - "more information on installing Keras." - ) - return True # No `keras.version()` means we are on an old version of keras. return False