Ë
    T^(h[°  ã                   óÊ  — d dl mZ d dlmZ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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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*m+Z+ ddl,m-Z- ddl.m/Z/  e)«       rd dl0m1Z1 ddl2m3Z3  e*jh                  e5«      Z6dZ7dZ8d„ Z9d=d„Z:dejv                  de<dejv                  fd„Z=	 d>de	j|                  dejv                  dejv                  dejv                  d eejv                     d!e?d"e?fd#„Z@ G d$„ d%e	j|                  «      ZA G d&„ d'e	j|                  «      ZB G d(„ d)e	j|                  «      ZC G d*„ d+e	j|                  «      ZDd,ZE e&d-eE«       G d.„ d/e «      «       ZFd0ZG e&d-eE«       G d1„ d2eF«      «       ZH G d3„ d4ee$«      ZI G d5„ d6eFe«      ZJ e&d7eE«       G d8„ d9eF«      «       ZK e&d:eE«       G d;„ d<eF«      «       ZLy)?é    )Úpartial)ÚCallableÚOptionalÚTupleÚUnionNé   )ÚACT2FN)ÚCacheÚDynamicCacheÚStaticCache)ÚGenerationMixin)ÚAttentionMaskConverter)ÚFlashAttentionKwargs)ÚBaseModelOutputWithPastÚCausalLMOutputWithPastÚ SequenceClassifierOutputWithPastÚTokenClassifierOutput)ÚROPE_INIT_FUNCTIONSÚdynamic_rope_update)ÚALL_ATTENTION_FUNCTIONSÚPreTrainedModel)ÚUnpack)Ú
LossKwargsÚadd_code_sample_docstringsÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚcan_return_tupleÚis_torch_flex_attn_availableÚloggingÚreplace_return_docstrings)Údeprecate_kwargé   )Ú	PhiConfig)Ú	BlockMask)Úmake_flex_block_causal_maskzmicrosoft/phi-1r#   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..Néÿÿÿÿé   ©Údim)ÚshapeÚtorchÚcat)ÚxÚx1Úx2s      úb/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/phi/modeling_phi.pyÚrotate_halfr2   5   sZ   € à	
ˆ3Ð"�!—'‘'˜"‘+ Ñ"Ð"Ð"Ñ	#€BØ	
ˆ3�—‘˜‘˜qÑ Ñ"Ð"Ñ	#€BÜ�9‰9�r�c˜2�Y BÔ'Ð'ó    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.
    )Ú	unsqueezer2   )ÚqÚkÚcosÚsinÚposition_idsÚunsqueeze_dimÚq_embedÚk_embeds           r1   Úapply_rotary_pos_embr>   <   sY   € ð( �-‰-˜Ó
&€CØ
�-‰-˜Ó
&€CØ�3‰wœ; q›>¨CÑ/Ñ0€GØ�3‰wœ; q›>¨CÑ/Ñ0€GØ�GÐÐr3   Úhidden_statesÚn_repÚreturnc                 óª   — | 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)r+   ÚexpandÚreshape)r?   r@   ÚbatchÚnum_key_value_headsÚslenÚhead_dims         r1   Ú	repeat_kvrI   W   so   € ð
 2?×1DÑ1DÑ.€EÐ  hØ�‚zØÐØ!¢!¢Q¨ªa²Ð"2Ñ3×:Ñ:¸5ÐBUÐW\Ð^bÐdlÓm€MØ× Ñ  Ð(;¸eÑ(CÀTÈ8ÓTÐTr3   ÚmoduleÚqueryÚkeyÚvalueÚattention_maskÚscalingÚdropoutc                 ó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*   Údtype)ÚpÚtrainingr"   )rI   Únum_key_value_groupsr,   ÚmatmulÚ	transposer+   ÚnnÚ
functionalÚsoftmaxÚfloat32ÚtorS   rP   rU   Ú
contiguous)rJ   rK   rL   rM   rN   rO   rP   ÚkwargsÚ
key_statesÚvalue_statesÚattn_weightsÚcausal_maskÚattn_outputs                r1   Úeager_attention_forwardre   c   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à˜Ð$Ð$r3   c                   ó,  ‡ — 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j                  e
ej                     e
e	ej                        f   fd„Zˆ xZS )ÚPhiAttentionz=Multi-headed attention from 'Attention Is All You Need' paperÚconfigÚ	layer_idxc                 ó–  •— t         ‰| �  «        || _        || _        t	        |d|j
                  |j                  z  «      | _        |j                  |j                  z  | _	        | j                  dz  | _
        |j                  | _        d| _        t        j                  |j
                  |j                  | j                  z  d¬«      | _        t        j                  |j
                  |j                  | j                  z  d¬«      | _        t        j                  |j
                  |j                  | j                  z  d¬«      | _        t        j                  |j                  | j                  z  |j
                  d¬«      | _        t'        | j                  |j(                  z  «      | _        |j,                  | _        | j,                  r}t        j.                  |j
                  |j                  z  |j0                  d¬«      | _        t        j.                  |j
                  |j                  z  |j0                  d¬«      | _        y y )NrH   g      à¿T©Úbias)ÚepsÚelementwise_affine)ÚsuperÚ__init__rh   ri   ÚgetattrÚhidden_sizeÚnum_attention_headsrH   rF   rV   rO   Úattention_dropoutÚ	is_causalrY   ÚLinearÚq_projÚk_projÚv_projÚdenseÚintÚpartial_rotary_factorÚrotary_ndimsÚqk_layernormÚ	LayerNormÚlayer_norm_epsÚq_layernormÚk_layernorm©Úselfrh   ri   Ú	__class__s      €r1   rp   zPhiAttention.__init__€   sÆ  ø€ Ü‰ÑÔØˆŒØ"ˆŒÜ ¨
