Ë
    éÓ^j3/  ã                   óÚ  — d dl mZmZmZ d dlZd dlmZ d dlmZ ddl	m
Z
 ddlmZ ddlmZ g d	¢Zej                   j"                  e
dd
ej$                  deej$                     fd„«       «       Zej                   j"                  e
	 	 ddedej$                  deej$                     dedeej$                     f
d„«       «       Z G d„ dej.                  «      Z G d„ dej.                  «      Zy)é    )ÚFinalÚOptionalÚTypeN)Únn)Ú
functionalé   )Úregister_notrace_function)Úuse_fused_attn)Úapply_rot_embed_cat)Ú	AttentionÚAttentionRopeÚmaybe_add_maskÚresolve_self_attn_maskÚscoresÚ	attn_maskc                 ó   — |€| S | |z   S ©N© )r   r   s     ú`/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/timm/layers/attention.pyr   r      s   € ð Ð&ˆ6Ð>¨F°YÑ,>Ð>ó    Úseq_lenÚattnÚ	is_causalÚreturnc                 ó.  — |r.|j                  | | ft        d«      «      j                  d«      }|S |€d }|S |j                  t        j
                  k(  r?t	        j                  ||j                  ¬«      }|j                  | t        d«      «       |S |}|S )Nz-infr   )Údtype)Únew_fullÚfloatÚtriu_r   ÚtorchÚboolÚ
zeros_likeÚmasked_fill_)r   r   r   r   Ú	attn_biass        r   r   r      s›   € ñ Ø—M‘M 7¨GÐ"4´e¸F³mÓD×JÑJÈ1ÓMˆ	ð Ðð 
Ð	Øˆ	ð Ðð 
�‰œEŸJ™JÒ	&Ü×$Ñ$ Y°d·j±jÔAˆ	Ø×Ñ 	˜z¬5°«=Ô9ð Ðð ˆ	ØÐr   c                   ó  ‡ — e Zd ZU dZee   ed<   	 	 	 	 	 	 	 	 	 	 	 	 	 ddededee   dee   ded	ed
ededede	de	dee
ej                        ddfˆ fd„Z	 	 ddej                  deej                     dedej                  fd„Zˆ xZS )r   a£  Standard Multi-head Self Attention module with QKV projection.

    This module implements the standard multi-head attention mechanism used in transformers.
    It supports both the fused attention implementation (scaled_dot_product_attention) for
    efficiency when available, and a manual implementation otherwise. The module includes
    options for QK normalization, attention dropout, and projection dropout.
    Ú
fused_attnNÚdimÚ	num_headsÚattn_head_dimÚdim_outÚqkv_biasÚqk_normÚ
scale_normÚ	proj_biasÚgatedÚ	attn_dropÚ	proj_dropÚ
norm_layerr   c                 ó6  •— t         ‰| �  «        ||dœ}|xs |}|}|€||z  dk(  sJ d«       ‚||z  }|s|r	|€J d«       ‚|| _        || _        ||z  | _        |dz  | _        t        «       | _        t        j                  || j                  dz  fd|i|¤Ž| _
        |r	 ||fi |¤Žnt        j                  «       | _        |r	 ||fi |¤Žnt        j                  «       | _        t        j                  |
«      | _        |r || j                  fi |¤Žnt        j                  «       | _        |	r#t        j                  || j                  fd|i|¤Žnd| _        t        j                  | j                  |fd|i|¤Ž| _        t        j                  |«      | _        y)	aà  Initialize the Attention module.

        Args:
            dim: Input dimension of the token embeddings.
            num_heads: Number of attention heads.
            attn_head_dim: Dimension of each attention head. If None, computed as dim // num_heads.
            dim_out: Output dimension. If None, same as dim.
            qkv_bias: Whether to use bias in the query, key, value projections.
            qk_norm: Whether to apply normalization to query and key vectors.
            scale_norm: Whether to apply normalization to attention output before projection.
            proj_bias: Whether to use bias in the output projection.
            gated: Apply an elementwise sigmoid gate to the attention output (anti attention-sink, GenLIP-style).
            attn_drop: Dropout rate applied to the attention weights.
            proj_drop: Dropout rate applied after the output projection.
            norm_layer: Normalization layer constructor for QK normalization if enabled.
        ©Údevicer   Nr   ú$dim should be divisible by num_headsú<norm_layer must be provided if qk_norm or scale_norm is Trueç      à¿é   Úbias)ÚsuperÚ__init__r(   Úhead_dimÚattn_dimÚscaler
   r&   r   ÚLinearÚqkvÚIdentityÚq_normÚk_normÚDropoutr0   ÚnormÚgateÚprojr1   )Úselfr'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r5   r   Úddr=   Ú	__class__s                    €r   r<   zAttention.__init__5   sw  ø€ ôB 	‰ÑÔØ¨Ñ/ˆØ’.˜SˆØ ˆØÐØ˜‘? aÒ'ÐOÐ)OÓOÐ'Ø˜iÑ'ˆHÙ‘jØÐ)ÐiÐ+iÓiÐ)à"ˆŒØ ˆŒØ! HÑ,ˆŒØ Ñ%ˆŒ
Ü(Ó*ˆŒä—9‘9˜S $§-¡-°!Ñ"3ÑI¸(ÐIÀbÑIˆŒÙ4;‘j Ñ0¨RÒ0ÄÇÁÃˆŒÙ4;‘j Ñ0¨RÒ0ÄÇÁÃˆŒÜŸ™ IÓ.ˆŒÙ7A‘J˜tŸ}™}Ñ3°Ò3ÄrÇ{Á{Ã}ˆŒ	ÙJO”B—I‘I˜c 4§=¡=ÑF°xÐFÀ2ÒFÐUYˆŒ	Ü—I‘I˜dŸm™m¨WÑK¸9ÐKÈÑKˆŒ	ÜŸ™ IÓ.ˆ�r   Úxr   r   c                 ó²  — |j                   \  }}}| j                  �| j                  |«      j                  «       nd }| j                  |«      j	                  ||d| j
                  | j                  «      j                  ddddd«      }|j                  d«      \  }	}
}| j                  |	«      | j                  |
«      }
}	| j                  r>t        j                  |	|
||| j                  r| j                  j                   nd|¬«      }nf|	| j"                  z  }	|	|
j%                  dd	«      z  }t'        ||||«      }t)        ||«      }|j+                  d	¬
«      }| j                  |«      }||z  }|j%                  dd«      j	                  ||| j,                  «      }| j/                  |«      }|�||z  }| j1                  |«      }| j3                  |«      }|S )Nr9   é   r   r   é   ç        ©r   Ú	dropout_pr   éþÿÿÿéÿÿÿÿ©r'   )ÚshaperG   ÚsigmoidrA   Úreshaper(   r=   ÚpermuteÚunbindrC   rD   r&   ÚFÚscaled_dot_product_attentionÚtrainingr0   Úpr?   Ú	transposer   r   Úsoftmaxr>   rF   rH   r1   )rI   rL   r   r   ÚBÚNÚCrG   rA   ÚqÚkÚvr   r$   s                 r   ÚforwardzAttention.forwardo   sŸ  € ð —'‘'‰ˆˆ1ˆaØ)-¯©Ð)>ˆt�y‰y˜‹|×#Ñ#Ô%ÀDˆØ�h‰h�q‹k×!Ñ! ! Q¨¨4¯>©>¸4¿=¹=ÓI×QÑQÐRSÐUVÐXYÐ[\Ð^_Ó`ˆØ—*‘*˜Q“-‰ˆˆ1ˆaØ�{‰{˜1‹~˜tŸ{™{¨1›~ˆ1ˆà�?Š?Ü×.Ñ.Ø�1�aØ#Ø.2¯mªm˜$Ÿ.™.×*Ò*ÀØ#ô	‰Að �D—J‘J‘ˆAØ�q—{‘{ 2 rÓ*Ñ*ˆDÜ.¨q°$¸	À9ÓMˆIÜ! $¨	Ó2ˆDØ—<‘< B�<Ó'ˆDØ—>‘> $Ó'ˆDØ�q‘ˆAà�K‰K˜˜1Ó×%Ñ% a¨¨D¯M©MÓ:ˆØ�I‰I�a‹LˆØÐØ�D‘ˆAØ�I‰I�a‹LˆØ�N‰N˜1ÓˆØˆr   )é   NNFFFTFrP   rP   NNN©NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   Ú__annotations__Úintr   r   r   r   ÚModuler<   r    ÚTensorrg   Ú__classcell__©rK   s   @r   r   r   +   s  ø… ñð �d‘Óð
 Ø+/Ø%)Ø"Ø!Ø$Ø"ØØ!Ø!Ø48ØØñ8/àð8/ð ð8/ð $ C™=ð	8/ð
 ˜c‘]ð8/ð ð8/ð ð8/ð ð8/ð ð8/ð ð8/ð ð8/ð ð8/ð !  b§i¡i¡Ñ1ð8/ð  
õ!8/ðz 15Ø#ñ	"à�|‰|ð"ð   §¡Ñ-ð"ð ð	"ð
 
�‰÷"r   r   c                   ó2  ‡ — e Zd ZU dZej
                  j                  e   ed<   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dde	de	de
e	   dedede	d	ed
ede
e	   deej                     dededededefˆ fd„Z	 	 	 dde
ej                      de
ej                      defd„Zˆ xZS )r   zÇ A Self Attention module with ROPE support.

    Includes options for:
     * QK normalization option
     * Attention output (scale) normalization
     * Fused or unfused QKV projection support
    r&   r'   r(   r*   r+   Ú	qkv_fusedÚnum_prefix_tokensr0   r1   r)   r2   r,   r-   r.   Úrotate_halfr/   c                 ó€  •— t         ‰| �  «        ||dœ}|xs |}|	}|€||z  dk(  sJ d«       ‚||z  }|s|r	|
€J d«       ‚|| _        || _        ||z  | _        |dz  | _        || _        t        «       | _        || _	        |rAt        j                  || j                  dz  fd|i|¤Ž| _        dx| _        x| _        | _        nd| _        t        j                  || j                  fd|i|¤Ž| _        t        j                  || j                  fd|i|¤Ž| _        t        j                  || j                  fd|i|¤Ž| _        |r	 |
|fi |¤Žnt        j                   «       | _        |r	 |
|fi |¤Žnt        j                   «       | _        t        j&                  |«      | _        |r |
| j                  fi |¤Žnt        j                   «       | _        |r#t        j                  || j                  fd|i|¤Žnd| _        t        j                  | j                  |fd|i|¤Ž| _        t        j&                  |«      | _        y)	a»  Initialize the Attention module.

        Args:
            dim: Input dimension of the token embeddings
            num_heads: Number of attention heads
            dim_out: Output dimension. If None, same as dim.
            qkv_bias: Whether to add a bias term to the query, key, and value projections
            qkv_fused: Whether to use fused QKV projection (single linear) or separate projections
            num_prefix_tokens: Number of reg/cls tokens at the beginning of the sequence that
                should not have position embeddings applied
            attn_drop: Dropout rate for attention weights
            proj_drop: Dropout rate for the output projection
            attn_head_dim: Dimension of each attention head. If None, computed as dim // num_heads.
            norm_layer: Normalization layer constructor to use for QK and scale normalization
            qk_norm: Enable normalization of query (Q) and key (K) vectors with norm_layer
            scale_norm: Enable normalization (scaling) of attention output with norm_layer
            proj_bias: Whether to use bias in the output projection
            rotate_half: Use 'half' ROPE layout instead of default 'interleaved'
        r4   Nr   r6   r7   r8   r9   r:   )r;   r<   r(   r=   r>   r?   rv   r
   r&   rw   r   r@   rA   Úq_projÚk_projÚv_projrB   rC   rD   rE   r0   rF   rG   rH   r1   )rI   r'   r(   r*   r+   ru   rv   r0   r1   r)   r2   r,   r-   r.   rw   r/   r5   r   rJ   r=   rK   s                       €r   r<   zAttentionRope.__init__ž   s  ø€ ôN 	‰ÑÔØ¨Ñ/ˆØ’.˜SˆØ ˆØÐØ˜‘? aÒ'ÐOÐ)OÓOÐ'Ø˜iÑ'ˆHÙ™ØÐ)ÐiÐ+iÓiÐ)à"ˆŒØ ˆŒØ  9Ñ,ˆŒØ Ñ%ˆŒ
