o
    ´‹
j35  ã                   @   s8  d dl mZmZ d dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZmZ G dd„ dejƒZG d	d
„ d
ejjƒZG dd„ dejƒZG dd„ deƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZG dd„ dejƒZdd„ Zdd„ ZG dd„ dejƒZG dd „ d ejƒZG d!d"„ d"ejƒZdS )#é    )ÚDictÚUnionN)Únn)Ú
functional)Ú	TorchSTFT)Údiscretized_mix_logistic_lossÚgaussian_lossc                       ó(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚSTFTLosszÊSTFT loss. Input generate and real waveforms are converted
    to spectrograms compared with L1 and Spectral convergence losses.
    It is from ParallelWaveGAN paper https://arxiv.org/pdf/1910.11480.pdfc                    s.   t ƒ  ¡  || _|| _|| _t|||ƒ| _d S ©N)ÚsuperÚ__init__Ún_fftÚ
hop_lengthÚ
win_lengthr   Ústft)Úselfr   r   r   ©Ú	__class__© úL/home/kuhnn/.local/lib/python3.10/site-packages/TTS/vocoder/layers/losses.pyr      s
   
zSTFTLoss.__init__c                 C   sT   |   |¡}|   |¡}t t |¡t |¡¡}tj|| ddtj|dd }||fS )NÚfro)Úp)r   ÚFÚl1_lossÚtorchÚlogÚnorm)r   Úy_hatÚyÚy_hat_MÚy_MÚloss_magÚloss_scr   r   r   Úforward   s
   

 zSTFTLoss.forward©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   Ú__classcell__r   r   r   r   r
      s    r
   c                       s*   e Zd ZdZd	‡ fdd„	Zdd„ Z‡  ZS )
ÚMultiScaleSTFTLosszÖMulti-scale STFT loss. Input generate and real waveforms are converted
    to spectrograms compared with L1 and Spectral convergence losses.
    It is from ParallelWaveGAN paper https://arxiv.org/pdf/1910.11480.pdf©i   i   i   ©éx   éð   é2   ©iX  i°  r/   c                    sF   t ƒ  ¡  tj ¡ | _t|||ƒD ]\}}}| j t|||ƒ¡ qd S r   )	r   r   r   r   Ú
ModuleListÚ
loss_funcsÚzipÚappendr
   )r   Ún_fftsÚhop_lengthsÚwin_lengthsr   r   r   r   r   r   r   *   s
   
ÿzMultiScaleSTFTLoss.__init__c           	      C   sT   t | jƒ}d}d}| jD ]}|||ƒ\}}||7 }||7 }q|| }|| }||fS )Nr   )Úlenr3   )	r   r   r   ÚNr#   r"   ÚfÚlmÚlscr   r   r   r$   0   s   


zMultiScaleSTFTLoss.forward)r,   r-   r1   r%   r   r   r   r   r+   %   s    r+   c                       s,   e Zd ZdZ	d‡ fdd„	Zdd„ Z‡  ZS )	Ú
L1SpecLossz\L1 Loss over Spectrograms as described in HiFiGAN paper https://arxiv.org/pdf/2010.05646.pdfNTc	           	   
      s.   t ƒ  ¡  || _t||||||||d| _d S )N)Úsample_rateÚmel_fminÚmel_fmaxÚn_melsÚuse_mel)r   r   rC   r   r   )	r   r?   r   r   r   r@   rA   rB   rC   r   r   r   r   @   s   
øzL1SpecLoss.__init__c                 C   s0   |   |¡}|   |¡}t t |¡t |¡¡}|S r   )r   r   r   r   r   )r   r   r   r    r!   r"   r   r   r   r$   P   s   

zL1SpecLoss.forward)NNNTr%   r   r   r   r   r>   =   s
    ÿr>   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚMultiScaleSubbandSTFTLosszsMultiscale STFT loss for multi band model outputs.
    From MultiBand-MelGAN paper https://arxiv.org/abs/2005.05106c                    sB   |  dd|jd ¡}|  dd|jd ¡}tƒ  | d¡| d¡¡S )Néÿÿÿÿé   é   )ÚviewÚshaper   r$   Úsqueeze)r   r   r   r   r   r   r$   ]   s   z!MultiScaleSubbandSTFTLoss.forward)r&   r'   r(   r)   r$   r*   r   r   r   r   rD   X   s    rD   c                   @   ó   e Zd ZdZdd„ ZdS )ÚMSEGLosszMean Squared Generator Lossc                 C   s   t  || |j¡¡}|S r   )r   Úmse_lossÚnew_onesrI   ©r   Ú
