Add decode latent to wan 2.1 models. Add gradinet checkpointing to wan vae.

This commit is contained in:
Jaret Burkett
2026-05-05 11:30:16 -06:00
parent 4260a3c5b6
commit 963a9f42b2
2 changed files with 45 additions and 11 deletions

View File

@@ -450,8 +450,6 @@ class WanMidBlock(nn.Module):
self.attentions = nn.ModuleList(attentions) self.attentions = nn.ModuleList(attentions)
self.resnets = nn.ModuleList(resnets) self.resnets = nn.ModuleList(resnets)
self.gradient_checkpointing = False
def forward(self, x, feat_cache=None, feat_idx=[0]): def forward(self, x, feat_cache=None, feat_idx=[0]):
# First residual block # First residual block
x = self.resnets[0](x, feat_cache, feat_idx) x = self.resnets[0](x, feat_cache, feat_idx)
@@ -602,15 +600,22 @@ class WanEncoder3d(nn.Module):
else: else:
x = self.conv_in(x) x = self.conv_in(x)
use_ckpt = torch.is_grad_enabled() and self.gradient_checkpointing and feat_cache is None
## downsamples ## downsamples
for layer in self.down_blocks: for layer in self.down_blocks:
if feat_cache is not None: if use_ckpt:
x = self._gradient_checkpointing_func(layer, x)
elif feat_cache is not None:
x = layer(x, feat_cache, feat_idx) x = layer(x, feat_cache, feat_idx)
else: else:
x = layer(x) x = layer(x)
## middle ## middle
x = self.mid_block(x, feat_cache, feat_idx) if use_ckpt:
x = self._gradient_checkpointing_func(self.mid_block, x)
else:
x = self.mid_block(x, feat_cache, feat_idx)
## head ## head
x = self.norm_out(x) x = self.norm_out(x)
@@ -682,8 +687,6 @@ class WanResidualUpBlock(nn.Module):
else: else:
self.upsampler = None self.upsampler = None
self.gradient_checkpointing = False
def forward(self, x, feat_cache=None, feat_idx=[0], first_chunk=False): def forward(self, x, feat_cache=None, feat_idx=[0], first_chunk=False):
""" """
Forward pass through the upsampling block. Forward pass through the upsampling block.
@@ -756,8 +759,6 @@ class WanUpBlock(nn.Module):
if upsample_mode is not None: if upsample_mode is not None:
self.upsamplers = nn.ModuleList([WanResample(out_dim, mode=upsample_mode)]) self.upsamplers = nn.ModuleList([WanResample(out_dim, mode=upsample_mode)])
self.gradient_checkpointing = False
def forward(self, x, feat_cache=None, feat_idx=[0], first_chunk=None): def forward(self, x, feat_cache=None, feat_idx=[0], first_chunk=None):
""" """
Forward pass through the upsampling block. Forward pass through the upsampling block.
@@ -889,12 +890,20 @@ class WanDecoder3d(nn.Module):
else: else:
x = self.conv_in(x) x = self.conv_in(x)
use_ckpt = torch.is_grad_enabled() and self.gradient_checkpointing and feat_cache is None
## middle ## middle
x = self.mid_block(x, feat_cache, feat_idx) if use_ckpt:
x = self._gradient_checkpointing_func(self.mid_block, x)
else:
x = self.mid_block(x, feat_cache, feat_idx)
## upsamples ## upsamples
for up_block in self.up_blocks: for up_block in self.up_blocks:
x = up_block(x, feat_cache, feat_idx, first_chunk = first_chunk) if use_ckpt:
x = self._gradient_checkpointing_func(up_block, x, None, [0], first_chunk)
else:
x = up_block(x, feat_cache, feat_idx, first_chunk = first_chunk)
## head ## head
x = self.norm_out(x) x = self.norm_out(x)
@@ -961,7 +970,7 @@ class AutoencoderKLWan(ModelMixin, ConfigMixin, FromOriginalModelMixin):
for all models (such as downloading or saving). for all models (such as downloading or saving).
""" """
_supports_gradient_checkpointing = False _supports_gradient_checkpointing = True
@register_to_config @register_to_config
def __init__( def __init__(

View File

@@ -655,6 +655,31 @@ class Wan21(BaseModel):
return latents.to(device, dtype=dtype) return latents.to(device, dtype=dtype)
def decode_latents(self, latents: torch.Tensor, device=None, dtype=None):
if device is None:
device = self.vae_device_torch
if dtype is None:
dtype = self.vae_torch_dtype
if self.vae.device == torch.device('cpu'):
self.vae.to(device)
latents = latents.to(device, dtype=dtype)
latents_mean = (
torch.tensor(self.vae.config.latents_mean)
.view(1, self.vae.config.z_dim, 1, 1, 1)
.to(latents.device, latents.dtype)
)
latents_std = torch.tensor(self.vae.config.latents_std).view(
1, self.vae.config.z_dim, 1, 1, 1
).to(latents.device, latents.dtype)
latents = latents * latents_std + latents_mean
images = self.vae.decode(latents).sample
return images.to(device, dtype=dtype)
def get_model_has_grad(self): def get_model_has_grad(self):
return False return False