SuperCS's picture
Add files using upload-large-folder tool
fff3baa verified
from causvid.models import get_diffusion_wrapper, get_text_encoder_wrapper, get_vae_wrapper
import torch.nn.functional as F
from typing import Tuple
from torch import nn
import torch
class ODERegression(nn.Module):
def __init__(self, args, device):
"""
Initialize the ODERegression module.
This class is self-contained and compute generator losses
in the forward pass given precomputed ode solution pairs.
This class supports the ode regression loss for both causal and bidirectional models.
See Sec 4.3 of CausVid https://arxiv.org/abs/2412.07772 for details
"""
super().__init__()
# Step 1: Initialize all models
self.generator = get_diffusion_wrapper(model_name=args.model_name)()
self.generator.set_module_grad(
module_grad=args.generator_grad
)
if getattr(args, "generator_ckpt", False):
print(f"Loading pretrained generator from {args.generator_ckpt}")
state_dict = torch.load(args.generator_ckpt, map_location="cpu")[
'generator']
self.generator.load_state_dict(
state_dict, strict=True
)
self.num_frame_per_block = getattr(args, "num_frame_per_block", 1)
if self.num_frame_per_block > 1:
self.generator.model.num_frame_per_block = self.num_frame_per_block
if args.gradient_checkpointing:
self.generator.enable_gradient_checkpointing()
self.text_encoder = get_text_encoder_wrapper(
model_name=args.model_name)()
self.text_encoder.requires_grad_(False)
self.vae = get_vae_wrapper(model_name=args.model_name)()
self.vae.requires_grad_(False)
# Step 2: Initialize all hyperparameters
self.denoising_step_list = torch.tensor(
args.denoising_step_list, dtype=torch.long, device=device)
self.scheduler = self.generator.get_scheduler()
if args.warp_denoising_step: # Warp the denoising step according to the scheduler time shift
timesteps = torch.cat((self.scheduler.timesteps.cpu(), torch.tensor([0], dtype=torch.float32))).cuda()
self.denoising_step_list = timesteps[1000 - self.denoising_step_list]
self.args = args
self.device = device
self.dtype = torch.bfloat16 if args.mixed_precision else torch.float32
# for latent frame with zero noise, we probablistically perturb it with an extra small noise
# self.extra_noise_step = getattr(args, "extra_noise_step", 0)
# self.scheduler = self.generator.get_scheduler()
def _process_timestep(self, timestep):
"""
Pre-process the randomly generated timestep based on the generator's task type.
Input:
- timestep: [batch_size, num_frame] tensor containing the randomly generated timestep.
Output Behavior:
- image: check that the second dimension (num_frame) is 1.
- bidirectional_video: broadcast the timestep to be the same for all frames.
- causal_video: broadcast the timestep to be the same for all frames **in a block**.
"""
if self.args.generator_task == "image":
assert timestep.shape[1] == 1
return timestep
elif self.args.generator_task == "bidirectional_video":
for index in range(timestep.shape[0]):
timestep[index] = timestep[index, 0]
return timestep
elif self.args.generator_task == "causal_video":
# make the noise level the same within every motion block
timestep = timestep.reshape(
timestep.shape[0], -1, self.num_frame_per_block)
timestep[:, :, 1:] = timestep[:, :, 0:1]
timestep = timestep.reshape(timestep.shape[0], -1)
return timestep
else:
raise NotImplementedError()
@torch.no_grad()
def _prepare_generator_input(self, ode_latent: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Given a tensor containing the whole ODE sampling trajectories,
randomly choose an intermediate timestep and return the latent as well as the corresponding timestep.
Input:
- ode_latent: a tensor containing the whole ODE sampling trajectories [batch_size, num_denoising_steps, num_frames, num_channels, height, width].
Output:
- noisy_input: a tensor containing the selected latent [batch_size, num_frames, num_channels, height, width].
- timestep: a tensor containing the corresponding timestep [batch_size].
"""
batch_size, num_denoising_steps, num_frames, num_channels, height, width = ode_latent.shape
# Step 1: Randomly choose a timestep for each frame
index = torch.randint(0, len(self.denoising_step_list), [
batch_size, num_frames], device=self.device, dtype=torch.long)
index = self._process_timestep(index)
noisy_input = torch.gather(
ode_latent, dim=1,
index=index.reshape(batch_size, 1, num_frames, 1, 1, 1).expand(
-1, -1, -1, num_channels, height, width)
).squeeze(1)
timestep = self.denoising_step_list[index]
# if self.extra_noise_step > 0:
# random_timestep = torch.randint(0, self.extra_noise_step, [
# batch_size, num_frames], device=self.device, dtype=torch.long)
# perturbed_noisy_input = self.scheduler.add_noise(
# noisy_input.flatten(0, 1),
# torch.randn_like(noisy_input.flatten(0, 1)),
# random_timestep.flatten(0, 1)
# ).detach().unflatten(0, (batch_size, num_frames)).type_as(noisy_input)
# noisy_input[timestep == 0] = perturbed_noisy_input[timestep == 0]
return noisy_input, timestep
def generator_loss(self, ode_latent: torch.Tensor, conditional_dict: dict) -> Tuple[torch.Tensor, dict]:
"""
Generate image/videos from noisy latents and compute the ODE regression loss.
Input:
- ode_latent: a tensor containing the ODE latents [batch_size, num_denoising_steps, num_frames, num_channels, height, width].
They are ordered from most noisy to clean latents.
- conditional_dict: a dictionary containing the conditional information (e.g. text embeddings, image embeddings).
Output:
- loss: a scalar tensor representing the generator loss.
- log_dict: a dictionary containing additional information for loss timestep breakdown.
"""
# Step 1: Run generator on noisy latents
target_latent = ode_latent[:, -1]
noisy_input, timestep = self._prepare_generator_input(
ode_latent=ode_latent)
pred_image_or_video = self.generator(
noisy_image_or_video=noisy_input, # torch.Size([2, 21, 16, 60, 104])
conditional_dict=conditional_dict,
timestep=timestep # torch.Size([2, 21])
)
# tensor([
# [1000, 1000, 1000, 0, 0, 0, 757, 757, 757, 757, 757, 757, 0, 0, 0, 522, 522, 522, 522, 522, 522],
# [ 522, 522, 522, 1000, 1000, 1000, 522, 522, 522, 1000, 1000, 1000, 0, 0, 0, 522, 522, 522, 1000, 1000, 1000]
# ],device='cuda:0')
# Step 2: Compute the regression loss
mask = timestep != 0 # mask
loss = F.mse_loss(pred_image_or_video[mask], target_latent[mask], reduction="mean")
log_dict = {
"unnormalized_loss": F.mse_loss(pred_image_or_video, target_latent, reduction='none').mean(dim=[1, 2, 3, 4]).detach(),
"timestep": timestep.float().mean(dim=1).detach()
}
return loss, log_dict