°F×4FÑ4FÈ&×JdÑJdÑ4dÓeˆŒØ$*×$>Ñ$>À&×B\ÑB\Ñ$\ˆÔ!Ø—}‘} dÑ*ˆŒØ!'×!9Ñ!9ˆÔØˆŒÜ—i‘i × 2Ñ 2°F×4NÑ4NÐQU×Q^ÑQ^Ñ4^ÐeiÔjˆŒÜ—i‘i × 2Ñ 2°F×4NÑ4NÐQU×Q^ÑQ^Ñ4^ÐeiÔjˆŒÜ—i‘i × 2Ñ 2°F×4NÑ4NÐQU×Q^ÑQ^Ñ4^ÐeiÔjˆŒÜ—Y‘Y˜v×9Ñ9¸D¿M¹MÑIÈ6×K]ÑK]ÐdhÔiˆŒ
Ü §¡°×0LÑ0LÑ LÓMˆÔØ"×/Ñ/ˆÔØ×ÒÜ!Ÿ|™|Ø×"Ñ" f×&@Ñ&@Ñ@Àf×F[ÑF[Ðptô ˆDÔô  "Ÿ|™|Ø×"Ñ" f×&@Ñ&@Ñ@Àf×F[ÑF[Ðptô ˆDÕð	 r3   r?   Úposition_embeddingsrN   Úpast_key_valueÚcache_positionrA   c                 ó  — |j                   d d }g |¢d‘| j                  ‘­}| j                  |«      j                  |«      j	                  dd«      }	| j                  |«      j                  |«      j	                  dd«      }
| j                  |«      j                  |«      j	                  dd«      }| j                  r"| j                  |	«      }	| j                  |
«      }
|\  }}|	dd | j                  …f   |	d| j                  d …f   }}|
dd | j                  …f   |
d| j                  d …f   }}t        ||||«      \  }}t        j                  ||fd¬«      }	t        j                  ||fd¬«      }
|�'|||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| j0                  | j2                  dœ|¤Ž\  }} |j4                  g |¢d‘­Ž j7                  «       }| j9                  |«      }||fS )Nr'   r"   r(   .r)   )r9   r8   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.ç        )rP   rO   )r+   rH   rw   ÚviewrX   rx   ry   r~   r�   r‚   r}   r>   r,   r-   Úupdateri   re   rh   Ú_attn_implementationÚgetÚloggerÚwarning_oncer   rU   rt   rO   rD   r^   rz   )r„   r?   r†   rN   r‡   rˆ   r_   Úinput_shapeÚhidden_shapeÚquery_statesr`   ra   r8   r9   Ú	query_rotÚ
query_passÚkey_rotÚkey_passÚcache_kwargsÚattention_interfacerd   rb   s                         r1   ÚforwardzPhiAttention.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ˆà×ÒØ×+Ñ+¨LÓ9ˆLØ×)Ñ)¨*Ó5ˆJà&‰ˆˆSð ˜Ð1 × 1Ñ 1Ð1Ð1Ñ2Ø˜˜d×/Ñ/Ñ1Ð1Ñ2ð ˆ	ð
 �sÐ/˜d×/Ñ/Ð/Ð/Ñ0Ø�s˜D×-Ñ-Ñ/Ð/Ñ0ð ˆô
 2°)¸WÀcÈ3ÓOÑˆ	�7ô —y‘y )¨ZÐ!8¸bÔAˆÜ—Y‘Y ¨Ð2¸Ô;ˆ
àÐ%à#&¨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ˆØ—j‘j Ó-ˆØ˜LÐ(Ð(r3   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   r{   rp   r,   ÚTensorr   r   r
   Ú
LongTensorr�   Ú__classcell__©r…   s   @r1   rg   rg   }   s¼   ø„ ÙGð˜yð °Sõ ð8 +/Ø59ñA)à—|‘|ðA)ð # 5§<¡<°·±Ð#=Ñ>ðA)ð ! §¡Ñ.ð	A)ð
 ! ™ðA)ð ! ×!1Ñ!1Ñ2ðA)ð 
