Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion keras_nlp/backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@

def detect_if_tensorflow_uses_keras_3():
# We follow the version of keras that tensorflow is configured to use.
from tensorflow import keras
import keras
Copy link
Member

@mattdangerw mattdangerw Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this actually keep any use case working, so might not be worth the extra complexity. tensorflow-text will use tf.keras, so if tf.keras is broken, our preprocessing is broken.

https://github.com/tensorflow/text/blob/b32645fbf1e4fd7e81d8d03fa2d2b4872e3a270d/tensorflow_text/python/keras/layers/todense.py#L28

I would somewhat consider this an expected fail state, and maybe instead do something like

try:
    from tensorflow import keras
except:
    raise ValueError(
        "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."
    )

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed it. Can you check if the updates make sense?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine!

I think we could also just keep the try/catch logic I posted above for simplicity. No version parsing. Might catch other weird edge cases we haven't thought of and still give the helpful link to keras.io. But ok with this too. Is there a reason we are avoiding trying from tensorflow import keras here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Reverted back to try/except with from tensorflow 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."):
Expand Down