Skip to content

Running a diffusers pipeline

TrashPandaPipeline.from_pretrained() takes a diffusers pipeline class and a source, and returns a pipeline of that class whose large components run in stages. It needs diffusers and, for the pipelines with a transformers text encoder, transformers; neither is a dependency of trash-panda.

from diffusers import StableDiffusionXLPipeline
from trash_panda.diffusers import TrashPandaPipeline

pipe = TrashPandaPipeline.from_pretrained(StableDiffusionXLPipeline, "stabilityai/stable-diffusion-xl-base-1.0",
                                          variant="fp16", compute_dtype="bf16")
image = pipe("a weathered fisherman in a yellow raincoat on a wooden dock at sunrise").images[0]
image.save("out.png")

With no other arguments, the plan comes from the free memory of the machine. The result is an instance of a subclass of the class you passed, with the same name, so isinstance checks and the other methods of the pipeline work as before.

The source: the Hub or a folder

The second argument is either a repo id of the Hugging Face Hub ("owner/name") or a folder on disk:

# the Hub: only the files that are needed are fetched into the Hub's cache (the weights of the variant you ask for,
# the configs, the tokenizers), not the whole repo
pipe = TrashPandaPipeline.from_pretrained(StableDiffusionXLPipeline, "stabilityai/stable-diffusion-xl-base-1.0",
                                          variant="fp16")

# a folder you already have, laid out as `from_pretrained` expects (a `model_index.json` and one folder per component)
pipe = TrashPandaPipeline.from_pretrained(StableDiffusionXLPipeline, "/models/sdxl-base", variant="fp16")

A folder that exists is used as a folder. hub=True forces a repo id and hub=False forces a folder. For the Hub, revision, token (for gated models; the default is the login or HF_TOKEN), cache_dir and local_files_only (use what is in the cache already) work as in diffusers.

What is staged

The large components are the models that the pipeline's model_cpu_offload_seq names, for example the two text encoders, the UNet and the VAE of SDXL. Each is built without weights (on the meta device, with its real buffers), and loaded when the pipeline first calls it. When the next one starts, the previous one is given back, and all are given back when the call ends. The tokenizers, the scheduler and any component that is not staged load the normal way.

print(list(pipe.stages.components))     # the staged components, in order
print(pipe.stages.plans["unet"])        # where each block of the UNet lives
pipe.release()                          # give everything back now

Options

Argument Default What it is for
variant None "fp16" and so on, for the staged components and the others
compute_dtype "bf16" The dtype the staged components run in
plans None Arguments of stage() per component, for example {"unet": {"max_vram": "3GB"}} or {"transformer": {"resident_blocks": 12}}
preload {"ram": False, "vram": True} What is prepared for the next component while one runs
offload, order from model_cpu_offload_seq Which components are staged, and in what order. order is needed for a pipeline that has no model_cpu_offload_seq

Every argument, the errors and the methods of the staged pipeline are in the reference.

low_cpu_mem_usage is False unless you say otherwise, because diffusers needs accelerate for it and prints a warning without it. It only concerns what diffusers loads itself, not the staged models.

pipe = TrashPandaPipeline.from_pretrained(
    StableDiffusionXLPipeline, "/models/sdxl-base", variant="fp16",
    plans={"unet": {"max_vram": "3GB"}},                  # keep at most 3 GB of the UNet on the GPU, stream the rest
    preload={"ram": False, "vram": False},
)

Some pipelines decode the image with a VAE that is large at high resolutions. pipe.vae.enable_tiling() works as in diffusers.

What to know

  • The weights of the staged components are read from safetensors files, either one file or shards with an index, with or without a variant.
  • Components of a pipeline that has no model_cpu_offload_seq need order=.
  • pipe.to(...) moves only the components that are not staged, and warns. enable_model_cpu_offload() and enable_sequential_cpu_offload() raise an error: the staging replaces them.
  • The first run of a component writes its block order next to its weights (<name>_order.json), so later runs know it.
  • See limitations for the pipelines that are not covered yet.

What has been run

A tiny Stable Diffusion 3 pipeline from the Hub (a T5 and two CLIP text encoders, a transformer and a VAE), and Stable Diffusion XL base 1.0 with the fp16 variant at 1024 x 1024 and 20 steps, on one GPU with 16 GB of VRAM. With the UNet capped at 3 GB the run took about 14 s, and with no cap about 9 s. Other pipelines, GPUs and transformers versions are not covered yet.