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(app): validate the architecture registry at boot

Alexander Eichhorn2af9d7d5a178
diff --git a/invokeai/app/api/dependencies.py b/invokeai/app/api/dependencies.py
index aad84485ed..c6a2f4b158 100644
--- a/invokeai/app/api/dependencies.py
+++ b/invokeai/app/api/dependencies.py
@@ -64,6 +64,7 @@ from invokeai.app.services.videos.videos_default import VideoService
from invokeai.app.services.wildcard_records.wildcard_records_sqlite import SqliteWildcardRecordsStorage
from invokeai.app.services.workflow_records.workflow_records_sqlite import SqliteWorkflowRecordsStorage
from invokeai.app.services.workflow_thumbnails.workflow_thumbnails_disk import WorkflowThumbnailFileStorageDisk
+from invokeai.backend.architectures import validate as validate_architectures
from invokeai.backend.stable_diffusion.diffusion.conditioning_data import (
AnimaConditioningInfo,
BasicConditioningInfo,
@@ -115,6 +116,12 @@ class ApiDependencies:
loop: asyncio.AbstractEventLoop,
logger: Logger = logger,
) -> None:
+ # Before anything else, and at module scope above rather than lazily inside a function: the
+ # registry has to be filled before `ObjectSerializerDisk` is constructed below, because that
+ # mutates process-global torch state (`add_safe_globals`). Also covers every embedder that
+ # never goes through `run_app` — tests, and scripts/generate_openapi_schema.py.
+ validate_architectures()
+
logger.info(f"InvokeAI version {__version__}")
logger.info(f"Root directory = {str(config.root_path)}")
diff --git a/invokeai/app/run_app.py b/invokeai/app/run_app.py
index 56cfa63246..317df47273 100644
--- a/invokeai/app/run_app.py
+++ b/invokeai/app/run_app.py
@@ -59,6 +59,7 @@ def run_app() -> None:
# This import must happen after configure_torch_cuda_allocator() is called, because the module imports torch.
from invokeai.app.invocations.baseinvocation import InvocationRegistry
from invokeai.app.invocations.load_custom_nodes import load_custom_nodes
+ from invokeai.backend.architectures import validate as validate_architectures
from invokeai.backend.util.devices import TorchDevice
torch_device_name = TorchDevice.get_generation_devices_summary(app_config.generation_devices)
@@ -102,6 +103,11 @@ def run_app() -> None:
f'Invocation "{invocation_type}" has unregistered output class "{output_annotation.__name__}"'
)
+ # Every registered architecture must declare every required facet. Unlike the invocation-output
+ # check above, this raises rather than warns: that one inspects third-party node packs, while
+ # architectures are first-party and the set is closed, so an incomplete one is a bug here.
+ validate_architectures()
+
if app_config.dev_reload:
# load_custom_nodes seems to bypass jurrigged's import sniffer, so be sure to call it *after* they're already
# imported.
diff --git a/tests/backend/architectures/test_import_isolation.py b/tests/backend/architectures/test_import_isolation.py
new file mode 100644
index 0000000000..e2db7e9d9e
--- /dev/null
+++ b/tests/backend/architectures/test_import_isolation.py
@@ -0,0 +1,47 @@
+"""Importing `dependencies` alone fills the registry.
+
+This is the constraint the module-scope import in `invokeai/app/api/dependencies.py` exists for, and
+it cannot be checked in-process: by the time any test runs, half the codebase has been imported and
+the registry would be full no matter where the import sat. A fresh interpreter is the only way to
+tell a module-scope import from a lazy one inside a function.
+
+The check matters because `ApiDependencies.initialize()` builds `ObjectSerializerDisk`, which mutates
+process-global torch state via `add_safe_globals`. Anything the registry is meant to contribute there
+has to be registered before that point, not on first use.
+
+Deliberately not marked `slow`: pytest's `addopts` carries `-m "not slow"`, so a slow marker would
+mean this never runs in CI.
+"""
+
+from tests.dangerously_run_function_in_subprocess import dangerously_run_function_in_subprocess
+
+
+def _registry_is_full_after_importing_dependencies() -> None:
+ # No arguments and no closure: the whole function body is re-executed in a fresh interpreter, so
+ # every name it uses has to be imported inside it.
+ import sys
+
+ import invokeai.app.api.dependencies # noqa: F401
+
+ # `sys.modules` first, and this ordering is the whole test. Importing anything under
+ # `invokeai.backend.architectures` runs that package's `__init__`, which fills the registry as a
+ # side effect — so reading the registry before this check would make the assertion true no
+ # matter where `dependencies` puts its import, or whether it has one at all.
+ assert "invokeai.backend.architectures.defs" in sys.modules, (
+ "importing invokeai.app.api.dependencies did not import the architecture registry. "
+ "Its import must stay at module scope; a lazy one inside initialize() is too late."
+ )
+
+ from invokeai.backend.architectures.registry import _ARCHITECTURES, _NOT_ARCHITECTURES
+ from invokeai.backend.model_manager.taxonomy import BaseModelType
+
+ expected = set(BaseModelType) - _NOT_ARCHITECTURES
+ missing = sorted(b.value for b in expected - set(_ARCHITECTURES))
+ assert not missing, f"not registered after importing dependencies: {missing}"
+
+
+def test_importing_dependencies_fills_the_registry() -> None:
+ _stdout, stderr, returncode = dangerously_run_function_in_subprocess(_registry_is_full_after_importing_dependencies)
+ # Asserted on the return code, not on empty stderr: importing this much of the codebase emits
+ # library deprecation warnings and an "InvokeAI" log line, none of which are failures.
+ assert returncode == 0, stderr