o
    Si"                     @   s   d dl mZmZ d dlZd dlZd dlmZ d dlm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S )    )ListTupleN)nn)safe_logc                	       sF   e Zd ZdZ	ddededed	ef fd
dZdejfddZ  Z	S )MelSpecReconstructionLosszw
    L1 distance between the mel-scaled magnitude spectrograms of the ground truth sample and the generated sample
    ]        d   sample_raten_fft
hop_lengthn_melsc                    s(   t    tjj||||ddd| _d S )NT   )r   r   r   r   centerpower)super__init__
torchaudio
transformsMelSpectrogrammel_spec)selfr   r   r   r   	__class__ >/home/ubuntu/.local/lib/python3.10/site-packages/vocos/loss.pyr      s   
z"MelSpecReconstructionLoss.__init__returnc                 C   s0   t | |}t | |}tjj||}|S )z
        Args:
            y_hat (Tensor): Predicted audio waveform.
            y (Tensor): Ground truth audio waveform.

        Returns:
            Tensor: L1 loss between the mel-scaled magnitude spectrograms.
        )r   r   torchr   
functionall1_loss)r   y_hatymel_hatmellossr   r   r   forward   s   	z!MelSpecReconstructionLoss.forward)r   r   r	   r
   )
__name__
__module____qualname____doc__intr   r   Tensorr&   __classcell__r   r   r   r   r   
   s    r   c                   @   s8   e Zd ZdZdeej deejeej f fddZdS )GeneratorLosszf
    Generator Loss module. Calculates the loss for the generator based on discriminator outputs.
    disc_outputsr   c                 C   s\   t jd|d j|d jd}g }|D ]}t t jd| dd}|| ||7 }q||fS )a  
        Args:
            disc_outputs (List[Tensor]): List of discriminator outputs.

        Returns:
            Tuple[Tensor, List[Tensor]]: Tuple containing the total loss and a list of loss values from
                                         the sub-discriminators
        r   r   devicedtypemin)r   zerosr1   r2   meanclampappend)r   r/   r%   
gen_lossesdglr   r   r   r&   -   s   	

zGeneratorLoss.forwardN	r'   r(   r)   r*   r   r   r,   r   r&   r   r   r   r   r.   (   s    ,r.   c                
   @   sJ   e Zd ZdZdeej deej deejeej eej f fddZdS )DiscriminatorLosszs
    Discriminator Loss module. Calculates the loss for the discriminator based on real and generated outputs.
    disc_real_outputsdisc_generated_outputsr   c           
      C   s   t jd|d j|d jd}g }g }t||D ],\}}t t jd| dd}t t jd| dd}	|||	 7 }|| ||	 q|||fS )a,  
        Args:
            disc_real_outputs (List[Tensor]): List of discriminator outputs for real samples.
            disc_generated_outputs (List[Tensor]): List of discriminator outputs for generated samples.

        Returns:
            Tuple[Tensor, List[Tensor], List[Tensor]]: A tuple containing the total loss, a list of loss values from
                                                       the sub-discriminators for real outputs, and a list of
                                                       loss values for generated outputs.
        r   r   r0   r3   )r   r5   r1   r2   zipr6   r7   r8   )
r   r>   r?   r%   r_lossesg_lossesdrr:   r_lossg_lossr   r   r   r&   E   s   

zDiscriminatorLoss.forwardNr<   r   r   r   r   r=   @   s    r=   c                   @   s<   e Zd ZdZdeeej  deeej  dejfddZdS )FeatureMatchingLossz|
    Feature Matching Loss module. Calculates the feature matching loss between feature maps of the sub-discriminators.
    fmap_rfmap_gr   c              	   C   sh   t jd|d d j|d d jd}t||D ]\}}t||D ]\}}|t t || 7 }q q|S )a
  
        Args:
            fmap_r (List[List[Tensor]]): List of feature maps from real samples.
            fmap_g (List[List[Tensor]]): List of feature maps from generated samples.

        Returns:
            Tensor: The calculated feature matching loss.
        r   r   r0   )r   r5   r1   r2   r@   r6   abs)r   rG   rH   r%   rC   r:   rlglr   r   r   r&   d   s   $	zFeatureMatchingLoss.forwardN)r'   r(   r)   r*   r   r   r,   r&   r   r   r   r   rF   _   s    0rF   )typingr   r   r   r   r   vocos.modulesr   Moduler   r.   r=   rF   r   r   r   r   <module>   s    