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.

9.4 KB
Raw Blame
"""Tests for the Wan 2.2 I2V reference-image VAE-latent encoder helper."""

from unittest.mock import MagicMock

import pytest
import torch
from PIL import Image

from invokeai.backend.wan.extensions.wan_ref_image_extension import (
    encode_reference_image_to_condition,
    encode_reference_image_to_video_condition,
    preprocess_reference_image,
)


def _make_fake_vae(z_dim: int = 16, spatial_scale: int = 8, temporal_scale: int = 4) -> MagicMock:
    """Stand-in for ``AutoencoderKLWan`` that returns deterministic latents.

    ``encode(pixel)`` returns a fake distribution whose ``mode()`` yields
    a tensor sized exactly as the real Wan VAE would: ``[B, z_dim, T_lat, H/8, W/8]``.
    """
    vae = MagicMock()

    # ``next(iter(vae.parameters())).dtype`` is queried; pin to float32.
    param = torch.zeros(1, dtype=torch.float32)
    vae.parameters = MagicMock(return_value=iter([param]))

    # Config carries per-channel normalisation stats.
    vae.config = MagicMock()
    vae.config.latents_mean = [0.0] * z_dim
    vae.config.latents_std = [1.0] * z_dim

    def fake_encode(pixel: torch.Tensor, return_dict: bool = False):
        b, _, t, h, w = pixel.shape
        t_lat = (t - 1) // temporal_scale + 1
        h_lat = h // spatial_scale
        w_lat = w // spatial_scale
        latents = torch.zeros(b, z_dim, t_lat, h_lat, w_lat, dtype=pixel.dtype)

        dist = MagicMock()
        dist.mode = MagicMock(return_value=latents)
        # The pipeline does ``vae.encode(...)[0]`` for non-dict returns.
        return (dist,) if return_dict is False else MagicMock(latent_dist=dist)

    vae.encode = fake_encode
    return vae


class TestPreprocess:
    def test_resize_to_target_dims(self):
        img = Image.new("RGB", (200, 300), (128, 128, 128))
        out = preprocess_reference_image(img, width=64, height=64)
        # Shape: [batch=1, channels=3, time=1, H, W]
        assert out.shape == (1, 3, 1, 64, 64)

    def test_normalised_to_minus_one_to_one(self):
        # Pure-grey image preprocessed should be exactly 0 (since 128/255*2 - 1 ≈ 0.004).
        img = Image.new("RGB", (64, 64), (255, 255, 255))
        out = preprocess_reference_image(img, width=64, height=64)
        # White → 1.0
        assert torch.allclose(out, torch.ones_like(out), atol=1e-4)

        black = Image.new("RGB", (64, 64), (0, 0, 0))
        out_b = preprocess_reference_image(black, width=64, height=64)
        # Black → -1.0
        assert torch.allclose(out_b, -torch.ones_like(out_b), atol=1e-4)

    def test_rejects_non_multiple_of_8(self):
        img = Image.new("RGB", (100, 100))
        import pytest

        with pytest.raises(ValueError, match="multiples of 8"):
            preprocess_reference_image(img, width=65, height=64)


class TestEncodeReferenceImageToCondition:
    """The condition tensor must be 20-channel (4 mask + 16 image latents)
    and shaped for the denoise step's later concat with 16-ch noise latents."""

    def test_shape_at_64x64(self):
        img = Image.new("RGB", (64, 64))
        vae = _make_fake_vae()
        cond = encode_reference_image_to_condition(
            image=img, vae=vae, width=64, height=64, device=torch.device("cpu"), dtype=torch.float32
        )
        # [1, 20, 1, 8, 8] — 4-ch mask + 16-ch latents at H/8, W/8.
        assert cond.shape == (1, 20, 1, 8, 8)

    def test_shape_at_1024x1024(self):
        img = Image.new("RGB", (1024, 1024))
        vae = _make_fake_vae()
        cond = encode_reference_image_to_condition(
            image=img, vae=vae, width=1024, height=1024, device=torch.device("cpu"), dtype=torch.float32
        )
        # 1024/8 = 128 latent spatial dim.
        assert cond.shape == (1, 20, 1, 128, 128)

    def test_first_four_channels_are_all_ones_mask(self):
        img = Image.new("RGB", (64, 64))
        vae = _make_fake_vae()
        cond = encode_reference_image_to_condition(
            image=img, vae=vae, width=64, height=64, device=torch.device("cpu"), dtype=torch.float32
        )
        mask = cond[:, :4]
        # First-frame mask is all-ones at num_frames=1 (every position is the first frame).
        assert torch.equal(mask, torch.ones_like(mask))

    def test_returns_dtype(self):
        img = Image.new("RGB", (64, 64))
        vae = _make_fake_vae()
        cond = encode_reference_image_to_condition(
            image=img, vae=vae, width=64, height=64, device=torch.device("cpu"), dtype=torch.bfloat16
        )
        assert cond.dtype == torch.bfloat16


