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

Set env var to hold Keras at Keras 2 #29598

Merged
merged 4 commits into from
Mar 12, 2024
Merged

Set env var to hold Keras at Keras 2 #29598

merged 4 commits into from
Mar 12, 2024

Conversation

Rocketknight1
Copy link
Member

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 in modeling_tf_utils to make sure that no Keras 3 objects sneak in - this should hopefully fix a lot of user scripts that are using tf.keras instead of keras or tf-keras as well.

Fixes #29470

@HuggingFaceDocBuilderDev

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.

Copy link
Collaborator

@amyeroberts amyeroberts left a 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
Copy link
Collaborator

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

Copy link
Member Author

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.

Copy link
Collaborator

@amyeroberts amyeroberts left a 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!

@Rocketknight1 Rocketknight1 merged commit df15425 into main Mar 12, 2024
21 checks passed
@Rocketknight1 Rocketknight1 deleted the keras_3_compat_fix branch March 12, 2024 13:49
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.

Suddenly unable to create BERT encodings
3 participants