
    fTh5                     X   S r SSKrSSKJrJrJrJr  SSK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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&  \#" 5       (       a  SSK'J(r(  SSK)J*r*  \" 5       (       a  SSKJ+r+  \$RX                  " \-5      r. " S S\	R^                  5      r0S r1S6S jr2 " S S\	R^                  5      r3 " S S\	R^                  5      r4S\Rj                  S\6S\Rj                  4S jr7 " S  S!\	R^                  5      r8 " S" S#\85      r9 " S$ S%\85      r:\8\9\:S&.r; " S' S(\	R^                  5      r<\! " S) S*\5      5       r=\! " S+ S,\=5      5       r> " S- S.\=\5      r?\!" S/S09 " S1 S2\=5      5       r@\! " S3 S4\=5      5       rA/ S5QrBg)7zPyTorch StableLM model.    N)ListOptionalTupleUnion)nn   )ACT2FN)CacheDynamicCache)GenerationMixin)AttentionMaskConverter)!flash_attn_supports_top_left_maskis_flash_attn_available)BaseModelOutputWithPastCausalLMOutputWithPast SequenceClassifierOutputWithPastTokenClassifierOutput)ROPE_INIT_FUNCTIONSdynamic_rope_update)PreTrainedModel)auto_docstringcan_return_tupleis_torch_flex_attn_availablelogging   )StableLmConfig)	BlockMask)make_flex_block_causal_mask)_flash_attention_forwardc                   l   ^  \ rS rSrSS\4U 4S jjjr\R                  " 5       \S 5       5       r	Sr
U =r$ )StableLmRotaryEmbedding<   configc                   > [         TU ]  5         [        US5      (       aH  UR                  b;  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defaultinv_freqF)
persistent)super__init__hasattrr%   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       f/var/www/auris/envauris/lib/python3.13/site-packages/transformers/models/stablelm/modeling_stablelm.pyr,    StableLmRotaryEmbedding.__init__=   s    6>**v/B/B/N#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tor7   
isinstancer'   strtorchautocast	transposecatcosr3   sinrE   )
r6   xposition_idsinv_freq_expandedposition_ids_expandedr@   freqsembrP   rQ   s
             r9   forwardStableLmRotaryEmbedding.forwardN   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.)r3   r#   r0   r5   r1   r2   r&   N)__name__
