Ë
    S^(h> ã                   óB  — d dl mZ d dlmZ d dlmZmZmZmZm	Z	 ddl
mZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZ ddlmZmZ ddlmZmZ ddl m!Z! ddl"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0 ddl1m2Z2m3Z3  e-«       r
d dl4Z4d dl4m5Z5  e'«       rd dl6m7Z7 ddl8m9Z9  e(jt                  e;«      Z<dZ= G d„ de5j|                  «      Z? G d„ de5j|                  «      Z@ G d„ de5j|                  «      ZA G d „ d!e5j|                  «      ZB G d"„ d#e5j|                  «      ZCd$„ ZD G d%„ d&e5j|                  «      ZE G d'„ d(e5j|                  «      ZF G d)„ d*e5j|                  «      ZGd+„ ZHdUd,„ZId-e4j”                  d.eKd/e4j”                  fd0„ZL	 dVd1e5j|                  d2e4j”                  d3e4j”                  d4e4j”                  d5ee4j”                     d6eMd7eMfd8„ZN G d9„ d:e5j|                  «      ZO G d;„ d<e5j|                  «      ZP G d=„ d>e«      ZQd?ZR e$d@eR«       G dA„ dBe«      «       ZS G dC„ dDe5j|                  «      ZTdEZU e$dFeR«       G dG„ dHeQ«      «       ZV G dI„ dJee#«      ZW G dK„ dLeQe«      ZXe G dM„ dNe«      «       ZYdOZZdPZ[ e$dQe[«       G dR„ dSeSe«      «       Z\g dT¢Z]y)Wé    )Ú	dataclass)Úpartial)ÚCallableÚListÚOptionalÚTupleÚUnioné   )ÚACT2FN)ÚCacheÚDynamicCacheÚStaticCache)ÚGenerationMixin)ÚAttentionMaskConverter)ÚFlashAttentionKwargs)ÚBaseModelOutputWithPastÚCausalLMOutputWithPastÚModelOutput)ÚROPE_INIT_FUNCTIONSÚdynamic_rope_update)ÚALL_ATTENTION_FUNCTIONSÚPreTrainedModel)ÚUnpack)Ú
LossKwargsÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚcan_return_tupleÚis_torch_flex_attn_availableÚloggingÚreplace_return_docstrings)Údeprecate_kwarg)Úis_torch_availableé   )Ú	AutoModelÚAutoModelForCausalLMé   )Ú
AriaConfigÚAriaTextConfigN)Únn)Ú	BlockMask)Úmake_flex_block_causal_maskr(   c                   ó,   ‡ — e Zd Zdˆ fd„	Zd„ Zd„ Zˆ xZS )ÚAriaTextRMSNormc                 óŠ   •— t         ‰| �  «        t        j                  t	        j
                  |«      «      | _        || _        y)z>
        AriaTextRMSNorm is equivalent to T5LayerNorm
        N)ÚsuperÚ__init__r)   Ú	ParameterÚtorchÚonesÚweightÚvariance_epsilon)ÚselfÚhidden_sizeÚepsÚ	__class__s      €úd/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/aria/modeling_aria.pyr0   zAriaTextRMSNorm.__init__A   s1   ø€ ô 	‰ÑÔÜ—l‘l¤5§:¡:¨kÓ#:Ó;ˆŒØ #ˆÕó    c                 ó"  — |j                   }|j                  t        j                  «      }|j	                  d«      j                  dd¬«      }|t        j                  || j                  z   «      z  }| j                  |j                  |«      z  S )Nr#   éÿÿÿÿT)Úkeepdim)	ÚdtypeÚtor2   Úfloat32ÚpowÚmeanÚrsqrtr5   r4   )r6   Úhidden_statesÚinput_dtypeÚvariances       r:   ÚforwardzAriaTextRMSNorm.forwardI   sy   € Ø#×)Ñ)ˆØ%×(Ñ(¬¯©Ó7ˆØ ×$Ñ$ QÓ'×,Ñ,¨R¸Ð,Ó>ˆØ%¬¯©°H¸t×?TÑ?TÑ4TÓ(UÑUˆØ�{‰{˜]×-Ñ-¨kÓ:Ñ:Ð:r;   c                 ó^   — t        | j                  j                  «      › d| j                  › �S )Nz, eps=)Útupler4   Úshaper5   ©r6   s    r:   Ú
extra_reprzAriaTextRMSNorm.extra_reprP   s*   € Ü˜Ÿ™×)Ñ)Ó*Ð+¨6°$×2GÑ2GÐ1HÐIÐIr;   )g�íµ ÷Æ°>)Ú__name__Ú
__module__Ú__qualname__r0   rH   rM   Ú__classcell__©r9   s   @r:   r-   r-   @   s   ø„ õ$ò;öJr;   r-   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚAriaProjectorMLPa!  
    Feed-Forward Network module for the Aria Projector.

    Args:
        in_features (`int`):
            Input embedding dimension.
        hidden_features (`int`):
            Hidden dimension of the feed-forward network.
        output_dim (`int`):
            Output dimension.
    c                 ó²   •— t         ‰| �  «        t        j                  ||d¬«      | _        t        j                  ||d¬«      | _        t        d   | _        y )NF©ÚbiasÚgelu_new)r/   r0   r)   ÚLinearÚ	linear_inÚ
linear_outr   Úact)r6   Úin_featuresÚhidden_featuresÚ
output_dimr9   s       €r:   r0   zAriaProjectorMLP.__init__a   sB   ø€ Ü‰ÑÔÜŸ™ ;°ÀeÔLˆŒÜŸ)™) O°ZÀeÔLˆŒÜ˜*Ñ%ˆ�r;   c                 óh   — | j                  | j                  |«      «      }| j                  |«      }|S ©N)r\   rZ   r[   )r6   rE   s     r:   rH   zAriaProjectorMLP.forwardg   s-   € ØŸ™ §¡°Ó!>Ó?ˆØŸ™¨Ó6ˆØÐr;   ©rN   rO   rP   Ú__doc__r0   rH   rQ   rR   s   @r:   rT   rT   T   s   ø„ ñ
ô&ör;   rT   c                   ó6   ‡ — e Zd ZdZddedefˆ fd„Zdd„Zˆ xZS )ÚAriaCrossAttentionzv
    Aria Cross-Attention module.

    Args:
        config (`AriaConfig`):
            The configuration to use.
    ÚconfigÚdropout_ratec                 óB  •— t         ‰| �  «        |j                  j                  }|j                  j                  }|| _        t        j                  ||d¬«      | _        t        j                  ||d¬«      | _	        t        j                  ||d¬«      | _
        t        j                  ||d¬«      | _        t        j                  ||«      | _        t        j                  |«      | _        t        j                   |«      | _        t        j                   |«      | _        y )NFrV   T)Úbatch_first)r/   r0   Úvision_configr7   Únum_attention_headsÚ	num_headsr)   rY   Úq_projÚk_projÚv_projÚMultiheadAttentionÚmultihead_attnÚlinearÚDropoutÚdropoutÚ	LayerNormÚ
layer_normÚlayer_norm_kv)r6   rf   rg   r7   rl   r9   s        €r:   r0   zAriaCrossAttention.__init__v   sÐ   ø€ Ü‰ÑÔØ×*Ñ*×6Ñ6ˆØ×(Ñ(×<Ñ<ˆ	Ø"ˆŒÜ—i‘i ¨[¸uÔEˆŒÜ—i‘i ¨[¸uÔEˆŒÜ—i‘i ¨[¸uÔEˆŒô !×3Ñ3°KÀÐX\Ô]ˆÔÜ—i‘i ¨[Ó9ˆŒÜ—z‘z ,Ó/ˆŒäŸ,™, {Ó3ˆŒÜŸ\™\¨+Ó6ˆÕr;   c                 ó  — | j                  | j                  |«      «      }| j                  |«      }| j                  |«      }| j	                  |«      }| j                  ||||¬«      \  }}| j                  | j                  |«      «      }|S )aÉ  
        Forward pass of the AriaCrossAttention module.

        Args:
            key_value_states (`torch.Tensor`):
                Input tensor for key and value.
            hidden_states (`torch.Tensor`):
                Input tensor for query.
            attn_mask (`torch.Tensor`, *optional*, defaults to None):
                Attention mask.

        Returns:
            torch.Tensor:
                Output tensor after cross-attention.
        ©Ú	attn_mask)rm   rv   rw   rn   ro   rq   rt   rr   )	r6   Úkey_value_statesrE   rz   ÚqueryÚkeyÚvalueÚattn_outputÚ_s	            r:   rH   zAriaCrossAttention.forward‡   s…   € ð  —‘˜DŸO™O¨MÓ:Ó;ˆà×-Ñ-Ð.>Ó?ÐØ�k‰kÐ*Ó+ˆØ—‘Ð,Ó-ˆà×,Ñ,¨U°C¸È)Ð,ÓT‰ˆ�Qà—l‘l 4§;¡;¨{Ó#;Ó<ˆàÐr;   )r   ra   )	rN   rO   rP   rc   r'   Úfloatr0   rH   rQ   rR   s   @r:   re   re   m   s    ø„ ññ7˜zð 7¸õ 7÷"r;   re   c                   óh   ‡ — e Zd ZdZdefˆ fd„Zddej                  deej                     fd„Z	ˆ xZ
S )ÚAriaProjectora  
    Aria Projector module.

    This module projects vision features into the language model's embedding space, enabling interaction between vision and language components.

    Args:
        config (`AriaConfig`):
            Configuration object for the model.
    rf   c                 óž  •— t         ‰| �  «        |j                  | _        |j                  j
                  | _        |j                  j                  | _        |j                  j
                  | _	        |j                  j
                  | _        |j                  j
                  | _        t        j                  t        j                   |j"                  | j                  «      «      | _        t'        |«      | _        t        j*                  | j                  «      | _        t/        | j                  | j                  | j                  «      | _        y ra   )r/   r0   Úprojector_patch_to_query_dictÚpatch_to_query_dictrj   r7   r]   rk   rl   Úkv_dimÚtext_configr^   r_   r)   r1   r2   ÚzerosÚ'max_value_projector_patch_to_query_dictr|   re   Ú