Ø!2ˆÔÜ(Ó*ˆŒØ&ˆÔáÜ—y‘y  d§m¡m°aÑ&7ÑM¸hÐMÈ"ÑMˆDŒHØ6:Ð:ˆDŒKÐ:˜$œ+¨­àˆDŒHÜŸ)™) C¨¯©ÑL¸XÐLÈÑLˆDŒKÜŸ)™) C¨¯©ÑL¸XÐLÈÑLˆDŒKÜŸ)™) C¨¯©ÑL¸XÐLÈÑLˆDŒKá4;‘j Ñ0¨RÒ0ÄÇÁÃˆŒÙ4;‘j Ñ0¨RÒ0ÄÇÁÃˆŒÜŸ™ IÓ.ˆŒÙ7A‘J˜tŸ}™}Ñ3°Ò3ÄrÇ{Á{Ã}ˆŒ	ÙJO”B—I‘I˜c 4§=¡=ÑF°xÐFÀ2ÒFÐUYˆŒ	Ü—I‘I˜dŸm™m¨WÑK¸9ÐKÈÑKˆŒ	ÜŸ™ IÓ.ˆ�r   Úroper   r   c                 óø  — |j                   \  }}}| j                  �| j                  |«      j                  «       nd}| j                  �c| j                  |«      }	|	j	                  ||d| j
                  | j                  «      j                  ddddd«      }	|	j                  d«      \  }
}}nÕ| j                  |«      j	                  ||| j
                  | j                  «      j                  dd«      }
