Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 8 min read How-to mediumAdept

Training a language model with 🤗 Transformers using TensorFlow and TPUs

Training 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.

Assumes: TensorFlow/Keras basicsHugging Face Transformersmasked language modeling

Related posts

  • Hugging Face ❤️ TorchTPU: TPUs, Natively

    Hugging Face mediumAdept

    TorchTPU is a native PyTorch backend that makes TPU an ordinary device, replacing the lazy-tensor torch_xla path and making Optimum TPU unnecessary for running Hugging Face models. Transformers loads models with device_map="tpu", and Diffusers pipelines run with pipe.to("tpu"), optionally with torch.compile and tensor parallelism across chips. Pallas kernels from the Kernels Hub, such as a Tokamax ragged-dot MoE kernel, speed up batch-64 decoding by 21.5Ă— on Qwen3-30B-A3B and 12.8Ă— on Gemma 4 26B-A4B. Of the Transformers tests that run on TPU, 96.5% pass, and 34 TorchTPU issues have been reported so far, 19 of them fixed.

  • Hugging Face on AMD Instinct MI300 GPU

    Hugging Face easyNovice

    Hugging Face's transformers and text-generation-inference (TGI) now run on AMD Instinct MI300X GPUs with no code changes, backed by a Kubernetes-scheduled CI pipeline that runs tens of thousands of unit tests on MI250 and MI300 hardware. TGI optimizations for MI300 include Flash Attention v2, Paged Attention, GPTQ/AWQ kernels, fused kernels, and PyTorch's ROCm TunableOp. TunableOp tunes GEMMs during server warmup and gives an 8-10% decode latency gain on small inputs. On Llama 3 70B, MI300X on Azure delivers 2-3x faster time to first token and 2x faster decoding than MI250, and roughly 2x faster LoRA fine-tuning. Its 192 GB HBM3 also fits the full ~140 GB fp16 model on a single GPU.

  • Make LLM Fine-tuning 2x faster with Unsloth and 🤗 TRL

    Hugging Face mediumAdept

    Unsloth speeds up QLoRA fine-tuning of Llama and Mistral-family models by rewriting PyTorch modules as Triton kernels with manually derived backpropagation. Because no approximations are made, it claims 0% accuracy loss relative to standard QLoRA. Across 59 benchmark runs on A100 and T4 GPUs, it ran 1.5–3.9x faster than Hugging Face transformers and used up to 74% less VRAM. Code examples show loading pre-quantized 4-bit models with FastLanguageModel, attaching LoRA adapters, and training with TRL's SFTTrainer or DPOTrainer.

  • “Llama 3.2 in Keras”

    Hugging Face mediumAdept

    Keras (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.

  • Hugging Face's TensorFlow Philosophy

    Hugging Face mediumAdept

    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.

The quest for software engineering knowledge.

Look:
Layout: