
    i6                        d dl Z d dl mZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZmZ dd	lmZ d
dlmZmZmZmZmZmZ d
dlmZ ddlmZ  ej4                  e      Z G d de      Z G d dej<                        Z G d dej<                        Z  G d de      Z! G d de      Z" G d de      Z# G d de      Z$ G d de      Z%g dZ&y)     N)nn   )ACT2FN)Cache)BaseModelOutputWithPooling)Unpack)auto_docstringlogging)check_model_inputs   )LlavaCausalLMOutputWithPastLlavaForConditionalGeneration
LlavaModelLlavaModelOutputWithPastLlavaPreTrainedModelTransformersKwargs)MistralRMSNorm   )Mistral3Configc                       e Zd Zy)Mistral3RMSNormN__name__
__module____qualname__     w/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/transformers/models/mistral3/modular_mistral3.pyr   r   (       r   r   c                   x     e Zd ZdZdef fdZdej                  dej                  dej                  fdZ xZ	S )Mistral3PatchMergerz<
    Learned merging of spatial_merge_size ** 2 patches
    configc                 "   t         |           || _        |j                  j                  }|j
                  | _        | j                  j                  j                  | _        t        j                  || j
                  dz  z  |d      | _	        y )Nr   Fbias)
super__init__r"   vision_confighidden_sizespatial_merge_size
patch_sizer   Linearmerging_layer)selfr"   r)   	__class__s      r   r'   zMistral3PatchMerger.__init__1   sr    **66"(";";++33>>YY{T5L5La5O'OQ\chir   image_featuresimage_sizesreturnc                    |D cg c]&  }|d   | j                   z  |d   | j                   z  f( }}|D cg c]
  \  }}||z   }}}|j                  d   }g }t        |j                  |            D ]  \  }	}
||	   \  }}|
j	                  |||      j                  ddd      j                  d      }t        j                  j                  j                  || j                  | j                        }|j	                  || j                  dz  z  d      j                         }|j                  |        t        j                  |d      }| j                  |      }|S c c}w c c}}w )Nr   r   r   )kernel_sizestridedim)r+   shape	enumeratesplitviewpermute	unsqueezetorchr   
functionalunfoldr*   tappendcatr-   )r.   r0   r1   
image_sizehwtokens_per_imagedpermuted_tensorimage_indeximage_tokens
image_gridgrids                r   forwardzMistral3PatchMerger.forward:   sl   cn
U_Z]doo-z!}/OP
 
 /::daAE::  $)2>3G3GHX3Y)Z 	)%K{+DAq%**1a3;;Aq!DNNqQJ88&&--(?(?H_H_ . D 99Q!8!8!!;;R@BBDD""4(	) ?:++N;)
 ;s
   +E"E')
r   r   r   __doc__r   r'   r?   TensorrO   __classcell__r/   s   @r   r!   r!   ,   s?    j~ jell  RWR^R^ r   r!   c                   \     e Zd Zdef fdZdej                  dej                  fdZ xZS )Mistral3MultiModalProjectorr"   c                    t         |           t        |j                  j                  |j
                  j                        | _        t        |      | _	        t        |j                  t              rdnt        |j                        | _        t        j                   |j                  j                  | j                  z  |j
                  j                  |j"                        | _        t&        |j(                     | _        t        j                   |j
                  j                  |j
                  j                  |j"                        | _        y )N)epsr   r$   )r&   r'   r   r(   r)   text_configrms_norm_epsnormr!   patch_merger
isinstancevision_feature_layerintlennum_feature_layersr   r,   multimodal_projector_biaslinear_1r   projector_hidden_actactlinear_2)r.   r"   r/   s     r   r'   z$Mistral3MultiModalProjector.__init__S   s    #F$8$8$D$D&J\J\JiJij	/7 F77=A3vGbGbCc 	 		  ,,t/F/FF**11

 &556		**F,>,>,J,JQWQqQq
r   r0   r1   c                     | j                  |      }| j                  ||      }| j                  |      }| j                  |      }| j	                  |      }|S N)rZ   r[   rb   rd   re   )r.   r0   r1   hidden_statess       r   rO   z#Mistral3MultiModalProjector.forwarde   sR    >2**>;Gn5/m4r   )	r   r   r   r   r'   r?   rQ   rO   rR   rS   s   @r   rU   rU   R   s*    
~ 
$ell  r   rU   c                       e Zd Zy)Mistral3CausalLMOutputWithPastNr   r   r   r   rj   rj   n   r   r   rj   c                       e Zd Zy)Mistral3ModelOutputWithPastNr   r   r   r   rl   rl   r   r   r   rl   c                       e Zd Zy)Mistral3PreTrainedModelNr   r   r   r   rn   rn   v   r   r   rn   c            "       "   e Zd Z ed       ed      	 	 ddej                  dej                  dee	e   z  dz  d	e
dz  d
ee   deez  fd              Z ed      e	 	 	 	 	 	 	 	 	 	 	 	 	 ddej                   dz  dej                  dz  dej                  dz  dej                   dz  dedz  dej                  dz  dee	e   z  dz  de
dz  de
dz  d	e
dz  de
dz  dej                   dz  dej                  dz  d
ee   deez  fd              Zy)Mistral3ModelF)tie_last_hidden_stateszWObtains image last hidden states from the vision tower and apply multimodal projection.)custom_introNpixel_valuesr1   r]   output_hidden_stateskwargsr2   c                    |j                         D ci c]  \  }}|	|| }}} | j                  |f|ddd|}t        |t              r|j                  |   }	n3|D 
cg c]  }
|j                  |
    }}
