o
    wiM                     @   sx   d dl Z d dlmZmZ d dlmZmZ d dlmZ G dd deZ	G dd deZ
G d	d
 d
eZG dd deZdS )    N)Loss	typecheck)LossTypeVoidType)
NeuralTypec                   @   s2   e Zd Zedd Zedd Ze dd ZdS )KlLossc                 C   sB   t dt gt dt gt dt gt dt gt dt gdS )N)BDT)z_plogs_qm_plogs_pz_maskr   r   self r   d/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nemo/collections/tts/losses/vits_losses.pyinput_types0   s   zKlLoss.input_typesc                 C      dt t diS Nlosselements_typer   r   r   r   r   r   output_types:      zKlLoss.output_typesc                 C   sv   |  }|  }|  }|  }|  }|| d }|d|| d  td|  7 }t|| }|t| }|S )z
        z_p: Input distribution
        logs_q: LogVariance of target distrubution
        m_p: Mean of input distrubution
        logs_p: LogVariance of input distrubution
        g      ?   g       )floattorchexpsum)r   r   r   r   r   r   kllr   r   r   forward@   s   "zKlLoss.forwardN)__name__
__module____qualname__propertyr   r   r   r%   r   r   r   r   r   /   s    
	
r   c                   @   6   e Zd ZdZedd Zedd Ze dd ZdS )	FeatureMatchingLossz!VITS Feature Matching Loss modulec                 C   s"   t t dggt t dggdS )Nr   )fmap_rfmap_gr   r   r   r   r   r   X   s   zFeatureMatchingLoss.input_typesc                 C   r   r   r   r   r   r   r   r   _   r   z FeatureMatchingLoss.output_typesc              	   C   s`   d}t ||D ]$\}}t ||D ]\}}|  }| }|tt|| 7 }qq|d S )z4
        fmap_r, fmap_g: List[List[Tensor]]
        r   r   )zipr   detachr    meanabs)r   r,   r-   r   drdgrlglr   r   r   r%   e   s   zFeatureMatchingLoss.forwardN	r&   r'   r(   __doc__r)   r   r   r   r%   r   r   r   r   r+   U   s    

r+   c                   @   r*   )	DiscriminatorLosszDiscriminator Loss modulec                 C   s   t dt gt dt gdS )Nr   r
   )disc_real_outputsdisc_generated_outputsr   r   r   r   r   r   w   s   zDiscriminatorLoss.input_typesc                 C   s(   t t dt t dgt t dgdS )Nr   )r   real_lossesfake_lossesr   r   r   r   r   r   ~   s   
zDiscriminatorLoss.output_typesc                 C   s   g }g }d}t t||D ]2\}\}}| }| }td| d }	t|d }
||	|
 7 }||	  ||
  q|||fS Nr      r   )	enumerater.   r   r    r0   appenditem)r   r:   r;   r_lossesg_lossesr   ir2   r3   r_lossg_lossr   r   r   r%      s   
zDiscriminatorLoss.forwardNr6   r   r   r   r   r8   t   s    

r8   c                   @   r*   )	GeneratorLosszGenerator Loss modulec                 C   s   dt dt giS )Ndisc_outputsr9   r   r   r   r   r   r      s   zGeneratorLoss.input_typesc                 C   s   t t dt t dgdS )Nr   )r   r=   r   r   r   r   r   r      s   
zGeneratorLoss.output_typesc                 C   sF   d}g }|D ]}|  }td| d }|| ||7 }q||fS r>   )r   r    r0   rA   )r   rI   r   
gen_lossesr3   r$   r   r   r   r%      s   

zGeneratorLoss.forwardNr6   r   r   r   r   rH      s    

rH   )r    nemo.core.classesr   r   nemo.core.neural_types.elementsr   r   "nemo.core.neural_types.neural_typer   r   r+   r8   rH   r   r   r   r   <module>   s   '&#