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.

fix(krea2): budget the attention score matrix

Alexander Eichhornb98ee98182f8
diff --git a/invokeai/app/invocations/krea2/krea2_denoise.py b/invokeai/app/invocations/krea2/krea2_denoise.py
index 5e7aee2be0..c794d51468 100644
--- a/invokeai/app/invocations/krea2/krea2_denoise.py
+++ b/invokeai/app/invocations/krea2/krea2_denoise.py
@@ -58,6 +58,7 @@ from invokeai.backend.rectified_flow.rectified_flow_inpaint_extension import Rec
from invokeai.backend.stable_diffusion.diffusers_pipeline import PipelineIntermediateState
from invokeai.backend.stable_diffusion.diffusion.conditioning_data import Krea2ConditioningInfo
from invokeai.backend.util import sdpa_scope
+from invokeai.backend.util.attention import sdpa_score_matrix_bytes
from invokeai.backend.util.devices import TorchDevice
from invokeai.backend.util.logging import InvokeAILogger
@@ -490,15 +491,26 @@ class Krea2DenoiseInvocation(BaseInvocation, WithMetadata, WithBoard):
# headroom. Without this hint the cache reserves only the small default working memory and places
# the model before LoRA patches are applied, so a model+LoRA combination that just fits the base
# forward OOMs once the LoRA's extra activations are added.
+ regional_attention_mask_bytes = self._regional_attention_mask_bytes(
+ pos_extension, neg_extension, inference_dtype
+ )
estimated_working_memory = self._estimate_working_memory(
image_seq_len=image_seq_len,
positive_text_seq_len=pos_prompt_embeds.shape[1],
negative_text_seq_len=neg_prompt_embeds.shape[1] if neg_prompt_embeds is not None else None,
do_cfg=do_cfg,
num_loras=len(self.transformer.loras),
- regional_attention_mask_bytes=self._regional_attention_mask_bytes(
- pos_extension, neg_extension, inference_dtype
- ),
+ regional_attention_mask_bytes=regional_attention_mask_bytes,
+ )
+ # The activation estimate assumes a fused kernel. Where the build has none for this attention -- ROCm on
+ # Windows, MPS -- the score matrix is built as well (chunked on ROCm, see `install_rocm_sdpa_guard`).
+ estimated_working_memory += self._attention_score_bytes(
+ transformer_info.model,
+ seq_len=image_seq_len
+ + max(pos_prompt_embeds.shape[1], neg_prompt_embeds.shape[1] if neg_prompt_embeds is not None else 0),
+ has_attn_mask=regional_attention_mask_bytes > 0,
+ device=device,
+ dtype=inference_dtype,
)
# An int8_tensorwise build stores its linears quantized and materializes the dequantized,
# derotated weight per forward call. That transient is alive alongside the activations above,
@@ -631,6 +643,32 @@ class Krea2DenoiseInvocation(BaseInvocation, WithMetadata, WithBoard):
peak_attention_bias_bytes *= attention_dtype_bytes
return final_mask_bytes + peak_build_scratch_bytes + peak_attention_bias_bytes
+ @staticmethod
+ def _attention_score_bytes(
+ transformer: object, seq_len: int, has_attn_mask: bool, device: torch.device, dtype: torch.dtype
+ ) -> int:
+ """The score matrix the main blocks' joint attention materializes, where it does (0 on a fused kernel).
+
+ One call is alive at a time: the blocks run in sequence, and so do the conditional and unconditional passes.
+ The K/V heads are expanded to the query heads before the call on any build without a fused grouped-query
+ kernel (`backend/krea2/attention.py`), so the query head count is the one that counts. Heads come from the
+ loaded model's config. The probe answers for torch's default policy, so forcing the math kernel through the
+ `INVOKE_KREA2_SDPA_BACKEND` debugging switch is not priced.
+ """
+ config = getattr(transformer, "config", None)
+ num_heads = getattr(config, "num_attention_heads", None)
+ head_dim = getattr(config, "attention_head_dim", None)
+ if not isinstance(num_heads, int) or not isinstance(head_dim, int):
+ return 0
+ return sdpa_score_matrix_bytes(
+ device=device,
+ dtype=dtype,
+ num_heads=num_heads,
+ head_dim=head_dim,
+ seq_len=seq_len,
+ has_attn_mask=has_attn_mask,
+ )
+
def _estimate_working_memory(
self,
image_seq_len: int,
diff --git a/tests/app/invocations/test_krea2_denoise.py b/tests/app/invocations/test_krea2_denoise.py
index 13fac5dbd8..2a5588042b 100644
--- a/tests/app/invocations/test_krea2_denoise.py
+++ b/tests/app/invocations/test_krea2_denoise.py
@@ -843,3 +843,37 @@ class TestTheInt8DequantTransientReachesTheReservation:
transient = peak_dequant_transient_bytes(quantized, torch.float32)
assert transient > 0
assert quantized_bytes == dense_bytes + transient
+
+
+class TestTheAttentionScoreMatrixReachesTheReservation:
+ """The activation estimate assumes a fused attention kernel. Where the build has none -- ROCm on Windows, MPS --
+ the main blocks also build a score matrix (12.9 GiB per call for 1024px unchunked), and the reservation must
+ price it: for the heads of the loaded model and the sequence the transformer really attends over."""
+
+ def _reservation_for(self, monkeypatch, tmp_path, score_bytes: int) -> tuple[int, list[dict], _Transformer]:
+ import invokeai.app.invocations.krea2.krea2_denoise as denoise_module
+
+ transformer = _Transformer()
+ transformer.config = SimpleNamespace(num_attention_heads=48, attention_head_dim=128)
+ info = _TransformerInfo(transformer)
+ context = _runtime_context(tmp_path, transformer)
+ context.models.load = lambda _identifier: info
+ _patch_runtime(monkeypatch)
+ asked: list[dict] = []
+
+ def score_matrix_bytes(**kwargs) -> int:
+ asked.append(kwargs)
+ return score_bytes
+
+ monkeypatch.setattr(denoise_module, "sdpa_score_matrix_bytes", score_matrix_bytes)
+ _runtime_invocation(cfg_scale=1.0)._run_diffusion(context)
+ return info.working_mem_bytes, asked, transformer
+
+ def test_the_score_matrix_is_reserved_for_the_attended_sequence(self, monkeypatch, tmp_path) -> None:
+ fused_bytes, _, _ = self._reservation_for(monkeypatch, tmp_path, score_bytes=0)
+ materializing_bytes, asked, transformer = self._reservation_for(monkeypatch, tmp_path, score_bytes=12345)
+
+ assert materializing_bytes == fused_bytes + 12345
+ assert len(asked) == 1
+ assert (asked[0]["num_heads"], asked[0]["head_dim"]) == (48, 128)
+ assert asked[0]["seq_len"] == transformer.combined_sequence_lengths[0]