
    fTh8m                     *   S SK Jr  S SKJrJrJrJr  S SKrS SKJr  SSK	J
r
  SSKJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr  SSKJr  SSKJrJrJrJr  SSKJr  SSKJ r   \" S5       " S S\RB                  5      5       r" " S S\RB                  5      r# " S S\RB                  5      r$\ " S S\5      5       r%\ " S S\5      5       r&\ " S S\5      5       r'\" SS 9 " S! S"\'5      5       r( " S# S$\\5      r)\" S%S 9 " S& S'\'\5      5       r*/ S(Qr+g))    )	dataclass)ListOptionalTupleUnionN)nn   )ACT2FN)GenerationMixin)use_kernel_forward_from_hub)FlashAttentionKwargs)BaseModelOutputWithPastModelOutput)PreTrainedModel)Unpack)
LossKwargsauto_docstringcan_return_tupleis_torchdynamo_compiling   )	AutoModel   )Mistral3ConfigRMSNormc                   8   ^  \ rS rSrSU 4S jjrS rS rSrU =r$ )Mistral3RMSNorm-   c                    > [         TU ]  5         [        R                  " [        R
                  " U5      5      U l        X l        g)z.
Mistral3RMSNorm is equivalent to T5LayerNorm
N)super__init__r   	Parametertorchonesweightvariance_epsilon)selfhidden_sizeeps	__class__s      f/var/www/auris/envauris/lib/python3.13/site-packages/transformers/models/mistral3/modeling_mistral3.pyr    Mistral3RMSNorm.__init__/   s/     	ll5::k#:; #    c                    UR                   nUR                  [        R                  5      nUR	                  S5      R                  SSS9nU[        R                  " X0R                  -   5      -  nU R                  UR                  U5      -  $ )Nr   T)keepdim)	dtypetor"   float32powmeanrsqrtr%   r$   )r&   hidden_statesinput_dtypevariances       r*   forwardMistral3RMSNorm.forward7   sw    #))%((7 $$Q',,R,>%H?T?T4T(UU{{]--k:::r,   c                 ^    [        U R                  R                  5       SU R                   3$ )Nz, eps=)tupler$   shaper%   r&   s    r*   
extra_reprMistral3RMSNorm.extra_repr>   s*    ))*+6$2G2G1HIIr,   )r%   r$   )gư>)	__name__
__module____qualname____firstlineno__r    r9   r?   __static_attributes____classcell__r)   s   @r*   r   r   -   s    $;J Jr,   r   c                      ^  \ rS rSrSrS\4U 4S jjrS\R                  S\R                  S\R                  4S jr	S	r
U =r$ )
Mistral3PatchMergerB   z4
Learned merging of spatial_merge_size ** 2 patches
configc                   > [         TU ]  5         Xl        UR                  R                  nUR
                  U l        U R                  R                  R                  U l        [        R                  " X R
                  S-  -  USS9U l	        g )Nr   Fbias)
