o
    óôj.  ã                   @   s®   d Z ddlZddlmZmZ ddlZddlm  mZ	 ddl
ZG dd„ dejjƒZG dd„ deƒZG dd	„ d	eƒZG d
d„ deƒZG dd„ dejjƒZG dd„ dejjƒZdS )zPositonal Encoding Module.é    N)ÚTupleÚUnionc                	       s”   e Zd ZdZ		ddedededef‡ fdd	„Z	
ddej	de
eej	f deej	ej	f fdd„Z	dde
eej	f dededej	fdd„Z‡  ZS )ÚPositionalEncodingzüPositional encoding.

    :param int d_model: embedding dim
    :param float dropout_rate: dropout rate
    :param int max_len: maximum input length

    PE(pos, 2i)   = sin(pos/(10000^(2i/dmodel)))
    PE(pos, 2i+1) = cos(pos/(10000^(2i/dmodel)))
    éˆ  FÚd_modelÚdropout_rateÚmax_lenÚreversec                    sä   t ƒ  ¡  || _t | j¡| _tjj|d�| _	|| _
t | j
| j¡| _tjd| j
tjd� d¡}t tjd| jdtjd�t d¡| j   ¡}t || ¡| jdd…ddd…f< t || ¡| jdd…ddd…f< | j d¡| _dS )ú'Construct an PositionalEncoding object.©Úpr   ©Údtypeé   é   ç     ˆÃ@N)ÚsuperÚ__init__r   ÚmathÚsqrtÚxscaleÚtorchÚnnÚDropoutÚdropoutr   ÚzerosÚpeÚarangeÚfloat32Ú	unsqueezeÚexpÚlogÚsinÚcos)Úselfr   r   r   r	   ÚpositionÚdiv_term©Ú	__class__© úU/var/www/html/academy/tools/voices/clone/CosyVoice/cosyvoice/transformer/embedding.pyr   %   s&   

ÿÿÿÿ""zPositionalEncoding.__init__r   ÚxÚoffsetÚreturnc                 C   sF   | j  |j¡| _ |  || d¡d¡}|| j | }|  |¡|  |¡fS )aN  Add positional encoding.

        Args:
            x (torch.Tensor): Input. Its shape is (batch, time, ...)
            offset (int, torch.tensor): position offset

        Returns:
            torch.Tensor: Encoded tensor. Its shape is (batch, time, ...)
            torch.Tensor: for compatibility to RelPositionalEncoding
        r   F)r   ÚtoÚdeviceÚposition_encodingÚsizer   r   ©r$   r+   r,   Úpos_embr)   r)   r*   Úforward;   s   zPositionalEncoding.forwardTr1   Úapply_dropoutc                 C   sè   t |tƒr|| | jksJ ‚| jdd…||| …f }nOt |tjƒr?| ¡ dkr?|| | jks1J ‚| jdd…||| …f }n,t |¡| | jksKJ ‚| d¡t 	d|¡ 
|j¡ }|dk}|| }t || jd ¡}|rr|  |¡}|S )á   For getting encoding in a streaming fashion

        Attention!!!!!
        we apply dropout only once at the whole utterance level in a none
        streaming way, but will call this function several times with
        increasing input size in a streaming scenario, so the dropout will
        be applied several times.

        Args:
            offset (int or torch.tensor): start offset
            size (int): required size of position encoding

        Returns:
            torch.Tensor: Corresponding encoding
        Nr   r   )Ú
isinstanceÚintr   r   r   ÚTensorÚdimÚmaxr   r   r.   r/   ÚFÚ	embeddingr   )r$   r,   r1   r5   r3   ÚindexÚflagr)   r)   r*   r0   O   s    
ÿ
z$PositionalEncoding.position_encoding)r   F©r   )T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r8   ÚfloatÚboolr   r   r9   r   r   r4   r0   Ú__classcell__r)   r)   r'   r*   r      s>    üÿþýüþÿþ
ýýÿþýýr   c                	       sb   e Zd ZdZddededef‡ fdd„Z	dd	ejd
e	eejf de
ejejf fdd„Z‡  ZS )ÚRelPositionalEncodingzñRelative positional encoding module.
    See : Appendix B in https://arxiv.org/abs/1901.02860
    Args:
        d_model (int): Embedding dimension.
        dropout_rate (float): Dropout rate.
        max_len (int): Maximum input length.
    r   r   r   r   c                    s   t ƒ j|||dd� dS )zInitialize class.T)r	   N)r   r   ©r$   r   r   r   r'   r)   r*   r   �   s   zRelPositionalEncoding.__init__r   r+   r,   r-   c                 C   sB   | j  |j¡| _ || j }|  || d¡d¡}|  |¡|  |¡fS )a  Compute positional encoding.
        Args:
            x (torch.Tensor): Input tensor (batch, time, `*`).
        Returns:
            torch.Tensor: Encoded tensor (batch, time, `*`).
            torch.Tensor: Positional embedding tensor (1, time, `*`).
        r   F)r   r.   r/   r   r0   r1   r   r2   r)   r)   r*   r4   …   s   
zRelPositionalEncoding.forward©r   r@   )rA   rB   rC   rD   r8   rE   r   r   r9   r   r   r4   rG   r)   r)   r'   r*   rH   x   s    þÿþýrH   c                       ó0   e Zd ZdZddededef‡ fdd„Z‡  ZS )	ÚWhisperPositionalEncodingz@ Sinusoids position encoding used in openai-whisper.encoder
    éÜ  r   r   r   c                    s®   t ƒ  |||¡ d| _t d¡|d d  }t | t |d ¡ ¡}t |¡d d …tjf |tjd d …f  }tj	t 
