Repo Models & Inference Limited info

huggingface/diffusers

Hugging Face's modular PyTorch library of pretrained diffusion pipelines, schedulers and models for generating images and audio, with support for training.

  • 35k GitHub stars
  • Python
  • ⚖️ Apache-2.0
  • 🎯 Intermediate

Last verified: repo last pushed

pip install --upgrade diffusers[torch]
huggingface/diffusers preview image

What it is

🤗 Diffusers is a library of pretrained diffusion models for generating images, audio, and 3D structures of molecules. It works as a modular toolbox for simple inference or for training your own diffusion models. It offers three core components: diffusion pipelines, interchangeable noise schedulers, and pretrained models that can be used as building blocks.

Who it's for

  • Developers who want to run pretrained diffusion models in a few lines of code
  • Researchers and engineers building custom end-to-end diffusion systems from models and schedulers
  • Practitioners who want to train their own diffusion models
  • Open-source contributors interested in adding new models, pipelines or schedulers

Requirements

Requirements

  • PyTorch (the README recommends installing in a virtual environment via PyPI or Conda and points to PyTorch's official install docs)
  • A CUDA device for the quickstart examples, which call .to("cuda")

Setup

  1. Install with pip (official package)

    Recommended inside a virtual environment.

    bash
    pip install --upgrade diffusers[torch]
  2. Install with conda (community-maintained)

    Alternative installation via conda-forge.

    sh
    conda install -c conda-forge diffusers

Examples

Text-to-image with a pretrained pipeline

python
python
from diffusers import DiffusionPipeline
import torch

pipeline = DiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16)
pipeline.to("cuda")
pipeline("An image of a squirrel in Picasso style").images[0]

What it does: Loads a pretrained checkpoint with from_pretrained, moves it to the GPU, and generates an image from a text prompt.

Build your own diffusion loop with a model and scheduler

python
python
from diffusers import DDPMScheduler, UNet2DModel
from PIL import Image
import torch

scheduler = DDPMScheduler.from_pretrained("google/ddpm-cat-256")
model = UNet2DModel.from_pretrained("google/ddpm-cat-256").to("cuda")
scheduler.set_timesteps(50)

sample_size = model.config.sample_size
noise = torch.randn((1, 3, sample_size, sample_size), device="cuda")
input = noise

for t in scheduler.timesteps:
    with torch.no_grad():
        noisy_residual = model(input, t).sample
        prev_noisy_sample = scheduler.step(noisy_residual, t, input).prev_sample
        input = prev_noisy_sample

image = (input / 2 + 0.5).clamp(0, 1)
image = image.cpu().permute(0, 2, 3, 1).numpy()[0]
image = Image.fromarray((image * 255).round().astype("uint8"))
image

What it does: Combines a UNet2DModel and a DDPMScheduler in a manual denoising loop over 50 timesteps, then converts the result to a PIL image.

Pros & cons

Pros

  • Pro:Pipelines run inference in just a few lines of code
  • Pro:Interchangeable schedulers let you trade off diffusion speed and output quality
  • Pro:Models and schedulers can be combined as building blocks for custom systems
  • Pro:Supports both inference and training, with the Hub offering 30,000+ checkpoints

Cons

  • Con:The README's examples assume a CUDA GPU, and Apple Silicon use requires a separate guide
  • Con:The conda package is community-maintained rather than official
  • Con:The README states a design focus on usability over performance, so speed and memory tuning relies on separate optimization guides

Images