Open-Sora: Democratizing Efficient Video Production for All
Python
mask added, no test due to occupation of gpus (#90)
ChangeFWorld338bd83f241d
diff --git a/opensora/schedulers/rf/__init__.py b/opensora/schedulers/rf/__init__.pyindex 5fb00ae..9c0e0f1 100644--- a/opensora/schedulers/rf/__init__.py+++ b/opensora/schedulers/rf/__init__.py@@ -6,6 +6,7 @@ from tqdm import tqdmfrom opensora.registry import SCHEDULERSfrom .rectified_flow import RFlowScheduler, timestep_transform+from ..iddpm.gaussian_diffusion import _extract_into_tensor@SCHEDULERS.register_module("rflow")@@ -45,7 +46,6 @@ class RFLOW:guidance_scale=None,progress=True,):- assert mask is None, "mask is not supported in rectified flow inference yet"# if no specific guidance scale is provided, use the default scale when initializing the schedulerif guidance_scale is None:guidance_scale = self.cfg_scale@@ -66,8 +66,23 @@ class RFLOW:if self.use_timestep_transform:timesteps = [timestep_transform(t, additional_args, num_timesteps=self.num_timesteps) for t in timesteps]+progress_wrap = tqdm if progress else (lambda x: x)for i, t in progress_wrap(enumerate(timesteps)):+ if mask is not None:+ if mask.shape[0] != z.shape[0]:+ mask = mask.repeat(2, 1)+ mask_t = (mask * self.num_timesteps).to(torch.int)+ x0 = z.clone()+ x_noise = self.scheduler.add_noise(x0, torch.randn_like(x0), t)+ mask_t_equall = (mask_t == torch.floor(t.unsqueeze(1)).long())[:, None, :, None, None]+ z = torch.where(mask_t_equall, x_noise, x0)++ mask_t_upper = (mask_t > t.unsqueeze(1))[:, None, :, None, None]+ batch_size = z.shape[0]+ x_mask = mask_t_upper.reshape(batch_size, -1).to(torch.bool)+ model_args["x_mask"] = x_mask+z_in = torch.cat([z, z], 0)pred = model(z_in, torch.tensor([t] * z_in.shape[0], device=device), **model_args).chunk(2, dim=1)[0]pred_cond, pred_uncond = pred.chunk(2, dim=0)@@ -77,6 +92,10 @@ class RFLOW:dt = dt / self.num_timestepsz = z + v_pred * dt+ if mask is not None:+ mask_t_lower = (mask_t < t.unsqueeze(1))[:, None, :, None, None]+ z = torch.where(mask_t_lower, x0, z)+return zdef training_losses(self, model, x_start, model_kwargs=None, noise=None, mask=None, weights=None):