__module____qualname____firstlineno__r   r,   rL   no_gradr   rX   __static_attributes____classcell__r8   s   @r9   r!   r!   <   s6    /~ / /" ]]_<  <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=   rB   rC   )rH   rL   rO   )rR   x1x2s      r9   rotate_halfrf   _   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.
)	unsqueezerf   )qkrP   rQ   rS   unsqueeze_dimq_embedk_embeds           r9   apply_rotary_pos_embrn   g   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r6   r#   r8   s     r9   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$ rZ   )r{   r}   ry   rz   )r6   rR   r{   s      r9   rX   StableLmMLP.forward   s6    NN4;;t~~a/@#ADLLQRO#ST	r;   )r}   r#   r{   ry   rv   rw   rz   )r[   r\   r]   r^   r,   rX   r`   ra   rb   s   @r9   rp   rp      s    0 r;   rp   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)epsru   )	r+   r,   rD   	num_headsr   
ModuleListrange	LayerNormnorms)r6   rD   r   r   ru   _r8   s         r9   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   rC   )rL   splitrO   zipr   )r6   r   states_per_headsnorms       r9   rX    StableLmLayerNormPerHead.forward   sQ     !;;}aQ?yyTZZYiIjkIj2E$$}-Ijkqrssks    A
)rD   r   r   )gh㈵>F)
r[   r\   r]   r^   r,   rL   TensorrX   r`   ra   rb   s   @r9   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)rH   rG   reshape)r   r   batchnum_key_value_headsslenhead_dims         r9   	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\	R                  S\\	R                     S\\	R                     S	\\   S
\S\S\\	R                     S\\\	R                  \	R                  4      S\\	R                  \\	R                     \\\	R                        4   4S jj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).rt   Fr   r#   )$r+   r,   r#   r   loggerwarning_oncer8   r[   rv   num_attention_headsr   r   r   num_key_value_groups
rope_thetaintpartial_rotary_factorrotary_ndims	is_causal
ValueErrorr   rx   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r6   r#   r   r8   s      r9   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;   r   attention_maskrS   past_key_valueoutput_attentions	use_cachecache_positionposition_embeddingsr   c	                 B   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U4$ )Nr   rB   .r=   rC   rQ   rP   partial_rotation_sizer   r   )rE   rD   z `attn_output` should be of size z	, but is )$sizer   r   r   viewr   r   rN   r   r   r   r   r   rn   rL   rO   updater   r   r   matmulmathsqrtrH   r   
functionalsoftmaxfloat32rI   rE   r   r   
contiguousr   rv   r   )r6   r   r   rS   r   r   r   r   r   bszq_lenr   query_states
key_statesvalue_statesrP   rQ   	query_rot
query_passkey_rotkey_passcache_kwargsattn_weightscausal_maskattn_outputs                            r9   rX   StableLmAttention.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 (6'<'<ZW[WeWegs't$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.88r;   )r   r#   r   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rZ   NNNFFNN)r[   r\   r]   r^   __doc__r   r   r   r,   rL   r   
LongTensorr
   boolr   rX   r`   ra   rb   s   @r9   r   r      s   G&F~ &F(3- &F &FV 2637*."'59KOS9||S9 !.S9 u//0	S9
 !S9  S9 S9 !!1!12S9 &eELL%,,,F&GHS9 
u||Xell3XeELL>Q5RR	SS9 S9r;   r   c                   \  ^  \ rS rSr       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rSrU =r$ )StableLmSdpaAttentioni0  r   r   rS   r   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Uc  U
S:  a  SOSn[$        R8                  R:                  R=                  UUUUU R>                  (       a  U R@                  RB                  OSUS9nUR                  SS5      R7                  5       nUR                  XU RD                  5      nU RG                  U5      nUS U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   rS   r   r   r   r   r   r   rB   .r=   rC   r   r   cudaTF        )	attn_mask	dropout_pr   )$r   r   r+   rX   r   r   r   r   r   r   r   rN   r   r   r   r   r   rn   rL   rO   r   r   r   r   rH   r7   r'   r   r   r   scaled_dot_product_attentiontrainingr   prv   r   )r6   r   r   rS   r   r   r   r   r   r   r   r   r   r   r   rP   rQ   r   r   r   r   r   r   r   r   r8   s                            r9   rX   StableLmSdpaAttention.forward1  st    [ 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 (6'<'<ZW[WeWegs't$J z+D+DE
 /H/HI$%(Aq2HJ4D4DR4H2H)HIK ##v-.2L'224L#..0J'224L
 (/EAID5	hh))FF!26--d,,..S G 
 "++Aq1<<>!&&s43C3CDkk+.D.00r;    r   )r[   r\   r]   r^   rL   r   r   r   r
   r   r   rX   r`   ra   rb   s   @r9   r   r   0  s     2637*."'59KOh1||h1 !.h1 u//0	h1
 !h1  h1 h1 !!1!12h1 &eELL%,,,F&GHh1 
u||Xell3XeELL>Q5RR	Sh1 h1r;   r   c                   f  ^  \ rS rSrSrU 4S jr       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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 rZ   )r+   r,   r   _flash_attn_uses_top_left_mask)r6   argskwargsr8   s      r9   r,    StableLmFlashAttention2.__init__  s#    $)&)
 /P.Q+r;   r   r   rS   r   r   r   r   r   r   c	                 b   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U4$ )
NFr   rB   .r=   rC   r   r   )rS   dropoutuse_top_left_maskr   )r   r   r   r   r   r   r   rN   r   r   r   r   r   rn   rL   rO   r   r   r   r   r   r   r   r   r   rv   r   r   )r6   r   r   rS   r   r   r   r   r   r   r   r   r   r   r   r   rP   rQ   r   r   r   r   r   dropout_rater   r   s                             r9   rX   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 (6'<'<ZW[WeWegs't$J $--a3))!Q/
#--a337==t--//c.% "AAnn

 "))#d6F6FGRRTkk+. LL.88r;   )r   r   )r[   r\   r]   r^   r   r,   rL   r   r   r   r
   r   r   rX   r`   ra   rb   s   @r9   r   r     s    R 6:37*."'59KOU9||U9 !!1!12U9 u//0	U9
 !U9  U9 U9 !!1!12U9 &eELL%,,,F&GHU9 
