Skip to content

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

Hugging Face 11 min read How-to mediumAdept

Faster Text Generation with TensorFlow and XLA

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.

Assumes: TensorFlow basicstransformer language modelsGPU inference

Related posts

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

  • Assisted Generation: a new direction toward low-latency text generation

    Hugging Face mediumAdept

    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. The large model keeps matching tokens plus the first divergent one. The draft length adapts: it grows by 2 when every candidate matches and shrinks by 1 otherwise. The approach exploits the fact that forward passes are memory-bandwidth bound. In 🤗 Transformers it is exposed through an `assistant_model` argument to `generate` (batch size 1 only) and yields up to 2x speedups, 3x with INT8, and up to 10x when the model relies on memory offloading. It works best on input-grounded tasks like ASR and summarization, and with sampling only at low temperatures.

  • Make your llama generation time fly with AWS Inferentia2

    Hugging Face mediumAdept

    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. Compiled models can be saved or pushed to the Hub and used via transformers-style generate() or optimum-neuron pipelines, supporting greedy search and top-k/top-p sampling. Benchmarks on inf2.48xlarge show Llama 2 7B encoding 256 tokens in about 0.5s, with throughput up to 750 tokens/s at batch size 4, while a 2-core 'budget' config on inf2.xlarge manages about 22–32 tokens/s. Latency is fairly insensitive to batch size, but throughput is capped by device memory and the static sequence length limits long contexts.

  • 🧨 Stable Diffusion in JAX / Flax !

    Hugging Face mediumAdept

    Hugging Face Diffusers 0.5.1 adds Flax support, enabling Stable Diffusion inference on Google TPUs. The walkthrough loads bf16 weights with FlaxStableDiffusionPipeline, then replicates parameters and shards prompts across 8 TPU devices to generate 8 images in parallel. It also covers PRNG key splitting for reproducibility and why input shapes must stay fixed to avoid recompilation. On a TPU v2-8, the first JIT compile takes over a minute, after which a batch of 8 images takes about 7 seconds. It closes by showing how jax.pmap compiles the generation function and runs it SPMD-style on each device.

  • 🧨 Accelerating Stable Diffusion XL Inference with JAX on Cloud TPU v5e

    Hugging Face mediumAdept

    Hugging Face Diffusers can now serve Stable Diffusion XL with JAX on Cloud TPU v5e. JIT compilation over static shapes plus pmap data parallelism lets a v5e-4 produce four 1024×1024 images in about 2.3s of generation time, after a one-time compile of roughly three minutes. A step-by-step pipeline converts parameters to bfloat16 while keeping scheduler state in float32 to avoid degraded or black images, then replicates inputs with per-device RNG seeds. Benchmarks show TPU v5e delivering up to 2.4x better performance per dollar than TPU v4 at similar latency.

The quest for software engineering knowledge.

Look:
Layout: