File size: 3,124 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
# SPDX-License-Identifier: GPL-3.0-only
# Copyright 2026 Data Archetype
"""Live sampling previews and the minimal model surface ComfyUI's custom
sampler node reads from a guider.

ComfyUI's ``Latent2RGBPreviewer`` applies a per-pixel linear map after an
optional reshape hook. dflow's preview is a 3x3 convolution to 2x2 RGB cells,
so the hook runs that projection and the linear map is the identity.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from comfy.latent_formats import LatentFormat

from ..dflow.vae import LATENT_CHANNELS, LATENT_STRIDE

if TYPE_CHECKING:
    import torch
    from torch import Tensor

    from ..dflow.preview import LatentRgbPreview

_RGB_IDENTITY = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]


class DFlowLatentFormat(LatentFormat):
    """dflow's latent format: 96 whitened channels at stride 16, unscaled."""

    latent_channels = LATENT_CHANNELS
    spacial_downscale_ratio = LATENT_STRIDE
    scale_factor = 1.0
    latent_rgb_factors = _RGB_IDENTITY

    def __init__(self, preview: LatentRgbPreview) -> None:
        """Use ``preview`` (on the sampling device) for live previews."""

        self.preview = preview
        self.latent_rgb_factors_reshape = self.project_first

    def project_first(self, latents: Tensor) -> Tensor:
        """RGB ``[1, 3, 2h, 2w]`` in ``[-1, 1]`` of the batch's first latent
        (the previewer shows only that one)."""

        return self.preview.project(latents[:1])

    def process_in(self, latent: Tensor) -> Tensor:
        """dflow latents enter the model unchanged."""

        return latent

    def process_out(self, latent: Tensor) -> Tensor:
        """dflow latents leave the model unchanged."""

        return latent


class DFlowPreviewModel:
    """The ``model_patcher.model`` attributes the custom sampler reads."""

    def __init__(self, latent_format: DFlowLatentFormat) -> None:
        self.latent_format = latent_format

    @staticmethod
    def process_latent_out(latents: Tensor) -> Tensor:
        """Denoised latents are already in dflow's latent space."""

        return latents


class DFlowModelPatcher:
    """The ``guider.model_patcher`` surface ``SamplerCustomAdvanced`` uses:
    the device, the latent format and the preview model."""

    def __init__(self, *, device: torch.device, model: DFlowPreviewModel) -> None:
        self.load_device = device
        self.model = model

    def get_model_object(self, name: str) -> DFlowLatentFormat:
        """Return the latent format (the only object dflow exposes).

        Raises:
            KeyError: For any other name.
        """

        match name:
            case "latent_format":
                return self.model.latent_format
            case _:
                raise KeyError(f"dflow does not expose the model object {name!r}.")


def build_model_patcher(
    preview: LatentRgbPreview, *, device: torch.device
) -> DFlowModelPatcher:
    """The preview-only model patcher of a dflow guider."""

    return DFlowModelPatcher(
        device=device, model=DFlowPreviewModel(DFlowLatentFormat(preview))
    )