u||Xell3XeELL>Q5RR	SU9 U9r;   r   )eagersdpaflash_attention_2c                     ^  \ rS rSrS\S\4U 4S jjr       SS\R                  S\	\R                     S\	\R                     S\	\\R                        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_residualrv   ATTENTION_CLASSES_attn_implementation	self_attnrp   mlpr   r   r   input_layernormpost_attention_layernormr   hidden_dropoutr   r   s      r9   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   rS   r   r   r   r   r   r   c	                 |   Un	U R                  U5      nU R                  UUUUUUUUS9u  pnU 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  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_value (`Tuple(torch.FloatTensor)`, *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	  )r6   r   r   rS   r   r   r   r   r   residualself_attn_outputself_attn_weightspresent_key_value
mlp_outputoutputss                  r9   rX   StableLmDecoderLayer.forward  s    H !,,]; BF')%)/) 3 BP 	B
>-> %% -0Jj1J$7*DM  2H$"?"?"IJJj1J$1M "++G++Gr;   )r   rv   r  r  r	  r  r  r   )r[   r\   r]   r^   r   r   r,   rL   r   r   r   r   r   FloatTensorrX   r`   ra   rb   s   @r9   r  r  
  s   
9~ 
9# 
9 26378<,1$)59KOL||L !.L u//0	L
 !u||!45L $D>L D>L !!1!12L &eELL%,,,F&GHL 
u  (51B1BEDUDU1U+V"WW	XL Lr;   r  c                   D    \ rS rSr\rSrSrS/rSr	Sr
SrSrSrSrS rSrg)	StableLmPreTrainedModelif  modelTr  past_key_valuesc                    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_rangerJ   r   rx   weightdatanormal_ru   zero_	Embeddingpadding_idxr   fill_)r6   moduler  s      r9   _init_weights%StableLmPreTrainedModel._init_weightss  s   kk++fbii((MM&&CS&9{{&  &&( '--MM&&CS&9!!-""6#5#56<<> .--MM$$S)KK""$ .r;   r   N)r[   r\   r]   r^   r   config_classbase_model_prefixsupports_gradient_checkpointing_no_split_modules_skip_keys_device_placement_supports_flash_attn_2_supports_cache_class_supports_sdpa_supports_quantized_cache_supports_static_cacher%  r`   r   r;   r9   r  r  f  sD    !L&*#/0"3! N $!%r;   r  c                   "  ^  \ rS rSrSrS\4U 4S jjrS rS r\	\
         SS\\R                     S\\R                     S	\\R                     S
\\\R                         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\R2                  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!  rv   embed_tokensr   r   num_hidden_layersr  layersr   r   r   r!   r   r  gradient_checkpointing	post_initr   s      r9   r,   StableLmModel.__init__  s     !.. ++LL):):F<N<NPTP`P`ammFKFLdLdFefFe!&4Fef
 LL!3!39N9NO	1@$*$?$?!&+# gs   Dc                     U R                   $ rZ   r6  r6   s    r9   get_input_embeddings"StableLmModel.get_input_embeddings  s       r;   c                     Xl         g rZ   r=  r6   values     r9   set_input_embeddings"StableLmModel.set_input_embeddings  s    !r;   	input_idsr   rS   r  inputs_embedsr   r   output_hidden_statesr   r   c
                 4   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Sn
