dflow_2b / comfyui /types.py
data-archetype's picture
dflow_2b 0.215: MOSS layout support
0957ad9 verified
Raw History Blame Contribute Delete
4.92 kB
# SPDX-License-Identifier: GPL-3.0-only
# Copyright 2026 Data Archetype
"""Values passed between the dflow ComfyUI nodes.
Every handle is a frozen dataclass around ``dflow`` objects; the nodes never
hand ComfyUI anything untyped except the latent mapping ComfyUI itself
defines.
"""
from __future__ import annotations
import math
from dataclasses import dataclass, replace
from enum import StrEnum
from typing import TYPE_CHECKING, TypedDict
if TYPE_CHECKING:
import torch
from torch import Tensor
from ..dflow.inference import DFlowEngine
from ..dflow.modeling import PreparedText
from ..dflow.pipeline import DFlowComponents
from ..dflow.preview import LatentRgbPreview
from ..dflow.prompt_emphasis import EmphasisSettings
from ..dflow.schedules import TimeSchedule
from ..dflow.solvers import Solver
class DFlowWeightSource(StrEnum):
"""Where the loader node reads the release from.
BUNDLED: the node folder itself (the Hugging Face repository checkout).
LOCAL_DIR: another local release directory.
HUB: a Hugging Face repository at a revision.
"""
BUNDLED = "bundled"
LOCAL_DIR = "local_dir"
HUB = "hub"
class PdgCurveChoice(StrEnum):
"""PDG scale schedules offered by the advanced guider node.
CONSTANT: the guider's constant PDG scale.
RAMP: linear in the schedule quantile from a start to an end scale.
LINEAR, LINEAR_LOGSNR, POWER, S_CURVE: per-step endpoint curves.
BEZIER: the cubic Bezier curve over flow time.
"""
CONSTANT = "constant"
RAMP = "ramp"
LINEAR = "linear"
LINEAR_LOGSNR = "linear_logsnr"
POWER = "power"
S_CURVE = "s_curve"
BEZIER = "bezier"
class DFlowLatent(TypedDict):
"""ComfyUI's latent mapping for dflow latents ``[B, 96, H/16, W/16]``."""
samples: Tensor
downscale_ratio_spacial: int
@dataclass(frozen=True)
class DFlowModelHandle:
"""The loaded release and the preview projection on its device.
Attributes:
components: Denoiser, text encoder and autoencoder.
preview: Latent-to-RGB preview projection on the model's device.
"""
components: DFlowComponents
preview: LatentRgbPreview
@property
def engine(self) -> DFlowEngine:
"""The sampling engine (denoiser and text encoder)."""
return self.components.engine
@property
def device(self) -> torch.device:
"""The CUDA device the model runs on."""
return self.components.engine.device
@dataclass(frozen=True)
class DFlowTextEncoderHandle:
"""The text-encoder output of the loader; encodes for one model."""
model: DFlowModelHandle
@dataclass(frozen=True)
class DFlowConditioning:
"""An encoded and refined prompt batch.
Attributes:
model: The model that encoded it (guiders reject other models').
prompts: The texts the encoder read, one per batch row (layouts in
their compiled form).
text: The refined text tokens.
emphasis: The prompt-emphasis settings used, or None for verbatim.
layout_size: The ``(width, height)`` DFlow Layout Encode compiled the
layout for (the guider requires the latent's size), or None.
"""
model: DFlowModelHandle
prompts: tuple[str, ...]
text: PreparedText
emphasis: EmphasisSettings | None
layout_size: tuple[int, int] | None
@property
def batch(self) -> int:
"""Number of prompts (latents) in the batch."""
return len(self.prompts)
@dataclass(frozen=True)
class ScheduleBinding:
"""The schedule a SIGMAS tensor was built from, as guidance needs it.
The quantile ramp and the linear-logSNR curve read the schedule family and
the shift; the size check ties the shift to the latent being sampled.
Attributes:
schedule: Family and beta parameters (``steps`` is the full count).
log_snr_shift: Effective shift the schedule was built with.
width: Image width the shift was computed for.
height: Image height the shift was computed for.
"""
schedule: TimeSchedule
log_snr_shift: float
width: int
height: int
@dataclass(frozen=True)
class DFlowSamplerSpec:
"""The SAMPLER value: solver, noise scale and, once a dflow schedule node
has seen it, the schedule it is paired with.
Raises:
ValueError: For a negative or non-finite noise scale.
"""
solver: Solver
noise_scale: float
binding: ScheduleBinding | None = None
def __post_init__(self) -> None:
"""Validate the noise scale."""
if not math.isfinite(self.noise_scale) or self.noise_scale < 0.0:
raise ValueError(f"noise_scale must be >= 0, got {self.noise_scale}.")
def bound(self, binding: ScheduleBinding) -> DFlowSamplerSpec:
"""Return this sampler paired with a schedule."""
return replace(self, binding=binding)