t        j                  |d      }	| j                  |	j                  d      |      }| j                  j                  | j                  j                  z  }t        j                  ||j                        |z  j                  d      j                         }t        j                   |j                  d      |      }||_        |S c c}}w c c}
w )NT)r1   rt   return_dictr4   r7   r   )device)itemsvision_towerr\   r^   rh   r?   rD   multi_modal_projectorsqueezer+   r"   r*   	as_tensorrx   prodtolistr;   pooler_output)r.   rs   r1   r]   rt   ru   kvimage_outputsselected_image_feature	layer_idxhs_poolr0   downsample_ratiosplit_sizess                  r   get_image_featuresz Mistral3Model.get_image_features{   sS    $*<<>C41aQ]!Q$CC)))
#!%	

 
 *C0%2%@%@AU%V"Ocd)}229=dGd%*YYwB%?"334J4R4RST4UWbc,,77$++:X:XX__[1F1FGK[[aafhaippr 	 ^%;%;A%>L&4#3 D es   
EE"E	input_idsattention_maskposition_idspast_key_valuesinputs_embeds	use_cacheoutput_attentionsrw   cache_positionc                    |	|	n| j                   j                  }	|
|
n| j                   j                  }
||n| j                   j                  }|d u |d uz  rt	        d      | | j                         |      }|| j                  |||d      j                  }t        j                  |d      j                  |j                  |j                        }| j                  |||      }|j                  ||      } | j                  d	||||||	|
d|d	|}t!        |j"                  |j$                  |j&                  |j(                  |      S d       S )
Nz:You must specify exactly one of input_ids or inputs_embedsT)rs   r]   r1   rw   r   r7   )r   r0   )	r   r   r   r   r   r   rt   rw   r   )last_hidden_stater   rh   
attentionsimage_hidden_statesr   )r"   r   rt   use_return_dict
ValueErrorget_input_embeddingsr   r   r?   rD   torx   dtypeget_placeholder_maskmasked_scatterlanguage_modelrl   r   r   rh   r   )r.   r   rs   r   r   r   r   r]   r   r   rt   rw   r   r1   ru   r0   special_image_maskoutputss                     r   rO   zMistral3Model.forward   s   & 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B]-t";<YZZ 7D557	BM#!44)%9' 	 5 
 m  #YY~1=@@AUAUWdWjWjkN!%!:!:~ "; " *889K^\M%$%% 
)%+'/!5)
 
 +%77#33!//))2>2J
 	

 QU
 	
