
    Pg                       d Z ddlZddlmZmZmZmZmZmZm	Z	 ddl
Z
ddlZ
ddl
mZ ddlmZmZmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZmZ ddl m!Z!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0 ddl1m2Z2  e0       rddl3m4Z4m5Z5 ddl6m7Z7 nd\  Z7Z5Z4 e/       r	ddl8m9Z9m:Z: nd\  Z:Z9 e;e7e5e9e:e4f      Z< e*jz                  e>      Z?dZ@ G d dej                        ZB e&j                  eB       de
j                  deEde
j                  fd ZF G d! d"e      ZG	 dDd#ej                  d$e
j                  d%e
j                  d&e
j                  d'ee
j                     d(eHd)eHfd*ZI G d+ d,ej                        ZJ G d- d.ej                        ZK G d/ d0ej                        ZL G d1 d2ej                        ZM G d3 d4ej                        ZN G d5 d6ej                        ZOd7ZP e(d8eP       G d9 d:e"             ZQd;ZR e(d8eP       G d< d=eQ             ZS G d> d?eQe      ZT e(d@eP       G dA dBeQ             ZUg dCZVy)EzPyTorch Zamba model.    N)AnyCallableDictListOptionalTupleUnion)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )ACT2FN)CacheDynamicCache)GenerationMixin)AttentionMaskConverter)FlashAttentionKwargs)BaseModelOutputWithPastCausalLMOutputWithPast SequenceClassifierOutputWithPast)ALL_ATTENTION_FUNCTIONSPreTrainedModel)Unpack)ALL_LAYERNORM_LAYERS)add_start_docstrings%add_start_docstrings_to_model_forwardloggingreplace_return_docstrings)deprecate_kwarg)is_causal_conv1d_availableis_mamba_ssm_available   )ZambaConfig)mamba_inner_fnselective_scan_fn)selective_state_update)NNN)causal_conv1d_fncausal_conv1d_updateNNr$   c                   ,     e Zd Zd fd	Zd Zd Z xZS )ZambaRMSNormc                     t         |           t        j                  t	        j
                  |            | _        || _        y)z;
        ZambaRMSNorm is equivalent to T5LayerNorm
        N)super__init__r
   	Parametertorchonesweightvariance_epsilon)selfhidden_sizeeps	__class__s      o/var/www/html/suriana-translation/venv/lib/python3.12/site-packages/transformers/models/zamba/modeling_zamba.pyr/   zZambaRMSNorm.__init__R   s1     	ll5::k#:; #    c                 "   |j                   }|j                  t        j                        }|j	                  d      j                  dd      }|t        j                  || j                  z         z  }| j                  |j                  |      z  S )N   T)keepdim)	dtypetor1   float32powmeanrsqrtr4   r3   )r5   hidden_statesinput_dtypevariances       r9   forwardzZambaRMSNorm.forwardZ   sy    #))%((7 $$Q',,R,>%Ht?T?T4T(UU{{]--k:::r:   c                 ^    t        | j                  j                         d| j                   S )Nz, eps=)tupler3   shaper4   r5   s    r9   
extra_reprzZambaRMSNorm.extra_repra   s*    ))*+6$2G2G1HIIr:   )gư>)__name__
__module____qualname__r/   rH   rM   __classcell__r8   s   @r9   r,   r,   Q   s    $;Jr:   r,   rE   n_repreturnc                     | j                   \  }}}}|dk(  r| S | dddddddddf   j                  |||||      } | j                  |||z  ||      S )z
    This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
    num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
    r#   N)rK   expandreshape)rE   rS   batchnum_key_value_headsslenhead_dims         r9   	repeat_kvr\   i   so    
 2?1D1D.Ehz!!Qa"23::5BUW\^bdlmM  (;e(CT8TTr:   c                   ~   e Zd ZdZej
                  dfdZ	 ddej                  dej                  dede	e
eef      deej                  ej                  f   f
d	Zd
ej                  fdZdde	e   defdZdeeej                     eej                     f   fdZedde	eeej(                           ddfd       Zy)ZambaHybridDynamicCachea  
    A dynamic cache that can handle both the attention cache (which has a seq_len dimension) and the mamba cache
    (which has a constant shape regardless of seq_len).

    This cache has two sets of lists of tensors: `key_cache` and `value_cache` for attention cache and `conv_states`
    and `ssm_states` for mamba cache. Each of these lists has `num_layers` tensors. The expected shape for each tensor
    For attention layers, `key_cache` and `value_cache` have a shape of `(batch_size, num_heads, seq_len, head_dim)`,
    while `conv_states` and `ssm_states` have a shape of `(batch_size, 0)` (empty tensors).
    For mamba layers, `key_cache` and `value_cache` have a shape of `(batch_size, 0)` (empty tensors),
    while `conv_states` represents the convolution state and has a shape of `(batch_size, d_inner, d_conv)`,
    and `ssm_states` represents the ssm state and has a shape of `(batch_size, d_inner, d_state)`.
    Nc           
         || _         |j                  | _        d| _        |j                  |j                  z  | _        |j                  | _        |j                  | _	        |j                  | _
        g | _        g | _        g | _        i | _        i | _        i | _        t#        |j$                        D ]  }| xj                  t'        j(                  || j
                  | j                  ||      gz  c_        || j                  | j
                  | j                  z  | j                  f}| xj                  t'        j(                  |||      gz  c_        | j                  |   dk(  s| j                  j+                  |        t#        |j$                        D cg c]  }t'        j,                  g g|z  |       c}| _        t#        |j$                        D cg c]  }t'        j,                  g g|z  |       c}| _        y c c}w c c}w )NFdevicer?   hybridra   )r?   layers_block_typehas_previous_statemamba_expandr6   intermediate_sizemamba_d_statessm_state_sizemamba_d_convconv_kernel_sizen_mamba_headsconv_states
