o
    if                     @   s<  d Z ddlmZmZmZ ddlZddlmZ ddlmZ ddl	m
Z
mZ ddlmZmZmZmZmZmZ ddlmZ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"m#Z#m$Z$ d	dl%m&Z& d	dl'm(Z(m)Z) ddl*m+Z+ e) rddl,m-Z- ddl.m/Z/m0Z0 ndZ-e( rddl1m2Z2m3Z3 nd\Z3Z2e4e-e2e3fZ5e$6e7Z8G dd deddZ9G dd de
Z
G dd deZ:d6dd Z;G d!d" d"eZ<G d#d$ d$eZ=d%d& Z>G d'd( d(ej?Z@G d)d* d*eZAG d+d, d,eZBG d-d. d.eZCe"G d/d0 d0eZDe"G d1d2 d2eDZEG d3d4 d4eZFg d5ZGdS )7zPyTorch Bamba model.    )Optional	TypedDictUnionN)nn)ACT2FN) HybridMambaAttentionDynamicCacheJambaAttentionDecoderLayer)LlamaAttentionLlamaForCausalLMLlamaMLPLlamaRMSNormLlamaRotaryEmbeddingrotate_half)MambaRMSNormGatedpad_tensor_by_sizereshape_into_chunkssegment_sum   )AttentionMaskConverter)BaseModelOutputWithPastCausalLMOutputWithPast)PreTrainedModel)Unpack)auto_docstringcan_return_tuplelogging)deprecate_kwarg)is_causal_conv1d_availableis_mamba_2_ssm_available   )BambaConfig)selective_state_update)mamba_chunk_scan_combined mamba_split_conv1d_scan_combined)causal_conv1d_fncausal_conv1d_update)NNc                   @   s@   e Zd ZU dZejed< ejed< eed< eed< ejed< dS )BambaFlashAttentionKwargsa  
    Keyword arguments for advanced Flash Attention, causal-conv1d, and mamba_ssm kernel usage.
    Use cases include padding-free training and fewer `torch.compile` graph breaks.

    Attributes:
        cu_seq_lens_q (`torch.LongTensor`)
            Gets cumulative sequence length for query state.
        cu_seq_lens_k (`torch.LongTensor`)
            Gets cumulative sequence length for key state.
        max_length_q (`int`):
            Maximum sequence length for query state.
        max_length_k (`int`):
            Maximum sequence length for key state.
        seq_idx (`torch.IntTensor):
            Index of each packed sequence.
    cu_seq_lens_qcu_seq_lens_kmax_length_qmax_length_kseq_idxN)	__name__
__module____qualname____doc__torch
LongTensor__annotations__int	IntTensor r5   r5   d/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/transformers/models/bamba/modular_bamba.pyr&   K   s   
 

r&   F)totalc                   @   s&   e Zd ZdZejdfdefddZdS )r   a  
    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)`.
    Nconfigc                    s0  |j | _ d| _|j}|j}g | _g | _g | _t|jD ]^}| j | dkrS|  jt	j
 |j|j d|j |  ||dg7  _|  jt	j
 |j|j||dg7  _q|  jt	jg g  dg7  _|  jt	jg g  dg7  _| j| q fddt|jD | _ fddt|jD | _d S )	NFmamba   devicedtyper<   c                        g | ]}t jg g  d qS r>   r0   tensor.0_
