Download comfyui/preview.py from data-archetype/dflow_2b: direct link, hf CLI and curl.
- Browser
- Download file 3.12 kB
-
https://huggingface.co/data-archetype/dflow_2b/resolve/main/comfyui/preview.py
- Command line
-
hf download hf://data-archetype/dflow_2b/comfyui/preview.py
-
curl -L -o preview.py https://huggingface.co/data-archetype/dflow_2b/resolve/main/comfyui/preview.py
3.12 kB
| # 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 | |
| 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)) | |
| ) | |