Diffusers pipelines¶
Run a diffusers pipeline with its large components staged one at a time. This is not exported from the top level:
import it with from trash_panda.diffusers import TrashPandaPipeline. It needs diffusers, and transformers for the
pipelines with a transformers text encoder. Neither is a dependency of trash-panda, and neither is imported until a
pipeline is built. A walk-through is in running a diffusers pipeline.
from_pretrained
classmethod
¶
from_pretrained(
pipeline_class,
source,
*,
offload=None,
order=None,
plans=None,
preload=None,
variant=None,
compute_dtype="bf16",
device=None,
hub=None,
revision=None,
token=None,
cache_dir=None,
local_files_only=False,
**kwargs,
)
Builds the pipeline in source and returns an instance of a subclass of pipeline_class. The subclass has the same name
and __module__, so isinstance checks and the rest of the pipeline's methods work as before. The staged components are
built from their configs with no weights in memory, and the other components load the normal way.
| Parameter | Default | Meaning |
|---|---|---|
pipeline_class |
the diffusers pipeline class, for example StableDiffusionXLPipeline |
|
source |
a folder on disk, or a Hugging Face Hub repo id ("owner/name"). A folder that exists is used as a folder |
|
offload |
None |
the names of the components to stage. None stages the models that model_cpu_offload_seq names and that have safetensors weights; one of them without weights is left to the normal loading, with a warning. With a list, a name that cannot be staged is an error |
order |
None |
the names in the order they run. None uses model_cpu_offload_seq. Needed when the pipeline class has none |
plans |
None |
{component: arguments of stage()}, for example {"unet": {"max_vram": "3GB"}}. See stage() and plan() |
preload |
None |
{"ram": bool, "vram": bool}, what is prepared for the next component while one runs. None is {"ram": False, "vram": True}. ram reads the next component's RAM tier into memory early; vram loads its GPU blocks early into VRAM that is free. Any other key is an error |
variant |
None |
"fp16" and so on, for the staged components and for the others |
compute_dtype |
"bf16" |
the dtype the staged components are expanded to and run in. A compute_dtype in plans overrides it for that component |
device |
None |
where the staged components run. None is CUDA if there is one, else the CPU |
hub |
None |
None decides from source; True means a repo id even if a folder of that name exists; False means a folder only |
revision |
None |
for the Hub: a branch, tag or commit |
token |
None |
for the Hub: a login token for gated models (default: the login, or HF_TOKEN) |
cache_dir |
None |
for the Hub: the cache folder (default: the Hub's own) |
local_files_only |
False |
for the Hub: use only what is in the cache already |
**kwargs |
the other arguments of the pipeline's own from_pretrained() (torch_dtype, scheduler, ...). low_cpu_mem_usage is False unless given |
pipe = TrashPandaPipeline.from_pretrained(
StableDiffusionXLPipeline, "stabilityai/stable-diffusion-xl-base-1.0", variant="fp16",
plans={"unet": {"max_vram": "3GB"}},
)
Where the files come from¶
- A folder must be laid out as a
diffuserspipeline: amodel_index.jsonand one folder per component. - A Hub repo is fetched into the Hub's cache, with the files that are needed only (the configs, the tokenizers, and
the weights of the variant that was asked for). A line says how many files are fetched. It needs
huggingface_hub. - The weights of a staged component are safetensors: one file or shards with an index, plain or of a variant.
The staged pipeline¶
The returned pipeline has these in addition to its own:
| Name | What it does |
|---|---|
pipe(...) |
runs the pipeline's call inside the stages. Each staged component loads when it is first called and is given back when the next one starts; all are given back when the call ends |
pipe.stages |
the stages of the pipeline. pipe.stages.components maps each staged name to its component, in run order; pipe.stages.plans maps a name to its plan from the latest time it ran; pipe.stages.active is the name of the component that is loaded, or None |
pipe.release() |
gives back the component that is loaded. A call of the pipeline does this when it ends; use it after calling a component or a method such as encode_prompt() outside a call |
pipe.to(...) |
moves the components that are not staged and leaves the staged ones where trash-panda puts them. It warns |
pipe.enable_model_cpu_offload(), pipe.enable_sequential_cpu_offload() |
raise a RuntimeError: the staging replaces them |
The dtype and device of a staged model answer the compute dtype and device while it runs. A staged component that is
called while another one is running (a ControlNet inside a transformer) raises a RuntimeError.
Errors and warnings¶
| Raised | When |
|---|---|
ValueError |
offload, order or plans names something that is not a component of the pipeline; offload names a component that cannot be built from a config; there is no model_cpu_offload_seq and no order; no component can be staged; preload has an unknown key; a staged component is also passed in **kwargs; source is neither a folder nor a repo id, or hub=False was given for something that is not a folder |
FileNotFoundError |
a staged component has no safetensors weights, or none of the variant that was asked for (the message lists what is there); there is no model_index.json |
OSError |
the Hub could not be reached or has no such repo. The message says to pass hub=False if source is a folder |
ImportError |
source is a repo id and huggingface_hub is not installed |
RuntimeError |
at load, if the file has no entry for a model weight ("model tensors were not found"). See the roadmap for the naming that is not handled |
UserWarning |
a model without weights is left to the normal loading, or pipe.to() was called |
See the limitations for what is not covered.