batch_sizer<   r5   r6   
<listcomp>        z=HybridMambaAttentionDynamicCache.__init__.<locals>.<listcomp>c                    r?   r@   rA   rC   rF   r5   r6   rH      rI   )layers_block_typehas_previous_statemamba_d_convmamba_d_stateconv_states
ssm_statestransformer_layersrangenum_hidden_layersr0   zerosmamba_expandhidden_sizemamba_n_groupsmamba_n_headsmamba_d_headrB   append	key_cachevalue_cache)selfr8   rG   r=   r<   conv_kernel_sizessm_state_sizeir5   rF   r6   __init__s   sB   	
   z)HybridMambaAttentionDynamicCache.__init__)r,   r-   r.   r/   r0   float16r    r`   r5   r5   r5   r6   r   e   s    r   c                   @      e Zd ZdS )BambaRotaryEmbeddingNr,   r-   r.   r5   r5   r5   r6   rc          rc   c                 C   s   | |}| |}|jd }| dd|f | d|df }}|dd|f |d|df }	}
|| t||  }|	| t|	|  }tj||gdd}tj||
gdd}||fS )a  Applies Rotary Position Embedding to the query and key tensors.

    Removes the interleaving of cos and sin from GLM

    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.
    .Ndim)	unsqueezeshaper   r0   cat)qkcossinposition_idsunsqueeze_dim
rotary_dimq_rotq_passk_rotk_passq_embedk_embedr5   r5   r6   apply_rotary_pos_emb   s   


""ry   c                   @   rb   )BambaAttentionNrd   r5   r5   r5   r6   rz      re   rz   c                   @   rb   )BambaRMSNormGatedNrd   r5   r5   r5   r6   r{      re   r{   c                 C   sN   |dur%|j d dkr%|j d dkr%| j}| |dddddf  |} | S )zm
    Tunes out the hidden states for padding tokens, see https://github.com/state-spaces/mamba/issues/66
    Nr   r   )rj   r=   to)hidden_statesattention_maskr=   r5   r5   r6   apply_mask_to_padding_states   s   $ r   c                       s   e Zd ZdZdedef fddZ				ddejde	e
 d	e	ej d
e	ej de	ej f
ddZ			dde	e
 d	e	ej d
e	ej fddZ				dde	e
 d	e	ej d
e	ej de	ej fddZ  ZS )
BambaMixeruP  
    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)

    The are a few differences between this and Mamba2Mixer:
    - The variable use_precomputed_states is slightly different due to the hybrid cache structure
    - There's a few non-obvious bugs fixed with batching in the slow path that exist in main
    - Some extra variables that our layer doesn't need have been removed
    - We ported most of the refactors in https://github.com/huggingface/transformers/pull/35154, which is (as of Dec 18, 2024) unmerged
    r8   	layer_idxc                    s  t    |j| _|j| _|j| _|j| _t	|j
| j | _|| _|j| _|j| _t|j | _|j| _|j| _|j| _|j| _|j| _dtdf| _d| _d| _ | jd| j | j  | _!t"j#| j!| j!|j| j| j!| jd d| _$| j| j! | j }t"j%| j|| jd| _&t"'t()| j| _*t(+d| jd }t"'t(,|| _-t.| j| jd	| _/t"'t()| j| _0t"j%| j| j| jd| _1t2st34d
 d S t34d d S )N        infgMbP?g?r:   r   )in_channelsout_channelsbiaskernel_sizegroupspadding)r   epsa  The fast path is not available because one of `(selective_state_update, causal_conv1d_fn, causal_conv1d_update)` is None. Falling back to the naive implementation. To install follow https://github.com/state-spaces/mamba/#installation and https://github.com/Dao-AILab/causal-conv1dzDThe fast path for Bamba will be used when running the model on a GPU)5superr`   rW   	num_headsrU   rM   r^   rL   r]   r3   rT   intermediate_sizer   mamba_conv_biasuse_conv_bias