cross_attnru   rv   rT   Úfeed_forward©r6   rf   r9   s     €r:   r0   zAriaProjector.__init__¯   sí   ø€ ô 	‰ÑÔà#)×#GÑ#GˆÔ Ø!×/Ñ/×;Ñ;ˆÔØ×-Ñ-×AÑAˆŒØ×*Ñ*×6Ñ6ˆŒØ%×1Ñ1×=Ñ=ˆÔØ ×,Ñ,×8Ñ8ˆŒä—\‘\¤%§+¡+¨f×.\Ñ.\Ð^b×^nÑ^nÓ"oÓpˆŒ
ä,¨VÓ4ˆŒäŸ,™, t×'7Ñ'7Ó8ˆŒÜ,¨T×-=Ñ-=¸t×?SÑ?SÐUY×UdÑUdÓeˆÕr;   r{   rz   c                 óP  — |j                   d   |j                   d   }}|| j                  j                  «       vr*t        d|› d| j                  j                  «       › d�«      ‚| j                  |   }| j                  d| j                  d«      j                  |dd«      }|�M|j                  | j                  d«      }|j                  d«      j                  d|j                  d«      d«      }| j                  |||¬«      }| j                  | j                  |«      «      }|S )	a�  
        Forward pass of the Projector module.

        Args:
            key_value_states (`torch.Tensor`):
                Input tensor of shape (batch_size, num_patches, kv_dim).
            attn_mask (`torch.Tensor`, *optional*, default is None):
                Attention mask.

        Returns:
            `torch.Tensor`: Output tensor of shape (batch_size, query_number, output_dim).
        r   r&   zNumber of patches z: not found in patch_to_query_dict amongst possible values ú.Nr=   ry   )rK   r†   ÚkeysÚKeyErrorr|   Ú	unsqueezeÚrepeatÚrepeat_interleaverl   ÚexpandÚsizer‹   rŒ   rv   )	r6   r{   rz   Ú
batch_sizeÚnum_patchesÚ	query_numÚqueriesÚattention_outÚouts	            r:   rH   zAriaProjector.forwardÃ   s?  € ð #3×"8Ñ"8¸Ñ";Ð=M×=SÑ=SÐTUÑ=V�Kˆ
à˜d×6Ñ6×;Ñ;Ó=Ñ=ÜØ$ [ MÐ1kÐlp÷  mEñ  mE÷  mJñ  mJó  mLð  lMð  MNð  Oóð ð ×,Ñ,¨[Ñ9ˆ	à—*‘*˜Z˜iÐ(×2Ñ2°1Ó5×<Ñ<¸ZÈÈAÓNˆàÐ Ø!×3Ñ3°D·N±NÀAÓFˆIØ!×+Ñ+¨AÓ.×5Ñ5°b¸'¿,¹,Àq»/È2ÓNˆIàŸ™Ð(8¸'ÈY˜ÓWˆà×Ñ §¡°Ó >Ó?ˆàˆ
r;   ra   )rN   rO   rP   rc   r'   r0   r2   ÚTensorr   rH   rQ   rR   s   @r:   rƒ   rƒ   ¤   s7   ø„ ñðfàõfñ(¨¯©ð ÀÈ%Ï,É,ÑAW÷ r;   rƒ   c                   ó.   ‡ — e Zd ZdZdefˆ fd„Zd„ Zˆ xZS )ÚAriaSharedExpertsMLPa/  
    Shared Expert MLP for shared experts.

    Unlike routed experts, shared experts process all tokens without routing.
    This class reconfigures the intermediate size in comparison to the LlamaMLP.

    Args:
        config (`AriaTextConfig`): Configuration object for the Aria language model.
    rf   c                 ó   •— t         ‰| �  «        || _        |j                  | _        |j                  |j
                  z  | _        t        j                  | j                  | j                  |j                  ¬«      | _	        t        j                  | j                  | j                  |j                  ¬«      | _
        t        j                  | j                  | j                  |j                  ¬«      | _        t        |j                     | _        y )NrV   )r/   r0   rf   r7   Úintermediate_sizeÚmoe_num_shared_expertsr)   rY   Úmlp_biasÚ	gate_projÚup_projÚ	down_projr   Ú
hidden_actÚact_fnr�   s     €r:   r0   zAriaSharedExpertsMLP.__init__ð   sÃ   ø€ Ü‰ÑÔØˆŒØ!×-Ñ-ˆÔØ!'×!9Ñ!9¸F×<YÑ<YÑ!YˆÔÜŸ™ 4×#3Ñ#3°T×5KÑ5KÐRX×RaÑRaÔbˆŒÜ—y‘y ×!1Ñ!1°4×3IÑ3IÐPV×P_ÑP_Ô`ˆŒÜŸ™ 4×#9Ñ#9¸4×;KÑ;KÐRX×RaÑRaÔbˆŒÜ˜V×.Ñ.Ñ/ˆ�r;   c                 óˆ   — | j                  | j                  | j                  |«      «      | j                  |«      z  «      }|S ra   )r¦   r¨   r¤   r¥   )r6   Úxr¦   s      r:   rH   zAriaSharedExpertsMLP.forwardú   s6   € Ø—N‘N 4§;¡;¨t¯~©~¸aÓ/@Ó#AÀDÇLÁLÐQRÃOÑ#SÓTˆ	ØÐr;   ©rN   rO   rP   rc   r(   r0   rH   rQ   rR   s   @r:   rŸ   rŸ   å   s   ø„ ñð0˜~õ 0ör;   rŸ   c                 óò  — | j                   d   }|j                   d   }t        j                  ||| j                  | j                  ¬«      }t        j
                  |d¬«      }t        j                  dt        j                  |j                  ¬«      }t        j                  ||f«      }t        |j                   d   «      D ]2  }||   }	||dz      }
| |	|
 }t        j                  |||   «      }|||	|
 Œ4 |S )a*  
    Compute the matrix multiplication (GEMM) for each expert sequentially. This approach is computationally inefficient, especially when dealing with a large number of experts.

    Args:
        token_states (torch.Tensor): Input tensor of shape (num_tokens, in_features).
        expert_weights (torch.Tensor): Weight tensor of shape (num_experts, in_features, out_features).
        tokens_per_expert (torch.Tensor): Number of tokens assigned to each expert.

    Returns:
        torch.Tensor: Output tensor of shape (num_tokens, out_features).
    r   r=   ©r?   Údevice©Údimr&   )
rK   r2   r‰   r?   r®   ÚcumsumÚlongÚcatÚrangeÚmatmul)Útoken_statesÚexpert_weightsÚtokens_per_expertÚ
num_tokensÚout_featuresÚoutputÚcumsum_num_tokensÚzero_tensorÚ
expert_numÚstartÚendÚtokensrœ   s                r:   Úsequential_experts_gemmrÂ   ÿ   sô   € ð ×#Ñ# AÑ&€JØ!×'Ñ'¨Ñ+€LÜ�[‰[˜ \¸×9KÑ9KÐT`×TgÑTgÔh€FäŸ™Ð%6¸AÔ>Ðä—+‘+˜a¤u§z¡zÐ:K×:RÑ:RÔS€KÜŸ	™	 ;Ð0AÐ"BÓCÐä˜N×0Ñ0°Ñ3Ó4ò  ˆ
Ø! *Ñ-ˆØ 
¨Q¡Ñ/ˆØ˜e CÐ(ˆä�l‰l˜6 >°*Ñ#=Ó>ˆØˆˆu�SÑð ð €Mr;   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚAriaGroupedExpertsGemmaP  
    Grouped GEMM (General Matrix Multiplication) module for efficient expert computation.
    This module utilizes the grouped_gemm library (https://github.com/fanshiqing/grouped_gemm)
    for optimized performance. If the grouped_gemm library is not installed, it gracefully
    falls back to a sequential GEMM implementation, which may be slower but ensures
    functionality.

    Args:
        in_features (`int`):
            Number of input features.
        out_features (`int`):
            Number of output features.
        groups (`int`):
            Number of expert groups.
    c                 óª   •— t         ‰| �  «        || _        || _        || _        t        j                  t        j                  |||«      «      | _	        y ra   )
r/   r0   r]   rº   Úgroupsr)   r1   r2   Úemptyr4   )r6   r]   rº   rÆ   r9   s       €r:   r0   zAriaGroupedExpertsGemm.__init__/  sB   ø€ Ü‰ÑÔØ&ˆÔØ(ˆÔØˆŒÜ—l‘l¤5§;¡;¨v°{ÀLÓ#QÓRˆ�r;   c                 óL   — t        || j                  |j                  «       «      S )au  
        Perform grouped matrix multiplication.

        Args:
            input (`torch.Tensor`):
                Input tensor of shape (num_tokens, in_features).
            tokens_per_expert (`torch.Tensor`):
                Number of tokens assigned to each expert.

        Returns:
            torch.Tensor: Output tensor of shape (num_tokens, out_features).
        )rÂ   r4   Úcpu)r6   Úinputr¸   s      r:   rH   zAriaGroupedExpertsGemm.forward6  s'   € ô 'ØØ�K‰KØ×!Ñ!Ó#ó
ð 	
r;   rb   rR   s   @r:   rÄ   rÄ     s   ø„ ñô Sö
r;   rÄ   c                   ó2   ‡ — e Zd ZdZdeddfˆ fd„Zd„ Zˆ xZS )ÚAriaGroupedExpertsMLPz’
    Grouped MLP module for Mixture of Experts.

    Args:
        config (`AriaTextConfig`):
            Configuration object for the model.
    rf   ÚreturnNc                 óö   •— t         ‰| �  «        || _        t        |j                  |j
                  dz  |j                  «      | _        t        |j
                  |j                  |j                  «      | _        y )Nr#   )	r/   r0   rf   rÄ   r7   r¡   Úmoe_num_expertsÚfc1Úfc2r�   s     €r:   r0   zAriaGroupedExpertsMLP.__init__S  sa   ø€ Ü‰ÑÔØˆŒÜ)¨&×*<Ñ*<¸f×>VÑ>VÐYZÑ>ZÐ\b×\rÑ\rÓsˆŒÜ)¨&×*BÑ*BÀF×DVÑDVÐX^×XnÑXnÓoˆ�r;   c                 óÈ   — | j                  ||«      }t        j                  |dd¬«      \  }}t        j                  j                  |«      |z  }| j                  ||«      }|S )a5  
        Forward pass of the Grouped MLP.

        Args:
            permuted_tokens (torch.Tensor): Permuted input tokens.
            tokens_per_expert (torch.Tensor): Number of tokens assigned to each expert.

        Returns:
            torch.Tensor: Output tensor after passing through the MLP.
        r#   r=   r¯   )rÐ   r2   Úchunkr)   Ú
functionalÚsilurÑ   )r6   Úpermuted_tokensr¸   Ú
fc1_outputÚ
projectionÚgateÚ
fc2_outputs          r:   rH   zAriaGroupedExpertsMLP.forwardY  s\   € ð —X‘X˜oÐ/@ÓAˆ
Ü Ÿ;™; z°1¸"Ô=Ñˆ
�DÜ—]‘]×'Ñ'¨
Ó3°dÑ:ˆ
Ø—X‘X˜jÐ*;Ó<ˆ
ØÐr;   r«   rR   s   @r:   rÌ   rÌ   J  s#   ø„ ñðp˜~ð p°$õ pör;   rÌ   c                   ó`   ‡ — e Zd ZdZdefˆ fd„Zdej                  dej                  fd„Zˆ xZ	S )ÚAriaTextMoELayerzþ
    Aria Text Mixture of Experts (MoE) Layer.

    This layer applies a gating mechanism to route input tokens to different experts.

    Args:
        config (`AriaTextConfig`):
            Configuration object for the text component of the model.
    rf   c                 óÒ   •— t         ‰| �  «        t        j                  |j                  |j
                  d¬«      | _        t        |«      | _        t        |«      | _
        || _        y ©NFrV   )r/   r0   r)   rY   r7   rÏ   ÚrouterrÌ   ÚexpertsrŸ   Úshared_expertsrf   r�   s     €r:   r0   zAriaTextMoELayer.__init__w  sO   ø€ Ü‰ÑÔä—i‘i × 2Ñ 2°F×4JÑ4JÐQVÔWˆŒÜ,¨VÓ4ˆŒÜ2°6Ó:ˆÔØˆ�r;   rE   rÍ   c                 óü  — |j                   }|j                  d|j                  d«      «      }| j                  |«      }t	        j
                  || j                  j                  d¬«      \  }}t        j                  j                  |d¬«      }|j                  }t	        j                  |j                  «       j                  t        j                  «      | j                  j                   d| j                  j                   dz
  ¬«      j                  |«      }|}	|	j                  d«      }