class TestEncodeReferenceImageToVideoCondition:
    """Multi-frame A14B I2V condition, including first-last-frame (FLF2V) mode.

    The fake VAE returns zero latents, so we can't assert on the *content* of the
    last frame — but the 4-channel mask is built independently of the VAE output,
    so we can verify exactly which latent frames get anchored.
    """

    def test_video_condition_shape(self):
        img = Image.new("RGB", (64, 64))
        vae = _make_fake_vae()
        # num_frames=5 → t_lat = (5-1)//4 + 1 = 2.
        cond = encode_reference_image_to_video_condition(
            image=img, vae=vae, width=64, height=64, num_frames=5, device=torch.device("cpu"), dtype=torch.float32
        )
        assert cond.shape == (1, 20, 2, 8, 8)

    def test_single_reference_anchors_only_first_latent_frame(self):
        img = Image.new("RGB", (64, 64))
        vae = _make_fake_vae()
        cond = encode_reference_image_to_video_condition(
            image=img, vae=vae, width=64, height=64, num_frames=5, device=torch.device("cpu"), dtype=torch.float32
        )
        mask = cond[:, :4]  # [1, 4, t_lat=2, 8, 8]
        # First latent frame fully anchored; the rest entirely free.
        assert torch.equal(mask[:, :, 0], torch.ones_like(mask[:, :, 0]))
        assert torch.equal(mask[:, :, 1:], torch.zeros_like(mask[:, :, 1:]))

    def test_flf2v_anchors_first_and_last_latent_frame(self):
        img = Image.new("RGB", (64, 64))
        end = Image.new("RGB", (64, 64), (255, 255, 255))
        vae = _make_fake_vae()
        cond = encode_reference_image_to_video_condition(
            image=img,
            vae=vae,
            width=64,
            height=64,
            num_frames=5,
            device=torch.device("cpu"),
            dtype=torch.float32,
            last_image=end,
        )
        mask = cond[:, :4]  # [1, 4, 2, 8, 8]
        # First latent frame still fully anchored.
        assert torch.equal(mask[:, :, 0], torch.ones_like(mask[:, :, 0]))
        # The end image anchors the final pixel-frame, which lands in the last
        # mask channel of the last latent frame (and only there).
        assert torch.equal(mask[:, 3, 1], torch.ones_like(mask[:, 3, 1]))
        assert torch.equal(mask[:, :3, 1], torch.zeros_like(mask[:, :3, 1]))

    def test_flf2v_requires_multiple_frames(self):
        img = Image.new("RGB", (64, 64))
        end = Image.new("RGB", (64, 64))
        vae = _make_fake_vae()
        with pytest.raises(ValueError, match="FLF2V"):
            encode_reference_image_to_video_condition(
                image=img,
                vae=vae,
                width=64,
                height=64,
                num_frames=1,
                device=torch.device("cpu"),
                dtype=torch.float32,
                last_image=end,
            )


class TestEncodeReferenceImageToTI2VCondition:
    """TI2V-5B's condition tensor is a single 48-channel latent frame (no mask
    channels) at the Wan2.2-VAE's 16x spatial scale. The denoise loop blends
    this with the noise via a first_frame_mask at each step.
    """

    def test_shape_at_64x64(self):
        from invokeai.backend.wan.extensions.wan_ref_image_extension import (
            encode_reference_image_to_ti2v_condition,
        )

        img = Image.new("RGB", (64, 64))
        vae = _make_fake_vae(z_dim=48, spatial_scale=16, temporal_scale=4)
        cond = encode_reference_image_to_ti2v_condition(
            image=img, vae=vae, width=64, height=64, device=torch.device("cpu"), dtype=torch.float32
        )
        # [1, 48, 1, 4, 4] — single latent frame at H/16, W/16.
        assert cond.shape == (1, 48, 1, 4, 4)

    def test_shape_at_832x480(self):
        # Common Wan video resolution: latent 30x52 single frame.
        from invokeai.backend.wan.extensions.wan_ref_image_extension import (
            encode_reference_image_to_ti2v_condition,
        )

        img = Image.new("RGB", (832, 480))
        vae = _make_fake_vae(z_dim=48, spatial_scale=16, temporal_scale=4)
        cond = encode_reference_image_to_ti2v_condition(
            image=img, vae=vae, width=832, height=480, device=torch.device("cpu"), dtype=torch.float32
        )
        assert cond.shape == (1, 48, 1, 30, 52)

    def test_no_mask_channels(self):
        # Distinguishing feature vs A14B: no leading 4-ch mask. All 48 channels
        # are latent content. With latents_mean=0 and latents_std=1, encoded zeros
        # stay zero (the function returns latents straight through).
        from invokeai.backend.wan.extensions.wan_ref_image_extension import (
            encode_reference_image_to_ti2v_condition,
        )

        img = Image.new("RGB", (64, 64))
        vae = _make_fake_vae(z_dim=48, spatial_scale=16, temporal_scale=4)
        cond = encode_reference_image_to_ti2v_condition(
            image=img, vae=vae, width=64, height=64, device=torch.device("cpu"), dtype=torch.float32
        )
        # Fake VAE.encode returns zero latents; normalized zeros stay zero.
        assert torch.equal(cond, torch.zeros_like(cond))