ˆu�|‰|˜X e§l¡lÑ3°X¸eÀEÇLÁLÑ>QÑ5RÐRÑ	S÷A)r3   rg   c                   óV   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚPhiMLPc                 ó  •— t         ‰| �  «        || _        t        |j                     | _        t        j                  |j                  |j                  «      | _
        t        j                  |j                  |j                  «      | _        y ©N)ro   rp   rh   r	   Ú
hidden_actÚactivation_fnrY   rv   rr   Úintermediate_sizeÚfc1Úfc2©r„   rh   r…   s     €r1   rp   zPhiMLP.__init__Ü   sd   ø€ Ü‰ÑÔØˆŒÜ# F×$5Ñ$5Ñ6ˆÔÜ—9‘9˜V×/Ñ/°×1IÑ1IÓJˆŒÜ—9‘9˜V×5Ñ5°v×7IÑ7IÓJˆ�r3   r?   rA   c                 ól   — | j                  |«      }| j                  |«      }| j                  |«      }|S r©   )r­   r«   r®   )r„   r?   s     r1   r�   zPhiMLP.forwardã   s4   € ØŸ™ Ó/ˆØ×*Ñ*¨=Ó9ˆØŸ™ Ó/ˆØÐr3   )rž   rŸ   r    rp   r,   r¢   r�   r¤   r¥   s   @r1   r§   r§   Û   s$   ø„ ôKð U§\¡\ð °e·l±l÷ r3   r§   c                   ó€  ‡ — e 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
ej                        dee   d	ee   d
eej                     dee
ej                  ej                  f      de
ej                  ee
ej                  ej                  f      f   fd„Zˆ xZS )ÚPhiDecoderLayerrh   ri   c                 ó  •— t         ‰| �  «        t        ||¬«      | _        t	        |«      | _        t        j                  |j                  |j                  ¬«      | _
        t        j                  |j                  «      | _        y )N)ri   ©rm   )ro   rp   rg   Ú	self_attnr§   ÚmlprY   r   rr   r€   Úinput_layernormÚDropoutÚresid_pdropÚresid_dropoutrƒ   s      €r1   rp   zPhiDecoderLayer.__init__ë   s]   ø€ Ü‰ÑÔÜ% f¸	ÔBˆŒÜ˜&“>ˆŒÜ!Ÿ|™|¨F×,>Ñ,>ÀF×DYÑDYÔZˆÔÜŸZ™Z¨×(:Ñ(:Ó;ˆÕr3   r?   rN   r:   r‡   rŒ   Ú	use_cacherˆ   r†   rA   c	                 óð   — |}
| j                  |«      } | j                  d||||||||dœ|	¤Ž\  }}| j                  |«      }| j                  | j                  |«      «      }||z   |
z   }|f}|r||fz  }|S )N)r?   rN   r:   r‡   rŒ   r»   rˆ   r†   © )r·   rµ   rº   r¶   )r„   r?   rN   r:   r‡   rŒ   r»   rˆ   r†   r_   ÚresidualÚattn_outputsÚself_attn_weightsÚfeed_forward_hidden_statesÚoutputss                  r1   r�   zPhiDecoderLayer.forwardò   s²   € ð !ˆà×,Ñ,¨]Ó;ˆð +9¨$¯.©.ð 
+
Ø'Ø)Ø%Ø)Ø/ØØ)Ø 3ñ
+
ð ñ
+
Ñ'ˆÐ'ð ×)Ñ)¨,Ó7ˆà%)×%7Ñ%7¸¿¹ÀÓ8OÓ%PÐ"Ø$Ð'AÑAÀHÑLˆØ Ð"ˆáØÐ)Ð+Ñ+ˆGàˆr3   )NNNFFNN)rž   rŸ   r    r#   r{   rp   r,   r¢   r   r£   r   ÚboolÚFloatTensorr�   r¤   r¥   s   @r1   r²   r²   ê   s  ø„ ð<˜yð <°Sõ <ð 26Ø37Ø8<Ø,1Ø$)Ø59ØKOñ%à—|‘|ð%ð ! §¡Ñ.ð%ð ˜u×/Ñ/Ñ0ð	%ð
 !  u§|¡|Ñ!4Ñ5ð%ð $ D™>ð%ð ˜D‘>ð%ð ! ×!1Ñ!1Ñ2ð%ð & e¨E¯L©L¸%¿,¹,Ð,FÑ&GÑHð%ð 
