Add tf_keras imports to prepare for Keras 3 (#28588)
* Port core files + ESM (because ESM code is odd) * Search-replace in modelling code * Fix up transfo_xl as well * Fix other core files + tests (still need to add correct import to tests) * Fix cookiecutter * make fixup, fix imports in some more core files * Auto-add imports to tests * Cleanup, add imports to sagemaker tests * Use correct exception for importing tf_keras * Fixes in modeling_tf_utils * make fixup * Correct version parsing code * Ensure the pipeline tests correctly revert to float32 after each test * Ensure the pipeline tests correctly revert to float32 after each test * More tf.keras -> keras * Add dtype cast * Better imports of tf_keras * Add a cast for tf.assign, just in case * Fix callback imports
This commit is contained in:
@@ -22,6 +22,7 @@ import os
|
||||
import re
|
||||
|
||||
import tensorflow as tf
|
||||
from packaging.version import parse
|
||||
|
||||
from transformers import (
|
||||
AutoConfig,
|
||||
@@ -33,6 +34,19 @@ from transformers import (
|
||||
)
|
||||
|
||||
|
||||
try:
|
||||
import tf_keras as keras
|
||||
except (ModuleNotFoundError, ImportError):
|
||||
import keras
|
||||
|
||||
if parse(keras.__version__).major > 2:
|
||||
raise ValueError(
|
||||
"Your currently installed version of Keras is Keras 3, but this is not yet supported in "
|
||||
"Transformers. Please install the backwards-compatible tf-keras package with "
|
||||
"`pip install tf-keras`."
|
||||
)
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
AUTO = tf.data.AUTOTUNE
|
||||
@@ -209,7 +223,7 @@ def main(args):
|
||||
strategy = tf.distribute.OneDeviceStrategy(device="/gpu:0")
|
||||
|
||||
if args.bfloat16:
|
||||
tf.keras.mixed_precision.set_global_policy("mixed_bfloat16")
|
||||
keras.mixed_precision.set_global_policy("mixed_bfloat16")
|
||||
|
||||
tokenizer = AutoTokenizer.from_pretrained(args.tokenizer)
|
||||
config = AutoConfig.from_pretrained(args.pretrained_model_config)
|
||||
|
||||
Reference in New Issue
Block a user