o
    i}                     @   s6   d dl Z d dlmZ d dlmZ G dd dejZdS )    N)cross_entropy_lossc                       s8   e Zd Z								d fdd	Zdd	d
Z  ZS )CrossEntropyLossmean              ?FNc	           	         sN   t    |dvrtd|| _|| _|| _|| _|| _|| _|| _	|| _
dS )aF  
        Arguments:
            ignore_index: int. If labels == ignore_index, the loss is set to 0.0.
            label_smoothing: float
            lse_square_scale: float. If > 0, we add lse_square_scale * lse(logits) ^ 2 to the loss.
                This is also referred to as "z-loss".
            inplace_backward: bool. If True, we do the backward pass in-place by modifying the logits.
                This saves memory.
            process_group: if not None, we're doing Tensor Parallel: each process is responsible for
                one part of the vocab. The loss will be aggregated across processes.
            return_z_loss: bool. If True, we return the component of the loss contributed by
                the lse_square_scale value. This value is only for logging and does not support
                backprop.
        )r   nonesumz2Only support reduction = 'mean' or 'none' or 'sum'N)super__init__NotImplementedErrorignore_index	reductionlabel_smoothinglogit_scalelse_square_scaleinplace_backwardprocess_groupreturn_z_loss)	selfr   r   r   r   r   r   r   r   	__class__ U/home/ubuntu/vllm_env/lib/python3.10/site-packages/flash_attn/losses/cross_entropy.pyr   
   s   

zCrossEntropyLoss.__init__c                 C   s   |j r|j s
J dt|||| j| j| j| j| j| jd	\}}| jdkr0|	 || jk	  }n| jdkr:|	 }n|}| j
sA|S | jdkrU|	 || jk	  }||fS | jdkrb|	 }||fS |}||fS )a!  
        Arguments:
            input: (batch, vocab_size)
            target: (batch,)
        Returns:
            losses: (batch,) if reduction is 'none', else (1,), dtype float
            z_loss: (batch,) if reduction is 'none', else (1,), dtype float (if self.return_z_loss)
        zOnly support CUDA tensors)precomputed_lser   r   r   r   r   r   r   r	   )is_cudar   r   r   r   r   r   r   r   r	   r   )r   inputtargetr   lossz_lossr   r   r   forward/   s6   	





zCrossEntropyLoss.forward)r   r   r   r   r   FNF)N)__name__
__module____qualname__r   r    __classcell__r   r   r   r   r   	   s    %r   )torchtorch.nnnn#flash_attn.ops.triton.cross_entropyr   Moduler   r   r   r   r   <module>   s   