t	        j"                  |
«      }|j%                  d|| j                  j                  z  «      }| j'                  ||«      }t	        j(                  |j                   d   | j                  j                  z  |j                  d«      f|j                  |j*                  ¬«      }|j-                  d||«       |j                  d| j                  j                  |j                  d«      «      }||j/                  d«      z  j1                  d¬«      j                  |«      }| j3                  |j                  |«      «      }||z   S )a.  
        Forward pass of the MoE Layer.

        Args:
            hidden_states (`torch.Tensor`):
                Input tensor of shape (batch_size, sequence_length, hidden_size).

        Returns:
            torch.Tensor: Output tensor after passing through the MoE layer.

        Process:
        1. Route tokens to experts using the router.
        2. Permute tokens based on routing decisions.
        3. Process tokens through experts.
        4. Unpermute and combine expert outputs.
        5. Add shared expert output to the final result.
        r=   r&   )Úkr°   r¯   r   )ÚbinsÚminÚmaxr­   )rK   Úviewr–   rß   r2   Útopkrf   Úmoe_topkr)   rÔ   Úsoftmaxr?   ÚhistcÚflattenr@   rA   rÏ   ÚargsortÚindex_selectrà   r‰   r®   Úindex_copy_r’   Úsumrá   )r6   rE   Úoriginal_shapeÚlogitsÚ
top_logitsÚtop_indicesÚscoresÚoriginal_dtyper¸   ÚindicesÚflatten_indicesÚsorted_indicesrÖ   Úexpert_outputÚunpermuted_tokensr»   Úshared_expert_outputs                    r:   rH   zAriaTextMoELayer.forward  s  € ð$ '×,Ñ,ˆØ%×*Ñ*¨2¨}×/AÑ/AÀ"Ó/EÓFˆð —‘˜]Ó+ˆÜ"'§*¡*¨V°t·{±{×7KÑ7KÐQRÔ"SÑˆ
�KÜ—‘×&Ñ& z°rÐ&Ó:ˆà$×*Ñ*ˆä!ŸK™KØ×ÑÓ!×$Ñ$¤U§]¡]Ó3Ø—‘×,Ñ,ØØ—‘×+Ñ+¨aÑ/ô	
÷
 ‰"ˆ^Ó
ð 	ð ˆð "Ÿ,™, rÓ*ˆÜŸ™ Ó7ˆØ'×4Ñ4°Q¸È$Ï+É+×J^ÑJ^Ñ8^Ó_ˆð Ÿ™ _Ð6GÓHˆô "ŸK™KØ�\‰\˜!‰_˜tŸ{™{×3Ñ3Ñ3°]×5GÑ5GÈÓ5JÐKØ×%Ñ%Ø ×'Ñ'ô
Ðð
 	×%Ñ% a¨¸ÔGØ-×2Ñ2°2°t·{±{×7KÑ7KÈ]×M_ÑM_Ð`aÓMbÓcÐà# f×&6Ñ&6°rÓ&:Ñ:×?Ñ?ÀAÐ?ÓF×KÑKÈNÓ[ˆð  $×2Ñ2°=×3EÑ3EÀnÓ3UÓVÐØÐ,Ñ,Ð,r;   )
rN   rO   rP   rc   r(   r0   r2   r�   rH   rQ   rR   s   @r:   rÜ   rÜ   l  s/   ø„ ñð˜~õ ð9- U§\¡\ð 9-°e·l±l÷ 9-r;   rÜ   c                 óš   — | dd| j                   d   dz  …f   }| d| j                   d   dz  d…f   }t        j                  | |fd¬«      S )z*Rotates half the hidden dims of the input..Nr=   r#   r¯   )rK   r2   r³   )rª   Úx1Úx2s      r:   Úrotate_halfr   »  sZ   € à	
ˆ3Ð"�!—'‘'˜"‘+ Ñ"Ð"Ð"Ñ	#€BØ	
ˆ3�—‘˜‘˜qÑ Ñ"Ð"Ñ	#€BÜ�9‰9�r�c˜2�Y BÔ'Ð'r;   c                 óž   — |j                  |«      }|j                  |«      }| |z  t        | «      |z  z   }||z  t        |«      |z  z   }||fS )aÛ  Applies Rotary Position Embedding to the query and key tensors.

    Args:
        q (`torch.Tensor`): The query tensor.
        k (`torch.Tensor`): The key tensor.
        cos (`torch.Tensor`): The cosine part of the rotary embedding.
        sin (`torch.Tensor`): The sine part of the rotary embedding.
        position_ids (`torch.Tensor`, *optional*):
            Deprecated and unused.
        unsqueeze_dim (`int`, *optional*, defaults to 1):
            The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
            sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
            that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
            k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
            cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
            the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
    Returns:
        `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
    )r’   r   )Úqrã   ÚcosÚsinÚposition_idsÚunsqueeze_dimÚq_embedÚk_embeds           r:   Úapply_rotary_pos_embr	  Â  sY   € ð( �-‰-˜Ó
&€CØ
�-‰-˜Ó
&€CØ�3‰wœ; q›>¨CÑ/Ñ0€GØ�3‰wœ; q›>¨CÑ/Ñ0€GØ�GÐÐr;   rE   Ún_reprÍ   c                 óª   — | j                   \  }}}}|dk(  r| S | dd…dd…ddd…dd…f   j                  |||||«      } | j                  |||z  ||«      S )zÔ
    This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
    num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
    r&   N)rK   r•   Úreshape)rE   r
  ÚbatchÚnum_key_value_headsÚslenÚhead_dims         r:   Ú	repeat_kvr  Ý  so   € ð
 2?×1DÑ1DÑ.€EÐ  hØ�‚zØÐØ!¢!¢Q¨ªa²Ð"2Ñ3×:Ñ:¸5ÐBUÐW\Ð^bÐdlÓm€MØ× Ñ  Ð(;¸eÑ(CÀTÈ8ÓTÐTr;   Úmoduler|   r}   r~   Úattention_maskÚscalingrt   c                 óT  — t        || j                  «      }t        || j                  «      }	t        j                  ||j	                  dd«      «      |z  }
|�#|d d …d d …d d …d |j
                  d   …f   }|
|z   }
t        j                  j                  |
dt        j                  ¬«      j                  |j                  «      }
t        j                  j                  |
|| j                  ¬«      }
t        j                  |
|	«      }|j	                  dd«      j                  «       }||
fS )Nr#   r
   éþÿÿÿr=   )r°   r?   )ÚpÚtrainingr&   )r  Únum_key_value_groupsr2   rµ   Ú	transposerK   r)   rÔ   rê   rA   r@   r?   rt   r  Ú
contiguous)r  r|   r}   r~   r  r  rt   ÚkwargsÚ
key_statesÚvalue_statesÚattn_weightsÚcausal_maskr   s                r:   Úeager_attention_forwardr!  é  s  € ô ˜3 × ;Ñ ;Ó<€JÜ˜U F×$?Ñ$?Ó@€Lä—<‘<  z×';Ñ';¸A¸qÓ'AÓBÀWÑL€LØÐ!Ø$¢Qªª1Ð.D°
×0@Ñ0@ÀÑ0DÐ.DÐ%DÑEˆØ# kÑ1ˆä—=‘=×(Ñ(¨¸2ÄUÇ]Á]Ð(ÓS×VÑVÐW\×WbÑWbÓc€LÜ—=‘=×(Ñ(¨¸È6Ï?É?Ð(Ó[€LÜ—,‘,˜|¨\Ó:€KØ×'Ñ'¨¨1Ó-×8Ñ8Ó:€Kà˜Ð$Ð$r;   c                   ó6  ‡ — e Zd ZdZdedefˆ fd„Z	 	 ddej                  de	ej                  ej                  f   de
ej                     de
e   d	e
ej                     d
ee   de	ej                  e
ej                     e
e	ej                        f   fd„Zˆ xZS )ÚAriaTextAttentionz=Multi-headed attention from 'Attention Is All You Need' paperrf   Ú	layer_idxc                 ód  •— t         ‰| �  «        || _        || _        t	        |d|j
                  |j                  z  «      | _        |j                  |j                  z  | _	        | j                  dz  | _
        |j                  | _        d| _        t        j                  |j
                  |j                  | j                  z  |j                  ¬«      | _        t        j                  |j
                  |j                  | j                  z  |j                  ¬«      | _        t        j                  |j
                  |j                  | j                  z  |j                  ¬«      | _        t        j                  |j                  | j                  z  |j
                  |j                  ¬«      | _        y )Nr  g      à¿TrV   )r/   r0   rf   r$  Úgetattrr7   rk   r  r  r  r  Úattention_dropoutÚ	is_causalr)   rY   Úattention_biasrm   rn   ro   Úo_proj©r6   rf   r$  r9   s      €r:   r0   zAriaTextAttention.__init__  sM  ø€ Ü‰ÑÔØˆŒØ"ˆŒÜ ¨
