o
    óôjz1  ã                   @   s¤   d Z ddlmZ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ƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )zSubsampling layer definition.é    )ÚTupleÚUnionNc                       s>   e Zd Z‡ fdd„Zdeeejf dedejfdd„Z‡  Z	S )ÚBaseSubsamplingc                    s   t ƒ  ¡  d| _d| _d S )Nr   é   )ÚsuperÚ__init__Úright_contextÚsubsampling_rate)Úself©Ú	__class__© úW/var/www/html/academy/tools/voices/clone/CosyVoice/cosyvoice/transformer/subsampling.pyr      s   

zBaseSubsampling.__init__ÚoffsetÚsizeÚreturnc                 C   s   | j  ||¡S ©N)Úpos_encÚposition_encoding)r
   r   r   r   r   r   r      s   z!BaseSubsampling.position_encoding)
Ú__name__Ú
__module__Ú__qualname__r   r   ÚintÚtorchÚTensorr   Ú__classcell__r   r   r   r   r      s    ÿÿr   c                       ór   e Zd ZdZdedededejjf‡ fdd„Z		dd	ej
d
ej
deeej
f deej
ej
ej
f fdd„Z‡  ZS )ÚEmbedinigNoSubsamplingz(Embedding input without subsampling
    ÚidimÚodimÚdropout_rateÚpos_enc_classc                    s$   t ƒ  ¡  tj ||¡| _|| _d S r   )r   r   r   ÚnnÚ	EmbeddingÚembedr   ©r
   r   r   r    r!   r   r   r   r   '   s   

zEmbedinigNoSubsampling.__init__r   ÚxÚx_maskr   r   c                 C   ó$   |   |¡}|  ||¡\}}|||fS ©a„  Input x.

        Args:
            x (torch.Tensor): Input tensor (#batch, time, idim).
            x_mask (torch.Tensor): Input mask (#batch, 1, time).

        Returns:
            torch.Tensor: linear input tensor (#batch, time', odim),
                where time' = time .
            torch.Tensor: linear input mask (#batch, 1, time'),
                where time' = time .

        )r$   r   ©r
   r&   r'   r   Úpos_embr   r   r   Úforward-   ó   

zEmbedinigNoSubsampling.forward©r   ©r   r   r   Ú__doc__r   Úfloatr   r"   ÚModuler   r   r   r   r,   r   r   r   r   r   r   #   s    ÿ
üþýüûr   c                       r   )ÚLinearNoSubsamplingú¶Linear transform the input without subsampling

    Args:
        idim (int): Input dimension.
        odim (int): Output dimension.
        dropout_rate (float): Dropout rate.

    r   r   r    r!   c                    sP   t ƒ  ¡  tj tj ||¡tjj|dd�tj |¡¡| _|| _	d| _
d| _dS ©zConstruct an linear object.gñhãˆµøä>)Úepsr   r   N)r   r   r   r"   Ú
SequentialÚLinearÚ	LayerNormÚDropoutÚoutr   r   r	   r%   r   r   r   r   O   s   

ý
zLinearNoSubsampling.__init__r   r&   r'   r   r   c                 C   r(   r)   ©r;   r   r*   r   r   r   r,   \   r-   zLinearNoSubsampling.forwardr.   r/   r   r   r   r   r3   E   s    	ÿüþýüûr3   c                       r   )ÚConv1dSubsampling2a!  Convolutional 1D subsampling (to 1/2 length).
       It is designed for Whisper, ref:
       https://github.com/openai/whisper/blob/main/whisper/model.py

    Args:
        idim (int): Input dimension.
        odim (int): Output dimension.
        dropout_rate (float): Dropout rate.

    r   r   r    r!   c                    sb   t ƒ  ¡  tj tjj||ddd�tj ¡ tjj||dddd�tj ¡ ¡| _|| _d| _	d| _
