
    fTh"              
          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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  SSKJr  SSK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&  \RN                  " \(5      r) " S S\	RT                  5      r+S r,S<S jr-S\R\                  S\/S\R\                  4S jr0S\R\                  S\/S\/S\\R\                     S\R\                  4
S jr1 " S  S!\	RT                  5      r2 " S" S#\25      r3 " S$ S%\25      r4\2\3\4S&.r5 " S' S(\	RT                  5      r6 " S) S*\	RT                  5      r7 " S+ S,\	RT                  5      r8 " S- S.\	RT                  5      r9 " S/ S0\	RT                  5      r: " S1 S2\	RT                  5      r;\ " S3 S4\5      5       r<\ " S5 S6\<5      5       r=\" S7S89 " S9 S:\<\5      5       r>/ S;Qr?g)=zPyTorch DBRX model.    N)AnyOptionalTupleUnion)nn   )ACT2FN)CacheDynamicCacheStaticCache)GenerationMixin)AttentionMaskConverter)!flash_attn_supports_top_left_maskis_flash_attn_available)MoeCausalLMOutputWithPastMoeModelOutputWithPast)PreTrainedModel)auto_docstringis_torch_flex_attn_availablelogging   )
DbrxConfig)	BlockMask)make_flex_block_causal_mask)_flash_attention_forwardc                   ^   ^  \ rS rSrSU 4S jjr\R                  " 5       SS j5       rSrU =r	$ )DbrxRotaryEmbedding/   c           	        > [         TU ]  5         Xl        X l        X0l        SU R                  [
        R                  " SU R                  S[
        R                  S9R                  5       U R                  -  -  -  nU R                  SUSS9  g )N      ?r      dtypeinv_freqF)tensor