°F×4FÑ4FÈ&×JdÑJdÑ4dÓeˆŒØ$*×$>Ñ$>À&×B\ÑB\Ñ$\ˆÔ!Ø—}‘} dÑ*ˆŒØ!'×!9Ñ!9ˆÔØˆŒä—i‘iØ×Ñ × :Ñ :¸T¿]¹]Ñ JÐQW×QfÑQfô
ˆŒô —i‘iØ×Ñ × :Ñ :¸T¿]¹]Ñ JÐQW×QfÑQfô
ˆŒô —i‘iØ×Ñ × :Ñ :¸T¿]¹]Ñ JÐQW×QfÑQfô
ˆŒô —i‘iØ×&Ñ&¨¯©Ñ6¸×8JÑ8JÐQW×QfÑQfô
ˆ�r;   rE   Úposition_embeddingsr  Úpast_key_valueÚcache_positionr  rÍ   c                 ó¶  — |j                   d d }g |¢d‘| j                  ‘­}| j                  |«      j                  |«      j	                  dd«      }	| j                  |«      j                  |«      j	                  dd«      }
| j                  |«      j                  |«      j	                  dd«      }|\  }}t        |	|
||«      \  }	}
|�'|||dœ}|j                  |
|| j                  |«      \  }
}t        }| j                  j                  dk7  r^| j                  j                  dk(  r(|j                  dd«      rt        j                  d	«       nt         | j                  j                     } || |	|
||f| j"                  sd
n| j$                  | j&                  dœ|¤Ž\  }} |j(                  g |¢d‘­Ž j+                  «       }| j-                  |«      }||fS )Nr=   r&   r#   )r  r  r.  ÚeagerÚsdpaÚoutput_attentionsFzã`torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to eager attention. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.ç        )rt   r  )rK   r  rm   rç   r  rn   ro   r	  Úupdater$  r!  rf   Ú_attn_implementationÚgetÚloggerÚwarning_oncer   r  r'  r  r  r  r*  )r6   rE   r,  r  r-  r.  r  Úinput_shapeÚhidden_shapeÚquery_statesr  r  r  r  Úcache_kwargsÚattention_interfacer   r  s                     r:   rH   zAriaTextAttention.forward  sá  € ð $×)Ñ)¨#¨2Ð.ˆØ8˜Ð8 bÐ8¨$¯-©-Ñ8ˆà—{‘{ =Ó1×6Ñ6°|ÓD×NÑNÈqÐRSÓTˆØ—[‘[ Ó/×4Ñ4°\ÓB×LÑLÈQÐPQÓRˆ
Ø—{‘{ =Ó1×6Ñ6°|ÓD×NÑNÈqÐRSÓTˆà&‰ˆˆSÜ#7¸ÀjÐRUÐWZÓ#[Ñ ˆ�jàÐ%à#&¨sÀnÑUˆLØ'5×'<Ñ'<¸ZÈÐW[×WeÑWeÐgsÓ'tÑ$ˆJ˜ä(?ÐØ�;‰;×+Ñ+¨wÒ6Ø�{‰{×/Ñ/°6Ò9¸f¿j¹jÐI\Ð^cÔ>dÜ×#Ñ#ðLõô
 '>¸d¿k¹k×>^Ñ>^Ñ&_Ð#á$7ØØØØØð	%
ð  $Ÿ}š}‘C°$×2HÑ2HØ—L‘Lñ	%
ð ñ	%
Ñ!ˆ�\ð *�k×)Ñ)Ð;¨;Ð;¸Ò;×FÑFÓHˆØ—k‘k +Ó.ˆØ˜LÐ(Ð(r;   )NN)rN   rO   rP   rc   r(   Úintr0   r2   r�   r   r   r   Ú
LongTensorr   r   rH   rQ   rR   s   @r:   r#  r#    sÅ   ø„ ÙGð
˜~ð 
¸#õ 
ð8 +/Ø59ñ/)à—|‘|ð/)ð # 5§<¡<°·±Ð#=Ñ>ð/)ð ! §¡Ñ.ð	/)ð
 ! ™ð/)ð ! ×!1Ñ!1Ñ2ð/)ð Ð-Ñ.ð/)ð 
ˆu�|‰|˜X e§l¡lÑ3°X¸eÀEÇLÁLÑ>QÑ5RÐRÑ	S÷/)r;   r#  c                   ót  ‡ — e Zd ZdZdedefˆ fd„Z	 	 	 	 	 	 	 ddej                  de	ej                     de	ej                     de	e   d	e	e   d
e	e   de	ej                     de	eej                  ej                  f      dee   deej                   e	eej                   ej                   f      f   fd„Zˆ xZS )ÚAriaTextDecoderLayerag  
    Aria Text Decoder Layer.

    This class defines a single decoder layer in the language model, incorporating self-attention and Mixture of Experts (MoE) feed-forward network.

    Args:
        config (`AriaTextConfig`):
            Configuration object for the text component of the model.
        layer_idx (`int`):
            Index of the layer.
    rf   r$  c                 ó   •— t         ‰| �  «        |j                  | _        t        ||¬«      | _        t        |«      | _        t        |j                  |j                  ¬«      | _	        t        |j                  |j                  ¬«      | _
        y )N)rf   r$  ©r8   )r/   r0   r7   r#  Ú	self_attnrÜ   Úmlpr-   Úrms_norm_epsÚinput_layernormÚpost_attention_layernormr+  s      €r:   r0   zAriaTextDecoderLayer.__init__\  sm   ø€ Ü‰ÑÔØ!×-Ñ-ˆÔä*°&ÀIÔNˆŒÜ# FÓ+ˆŒÜ.¨v×/AÑ/AÀv×GZÑGZÔ[ˆÔÜ(7¸×8JÑ8JÐPV×PcÑPcÔ(dˆÕ%r;   rE   r  r  r-  r2  Ú	use_cacher.  r,  r  rÍ   c	                 óÚ   — |}
| j                  |«      } | j                  d||||||||dœ|	¤Ž\  }}|
|z   }|}
| j                  |«      }| j                  |«      }|
|z   }|f}|r||fz  }|S )N)rE   r  r  r-  r2  rI  r.  r,  © )rG  rD  rH  rE  )r6   rE   r  r  r-  r2  rI  r.  r,  r  ÚresidualÚself_attn_weightsÚoutputss                r:   rH   zAriaTextDecoderLayer.forwarde  s±   € ð !ˆà×,Ñ,¨]Ó;ˆð ,:¨4¯>©>ð 
,
Ø'Ø)Ø%Ø)Ø/ØØ)Ø 3ñ
,
ð ñ
,
Ñ(ˆÐ(ð ! =Ñ0ˆð !ˆØ×5Ñ5°mÓDˆØŸ™ Ó/ˆØ  =Ñ0ˆà Ð"ˆÙØÐ)Ð+Ñ+ˆGàˆr;   )NNNFFNN)rN   rO   rP   rc   r(   r>  r0   r2   r�   r   r?  r   Úboolr   r   r   ÚFloatTensorrH   rQ   rR   s   @r:   rA  rA  O  s  ø„ ñ
ðe˜~ð e¸#õ eð 26Ø37Ø*.Ø,1Ø$)Ø59ØKOñ(à—|‘|ð(ð ! §¡Ñ.ð(ð ˜u×/Ñ/Ñ0ð	(ð
 ! ™ð(ð $ D™>ð(ð ˜D‘>ð(ð ! ×!1Ñ!1Ñ2ð(ð & e¨E¯L©L¸%¿,¹,Ð,FÑ&GÑHð(ð Ð-Ñ.ð(ð 
ˆu× Ñ  (¨5°×1BÑ1BÀE×DUÑDUÐ1UÑ+VÑ"WÐWÑ	X÷(r;   rA  c                   ó:   — e Zd ZdZeZdZddgZdZdZ	dZ
dZdZd„ Zy	)
ÚAriaTextPreTrainedModelz‚
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models.
    ÚmodelrA  rÄ   TÚpast_key_valuesFc                 ó†  — | j                   j                  }t        |t        j                  «      rY|j
                  j                  j                  d|¬«       |j                  �%|j                  j                  j                  «        y y t        |t        j                  «      rf|j
                  j                  j                  d|¬«       |j                  �2|j
                  j                  |j                     j                  «        y y t        |t        «      r(|j
                  j                  j                  d|¬«       y t        |t        j                  «      rf|j
                  j                  j                  d|¬«       t        |d«      r2|j                  �%|j                  j                  j                  «        y y y y )Nr3  ©rC   ÚstdrW   )rf   Úinitializer_rangeÚ
isinstancer)   rY   r4   ÚdataÚnormal_rW   Úzero_Ú	EmbeddingÚpadding_idxrÄ   ÚConv2dÚhasattr©r6   r  rW  s      r:   Ú_init_weightsz%AriaTextPreTrainedModel._init_weightsž  sH  € Ø�k‰k×+Ñ+ˆÜ�fœbŸi™iÔ(Ø�M‰M×Ñ×&Ñ&¨C°SÐ&Ô9Ø�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ä˜¤§¡Ô-Ø�M‰M×Ñ×&Ñ&¨C°SÐ&Ô9Ø×!Ñ!Ð-Ø—‘×"Ñ" 6×#5Ñ#5Ñ6×<Ñ<Õ>ð .ä˜Ô 6Ô7Ø�M‰M×Ñ×&Ñ&¨C°SÐ&Õ9Ü˜¤§	¡	Ô*Ø�M‰M×Ñ×&Ñ&¨C°SÐ&Ô9Ü�v˜vÔ&¨6¯;©;Ð+BØ—‘× Ñ ×&Ñ&Õ(ð ,CÐ&ð +r;   N)rN   rO   rP   rc   r'   Úconfig_classÚbase_model_prefixÚ_no_split_modulesÚsupports_gradient_checkpointingÚ_skip_keys_device_placementÚ_supports_flash_attn_2Ú_supports_sdpaÚ_supports_cache_classrb  rK  r;   r:   rR  rR  �  sB   „ ñð €LØÐØ/Ð1IÐJÐØ&*Ð#Ø"3ÐØ"ÐØ€NØ Ðó)r;   rR  aM  
    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

    This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
    Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
    and behavior.

    Parameters:
        config ([`AriaTextConfig`]):
            Model configuration class with all the parameters of the model. Initializing with a config file does not
            load the weights associated with the model, only the configuration. Check out the
            [`~PreTrainedModel.from_pretrained`] method to load the model weights.
zRThe bare Aria Model outputting raw hidden-states without any specific head on top.c                   óF   — e Zd ZeZdZdZdgZdgZdZ	dZ
dZdZdZdZdZd„ Zy)ÚAriaPreTrainedModelrS  TÚAriaDecoderLayerrT  Fc                 óŽ  — | j                   j                  }t        |t        j                  «      rY|j
                  j                  j                  d|¬«       |j                  �%|j                  j                  j                  «        y y t        |t        j                  «      rf|j
                  j                  j                  d|¬«       |j                  �2|j
                  j                  |j                     j                  «        y y t        |t        «      r,t        j                  j                  |j                  |¬«       y y )Nr3  rV  )rW  )rf   rX  rY  r)   rY   r4   rZ  r[  rW   r\  r]  r^  rƒ   ÚinitÚtrunc_normal_r|   ra  s      r:   rb  z!AriaPreTrainedModel._init_weightsÓ  sè   € Ø�k‰k×+Ñ+ˆÜ�fœbŸi™iÔ(Ø�M‰M×Ñ×&Ñ&¨C°SÐ&Ô9Ø�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ä˜¤§¡Ô-Ø�M‰M×Ñ×&Ñ&¨C°SÐ&Ô9Ø×!Ñ!Ð-Ø—‘×"Ñ" 6×#5Ñ#5Ñ6×<Ñ<Õ>ð .ä˜¤Ô.Ü�G‰G×!Ñ! &§,¡,°CÐ!Õ8ð /r;   N)rN   rO   rP   r(   rc  rd  rf  re  rg  rh  ri  Ú_supports_flex_attnrj  Ú_supports_quantized_cacheÚ_supports_static_cacheÚ_supports_attention_backendrb  rK  r;   r:   rl  rl  Á  sU   „ ð
 "€LØÐØ&*Ð#Ø+Ð,ÐØ#4Ð"5ÐØ!ÐØ€NØÐØ ÐØ $ÐØ"ÐØ"'Ðó9r;   rl  c                   ó^   ‡ — e Zd Zddefˆ fd„Z ej                  «       ed„ «       «       Zˆ xZ	S )ÚAriaTextRotaryEmbeddingrf   c                 óê  •— t         ‰| �  «        t        |d«      rG|j                  �;|j                  j	                  d|j                  j	                  d«      «      | _        nd| _        |j                  | _        |j                  | _        || _	        t        | j
                     | _        | j                  | j                  |«      \  }| _        | j                  d|d¬«       | j                  | _        y )NÚrope_scalingÚ	rope_typeÚtypeÚdefaultÚinv_freqF)Ú
