
    cCi              
          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J	r	J
r
Jr  SSKJr  SSKJr  SS	KJr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  SSKJr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*  SSK+J,r,  \" 5       (       a  SSKJ-r-  \*R\                  " \/5      r0 " S S\Rb                  5      r2S r3S>S jr4 " S S\Rj                  5      r6S\Rn                  S\8S\Rr                  S\Rn                  4S jr:S \Rn                  S!\Rn                  S"\;S#\<S\Rn                  4
S$ jr= " S% S&\Rj                  5      r> " S' S(\>5      r? " S) S*\Rj                  5      r@\>\>\?S+.rA " S, S-\5      rB\) " S. S/\'5      5       rC\) " S0 S1\C5      5       rD\)" S2S39 " S4 S5\C\5      5       rE\)" S6S39 " S7 S8\C5      5       rF\) " S9 S:\C5      5       rG\) " S; S<\C5      5       rH/ S=QrIg)?zPyTorch Falcon model.    N)OptionalUnion)nn)BCEWithLogitsLossCrossEntropyLoss	LayerNormMSELoss)
functional   )get_activation)CacheDynamicCacheStaticCache)GenerationMixin)AttentionMaskConverter)!flash_attn_supports_top_left_maskis_flash_attn_available)GradientCheckpointingLayer))BaseModelOutputWithPastAndCrossAttentions!CausalLMOutputWithCrossAttentionsQuestionAnsweringModelOutput SequenceClassifierOutputWithPastTokenClassifierOutput)ROPE_INIT_FUNCTIONSdynamic_rope_update)PreTrainedModel)auto_docstringlogging   )FalconConfig)_flash_attention_forwardc                   N    \ rS rSrS\R
                  S\R
                  4S jrSrg)FalconLinear9   inputreturnc                 l    XR                   R                  -  nU R                  c  U$ X R                  -   $ N)weightTbias)selfr%   hidden_statess      d/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/falcon/modeling_falcon.pyforwardFalconLinear.forward:   s/    -99  yy((     N)__name__
__module____qualname____firstlineno__torchTensorr/   __static_attributes__r2   r1   r.   r#   r#   9   s    )U\\ )ell )r1   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..N   dim)shaper7   cat)xx1x2s      r.   rotate_halfrD   B   sZ    	
3"!''"+"""	#B	
3q ""	#B99rc2YB''r1   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.
)	unsqueezerD   )qkcossinposition_idsunsqueeze_dimq_embedk_embeds           r.   apply_rotary_pos_embrO   J   sS    ( --
&C
--
&Cw;q>C/0Gw;q>C/0Gr1   c                      ^  \ 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$ )FalconRotaryEmbeddingf   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defaultrS   F)
persistent)super__init__hasattr
isinstancerV   dictgetrW   max_position_embeddingsmax_seq_len_cachedoriginal_max_seq_lenrT   r   rope_init_fnattention_scalingregister_bufferrS   original_inv_freq)r,   rT   devicerS   	__class__s       r.   r\   FalconRotaryEmbedding.__init__i   s    6>**z&:M:Mt/T/T#0044[&BUBUBYBYZ`BabDN&DN"("@"@$*$B$B!/?+/+<+<T[[&+Q((ZeD!%r1   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;   r   mpscpuF)device_typeenabledr<   r=   dtype)rS   floatexpandr?   torh   r^   rX   strr7   autocast	transposer@   rI   re   rJ   rq   )
r,   rA   rK   inv_freq_expandedposition_ids_expandedrn   freqsembrI   rJ   s
             r.   r/   FalconRotaryEmbedding.forwardz   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.)re   rT   rb   rg   rc   rd   rW   r(   )r3   r4   r5   r6   r7   r8   __annotations__r    r\   no_gradr   r/   r9   __classcell__ri   s   @r.   rQ   rQ   f   s@    ll/| / /" ]]_<  <r1   rQ   attention_mask	num_headsrq   r&   c                    U R                   u  p4S[        R                  " [        R                  " U5      5      -  n[        R
                  " SS[        R                  " U5      S-
  * -  * -  U R                  [        R                  S9n[        R                  " SSU-   U R                  [        R                  S9n[        R                  " Xg5      nXQ:w  a  [        R
                  " SS[        R                  " SU-  5      S-
  * -  * -  U R                  [        R                  S9n	[        XQU-
  5      n
[        R                  " SSSU
-  -   SU R                  [        R                  S9n[        R                  " U[        R                  " X5      /SS9nU R                  SS9S-
  U -  S S 2S S S 24   nUS   R                  5       U-  nUR                  X1-  SU5      R!                  U5      $ )	Nr<   r   rh   rq   r   r   r=   r;   ).N)r?   mathfloorlog2r7   tensorrh   float32arangeint32powminr@   cumsumbfloat16reshapert   )r   r   rq   
batch_size
seq_lengthclosest_power_of_2basepowersslopes
extra_basenum_remaining_headsextra_powersarange_tensoralibis                 r.   build_alibi_tensorr      s   +11Jdjj9)=>><<	tyy!34q899:;NDYDYafananD \\!Q!33N<Q<QY^YdYdeFYYt$F&\\A499Q);%;<q@AABCNLaLainiviv

 ""4BT6TU||Aq1/B+B'BAnNcNckpkvkvwFEIIj$GHaP %+++3a7>I1dTU:VM9&&(=8E==/J?BB5IIr1   rA   residualprobtrainingc                 8    [         R                  " XUS9nX-   nU$ )z
Dropout add function

Args:
    x (`torch.tensor`):
        input tensor
    residual (`torch.tensor`):
        residual tensor
    prob (`float`):
        dropout probability
    training (`bool`):
        training mode
)pr   )Fdropout)rA   r   r   r   outs        r.   dropout_addr      s      ))A
1C
.CJr1   c                     ^  \ rS rSrSS\4U 4S jjjrS\R                  S\\R                  \R                  \R                  4   4S jr	S\R                  S\R                  4S jr
       SS	\R                  S
\\R                     S\R                  S\\R                     S\\   S\\R                     S\S\S\\R                     S\\\R                  \R                  4      4S jjrSrU =r$ )FalconAttention   rT   c                 R  > [         TU ]  5         Xl        UR                  U l        UR                  U l        U R                  U R
                  -  U l        U R                  U l        UR                  U l        UR                  U l	        UR                  U l
        SU l        X l        Uc-  [        R                  SU R                  R                    S35        U R                  U R
                  -  U R                  :w  a&  [#        SU R                   SU R
                   S35      eS[$        R&                  " U R                  5      -  U l        U R(                  U l        UR,                  (       a*  UR.                  S-  UR                  -   U R                  -  nO=UR0                  (       a  U R                  SU R                  -  -   nOS	U R                  -  n[3        U R                  X1R4                  S
9U l        UR,                  U l        UR0                  U l        [3        U R                  U R                  UR4                  S
9U l        [:        R<                  " UR>                  5      U l        U R,                  (       d  U R0                  (       d  UR.                  U l        g SU l        g )NTz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.zA`hidden_size` must be divisible by num_heads (got `hidden_size`: z and `num_heads`: z).      ?r<   r   r+   r   ) r[   r\   rT   hidden_sizenum_attention_headsr   head_dim
split_sizehidden_dropoutra   
rope_theta	is_causal	layer_idxloggerwarning_onceri   r3   
ValueErrorr   sqrtinv_norm_factorbetanew_decoder_architecturenum_kv_headsmulti_queryr#   r+   query_key_valuedenser   Dropoutattention_dropout)r,   rT   r   qkv_out_dimri   s       r.   r\   FalconAttention.__init__   s&   !--33((DNN:**$33'-'E'E$ ++" !8!8 9 :, , ==4>>)T-=-==STXTdTdSe fNN#2'   #TYYt}}%==((	**!..2V5O5OOSWS`S``K**Q->>Kd...K+D,<,<kP[P[\(.(G(G%!--!$"2"2D4D4D6;;W
!#F,D,D!E484Q4QY]YiYiF//pqr1   	fused_qkvr&   c                 r   U R                   (       a  UR                  u  p#nUR                  X#SU R                  U R                  -  S-   U R
                  5      nUSS2SS2SS2SS24   nUSS2SS2SS2S/4   nUSS2SS2SS2S/4   n[        R                  " XvR                  5      n[        R                  " XR                  5      nXgU4 V	s/ s H  oR                  SS5      PM     sn	u  pgnXgU4$ U R                  (       dT  UR                  u  pnUR                  XU R                  SU R
                  5      nUSSSS24   USSSS24   USSSS24   4$ UR                  u  pnUR                  XU R                  S-   U R
                  5      nUSSS2SS24   USS/SS24   USS/SS24   4$ s  sn	f )	ap  
Split the last dimension into (num_heads, head_dim), results share same memory storage as `fused_qkv`

Args:
    fused_qkv (`torch.tensor`): [batch_size, seq_length, num_heads * 3 * head_dim]

Returns:
    query: [batch_size, seq_length, num_heads, head_dim] key: [batch_size, seq_length, num_heads, head_dim]
    value: [batch_size, seq_length, num_heads, head_dim]
r;   r<   Nr   .r   r   )
r   r?   viewr   r   r   r7   broadcast_toflattenr   )r,   r   batchseq_len_qkvquerykeyvaluerA   r   r   three_times_hidden_sizes                r.   _split_headsFalconAttention._split_heads   s    (( )EA..T^^tGXGX5X[\5\^b^k^klC1a"%EaAtm$C1a"&E$$S++6C&&ukk:E;@u:M N:MQ1a:M NEu$$!!>Goo;J$;!zt~~qRVR_R_`IS!QY'319)=yaQR?SSS>Goo;J$;!zt~~PQ?QSWS`S`aIS#2#q[)9S2$\+BIcTVSWYZlD[[[ !Os   F4rA   c                    UR                   u  p#nX R                  -  nUR                  XPR                  X0R                  5      nUR	                  SSSS5      nUR                  XSU R                  U R                  -  5      $ )z
Merge heads together over the last dimension

Args:
    x (`torch.tensor`): [batch_size * num_heads, seq_length, head_dim]

Returns:
    torch.tensor: [batch_size, seq_length, num_heads * head_dim]
r   r<   r   r   )r?   r   r   r   permuter   )r,   rA   batch_size_and_num_headsr   r   r   s         r.   _merge_headsFalconAttention._merge_heads  so     34''/ a-?
 FF:~~z==I IIaAq! yy$--1OPPr1   r-   r   r   rK   
layer_past	head_mask	use_cacheoutput_attentionscache_positionposition_embeddingsc           	      
   U R                  U5      nU R                  (       a  U R                  OU R                  nU R	                  U5      u  pnUR
                  u  nn  nUR                  SS5      R                  UU R                  UU R                  5      nUR                  SS5      R                  UUUU R                  5      nUR                  SS5      R                  UUUU R                  5      nUc  U
u  nn[        XUU5      u  pUb:  SU	0nUc  UR                  WWS.5        UR                  XU R                  U5      u  pUR
                  S   nU R                  R                  S:X  aM  UR                  R                  S:X  a3  Ub0  UR!                  5       nUR!                  5       nUR!                  5       nUb  US S 2S S 2S S 2S UR
                  S   24   nUGcM  U R                  R                  S:X  aY  U(       dR  U R"                  =(       a    US L =(       a    US:  n[$        R&                  R(                  R+                  UUUUSUS	9nS nO_XR                  S
S5      -  nU[,        R.                  " U R                  5      -  n[0        R2                  " UU-   S
UR4                  S9nUU-  nUR7                  UU R                  UU R                  5      nUR9                  SSSS5      nUR                  UUU R                  U R                  -  5      nU R;                  U5      nUU4$ U R                  R                  S:X  a  U(       d  Uc  U R"                  =(       a    US L =(       a    US:  n[$        R&                  R(                  R+                  UUUUU R<                  (       a  U R>                  R@                  OSUS	9nS nUR                  SS5      nUR                  UUU R                  U R                  -  5      nU R;                  U5      nUU4$ XR                  S
S5      -  nUR7                  UU R                  UU5      nUR4                  nU[$        RB                  :X  d  U[$        RD                  :X  a  URG                  [$        RH                  5      nUUR7                  UU R                  SS
5      -   nUU RJ                  -  n[0        R2                  " UU-   S
UR4                  S9nU R?                  U5      nUb  UU-  nUR7                  UU R                  UU5      nUU-  RM                  SS5      nU RO                  U5      nU R;                  U5      nUU4$ )Nr   r<   r   rJ   rI   r   sdpacuda        )	attn_mask	dropout_pr   r;   )r>   rq   r   r   )(r   r   r   r   r   r?   rw   r   r   rO   updater   rT   _attn_implementationrh   rX   
contiguousr   r7   r   r
   scaled_dot_product_attentionr   r   r   softmaxrq   r   r   r   r   r   r   float16r   rt   r   r   r   r   )r,   r-   r   r   rK   r   r   r   r   r   r   r   r   query_layer	key_layervalue_layerr   query_lengthr   rI   rJ   cache_kwargs	kv_lengthr   attn_outputattention_scoresattention_probsmatmul_resultinput_dtypeattention_logitsattention_probs_reshapeds                                  r.   r/   FalconAttention.forward  s0    ((7	)-)F)Ft~~DL]L]040A0A)0L-)4):):&
L!Q!++Aq199*dnnVbdhdqdqr''1-55j,P\^b^k^kl	!++Aq199*lT`bfbobop=*HC%9+RUWZ%["K!,n=L}##C$<=%/%6%6yt~~_k%l"IOOB'	KK,,6""''61* &002K!,,.I%002K%+Aq!5Jyr7J5J,JKN={{//69BS
 !NNZ~/EZ,YZJZ	#hh11NN,!' O  $( #.1D1DR1L#L  DIIdmm$<< #$99-=-NTV^k^q^q#r .<%**:t~~|UYUbUbcK%--aAq9K%--j,Y]YfYfHfgK**[1K 000 {{//69BSXaXi !NNZ~/EZ,YZJZ	#hh11NN,:>--d4466S' O  #')33Aq9)11*lDNN]a]j]jLjk"jj5B //? !,.A.A"b.I I $1#5#5j$..R^`i#j  /44%--/;%..3P'7':':5=='I$#3ejjT^^]^`b6c#c  D$8$88 "#)),<~,MSU]j]p]p"q"&"8"8"I(&5	&AO ,;+?+?
DNN\hjs+t(  8+ENNqRST #//<"jj5//r1   )r   r   rT   r   r   r   r   r   r   r   ra   r   r   r   r   r   r   r   r(   NNNFFNN)r3   r4   r5   r6   r    r\   r7   r8   tupler   r   r   
LongTensorr   boolr/   r9   r   r   s   @r.   r   r      sF   (r| (r (rT\ell \uU\\5<<Y^YeYe=e7f \@Qell Qu|| Q< 48&*,0"'59KOC0||C0 %C0 	C0
 u//0C0 UOC0 ELL)C0 C0  C0 !!1!12C0 &eELL%,,,F&GHC0 C0r1   r   c                   >  ^  \ rS rSrSrU 4S jr       SS\R                  S\\R                     S\R                  S\\R                     S\\
   S	\\R                     S
\S\S\\R                     S\\\R                  \R                  4      4S jjrSrU =r$ )FalconFlashAttention2i  a8  
Falcon flash attention module. This module inherits from `FalconAttention` 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)r,   argskwargsri   s      r.   r\   FalconFlashAttention2.__init__  s#    $)&)
 /P.Q+r1   r-   r   r   rK   r   r   r   r   r   r   c                    U R                  U5      nU R                  (       a  U R                  OU R                  nU R	                  U5      u  pnUR
                  u  nn  nUR                  SS5      R                  UU R                  UU R                  5      nUR                  SS5      R                  UUUU R                  5      nUR                  SS5      R                  UUUU R                  5      nUc  U
u  nn[        XUU5      u  pUb:  SU	0nUc  UR                  WWS.5        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b  [        S5      eU R                  (       a  U R                  R                  OSnUR                   nUR"                  R$                  S:w  a  UR"                  R$                  OSnU[&        R(                  :X  a  [&        R*                  " 5       (       aA  [-        [&        S	5      (       a  [&        R.                  " U5      O[&        R0                  " 5       nOR[-        U R                  S
5      (       a  U R                  R2                  nO U R                   R4                  R                   n[6        R9                  SU S35        UR;                  U5      nUR;                  U5      nUR;                  U5      n[=        UUUUUUUU R>                  U R@                  S9	nUR                  UUU R                  U R                  -  5      nU RC                  U5      nU(       d  S nUU4$ )Nr   r<   r   r   z6`alibi` is not supported when `use_flash_attn` is Truer   rl   rm   get_autocast_dtype_pre_quantization_dtypezThe input hidden states seems to be silently casted in float32, this might be related to the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in .)rK   r   r   use_top_left_mask)"r   r   r   r   r   r?   rw   r   r   rO   r   r   r   r   rT   r   rq   rh   rX   r7   r   is_autocast_enabledr]   r  get_autocast_gpu_dtyper  r)   r   r   rt   r!   r   r   r   )r,   r-   r   r   rK   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rI   rJ   r   attn_dropoutr   rn   target_dtyper   attn_weightss                               r.   r/   FalconFlashAttention2.forward  s    ((7	)-)F)Ft~~DL]L]040A0A)0L-)4):):&
L!Q!++Aq199*dnnVbdhdqdqr''1-55j,P\^b^k^kl	!++Aq199*lT`bfbobop=*HC%9+RUWZ%["K!,n=L}##C$<=%/%6%6yt~~_k%l"I "++Aq1''1-	!++Aq1UVV8<t{{443
 "''1<1C1C1H1HE1Qk((--W\%--'((** u&:;; ,,[9557  &?@@#{{BB#33::@@ >$ &..6K!\2I%..6K.% nn"AA

 #**:|T^^VZVcVcEcdjj. LL((r1   )r   r   )r3   r4   r5   r6   __doc__r\   r7   r8   r   r   r   r   r   r/   r9   r   r   s   @r.   r   r     s    R 48&*,0"'59KO[)||[) %[) 	[)
 u//0[) UO[) ELL)[) [)  [) !!1!12[) &eELL%,,,F&GH[) [)r1   r   c                   j   ^  \ rS rSrS\4U 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	FalconMLPi  rT   c                   > [         TU ]  5         UR                  n[        X!R                  UR
                  S9U l        [        UR                  5      U l	        [        UR                  X!R
                  S9U l
        UR                  U l        g )Nr   )r[   r\   r   r#   ffn_hidden_sizer+   dense_h_to_4hr   
activationactdense_4h_to_hr   )r,   rT   r   ri   s      r.   r\   FalconMLP.__init__  sp    (()+7M7MTZT_T_`!&"3"34)&*@*@+T_T_`$33r1   rA   r&   c                 h    U R                  U R                  U5      5      nU R                  U5      nU$ r(   )r  r  r  )r,   rA   s     r.   r/   FalconMLP.forward  s0    HHT''*+q!r1   )r  r  r  r   )r3   r4   r5   r6   r    r\   r7   r8   r/   r9   r   r   s   @r.   r  r    s/    4| 4 %,,  r1   r  )eagerr   flash_attention_2c                     ^  \ rS rSrSS\4U 4S jjjr       SS\R                  S\\R                     S\R                  S\\R                     S\\
\\\R                  \R                  4   4      S	\\R                     S
\S\S\\R                     S\\\R                  \R                  4      4S jjrSrU =r$ )FalconDecoderLayeri(  rT   c                 f  > [         TU ]  5         UR                  nUR                  U l        [
        UR                     " X5      U l        [        U5      U l	        UR                  U l
        Xl        UR                  c  UR                  (       a  SUl        UR                  (       d1  [        X1R                   S9U l        [        X1R                   S9U l        g UR                  S:X  a1  [        X1R                   S9U l        [        X1R                   S9U l        g [        X1R                   S9U l        g )Nr<   eps)r[   r\   r   r   r   FALCON_ATTENTION_CLASSESr   self_attentionr  mlpr   rT   num_ln_in_parallel_attnr   parallel_attnr   layer_norm_epsilonpost_attention_layernorminput_layernormln_attnln_mlp)r,   rT   r   r   ri   s       r.   r\   FalconDecoderLayer.__init__)  s    ((336v7R7RSTZfV$$33))1f6U6U-.F*##,5kG`G`,aD)#,[>W>W#XD --2(:S:ST'9R9RS'0B[B['\$r1   r-   r   r   rK   r   r   r   r   r   r   c                    UnU R                   R                  (       a=  U R                   R                  S:X  a#  U R                  U5      nU R	                  U5      nOU R                  U5      nU R                  UUUUUUUUU	U
S9
u  nnU R                   R                  (       dX  U R                   R                  (       a  UnO:[        XU R                   R                  U R                  S9nU R                  U5      nU R                   R                  (       a7  U R                   R                  (       a  U R                   R                  S:X  a  UnU R                  W5      nU R                   R                  (       d  U R                   R                  (       a  UU-  n[        UXR                   R                  U R                  S9nUU4$ )Nr<   )	r   r   rK   r   r   r   r   r   r   )r   r   )rT   r   r%  r*  r+  r)  r#  r&  r   r   r   r(  r$  r   )r,   r-   r   r   rK   r   r   r   r   r   r   r  r   attention_layernorm_outmlp_layernorm_outattention_outputr  
mlp_outputoutputs                      r.   r/   FalconDecoderLayer.forwardB  s|    !;;//DKK4W4W[\4\&*ll=&A# $M :&*&:&:=&I# *.)<)<#!)%/) 3 *= *
&, {{33{{(($;!&$0M0MX\XeXe %)$A$A($K! KK00))33q8 7 XX/0
;;//4;;3L3L**JZ;;3M3MX\XeXef|##r1   )	rT   r   r)  r*  r+  r$  r   r(  r#  r(   r   )r3   r4   r5   r6   r    r\   r7   r8   r   r   r   r   r   r   r/   r9   r   r   s   @r.   r  r  (  s   ]| ] ]< 48PT,0"'59KO<$||<$ %<$ 	<$
 u//0<$ U5%ell0J*K#KLM<$ ELL)<$ <$  <$ !!1!12<$ &eELL%,,,F&GH<$ <$r1   r  c                      ^  \ rS rSr% \\S'   SrSrS/rSr	Sr
SrU 4S jrS\R                  4S jr\SS	\4S
 jj5       rSrU =r$ )FalconPreTrainedModeli  rT   transformerTr  c                 &   > [         TU ]  " U0 UD6  g r(   )r[   r\   )r,   inputsr  ri   s      r.   r\   FalconPreTrainedModel.__init__  s    &+F+r1   modulec                    [        U[        R                  [        45      (       ak  UR                  R
                  R                  SU R                  R                  S9  UR                  b%  UR                  R
                  R                  5         gg[        U[        R                  5      (       ax  UR                  R
                  R                  SU R                  R                  S9  UR                  b2  UR                  R
                  UR                     R                  5         gg[        U[        5      (       aJ  UR                  R
                  R                  5         UR                  R
                  R                  S5        gg)zInitialize the weights.r   )meanstdNr   )r^   r   Linearr#   r)   datanormal_rT   initializer_ranger+   zero_	Embeddingpadding_idxr   fill_)r,   r:  s     r.   _init_weights#FalconPreTrainedModel._init_weights  s   fryy,788 MM&&CT[[5R5R&S{{&  &&( '--MM&&CT[[5R5R&S!!-""6#5#56<<> .	**KK""$MM$$S) +r1   hard_check_onlyc                 N    [        U SS5      nU(       a  U$ U(       d  SUl        U$ )Nuse_bettertransformerFr   )getattrr   )clsrT   rH  _is_bettertransformers       r.   _check_and_enable_sdpa,FalconPreTrainedModel._check_and_enable_sdpa  s*     '-De L M*0F'r1   r2   )F)r3   r4   r5   r6   r    r}   base_model_prefixsupports_gradient_checkpointing_no_split_modules_supports_flash_attn_supports_sdpa_can_compile_fullgraphr\   r   ModulerF  classmethodr   rN  r9   r   r   s   @r.   r5  r5    s_    %&*#-.N!,*BII *" T  r1   r5  c                     ^  \ rS rSrS\4U 4S jjrS rS\R                  4S jr	\
           SS\\R                     S\\\\\\R                  \R                  4   S	4   4      S
\\R                     S\\R                     S\\R                     S\\R                     S\\   S\\   S\\   S\\   S\\R                     S\\\R                  S	4   \4   4S jj5       rS
\R                  S\R                  S\R                  S\S\S\R                  S\R                  4S jr\S
\R                  S\S\S\R,                  S\R                  S\4S j5       rSrU =r$ )FalconModeli  rT   c           
        > [         TU ]  U5        UR                  U l        UR                  U l        UR                  U l        [        R                  " UR                  U R                  5      U l        [        R                  " [        UR                  5       Vs/ s H  n[        XS9PM     sn5      U l        [#        U R                  UR$                  S9U l        [)        US9U l        SU l        U R/                  5         g s  snf )N)r   r   rT   F)r[   r\   r   	embed_dimr   r   r   	use_alibir   rC  
vocab_sizeword_embeddings
ModuleListrangenum_hidden_layersr  hr   r'  ln_frQ   
rotary_embgradient_checkpointing	post_init)r,   rT   iri   s      r.   r\   FalconModel.__init__  s     ++33  "||F,=,=t~~N QVW]WoWoQpqQpA 26 GQpqr dnn&2K2KL	/v>&+# 	  rs   Dc                     U R                   $ r(   r_  )r,   s    r.   get_input_embeddings FalconModel.get_input_embeddings  s    ###r1   new_embeddingsc                     Xl         g r(   rk  r,   rn  s     r.   set_input_embeddings FalconModel.set_input_embeddings  s    -r1   	input_idspast_key_values.r   rK   r   inputs_embedsr   r   output_hidden_statesreturn_dictr   r&   c                 x   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
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c  U R                  U5      nU(       a  Uc  [        U R                   S9nSnUb  UR                  5       OSnUR                  u  pnU R                  (       aV  Uc2  [        R                   " XU-   4UR"                  [        R$                  S9OUn['        UU R(                  UR*                  S9nUc"  [        R,                  " XU-   UR"                  S	9nUc  UR/                  S5      nU R1                  X6XXU5      nU R3                  XPR                   R4                  5      nUnU R7                  UU5      nU(       a  S
OSnU	(       a  S
OSn[9        U R:                  5       H<  u  nnU	(       a  UU4-   nU" UUUUUU   UUUUUS9
nUS   nU(       d  M3  UUS   4-   nM>     U R=                  U5      nU	(       a  UU4-   nU
(       d  [?        S UUUU4 5       5      $ [A        UUUUS9$ )j  
input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`):
    `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values.get_seq_length()`
    (`sequence_length` of input past key value states). Indices of input sequence tokens in the vocabulary.

    If `past_key_values` is used, only `input_ids` that do not have their past calculated should be passed as
    `input_ids`.

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

    [What are input IDs?](../glossary#input-ids)
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   rp   rh   r2   )	r   r   rK   r   r   r   r   r   r   r   c              3   .   #    U  H  oc  M  Uv   M     g 7fr(   r2   ).0vs     r.   	<genexpr>&FalconModel.forward.<locals>.<genexpr>@  s      cacs   	)last_hidden_statert  r-   
attentions)!rT   r   rv  r   use_return_dictr   rf  r   r   r   r_  r   get_seq_lengthr?   r]  r7   onesrh   longr   r   rq   r   rF   _update_causal_maskget_head_maskrb  re  	enumeraterc  rd  r   r   )r,   rs  rt  r   rK   r   ru  r   r   rv  rw  r   r   past_key_values_lengthr   r   r   maskcausal_maskr-   r   all_self_attentionsall_hidden_statesrh  blockoutputss                             r.   r/   FalconModel.forward  s   8 2C1N-TXT_T_TqTq$8$D $++JjJj 	 "+!6IDKK<Q<Q	%0%<k$++B]B]-t";<YZZ&&4==##p "	  00;M0*$++>O ETE`!?!?!Afg$1$7$7!
>>
 ") 

.D!DEmNbNbjojtjt $  'tT^^=CVCVWE!"\\&(KTaThThN )33A6L..>L]jo
 &&y++2O2OP	% #oom\J$5b4"6BD!$&&)HAu#$58H$H!**)#A,#"3-$7G $AJM  &9WQZM&I#' *, 		-0 1]4D D )?<MObc   9+++*	
 	
r1   input_tensorr   c           
      H   U R                   R                  S:X  a  Ub  SU;   a  U$ g Ub  UR                  5       OSn[        U[        5      n	U R                   R                  S:X  a;  U	(       d4  U(       d-  Uc*  Uc'  [
        R                  " UUUU R                  S9(       a  g UR                  UR                  p[        R                  " U
5      R                  nUR                  u  pnU	(       a  UR                  5       nO2[        U[        R                  5      (       a  UR                  S   OX-   nU R!                  UUUU
UUUR                  S   S9nUcw  Ubt  UR"                  " US/UR                  SS  Q76 n[        R$                  " U[&        R(                  " U R                   R*                  U R,                  -  5      -  US:  U5      nU R                   R                  S:X  a;  Ub8  UR                  R.                  S	;   a  U(       d  [
        R0                  " UU5      nU$ )
Nr  r   r   r   )ru  r  is_trainingr;   )sequence_lengthtarget_lengthrq   rh   r   r   r   )r   xpunpu)rT   r   r  r^   r   r   _ignore_causal_mask_sdpar   rq   rh   r7   finfor   r?   get_max_cache_shaper8   5_prepare_4d_causal_attention_mask_with_cache_positionr   masked_fillr   r   r   r   rX   _unmask_unattended)r,   r   r  r   rt  r   r   r   past_seen_tokensusing_static_cacherq   rh   	min_dtyper   r  r   r  r  s                     r.   r  FalconModel._update_causal_maskK  s    ;;++/BB)c^.C%%
 @O?Z?99;`a'E KK,,6&%!%>>*'7 MM	 $**L,?,?vKK&**	)5););&
Q+??AM nell;; $$R(%7  PP+')#))!, Q 
 !2MM*bC5;;qr?CE++		$++"9"9T^^"KLLb K KK,,6*%%**.DD%
 1CCKQZ[Kr1   r  r  rq   r   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_valuerq   rh   r   )diagonalrz  r;   r   )r>   r7   r  r   fullrh   triur   r   rs   cloner?   rt   r  )r   r  r  rq   r   r   r  r  r  mask_lengthpadding_masks              r.   r  AFalconModel._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 r1   )r\  rf  rc  rd  r   re  r]  r_  )NNNNNNNNNNN)r3   r4   r5   r6   r    r\   rl  r7   r8   rq  r   r   r   r   r   r   r   r   r/   r  staticmethodintrq   r  r9   r   r   s   @r.   rY  rY    s3   | .$.5<< .  15ae15370448$(,0/3&*59~
E,,-~
 "%uU5<<;U5VX[5[/\(\"]^~
 !.	~

 u//0~
 E,,-~
   0 01~
 D>~
 $D>~
 'tn~
 d^~
 !!1!12~
 
uU\\3&')RR	S~
 ~
@UU llU 	U
 U  U <<U ||Un 444 4 {{	4
 4 4 4r1   rY  z
    The Falcon Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings).
    )custom_introc                      ^  \ rS rSrS/rS\4U 4S jjrS\R                  4S jr	\
             SS\\R                     S\\\\\\R                  \R                  4   S	4   4      S
\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S\\   S\\   S\\   S\\   S\\R                     S\\\R                  4   S\\\R                     \4   4S jj5       rSrU =r$ )FalconForCausalLMi  zlm_head.weightrT   c                    > [         TU ]  U5        [        U5      U l        [        R
                  " UR                  UR                  SS9U l        U R                  5         g NFr   )
r[   r\   rY  r6  r   r>  r   r^  lm_headrg  r,   rT   ri   s     r.   r\   FalconForCausalLM.__init__  sI     &v.yy!3!3V5F5FUS 	r1   rn  c                     Xl         g r(   )r  rp  s     r.   set_output_embeddings'FalconForCausalLM.set_output_embeddings  s    %r1   rs  rt  .r   rK   r   ru  labelsr   r   rv  rw  r   logits_to_keepr&   c                    Ub  UOU R                   R                  nU R                  UUUUUUUU	U
UUS9nUS   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(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  UR                  S9$ )a  
input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`):
    `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values.get_seq_length()`
    (`sequence_length` of input past key value states). Indices of input sequence tokens in the vocabulary.

    If `past_key_values` is used, only `input_ids` that do not have their past calculated should be passed as
    `input_ids`.

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

    [What are input IDs?](../glossary#input-ids)
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
    Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
    `labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
    are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`
N)
rt  r   rK   r   ru  r   r   rv  rw  r   r   r^  r   losslogitsrt  r-   r  )rT   r  r6  r^   r  slicer  loss_functionr^  r   rt  r-   r  )r,   rs  rt  r   rK   r   ru  r  r   r   rv  rw  r   r  r  transformer_outputsr-   slice_indices	lm_logitsr  r2  s                        r.   r/   FalconForCausalLM.forward  s3   H &1%<k$++B]B]"..+)%'/!5#) / 
 ,A.8B>SV8W8W~ot4]kLLq-/B!CD	%%  ;;11 	D \$7$;;F)-)9TGf$EvE0/??-;;*55
 	
r1   )r  r6  )NNNNNNNNNNNNr   )r3   r4   r5   r6   _tied_weights_keysr    r\   r7   r8   r  r   r   r   r   r   r   r   r  r   r/   r9   r   r   s   @r.   r  r    s    ++| &ELL &  15ae1537,004)-$(,0/3&*5934J
E,,-J
 "%uU5<<;U5VX[5[/\(\"]^J
 !.	J

 u//0J
 ELL)J
  -J
 &J
 D>J
 $D>J
 'tnJ
 d^J
 !!1!12J
 c5<</0J
  
uU\\"$EE	F!J
 J
r1   r  a  
    The Falcon Model transformer with a sequence classification head on top (linear layer).

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

    Since it does classification on the last token, it requires to know the position of the last token. If a
    `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
    no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
    padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
    each row of the batch).
    c                   L  ^  \ rS rSrS\4U 4S jjr\          SS\\R                     S\\
   S\\R                     S\\R                     S\\R                     S	\\R                     S
\\   S\\   S\\   S\\   S\\\R                     \4   4S jj5       rSrU =r$ )FalconForSequenceClassificationi<  rT   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 r  )
r[   r\   
num_labelsrY  r6  r   r>  r   scorerg  r  s     r.   r\   (FalconForSequenceClassification.__init__K  sV      ++&v.YYv1163D3D5Q
 	r1   rs  rt  r   r   ru  r  r   r   rv  rw  r&   c                    U
b  U
OU R                   R                  n
U R                  UUUUUUUU	U
S9	nUS   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Gbg  U R                   R"                  c  U R$                  S:X  a  S
U R                   l        OoU R$                  S:  aN  UR&                  [        R(                  :X  d  UR&                  [        R*                  :X  a  SU R                   l        OSU R                   l        U R                   R"                  S
:X  aJ  [-        5       nU R$                  S:X  a&  U" UR/                  5       UR/                  5       5      nOeU" UU5      nO[U R                   R"                  S:X  a  [1        5       nU" UU5      nO-U R                   R"                  S:X  a  [3        5       nU" UU5      nU
(       d  U4USS -   nUb  U4U-   $ U$ [5        UUUR6                  UR8                  UR:                  S9$ )  
input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`):
    `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values.get_seq_length()`
    (`sequence_length` of input past key value states). Indices of input sequence tokens in the vocabulary.

    If `past_key_values` is used, only `input_ids` that do not have their past calculated should be passed as
    `input_ids`.

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

    [What are input IDs?](../glossary#input-ids)
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).
Nrt  r   r   ru  r   r   rv  rw  r   r   z=Cannot handle batch sizes > 1 if no padding token is defined.r;   r   z will not detect padding tokens in `inputs_embeds`. Results may be unexpected if using padding tokens in conjunction with `inputs_embeds.`rz  
regressionsingle_label_classificationmulti_label_classificationr  )rT   r  r6  r  r?   pad_token_idr   rt   rh   r7   r   r   argmaxr   r   ri   r3   problem_typer  rq   r  r  r	   squeezer   r   r   rt  r-   r  )r,   rs  rt  r   r   ru  r  r   r   rv  rw  r  r-   r  r   last_non_pad_tokennon_pad_masktoken_indicespooled_logitsr  loss_fctr2  s                         r.   r/   'FalconForSequenceClassification.forwardT  s   @ &1%<k$++B]B]"..+)'/!5# / 

 ,A.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{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#M$9$9$;V^^=MND#M6:D))-JJ+-v6))-II,.v6#%(;AB(??F)-)9TGf$EvE/ /??-;;*55
 	
r1   )r  r  r6  
NNNNNNNNNN)r3   r4   r5   r6   r    r\   r   r   r7   r   r   r8   r   r   r   r   r/   r9   r   r   s   @r.   r  r  <  s   |   15+/15,004)-$(,0/3&*g
E,,-g
 "%g
 !.	g

 ELL)g
  -g
 &g
 D>g
 $D>g
 'tng
 d^g
 
uU\\"$DD	Eg
 g
r1   r  c                   L  ^  \ rS rSrS\4U 4S jjr\          SS\\R                     S\\
   S\\R                     S\\R                     S\\R                     S	\\R                     S
\\   S\\   S\\   S\\   S\\\R                     \4   4S jj5       rSrU =r$ )FalconForTokenClassificationi  rT   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  rY  r6  rK  r  r   r   r   r   r>  r   
classifierrg  )r,   rT   r  ri   s      r.   r\   %FalconForTokenClassification.__init__  s      ++&v.6/6B!'!:!:V-t4@!'!6!6!$zz"45))F$6$68I8IJ 	r1   rs  rt  r   r   ru  r  r   r   rv  rw  r&   c                    U
b  U
OU R                   R                  n
U R                  UUUUUUUU	U
S9	nUS   nU R                  U5      nU R	                  U5      nSnUbQ  UR
                  u  nn[        5       nU" UR                  UU-  U R                  5      UR                  UU-  5      5      nU
(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  S9$ )r  Nr  r   r<   )r  r  r-   r  )rT   r  r6  r   r  r?   r   r   r  r   r-   r  )r,   rs  rt  r   r   ru  r  r   r   rv  rw  r  r-   r  r  r   r   r  r2  s                      r.   r/   $FalconForTokenClassification.forward  s   @ &1%<k$++B]B]"..+)'/!5# / 

 ,A.]3/%+\\"J
')HJ3T__Ev{{S]`jSjGkD Y!4QR!88F)-)9TGf$EvE$-;;*55	
 	
r1   )r  r   r  r6  r  )r3   r4   r5   r6   r    r\   r   r   r7   r   r   r8   r   r   r   r   r/   r9   r   r   s   @r.   r  r    s   | "  15+/15,004)-$(,0/3&*B
E,,-B
 "%B
 !.	B

 ELL)B
  -B
 &B
 D>B
 $D>B
 'tnB
 d^B
 
uU\\"$99	:B
 B
r1   r  c                   2  ^  \ rS rSrU 4S jr\         SS\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S	\\
   S
\\
   S\\
   S\\\4   4S jj5       rSrU =r$ )FalconForQuestionAnsweringi  c                    > [         TU ]  U5        [        U5      U l        [        R
                  " UR                  S5      U l        U R                  5         g )Nr<   )	r[   r\   rY  r6  r   r>  r   
qa_outputsrg  r  s     r.   r\   #FalconForQuestionAnswering.__init__  sA     &v.))F$6$6: 	r1   rs  r   r   ru  start_positionsend_positionsr   rv  rw  r&   c
           
         U	b  U	OU R                   R                  n	U R                  UUUUUUU	S9n
U
S   nU R                  U5      nUR	                  SSS9u  pUR                  S5      R                  5       nUR                  S5      R                  5       nSnUb  Ub  [        UR                  5       5      S:  a  UR                  S5      n[        UR                  5       5      S:  a  UR                  S5      nUR                  S5      nUR                  SU5      nUR                  SU5      n[        US9nU" X5      nU" X5      nUU-   S-  nU	(       d  X4U
SS -   nUb  U4U-   $ U$ [        UUUU
R                  U
R                  S	9$ )
ry  N)r   r   ru  r   rv  rw  r   r   r;   r=   )ignore_indexr<   )r  start_logits
end_logitsr-   r  )rT   r  r6  r  splitr  r   lensizeclampr   r   r-   r  )r,   rs  r   r   ru  r  r  r   rv  rw  r  sequence_outputr  r  r  
total_lossignored_indexr  
start_lossend_lossr2  s                        r.   r/   "FalconForQuestionAnswering.forward"  s   4 &1%<k$++B]B]"")'/!5# # 
 "!*1#)<<r<#: #++B/::<''+668

&=+D?'')*Q."1"9"9""==%%'(1, - 5 5b 9(--a0M-33A}EO)//=AM']CH!,@J
:H$x/14J"/'!"+=F/9/EZMF*Q6Q+%!!//))
 	
r1   )r  r6  )	NNNNNNNNN)r3   r4   r5   r6   r\   r   r   r7   r   FloatTensorr   r   r   r   r/   r9   r   r   s   @r.   r  r    s      156:15596:48,0/3&*G
E,,-G
 !!2!23G
 E--.	G

   1 12G
 "%"2"23G
   0 01G
 $D>G
 'tnG
 d^G
 
u22	3G
 G
r1   r  )r  rY  r5  r  r  r  )Nr   )Jr  r   typingr   r   r7   r   torch.nnr   r   r   r	   r
   r   activationsr   cache_utilsr   r   r   
generationr   modeling_attn_mask_utilsr   modeling_flash_attention_utilsr   r   modeling_layersr   modeling_outputsr   r   r   r   r   modeling_rope_utilsr   r   modeling_utilsr   utilsr   r   configuration_falconr    r!   
get_loggerr3   r   r>  r#   rD   rO   rV  rQ   r8   r  rq   r   rr   r   r   r   r   r  r"  r  r5  rY  r  r  r  r  __all__r2   r1   r.   <module>r
     s:     "   L L $ ) ; ; ) i 9  L - / J			H	%
)299 )(8!<BII !<HJu|| J JEKK J\a\h\h J:5<< 5<< u PT Y^YeYe &g0bii g0Tj)O j)Z		 " . V$3 V$r &O & &R l' l l^	 
Y
- Y

Y
x r
&; r
r
j U
#8 U
 U
p Q
!6 Q
 Q
hr1   