U(       aP  [        U[        5      (       d;  Sn
Uc  [        5       nO+[        R                  " U5      n[        R                  S5        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S nU R,                   H  nU(       a  X4-  nU R
                  (       a5  U R                  (       a$  U R/                  UR0                  UUUUUUU	U5	      nOU" UUUUUUU	US
9nUS   nU(       a  UU(       a  SOS   nU(       d  M  UUS   4-  nM     U R3                  U5      nU(       a  X4-  nU(       a  UOS nU
(       a  UR5                  5       n[7        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`...FTzWe detected that you are passing `past_key_values` as a tuple of tuples. This is deprecated and will be removed in v4.47. Please convert your cache or use an appropriate `Cache` class (https://huggingface.co/docs/transformers/kv_cache#legacy-cache-format)r   r   r7   r   )r   rS   r   r   r   r   r   rB   )last_hidden_stater  r   
attentions)r#   r   rH  r   r   r9  r   r   r   rJ   r
   r   from_legacy_cacher6  get_seq_lengthrL   arangerH   r7   rh   _update_causal_maskr   r8  _gradient_checkpointing_func__call__r   to_legacy_cacher   )r6   rF  r   rS   r  rG  r   r   rH  r   return_legacy_cachepast_seen_tokensr   r   r   all_hidden_statesall_self_attnsnext_decoder_cachedecoder_layerlayer_outputs
next_caches                        r9   rX   StableLmModel.forward  s    2C1N-TXT_T_TqTq$8$D $++JjJj 	 "+!6IDKK<Q<Q	-t";<YZZ&&4==##p "	 $Z??"&&".."."@"@"Q##^   --i8M!CRC^==?de"\\ ]5H5H5K"KTaThThN )33A6L..>L]
 & #oomJ #7BD0d!![[M#!%55!**t}} $ A A!**! #%"'
! !.!#.!-#2&7'#1(;	! *!,M%28I1q%Q"  =#3"55E )H 		-0  !11+4'$
#335J&+&+%	
 	
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   )rG  past_key_values_lengthis_trainingr   r=   )sequence_lengthtarget_lengthrE   r   
batch_size)r   xpunpu)r#   r  anyrJ   rL   r   r   rN  is_compileabler   _ignore_causal_mask_sdpar   rE   rH   get_max_cache_shape5_prepare_4d_causal_attention_mask_with_cache_positionr7   r'   finfomin_unmask_unattended)r6   r   r]  r   r  r   rU  using_compilable_cacherE   rb  rc  r   	min_dtypes                r9   rP  !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;   rb  rc  rE   rd  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_valuerE   r7   r   )diagonalrJ  r=   r   )rD   rL   rl  rm  fullr7   triurO  r   rG   clonerH   rI   masked_fill)r   rb  rc  rE   r   rd  r   r   rp  mask_lengthpadding_masks              r9   rk  CStableLmModel._prepare_4d_causal_attention_mask_with_cache_positiona  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  r6  r9  r8  r   r"  r   r5  	NNNNNNNNN)F)r[   r\   r]   r^   r   r   r,   r?  rD  r   r   r   rL   r   r   r   r  r   r   rX   r   r
   rP  staticmethodr   rE   rk  r`   ra   rb   s   @r9   r2  r2    s   ~ "!"  151537=A59$(,0/359v
E,,-v
 !.v
 u//0	v

 "$u'8'8"9: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;   r2  c                     ^  \ rS rSrS/rU 4S jrS rS rS rS r	S r
S	 r\\           SS
\\R                      S\\R"                     S\\R                      S\\\R&                        S\\R&                     S\\R                      S\\   S\\   S\\   S\\R                      S\\\R"                  4   S\4S jj5       5       rSrU =r$ )StableLmForCausalLMi  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 rs   )
r+   r,   r2  r  r5  r   rx   rv   lm_headr:  r~   s     r9   r,   StableLmForCausalLM.__init__  sU     "6*
 ++yy!3!3V5F5FUS 	r;   c                 .    U R                   R                  $ rZ   r  r6  r>  s    r9   r?  (StableLmForCausalLM.get_input_embeddings      zz&&&r;   c                 $    XR                   l        g rZ   r  rB  s     r9   rD  (StableLmForCausalLM.set_input_embeddings      "'

r;   c                     U R                   $ rZ   r  r>  s    r9   get_output_embeddings)StableLmForCausalLM.get_output_embeddings  s    ||r;   c                     Xl         g rZ   r  )r6   new_embeddingss     r9   set_output_embeddings)StableLmForCausalLM.set_output_embeddings  s    %r;   c                     Xl         g rZ   r  )r6   decoders     r9   set_decoderStableLmForCausalLM.set_decoder  s    
r;   c                     U R                   $ rZ   r  r>  s    r9   get_decoderStableLmForCausalLM.get_decoder  s    zzr;   rF  r   rS   r  rG  labelsr   r   rH  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)	rF  r   rS   r  rG  r   r   rH  r   r5  losslogitsr  r   rL  )r#   r   rH  r  rK  rJ   r   slicer  loss_functionr5  r   r  r   rL  )r6   rF  r   rS   r  rG  r  r   r   rH  r   r  r   r  r   slice_indicesr  r  s                     r9   rX   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;   )r  r  r5  )NNNNNNNNNNr   )r[   r\   r]   r^   _tied_weights_keysr,   r?  rD  r  r  r  r  r   r   r   rL   r   r   r   r  r   r   r   r   rX   r`   ra   rb   s   @r9   r  r    s\   *+'(&  151537=A59-1$(,0/35934L
E,,-L
 !.L
 u//0	L

 "$u'8'8"9:L
   1 12L
 ))*L
 D>L
 $D>L
 'tnL
 !!1!12L
 c5<</0L
 
 L
  L
r;   r  a  
    The StableLm transformer with a sequence classification head on top (linear layer).

    [`StableLmForSequenceClassification`] 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).
    )custom_introc                   *  ^  \ rS rSrU 4S jrS rS r\\         SS\	\
R                     S\	\
R                     S\	\
R                     S\	\   S	\	\
R                     S
\	\
R                     S\	\   S\	\   S\	\   S\4S jj5       5       rSrU =r$ )!StableLmForSequenceClassificationi  c                    > [         TU ]  U5        UR                  U l        [        U5      U l        [
        R                  " UR                  U R                  SS9U l        U R                  5         g rs   )
r+   r,   
num_labelsr2  r  r   rx   rv   scorer:  r~   s     r9   r,   *StableLmForSequenceClassification.__init__"  sS      ++"6*
YYv114??O
 	r;   c                 .    U R                   R                  $ rZ   r  r>  s    r9   r?  6StableLmForSequenceClassification.get_input_embeddings+  r  r;   c                 $    XR                   l        g rZ   r  rB  s     r9   rD  6StableLmForSequenceClassification.set_input_embeddings.  r  r;   rF  r   rS   r  rG  r  r   r   rH  r   c
                    U R                  UUUUUUUU	S9n
U
R                  nU R                  U5      nUb  UR                  S   nOUR                  S   nU R                  R
                  c  US:w  a  [        S5      eU R                  R
                  c  SnOUb  XR                  R
                  :g  R                  UR                  [        R                  5      n[        R                  " UR                  S   UR                  [        R                  S9nUU-  R                  S5      nO.Sn[        R                  U R                  R                    S35        U[        R                  " XR                  S	9U4   nSnUb  U R#                  XUU R                  S
9n[%        UUU
R&                  U
R(                  U
R*                  S9$ )e  
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).
r   rS   r  rG  r   r   rH  Nr   r   z=Cannot handle batch sizes > 1 if no padding token is defined.r=   )r7   rE   z will not detect padding tokens in `inputs_embeds`. Results may be unexpected if using padding tokens in conjunction with `inputs_embeds.`rJ  )r  r  pooled_logitsr#   r  )r  rK  r  rH   r#   r4  r   rI   r7   rL   int32rO  argmaxr   r   r8   r[   r  r   r  r   rL  )r6   rF  r   rS   r  rG  r  r   r   rH  transformer_outputsr   r  rd  last_non_pad_tokennon_pad_masktoken_indicesr  r  s                      r9   rX   )StableLmForSequenceClassification.forward1  s   * 8<zz)%+'/!5 8B 	8
 ,==M* "+J&,,Q/J;;##+
a\]];;##+!#"%)A)AAEEfmmUZU`U`aL!LL)<V]]Z_ZeZefM"/,">!F!Fr!J!#>>**+ ,Z Z
 u||J}}MOaab%%VR_hlhshs%tD/ /??-;;*55
 	
r;   )r  r  r  r}  )r[   r\   r]   r^   r,   r?  rD  r   r   r   rL   r   r   r
   r  r   r   rX   r`   ra   rb   s   @r9   r  r    s     '(  151537+/59-1$(,0/3A
E,,-A
 !.A
 u//0	A

 "%A
   1 12A
 ))*A
 D>A
 $D>A
 'tnA
 
*A
  A
r;   r  c                   *  ^  \ rS rSrU 4S jrS rS r\\         SS\	\
R                     S\	\
R                     S\	\
R                     S\	\   S	\	\
R                     S
\	\
R                     S\	\   S\	\   S\	\   S\4S jj5       5       rSrU =r$ )StableLmForTokenClassificationiw  c                   > [         TU ]  U5        UR                  U l        [        U5      U l        [        USS 5      b  UR                  nO[        USS 5      b  UR                  nOSn[        R                  " U5      U l
        [        R                  " UR                  UR                  5      U l        U R                  5         g )Nclassifier_dropoutr
  g?)r+   r,   r  r2  r  getattrr  r
  r   r   r   rx   rv   r  r:  )r6   r#   r  r8   s      r9   r,   'StableLmForTokenClassification.__init__z  s      ++"6*
6/6B!'!:!:V-t4@!'!6!6!$zz"45YYv1163D3DE
 	r;   c                 .    U R                   R                  $ rZ   r  r>  s    r9   r?  3StableLmForTokenClassification.get_input_embeddings  r  r;   c                 $    XR                   l        g rZ   r  rB  s     r9   rD  3StableLmForTokenClassification.set_input_embeddings  r  r;   rF  r   rS   r  rG  r  r   r   rH  r   c
                    U R                  UUUUUUUU	S9n
U
R                  nU R                  U5      nU R                  U5      nSnUb  U R	                  XU R
                  5      n[        UUU
R                  U
R                  S9$ )r  r  N)r  r  r   rL  )	r  rK  r   r  r  r#   r   r   rL  )r6   rF  r   rS   r  rG  r  r   r   rH  r  sequence_outputr  r  s                 r9   rX   &StableLmForTokenClassification.forward  s    * ,0::)%+'/!5 ,6 	,
 "33,,7O,%%fdkkBD$!//))	
 	
r;   )r   r  r  r  r}  )r[   r\   r]   r^   r,   r?  rD  r   r   r   rL   r   r   r
   r  r   r   rX   r`   ra   rb   s   @r9   r  r  w  s     '(  151537+/59-1$(,0/3*
E,,-*
 !.*
 u//0	*

 "%*
   1 12*
 ))**
 D>*
 $D>*
 'tn*
 
*
  *
r;   r  )r  r2  r  r  r  )Nr   )Cr   r   typingr   r   r   r   rL   torch.utils.checkpointr   activationsr	   cache_utilsr
   r   
generationr   modeling_attn_mask_utilsr   modeling_flash_attention_utilsr   r   modeling_outputsr   r   r   r   modeling_rope_utilsr   r   modeling_utilsr   utilsr   r   r   r   configuration_stablelmr   !torch.nn.attention.flex_attentionr   integrations.flex_attentionr   r   
get_loggerr[   r   Moduler!   rf   rn   rp   r   r   r   r   r   r   r   r  r  r  r2  r  r  r  __all__r   r;   r9   <module>r     s  (   / /    ! . ) > h  L - \ \ 2  !!;J J 
		H	%<bii <F(8"))  tryy t 	UU\\ 	U# 	U%,, 	U~9		 ~9Bi1- i1Xd9/ d9P !0 Y299 Yx %o % %6 T+ T Tpt
1? t
n S
(? S
S
l C
%< C
 C
Lr;   