|¡t |¡gdd�}t| dƒ |  d| d¡¡ d S )Nç      ð?i'  r   r   ©r:   r   r   )r   r   r   Únpr!   r   r    r   ÚnewaxisÚcatr"   r#   ÚdelattrÚregister_bufferr   )r$   r   r   r   Úlog_timescale_incrementÚinv_timescalesÚscaled_timer   r'   r)   r*   r   š   s   ÿÿ
z"WhisperPositionalEncoding.__init__)rM   ©rA   rB   rC   rD   r8   rE   r   rG   r)   r)   r'   r*   rL   –   ó    $rL   c                       rK   )	ÚLearnablePositionalEncodingz@ Learnable position encoding used in openai-whisper.decoder
    éÀ  r   r   r   c                    s2   t ƒ  |||¡ tj t d||¡¡| _d| _d S )Nr   rN   )r   r   r   r   Ú	ParameterÚemptyr   r   rI   r'   r)   r*   r   «   s   
z$LearnablePositionalEncoding.__init__)r[   rX   r)   r)   r'   r*   rZ   §   rY   rZ   c                	       s~   e Zd ZdZdedef‡ fdd„Z	ddejde	eejf d	e
ejejf fd
d„Zde	eejf ded	ejfdd„Z‡  ZS )ÚNoPositionalEncodingz No position encoding
    r   r   c                    s$   t ƒ  ¡  || _tjj|d�| _d S )Nr   )r   r   r   r   r   r   r   )r$   r   r   r'   r)   r*   r   ¶   s   
zNoPositionalEncoding.__init__r   r+   r,   r-   c                 C   s,   t  d| d¡| j¡ |j¡}|  |¡|fS )z= Just return zero vector for interface compatibility
        r   )r   r   r1   r   r.   r/   r   r2   r)   r)   r*   r4   »   s   zNoPositionalEncoding.forwardr1   c                 C   s   t  d|| j¡S )Nr   )r   r   r   )r$   r,   r1   r)   r)   r*   r0   Ä   s   z&NoPositionalEncoding.position_encodingr@   )rA   rB   rC   rD   r8   rE   r   r   r9   r   r   r4   r0   rG   r)   r)   r'   r*   r^   ²   s     þÿþ
ý	ÿÿr^   c                	       s’   e Zd ZdZddededef‡ fdd„Zdejfd	d
„Z	ddejde
eejf deejejf fdd„Zde
eejf dedejfdd„Z‡  ZS )ÚEspnetRelPositionalEncodingaR  Relative positional encoding module (new implementation).

    Details can be found in https://github.com/espnet/espnet/pull/2816.

    See : Appendix B in https://arxiv.org/abs/1901.02860

    Args:
        d_model (int): Embedding dimension.
        dropout_rate (float): Dropout rate.
        max_len (int): Maximum input length.

    r   r   r   r   c                    sT   t t| ƒ ¡  || _t | j¡| _tjj	|d�| _
d| _|  t d¡ d|¡¡ dS )r
   r   Ng        r   )r   r_   r   r   r   r   r   r   r   r   r   r   Ú	extend_peÚtensorÚexpandrI   r'   r)   r*   r   ×   s   z$EspnetRelPositionalEncoding.__init__r+   c                 C   sª  | j dur/| j  d¡| d¡d d kr/| j j|jks"| j j|jkr-| j j|j|jd�| _ dS t | d¡| j¡}t | d¡| j¡}tjd| d¡tj	d� 
d¡}t tjd| jdtj	d�t d¡| j   ¡}t || ¡|dd…ddd…f< t || ¡|dd…ddd…f< t d| | ¡|dd…ddd…f< t d| | ¡|dd…ddd…f< t |dg¡ 
d¡}|dd…  
d¡}tj||gdd	�}|j|j|jd
�| _ dS )zReset the positional encodings.Nr   r   )r   r/   r   r   r   éÿÿÿÿrO   )r/   r   )r   r1   r   r/   r.   r   r   r   r   r   r   r    r   r!   r"   r#   ÚfliprR   )r$   r+   Zpe_positiveZpe_negativer%   r&   r   r)   r)   r*   r`   à   s*   
ÿÿ  $$z%EspnetRelPositionalEncoding.extend_per   r,   r-   c                 C   s<   |   |¡ || j }| j| d¡|d�}|  |¡|  |¡fS )z¿Add positional encoding.

        Args:
            x (torch.Tensor): Input tensor (batch, time, `*`).

        Returns:
            torch.Tensor: Encoded tensor (batch, time, `*`).

        r   )r1   r,   )r`   r   r0   r1   r   r2   r)   r)   r*   r4      s   

z#EspnetRelPositionalEncoding.forwardr1   c                 C   s¢   t |tƒr(| jdd…| j d¡d | | d | j d¡d | | …f }|S t |tjƒrO| jdd…| j d¡d | | d | j d¡d | | …f }|S )r6   Nr   r   )r7   r8   r   r1   r   r9   )r$   r,   r1   r3   r)   r)   r*   r0     s   
2ÿÿ	û2ÿÿz-EspnetRelPositionalEncoding.position_encodingrJ   r@   )rA   rB   rC   rD   r8   rE   r   r   r9   r`   r   r   r4   r0   rG   r)   r)   r'   r*   r_   É   s    	 
ÿÿþþr_   )rD   r   Útypingr   r   r   Ztorch.nn.functionalr   Ú
functionalr<   ÚnumpyrP   ÚModuler   rH   rL   rZ   r^   r_   r)   r)   r)   r*   Ú<module>   s   ^