| j                  |«      j	                  ||| j
                  | j                  «      j                  dd«      }| j                  |«      j	                  ||| j
                  | j                  «      j                  dd«      }| j                  |
«      | j                  |«      }}
|�½| j                  }t!        | dd«      }t#        j$                  |
dd…dd…d|…dd…f   t'        |
dd…dd…|d…dd…f   ||¬	«      gd¬
«      j)                  |«      }
t#        j$                  |dd…dd…d|…dd…f   t'        |dd…dd…|d…dd…f   ||¬	«      gd¬
«      j)                  |«      }| j*                  r>t-        j.                  |
|||| j0                  r| j2                  j4                  nd|¬«      }nf|
| j6                  z  }
|
|j                  dd«      z  }t9        ||||«      }t;        ||«      }|j=                  d¬
«      }| j3                  |«      }||z  }|j                  dd«      j	                  ||| j>                  «      }| jA                  |«      }|�||z  }| jC                  |«      }| jE                  |«      }|S )aÏ  Forward pass for the attention module.

        Args:
            x: Input tensor of shape (batch_size, sequence_length, embedding_dim)
            rope: Rotary position embeddings tensor for position-aware attention
            attn_mask: Optional attention mask to apply during attention computation
            is_causal: If True, use causal (autoregressive) masking

        Returns:
            Tensor of shape (batch_size, sequence_length, dim_out)
        Nr9   rN   r   r   rO   rw   F)ÚhalfrU   rP   rQ   rS   rT   )#rV   rG   rW   rA   rX   r(   r=   rY   rZ   ry   r_   rz   r{   rC   rD   rv   Úgetattrr    Úcatr   Útype_asr&   r[   r\   r]   r0   r^   r?   r   r   r`   r>   rF   rH   r1   )rI   rL   r|   r   r   ra   rb   rc   rG   rA   rd   re   rf   Únptr~   r   r$   s                    r   rg   zAttentionRope.forwardè   s  € ð$ —'‘'‰ˆˆ1ˆaØ)-¯©Ð)>ˆt�y‰y˜‹|×#Ñ#Ô%ÀDˆà�8‰8ÐØ—(‘(˜1“+ˆCØ—+‘+˜a  A t§~¡~°t·}±}ÓE×MÑMÈaÐQRÐTUÐWXÐZ[Ó\ˆCØ—j‘j “m‰GˆAˆq‘!à—‘˜A“×&Ñ& q¨!¨T¯^©^¸T¿]¹]ÓK×UÑUÐVWÐYZÓ[ˆAØ—‘˜A“×&Ñ& q¨!¨T¯^©^¸T¿]¹]ÓK×UÑUÐVWÐYZÓ[ˆAØ—‘˜A“×&Ñ& q¨!¨T¯^©^¸T¿]¹]ÓK×UÑUÐVWÐYZÓ[ˆAà�{‰{˜1‹~˜tŸ{™{¨1›~ˆ1ˆàÐØ×(Ñ(ˆCÜ˜4 °Ó6ˆDÜ—	‘	˜1šQ¢ 4 C 4ª˜]Ñ+Ô-@ÀÂ1ÂaÈÉÊqÀ=ÑAQÐSWÐ^bÔ-cÐdÐjkÔl×tÑtÐuvÓwˆAÜ—	‘	˜1šQ¢ 4 C 4ª˜]Ñ+Ô-@ÀÂ1ÂaÈÉÊqÀ=ÑAQÐSWÐ^bÔ-cÐdÐjkÔl×tÑtÐuvÓwˆAà�?Š?Ü×.Ñ.Ø�1�aØ#Ø.2¯mªm˜$Ÿ.™.×*Ò*ÀØ#ô	‰Að �D—J‘J‘ˆAØ�q—{‘{ 2 rÓ*Ñ*ˆDÜ.¨q°$¸	À9ÓMˆIÜ! $¨	Ó2ˆDØ—<‘< B�<Ó'ˆDà—>‘> $Ó'ˆDØ�q‘ˆAà�K‰K˜˜1Ó×%Ñ% a¨¨D¯M©MÓ:ˆØ�I‰I�a‹LˆØÐØ�D‘ˆAØ�I‰I�a‹LˆØ�N‰N˜1ÓˆØˆr   )rh   NTTr   rP   rP   NNFFTFFNN)NNF)rj   rk   rl   rm   r    Újitr   r!   rn   ro   r   r   r   r   rp   r<   rq   rg   rr   rs   s   @r   r   r   ”   sI  ø… ñð —	‘	—‘ Ñ%Ó%ð
 Ø%)Ø!Ø"Ø%&Ø!Ø!Ø+/Ø*.Ø!Ø$Ø"Ø %ØØØñ%H/àðH/ð ðH/ð ˜c‘]ð	H/ð
 ðH/ð ðH/ð  #ðH/ð ðH/ð ðH/ð $ C™=ðH/ð ˜RŸY™Y™ðH/ð ðH/ð ðH/ð ðH/ð ðH/ð  õ!H/ðZ ,0Ø04Ø#ñ=ð ˜5Ÿ<™<Ñ(ð=ð   §¡Ñ-ð	=ð
 ÷=r   r   r   ri   )Útypingr   r   r   r    r   Útorch.nnr   r[   Ú_fxr	   Úconfigr
   Úpos_embed_sincosr   Ú__all__ÚfxÚwraprq   r   ro   r!   r   rp   r   r   r   r   r   ú<module>rŒ      sù   ðß (Ñ (ã Ý Ý $å *Ý "Ý 1ò U€ð ‡�‡�Øñ?˜5Ÿ<™<ð ?°H¸U¿\¹\Ñ4Jò ?ó ó ð?ð ‡�‡�Øð -1Øñ	Øðà�l‰lðð ˜EŸL™LÑ)ðð ð	ð
 ˆe�l‰lÑòó ó ðô(f�—	‘	ô fôRQ�B—I‘Iõ Qr   