hidden_act
activationr   actmamba_proj_biasuse_biasrms_norm_epslayer_norm_epsilonrV   n_groupsrX   head_dimmamba_chunk_size
chunk_sizefloattime_step_limittime_step_mintime_step_maxconv_dimr   Conv1dconv1dLinearin_proj	Parameterr0   onesdt_biasarangelogA_logr{   normDout_projis_fast_path_availableloggerwarning_once)r\   r8   r   projection_sizeA	__class__r5   r6   r`      sX   

	zBambaMixer.__init__Nr}   cache_paramscache_positionr~   r+   c                 C   s  t ||}| |}|j\}}}	| j| j }
|d uoD|joD|dkoD|j| j jd |j| j jd   ko8|kn  oD|d uoD|d dk}|r)|	dj
| j| j| jgdd\}}}t||j| j | jj	d| jj| j}tj
|| j|
|
gdd\}}}t| j  }|d d d df d d d d d f d| j| jjtjd}|d d d d d f dd| j}| jd d d df d| j}| jd d d df d| j}||| j|jd | j }||| j|jd | j }||| j| j}t|j| j ||||||d |dd
}||| j| j }| ||}|  |d d d df }|S t| j  }| j!d	td
fkr>i nd| j!i}| j"r||d u r|t#|| jj	d| jj| j|f| j| j$|| j| jj| jj%| j j| j j| j| jddd|}|S |j
| j| j| jgdd\}}}|d ur|&dd}t'j()|| j*|jd  df}|j| j +| | jdvr| ,| |&dddd |f &dd}nt-|&dd| jj	d| jj| j|d&dd}t ||}tj
|| j|
|
gdd\}}}t.|||d| j|||||| jd|||| jdf| j$| jd |d| jdd|\}}|d ur:|d ur:|j| j +| |||d}| ||}|  |}|S )Nr   r   rf   rg   .r=   T)zr   dt_softplusr   r   dt_limitF)r   r   r+   r   rmsnorm_weightrmsnorm_epsoutproj_weightoutproj_biasheaddimngroupsnorm_before_gatereturn_final_statesr:   )siluswish)xweightr   r   r+   )r   r   r   r+   r   r   r   )/r   r   rj   r   r^   rK   rN   r   rO   squeezesplitr   r   r   r%   r   r   r   r   r0   expr   r   expandr   r|   float32r   r   viewr!   r   r   r   trainingr#   r   variance_epsilon	transposer   
functionalpadr]   copy_r   r$   r"   )r\   r}   r   r   r~   r+   projected_statesrG   seq_lenrE   groups_time_state_sizeuse_precomputed_statesgatehidden_states_B_CdtBCr   r   r   hidden_states_reshapedoutdt_limit_kwargshidden_states_B_C_transposedrN   scan_output	ssm_stater5   r5   r6   cuda_kernels_forward*  s  
	




<"
^"V
$




zBambaMixer.cuda_kernels_forwardc           3   
      s  |j \}}}|j}t||}|}	|	jjjjgdd\}
}}|d uoQ|joQ|dkoQ|j	j
 j d |jj
 j d   koE|kn  oQ|d uoQ|d dk}|r|j	j
 jddd|j	j
< |d d dd d f |j	j
 j|j	j
 d d d d df< |j	j
 jjjjd}tj|jjd dd}jr|jj }|}n8|d ur|dd}tj|j|j d  df}|j	j
 | |dddd |f dd}t||}tj|jjj jj gdd\}}}tj !  }|r[|jj
 j}|d d dd d f d d d df }|dd"||j d j#}j$d	 "j$j d j#}tjj%|||j }t&|j'd j'd }|d
 "jj#jjtj(d}t|d	 | j|d}|)|jddd d d f }|"|jjj |j d * }|)|d|j d }|d	 |dd d d f  }|)|dj#}||d	  j|d}|jj
 |jj
 | |  |)|jddd d d f }|"|jjj |j d * }|)|d|j d }|jj
 j|j|jd}|+|j j#j}|+|j jd}t,||}|+|jj#}j-d	 "j-j d j#}|||  |j}|)|dd d d df }ntj%|j$ }t&|j'd j'd }|)||dj#! }|)||dj! }|)||dj! }|j.jj djd}|j.jj djd}j/|j/  j/  j-d	 t0|  }||d	  }||j| } fdd||||fD \}}}}|1dddd}tj2|dd}tt3|} |d d d d d d d d d d d f |d d d d d d d d d d d f  }!|!jdd}"|"d	 | 1dddddd	  }#|#jdd}$|$d	 |d d d d d f  jdd}%t|d d d d d d dd f | }&||&1ddddd	  }'|'dd d d f |d	  jdd}(|r|jj
 d d d df j|(jd})nt4|(d d d df })tj5|)|(gdd}(tt3tj|d d d d d d df d}*|*dd}*|*d
 |(d d d d d df  jdd}+|+d d d df |+d d df }(},t|}-|dd d d f |(d d d d d df  }.|-1dddd}/|.d|/d	  }0|%|0 }|)|djj#}|| } dkrB|d d d |d d d d f }|)||d}|,d ur\|d ur\|jj
 |, 6||
}17|1|}2|2S )Nrf   rg   r   r   )shiftsdimsr>   r:   .).N).NNr   r;   )rh   output_sizec                    s   g | ]	}t | jqS r5   )r   r   )rD   tpad_sizer\   r5   r6   rH   ]  s    z,BambaMixer.torch_forward.<locals>.<listcomp>r      )r   r   )8rj   r=   r   r   r   r   r   r   rK   rN   r   rO   rollr|   r<   r   r   r0   sumr   r   r   r   r   r   r   r   r]   r   r   r^   r   r   r   r   r   r   softplusclampr   r   reshape
contiguousr   bmmr   repeat_interleaver   r   permutecumsumr   
zeros_likerk   r   r   )3r\   input_statesr   r   r~   rG   r   rE   r=   r   r   r   r   r   rN   r   r}   r   r   r   cache_devicer   dAdBdBxrO   ssm_states_reshaped
C_reshapedyr   
D_residualA_cumsumLG_intermediateGM_intermediateMY_diagdecay_statesB_decaystatesprevious_statesdecay_chunk
new_statesr   state_decay_outC_times_statesstate_decay_out_permutedY_offr   contextualized_statesr5   r   r6   torch_forward  s   


@,
$"$$$P&*"&0(&
*
 zBambaMixer.torch_forwardc                 K   s   t rd| jjjjv r| |||||S |d urtd|j}|d ur@|jd dkr@|jd dkr@||d d d d d f  	|}| 
||||S )Ncudaz\`seq_idx` support requires fast path support. Please install `mamba_ssm` and `causal_conv1d`r   r   )r   r   r   r<   typer   NotImplementedErrorr=   rj   r|   r  )r\   r}   r   r   r~   r+   kwargsr=   r5   r5   r6   forward  s   	$ zBambaMixer.forward)NNNN)NNN)r,   r-   r.   r/   r    r3   r`   r0   Tensorr   r   r1   r4   r   r  r  __classcell__r5   r5   r   r6   r      sV    D
 .
 Sr   c                   @   rb   )BambaMLPNrd   r5   r5   r5   r6   r    re   r  c                   @   rb   )BambaRMSNormNrd   r5   r5   r5   r6   r    re   r  c                       s   e Zd Zddededef fddZeddd	d
							ddej	de
ej	 de
ej de
e de
e de
e de
ej de
eej	ej	f  dee deeje
eejejf  f fddZ  ZS )BambaDecoderLayerr9   r8   r   
layer_typec                    sp   t  || | `d}|dkrtnd }||| _|| _|dkr(t||d| _d S |dkr4t||| _d S t	d)Nr   r9   )r8   r   	attentionzInvalid layer_type)
r   r`   	self_attnr  feed_forwardr  r   r9   rz   
ValueError)r\   r8   r   r  num_expertsffn_layer_classr   r5   r6   r`     s   
zBambaDecoderLayer.__init__past_key_valuepast_key_valuesz4.58)new_nameversionNFr}   r~   rp   output_attentions	use_cacher   position_embeddingsr  returnc	                 K   s   |}
|  |}| jdkr| jd||||d|	}d}n| jdkr4| jd||||||||d|	\}}|
| }|}
| |}| |}|
| }|f}|rR||f7 }|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_values (`HybridMambaAttentionDynamicCache`, *optional*): cached past key and value projection states
            output_attentions (`bool`, *optional*):
                Whether or not to return the attentions tensors of all attention layers. See `attentions` under
                returned tensors for more detail.
            use_cache (`bool`, *optional*):
                If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
                (see `past_key_values`).
            cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
                Indices depicting the position of the input sequence tokens in the sequence.
            position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*):
                Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`,
                with `head_dim` being the embedding dimension of each attention head.
            kwargs (`dict`, *optional*):
                Arbitrary kwargs. Can be used to provide `BambaFlashAttentionKwargs` for
                padding-free training and/or improve torch.compile performance.
        r9   )r}   r   r   r~   Nr  )r}   r~   rp   r$  r'  r(  r   r)  r5   )input_layernormr  r9   r  pre_ff_layernormr  )r\   r}   r~   rp   r$  r'  r(  r   r)  r  residualself_attn_weightsoutputsr5   r5   r6   r    sD   #


	



zBambaDecoderLayer.forward)r9   )NNNFFNN)r,   r-   r.   r    r3   strr`   r   r0   r  r   r1   r   booltupler   r&   FloatTensorr  r  r5   r5   r   r6   r    s>    	
r  c                       sD   e Zd ZU eed< dZdZdgZdZdZ	dZ
dZ fddZ  ZS )BambaPreTrainedModelr8   modelTr  r$  c                    sV   t  | t|tr)|jjd tt	d|j
d |j_|jjd d S d S )Ng      ?r   )r   _init_weights
isinstancer   r   datafill_r0   r   r   r   r   r   )r\   moduler   r5   r6   r6  /  s   
z"BambaPreTrainedModel._init_weights)r,   r-   r.   r    r2   base_model_prefixsupports_gradient_checkpointing_no_split_modules_skip_keys_device_placement_supports_flash_attn_supports_sdpa_is_statefulr6  r  r5   r5   r   r6   r4  #  s   
 r4  c                       s   e Zd Zdef fddZee									dde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j	 dee defddZdej
