o
    }oi
                     @   sp   d dl mZ d dlZd dlmZ d dlmZmZmZ d dlm	Z	m
Z
mZmZmZ dgZG dd dejeeZdS )    )ListN)nn)SerializationTyping	typecheck)
LabelsType
LogitsTypeLossTypeMaskType
NeuralTypeBCEWithLogitsLossc                	       s   e Zd ZdZedd Zedd Z				dd	ed
ej	de
dej	f fddZe ddedee dej	f fddZ  ZS )r   zi
    BCEWithLogitsLoss

    https://pytorch.org/docs/1.9.1/generated/torch.nn.BCEWithLogitsLoss.html
    c                 C   s`   t dgdg| jd   t t dgdg| jd   t gt dgdg| jd   t dddS )z3Returns definitions of module input ports.
        BANY      T)optional)logitslabels	loss_mask)r   _logits_dimr   r   r
   self r   b/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/common/losses/bce_logits_loss.pyinput_types!   s    zBCEWithLogitsLoss.input_typesc                 C   s   dt t diS )z4Returns definitions of module output ports.
        loss)elements_type)r   r	   r   r   r   r   output_types+   s   zBCEWithLogitsLoss.output_typesr   Nmeanlogits_ndimweight	reduction
pos_weightc                    s8   |durt |st |}t j|||d || _dS )a  
        Args:
            logits_ndim: number of dimensions (or rank) of the logits tensor
            weight: list of rescaling weight given to each class
            reduction: type of the reduction over the batch
            pos_weight: weight given to positive samples
        N)r    r"   r!   )torch	is_tensorFloatTensorsuper__init__r   )r   r   r    r!   r"   	__class__r   r   r'   1   s   

zBCEWithLogitsLoss.__init__r   r   r   c                    s$   t |}|  }t ||S )zl
        Args:
            logits: output of the classifier
            labels: ground truth labels
        )r#   stacktfloatr&   forward)r   r   r   r   r(   r   r   r-   E   s   
zBCEWithLogitsLoss.forward)r   Nr   N)N)__name__
__module____qualname____doc__propertyr   r   intr#   Tensorstrr'   r   r,   r   r-   __classcell__r   r   r(   r   r      s*    
	
,)typingr   r#   r   nemo.core.classesr   r   r   nemo.core.neural_typesr   r   r	   r
   r   __all__r   r   r   r   r   <module>   s   