o
    ´‹
j§	  ã                   @   s2   d dl Z d dlZd dlmZ G dd„ dejƒZdS )é    N)Únnc                       s,   e Zd ZdZd
‡ fdd„	Zddd	„Z‡  ZS )ÚPositionalEncodingae  Sinusoidal positional encoding for non-recurrent neural networks.
    Implementation based on "Attention Is All You Need"

    Args:
       channels (int): embedding size
       dropout_p (float): dropout rate applied to the output.
       max_len (int): maximum sequence length.
       use_scale (bool): whether to use a learnable scaling coefficient.
    ç        éˆ  Fc                    s  t ƒ  ¡  |d dkrtd |¡ƒ‚|| _|r!tj t d¡¡| _	t 
||¡}t d|¡ d¡}t dt d|d¡ ¡ | ¡}t | ¡ | ¡|d d …dd d…f< t | ¡ | ¡|d d …dd d…f< | d¡ dd¡}|  d|¡ |dkr}tj|d| _|| _d S )Né   r   zLCannot use sin/cos positional encoding with odd channels (got channels={:d})é   i'  Úpe)Úp)ÚsuperÚ__init__Ú
ValueErrorÚformatÚ	use_scaleÚtorchr   Ú	ParameterÚonesÚscaleÚzerosÚarangeÚ	unsqueezeÚpowÚfloatÚsinÚcosÚ	transposeÚregister_bufferÚDropoutÚdropoutÚchannels)Úselfr   Ú	dropout_pÚmax_lenr   r   ÚpositionÚdiv_term©Ú	__class__© úV/home/kuhnn/.local/lib/python3.10/site-packages/TTS/tts/layers/generic/pos_encoding.pyr      s$   
ÿ$$
zPositionalEncoding.__init__Nc                 C   s*  |t  | j¡ }|du rb| j d¡| d¡k r)td| d¡› d| j d¡› dƒ‚|durA| jdd…dd…d| d¡…f | }n| jdd…dd…d| d¡…f }| jr]|| j|  }n,|| }n'| jry|| j| jdd…dd…||…f   }n|| jdd…dd…||…f  }t| dƒr“|  	|¡}|S )zƒ
        Shapes:
            x: [B, C, T]
            mask: [B, 1, T]
            first_idx: int
            last_idx: int
        Nr   zSequence is z& but PositionalEncoding is limited to z. See max_len argument.r   )
ÚmathÚsqrtr   r   ÚsizeÚRuntimeErrorr   r   Úhasattrr   )r   ÚxÚmaskÚ	first_idxÚlast_idxÚpos_encr&   r&   r'   Úforward&   s(   	
ÿÿ("
( 

zPositionalEncoding.forward)r   r   F)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r2   Ú__classcell__r&   r&   r$   r'   r      s    
r   )r(   r   r   ÚModuler   r&   r&   r&   r'   Ú<module>   s    