v4's Chain Sampler became a hub: 8183 lines, roughly seventy optional inputs, a _sample_chunk that grew six ALG arguments, and a hardcoded list of knobs silently inert on one model family. Every technique that needed the loop was added to it, so it collected all of them. This one picks an algorithm and a schedule from ComfyUI's own registries -- so a sampler it gains appears here without an edit -- and runs them. Three things stop it becoming a hub again. It never learns what a model-side modifier is. Sampler-side ones reach it through ONE call site, chain.process(ctx, latent), which cannot grow because the step travels in a context object rather than in the signature. And it declares the hook points it offers, so core hands it only what fits and it never names a modifier. Some modifiers genuinely cannot ride on a model: they need the step index, the schedule, or a second pass. Those attach here instead, and one asking for a hook this sampler does not offer is absent and said so, not half-running. Verified against ComfyUI's own guider path with real weights: bit-identical output. That took correcting the method, not the code -- see below. MEASUREMENT, corrected: sampling on this Mac's MPS backend is NONDETERMINISTIC. The same node with the same seed and the same inputs differs run to run: 2.42 by max latent difference, 0.0008 by mean pixel gap. Comparing by MAX said this sampler diverged from core by 14.2 and it was about to be "fixed"; by MEAN against a measured noise floor it is 0.00000 while core against itself is 0.00183. Every comparison here now runs a control twice first. Re-checked the Detail modifier the same way: 145x the floor, so that claim stands. Also matched what core's sampler actually does and this did not: fix_empty_latent_channels using the ratios our own empty-latent node reports, the preview and progress callback, the move back to the intermediate device, and dropping the ratio keys so nothing rescales twice. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
135 lines
5.0 KiB
Python
135 lines
5.0 KiB
Python
"""Modifiers that belong to the sampler rather than to the model.
|
|
|
|
Some cannot ride on a ModelPatcher and it is not a limitation of the plumbing:
|
|
they need the step index, the schedule, or the chance to run the whole thing
|
|
twice. A model patch has none of those.
|
|
|
|
So a sampler offers HOOK POINTS by name and core hands it the modifiers that
|
|
asked for one it offers. Two properties fall out, and both are the point:
|
|
|
|
* A sampler file never names a modifier. Five samplers and six modifiers stay
|
|
zero hand-written call sites rather than thirty.
|
|
* The sampler keeps the veto -- it says what it can host -- and core keeps the
|
|
plumbing. A modifier wanting a hook this sampler does not offer is absent, and
|
|
said, rather than half-working.
|
|
|
|
The call site is deliberately ONE, and shaped so it cannot grow:
|
|
|
|
latent = chain.process(ctx, latent)
|
|
|
|
`ctx` carries the step, not the signature. v4 passed each technique's parameters
|
|
down as keyword arguments and `_sample_chunk` ended up with six ALG arguments
|
|
before anyone noticed the shape was wrong.
|
|
"""
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Any, List, Optional, Sequence, Tuple
|
|
|
|
from . import log, patching
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Step:
|
|
"""Everything a per-step modifier is allowed to know about where it is.
|
|
|
|
A small closed object rather than loose arguments: adding a field here is
|
|
one edit, whereas widening a call signature is an edit in every sampler and
|
|
every modifier that ever passed it along.
|
|
"""
|
|
|
|
index: int
|
|
sigma: Any
|
|
sigmas: Any
|
|
total: int
|
|
run: Optional[str] = None
|
|
anchor: Any = None # the pinned latent, where there is one
|
|
|
|
|
|
class Chain:
|
|
"""The modifiers a sampler is actually running, in order."""
|
|
|
|
def __init__(self, members: Sequence[Tuple[str, Any]], dropped: patching.Dropped):
|
|
self.members = list(members)
|
|
self.dropped = dropped
|
|
|
|
def __bool__(self) -> bool:
|
|
return bool(self.members)
|
|
|
|
@property
|
|
def ids(self) -> List[str]:
|
|
return [name for name, _ in self.members]
|
|
|
|
def process(self, ctx: Step, latent):
|
|
"""Every member that wants this step, in declared order.
|
|
|
|
A member that raises is dropped for the rest of the run and the run
|
|
continues -- the same rule the model-side hooks follow, for the same
|
|
reason: a modifier is an opinion about the picture and is never worth
|
|
the picture.
|
|
"""
|
|
for name, member in self.members:
|
|
key = f"funpack.{name}"
|
|
if key in self.dropped:
|
|
continue
|
|
try:
|
|
if not member.active(ctx):
|
|
continue
|
|
changed = member.process(ctx, latent)
|
|
except Exception as exc: # noqa: BLE001
|
|
if self.dropped.record(key, exc):
|
|
import traceback
|
|
log.warning(key, "failed during sampling and is now OFF for the "
|
|
"rest of this run; the run continues without it\n"
|
|
+ "".join(traceback.format_exception(exc)).rstrip())
|
|
continue
|
|
if changed is not None:
|
|
latent = changed
|
|
return latent
|
|
|
|
|
|
def build(specs, values, accepts: Sequence[str], dropped: patching.Dropped,
|
|
capability: str = "sampler_modifier") -> Tuple[Chain, List[str]]:
|
|
"""(chain, notes) for the modifiers this sampler can host.
|
|
|
|
`specs` are already filtered by trait and ordered by relation -- this only
|
|
decides who fits THIS sampler, and builds them.
|
|
"""
|
|
offered = set(accepts or ())
|
|
members, notes = [], []
|
|
|
|
for spec in specs:
|
|
make = spec.provides.get(capability)
|
|
if make is None:
|
|
continue
|
|
|
|
wanted = tuple(getattr(spec, "hooks", ()) or ())
|
|
missing = [hook for hook in wanted if hook not in offered]
|
|
if missing:
|
|
# Absent rather than half-working: a modifier that needs a second
|
|
# pass cannot be usefully run by a sampler that makes one.
|
|
notes.append(f"{spec.id}: needs {', '.join(missing)}, which this sampler "
|
|
f"does not offer")
|
|
continue
|
|
|
|
try:
|
|
member = make(values.get(spec.id) or {}, tuple(offered))
|
|
except Exception as exc: # noqa: BLE001
|
|
log.failed(f"{spec.id}.{capability}", exc)
|
|
notes.append(f"{spec.id}: failed to start -- {type(exc).__name__}: {exc}")
|
|
continue
|
|
|
|
if member is None:
|
|
continue # the module decided it is off
|
|
for required in ("active", "process"):
|
|
if not callable(getattr(member, required, None)):
|
|
notes.append(f"{spec.id}: is not a sampler modifier (no {required}())")
|
|
member = None
|
|
break
|
|
if member is None:
|
|
continue
|
|
|
|
members.append((spec.id, member))
|
|
notes.append(f"{spec.id}: on")
|
|
|
|
return Chain(members, dropped), notes
|