File size: 4,513 Bytes
6daffc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""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)