|
| 1 | +# Copyright 2025 The HuggingFace Team. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +import torch |
| 19 | + |
| 20 | +from diffusers import AutoencoderKLLTXVideo, LTXLatentUpsamplePipeline |
| 21 | +from diffusers.pipelines.ltx.modeling_latent_upsampler import LTXLatentUpsamplerModel |
| 22 | +from diffusers.utils.testing_utils import enable_full_determinism |
| 23 | + |
| 24 | +from ..test_pipelines_common import PipelineTesterMixin, to_np |
| 25 | + |
| 26 | + |
| 27 | +enable_full_determinism() |
| 28 | + |
| 29 | + |
| 30 | +class LTXLatentUpsamplePipelineFastTests(PipelineTesterMixin, unittest.TestCase): |
| 31 | + pipeline_class = LTXLatentUpsamplePipeline |
| 32 | + params = {"video", "generator"} |
| 33 | + batch_params = {"video", "generator"} |
| 34 | + required_optional_params = frozenset(["generator", "latents", "return_dict"]) |
| 35 | + test_xformers_attention = False |
| 36 | + supports_dduf = False |
| 37 | + |
| 38 | + def get_dummy_components(self): |
| 39 | + torch.manual_seed(0) |
| 40 | + vae = AutoencoderKLLTXVideo( |
| 41 | + in_channels=3, |
| 42 | + out_channels=3, |
| 43 | + latent_channels=8, |
| 44 | + block_out_channels=(8, 8, 8, 8), |
| 45 | + decoder_block_out_channels=(8, 8, 8, 8), |
| 46 | + layers_per_block=(1, 1, 1, 1, 1), |
| 47 | + decoder_layers_per_block=(1, 1, 1, 1, 1), |
| 48 | + spatio_temporal_scaling=(True, True, False, False), |
| 49 | + decoder_spatio_temporal_scaling=(True, True, False, False), |
| 50 | + decoder_inject_noise=(False, False, False, False, False), |
| 51 | + upsample_residual=(False, False, False, False), |
| 52 | + upsample_factor=(1, 1, 1, 1), |
| 53 | + timestep_conditioning=False, |
| 54 | + patch_size=1, |
| 55 | + patch_size_t=1, |
| 56 | + encoder_causal=True, |
| 57 | + decoder_causal=False, |
| 58 | + ) |
| 59 | + vae.use_framewise_encoding = False |
| 60 | + vae.use_framewise_decoding = False |
| 61 | + |
| 62 | + torch.manual_seed(0) |
| 63 | + latent_upsampler = LTXLatentUpsamplerModel( |
| 64 | + in_channels=8, |
| 65 | + mid_channels=32, |
| 66 | + num_blocks_per_stage=1, |
| 67 | + dims=3, |
| 68 | + spatial_upsample=True, |
| 69 | + temporal_upsample=False, |
| 70 | + ) |
| 71 | + |
| 72 | + components = { |
| 73 | + "vae": vae, |
| 74 | + "latent_upsampler": latent_upsampler, |
| 75 | + } |
| 76 | + return components |
| 77 | + |
| 78 | + def get_dummy_inputs(self, device, seed=0): |
| 79 | + if str(device).startswith("mps"): |
| 80 | + generator = torch.manual_seed(seed) |
| 81 | + else: |
| 82 | + generator = torch.Generator(device=device).manual_seed(seed) |
| 83 | + |
| 84 | + video = torch.randn((5, 3, 32, 32), generator=generator, device=device) |
| 85 | + |
| 86 | + inputs = { |
| 87 | + "video": video, |
| 88 | + "generator": generator, |
| 89 | + "height": 16, |
| 90 | + "width": 16, |
| 91 | + "output_type": "pt", |
| 92 | + } |
| 93 | + |
| 94 | + return inputs |
| 95 | + |
| 96 | + def test_inference(self): |
| 97 | + device = "cpu" |
| 98 | + |
| 99 | + components = self.get_dummy_components() |
| 100 | + pipe = self.pipeline_class(**components) |
| 101 | + pipe.to(device) |
| 102 | + pipe.set_progress_bar_config(disable=None) |
| 103 | + |
| 104 | + inputs = self.get_dummy_inputs(device) |
| 105 | + video = pipe(**inputs).frames |
| 106 | + generated_video = video[0] |
| 107 | + |
| 108 | + self.assertEqual(generated_video.shape, (5, 3, 32, 32)) |
| 109 | + expected_video = torch.randn(5, 3, 32, 32) |
| 110 | + max_diff = np.abs(generated_video - expected_video).max() |
| 111 | + self.assertLessEqual(max_diff, 1e10) |
| 112 | + |
| 113 | + def test_vae_tiling(self, expected_diff_max: float = 0.25): |
| 114 | + generator_device = "cpu" |
| 115 | + components = self.get_dummy_components() |
| 116 | + |
| 117 | + pipe = self.pipeline_class(**components) |
| 118 | + pipe.to("cpu") |
| 119 | + pipe.set_progress_bar_config(disable=None) |
| 120 | + |
| 121 | + # Without tiling |
| 122 | + inputs = self.get_dummy_inputs(generator_device) |
| 123 | + inputs["height"] = inputs["width"] = 128 |
| 124 | + output_without_tiling = pipe(**inputs)[0] |
| 125 | + |
| 126 | + # With tiling |
| 127 | + pipe.vae.enable_tiling( |
| 128 | + tile_sample_min_height=96, |
| 129 | + tile_sample_min_width=96, |
| 130 | + tile_sample_stride_height=64, |
| 131 | + tile_sample_stride_width=64, |
| 132 | + ) |
| 133 | + inputs = self.get_dummy_inputs(generator_device) |
| 134 | + inputs["height"] = inputs["width"] = 128 |
| 135 | + output_with_tiling = pipe(**inputs)[0] |
| 136 | + |
| 137 | + self.assertLess( |
| 138 | + (to_np(output_without_tiling) - to_np(output_with_tiling)).max(), |
| 139 | + expected_diff_max, |
| 140 | + "VAE tiling should not affect the inference results", |
| 141 | + ) |
| 142 | + |
| 143 | + @unittest.skip("Test is not applicable.") |
| 144 | + def test_callback_inputs(self): |
| 145 | + pass |
| 146 | + |
| 147 | + @unittest.skip("Test is not applicable.") |
| 148 | + def test_attention_slicing_forward_pass( |
| 149 | + self, test_max_difference=True, test_mean_pixel_difference=True, expected_max_diff=1e-3 |
| 150 | + ): |
| 151 | + pass |
| 152 | + |
| 153 | + @unittest.skip("Test is not applicable.") |
| 154 | + def test_inference_batch_consistent(self): |
| 155 | + pass |
| 156 | + |
| 157 | + @unittest.skip("Test is not applicable.") |
| 158 | + def test_inference_batch_single_identical(self): |
| 159 | + pass |
0 commit comments