persistent)r/   r0   r`  rx  r6  ry  Úmax_position_embeddingsÚmax_seq_len_cachedÚoriginal_max_seq_lenrf   r   Úrope_init_fnÚattention_scalingÚregister_bufferr|  Úoriginal_inv_freq)r6   rf   r®   r|  r9   s       €r:   r0   z AriaTextRotaryEmbedding.__init__â  sÉ   ø€ Ü‰ÑÔä�6˜>Ô*¨v×/BÑ/BÐ/NØ#×0Ñ0×4Ñ4°[À&×BUÑBU×BYÑBYÐZ`ÓBaÓbˆD�Nà&ˆDŒNØ"(×"@Ñ"@ˆÔØ$*×$BÑ$BˆÔ!àˆŒÜ/°·±Ñ?ˆÔà+/×+<Ñ+<¸T¿[¹[È&Ó+QÑ(ˆ�$Ô(Ø×Ñ˜Z¨¸eÐÔDØ!%§¡ˆÕr;   c                 ób  — | j                   d d d …d f   j                  «       j                  |j                  d   dd«      j	                  |j
                  «      }|d d …d d d …f   j                  «       }t        |j
                  j                  t        «      r/|j
                  j                  dk7  r|j
                  j                  nd}t        j                  |d¬«      5  |j                  «       |j                  «       z  j                  dd«      }t        j                  ||fd¬	«      }|j                  «       | j                  z  }|j                  «       | j                  z  }	d d d «       j	                  |j                   ¬
«      	j	                  |j                   ¬
«      fS # 1 sw Y   ŒAxY w)Nr   r=   r&   ÚmpsrÉ   F)Údevice_typeÚenabledr#   r¯   )r?   )r|  r�   r•   rK   r@   r®   rY  rz  Ústrr2   Úautocastr  r³   r  r‚  r  r?   )
r6   rª   r  Úinv_freq_expandedÚposition_ids_expandedr‡  ÚfreqsÚembr  r  s
             r:   rH   zAriaTextRotaryEmbedding.forwardó  sV  € ð !ŸM™M¨$²°4¨-Ñ8×>Ñ>Ó@×GÑGÈ×HZÑHZÐ[\ÑH]Ð_aÐcdÓe×hÑhÐij×iqÑiqÓrÐØ ,ªQ°²a¨ZÑ 8× >Ñ >Ó @Ðä'1°!·(±(·-±-ÄÔ'EÈ!Ï(É(Ï-É-Ð[`ÒJ`�a—h‘h—m’mÐfkˆÜ�^‰^¨¸UÔCñ 	5Ø&×,Ñ,Ó.Ð1F×1LÑ1LÓ1NÑN×YÑYÐZ[Ð]^Ó_ˆEÜ—)‘)˜U E˜N°Ô3ˆCØ—'‘'“)˜d×4Ñ4Ñ4ˆCØ—'‘'“)˜d×4Ñ4Ñ4ˆC÷		5ð �v‰v˜AŸG™GˆvÓ$ c§f¡f°1·7±7 fÓ&;Ð;Ð;÷	5ð 	5ús   Ã BF%Æ%F.ra   )
rN   rO   rP   r(   r0   r2   Úno_gradr   rH   rQ   rR   s   @r:   rv  rv  á  s3   ø„ ñ/˜~õ /ð" €U‡]�]ƒ_Øñ<ó ó ô<r;   rv  a$  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
            it.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

            [What are attention masks?](../glossary#attention-mask)

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            If `past_key_values` is used, optionally only the last `input_ids` have to be input (see
            `past_key_values`).

            If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
            and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
            information on the default strategy.

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.
        position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.n_positions - 1]`.

            [What are position IDs?](../glossary#position-ids)
        past_key_values (`Cache`, *optional*):
            Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
            blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
            returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.

            It is a [`~cache_utils.Cache`] instance. For more details, see our [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache).

            If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
            have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
            of shape `(batch_size, sequence_length)`.
        inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        use_cache (`bool`, *optional*):
            If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
            `past_key_values`).
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
        cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
            Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`,
            this tensor is not affected by padding. It is used to update the cache in the correct position and to infer
            the complete sequence length.
zVThe bare AriaText Model outputting raw hidden-states without any specific head on top.c                   ó  ‡ — e Zd ZdZdefˆ fd„Zd„ Zd„ Ze e	e
«      	 	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     d	ee   d
eej                      dee   dee   dee   deej                     dee   defd„«       «       Z	 ddej                  dej                  dej                  d	edef
d„Zedej                  dededej2                  dej4                  dej                  defd„«       Zˆ xZS )ÚAriaTextModelz¡
    Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`AriaTextDecoderLayer`]

    Args:
        config: AriaTextConfig
    rf   c           	      ó  •— t         ‰| �  |«       |j                  | _        |j                  | _        t        j                  |j                  |j                  | j                  «      | _        t        j                  t        |j                  «      D �cg c]  }t        ||«      ‘Œ c}«      | _        t        |j                  |j                  ¬«      | _        t#        |¬«      | _        d| _        | j)                  «        y c c}w )NrC  )rf   F)r/   r0   Úpad_token_idr^  Ú
vocab_sizer)   r]  r7   Úembed_tokensÚ
ModuleListr´   Únum_hidden_layersrA  Úlayersr-   rF  Únormrv  Ú
rotary_embÚgradient_checkpointingÚ	post_initr+  s      €r:   r0   zAriaTextModel.__init__R  sÆ   ø€ Ü‰Ñ˜Ô Ø!×.Ñ.ˆÔØ ×+Ñ+ˆŒäŸL™L¨×):Ñ):¸F×<NÑ<NÐPT×P`ÑP`ÓaˆÔÜ—m‘mÜFKÈF×LdÑLdÓFeÖf¸Ô! &¨)Õ4Òfó
ˆŒô $ F×$6Ñ$6¸F×<OÑ<OÔPˆŒ	Ü1¸Ô@ˆŒØ&+ˆÔ#ð 	�‰Õùò gs   ÂDc                 ó   — | j                   S ra   ©r•  rL   s    r:   Úget_input_embeddingsz"AriaTextModel.get_input_embeddingsb  s   € Ø× Ñ Ð r;   c                 ó   — || _         y ra   rž  ©r6   r~   s     r:   Úset_input_embeddingsz"AriaTextModel.set_input_embeddingse  s
   € Ø!ˆÕr;   Ú	input_idsr  r  rT  Úinputs_embedsrI  r2  Úoutput_hidden_statesr.  Úflash_attn_kwargsrÍ   c
                 ó^  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|d u |d uz  rt	        d«      ‚| j
                  r%| j                  r|rt        j                  d«       d}t        |t        d «      t        f«      st	        d«      ‚|€| j                  |«      }|r|€
t        «       }|	€F|�|j                  «       nd}t        j                   |||j"                  d   z   |j$                  ¬«      }	|€|	j'                  d«      }| j)                  |||	||«      }|}| j+                  ||«      }|rdnd }|rdnd }| j,                  d | j                   j.                   D ]r  }|r||fz  }| j
                  r:| j                  r.| j1                  t3        |j4                  fi |
¤Ž|||||||	|«	      }n ||f||||||	|d	œ|
¤Ž}|d   }|sŒj||d   fz  }Œt | j7                  |«      }|r||fz  }t9        ||r|nd ||¬
«      S )Nz:You must specify exactly one of input_ids or inputs_embedszX`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`.FzBThe `past_key_values` should be either a `Cache` object or `None`.r   r&   ©r®   rK  )r  r  r-  r2  rI  r.  r,  )Úlast_hidden_staterT  rE   Ú
attentions)rf   r2  r¥  rI  Ú
ValueErrorr›  r  r7  r8  rY  rz  r   r•  r   Úget_seq_lengthr2   ÚarangerK   r®   r’   Ú_update_causal_maskrš  r˜  r—  Ú_gradient_checkpointing_funcr   Ú__call__r™  r   )r6   r£  r  r  rT  r¤  rI  r2  r¥  r.  r¦  Úpast_seen_tokensr   rE   r,  Úall_hidden_statesÚall_self_attnsÚdecoder_layerÚlayer_outputss                      r:   rH   zAriaTextModel.forwardh  s£  € ð 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð "+Ð!6‘I¸D¿K¹K×<QÑ<Qˆ	à˜Ð -°tÐ";Ò<ÜÐYÓZÐZà×&Ò&¨4¯=ª=¹YÜ×ÑØjôð ˆIô ˜/¬D°«J¼Ð+>Ô?ÜÐaÓbÐbàÐ Ø ×-Ñ-¨iÓ8ˆMá˜Ð0Ü*›nˆOàÐ!ØCRÐC^˜×=Ñ=Ô?ÐdeÐÜ"Ÿ\™\Ø Ð"2°]×5HÑ5HÈÑ5KÑ"KÐTa×ThÑThôˆNð ÐØ)×3Ñ3°AÓ6ˆLà×.Ñ.Ø˜M¨>¸?ÐL]ó
ˆð &ˆð #Ÿo™o¨m¸\ÓJÐñ #7™B¸DÐÙ0™°dˆà!Ÿ[™[Ð)H¨4¯;©;×+HÑ+HÐIò  	6ˆMÙ#Ø! mÐ%5Ñ5Ð!à×*Ò*¨t¯}ª}Ø $× AÑ AÜ˜M×2Ñ2ÑHÐ6GÑHØ!ØØ Ø#Ø%ØØ"Ø'ó
!‘ñ !.Ø!ð
!à#.Ø!-Ø#2Ø&7Ø'Ø#1Ø(;ñ
!ð (ñ
!�ð *¨!Ñ,ˆMâ Ø =°Ñ#3Ð"5Ñ5‘ðA 	6ðD Ÿ	™	 -Ó0ˆñ  Ø -Ð!1Ñ1Ðä&Ø+Ù/8™O¸dØ+Ø%ô	
ð 	
r;   Úinput_tensorc           
      óÆ  — | j                   j                  dk(  r|�|dk(  j                  «       r|S y | j                   j                  dk(  r7t        |t        j
                  «      rt        |«      }t        |t        «      r|S |�|j                  «       nd}t        |t        «      }| j                   j                  dk(  r(|s&|s$t        j                  |||| j                  ¬«      ry |j                  |j                  }	}|j                  d   }
|r|j!                  «       }n1t        |t        j
                  «      r|j                  d   n||
