U
    ,:%e\*  ã                   @   sJ   d Z ddlZddlmZmZ ddlZddlmZmZ G dd„ dejƒZ	dS )a<  
The MIT License (MIT)

Copyright (c) Microsoft Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
é    N)ÚOptionalÚTuple)ÚnnÚTensorc                
       sŠ   e Zd ZdZdeeeeeeeedœ‡ fdd	„Zeeed
œdd„Z	deedœdd„Z
deee ee ee eeee f dœdd„Z‡  ZS )ÚWavLMSelfAttentiona¦  Multi-headed self-attention for WavLM model :cite:`chen2022wavlm`.
    Wraps around ``torch.nn.MultiheadAttention``, creating relaive position embeddings and passing them to multi-headed
    attention as a mask.
    Source: https://github.com/microsoft/unilm/blob/2d8302f09c99bca2b82e6e868d81d4281cceebc8/wavlm/modules.py#L303-L763

    Args:
        embed_dim (int): Total dimension of the model.
        num_heads (int): The number of heads.
        dropout (float, optional): Dropout probability on attn_output_weights. (Default: to ``0.0``)
        bias (bool, optional): If ``True``, add bias to input / output projection layers. (Default: ``True``)
        has_relative_attention_bias (bool, optional): If ``True``, apply relative position embedding.
            Necessary in the first encoder layer, but not in the subsequent ones. (Default: ``False``)
        num_buckets (int, optional): Number of buckets for relative position embedding. (Default: ``32``)
        max_distance (int, optional): Naximum distance for relative position embedding. (Default: ``128``)
        gru_rel_pos (bool, optional): If ``True``, apply gated relative position embedding. (Default: ``False``)
    ç        TFé    é€   )Ú	embed_dimÚ	num_headsÚdropoutÚbiasÚhas_relative_attention_biasÚnum_bucketsÚmax_distanceÚgru_rel_posc	           	         s¾   t ƒ  ¡  || _|| _|| _|| _|| _|r<t ||¡| _	nd | _	|| | _
| j
| | jksdtdƒ‚|| _tj||||dd| _|| _| jr´t | j
d¡| _t t d|dd¡¡| _d| _d S )Nz(embed_dim must be divisible by num_headsT)r   r   Zbatch_firsté   é   )ÚsuperÚ__init__r
   r   r   r   r   r   Z	EmbeddingÚrel_attn_embedÚhead_dimÚAssertionErrorr   ZMultiheadAttentionÚ	attentionr   ZLinearÚgru_rel_pos_linearÚ	ParameterÚtorchZonesÚgru_rel_pos_constZhas_position_bias)	Úselfr
   r   r   r   r   r   r   r   ©Ú	__class__© úi/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torchaudio/models/wav2vec2/wavlm_attention.pyr   2   s$    

zWavLMSelfAttention.__init__)Úquery_lengthÚ
key_lengthÚreturnc                 C   s|   t j|t jddd…df }t j|t jdddd…f }|| }| j|dd}| | jjj¡}|  |¡}| dddg¡}|S )a€  Compute relative position embeddings for WavLM model.
        Args:
            query_length (int): Query position can take values between 0 and ``query_length - 1``.
            key_length (int): Key position can take values between 0 and ``key_length - 1``.
        Returns:
            Tensor of shape `(num_heads, query_length, key_length)`, relative positions embeddings
        ©ÚdtypeNT)Úbidirectionalé   r   r   )	r   ZarangeÚlongÚ_relative_positions_bucketÚtor   ÚweightZdeviceÚpermute)r   r#   r$   Zcontext_positionZmemory_positionZrelative_positionZrelative_position_bucketÚvaluesr!   r!   r"   Úcompute_biasU   s    
