---
title: "Faster Text Generation with TensorFlow and XLA"
description: "TensorFlow 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."
url: "https://swe.quest/articles/faster-text-generation-with-tensorflow-and-xla-627c823778b1885e"
canonical: "https://huggingface.co/blog/tf-xla-generate"
source: "Hugging Face"
published: "2022-07-27"
difficulty: "medium"
kind: "How-to"
reading_minutes: 11
tags: ["inference","compilers","llms","performance","python"]
---

# Faster Text Generation with TensorFlow and XLA

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-07-27, 11 min read, How-to. Original: [https://huggingface.co/blog/tf-xla-generate](https://huggingface.co/blog/tf-xla-generate)

## Summary

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

Written by swe.quest, not by the author.

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

## Topics

- [Inference & serving](https://swe.quest/tags/inference.md) (AI & ML)
- [Compilers](https://swe.quest/tags/compilers.md) (Compilers & runtimes)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- TensorFlow basics
- transformer language models
- GPU inference ([easy posts on Hardware](https://swe.quest/tags/hardware.md?difficulty=easy))

## Keywords

XLA, TensorFlow, Hugging Face Transformers, GPT-2, tf.function, beam search

## Related posts

- [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.
- [Assisted Generation: a new direction toward low-latency text generation](https://swe.quest/articles/assisted-generation-a-new-direction-toward-low-latency-text-generation-46c4fea13508392f.md): Hugging Face · 2023-05-11 · Medium · 12 min read · Inference & serving, LLMs, Performance engineering, Python
  Assisted generation cuts autoregressive LLM latency by letting a much smaller assistant model with the same tokenizer greedily draft candidate tokens, then verifying them all in a single forward pass of the large model.
- [Make your llama generation time fly with AWS Inferentia2](https://swe.quest/articles/make-your-llama-generation-time-fly-with-aws-inferentia2-bc8b5e1981a590c0.md): Hugging Face · 2023-11-07 · Medium · 7 min read · Inference & serving, LLMs, Hardware, Cloud platforms, Performance engineering
  optimum-neuron can now compile and export Llama 2 models for text generation on AWS Inferentia2, using static input shapes (batch size, 2048 sequence length that also bounds the KV cache) and a configurable core count and fp16 precision.
- [🧨 Stable Diffusion in JAX / Flax !](https://swe.quest/articles/stable-diffusion-in-jax-flax-24ee6f1fbf14e9db.md): Hugging Face · 2022-10-13 · Medium · 9 min read · Inference & serving, Computer vision, Python, Hardware
  Hugging Face Diffusers 0.5.1 adds Flax support, enabling Stable Diffusion inference on Google TPUs.
- [🧨 Accelerating Stable Diffusion XL Inference with JAX on Cloud TPU v5e](https://swe.quest/articles/accelerating-stable-diffusion-xl-inference-with-jax-on-cloud-tpu-v5e-70ca37a9ec6835bd.md): Hugging Face · 2023-10-03 · Medium · 8 min read · Inference & serving, Computer vision, Hardware, Python, Performance engineering
  Hugging Face Diffusers can now serve Stable Diffusion XL with JAX on Cloud TPU v5e.

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)