ˆu× Ñ  (¨5°×1BÑ1BÀE×DUÑDUÐ1UÑ+VÑ"WÐWÑ	X÷%r3   r²   c                   ó^   ‡ — e Zd Zddefˆ fd„Z ej                  «       ed„ «       «       Zˆ xZ	S )ÚPhiRotaryEmbeddingrh   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)ro   rp   ÚhasattrrÈ   r‘   rÉ   Úmax_position_embeddingsÚmax_seq_len_cachedÚoriginal_max_seq_lenrh   r   Úrope_init_fnÚattention_scalingÚregister_bufferrÌ   Úoriginal_inv_freq)r„   rh   ÚdevicerÌ   r…   s       €r1   rp   zPhiRotaryEmbedding.__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Ø!%§¡ˆÕr3   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"   ÚmpsÚcpuF)Údevice_typeÚenabledr(   r)   )rS   )rÌ   ÚfloatrC   r+   r]   rÖ   Ú
isinstancerÊ   Ústrr,   ÚautocastrX   r-   r8   rÓ   r9   rS   )
r„   r.   r:   Úinv_freq_expandedÚposition_ids_expandedrÚ   ÚfreqsÚembr8   r9   s
             r1   r�   zPhiRotaryEmbedding.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.r©   )
rž   rŸ   r    r#   rp   r,   Úno_gradr   r�   r¤   r¥   s   @r1   rÆ   rÆ     s3   ø„ ñ/˜yõ /ð" €U‡]�]ƒ_Øñ<ó ó ô<r3   rÆ   aH  
    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 ([`PhiConfig`]):
            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.
zQThe bare Phi 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)ÚPhiPreTrainedModelÚmodelTr²   Úpast_key_valuesc                 ó  — | 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 y )Nr�   )ÚmeanÚstd)rh   Úinitializer_rangerÝ   rY   rv   ÚweightÚdataÚnormal_rl   Úzero_Ú	EmbeddingÚpadding_idx)r„   rJ   rë   s      r1   Ú_init_weightsz PhiPreTrainedModel._init_weights_  sÃ   € Ø�k‰k×+Ñ+ˆÜ�fœbŸi™iÔ(Ø�M‰M×Ñ×&Ñ&¨C°SÐ&Ô9Ø�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ä˜¤§¡Ô-Ø�M‰M×Ñ×&Ñ&¨C°SÐ&Ô9Ø×!Ñ!Ð-Ø—‘×"Ñ" 6×#5Ñ#5Ñ6×<Ñ<Õ>ð .ð .r3   N)rž   rŸ   r    r#   Úconfig_classÚbase_model_prefixÚsupports_gradient_checkpointingÚ_no_split_modulesÚ_skip_keys_device_placementÚ_supports_flash_attn_2Ú_supports_sdpaÚ_supports_flex_attnÚ_supports_cache_classÚ_supports_quantized_cacheÚ_supports_static_cacheÚ_supports_attention_backendró   r½   r3   r1   ræ   ræ   M  sU   „ ð
 €LØÐØ&*Ð#Ø*Ð+ÐØ#4Ð"5ÐØ!ÐØ€NØÐØ ÐØ $ÐØ!ÐØ"&Ðó	?r3   ræ   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.
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 )ÚPhiModelz—
    Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`PhiDecoderLayer`]

    Args:
        config: PhiConfig
    rh   c           	      óh  •— t         ‰| �  |«       |j                  | _        |j                  | _        t        j                  |j                  |j                  | j                  «      | _        t        j                  t        |j                  «      D �cg c]  }t        ||«      ‘Œ c}«      | _        t        |¬«      | _        d| _        t        j"                  |j$                  «      | _        t        j(                  |j                  |j*                  ¬«      | _        | j/                  «        y c c}w )N)rh   Fr´   )ro   rp   Úpad_token_idrò   Ú
