
    cCi                     4   S r SSKrSSKJrJr  SSKrSSKJr  SSKJr  SSK	J
r
Jr  SSKJr  SS	KJr  SS
KJrJr  SSKJrJrJr  SSKJrJr  SSKJrJr  SSKJr  SSKJ r J!r!J"r"J#r#  SSK$J%r%  SSK&J'r'  \"" 5       (       a  SSK(J)r)  SSK*J+r+  \" 5       (       a  SSKJ,r,  \#RZ                  " \.5      r/ " S S\R`                  5      r1S r2S6S jr3 " S S\R`                  5      r4 " S S\R`                  5      r5S\Rl                  S\7S \Rl                  4S! jr8 " S" S#\R`                  5      r9 " S$ S%\95      r: " S& S'\95      r;\9\:\;S(.r< " S) S*\5      r=\  " S+ S,\5      5       r>\  " S- S.\>5      5       r? " S/ S0\>\5      r@ " S1 S2\\>5      rA " S3 S4\\>5      rB/ S5QrCg)7zPyTorch StableLM model.    N)OptionalUnion)nn   )ACT2FN)CacheDynamicCache)GenerationMixin)AttentionMaskConverter)!flash_attn_supports_top_left_maskis_flash_attn_available) GenericForSequenceClassificationGenericForTokenClassificationGradientCheckpointingLayer)BaseModelOutputWithPastCausalLMOutputWithPast)ROPE_INIT_FUNCTIONSdynamic_rope_update)PreTrainedModel)auto_docstringcan_return_tupleis_torch_flex_attn_availablelogging)deprecate_kwarg   )StableLmConfig)	BlockMask)make_flex_block_causal_mask)_flash_attention_forwardc                      ^  \ rS rSr% \R
                  \S'   SS\4U 4S jjjr\R                  " 5       \
S 5       5       rSrU =r$ )StableLmRotaryEmbedding?   inv_freqconfigc                   > [         TU ]  5         [        US5      (       aZ  [        UR                  [
        5      (       a;  UR                  R                  SUR                  R                  S5      5      U l        OSU l        UR                  U l	        UR                  U l
        Xl        [        U R                     U l        U R                  U R                  U5      u  o0l        U R                  SUSS9  U R                   U l        g )Nrope_scaling	rope_typetypedefaultr#   F)
persistent)super__init__hasattr
isinstancer&   dictgetr'   max_position_embeddingsmax_seq_len_cachedoriginal_max_seq_lenr$   r   rope_init_fnattention_scalingregister_bufferr#   original_inv_freq)selfr$   devicer#   	__class__s       h/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/stablelm/modeling_stablelm.pyr,    StableLmRotaryEmbedding.__init__B   s    6>**z&:M:Mt/T/T#0044[&BUBUBYBYZ`BabDN&DN"("@"@$*$B$B!/?+/+<+<T[[&+Q((ZeD!%    c                 b   U R                   S S S 2S 4   R                  5       R                  UR                  S   SS5      R	                  UR
                  5      nUS S 2S S S 24   R                  5       n[        UR
                  R                  [        5      (       a0  UR
                  R                  S:w  a  UR
                  R                  OSn[        R                  " USS9   UR                  5       UR                  5       -  R                  SS5      n[        R                  " Xf4SS	9nUR                  5       U R                  -  nUR                  5       U R                  -  n	S S S 5        WR	                  UR                   S
9W	R	                  UR                   S
94$ ! , (       d  f       N@= f)Nr   r   mpscpuF)device_typeenabled   dim)dtype)r#   floatexpandshapetor9   r.   r(   strtorchautocast	transposecatcosr5   sinrG   )
r8   xposition_idsinv_freq_expandedposition_ids_expandedrB   freqsembrQ   rR   s
             r;   forwardStableLmRotaryEmbedding.forwardS   sR    !MM$4-8>>@GGHZHZ[\H]_acdehhijiqiqr ,QaZ 8 > > @'1!((--'E'E!((--[`J`ahhmmfk^^UC&,,.1F1L1L1NNYYZ[]^_E))UN3C'')d444C'')d444C	 D vvAGGv$cff177f&;;; DCs   $BF  
F.)r5   r$   r2   r7   r3   r4   r'   N)__name__
__module____qualname____firstlineno__rM   Tensor__annotations__r   r,   no_gradr   rY   __static_attributes____classcell__r:   s   @r;   r!   r!   ?   s@    ll/~ / /" ]]_<  <r=   r!   c                     U SSU R                   S   S-  24   nU SU R                   S   S-  S24   n[        R                  " U* U4SS9$ )z*Rotates half the hidden dims of the input..Nr?   rD   rE   )rJ   rM   rP   )rS   x1x2s      r;   rotate_halfri   d   sZ    	
3"!''"+"""	#B	
3q ""	#B99rc2YB''r=   c                     UR                  U5      nUR                  U5      nX-  [        U 5      U-  -   nX-  [        U5      U-  -   nXg4$ )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.
)	unsqueezeri   )qkrQ   rR   rT   unsqueeze_dimq_embedk_embeds           r;   apply_rotary_pos_embrq   l   sS    ( --
&C
--
&Cw;q>C/0Gw;q>C/0Gr=   c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )StableLmMLP   c                   > [         TU ]  5         Xl        UR                  U l        UR                  U l        [
        R                  " U R                  U R                  SS9U l        [
        R                  " U R                  U R                  SS9U l        [
        R                  " U R                  U R                  SS9U l	        [        UR                     U l        g NFbias)r+   r,   r$   hidden_sizeintermediate_sizer   Linear	gate_projup_proj	down_projr   
hidden_actact_fnr8   r$   r:   s     r;   r,   StableLmMLP.__init__   s    !--!'!9!94#3#3T5K5KRWXyy!1!143I3IPUV4#9#94;K;KRWXV../r=   c                     U R                  U R                  U R                  U5      5      U R                  U5      -  5      nU$ r[   )r~   r   r|   r}   )r8   rS   r~   s      r;   rY   StableLmMLP.forward   s6    NN4;;t~~a/@#ADLLQRO#ST	r=   )r   r$   r~   r|   ry   rz   r}   )r\   r]   r^   r_   r,   rY   rc   rd   re   s   @r;   rs   rs      s    0 r=   rs   c                   N   ^  \ rS rSrSU 4S jjrS\R                  4S jrSrU =r	$ )StableLmLayerNormPerHead   c                    > [         TU ]  5         Xl        X l        [        R
                  " [        U R                  5       Vs/ s H  n[        R                  " XUS9PM     sn5      U l        g s  snf )N)epsrx   )	r+   r,   rF   	num_headsr   
ModuleListrange	LayerNormnorms)r8   rF   r   r   rx   _r:   s         r;   r,   !StableLmLayerNormPerHead.__init__   sT    "]]SXY]YgYgSh#iShaBLLD$ISh#ij
#is   A/hidden_statesc           	          [         R                  " USSS9n[         R                  " [        U R                  U5       VVs/ s H  u  p1U" U5      PM     snnSS9$ s  snnf )Nr   rE   )rM   splitrP   zipr   )r8   r   states_per_headsnorms       r;   rY    StableLmLayerNormPerHead.forward   sQ     !;;}aQ?yyTZZYiIjkIj2E$$}-Ijkqrssks    A
)rF   r   r   )gh㈵>F)
r\   r]   r^   r_   r,   rM   r`   rY   rc   rd   re   s   @r;   r   r      s!    ktU\\ t tr=   r   r   n_repreturnc                     U R                   u  p#pEUS:X  a  U $ U SS2SS2SSS2SS24   R                  X#XU5      n U R                  X#U-  XE5      $ )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)rJ   rI   reshape)r   r   batchnum_key_value_headsslenhead_dims         r;   	repeat_kvr      s_    
 2?1D1D.Ez!!Qa"23::5W\dlmM  e(CTTTr=   c                     ^  \ rS rSrSrSS\S\\   4U 4S jjjr\	" SSSS	9       SS
\
R                  S\\
R                     S\\
R                     S\\   S\S\S\\
R                     S\\\
R                  \
R                  4      S\\
R                  \\
R                     \\\
R                        4   4S jj5       rSrU =r$ )StableLmAttention   z=Multi-headed attention from 'Attention Is All You Need' paperr$   	layer_idxc                   > [         TU ]  5         Xl        X l        Uc-  [        R                  SU R                  R                   S35        UR                  U l        UR                  U l
        U R                  U R                  -  U l        UR                  U l        U R                  U R                  -  U l        UR                  U l        [        U R                  UR                   -  5      U l        SU l        U R                  U R                  -  U R                  :w  a&  ['        SU R                   SU R                   S35      e[(        R*                  " U R                  U R                  U R                  -  UR,                  S9U l        [(        R*                  " U R                  U R                  U R                  -  UR,                  S9U l        [(        R*                  " U R                  U R                  U R                  -  UR,                  S9U l        [(        R*                  " U R                  U R                  SS9U l        UR6                  U l        U R6                  (       a\  [9        U R                  U R                  UR:                  S	9U l        [9        U R                  U R                  UR:                  S	9U l        [(        R@                  " URB                  5      U l!        [E        U R                  S
9U l#        g )NzInstantiating z without passing a `layer_idx` is not recommended and will lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` when creating this class.Tz?hidden_size must be divisible by num_heads (got `hidden_size`: z and `num_heads`: z).rw   Fr   r$   )$r+   r,   r$   r   loggerwarning_oncer:   r\   ry   num_attention_headsr   r   r   num_key_value_groups
rope_thetaintpartial_rotary_factorrotary_ndims	is_causal
ValueErrorr   r{   use_qkv_biasq_projk_projv_projo_projqk_layernormr   layer_norm_epsq_layernormk_layernormDropoutattention_dropoutr!   
rotary_embr8   r$   r   r:   s      r;   r,   StableLmAttention.__init__   sN   " !8!8 9 :, , "--33((DNN:#)#=#= $(NNd6N6N$N! ++0L0L LMMMDNN*t/?/??QRVRbRbQc$T^^$4B8  ii 0 0$..4==2PW]WjWjkii 0 0$2J2JT]]2Zagatatuii 0 0$2J2JT]]2Zagatatuii 0 0$2B2BO"//7t~~[a[p[pqD7t77V=R=R D "$F,D,D!E1Er=   past_key_valuepast_key_values4.58new_nameversionr   attention_maskrT   output_attentions	use_cachecache_positionposition_embeddingsr   c	                 @   UR                  5       u  pnU R                  U5      nU R                  U5      nU R                  U5      nUR	                  XU R
                  U R                  5      R                  SS5      nUR	                  XU R                  U R                  5      R                  SS5      nUR	                  XU R                  U R                  5      R                  SS5      nU R                  (       a"  U R                  U5      nU R                  U5      nUu  nnUSS U R                  24   USU R                  S 24   nnUSS U R                  24   USU R                  S 24   nn[        UUUU5      u  nn[        R                  " UU4SS9n[        R                  " UU4SS9nUb0  UUU R                  US.nUR!                  XU R"                  U5      u  p[%        XR&                  5      n[%        XR&                  5      n[        R(                  " XR                  SS5      5      [*        R,                  " U R                  5      -  nUb#  US S 2S S 2S S 2S UR.                  S   24   nUU-  n[0        R2                  R5                  U[        R6                  SS	9R9                  UR:                  5      nU R=                  U5      n[        R(                  " UU5      nUR                  5       XR
                  XR                  4:w  a5  [?        S
XR
                  XR                  4 SUR                  5        35      eUR                  SS5      RA                  5       nURC                  XU RD                  5      nU RG                  U5      nU(       d  S nUU4$ )Nr   rD   .r?   rE   rR   rQ   partial_rotation_sizer   r   )rG   rF   z `attn_output` should be of size z	, but is )$sizer   r   r   viewr   r   rO   r   r   r   r   r   rq   rM   rP   updater   r   r   matmulmathsqrtrJ   r   
functionalsoftmaxfloat32rK   rG   r   r   
contiguousr   ry   r   )r8   r   r   rT   r   r   r   r   r   bszq_lenr   query_states
key_statesvalue_statesrQ   rR   	query_rot
query_passkey_rotkey_passcache_kwargsattn_weightscausal_maskattn_outputs                            r;   rY   StableLmAttention.forward   sy    &**,A{{=1[[/
{{=1#((T^^T]]S]]^_abc__S1I1I4==Yccdeghi
#((T5M5Mt}}]gghiklm++L9L))*5J&S 1 1 1112d//112 	
 s/d////0sD--//0 
 2)Wc3O	7 yy)Z!8bAYY2;
& )-):):"0	L (7'='=jX\XfXfht'u$J z+D+DE
 /H/HI||L2F2Fq!2LMPTPYPYZ^ZgZgPhh%(Aq2HJ4D4DR4H2H)HIKK'L }},,\TV,WZZ[g[m[mn--l;ll<>#~~umm!LL2CP]P]3^2_ `$$&') 
 "++Aq1<<>!))#d6F6FGkk+. LL((r=   )r   r$   r   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r[   NNNFFNN)r\   r]   r^   r_   __doc__r   r   r   r,   r   rM   r`   
LongTensorr   booltuplerY   rc   rd   re   s   @r;   r   r      s&   G&F~ &F(3- &F &FP %0A6R 2637+/"'59KOS)||S) !.S) u//0	S)
 "%S)  S) S) !!1!12S) &eELL%,,,F&GHS) 
u||Xell3XeELL>Q5RR	SS) SS)r=   r   c                   r  ^  \ rS rSr\" SSSS9       SS\R                  S\\R                     S\\R                     S\\	   S	\
S
\
S\\R                     S\\\R                  \R                  4      S\\R                  \\R                     \\\R                        4   4U 4S jjj5       rSrU =r$ )StableLmSdpaAttentioni6  r   r   r   r   r   r   rT   r   r   r   r   r   c	                   > U(       a)  [         R                  S5        [        TU ]  UUUUUUUUS9$ UR	                  5       u  pnU R                  U5      nU R                  U5      nU R                  U5      nUR                  XU R                  U R                  5      R                  SS5      nUR                  XU R                  U R                  5      R                  SS5      nUR                  XU R                  U R                  5      R                  SS5      nU R                  (       a"  U R                  U5      nU R                  U5      nUu  nnUSS U R                   24   USU R                   S 24   nnUSS U R                   24   USU R                   S 24   nn[#        UUUU5      u  nn[$        R&                  " UU4SS9n[$        R&                  " UU4SS9nUb0  UUU R                   US.nUR)                  XU R*                  U5      u  p[-        XR.                  5      n[-        XR.                  5      nUnUb  US S 2S S 2S S 2S UR0                  S	   24   nUR2                  R4                  S
:X  a3  Ub0  UR7                  5       nUR7                  5       nUR7                  5       n[9        US L =(       a    U
S:  5      n[$        R:                  R<                  R?                  UUUUU R@                  (       a  U RB                  RD                  OSUS9nUR                  SS5      R7                  5       nUR                  XU RF                  5      nU RI                  U5      nUS 4$ )Na  StableLmModel is using StableLmSdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.r   r   rT   r   r   r   r   r   r   rD   .r?   rE   r   r   cuda        )	attn_mask	dropout_pr   )%r   r   r+   rY   r   r   r   r   r   r   r   rO   r   r   r   r   r   rq   rM   rP   r   r   r   r   rJ   r9   r(   r   r   r   r   scaled_dot_product_attentiontrainingr   pry   r   )r8   r   r   rT   r   r   r   r   r   r   r   r   r   r   r   rQ   rR   r   r   r   r   r   r   r   r   r:   s                            r;   rY   StableLmSdpaAttention.forward7  sx    [ 7?+-) /"3#-$7 # 	 	 &**,A{{=1[[/
{{=1#((T^^T]]S]]^_abc__S1I1I4==Yccdeghi
#((T5M5Mt}}]gghiklm++L9L))*5J&S 1 1 1112d//112 	
 s/d////0sD--//0 
 2)Wc3O	7 yy)Z!8bAYY2;
& )-):):"0	L (7'='=jX\XfXfht'u$J z+D+DE
 /H/HI$%(Aq2HJ4D4DR4H2H)HIK ##v-.2L'224L#..0J'224L
 ,:;	hh))FF!26--d,,..S G 
 "++Aq1<<>!&&s43C3CDkk+.D  r=    r   )r\   r]   r^   r_   r   rM   r`   r   r   r   r   r   rY   rc   rd   re   s   @r;   r   r   6  s    %0A6R 2637+/"'59KOh!||h! !.h! u//0	h!
 "%h!  h! h! !!1!12h! &eELL%,,,F&GHh! 
u||Xell3XeELL>Q5RR	Sh! Sh!r=   r   c                   |  ^  \ rS rSrSrU 4S jr\" SSSS9       SS\R                  S	\	\R                     S
\	\R                     S\	\   S\S\S\	\R                     S\	\\R                  \R                  4      S\\R                  \	\R                     \	\\R                        4   4S jj5       rSrU =r$ )StableLmFlashAttention2i  a<  
StableLM flash attention module. This module inherits from `StableLmAttention` as the weights of the module stays
untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
flash attention and deal with padding tokens in case the input contains any of them.
c                 D   > [         TU ]  " U0 UD6  [        5       U l        g r[   )r+   r,   r   _flash_attn_uses_top_left_mask)r8   argskwargsr:   s      r;   r,    StableLmFlashAttention2.__init__  s#    $)&)
 /P.Q+r=   r   r   r   r   r   r   rT   r   r   r   r   r   c	                 `   SnUR                  5       u  pnU R                  U5      nU R                  U5      nU R                  U5      nUR	                  XU R
                  U R                  5      R                  SS5      nUR	                  XU R                  U R                  5      R                  SS5      nUR	                  XU R                  U R                  5      R                  SS5      nU R                  (       a"  U R                  U5      nU R                  U5      nUu  nnUSS U R                  24   USU R                  S 24   nnUSS U R                  24   USU R                  S 24   nn[        UUUU5      u  nn[        R                  " UU4SS9n[        R                  " UU4SS9nUb0  UUU R                  US.nUR!                  XU R"                  U5      u  pUR                  SS5      nUR                  SS5      nUR                  SS5      nU R$                  (       a  U R&                  R(                  OSn[+        UUUUUUUU R,                  U R.                  S	9	nUR1                  XU R2                  5      R5                  5       nU R7                  U5      nU(       d  S nUW4$ )
NFr   rD   .r?   rE   r   r   )rT   dropoutuse_top_left_maskr   )r   r   r   r   r   r   r   rO   r   r   r   r   r   rq   rM   rP   r   r   r   r   r   r   r   r   r   ry   r   r   )r8   r   r   rT   r   r   r   r   r   r   r   r   r   r   r   r   rQ   rR   r   r   r   r   r   dropout_rater   r   s                             r;   rY   StableLmFlashAttention2.forward  s    "%**,A{{=1[[/
{{=1
 $((T^^T]]S]]^_abc__S1I1I4==Yccdeghi
#((T5M5Mt}}]gghiklm++L9L))*5J&S 1 1 1112d//112 	
 s/d////0sD--//0  2)Wc3O	7 yy)Z!8bAYY2;
&)-):):"0	L (7'='=jX\XfXfht'u$J $--a3))!Q/
#--a337==t--//c.% "AAnn

 "))#d6F6FGRRTkk+. LL((r=   )r   r   )r\   r]   r^   r_   r   r,   r   rM   r`   r   r   r   r   r   rY   rc   rd   re   s   @r;   r   r     s   R %0A6R 6:37+/"'59KOU)||U) !!1!12U) u//0	U)
 "%U)  U) U) !!1!12U) &eELL%,,,F&GHU) 
u||Xell3XeELL>Q5RR	SU) SU)r=   r   )eagersdpaflash_attention_2c                   v  ^  \ rS rSrS\S\4U 4S jjr       SS\R                  S\	\R                     S\	\R                     S\	\   S	\	\   S
\	\   S\	\R                     S\	\\R                  \R                  4      S\\R                  \	\\R                  \R                  4      4   4S jjrSrU =r$ )StableLmDecoderLayeri  r$   r   c                   > [         TU ]  5         UR                  U l        UR                  U l        [        UR
                     " XS9U l        [        U5      U l        [        R                  " UR                  UR                  S9U l        S U l        U R                  (       d.  [        R                  " UR                  UR                  S9U l        [        R                  " UR                  5      U l        g )N)r   r   )r+   r,   use_parallel_residualry   ATTENTION_CLASSES_attn_implementation	self_attnrs   mlpr   r   r   input_layernormpost_attention_layernormr   hidden_dropoutr  r   s      r;   r,   StableLmDecoderLayer.__init__  s    %+%A%A"!--*6+F+FGdv&!||F,>,>FDYDYZ(,%)),.LL9K9KQWQfQf,gD)zz&"7"78r=   r   r   rT   r   r   r   r   r   r   c	                 b   Un	U R                  U5      nU R                  UUUUUUUUS9u  pU R                  (       a*  U R                  U5      nU R	                  U5      nX-   U-   nO9X-   n	U R                  U R                  U	5      5      nU R	                  U5      nX-   nU4nU(       a  X4-  nU$ )a  
Args:
    hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
    attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
        `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
    position_ids (`torch.LongTensor` of shape `({0})`, *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*):
        cached past key and value projection states
    output_attentions (`bool`, *optional*):
        Whether or not to return the attentions tensors of all attention layers. See `attentions` under
        returned tensors for more detail.
    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`).
    cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
        Indices depicting the position of the input sequence tokens in the sequence
    position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*):
        Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`,
        with `head_dim` being the embedding dimension of each attention head.
r   )r  r  r  r  r  r  )r8   r   r   rT   r   r   r   r   r   residualself_attn_outputself_attn_weights
mlp_outputoutputss                 r;   rY   StableLmDecoderLayer.forward  s    H !,,]; /3nn')%+/) 3 /= 	/
+ %% -0Jj1J$7*DM  2H$"?"?"IJJj1J$1M "++Gr=   )r  ry   r  r  r  r  r  r   )r\   r]   r^   r_   r   r   r,   rM   r`   r   r   r   r   r   FloatTensorrY   rc   rd   re   s   @r;   r	  r	    s   
9~ 
9# 
9 2637+/,1$)59KOI||I !.I u//0	I
 "%I $D>I D>I !!1!12I &eELL%,,,F&GHI 
u  (51B1BEDUDU1U+V"WW	XI Ir=   r	  c                   D    \ rS rSr% \\S'   SrSrS/rSr	Sr
SrSrS rSrg	)
StableLmPreTrainedModelik  r$   modelTr	  r   c                    U R                   R                  n[        U[        R                  5      (       aW  UR
                  R                  R                  SUS9  UR                  b%  UR                  R                  R                  5         g g [        U[        R                  5      (       ad  UR
                  R                  R                  SUS9  UR                  b2  UR
                  R                  UR                     R                  5         g g [        U[        R                  5      (       aJ  UR
                  R                  R                  S5        UR                  R                  R                  5         g g )Nr   )meanstdg      ?)r$   initializer_ranger.   r   r{   weightdatanormal_rx   zero_	Embeddingpadding_idxr   fill_)r8   moduler!  s      r;   _init_weights%StableLmPreTrainedModel._init_weightsw  s   kk++fbii((MM&&CS&9{{&  &&( '--MM&&CS&9!!-""6#5#56<<> .--MM$$S)KK""$ .r=   r   N)r\   r]   r^   r_   r   ra   base_model_prefixsupports_gradient_checkpointing_no_split_modules_skip_keys_device_placement_supports_flash_attn_supports_sdpa_can_compile_fullgraphr+  rc   r   r=   r;   r  r  k  s9    &*#/0"3N!%r=   r  c                     ^  \ rS rSrSrS\4U 4S jjr\\         SS\	\
R                     S\	\
R                     S\	\
R                     S\	\   S	\	\
R                     S
\	\   S\	\   S\	\   S\	\
R                     S\4S jj5       5       r SS\\
R                  S4   S\
R                  S\
R                  S\S\4
S jjr\S\
R                  S\S\S\
R,                  S\
R                  S\4S j5       rSrU =r$ )StableLmModeli  z
Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`StableLmDecoderLayer`]

Args:
    config: StableLmConfig
r$   c           	      @  > [         TU ]  U5        UR                  U l        UR                  U l        [
        R                  " UR                  UR                  U R                  5      U l        [
        R                  " [        UR                  5       Vs/ s H  n[        X5      PM     sn5      U l        [
        R                  " UR                  UR                  S9U l        [#        US9U l        UR&                  U l        SU l        U R+                  5         g s  snf )Nr   r   F)r+   r,   pad_token_idr(  
vocab_sizer   r'  ry   embed_tokensr   r   num_hidden_layersr	  layersr   r   r   r!   r   r  gradient_checkpointing	post_initr   s      r;   r,   StableLmModel.__init__  s     !.. ++LL):):F<N<NPTP`P`ammFKFLdLdFefFe!&4Fef
 LL!3!39N9NO	1@$*$?$?!&+# gs   D	input_idsr   rT   r   inputs_embedsr   r   output_hidden_statesr   r   c
                    Ub  UOU R                   R                  nUb  UOU R                   R                  nUb  UOU R                   R                  nUS L US L-  (       a  [	        S5      eU R
                  (       a/  U R                  (       a  U(       a  [        R                  S5        SnU(       a  Uc  [        U R                   S9nUc  U R                  U5      nU	cD  Ub  UR                  5       OSn
[        R                  " XUR                  S   -   UR                  S9n	Uc  U	R!                  S5      nU R#                  X%XU5      nUnU R%                  X5      nU(       a  SOS nU(       a  SOS nU R&                   H3  nU(       a  X4-  nU" UUUUUUU	US	9nUS   nU(       d  M*  UUS   4-  nM5     U R)                  U5      nU(       a  X4-  n[+        UUUUS
9$ )Nz:You must specify exactly one of input_ids or inputs_embedszZ`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...Fr   r   r   r9   r   )r   rT   r   r   r   r   r   )last_hidden_stater   r   
attentions)r$   r   rA  r   r   r<  r   r   r   r	   r9  get_seq_lengthrM   arangerJ   r9   rk   _update_causal_maskr   r;  r   r   )r8   r?  r   rT   r   r@  r   r   rA  r   past_seen_tokensr   r   r   all_hidden_statesall_self_attnsdecoder_layerlayer_outputss                     r;   rY   StableLmModel.forward  s    2C1N-TXT_T_TqTq$8$D $++JjJj 	 "+!6IDKK<Q<Q	-t";<YZZ&&4==##p "	0*$++>O  --i8M!CRC^==?de"\\ ]5H5H5K"KTaThThN )33A6L..>L]
 & #oomJ #7BD0d![[M#!%55!)*) /"3#-$7	M *!,M  =#3"55% )( 		-0  !11&+++%	
 	
r=   r   input_tensorc           	         U R                   R                  S:X  a  Ub  US:H  R                  5       (       a  U$ g U R                   R                  S:X  a,  [        U[        R
                  5      (       a  [        U5      nU$ Ub  UR                  5       OSnUb  UR                  OSnU R                   R                  S:X  a5  U(       d.  U(       d'  [        R                  " UUUU R                  S9(       a  g UR                  nUR                  S   n	U(       a  UR                  5       n
O5[        U[        R
                  5      (       a  UR                  S	   OXi-   S-   n
U R                  UU	U
UUUR                  S   S
9nU R                   R                  S:X  aZ  UbW  UR                   R"                  S;   a=  U(       d6  [        R$                  " U5      R&                  n[        R(                  " X5      nU$ )Nr  r   flex_attentionr   Fr  )r@  past_key_values_lengthis_trainingr   r?   )sequence_lengthtarget_lengthrG   r   
batch_size)r   xpunpu)r$   r  anyr.   rM   r`   r   rF  is_compileabler   _ignore_causal_mask_sdpar   rG   rJ   get_max_cache_shape5_prepare_4d_causal_attention_mask_with_cache_positionr9   r(   finfomin_unmask_unattended)r8   r   rO  r   r   r   rI  using_compilable_cacherG   rT  rU  r   	min_dtypes                r;   rH  !StableLmModel._update_causal_mask  s    ;;++/BB)~/D.I.I.K.K%%;;++/??.%,,77!<^!L!!
 @O?Z?99;`aCRC^!?!?di ;;++v5>T]n%>>*'7 MM	 ""&,,Q/!+??AM nell;; $$R(%7!;  PP+')#))!, Q 
 KK,,6*%%**.DD%
 E*..I0CCK[Kr=   rT  rU  rG   rV  c                    U b  U R                  5       S:X  a  U nU$ [        R                  " U5      R                  n[        R                  " X4XUR
                  S9nUS:w  a  [        R                  " USS9nU[        R                  " X$R
                  S9UR                  SS5      :  -  nUSSSS2SS24   R                  USSS5      nU b  UR                  5       nU R                  S   n	USS2SS2SS2SU	24   U SS2SSSS24   R                  UR
                  5      -   n
U
S:H  n
USS2SS2SS2SU	24   R                  X5      USS2SS2SS2SU	24'   U$ )	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.
    cache_position (`torch.Tensor`):
        Indices depicting the position of the input sequence tokens in the sequence.
    batch_size (`torch.Tensor`):
        Batch size.
N   )
fill_valuerG   r9   r   )diagonalrC  r?   r   )rF   rM   r^  r_  fullr9   triurG  r   rI   clonerJ   rK   masked_fill)r   rT  rU  rG   r   rV  r   r   rb  mask_lengthpadding_masks              r;   r]  CStableLmModel._prepare_4d_causal_attention_mask_with_cache_position?  s}   > %.*<*<*>!*C(K* ' E*..I** 0Y\j\q\qK !##jjqA5<<>S>STWeWmWmnprsWtttK%dD!Q&67>>z1bRTUK))//1,2226*1aL[L+@ANSTVZ\`bcScDdDgDg&&E    ,q05@Aq,;,AV5W5c5c 6Aq!\k\12 r=   )r  r9  r<  r;  r   r(  r   r8  )	NNNNNNNNN)F)r\   r]   r^   r_   r   r   r,   r   r   r   rM   r   r`   r   r  r   r   rY   r   rH  staticmethodr   rG   r]  rc   rd   re   s   @r;   r5  r5    s   ~ "  151537+/59$(,0/359V
E,,-V
 !.V
 u//0	V

 "%V
   1 12V
 D>V
 $D>V
 'tnV
 !!1!12V
 
!V
  V
~ #(BellK78B llB 	B
 B  BH 444 4 {{	4
 4 4 4r=   r5  c                   h  ^  \ rS rSrS/rU 4S jr\\           SS\\	R                     S\\	R                     S\\	R                     S\\   S\\	R                     S	\\	R                     S
\\   S\\   S\\   S\\	R                     S\\\	R                  4   S\4S jj5       5       rSrU =r$ )StableLmForCausalLMiy  zlm_head.weightc                    > [         TU ]  U5        [        U5      U l        UR                  U l        [
        R                  " UR                  UR                  SS9U l        U R                  5         g rv   )
r+   r,   r5  r  r8  r   r{   ry   lm_headr=  r   s     r;   r,   StableLmForCausalLM.__init__}  sU     "6*
 ++yy!3!3V5F5FUS 	r=   r?  r   rT   r   r@  labelsr   r   rA  r   logits_to_keepr   c                    Ub  UOU R                   R                  nU	b  U	OU R                   R                  n	U R                  UUUUUUUU	U
S9	nUR                  n[        U[        5      (       a  [        U* S5      OUnU R                  USS2USS24   5      nSnUb*  U R                  " UU4SU R                   R                  0UD6n[        UUUR                  UR                  UR                  S9$ )u  
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]`.

Example:

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

>>> model = StableLmForCausalLM.from_pretrained("adept/persimmon-8b-base")
>>> tokenizer = AutoTokenizer.from_pretrained("adept/persimmon-8b-base")

>>> prompt = "human: Hey, what should I eat for dinner?"
>>> 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]
'human: Hey, what should I eat for dinner?\n\ncat: 🐱\n\nhuman: 😐\n\n'
```N)	r?  r   rT   r   r@  r   r   rA  r   r8  )losslogitsr   r   rE  )r$   r   rA  r  rD  r.   r   slicers  loss_functionr8  r   r   r   rE  )r8   r?  r   rT   r   r@  ru  r   r   rA  r   rv  r   r  r   slice_indicesry  rx  s                     r;   rY   StableLmForCausalLM.forward  s   R 2C1N-TXT_T_TqTq$8$D $++JjJj 	
 ,0::)%+'/!5) ,6 
,
  118B>SV8W8W~ot4]kmA}a,?@A%%  ;;11 	D &#33!//))
 	
r=   )rs  r  r8  )NNNNNNNNNNr   )r\   r]   r^   r_   _tied_weights_keysr,   r   r   r   rM   r   r`   r   r  r   r   r   r   rY   rc   rd   re   s   @r;   rq  rq  y  s2   *+  151537+/59-1$(,0/35934L
E,,-L
 !.L
 u//0	L

 "%L
   1 12L
 ))*L
 D>L
 $D>L
 'tnL
 !!1!12L
 c5<</0L
 
 L
  L
r=   rq  c                       \ rS rSrSrg)!StableLmForSequenceClassificationi  r   Nr\   r]   r^   r_   rc   r   r=   r;   r  r    s    dgr=   r  c                       \ rS rSrSrg)StableLmForTokenClassificationi  r   Nr  r   r=   r;   r  r    s    ^ar=   r  )rq  r5  r  r  r  )Nr   )Dr   r   typingr   r   rM   r   activationsr   cache_utilsr   r	   
generationr
   modeling_attn_mask_utilsr   modeling_flash_attention_utilsr   r   modeling_layersr   r   r   modeling_outputsr   r   modeling_rope_utilsr   r   modeling_utilsr   utilsr   r   r   r   utils.deprecationr   configuration_stablelmr   !torch.nn.attention.flex_attentionr   integrations.flex_attentionr   r   
get_loggerr\   r   Moduler!   ri   rq   rs   r   r`   r   r   r   r   r   r  r	  r  r5  rq  r  r  __all__r   r=   r;   <module>r     s  (   "   ! . ) > h 
 L - \ \ 0 2  !!;J J 
		H	%!<bii !<J(8"))  tryy t 	UU\\ 	U# 	U%,, 	U)		 )Dj!- j!Ze)/ e)R !0 V5 Vr %o % %4 n+ n nd\
1? \
~ h(HJa g b%BD[ ar=   