---
title: "🧨 Stable Diffusion in JAX / Flax !"
description: "Hugging Face Diffusers 0.5.1 adds Flax support, enabling Stable Diffusion inference on Google TPUs."
url: "https://swe.quest/articles/stable-diffusion-in-jax-flax-24ee6f1fbf14e9db"
canonical: "https://huggingface.co/blog/stable_diffusion_jax"
source: "Hugging Face"
published: "2022-10-13"
difficulty: "medium"
kind: "How-to"
reading_minutes: 9
tags: ["inference","computer-vision","python","hardware"]
---

# 🧨 Stable Diffusion in JAX / Flax !

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-10-13, 9 min read, How-to. Original: [https://huggingface.co/blog/stable\_diffusion\_jax](https://huggingface.co/blog/stable_diffusion_jax)

## Summary

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.

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)
- [Computer vision](https://swe.quest/tags/computer-vision.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)
- [Hardware](https://swe.quest/tags/hardware.md) (Infrastructure)

## Prerequisites

- Python ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))
- basic JAX
- diffusion models basics

## Keywords

JAX, Flax, Stable Diffusion, Diffusers, TPU, pmap, bfloat16

## Related posts

- [🧨 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.
- [Fine-tuning Stable Diffusion models on Intel CPUs](https://swe.quest/articles/fine-tuning-stable-diffusion-models-on-intel-cpus-a7b97eec2af1f8ba.md): Hugging Face · 2023-07-14 · Medium · 7 min read · Model training, Hardware, Inference & serving, Computer vision
  Stable Diffusion v1.5 can be fine-tuned with textual inversion on a four-node Intel Sapphire Rapids Xeon cluster, using AMX acceleration via Intel Extension for PyTorch, oneCCL for distributed communication, and Accelerate launched under mpirun with 16 tasks.
- [Accelerating Stable Diffusion Inference on Intel CPUs](https://swe.quest/articles/accelerating-stable-diffusion-inference-on-intel-cpus-b246e61d473debb6.md): Hugging Face · 2023-03-28 · Medium · 6 min read · Inference & serving, Performance engineering, Hardware, Python
  Stable Diffusion v1.5 inference on Sapphire Rapids Xeon CPUs (EC2 r7iz) can be cut from a 32.3s baseline to a few seconds per image with a stack of optimizations.
- [Diffusers welcomes Stable Diffusion 3.5 Large](https://swe.quest/articles/diffusers-welcomes-stable-diffusion-3-5-large-23c0b39797ac94ee.md): Hugging Face · 2024-10-22 · Medium · 6 min read · Computer vision, Inference & serving, Model training, Python
  Stable Diffusion 3.5 Large (8B) and a timestep-distilled Turbo variant are now usable in Diffusers.
- [Diffusers welcomes Stable Diffusion 3](https://swe.quest/articles/diffusers-welcomes-stable-diffusion-3-f6ced777e2fcbd47.md): Hugging Face · 2024-06-12 · Medium · 7 min read · Computer vision, Inference & serving, Model training, Python
  Stable Diffusion 3 Medium (2B parameters) is now usable through Diffusers.

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)
