o
    }oi                     @   s\   d dl Z d dlmZ d dlmZmZ d dlmZmZm	Z	m
Z
 d dlmZ G dd deZdS )    N)get_mask_from_lengths)Loss	typecheck)LengthsType
LogitsTypeLossTypeMelSpectrogramType)
NeuralTypec                   @   s6   e Zd ZdZedd Zedd Ze dd ZdS )	Tacotron2Lossz.A Loss module that computes loss for Tacotron2c                 C   s<   t dt t dt t dt t dt t dt t  dS )N)BDTr   r   r   )spec_pred_decspec_pred_postnet	gate_predspec_targetspec_target_len	pad_value)r	   r   r   r   self r   ]/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/tts/losses/tacotron2loss.pyinput_types   s   




zTacotron2Loss.input_typesc                 C   s   t t dt dt dS )N)elements_typer   )lossgate_target)r	   r   r   r   r   r   r   output_types%   s   

zTacotron2Loss.output_typesc                C   s  |j d }t|j d |}||}t|D ]\}	}
d||	|
jd d f< qd|_d|_|dd}|j d }||j d k rW|dd|}|dd|}|dd|	 }n2||j d kr||j d  }tj
jj|d|f|d}tj
jj|d|f|d}tj
jj|d|fdd}t|| }||j d |d|d}|ddd}|j|| |j|| |j|d d dd d f d |dd}tj
j||}tj
j||}tj
j||}|| | |fS )N   r      F)valueg     @@)shapetorchzerostype_as	enumeratedatarequires_gradviewnarrow
contiguousnn
functionalpadr   expandsizepermutemasked_fill_mse_loss binary_cross_entropy_with_logits)r   r   r   r   r   r   r   max_lenr   ilength
pad_amountmaskrnn_mel_losspostnet_mel_loss	gate_lossr   r   r   forward,   s:   


  zTacotron2Loss.forwardN)	__name__
__module____qualname____doc__propertyr   r   r   r=   r   r   r   r   r
      s    


r
   )r#   (nemo.collections.tts.parts.utils.helpersr   nemo.core.classesr   r   nemo.core.neural_types.elementsr   r   r   r   "nemo.core.neural_types.neural_typer	   r
   r   r   r   r   <module>   s   