ssm_statestransformer_layers_modules_parameters_buffersrangenum_hidden_layersr1   zerosappendtensor	key_cachevalue_cache)r5   config
batch_sizer?   ra   icache_shape_s           r9   r/   z ZambaHybridDynamicCache.__init__   s   
!'!9!9"'!'!4!4v7I7I!I$22 & 3 3#11"$v//0 	2AJ(>(>@U@U^dlqr!  ""&&$*<*<<##	K OOKe TUUO%%a(H4''..q1	2 SXX^XpXpRqrQ%,,tj'8HrTYZ`ZrZrTstqELL"
):6Jt sts   "H"H

key_statesvalue_states	layer_idxcache_kwargsrT   c                    | j                   |   j                  d   dk(  r|| j                   |<   || j                  |<   nft        j                  | j                   |   |gd      | j                   |<   t        j                  | j                  |   |gd      | j                  |<   | j                   |   | j                  |   fS )Nr=   r   r<   dim)rx   rK   ry   r1   cat)r5   r   r   r   r   s        r9   updatezZambaHybridDynamicCache.update   s     >>)$**2.!3(2DNN9%*6DY'(-		4>>)3Lj2Y_`(aDNN9%*/))T5E5Ei5PR^4_ef*gDY'~~i($*:*:9*EEEr:   beam_idxc                    t        t        | j                              D ]S  }| j                  |   j                  }| j                  |   j	                  d|j                  |            | j                  |<   | j                  |   j                  }| j                  |   j	                  d|j                  |            | j                  |<   | j                  |   j                  }| j                  |   j	                  d|j                  |            | j                  |<   | j                  |   j                  }| j                  |   j	                  d|j                  |            | j                  |<   V y)zDReorders the cache for beam search, given the selected beam indices.r   N)	rs   lenrx   ra   index_selectr@   ry   rm   rn   )r5   r   r   ra   s       r9   reorder_cachez%ZambaHybridDynamicCache.reorder_cache   sD   s4>>23 		iI^^I.55F(,y(A(N(NqRZR]R]^dRe(fDNN9%%%i077F*.*:*:9*E*R*RSTV^VaVabhVi*jDY'%%i077F*.*:*:9*E*R*RSTV^VaVabhVi*jDY'__Y/66F)-)C)P)PQRT\T_T_`fTg)hDOOI&		ir:   c                     || j                   vr| j                   d   n|}t        | j                        |k  ry| j                  |   j                  d   S )zYReturns the sequence length of the cached states. A layer index can be optionally passed.r   )ro   r   rx   rK   )r5   r   s     r9   get_seq_lengthz&ZambaHybridDynamicCache.get_seq_length   sR     3<4CZCZ2ZD++A.`i	t~~)+~~i(..r22r:   c                     t        d      Nz@ZambaHybridDynamicCache does not have a legacy cache equivalent.NotImplementedErrorrL   s    r9   to_legacy_cachez'ZambaHybridDynamicCache.to_legacy_cache   s    !"deer:   past_key_valuesr   c                     t        d      r   r   )clsr   s     r9   from_legacy_cachez)ZambaHybridDynamicCache.from_legacy_cache   s    !"deer:   N)r   )rN   rO   rP   __doc__r1   float16r/   Tensorintr   r   strr   r   r   
LongTensorr   r   r   classmethodFloatTensorr    r:   r9   r^   r^   u   s    27t uJ 26FLLF llF 	F
 tCH~.F 
u||U\\)	*F$ie&6&6 i3 3c 3fuU\\':E%,,<O'O!P f fuUEVEV?W9X0Y fes f fr:   r^   modulequerykeyvalueattention_maskscalingdropoutc                 T   t        || j                        }t        || j                        }	t        j                  ||j	                  dd            |z  }
|#|d d d d d d d |j
                  d   f   }|
|z   }
t        j                  j                  |
dt        j                        j                  |j                        }
t        j                  j                  |
|| j                        }
t        j                  |
|	      }|j	                  dd      j                         }||
fS )Nr<   r   r   r=   )r   r?   )ptrainingr#   )r\   num_key_value_groupsr1   matmul	transposerK   r
   
functionalsoftmaxrA   r@   r?   r   r   
contiguous)r   r   r   r   r   r   r   kwargsr   r   attn_weightscausal_maskattn_outputs                r9   eager_attention_forwardr      s    3 ; ;<JUF$?$?@L<<z';';Aq'ABWLL!$Q1.D
0@0@0D.D%DE#k1==((2U]](SVVW\WbWbcL==((6??([L,,|\:K''1-88:K$$r:   c                        e Zd ZdZdedef fdZ	 ddej                  dede	ej                     de	e
   dee   d	eej                  e	ej                     e	eej                        f   fd
Z xZS )ZambaAttentiona9  
    Multi-headed attention from 'Attention Is All You Need' paper. Modified to use sliding window attention: Longformer
    and "Generating Long Sequences with Sparse Transformers".

    Adapted from transformers.models.mistral.modeling_mistral.MistralAttention:
    The input dimension here is attention_hidden_size = 2 * hidden_size, and head_dim = attention_hidden_size // num_heads.
    The extra factor of 2 comes from the input being the concatenation of original_hidden_states with the output of the previous (mamba) layer
    (see fig. 2 in https://arxiv.org/pdf/2405.16712).
    Additionally, replaced
    attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim) with
    attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim/2)
    rz   r   c                 .   t         |           || _        || _        |j                  | _        |j
                  | _        |j                  |j                  z  | _	        |j                  | _
        | j                  dz  dz  | _        d| _        |j                  | _        t        j                  |j                  |j                  | j                  z  d      | _        t        j                  |j                  |j                  | j                  z  d      | _        t        j                  |j                  |j                  | j                  z  d      | _        t        j                  |j                  | j                  z  |j&                  d      | _        y )Nr<         TFbias)r.   r/   rz   r   attention_hidden_sizeattention_head_dimr[   num_attention_headsrY   r   max_position_embeddingsr   	is_causalattention_dropoutr
   Linearq_projk_projv_projr6   o_projr5   rz   r   r8   s      r9   r/   zZambaAttention.__init__   s9   "%+%A%A"11$*$>$>&B\B\$\!'-'E'E$)d2!'!9!9ii < <f>X>X[_[h[h>hotuii < <f>X>X[_[h[h>hotuii < <f>X>X[_[h[h>hotuii : :T]] JFL^L^ejkr:   rE   r   past_key_valuer   rT   c                 h   |j                   d d }g |d| j                  }| j                  |      j                  |      j	                  dd      }| j                  |      j                  |      j	                  dd      }	| j                  |      j                  |      j	                  dd      }
||j                  |	|
|      \  }	}
t        }| j                  j                  dk7  r^| j                  j                  dk(  r(|j                  dd      rt        j                  d       nt        | j                  j                     } || ||	|
|f| j                  sd	n| j                   | j"                  d
|\  }} |j$                  g |d j'                         }| j)                  |      }||fS )Nr=   r#   r<   eagersdpaoutput_attentionsFz`torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to eager attention. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.        )r   r   )rK   r[   r   viewr   r   r   r   r   rz   _attn_implementationgetloggerwarning_oncer   r   r   r   rW   r   r   )r5   rE   r   r   r   r   input_shapehidden_shapequery_statesr   r   attention_interfacer   r   s                 r9   rH   zZambaAttention.forward  s    $))#2.88b8$--8{{=166|DNNqRST[[/44\BLLQPQR
{{=166|DNNqRST%'5'<'<ZW`'a$J(?;;++w6{{//69fjjI\^c>d##L
 '>dkk>^>^&_#$7	%
  $}}C$2H2HLL	%
 	%
!\ *k));;;;FFHkk+.L((r:   r   )rN   rO   rP   r   r$   r   r/   r1   r   r   r^   r   r   r   rH   rQ   rR   s   @r9   r   r      s    l{ ls l. =A))||)) )) !.	))
 !!89)) -.)) 
