Sync CFG-enabled flow_matching.py
Browse files117 unmodified lines11811912012112212312412512612712812913013113213313422 unmodified lines15715815916016116216316416516616716816917017117217313 unmodified lines187188189190191192193194195196197198199200201117 unmodified linesx1: torch.Tensor,cond: torch.Tensor,mel_mask: Optional[torch.Tensor] = None,) -> torch.Tensor:"""x1: Ground truth target Mel spectrogram (B, mel_dim, T)cond: Text condition (B, cond_dim, T)mel_mask: Optional (B, T) bool mask for valid frames"""B, C, T = x1.shapedevice = x1.devicemask_3d = mel_mask.unsqueeze(1).to(x1.dtype) if mel_mask is not None else None# 1. Sample continuous time t uniformly in [0, 1]t = torch.rand(B, device=device, dtype=x1.dtype)22 unmodified linesloss = sq_err.mean()return loss@torch.no_grad()def sample(self,cond: torch.Tensor,num_steps: int = 8,method: str = "midpoint",mel_mask: Optional[torch.Tensor] = None,) -> torch.Tensor:"""Ultrafast ODE sampling from t=0 (noise) to t=1 (mel spectrogram).Takes only 4 to 10 steps!cond: (B, cond_dim, T)"""B, _, T = cond.shape13 unmodified linest_curr = t_steps[i].repeat(B)if method == "midpoint":v1 = self.vf(x, t_curr, cond, mask=mask_3d)x_mid = x + 0.5 * dt * v1if mask_3d is not None:x_mid = x_mid * mask_3dt_mid = (t_steps[i] + 0.5 * dt).repeat(B)v2 = self.vf(x_mid, t_mid, cond, mask=mask_3d)x = x + dt * v2else:v = self.vf(x, t_curr, cond, mask=mask_3d)x = x + dt * vif mask_3d is not None:117 unmodified lines11811912012112212312412512612712812913013113213313413513613713813914014122 unmodified lines16416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619713 unmodified lines211212213214215216217218219220221222223224225117 unmodified linesx1: torch.Tensor,cond: torch.Tensor,mel_mask: Optional[torch.Tensor] = None,p_uncond: float = 0.0,) -> torch.Tensor:"""x1: Ground truth target Mel spectrogram (B, mel_dim, T)cond: Text condition (B, cond_dim, T)mel_mask: Optional (B, T) bool mask for valid framesp_uncond: Probability of dropping condition for Classifier-Free Guidance"""B, C, T = x1.shapedevice = x1.devicemask_3d = mel_mask.unsqueeze(1).to(x1.dtype) if mel_mask is not None else None# Classifier-Free Guidance: randomly zero condition for p_uncond fraction of batchif p_uncond > 0.0:keep_mask = (torch.rand(B, 1, 1, device=device) >= p_uncond).to(cond.dtype)cond = cond * keep_mask# 1. Sample continuous time t uniformly in [0, 1]t = torch.rand(B, device=device, dtype=x1.dtype)22 unmodified linesloss = sq_err.mean()return lossdef _eval_vf(self,x: torch.Tensor,t: torch.Tensor,cond: torch.Tensor,mask_3d: Optional[torch.Tensor],cfg_scale: float,) -> torch.Tensor:if cfg_scale == 1.0:return self.vf(x, t, cond, mask=mask_3d)x_cat = torch.cat([x, x], dim=0)t_cat = torch.cat([t, t], dim=0)cond_cat = torch.cat([torch.zeros_like(cond), cond], dim=0)mask_cat = torch.cat([mask_3d, mask_3d], dim=0) if mask_3d is not None else Nonev_uncond, v_cond = self.vf(x_cat, t_cat, cond_cat, mask=mask_cat).chunk(2, dim=0)return v_uncond + cfg_scale * (v_cond - v_uncond)@torch.no_grad()def sample(self,cond: torch.Tensor,num_steps: int = 16,method: str = "midpoint",mel_mask: Optional[torch.Tensor] = None,cfg_scale: float = 1.0,) -> torch.Tensor:"""Ultrafast ODE sampling from t=0 (noise) to t=1 (mel spectrogram) with optional CFG.cond: (B, cond_dim, T)"""B, _, T = cond.shape13 unmodified linest_curr = t_steps[i].repeat(B)if method == "midpoint":v1 = self._eval_vf(x, t_curr, cond, mask_3d, cfg_scale)x_mid = x + 0.5 * dt * v1if mask_3d is not None:x_mid = x_mid * mask_3dt_mid = (t_steps[i] + 0.5 * dt).repeat(B)v2 = self._eval_vf(x_mid, t_mid, cond, mask_3d, cfg_scale)x = x + dt * v2else:v = self._eval_vf(x, t_curr, cond, mask_3d, cfg_scale)x = x + dt * vif mask_3d is not None:
models/flow_matching.py+30-6