Hugging Face's TensorFlow Philosophy
Hugging Face's transformers library treats TensorFlow models as first-class Keras citizens: every model is a Keras Model and every layer a Keras Layer, so fit(), compile() and predict() work directly, and models compose into custom hybrids via subclassing. If compile() is called without a loss argument, a sensible default loss is supplied (e.g. masked LM loss that matches BERT's original training), but a user-specified loss overrides it. Labels can now be passed the standard Keras way, and prepare_tf_dataset() streams disk-backed datasets with per-batch padding. XLA via jit_compile=True brings generation and training to roughly JAX-level speed, with the caveat that variable input shapes force recompilation.
Related posts
- Faster Text Generation with TensorFlow and XLA
Hugging Face mediumAdeptTensorFlow text generation in Hugging Face transformers can be compiled with XLA by wrapping `model.generate` in `tf.function(jit_compile=True)`, yielding up to 100x speedups and outperforming PyTorch. XLA JIT-compiles per input shape, dtype and non-tensor argument, so every new prompt length or generation option triggers a costly retrace (seconds). Padding inputs with `pad_to_multiple_of` bounds the number of shapes, cutting repeated GPT-2 generation calls from several seconds to about 19 ms on an RTX 3090. A recap of greedy decoding, sampling, temperature and beam search options comes first.
- Announcing New Hugging Face and KerasHub integration
Hugging Face mediumAdeptKerasHub can now load Transformers-format checkpoints from the Hugging Face Hub directly, through a shared model save format. Initial support covers Gemma 1 and 2, Llama 3 and PaliGemma, and the models can run on TensorFlow, JAX or PyTorch backends. Loading works by mapping the Transformers JSON config, tokenizer and safetensors weight names onto KerasHub's equivalent architecture code, which also allows conversion in the other direction. Code examples show `from_preset("hf://...")` loading, text generation, setting bfloat16 precision and switching to the JAX backend.
- “Llama 3.2 in Keras”
Hugging Face mediumAdeptKeras (via keras_hub) loads Llama 3.2 directly from standard safetensors Hugging Face checkpoints with on-the-fly conversion, so it has worked since day one. A tour of the workflow covers the multi-backend switch (JAX, PyTorch, TensorFlow), tokenizer-included generate/fit on raw strings, chat formatting for the Instruct model, preprocessor and backbone access, LoRA fine-tuning, and uploading presets back to the Hub. For larger models like Llama 3.1 8B, Keras's ModelParallel distribution with a device mesh and layout map shards weights across TPU/GPU cores. A custom layout map cut epoch time from 62s to 54s on an 8-core TPU v5e.
- Fine-Tuning Gemma Models in Hugging Face
Hugging Face mediumAdeptGemma 2B and 7B can be fine-tuned cheaply with LoRA via the Hugging Face PEFT library, loading the base model in 4-bit NF4 with bitsandbytes (QLoRA) so it fits on free Colab GPUs. A worked example adapts all linear projection layers (r=8) and trains with TRL's SFTTrainer on an English quotes dataset, teaching the model to emit a "Quote/Author" format in just 10 steps. For Cloud TPUs, transformers now supports PyTorch/XLA FSDP via SPMD, enabled by adding an fsdp_config that wraps GemmaDecoderLayer with xla_fsdp_v2 and gradient checkpointing.
- Training a language model with 🤗 Transformers using TensorFlow and TPUs
Hugging Face mediumAdeptTraining a RoBERTa-base masked language model from scratch on WikiText with TensorFlow on TPUs, end to end: training a Unigram tokenizer with 🤗 tokenizers, tokenizing and chunking text into fixed 128-token sequences, sharding into TFRecords stored in Google Cloud Storage, and training under TPUStrategy with XLA-compatible Transformers models. Key details include initializing the model and optimizer inside the strategy scope, using a TF-native DataCollatorForLanguageModeling (return_tensors="tf"), and streaming data from GCS because TPU node host memory is small. The takeaway is that existing TF/Keras Transformers pipelines need little change to scale on TPU pods, offering an alternative to scarce GPUs.