u||Xell3XeELL>Q5RR	S))r:   r   c                   l     e Zd ZdZdef fdZ	 d	dej                  defdZ	d	defdZ
d	defdZ xZS )
ZambaMambaMixeruE  
    Compute ∆, A, B, C, and D the state space parameters and compute the `contextualized_states`.
    A, D are input independent (see Mamba paper [1] Section 3.5.2 "Interpretation of A" for why A isn't selective)
    ∆, B, C are input-dependent (this is a key difference between Mamba and the linear time invariant S4,
    and is why Mamba is called **selective** state spaces)

    This module differs from `transformers.models.mamba.modeling_mamba.MambaMixer` in two ways:
    - Added multi-head: the output of `self.in_proj` is split into `self.n_mamba_heads` heads, and each head
    undergoes an independent forward pass, identical to the original `MambaMixer`, up until the pre-activations of
    `self.out_proj`. The pre-activations, coming from different mamba heads, are then concatenated and fed into `self.out_proj`.
    rz   c           	         t         |           || _        || _        |j                  | _        |j
                  | _        |j                  | _        |j                  |j                  z  | _
        |j                  | _        |j                  | _        | j                  | j                  z  | _        |j                  | _        |j"                  | _        t'        j(                  | j                  | j                  | j                   | j                  | j                  | j                  dz
        | _        |j,                  | _        t0        |j,                     | _        |j4                  | _        t'        j8                  | j                  | j                  dz  | j$                        | _        t'        j<                  t?        j@                  | j                  | j                  | j                  dz  z   | j                              | _!        t'        j<                  t?        j@                  | j                  | j                  | j                        dz
  dz  | j                  dz  z        | _"        t'        j<                  t?        j@                  | j                  | j                              | _#        t?        jH                  d| j                  dz   t>        jJ                        d d d f   }|jM                  | j                  d      jO                         }t'        j<                  t?        jP                  |      jS                  | j                  | j                  d            | _*        t'        j<                  t?        jV                  | j                  | j                              | _,        t'        j8                  | j                  | j                  | j$                        | _-        t\        st^        ja                  d       y y )	Nr#   )in_channelsout_channelsr   kernel_sizegroupspaddingr<   r   g      ?)r?   r=   ap  The fast path is not available because on of `(selective_state_update, selective_scan_fn, causal_conv1d_fn, causal_conv1d_update, mamba_inner_fn)` is None. To install follow https://github.com/state-spaces/mamba/#installation and https://github.com/Dao-AILab/causal-conv1d. If you want to use the naive implementation, set `use_mamba_kernels=False` in the model config)1r.   r/   rz   r   r6   rh   ri   rj   rk   rf   rg   mamba_dt_ranktime_step_rankrl   mamba_head_dimmamba_conv_biasuse_conv_biasmamba_proj_biasuse_biasr
   Conv1dconv1dhidden_mamba_act
activationr   actuse_mamba_kernelsuse_fast_kernelsr   in_projr0   r1   ru   x_proj_weightdt_proj_weightdt_proj_biasarangerA   rV   r   logrW   A_logr2   Dout_projis_fast_path_availabler   r   )r5   rz   r   Ar8   s       r9   r/   zZambaMambaMixer.__init__F  s   "!--$22 & 3 3!'!4!4v7I7I!I$22#11"448J8JJ#33..ii..//##--))))A-
 !11&112 & 8 8 yy!1!143I3IA3MTXTaTab  \\&&''$*=*=*AA''
 !ll[[++T-@-@$BUBUVY\\!!3&'

 LLT5G5GI\I\)]^ LLD//!35==I$PQ'RHHT++R0;;=\\%))A,"6"6t7I7I4K^K^`b"cd
ejj););T=P=PQR		$"8"8$:J:JQUQ^Q^_%^ &r:   rE   cache_paramsc                    |j                   \  }}}|d uxr |j                  xr |dk(  }| j                  |      j                  dd      }|j	                  |dd|      j                  dd      \  }}	|j                  d      j                         }|	j                  d      }	|	j                  || j                  d|      j                  dd      }	| j                  j                  j	                  | j                  j                  j                  d      | j                  j                  j                  d            }
|ret        |j                  d      |j                  | j                     |
| j                  j                   | j"                        }|j%                  d      }n|,t'        j(                  |dk(        s||j%                  d      z  }|dt*        j,                  j/                  || j0                  |j                   d   z
  df      }|j                  | j                     j3                  |       t5        ||
| j                  j                   | j"                        }|,t'        j(                  |dk(        s||j%                  d      z  }|j                  d| j                  | j6                  |      j                  dd      }| j8                  d d d d d d d f   |z  j                  dd      }t'        j:                  || j<                  | j>                  | j>                  gd      \  }}}| j@                  d d d f   |j                  dd      z  }t'        jB                  | jD                  jG                                }| jH                  | jH                  jG                         nd }t'        jJ                  |d|f|jL                  |jN                        }|rtQ        | j                        D ]  }tS        |jT                  | j                     d d |f   ||d	df   ||d	df   ||   ||d d df   ||d d df   | jV                  |   |	|d	df   ||   d

      j%                  d      }t'        jX                  ||fd      } nAt'        jJ                  |d| j6                  | j>                  f|jL                  |jN                        }tQ        | j                        D ]  }t[        ||   ||   ||   ||   j                  dd      ||   j                  dd      | jV                  |   jG                         |	|   ||   d
d

      \  }}t'        jX                  ||fd      j                         }t'        jX                  ||j%                  d      fd      } |*|(|jT                  | j                     j3                  |       | j]                  |j                  dd            }|S )Nr#   r<   r=   r   r   )r   r   r`   .T)dt_softplus)delta_softplusreturn_last_state)/rK   re   r   r   r   chunksqueezer   rW   rl   r   r3   sizer)   rm   r   r   r   	unsqueezer1   allr
   r   padrk   copy_r(   r   r   splitr   ri   r   expr   floatr   emptyra   r?   rs   r'   rn   r   r   r&   r   )r5   rE   r   r   r{   seq_lenr~   use_precomputed_statesprojected_statesgateconv_weightsrm   ssm_parameters	time_stepBCdiscrete_time_stepr   time_proj_biasscan_outputsnscan_outputs_	ssm_state
ssm_state_contextualized_statess                            r9   cuda_kernels_forwardz$ZambaMambaMixer.cuda_kernels_forward  s    "/!4!4
GQ!-T!9!nl>]>]!nbimnbn  <<6@@AF.33JAwOUUVW]^U_t%--a0;;=||A||J(:(:BHRRSTVWX {{))..t{{/A/A/F/Fq/I4;;K]K]KbKbcdKef!0%%b)((8  M *33B7M)%))Na<O2P -0H0H0K K' mm//@U@UXeXkXklnXo@oqr?st((8>>{K,]L$++JZJZgkgvgvwM)%))Na<O2P -0H0H0K K
 &--b$2D2DdFYFY[bcmmnoqrs,,Qa];mKVVWY[]^++T00$2E2EtGZGZ[ac
	1a "00D9I<O<OPRTV<WWYYtzz'')** 7;6G6G6S**002Y]{{J7#;MDXDX`m`s`st!4--. O 6 ++DNN;AqDA!!S!),&q#qy1aDaAgJaAgJFF1ICO"1% $! )B-   %yy,)FANO  Q 3 3T5H5HI$++#))I
 4--. S,=!!$&q)aDaDNN1a(aDNN1a(FF1IOO%G"1%#'&*-)z  %yy,)FANYY[!IIy*2F2Fq2I&JPQR	S $)A''7==iH !%l.D.DQ.J K$$r:   c           
         |j                   \  }}}|j                  }| j                  |      j                  dd      }|j	                  |dd|      j                  dd      \  }	}