z   dz   }| j#                  ||
|||	||j                  d   ¬	«      }| j                   j                  dk(  rQ|�O|j                  j$                  d
v r7|s5t	        j&                  |«      j(                  }t        j*                  ||«      }|S )NÚflash_attention_2r3  Úflex_attentionr   r1  )r¤  Úpast_key_values_lengthÚis_trainingr&   r=   )Úsequence_lengthÚtarget_lengthr?   r®   r.  r—   )ÚcudaÚxpu)rf   r5  ÚanyrY  r2   r�   r+   r*   r¬  r   r   Ú_ignore_causal_mask_sdpar  r?   r®   rK   Úget_max_cache_shapeÚ5_prepare_4d_causal_attention_mask_with_cache_positionrz  Úfinforå   Ú_unmask_unattended)r6   r  r¶  r.  rT  r2  r±  Úusing_static_cacher?   r®   r¼  r½  r   Ú	min_dtypes                 r:   r®  z!AriaTextModel._update_causal_maskÕ  sÖ  € ð �;‰;×+Ñ+Ð/BÒBØÐ)¨~ÀÑ/D×.IÑ.IÔ.KØ%Ð%ØØ�;‰;×+Ñ+Ð/?Ò?Ü˜.¬%¯,©,Ô7Ü!<¸^Ó!L�Ü˜.¬)Ô4Ø%Ð%ð
 @OÐ?Z˜?×9Ñ9Ô;Ð`aÐÜ'¨¼ÓEÐð �;‰;×+Ñ+¨vÒ5Ñ>PÑYjÜ%×>Ñ>ØØ*Ø'7Ø ŸM™Mõ	ð à$×*Ñ*¨L×,?Ñ,?ˆvˆØ&×,Ñ,¨QÑ/ˆÙØ+×?Ñ?ÓA‰Mô ˜n¬e¯l©lÔ;ð ×$Ñ$ RÒ(à%¨Ñ7¸!Ñ;ð ð ×PÑPØØ+Ø'ØØØ)Ø#×)Ñ)¨!Ñ,ð Qó 
ˆð �K‰K×,Ñ,°Ò6ØÐ*Ø×%Ñ%×*Ñ*¨oÑ=Ù%ô
 Ÿ™ EÓ*×.Ñ.ˆIÜ0×CÑCÀKÐQZÓ[ˆKàÐr;   r¼  r½  r?   r®   r—   c                 ó˜  — | �| j                  «       dk(  r| }|S t        j                  |«      j                  }	t        j                  ||f|	||¬«      }|dk7  rt        j
                  |d¬«      }|t        j                  ||¬«      |j                  dd«      kD  z  }|dddd…dd…f   j                  |ddd«      }| �Œ|j                  «       }| j                  d   }
|dd…dd…dd…d|
…f   | dd…dddd…f   j                  |j                  «      z   }|dk(  }|dd…dd…dd…d|
…f   j                  ||	«      |dd…dd…dd…d|
…f<   |S )	a°  
        Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape
        `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing.

        Args:
            attention_mask (`torch.Tensor`):
                A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape
                `(batch_size, 1, query_length, key_value_length)`.
            sequence_length (`int`):
                The sequence length being processed.
            target_length (`int`):
                The target length: when generating with static cache, the mask should be as long as the static cache,
                to account for the 0 padding, the part of the cache that is not filled yet.
            dtype (`torch.dtype`):
                The dtype to use for the 4D attention mask.
            device (`torch.device`):
                The device to place the 4D attention mask on.
            cache_position (`torch.Tensor`):
                Indices depicting the position of the input sequence tokens in the sequence.
            batch_size (`torch.Tensor`):
                Batch size.
        Né   )Ú
fill_valuer?   r®   r&   )Údiagonalr¨  r=   r   )r°   r2   rÄ  rå   ÚfullÚtriur­  r  r•   ÚclonerK   r@   r®   Úmasked_fill)r  r¼  r½  r?   r®   r.  r—   r  r   rÇ  Úmask_lengthÚpadding_masks               r:   rÃ  zCAriaTextModel._prepare_4d_causal_attention_mask_with_cache_position  sy  € ðB Ð%¨.×*<Ñ*<Ó*>À!Ò*Cà(ˆKð* Ðô' Ÿ™ EÓ*×.Ñ.ˆIÜŸ*™*Ø  -Ð0¸YÈeÐ\bôˆKð  !Ò#Ü#Ÿj™j¨¸qÔA�Øœ5Ÿ<™<¨¸fÔEÈ×H^ÑH^Ð_aÐcdÓHeÑeÑeˆKØ% d¨D²!²QÐ&6Ñ7×>Ñ>¸zÈ1ÈbÐRTÓUˆKØÐ)Ø)×/Ñ/Ó1�Ø,×2Ñ2°2Ñ6�Ø*ª1ªa²°L°[°LÐ+@ÑAÀNÒSTÐVZÐ\`ÒbcÐScÑDd×DgÑDgØ×&Ñ&óEñ  �ð  ,¨qÑ0�Ø5@ÂÂAÂqÈ,È;È,ÐAVÑ5W×5cÑ5cØ  )ó6�šAšq¢! \ k \Ð1Ñ2ð Ðr;   )	NNNNNNNNN)F)rN   rO   rP   rc   r(   r0   rŸ  r¢  r   r   ÚARIA_TEXT_INPUTS_DOCSTRINGr   r2   r?  r�   r   rP  rO  r   r   r   rH   r®  Ústaticmethodr>  r?   r®   rÃ  rQ   rR   s   @r:   r‘  r‘  F  sÖ  ø„ ñ
ð˜~õ ò !ò"ð Ù*Ð+EÓFð 15Ø15Ø37Ø+/Ø59Ø$(Ø,0Ø/3Ø59ñi
à˜E×,Ñ,Ñ-ði
ð ! §¡Ñ.ði
ð ˜u×/Ñ/Ñ0ð	i
ð
 " %™ði
ð   × 1Ñ 1Ñ2ði
ð ˜D‘>ði
ð $ D™>ði
ð ' t™nði
ð ! ×!1Ñ!1Ñ2ði
ð $Ð$8Ñ9ði
ð 
!òi
ó Gó ði
ðb #(ñDàŸ™ðDð —l‘lðDð Ÿ™ð	Dð
 ðDð  óDðL ð7ØŸ™ð7àð7ð ð7ð �{‰{ð	7ð
 —‘ð7ð Ÿ™ð7ð ò7ó ô7r;   r‘  c                   ó   — e Zd Zy)ÚKwargsForCausalLMN)rN   rO   rP   rK  r;   r:   rÕ  rÕ  V  s   … r;   rÕ  c                   óð  ‡ — e Zd ZdZdgZddiZddgdgfiZeZdefˆ fd„Z	d	„ Z
d
„ Zd„ Zd„ Zd„ Zd„ Ze eddd¬«       ee«       eee¬«      	 	 	 	 	 	 	 	 	 	 	 d!deej2                     deej4                     deej2                     dee   deej8                     deej2                     dee   dee   dee   deej2                     deeej4                  f   de e!   defd „«       «       «       «       Z"ˆ xZ#S )"ÚAriaTextForCausalLMa7  
    Aria model for causal language modeling tasks.

    This class extends `LlamaForCausalLM` to incorporate the Mixture of Experts (MoE) approach,
    allowing for more efficient and scalable language modeling.

    Args:
        config (`AriaTextConfig`):
            Configuration object for the model.
    zlm_head.weightÚlm_headÚcolwise_reprE   rò   rf   c                 óè   •— t         ‰| �  |«       t        |«      | _        |j                  | _        t        j                  |j                  |j                  d¬«      | _        | j                  «        y rÞ   )
r/   r0   r‘  rS  r”  r)   rY   r7   rØ  rœ  r�   s     €r:   r0   zAriaTextForCausalLM.__init__j  sU   ø€ Ü‰Ñ˜Ô Ü" 6Ó*ˆŒ
Ø ×+Ñ+ˆŒÜ—y‘y ×!3Ñ!3°V×5FÑ5FÈUÔSˆŒð 	�‰Õr;   c                 ó.   — | j                   j                  S ra   ©rS  r•  rL   s    r:   rŸ  z(AriaTextForCausalLM.get_input_embeddingss  s   € Ø�z‰z×&Ñ&Ð&r;   c                 ó&   — || j                   _        y ra   rÜ  r¡  s     r:   r¢  z(AriaTextForCausalLM.set_input_embeddingsv  s   € Ø"'ˆ�
‰
Õr;   c                 ó   — | j                   S ra   ©rØ  rL   s    r:   Úget_output_embeddingsz)AriaTextForCausalLM.get_output_embeddingsy  s   € Ø�|‰|Ðr;   c                 ó   — || _         y ra   rß  ©r6   Únew_embeddingss     r:   Úset_output_embeddingsz)AriaTextForCausalLM.set_output_embeddings|  s	   € Ø%ˆ�r;   c                 ó   — || _         y ra   ©rS  ©r6   Údecoders     r:   Úset_decoderzAriaTextForCausalLM.set_decoder  s	   € Øˆ�
r;   c                 ó   — | j                   S ra   ræ  rL   s    r:   Úget_decoderzAriaTextForCausalLM.get_decoder‚  s   € Ø�z‰zÐr;   Únum_logits_to_keepú4.50Úlogits_to_keep©ÚversionÚnew_name©Úoutput_typerc  r£  r  r  rT  r¤  ÚlabelsrI  r2  r¥  r.  r  rÍ   c                 óæ  — |�|n| j                   j                  }|	�|	n| j                   j                  }	 | j                  d||||||||	|
dœ	|¤Ž}|j                  }t        |t        «      rt        | d«      n|}| j                  |dd…|dd…f   «      }d}|�* | j                  d||| j                   j                  dœ|¤Ž}t        |||j                  |j                  |j                  ¬«      S )a>  
            labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
                Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
                config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
                (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

            logits_to_keep (`int` or `torch.Tensor`, *optional*):
                If an `int`, compute logits for the last `logits_to_keep` tokens. If `0`, calculate logits for all
                `input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
                token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
                If a `torch.Tensor`, must be 1D corresponding to the indices to keep in the sequence length dimension.
                This is useful when using packed tensor format (single dimension for batch and sequence length).

        Returns:

        Example:

        ```python
        >>> from transformers import AutoTokenizer, AriaTextForCausalLM

        >>> model = AriaTextForCausalLM.from_pretrained("meta-aria_text/AriaText-2-7b-hf")
        >>> tokenizer = AutoTokenizer.from_pretrained("meta-aria_text/AriaText-2-7b-hf")

        >>> prompt = "Hey, are you conscious? Can you talk to me?"
        >>> inputs = tokenizer(prompt, return_tensors="pt")

        >>> # Generate
        >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
        >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
        "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
        ```N)	r£  r  r  rT  r¤  rI  r2  r¥  r.  ©rò   rô  r”  ©Úlossrò   rT  rE   rª  rK  )rf   r2  r¥  rS  r©  rY  r>  ÚslicerØ  Úloss_functionr”  r   rT  rE   rª  )r6   r£  r  r  rT  r¤  rô  rI  r2  r¥  r.  rî  r  rN  rE   Úslice_indicesrò   rø  s                     r:   rH   zAriaTextForCausalLM.forward…  s  € ðd 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð
 ,6¨4¯:©:ð ,
ØØ)Ø%Ø+Ø'ØØ/Ø!5Ø)ñ,
ð ñ,
ˆð  ×1Ñ1ˆä8BÀ>ÔSVÔ8Wœ˜~˜o¨tÔ4Ð]kˆØ—‘˜mªA¨}ºaÐ,?Ñ@ÓAˆàˆØÐØ%�4×%Ñ%Ðp¨V¸FÈtÏ{É{×OeÑOeÑpÐioÑpˆDä%ØØØ#×3Ñ3Ø!×/Ñ/Ø×)Ñ)ô
ð 	
r;   )NNNNNNNNNNr   )$rN   rO   rP   rc   Ú_tied_weights_keysÚ_tp_planÚ_pp_planr(   rc  r0   rŸ  r¢  rà  rä  ré  rë  r   r!   r   rÒ  r    r   Ú_CONFIG_FOR_DOCr   r2   r?  r�   r   rP  rO  r	   r>  r   rÕ  rH   rQ   rR   s   @r:   r×  r×  Y  s¿  ø„ ñ	ð +Ð+ÐØ˜=Ð)€HØ˜_Ð-°¨zÐ:Ð;€HØ!€Lð˜~õ ò'ò(òò&òòð ÙÐ)°6ÐDTÔUÙ*Ð+EÓFÙÐ+AÐP_Ô`ð 15Ø15Ø37Ø+/Ø59Ø-1Ø$(Ø,0Ø/3Ø59Ø34ñP
à˜E×,Ñ,Ñ-ðP
ð ! §¡Ñ.ðP
ð ˜u×/Ñ/Ñ0ð	P
ð
 " %™ðP
ð   × 1Ñ 1Ñ2ðP
ð ˜×)Ñ)Ñ*ðP
ð ˜D‘>ðP
ð $ D™>ðP
ð ' t™nðP
ð ! ×!1Ñ!1Ñ2ðP
ð ˜c 5§<¡<Ð/Ñ0ðP
ð Ð*Ñ+ðP
ð 
 òP
