Ë
    óÿæiT'  ã                   óR  — d dl mZmZ d dlZdgZdej
                  dej
                  fd„Z G d„ dej                  j                  «      Z	 G d	„ d
ej                  j                  «      Z
 G d„ dej                  j                  «      Z G d„ dej                  j                  «      Zy)é    )ÚOptionalÚTupleNÚ	ConformerÚlengthsÚreturnc                 ó  — | j                   d   }t        t        j                  | «      j	                  «       «      }t        j
                  || j                  | j                  ¬«      j                  ||«      | j                  d«      k\  }|S )Nr   )ÚdeviceÚdtypeé   )
ÚshapeÚintÚtorchÚmaxÚitemÚaranger	   r
   ÚexpandÚ	unsqueeze)r   Ú
batch_sizeÚ
max_lengthÚpadding_masks       úp/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchaudio/models/conformer.pyÚ_lengths_to_padding_maskr   	   sq   € Ø—‘˜qÑ!€JÜ”U—Y‘Y˜wÓ'×,Ñ,Ó.Ó/€JÜ—<‘< 
°7·>±>ÈÏÉÔW×^Ñ^Ø�Jóà	×	Ñ	˜1Ó	ñ€Lð Ðó    c                   ó€   ‡ — e Zd ZdZ	 	 	 ddededededededd	fˆ fd
„Zdej                  dej                  fd„Z
ˆ xZS )Ú_ConvolutionModulea  Conformer convolution module.

    Args:
        input_dim (int): input dimension.
        num_channels (int): number of depthwise convolution layer input channels.
        depthwise_kernel_size (int): kernel size of depthwise convolution layer.
        dropout (float, optional): dropout probability. (Default: 0.0)
        bias (bool, optional): indicates whether to add bias term to each convolution layer. (Default: ``False``)
        use_group_norm (bool, optional): use GroupNorm rather than BatchNorm. (Default: ``False``)
    Ú	input_dimÚnum_channelsÚdepthwise_kernel_sizeÚdropoutÚbiasÚuse_group_normr   Nc                 óþ  •— t         ‰| �  «        |dz
  dz  dk7  rt        d«      ‚t        j                  j                  |«      | _        t        j                  j                  t        j                  j                  |d|z  ddd|¬«      t        j                  j                  d¬«      t        j                  j                  |||d|dz
  dz  ||¬«      |r!t        j                  j                  d|¬«      nt        j                  j                  |«      t        j                  j                  «       t        j                  j                  ||ddd|¬	«      t        j                  j                  |«      «      | _        y )
