Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Keras 3 version check #1328

Merged
merged 4 commits into from
Nov 28, 2023
Merged

Conversation

sampathweb
Copy link
Collaborator

keras-nlp has a function to check if TensorFlow uses Keras 3 via -
https://github.com/keras-team/keras-nlp/blob/master/keras_nlp/backend/config.py#L66

This works when using tf-nightly or TensorFlow 2.15, but breaks on TensorFlow 2.14 which is the default in Colab. To fix it, use import keras instead of from tensorflow import keras when doing this check.

Colab to repro the issue:
https://colab.research.google.com/drive/17AwBX5S29d-hjUDPLN8roHSNzrjXricG?usp=sharing

@sampathweb sampathweb requested a review from fchollet November 26, 2023 22:52
@@ -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

@sampathweb sampathweb removed the request for review from fchollet November 27, 2023 20:47
@@ -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

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
Member

@mattdangerw mattdangerw left a comment

Choose a reason for hiding this comment

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

lgtm! thank you

@sampathweb sampathweb merged commit 36a62a6 into keras-team:master Nov 28, 2023
@sampathweb sampathweb deleted the fix-use-keras3 branch November 28, 2023 03:13
mattdangerw pushed a commit to mattdangerw/keras-hub that referenced this pull request Dec 7, 2023
* Fix Keras 3 version check

* Fix Keras 3 version check

* Update version check

* Raise error if Keras is not compatible with TF
mattdangerw pushed a commit to mattdangerw/keras-hub that referenced this pull request Dec 7, 2023
* Fix Keras 3 version check

* Fix Keras 3 version check

* Update version check

* Raise error if Keras is not compatible with TF
mattdangerw pushed a commit to mattdangerw/keras-hub that referenced this pull request Dec 7, 2023
* Fix Keras 3 version check

* Fix Keras 3 version check

* Update version check

* Raise error if Keras is not compatible with TF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants