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): free the probe tensor, and stop the tests reading the shell

Alexander Eichhorn3bd563b6e549
diff --git a/invokeai/app/util/startup_utils.py b/invokeai/app/util/startup_utils.py
index 248b145cf2..509bfcf047 100644
--- a/invokeai/app/util/startup_utils.py
+++ b/invokeai/app/util/startup_utils.py
@@ -54,6 +54,7 @@ def probe_attention_backends(device: torch.device) -> dict[str, bool] | None:
"""
if device.type != "cuda":
return None
+ probe = params = None
try:
probe = torch.empty(
1,
@@ -76,6 +77,11 @@ def probe_attention_backends(device: torch.device) -> dict[str, bool] | None:
return None
finally:
# Do not leave the probe tensor sitting in the caching allocator for the first generation.
+ # `empty_cache()` only releases blocks nothing references. Both locals still hold the probe
+ # here -- `SDPAParams` keeps a reference of its own -- so dropping both is what makes the
+ # call do anything at all. Measured: without this the segment stays reserved for the
+ # process lifetime, which is the opposite of what the line above says.
+ del probe, params
torch.cuda.empty_cache()
diff --git a/tests/app/util/test_attention_backend_probe.py b/tests/app/util/test_attention_backend_probe.py
index 11dba3bce0..8e9043a1cb 100644
--- a/tests/app/util/test_attention_backend_probe.py
+++ b/tests/app/util/test_attention_backend_probe.py
@@ -38,17 +38,20 @@ class TestProbe:
available = probe_attention_backends(torch.device("cuda"))
assert available == {"cudnn": False, "flash": False, "efficient": False, "math": True}
+ @pytest.mark.skipif(not torch.cuda.is_available(), reason="the allocator is the thing under test")
def test_the_probe_tensor_is_not_left_in_the_allocator(self):
- with (
- patch.object(torch, "empty", return_value=MagicMock()),
- patch.object(torch.backends.cuda, "SDPAParams", return_value=MagicMock()),
- patch.object(torch.backends.cuda, "can_use_cudnn_attention", return_value=True),
- patch.object(torch.backends.cuda, "can_use_flash_attention", return_value=True),
- patch.object(torch.backends.cuda, "can_use_efficient_attention", return_value=True),
- patch.object(torch.cuda, "empty_cache") as empty_cache,
- ):
- probe_attention_backends(torch.device("cuda"))
- empty_cache.assert_called_once()
+ """Measured against the real allocator, because that is the claim.
+
+ Patching `torch.empty` and `torch.cuda.empty_cache` and asserting the call happened tests
+ choreography: it passes just as happily when the probe is still referenced and the call
+ therefore frees nothing, which is what it did.
+ """
+ torch.cuda.empty_cache()
+ before = torch.cuda.memory_reserved()
+
+ probe_attention_backends(torch.device("cuda"))
+
+ assert torch.cuda.memory_reserved() == before
class TestLogLine:
diff --git a/tests/backend/krea2/test_attention.py b/tests/backend/krea2/test_attention.py
index 9b0d8e7b22..14a23a31f4 100644
--- a/tests/backend/krea2/test_attention.py
+++ b/tests/backend/krea2/test_attention.py
@@ -7,6 +7,16 @@ import invokeai.backend.krea2.attention as krea2_attention
from invokeai.backend.krea2.attention import Krea2MemoryEfficientAttnProcessor, Krea2RegionalPromptingState
[email protected](autouse=True)
+def _no_ambient_override(monkeypatch):
+ """These assert the default ranking, so they have to own the default rather than inherit it.
+
+ Four of the six fail in a shell where `INVOKE_KREA2_SDPA_BACKEND` is exported -- which is the
+ shell the PR asks users and its own A/B workflow to run in.
+ """
+ monkeypatch.delenv(krea2_attention.KREA2_SDPA_BACKEND_ENV_VAR, raising=False)
+
+
def _build_gqa_attention() -> Krea2Attention:
# Krea-2's main blocks use grouped-query attention: more query heads than key/value heads.
torch.manual_seed(0)
diff --git a/tests/backend/krea2/test_regional_prompting.py b/tests/backend/krea2/test_regional_prompting.py
index 3d68d25178..7fd8152006 100644
--- a/tests/backend/krea2/test_regional_prompting.py
+++ b/tests/backend/krea2/test_regional_prompting.py
@@ -3,6 +3,7 @@ import torch
from diffusers.models.transformers.transformer_krea2 import Krea2Transformer2DModel
from invokeai.backend.krea2.attention import (
+ KREA2_SDPA_BACKEND_ENV_VAR,
Krea2RegionalPromptingState,
build_krea2_attention_processors,
)
@@ -13,6 +14,16 @@ from invokeai.backend.krea2.regional_prompting import (
from invokeai.backend.krea2.sampling_utils import prepare_position_ids
[email protected](autouse=True)
+def _no_ambient_override(monkeypatch):
+ """A forced backend changes the numerics these compare, so the environment is decided here.
+
+ `test_only_even_main_blocks_apply_the_regional_mask_during_a_forward` runs two real forward
+ passes and compares pixels; exporting `INVOKE_KREA2_SDPA_BACKEND` makes them differ.
+ """
+ monkeypatch.delenv(KREA2_SDPA_BACKEND_ENV_VAR, raising=False)
+
+
def _conditioning(length: int, value: float, mask: torch.Tensor | None = None) -> Krea2TextConditioning:
return Krea2TextConditioning(prompt_embeds=torch.full((1, length, 12, 8), value), mask=mask)
diff --git a/tests/backend/krea2/test_sdpa_backend_selection.py b/tests/backend/krea2/test_sdpa_backend_selection.py
index 9bc1422d7f..ed86e34ea9 100644
--- a/tests/backend/krea2/test_sdpa_backend_selection.py
+++ b/tests/backend/krea2/test_sdpa_backend_selection.py
@@ -26,6 +26,18 @@ from invokeai.backend.krea2.attention import (
)
[email protected](autouse=True)
+def _no_ambient_override(monkeypatch):
+ """Decide the override here, never inherit it from the shell.
+
+ The PR tells users -- and its own A/B workflow -- to export this variable, so the suite is run
+ in exactly the shell where it is set. Reading it made 11 of these 24 tests fail there, including
+ the fallback test the design rests on. A test that asserts what the default ranking is has to
+ own that default; one that wants an override sets it explicitly.
+ """
+ monkeypatch.delenv(KREA2_SDPA_BACKEND_ENV_VAR, raising=False)
+
+
class TestTheDefaultRanking:
def test_flash_leads_and_cudnn_follows(self):
"""Flash is the fastest kernel where the build has it -- and is already what runs today,