-
Notifications
You must be signed in to change notification settings - Fork 27.9k
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
Set env var to hold Keras at Keras 2 #29598
Conversation
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does TF have to be so tricky 🙃
General comment on not overriding environment variables quietly
@@ -78,6 +78,8 @@ | |||
if TYPE_CHECKING: | |||
from . import PreTrainedTokenizerBase | |||
|
|||
os.environ["TF_USE_LEGACY_KERAS"] = "1" # Compatibility fix to make sure tf.keras stays at Keras 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't just override an environment variable like this - this can lead to all sorts of unexpected behaviours on the users end e.g. if they explicitly set it to "0" in their own environment before running a script.
Instead, we should:
- Check if it's already set
- If it's set to 1, then no worries
- If it's set to 0, raise an error saying this isn't compatible with transformers
- If it's unset, set it to 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! I raise a warning instead of an error because I expect it to be relatively uncommon for people to explicitly set the env var to 0 (as 0 just maintains the default behaviour). Also, it might be possible to use some objects like tokenizers from transformers
while still using Keras 3, so I don't want to forcibly raise an error every time modeling_tf_utils
is touched.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for digging into and handling this tricky TF issue!
Newer versions of TF use the environment variable
TF_USE_LEGACY_KERAS
to decide whether to use Keras 2 or Keras 3. We set this immediately inmodeling_tf_utils
to make sure that no Keras 3 objects sneak in - this should hopefully fix a lot of user scripts that are usingtf.keras
instead ofkeras
ortf-keras
as well.Fixes #29470