---
title: "Training a language model with 🤗 Transformers using TensorFlow and TPUs"
description: "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."
url: "https://swe.quest/articles/training-a-language-model-with-transformers-using-tensorflow-and-tpus-7d72fd7dce30644b"
canonical: "https://huggingface.co/blog/tf_tpu"
source: "Hugging Face"
published: "2023-04-27"
difficulty: "medium"
kind: "How-to"
reading_minutes: 8
tags: ["model-training","hardware","python","llms"]
---

# Training a language model with 🤗 Transformers using TensorFlow and TPUs

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2023-04-27, 8 min read, How-to. Original: [https://huggingface.co/blog/tf\_tpu](https://huggingface.co/blog/tf_tpu)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [Hardware](https://swe.quest/tags/hardware.md) (Infrastructure)
- [Python](https://swe.quest/tags/python.md) (Languages)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)

## Prerequisites

- TensorFlow/Keras basics
- Hugging Face Transformers
- masked language modeling

## Keywords

TPU, TensorFlow, XLA, TPUStrategy, Hugging Face Transformers, RoBERTa, TFRecord, Google Cloud Storage

## Related posts

- [Hugging Face ❤️ TorchTPU: TPUs, Natively](https://swe.quest/articles/hugging-face-torchtpu-tpus-natively-25e58a9bb63cbf24.md): Hugging Face · 2026-10-20 · Medium · 6 min read · Inference & serving, Hardware, Python, Machine learning
  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.
- [Hugging Face on AMD Instinct MI300 GPU](https://swe.quest/articles/hugging-face-on-amd-instinct-mi300-gpu-f116bf43c7c06916.md): Hugging Face · 2024-05-21 · Easy · 7 min read · Inference & serving, Hardware, LLMs, Performance engineering, Model training
  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.
- [Make LLM Fine-tuning 2x faster with Unsloth and 🤗 TRL](https://swe.quest/articles/make-llm-fine-tuning-2x-faster-with-unsloth-and-trl-5db8c0b6a40cf74e.md): Hugging Face · 2024-01-10 · Medium · 5 min read · Model training, LLMs, Performance engineering, Python
  Unsloth speeds up QLoRA fine-tuning of Llama and Mistral-family models by rewriting PyTorch modules as Triton kernels with manually derived backpropagation.
- [“Llama 3.2 in Keras”](https://swe.quest/articles/llama-3-2-in-keras-c28d5e4494723a08.md): Hugging Face · 2024-10-21 · Medium · 5 min read · Model training, LLMs, Python, Inference & serving
  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.
- [Hugging Face's TensorFlow Philosophy](https://swe.quest/articles/hugging-face-s-tensorflow-philosophy-1d3751167c605c69.md): Hugging Face · 2022-08-12 · Medium · 14 min read · Machine learning, Python, Model training, Compilers
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