r   r    rK   vision_configr'   spatial_merge_size
patch_sizer   Linearmerging_layer)r&   rK   r'   r)   s      r*   r    Mistral3PatchMerger.__init__G   sn    **66"(";";++33>>YY{5L5La5O'OQ\chir,   image_featuresimage_sizesreturnc                    U Vs/ s H&  o3S   U R                   -  US   U R                   -  4PM(     nnU VVs/ s H	  u  pEXE-  PM     nnnUR                  S   n/ n[        UR                  U5      5       H  u  pX)   u  pEU
R	                  XEU5      R                  SSS5      R                  S5      n[        R                  R                  R                  XR                  U R                  S9nUR	                  XpR                  S-  -  S5      R                  5       nUR                  U5        M     [        R                  " USS9nU R                  U5      nU$ s  snf s  snnf )Nr   r   r.   r   )kernel_sizestridedim)rQ   r=   	enumeratesplitviewpermute	unsqueezer"   r   
functionalunfoldrP   tappendcatrS   )r&   rU   rV   
image_sizehwtokens_per_imagedpermuted_tensorimage_indeximage_tokens
image_gridgrids                r*   r9   Mistral3PatchMerger.forwardP   s[   cn
cnU_]doo-z!}/OPcn 	 
 /::kdaAEk:  $)2>3G3GHX3Y)Z%K+DA%**13;;Aq!DNNqQJ88&&--(?(?H_H_ . D 99Q!8!8!!;;R@BBDD""4( *[ ?:++N;)
 ;s
   -EE!)rK   rS   rQ   rP   )rA   rB   rC   rD   __doc__r   r    r"   Tensorr9   rE   rF   rG   s   @r*   rI   rI   B   sD    j~ jell  RWR^R^  r,   rI   c                   j   ^  \ rS rSrS\4U 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )Mistral3MultiModalProjectorh   rK   c                   > [         TU ]  5         [        UR                  R                  UR
                  R                  S9U l        [        U5      U l	        [        UR                  [        5      (       a  SO[        UR                  5      n[        R                  " UR                  R                  U-  UR
                  R                  UR                   S9U l        [$        UR&                     U l        [        R                  " UR
                  R                  UR
                  R                  UR                   S9U l        g )N)r(   r   rM   )r   r    r   rO   r'   text_configrms_norm_epsnormrI   patch_merger
isinstancevision_feature_layerintlenr   rR   multimodal_projector_biaslinear_1r
   projector_hidden_actactlinear_2)r&   rK   num_feature_layersr)   s      r*   r    $Mistral3MultiModalProjector.__init__i   s    #F$8$8$D$D&J\J\JiJij	/7",V-H-H#"N"NQTWX^XsXsTt		  ,,/AA**11

 &556		**F,>,>,J,JQWQqQq
r,   rU   rV   c                     U R                  U5      nU R                  X5      nU R                  U5      nU R                  U5      nU R	                  U5      nU$ N)rz   r{   r   r   r   )r&   rU   rV   r6   s       r*   r9   #Mistral3MultiModalProjector.forwardy   sP    >2**>Gn5/m4r,   )r   r   r   rz   r{   )rA   rB   rC   rD   r   r    r"   rs   r9   rE   rF   rG   s   @r*   ru   ru   h   s/    
~ 
 ell   r,   ru   c                      \ rS rSr% SrSr\\R                     \	S'   Sr
\\R                     \	S'   Sr\\\R                        \	S'   Sr\\\R                        \	S'   Sr\\\R                        \	S'   Sr\\R                     \	S	'   S
rg)Mistral3CausalLMOutputWithPast   a@  
Base class for Mistral3 causal language model (or autoregressive) outputs.

Args:
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
        Language modeling loss (for next-token prediction).
    logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
        Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
    past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
        Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
        `(batch_size, num_heads, sequence_length, embed_size_per_head)`)

        Contains pre-computed hidden-states (key and values in the self-attention blocks) that can be used (see
        `past_key_values` input) to speed up sequential decoding.
    hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
        one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.

        Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
    attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
        Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
        sequence_length)`.

        Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
        heads.
    image_hidden_states (`torch.FloatTensor`, *optional*):
        A `torch.FloatTensor` of size `(batch_size, num_images, sequence_length, hidden_size)`.
        image_hidden_states of the model produced by the vision encoder and after projecting the last hidden state.
Nlosslogitspast_key_valuesr6   
attentionsimage_hidden_states )rA   rB   rC   rD   rr   r   r   r"   FloatTensor__annotations__r   r   r   r6   r   r   r   rE   r   r,   r*   r   r      s    < )-D(5$$
%,*.FHU&&'.9=OXd5#4#456=8<M8E%"3"345<59Ju001297;%"3"34;r,   r   c                   B    \ rS rSr% SrSr\\R                     \	S'   Sr
g)Mistral3ModelOutputWithPast   a  
Base class for Mistral3 outputs, with hidden states and attentions.

Args:
    last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
        Sequence of hidden-states at the output of the last layer of the model.
    past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
        Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
        `(batch_size, num_heads, sequence_length, embed_size_per_head)`)

        Contains pre-computed hidden-states (key and values in the self-attention blocks) that can be used (see
        `past_key_values` input) to speed up sequential decoding.
    hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
        one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.

        Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
    attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
        Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
        sequence_length)`.

        Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
        heads.
    image_hidden_states (`torch.FloatTensor`, *optional*):
        A `torch.FloatTensor` of size `(batch_size, num_images, sequence_length, hidden_size)`.
        image_hidden_states of the model produced by the vision encoder and after projecting the last hidden state.
Nr   r   )rA   rB   rC   rD   rr   r   r   r"   r   r   rE   r   r,   r*   r   r      s    8 8<%"3"34;r,   r   c                   B    \ rS rSr\rSrSrSrSr	Sr
SrSrSrSrS rSrg)Mistral3PreTrainedModel    Tr   c                    [        U R                  SU R                  R                  5       R                  5      n[	        U[
        R                  5      (       aW  UR                  R                  R                  SUS9  UR                  b%  UR                  R                  R                  5         g g [	        U[
        R                  5      (       aJ  UR                  R                  R                  S5        UR                  R                  R                  5         g [	        U[        5      (       a&  UR                  R                  R                  S5        g g )Ninitializer_rangeg        )r4   stdg      ?)getattrrK   get_text_configr   r|   r   rR   r$   datanormal_rN   zero_	LayerNormfill_r   )r&   moduler   s      r*   _init_weights%Mistral3PreTrainedModel._init_weights   s     dkk#68S8S8U8g8ghfbii((MM&&CS&9{{&  &&( '--MM$$S)KK""$00MM$$S) 1r,   r   N)rA   rB   rC   rD   r   config_classbase_model_prefixsupports_gradient_checkpointing_skip_keys_device_placement_supports_cache_class_supports_flash_attn_2_supports_sdpa_supports_quantized_cache_supports_static_cache_supports_attention_backendr   rE   r   r,   r*   r   r      sA    !L&*#"3 !N $!"&*r,   r   zx
    The Mistral3 model which consists of a vision backbone and a language model, without a language modeling head.
    )custom_introc            #         ^  \ rS rSrSS0rS\4U 4S jjrS rS r SS\	R                  S	\	R                  S
\\\\\   4      4S jjr\\             SS\	R&                  S\	R                  S\\	R                     S\\	R&                     S\\\	R                        S\\	R                     S
\\\\\   4      S\\   S\\   S\\   S\\   S\\	R&                     S	\	R                  S\\   S\\\4   4S jj5       5       rSrU =r$ )Mistral3Model   zlanguage_model.modellanguage_modelrK   c                    > [         TU ]  U5        [        R                  " UR                  5      U l        [        U5      U l        [        R                  " UR                  5      U l	        U R                  5         g r   )r   r    r   from_configrO   vision_towerru   multi_modal_projectorrx   r   	post_initr&   rK   r)   s     r*   r    Mistral3Model.__init__   sY     %11&2F2FG%@%H"'33F4F4FGr,   c                 6    U R                   R                  5       $ r   )r   get_input_embeddingsr>   s    r*   r   "Mistral3Model.get_input_embeddings   s    ""7799r,   c                 :    U R                   R                  U5        g r   )r   set_input_embeddingsr&   values     r*   r   "Mistral3Model.set_input_embeddings   s    007r,   pixel_valuesrV   r}   c                    Ub  UOU R                   R                  nUR                  5        VVs0 s H  u  pVUc  M
  XV_M     nnnU R                  " U4USS.UD6n[	        U[
        5      (       a  UR                  U   nO3U V	s/ s H  oR                  U	   PM     n
n	[        R                  " U
SS9nU R                  UR                  S5      U5      nU$ s  snnf s  sn	f )a  
Obtains image last hidden states from the vision tower and apply multimodal projection.

Args:
    pixel_values (`torch.FloatTensor]` of shape `(batch_size, channels, height, width)`):
       The tensors corresponding to the input images.
    vision_feature_layer (`Union[int, List[int]]`, *optional*):
        The index of the layer to select the vision feature. If multiple indices are provided,
        the vision feature of the corresponding indices will be concatenated to form the
        vision features.
    image_sizes (`torch.Tensor`, *optional*):
        Tensor containing the image sizes as returned by the processor.
Returns:
    image_features (`torch.Tensor`): Image feature tensor of shape `(num_images, image_length, embed_dim)`).
T)rV   output_hidden_statesr.   r[   r   )rK   r}   itemsr   r|   r~   r6   r"   rf   r   squeeze)r&   r   rV   r}   kwargskvimage_outputsselected_image_feature	layer_idxhs_poolrU   s               r*   get_image_features Mistral3Model.get_image_features   s    . %9$D $++JjJj 	 $*<<>C>41Q$!$>C)),uKfjuntu *C00%2%@%@AU%V"OcdOc)229=OcGd%*YYwB%?"334J4R4RST4UWbc D es   	CCC	input_idsattention_maskposition_idsr   inputs_embeds	use_cacheoutput_attentionsr   return_dictcache_positionr   rW   c                 `   U	b  U	OU R                   R                  n	U
b  U
OU R                   R                  n
Ub  UOU R                   R                  nUb  UOU R                   R                  nUS L US L-  (       a  [        S5      eUb  Ub  [        S5      eUc  U R                  5       " U5      nUGb$  U R                  UUUS9nXR                   R                  :H  R                  S5      nUR                  U5      R                  UR                  5      n[        5       (       d{  UU   R                  5       UR                  5       :w  aV  XR                   R                  :H  R                  5       nUR                   S   UR                   S   -  n[        SU SU 35      eUR                  UR                  UR"                  5      nUR%                  UU5      nU R&                  " SUUUUUU	U
S	US
.	UD6n[)        UR*                  UR,                  UR.                  UR0                  Ub  WS9$ S S9$ )Nz:You must specify exactly one of input_ids or inputs_embedszdYou cannot specify both pixel_values and inputs_embeds at the same time, and must specify either one)r   r}   rV   r.   r   r   z6Image features and image tokens do not match: tokens: z, features T)	r   r   r   r   r   r   r   r   r   )last_hidden_stater   r6   r   r   r   )rK   r   r   use_return_dictr}   
ValueErrorr   r   image_token_idra   	expand_asr1   devicer   numelsumr=   r0   masked_scatterr   r   r   r   r6   r   )r&   r   r   r   r   r   r   r}   r   r   r   r   r   rV   r   rU   special_image_maskn_image_tokensn_image_featuresoutputss                       r*   r9   Mistral3Model.forward'  sj   & 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B]$8$D $++JjJj 	 -t";<YZZ#(Av    557	BM#!44)%9' 5 N #,{{/I/I"I!T!TUW!X!3!=!=m!L!O!OP]PdPd!e+---@R2S2Y2Y2[_m_s_s_u2u"+{{/I/I"I!N!N!P#1#7#7#:^=Q=QRS=T#T  L^L\\ghxgyz  ,..}/C/C]EXEXYN)889K^\M%% 
)%+'/!5)
 
 +%77#33!//))2>2J
 	

 QU
 	