vocab_sizerY   rñ   rr   Úembed_tokensÚ
ModuleListÚrangeÚnum_hidden_layersr²   ÚlayersrÆ   Ú
rotary_embÚgradient_checkpointingr¸   Ú
embd_pdropÚembed_dropoutr   r€   Úfinal_layernormÚ	post_initrƒ   s      €r1   rp   zPhiModel.__init__º  sá   ø€ Ü‰Ñ˜Ô Ø!×.Ñ.ˆÔØ ×+Ñ+ˆŒäŸL™L¨×):Ñ):¸F×<NÑ<NÐPT×P`ÑP`ÓaˆÔÜ—m‘mÜAFÀv×G_ÑG_ÓA`Öa°IŒ_˜V YÕ/Òaó
ˆŒô -°FÔ;ˆŒØ&+ˆÔ#ÜŸZ™Z¨×(9Ñ(9Ó:ˆÔÜ!Ÿ|™|¨F×,>Ñ,>ÀF×DYÑDYÔZˆÔð 	�‰Õùò bs   ÂD/c                 ó   — | j                   S r©   ©r  ©r„   s    r1   Úget_input_embeddingszPhiModel.get_input_embeddingsË  s   € Ø× Ñ Ð r3   c                 ó   — || _         y r©   r  ©r„   rM   s     r1   Úset_input_embeddingszPhiModel.set_input_embeddingsÎ  s
   € Ø!ˆÕr3   Ú	input_idsrN   r:   rè   Úinputs_embedsr»   rŒ   Úoutput_hidden_statesrˆ   Úflash_attn_kwargsrA   c
                 ó4  — |�|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}|€| j                  |«      }|r|€
t        «       }|	€F|�|j                  «       nd}t        j                  |||j                  d   z   |j                  ¬«      }	|€|	j!                  d«      }| j#                  |||	||«      }| j%                  |«      }|}| j'                  ||«      }|rdnd }|rdnd }| j(                  d | j                   j*                   D ]r  }|r||fz  }| j
                  r:| j                  r.| j-                  t/        |j0                  fi |
¤Ž|||||||	|«	      }n ||f||||||	|dœ|
¤Ž}|d   }|sŒj||d   fz  }Œt | j3                  |«      }|r||fz  }t5        ||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`.Fr   r"   ©rÖ   r½   )rN   r:   r‡   rŒ   r»   rˆ   r†   )Úlast_hidden_staterè   r?   Ú
attentions)rh   rŒ   r  r»   Ú
ValueErrorr  rU   r’   r“   r  r   Úget_seq_lengthr,   Úaranger+   rÖ   r5   Ú_update_causal_maskr  r
  r	  r  Ú_gradient_checkpointing_funcr   Ú__call__r  r   )r„   r  rN   r:   rè   r  r»   rŒ   r  rˆ   r  Úpast_seen_tokensrc   r?   r†   Úall_hidden_statesÚall_self_attnsÚdecoder_layerÚlayer_outputss                      r1   r�   zPhiModel.forwardÑ  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àÐ Ø ×-Ñ-¨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]ó
ˆð ×*Ñ*¨=Ó9ˆØ%ˆð #Ÿ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 ×,Ñ,¨]Ó;ˆñ  Ø -Ð!1Ñ1Ðä&Ø+Ù/8™O¸dØ+Ø%ô	
ð 	
r3   Ú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_2r�   Úflex_attentionr   r‹   )r  Úpast_key_values_lengthÚis_trainingr"   r'   )Úsequence_lengthÚtarget_lengthrS   rÖ   rˆ   Ú
batch_size)ÚcudaÚxpu)rh   r�   ÚanyrÝ   r,   r¢   r%   r$   r   r   r   Ú_ignore_causal_mask_sdparU   rS   rÖ   r+   Úget_max_cache_shapeÚ5_prepare_4d_causal_attention_mask_with_cache_positionrÊ   ÚfinfoÚminÚ_unmask_unattended)r„   rN   r*  rˆ   rè   rŒ   r%  Úusing_static_cacherS   rÖ   r0  r1  rc   Ú	min_dtypes                 r1   r"  zPhiModel._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àÐr3   r0  r1  rS   rÖ   r2  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_valuerS   rÖ   r"   )Údiagonalr  r'   r   )r*   r,   r9  r:  ÚfullÚtriur!  rD   rC   Úcloner+   r]   rÖ   Úmasked_fill)rN   r0  r1  rS   rÖ   rˆ   r2  r_   rc   r=  Úmask_lengthÚpadding_masks               r1   r8  z>PhiModel._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ð Ðr3   ©	NNNNNNNNN)F)rž   rŸ   r    r¡   r#   rp   r  r  r   r   ÚPHI_INPUTS_DOCSTRINGr   r,   r£   r¢   r
   rÄ   rÃ   r   r   r   r�   r"  Ústaticmethodr{   rS   rÖ   r8  r¤   r¥   s   @r1   r  r  ®  sÖ  ø„ ñ
ð˜yõ ò"!ò"ð Ù*Ð+?Ó@ð 15Ø15Ø37Ø+/Ø59Ø$(Ø,0Ø/3Ø59ñf
à˜E×,Ñ,Ñ-ðf
ð ! §¡Ñ.ðf
ð ˜u×/Ñ/Ñ0ð	f
ð
 " %™ðf
ð   × 1Ñ 1Ñ2ðf
ð ˜D‘>ðf
ð $ D™>ðf
ð ' t™nðf
ð ! ×!1Ñ!1Ñ2ðf
ð $Ð$8Ñ9ðf
ð 
!òf
ó Aó ðf
ð\ #(ñDàŸ™ðDð —l‘lðDð Ÿ™ð	Dð
 ðDð  óDðL ð7ØŸ™ð7àð7ð ð7ð �{‰{ð	7ð
 —‘ð7ð Ÿ™ð7ð ò7ó ô7r3   r  c                   ó   — e Zd Zy)ÚKwargsForCausalLMN)rž   rŸ   r    r½   r3   r1   rL  rL  ¼  s   … r3   rL  c                   óâ  ‡ — e Zd ZdgZddiZddgdgfiZˆ 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j,                     deej.                     deej,                     dee   deej2                     deej,                     dee   dee   dee   deej,                     deeej.                  f   dee   defd„«       «       «       «       Zˆ xZ S ) ÚPhiForCausalLMzlm_head.weightÚlm_headÚcolwise_repr?   Úlogitsc                 óè   •— t         ‰| �  |«       t        |«      | _        |j                  | _        t        j                  |j                  |j                  d¬«      | _        | j                  «        y )NTrk   )
