o
    wi>                     @   s(   d dl Z d dlm  mZ dddZdS )    NTc                 C   s   |j | j kr|| j }|dur<t   |j |j kr!||j }||ddk| ~W d   n1 s7w   Y  |rB|  } t| |S )a{  
    Compute the masked cross-entropy loss between logits and targets.

    If a mask is provided, the loss is computed per element, multiplied by the mask,
    and then averaged. If no mask is provided, the standard cross-entropy loss is used.

    Args:
        logits (torch.Tensor): The predicted logits with shape (N, C) where C is the number of classes.
        targets (torch.Tensor): The ground truth class indices with shape (N,).
        mask (torch.Tensor, optional): A tensor that masks the loss computation. Items marked with
            1 will be used to calculate loss, otherwise ignored. Must be broadcastable to the shape
            of the loss. Defaults to None.
        fp32_upcast (bool, optional): if True it will cast logits to float32 before computing
        cross entropy. Default: True.
    Returns:
        torch.Tensor: The computed loss as a scalar tensor.
    Nr   )	devicetotorchno_gradmasked_fill_viewfloatFcross_entropy)logitstargetsmaskfp32_upcastignore_index r   Z/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nemo/automodel/loss/masked_ce.pymasked_cross_entropy   s   
r   )NTr   )r   torch.nn.functionalnn
functionalr   r   r   r   r   r   <module>   s   