persistent)
super__init__dimmax_position_embeddingsbasetorcharangeint64floatregister_buffer)selfr)   r*   r+   devicer$   	__class__s         ^/var/www/auris/envauris/lib/python3.13/site-packages/transformers/models/dbrx/modeling_dbrx.pyr(   DbrxRotaryEmbedding.__init__0   sr    '>$	$))Q!5;;(W(](](_bfbjbj(jklZUK    c                    U R                   R                  UR                  5        U R                   S S S 2S 4   R                  5       R	                  UR
                  S   SS5      nUS S 2S S S 24   R                  5       nUR                  R                  n[        U[        5      (       a  US:w  a  UOSn[        R                  " USS9   UR                  5       UR                  5       -  R                  SS5      n[        R                  " Xw4SS	9nUR                  5       n	UR                  5       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enabledr!   r)   r"   )r$   tor2   r/   expandshapetype
isinstancestrr,   autocast	transposecatcossinr#   )r1   xposition_idsseq_leninv_freq_expandedposition_ids_expandedr;   freqsembrG   rH   s              r4   forwardDbrxRotaryEmbedding.forward:   s7    	" MM$4-8>>@GGHZHZ[\H]_acde ,QaZ 8 > > @ hhmm%/S%A%AkUZFZk`e^^UC&,,.1F1L1L1NNYYZ[]^_E))UN3C'')C'')C	 D
 vvAGGv$cff177f&;;; DCs   
A(E,,
E:)r+   r)   r*   )i   i'  NN)
__name__
__module____qualname____firstlineno__r(   r,   no_gradrP   __static_attributes____classcell__r3   s   @r4   r   r   /   s#    L ]]_< <r6   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..Nr8   r!   r=   )r@   r,   rF   )rI   x1x2s      r4   rotate_halfr^   M   sZ    	
3"!''"+"""	#B	
3q ""	#B99rc2YB''r6   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.
)	unsqueezer^   )qkrG   rH   rJ   unsqueeze_dimq_embedk_embeds           r4   apply_rotary_pos_embrf   U   sS    ( --
&C
--
&Cw;q>C/0Gw;q>C/0Gr6   hidden_states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)r@   r?   reshape)rg   rh   batchnum_key_value_headsslenhead_dims         r4   	repeat_kvrp   q   s_    
 2?1D1D.Ez!!Qa"23::5W\dlmM  e(CTTTr6   gate_logitsnum_expertstop_kattention_maskc                    U b  [        U [        5      (       d  [        R                  " S5      $ [        U [        5      (       aC  U S   R                  n[        R
                  " U  Vs/ s H  oUR                  U5      PM     snSS9n[        R                  R                  R                  WSS9n[        R                  " XrSS9u  p[        R                  R                  R                  X5      n
Uc:  [        R                  " U
R                  5       SS9n[        R                  " USS9nGOUR                  u  pUR                  S   X-  -  nUSSS2SS2SS4   R                  XXU45      R!                  SX!5      R                  W5      n[        R"                  " U
R                  5       U-  SS9[        R"                  " USS9-  nUSSS2SS2S4   R                  XX45      R!                  SU5      R                  U5      n[        R"                  " UU-  SS9[        R"                  " USS9-  n[        R"                  " XR%                  S5      -  5      nUU-  $ s  snf )ao  Computes auxiliary load balancing loss as in Switch Transformer - implemented in Pytorch.

See Switch Transformer (https://arxiv.org/abs/2101.03961) for more details. This function implements the loss
function presented in equations (4) - (6) of the paper. It aims at penalizing cases where the routing between
experts is too unbalanced.

Args:
    gate_logits (Union[`torch.Tensor`, Tuple[torch.Tensor]):
        Logits from the `gate`, should be a tuple of model.config.num_hidden_layers tensors of
        shape [batch_size X sequence_length, num_experts].
    num_experts (`int`):
        Number of experts.
    top_k (`int`):
        The number of experts each token is routed to.
    attention_mask (`torch.Tensor`, *optional*):
        The attention_mask used in forward function
        shape [batch_size X sequence_length] if not None.

Returns:
    The auxiliary loss.
N        r   r=   r8   )rB   tupler,   r%   r2   rF   r>   r   
functionalsoftmaxtopkone_hotmeanr/   r@   r?   rk   sumr`   )rq   rr   rs   rt   compute_device
layer_gateconcatenated_gate_logitsrouting_weights_selected_expertsexpert_masktokens_per_expertrouter_prob_per_expert
batch_sizesequence_lengthnum_hidden_layersexpert_attention_mask router_per_expert_attention_maskoverall_losss                      r4   load_balancing_loss_funcr   }   s7   6 *[%"@"@||C  +u%%$Q..#(99^i-j^iPZmmN.K^i-jpq#r hh))112JPR1SO**_DA((%%--.>LK!JJ{'8'8':B "'O!C&4&:&:#
4::1=*B^_ 4AtT12V&OKXYWR,R	 	 "IIk&7&7&9<Q&QWXY\a\e\e!q]
 
 4At+,V&OQRWR%R	 	) "'?=]+]cd!ehmhqhq,!i
 "
 99.1Q1QRS1TTUL+%%[ .ks   'I 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\S\\	R                  \\	R                     \\   4   4S jjrSrU =r$ )DbrxAttention   zMulti-head self attention.config	block_idxc                   > [         TU ]  5         Xl        UR                  U l        UR
                  U l        U R                  U R                  -  U l        UR                  U l	        X l
        Uc3  [        R                  SU R                  R                   S3S-   S-   5        UR                  nUR                   U l        UR"                  U l        UR$                  U l        U R                  U R&                  -  U l        UR*                  U l        SU l        [.        R0                  " U R                  U R                  SU R&                  -  U R                  -  -   SS9U l        [.        R0                  " U R                  U R                  SS9U l        [7        U R                  U R                  U R*                  S	9U l        g )
NzInstantiating z; without passing a `block_idx` is not recommended and will zelead to errors during the forward call if caching is used. Please make sure to provide a `block_idx` zwhen creating this class.Tr!   Fbias)r*   r+   )r'   r(   r   d_modelhidden_sizen_heads	num_headsro   max_seq_lenr*   r   loggerwarning_oncer3   rS   attn_config
attn_pdropclip_qkv
kv_n_headsrm   num_key_value_groups
rope_theta	is_causalr   LinearWqkvout_projr   
rotary_emb)r1   r   r   r   r3   s       r4   r(   DbrxAttention.__init__   su   !>>((DNN:'-'9'9$" !8!8 99tuyz-. ((%00#,,#.#9#9 $(NNd6N6N$N!%00IId..T5M5M1MPTP]P]1]]di
	 		$"2"2D4D4D5Q-MM$($@$@
r6   rg   rJ   rt   past_key_valueoutput_attentions	use_cachecache_positionkwargsri   c                    UR                  5       u  pnU R                  U5      nU R                  b  U R                  * OS nU R                  nUR                  XS9nUR	                  U R
                  U R                  U R                  -  U R                  U R                  -  /SS9u  nn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                  UU5      u  nn[        UUUU5      u  nnUb'  UUUS.nUR                  UUU R                  U5      u  nn[        UU R                   5      n[        UU R                   5      n["        R$                  " UUR                  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[,        R.                  R1                  US["        R2                  S	9R5                  UR6                  5      n[,        R.                  R9                  UU R:                  U R<                  S
9n["        R$                  " UU5      nUR                  5       XR                  XR                  4:w  a9  [?        SXR                  XR                  4 S3SUR                  5        3-   5      eUR                  SS5      RA                  5       nURC                  XU R
                  5      nU RE                  U5      nU(       d  S nUUU4$ )Nminmaxr!   r=   r   rH   rG   r   r   r8   r)   r#   ptrainingz `attn_output` should be of size z, but is )#sizer   r   clampsplitr   rm   ro   viewr   rE   r   rf   updater   rp   r   r,   matmulmathsqrtr@   r   rx   ry   float32r>   r#   dropoutr   r   
ValueError
contiguousrk   r   )r1   rg   rJ   rt   r   r   r   r   r   bszq_lenr   
qkv_statesmin_valmax_valquery_states
key_statesvalue_statesrG   rH   cache_kwargsattn_weightscausal_maskattn_outputs                           r4   rP   DbrxAttention.forward   s>    &**,AYY}-
$(MM$=4==.4--%%'%?
1;1A1A  ((4==8((4==8
  2B 2
.j, $((T^^T]]S]]^_abc__S1I1I4==Yccdeghi
#((T5M5Mt}}]gghiklm??<>S#7jRUWZ#[ j%#&snUL'5'<'<ZW[WeWegs't$Jz4+D+DE
 t/H/HI||L*2F2Fq!2LMPTPYPYZ^ZgZgPhh%(Aq2HJ4D4DR4H2H)HIK'+5L }},,\r,WZZ[g[m[mn}},,\T__W[WdWd,ell<>#~~umm!LL2CP]P]3^2__ghk&&()*+ 
 "++Aq1<<>!))#d6F6FGmmK0 LL.88r6   )r   r   r   r   r   ro   r   r   r*   r   r   rm   r   r   r   rR   NNFFN)rS   rT   rU   rV   __doc__r   r   intr(   r,   Tensor
LongTensorr
   boolr   r   rP   rX   rY   rZ   s   @r4   r   r      s    $
z 
hsm 
 
J 26*."'59B9||B9 &&B9 !.	B9
 !B9  B9 B9 !!1!12B9 B9 
u||Xell3Xe_D	EB9 B9r6   r   c                   ,  ^  \ 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\S\\R                  \\R                     \\\R                        4   4S jjrSrU =r$ )DbrxFlashAttention2i6  zDbrx flash attention module.

This module inherits from `DbrxAttention` as the weights of the module stays
untouched. The only required change would be on the forward pass where it
calls the public API of flash attention.
c                 D   > [         TU ]  " U0 UD6  [        5       U l        g rR   )r'   r(   r   _flash_attn_uses_top_left_mask)r1   argsr   r3   s      r4   r(   DbrxFlashAttention2.__init__>  s#    $)&)
 /P.Q+r6   rg   rt   rJ   r   r   r   r   r   ri   c                    [        U[        5      (       a  [        S5      e[        R	                  S5        SnUR                  5       u  pnU R                  U5      nU R                  b%  UR                  U R                  * U R                  S9nUR                  U R                  U R                  U R                  -  U R                  U R                  -  /SS9u  pn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!                  X5      u  nn[#        XUU5      u  pUb%  UU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*                  OS	nUR,                  nU[.        R0                  :X  a  [.        R2                  " 5       (       a  [.        R4                  " 5       nO>[7        U R8                  S
5      (       a  U R8                  R:                  nOUR,                  n[        R=                  SSU S3-   5        UR?                  U5      nUR?                  U5      nUR?                  U5      n[A        UUUUU
UUU RB                  U RD                  S9	nURG                  XU R                  5      RI                  5       nU RK                  U5      nU(       d  S nUWU4$ )Nz`static` cache implementation is not compatible with `attn_implementation==flash_attention_2` make sure to use `sdpa` in the mean time, and open an issue at https://github.com/huggingface/transformerszZImplicitly setting `output_attentions` to False as it is not supported in Flash Attention.Fr   r!   r=   r   r   rv   _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 z(float32. We will cast back the input in .)rJ   r   r   use_top_left_mask)&rB   r   r   r   infor   r   r   r   r   r   rm   ro   r   r   rE   r   rf   r   r   r   r   r#   r,   r   is_autocast_enabledget_autocast_gpu_dtypehasattrr   r   r   r>   r   r   r   rk   r   r   )r1   rg   rt   rJ   r   r   r   r   r   r   r   r   r   r   r   r   rG   rH   r   dropout_rateinput_dtypetarget_dtyper   r   s                           r4   rP   DbrxFlashAttention2.forwardF  s!    nk22}  	pq!%**,AYY}-
==$#))t}}n$--)PJ1;1A1A  ((4==8((4==8
  2B 2
., $((T^^T]]S]]^_abc__S1I1I4==Yccdeghi
#((T5M5Mt}}]gghiklm??<>S#7RUWZ#[ %#&snUL'5'<'<ZW[WeWegs't$J
 $--a3))!Q/
#--a3*.--tS #((%--'((**$;;=&?@@#{{BB+11]<\N!LM (??<8L#|4J'??<8L.% nn"AA

 "))#d6F6FGRRTmmK0 LL.88r6   )r   NNNFFN)rS   rT   rU   rV   r   r(   r,   r   r   r   r
   r   r   r   rP   rX   rY   rZ   s   @r4   r   r   6  s    R 6:37*."'59e9||e9 !!1!12e9 u//0	e9
 !e9  e9 e9 !!1!12e9 e9 
u||Xell3XeELL>Q5RR	Se9 e9r6   r   c                   "  ^  \ rS rSrSr      SS\R                  S\\R                     S\\R                     S\\	   S\
S\
S	\\R                     S
\\R                  \\R                     \\\R                        4   4U 4S jjjrSrU =r$ )DbrxSdpaAttentioni  z
Dbrx attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from
`DbrxAttention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to
SDPA API.
rg   rt   rJ   r   r   r   r   ri   c           
        > U(       a(  [         R                  S5        [        TU ]  UUUUUUUS9$ UR	                  5       u  pn
U R                  U5      nU R                  b%  UR                  U R                  * U R                  S9nUR                  U R                  U R                  U R                  -  U R                  U R                  -  /SS9u  pn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                  XS S9u  nn[!        XUUS 5      u  pUb$  UXS.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R*                  S	   24   nUR,                  R.                  S
:X  a3  Ub0  UR1                  5       nUR1                  5       nUR1                  5       nUc  U	S:  a  SOSn[2        R4                  R6                  R9                  UUUUU R:                  (       a  U R<                  OSUS9nUR                  SS5      R1                  5       nUR                  XS5      nU R?                  U5      nUS U4$ )Na  DbrxModel is using DbrxSdpaAttention, 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.rg   rt   rJ   r   r   r   r   r   r!   r=   r   )rK   r   r   cudaTFrv   )	attn_mask	dropout_pr   r8   ) r   r   r'   rP   r   r   r   r   r   r   rm   ro   r   r   rE   r   rf   r   r   rp   r   r@   r2   rA   r   r,   r   rx   scaled_dot_product_attentionr   r   r   )r1   rg   rt   rJ   r   r   r   r   r   r   r   r   r   r   r   rG   rH   r   r   r   r   r3   s                        r4   rP   DbrxSdpaAttention.forward  s    [ 7?+-)-"3#- #   &**,AYY}-
==$#))t}}n$--)PJ1;1A1A  ((4==8((4==8
  2B 2
., $((T^^T]]S]]^_abc__S1I1I4==Yccdeghi
#((T5M5Mt}}]gghiklm??<t?LS#7RUWZ\`#a %#&sUL'5'<'<ZW[WeWegs't$Jz+D+DE
 /H/HI$%%aA/E1A1A"1E/E&EFK ##v-+2I'224L#..0J'224L (/EAID5	hh))FF!)-dooC G 
 "++Aq1<<>!&&s26mmK0D.00r6    r   )rS   rT   rU   rV   r   r,   r   r   r   r
   r   r   rP   rX   rY   rZ   s   @r4   r   r     s     2637*."'59U1||U1 !.U1 u//0	U1
 !U1  U1 U1 !!1!12U1 
u||Xell3XeELL>Q5RR	SU1 U1r6   r   )eagerflash_attention_2sdpac                   2  ^  \ 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\S\\R                  \R                  \\R                     \\   4   4S jjrSrU =r$ )DbrxNormAttentionNormi  r   r   c                   > [         TU ]  5         X l        UR                  U l        [        R
                  " UR                  SS9U l        [        UR                     " UUS9U l
        [        R
                  " UR                  SS9U l        g )NFr   r   r   )r'   r(   r   resid_pdropr   	LayerNormr   norm_1DBRX_ATTENTION_CLASSES_attn_implementationattnnorm_2r1   r   r   r3   s      r4   r(   DbrxNormAttentionNorm.__init__  sl    "!--ll6>>>*6+F+FG
	 ll6>>>r6   rg   rJ   rt   r   r   r   r   r   ri   c                 `   Un	U R                  U5      R                  UR                  5      nU R                  " SUUUUUUUS.UD6u  pn[        R
                  R                  XR                  U R                  S9nX-   nUn	U R                  U5      R                  UR                  5      nXX4$ )Nr   r   r   )
r   r>   r#   r  r   rx   r   r   r   r  )r1   rg   rJ   rt   r   r   r   r   r   residual_statesr   s              r4   rP   DbrxNormAttentionNorm.forward   s     (M255m6I6IJ6:ii 	7
')%)/)	7
 	7
3^ --m?O?OZ^ZgZg-h%7'M255m6I6IJ|KKr6   )r  r   r   r  r   rR   r   )rS   rT   rU   rV   r   r   r   r(   r,   r   r   r
   r   r   r   rP   rX   rY   rZ   s   @r4   r   r     s    	?z 	?hsm 	? 	? 26*."'59L||L &&L !.	L
 !L  L L !!1!12L L 
u||U\\8ELL+A8E?R	SL Lr6   r   c                      ^  \ rS rSrS\S\S\S\\   S\\   4
U 4S jjrS\R                  S	\
\R                  \R                  \R                  4   4S
 jrSrU =r$ )
DbrxRouteriB  r   moe_num_experts	moe_top_kmoe_jitter_epsmoe_normalize_expert_weightsc                    > [         TU ]  5         Xl        X l        X0l        X@l        XPl        [        R                  " U R                  U R                  SS9U l	        g NFr   )
r'   r(   r   r
  r  r  r  r   r   layer)r1   r   r
  r  r  r  r3   s         r4   r(   DbrxRouter.__init__C  sM     	&.",,H)YYt//1E1EER
r6   rg   ri   c                 x   U R                   (       aP  U R                  bC  U[        R                  " U5      R	                  SU R                  -
  SU R                  -   5      -  nUR                  SUR                  S   5      nU R                  U5      R                  S[        R                  S9n[        R                  " X R                  SS9u  p4U R                  b   [        R                  " X0R                  SSS9OSnX5-  nUR                  UR                  5      nUR                  UR                  5      nX#U4$ )Nr    r8   r   r=   T)r   r)   keepdim)r   r  r,   
empty_likeuniform_r   r@   r  ry   r   rz   r  r  normr>   r#   )r1   rg   weightstop_weightstop_expertstop_weights_scales         r4   rP   DbrxRouter.forwardT  s   ==T00<U--m<EEd)))31D1D+D M &**2}/B/B2/FG**]+33%--3P#(::g~~2#N  00< JJ{&G&GRY]^ 	
 "5**]001!nn]%8%89[00r6   )r   r  r  r  r
  r  )rS   rT   rU   rV   r   r   r/   r(   r,   r   r   r   rP   rX   rY   rZ   s   @r4   r	  r	  B  s}    SS S 	S
 !S '/uoS"1U\\ 1eELL%,,X]XhXh<h6i 1 1r6   r	  c            
          ^  \ rS rSrS\S\S\S\4U 4S jjrS\R                  S\R                  S	\R                  S
\R                  S\R                  4
S jr	Sr
U =r$ )DbrxExpertGLUii  r   ffn_hidden_sizer
  
ffn_act_fnc                   > [         TU ]  5         Xl        X l        X0l        [
        R                  " [        R                  " X2-  U5      5      U l	        [
        R                  " [        R                  " X2-  U5      5      U l
        [
        R                  " [        R                  " X2-  U5      5      U l        UR                  SS5      n[        U   U l        g )Nnamesilu)r'   r(   r   r  r
  r   	Parameterr,   emptyw1v1w2getr	   activation_fn)r1   r   r  r
  r  act_fn_namer3   s         r4   r(   DbrxExpertGLU.__init__j  s    &..,,u{{?+LkZ[,,u{{?+LkZ[,,u{{?+LkZ[ nnVV4#K0r6   rI   	expert_w1	expert_v1	expert_w2ri   c                     UR                  UR                  5       5      nUR                  UR                  5       5      nU R                  U5      nXV-  nUR                  U5      nU$ rR   )r   tr)  )	r1   rI   r,  r-  r.  	gate_projup_projintermediate_states	down_projs	            r4   rP   DbrxExpertGLU.forwardw  s[     HHY[[]+	((9;;=)&&y1	'1'..y9	r6   )r)  r  r   r
  r&  r%  r'  )rS   rT   rU   rV   r   dictr(   r,   r   rP   rX   rY   rZ   s   @r4   r  r  i  sn    1C 1# 1PS 1ae 1*/,,CH<<\a\h\h	 r6   r  c            
          ^  \ rS rSrS\S\S\S\4U 4S jjrS\R                  S\R                  S	\R                  S
\R                  S\R                  4
S jr
SrU =r$ )DbrxExpertsi  r   r  r
  r  c                 P   > [         TU ]  5         X0l        [        UUUUS9U l        g )Nr   r  r
  r  )r'   r(   r
  r  mlp)r1   r   r  r
  r  r3   s        r4   r(   DbrxExperts.__init__  s,    . #++!	
r6   rI   r  r  r  ri   c                     UR                   u  pVnUR                  SU5      n[        R                  " U5      n[        R
                  R                  X@R                  S9R                  SSS5      n	U R                  R                  R                  U R                  R                  U R                  R                  U R                  R                  5      R                  U R                  SS9n
U R                  R                  R                  U R                  R                  U R                  R                  U R                  R                  5      R                  U R                  SS9nU R                  R                  R                  U R                  R                  U R                  R                  U R                  R                  5      R                  U R                  SS9nU
 Vs/ s H  oR!                  SS9PM     n
nU Vs/ s H  oR!                  SS9PM     nnU Vs/ s H  oR!                  SS9PM     nn[#        SU R                  5       H  n[        R$                  " U	U   5      u  nnUR                   S   S:X  a  M4  UnUnUS U4   R'                  SU5      nU R                  UU
U   UU   UU   5      UUUS 4   -  nUR)                  SUU5        M     UR'                  XVU5      nU$ s  snf s  snf s  snf )Nr8   )num_classesr!   r   r   r=   )r@   r   r,   
zeros_liker   rx   r{   r
  permuter;  r%  r  r   chunkr&  r'  squeezerangewhererk   
index_add_)r1   rI   r  r  r  r   r   r   outr   
w1_chunked
v1_chunked
w2_chunkedr%  r&  r'  
expert_idxtopk_idx	token_idx
token_list	topk_listexpert_tokens
expert_outs                          r4   rP   DbrxExperts.forward  s    #$''KFF2{#q!mm++KEYEY+ZbbcdfgijkXX[[%%dhh&>&>@X@XZ^ZbZbZnZnouu  a v 

 XX[[%%dhh&>&>@X@XZ^ZbZbZnZnouu  a v 

 XX[[%%dhh&>&>@X@XZ^ZbZbZnZnouu  a v 

 3==*BjjQj'*
=2<=*BjjQj'*
=2<=*BjjQj'*
=4#7#78J #(++k*.E"FHiq!Q&"J IdJ./77KHM
:(>
:@VXbcmXnoj)T9:; 
 NN1i4! 9$ kk#k2
- >==s   %K1K6!K;)r;  r
  )rS   rT   rU   rV   r   r6  r(   r,   r   r   rP   rX   rY   rZ   s   @r4   r8  r8    sn    
C 
# 
PS 
ae 
(((-(CH<<(^c^n^n(	( (r6   r8  c                      ^  \ rS rSrS\4U 4S jjrS\R                  S\\R                  \R                  4   4S jr	Sr
U =r$ )DbrxFFNi  r   c                 4  > [         TU ]  5         UR                  n[        UR                  UR
                  UR                  UR                  UR                  S9U l	        [        UR                  UR                  UR
                  UR                  S9U l        g )N)r   r
  r  r  r  r:  )r'   r(   
ffn_configr	  r   r
  r  r  r  routerr8  r  r  experts)r1   r   rU  r3   s      r4   r(   DbrxFFN.__init__  s    &&
 &66 **%44)3)P)P
 #&66&66!,,	
r6   rI   ri   c                 T    U R                  U5      u  p#nU R                  XX45      nXR4$ rR   )rV  rW  )r1   rI   r  r  r  rF  s         r4   rP   DbrxFFN.forward  s,    ,0KKN)kll1{@|r6   )rW  rV  )rS   rT   rU   rV   r   r(   r,   r   r   rP   rX   rY   rZ   s   @r4   rS  rS    s=    
z 
& %ell0J*K  r6   rS  c            !         ^  \ rS rSrS\S\4U 4S jjr       SS\R                  S\	\R                     S\	\R                     S\	\   S	\	\   S
\	\   S\	\   S\	\R                     S\S\\\R                     \\R                  \	\R                     4   \\R                  \	\   4   \\R                  \	\R                     \	\   4   \\R                  \	\R                     \	\R                     4   \\R                  \	\   \	\R                     4   \\R                  \	\R                     \	\   \	\R                     4   4   4S jjrSrU =r$ )	DbrxBlocki  r   r   c                    > [         TU ]  5         UR                  U l        UR                  U l        X l        [        UUS9U l        [        US9U l	        g )Nr   )r   )
r'   r(   r   r   r   r   r   norm_attn_normrS  ffnr  s      r4   r(   DbrxBlock.__init__  sN    !>>!--"3
 &)r6   rg   rt   rJ   r   r   output_router_logitsr   r   r   ri   c	                     U R                   " SUUUUUUUS.U	D6u  ppU R                  U5      u  p[        R                  R	                  XR
                  U R                  S9nX-   nU4nU(       a  X4-  nU(       a  X4-  nU(       a  X4-  nU$ )a3  Forward function for DbrxBlock.

Args:
    hidden_states (`torch.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
    position_ids (`torch.LongTensor`): position ids of shape `(batch, seq_len)`
    attention_mask (`torch.Tensor`, *optional*): attention mask of size (batch_size, sequence_length)
        if flash attention is used or (batch_size, 1, query_sequence_length, key_sequence_length)
        if default attention is used.
    past_key_value (`Tuple(torch.Tensor)`, *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.
    output_router_logits (`bool`, *optional*): Whether or not to return the router logits.
    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`, *optional*): position ids of the cache
r   r   r   )r^  r_  r   rx   r   r   r   )r1   rg   rt   rJ   r   r   ra  r   r   r   resid_statesself_attn_weightspresent_key_valuerouter_logitsoutputss                  r4   rP   DbrxBlock.forward  s    L MQL_L_ 	M
')%)/)	M
 	M
I%6 (,xx'>$--m?O?OZ^ZgZg-h$4 "++G++G''Gr6   )r   r_  r   r^  r   )NNNFFFN)rS   rT   rU   rV   r   r   r(   r,   r   r   r   r
   r   r   r   r   rP   rX   rY   rZ   s   @r4   r\  r\    s   	*z 	*c 	* 2637*.,1/4$)59A||A !.A u//0	A
 !A $D>A 'tnA D>A !!1!12A A 
ellellHU\\223ellHUO+,ellHU\\2HUOCDellHU\\2HU\\4JJKellHUOXell-CCDellHU\\2HUOXellE[[\	^
A Ar6   r\  c                   b    \ 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                   4S jrS	rg
)DbrxPreTrainedModeli"  transformerTr\  past_key_valuesFmodulec                    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      (       aX  UR
                  R                  R                  S5        UR                  b%  UR                  R                  R                  5         g g [        U[        5      (       am  UR                  R                  R                  SUS9  UR                   R                  R                  SUS9  UR"                  R                  R                  SUS9  g g )Nrv   )r|   stdr    )r   initializer_rangerB   r   r   weightdatanormal_r   zero_	Embeddingpadding_idxr   fill_r  r%  r&  r'  )r1   rm  ro  s      r4   _init_weights!DbrxPreTrainedModel._init_weights/  su   kk++fbii((MM&&CS&9{{&  &&( '--MM&&CS&9!!-""6#5#56<<> .--MM$$S){{&  &&( '..IINN"""5IINN"""5IINN"""5 /r6   r   N)rS   rT   rU   rV   r   config_classbase_model_prefixsupports_gradient_checkpointing_no_split_modules_skip_keys_device_placement_supports_flash_attn_2_supports_sdpa_supports_cache_class_supports_quantized_cache_supports_static_cacher   Modulerx  rX   r   r6   r4   rj  rj  "  sQ    L%&*#$#4"5!N  $"6BII 6r6   rj  c                   X  ^  \ rS rSrSrS\4U 4S jjrS\R                  4S jr	S\R                  4S jr
\           SS	\\R                     S
\\R                     S\\R                     S\\   S\\R                     S\\   S\\   S\\   S\\   S\\   S\\R                     S\\\4   4S jj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$ ) 	DbrxModeliC  a  Transformer decoder consisting of *config.num_hidden_layers*. Each layer is a [`DbrxBlock`] layer.

Args:
    config ([`DbrxConfig`]): Model configuration class with all parameters of the model.
        Initializing with a config file does not load the weights associated with the model, only the
        configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
r   c           	        > [         TU ]  U5        UR                  U l        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                  SS9U l        SU l        U R%                  5         g s  snf r  )r'   r(   pad_token_idrv  
vocab_size	emb_pdropr   ru  r   wte
ModuleListrC  n_layersr\  blocksr   norm_fgradient_checkpointing	post_initr  s      r4   r(   DbrxModel.__init__M  s     !.. ++))<< 1 16>>4CSCSTmmSXY_YhYhSi$jSiiYv%ASi$jkll6>>>&+# 	 %ks   &Dri   c                     U R                   $ rR   r  r1   s    r4   get_input_embeddingsDbrxModel.get_input_embeddings[  s    xxr6   valuec                     Xl         g rR   r  r1   r  s     r4   set_input_embeddingsDbrxModel.set_input_embeddings^  s    r6   	input_idsrt   rJ   rl  inputs_embedsr   r   output_hidden_statesra  return_dictr   c                 Z   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
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[        R                  R                  XPR                  U R                  S9nSnU(       aP  [!        U["        5      (       d;  SnUc  [%        5       nO+[$        R&                  " U5      n[        R                  S5        UcD  Ub  UR)                  5       OSn[*        R,                  " XUR.                  S   -   UR0                  S	9nUc  UR3                  S5      nU R5                  X%XU5      nUnU(       a  S
OS nU(       a  S
OS nU	(       a  S
OS nS nU R6                   H  nU(       a  UU4-  nU R                  (       a5  U R                  (       a$  U R9                  UR:                  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(       a	  UUS   4-  nU	(       d  M  UUS   4-  nM     U R=                  U5      nU(       a  UU4-  nU(       a  UOS nU(       a  UR?                  5       nU
(       d  [A        S UUUUU4 5       5      $ [C        UUUUUS9$ )Nz:You must specify exactly one of input_ids or inputs_embedszX`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`.Fr   TzWe 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   r2   r   )rt   rJ   r   r   ra  r   r   r!   r8   c              3   0   #    U  H  nUc  M  Uv   M     g 7frR   r   ).0vs     r4   	<genexpr>$DbrxModel.forward.<locals>.<genexpr>  s      jA js   	)last_hidden_staterl  rg   
attentionsrf  )"r   r   r  ra  r   use_return_dictr   r  r   r   r   r  r   rx   r   r  rB   r
   r   from_legacy_cacheget_seq_lengthr,   r-   r@   r2   r`   _update_causal_maskr  _gradient_checkpointing_func__call__r  to_legacy_cacherw   r   )r1   r  rt   rJ   rl  r  r   r   r  ra  r  r   r   return_legacy_cachepast_seen_tokensr   rg   all_hidden_statesall_self_attnsall_router_logitsnext_decoder_cacheblockblock_outputs
next_caches                           r4   rP   DbrxModel.forwarda  sL     2C1N-TXT_T_TqTq$8$D $++JjJj 	 %9$D $++JjJj 	 "+!6IDKK<Q<Q	%0%<k$++B]B]-t";<YZZ&&4==Yj I  HHY/M--m~~X\XeXe-f $Z??"&&".."."@"@"Q##^ !CRC^==?de"\\ ]5H5H5K"KTaThThN )33A6L..>L]

 & #7BD0d"6BD![[E#!m%55!**t}} $ A ANN! #%("
! !&!#.!-#2&7)='#1	! *!,M%28I1q%Q" =#3"55##!mB&7%99!K !N M2  -!11+4'$
#335J '5FXij  
 &+&+%+
 	
r6   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   rv   flex_attentionr   Fr   )r  past_key_values_lengthis_trainingr   r8   )r   target_lengthr#   r   r   )r   xpunpu)r   r   anyrB   r,   r   r   r  is_compileabler   _ignore_causal_mask_sdpar   r#   r@   get_max_cache_shape5_prepare_4d_causal_attention_mask_with_cache_positionr2   rA   finfor   _unmask_unattended)r1   rt   r  r   rl  r   r  using_compilable_cacher#   r   r  r   	min_dtypes                r4   r  DbrxModel._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r6   r   r  r#   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_valuer#   r2   r   )diagonalr  r8   r   )r)   r,   r  r   fullr2   triur-   rk   r?   cloner@   r>   masked_fill)rt   r   r  r#   r   r   r   r   r  mask_lengthpadding_masks              r4   r  ?DbrxModel._prepare_4d_causal_attention_mask_with_cache_position1  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 r6   )r  r  r  r  rv  r  r  )NNNNNNNNNNN)F)rS   rT   rU   rV   r   r   r(   r   ru  r  r  r   r   r,   r   r   r
   r   r   r   r   rP   r  staticmethodr   r#   r  rX   rY   rZ   s   @r4   r  r  C  s   z bll ",,   151537+/04$(,0/3/3&*59H
E,,-H
 !.H
 u//0	H

 "%H
  -H
 D>H
 $D>H
 'tnH
 'tnH
 d^H
 !!1!12H
 
u,,	-H
 H
b #(BellK78B llB 	B
 B  BH 444 4 {{	4
 4 4 4r6   r  zB
    The DBRX Model transformer for causal language modeling.
    )custom_introc                    &  ^  \ rS rSrS\4U 4S jjrS\R                  4S jrS\R                  4S jr	S\R                  4S jrS	\R                  4S
 jrS\4S jrS\4S jr\             SS\\R&                     S\\R(                     S\\R&                     S\\   S\\R(                     S\\R&                     S\\   S\\   S\\   S\\   S\\   S\\R&                     S\\\R(                  4   S\\\4   4S jj5       rSrU =r$ )DbrxForCausalLMij  r   c                   > [         TU ]  U5        [        U5      U l        UR                  U l        [
        R                  " UR                  UR                  SS9U l        UR                  R                  U l
        UR                  R                  U l        UR                  R                  U l        U R                  5         g r  )r'   r(   r  rk  r  r   r   r   lm_headrU  moe_loss_weightr
  rr   r  num_experts_per_tokr  )r1   r   r3   s     r4   r(   DbrxForCausalLM.__init__p  s     $V, ++yy!3!3V5F5FUS%00@@!,,<<#)#4#4#>#>  	r6   ri   c                 6    U R                   R                  5       $ rR   )rk  r  r  s    r4   r  $DbrxForCausalLM.get_input_embeddings|  s    4466r6   r  c                 :    U R                   R                  U5        g rR   )rk  r  r  s     r4   r  $DbrxForCausalLM.set_input_embeddings  s    --e4r6   c                     U R                   $ rR   r  r  s    r4   get_output_embeddings%DbrxForCausalLM.get_output_embeddings  s    ||r6   new_embeddingsc                     Xl         g rR   r  )r1   r  s     r4   set_output_embeddings%DbrxForCausalLM.set_output_embeddings  s    %r6   decoderc                     Xl         g rR   rk  )r1   r  s     r4   set_decoderDbrxForCausalLM.set_decoder  s    "r6   c                     U R                   $ rR   r  r  s    r4   get_decoderDbrxForCausalLM.get_decoder  s    r6   r  rt   rJ   rl  r  labelsr   r   r  ra  r  r   logits_to_keepc                    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 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SnU
(       ai  [        U(       a  UR                  OUS   U R                  U R                  U5      nUb.  Ub+  UU R                   UR#                  UR$                  5      -  -  nU(       d!  U4USS -   nU
(       a  U4U-   nUb  U4U-   $ U$ ['        UUUUR(                  UR*                  UR,                  UR                  S9$ )a  
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
    Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
    config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
    (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

Example:

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

>> model = DbrxForCausalLM.from_pretrained("databricks/dbrx-instruct")
>> tokenizer = AutoTokenizer.from_pretrained("databricks/dbrx-instruct")

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

>> # Generate
>> generate_ids = model.generate(inputs.input_ids, max_length=30)
>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
```
N)r  rt   rJ   rl  r  r   r   r  ra  r  r   r   r  r8   r   )lossaux_losslogitsrl  rg   r  rf  )r   r   r  ra  r  rk  rB   r   slicer  loss_functionr  r   rf  rr   r  r  r>   r2   r   rl  rg   r  )r1   r  rt   rJ   rl  r  r  r   r   r  ra  r  r   r  r   rg  rg   slice_indicesr  r  r  outputs                         r4   rP   DbrxForCausalLM.forward  s   R 2C1N-TXT_T_TqTq$8$D $++JjJj 	 %9$D $++JjJj 	 &1%<k$++B]B] "")%+'/!5!5#) # 
  
8B>SV8W8W~ot4]kmA}a,?@A%%  ;;11 	D /)4%%'"+  ((	H !d&6,,x{{4;;/GGGY,F#"v-'+'7D7V#CVC(#33!//))!//
 	
r6   )r  r  rr   r  rk  r  )NNNNNNNNNNNNr   )rS   rT   rU   rV   r   r(   r   ru  r  r  r   r  r  r  r  r  r   r   r,   r   r   r
   r   r   r   r   r   rP   rX   rY   rZ   s   @r4   r  r  j  s   
z 
7bll 75",, 5ryy &BII &#9 # Y    151537+/04-1$(,0/3/3&*5934g
E,,-g
 !.g
 u//0	g

 "%g
  -g
 ))*g
 D>g
 $D>g
 'tng
 'tng
 d^g
 !!1!12g
 c5<</0g
  
u//	0!g
 g
r6   r  )r  r  rj  )Nr   )@r   r   typingr   r   r   r   r,   torch.utils.checkpointr   activationsr	   cache_utilsr
   r   r   
generationr   modeling_attn_mask_utilsr   modeling_flash_attention_utilsr   r   modeling_outputsr   r   modeling_utilsr   utilsr   r   r   configuration_dbrxr   !torch.nn.attention.flex_attentionr   integrations.flex_attentionr   r   
get_loggerrS   r   r  r   r^   rf   r   r   rp   r   r   r   r   r   r   r	  r  r8  rS  r\  rj  r  r  __all__r   r6   r4   <module>r     s     . .    ! ; ; ) > h Q - J J *  !!;J J			H	%<")) <<(8	UU\\ 	U# 	U%,, 	UM&M&M& M& U\\*	M&
 \\M&`f9BII f9Ru9- u9p\1 \1@ , +LBII +L\$1 $1NBII 23")) 3lbii 4M		 M` 6/ 6 6@ c# c cL	 
G
)? G

G
T Br6   