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(minimax-h3): size VAE decode memory from measured peaks

Lincoln Steindb2ce749537a
diff --git a/invokeai/backend/util/vae_working_memory.py b/invokeai/backend/util/vae_working_memory.py
index e1be5c4c60..5686af9559 100644
--- a/invokeai/backend/util/vae_working_memory.py
+++ b/invokeai/backend/util/vae_working_memory.py
@@ -125,6 +125,15 @@ def estimate_vae_working_memory_anima(
return int(working_memory)
+# Bytes of chunk working set per output pixel per element byte, measured at ~12 on a W7900 (a
+# 768x1344 28-frame chunk peaks at 1.31 GiB allocated in fp32) and rounded up for other canvases.
+MINIMAX_H3_CHUNK_BYTES_PER_PIXEL = 14
+
+# The caching allocator holds more than is live — measured 1.3-1.7x across tile-heavy decodes — and
+# the reservation has to cover what it holds.
+MINIMAX_H3_ALLOCATOR_HEADROOM = 1.6
+
+
def estimate_vae_working_memory_minimax_h3(
operation: Literal["encode", "decode"],
vae: "torch.nn.Module",
@@ -134,25 +143,49 @@ def estimate_vae_working_memory_minimax_h3(
) -> int:
"""Estimate the working memory to encode/decode with the MiniMax H3 video VAE.
- The H3 VAE always tiles spatially (``use_tiling`` defaults to True with 256px tiles / 64px
- overlap, and the released frames are the blended-tile ones), so the conv working set is
- per-tile, not per-frame: the tile geometry is read from the instance. The full RGB clip is
- still assembled on the execution device (fp32 — the VAE's weights are pinned fp32), plus
- one transient copy. The Wan per-pixel calibration constant is kept as a conservative
- stand-in until an H3-specific calibration exists.
+ Two terms, both measured rather than borrowed from Wan (whose VAE is a pixel-resolution conv
+ stack decoded one frame at a time; H3's decoder is a ViT over the 16x16 latent grid, so Wan's
+ per-pixel constant is orders of magnitude too large here):
+
+ - **Chunk term.** The VAE processes one temporal chunk at a time, spatially tiled
+ (``use_tiling`` defaults to True with 256px tiles; the released frames are the blended-tile
+ ones). ``_decode_clip`` materializes ``tokens_chunk_size + token_overlap`` latent frames — 28
+ pixel frames with the released geometry — as tile activations, the accumulated tile rows and
+ the stitched chunk; ``_encode_clip`` does the same over ``clip_length`` frames. It therefore
+ scales with chunk frames x canvas, not with clip length.
+ - **Clip term.** ``_decode`` accumulates every chunk and then concatenates, so two copies of
+ the whole RGB clip are live at the peak. Encode keeps one.
+
+ The sum is scaled by :data:`MINIMAX_H3_ALLOCATOR_HEADROOM` because the reservation has to cover
+ what the caching allocator *holds*, not what is live: across the hundreds of tile calls in a
+ long clip, reserved runs ~1.3-1.7x allocated from block rounding and fragmentation. Ignoring
+ that gap is what made a 243-frame 768x1344 decode fail — it needed 7.92 GiB reserved against a
+ 6.49 GiB reservation, and since partial loading packs VRAM up to exactly this number, the
+ shortfall was a hard failure rather than a near miss (on ROCm it surfaces from hipBLAS as
+ HIPBLAS_STATUS_INTERNAL_ERROR, so neither the caller nor the cache recognizes it as an OOM).
+
+ Calibrated 2026-08-09 on a W7900 (gfx1100, fp32, real released config), peak reserved:
+ one 256px tile at 28 frames 0.56 GiB; one 768x1344 chunk 1.81 GiB; a full 768x1344 decode
+ 4.37 GiB at 90 frames and 7.92 GiB at 243 frames. This formula returns ~1.3-1.4x those.
"""
element_size = next(vae.parameters()).element_size()
- scaling_constant = 2900 if operation == "decode" else 1450
- tile_height = min(int(getattr(vae, "tile_sample_min_height", 256)), pixel_height)
- tile_width = min(int(getattr(vae, "tile_sample_min_width", 256)), pixel_width)
- # 1.25 accounts for tile overlap.
- tile_working = tile_height * tile_width * element_size * scaling_constant * 1.25
+ if operation == "decode":
+ tokens_chunk_size = int(getattr(vae, "tokens_chunk_size", 5))
+ token_overlap = int(getattr(vae, "token_overlap", 2))
+ temporal_ratio = int(getattr(vae, "temporal_compression_ratio", 4))
+ chunk_frames = (tokens_chunk_size + token_overlap) * temporal_ratio
+ else:
+ chunk_frames = int(getattr(getattr(vae, "config", None), "clip_length", 17))
+ # A clip with fewer frames than a chunk cannot fill one.
+ chunk_frames = max(1, min(chunk_frames, pixel_frames))
+
+ chunk_bytes = chunk_frames * pixel_height * pixel_width * element_size * MINIMAX_H3_CHUNK_BYTES_PER_PIXEL
clip_copies = 2 if operation == "decode" else 1
clip_bytes = clip_copies * 3 * pixel_frames * pixel_height * pixel_width * element_size
- return int(tile_working + clip_bytes)
+ return int((chunk_bytes + clip_bytes) * MINIMAX_H3_ALLOCATOR_HEADROOM)
def estimate_vae_working_memory_wan(
diff --git a/tests/backend/util/test_vae_working_memory_minimax_h3.py b/tests/backend/util/test_vae_working_memory_minimax_h3.py
new file mode 100644
index 0000000000..8cbd1fc2ba
--- /dev/null
+++ b/tests/backend/util/test_vae_working_memory_minimax_h3.py
@@ -0,0 +1,113 @@
+"""The MiniMax H3 VAE working-memory estimate, calibrated against measured decodes.
+
+Measured 2026-08-09 on a W7900 (gfx1100, fp32, real released config), peak RESERVED — which is what
+the reservation must cover, since the caching allocator holds more than is live:
+
+ one 256px tile, 28 frames 0.56 GiB
+ one 768x1344 chunk, 28 frames 1.81 GiB
+ full 768x1344 decode, 90f 4.37 GiB
+ full 768x1344 decode, 243f 7.92 GiB
+
+The 243-frame case is the one that crashed: it needed 7.92 GiB against a 6.49 GiB reservation, and
+partial loading packs VRAM up to exactly the reservation, so the shortfall was fatal.
+"""
+
+import torch
+
+from invokeai.backend.util.vae_working_memory import (
+ MINIMAX_H3_ALLOCATOR_HEADROOM,
+ MINIMAX_H3_CHUNK_BYTES_PER_PIXEL,
+ estimate_vae_working_memory_minimax_h3,
+)
+
+GIB = 1024**3
+
+# Released FL2VA geometry: 5-token chunks, 2 tokens of overlap, 4x temporal compression (28 pixel
+# frames per chunk call), 17-frame encode clips.
+RELEASED = {"tokens_chunk_size": 5, "token_overlap": 2, "temporal_compression_ratio": 4, "clip_length": 17}
+RELEASED_CHUNK_FRAMES = (5 + 2) * 4
+
+# Deliberately NOT the estimator's fallback defaults, so a renamed attribute read is caught instead
+# of silently resolving to the same number.
+ODD = {"tokens_chunk_size": 3, "token_overlap": 1, "temporal_compression_ratio": 2, "clip_length": 9}
+ODD_CHUNK_FRAMES = (3 + 1) * 2
+
+
+class _FakeVAE(torch.nn.Module):
+ def __init__(self, geometry: dict) -> None:
+ super().__init__()
+ self.param = torch.nn.Parameter(torch.zeros(1, dtype=torch.float32)) # fp32-pinned weights
+ self.tokens_chunk_size = geometry["tokens_chunk_size"]
+ self.token_overlap = geometry["token_overlap"]
+ self.temporal_compression_ratio = geometry["temporal_compression_ratio"]
+
+ class _Config:
+ clip_length = geometry["clip_length"]
+ latent_channels = 24
+
+ self.config = _Config()
+
+
+def _estimate(operation: str, frames: int, width: int = 768, height: int = 1344, geometry: dict = RELEASED) -> int:
+ return estimate_vae_working_memory_minimax_h3(
+ operation=operation, # type: ignore[arg-type]
+ vae=_FakeVAE(geometry),
+ pixel_height=height,
+ pixel_width=width,
+ pixel_frames=frames,
+ )
+
+
+def _expected(chunk_frames: int, frames: int, width: int, height: int, clip_copies: int) -> int:
+ chunk = chunk_frames * height * width * 4 * MINIMAX_H3_CHUNK_BYTES_PER_PIXEL
+ clip = clip_copies * 3 * frames * height * width * 4
+ return int((chunk + clip) * MINIMAX_H3_ALLOCATOR_HEADROOM)
+
+
+def test_decode_matches_the_calibrated_formula():
+ assert _estimate("decode", 243) == _expected(RELEASED_CHUNK_FRAMES, 243, 768, 1344, clip_copies=2)
+
+
+def test_encode_uses_the_clip_length_and_one_clip_copy():
+ assert _estimate("encode", 243) == _expected(RELEASED["clip_length"], 243, 768, 1344, clip_copies=1)
+
+
+def test_geometry_is_read_from_the_instance_not_assumed():
+ """A VAE whose chunking differs from the released defaults must be estimated on its own terms."""
+ odd = _estimate("decode", 243, geometry=ODD)
+ assert odd == _expected(ODD_CHUNK_FRAMES, 243, 768, 1344, clip_copies=2)
+ # ...and that is genuinely different from the released-geometry answer, so a misread attribute
+ # (which would fall back to the released defaults) cannot pass this test.
+ assert odd != _estimate("decode", 243)
+ assert _estimate("encode", 243, geometry=ODD) != _estimate("encode", 243)
+
+
+def test_a_clip_shorter_than_one_chunk_only_pays_for_its_frames():
+ assert _estimate("decode", 5) == _expected(5, 5, 768, 1344, clip_copies=2)
+
+
+def test_covers_the_measured_peak_of_the_decode_that_crashed():
+ # Measured 7.92 GiB reserved for 243 frames at 768x1344; 4.37 GiB at 90 frames.
+ assert _estimate("decode", 243) > 7.92 * GIB
+ assert _estimate("decode", 90) > 4.37 * GIB
+ # ...without the runaway conservatism that would push a 24 GB card's VAE out of VRAM: the VAE
+ # is 9.70 GiB, so the reservation plus weights has to leave room on a 24 GiB device.
+ assert (_estimate("decode", 90, width=768, height=1024) / GIB) + 9.70 < 24
+
+
+def test_grows_with_clip_length_and_canvas():
+ assert _estimate("decode", 243) > _estimate("decode", 90)
+ assert _estimate("decode", 90, width=768, height=1344) > _estimate("decode", 90, width=512, height=512)
+
+
+def test_falls_back_to_released_geometry_when_attributes_are_missing():
+ class _BareVAE(torch.nn.Module):
+ def __init__(self) -> None:
+ super().__init__()
+ self.param = torch.nn.Parameter(torch.zeros(1, dtype=torch.float32))
+
+ bare = estimate_vae_working_memory_minimax_h3(
+ operation="decode", vae=_BareVAE(), pixel_height=1344, pixel_width=768, pixel_frames=243
+ )
+ # Not a tautology against the fake: pinned to the released-geometry arithmetic directly.
+ assert bare == _expected(RELEASED_CHUNK_FRAMES, 243, 768, 1344, clip_copies=2)