CoolFace
Apppublic

antoniomae1234/Seed-VC-characters

sourceHugging Facegpl-3.0updated 2y agoView on Hugging Face
6likes
flow_matching.cpython-310.pyc79 linesDownload Raw Back to __pycache__
1o

2\9�f?�@shddlmZddlZddlmmZddlmZddl	m3Z4Gdd�dejje�ZGdd�de�Z
dS)	�)�ABCN)�DiT)�
sequence_maskcsBeZdZ�fdd�Ze��ddd��Zd
dd�Zdd5d�Z�Z	S)�BASECFMcsdt���d|_d|_|jj|_|jdkrtj�	�ntj�6�|_t|jd�r-|jj
|_
dSd|_
dS)Ng�����ư>�l2�zero_prompt_speech_tokenF)�super�__init__�	sigma_min�	estimatorr�in_channelsZ
reg_loss_type�torch�nn�MSELoss�L1Loss�	criterion�hasattrr��self�args��	__class__��#D:\seed-vc\modules\flow_matching.pyr	7s89 10zBASECFM.__init__��?��?c	
11Csb|�d�|�d�}	}12tj|	|j|13g|jd�|}tjdd|d|jd�}|�||||||||�S)a�Forward diffusion14 15        Args:16            mu (torch.Tensor): output of encoder17                shape: (batch_size, n_feats, mel_timesteps)18            mask (torch.Tensor): output_mask19                shape: (batch_size, 1, mel_timesteps)20            n_timesteps (int): number of diffusion steps21            temperature (float, optional): temperature for scaling noise. Defaults to 1.0.22            spks (torch.Tensor, optional): speaker ids. Defaults to None.23                shape: (batch_size, spk_emb_dim)24            cond: Not used but kept for future purposes25 26        Returns:27            sample: generated mel-spectrogram28                shape: (batch_size, n_feats, mel_timesteps)29        r�)�device)�sizer
�randnrr�linspace�solve_euler)
r�mu�x_lens�prompt�style�f0Zn_timesteps�temperature�inference_cfg_rate�B�T�z�t_spanrrr�	inferenceszBASECFM.inferencec	CsX|d|d|d|d}	}30}g}|�d�}
t�|�}|dd|
�f|dd|
�f<d|dd|
�f<|jr?d|dd|
�f<tdt|��D]a}|�||||	�d�|||�}|dkr{|�|t�|�||	�d�t�|�t�|�d�}d||||}|||}|	|}	|�|�|t|�dkr�||d|	}d|dd�dd�d|
�f<qF|dS)aP31        Fixed euler solver for ODEs.32        Args:33            x (torch.Tensor): random noise34            t_span (torch.Tensor): n_timesteps interpolated35                shape: (n_timesteps + 1,)36            mu (torch.Tensor): output of encoder37                shape: (batch_size, n_feats, mel_timesteps)38            mask (torch.Tensor): output_mask39                shape: (batch_size, 1, mel_timesteps)40            spks (torch.Tensor, optional): speaker ids. Defaults to None.41                shape: (batch_size, spk_emb_dim)42            cond: Not used but kept for future purposes43        r�����r.Nr)	rr
�44zeros_liker�range�lenr�	unsqueeze�append)r�xr#r$r"r%r&r,r(�t�_�dt�solZ45prompt_lenZprompt_x�stepZdphi_dtZcfg_dphi_dtrrrr!4s4$464748�49�50zBASECFM.solve_eulerNcCsb|j\}}}	tj|ddg|j|jd�}	t�|�}51dd|j|	|52|	|}|d|j|53}t�|�}
t|�D]7}||dd�d||�f|
|dd�d||�f<d||dd�d||�f<|j	rpd||dd�d||�f<q9|�54||
||	��|||�}d}t|�D]$}||�||dd�||||�f||dd�||||�f�7}q�||}||fS)a�Computes diffusion loss55 56        Args:57            x1 (torch.Tensor): Target58                shape: (batch_size, n_feats, mel_timesteps)59            mask (torch.Tensor): target mask60                shape: (batch_size, 1, mel_timesteps)61            mu (torch.Tensor): output of encoder62                shape: (batch_size, n_feats, mel_timesteps)63            spks (torch.Tensor, optional): speaker embedding. Defaults to None.64                shape: (batch_size, spk_emb_dim)65 66        Returns:67            loss: conditional flow matching loss68            y: conditional flow69                shape: (batch_size, n_feats, mel_timesteps)70        r)r�dtypeNr)
�shaper
�randrr:�71randn_liker72r/r0rr�squeezer)r�x1r#Zprompt_lensr"r%r&�br6r5r+�y�ur$�bibZ
estimator_out�lossrrr�forwardcs$73740�FzBASECFM.forward)rr)r)N)75�__name__�76__module__�__qualname__r	r
�inference_moder-r!rE�
__classcell__rrrrr	s77/rcseZdZ�fdd�Z�ZS)�CFMcs4t��|�|jdkrt|�|_dStd|j����)NrzUnknown diffusion type )rr	Zdit_typerr�NotImplementedErrorrrrrr	�s�78zCFM.__init__)rFrGrHr	rJrrrrrK�srK)�abcrr
Ztorch.nn.functionalr�79functional�FZmodules.diffusion_transformerr�modules.commonsr�ModulerrKrrrr�<module>s