dej
dej
dedef
ddZedej
dededejdej
defddZdd Z  ZS )
BambaModelr8   c                    s   t  | |j| _|j| _t|j|j| j| _g }t	|j
D ]}|t|||j| d q t|| _|j| _t|j|jd| _t|d| _d| _|   d S )N)r   r  r   )r8   F)r   r`   pad_token_idpadding_idx
vocab_sizer   	EmbeddingrU   embed_tokensrQ   rR   rY   r  rJ   
ModuleListlayers_attn_implementationr  r   final_layernormrc   
rotary_embgradient_checkpointing	post_init)r\   r8   decoder_layersr_   r   r5   r6   r`   9  s   zBambaModel.__init__N	input_idsr~   rp   r$  inputs_embedsr(  r'  output_hidden_statesr   r  r*  c
                 K   s  |d ur|n| j j}|d ur|n| j j}|d ur|n| j j}|d u |d uA r*td| jr9| jr9|r9td d}|d u rB| 	|}|}|rO|d u rOtd |	d u r^t
j|jd |jd}	|d u rg|	d}| |||	||}| ||	}| ||}|rdnd }|rdnd }| jD ]5}|jd	kr|n|}|r||f7 }||f||||||	|d
|
}|d }|r|d d ur||d f7 }q| |}|r||f7 }|r|jsd|_|sd n|}t||||dS )Nz:You must specify exactly one of input_ids or inputs_embedszX`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`.FzBamba requires an initialized `HybridMambaAttentionDynamicCache` to return a cache. None was provided, so no cache will be returned.r   r>   r   r5   r9   )r~   rp   r$  r'  r(  r   r)  T)last_hidden_stater$  r}   
attentions)r8   r'  rR  r(  r   rM  r   r   r   rG  r0   r   rj   r<   ri   _update_causal_mask_update_mamba_maskrL  rI  r  rK  rK   r   )r\   rP  r~   rp   r$  rQ  r(  r'  rR  r   r  r}   causal_mask
mamba_maskr)  all_hidden_statesall_self_attnsdecoder_layer
layer_masklayer_outputs
next_cacher5   r5   r6   r  L  s~   




	