|	j                  d      j                         }	|
j                  d      }
|
j                  || j                  d|      j                  dd      }
t        |t              }|r|j                  | j                     j                   d   |k(  r| j                  r(|j                  | j                     j                         }n|j                  | j                     }|j!                  |	j"                        }|j$                  r|dk(  r|j&                  | j                     j                   d   |k(  r|j&                  | j                     }t)        j*                  |dd      }|	d d d d df   |d d d d df<   ||j&                  | j                  <   t)        j,                  || j.                  j0                  d d dd d f   z  d      }	| j2                  r|	| j.                  j4                  z  }	| j7                  |	      j!                  |      j9                  d      }	n|Ct)        j:                  |dk(        s+|	|d d |	j                   d    d f   j9                  d      z  }	t<        j>                  jA                  |	| jB                  |	j                   d   z
  df      }||j&                  | j                  <   | j7                  | j/                  |	      dd |f         }	|t)        j:                  |dk(        s|	|d d |	j                   d    d f   j9                  d      z  }	nt)        jD                  || j                  | jF                  | jH                  f|	j"                  |      }|,t)        j:                  |dk(        s|	|j9                  d      z  }	| j7                  | j/                  |	      dd |f         }	|,t)        j:                  |dk(        s|	|j9                  d      z  }	|	j                  d| j                  | jF                  |      j                  dd      }	| jJ                  d d d d d d d f   |	z  j                  dd	      }t)        jL                  || jN                  | jH                  | jH                  gd      \  }}}| jP                  d d d f   |j                  dd	      z  | jR                  d d d d d d f   z   }t<        j>                  jU                  |      }t)        jV                  | jX                  j[                                }t)        jV                  |d d d d d d d d f   |d d d d d d d d d f   z        }|d d d d d d d d d f   |d d d d d d d d d f   j[                         z  }||	d d d d d d d d d f   j[                         z  }g }t]        |      D ]  }|d d d d d d |d d f   j                  dd      |z  |d d d d d d |d d f   j                  dd      z   }t)        j^                  |j                  dd      j!                  |      |d d d d |d d f   j9                  d            }|ja                  |d d d d d d df           t)        jb                  |d      }||	| jd                  d d d d d d f   z  z   }|| j7                  |
      z  }|r||j                  | j                  <   | jg                  |j                  dd      j                  |d|      j                  dd            }|S )
Nr#   r<   r=   r   r   )shiftsdims.r`   r   )4rK   r?   r   r   r   r   r   r   rW   rl   
isinstancer^   rn   r   r   cloner@   ra   re   rm   r1   rollsumr   r3   r   r   r   r  r  r
   r   r  rk   ru   r   ri   r   r  r   r   r   softplusr  r   r  rs   r   rv   stackr   r   )r5   input_statesr   r   r{   r
  r~   r?   r  rE   r  	use_cacher  
conv_stater  r  r  r  r  r   
discrete_A
discrete_BdeltaB_ur  r|   scan_outputr  s                              r9   slow_forwardzZambaMambaMixer.slow_forward  s   !-!3!3
GQ""<<5??1E.33JAwOUUVW]^U_t%--a0;;=||A||J(:(:BHRRSTVWX|-DE	00@FFqIZW}}(33DNNCIIK	(33DNNC	!]%9%9:I //qL ,,T^^<BB1ES)55dnnE
"ZZ
2BG
'4Q1W'=
1a8$;E((8 %		*t{{7I7I!QPQ'7R*RXZ [%%!T[[%5%55M $ 7 : :5 A K KB O!-eiiRS@S6T$1N1}GZGZ[]G^F^F`C`4a4k4klm4n$nM]]..}t?T?TWdWjWjkmWn?npq>rs
;E((8 $])CC'M)R S!-eiiRS@S6T$1N1}GZGZ[]G^F^F`C`4a4k4klm4n$nMT//1D1DdFYFYZ$++I
 )%))Na<O2P -0H0H0K K HHT[[%?XgX%NOM)%))Na<O2P -0H0H0K K &--b$2D2DdFYFY[bcmmnoqrs,,Qa];mKVVWY[]^++T00$2E2EtGZGZ[ac
	1a #11!T':Y=P=PQSUW=XX\`\m\mtQ]
 
  ]]334FG YYtzz'')**YYqD!T1!458J1aQRTUW[K[8\\]
'1aD(89AaD!Q>N<O<U<U<WW
aAq$.> ? E E GGw 	9A"1aAq=1;;AqAIMPXYZ\]_`bcefYfPgPqPqrsuvPwwI,,y':':1a'@'C'CE'JAaQRTUWXjMLcLcdfLghKAq!QJ 78	9 kk,B7!]TVVAtQ<L5M%MN!DHHTN26?L##DNN3 !%!!!Q'//
BHRRSTVWX!
 %$r:   c                     | j                   rGt        r"d| j                  j                  j                  vrt        d      | j                  |||      S | j                  |||      S )NcudazFast Mamba kernels are not available. Make sure to they are installed and that the mamba module is on a CUDA device. lease run 'pip install causal-conv1d>=1.2.0' and 'pip install mamba-ssm', or set use_mamba_kernels=False in the model's config.)r   )r   r   r   ra   type
ValueErrorr  r,  )r5   rE   r   r   s       r9   rH   zZambaMambaMixer.forwardC  sm      )V4;M;M;T;T;Y;Y-Y i 
 ,,]LYg,hh  ^ \\r:   r*   )rN   rO   rP   r   r$   r/   r1   r   r^   r  r,  rH   rQ   rR   s   @r9   r   r   9  sX    
={ =@ im_%"\\_%9P_%B[%7N [%z	]3J 	]r:   r   c                   $     e Zd Z fdZd Z xZS )ZambaMLPc                    t         |           || _        |j                  | _        |j                  | _        t        j                  | j                  | j                  d      | _        t        j                  | j                  | j                  d      | _        t        j                  | j                  | j                  d      | _	        t        |j                     | _        y NFr   )r.   r/   rz   r6   rg   r
   r   	gate_projup_proj	down_projr   
hidden_actact_fnr5   rz   r8   s     r9   r/   zZambaMLP.__init__Q  s    !--!'!9!94#3#3T5K5KRWXyy!1!143I3IPUV4#9#94;K;KRWXV../r:   c                     | j                  | j                  | j                  |            | j                  |      z        }|S r   )r7  r9  r5  r6  )r5   xr7  s      r9   rH   zZambaMLP.forward[  s6    NN4;;t~~a/@#ADLLQRO#ST	r:   )rN   rO   rP   r/   rH   rQ   rR   s   @r9   r2  r2  P  s    0r:   r2  c                       e Zd Zddedee   f fdZ	 	 	 	 ddej                  dej                  dedeej                     dee	   dee
   d	ee
   d
ee   deej                  eeej                  ej                  f      f   fdZ xZS )ZambaAttentionDecoderLayerrz   r   c                     t         |           t        ||      | _        t	        |      | _        t        |j                  |j                        | _	        t        |j                  |j                        | _        y )Nr7   )r.   r/   r   	self_attnr2  feed_forwardr,   r   rms_norm_epsinput_layernormr6   pre_ff_layernormr   s      r9   r/   z#ZambaAttentionDecoderLayer.__init__a  s_    '	:$V,+F,H,HfNaNab ,V-?-?VEXEX Yr:   rE   original_hidden_statesr   r   r   r&  r   rT   c           
          t        j                  ||gd      }| j                  |      } | j                  d||||||d|\  }}	| j	                  |      }| j                  |      }|f}