score_realÚ	loss_faker   r   r   r$   g   s   zMSEGLoss.forwardN©r&   r'   r(   r)   r$   r   r   r   r   rL   c   ó    rL   c                   @   rK   )Ú
HingeGLossúHinge Discriminator Lossc                 C   s   t  t d| ¡¡}|S ©Ng      ð?©r   Úmeanr   ÚrelurO   r   r   r   r$   p   s   zHingeGLoss.forwardNrR   r   r   r   r   rT   l   rS   rT   c                       r	   )ÚMSEDLosszMean Squared Discriminator Lossc                    ó   t ƒ  ¡  t ¡ | _d S r   )r   r   r   ÚMSELossÚ	loss_func©r   r   r   r   r   ~   ó   
zMSEDLoss.__init__c                 C   s:   |   || |j¡¡}|   || |j¡¡}|| }|||fS r   )r]   rN   rI   Ú	new_zeros©r   Ú
score_fakerP   Ú	loss_realrQ   Úloss_dr   r   r   r$   …   ó   
zMSEDLoss.forwardr%   r   r   r   r   rZ   {   s    rZ   c                   @   rK   )Ú
HingeDLossrU   c                 C   s:   t  t d| ¡¡}t  t d| ¡¡}|| }|||fS rV   rW   ra   r   r   r   r$      re   zHingeDLoss.forwardNrR   r   r   r   r   rf   Œ   rS   rf   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚMelganFeatureLossc                    r[   r   )r   r   r   ÚL1Lossr]   r^   r   r   r   r   ˜   r_   zMelganFeatureLoss.__init__c           	      C   sZ   d}d}t |ƒD ]\}}t|| || ƒD ]\}}||  ||¡7 }|d7 }qq|| }|S )Nr   rF   )Ú	enumerater4   r]   )	r   Ú
fake_featsÚ
real_featsÚ
loss_featsÚ	num_featsÚidxÚ_Ú	fake_featÚ	real_featr   r   r   r$   Ÿ   s   
þzMelganFeatureLoss.forward)r&   r'   r(   r   r$   r*   r   r   r   r   rg   —   s    rg   c                 C   sH   d}t | tƒr| D ]
}||ƒ}||7 }q	|t| ƒ }|S || ƒ}|}|S )z<Compute G adversarial loss function
    and normalize valuesr   )Ú
isinstanceÚlistr9   )Úscores_faker]   Úadv_lossrb   Ú	fake_lossr   r   r   Ú_apply_G_adv_loss¯   s   

þrw   c                 C   s˜   d}d}d}t | tƒr=t| |ƒD ]\}}|||d\}}	}
||7 }||	7 }||
7 }q|t| ƒ }|t|ƒ }|t| ƒ }n
|| |ƒ\}}}|}|||fS )z-Compute D loss func and normalize loss valuesr   )rb   rP   )rr   rs   r4   r9   )rt   Úscores_realr]   ÚlossÚ	real_lossrv   rb   rP   Ú
total_lossÚ
real_loss_Ú
fake_loss_r   r   r   Ú_apply_D_loss¾   s   


r~   c                       s,   e Zd ZdZ‡ fdd„Z	ddd„Z‡  ZS )ÚGeneratorLossa/  Generator Loss Wrapper. Based on model configuration it sets a right set of loss functions and computes
    losses. It allows to experiment with different combinations of loss functions with different models by just
    changing configurations.

    Args:
        C (AttrDict): model configuration.
    c                    s   t ƒ  ¡  |jr|jrJ dƒ‚d|v r|jnd| _d|v r |jnd| _d|v r*|jnd| _d|v r4|jnd| _d|v r>|jnd| _d|v rH|jnd| _d	|v rR|jnd
