|
| 1 | +""" |
| 2 | +Copyright 2026 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +import unittest |
| 18 | +from unittest.mock import MagicMock |
| 19 | +import jax |
| 20 | +import jax.numpy as jnp |
| 21 | + |
| 22 | +from maxdiffusion.pipelines.wan.wan_pipeline_i2v_2p1 import WanPipelineI2V_2_1 |
| 23 | +from maxdiffusion.pipelines.wan.wan_pipeline_i2v_2p2 import WanPipelineI2V_2_2 |
| 24 | + |
| 25 | + |
| 26 | +class WanI2VPrepareLatentsTest(unittest.TestCase): |
| 27 | + |
| 28 | + def _create_mock_pipeline(self, pipeline_cls): |
| 29 | + """Creates a mock pipeline instance with required VAE attributes.""" |
| 30 | + pipeline = object.__new__(pipeline_cls) |
| 31 | + pipeline.vae = MagicMock(z_dim=16) |
| 32 | + pipeline.vae_scale_factor_temporal = 4 |
| 33 | + pipeline.vae_scale_factor_spatial = 8 |
| 34 | + |
| 35 | + def mock_prepare_latents_i2v_base(image, num_frames, dtype, last_image=None, trace=None): |
| 36 | + num_latent_frames = (num_frames - 1) // pipeline.vae_scale_factor_temporal + 1 |
| 37 | + latent_height = 32 // pipeline.vae_scale_factor_spatial |
| 38 | + latent_width = 32 // pipeline.vae_scale_factor_spatial |
| 39 | + latent_condition = jnp.zeros( |
| 40 | + (image.shape[0], num_latent_frames, latent_height, latent_width, pipeline.vae.z_dim), |
| 41 | + dtype=dtype, |
| 42 | + ) |
| 43 | + return latent_condition, None |
| 44 | + |
| 45 | + pipeline.prepare_latents_i2v_base = MagicMock(side_effect=mock_prepare_latents_i2v_base) |
| 46 | + return pipeline |
| 47 | + |
| 48 | + def test_single_image_repetition(self): |
| 49 | + """Verifies that a single conditioning image is repeated when batch_size > 1.""" |
| 50 | + rng = jax.random.key(0) |
| 51 | + for pipeline_cls in (WanPipelineI2V_2_1, WanPipelineI2V_2_2): |
| 52 | + with self.subTest(pipeline=pipeline_cls.__name__): |
| 53 | + pipeline = self._create_mock_pipeline(pipeline_cls) |
| 54 | + image = jnp.zeros((1, 3, 32, 32)) |
| 55 | + batch_size = 4 |
| 56 | + latents, condition, _ = pipeline.prepare_latents( |
| 57 | + image=image, |
| 58 | + batch_size=batch_size, |
| 59 | + height=32, |
| 60 | + width=32, |
| 61 | + num_frames=5, |
| 62 | + dtype=jnp.float32, |
| 63 | + rng=rng, |
| 64 | + ) |
| 65 | + self.assertEqual(latents.shape[0], batch_size) |
| 66 | + self.assertEqual(condition.shape[0], batch_size) |
| 67 | + call_image = pipeline.prepare_latents_i2v_base.call_args[0][0] |
| 68 | + self.assertEqual(call_image.shape[0], batch_size) |
| 69 | + |
| 70 | + def test_batched_image_repetition(self): |
| 71 | + """Verifies that multiple conditioning images are repeated correctly when divisible.""" |
| 72 | + rng = jax.random.key(0) |
| 73 | + for pipeline_cls in (WanPipelineI2V_2_1, WanPipelineI2V_2_2): |
| 74 | + with self.subTest(pipeline=pipeline_cls.__name__): |
| 75 | + pipeline = self._create_mock_pipeline(pipeline_cls) |
| 76 | + image = jnp.zeros((2, 3, 32, 32)) |
| 77 | + batch_size = 4 |
| 78 | + latents, condition, _ = pipeline.prepare_latents( |
| 79 | + image=image, |
| 80 | + batch_size=batch_size, |
| 81 | + height=32, |
| 82 | + width=32, |
| 83 | + num_frames=5, |
| 84 | + dtype=jnp.float32, |
| 85 | + rng=rng, |
| 86 | + ) |
| 87 | + self.assertEqual(latents.shape[0], batch_size) |
| 88 | + self.assertEqual(condition.shape[0], batch_size) |
| 89 | + call_image = pipeline.prepare_latents_i2v_base.call_args[0][0] |
| 90 | + self.assertEqual(call_image.shape[0], batch_size) |
| 91 | + |
| 92 | + def test_with_last_image(self): |
| 93 | + """Verifies that both start and last images are repeated when provided.""" |
| 94 | + rng = jax.random.key(0) |
| 95 | + for pipeline_cls in (WanPipelineI2V_2_1, WanPipelineI2V_2_2): |
| 96 | + with self.subTest(pipeline=pipeline_cls.__name__): |
| 97 | + pipeline = self._create_mock_pipeline(pipeline_cls) |
| 98 | + image = jnp.zeros((1, 3, 32, 32)) |
| 99 | + last_image = jnp.zeros((1, 3, 32, 32)) |
| 100 | + batch_size = 3 |
| 101 | + latents, condition, _ = pipeline.prepare_latents( |
| 102 | + image=image, |
| 103 | + batch_size=batch_size, |
| 104 | + height=32, |
| 105 | + width=32, |
| 106 | + num_frames=5, |
| 107 | + dtype=jnp.float32, |
| 108 | + rng=rng, |
| 109 | + last_image=last_image, |
| 110 | + ) |
| 111 | + self.assertEqual(latents.shape[0], batch_size) |
| 112 | + self.assertEqual(condition.shape[0], batch_size) |
| 113 | + call_image = pipeline.prepare_latents_i2v_base.call_args[0][0] |
| 114 | + call_last_image = pipeline.prepare_latents_i2v_base.call_args[0][3] |
| 115 | + self.assertEqual(call_image.shape[0], batch_size) |
| 116 | + self.assertEqual(call_last_image.shape[0], batch_size) |
| 117 | + |
| 118 | + def test_indivisible_image_batch_size_raises(self): |
| 119 | + """Verifies ValueError when batch_size is not divisible by image batch size.""" |
| 120 | + rng = jax.random.key(0) |
| 121 | + for pipeline_cls in (WanPipelineI2V_2_1, WanPipelineI2V_2_2): |
| 122 | + with self.subTest(pipeline=pipeline_cls.__name__): |
| 123 | + pipeline = self._create_mock_pipeline(pipeline_cls) |
| 124 | + image = jnp.zeros((2, 3, 32, 32)) |
| 125 | + with self.assertRaisesRegex(ValueError, "divisible by image batch size"): |
| 126 | + pipeline.prepare_latents( |
| 127 | + image=image, |
| 128 | + batch_size=3, |
| 129 | + height=32, |
| 130 | + width=32, |
| 131 | + num_frames=5, |
| 132 | + dtype=jnp.float32, |
| 133 | + rng=rng, |
| 134 | + ) |
| 135 | + |
| 136 | + def test_indivisible_last_image_batch_size_raises(self): |
| 137 | + """Verifies ValueError when batch_size is not divisible by last_image batch size.""" |
| 138 | + rng = jax.random.key(0) |
| 139 | + for pipeline_cls in (WanPipelineI2V_2_1, WanPipelineI2V_2_2): |
| 140 | + with self.subTest(pipeline=pipeline_cls.__name__): |
| 141 | + pipeline = self._create_mock_pipeline(pipeline_cls) |
| 142 | + image = jnp.zeros((1, 3, 32, 32)) |
| 143 | + last_image = jnp.zeros((2, 3, 32, 32)) |
| 144 | + with self.assertRaisesRegex(ValueError, "divisible by last_image batch size"): |
| 145 | + pipeline.prepare_latents( |
| 146 | + image=image, |
| 147 | + batch_size=3, |
| 148 | + height=32, |
| 149 | + width=32, |
| 150 | + num_frames=5, |
| 151 | + dtype=jnp.float32, |
| 152 | + rng=rng, |
| 153 | + last_image=last_image, |
| 154 | + ) |
| 155 | + |
| 156 | + def test_mismatched_image_and_last_image_batch_sizes_raises(self): |
| 157 | + """Verifies ValueError when image and last_image have conflicting batch sizes > 1.""" |
| 158 | + rng = jax.random.key(0) |
| 159 | + for pipeline_cls in (WanPipelineI2V_2_1, WanPipelineI2V_2_2): |
| 160 | + with self.subTest(pipeline=pipeline_cls.__name__): |
| 161 | + pipeline = self._create_mock_pipeline(pipeline_cls) |
| 162 | + image = jnp.zeros((2, 3, 32, 32)) |
| 163 | + last_image = jnp.zeros((3, 3, 32, 32)) |
| 164 | + with self.assertRaisesRegex(ValueError, "must match when both are greater than 1"): |
| 165 | + pipeline.prepare_latents( |
| 166 | + image=image, |
| 167 | + batch_size=6, |
| 168 | + height=32, |
| 169 | + width=32, |
| 170 | + num_frames=5, |
| 171 | + dtype=jnp.float32, |
| 172 | + rng=rng, |
| 173 | + last_image=last_image, |
| 174 | + ) |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == "__main__": |
| 178 | + unittest.main() |
0 commit comments