|r|
|	fz  }
|
S )a  
        Args:
            hidden_states (`torch.FloatTensor`): output of previous Mamba layer of shape `(batch, seq_len, embed_dim)`
            original_hidden_states (`torch.FloatTensor`): word embedding output of shape `(batch, seq_len, embed_dim)`.
                This is concatenated with `hidden_states` (which is the output of the previous (mamba) layer). The
                concatenated tensor is then used as input of the pre-attention RMSNorm
                (see fig. 2 in https://arxiv.org/pdf/2405.16712).
            layer_idx (`int`): layer_idx in the forward pass. Used to distinguish Zamba's tied transformer layers.
            attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
                `(batch, sequence_length)` where padding elements are indicated by 0.
            past_key_value (`ZambaHybridDynamicCache`, *optional*): cached past key and value projection states
            output_attentions (`bool`, *optional*):
                Whether or not to return the attentions tensors of all attention layers. See `attentions` under
                returned tensors for more detail.
            use_cache (`bool`, *optional*):
                If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
                (see `past_key_values`).
            cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
                Indices depicting the position of the input sequence tokens in the sequence.
        r=   r   )rE   r   r   r   r   r&  r   )r1   concatenaterD  rA  rE  rB  )r5   rE   rF  r   r   r   r   r&  r   self_attn_weightsoutputss              r9   rH   z"ZambaAttentionDecoderLayer.forwardi  s    > ))=:P*QWYZ,,];+94>> ,
'))/,
 ,
(( --m<))-8 ")++Gr:   r   NNFF)rN   rO   rP   r$   r   r   r/   r1   r   r^   boolr   r   r   r   rH   rQ   rR   s   @r9   r>  r>  `  s    Z{ Zx} Z 26<@,1$)3||3 !&3 	3
 !.3 !!893 $D>3 D>3 -.3 
u  (51B1BEDUDU1U+V"WW	X3r:   r>  c                   n    e Zd Zdedef fdZ	 	 	 	 	 	 	 	 	 ddej                  deej                     dedeej                     deej                     dee	   d	ee
   d
ee
   deej                     deej                     deej                  eeej                  ej                  f      f   fdZ xZS )ZambaMambaDecoderLayerrz   r   c                     t         |           t        ||      | _        t	        |j
                  |j                        | _        || _        y )N)rz   r   r@  )	r.   r/   r   mambar,   r6   rC  rD  r   r   s      r9   r/   zZambaMambaDecoderLayer.__init__  s>    $FiH
+F,>,>FDWDWX"r:   rE   rF  r   r   r   r   r&  cache_positiontransformer_hidden_statesrT   c                     |}|
||
z   n|}| j                  |      }| j                  |||      }d}||z   }|f}|r||fz  }|r||fz  }|S )a  
        Args:
            hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
            attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
                `(batch, sequence_length)` where padding elements are indicated by 0.
            past_key_value (`ZambaHybridDynamicCache`, *optional*): cached past key and value projection states
            output_attentions (`bool`, *optional*):
                Whether or not to return the attentions tensors of all attention layers. See `attentions` under
                returned tensors for more detail.
            use_cache (`bool`, *optional*):
                If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
                (see `past_key_values`).
            cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
                Indices depicting the position of the input sequence tokens in the sequence.
        N)rE   r   r   )rD  rP  )r5   rE   rF  r   r   r   r   r   r&  rQ  rR  r   residualrI  rJ  s                  r9   rH   zZambaMambaDecoderLayer.forward  s    < !
 :S9^M55dq 	 ,,];

'') # 
 ! !=0 ")++G((Gr:   )	NNNNNFFNN)rN   rO   rP   r$   r   r/   r1   r   r   r^   rL  r   r   r   rH   rQ   rR   s   @r9   rN  rN    s   #{ #s # :>15.2<@,1$)59<@:||: !) 6: 	:
 !.: ell+: !!89: $D>: D>: !!1!12: $,ELL#9: 
u  (51B1BEDUDU1U+V"WW	X:r:   rN  c                   f    e Zd Zdedej
                  def fdZ	 	 	 	 	 	 	 	 ddej                  de
ej                     dede
ej                     d	e
ej                     d
e
e   de
e   de
e   de
ej                     deej                   e
eej                   ej                   f      f   fdZ xZS )ZambaHybridLayershared_transflinearrP  c                 L    t         |           || _        || _        || _        y r   )r.   r/   rW  rX  mamba_decoder)r5   rW  rX  rP  r8   s       r9   r/   zZambaHybridLayer.__init__  s%    *"r:   rE   rF  r   r   r   r   r   r&  rQ  rT   c
           
          | j                  ||||||||	      }
|
d   }|r|
d   }| j                  |      }| j                  |||||||	      }
|r|
d   f|
dd z   }
|
S )a  
        Args:
            hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
            original_hidden_states (`torch.FloatTensor`): word embedding output that will be concatenated with
            hidden activations to form the input of the shared transformer layer.
            layer_idx (`int`): layer number.
            attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
                `(batch, sequence_length)` where padding elements are indicated by 0.
            past_key_value (`ZambaHybridDynamicCache`, *optional*): cached past key and value projection states
            output_attentions (`bool`, *optional*):
                Whether or not to return the attentions tensors of all attention layers. See `attentions` under
                returned tensors for more detail.
            use_cache (`bool`, *optional*):
                If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
                (see `past_key_values`).
            cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
                Indices depicting the position of the input sequence tokens in the sequence.
        )rF  r   r   r   r   r&  rQ  r   r#   )rR  r   r   r   r&  rQ  r<   N)rW  rX  rZ  )r5   rE   rF  r   r   r   r   r   r&  rQ  layer_outputsrR  rI  s                r9   rH   zZambaHybridLayer.forward  s    > **#9&)/) + 	
 %2!$4! -a 0$(KK0I$J!**&?))/) + 
 *1-/@AMRSRTDUUMr:   )NNNNNFFN)rN   rO   rP   r>  r
   r   rN  r/   r1   r   r   r   r^   rL  r   r   r   rH   rQ   rR   s   @r9   rV  rV    s   #&@ #")) #\r # :>15.2<@,1$)59>||> !) 6> 	>
 !.> ell+> !!89> $D>> D>> !!1!12> 