dS )z'Construct an Conv1dSubsampling2 object.é   r   )Úkernel_sizeÚpaddingé   )r?   Ústrider@   é   N)r   r   r   r"   r7   ÚConv1dÚGELUÚconvr   r	   r   r%   r   r   r   r   €   s   
ü
zConv1dSubsampling2.__init__r   r&   r'   r   r   c                 C   sf   |  d¡}| dd¡}|  |¡}| dd¡}|  ||¡\}}|||dd…dd…|d d dd…f fS )aº  Subsample x.

        Args:
            x (torch.Tensor): Input tensor (#batch, time, idim).
            x_mask (torch.Tensor): Input mask (#batch, 1, time).

        Returns:
            torch.Tensor: Subsampled tensor (#batch, time', odim),
                where time' = time // 2.
            torch.Tensor: Subsampled mask (#batch, 1, time'),
                where time' = time // 2.
            torch.Tensor: positional encoding

        r   rA   N)r   Ú	transposerF   r   )r
   r&   r'   r   Útimer+   r   r   r   r,   ‘   s   

*zConv1dSubsampling2.forwardr.   r/   r   r   r   r   r=   t   s    ÿüþýüûr=   c                       r   )ÚConv2dSubsampling4zµConvolutional 2D subsampling (to 1/4 length).

    Args:
        idim (int): Input dimension.
        odim (int): Output dimension.
        dropout_rate (float): Dropout rate.

    r   r   r    r!   c              
      sˆ   t ƒ  ¡  tj tj d|dd¡tj ¡ tj ||dd¡tj ¡ ¡| _tj tj ||d d d d  |¡¡| _	|| _
d| _d| _dS )z'Construct an Conv2dSubsampling4 object.r   r>   rA   rC   é   N)r   r   r   r"   r7   ÚConv2dÚReLUrF   r8   r;   r   r	   r   r%   r   r   r   r   ·   s   
ü ÿ
zConv2dSubsampling4.__init__r   r&   r'   r   r   c           	      C   s’   |  d¡}|  |¡}| ¡ \}}}}|  | dd¡ ¡  |||| ¡¡}|  ||¡\}}|||dd…dd…ddd…f dd…dd…ddd…f fS )aº  Subsample x.

        Args:
            x (torch.Tensor): Input tensor (#batch, time, idim).
            x_mask (torch.Tensor): Input mask (#batch, 1, time).

        Returns:
            torch.Tensor: Subsampled tensor (#batch, time', odim),
                where time' = time // 4.
            torch.Tensor: Subsampled mask (#batch, 1, time'),
                where time' = time // 4.
            torch.Tensor: positional encoding

        r   rA   N)Ú	unsqueezerF   r   r;   rG   Ú
contiguousÚviewr   ©	r
   r&   r'   r   ÚbÚcÚtÚfr+   r   r   r   r,   Ê   s   

$:zConv2dSubsampling4.forwardr.   r/   r   r   r   r   rI   ­   ó    	ÿüþýüûrI   c                       r   )ÚConv2dSubsampling6zöConvolutional 2D subsampling (to 1/6 length).
    Args:
        idim (int): Input dimension.
        odim (int): Output dimension.
        dropout_rate (float): Dropout rate.
        pos_enc (torch.nn.Module): Custom position encoding layer.
    r   r   r    r!   c              
      s€   t ƒ  ¡  tj tj d|dd¡tj ¡ tj ||dd¡tj ¡ ¡| _tj ||d d d d  |¡| _	|| _
d| _d| _dS )z'Construct an Conv2dSubsampling6 object.r   r>   rA   é   rJ   é
   N©r   r   r   r"   r7   rK   rL   rF   r8   Úlinearr   r	   r   r%   r   r   r   r   ï   s   
üÿ
zConv2dSubsampling6.__init__r   r&   r'   r   r   c           	      C   s’   |  d¡}|  |¡}| ¡ \}}}}|  | dd¡ ¡  |||| ¡¡}|  ||¡\}}|||dd…dd…ddd…f dd…dd…ddd…f fS )a¸  Subsample x.
        Args:
            x (torch.Tensor): Input tensor (#batch, time, idim).
            x_mask (torch.Tensor): Input mask (#batch, 1, time).

        Returns:
            torch.Tensor: Subsampled tensor (#batch, time', odim),
                where time' = time // 6.
            torch.Tensor: Subsampled mask (#batch, 1, time'),
                where time' = time // 6.
            torch.Tensor: positional encoding
        r   rA   NrC   r>   ©rM   rF   r   rZ   rG   rN   rO   r   rP   r   r   r   r,      s   

$:zConv2dSubsampling6.forwardr.   r/   r   r   r   r   rV   æ   s    ÿüþýüûrV   c                       r   )ÚConv2dSubsampling8zµConvolutional 2D subsampling (to 1/8 length).

    Args:
        idim (int): Input dimension.
        odim (int): Output dimension.
        dropout_rate (float): Dropout rate.

    r   r   r    r!   c                    s    t ƒ  ¡  tj tj d|dd¡tj ¡ tj ||dd¡tj ¡ tj ||dd¡tj ¡ ¡| _tj ||d d d d d d  |¡| _	|| _
d| _d| _dS )z'Construct an Conv2dSubsampling8 object.r   r>   rA   é   é   NrY   r%   r   r   r   r   $  s   
ú ÿ
zConv2dSubsampling8.__init__r   r&   r'   r   r   c           	      C   sª   |  d¡}|  |¡}| ¡ \}}}}|  | dd¡ ¡  |||| ¡¡}|  ||¡\}}|||dd…dd…ddd…f dd…dd…ddd…f dd…dd…ddd…f fS )a¹  Subsample x.

        Args:
            x (torch.Tensor): Input tensor (#batch, time, idim).
            x_mask (torch.Tensor): Input mask (#batch, 1, time).

        Returns:
            torch.Tensor: Subsampled tensor (#batch, time', odim),
                where time' = time // 8.
            torch.Tensor: Subsampled mask (#batch, 1, time'),
                where time' = time // 8.
            torch.Tensor: positional encoding
        r   rA   Nr[   rP   r   r   r   r,   7  s   

$RzConv2dSubsampling8.forwardr.   r/   r   r   r   r   r\     rU   r\   c                       r   )ÚLegacyLinearNoSubsamplingr4   r   r   r    r!   c                    sX   t ƒ  ¡  tj tj ||¡tjj|dd�tj |¡tj ¡ ¡| _	|| _
d| _d| _dS r5   )r   r   r   r"   r7   r8   r9   r:   rL   r;   r   r   r	   r%   r   r   r   r   \  s   

ü
z"LegacyLinearNoSubsampling.__init__r   r&   r'   r   r   c                 C   r(   r)   r<   r*   r   r   r   r,   j  r-   z!LegacyLinearNoSubsampling.forwardr.   r/   r   r   r   r   r_   R  s    	ÿüþýüûr_   )r0   Útypingr   r   r   r"   r2   r   r   r3   r=   rI   rV   r\   r_   r   r   r   r   Ú<module>   s   "/9948