r,   )r   r   r   r   )NNNNNNNNNNNNN)rA   rB   rC   rD   _checkpoint_conversion_mappingr   r    r   r   r"   r   rs   r   r   r~   r   r   r   r   
LongTensorboolr   r   r   r   r9   rE   rF   rG   s   @r*   r   r      s    '=>N%O"~ :8 AE	&''& \\& 'uS$s)^'<=	&P  '+*.1537=A59@D$(,0/3&*59$(J
##J
 ''J
 !.	J

 u//0J
 "$u'8'8"9:J
   1 12J
 'uS$s)^'<=J
 D>J
 $D>J
 'tnJ
 d^J
 !!1!12J
 \\J
 -.J
  
u11	2!J
  J
r,   r   c                       \ rS rSrSrg)KwargsForCausalLMiv  r   N)rA   rB   rC   rD   rE   r   r,   r*   r   r   v  s    3r,   r   zV
    The MISTRAL3 model which consists of a vision backbone and a language model.
    c            %         ^  \ rS rSrSSSSS.rS/rS\4U 4S	 jjrS
 rS r	S\
R                  4S jrS r\S 5       r\S 5       r\S 5       r\\              S)S\R*                  S\R,                  S\\R0                     S\\R*                     S\\\R,                        S\\R,                     S\\R*                     S\\   S\\   S\\   S\\   S\\R*                     S\\\R0                  4   S\\R0                     S \\   S\\\ 4   4 S! jj5       5       r!      S*U 4S" jjr"\#S\R0                  S#\S$\S%\RH                  S\R0                  S&\4S' j5       r%S(r&U =r'$ )+ Mistral3ForConditionalGenerationiy  zmodel.language_modelzmodel.vision_towerzmodel.multi_modal_projectorlm_head)z^language_model.modelz^vision_towerz^multi_modal_projectorz^language_model.lm_headzlm_head.weightrK   c                    > [         TU ]  U5        [        U5      U l        [        R
                  " UR                  R                  UR                  R                  SS9U l	        U R                  5         g )NFrM   )r   r    r   modelr   rR   rx   r'   