u  (51B1BEDUDU1U+V"WW	X>r:   rV  aJ  
    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

    This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
    Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
    and behavior.

    Parameters:
        config ([`ZambaConfig`]):
            Model configuration class with all the parameters of the model. Initializing with a config file does not
            load the weights associated with the model, only the configuration. Check out the
            [`~PreTrainedModel.from_pretrained`] method to load the model weights.
zSThe bare Zamba Model outputting raw hidden-states without any specific head on top.c                        e Zd ZeZdZdZddgZdZdZ	dZ
dZdZd Zee	 	 	 	 ddeej"                     d	eeeeeef   f      d
edef fd              Z xZS )ZambaPreTrainedModelmodelTr>  rN  r   Fc                    | j                   j                  }t        |t        j                  t        j
                  f      rY|j                  j                  j                  d|       |j                  %|j                  j                  j                          y y t        |t        j                        rf|j                  j                  j                  d|       |j                  2|j                  j                  |j                     j                          y y t        |t              r(d|j                  _        d|j                   _        |j"                  j                  j                  d|       | j                   j$                  dz  }t        j&                  j)                  |j*                  | |       | j                   j,                  | j                   j.                  z  | j                   j0                  z  }t3        j4                  t3        j6                  | j                   j0                  |      t9        j:                  | j                   j<                        t9        j:                  | j                   j>                        z
  z  t9        j:                  | j                   j>                        z         jA                  | j                   jB                        }|t3        j:                  t3        jD                  |              z   }t3        jF                         5  |jH                  jK                  |       d d d        d|jH                  _&        y y # 1 sw Y   xY w)Nr   )rC   stdTr   )min)'rz   initializer_ranger  r
   r   r   r3   datanormal_r   zero_	Embeddingpadding_idxr   r   _no_weight_decayr   r   r   inituniform_r   rf   r6   rl   r1   r  randmathr   time_step_maxtime_step_minclamptime_step_floorexpm1no_gradr   r  
_no_reinit)r5   r   ra  dt_init_stdr   dtinv_dts          r9   _init_weightsz"ZambaPreTrainedModel._init_weightsK  sb   kk++fryy"))45MM&&CS&9{{&  &&( '-MM&&CS&9!!-""6#5#56<<> .0,0FLL)(,FHH%  %%--3C-@++33T9KGGV22[L+N![[558O8OOSWS^S^SlSllN

4;;44nE88DKK556$++B[B[9\\^((4;;4456 e33e4	  %))U[["%5$566F 2##))&12-1F*' 1"2 2s   MMtorch_dtype
device_maphard_check_onlycheck_device_mapc                 `    t         |   |||||      }|s|j                  dk(  rd|_        |S )z
        Overloads `PreTrainedModel._check_and_enable_flash_attn_2` so as to DISABLE Flash Attention 2 by default on Zamba models.
        Flash attention 2 is currently not supported in the HuggingFace implementation of Zamba v1.
        )r{  r|  flash_attention_2r   )r.   _check_and_enable_flash_attn_2r   )r   rz   ry  rz  r{  r|  r8   s         r9   r  z3ZambaPreTrainedModel._check_and_enable_flash_attn_2j  sE     7K__o 8 

 6#>#>BU#U*1F'r:   rK  )rN   rO   rP   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_is_statefulrx  r   r   r1   r?   r	   r   r   r   rL  r  rQ   rR   s   @r9   r^  r^  <  s    
 L&*#57OP"3"N L2>  .2;? %!& ekk* U3S#X#678	
    r:   r^  a  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
            it.

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

            [What are input IDs?](../glossary#input-ids)
        attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

            [What are attention masks?](../glossary#attention-mask)

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

            If `past_key_values` is used, optionally only the last `input_ids` have to be input (see
            `past_key_values`).

            If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
            and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
            information on the default strategy.

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.
        position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.n_positions - 1]`.

            [What are position IDs?](../glossary#position-ids)
        past_key_values (`ZambaHybridDynamicCache`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
            A ZambaHybridDynamicCache object containing pre-computed hidden-states (keys and values in the
            self-attention blocks and convolution and ssm states in the mamba blocks) that can be used (see
            `past_key_values` input) to speed up sequential decoding.
            Key and value cache tensors have shape `(batch_size, num_heads, seq_len, head_dim)`.
            Convolution and ssm states tensors have shape `(batch_size, d_inner, d_conv)` and
            `(batch_size, d_inner, d_state)` respectively.
            See the `ZambaHybridDynamicCache` class for more details.

            If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that
            don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
            `input_ids` of shape `(batch_size, sequence_length)`.
        inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        use_cache (`bool`, *optional*):
            If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
            `past_key_values`).
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
        cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
            Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`,
            this tensor is not affected by padding. It is used to update the cache in the correct position and to infer
            the complete sequence length.
c                   >    e Zd ZdZdef fdZd Zd Z ee	      	 	 	 	 	 	 	 	 	 	 dde
j                  dee
j                     dee
j                     d	ee   d
ee
j                     dee   dee   dee   dee   dee
j                     deeef   fd       Zd Z xZS )
ZambaModelz
    Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`ZambaDecoderLayer`]

    Args:
        config: ZambaConfig
    rz   c           
         t         |   |       |j                  | _        |j                  | _        t        j                  |j                  |j                  | j                        | _        t        |      }g }g }|j                  | _
        t        |j                        D ]  }|j                  |   dk(  r|j                  t        ||             2|j                  |   dk(  sE|j                  t        j                  | j                   j                  | j                   j                  d             |j                  t        ||              t#        |      }t#        |      }g }g | _        t'        | j                        D ]  \  }}|dk(  r_d| d}	g d}
g | j$                  |
D cg c]  }|	|z   	 c}| _        |j                  t)        |t+        |      t+        |                   j|j                  t+        |              t        j,                  |      | _        |j0                  | _        t3        |j                  |j4                  	      | _        d| _        | j;                          y c c}w )
NrP  )r   rb   Fr   zlayers..)	z%shared_transf.self_attn.q_proj.weightz%shared_transf.self_attn.k_proj.weightz%shared_transf.self_attn.v_proj.weightz%shared_transf.self_attn.o_proj.weightz+shared_transf.feed_forward.gate_proj.weightz)shared_transf.feed_forward.up_proj.weightz+shared_transf.feed_forward.down_proj.weightz$shared_transf.input_layernorm.weightz%shared_transf.pre_ff_layernorm.weightr@  )r.   r/   pad_token_idrh  
vocab_sizer
   rg  r6   embed_tokensr>  rd   rs   rt   rv   rN  r   rz   iter_tied_weights_keys	enumeraterV  next
ModuleListlayersr   r,   rC  final_layernormgradient_checkpointing	post_init)r5   rz   blockmamba_layerslinear_layersr|   r  layer_id
layer_typeprefix_name	tied_keysr   r8   s               r9   r/   zZambaModel.__init__  s&    !.. ++LL):):F<N<NPTP`P`a*62!'!9!9v//0 	QA''*g5##$:6Q$OP))!,8$$RYYt{{/F/FH_H_fk%lm##$:6Q$OP	Q L)]+"$$-d.D.D$E 	2 HjX% 'z3
	 +pD,C,C*odmFn]`{UXGXFn*o'.ud=6I4P\K]^_d<01#	2$ mmF+$*$?$?!+F,>,>FDWDWX&+# Gos   .I7c                     | j                   S r   r  rL   s    r9   get_input_embeddingszZambaModel.get_input_embeddings  s       r:   c                     || _         y r   r  r5   r   s     r9   set_input_embeddingszZambaModel.set_input_embeddings  s
    !r:   	input_idsr   position_idsr   inputs_embedsr&  r   output_hidden_statesreturn_dictrQ  rT   c                 d   ||n| j                   j                  }||n| j                   j                  }||n| j                   j                  }|	|	n| j                   j                  }	|d u |d uz  rt        d      | j                  r%| j                  r|rt        j                  d       d}|| j                  |      }|}t        j                  |      }|r|t        j                  d       |
.t        j                  |j                  d   |j                        }
||
j!                  d      }| j#                  |||
      }|rdnd }|rdnd }t%        | j&                        D ]r  \  }}|r||fz  }| j                  r1| j                  r%| j)                  |j*                  |||||||||

      }n ||||||||||
		      }|d   }|sd|d   j||d   fz  }t | j-                  |      }|r||fz  }|r|j.                  sd
