added empty gen flag
Browse files- MANUAL.md +4 -0
- comfyui/sampling.py +1 -0
- config.json +3 -0
- dflow/configuration.py +54 -1
- dflow/inference.py +25 -2
- dflow/pipeline.py +8 -2
MANUAL.md
CHANGED
|
@@ -1209,6 +1209,10 @@ toolchain and a matching PyTorch build. To check whether compilation is the
|
|
| 1209 |
cause, run eagerly: `compile=False` in Python, or `compile_model` off in DFlow
|
| 1210 |
Loader. Eager mode runs the same model at lower speed.
|
| 1211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1212 |
**Out of memory.** An 832×1216 image needs about 7 GB of VRAM at peak, and
|
| 1213 |
memory grows with the image area and the batch size. Generate one image at a
|
| 1214 |
time, reduce the size, and close other programs that hold GPU memory. In
|
|
|
|
| 1209 |
cause, run eagerly: `compile=False` in Python, or `compile_model` off in DFlow
|
| 1210 |
Loader. Eager mode runs the same model at lower speed.
|
| 1211 |
|
| 1212 |
+
**"Please enter a prompt."** Empty prompts are off in the release's
|
| 1213 |
+
`config.json`, so a forgotten prompt fails instead of generating freely. To
|
| 1214 |
+
allow them, set `"allow_empty_prompt": true` under `"inference"` there.
|
| 1215 |
+
|
| 1216 |
**Out of memory.** An 832×1216 image needs about 7 GB of VRAM at peak, and
|
| 1217 |
memory grows with the image area and the batch size. Generate one image at a
|
| 1218 |
time, reduce the size, and close other programs that hold GPU memory. In
|
comfyui/sampling.py
CHANGED
|
@@ -215,6 +215,7 @@ class DFlowGuider:
|
|
| 215 |
"the schedule node."
|
| 216 |
)
|
| 217 |
check_three_quarter_size(self.guidance, width, height)
|
|
|
|
| 218 |
conditionings = (
|
| 219 |
[self.positive]
|
| 220 |
if self.negative is None
|
|
|
|
| 215 |
"the schedule node."
|
| 216 |
)
|
| 217 |
check_three_quarter_size(self.guidance, width, height)
|
| 218 |
+
self.model.engine.check_prompts(self.positive.prompts)
|
| 219 |
conditionings = (
|
| 220 |
[self.positive]
|
| 221 |
if self.negative is None
|
config.json
CHANGED
|
@@ -26,5 +26,8 @@
|
|
| 26 |
"time_max_period": 10000.0,
|
| 27 |
"time_scale": 1000.0,
|
| 28 |
"width": 2304
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
}
|
|
|
|
| 26 |
"time_max_period": 10000.0,
|
| 27 |
"time_scale": 1000.0,
|
| 28 |
"width": 2304
|
| 29 |
+
},
|
| 30 |
+
"inference": {
|
| 31 |
+
"allow_empty_prompt": false
|
| 32 |
}
|
| 33 |
}
|
dflow/configuration.py
CHANGED
|
@@ -5,7 +5,8 @@ The denoiser has one fixed architecture. ``DFlowConfig`` holds its constants;
|
|
| 5 |
values under the ``"denoiser"`` key, and loading compares them field by field,
|
| 6 |
so a file for a different architecture fails before any weight is read.
|
| 7 |
Other top-level keys of ``config.json`` belong to other components (the text
|
| 8 |
-
adapter) and are validated by their own loaders.
|
|
|
|
| 9 |
"""
|
| 10 |
|
| 11 |
from __future__ import annotations
|
|
@@ -15,6 +16,11 @@ from dataclasses import asdict, dataclass
|
|
| 15 |
from pathlib import Path
|
| 16 |
|
| 17 |
DENOISER_CONFIG_KEY = "denoiser"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
@dataclass(frozen=True)
|
|
@@ -192,3 +198,50 @@ def read_denoiser_config(path: Path, expected: DFlowConfig) -> DFlowConfig:
|
|
| 192 |
raise ValueError(f"{path} has no {DENOISER_CONFIG_KEY!r} mapping")
|
| 193 |
case _:
|
| 194 |
raise ValueError(f"{path} must hold a JSON object")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
values under the ``"denoiser"`` key, and loading compares them field by field,
|
| 6 |
so a file for a different architecture fails before any weight is read.
|
| 7 |
Other top-level keys of ``config.json`` belong to other components (the text
|
| 8 |
+
adapter) and are validated by their own loaders. The ``"inference"`` key holds
|
| 9 |
+
the release's ``InferencePolicy``.
|
| 10 |
"""
|
| 11 |
|
| 12 |
from __future__ import annotations
|
|
|
|
| 16 |
from pathlib import Path
|
| 17 |
|
| 18 |
DENOISER_CONFIG_KEY = "denoiser"
|
| 19 |
+
INFERENCE_CONFIG_KEY = "inference"
|
| 20 |
+
EMPTY_PROMPT_ERROR = (
|
| 21 |
+
'Please enter a prompt. Empty prompts are off: set "allow_empty_prompt": '
|
| 22 |
+
'true under "inference" in config.json to allow them.'
|
| 23 |
+
)
|
| 24 |
|
| 25 |
|
| 26 |
@dataclass(frozen=True)
|
|
|
|
| 198 |
raise ValueError(f"{path} has no {DENOISER_CONFIG_KEY!r} mapping")
|
| 199 |
case _:
|
| 200 |
raise ValueError(f"{path} must hold a JSON object")
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
@dataclass(frozen=True)
|
| 204 |
+
class InferencePolicy:
|
| 205 |
+
"""Inference rules a release sets in ``config.json`` ``"inference"``.
|
| 206 |
+
|
| 207 |
+
Attributes:
|
| 208 |
+
allow_empty_prompt: Whether a blank prompt may sample (from the learned
|
| 209 |
+
unconditional text). Off in the release, so a public interface does
|
| 210 |
+
not generate freely when the prompt is forgotten; set it to true
|
| 211 |
+
locally to allow it.
|
| 212 |
+
"""
|
| 213 |
+
|
| 214 |
+
allow_empty_prompt: bool
|
| 215 |
+
|
| 216 |
+
def to_json_dict(self) -> dict[str, object]:
|
| 217 |
+
"""The ``"inference"`` mapping of ``config.json``."""
|
| 218 |
+
|
| 219 |
+
return {"allow_empty_prompt": self.allow_empty_prompt}
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
# The policy the release ships.
|
| 223 |
+
RELEASE_INFERENCE_POLICY = InferencePolicy(allow_empty_prompt=False)
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
def read_inference_policy(path: Path) -> InferencePolicy:
|
| 227 |
+
"""Read the ``"inference"`` mapping of ``config.json``.
|
| 228 |
+
|
| 229 |
+
Raises:
|
| 230 |
+
FileNotFoundError: If ``path`` does not exist.
|
| 231 |
+
ValueError: If the mapping is missing, has other keys, or
|
| 232 |
+
``allow_empty_prompt`` is not a boolean.
|
| 233 |
+
"""
|
| 234 |
+
|
| 235 |
+
if not path.is_file():
|
| 236 |
+
raise FileNotFoundError(f"Missing config: {path}")
|
| 237 |
+
payload = json.loads(path.read_text(encoding="utf-8"))
|
| 238 |
+
match payload:
|
| 239 |
+
case {"inference": {"allow_empty_prompt": bool() as allow} as policy} if (
|
| 240 |
+
len(policy) == 1
|
| 241 |
+
):
|
| 242 |
+
return InferencePolicy(allow_empty_prompt=allow)
|
| 243 |
+
case _:
|
| 244 |
+
raise ValueError(
|
| 245 |
+
f'{path} needs "{INFERENCE_CONFIG_KEY}": {{"allow_empty_prompt": '
|
| 246 |
+
"true or false} and no other keys under it."
|
| 247 |
+
)
|
dflow/inference.py
CHANGED
|
@@ -23,6 +23,7 @@ from typing import TYPE_CHECKING
|
|
| 23 |
import torch
|
| 24 |
from torch import Tensor
|
| 25 |
|
|
|
|
| 26 |
from .guidance import (
|
| 27 |
BranchCall,
|
| 28 |
BranchText,
|
|
@@ -209,13 +210,33 @@ class DFlowEngine:
|
|
| 209 |
Args:
|
| 210 |
model: The denoiser on a CUDA device.
|
| 211 |
text_encoder: The prompt encoder on the same device.
|
|
|
|
| 212 |
"""
|
| 213 |
|
| 214 |
-
def __init__(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
self.model = model
|
| 216 |
self.text_encoder = text_encoder
|
|
|
|
| 217 |
self.device = next(model.parameters()).device
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
def prepare_texts(
|
| 220 |
self,
|
| 221 |
prompts: Sequence[str],
|
|
@@ -227,9 +248,11 @@ class DFlowEngine:
|
|
| 227 |
The negative branch never takes prompt emphasis.
|
| 228 |
|
| 229 |
Raises:
|
| 230 |
-
ValueError: If
|
|
|
|
| 231 |
"""
|
| 232 |
|
|
|
|
| 233 |
positives = [
|
| 234 |
resolve_prompt(prompt, settings.width, settings.height)
|
| 235 |
for prompt in prompts
|
|
|
|
| 23 |
import torch
|
| 24 |
from torch import Tensor
|
| 25 |
|
| 26 |
+
from .configuration import EMPTY_PROMPT_ERROR, InferencePolicy
|
| 27 |
from .guidance import (
|
| 28 |
BranchCall,
|
| 29 |
BranchText,
|
|
|
|
| 210 |
Args:
|
| 211 |
model: The denoiser on a CUDA device.
|
| 212 |
text_encoder: The prompt encoder on the same device.
|
| 213 |
+
policy: The release's inference rules (``config.json`` ``"inference"``).
|
| 214 |
"""
|
| 215 |
|
| 216 |
+
def __init__(
|
| 217 |
+
self,
|
| 218 |
+
model: DFlowModel,
|
| 219 |
+
text_encoder: DFlowTextEncoder,
|
| 220 |
+
*,
|
| 221 |
+
policy: InferencePolicy,
|
| 222 |
+
) -> None:
|
| 223 |
self.model = model
|
| 224 |
self.text_encoder = text_encoder
|
| 225 |
+
self.policy = policy
|
| 226 |
self.device = next(model.parameters()).device
|
| 227 |
|
| 228 |
+
def check_prompts(self, prompts: Sequence[str]) -> None:
|
| 229 |
+
"""Reject blank prompts unless the policy allows them.
|
| 230 |
+
|
| 231 |
+
Raises:
|
| 232 |
+
ValueError: ``EMPTY_PROMPT_ERROR`` for a blank prompt.
|
| 233 |
+
"""
|
| 234 |
+
|
| 235 |
+
if not self.policy.allow_empty_prompt and any(
|
| 236 |
+
not prompt.strip() for prompt in prompts
|
| 237 |
+
):
|
| 238 |
+
raise ValueError(EMPTY_PROMPT_ERROR)
|
| 239 |
+
|
| 240 |
def prepare_texts(
|
| 241 |
self,
|
| 242 |
prompts: Sequence[str],
|
|
|
|
| 248 |
The negative branch never takes prompt emphasis.
|
| 249 |
|
| 250 |
Raises:
|
| 251 |
+
ValueError: If a prompt is blank while the policy forbids it, or the
|
| 252 |
+
positive prompts do not share one token count.
|
| 253 |
"""
|
| 254 |
|
| 255 |
+
self.check_prompts(prompts)
|
| 256 |
positives = [
|
| 257 |
resolve_prompt(prompt, settings.width, settings.height)
|
| 258 |
for prompt in prompts
|
dflow/pipeline.py
CHANGED
|
@@ -13,13 +13,14 @@ from huggingface_hub import snapshot_download
|
|
| 13 |
from PIL import Image, PngImagePlugin
|
| 14 |
from torch import Tensor
|
| 15 |
|
|
|
|
| 16 |
from .inference import (
|
| 17 |
DFlowEngine,
|
| 18 |
GenerationSettings,
|
| 19 |
ImageToImage,
|
| 20 |
LatentCallback,
|
| 21 |
)
|
| 22 |
-
from .loading import load_dflow_denoiser, load_text_adapter_tensors
|
| 23 |
from .preview import preview_images
|
| 24 |
from .text_conditioning import DFlowTextEncoder, TextAdapter
|
| 25 |
from .vae import DFlowVae
|
|
@@ -134,7 +135,12 @@ class DFlowComponents:
|
|
| 134 |
vae = DFlowVae.from_pretrained(
|
| 135 |
device=target, compile=compile, local_dir=vae_dir, cache_dir=cache_dir
|
| 136 |
)
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
|
| 140 |
class OutputType(Enum):
|
|
|
|
| 13 |
from PIL import Image, PngImagePlugin
|
| 14 |
from torch import Tensor
|
| 15 |
|
| 16 |
+
from .configuration import read_inference_policy
|
| 17 |
from .inference import (
|
| 18 |
DFlowEngine,
|
| 19 |
GenerationSettings,
|
| 20 |
ImageToImage,
|
| 21 |
LatentCallback,
|
| 22 |
)
|
| 23 |
+
from .loading import CONFIG_FILENAME, load_dflow_denoiser, load_text_adapter_tensors
|
| 24 |
from .preview import preview_images
|
| 25 |
from .text_conditioning import DFlowTextEncoder, TextAdapter
|
| 26 |
from .vae import DFlowVae
|
|
|
|
| 135 |
vae = DFlowVae.from_pretrained(
|
| 136 |
device=target, compile=compile, local_dir=vae_dir, cache_dir=cache_dir
|
| 137 |
)
|
| 138 |
+
policy = read_inference_policy(directory / CONFIG_FILENAME)
|
| 139 |
+
return cls(
|
| 140 |
+
engine=DFlowEngine(model, text_encoder, policy=policy),
|
| 141 |
+
vae=vae,
|
| 142 |
+
compiled=compile,
|
| 143 |
+
)
|
| 144 |
|
| 145 |
|
| 146 |
class OutputType(Enum):
|