o
    óôjƒ  ã                   @   s.   d Z ddlZddlmZ G dd„ dejƒZdS )zLabel smoothing module.é    N)Únnc                	       sR   e Zd ZdZ	ddedededef‡ fdd„Zd	ej	d
ej	dej	fdd„Z
‡  ZS )ÚLabelSmoothingLossa%  Label-smoothing loss.

    In a standard CE loss, the label's data distribution is:
    [0,1,2] ->
    [
        [1.0, 0.0, 0.0],
        [0.0, 1.0, 0.0],
        [0.0, 0.0, 1.0],
    ]

    In the smoothing version CE Loss,some probabilities
    are taken from the true label prob (1.0) and are divided
    among other labels.

    e.g.
    smoothing=0.1
    [0,1,2] ->
    [
        [0.9, 0.05, 0.05],
        [0.05, 0.9, 0.05],
        [0.05, 0.05, 0.9],
    ]

    Args:
        size (int): the number of class
        padding_idx (int): padding class id which will be ignored for loss
        smoothing (float): smoothing rate (0.0 means the conventional CE)
        normalize_length (bool):
            normalize loss by sequence length if True
            normalize loss by batch size if False
    FÚsizeÚpadding_idxÚ	smoothingÚnormalize_lengthc                    sB   t t| ƒ ¡  tjdd�| _|| _d| | _|| _|| _	|| _
dS )z'Construct an LabelSmoothingLoss object.Únone)Ú	reductiong      ð?N)Úsuperr   Ú__init__r   Ú	KLDivLossÚ	criterionr   Ú
confidencer   r   r   )Úselfr   r   r   r   ©Ú	__class__© ú`/var/www/html/academy/tools/voices/clone/CosyVoice/cosyvoice/transformer/label_smoothing_loss.pyr   6   s   

zLabelSmoothingLoss.__init__ÚxÚtargetÚreturnc           	      C   sÔ   |  d¡| j ks
J ‚|  d¡}| d| j ¡}| d¡}t |¡}| | j| j d  ¡ || jk}t|ƒ| ¡  	¡  }| 
|d¡}| d| d¡| j¡ |  tj|dd�|¡}| jr[|n|}| 
| d¡d¡ ¡ | S )aû  Compute loss between x and target.

        The model outputs and data labels tensors are flatten to
        (batch*seqlen, class) shape and a mask is applied to the
        padding part which should not be calculated for loss.

        Args:
            x (torch.Tensor): prediction (batch, seqlen, class)
            target (torch.Tensor):
                target signal masked with self.padding_id (batch, seqlen)
        Returns:
            loss (torch.Tensor) : The KL loss, scalar float value
        é   r   éÿÿÿÿé   )Údim)r   ÚviewÚtorchÚ
zeros_likeÚfill_r   r   ÚlenÚsumÚitemÚmasked_fillÚscatter_Ú	unsqueezer   r   Úlog_softmaxr   )	r   r   r   Ú
batch_sizeZ	true_distÚignoreÚtotalÚklÚdenomr   r   r   ÚforwardD   s   



zLabelSmoothingLoss.forward)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚintÚfloatÚboolr   r   ÚTensorr+   Ú__classcell__r   r   r   r   r      s    $üÿþýü$r   )r/   r   r   ÚModuler   r   r   r   r   Ú<module>   s   