ó aó Gó Vó ôP
r;   r×  c                   ó  — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eeej                        ed<   dZeeej                        ed<   dZeeej                        ed<   dZeej                     ed<   y)	ÚAriaCausalLMOutputWithPasta›  
    Base class for Aria causal language model (or autoregressive) outputs.

    Args:
        loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
            Language modeling loss (for next-token prediction).
        logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
            Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
        past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
            Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
            `(batch_size, num_heads, sequence_length, embed_size_per_head)`)

            Contains pre-computed hidden-states (key and values in the self-attention blocks) that can be used (see
            `past_key_values` input) to speed up sequential decoding.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
            one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.

            Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
        attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`.

            Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
            heads.
        image_hidden_states (`torch.FloatTensor`, *optional*):
            A `torch.FloatTensor` of size (batch_size, num_images, sequence_length, hidden_size)`.
            image_hidden_states of the model produced by the vision encoder and after projecting the last hidden state.
    Nrø  rò   rT  rE   rª  Úimage_hidden_states)rN   rO   rP   rc   rø  r   r2   rP  Ú__annotations__rò   rT  r   rE   r   rª  r  rK  r;   r:   r  r  Ü  s—   … ñð< )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø*.€FˆH�U×&Ñ&Ñ'Ó.Ø9=€O�X˜d 5×#4Ñ#4Ñ5Ñ6Ó=Ø8<€M�8˜E %×"3Ñ"3Ñ4Ñ5Ó<Ø59€J�˜˜u×0Ñ0Ñ1Ñ2Ó9Ø7;Ð˜ %×"3Ñ"3Ñ4Ô;r;   r  a4  
    Args:
        input_ids (`torch.LongTensor`, *optional*):
            Input token IDs.
        pixel_values (`torch.FloatTensor`, *optional*):
            Pixel values of the images.
        pixel_mask (`torch.LongTensor`, *optional*):
            Mask for the pixel values.
        attention_mask (`torch.Tensor`, *optional*):
            Attention mask.
        position_ids (`torch.LongTensor`, *optional*):
            Position IDs.
        past_key_values (`List[torch.FloatTensor]`, *optional*):
            Past key values for efficient processing.
        inputs_embeds (`torch.FloatTensor`, *optional*):
            Input embeddings.
        labels (`torch.LongTensor`, *optional*):
            Labels for computing the language modeling loss.
        use_cache (`bool`, *optional*):
            Whether to use the model's cache mechanism.
        output_attentions (`bool`, *optional*):
            Whether to output attention weights.
        output_hidden_states (`bool`, *optional*):
            Whether to output hidden states.
        return_dict (`bool`, *optional*):
            Whether to return a `ModelOutput` object.
        logits_to_keep (`int` or `torch.Tensor`, *optional*, defaults to 0):
            If an `int`, calculate logits for the last `logits_to_keep` tokens, or all `input_ids` if `0`.
            Otherwise, slice according to the 1D tensor in the sequence length dimension
        cache_position (`torch.LongTensor`, *optional*):
            Cache positions.
        **loss_kwargs:
            Additional keyword arguments for loss calculation.
aG  
    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

    This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
    Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
    and behavior.

    Parameters:
        config (`AriaConfig`):
            Model configuration class with all the parameters of the model. Initializing with a config file does not
            load the weights associated with the model, only the configuration. Check out the
            [`~PreTrainedModel.from_pretrained`] method to load the model weights.
zÁAria model for conditional generation tasks.

    This model combines a vision tower, a multi-modal projector, and a language model
    to perform tasks that involve both image and text inputs.c            !       ó”  ‡ — e Zd ZeZdZdZdZdgZdefˆ fd„Z	d„ Z
d„ Zd„ Zd„ Zd	„ Zd
„ Zd„ Z	 	 d"dej$                  deej$                     defd„Ze eddd¬«       ee«       eee¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 d#deej8                     deej$                     deej8                     deej:                     deej8                     deeej$                        deej$                     deej8                     dee   dee   dee   de eej:                  f   deej8                     defd „«       «       «       «       Z!	 	 	 	 	 	 	 d$d!„Z"ˆ xZ#S )%ÚAriaForConditionalGenerationFzlanguage_model.lm_head.weightrf   c                 óÌ  •— t         ‰| �  |«       t        j                  |j                  «      | _        t        |«      | _        |j                  j                  | _	        t        j                  |j                  «      | _        | j                  j                  �| j                  j                  nd| _        |j                  j                  dk(  | _        | j!                  «        y )Nr=   r¸  )r/   r0   r$   Úfrom_configrj   Úvision_towerrƒ   Úmulti_modal_projectorrˆ   r”  r%   Úlanguage_modelrf   r“  r5  Ú_use_flash_attention_2rœ  r�   s     €r:   r0   z%AriaForConditionalGeneration.__init__F  sª   ø€ Ü‰Ñ˜Ô ä%×1Ñ1°&×2FÑ2FÓGˆÔÜ%2°6Ó%:ˆÔ"Ø ×,Ñ,×7Ñ7ˆŒÜ2×>Ñ>¸v×?QÑ?QÓRˆÔØ8<¿¹×8PÑ8PÐ8\˜DŸK™K×4Ò4ÐbdˆÔØ&,×&8Ñ&8×&MÑ&MÐQdÑ&dˆÔ#Ø�‰Õr;   c                 óŽ  — |€y |j                  d| j                  j                  j                  | j                  j                  j                  ¬«      }|j                  d| j                  j                  j                  | j                  j                  j                  ¬«      }|j	                  d¬«      dkD  j                  «       S )Nr&   )Ú	dimensionr–   Ústepr#   )r=   r  r¯   r   )Úunfoldr  rf   Ú
patch_sizerð   rO  )r6   Ú
pixel_maskÚpatches_subgrids      r:   Ú_create_patch_attention_maskz9AriaForConditionalGeneration._create_patch_attention_maskQ  sµ   € ØÐØà$×+Ñ+ØØ×"Ñ"×)Ñ)×4Ñ4Ø×"Ñ"×)Ñ)×4Ñ4ð ,ó 
ˆð
 *×0Ñ0ØØ×"Ñ"×)Ñ)×4Ñ4Ø×"Ñ"×)Ñ)×4Ñ4ð 1ó 
ˆð
  ×#Ñ#¨Ð#Ó1°AÑ5×;Ñ;Ó=Ð=r;   c                 ó6   — | j                   j                  «       S ra   )r
  rŸ  rL   s    r:   rŸ  z1AriaForConditionalGeneration.get_input_embeddingsa  s   € Ø×"Ñ"×7Ñ7Ó9Ð9r;   c                 ó:   — | j                   j                  |«       y ra   )r
  r¢  r¡  s     r:   r¢  z1AriaForConditionalGeneration.set_input_embeddingsd  s   € Ø×Ñ×0Ñ0°Õ7r;   c                 ó6   — | j                   j                  «       S ra   )r
  rà  rL   s    r:   rà  z2AriaForConditionalGeneration.get_output_embeddingsg  s   € Ø×"Ñ"×8Ñ8Ó:Ð:r;   c                 ó:   — | j                   j                  |«       y ra   )r
  rä  râ  s     r:   rä  z2AriaForConditionalGeneration.set_output_embeddingsj  s   € Ø×Ñ×1Ñ1°.ÕAr;   c                 ó:   — | j                   j                  |«       y ra   )r
  ré  rç  s     r:   ré  z(AriaForConditionalGeneration.set_decoderm  s   € Ø×Ñ×'Ñ'¨Õ0r;   c                 ó6   — | j                   j                  «       S ra   )r
  rë  rL   s    r:   rë  z(AriaForConditionalGeneration.get_decoderp  s   € Ø×"Ñ"×.Ñ.Ó0Ð0r;   Úpixel_valuesr  Úvision_feature_layerc                 óè   — | j                  |«      }| j                  ||d¬«      }d }|�&|j                  d«      }t        j                  |«      }|j
                  |   }| j                  ||¬«      }	|	S )NT)Úpatch_attention_maskr¥  r&   ry   )r  r  rì   r2   Úlogical_notrE   r	  )
r6   r  r  r  r  Úimage_outputsÚimage_attn_maskÚflattened_maskÚselected_image_featureÚimage_featuress
             r:   Úget_image_featuresz/AriaForConditionalGeneration.get_image_featuress  s�   € ð  $×@Ñ@ÀÓLÐØ×)Ñ)ØÐ/CÐZ^ð *ó 
