o
    wiv                     @   s   d dl Z d dlm  mZ d dlmZ d dlmZm	Z	 d dl
mZmZmZmZmZmZ d dlmZ G dd deZG dd	 d	eZG d
d deZG dd deZdS )    N)mask_from_lens)Loss	typecheck)LengthsTypeLossTypeMelSpectrogramTypeRegressionValuesTypeTokenDurationTypeTokenLogDurationType)
NeuralTypec                       D   e Zd Zd
 fdd	Zedd Zedd Ze dd	 Z  Z	S )DurationLoss皙?c                       t    || _d S Nsuper__init__
loss_scaleselfr   	__class__ f/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nemo/collections/tts/losses/fastpitchloss.pyr   >      

zDurationLoss.__init__c                 C   s$   t dt t dt t dt dS )NBTr   )log_durs_predicteddurs_tgtlen)r   r
   r	   r   r   r   r   r   input_typesB      


zDurationLoss.input_typesc                 C      dt t diS Nloss)elements_typer   r   r"   r   r   r   output_typesJ      zDurationLoss.output_typesc                 C   sZ   t ||dd}t| d }tj}|||dd}||  |  }|| j9 }|S )N   max_lennone	reduction)	r   sizetorchlogfloatFmse_losssumr   )r   r   r    r!   dur_masklog_durs_tgtloss_fndur_lossr   r   r   forwardP   s   
zDurationLoss.forwardr   
__name__
__module____qualname__r   propertyr#   r*   r   r=   __classcell__r   r   r   r   r   =       

r   c                       r   )	PitchLossr   c                    r   r   r   r   r   r   r   r   ]   r   zPitchLoss.__init__c                 C   $   t dt t dt t dt dS )Nr   r   )pitch_predicted	pitch_tgtr!   r   r   r   r"   r   r   r   r#   a   r$   zPitchLoss.input_typesc                 C   r%   r&   r)   r"   r   r   r   r*   i   r+   zPitchLoss.output_typesc                 C   sp   t ||dd}|d|d }tj|d|ddfdd}tj||dd}||  |  }|| j9 }|S )Nr,   r-   r           valuer/   r0   )r   r2   r6   padr7   r8   r   )r   rH   rI   r!   r9   ldiff
pitch_lossr   r   r   r=   o   s   
zPitchLoss.forwardr>   r?   r   r   r   r   rF   \   rE   rF   c                       r   )
EnergyLossr   c                    r   r   r   r   r   r   r   r   |   r   zEnergyLoss.__init__c                 C   rG   )Nr   r   )energy_predicted
energy_tgtlengthrJ   r"   r   r   r   r#      r$   zEnergyLoss.input_typesc                 C   r%   r&   r)   r"   r   r   r   r*      r+   zEnergyLoss.output_typesc                 C   sP   |d u rdS t ||dd}tj||dd}||  |  }|| j9 }|S )NrK   r,   r-   r/   r0   )r   r2   r6   r7   r8   r   )r   rR   rS   rT   r9   energy_lossr   r   r   r=      s   
zEnergyLoss.forwardr>   r?   r   r   r   r   rQ   {   rE   rQ   c                   @   s2   e Zd Zedd Zedd Ze dd ZdS )MelLossc                 C   s   t dt t dt dS )N)r   Dr   )spect_predicted	spect_tgt)r   r   r"   r   r   r   r#      s   

zMelLoss.input_typesc                 C   r%   r&   r)   r"   r   r   r   r*      r+   zMelLoss.output_typesc                 C   s   d|_ |dd}|dd}|d|d }tj|ddd|ddfdd}|d }tj}|||dd}||  |  }|S )	NFr,      r   rK   rL   r/   r0   )	requires_grad	transposer2   r6   rN   ner5   r7   r8   )r   rX   rY   rO   mel_maskr;   mel_lossr   r   r   r=      s   zMelLoss.forwardN)r@   rA   rB   rC   r#   r*   r   r=   r   r   r   r   rV      s    

rV   )r3   torch.nn.functionalnn
functionalr6   (nemo.collections.tts.modules.transformerr   nemo.core.classesr   r   nemo.core.neural_types.elementsr   r   r   r   r	   r
   "nemo.core.neural_types.neural_typer   r   rF   rQ   rV   r   r   r   r   <module>   s   , 