|_        t1        ||r|nd ||      }|	r|S |j3                         S )NzaYou cannot specify both input_ids and inputs_embeds at the same time, and must specify either onezX`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`.Fz{Zamba requires an initialized `ZambaHybridDynamicCache` to return a cache. None was provided, so no cache will be returned.r#   rc   r   r   )rF  r   r   r   r   r   r&  rQ  T)last_hidden_stater   rE   
attentions)rz   r   r  r&  use_return_dictr0  r  r   r   r   r  r1   r   r   rK   ra   r  _update_causal_maskr  r  _gradient_checkpointing_func__call__r  re   r   to_tuple)r5   r  r   r  r   r  r&  r   r  r  rQ  rE   rF  r   all_hidden_statesall_self_attnsr   layerr\  outputs                       r9   rH   zZambaModel.forward	  s    2C1N-TXT_T_TqTq$8$D $++JjJj 	 "+!6IDKK<Q<Q	%0%<k$++B]B]-t";<s  &&4==Yj I  --i8M%!&]!; 0:
 !"\\-*=*=a*@I]I]^N)33A6L..~}n]"6BD0d )$++ 6 "	:Iu#!m%55!**t}} $ A ANN!*"#%"! !&!+A'#1 +#2&7'#1
! *!,M  #/"}Q'7&99NE"	:H ,,];  -!11?#E#E15O.(+/8Od+%	
 %v;&//*;;r:   c                    | j                   j                  dk(  r	|d|v r|S y |j                  |j                  }}t	        j
                  |      j                  }|j                  d   }|d   dz   }t	        j                  ||f|||      }	|dk7  rt	        j                  |	d      }	|	t	        j                  ||      |j                  dd      kD  z  }	|	d d d d d d f   j                  |j                  d   ddd      }	||	j                         }	|j                         d	k(  rd|j                  d   }
|	d
d |
f   j                  d      |d d d d d d f   j                  d      z  }|	d
d |
f   j!                  ||      |	d
d |
f<   | j                   j                  dk(  r0|.|j                  j"                  dv rt%        j&                  |	|      }	|	S )Nr~  r   r#   r=   )
fill_valuer?   ra   )diagonalrc   r   r<   .r   )r.  xpu)rz   r   r?   ra   r1   finforb  rK   fulltriur   rW   rV   r   r   eqmasked_fillr/  r   _unmask_unattended)r5   r   input_tensorrQ  r?   ra   	min_dtypesequence_lengthtarget_lengthr   mask_lengthpadding_masks               r9   r  zZambaModel._update_causal_maskx  s   ;;++/BB)c^.C%%$**L,?,?vKK&**	&,,Q/&r*Q.jj/=!Ai_dmsta**[1=Ku||M&ANDZDZ[]_`Daaa!$a"23::<;M;Ma;PRSUWY[\%%++-K!!#q(,2226*3+<=@@EWXZ^`dfgWgHhHkHkloHpp1<S,;,=N1O1[1[\hjs1tC+-. KK,,6*%%**o=
 1CCKQZ[Kr:   
NNNNNNNNNN)rN   rO   rP   r   r$   r/   r  r  r   ZAMBA_INPUTS_DOCSTRINGr1   r   r   r   r^   r   rL  r	   r   r   rH   r  rQ   rR   s   @r9   r  r    s*   
-{ -^!" ++AB '+1537=A59$(,0/3&*59k<##k< !.k< u//0	k<
 ""9:k<   1 12k< D>k< $D>k< 'tnk< d^k< !!1!12k< 
u--	.k< Ck<\!r:   r  c                        e Zd Zdef fdZd Zd Zd Zd Zd Z	d Z
 ed	d
d       ee       eee      	 	 	 	 	 	 	 	 	 	 	 	 ddej$                  deej(                     deej$                     dee   deej,                     deej$                     dee   dee   dee   dee   deej$                     deeej(                  f   deeef   fd                     Z	 	 	 	 	 	 ddZ xZS )ZambaForCausalLMrz   c                 $   t         |   |       t        |      | _        dg| j                  j                  | _        |j
                  | _        t        j                  |j                  |j
                  d      | _	        | j                          y )Nzlm_head.weightFr   )r.   r/   r  r_  r  r  r
   r   r6   lm_headr  r:  s     r9   r/   zZambaForCausalLM.__init__  so     '
#3"Tdjj6S6S"T ++yy!3!3V5F5FUS 	r:   c                 .    | j                   j                  S r   r_  r  rL   s    r9   r  z%ZambaForCausalLM.get_input_embeddings      zz&&&r:   c                 &    || j                   _        y r   r  r  s     r9   r  z%ZambaForCausalLM.set_input_embeddings      "'

r:   c                     | j                   S r   r  rL   s    r9   get_output_embeddingsz&ZambaForCausalLM.get_output_embeddings  s    ||r:   c                     || _         y r   r  )r5   new_embeddingss     r9   set_output_embeddingsz&ZambaForCausalLM.set_output_embeddings  s	    %r:   c                     || _         y r   r_  )r5   decoders     r9   set_decoderzZambaForCausalLM.set_decoder  s	    
r:   c                     | j                   S r   r  rL   s    r9   get_decoderzZambaForCausalLM.get_decoder  s    zzr:   num_logits_to_keepz4.50logits_to_keep)versionnew_name)output_typer  r  r   r  r   r  labelsr&  r   r  r  rQ  rT   c                    ||n| j                   j                  }|	|	n| j                   j                  }	|
|
n| j                   j                  }
| j	                  ||||||||	||

      }|d   }t        |t              rt        | d      n|}| j                  |dd|ddf         }d}| | j                  ||| j                  fi |}|