Nr   é   r   z<depthwise_kernel_size must be odd to achieve 'SAME' padding.)ÚstrideÚpaddingr    )Údim)r$   r%   Úgroupsr    )Ú
num_groupsr   )Úkernel_sizer$   r%   r    )ÚsuperÚ__init__Ú
ValueErrorr   ÚnnÚ	LayerNormÚ
layer_normÚ
SequentialÚConv1dÚGLUÚ	GroupNormÚBatchNorm1dÚSiLUÚDropoutÚ
sequential)Úselfr   r   r   r   r    r!   Ú	__class__s          €r   r+   z_ConvolutionModule.__init__   sF  ø€ ô 	‰ÑÔØ! AÑ%¨Ñ*¨aÒ/ÜÐ[Ó\Ð\ÜŸ(™(×,Ñ,¨YÓ7ˆŒÜŸ(™(×-Ñ-Ü�H‰H�O‰OØØ�LÑ ØØØØð ó ô �H‰H�L‰L˜QˆLÓÜ�H‰H�O‰OØØØ%ØØ.°Ñ2°qÑ8Ø#Øð ó ñ ô �H‰H×Ñ¨!¸,ÐÔGä—‘×%Ñ% lÓ3Ü�H‰H�M‰M‹OÜ�H‰H�O‰OØØØØØØð ó ô �H‰H×Ñ˜WÓ%ó? 
ˆ�r   Úinputc                 óŽ   — | j                  |«      }|j                  dd«      }| j                  |«      }|j                  dd«      S )z�
        Args:
            input (torch.Tensor): with shape `(B, T, D)`.

        Returns:
            torch.Tensor: output, with shape `(B, T, D)`.
        r   r#   )r/   Ú	transposer7   )r8   r:   Úxs      r   Úforwardz_ConvolutionModule.forwardM   sA   € ð �O‰O˜EÓ"ˆØ�K‰K˜˜1ÓˆØ�O‰O˜AÓˆØ�{‰{˜1˜aÓ Ð r   ©ç        FF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚfloatÚboolr+   r   ÚTensorr>   Ú__classcell__©r9   s   @r   r   r      sx   ø„ ñ	ð  ØØ$ñ-
àð-
ð ð-
ð  #ð	-
ð
 ð-
ð ð-
ð ð-
ð 
õ-
ð^!˜UŸ\™\ð !¨e¯l©l÷ !r   r   c            	       ón   ‡ — e Zd ZdZd
dedededdfˆ fd„Zdej                  dej                  fd	„Z	ˆ xZ
S )Ú_FeedForwardModulezÍPositionwise feed forward layer.

    Args:
        input_dim (int): input dimension.
        hidden_dim (int): hidden dimension.
        dropout (float, optional): dropout probability. (Default: 0.0)
    r   Ú
hidden_dimr   r   Nc                 óÚ  •— t         ‰| �  «        t        j                  j	                  t        j                  j                  |«      t        j                  j                  ||d¬«      t        j                  j                  «       t        j                  j                  |«      t        j                  j                  ||d¬«      t        j                  j                  |«      «      | _	        y )NT)r    )
r*   r+   r   r-   r0   r.   ÚLinearr5   r6   r7   )r8   r   rL   r   r9   s       €r   r+   z_FeedForwardModule.__init__d   s“   ø€ Ü‰ÑÔÜŸ(™(×-Ñ-Ü�H‰H×Ñ˜yÓ)Ü�H‰H�O‰O˜I z¸ˆOÓ=Ü�H‰H�M‰M‹OÜ�H‰H×Ñ˜WÓ%Ü�H‰H�O‰O˜J¨	¸ˆOÓ=Ü�H‰H×Ñ˜WÓ%ó
ˆ�r   r:   c                 ó$   — | j                  |«      S )z—
        Args:
            input (torch.Tensor): with shape `(*, D)`.

        Returns:
            torch.Tensor: output, with shape `(*, D)`.
        )r7   )r8   r:   s     r   r>   z_FeedForwardModule.forwardo   s   € ð �‰˜uÓ%Ð%r   )r@   )rA   rB   rC   rD   r   rE   r+   r   rG   r>   rH   rI   s   @r   rK   rK   [   sE   ø„ ññ	
 #ð 	
°3ð 	
Àð 	
ÐQUõ 	
ð&˜UŸ\™\ð &¨e¯l©l÷ &r   rK   c                   óÚ   ‡ — e Zd ZdZ	 	 	 ddededededededed	d
fˆ fd„Zdej                  d	ej                  fd„Z
dej                  deej                     d	ej                  fd„Zˆ xZS )ÚConformerLayera¢  Conformer layer that constitutes Conformer.

    Args:
        input_dim (int): input dimension.
        ffn_dim (int): hidden layer dimension of feedforward network.
        num_attention_heads (int): number of attention heads.
        depthwise_conv_kernel_size (int): kernel size of depthwise convolution layer.
        dropout (float, optional): dropout probability. (Default: 0.0)
        use_group_norm (bool, optional): use ``GroupNorm`` rather than ``BatchNorm1d``
            in the convolution module. (Default: ``False``)
        convolution_first (bool, optional): apply the convolution module ahead of
            the attention module. (Default: ``False``)
    r   Úffn_dimÚnum_attention_headsÚdepthwise_conv_kernel_sizer   r!   Úconvolution_firstr   Nc                 óÎ  •— t         ‰| �  «        t        |||¬«      | _        t        j
                  j                  |«      | _        t        j
                  j                  |||¬«      | _	        t        j
                  j                  |«      | _        t        ||||d|¬«      | _        t        |||¬«      | _        t        j
                  j                  |«      | _        || _        y )N)r   T)r   r   r   r   r    r!   )r*   r+   rK   Úffn1r   r-   r.   Úself_attn_layer_normÚMultiheadAttentionÚ	self_attnr6   Úself_attn_dropoutr   Úconv_moduleÚffn2Úfinal_layer_normrU   )	r8   r   rR   rS   rT   r   r!   rU   r9   s	           €r   r+   zConformerLayer.__init__‰   s½   ø€ ô 	‰ÑÔä& y°'À7ÔKˆŒ	ä$)§H¡H×$6Ñ$6°yÓ$AˆÔ!ÜŸ™×4Ñ4°YÐ@SÐ]dÐ4ÓeˆŒÜ!&§¡×!1Ñ!1°'Ó!:ˆÔä-ØØ"Ø"<ØØØ)ô
