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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
"""Tests for the MiniMax H3 packed-sequence math and denoise-state construction."""
import dataclasses
from collections.abc import Iterator
import pytest
import torch
from invokeai.backend.minimax_h3.packing import (
align_num_frames,
audio_latent_num_frames,
resolve_canvas_size,
video_latent_num_frames,
)
from invokeai.backend.minimax_h3.presets import MINIMAX_H3_VIDEO_FRAME_CHOICES, resolve_lowres_canvas_size
from invokeai.backend.minimax_h3.sampling import (
MiniMaxH3DenoiseState,
MiniMaxH3EncodedReference,
build_denoise_state,
build_ref2va_denoise_state,
validate_canvas,
validate_num_frames,
)
from invokeai.backend.minimax_h3.scheduling_minimax_h3 import MiniMaxH3Scheduler
from invokeai.backend.minimax_h3.transformer_minimax_h3 import MiniMaxH3RotaryPosEmbed
class TestCanvas:
def test_every_resolvable_canvas_is_accepted(self):
# The guard must never reject a canvas resolve_canvas_size can emit. Rounding both axes to
# the 32 grid happens *after* the area cap, so its own outputs reach ~1.04x the cap.
canvases = set()
for index in range(1501):
ratio = min(0.25 + index * (3.75 / 1500), 4.0)
canvases.add(resolve_canvas_size(ratio, 1.0))
assert len(canvases) > 50
for height, width in canvases:
validate_canvas(height, width)
def test_native_canvas_accepted(self):
validate_canvas(768, 1344)
validate_canvas(1344, 768)
def test_oversized_canvas_rejected(self):
# 4096x4096 builds a ~607k-row packed sequence against the native canvas's ~38k.
with pytest.raises(ValueError, match="soft area cap"):
validate_canvas(4096, 4096)
with pytest.raises(ValueError, match="soft area cap"):
validate_canvas(2048, 2048)
def test_off_grid_canvas_rejected(self):
with pytest.raises(ValueError, match="multiples of 32"):
validate_canvas(768, 1000)
def test_extreme_aspect_ratio_rejected(self):
with pytest.raises(ValueError, match="aspect ratios"):
validate_canvas(128, 1344)
def test_nonpositive_rejected(self):
with pytest.raises(ValueError, match="must be positive"):
validate_canvas(0, 1344)
class TestFrameGrid:
def test_legal_frame_counts_pass(self):
# 90 (3.75 s) is the accepted floor for video; 345 = 17*20+5 (14.375 s) is the longest legal
# clip, since 362 rounds past the 15 s cap.
for n in (5, 90, 107, 124, 141, 345):
validate_num_frames(n)
def test_misaligned_frame_counts_rejected(self):
with pytest.raises(ValueError, match="17"):
validate_num_frames(6)
with pytest.raises(ValueError, match="17"):
validate_num_frames(121)
def test_frame_counts_outside_the_accepted_range_rejected(self):
# 22/39/56/73 are grid-aligned but below the 90-frame floor (and not the still path).
for n in (22, 39, 56, 73):
with pytest.raises(ValueError, match="frames on the 17n"):
validate_num_frames(n)
# 362 = 17*21+5 is aligned but 15.083 s > 15 s.
with pytest.raises(ValueError, match="frames on the 17n"):
validate_num_frames(362)
def test_still_image_minimum_allowed(self):
validate_num_frames(5)
def test_every_offered_choice_validates(self):
# The node's dropdown is built from this tuple; nothing in it may be rejected at invoke time.
assert MINIMAX_H3_VIDEO_FRAME_CHOICES[0] == 90
assert MINIMAX_H3_VIDEO_FRAME_CHOICES[-1] == 345
for n in MINIMAX_H3_VIDEO_FRAME_CHOICES:
validate_num_frames(n)
def test_choices_are_exactly_the_grid_points_in_range(self):
expected = tuple(n for n in range(90, 361) if n % 17 == 5)
assert MINIMAX_H3_VIDEO_FRAME_CHOICES == expected
def test_align_num_frames(self):
assert align_num_frames(121) == 124
assert align_num_frames(5) == 5
assert align_num_frames(1) == 5
def test_latent_frame_math(self):
assert video_latent_num_frames(5) == 2
assert video_latent_num_frames(124) == 37
with pytest.raises(ValueError):
video_latent_num_frames(6)
def test_audio_latent_math(self):
# 40 latents/s at 24 fps.
assert audio_latent_num_frames(24) == 40
assert audio_latent_num_frames(124) == round(124 / 24 * 40)
class TestLowresCanvasResolution:
# The native short-edge-768 policy is covered by tests/app/invocations/test_minimax_h3_ideal_dimensions.py.
def test_lowres_pins_the_long_edge(self):
# Same aspect inputs, but the LONG edge is fixed at 768.
assert resolve_lowres_canvas_size(1, 1) == (768, 768)
# 2:3 portrait -> 512x768 (the user-facing "small test render" case).
assert resolve_lowres_canvas_size(800, 1200) == (768, 512)
assert resolve_lowres_canvas_size(1200, 800) == (512, 768)
# 16:9 -> short edge 432 rounds to the nearest multiple of 32 (448).
assert resolve_lowres_canvas_size(16, 9) == (448, 768)
assert resolve_lowres_canvas_size(9, 16) == (768, 448)
def test_lowres_never_exceeds_the_highres_area(self):
for w, h in ((1, 1), (16, 9), (9, 16), (2, 3), (4, 1), (1, 4), (1024, 1000)):
lo_h, lo_w = resolve_lowres_canvas_size(w, h)
hi_h, hi_w = resolve_canvas_size(w, h)
assert lo_h * lo_w <= hi_h * hi_w
assert lo_h % 32 == 0 and lo_w % 32 == 0
def test_extreme_aspect_ratios_stay_on_grid(self):
# 4:1 is the limit of the supported envelope; 768/4 = 192 divides the grid exactly (6 * 32).
assert resolve_lowres_canvas_size(4, 1) == (192, 768)
assert resolve_lowres_canvas_size(1, 4) == (768, 192)
def test_invalid_aspects_rejected_by_both_presets(self):
for fn in (resolve_canvas_size, resolve_lowres_canvas_size):
with pytest.raises(ValueError, match="positive"):
fn(0, 100)
with pytest.raises(ValueError, match="1:4"):
fn(9, 1)
def _build_state(seed: int, with_keyframe: bool = False, device: str = "cpu"):
kwargs = {}
if with_keyframe:
# One keyframe -> rows_per_frame = (4//2) * (4//2) = 4 condition rows of width 96.
kwargs = {
"keyframe_anchors": ("first",),
"clean_condition_rows": torch.zeros(4, 96),
}
return build_denoise_state(
text_token_tags=torch.tensor([1, 1, 0], dtype=torch.long),
num_latent_frames=2,
latent_height=4,
latent_width=4,
num_audio_latents=8,
num_inference_steps=4,
seed=seed,
device=torch.device(device),
**kwargs,
)
class TestDenoiseState:
def test_shapes_and_layout(self):
state = _build_state(seed=123)
# 2 latent frames x (4/2 * 4/2) rows/frame = 8 video rows, patch dim 24*1*2*2 = 96.
assert state.video_rows.shape == (8, 96)
# 8 audio latents x 2 stereo channels, 32 channels each.
assert state.audio_rows.shape == (16, 32)
assert state.layout.num_condition_video_rows == 0
assert state.layout.sequence_length == 3 + 0 + 16 + 8
# num_inference_steps counts sigma grid points (terminal included): N -> N-1 evals.
assert len(state.row_timestep_plan) == 3
assert len(state.timesteps) == 3
def test_noise_is_deterministic_per_seed(self):
a, b = _build_state(seed=7), _build_state(seed=7)
assert torch.equal(a.video_rows, b.video_rows)
assert torch.equal(a.audio_rows, b.audio_rows)
c = _build_state(seed=8)
assert not torch.equal(a.video_rows, c.video_rows)
assert not torch.equal(a.audio_rows, c.audio_rows)
def test_keyframe_draw_shifts_generated_noise(self):
# The keyframe conditioning noise is the generator's FIRST draw, so the video/audio
# draws of a keyframed request differ from an unkeyframed one at the same seed.
plain = _build_state(seed=7)
keyframed = _build_state(seed=7, with_keyframe=True)
assert keyframed.layout.num_condition_video_rows == 4
assert keyframed.video_rows.shape == (12, 96) # 4 condition rows + 8 generated
assert not torch.equal(keyframed.video_rows[4:], plain.video_rows)
def test_condition_rows_are_noise_augmented(self):
keyframed = _build_state(seed=7, with_keyframe=True)
# Clean rows were zeros; scale_noise(x0=0, t=0.999, noise) = 0.001 * noise != 0.
condition = keyframed.video_rows[:4]
assert not torch.equal(condition, torch.zeros_like(condition))
assert condition.abs().max() < 0.1 # 0.1% of unit-normal noise
def test_mismatched_condition_rows_rejected(self):
with pytest.raises(ValueError, match="rows"):
build_denoise_state(
text_token_tags=torch.tensor([1], dtype=torch.long),
num_latent_frames=2,
latent_height=4,
latent_width=4,
num_audio_latents=8,
num_inference_steps=2,
seed=0,
device=torch.device("cpu"),
keyframe_anchors=("first",),
clean_condition_rows=torch.zeros(3, 96), # wrong: layout expects 4
)
def test_anchors_and_rows_must_travel_together(self):
with pytest.raises(ValueError, match="together"):
build_denoise_state(
text_token_tags=torch.tensor([1], dtype=torch.long),
num_latent_frames=2,
latent_height=4,
latent_width=4,
num_audio_latents=8,
num_inference_steps=2,
seed=0,
device=torch.device("cpu"),
keyframe_anchors=("first",),
clean_condition_rows=None,
)
def _build_ref2va_state(device: str) -> MiniMaxH3DenoiseState:
# One image reference whose (1, 6, 2) latent packs into 3 condition rows at the micro geometry.
return build_ref2va_denoise_state(
text_token_tags=torch.tensor([1, 1, 0], dtype=torch.long),
references=[MiniMaxH3EncodedReference(kind="image", video_rows=torch.zeros(3, 96), latent_shape=(1, 6, 2))],
num_latent_frames=2,
latent_height=4,
latent_width=4,
num_audio_latents=8,
num_inference_steps=4,
seed=0,
device=torch.device(device),
)
_STATE_BUILDERS = {
"fl2va": lambda device: _build_state(seed=0, with_keyframe=True, device=device),
"ref2va": _build_ref2va_state,
}
_CUDA = pytest.param("cuda", marks=pytest.mark.skipif(not torch.cuda.is_available(), reason="requires CUDA device"))
_MPS = pytest.param(
"mps", marks=pytest.mark.skipif(not torch.backends.mps.is_available(), reason="requires MPS device")
)
def _device_tensors(state: MiniMaxH3DenoiseState) -> Iterator[tuple[str, torch.Tensor]]:
"""Every tensor the denoise loop receives. The layout stays on CPU in float64 by design and is skipped."""
def walk(value: object, path: str) -> Iterator[tuple[str, torch.Tensor]]:
if isinstance(value, torch.Tensor):
yield path, value
elif isinstance(value, (list, tuple)):
for index, item in enumerate(value):
yield from walk(item, f"{path}[{index}]")
elif isinstance(value, MiniMaxH3Scheduler):
for name, item in vars(value).items():
yield from walk(item, f"{path}.{name}")
for field in dataclasses.fields(state):
if field.name != "layout":
yield from walk(getattr(state, field.name), field.name)
class TestDevicePortableState:
@pytest.mark.parametrize("device", ["cpu", _CUDA, _MPS])
@pytest.mark.parametrize("builder", sorted(_STATE_BUILDERS))
def test_no_float64_tensor_reaches_the_device(self, builder: str, device: str):
# MPS has no float64: any float64 tensor in the state fails the request on Apple Silicon.
state = _STATE_BUILDERS[builder](device)
tensors = dict(_device_tensors(state))
assert {"position_ids", "row_timestep_plan[0][0]", "scheduler.sigmas"} <= tensors.keys()
assert state.layout.position_ids.dtype == torch.float64
assert [path for path, tensor in tensors.items() if tensor.dtype == torch.float64] == []
assert state.position_ids.dtype == torch.float32
assert state.position_ids.device.type == device
@pytest.mark.parametrize("device", [_MPS])
@pytest.mark.parametrize("builder", sorted(_STATE_BUILDERS))
def test_rope_runs_on_the_state(self, builder: str, device: str):
state = _STATE_BUILDERS[builder](device)
rope = MiniMaxH3RotaryPosEmbed().to(device)
cos, sin = rope(state.position_ids)
assert cos.shape == sin.shape == (state.layout.sequence_length, 6 * rope.rope_freq_dim)
assert torch.isfinite(cos).all() and torch.isfinite(sin).all()