File size: 4,917 Bytes
6daffc2 0957ad9 6daffc2 0957ad9 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | # 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)
|