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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(architectures): report a variant's own latent compression, and gate the contracts in CI
Alexander Eichhornf538f2261a08
diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.ymlindex 976f5ebc56..a0da6f4be7 100644--- a/.github/workflows/python-tests.yml+++ b/.github/workflows/python-tests.yml@@ -103,6 +103,11 @@ jobs:- 'invokeai/frontend/webv2/**/*.py'# test_workflow_graph_builder.py validates these editor fixtures against the backend.- 'invokeai/frontend/webv2/src/features/workflow/core/fixtures/**'+ # Cross-stack generation contracts: the frontend writes these, Python validates them+ # against the invocation and architecture registries. A frontend-only change that+ # regenerates one would otherwise pass vitest while skipping the backend half.+ - 'invokeai/frontend/webv2/src/features/generation/core/__snapshots__/**'+ - 'invokeai/frontend/webv2/src/features/generation/core/__fixtures__/**'- 'tests/**'# tests/ covers a few of these (e.g. test_check_pins.py, test_docs_json_export.py)- 'scripts/**'diff --git a/invokeai/backend/architectures/capabilities.py b/invokeai/backend/architectures/capabilities.pyindex dc4fbf8b91..cd8d999d88 100644--- a/invokeai/backend/architectures/capabilities.py+++ b/invokeai/backend/architectures/capabilities.py@@ -96,15 +96,18 @@ class ArchitectureCapabilities(BaseModel):def _features_of(- facet: FeaturesFacet, spatial_compression: int, variant: AnyVariant | None = None+ facet: FeaturesFacet, latent_space: LatentSpaceFacet, variant: AnyVariant | None = None) -> ArchitectureFeatures:+ # Resolved per row, not once per architecture: Wan TI2V-5B denoises in the 48-channel Wan2.2+ # space at 16x where A14B is 16 channels at 8x, and a client joining on `(base, variant)`+ # would otherwise read its base's compression.return ArchitectureFeatures(negative_prompt=NegativePromptPolicy(visible=facet.negative_prompt.visible,usage=facet.negative_prompt.usage,),dimension_grid=facet.resolve_dimension_grid(variant),- spatial_compression=spatial_compression,+ spatial_compression=latent_space.resolve_variant(variant).spatial_compression,guidance_label=facet.guidance_label,scheduler_set=facet.scheduler_set,scheduler_applies_to_graph=facet.scheduler_applies_to_graph,@@ -146,18 +149,20 @@ def architecture_capabilities() -> list[ArchitectureCapabilities]:defaults = require(base, DefaultSettingsFacet)rendered = ArchitectureModality(modes=sorted(modality.modes), metadata_slug=modality.metadata_slug)- compression = latent_space.primary.spatial_compression+base_defaults = defaults.resolve()rows.append(ArchitectureCapabilities(base=base,modality=rendered,- features=_features_of(features, compression),+ features=_features_of(features, latent_space),defaults=base_defaults,))- differing = (set(defaults.by_variant) | set(features.dimension_grid_by_variant)) - {None}+ differing = (+ set(defaults.by_variant) | set(features.dimension_grid_by_variant) | set(latent_space.by_variant)+ ) - {None}for variant in sorted(differing):rows.append(ArchitectureCapabilities(@@ -166,7 +171,7 @@ def architecture_capabilities() -> list[ArchitectureCapabilities]:# "FluxVariantType.DevFill" rather than the "dev_fill" a client stores and sends.variant=variant.value,modality=rendered,- features=_features_of(features, compression, variant),+ features=_features_of(features, latent_space, variant),defaults=defaults.by_variant.get(variant, base_defaults),))diff --git a/invokeai/backend/architectures/defs/wan.py b/invokeai/backend/architectures/defs/wan.pyindex 0f07399ba7..b52b32d4ac 100644--- a/invokeai/backend/architectures/defs/wan.py+++ b/invokeai/backend/architectures/defs/wan.py@@ -16,7 +16,13 @@ from invokeai.backend.stable_diffusion.diffusion.conditioning_data import WanCon# decides, so the sample's channel count is what resolves it.register(BaseModelType.Wan,- LatentSpaceFacet(WAN21_16, alternates=(WAN22_48,)),+ LatentSpaceFacet(+ WAN21_16,+ alternates=(WAN22_48,),+ # Generation tells these apart by the sample's channel count; the served table has only a+ # model record, so the variant names it there.+ by_variant={WanVariantType.TI2V_5B: WAN22_48},+ ),ConditioningFacet(WanConditioningInfo),DefaultSettingsFacet({diff --git a/invokeai/backend/architectures/facets/latent_space.py b/invokeai/backend/architectures/facets/latent_space.pyindex 06c679c3fd..f3db89859f 100644--- a/invokeai/backend/architectures/facets/latent_space.py+++ b/invokeai/backend/architectures/facets/latent_space.py@@ -11,7 +11,8 @@ Nine latent spaces serve sixteen architectures, which is the point: the sharingpreviously expressed by duplicating matrices under different names."""-from dataclasses import dataclass+from collections.abc import Mapping+from dataclasses import dataclass, fieldfrom typing import ClassVar, Finalimport torch@@ -19,7 +20,7 @@ from PIL import Imagefrom invokeai.backend.architectures.facet import Facetfrom invokeai.backend.architectures.registry import ArchitectureError, require-from invokeai.backend.model_manager.taxonomy import BaseModelType+from invokeai.backend.model_manager.taxonomy import AnyVariant, BaseModelType_DEFS_PACKAGE: Final = "invokeai/backend/architectures/defs/""""Where a declaration lives. `registry.defs_module_path` names the exact file given a base; a@@ -394,6 +395,33 @@ class LatentSpaceFacet(Facet):architecture, which is what keeps them from depending on the shape of a tensor."""+ by_variant: Mapping[AnyVariant, LatentSpace] = field(default_factory=dict)+ """Which of these spaces a variant denoises in, where the variant alone settles it.++ `resolve` answers the same question from a tensor, which is what generation has; this answers+ it from a model record, which is what the served capability table has. Wan TI2V-5B compresses+ 16x per side where A14B compresses 8x, and a client joining on `(base, variant)` would+ otherwise be told its base's number.++ Every value must be `primary` or one of `alternates`: this names an existing space for a+ variant, it does not introduce one."""++ def __post_init__(self) -> None:+ known = (self.primary, *self.alternates)+ unknown = sorted(str(v.channels) for v in self.by_variant.values() if v not in known)+ if unknown:+ raise ArchitectureError(+ f"by_variant names {len(unknown)} latent space(s) this facet does not declare "+ f"({', '.join(unknown)} channels). Add them to `alternates`, or `resolve` will "+ "never return them for a real sample. See " + _DEFS_PACKAGE + "."+ )++ def resolve_variant(self, variant: AnyVariant | None = None) -> LatentSpace:+ """The space one concrete model denoises in, from its variant alone."""+ if variant is None:+ return self.primary+ return self.by_variant.get(variant, self.primary)+def resolve(self, sample: torch.Tensor) -> LatentSpace:"""Which space this sample is in.diff --git a/tests/backend/architectures/test_dimension_grid.py b/tests/backend/architectures/test_dimension_grid.pyindex 23f35d6660..e1884fd0e9 100644--- a/tests/backend/architectures/test_dimension_grid.py+++ b/tests/backend/architectures/test_dimension_grid.py@@ -17,7 +17,8 @@ import pytestfrom invokeai.app.invocations.wan.wan_denoise import _validate_spatial_dimensionsfrom invokeai.backend.architectures import architecture_capabilities, generative_basesfrom invokeai.backend.architectures.facets.features import FeaturesFacet, NegativePrompt-from invokeai.backend.architectures.registry import require+from invokeai.backend.architectures.facets.latent_space import WAN21_16, WAN22_48, LatentSpace, LatentSpaceFacet+from invokeai.backend.architectures.registry import ArchitectureError, requirefrom invokeai.backend.model_manager.taxonomy import BaseModelType, ModelVariantType, WanVariantType@@ -117,3 +118,42 @@ def test_the_variant_rows_of_a_base_stay_sorted() -> None:for base in generative_bases():variants = [r.variant for r in architecture_capabilities() if r.base is base and r.variant is not None]assert variants == sorted(variants), base.value+++def test_a_variant_row_reports_the_compression_of_the_space_it_denoises_in() -> None:+ """The grid and the compression are two facts about the same space, and a row that got one+ from the variant and the other from the base would be internally inconsistent. TI2V-5B+ compresses 16x per side; A14B, and the base row, 8x."""+ rows = {(r.base.value, r.variant): r for r in architecture_capabilities()}++ wan_base = rows[("wan", None)]+ wan_ti2v = rows[("wan", "ti2v_5b")]++ assert (wan_base.features.dimension_grid, wan_base.features.spatial_compression) == (16, 8)+ assert (wan_ti2v.features.dimension_grid, wan_ti2v.features.spatial_compression) == (32, 16)+++def test_resolving_a_space_by_variant_agrees_with_resolving_it_by_sample() -> None:+ """Two ways to the same answer: generation has a tensor, the served table has a model record.+ If they disagreed, a preview and the size the client was told would describe different spaces."""+ import torch++ from invokeai.backend.model_manager.taxonomy import BaseModelType, WanVariantType++ facet = require(BaseModelType.Wan, LatentSpaceFacet)++ for variant, channels in ((WanVariantType.TI2V_5B, 48), (None, 16)):+ by_variant = facet.resolve_variant(variant)+ by_sample = facet.resolve(torch.zeros(1, channels, 8, 8))+ assert by_variant is by_sample, variant+ assert by_variant.channels == channels+++def test_a_variant_cannot_name_a_space_the_facet_does_not_declare() -> None:+ """`by_variant` names one of this facet's spaces; it does not introduce one. A space missing+ from `alternates` is unreachable for `resolve`, so the two resolvers would disagree for every+ real sample."""+ stray = LatentSpace(channels=64, spatial_compression=8, rgb_factors=[[0.1, 0.1, 0.1]] * 64)++ with pytest.raises(ArchitectureError, match="does not declare"):+ LatentSpaceFacet(WAN21_16, alternates=(WAN22_48,), by_variant={WanVariantType.TI2V_5B: stray})