-
Notifications
You must be signed in to change notification settings - Fork 92
Repeat conditioning images along batch dimension in prepare_latents #469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prishajain1
wants to merge
1
commit into
main
Choose a base branch
from
test_fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
src/maxdiffusion/tests/wan/wan_i2v_prepare_latents_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| """ | ||
| Copyright 2026 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| """ | ||
|
|
||
| import unittest | ||
| from unittest.mock import MagicMock | ||
| import jax | ||
| import jax.numpy as jnp | ||
|
|
||
| from maxdiffusion.pipelines.wan.wan_pipeline_i2v_2p1 import WanPipelineI2V_2_1 | ||
| from maxdiffusion.pipelines.wan.wan_pipeline_i2v_2p2 import WanPipelineI2V_2_2 | ||
|
|
||
|
|
||
| class WanI2VPrepareLatentsTest(unittest.TestCase): | ||
|
|
||
| def _create_mock_pipeline(self, pipeline_cls): | ||
| """Creates a mock pipeline instance with required VAE attributes.""" | ||
| pipeline = object.__new__(pipeline_cls) | ||
| pipeline.vae = MagicMock(z_dim=16) | ||
| pipeline.vae_scale_factor_temporal = 4 | ||
| pipeline.vae_scale_factor_spatial = 8 | ||
|
|
||
| def mock_prepare_latents_i2v_base(image, num_frames, dtype, last_image=None, trace=None): | ||
| num_latent_frames = (num_frames - 1) // pipeline.vae_scale_factor_temporal + 1 | ||
| latent_height = 32 // pipeline.vae_scale_factor_spatial | ||
| latent_width = 32 // pipeline.vae_scale_factor_spatial | ||
| latent_condition = jnp.zeros( | ||
| (image.shape[0], num_latent_frames, latent_height, latent_width, pipeline.vae.z_dim), | ||
| dtype=dtype, | ||
| ) | ||
| return latent_condition, None | ||
|
|
||
| pipeline.prepare_latents_i2v_base = MagicMock(side_effect=mock_prepare_latents_i2v_base) | ||
| return pipeline | ||
|
|
||
| def test_single_image_repetition(self): | ||
| """Verifies that a single conditioning image is repeated when batch_size > 1.""" | ||
| rng = jax.random.key(0) | ||
| for pipeline_cls in (WanPipelineI2V_2_1, WanPipelineI2V_2_2): | ||
| with self.subTest(pipeline=pipeline_cls.__name__): | ||
| pipeline = self._create_mock_pipeline(pipeline_cls) | ||
| image = jnp.zeros((1, 3, 32, 32)) | ||
| batch_size = 4 | ||
| latents, condition, _ = pipeline.prepare_latents( | ||
| image=image, | ||
| batch_size=batch_size, | ||
| height=32, | ||
| width=32, | ||
| num_frames=5, | ||
| dtype=jnp.float32, | ||
| rng=rng, | ||
| ) | ||
| self.assertEqual(latents.shape[0], batch_size) | ||
| self.assertEqual(condition.shape[0], batch_size) | ||
| call_image = pipeline.prepare_latents_i2v_base.call_args[0][0] | ||
| self.assertEqual(call_image.shape[0], batch_size) | ||
|
|
||
| def test_batched_image_repetition(self): | ||
| """Verifies that multiple conditioning images are repeated correctly when divisible.""" | ||
| rng = jax.random.key(0) | ||
| for pipeline_cls in (WanPipelineI2V_2_1, WanPipelineI2V_2_2): | ||
| with self.subTest(pipeline=pipeline_cls.__name__): | ||
| pipeline = self._create_mock_pipeline(pipeline_cls) | ||
| image = jnp.zeros((2, 3, 32, 32)) | ||
| batch_size = 4 | ||
| latents, condition, _ = pipeline.prepare_latents( | ||
| image=image, | ||
| batch_size=batch_size, | ||
| height=32, | ||
| width=32, | ||
| num_frames=5, | ||
| dtype=jnp.float32, | ||
| rng=rng, | ||
| ) | ||
| self.assertEqual(latents.shape[0], batch_size) | ||
| self.assertEqual(condition.shape[0], batch_size) | ||
| call_image = pipeline.prepare_latents_i2v_base.call_args[0][0] | ||
| self.assertEqual(call_image.shape[0], batch_size) | ||
|
|
||
| def test_with_last_image(self): | ||
| """Verifies that both start and last images are repeated when provided.""" | ||
| rng = jax.random.key(0) | ||
| for pipeline_cls in (WanPipelineI2V_2_1, WanPipelineI2V_2_2): | ||
| with self.subTest(pipeline=pipeline_cls.__name__): | ||
| pipeline = self._create_mock_pipeline(pipeline_cls) | ||
| image = jnp.zeros((1, 3, 32, 32)) | ||
| last_image = jnp.zeros((1, 3, 32, 32)) | ||
| batch_size = 3 | ||
| latents, condition, _ = pipeline.prepare_latents( | ||
| image=image, | ||
| batch_size=batch_size, | ||
| height=32, | ||
| width=32, | ||
| num_frames=5, | ||
| dtype=jnp.float32, | ||
| rng=rng, | ||
| last_image=last_image, | ||
| ) | ||
| self.assertEqual(latents.shape[0], batch_size) | ||
| self.assertEqual(condition.shape[0], batch_size) | ||
| call_image = pipeline.prepare_latents_i2v_base.call_args[0][0] | ||
| call_last_image = pipeline.prepare_latents_i2v_base.call_args[0][3] | ||
| self.assertEqual(call_image.shape[0], batch_size) | ||
| self.assertEqual(call_last_image.shape[0], batch_size) | ||
|
|
||
| def test_indivisible_image_batch_size_raises(self): | ||
| """Verifies ValueError when batch_size is not divisible by image batch size.""" | ||
| rng = jax.random.key(0) | ||
| for pipeline_cls in (WanPipelineI2V_2_1, WanPipelineI2V_2_2): | ||
| with self.subTest(pipeline=pipeline_cls.__name__): | ||
| pipeline = self._create_mock_pipeline(pipeline_cls) | ||
| image = jnp.zeros((2, 3, 32, 32)) | ||
| with self.assertRaisesRegex(ValueError, "divisible by image batch size"): | ||
| pipeline.prepare_latents( | ||
| image=image, | ||
| batch_size=3, | ||
| height=32, | ||
| width=32, | ||
| num_frames=5, | ||
| dtype=jnp.float32, | ||
| rng=rng, | ||
| ) | ||
|
|
||
| def test_indivisible_last_image_batch_size_raises(self): | ||
| """Verifies ValueError when batch_size is not divisible by last_image batch size.""" | ||
| rng = jax.random.key(0) | ||
| for pipeline_cls in (WanPipelineI2V_2_1, WanPipelineI2V_2_2): | ||
| with self.subTest(pipeline=pipeline_cls.__name__): | ||
| pipeline = self._create_mock_pipeline(pipeline_cls) | ||
| image = jnp.zeros((1, 3, 32, 32)) | ||
| last_image = jnp.zeros((2, 3, 32, 32)) | ||
| with self.assertRaisesRegex(ValueError, "divisible by last_image batch size"): | ||
| pipeline.prepare_latents( | ||
| image=image, | ||
| batch_size=3, | ||
| height=32, | ||
| width=32, | ||
| num_frames=5, | ||
| dtype=jnp.float32, | ||
| rng=rng, | ||
| last_image=last_image, | ||
| ) | ||
|
|
||
| def test_mismatched_image_and_last_image_batch_sizes_raises(self): | ||
| """Verifies ValueError when image and last_image have conflicting batch sizes > 1.""" | ||
| rng = jax.random.key(0) | ||
| for pipeline_cls in (WanPipelineI2V_2_1, WanPipelineI2V_2_2): | ||
| with self.subTest(pipeline=pipeline_cls.__name__): | ||
| pipeline = self._create_mock_pipeline(pipeline_cls) | ||
| image = jnp.zeros((2, 3, 32, 32)) | ||
| last_image = jnp.zeros((3, 3, 32, 32)) | ||
| with self.assertRaisesRegex(ValueError, "must match when both are greater than 1"): | ||
| pipeline.prepare_latents( | ||
| image=image, | ||
| batch_size=6, | ||
| height=32, | ||
| width=32, | ||
| num_frames=5, | ||
| dtype=jnp.float32, | ||
| rng=rng, | ||
| last_image=last_image, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.