o
    }oid'                     @   sd   d Z ddlZddlmZmZmZ ddlmZmZm	Z	m
Z
mZ ddlmZ dgZG dd deeZdS )z
This file contains code artifacts adapted from the original implementation:
https://github.com/google-research/google-research/blob/master/schema_guided_dst/baseline/train_and_predict.py
    N)LossTyping	typecheck)ChannelType
LabelsType
LogitsTypeLossType
NeuralType)loggingSGDDialogueStateLossc                       sX   e Zd ZdZedd Zedd Zddef fdd	ZdddZ	e
 dd Z  ZS )r   z<
    Neural module which implements loss for SGD model.
    c                 C   s   t dt t dt t dt t dt t dt t dt t dt t dt t dt t dt t dt t dt t dt t dt dS )a  Returns definitions of module input ports.
            logit_intent_status: Output of SGD model
            intent_status: intent label
            logit_req_slot_status: Output of SGD model
            requested_slot_status: Takes value 1 if the corresponding slot is requested, 0 otherwise
            logit_cat_slot_status: Output of SGD model
            categorical_slot_status: The status of each categorical slot in the service
            logit_cat_slot_value_status: Output of SGD model
            categorical_slot_value_status: Takes value 1 if the corresponding slot value is correct, 0 otherwise
            logit_noncat_slot_status: Output of SGD model
            noncategorical_slot_status: The status of each noncategorical slot in the service            logit_spans: Output of SGD model
            noncategorical_slot_value_start: The index of the starting subword corresponding to the slot span for a non-categorical slot value
            noncategorical_slot_value_end: The index of the ending (inclusive) subword corresponding to the slot span for a non-categorical slot value
            task_mask: Mask contains 1 if its the current task, 0 otherwise
        )BTr   )r   r   D)logit_intent_statusintent_statuslogit_req_slot_statusrequested_slot_statuslogit_cat_slot_statuscategorical_slot_statuslogit_cat_slot_value_statuscategorical_slot_value_statuslogit_noncat_slot_statusnoncategorical_slot_statuslogit_spansnoncategorical_slot_value_startnoncategorical_slot_value_end	task_mask)r	   r   r   r   self r   X/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/nlp/losses/sgd_loss.pyinput_types#   s   













z SGDDialogueStateLoss.input_typesc                 C   s   dt t diS )zh
        Returns definitions of module output ports.
        loss:
            NeuralType(None)
        loss)elements_type)r	   r   r   r   r   r    output_typesF   s   z!SGDDialogueStateLoss.output_typesmean	reductionc                    sT   t    |dvrt| d d}|| _tjj| jd| _tjj	| jd| _
dS )zy
        Args:
            reduction: specifies the reduction to apply to the final loss, choose 'mean' or 'sum'
        )r%   sumz8 reduction is not supported. Setting reduction to "mean"r%   )r&   N)super__init__r
   warningr&   torchnnCrossEntropyLoss_cross_entropyBCEWithLogitsLoss_cross_entropy_bin)r   r&   	__class__r   r    r)   O   s   
zSGDDialogueStateLoss.__init__Nc                 C   sd   t j|ddd}t j|ddd}|dur.|jt jur|dk}t j|ddd}|| }|| }||fS )a@  
        flattens logits and labels according loss mask
        Args:
            logits: logits
            labels: labels
            loss_mask: loss mask
        Returns:
            logits_flatten: flattened logits where loss mask is true
            labels_flatten: flattened labels where loss mask is true
        r   )	start_dimend_dimNg      ?)r+   flattendtypebool)r   logitslabels	loss_masklogits_flattenlabels_flattenloss_mask_flattenr   r   r    _helper^   s   zSGDDialogueStateLoss._helperc           $      C   s  |}|  |||d d df \}}t|dkr%tt|ddd}n
| |jdd|}|}|  |||d d df \}}t|dkrTtt|ddd}n
| |jdd|}|}|  |||d d df \}}t|dkrtt|ddd}n| ||}|}|  |||d d df \}}t|dkrtt|ddd}n
| |jdd|}|	}|  |	|
|d d df \}	}
t|
dkrtt|ddd}n| |	|
}tj	|dd\}}|
 \}}|}|  |||d d df \}}t|dkrtt|ddd}n| ||}|}|  |||d d df \}}t|dkrDtt|ddd} n| ||} ||||||| d	}!t|! }"| jd
krh|"t|! }"|"S |jd }#|"|# }"|"S )Nr   r6   )dim               )intent_lossrequested_slot_losscat_slot_status_losscat_slot_value_status_lossnoncat_slot_status_lossspan_start_lossspan_end_lossr%   )r@   lenr+   clampmaxviewr0   squeezer.   unbindsizer'   valuesr&   shape)$r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   old_logit_intent_statusrG   old_logit_req_slot_statusrH   old_logit_cat_slot_statusrI   old_logit_cat_slot_value_statusrJ   old_logit_noncat_slot_statusrK   logit_noncat_slot_startlogit_noncat_slot_end_max_num_tokensold_logit_noncat_slot_startrL   old_logit_noncat_slot_endrM   losses
total_loss
batch_sizer   r   r    forwardu   s   

zSGDDialogueStateLoss.forward)r%   )N)__name__
__module____qualname____doc__propertyr!   r$   strr)   r@   r   re   __classcell__r   r   r1   r    r      s    
"

)ri   r+   nemo.core.classesr   r   r   nemo.core.neural_typesr   r   r   r   r	   
nemo.utilsr
   __all__r   r   r   r   r    <module>   s   