o
    Zh;                     @   s2  d dl mZ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
 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 ddlmZ eeZG dd deZG dd dejZ G dd dejZ!G dd deZ"G dd deZ#G dd deZ$G dd deZ%G dd deZ&g dZ'dS )    )ListOptionalTupleUnionN)nn   )ACT2FN)FlashAttentionKwargs)Unpack)is_torchdynamo_compilinglogging   )KwargsForCausalLMLlavaCausalLMOutputWithPastLlavaForConditionalGeneration
LlavaModelLlavaModelOutputWithPastLlavaPreTrainedModel)MistralRMSNorm   )Mistral3Configc                   @      e Zd ZdS )Mistral3RMSNormN__name__
__module____qualname__ r   r   \/var/www/auris/lib/python3.10/site-packages/transformers/models/mistral3/modular_mistral3.pyr   (       r   c                       sB   e Zd ZdZdef fddZdejdejdejfdd	Z  Z	S )
Mistral3PatchMergerz<
    Learned merging of spatial_merge_size ** 2 patches
    configc                    sL   t    || _|jj}|j| _| jjj| _tj|| jd  |dd| _	d S )Nr   Fbias)
super__init__r!   vision_confighidden_sizespatial_merge_size
patch_sizer   Linearmerging_layer)selfr!   r'   	__class__r   r   r%   1   s   
 zMistral3PatchMerger.__init__image_featuresimage_sizesreturnc                    s    fdd|D }dd |D }|j d }g }t||D ]7\}}|| \}}	|||	|dddd}
tjjj	|
 j
 j
d}|| j
d  d }|| qtj|dd	} |}|S )
Nc                    s(   g | ]}|d   j  |d  j  fqS )r   r   )r)   ).0Z
image_sizer,   r   r   
<listcomp>;   s    z/Mistral3PatchMerger.forward.<locals>.<listcomp>c                 S   s   g | ]\}}|| qS r   r   )r2   hwr   r   r   r4   ?   s    r   r   r   )Zkernel_sizeZstridedim)shape	enumeratesplitviewZpermute	unsqueezetorchr   Z
functionalZunfoldr(   tappendcatr+   )r,   r/   r0   Ztokens_per_imagedZpermuted_tensorZimage_indexZimage_tokensr5   r6   Z
image_gridgridr   r3   r   forward:   s"   



zMistral3PatchMerger.forward)
r   r   r   __doc__r   r%   r?   TensorrE   __classcell__r   r   r-   r   r    ,   s    $	r    c                       s8   e Zd Zdef fddZdejdejfddZ  ZS )Mistral3MultiModalProjectorr!   c                    s   t    t|jj|jjd| _t|| _	t
|jtrdnt|j}tj|jj| |jj|jd| _t|j | _tj|jj|jj|jd| _d S )N)Zepsr   r"   )r$   r%   r   r&   r'   text_configZrms_norm_epsnormr    patch_merger
isinstancevision_feature_layerintlenr   r*   Zmultimodal_projector_biaslinear_1r   Zprojector_hidden_actactlinear_2)r,   r!   Znum_feature_layersr-   r   r   r%   S   s   


z$Mistral3MultiModalProjector.__init__r/   r0   c                 C   s8   |  |}| ||}| |}| |}| |}|S N)rK   rL   rQ   rR   rS   )r,   r/   r0   hidden_statesr   r   r   rE   c   s   



z#Mistral3MultiModalProjector.forward)	r   r   r   r   r%   r?   rG   rE   rH   r   r   r-   r   rI   R   s    rI   c                   @   r   )Mistral3CausalLMOutputWithPastNr   r   r   r   r   rV   l   r   rV   c                   @   r   )Mistral3ModelOutputWithPastNr   r   r   r   r   rW   p   r   rW   c                   @   s   e Zd Zdd ZdS )Mistral3PreTrainedModelc                 C   s   t | jd| j j}t|tjr)|jjj	d|d |j
d ur'|j
j  d S d S t|tjr>|jjd |j
j  d S t|trL|jjd d S d S )Ninitializer_rangeg        )meanstdg      ?)getattrr!   Zget_text_configrY   rM   r   r*   weightdataZnormal_r#   Zzero_Z	LayerNormZfill_r   )r,   moduler[   r   r   r   _init_weightsu   s   

