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(backend): ignore ComfyUI model_sampling keys when loading Anima checkpoints (#9404)
kappacommit01ed5154f310
diff --git a/invokeai/backend/model_manager/load/model_loaders/anima.py b/invokeai/backend/model_manager/load/model_loaders/anima.pyindex 1b3432ee52..533ca12623 100644--- a/invokeai/backend/model_manager/load/model_loaders/anima.py+++ b/invokeai/backend/model_manager/load/model_loaders/anima.py@@ -54,6 +54,28 @@ def _strip_anima_bundle_prefix(sd: dict) -> dict:return stripped_sd+# Checkpoint tensors that are not part of the transformer's in-memory state. Suffixes match+# derived buffers that the model regenerates at runtime (registered as non-persistent or+# recomputed locally); prefixes match metadata that export tools serialize alongside the+# weights (e.g. sampling schedules). Extend these tuples as new checkpoint variants surface.+_NON_MODEL_KEY_SUFFIXES = (+ ".inv_freq",+ "pos_embedder.dim_spatial_range",+ "pos_embedder.dim_temporal_range",+ "pos_embedder.seq",+)+_NON_MODEL_KEY_PREFIXES = ("model_sampling.",)+++def _filter_non_model_keys(sd: dict) -> dict:+ """Drop checkpoint keys that don't belong to the transformer module's state dict."""+ return {+ k: v+ for k, v in sd.items()+ if not (k.endswith(_NON_MODEL_KEY_SUFFIXES) or k.startswith(_NON_MODEL_KEY_PREFIXES))+ }++@ModelLoaderRegistry.register(base=BaseModelType.Anima, type=ModelType.Main, format=ModelFormat.Checkpoint)class AnimaCheckpointModel(ModelLoader):"""Class to load Anima transformer models from single-file checkpoints.@@ -100,6 +122,9 @@ class AnimaCheckpointModel(ModelLoader):# Strip the transformer-key prefix (`net.` or bundled `model.diffusion_model.`).sd = _strip_anima_bundle_prefix(sd)+ # Drop runtime-derived buffers and exporter metadata that aren't model weights.+ sd = _filter_non_model_keys(sd)+# Create an empty AnimaTransformer with Anima's default architecture parameterswith accelerate.init_empty_weights():model = AnimaTransformer(@@ -144,20 +169,6 @@ class AnimaCheckpointModel(ModelLoader):if sd[k].is_floating_point():sd[k] = sd[k].to(model_dtype)- # Filter out tensors that are regenerated at runtime and therefore not part of the- # in-memory module state. Some community-trained checkpoints (e.g. animaCatTower_v10)- # serialize derived pos_embedder buffers/cached tensors that the official model- # registers as non-persistent (or recomputes locally).- runtime_only_suffixes = (- ".inv_freq",- "pos_embedder.dim_spatial_range",- "pos_embedder.dim_temporal_range",- "pos_embedder.seq",- )- keys_to_remove = [k for k in sd.keys() if k.endswith(runtime_only_suffixes)]- for k in keys_to_remove:- del sd[k]-load_result = model.load_state_dict(sd, assign=True, strict=False)if load_result.unexpected_keys:raise RuntimeError(diff --git a/tests/backend/model_manager/load/test_anima_state_dict_utils.py b/tests/backend/model_manager/load/test_anima_state_dict_utils.pyindex 769ea67df8..b9a74f4bbb 100644--- a/tests/backend/model_manager/load/test_anima_state_dict_utils.py+++ b/tests/backend/model_manager/load/test_anima_state_dict_utils.py@@ -2,7 +2,10 @@import torch-from invokeai.backend.model_manager.load.model_loaders.anima import _strip_anima_bundle_prefix+from invokeai.backend.model_manager.load.model_loaders.anima import (+ _filter_non_model_keys,+ _strip_anima_bundle_prefix,+)from tests.backend.model_manager.load.state_dicts.anima_comfyui_keys import state_dict_keys as anima_keysfrom tests.backend.model_manager.load.state_dicts.utils import keys_to_mock_state_dict@@ -36,3 +39,36 @@ class TestStripAnimaBundlePrefix:def test_no_known_prefix_is_a_noop(self):sd = {"blocks.0.attn.qkv.weight": torch.empty(1)}assert _strip_anima_bundle_prefix(sd) is sd+++class TestFilterNonModelKeys:+ def test_runtime_derived_buffers_are_dropped_by_suffix(self):+ sd = {+ "blocks.0.attn.qkv.weight": torch.empty(1),+ "blocks.0.rope.inv_freq": torch.empty(1),+ "pos_embedder.seq": torch.empty(1),+ }++ out = _filter_non_model_keys(sd)++ assert set(out.keys()) == {"blocks.0.attn.qkv.weight"}++ def test_exporter_metadata_is_dropped_by_prefix(self):+ sd = {+ "blocks.0.attn.qkv.weight": torch.empty(1),+ "model_sampling.sigmas": torch.empty(1),+ }++ out = _filter_non_model_keys(sd)++ assert set(out.keys()) == {"blocks.0.attn.qkv.weight"}++ def test_model_weights_are_untouched(self):+ sd = {+ "blocks.0.attn.qkv.weight": torch.empty(1),+ "final_layer.weight": torch.empty(1),+ }++ out = _filter_non_model_keys(sd)++ assert out == sd