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
feat(fp8): enable FP8 storage for Anima
Alexander Eichhornb47a92cd8826
diff --git a/invokeai/backend/anima/anima_transformer.py b/invokeai/backend/anima/anima_transformer.pyindex 36c5764e97..670c3f70cc 100644--- a/invokeai/backend/anima/anima_transformer.py+++ b/invokeai/backend/anima/anima_transformer.py@@ -985,6 +985,27 @@ class AnimaTransformer(MiniTrainDIT):text embeddings before they are fed to the DiT cross-attention layers."""+ # Modules that must keep their compute dtype when FP8 storage is enabled. Read by+ # `ModelLoader._apply_fp8_layerwise_casting`, which uses the same attribute name diffusers+ # models use, so no loader-side special-casing is needed.+ #+ # The generic skip patterns don't reach these: they match `norm`, `pos_embed`, `patch_embed`+ # and `proj_in/out`, but this architecture names the equivalent modules differently.+ #+ # `t_embedder` is the one that matters, and it is not a rounding-quality nicety: with it cast+ # to FP8 the model renders a heavily dithered image with no fine detail at all (verified+ # against a bf16 run at the same seed/steps/CFG). It feeds `adaln_lora` into every block, so+ # its error is applied to every token everywhere. Measured, same seed each time: casting+ # nothing = broken; `t_embedder` alone = clean, and adding either of the two below changes+ # nothing further. They are kept as ~2MB of margin on the I/O layers, matching what diffusers+ # skips by default for comparable DiTs. `adaln_modulation` was also tested and is deliberately+ # NOT listed — it costs 168MB and made no difference.+ _skip_layerwise_casting_patterns = [+ "t_embedder", # timestep embedding MLP -> adaln_lora for every block+ "x_embedder", # patch embedding (named `patch_embed` in diffusers models)+ "final_layer", # output projection+ ]+def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)self.llm_adapter = LLMAdapter()diff --git a/invokeai/backend/model_manager/load/model_loaders/anima.py b/invokeai/backend/model_manager/load/model_loaders/anima.pyindex 1b3432ee52..b981b39930 100644--- a/invokeai/backend/model_manager/load/model_loaders/anima.py+++ b/invokeai/backend/model_manager/load/model_loaders/anima.py@@ -170,6 +170,12 @@ class AnimaCheckpointModel(ModelLoader):f"Checkpoint is missing {len(load_result.missing_keys)} keys "f"(expected for inv_freq buffers). First 5: {load_result.missing_keys[:5]}")++ # Without this the `fp8_storage` toggle is shown for Anima models but does nothing. The+ # state dict was cast to a single `model_dtype` above, so the layerwise cast has one+ # unambiguous compute dtype to restore to. AnimaTransformer is a plain nn.Module, so this+ # takes the hook-based path in `_apply_fp8_to_nn_module`.+ model = self._apply_fp8_layerwise_casting(model, config, SubModelType.Transformer)return modeldiff --git a/tests/backend/model_manager/load/test_load_default_fp8.py b/tests/backend/model_manager/load/test_load_default_fp8.pyindex 460ec8f133..b807820d87 100644--- a/tests/backend/model_manager/load/test_load_default_fp8.py+++ b/tests/backend/model_manager/load/test_load_default_fp8.py@@ -329,6 +329,37 @@ def test_apply_fp8_layerwise_casting_passes_model_declared_skip_patterns():assert model.layers.weight.dtype == torch.float8_e4m3fn+def test_anima_transformer_declares_t_embedder_skip():+ """Regression guard for Anima + FP8 rendering a heavily dithered image.++ `AnimaTransformer.t_embedder` produces the `adaln_lora` conditioning consumed by every block,+ so casting it to FP8 corrupts every token of every block — verified against a bf16 run at the+ same seed/steps/CFG. None of the generic `_FP8_DEFAULT_SKIP_PATTERNS` match it (this+ architecture doesn't use diffusers' module names), so the model has to declare it itself.+ """+ from invokeai.backend.anima.anima_transformer import AnimaTransformer++ assert "t_embedder" in AnimaTransformer._skip_layerwise_casting_patterns++ # And the declared patterns actually reach the cast, matched against dotted module paths.+ class _Model(torch.nn.Module):+ def __init__(self):+ super().__init__()+ self.t_embedder = torch.nn.Sequential(torch.nn.Linear(4, 4), torch.nn.Linear(4, 4))+ self.blocks = torch.nn.Linear(4, 4)++ model = _Model().to(torch.float32)+ ModelLoader._apply_fp8_to_nn_module(+ model,+ storage_dtype=torch.float16,+ compute_dtype=torch.float32,+ extra_skip_patterns=tuple(AnimaTransformer._skip_layerwise_casting_patterns),+ )++ assert model.t_embedder[0].weight.dtype == torch.float32+ assert model.blocks.weight.dtype == torch.float16++def test_should_use_fp8_allows_z_image():"""Z-Image was excluded while we used diffusers' `enable_layerwise_casting()` with the globaltorch dtype (fp16) as compute dtype, which clashed with the model's bf16 weights. The compute