zWavLMSelfAttention.compute_bias)Úrelative_positionsr(   c           	      C   sÒ   | j }| j}tj|tjd}|rL|d }||dk tj¡| 7 }t |¡}nt |t |¡¡ }|d }||k }|t | 	¡ | ¡t
 || ¡ ||   tj¡ }t |t ||d ¡¡}|t |||¡7 }|S )aÌ  Compute relative position buckets for WavLM model. Computation similar to formula (5) in WavLM
           paper :cite:`chen2022wavlm`.
        Args:
            relative_positions (Tensor): Relative offsets between query and key positions,
                of shape ``(query_length, key_length)``.
            bidirectional (bool): If ``True``, values will be filled both above and below the diagonal in the resulting
                matrix. If ``False``, the elements above the diagonal (i.e. with negative relative offsets) will be set
                to zero. (Default ``True``)
        Returns:
            Tensor of shape ``(query_length, key_length)`` filled bucketed values of with relative positions.
        r&   r)   r   r   )r   r   r   Z
zeros_liker*   r,   ÚabsÚminÚlogÚfloatÚmathZ	full_likeÚwhere)	r   r1   r(   r   r   Zrelative_bucketsZ	max_exactZis_smallZrelative_postion_if_larger!   r!   r"   r+   f   s2    ÿþÿü ÿz-WavLMSelfAttention._relative_positions_bucketN)ÚqueryÚkey_padding_maskÚattention_maskÚposition_biasr%   c              	   C   sP  |  ¡ \}}}|| jkst‚|dks(t‚| jdk	r\|dkr\|  ||¡}| d¡ |ddd¡}d}|dk	r|}| jrú| ||| j	d¡}	|	 
dddd¡}	t |  |	¡ || j	|dd¡jddd	¡jddd
\}
}|
|| j d  d }| || j	dd¡| }| || j	||f¡}|dk	rd|dk	rd| |dd|¡ d| j	dd¡}tjjj|dtjj |¡d|jd}|dk	r€|dk	r€|| }tjj || jj| jj¡}| dd¡\}}}||| j	| jf}| |¡ dd¡}| |¡ dd¡}| |¡ dd¡}| jr | jnd}tjjj|||||dd}| dd¡  |d| j	| j ¡}| j !|¡}||fS )aë  
        Args:
            query (Tensor): Input of shape ``(batch_size, src_len, embed_dim)``.
            key_padding_mask (Tensor or None, optional): Mask to exclude keys that are pads, of shape
                `(batch, src_len)`, where padding elements are indicated by 1s. (Default: ``None``)
            attn_mask: Needs to be ``None``. The argument exists for compatibility with
                ``EncoderLayer``. (Default: ``None``)
            position_bias (Tensor or None, optional): Position bias of shape
                ``(batch_size * num_heads, src_len, src_len)``. When used inside WavLM model encoder, will be
                generated in the first layer and then passed from each encoder layer to the next one.
                (Default: ``None``)
        Returns:
            attn_output (Tensor): Attention output of shape ``(batch_size, src_len, embed_dim)``.
            position_bias (Tensor or None): Position bias of shape ``(batch_size * num_heads, src_len, src_len)``.
        Nr   r   éÿÿÿÿr)   é   é   F)Zkeepdim)Údimg      ð?g       @r9   Ú )ÚmaskZ	mask_nameZ
other_typeZ
other_nameZtarget_typer   )Z	attn_maskZ	dropout_pZ	is_causal)"Úsizer
   r   r   r0   Z	unsqueezeÚrepeatr   Úviewr   r.   r   Zsigmoidr   ÚsumÚchunkr   Úexpandr   Z
functionalZ_canonical_maskZ_none_or_dtyper'   Zlinearr   Zin_proj_weightZin_proj_biasr   Z	transposeZtrainingr   Zscaled_dot_product_attentionZreshapeZout_proj)r   r8   r9   r:   r;   ZbszZseq_lenr
   Zattn_mask_rel_posZquery_layerZgate_aZgate_bZgate_a_1Zquery_projectedÚkeyÚvalueÚshaper   Zattn_outputr!   r!   r"   Úforward   sd    
"ÿ þ
ûúzWavLMSelfAttention.forward)r   TFr   r	   T)T)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úintr5   Úboolr   r   r0   r+   r   r   rK   Ú__classcell__r!   r!   r   r"   r       s:         ÷÷#*   ûúr   )
rO   r6   Útypingr   r   r   r   r   ÚModuler   r!   r!   r!   r"   Ú<module>   s
   