s|f|dd z   }||f|z   S |S t        |||j                  |j                  |j                        S )a  
            labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
                Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
                config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
                (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

            logits_to_keep (`int` or `torch.Tensor`, *optional*):
                If an `int`, compute logits for the last `logits_to_keep` tokens. If `0`, calculate logits for all
                `input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that
                token can save memory, which becomes pretty significant for long sequences or large vocabulary size.
                If a `torch.Tensor`, must be 1D corresponding to the indices to keep in the sequence length dimension.
                This is useful when using packed tensor format (single dimension for batch and sequence length).

        Returns:

        Example:

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

        >>> model = ZambaForCausalLM.from_pretrained("Zyphra/Zamba-7B-v1")
        >>> tokenizer = AutoTokenizer.from_pretrained("Zyphra/Zamba-7B-v1")

        >>> 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  r   r  r   r  r&  r   r  rQ  r  r   r#   losslogitsr   rE   r  )rz   r   r  r  r_  r  r   slicer  loss_functionr  r   r   rE   r  )r5   r  r   r  r   r  r  r&  r   r  r  rQ  r  loss_kwargsrJ  rE   slice_indicesr  r  r  s                       r9   rH   zZambaForCausalLM.forward  sL   f 2C1N-TXT_T_TqTq %9$D $++JjJj 	 &1%<k$++B]B] **)%+'/!5)#  
  
8B>SV8W~ot4]kmA}a,?@A%4%%ffdooUUDY,F'+'7D7V#CVC%#33!//))
 	
r:   c           	      t   |d u }	|	sZ||d   |j                   d   k\  r|d d |j                   d    d f   }nc|j                   d   |j                   d   k7  rD|d d |f   }n:t        | j                  |j                   d   | j                  | j                        }|T|R|j                         j                  d      dz
  }|j                  |dk(  d       |	s|d d |j                   d    d f   }||	rd|i}
nd|j                         i}
|
j                  ||||| j                  j                  |d       |
S )Nr=   r#   r   )r?   ra   r  r  )r  r   r&  r   r  rQ  )rK   r^   rz   r?   ra   longcumsummasked_fill_r   r   r  )r5   r  r   r   r  rQ  r  r&  r   empty_past_kvmodel_inputss              r9   prepare_inputs_for_generationz.ZambaForCausalLM.prepare_inputs_for_generation  sc    (4/  )!"%);;%a.*>*>q*A)A)C&CD	#~';';A'>>%a&78	5Y__Q/tzz$++O %,*>)..077;a?L%%n&91= +A	0B/B/D,DE $+];L')=)=)?@L ,#2&"0"&++"@"@"0		
 r:   )NNNNNNNNNNNr   )NNNNNT)rN   rO   rP   r$   r/   r  r  r  r  r  r  r    r   r  r   r   _CONFIG_FOR_DOCr1   r   r   r   r^   r   rL  r	   r   r   rH   r  rQ   rR   s   @r9   r  r    s   { '(& )6DTU*+AB+AP_` '+1537=A59-1$(,0/3&*5934X
##X
 !.X
 u//0	X

 ""9:X
   1 12X
 ))*X
 D>X
 $D>X
 'tnX
 d^X
 !!1!12X
 c5<</0X
 
u,,	-X
 a C VX
z 9r:   r  a  
    The Zamba Model with a sequence classification head on top (linear layer).

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

    Since it does classification on the last token, it requires to know the position of the last token. If a
    `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
    no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
    padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
    each row of the batch).
    c                   X    e Zd Z fdZd Zd Z ee      	 	 	 	 	 	 	 	 	 	 ddee	j                     dee	j                     dee	j                     deeeee	j                     f      dee	j                     d	ee	j                     d
ee   dee   dee   dee   deeef   fd       Z xZS )ZambaForSequenceClassificationc                    t         |   |       |j                  | _        t        |      | _        | j                  j
                  | _        t        j                  |j                  | j                  d      | _	        | j                          y r4  )r.   r/   
num_labelsr  r_  r  r
   r   r6   scorer  r:  s     r9   r/   z'ZambaForSequenceClassification.__init__c  se      ++'
"&**"?"?YYv114??O
 	r:   c                 .    | j                   j                  S r   r  rL   s    r9   r  z3ZambaForSequenceClassification.get_input_embeddingsm  r  r:   c                 &    || j                   _        y r   r  r  s     r9   r  z3ZambaForSequenceClassification.set_input_embeddingsp  r  r:   r  r   r  r   r  r  r&  r   r  r  rT   c                    |
|
n| j                   j                  }
| j                  ||||||||	|
	      }|d   }| j                  |      }||j                  d   }n|j                  d   }| j                   j
                  |dk7  rt        d      | j                   j
                  d}n||| j                   j
                  k7  j                  |j                  t        j                        }t        j                  |j                  d   |j                  t        j                        }||z  j                  d      }n.d}t        j                  | j                  j                    d       |t        j                  ||j                  	      |f   }d}||j                  |j                        }| j                   j"                  | j$                  dk(  rd
| j                   _        nl| j$                  dkD  rL|j&                  t        j(                  k(  s|j&                  t        j*                  k(  rd| j                   _        nd| j                   _        | j                   j"                  d
k(  rIt-               }| j$                  dk(  r& ||j/                         |j/                               }n |||      }n| j                   j"                  dk(  r=t1               } ||j3                  d| j$                        |j3                  d            }n,| j                   j"                  dk(  rt5               } |||      }|
s|f|dd z   }||f|z   S |S t7        |||j8                  |j:                  |j<                        S )a  
        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)r   r  r   r  r&  r   r  r  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.`rc   
regressionsingle_label_classificationmulti_label_classificationr  )rz   r  r_  r  rK   r  r0  r@   ra   r1   int32r   argmaxr   r   r8   rN   problem_typer  r?   r  r   r   r   r   r   r   r   r   rE   r  )r5   r  r   r  r   r  r  r&  r   r  r  transformer_outputsrE   r  r{   last_non_pad_tokennon_pad_masktoken_indicespooled_logitsr  loss_fctr  s                         r9   rH   z&ZambaForSequenceClassification.forwards  s   ( &1%<k$++B]B]"jj)%+'/!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||Jv}}MOaabYYv}}-F{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#M$9$9$;V^^=MND#M6:D))-JJ+- 2 22t GUWY))-II,.v6#%(;AB(??F)-)9TGf$EvE/ /??-;;*55
 	
r:   r  )rN   rO   rP   r/   r  r  r   r  r   r1   r   r   r	   r   r   r   rL  r   r   rH   rQ   rR   s   @r9   r  r  S  s0    '( ++AB 151537KO59-1$(,0/3&*[
E,,-[
 !.[
 u//0	[

 "%tE4E4E/F(F"GH[
   1 12[
 ))*[
 D>[
 $D>[
 'tn[
 d^[
 
u66	7[
 C[
r:   r  )r  r  r  r^  )r   )Wr   rm  typingr   r   r   r   r   r   r	   r1   torch.utils.checkpointr
   torch.nnr   r   r   activationsr   cache_utilsr   r   
generationr   modeling_attn_mask_utilsr   modeling_flash_attention_utilsr   modeling_outputsr   r   r   modeling_utilsr   r   processing_utilsr   pytorch_utilsr   utilsr   r   r   r   utils.deprecationr    utils.import_utilsr!   r"   configuration_zambar$   &mamba_ssm.ops.selective_scan_interfacer%   r&   +mamba_ssm.ops.triton.selective_state_updater'   causal_conv1dr(   r)   r  r   
get_loggerrN   r   r  Moduler,   rv   r   r   r\   r^   r  r   r   r   r2  r>  rN  rV  ZAMBA_START_DOCSTRINGr^  r  r  r  r  __all__r   r:   r9   <module>r     s  (   D D D    A A ! . ) C 
 G & 1  1 - XR@P=-~DD-7**.0@BVXfg 
 
		H	%J299 J(    L )	UU\\ 	U# 	U%,, 	U[fl [fJ %II%<<% 
% <<	%
 U\\*% % %4I)RYY I)XS]bii S]nryy  < <~ARYY AHEryy EP " Y@? @	@FB J YM% M	Mbs+_ sl  m
%9 m
m
` gr:   