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>
141 lines
5.1 KiB
Python
141 lines
5.1 KiB
Python
"""Finding modules.
|
|
|
|
Core scans a folder and imports what it finds. It holds no list, so adding a
|
|
module is adding a folder; and each import is guarded, so a module that raises
|
|
is absent and logged rather than taking the app down with it.
|
|
"""
|
|
|
|
import importlib
|
|
import importlib.util
|
|
import pkgutil
|
|
from pathlib import Path
|
|
from typing import Dict, List, Optional, Tuple
|
|
|
|
from . import config, log
|
|
from .contract import ModuleSpec
|
|
from .schema import SchemaError, validate
|
|
|
|
# Read off the module after import. Uppercase because they are declarations, not
|
|
# behaviour -- a module's announcement should be legible without running it.
|
|
FIELDS = {
|
|
"id": "ID", "title": "TITLE", "mount": "MOUNT", "settings": "SETTINGS",
|
|
"requires": "REQUIRES", "after": "AFTER", "before": "BEFORE",
|
|
"stage": "STAGE", "status": "STATUS", "nodes": "NODES", "traits": "TRAITS", "provides": "PROVIDES", "hooks": "HOOKS",
|
|
}
|
|
|
|
|
|
class Registry:
|
|
def __init__(self) -> None:
|
|
self.specs: Dict[str, ModuleSpec] = {}
|
|
self.failed: List[Tuple[str, str]] = [] # (where, why)
|
|
|
|
@property
|
|
def ids(self) -> List[str]:
|
|
return sorted(self.specs)
|
|
|
|
def providers(self, capability: str) -> List[Tuple[ModuleSpec, object]]:
|
|
"""Every module offering this capability, in a stable order.
|
|
|
|
Sorted by id so which provider answers first is a property of the tree
|
|
rather than of filesystem order.
|
|
"""
|
|
found = []
|
|
for spec in sorted(self.specs.values(), key=lambda s: s.id):
|
|
fn = spec.provides.get(capability)
|
|
if fn is not None:
|
|
found.append((spec, fn))
|
|
return found
|
|
|
|
def add(self, spec: ModuleSpec) -> None:
|
|
if spec.id in self.specs:
|
|
# Two modules answering to one id means one wins by import order,
|
|
# and which one is nobody's decision.
|
|
self.failed.append((spec.source, f"duplicate id {spec.id!r}, already declared by "
|
|
f"{self.specs[spec.id].source}"))
|
|
return
|
|
self.specs[spec.id] = spec
|
|
|
|
|
|
def announcement_of(module, source: str) -> dict:
|
|
raw = {}
|
|
for key, const in FIELDS.items():
|
|
if hasattr(module, const):
|
|
raw[key] = getattr(module, const)
|
|
return raw
|
|
|
|
|
|
def _feature_dirs(root: Path) -> List[Path]:
|
|
"""modules/<domain>/<feature>/ -- two levels, each a package."""
|
|
if not root.is_dir():
|
|
return []
|
|
found = []
|
|
for domain in sorted(p for p in root.iterdir() if p.is_dir() and not p.name.startswith((".", "_"))):
|
|
for feature in sorted(p for p in domain.iterdir() if p.is_dir() and not p.name.startswith((".", "_"))):
|
|
if (feature / "__init__.py").is_file():
|
|
found.append(feature)
|
|
return found
|
|
|
|
|
|
def package_root() -> str:
|
|
"""The dotted prefix `modules` lives under, or "" when it is top-level.
|
|
|
|
ComfyUI imports a pack with `spec_from_file_location(<full path>, ...)`, so
|
|
the pack's own name is that path string and its subpackages are only
|
|
reachable beneath it -- an absolute `import modules` finds nothing. Under the
|
|
dev server and the tests the repo root IS on sys.path, so the prefix is empty.
|
|
Deriving it means one code path works in both.
|
|
"""
|
|
parent = (__package__ or "").rsplit(".", 1)
|
|
return parent[0] if len(parent) == 2 else ""
|
|
|
|
|
|
def scan(root: Optional[Path] = None, package: Optional[str] = None) -> Registry:
|
|
"""Import every module under `root` and collect the valid announcements."""
|
|
if package is None:
|
|
prefix = package_root()
|
|
package = f"{prefix}.modules" if prefix else "modules"
|
|
root = Path(root or config.MODULES_DIR)
|
|
registry = Registry()
|
|
|
|
for feature in _feature_dirs(root):
|
|
rel = feature.relative_to(root)
|
|
dotted = f"{package}." + ".".join(rel.parts)
|
|
try:
|
|
module = importlib.import_module(dotted)
|
|
except Exception as exc: # noqa: BLE001 -- any failure is absence
|
|
log.failed(dotted, exc)
|
|
registry.failed.append((dotted, f"{type(exc).__name__}: {exc}"))
|
|
continue
|
|
|
|
raw = announcement_of(module, dotted)
|
|
if not raw.get("id"):
|
|
# A folder that is not a module is not an error -- shared helpers
|
|
# live in packages too.
|
|
continue
|
|
|
|
# A module's ui.js is optional, and the default is none: most modules
|
|
# are a settings schema and no JavaScript at all.
|
|
ui_file = feature / "ui.js"
|
|
if ui_file.is_file():
|
|
raw["ui"] = f"{config.UI_PREFIX}/modules/{'/'.join(rel.parts)}/ui.js"
|
|
|
|
try:
|
|
registry.add(validate(raw, source=dotted))
|
|
except SchemaError as exc:
|
|
log.warning(dotted, f"is not a valid module and was not loaded -- {exc}")
|
|
registry.failed.append((dotted, str(exc)))
|
|
|
|
return registry
|
|
|
|
|
|
# Scanned once. A module list that changed under a running graph would mean two
|
|
# nodes in one run disagreeing about what exists.
|
|
_current: Optional[Registry] = None
|
|
|
|
|
|
def current(rescan: bool = False) -> Registry:
|
|
global _current
|
|
if _current is None or rescan:
|
|
_current = scan()
|
|
return _current
|