| _d|v r\|j	nd
| _	d|v rf|j
nd
| _d|v rp|jnd
| _d|v rz|jnd
| _d|v r„|jnd
| _|jr“tdi |j¤Ž| _|jrŸtdi |j¤Ž| _|jr¦tƒ | _|jr­tƒ | _|jr´tƒ | _|jrÎ|jd |jd ksÃJ ‚tdi |j¤Ž| _d S d S )Nú5 [!] Cannot use HingeGANLoss and MSEGANLoss together.Úuse_stft_lossFÚuse_subband_stft_lossÚuse_mse_gan_lossÚuse_hinge_gan_lossÚuse_feat_match_lossÚuse_l1_spec_lossÚstft_loss_weightg        Úsubband_stft_loss_weightÚmse_G_loss_weightÚhinde_G_loss_weightÚfeat_match_loss_weightÚl1_spec_loss_weightr?   r   ) r   r   rƒ   r„   r   r‚   r…   r†   r‡   rˆ   r‰   Úmse_gan_loss_weightÚhinge_G_loss_weightÚhinge_gan_loss_weightr‹   rŒ   r+   Ústft_loss_paramsÚ	stft_lossrD   Úsubband_stft_loss_paramsÚsubband_stft_lossrL   rM   rT   Ú
hinge_lossrg   Úfeat_match_lossÚaudioÚl1_spec_loss_paramsr>   Úl1_spec_loss©r   ÚCr   r   r   r   ã   sB   
ÿÿþþzGeneratorLoss.__init__Nc              	   C   s|  d}d}	i }
| j r6|  |d d …d d …d | d¡…f  d¡| d¡¡\}}||
d< ||
d< || j||   }| jrJ|  ||¡}||
d< || j|  }| jrf|  	||¡\}}||
d< ||
d< || j
||   }| jr~|d ur~t|| jƒ}||
d	< |	| j|  }	| jr–|d u r–t|| jƒ}||
d
< |	| j|  }	| jr®|d ur®|  ||¡}||
d< |	| j|  }	||	 |
d< ||
d< |	|
d< |
S )Nr   rG   rF   ÚG_stft_loss_mgÚG_stft_loss_scÚG_l1_spec_lossÚG_subband_stft_loss_mgÚG_subband_stft_loss_scÚG_mse_fake_lossÚG_hinge_fake_lossÚG_feat_match_lossry   Ú
G_gen_lossÚ
G_adv_loss)r   r‘   ÚsizerJ   r‡   r†   r˜   rŒ   r‚   r“   rˆ   rƒ   rw   rM   r   r„   r”   r   r…   r•   r‹   )r   r   r   rt   Ú
feats_fakeÚ
feats_realÚ	y_hat_subÚy_subÚgen_lossru   Úreturn_dictÚstft_loss_mgÚstft_loss_scr˜   Úsubband_stft_loss_mgÚsubband_stft_loss_scÚmse_fake_lossÚhinge_fake_lossr•   r   r   r   r$     sB   8zGeneratorLoss.forward)NNNNNNNr%   r   r   r   r   r   Ú   s
    #ÿr   c                       r	   )ÚDiscriminatorLosszLike ```GeneratorLoss```c                    sR   t ƒ  ¡  |jr|jrJ dƒ‚|j| _|j| _|jrtƒ | _|jr'tƒ | _d S d S )Nr€   )r   r   rƒ   r„   rZ   rM   rf   r”   r™   r   r   r   r   :  s   
ÿÿþÿzDiscriminatorLoss.__init__c                 C   sŒ   d}i }| j r"t||| jd\}}}||d< ||d< ||d< ||7 }| jr@t||| jd\}}	}
||d< |	|d< |
|d< ||7 }||d	< |S )
Nr   )rt   rx   r]   ÚD_mse_gan_lossÚD_mse_gan_real_lossÚD_mse_gan_fake_lossÚD_hinge_gan_lossÚD_hinge_gan_real_lossÚD_hinge_gan_fake_lossry   )rƒ   r~   rM   r„   r”   )r   rt   rx   ry   r«   Ú
mse_D_lossÚmse_D_real_lossÚmse_D_fake_lossÚhinge_D_lossÚhinge_D_real_lossÚhinge_D_fake_lossr   r   r   r$   H  s(   ÿÿzDiscriminatorLoss.forwardr%   r   r   r   r   r²   7  s    r²   c                       s8   e Zd Zdeeef f‡ fdd„Zdefdd„Z‡  Z	S )ÚWaveRNNLossÚwave_rnn_modec                    sP   t ƒ  ¡  |dkrt| _d S |dkrt| _d S t|tƒr$tj 	¡ | _d S t
dƒ‚)NÚmoldÚgaussz [!] Unknown mode for Wavernn.)r   r   r   r]   r   rr   Úintr   r   ÚCrossEntropyLossÚ
ValueError)r   rÀ   r   r   r   r   c  s   



zWaveRNNLoss.__init__Úreturnc                 C   s   |   ||¡}d|iS )Nry   )r]   )r   r   r   ry   r   r   r   r$   n  s   zWaveRNNLoss.forward)
r&   r'   r(   r   ÚstrrÃ   r   r   r$   r*   r   r   r   r   r¿   b  s    r¿   )Útypingr   r   r   r   Útorch.nnr   r   Ú TTS.utils.audio.torch_transformsr   ÚTTS.vocoder.utils.distributionr   r   ÚModuler
   r+   r>   rD   rL   rT   rZ   rf   rg   rw   r~   r   r²   r¿   r   r   r   r   Ú<module>   s(    	]+