vocab_sizer   r   r   s     r*   r    )Mistral3ForConditionalGeneration.__init__  sS     "6*
yy!3!3!?!?ASASA^A^ejkr,   c                 6    U R                   R                  5       $ r   )r   r   r>   s    r*   r   5Mistral3ForConditionalGeneration.get_input_embeddings  s    zz..00r,   c                 :    U R                   R                  U5        g r   )r   r   r   s     r*   r   5Mistral3ForConditionalGeneration.set_input_embeddings  s    

''.r,   rW   c                     U R                   $ r   r   r>   s    r*   get_output_embeddings6Mistral3ForConditionalGeneration.get_output_embeddings  s    ||r,   c                     Xl         g r   r   )r&   new_embeddingss     r*   set_output_embeddings6Mistral3ForConditionalGeneration.set_output_embeddings  s    %r,   c                 .    U R                   R                  $ r   )r   r   r>   s    r*   r   /Mistral3ForConditionalGeneration.language_model  s    zz(((r,   c                 .    U R                   R                  $ r   )r   r   r>   s    r*   r   -Mistral3ForConditionalGeneration.vision_tower  s    zz&&&r,   c                 .    U R                   R                  $ r   )r   r   r>   s    r*   r   6Mistral3ForConditionalGeneration.multi_modal_projector  s    zz///r,   r   r   r   r   r   r   labelsr   r   r   r   r   logits_to_keeprV   r   c                 F   U	b  U	OU R                   R                  n	U
b  U
OU R                   R                  n
Ub  UOU R                   R                  nU R                  " SUUUUUUUU	U
SUUS.UD6nUS   n[        U[        5      (       a  [        U* S5      OUnU R                  USS2USS24   5      nSnUb3  U R                  " SUXpR                   R                  R                  S.UD6n[        UUUR                  UR                  UR                  UR                   S9$ )a  
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
    Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
    config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
    (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

Example:

```python
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, Mistral3ForConditionalGeneration

>>> model = Mistral3ForConditionalGeneration.from_pretrained("mistralai/Mistral-Small-3.1-24B-Instruct-2503")
>>> processor = AutoProcessor.from_pretrained("mistralai/Mistral-Small-3.1-24B-Instruct-2503")

>>> prompt = "<s>[INST][IMG]What is the image?[/INST]"
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)

>>> inputs = processor(images=image, text=prompt, return_tensors="pt")

>>> # Generate
>>> generate_ids = model.generate(**inputs, max_new_tokens=15)
>>> processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
"What is the image?The image depicts two cats lying on a pink blanket."
```NT)r   r   r   r   r   r   r   r   r   r   r   rV   r   )r   r
  r   )r   r   r   r6   r   r   r   )rK   r   r   r   r   r|   r~   slicer   loss_functionrx   r   r   r   r6   r   r   )r&   r   r   r   r   r   r   r
  r   r   r   r   r   r  rV   r   r   r6   slice_indicesr   r   s                        r*   r9   (Mistral3ForConditionalGeneration.forward  sM   ^ 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B]** 
%)%+'/!5)#
 
   
8B>SV8W8W~ot4]kmA}a,?@A%% f9P9P9[9[_eD .#33!//)) ' ; ;
 	
r,   c           	      P   > [         T
U ]  " U4UUUUUS.UD6n	US   S:X  a  XIS'   U	$ )N)r   r   r   r   r  r   r   )r   prepare_inputs_for_generation)r&   r   r   r   r   r   r   r  r   model_inputsr)   s             r*   r  >Mistral3ForConditionalGeneration.prepare_inputs_for_generation  sT     w<
+')))
 
 !! ,8(r,   sequence_lengthtarget_lengthr0   
batch_sizec                    U b  U R                  5       S:X  a  U nU$ [        R                  " U5      R                  n[        R                  " X4XUR
                  S9nUS:w  a  [        R                  " USS9nU[        R                  " X$R
                  S9UR                  SS5      :  -  nUSSSS2SS24   R                  USSS5      nU b  UR                  5       nU R                  S   n	USS2SS2SS2SU	24   U SS2SSSS24   R                  UR
                  5      -   n
U
S:H  n
USS2SS2SS2SU	24   R                  X5      USS2SS2SS2SU	24'   U$ )	a  
Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape
`(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing.

Args:
    attention_mask (`torch.Tensor`):
        A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape
        `(batch_size, 1, query_length, key_value_length)`.
    sequence_length (`int`):
        The sequence length being processed.
    target_length (`int`):
        The target length: when generating with static cache, the mask should be as long as the static cache,
        to account for the 0 padding, the part of the cache that is not filled yet.
    dtype (`torch.dtype`):
        The dtype to use for the 4D attention mask.
    cache_position (`torch.Tensor`):
        Indices depicting the position of the input sequence tokens in the sequence.
    batch_size (`torch.Tensor`):
        Batch size.
N   )
fill_valuer0   r   r   )diagonal)r   r.   r   )r\   r"   finfominfullr   triuarangereshapeexpandcloner=   r1   masked_fill)r   r  r  r0   r   r  r   causal_mask	min_dtypemask_lengthpadding_masks              r*   5_prepare_4d_causal_attention_mask_with_cache_positionVMistral3ForConditionalGeneration._prepare_4d_causal_attention_mask_with_cache_position  s}   < %.*<*<*>!*C(K* ' E*..I** 0Y\j\q\qK !##jjqA5<<>S>STWeWmWmnprsWtttK%dD!Q&67>>z1bRTUK))//1,2226*1aL[L+@ANSTVZ\`bcScDdDgDg&&E    ,q05@Aq,;,AV5W5c5c 6Aq!\k\12 r,   )r   r   )NNNNNNNNNNNNr   N)NNNNNN)(rA   rB   rC   rD   r   _tied_weights_keysr   r    r   r   r   Moduler   r  propertyr   r   r   r   r   r"   r   r   r   rs   r   r   r   r~   r   r   r   r   r9   r  staticmethodr0   r)  rE   rF   rG   s   @r*   r   r   y  s~    "8-"?#,	&" ++~ 1/ryy & ) ) ' ' 0 0  '+*.1537=A59-1$(,0/3&*5934.2U
##U
 ''U
 !.	U

 u//0U
 "$u'8'8"9:U
   1 12U
 ))*U
 D>U
 $D>U
 'tnU
 d^U
 !!1!12U
 c5<</0U
 ell+U
  *+!U
" 
u44	5#U
  U
t < 444 4 {{	4
 4 4 4r,   r   )r   r   r   ),dataclassesr   typingr   r   r   r   r"   r   activationsr
   
generationr   integrationsr   modeling_flash_attention_utilsr   modeling_outputsr   r   modeling_utilsr   processing_utilsr   utilsr   r   r   r   autor   configuration_mistral3r   r,  r   rI   ru   r   r   r   r   r   r   __all__r   r,   r*   <module>r<     sD  , " / /   ! ) 7 B D - &   2 Y'Jbii J (J(#")) #L")) 4 $<[ $< $<N <"9 < <@ *o * *: 
E
+ E

E
P ?,j > 
T'> T
Tn [r,   