z%Mistral3PreTrainedModel._init_weightsN)r   r   r   r`   r   r   r   r   rX   t   s    rX   c                !   @   s   e Zd Z	ddejdejdeeee	e f  fddZ
													ddejdejdeej d	eej d
ee	ej  deej deeee	e f  dee dee dee dee deej dejdee deeef fddZdS )Mistral3ModelNpixel_valuesr0   rN   c                    s   |dur|n| j j}dd | D }| j|f|dd| t|tr* j| }n fdd|D }tj|dd	}| 	|
d
|}|S )aU  
        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)`).
        Nc                 S   s   i | ]\}}|d ur||qS rT   r   )r2   kvr   r   r   
<dictcomp>   s    z4Mistral3Model.get_image_features.<locals>.<dictcomp>T)r0   output_hidden_statesc                    s   g | ]} j | qS r   )rU   )r2   Z	layer_idxZimage_outputsr   r   r4      s    z4Mistral3Model.get_image_features.<locals>.<listcomp>r7   r8   r   )r!   rN   itemsZvision_towerrM   rO   rU   r?   rB   Zmulti_modal_projectorZsqueeze)r,   rb   r0   rN   kwargsZselected_image_featureZhs_poolr/   r   rg   r   get_image_features   s   
z Mistral3Model.get_image_features	input_idsattention_maskposition_idspast_key_valuesinputs_embeds	use_cacheoutput_attentionsrf   return_dictcache_positionri   r1   c                 K   s  |	d ur|	n| j j}	|
d ur|
n| j j}
|d ur|n| j j}|d ur$|n| j j}|d u |d uA r4td|d ur@|d ur@td|d u rJ|  |}|d ur| j|||d}|| j jk	d}|
||j}t s||  | kr|| j jk }|jd |jd  }td| d| ||j|j}|||}| jd||||||	|
d	|d
	|}t|j|j|j|j|d ur|dS d dS )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)rb   rN   r0   r7   r   r   z6Image features and image tokens do not match: tokens: z, features T)	rl   rm   rn   ro   rp   rq   rf   rr   rs   )last_hidden_statern   rU   
attentionsimage_hidden_statesr   )r!   rq   rf   use_return_dictrN   
ValueErrorZget_input_embeddingsrj   Zimage_token_idr>   Z	expand_astoZdevicer   Znumelsumr:   ZdtypeZmasked_scatterlanguage_modelrW   rt   rn   rU   ru   )r,   rk   rb   rl   rm   rn   ro   rN   rp   rq   rf   rr   rs   r0   ri   r/   Zspecial_image_maskZn_image_tokensZn_image_featuresoutputsr   r   r   rE      sh   

zMistral3Model.forwardrT   )NNNNNNNNNNNNN)r   r   r   r?   FloatTensorrG   r   r   rO   r   rj   
LongTensorboolr
   r	   r   rW   rE   r   r   r   r   ra      sj    
*	

ra   c                #   @   s   e Zd Z														ddejdejdeej deej deeej  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j dee de
eef f ddZdS ) Mistral3ForConditionalGenerationNr   rk   rb   rl   rm   rn   ro   labelsrp   rq   rf   rr   rs   logits_to_keepr0   ri   r1   c                 K   s   |	dur|	n| j j}	|
dur|
n| j j}
|dur|n| j j}| jd||||||||	|
d||d|}|d }t|trBt| dn|}| |dd|ddf }d}|durg| j	d||| j j
jd|}t|||j|j|j|jd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 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)rk   rb   rl   rm   rn   ro   rp   rq   rf   rr   rs   r0   r   )logitsr   
vocab_size)lossr   rn   rU   ru   rv   r   )r!   rq   rf   rw   modelrM   rO   sliceZlm_headZloss_functionrJ   r   rV   rn   rU   ru   rv   )r,   rk   rb   rl   rm   rn   ro   r   rp   rq   rf   rr   rs   r   r0   ri   r|   rU   Zslice_indicesr   r   r   r   r   rE      sL   -z(Mistral3ForConditionalGeneration.forward)NNNNNNNNNNNNr   N)r   r   r   r?   r~   r}   r   rG   r   r   r   rO   r
   r   r   rV   rE   r   r   r   r   r      s`    	

r   )ra   rX   r   )(typingr   r   r   r   r?   r   Zactivationsr   Zmodeling_flash_attention_utilsr	   Zprocessing_utilsr
   utilsr   r   Zllava.modeling_llavar   r   r   r   r   r   Zmistral.modeling_mistralr   Zconfiguration_mistral3r   Z
get_loggerr   loggerr   Moduler    rI   rV   rW   rX   ra   r   __all__r   r   r   r   <module>   s(    
&vY