ˆð ˆØÐ+Ø1×9Ñ9¸!Ó<ˆNÜ#×/Ñ/°Ó?ˆOà!.×!<Ñ!<Ð=QÑ!RÐØ×3Ñ3Ð4JÐVeÐ3ÓfˆØÐr;   rì  rí  rî  rï  rò  r£  r  r  rT  r¤  rô  rI  r2  r¥  r.  rÍ   c                 óþ  — |
�|
n| j                   j                  }
|�|n| j                   j                  }|€ | j                  «       |«      }|��£|j                  d   dk7  �r�|€�| | j                  «       t        j                  | j                   j                  t
        j                  |j                  ¬«      «      k(  }|j                  d¬«      j                  d¬«      d   }nt|| j                   j                  k(  }|j                  d«      j                  |«      j                  |j                  «      }|j                  d¬«      j                  d¬«      }| j                  ||| j                   j                  ¬«      }|j                  d   |j                  d   }}||z  }||k7  rt!        d|› d	|› �«      ‚|j                  |j                  |j"                  «      }|j%                  ||«      }| j'                  |||||	|
|||¬
«	      }|j(                  }d}|�4 | j*                  d||| j                   j,                  j.                  dœ|¤Ž}t1        |||j2                  |j4                  |j6                  ¬«      S )aŠ  
            labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
                Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
                config.vocab_size]` or `model.image_token_id` (where `model` is your instance of `Idefics3ForConditionalGeneration`).
                Tokens with indices set to `model.image_token_id` are ignored (masked), the loss is only
                computed for the tokens with labels in `[0, ..., config.vocab_size]`.
        Returns:

        Example:

        ```python
        >>> import requests
        >>> import torch
        >>> from PIL import Image
        >>> from io import BytesIO

        >>> from transformers import AutoProcessor, AutoModel
        >>> from transformers.image_utils import load_image

        >>> # Note that passing the image urls (instead of the actual pil images) to the processor is also possible
        >>> image1 = load_image("https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg")
        >>> image2 = load_image("https://cdn.britannica.com/59/94459-050-DBA42467/Skyline-Chicago.jpg")
        >>> image3 = load_image("https://cdn.britannica.com/68/170868-050-8DDE8263/Golden-Gate-Bridge-San-Francisco.jpg")

        >>> processor = AutoProcessor.from_pretrained("Rhymes-AI/Aria")
        >>> model = AutoModel.from_pretrained("Rhymes-AI/Aria", torch_dtype=torch.bfloat16, device_map="auto")

        >>> # Create inputs
        >>> messages = [
        ...     {
        ...         "role": "user",
        ...         "content": [
        ...             {"type": "image"},
        ...             {"type": "text", "text": "In this image, we can see the city of New York, and more specifically the Statue of Liberty."},
        ...             {"type": "image"},
        ...             {"type": "text", "text": "What can we see in this image?"},
        ...         ]
        ...     },
        ...     {
        ...         "role": "user",
        ...         "content": [
        ...             {"type": "image"},
        ...             {"type": "text", "text": "In which city is that bridge located?"},
        ...         ]
        ...     }
        ... ]

        >>> prompts = [processor.apply_chat_template([message], add_generation_prompt=True) for message in messages]
        >>> images = [[image1, image2], [image3]]
        >>> inputs = processor(text=prompts, images=images, padding=True, return_tensors="pt").to(model.device)

        >>> # Generate
        >>> generated_ids = model.generate(**inputs, max_new_tokens=256)
        >>> generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)

        >>> print(generated_texts[0])
        Assistant: There are buildings, trees, lights, and water visible in this image.

        >>> print(generated_texts[1])
        Assistant: The bridge is in San Francisco.
        ```Nr&   r­   r¯   r   r=   )r  r  r  z6Image features and image tokens do not match: tokens: z, features )	r  r  rT  r¤  rI  r2  r¥  rî  r.  rö  r÷  rK  )rf   r2  r¥  rŸ  rK   r2   ÚtensorÚimage_token_indexr²   r®   rð   r’   Ú	expand_asr@   r$  r  r«  r?   Úmasked_scatterr
  rò   rú  rˆ   r”  r  rT  rE   rª  )r6   r£  r  r  r  r  rT  r¤  rô  rI  r2  r¥  rî  r.  Úloss_kwargsÚspecial_image_maskÚn_image_tokensÚimage_embedsr#  Ún_imagesÚn_features_per_imageÚn_image_featuresrN  rò   rø  s                            r:   rH   z$AriaForConditionalGeneration.forward†  s¦  € ðd 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð Ð Ø7˜D×5Ñ5Ó7¸	ÓBˆMð Ñ#¨×(;Ñ(;¸AÑ(>À!Ó(CØÐ Ø%2Ð6Q°d×6OÑ6OÓ6QÜ—L‘L §¡×!>Ñ!>ÄeÇjÁjÐYf×YmÑYmÔnó7ñ &Ð"ð #5×!9Ñ!9¸aÐ!9Ó!@×!DÑ!DÈÐ!DÓ!KÈAÑ!N‘à(¨D¯K©K×,IÑ,IÑI�Ø%1×%;Ñ%;¸BÓ%?×%IÑ%IÈ-Ó%X×%[Ñ%[Ð\i×\pÑ\pÓ%qÐ"Ø".×!3Ñ!3¸Ð!3Ó!:×!>Ñ!>À1Ð!>Ó!E�Ø!×4Ñ4Ø)Ø%Ø%)§[¡[×%EÑ%Eð 5ó ˆNð
 .<×-AÑ-AÀ!Ñ-DÀn×FZÑFZÐ[\ÑF]Ð*ˆHØ'Ð*>Ñ>ÐØÐ!1Ò1Ü ØLÈ^ÐL\Ð\gÐhxÐgyÐzóð ð ,×.Ñ.¨}×/CÑ/CÀ]×EXÑEXÓYˆNØ)×8Ñ8Ð9KÈ^Ó\ˆMà*.×*=Ñ*=Ø)Ø%Ø+Ø'ØØ/Ø!5Ø)Ø)ð +>ó 
+
ˆð —‘ˆàˆØÐØ%�4×%Ñ%ð Ø f¸¿¹×9PÑ9P×9[Ñ9[ñØ_jñˆDô *ØØØ#×3Ñ3Ø!×/Ñ/Ø×)Ñ)ô
ð 	
r;   c	           	      óp   —  | j                   j                  |f|||||dœ|	¤Ž}
|d   dk(  r
||
d<   ||
d<   |
S )N)rT  r¤  r  r.  rî  r   r  r  )r
  Úprepare_inputs_for_generation)r6   r£  rT  r¤  r  r  r  r.  rî  r  Úmodel_inputss              r:   r2  z:AriaForConditionalGeneration.prepare_inputs_for_generation  si   € ð I�t×*Ñ*×HÑHØð
à+Ø'Ø)Ø)Ø)ñ
ð ñ
ˆð ˜!Ñ Ò!ð ,8ˆL˜Ñ(Ø)3ˆL˜Ñ&àÐr;   )Nr=   )NNNNNNNNNNNr   N)NNNNNNN)$rN   rO   rP   r'   rc  rh  rq  ri  rü  r0   r  rŸ  r¢  rà  rä  ré  rë  r2   rP  r   r>  r$  r   r!   r   ÚARIA_INPUTS_DOCSTRINGr    r  r?  r�   r   rO  r	   rH   r2  rQ   rR   s   @r:   r  r  8  s7  ø„ ð €LØ"ÐØÐØ€NØ9Ð:Ðð	˜zõ 	ò>ò :ò8ò;òBò1ò1ð 37Ø$&ñ	à×'Ñ'ðð ˜U×.Ñ.Ñ/ðð "ó	ð& ÙÐ)°6ÐDTÔUÙ*Ð+@ÓAÙÐ+EÐT^Ô_ð 15Ø48Ø15Ø15Ø37Ø=AØ59Ø-1Ø$(Ø,0Ø/3Ø34Ø59ñJ
à˜E×,Ñ,Ñ-ðJ
ð ˜u×0Ñ0Ñ1ðJ
ð ˜U×-Ñ-Ñ.ð	J
ð
 ! §¡Ñ.ðJ
ð ˜u×/Ñ/Ñ0ðJ
ð " $ u×'8Ñ'8Ñ"9Ñ:ðJ
ð   × 1Ñ 1Ñ2ðJ
ð ˜×)Ñ)Ñ*ðJ
ð ˜D‘>ðJ
ð $ D™>ðJ
ð ' t™nðJ
ð ˜c 5§<¡<Ð/Ñ0ðJ
ð ! ×!1Ñ!1Ñ2ðJ
ð  
$ò!J
ó `ó Bó Vó ðJ
ð^ ØØØØØØ÷r;   r  )r  rl  rR  r‘  r×  )Nr&   )r3  )^Údataclassesr   Ú	functoolsr   Útypingr   r   r   r   r	   Úactivationsr   Úcache_utilsr   r   r   Ú
generationr   Úmodeling_attn_mask_utilsr   Úmodeling_flash_attention_utilsr   Úmodeling_outputsr   r   r   Úmodeling_rope_utilsr   r   Úmodeling_utilsr   r   Úprocessing_utilsr   Úutilsr   r   r   r   r   r   r    Úutils.deprecationr!   Úutils.import_utilsr"   Úautor$   r%   Úconfiguration_ariar'   r(   r2   r)   Ú!torch.nn.attention.flex_attentionr*   Úintegrations.flex_attentionr+   Ú
get_loggerrN   r7  rÿ  ÚModuler-   rT   re   rƒ   rŸ   rÂ   rÄ   rÌ   rÜ   r   r	  r�   r>  r  r�   r!  r#  rA  rR  ÚARIA_TEXT_START_DOCSTRINGrl  rv  rÒ  r‘  rÕ  r×  r  r4  ÚARIA_START_DOCSTRINGr  Ú__all__rK  r;   r:   ú<module>rM     sÓ  ðõ* "Ý ß 9Õ 9å !ß ;Ñ ;Ý )Ý >Ý Bß \Ñ \ß Kß FÝ &÷÷ ñ õ 1Ý 4ß 2ß :ñ ÔÛÝñ  Ô!Ý;åJð 
ˆ×	Ñ	˜HÓ	%€Ø"€ôJ�b—i‘iô Jô(�r—y‘yô ô24˜Ÿ™ô 4ôn>�B—I‘Iô >ôB˜2Ÿ9™9ô ò4ô>)
˜RŸY™Yô )
ôX˜BŸI™Iô ôDL-�r—y‘yô L-ò^(óð6	U˜UŸ\™\ð 	U°#ð 	U¸%¿,¹,ó 	Uð& ñ%Ø�I‰Ið%à�<‰<ð%ð 
�‰ð%ð �<‰<ð	%ð
 ˜UŸ\™\Ñ*ð%ð ð%ð ó%ô4I)˜Ÿ	™	ô I)ôX>˜2Ÿ9™9ô >ôB)˜oô )ð@Ð ñ" ØXØóô9˜/ó 9ó	ð9ô8<˜bŸi™iô <ðD@Ð ñF Ø\ØóôIÐ+ó Ió	ðIôX ?Ð,¨jÔ >ô@
Ð1°?ô @
ðF ô$< ó $<ó ð$<ðN!Ð ðFÐ ñ" ðAð óôsÐ#6¸ó sóðsòl�r;   