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.

test(vae): add failing coverage for two up-front tiling defects

Lincoln Stein33e16af98918
diff --git a/tests/app/invocations/test_qwen_image_working_memory.py b/tests/app/invocations/test_qwen_image_working_memory.py
index dd8e4e8a75..6ff4dee1e1 100644
--- a/tests/app/invocations/test_qwen_image_working_memory.py
+++ b/tests/app/invocations/test_qwen_image_working_memory.py
@@ -457,6 +457,70 @@ class TestQwenImageWorkingMemory:
mock_vae.enable_tiling.assert_not_called()
+# Peak reserved bytes per output pixel per element byte actually measured for the Qwen-Image VAE
+# decode on ROCm, from the per-point table in `estimate_vae_working_memory_qwen_image`. The shipped
+# constant is a flat 5500 -- "max observed + ~8% headroom" -- which is the right figure to *reserve*,
+# but the real curve is not flat, so it over-predicts the peak by up to 68%.
+MEASURED_ROCM_DECODE_CONSTANT = {512: 5132, 768: 4596, 1024: 4570, 1536: 3273, 1792: 3735, 2048: 4813}
+
+
+class TestPretilingDoesNotFireOnReservationHeadroom:
+ """A decode whose real peak fits the card must not be tiled.
+
+ Tiling is not pixel-identical, so it must be triggered by a decode that genuinely does not fit --
+ not by the headroom baked into a reservation constant. Over-reserving used to cost only cache
+ eviction; comparing that padded figure against a share of the card turns it into a silent output
+ change for images that would have decoded in a single pass.
+
+ Krea-2 decodes through this node too, and it has no out-of-memory retry, so the estimate is the
+ only trigger either way.
+ """
+
+ @pytest.mark.parametrize("px, total_gib", [(1536, 24), (1792, 32)])
+ def test_a_decode_whose_measured_peak_fits_the_card_is_not_tiled(self, px, total_gib, monkeypatch):
+ 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.
+ monkeypatch.setattr(torch.version, "hip", "7.2.0")
+ 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,
+ # so `parameters()` has to hand out a fresh iterator on every call.
+ mock_vae = MagicMock(spec=AutoencoderKLQwenImage)
+ mock_vae.parameters.side_effect = lambda: iter([torch.zeros(1, dtype=torch.float16)])
+ mock_vae_info = MagicMock()
+ mock_vae_info.model = mock_vae
+ mock_vae_info.compute_device = torch.device("cuda", 0)
+ # Stop the moment the decision has been made: everything after this would need a real GPU.
+ mock_vae_info.model_on_device.side_effect = RuntimeError("stop after the tiling decision")
+
+ untiled = estimate_vae_working_memory_qwen_image(
+ operation="decode", image_tensor=latents, vae=mock_vae, tile_size=None
+ )
+ measured_peak = px * px * 2 * MEASURED_ROCM_DECODE_CONSTANT[px]
+ assert measured_peak < 0.9 * total_bytes, "test shape must actually fit the card"
+
+ mock_context = MagicMock()
+ mock_context.models.load.return_value = mock_vae_info
+ mock_context.tensors.load.return_value = latents
+ mock_context.config.get.return_value.force_tiled_decode = False
+ mock_context.config.get.return_value.auto_tiled_decode = True
+
+ invocation = QwenImageLatentsToImageInvocation.model_construct(
+ latents=MagicMock(latents_name="test_latents"),
+ vae=MagicMock(vae=MagicMock(), seamless_axes=[]),
+ tiled=False,
+ tile_size=0,
+ )
+ with pytest.raises(RuntimeError, match="stop after the tiling decision"):
+ invocation.invoke(mock_context)
+
+ # A pre-tiled decode re-estimates against one tile, so the reservation shrinks. Reserving the
+ # full-frame figure is what "decoded in a single pass" looks like from here.
+ assert mock_vae_info.model_on_device.call_args.kwargs["working_mem_bytes"] == untiled
+
+
class TestQwenImageVaeTiling:
"""Exercise the tiling parameters against a real (tiny, randomly initialised) Qwen-Image VAE.
diff --git a/tests/backend/util/test_vae_pretile.py b/tests/backend/util/test_vae_pretile.py
index 6ef2fc1098..a0213b2808 100644
--- a/tests/backend/util/test_vae_pretile.py
+++ b/tests/backend/util/test_vae_pretile.py
@@ -38,3 +38,37 @@ def test_1024px_stays_untiled_on_an_8gb_card_and_1536px_tiles_on_16gb():
assert not should_pretile_vae_decode(torch.device("cuda"), 1024 * 1024 * 2 * 2900, VAE_PRETILE_VRAM_FRACTION)
with patch("torch.cuda.get_device_properties", return_value=MagicMock(total_memory=TOTAL)):
assert should_pretile_vae_decode(torch.device("cuda"), 1536 * 1536 * 2 * 3600, VAE_PRETILE_VRAM_FRACTION)
+
+
+def test_the_pretile_gate_uses_the_memory_the_device_will_keep_resident(monkeypatch):
+ """The gate must measure against what the device keeps resident, not the card's nameplate total.
+
+ This is the platform the feature was written for: the docstring justifies up-front tiling with
+ "on Windows, drivers page an allocation that does not fit into system memory instead of failing
+ it (always for ROCm)". Windows pages once the process passes its WDDM budget, and another GPU
+ process lowers that budget within about a second -- which is exactly why this PR added
+ `TorchDevice.cuda_mem_get_info` to cap the model cache's free figure by `wddm.video_memory_budget`.
+ The tiling gate consults neither that budget nor free memory, only `total_memory`.
+
+ A 16 GiB card whose budget is down to 12.5 GiB pages a 13.3 GiB decode (FLUX.1 at 1408px on
+ MIOpen). No out-of-memory error is raised there, so the nodes' tiled retry never fires and the
+ generation just crawls. Measured against the card total the decode looks fine (13.3 < 14.4 GiB),
+ so it is not tiled.
+ """
+ total_bytes = 16 * 2**30
+ budget_bytes = int(12.5 * 2**30)
+ estimate = 1408 * 1408 * 2 * 3600 # 13.3 GiB: under 90% of the card, over the budget
+
+ monkeypatch.setattr("torch.cuda.get_device_properties", lambda device: MagicMock(total_memory=total_bytes))
+ # torch cannot see the shortfall: on Windows ROCm its free figure is the device total minus this
+ # process's own live allocations, and this process has allocated nothing yet.
+ monkeypatch.setattr("torch.cuda.mem_get_info", lambda device: (total_bytes, total_bytes))
+ # Patch both binding sites: `devices` imports the name directly, so a fix routing through
+ # `TorchDevice.cuda_mem_get_info` and one calling `wddm.video_memory_budget` are both covered.
+ monkeypatch.setattr("invokeai.backend.util.devices.video_memory_budget", lambda device: budget_bytes)
+ monkeypatch.setattr("invokeai.backend.util.wddm.video_memory_budget", lambda device: budget_bytes)
+
+ assert estimate < VAE_PRETILE_VRAM_FRACTION * total_bytes, "test shape must look fine against the card total"
+ assert estimate > budget_bytes, "test shape must exceed what Windows would keep resident"
+
+ assert should_pretile_vae_decode(torch.device("cuda", 0), estimate) is True