Download models/flow_matching.py from ViuAI/Murli-V1.5-Code: direct link, hf CLI and curl.
- Browser
- Download file 4.94 kB
-
https://huggingface.co/ViuAI/Murli-V1.5-Code/resolve/main/models/flow_matching.py
- Command line
-
hf download hf://ViuAI/Murli-V1.5-Code/models/flow_matching.py
-
curl -L -o flow_matching.py https://huggingface.co/ViuAI/Murli-V1.5-Code/resolve/main/models/flow_matching.py
4.94 kB
| """ | |
| Murli V1.5 ConvNeXt-V2 Optimal Transport Conditional Flow Matching (OT-CFM) Vector Field. | |
| Pure Conv1d + Linear + GRN + RMSNorm architecture: | |
| - ~2.5x larger capacity than V1 (12 blocks @ 384 dim) to push CFM Loss < 0.35 and eliminate metallic buzz. | |
| - 100% opset-17 Android ONNX Runtime Mobile compatible with zero attention/RoPE overhead during ODE steps. | |
| """ | |
| from typing import Optional | |
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| from models.layers import ConvNeXt1DBlock, RMSNorm, SinusoidalTimestepEmbed | |
| class ConvNeXtCFMVectorField(nn.Module): | |
| """OT-CFM Velocity Estimator v_theta(x_t, t, cond) using 12 ConvNeXt-V2 blocks.""" | |
| def __init__( | |
| self, | |
| mel_dim: int = 100, | |
| cond_dim: int = 320, | |
| hidden_dim: int = 384, | |
| num_blocks: int = 12, | |
| kernel_size: int = 7, | |
| time_dim: int = 128, | |
| ): | |
| super().__init__() | |
| self.mel_dim = mel_dim | |
| self.cond_dim = cond_dim | |
| self.hidden_dim = hidden_dim | |
| self.time_embed = nn.Sequential( | |
| SinusoidalTimestepEmbed(time_dim), | |
| nn.Linear(time_dim, hidden_dim), | |
| nn.SiLU(), | |
| nn.Linear(hidden_dim, hidden_dim), | |
| ) | |
| self.in_proj = nn.Conv1d(mel_dim + cond_dim, hidden_dim, kernel_size=1) | |
| self.blocks = nn.ModuleList([ | |
| ConvNeXt1DBlock(hidden_dim, kernel_size=kernel_size, mult=3) | |
| for _ in range(num_blocks) | |
| ]) | |
| self.time_projs = nn.ModuleList([ | |
| nn.Linear(hidden_dim, hidden_dim) | |
| for _ in range(num_blocks) | |
| ]) | |
| self.final_norm = RMSNorm(hidden_dim) | |
| self.out_proj = nn.Conv1d(hidden_dim, mel_dim, kernel_size=1) | |
| def forward( | |
| self, | |
| x: torch.Tensor, | |
| t: torch.Tensor, | |
| cond: torch.Tensor, | |
| mask: Optional[torch.Tensor] = None, | |
| ) -> torch.Tensor: | |
| """ | |
| x: [B, mel_dim, T_mel] | |
| t: [B] | |
| cond: [B, cond_dim, T_mel] | |
| mask: Optional [B, T_mel] bool | |
| """ | |
| t_emb = self.time_embed(t) # [B, hidden_dim] | |
| h = self.in_proj(torch.cat([x, cond], dim=1)).transpose(1, 2) # [B, T_mel, hidden_dim] | |
| for blk, t_proj in zip(self.blocks, self.time_projs): | |
| h = h + t_proj(t_emb).unsqueeze(1) | |
| h = blk(h, mask=mask) | |
| h = self.final_norm(h).transpose(1, 2) # [B, hidden_dim, T_mel] | |
| out = self.out_proj(h) | |
| if mask is not None: | |
| out = out * mask.unsqueeze(1).to(out.dtype) | |
| return out | |
| def compute_cfm_loss( | |
| vector_field: ConvNeXtCFMVectorField, | |
| mel_target: torch.Tensor, | |
| cond: torch.Tensor, | |
| mel_mask: torch.Tensor, | |
| cfg_drop_prob: float = 0.1, | |
| sigma_min: float = 1e-4, | |
| ) -> torch.Tensor: | |
| """Computes masked OT-CFM velocity MSE loss with CFG conditioning dropout.""" | |
| bsz, _, _ = mel_target.shape | |
| device = mel_target.device | |
| if cfg_drop_prob > 0.0: | |
| keep = (torch.rand(bsz, 1, 1, device=device) >= cfg_drop_prob).to(cond.dtype) | |
| cond_in = cond * keep | |
| else: | |
| cond_in = cond | |
| t = torch.rand(bsz, device=device, dtype=mel_target.dtype) | |
| x0 = torch.randn_like(mel_target) | |
| t_3d = t.view(bsz, 1, 1) | |
| # OT straight path: x_t = (1 - (1 - sigma_min) * t) * x0 + t * x1 | |
| xt = (1.0 - (1.0 - sigma_min) * t_3d) * x0 + t_3d * mel_target | |
| target_v = mel_target - (1.0 - sigma_min) * x0 | |
| pred_v = vector_field(xt, t, cond_in, mask=mel_mask) | |
| mask_3d = mel_mask.unsqueeze(1).to(pred_v.dtype) | |
| return F.mse_loss(pred_v * mask_3d, target_v * mask_3d, reduction="sum") / ( | |
| mask_3d.sum(dtype=torch.float32) * mel_target.shape[1] + 1e-6 | |
| ) | |
| def sample_ode( | |
| vector_field: ConvNeXtCFMVectorField, | |
| cond: torch.Tensor, | |
| prior_mel: Optional[torch.Tensor] = None, | |
| steps: int = 48, | |
| cfg_scale: float = 1.3, | |
| temperature: float = 0.90, | |
| ) -> torch.Tensor: | |
| """Euler OT-CFM ODE sampler with Classifier-Free Guidance (CFG).""" | |
| bsz, _, t_mel = cond.shape | |
| device = cond.device | |
| dtype = cond.dtype | |
| x = torch.randn((bsz, vector_field.mel_dim, t_mel), device=device, dtype=dtype) * temperature | |
| dt = 1.0 / float(steps) | |
| null_cond = torch.zeros_like(cond) | |
| for i in range(steps): | |
| t_cur = torch.full((bsz,), float(i) * dt, device=device, dtype=dtype) | |
| if cfg_scale > 1.0: | |
| x_in = torch.cat([x, x], dim=0) | |
| t_in = torch.cat([t_cur, t_cur], dim=0) | |
| c_in = torch.cat([cond, null_cond], dim=0) | |
| v_cond, v_uncond = vector_field(x_in, t_in, c_in, mask=None).chunk(2, dim=0) | |
| v = v_uncond + cfg_scale * (v_cond - v_uncond) | |
| else: | |
| v = vector_field(x, t_cur, cond, mask=None) | |
| x = x + v * dt | |
| return x | |