Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

feat(scripts): scaffold a new architecture, and derive what cannot be scaffolded

Alexander Eichhorn75877289e4be
diff --git a/docs/src/content/docs/contributing/new-model-integration.mdx b/docs/src/content/docs/contributing/new-model-integration.mdx
index f3d700bf48..ce8d287de1 100644
--- a/docs/src/content/docs/contributing/new-model-integration.mdx
+++ b/docs/src/content/docs/contributing/new-model-integration.mdx
@@ -12,6 +12,24 @@ This guide describes all the steps required to integrate a new model type into I
The code examples use a hypothetical `NewModel` architecture. The implementations of FLUX.1, FLUX.2 Klein, SD3, SDXL, and Z-Image in the InvokeAI codebase serve as excellent real-world references.
:::
+:::tip[Start with the scaffolder]
+Add the `BaseModelType` member first (step 1 below), then run:
+
+```sh
+python scripts/new_architecture.py --base newmodel --name NewModel --write
+```
+
+It writes the three files that are mechanical — the architecture's declaration under
+`invokeai/backend/architectures/defs/`, its invocation package, and its starter-model module — and
+then prints everything it *cannot* write, which is most of this guide. That list is derived from the
+codebase on each run rather than kept in the script, so it does not go stale.
+
+The generated declaration deliberately does not work. It carries every required facet with an
+obviously wrong value, so the app refuses to boot until each one is filled in. A stub that booted
+would let a half-integrated architecture reach a user, which is the failure this whole structure
+exists to prevent.
+:::
+
---
## 1. Backend: Model Manager
diff --git a/scripts/new_architecture.py b/scripts/new_architecture.py
new file mode 100644
index 0000000000..a13dd2bd9f
--- /dev/null
+++ b/scripts/new_architecture.py
@@ -0,0 +1,204 @@
+"""Scaffold the files a new model architecture needs, and list the ones it cannot scaffold.
+
+ python scripts/new_architecture.py --base new-model --name NewModel # dry run
+ python scripts/new_architecture.py --base new-model --name NewModel --write
+
+Three files can be generated, because the registry made them mechanical: the architecture's
+declaration under `architectures/defs/`, its invocation package, and its starter-model module. The
+declaration is generated with every required facet present but obviously wrong, so it fails loudly
+at boot until someone fills it in — a stub that boots would be worse than no stub.
+
+The rest cannot be generated, and the point of this script is as much to enumerate that rest as to
+write the three. That list is *derived* on each run rather than written down here: any module naming
+five or more `BaseModelType` members is dispatching on base, so a new one has to be added to it by
+hand. A hardcoded list would be wrong the first time someone removed a dispatch — which is what the
+last several changes have been doing.
+"""
+
+import argparse
+import ast
+import sys
+from pathlib import Path
+
+REPO_ROOT = Path(__file__).resolve().parents[1]
+SOURCE_ROOT = REPO_ROOT / "invokeai"
+
+# Excluded from the derived list: the registry is the mechanism, not the cost, and the starter
+# package is scaffolded below.
+NOT_A_COST = (
+ "invokeai/frontend",
+ "invokeai/backend/architectures/",
+ "invokeai/backend/model_manager/starter_models/",
+)
+
+# Files a derivation cannot find, because the work there is to *add* something rather than to extend
+# an existing dispatch. Kept short and specific; each says what to do, not just where.
+UNDERIVABLE = [
+ (
+ "invokeai/backend/model_manager/taxonomy.py",
+ "Add the `BaseModelType` member. If the architecture has variants, add its enum and list it "
+ "in `AnyVariant` and `variant_type_adapter`.",
+ ),
+ (
+ "invokeai/backend/stable_diffusion/diffusion/conditioning_data.py",
+ "Define the `*ConditioningInfo` its text encoder produces, unless it reuses an existing one.",
+ ),
+ (
+ "invokeai/app/invocations/metadata.py",
+ "Add the mode strings to `GENERATION_MODES`. They must match what the new `ModalityFacet` "
+ "declares — a test compares the two — and they are persisted in image metadata, so they "
+ "cannot be changed later.",
+ ),
+]
+
+
+def defs_module(base_value: str, enum_name: str) -> str:
+ """The architecture's declaration. Every required facet, none of them plausible."""
+ return f'''"""What the {base_value} architecture declares."""
+
+from invokeai.backend.architectures.facets.conditioning import ConditioningFacet
+from invokeai.backend.architectures.facets.default_settings import DefaultSettingsFacet
+from invokeai.backend.architectures.facets.features import FeaturesFacet, NegativePrompt
+from invokeai.backend.architectures.facets.latent_space import LatentSpace, LatentSpaceFacet
+from invokeai.backend.architectures.facets.modality import ModalityFacet
+from invokeai.backend.architectures.registry import register
+from invokeai.backend.model_manager.configs.default_settings import MainModelDefaultSettings
+from invokeai.backend.model_manager.taxonomy import BaseModelType
+from invokeai.backend.stable_diffusion.diffusion.conditioning_data import BasicConditioningInfo
+
+# TODO: every value below is a placeholder. The registry refuses to boot without these facets, but
+# it cannot tell whether they are true — replace each one before generating anything.
+
+# TODO: the latent -> RGB projection for this VAE, one row per latent channel. If the architecture
+# reuses another's VAE, import that LatentSpace instead of declaring a second copy of its matrix.
+{enum_name.upper()}_LATENT_RGB_FACTORS = [
+ [0.0, 0.0, 0.0],
+]
+
+{enum_name.upper()}_LATENT_SPACE = LatentSpace(
+ channels=1, # TODO: latent channels; must equal len(...LATENT_RGB_FACTORS)
+ spatial_compression=8, # TODO: how much smaller a latent is than the image, per side
+ rgb_factors={enum_name.upper()}_LATENT_RGB_FACTORS,
+)
+
+register(
+ BaseModelType.{enum_name},
+ LatentSpaceFacet({enum_name.upper()}_LATENT_SPACE),
+ # TODO: the class its text encoder produces. Reuse an existing one where the shape matches.
+ ConditioningFacet(BasicConditioningInfo),
+ # TODO: what the generation sliders should say. Cite the model card in a comment.
+ DefaultSettingsFacet({{None: MainModelDefaultSettings(width=1024, height=1024)}}),
+ # TODO: which of txt2img / img2img / inpaint / outpaint / t2v / i2v it can do, and the prefix
+ # its mode strings carry in image metadata. Both must match GENERATION_MODES.
+ ModalityFacet(frozenset({{"txt2img"}}), metadata_slug="{base_value.replace("-", "_")}"),
+ # TODO: dimension_grid must equal the `multiple_of` on this architecture's denoise node.
+ FeaturesFacet(
+ negative_prompt=NegativePrompt(visible=True, usage="always"),
+ dimension_grid=8,
+ ),
+)
+'''
+
+
+def invocations_package(base_value: str, enum_name: str) -> str:
+ return f'''"""{enum_name} nodes.
+
+Node modules go here, keeping their architecture prefix: `{base_value.replace("-", "_")}_denoise.py`,
+`{base_value.replace("-", "_")}_model_loader.py`. VAE, text-encoder and PiD nodes belong in
+`invocations/vae/`, `invocations/text_encoder/` and `invocations/pid/` instead — they are shared
+across architectures rather than owned by one.
+
+This package is discovered automatically; there is no list to add it to. The `__init__.py` is what
+makes it a package, and without it every node in here would silently not exist.
+"""
+'''
+
+
+def starter_models_module(base_value: str, enum_name: str) -> str:
+ return f'''"""{enum_name} starter models."""
+
+from invokeai.backend.model_manager.starter_models.types import StarterModel # noqa: F401
+
+# TODO: declare the starter models, then add them to STARTER_MODELS in this package's __init__.py.
+# That list is a curated order, not a derived one — insert where it belongs rather than appending.
+'''
+
+
+def derive_residual_edits(source_root: Path, threshold: int = 5) -> list[tuple[str, int]]:
+ """Modules that dispatch on `BaseModelType`, worst first.
+
+ The heuristic is deliberately crude and deliberately recomputed: a module naming many bases is
+ choosing behaviour per base, and a new base has to be added to it.
+ """
+ found: list[tuple[str, int]] = []
+ for path in sorted(source_root.rglob("*.py")):
+ relative = path.relative_to(source_root.parent).as_posix()
+ if relative.startswith(NOT_A_COST):
+ continue
+ try:
+ tree = ast.parse(path.read_text(encoding="utf-8"))
+ except (SyntaxError, UnicodeDecodeError):
+ continue
+ members = {
+ node.attr
+ for node in ast.walk(tree)
+ if isinstance(node, ast.Attribute) and isinstance(node.value, ast.Name) and node.value.id == "BaseModelType"
+ }
+ if len(members) >= threshold:
+ found.append((relative, len(members)))
+ return sorted(found, key=lambda row: (-row[1], row[0]))
+
+
+def planned_files(base_value: str, enum_name: str) -> dict[str, str]:
+ slug = base_value.replace("-", "_")
+ return {
+ f"invokeai/backend/architectures/defs/{slug}.py": defs_module(base_value, enum_name),
+ f"invokeai/app/invocations/{slug}/__init__.py": invocations_package(base_value, enum_name),
+ f"invokeai/backend/model_manager/starter_models/{slug}.py": starter_models_module(base_value, enum_name),
+ }
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+ parser.add_argument("--base", required=True, help="The BaseModelType value, e.g. 'new-model'")
+ parser.add_argument("--name", required=True, help="The BaseModelType member name, e.g. 'NewModel'")
+ parser.add_argument("--write", action="store_true", help="Write the files. Without this, print what would be.")
+ args = parser.parse_args()
+
+ files = planned_files(args.base, args.name)
+
+ existing = [path for path in files if (REPO_ROOT / path).exists()]
+ if existing:
+ print("Refusing to overwrite:", *existing, sep="\n ")
+ return 1
+
+ for path, content in files.items():
+ if args.write:
+ target = REPO_ROOT / path
+ target.parent.mkdir(parents=True, exist_ok=True)
+ target.write_text(content, encoding="utf-8")
+ print(f"wrote {path} ({content.count(chr(10))} lines)")
+ else:
+ print(f"would write {path} ({content.count(chr(10))} lines)")
+
+ print(
+ "\nThese cannot be scaffolded — the work is to add something, not to extend a dispatch:",
+ )
+ for path, what in UNDERIVABLE:
+ print(f" {path}\n {what}")
+
+ residual = derive_residual_edits(SOURCE_ROOT)
+ print(f"\nAnd these {len(residual)} modules dispatch on BaseModelType, so check each one:")
+ for path, count in residual:
+ print(f" {count:>3} bases {path}")
+
+ print(
+ "\nThe app will not boot until the generated declaration is filled in — that is deliberate."
+ if args.write
+ else "\nNothing was written. Re-run with --write."
+ )
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/tests/test_new_architecture_script.py b/tests/test_new_architecture_script.py
new file mode 100644
index 0000000000..deaf955c9c
--- /dev/null
+++ b/tests/test_new_architecture_script.py
@@ -0,0 +1,73 @@
+"""The scaffolder generates valid stubs, and its residual list stays honest."""
+
+import ast
+import sys
+from pathlib import Path
+
+import pytest
+
+REPO_ROOT = Path(__file__).resolve().parents[1]
+sys.path.insert(0, str(REPO_ROOT / "scripts"))
+
+from new_architecture import ( # noqa: E402
+ defs_module,
+ derive_residual_edits,
+ invocations_package,
+ planned_files,
+ starter_models_module,
+)
+
+REQUIRED_FACETS = {"LatentSpaceFacet", "ConditioningFacet", "DefaultSettingsFacet", "ModalityFacet", "FeaturesFacet"}
+
+
[email protected]("render", [defs_module, invocations_package, starter_models_module], ids=lambda f: f.__name__)
+def test_every_stub_is_valid_python(render) -> None: # type: ignore[no-untyped-def]
+ ast.parse(render("new-model", "NewModel"))
+
+
+def test_the_declaration_carries_every_required_facet() -> None:
+ """A stub missing one would fail at boot with a message about that facet rather than about the
+ stub, which is a worse first experience than the TODOs."""
+ tree = ast.parse(defs_module("new-model", "NewModel"))
+ called = {node.func.id for node in ast.walk(tree) if isinstance(node, ast.Call) and isinstance(node.func, ast.Name)}
+ assert REQUIRED_FACETS <= called, sorted(REQUIRED_FACETS - called)
+
+
+def test_the_declaration_is_obviously_unfinished() -> None:
+ """It must not look plausible. Someone who runs the scaffolder and forgets to fill it in should
+ ship nothing — the placeholder projection is a single black row."""
+ source = defs_module("new-model", "NewModel")
+ assert source.count("TODO") >= 5
+ assert "[0.0, 0.0, 0.0]" in source
+
+
+def test_the_slug_convention_matches_the_registry() -> None:
+ """`-` becomes `_`, the same rule `registry.defs_module_path` computes."""
+ paths = set(planned_files("new-model", "NewModel"))
+ assert "invokeai/backend/architectures/defs/new_model.py" in paths
+ assert "invokeai/app/invocations/new_model/__init__.py" in paths
+ assert "invokeai/backend/model_manager/starter_models/new_model.py" in paths
+
+
+def test_the_residual_list_is_derived_from_the_tree() -> None:
+ """Not a written-down list. It has already shrunk twice while this series ran, and a hardcoded
+ one would still be naming files that no longer dispatch on base."""
+ residual = dict(derive_residual_edits(REPO_ROOT / "invokeai"))
+
+ # Still dispatching: one config class per architecture is inherent to how configs work.
+ assert "invokeai/backend/model_manager/configs/main.py" in residual
+
+ # No longer dispatching — each of these was a chain the registry absorbed. If one reappears
+ # here, a facet has been bypassed.
+ for absorbed in (
+ "invokeai/app/util/step_callback.py",
+ "invokeai/app/api/dependencies.py",
+ "invokeai/app/invocations/ideal_size.py",
+ ):
+ assert absorbed not in residual, f"{absorbed} dispatches on base again"
+
+
+def test_the_registry_itself_is_never_listed_as_a_cost() -> None:
+ """`architectures/` names every base by construction; listing it would drown the real entries."""
+ residual = dict(derive_residual_edits(REPO_ROOT / "invokeai"))
+ assert not [path for path in residual if path.startswith("invokeai/backend/architectures/")]