r   )NN)NNNNNNNNNNNNN)r   r   r   r   r	   r?   FloatTensorrQ   r^   listboolr   r   tupler   r   
LongTensorr   rl   rO   r   r   r   rp   rp   z   s   u5n 8<,0!''! \\! "DIo4	!
 #Tk! +,! 
+	+! 6!F u5 .215.204(,267;!%)-,0#'26+/=
##d*=
 ''$.=
 t+	=

 &&-=
 =
 ((4/=
 "DIo4=
 $;=
  $;=
 #Tk=
 D[=
 ((4/=
 \\D(=
 +,=
  
,	,!=
  6=
r   rp   c            "          e Zd Ze	 ddej
                  dej                  deee   z  dz  de	e
   deez  f
d       Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddej                  dz  dej
                  dz  d	ej                  dz  d
ej                  dz  dedz  dej
                  dz  dej                  dz  dedz  dedz  dedz  dedz  dej                  dz  deej                  z  dej                  dz  de	e
   deez  f dZy) Mistral3ForConditionalGenerationNrs   r1   r]   ru   r2   c                 B     | j                   j                  d|||d|S )N)rs   r1   r]   r   )modelr   )r.   rs   r1   r]   ru   s        r   r   z3Mistral3ForConditionalGeneration.get_image_features   s5     -tzz,, 
%#!5
 	
 	
r   r   r   r   r   r   labelsr   r   rt   rw   r   logits_to_keepc                 <   |	|	n| j                   j                  }	|
|
n| j                   j                  }
||n| j                   j                  } | j                  d||||||||	|
d||d|}|d   }t        |t              rt        | d      n|}| j                  |dd|ddf         }d}|4 | j                  d||| j                   j                  j                  d|}t        |||j                  |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]`.

        Example:

        ```python
        >>> from PIL import Image
        >>> import httpx
        >>> from io import BytesIO
        >>> 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"
        >>> with httpx.stream("GET", url) as response:
        ...     image = Image.open(BytesIO(response.read()))

        >>> 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   rs   r   r   r   r   r   r   rt   rw   r   r1   r   )logitsr   
vocab_size)lossr   r   rh   r   r   r   )r"   r   rt   r   r   r\   r^   slicelm_headloss_functionrX   r   rj   r   rh   r   r   )r.   r   rs   r   r   r   r   r   r   r   rt   rw   r   r   r1   ru   r   rh   slice_indicesr   r   s                        r   rO   z(Mistral3ForConditionalGeneration.forward   sP   ^ 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B]$** 
%)%+'/!5)#
 
   
8B>SV8W~ot4]kmA}a,?@A%4%% f9P9P9[9[_eD .#33!//)) ' ; ;
 	
r   rg   )NNNNNNNNNNNNr   N)r   r   r   r	   r?   r   rQ   r^   r   r   r   r   r   r   r   r   r   rj   rO   r   r   r   r   r      s   
 8<	
''
 \\
 "DIo4	

 +,
 
+	+
 
  .215.204(,26*.!%)-,0#'26-.+/W
##d*W
 ''$.W
 t+	W

 &&-W
 W
 ((4/W
   4'W
 $;W
  $;W
 #TkW
 D[W
 ((4/W
 ell*W
 \\D(W
  +,!W
" 
/	/#W
r   r   )rp   rn   r   )'r?   r   activationsr   cache_utilsr   modeling_outputsr   processing_utilsr   utilsr	   r
   utils.genericr   llava.modeling_llavar   r   r   r   r   r   mistral.modeling_mistralr   configuration_mistral3r   
get_loggerr   loggerr   Moduler!   rU   rj   rl   rn   rp   r   __all__r   r   r   <module>r      s       !   : & , /  6 2 
		H	%	n 	#")) #L")) 8	%@ 		": 		2 	g
J g
Tg
'D g
Tr   