dflow_2b / dflow /diffusers_pipeline.py
data-archetype's picture
dflow_2b 0.2
6daffc2
Raw History Blame Contribute Delete
4.51 kB
"""An optional 🤗 diffusers wrapper around ``DFlowPipeline``.
``diffusers`` is not a dependency of dflow; importing this module without it
raises ImportError. The wrapper gives the familiar ``DiffusionPipeline`` call
signature and output type; the model, sampler and guidance are dflow's own.
It does not support ``save_pretrained`` or loading through
``DiffusionPipeline.from_pretrained``.
Example::
from dflow.diffusers_pipeline import DFlowDiffusersPipeline
pipe = DFlowDiffusersPipeline.from_pretrained("data-archetype/dflow_2b")
image = pipe("A lighthouse on a cliff at dusk.", generator_seed=7).images[0]
"""
from __future__ import annotations
from dataclasses import replace
from pathlib import Path
import torch
from diffusers import DiffusionPipeline
from diffusers.pipelines.pipeline_utils import ImagePipelineOutput
from .guidance import CfgGuidance, ConstantPdgScale, GuidanceSettings, PdgGuidance
from .inference import GenerationSettings, SamplingSettings
from .pipeline import REPOSITORY, DFlowPipeline, OutputType
from .schedules import TimeSchedule
class DFlowDiffusersPipeline(DiffusionPipeline):
"""``DiffusionPipeline``-style access to dflow_2b.
Args:
pipeline: A loaded ``DFlowPipeline``.
"""
def __init__(self, pipeline: DFlowPipeline) -> None:
super().__init__()
self.dflow = pipeline
@classmethod
def from_pretrained( # type: ignore[override]
cls,
pretrained_model_name_or_path: str | Path = REPOSITORY,
*,
device: torch.device | str = "cuda",
compile: bool = True,
revision: str | None = None,
) -> DFlowDiffusersPipeline:
"""Load dflow_2b (see ``DFlowPipeline.from_pretrained``)."""
return cls(
DFlowPipeline.from_pretrained(
pretrained_model_name_or_path,
device=device,
compile=compile,
revision=revision,
)
)
@torch.inference_mode()
def __call__(
self,
prompt: str,
negative_prompt: str = "",
*,
height: int = 1216,
width: int = 832,
num_inference_steps: int = 50,
guidance_scale: float = 2.5,
num_images_per_prompt: int = 1,
generator_seed: int = 0,
settings: GenerationSettings | None = None,
) -> ImagePipelineOutput:
"""Generate images.
``guidance_scale`` is the path-drop guidance (PDG) scale of the
default preset. A negative prompt needs a guidance mode that reads
one (CFG or contrastive PDG), so it is accepted only together with
``settings`` whose guidance reads it; it then replaces the settings'
negative prompt. Pass ``settings`` to control everything else; it then
overrides every other argument except the prompt, the negative prompt
and the image count.
Raises:
ValueError: For a negative prompt without ``settings``, or with
settings whose guidance never reads it.
"""
chosen = settings
if chosen is not None and negative_prompt:
if not chosen.guidance.uses_negative:
raise ValueError(
"negative_prompt is set but the settings' guidance never reads "
"it; enable CFG or a contrastive PDG mode."
)
chosen = replace(chosen, negative_prompt=negative_prompt)
if chosen is None:
if negative_prompt:
raise ValueError(
"The default preset uses no negative prompt; pass settings "
"with CFG or contrastive PDG and set negative_prompt there."
)
defaults = GenerationSettings()
chosen = replace(
defaults,
width=width,
height=height,
seed=generator_seed,
sampling=replace(
SamplingSettings(),
schedule=TimeSchedule(steps=num_inference_steps),
),
guidance=GuidanceSettings(
pdg=PdgGuidance(scale=ConstantPdgScale(guidance_scale)),
cfg=CfgGuidance(),
),
)
output = self.dflow(
prompt,
chosen,
num_images=num_images_per_prompt,
output_type=OutputType.PIL,
)
return ImagePipelineOutput(images=output.images)