ro   rp   r  rç   r  rY   rv   rr   rO  r  r¯   s     €r1   rp   zPhiForCausalLM.__init__Ä  sU   ø€ Ü‰Ñ˜Ô Ü˜fÓ%ˆŒ
Ø ×+Ñ+ˆŒÜ—y‘y ×!3Ñ!3°V×5FÑ5FÈTÔRˆŒð 	�‰Õr3   c                 ó.   — | j                   j                  S r©   ©rç   r  r  s    r1   r  z#PhiForCausalLM.get_input_embeddingsÍ  ó   € Ø�z‰z×&Ñ&Ð&r3   c                 ó&   — || j                   _        y r©   rT  r  s     r1   r  z#PhiForCausalLM.set_input_embeddingsÐ  ó   € Ø"'ˆ�
‰
Õr3   c                 ó   — | j                   S r©   ©rO  r  s    r1   Úget_output_embeddingsz$PhiForCausalLM.get_output_embeddingsÓ  s   € Ø�|‰|Ðr3   c                 ó   — || _         y r©   rY  )r„   Únew_embeddingss     r1   Úset_output_embeddingsz$PhiForCausalLM.set_output_embeddingsÖ  s	   € Ø%ˆ�r3   c                 ó   — || _         y r©   ©rç   )r„   Údecoders     r1   Úset_decoderzPhiForCausalLM.set_decoderÙ  s	   € Øˆ�
r3   c                 ó   — | j                   S r©   r_  r  s    r1   Úget_decoderzPhiForCausalLM.get_decoderÜ  s   € Ø�z‰zÐr3   Únum_logits_to_keepz4.50Úlogits_to_keep)ÚversionÚnew_name)Úoutput_typerô   r  rN   r:   rè   r  Úlabelsr»   rŒ   r  rˆ   r_   rA   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, PhiForCausalLM

        >>> model = PhiForCausalLM.from_pretrained("meta-phi/Phi-2-7b-hf")
        >>> tokenizer = AutoTokenizer.from_pretrained("meta-phi/Phi-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  rN   r:   rè   r  r»   rŒ   r  rˆ   )rQ  ri  r  ©ÚlossrQ  rè   r?   r  r½   )rh   rŒ   r  rç   r  rÝ   r{   ÚslicerO  Úloss_functionr  r   rè   r?   r  )r„   r  rN   r:   rè   r  ri  r»   rŒ   r  rˆ   re  r_   rÂ   r?   Úslice_indicesrQ  rl  s                     r1   r�   zPhiForCausalLM.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Ø!×/Ñ/Ø×)Ñ)ô
ð 	
r3   )NNNNNNNNNNr   )!rž   rŸ   r    Ú_tied_weights_keysÚ_tp_planÚ_pp_planrp   r  r  rZ  r]  ra  rc  r   r!   r   rI  r    r   Ú_CONFIG_FOR_DOCr   r,   r£   r¢   r
   rÄ   rÃ   r   r{   r   rL  r�   r¤   r¥   s   @r1   rN  rN  ¿  s¬  ø„ Ø*Ð+ÐØ˜=Ð)€HØ˜_Ð-°¨zÐ:Ð;€Hôò'ò(òò&òòð ÙÐ)°6ÐDTÔUÙ*Ð+?Ó@ÙÐ+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ó Aó Vó ôP
