Skip to content

Conversation

markjm
Copy link

@markjm markjm commented Aug 22, 2025

What does this PR do?

Tensorflow 2.16 made keras 3 the default https://github.com/tensorflow/tensorflow/releases/tag/v2.16.1

Since huggingface does not support keras 3 yet, this compat logic was introduced in df15425

However, in tensorflow 2.15, keras is still keras 2, meaning we don't need tf-keras at this point. However, since this TF_USE_LEGACY_KERAS is set unconditionally (even in 2.15):

  1. import of this file sets TF_USE_LEGACY_KERAS -> 1
  2. import tensorflow attempts to import tf_keras
  3. we dont have that package because we dont need it
  4. import fails and process bails

This change slightly modifies the environment modification to only happen if tf_keras is available. If it is available, we ensure tensorflow also uses it internally. If its not available, we go into the except block and use raw keras

Repro

With tensorflow 2.15 installed

from transformers.models.distilbert import TFDistilBertMainLayer # causes TF_USE_LEGACY_KERAS to be set
import tensorflow as tf                
tf.keras.models.load_model # simply getting this will cause error
WARNING:tensorflow:Your environment has TF_USE_LEGACY_KERAS set to True, but you do not have the tf_keras package installed. You must install it in order to use the legacy tf.keras. Install it via: `pip install tf_keras`
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/...lib/python3.11/site-packages/tensorflow/python/util/lazy_loader.py", line 146, in __getattr__
    self._initialize()
  File "/home/.../lib/python3.11/site-packages/tensorflow/python/util/lazy_loader.py", line 138, in _initialize
    raise ImportError(  # pylint: disable=raise-missing-from
ImportError: Keras cannot be imported. Check that it is installed.

after this change

from transformers.models.distilbert import TFDistilBertMainLayer
import tensorflow as tf                
tf.keras.models.load_model # happy

Note: there is no current associated issue but i can make one if necessary

Who can review?

@Rocketknight1 based on linked commit

Tensorflow 2.16 made keras 3 the default https://github.com/tensorflow/tensorflow/releases/tag/v2.16.1

Since huggingface does not support keras 3 yet, this compat logic was introduced in huggingface@df15425

However, in tensorflow 2.15, keras is still keras 2, meaning we don't need `tf-keras` at this point. However, since this `TF_USE_LEGACY_KERAS` is set unconditionally in 2.15:

1. TF_USE_LEGACY_KERAS -> 1
2. import tensorflow attempts to `import tf_keras`
3. import fails and process bails

This change slightly modifies the environment modification to only happen if tf_keras is available. If it is available, we ensure tensorflow also uses it internally.

If its not available, we go into the `except` block and use raw `keras`

if "TF_USE_LEGACY_KERAS" not in os.environ:
os.environ["TF_USE_LEGACY_KERAS"] = "1" # Compatibility fix to make sure tf.keras stays at Keras 2
elif os.environ.get("TF_USE_LEGACY_KERAS", None) not in ("true", "True", "1"):
Copy link
Author

Choose a reason for hiding this comment

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

@markjm markjm changed the title fix: fix keras compat with TF_USE_LEGACY_KERAS in tensorflow >2.15 fix: fix keras compat with TF_USE_LEGACY_KERAS in tensorflow <2.15 Aug 23, 2025
@Rocketknight1
Copy link
Member

Hi, we've unfortunately deprecated TF support entirely at this point! It will likely be removed in a future version. I'd recommend just trying to use an older version of Transformers if TF2 support is essential. If you want Keras 3 you can try KerasNLP, which can load some models from the Hub, or you can just switch to PyTorch, which is our only core1 supported framework now.

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