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.

perf(krea2): rank flash ahead of cuDNN, measured on both build kinds

Alexander Eichhorn7ccaf3ac1f00
diff --git a/invokeai/backend/krea2/attention.py b/invokeai/backend/krea2/attention.py
index 9bfcb65b7d..2bbdb63ab7 100644
--- a/invokeai/backend/krea2/attention.py
+++ b/invokeai/backend/krea2/attention.py
@@ -27,18 +27,33 @@ from invokeai.backend.util.logging import InvokeAILogger
logger = InvokeAILogger.get_logger(__name__)
-# Rank cuDNN first: it is ~1.6x the memory-efficient kernel on the Krea-2 attention shape at identical
-# peak memory, worth 4-8% per generation and growing with resolution. Everything below it is a
-# fallback, never an exclusive choice -- an unavailable backend is skipped by the dispatcher, so the
-# list degrades on its own: to `efficient` where cuDNN is unusable (which is today's behaviour), and
-# on ROCm, where cuDNN is absent and flash rejects the additive mask.
+# Measured on the real Krea-2 attention shape ([1, 48, 4608, 128], bf16), per call:
#
-# FLASH stays in the list even where a probe would show it absent: a dead entry in a ranked list costs
-# nothing, while a missing one costs a platform (flash *is* the ROCm path for the unmasked blocks).
+# RTX 4090 / Windows RTX 30-series / Linux
+# flash not compiled in 19.74 ms
+# cudnn 3.72 ms 21.27 ms
+# efficient 5.92 ms 31.45 ms
+# math 51.23 ms 168.48 ms
+#
+# Two things follow, and the order below encodes both.
+#
+# **Flash first.** Where the build has it, flash is the fastest kernel and is *already* what runs
+# today: without `set_priority` torch picks by its own order, in which flash outranks efficient. On
+# the 30-series card an unprefixed call lands at 20.15 ms, i.e. on flash, not on efficient. Ranking
+# cuDNN above it would therefore be a small regression on every flash-capable build.
+#
+# **cuDNN second, and it is not a formality.** Flash refuses the additive padding mask that the
+# regional-prompting blocks pass, so on exactly those blocks it is skipped and cuDNN takes over --
+# where it beats efficient by 1.6x-2.0x. Windows CUDA builds have no flash at all, so there cuDNN is
+# what every block gets. Both are the cases the win comes from; flash-first does not give either up.
+#
+# Everything here is a fallback, never an exclusive choice: an unavailable backend is skipped by the
+# dispatcher, so the list degrades on its own -- to efficient on ROCm, where cuDNN is absent and
+# flash rejects the mask, and to efficient anywhere neither fused kernel can serve the call.
_KREA2_SDPA_BACKENDS = [
+ SDPBackend.FLASH_ATTENTION,
SDPBackend.CUDNN_ATTENTION,
SDPBackend.EFFICIENT_ATTENTION,
- SDPBackend.FLASH_ATTENTION,
SDPBackend.MATH,
]
diff --git a/tests/backend/krea2/test_sdpa_backend_selection.py b/tests/backend/krea2/test_sdpa_backend_selection.py
index 80ce58941f..9bc1422d7f 100644
--- a/tests/backend/krea2/test_sdpa_backend_selection.py
+++ b/tests/backend/krea2/test_sdpa_backend_selection.py
@@ -1,9 +1,14 @@
"""Which SDPA backends the Krea-2 attention processors run under, and the opt-in override.
-The ranking is a measurement, not a preference: cuDNN is ~1.6x the memory-efficient kernel on the
-Krea-2 attention shape at identical peak memory, worth 4-8% per generation and growing with
-resolution. These tests pin the two things that make that reachable at all -- cuDNN being first, and
-`set_priority=True` -- because without either the change is a silent no-op.
+The ranking is a measurement, not a preference. On the real Krea-2 attention shape, per call: on a
+Windows 4090 (no flash in the build) cuDNN runs at 3.72ms against efficient's 5.92ms; on a Linux
+30-series card flash runs at 19.74ms, cuDNN at 21.27ms, efficient at 31.45ms. So flash leads where it
+exists and cuDNN takes the blocks it cannot serve -- the masked ones, and every block on a build
+without flash.
+
+These tests pin what makes that reachable at all: the order, and `set_priority=True`. Without the
+latter the list only *permits* backends and torch picks by its own order, so the ranking would be a
+silent no-op.
"""
from unittest.mock import MagicMock
@@ -22,9 +27,24 @@ from invokeai.backend.krea2.attention import (
class TestTheDefaultRanking:
- def test_cudnn_is_ranked_first(self):
- choice = resolve_krea2_sdpa_backends(raw_override=None)
- assert choice.backends[0] is SDPBackend.CUDNN_ATTENTION
+ def test_flash_leads_and_cudnn_follows(self):
+ """Flash is the fastest kernel where the build has it -- and is already what runs today,
+ because torch's own order puts it above efficient. Ranking cuDNN over it would be a small
+ regression on every flash-capable build."""
+ backends = resolve_krea2_sdpa_backends(raw_override=None).backends
+ assert backends[0] is SDPBackend.FLASH_ATTENTION
+ assert backends[1] is SDPBackend.CUDNN_ATTENTION
+
+ def test_cudnn_outranks_efficient(self):
+ """This is where the win actually comes from: flash refuses the additive mask the
+ regional-prompting blocks pass, so on those blocks it is skipped and cuDNN takes over --
+ 1.6x-2.0x over efficient. On a build without flash, that is every block."""
+ backends = resolve_krea2_sdpa_backends(raw_override=None).backends
+ assert backends.index(SDPBackend.CUDNN_ATTENTION) < backends.index(SDPBackend.EFFICIENT_ATTENTION)
+
+ def test_math_is_last(self):
+ # The unfused fallback: correct everywhere, and ~35x slower at ~35x the memory.
+ assert resolve_krea2_sdpa_backends(raw_override=None).backends[-1] is SDPBackend.MATH
def test_priority_is_set(self):
"""Load-bearing: without set_priority the list only *permits* backends and torch picks by
@@ -42,9 +62,11 @@ class TestTheDefaultRanking:
SDPBackend.MATH,
}
- def test_flash_is_kept_even_though_a_probe_would_call_it_dead_on_cuda(self):
- """Dropping FLASH statically would be actively wrong on ROCm, where flash is available and
- cuDNN is not. A dead entry in a ranked list costs nothing; a missing one costs a platform."""
+ def test_flash_is_kept_even_where_a_probe_would_call_it_dead(self):
+ """A sibling plan proposed dropping FLASH where a probe shows it absent. That would be wrong
+ twice over: on ROCm flash is available and cuDNN is not, and on Linux CUDA it is the fastest
+ kernel of the four. A dead entry in a ranked list costs nothing; a missing one costs a
+ platform."""
assert SDPBackend.FLASH_ATTENTION in resolve_krea2_sdpa_backends(raw_override=None).backends
def test_no_override_is_recorded_by_default(self):
@@ -117,11 +139,11 @@ class TestProcessorsCarryTheChoice:
choices = {id(p.sdpa_backends) for p in processors.values()}
assert len(choices) == 1
- assert next(iter(processors.values())).sdpa_backends.backends[0] is SDPBackend.CUDNN_ATTENTION
+ assert next(iter(processors.values())).sdpa_backends.backends[0] is SDPBackend.FLASH_ATTENTION
def test_a_standalone_processor_resolves_for_itself(self):
# Constructed directly in tests and by custom code; it must not depend on the builder.
- assert Krea2MemoryEfficientAttnProcessor().sdpa_backends.backends[0] is SDPBackend.CUDNN_ATTENTION
+ assert Krea2MemoryEfficientAttnProcessor().sdpa_backends.backends[0] is SDPBackend.FLASH_ATTENTION
@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA is required to exercise the SDPA dispatcher")