zBambaModel.forwardinput_tensorc                 C   s   | j jdkr|d urd|v r|S d S |d ur| nd}| j jdkr0|s0tj|||| jdr0d S |j}|jd }t|t	j
rC|jd n|| d }	| j|||	|||jd d}
| j jdkru|d uru|jjd	v ru|sut	|j}t|
|}
|
S )
Nflash_attention_2r   r   sdpa)rQ  past_key_values_lengthis_trainingr   rf   )sequence_lengthtarget_lengthr=   r   rG   )r  xpunpu)r8   rJ  get_seq_lengthr   _ignore_causal_mask_sdpar   r=   rj   r7  r0   r  5_prepare_4d_causal_attention_mask_with_cache_positionr<   r  finfomin_unmask_unattended)r\   r~   r_  r   r$  r'  past_seen_tokensr=   rd  re  rW  	min_dtyper5   r5   r6   rU    sF   



zBambaModel._update_causal_maskrd  re  r=   rG   c                 K   s|  | dur|   dkr| }|S t|j}tj||f|||jd}|dkr+tj|dd}|tj||jd|ddk9 }|ddddddf 	|ddd}| dur|
 }| jd }	| ddddddf | ddddddf kdddd| dddf |}
|ddddddd|	f |
 }|dk}|ddddddd|	f |||ddddddd|	f< |S )	aM  
        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.
        Nr   )
