Max Speed Fix: Mel SSD caching, Length-Bucketed batching & Numba MAS
Browse files9 unmodified lines1011121314155 unmodified lines21222324252627282930313233343536373839404142434445464748495051525354555657589 unmodified linesfrom .convnext import ChannelLayerNorm1ddef maximum_path(neg_cent: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:"""5 unmodified linesdevice = neg_cent.devicedtype = neg_cent.dtypeneg_cent_np = neg_cent.detach().float().cpu().numpy()mask_np = mask.detach().cpu().numpy() > 0B, T_text, T_mel = neg_cent_np.shapepath_np = np.zeros((B, T_text, T_mel), dtype=np.float32)for b in range(B):t_len = int(mask_np[b, :, 0].sum()) if T_mel > 0 else 0m_len = int(mask_np[b, 0, :].sum()) if T_text > 0 else 0if t_len <= 0 or m_len < t_len:raise ValueError(f"Sample {b} cannot be aligned: {t_len} tokens and {m_len} mel frames.")cent = neg_cent_np[b, :t_len, :m_len]value = np.full((t_len, m_len), -np.inf, dtype=np.float32)value[0, 0] = cent[0, 0]for t in range(1, m_len):s_max = min(t + 1, t_len)# Vectorized transition across s for timestep tv_stay = value[:s_max, t - 1]v_prev = np.empty(s_max, dtype=np.float32)v_prev[0] = -np.infif s_max > 1:v_prev[1:] = value[: s_max - 1, t - 1]value[:s_max, t] = np.maximum(v_stay, v_prev) + cent[:s_max, t]s = t_len - 1for t in range(m_len - 1, -1, -1):path_np[b, s, t] = 1.0if s > 0 and (t == 0 or value[s - 1, t - 1] >= value[s, t - 1]):s -= 1return torch.from_numpy(path_np).to(device=device, dtype=dtype)9 unmodified lines10111213141516171819202122232425262728293031323334353637383940414243445 unmodified lines505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959 unmodified linesfrom .convnext import ChannelLayerNorm1dtry:from numba import njit, prange_HAS_NUMBA = Trueexcept ImportError:_HAS_NUMBA = Falseif _HAS_NUMBA:@njit(parallel=True, cache=True)def _maximum_path_numba(path_np, neg_cent_np, t_lens, m_lens):B = neg_cent_np.shape[0]for b in prange(B):t_len = t_lens[b]m_len = m_lens[b]value = np.full((t_len, m_len), -np.inf, dtype=np.float32)value[0, 0] = neg_cent_np[b, 0, 0]for t in range(1, m_len):s_max = min(t + 1, t_len)for s in range(s_max):v_stay = value[s, t - 1]v_prev = value[s - 1, t - 1] if s > 0 else -np.infv_best = v_stay if v_stay >= v_prev else v_prevvalue[s, t] = v_best + neg_cent_np[b, s, t]s = t_len - 1for t in range(m_len - 1, -1, -1):path_np[b, s, t] = 1.0if s > 0 and (t == 0 or value[s - 1, t - 1] >= value[s, t - 1]):s -= 1def maximum_path(neg_cent: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:"""5 unmodified linesdevice = neg_cent.devicedtype = neg_cent.dtypeneg_cent_np = np.ascontiguousarray(neg_cent.detach().float().cpu().numpy())mask_np = mask.detach().cpu().numpy() > 0B, T_text, T_mel = neg_cent_np.shapepath_np = np.zeros((B, T_text, T_mel), dtype=np.float32)t_lens = np.empty(B, dtype=np.int64)m_lens = np.empty(B, dtype=np.int64)for b in range(B):t_len = int(mask_np[b, :, 0].sum()) if T_mel > 0 else 0m_len = int(mask_np[b, 0, :].sum()) if T_text > 0 else 0if t_len <= 0 or m_len < t_len:raise ValueError(f"Sample {b} cannot be aligned: {t_len} tokens and {m_len} mel frames.")t_lens[b] = t_lenm_lens[b] = m_lenif _HAS_NUMBA and B > 0:_maximum_path_numba(path_np, neg_cent_np, t_lens, m_lens)else:for b in range(B):t_len = int(t_lens[b])m_len = int(m_lens[b])cent = neg_cent_np[b, :t_len, :m_len]value = np.full((t_len, m_len), -np.inf, dtype=np.float32)value[0, 0] = cent[0, 0]for t in range(1, m_len):s_max = min(t + 1, t_len)v_stay = value[:s_max, t - 1]v_prev = np.empty(s_max, dtype=np.float32)v_prev[0] = -np.infif s_max > 1:v_prev[1:] = value[: s_max - 1, t - 1]value[:s_max, t] = np.maximum(v_stay, v_prev) + cent[:s_max, t]s = t_len - 1for t in range(m_len - 1, -1, -1):path_np[b, s, t] = 1.0if s > 0 and (t == 0 or value[s - 1, t - 1] >= value[s, t - 1]):s -= 1return torch.from_numpy(path_np).to(device=device, dtype=dtype)
models/duration_predictor.py+59-22