r3   rN  aÒ  
    The Phi Model transformer with a sequence classification head on top (linear layer).

    [`PhiForSequenceClassification`] uses the last token in order to do the classification, as other causal models
    (e.g. GPT-2) do.

    Since it does classification on the last token, it requires to know the position of the last token. If a
    `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
    no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
    padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
    each row of the batch).
    c                   ó(  ‡ — e Zd Zˆ 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
j                     d
e	e   de	e   de	e   defd„«       «       Zˆ xZS )ÚPhiForSequenceClassificationc                 óè   •— t         ‰| �  |«       |j                  | _        t        |«      | _        t        j                  |j                  | j                  d¬«      | _        | j                  «        y )NFrk   )
ro   rp   Ú
num_labelsr  rç   rY   rv   rr   Úscorer  r¯   s     €r1   rp   z%PhiForSequenceClassification.__init__F  sS   ø€ Ü‰Ñ˜Ô Ø ×+Ñ+ˆŒÜ˜fÓ%ˆŒ
Ü—Y‘Y˜v×1Ñ1°4·?±?ÈÔOˆŒ
ð 	�‰Õr3   c                 ó.   — | j                   j                  S r©   rT  r  s    r1   r  z1PhiForSequenceClassification.get_input_embeddingsO  rU  r3   c                 ó&   — || j                   _        y r©   rT  r  s     r1   r  z1PhiForSequenceClassification.set_input_embeddingsR  rW  r3   r  rN   r:   rè   r  ri  r»   rŒ   r  rA   c
           
      ó¢  — | j                  ||||||||	¬«      }
|
j                  }| j                  |«      }|�|j                  d   }n|j                  d   }| j                  j
                  €|dk7  rt        d«      ‚| j                  j
                  €d}nÃ|�“|| j                  j
                  k7  j                  |j                  t        j                  «      }t        j                  |j                  d   |j                  t        j                  ¬«      }||z  j                  d«      }n.d}t        j                  | j                  j                   › d�«       |t        j                  ||j                  ¬	«      |f   }d}|�| j#                  |||| j                  ¬
«      }t%        |||
j&                  |
j(                  |
j*                  ¬«      S )á�  
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        ©rN   r:   rè   r  r»   rŒ   r  Nr   r"   z=Cannot handle batch sizes > 1 if no padding token is defined.r'   )rÖ   rS   zŠ will not detect padding tokens in `inputs_embeds`. Results may be unexpected if using padding tokens in conjunction with `inputs_embeds.`r  )rQ  ri  Úpooled_logitsrh   rk  )rç   r  rx  r+   rh   r  r  r]   rÖ   r,   Úint32r!  Úargmaxr’   r“   r…   rž   rn  r   rè   r?   r  )r„   r  rN   r:   rè   r  ri  r»   rŒ   r  Útransformer_outputsr?   rQ  r2  Úlast_non_pad_tokenÚnon_pad_maskÚtoken_indicesr~  rl  s                      r1   r�   z$PhiForSequenceClassification.forwardU  sÁ  € ð* 8<·z±zØØ)Ø%Ø+Ø'ØØ/Ø!5ð 8Bó 	8
Ðð ,×=Ñ=ˆØ—‘˜MÓ*ˆàÐ Ø"Ÿ™¨Ñ+‰Jà&×,Ñ,¨QÑ/ˆJà�;‰;×#Ñ#Ð+°
¸a²ÜÐ\Ó]Ð]Ø�;‰;×#Ñ#Ð+Ø!#ÑØÐ"à%¨¯©×)AÑ)AÑA×EÑEÀfÇmÁmÔUZ×U`ÑU`ÓaˆLÜ!ŸL™L¨¯©¸Ñ)<ÀVÇ]Á]ÔZ_×ZeÑZeÔfˆMØ"/°,Ñ">×!FÑ!FÀrÓ!JÑà!#ÐÜ×ÑØ—>‘>×*Ñ*Ð+ð ,Zð Zôð
 œuŸ|™|¨J¸v¿}¹}ÔMÐOaÐaÑbˆàˆØÐØ×%Ñ%¨V¸FÐR_Ðhl×hsÑhsÐ%ÓtˆDä/ØØ Ø/×?Ñ?Ø-×;Ñ;Ø*×5Ñ5ô
ð 	
r3   rH  )rž   rŸ   r    rp   r  r  r   r   rI  r   r,   r£   r¢   r
   rÄ   rÃ   r   r�   r¤   r¥   s   @r1   ru  ru  6  s  ø„ ô ò'ò(ð Ù*Ð+?Ó@ð 15Ø15Ø37Ø+/Ø59Ø-1Ø$(Ø,0Ø/3ñA
à˜E×,Ñ,Ñ-ðA
ð ! §¡Ñ.ðA
ð ˜u×/Ñ/Ñ0ð	A
ð
 " %™ðA
ð   × 1Ñ 1Ñ2ðA
ð ˜×)Ñ)Ñ*ðA
ð ˜D‘>ðA
ð $ D™>ðA
ð ' t™nðA
ð 
*òA
ó Aó ôA
r3   ru  z²
    The Phi Model transformer with a token classification head on top (a linear layer on top of the hidden-states
    output) e.g. for Named-Entity-Recognition (NER) tasks.
    c                   óD  ‡ — e Zd Zˆ fd„Zd„ Zd„ Ze ee«       e	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j                     dee   dee   dee   defd„«       «       «       Zˆ xZS )ÚPhiForTokenClassificationc                 ó„  •— t         ‰| �  |«       |j                  | _        t        |«      | _        t        |dd «      �|j                  }nt        |dd «      �|j                  }nd}t        j                  |«      | _
        t        j                  |j                  |j                  «      | _        | j                  «        y )NÚclassifier_dropoutÚhidden_dropoutgš™™™™™¹?)ro   rp   rw  r  rç   rq   rˆ  r‰  rY   r¸   rP   rv   rr   rx  r  )r„   rh   rˆ  r…   s      €r1   rp   z"PhiForTokenClassification.__init__£  s¢   ø€ Ü‰Ñ˜Ô Ø ×+Ñ+ˆŒÜ˜fÓ%ˆŒ
Ü�6Ð/°Ó6ÐBØ!'×!:Ñ!:ÑÜ�VÐ-¨tÓ4Ð@Ø!'×!6Ñ!6Ñà!$ÐÜ—z‘zÐ"4Ó5ˆŒÜ—Y‘Y˜v×1Ñ1°6×3DÑ3DÓEˆŒ
ð 	�‰Õr3   c                 ó.   — | j                   j                  S r©   rT  r  s    r1   r  z.PhiForTokenClassification.get_input_embeddings³  rU  r3   c                 ó&   — || j                   _        y r©   rT  r  s     r1   r  z.PhiForTokenClassification.set_input_embeddings¶  rW  r3   )Ú
checkpointrh  rô   r  rN   r:   rè   r  ri  r»   rŒ   r  rA   c
           
      ó  — | j                  ||||||||	¬«      }
|
j                  }| j                  |«      }| j                  |«      }d}|�| j	                  ||| j
                  «      }t        |||
j                  |
j                  ¬«      S )r|  r}  N)rl  rQ  r?   r  )	rç   r  rP   rx  rn  rh   r   r?   r  )r„   r  rN   r:   rè   r  ri  r»   rŒ   r  rÂ   Úsequence_outputrQ  rl  s                 r1   r�   z!PhiForTokenClassification.forward¹  s    € ð4 ,0¯:©:ØØ)Ø%Ø+Ø'ØØ/Ø!5ð ,6ó 	,
ˆð "×3Ñ3ˆØŸ,™, Ó7ˆØ—‘˜OÓ,ˆàˆØÐØ×%Ñ% f¨f°d·k±kÓBˆDä$ØØØ!×/Ñ/Ø×)Ñ)ô	
ð 	
r3   rH  )rž   rŸ   r    rp   r  r  r   r   rI  r   Ú_CHECKPOINT_FOR_DOCr   rs  r   r,   r£   r¢   r
   rÄ   rÃ   r�   r¤   r¥   s   @r1   r†  r†  ›  s  ø„ ôò 'ò(ð Ù*Ð+?Ó@ÙØ&Ø)Ø$ôð 15Ø15Ø37Ø+/Ø59Ø-1Ø$(Ø,0Ø/3ñ*
à˜E×,Ñ,Ñ-ð*
ð ! §¡Ñ.ð*
ð ˜u×/Ñ/Ñ0ð	*
ð
 " %™ð*
ð   × 1Ñ 1Ñ2ð*
ð ˜×)Ñ)Ñ*ð*
ð ˜D‘>ð*
ð $ D™>ð*
ð ' t™nð*
ð 
ò*
óó Aó ô*
r3   r†  )Nr"   )r�   )MÚ	functoolsr   Útypingr   r   r   r   r,   Útorch.nnrY   Úactivationsr	   Úcache_utilsr
   r   r   Ú
generationr   Úmodeling_attn_mask_utilsr   Úmodeling_flash_attention_utilsr   Úmodeling_outputsr   r   r   r   Úmodeling_rope_utilsr   r   Úmodeling_utilsr   r   Úprocessing_utilsr   Úutilsr   r   r   r   r   r   r   r    Úutils.deprecationr!   Úconfiguration_phir#   Ú!torch.nn.attention.flex_attentionr$   Úintegrations.flex_attentionr%   Ú
get_loggerrž   r’   r�  rs  r2   r>   r¢   r{   rI   ÚModulerÜ   re   rg   r§   r²   rÆ   ÚPHI_START_DOCSTRINGræ   rI  r  rL  rN  ru  r†  r½   r3   r1   ú<module>r¤     s;  ðõ ß 3Ó 3ã Ý å !ß ;Ñ ;Ý )Ý >Ý B÷ó ÷ Lß FÝ &÷	÷ 	ó 	õ 1Ý (ñ  Ô!Ý;åJð 
ˆ×	Ñ	˜HÓ	%€à'Ð Ø€ò(óð6	U˜UŸ\™\ð 	U°#ð 	U¸%¿,¹,ó 	Uð& ñ%Ø�I‰Ið%à�<‰<ð%ð 
�‰ð%ð �<‰<ð	%ð
 ˜UŸ\™\Ñ*ð%ð ð%ð ó%ô4[)�2—9‘9ô [)ô|ˆR�Y‰Yô ô-�b—i‘iô -ô`<˜Ÿ™ô <ðDÐ ñ" ØWØóô?˜ó ?ó	ð?ð4@Ð ñF ØWØóôGÐ!ó Gó	ðGôT ?Ð,¨jÔ >ôt
Ð'¨ô t
ñn ðð óôS
Ð#5ó S
óðS
ñl ðð óôH
Ð 2ó H
óñH
r3   