fill_valuer=   r<   r   )diagonalr>   rf   r   )rh   r0   rk  rl  fullr<   triur   r   r   clonerj   r|   masked_fill)r~   rd  re  r=   r   rG   r  rW  ro  mask_lengthpadding_attention_maskpadding_maskr5   r5   r6   rj    s2    $
.$  z@BambaModel._prepare_4d_causal_attention_mask_with_cache_positionc                 C   s.   |}|d dks|durt |dkrd}|S )zv
        No need for zeroing states when
            1. Cached forward
            2. Attending to all inputs
        r   Nr   )r0   all)r\   r~   r   rX  r5   r5   r6   rV  $  s   "zBambaModel._update_mamba_mask)	NNNNNNNNN)r,   r-   r.   r    r`   r   r   r   r0   r1   r  r   r3  r1  r   r&   r   r  rU  staticmethodr3   r=   rj  rV  r  r5   r5   r   r6   rB  7  s|    	
b
<7rB  c                       s   e Zd Z fddZ											dde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j deeejf defddZ						dddZ  ZS )BambaForCausalLMc                    s    t  | |j| _|   d S )N)r   r`   z_loss_coefficientrN  )r\   r8   r   r5   r6   r`   1  s   zBambaForCausalLM.__init__Nr   rP  r~   rp   r$  rQ  labelsr(  r'  rR  r   logits_to_keepr*  c                 K   s   |dur|n| j j}|	dur|	n| j j}	| jd
||||||||	|
d	|}|j}t|tr4t| dn|}| |dd|ddf }d}|durt| j	d
||| j j
d|}| jdkrt|jddj|jdd }|| j|  }t|||j|j|jd	S )aJ  
        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, BambaForCausalLM

        >>> model = BambaForCausalLM.from_pretrained("...")
        >>> tokenizer = AutoTokenizer.from_pretrained("...")

        >>> 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)	rP  r~   rp   r$  rQ  r(  r'  rR  r   )logitsr}  rE  r   rf   rg   r   r:   )lossr  r$  r}   rT  r5   )r8   r'  rR  r5  rS  r7  r3   slicelm_headloss_functionrE  r|  	logsumexpr|   r=   powmeanr   r$  r}   rT  )r\   rP  r~   rp   r$  rQ  r}  r(  r'  rR  r   r~  r  r/  r}   slice_indicesr  r  z_lossr5   r5   r6   r  8  s@   %

 zBambaForCausalLM.forwardTc              	   K   s<  |d u }	|	s5|d us|d |j d kr"|d d |j d  d f }n!|j d |j d kr4|d d |f }nt| j|j d | j| jd}|d url|d u rl| dd }||dkd |	sl|d d |j d  d f }|d urw|	rwd|i}
nd| i}
|
	||||| jj
|d | D ]\}}||
vr||
|< q|
S )Nrf   r   r   r>   rQ  rP  )rp   r$  r(  r~   r~  r   )rj   r   r8   r=   r<   longr   masked_fill_r   updatenum_logits_to_keepitems)r\   rP  r$  r~   rQ  r   rp   r(  r  empty_past_kvmodel_inputskeyvaluer5   r5   r6   prepare_inputs_for_generation  sB   
z.BambaForCausalLM.prepare_inputs_for_generation)NNNNNNNNNNr   )NNNNNT)r,   r-   r.   r`   r   r0   r1   r  r   r3  r1  r   r3   r   r  r  r  r5   r5   r   r6   r{  0  sZ    		

Pr{  )rB  r{  r4  )Nr   )Hr/   typingr   r   r   r0   r   transformers.activationsr   (transformers.models.jamba.modeling_jambar   r   (transformers.models.llama.modeling_llamar	   r
   r   r   r   r   *transformers.models.mamba2.modeling_mamba2r   r   r   r   modeling_attn_mask_utilsr   modeling_outputsr   r   modeling_utilsr   processing_utilsr   utilsr   r   r   utils.deprecationr   utils.import_utilsr   r   configuration_bambar    +mamba_ssm.ops.triton.selective_state_updater!   !mamba_ssm.ops.triton.ssd_combinedr"   r#   causal_conv1dr$   r%   ry  r   
get_loggerr,   r   r&   rc   ry   rz   r{   r   Moduler   r  r  r  r4  rB  r{  __all__r5   r5   r5   r6   <module>   s^    
5
(   ba y 