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(vae): tile on what the GPU keeps resident, not on reservation headroom

Alexander Eichhorn40a78452df1d
diff --git a/invokeai/app/invocations/vae/qwen_image_image_to_latents.py b/invokeai/app/invocations/vae/qwen_image_image_to_latents.py
index 1c94243d51..9f834092ec 100644
--- a/invokeai/app/invocations/vae/qwen_image_image_to_latents.py
+++ b/invokeai/app/invocations/vae/qwen_image_image_to_latents.py
@@ -75,6 +75,7 @@ class QwenImageImageToLatentsInvocation(BaseInvocation, WithMetadata, WithBoard)
image_tensor=image_tensor,
vae=vae_info.model,
tile_size=effective_tile_size,
+ device=vae_info.compute_device,
)
with vae_info.model_on_device(working_mem_bytes=estimated_working_memory) as (_, vae):
# Reinterpret an Anima-classified Wan VAE as AutoencoderKLQwenImage (identical weights).
diff --git a/invokeai/app/invocations/vae/qwen_image_latents_to_image.py b/invokeai/app/invocations/vae/qwen_image_latents_to_image.py
index 2c3034cd5e..f1f8312380 100644
--- a/invokeai/app/invocations/vae/qwen_image_latents_to_image.py
+++ b/invokeai/app/invocations/vae/qwen_image_latents_to_image.py
@@ -75,18 +75,21 @@ class QwenImageLatentsToImageInvocation(BaseInvocation, WithMetadata, WithBoard)
image_tensor=latents,
vae=vae_info.model,
tile_size=effective_tile_size,
+ device=vae_info.compute_device,
)
if (
not tiled
and config.auto_tiled_decode
and should_pretile_vae_decode(vae_info.compute_device, estimated_working_memory)
):
+ tiled = True
effective_tile_size = resolve_qwen_image_vae_tile_size(self.tile_size)
estimated_working_memory = estimate_vae_working_memory_qwen_image(
operation="decode",
image_tensor=latents,
vae=vae_info.model,
tile_size=effective_tile_size,
+ device=vae_info.compute_device,
)
with vae_info.model_on_device(working_mem_bytes=estimated_working_memory) as (_, vae):
context.util.signal_progress("Running VAE")
diff --git a/invokeai/backend/util/vae_working_memory.py b/invokeai/backend/util/vae_working_memory.py
index 16c7015f85..1bb90aa16c 100644
--- a/invokeai/backend/util/vae_working_memory.py
+++ b/invokeai/backend/util/vae_working_memory.py
@@ -9,7 +9,8 @@ from diffusers.models.autoencoders.autoencoder_tiny import AutoencoderTiny
from invokeai.app.invocations.constants import LATENT_SCALE_FACTOR
from invokeai.backend.flux.modules.autoencoder import AutoEncoder, resolve_tile_size
-from invokeai.backend.util.attention import sdpa_score_matrix_bytes
+from invokeai.backend.util import wddm
+from invokeai.backend.util.attention import rocm_sdpa_chunks_math, sdpa_score_matrix_bytes
from invokeai.backend.util.devices import TorchDevice
from invokeai.backend.util.logging import InvokeAILogger
@@ -23,15 +24,22 @@ def should_pretile_vae_decode(
device: torch.device, full_decode_bytes: int, vram_fraction: float = VAE_PRETILE_VRAM_FRACTION
) -> bool:
"""Whether a decode should be tiled up front because its untiled working memory would claim more than
- ``vram_fraction`` of ``device``'s memory.
+ ``vram_fraction`` of the memory ``device`` will keep resident.
Waiting for an out-of-memory error does not work everywhere: on Windows, drivers page an allocation that does not
fit into system memory instead of failing it (always for ROCm, by default for NVIDIA's sysmem fallback), and the
decode just runs very slowly. ``device`` is where the VAE runs: a ``cpu_only`` VAE (and MPS, sharing system
memory) is never tiled on these grounds.
+
+ The comparison is against the Windows video-memory budget where there is one, not the card's nameplate total:
+ that budget is the point past which Windows pages this process out, and another GPU process lowers it within
+ about a second. A decode measured against the total alone would look like it fits and then crawl.
"""
if device.type == "cuda":
total_bytes = torch.cuda.get_device_properties(device).total_memory
+ budget_bytes = wddm.video_memory_budget(device)
+ if budget_bytes is not None:
+ total_bytes = min(total_bytes, budget_bytes)
elif device.type == "xpu":
total_bytes = torch.xpu.get_device_properties(device).total_memory
else:
@@ -40,7 +48,7 @@ def should_pretile_vae_decode(
return False
InvokeAILogger.get_logger(__name__).debug(
f"Decoding in tiles: an untiled decode would need ~{full_decode_bytes / 2**30:.1f} GiB of working memory, more "
- f"than {vram_fraction:.0%} of the GPU's {total_bytes / 2**30:.1f} GiB."
+ f"than {vram_fraction:.0%} of the {total_bytes / 2**30:.1f} GiB the GPU keeps resident."
)
return True
@@ -78,15 +86,17 @@ def estimate_vae_working_memory_sd15_sdxl(
vae: AutoencoderKL | AutoencoderTiny,
tile_size: int | None,
fp32: bool,
+ device: torch.device | None = None,
) -> int:
"""Estimate the working memory required to encode or decode the given tensor."""
# It was found experimentally that the peak working memory scales linearly with the number of pixels and the
# element size (precision). This estimate is accurate for both SD1 and SDXL.
element_size = 4 if fp32 else 2
- # This constant is determined experimentally and takes into consideration both allocated and reserved memory. See #8414
- # Encoding uses ~45% the working memory as decoding.
- scaling_constant = 2200 if operation == "decode" else 1100
+ # The same AutoencoderKL convolution stack as the FLUX.1 autoencoder, so it takes the same measured constants per
+ # convolution backend: MIOpen needs ~1.64x what cuDNN does (see `_FLUX_VAE_SCALING_CONSTANTS`). `device` is where
+ # the VAE will run; it defaults to the session device.
+ scaling_constant = _flux_vae_scaling_constant(operation, device or TorchDevice.choose_torch_device())
latent_scale_factor_for_operation = LATENT_SCALE_FACTOR if operation == "decode" else 1
@@ -121,7 +131,10 @@ def estimate_vae_working_memory_sd15_sdxl(
def estimate_vae_working_memory_cogview4(
- operation: Literal["encode", "decode"], image_tensor: torch.Tensor, vae: AutoencoderKL
+ operation: Literal["encode", "decode"],
+ image_tensor: torch.Tensor,
+ vae: AutoencoderKL,
+ device: torch.device | None = None,
) -> int:
"""Estimate the working memory required by the invocation in bytes."""
latent_scale_factor_for_operation = LATENT_SCALE_FACTOR if operation == "decode" else 1
@@ -130,9 +143,10 @@ def estimate_vae_working_memory_cogview4(
w = latent_scale_factor_for_operation * image_tensor.shape[-1]
element_size = next(vae.parameters()).element_size()
- # This constant is determined experimentally and takes into consideration both allocated and reserved memory. See #8414
- # Encoding uses ~45% the working memory as decoding.
- scaling_constant = 2200 if operation == "decode" else 1100
+ # The same AutoencoderKL convolution stack as the FLUX.1 autoencoder, so it takes the same measured constants per
+ # convolution backend: MIOpen needs ~1.64x what cuDNN does (see `_FLUX_VAE_SCALING_CONSTANTS`). `device` is where
+ # the VAE will run; it defaults to the session device.
+ scaling_constant = _flux_vae_scaling_constant(operation, device or TorchDevice.choose_torch_device())
working_memory = h * w * element_size * scaling_constant
# max, not sum: the mid-block attention and the full-resolution convolutions peak in different
@@ -540,6 +554,7 @@ def estimate_vae_working_memory_qwen_image(
image_tensor: torch.Tensor,
vae: AutoencoderKLQwenImage,
tile_size: int | None = None,
+ device: torch.device | None = None,
) -> int:
"""Estimate the working memory required by the invocation in bytes.
@@ -554,7 +569,8 @@ def estimate_vae_working_memory_qwen_image(
plus the pixel-space buffers, which stay resident on the execution device either way.
``tile_size`` is the resolved tile size (the nodes' 0 sentinel already substituted), and assumes
- the 4:3 tile-to-stride ratio applied by ``patch_qwen_image_vae_tiling``.
+ the 4:3 tile-to-stride ratio applied by ``patch_qwen_image_vae_tiling``. ``device`` is where the
+ VAE will run; it selects the constants below and defaults to the session device.
"""
latent_scale_factor_for_operation = LATENT_SCALE_FACTOR if operation == "decode" else 1
@@ -592,11 +608,20 @@ def estimate_vae_working_memory_qwen_image(
# materialised seq^2 score matrix), i.e. XPU gets an efficient kernel and is in the same
# O(area) regime as CUDA. If a future driver regresses to math attention, this branch --
# not the constants -- is what needs to change.
- is_rocm = torch.version.hip is not None
- if operation == "decode":
- scaling_constant = 5500 if is_rocm else 2900
- else: # encode
- scaling_constant = 6300 if is_rocm else 1600
+ # On ROCm the figures above were measured while a math-kernel attention call built its whole score matrix. Where
+ # the guard bounds that call (`rocm_sdpa_chunks_math`), the super-linear term is gone and the curve is flat and
+ # linear again -- measured 2026-09-23 on an RX 9060 XT (gfx1200, torch 2.12+rocm7.14, fp16, the same script):
+ # decode 2650-2772 across 512^2 to 2048^2, encode 1541-1552, i.e. what CUDA measures (2660 decode). Shipping the
+ # unbounded figures there costs two things at once: eviction of models the decode does not need out of the way,
+ # and -- since the same figure decides it -- tiling a decode that fits, which is not pixel-identical.
+ device = device if device is not None else TorchDevice.choose_torch_device()
+ is_rocm = device.type == "cuda" and torch.version.hip is not None
+ if is_rocm and rocm_sdpa_chunks_math(device.type):
+ scaling_constant = 3000 if operation == "decode" else 1700
+ elif is_rocm:
+ scaling_constant = 5500 if operation == "decode" else 6300
+ else:
+ scaling_constant = 2900 if operation == "decode" else 1600
if tile_size is not None and tile_size > 0:
# Bounded by one tile (plus overlap) rather than the full frame.
@@ -619,7 +644,10 @@ def estimate_vae_working_memory_qwen_image(
def estimate_vae_working_memory_sd3(
- operation: Literal["encode", "decode"], image_tensor: torch.Tensor, vae: AutoencoderKL
+ operation: Literal["encode", "decode"],
+ image_tensor: torch.Tensor,
+ vae: AutoencoderKL,
+ device: torch.device | None = None,
) -> int:
"""Estimate the working memory required by the invocation in bytes."""
# Encode operations use approximately 50% of the memory required for decode operations
@@ -630,9 +658,10 @@ def estimate_vae_working_memory_sd3(
w = latent_scale_factor_for_operation * image_tensor.shape[-1]
element_size = next(vae.parameters()).element_size()
- # This constant is determined experimentally and takes into consideration both allocated and reserved memory. See #8414
- # Encoding uses ~45% the working memory as decoding.
- scaling_constant = 2200 if operation == "decode" else 1100
+ # The same AutoencoderKL convolution stack as the FLUX.1 autoencoder, so it takes the same measured constants per
+ # convolution backend: MIOpen needs ~1.64x what cuDNN does (see `_FLUX_VAE_SCALING_CONSTANTS`). `device` is where
+ # the VAE will run; it defaults to the session device.
+ scaling_constant = _flux_vae_scaling_constant(operation, device or TorchDevice.choose_torch_device())
working_memory = h * w * element_size * scaling_constant
diff --git a/scripts/calibrate_qwen_vae_working_memory.py b/scripts/calibrate_qwen_vae_working_memory.py
index 2f81752aec..552c32a16a 100644
--- a/scripts/calibrate_qwen_vae_working_memory.py
+++ b/scripts/calibrate_qwen_vae_working_memory.py
@@ -47,6 +47,8 @@ from pathlib import Path
import torch
from diffusers.models.autoencoders.autoencoder_kl_qwenimage import AutoencoderKLQwenImage
+from invokeai.backend.util.attention import install_rocm_sdpa_guard
+
LATENT_SCALE_FACTOR = 8
# (height, width) pixel-space resolutions. Squares to test linearity in area, plus non-square
@@ -142,6 +144,11 @@ def _build_input(operation: str, h: int, w: int, z_dim: int, dtype: torch.dtype)
@torch.inference_mode()
def measure_one(vae_path: str, operation: str, h: int, w: int, dtype: torch.dtype) -> dict:
"""Measure peak reserved-memory growth for a single decode/encode. Runs in a child process."""
+ # The production path on ROCm runs attention through this guard, which bounds the score matrix a
+ # math-kernel call materializes. Measuring without it fits the constants to a peak the app never
+ # reaches. A no-op on every other build.
+ install_rocm_sdpa_guard()
+
vae = _load_vae(vae_path, dtype)
vae.to("cuda")
vae.disable_tiling() # Qwen invocations never tile; match that.
diff --git a/tests/app/invocations/test_qwen_image_working_memory.py b/tests/app/invocations/test_qwen_image_working_memory.py
index 6ff4dee1e1..b476221672 100644
--- a/tests/app/invocations/test_qwen_image_working_memory.py
+++ b/tests/app/invocations/test_qwen_image_working_memory.py
@@ -28,15 +28,27 @@ class TestQwenImageWorkingMemoryEstimate:
# (operation, latent_h, latent_w) -> the estimator scales pixel area (latent * 8 for decode,
# raw for encode) by element_size and the constant.
@pytest.mark.parametrize(
- "operation, is_rocm, expected_constant",
+ "operation, is_rocm, chunked, expected_constant",
[
- ("decode", True, 5500),
- ("decode", False, 2900),
- ("encode", True, 6300),
- ("encode", False, 1600),
+ ("decode", True, True, 3000),
+ ("decode", True, False, 5500),
+ ("decode", False, False, 2900),
+ ("encode", True, True, 1700),
+ ("encode", True, False, 6300),
+ ("encode", False, False, 1600),
+ ],
+ ids=[
+ "rocm-decode-chunked",
+ "rocm-decode-whole",
+ "cuda-decode",
+ "rocm-encode-chunked",
+ "rocm-encode-whole",
+ "cuda-encode",
],
)
- def test_constant_selected_per_backend(self, operation, is_rocm, expected_constant):
+ def test_constant_selected_per_backend(self, operation, is_rocm, chunked, expected_constant):
+ """The ROCm figures split by whether math attention is chunked: the larger pair was measured while a single
+ call built its whole score matrix, which is the super-linear term the guard removes."""
mock_vae = MagicMock(spec=AutoencoderKLQwenImage)
mock_vae.parameters.return_value = iter([torch.zeros(1, dtype=torch.float16)]) # element_size == 2
@@ -49,9 +61,12 @@ class TestQwenImageWorkingMemoryEstimate:
h = w = 512
hip_value = "7.1.0" if is_rocm else None
- with patch("torch.version.hip", hip_value):
+ with (
+ patch("torch.version.hip", hip_value),
+ patch("invokeai.backend.util.vae_working_memory.rocm_sdpa_chunks_math", return_value=chunked),
+ ):
result = estimate_vae_working_memory_qwen_image(
- operation=operation, image_tensor=image_tensor, vae=mock_vae
+ operation=operation, image_tensor=image_tensor, vae=mock_vae, device=torch.device("cuda")
)
assert result == h * w * 2 * expected_constant
@@ -481,8 +496,11 @@ class TestPretilingDoesNotFireOnReservationHeadroom:
total_bytes = total_gib * 2**30
latents = torch.zeros(1, 16, 1, px // 8, px // 8)
- # The constants are measured per backend, so pin it rather than inheriting the host's build.
+ # The constants are measured per backend, so pin it rather than inheriting the host's build. A running app
+ # always has the ROCm attention guard installed (`apply_monkeypatches`), and the ROCm constants differ by
+ # whether it is: measured 2650-2772 with math attention chunked against 5500 with the whole score matrix.
monkeypatch.setattr(torch.version, "hip", "7.2.0")
+ monkeypatch.setattr("invokeai.backend.util.vae_working_memory.rocm_sdpa_chunks_math", lambda device: True)
monkeypatch.setattr("torch.cuda.get_device_properties", lambda device: MagicMock(total_memory=total_bytes))
# The measured table is fp16, and the estimator runs once here and once inside the node,