Add cached conditioning recovery to wan 22 5b model

This commit is contained in:
Jaret Burkett committed 2026-07-10 10:22:24 -06:00
1 parent ad07b06de5
commit 71c75357eb
1 file changed
+17 -5
+17 -5
View File
@@ -115,9 +115,10 @@ def add_first_frame_conditioning(
def add_first_frame_conditioning_v22( def add_first_frame_conditioning_v22(
latent_model_input, latent_model_input,
first_frame, first_frame=None,
vae, vae=None,
last_frame=None last_frame=None,
first_frame_latents=None,
): ):
""" """
Overwrites first few time steps in latent_model_input with VAE-encoded first_frame, Overwrites first few time steps in latent_model_input with VAE-encoded first_frame,
@@ -127,6 +128,8 @@ def add_first_frame_conditioning_v22(
latent_model_input: torch.Tensor of shape (bs, 48, T, H, W) latent_model_input: torch.Tensor of shape (bs, 48, T, H, W)
first_frame: torch.Tensor of shape (bs, 3, H*scale, W*scale) first_frame: torch.Tensor of shape (bs, 3, H*scale, W*scale)
vae: VAE model with .encode() and .config.latents_mean/std vae: VAE model with .encode() and .config.latents_mean/std
first_frame_latents: optional pre-encoded, normalized first frame latents of
shape (bs, 48, 1, H, W); skips the VAE encode when provided
Returns: Returns:
latent: (bs, 48, T, H, W) - modified input latent latent: (bs, 48, T, H, W) - modified input latent
@@ -139,6 +142,17 @@ def add_first_frame_conditioning_v22(
target_h = H * scale target_h = H * scale
target_w = W * scale target_w = W * scale
mean = torch.tensor(vae.config.latents_mean).view(1, -1, 1, 1, 1).to(device, dtype)
std = 1.0 / torch.tensor(vae.config.latents_std).view(1, -1, 1, 1, 1).to(device, dtype)
if first_frame_latents is not None:
# cached latents are already encoded and normalized
encoded = first_frame_latents.to(device, dtype)
if encoded.ndim == 4:
encoded = encoded.unsqueeze(0)
if encoded.shape[0] != bs:
encoded = encoded.expand(bs, -1, -1, -1, -1)
else:
# Ensure shape # Ensure shape
if first_frame.ndim == 3: if first_frame.ndim == 3:
first_frame = first_frame.unsqueeze(0) first_frame = first_frame.unsqueeze(0)
@@ -151,8 +165,6 @@ def add_first_frame_conditioning_v22(
encoded = vae.encode(first_frame_up).latent_dist.sample().to(dtype).to(device) encoded = vae.encode(first_frame_up).latent_dist.sample().to(dtype).to(device)
# Normalize # Normalize
mean = torch.tensor(vae.config.latents_mean).view(1, -1, 1, 1, 1).to(device, dtype)
std = 1.0 / torch.tensor(vae.config.latents_std).view(1, -1, 1, 1, 1).to(device, dtype)
encoded = (encoded - mean) * std encoded = (encoded - mean) * std
# Replace in latent # Replace in latent