ˆÔô ' y°'À7ÔKˆŒ	Ü %§¡× 2Ñ 2°9Ó =ˆÔØ!2ˆÕr   r:   c                 ó~   — |}|j                  dd«      }| j                  |«      }|j                  dd«      }||z   }|S )Nr   r   )r<   r\   )r8   r:   Úresiduals      r   Ú_apply_convolutionz!ConformerLayer._apply_convolution¨   sF   € ØˆØ—‘  1Ó%ˆØ× Ñ  Ó'ˆØ—‘  1Ó%ˆØ˜5Ñ ˆØˆr   Úkey_padding_maskc                 óŒ  — |}| j                  |«      }|dz  |z   }| j                  r| j                  |«      }|}| j                  |«      }| j	                  ||||d¬«      \  }}| j                  |«      }||z   }| j                  s| j                  |«      }|}| j                  |«      }|dz  |z   }| j                  |«      }|S )a
  
        Args:
            input (torch.Tensor): input, with shape `(T, B, D)`.
            key_padding_mask (torch.Tensor or None): key padding mask to use in self attention layer.

        Returns:
            torch.Tensor: output, with shape `(T, B, D)`.
        g      à?F)ÚqueryÚkeyÚvaluerb   Úneed_weights)rW   rU   ra   rX   rZ   r[   r]   r^   )r8   r:   rb   r`   r=   Ú_s         r   r>   zConformerLayer.forward°   sâ   € ð ˆØ�I‰I�eÓˆØ�‰G�hÑˆà×!Ò!Ø×'Ñ'¨Ó*ˆAàˆØ×%Ñ% aÓ(ˆØ�~‰~ØØØØ-Øð ó 
‰ˆˆ1ð ×"Ñ" 1Ó%ˆØ�‰Lˆà×%Ò%Ø×'Ñ'¨Ó*ˆAàˆØ�I‰I�a‹LˆØ�‰G�hÑˆà×!Ñ! !Ó$ˆØˆr   r?   )rA   rB   rC   rD   r   rE   rF   r+   r   rG   ra   r   r>   rH   rI   s   @r   rQ   rQ   z   s¯   ø„ ñð( Ø$Ø"'ñ3àð3ð ð3ð !ð	3ð
 %(ð3ð ð3ð ð3ð  ð3ð 
õ3ð>¨¯©ð ¸¿¹ó ð$˜UŸ\™\ð $¸XÀeÇlÁlÑ=Sð $ÐX]×XdÑXd÷ $r   rQ   c                   óº   ‡ — e Zd ZdZ	 	 	 ddededededededed	efˆ fd
„Zdej                  dej                  de
ej                  ej                  f   fd„Zˆ xZS )r   a(  Conformer architecture introduced in
    *Conformer: Convolution-augmented Transformer for Speech Recognition*
    :cite:`gulati2020conformer`.

    Args:
        input_dim (int): input dimension.
        num_heads (int): number of attention heads in each Conformer layer.
        ffn_dim (int): hidden layer dimension of feedforward networks.
        num_layers (int): number of Conformer layers to instantiate.
        depthwise_conv_kernel_size (int): kernel size of each Conformer layer's depthwise convolution layer.
        dropout (float, optional): dropout probability. (Default: 0.0)
        use_group_norm (bool, optional): use ``GroupNorm`` rather than ``BatchNorm1d``
            in the convolution module. (Default: ``False``)
        convolution_first (bool, optional): apply the convolution module ahead of
            the attention module. (Default: ``False``)

    Examples:
        >>> conformer = Conformer(
        >>>     input_dim=80,
        >>>     num_heads=4,
        >>>     ffn_dim=128,
        >>>     num_layers=4,
        >>>     depthwise_conv_kernel_size=31,
        >>> )
        >>> lengths = torch.randint(1, 400, (10,))  # (batch,)
        >>> input = torch.rand(10, int(lengths.max()), input_dim)  # (batch, num_frames, input_dim)
        >>> output = conformer(input, lengths)
    r   Ú	num_headsrR   Ú
num_layersrT   r   r!   rU   c	                 óÂ   •— t         ‰
| �  «        t        j                  j	                  t        |«      D �	cg c]  }	t        |||||||¬«      ‘Œ c}	«      | _        y c c}	w )N)r   r!   rU   )r*   r+   r   r-   Ú
ModuleListÚrangerQ   Úconformer_layers)r8   r   rj   rR   rk   rT   r   r!   rU   rh   r9   s             €r   r+   zConformer.__init__õ   sl   ø€ ô 	‰ÑÔä %§¡× 3Ñ 3ô ˜zÔ*óñ +�Aô ØØØØ.Ø#Ø#1Ø&7öð +ñó!
ˆÕùòs   ¶Ar:   r   r   c                 ó˜   — t        |«      }|j                  dd«      }| j                  D ]  } |||«      }Œ |j                  dd«      |fS )aX  
        Args:
            input (torch.Tensor): with shape `(B, T, input_dim)`.
            lengths (torch.Tensor): with shape `(B,)` and i-th element representing
                number of valid frames for i-th batch element in ``input``.

        Returns:
            (torch.Tensor, torch.Tensor)
                torch.Tensor
                    output frames, with shape `(B, T, input_dim)`
                torch.Tensor
                    output lengths, with shape `(B,)` and i-th element representing
                    number of valid frames for i-th batch element in output frames.
        r   r   )r   r<   ro   )r8   r:   r   Úencoder_padding_maskr=   Úlayers         r   r>   zConformer.forward  sR   € ô  8¸Ó@Ðà�O‰O˜A˜qÓ!ˆØ×*Ô*ˆEÙ�aÐ-Ó.‰Að +à�{‰{˜1˜aÓ  'Ð)Ð)r   r?   )rA   rB   rC   rD   r   rE   rF   r+   r   rG   r   r>   rH   rI   s   @r   r   r   ×   sž   ø„ ñðH Ø$Ø"'ñ
àð
ð ð
ð ð	
ð
 ð
ð %(ð
ð ð
ð ð
ð  õ
ð8*˜UŸ\™\ð *°E·L±Lð *ÀUÈ5Ï<É<ÐY^×YeÑYeÐKeÑEf÷ *r   )Útypingr   r   r   Ú__all__rG   r   r-   ÚModuler   rK   rQ   r   © r   r   Ú<module>rw      s‡   ðß "ã ð ˆ-€ð e§l¡lð °u·|±|ó ôF!˜Ÿ™Ÿ™ô F!ôR&˜Ÿ™Ÿ™ô &ô>Z�U—X‘X—_‘_ô ZôzN*�—‘—‘õ N*r   