(
video_tensor: torch.Tensor,
vae: AutoencoderKLWan,
generator: torch.Generator,
device: torch.device,
dtype: torch.dtype,
latent_channels: int = 16,
)
| 116 | |
| 117 | |
| 118 | def encode_vae_image( |
| 119 | video_tensor: torch.Tensor, |
| 120 | vae: AutoencoderKLWan, |
| 121 | generator: torch.Generator, |
| 122 | device: torch.device, |
| 123 | dtype: torch.dtype, |
| 124 | latent_channels: int = 16, |
| 125 | ): |
| 126 | if not isinstance(video_tensor, torch.Tensor): |
| 127 | raise ValueError(f"Expected video_tensor to be a tensor, got {type(video_tensor)}.") |
| 128 | |
| 129 | if isinstance(generator, list) and len(generator) != video_tensor.shape[0]: |
| 130 | raise ValueError( |
| 131 | f"You have passed a list of generators of length {len(generator)}, but it is not same as number of images {video_tensor.shape[0]}." |
| 132 | ) |
| 133 | |
| 134 | video_tensor = video_tensor.to(device=device, dtype=dtype) |
| 135 | |
| 136 | if isinstance(generator, list): |
| 137 | video_latents = [ |
| 138 | retrieve_latents(vae.encode(video_tensor[i : i + 1]), generator=generator[i], sample_mode="argmax") |
| 139 | for i in range(video_tensor.shape[0]) |
| 140 | ] |
| 141 | video_latents = torch.cat(video_latents, dim=0) |
| 142 | else: |
| 143 | video_latents = retrieve_latents(vae.encode(video_tensor), sample_mode="argmax") |
| 144 | |
| 145 | latents_mean = ( |
| 146 | torch.tensor(vae.config.latents_mean) |
| 147 | .view(1, latent_channels, 1, 1, 1) |
| 148 | .to(video_latents.device, video_latents.dtype) |
| 149 | ) |
| 150 | latents_std = 1.0 / torch.tensor(vae.config.latents_std).view(1, latent_channels, 1, 1, 1).to( |
| 151 | video_latents.device, video_latents.dtype |
| 152 | ) |
| 153 | video_latents = (video_latents - latents_mean) * latents_std |
| 154 | |
| 155 | return video_latents |
| 156 | |
| 157 | |
| 158 | class WanTextEncoderStep(ModularPipelineBlocks): |
no test coverage detected
searching dependent graphs…