Ë
    ÜÓ^jÈ8  ã                   ó  — 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 ddl	m
Z
 ddlmZ dd	lmZ dd
lmZ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!m"Z"m#Z#  ejH                  e%«      Z& ed¬«      e G d„ de«      «       «       Z' G d„ de"«      Z( G d„ de#«      Z) ed¬«      e 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#„ d$e «      Z/g d%¢Z0y)&é    N)Ústrict)Únné   )ÚCacheÚDynamicCache)ÚGenerationConfig)ÚFlashAttentionKwargs)ÚBaseModelOutputWithPooling)ÚUnpack)ÚTransformersKwargsÚauto_docstringÚcan_return_tupleÚloggingÚtorch_compilable_check)Úmerge_with_config_defaultsé   )ÚIdefics3ConfigÚIdefics3VisionConfig)ÚIdefics3ImageProcessor)ÚIdefics3ImageProcessorPil)ÚIdefics3BaseModelOutputWithPastÚ Idefics3ForConditionalGenerationÚIdefics3ModelÚIdefics3PreTrainedModelÚIdefics3VisionTransformerz$HuggingFaceTB/SmolVLM2-2.2B-Instruct)Ú
checkpointc                   ó   — e Zd ZdZdZy)ÚSmolVLMVisionConfiga  
    Example:

    ```python
    >>> from transformers.models.smolvlm.modeling_smolvlm import SmolVLMVisionTransformer
    >>> from transformers.models.smolvlm.configuration_smolvlm import SmolVLMVisionConfig

    >>> # Initializing a SmolVLMVisionConfig with google/siglip-so400m-patch14-384 style configuration
    >>> configuration = SmolVLMVisionConfig()

    >>> # Initializing a SmolVLMVisionTransformer (with random weights) from the google/siglip-so400m-patch14-384 style configuration
    >>> model = SmolVLMVisionTransformer(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```Úsmolvlm_visionN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
model_type© ó    úv/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/smolvlm/modular_smolvlm.pyr   r   +   s   „ ñð" "�Jr'   r   c                   ó   — e Zd Zy)ÚSmolVLMPreTrainedModelN©r!   r"   r#   r&   r'   r(   r*   r*   B   ó   „ Ør'   r*   c                   ó   — e Zd Zy)ÚSmolVLMVisionTransformerNr+   r&   r'   r(   r.   r.   F   r,   r'   r.   c                   ó   — e Zd ZdZdZy)ÚSmolVLMConfigaÆ  
    scale_factor (`int`, *optional*, defaults to 2):
        The scale factor for the image encoder.

    Example:
    ```python
    >>> from transformers import SmolVLMModel, SmolVLMConfig
    >>> # Initializing configuration
    >>> configuration = SmolVLMConfig()
    >>> # Initializing a model from the configuration
    >>> model = SmolVLMModel(configuration)
    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```ÚsmolvlmNr    r&   r'   r(   r0   r0   J   s   „ ñð �Jr'   r0   c                   ó   — e Zd Zy)ÚSmolVLMImageProcessorNr+   r&   r'   r(   r3   r3   _   r,   r'   r3   c                   ó   — e Zd Zy)ÚSmolVLMImageProcessorPilNr+   r&   r'   r(   r5   r5   c   r,   r'   r5   c                   ó   — e Zd Zy)ÚSmolVLMBaseModelOutputWithPastNr+   r&   r'   r(   r7   r7   g   r,   r'   r7   c                   ó  — e Zd ZdZdej
                  dej                  dej                  fd„Ze e	d¬«      	 dd	ej                  d
ej
                  dz  dee   deez  fd„«       «       Zee e	d¬«      	 	 	 	 	 	 	 	 	 d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j$                  dz  dej                  dz  dedz  dee   deez  fd„«       «       «       Zy)ÚSmolVLMModelz§
    A subclass of Idefics3Model. We do *not* remove or block the call to inputs_merger
    in forward. Instead, we override inputs_merger here with custom logic.
    Ú	input_idsÚinputs_embedsÚimage_hidden_statesc                 ó   — |j                   \  }}}|€a| | j                  «       t        j                  | j                  j
                  t        j                  |j                  ¬«      «      k(  }|d   }n|| j                  j
                  k(  }|j                  d¬«      }t        t        j                  ||z  dk(  «      d«       ||z  }t        j                  j                  j                  |j                  d¬«      dd¬«      }	|	d d	 }
|j                  d	¬«      }|dz
  |z  }|dz
  |z  }|
j                  d«      |z   }t        j                   |«      }|||   ||   d d …f   ||<   t        j"                  |j                  d	«      ||«      }|S )
N©ÚdtypeÚdevice).r   é   ©Údimr   zCAt least one sample has <image> tokens not divisible by patch_size.)rA   r   )Úvalueéÿÿÿÿ)ÚshapeÚget_input_embeddingsÚtorchÚtensorÚconfigÚimage_token_idÚlongr@   Úsumr   Úallr   Ú
functionalÚpadÚcumsumÚ	unsqueezeÚ
zeros_likeÚwhere)Úselfr:   r;   r<   Ú_Ú
patch_sizeÚ
image_maskÚnum_image_tokensÚblocks_per_sampleÚoffsetsÚblock_offsetÚrow_cumÚ	chunk_idxÚ	local_idxÚ	block_idxÚimage_embedsÚmerged_embedss                    r(   Úinputs_mergerzSmolVLMModel.inputs_mergerq   s“  € ð /×4Ñ4Ñˆˆ:�qàÐØ&Ð*E¨$×*CÑ*CÓ*EÜ—‘˜TŸ[™[×7Ñ7¼u¿z¹zÐR_×RfÑRfÔgó+ñ ˆJð $ FÑ+‰Jà" d§k¡k×&@Ñ&@Ñ@ˆJà%Ÿ>™>¨a˜>Ó0ÐÜÜ�I‰IÐ&¨Ñ3°qÑ8Ó9ØQô	
ð -°
Ñ:Ðä—(‘(×%Ñ%×)Ñ)Ð*;×*BÑ*BÀqÐ*BÓ*IÈ6ÐYZÐ)Ó[ˆØ˜s �|ˆØ×#Ñ#¨Ð#Ó+ˆØ˜q‘[ ZÑ/ˆ	Ø˜q‘[ JÑ.ˆ	Ø ×*Ñ*¨1Ó-°	Ñ9ˆ	ä×'Ñ'¨Ó6ˆØ#6°yÀÑ7LÈiÐXbÑNcÒefÐ7fÑ#gˆ�ZÑ äŸ™ J×$8Ñ$8¸Ó$<¸lÈMÓZˆØÐr'   zVEncodes images into continuous embeddings that can be forwarded to the language model.)Úcustom_introNÚpixel_valuesÚpixel_attention_maskÚkwargsÚreturnc                 óÖ  — |j                   \  }}}}}|j                  | j                  ¬«      } |j                  ||z  g|j                   dd ¢­Ž }|j                   dd j	                  «       }	|dk(  j                  d¬«      |	k7  }
|
dxx   t        j                  |
«       z  cc<   ||
   j                  «       }|€Lt        j                  d	D �cg c]  }|j                   |   ‘Œ c}t        j                  |j                  ¬
«      }n6 |j                  ||z  g|j                   dd ¢­Ž }||
   j                  «       }| j                  j                  j                  }|j                  d||¬«      }|j                  d||¬«      }|j                  d¬«      dkD  j                  «       } | j                   d||ddœ|¤Ž}|j"                  }| j%                  |«      }||_        |S c c}w )a4  
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)`):
            The tensors corresponding to the input images.
        pixel_attention_mask (`torch.LongTensor`, *optional*):
            The attention mask indicating padded regions in the image.
        )r?   r   NrA   g        )rE   éþÿÿÿéýÿÿÿrB   r   )r   r   r   )Úsizer?   r@   )Ú	dimensionrl   Ústep)rE   rj   T)re   Úpatch_attention_maskÚreturn_dictr&   )rF   Útor?   ÚviewÚnumelrM   rH   ÚanyÚ
contiguousÚonesÚboolr@   rJ   Úvision_configrW   ÚunfoldÚvision_modelÚlast_hidden_stateÚ	connectorÚpooler_output)rU   re   rf   rg   Ú
batch_sizeÚ
num_imagesÚnum_channelsÚheightÚwidthÚnb_values_per_imageÚreal_images_indsÚirW   Úpatches_subgridro   Úimage_outputsr<   Úimage_featuress                     r(   Úget_image_featureszSmolVLMModel.get_image_features’   s  € ð  ?K×>PÑ>PÑ;ˆ
�J ¨f°eØ#—‘¨T¯Z©Z�Ó8ˆØ(�|×(Ñ(¨°jÑ)@ÐZÀ<×CUÑCUÐVWÐVXÐCYÒZˆð +×0Ñ0°°Ð4×:Ñ:Ó<ÐØ(¨CÑ/×4Ñ4¸Ð4ÓFÐJ]Ñ]Ðð 	˜Ó¤§	¡	Ð*:Ó ;Ð;Ñ;Óà#Ð$4Ñ5×@Ñ@ÓBˆàÐ'Ü#(§:¡:Ø5>Ö?°�l×(Ñ(¨Ó+Ò?Ü—j‘jØ#×*Ñ*ô$Ñ ð $=Ð#7×#<Ñ#<¸ZÈ*Ñ=TÐ#vÐWk×WqÑWqÐrsÐrtÐWuÒ#vÐ Ø#7Ð8HÑ#I×#TÑ#TÓ#VÐ Ø—[‘[×.Ñ.×9Ñ9ˆ
Ø.×5Ñ5ÀÈ
ÐYcÐ5ÓdˆØ)×0Ñ0¸1À:ÐT^Ð0Ó_ˆØ /× 3Ñ 3¸Ð 3Ó AÀAÑ E×KÑKÓMÐð *˜×)Ñ)ð 
Ø%Ð<PÐ^bñ
Øflñ
ˆð ,×=Ñ=Ðð Ÿ™Ð(;Ó<ˆØ&4ˆÔ#àÐùò/ @s   ÃG&aØ  
        Inputs fed to the model can have an arbitrary number of images. To account for this, pixel_values fed to
        the model have image padding -> (batch_size, max_num_images, 3, max_heights, max_widths) where
        max_num_images is the maximum number of images among the batch_size samples in the batch.
        Padding images are not needed beyond padding the pixel_values at the entrance of the model.
        For efficiency, we only pass through the vision_model's forward the real images by
        discarding the padding images i.e. pixel_values of size (image_batch_size, 3, height, width) where
        image_batch_size would be 7 when num_images_per_sample=[1, 3, 1, 2] and max_num_images would be 3.
        Úattention_maskÚposition_idsÚpast_key_valuesÚ	use_cachec
           	      óÀ  — |�|j                   \  }}n|�|j                   \  }}}nt        d«      ‚|	r|€t        | j                  ¬«      }|€9 | j                  j                  «       |«      j                  |j                  «      }|�|�t        d«      ‚|�:| j                  ||d¬«      j                  }|j                  |j                  «      }n)|�'|j                  | j                  |j                  ¬«      }|�| j                  |||¬«      } | j                  d
|||||	dœ|
¤Ž}t        |j                  |j                  |j                  |j                   |¬	«      S )Nz5You have to specify either input_ids or inputs_embeds)rJ   zMYou cannot specify both pixel_values and image_hidden_states at the same timeT)rp   r>   )r:   r;   r<   )r;   rŠ   r‹   rŒ   r�   )r{   rŒ   Úhidden_statesÚ
attentionsr<   r&   )rF   Ú
ValueErrorr   rJ   Ú
text_modelrG   rq   r@   r‰   r}   r?   rc   r7   r{   rŒ   r�   r�   )rU   r:   rŠ   r‹   rŒ   r;   re   rf   r<   r�   rg   r~   Ú
seq_lengthrV   Úoutputss                  r(   ÚforwardzSmolVLMModel.forwardÊ   s�  € ð4 Ð Ø%.§_¡_Ñ"ˆJ™
ØÐ&Ø(5×(;Ñ(;Ñ%ˆJ˜
¡AäÐTÓUÐUá˜Ð0Ü*°$·+±+Ô>ˆOàÐ ØB˜DŸO™O×@Ñ@ÓBÀ9ÓM×PÑPÐQZ×QaÑQaÓbˆMàÐ#Ð(;Ð(GÜÐlÓmÐmàÐ#Ø"&×"9Ñ"9ØÐ2Àð #:ó #ç‰mð  ð #6×"8Ñ"8¸×9MÑ9MÓ"NÑØ Ð,Ø"5×"8Ñ"8¸t¿z¹zÐR_×RfÑRfÐ"8Ó"gÐàÐ*Ø ×.Ñ.Ø#Ø+Ø$7ð /ó ˆMð "�$—/‘/ð 
Ø'Ø)Ø%Ø+Øñ
ð ñ
ˆô .Ø%×7Ñ7Ø#×3Ñ3Ø!×/Ñ/Ø×)Ñ)Ø 3ô
ð 	
r'   )N)	NNNNNNNNN)r!   r"   r#   r$   rH   Ú
LongTensorÚTensorrc   r   r   ÚFloatTensorr   r   Útupler
   r‰   r   r   Ú
BoolTensorrw   r	   r7   r•   r&   r'   r(   r9   r9   k   s¸  „ ñð
Ø×)Ñ)ðØ:?¿,¹,ðØ]b×]iÑ]ióðB ÙØmôð 9=ñ2à×'Ñ'ð2ð $×.Ñ.°Ñ5ð2ð Ð+Ñ,ð	2ð
 
Ð+Ñ	+ò2óó ð2ðh  ØÙðô
ð .2Ø.2Ø04Ø(,Ø26Ø15Ø8<Ø8<Ø!%ñ;
à×#Ñ# dÑ*ð;
ð Ÿ™ tÑ+ð;
ð ×&Ñ&¨Ñ-ð	;
ð
  ™ð;
ð ×(Ñ(¨4Ñ/ð;
ð ×'Ñ'¨$Ñ.ð;
ð $×.Ñ.°Ñ5ð;
ð #×.Ñ.°Ñ5ð;
ð ˜$‘;ð;
ð Ð-Ñ.ð;
ð 
Ð/Ñ	/ò;
ó
ó ó  ñ;
r'   r9   c                   ó0   ‡ — e Zd ZddiZˆ fd„Zˆ fd„Zˆ xZS )ÚSmolVLMForConditionalGenerationzlm_head.weightz$model.text_model.embed_tokens.weightc                 óJ  •— t         ‰| �  |«       t        |«      | _        t	        j
                  |«      | j                  j                  _        t        j                  |j                  j                  |j                  j                  d¬«      | _        | j                  «        y )NF)Úbias)ÚsuperÚ__init__r9   Úmodelr   Úfrom_model_configr’   Úgeneration_configr   ÚLinearÚtext_configÚhidden_sizeÚ
vocab_sizeÚlm_headÚ	post_init)rU   rJ   Ú	__class__s     €r(   r    z(SmolVLMForConditionalGeneration.__init__  sq   ø€ Ü‰Ñ˜Ô Ü! &Ó)ˆŒ
Ü2B×2TÑ2TÐU[Ó2\ˆ�
‰
×ÑÔ/Ü—y‘y ×!3Ñ!3×!?Ñ!?À×ASÑAS×A^ÑA^ÐejÔkˆŒØ�‰Õr'   c                 ó$   •— t        ‰| �  di |¤Ž y)aÔ	  
        pixel_attention_mask (`torch.Tensor` of shape `(batch_size, image_size, image_size)`, *optional*):
            Mask to avoid performing attention on padding pixel indices.
        image_hidden_states (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)`):
            The hidden states of the image encoder after modality projection.
        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 `model.image_token_id`. Tokens with indices set to `model.image_token_id` are
            ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

        Example:

        ```python
        >>> import httpx
        >>> from io import BytesIO
        >>> import torch
        >>> from PIL import Image
        >>> from io import BytesIO

        >>> from transformers import AutoProcessor, AutoModelForImageTextToText
        >>> from transformers.image_utils import load_image

        >>> # Note that passing the image urls (instead of the actual pil images) to the processor is also possible
        >>> image1 = load_image("https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg")
        >>> image2 = load_image("https://cdn.britannica.com/59/94459-050-DBA42467/Skyline-Chicago.jpg")
        >>> image3 = load_image("https://cdn.britannica.com/68/170868-050-8DDE8263/Golden-Gate-Bridge-San-Francisco.jpg")

        >>> processor = AutoProcessor.from_pretrained("HuggingFaceTB/SmolVLM2-2.2B-Instruct")
        >>> model = AutoModelForImageTextToText.from_pretrained("HuggingFaceTB/SmolVLM2-2.2B-Instruct", dtype=torch.bfloat16, device_map="auto")

        >>> # Create inputs
        >>> messages = [
        ...     {
        ...         "role": "user",
        ...         "content": [
        ...             {"type": "video", "path": path/to/video},
        ...             {"type": "text", "text": "What is happening in this video?"},
        ...         ]
        ...     }
        ... ]

        >>> inputs = processor.apply_chat_template([messages], add_generation_prompt=True)

        >>> # Generate
        >>> generated_ids = model.generate(**inputs, max_new_tokens=256)
        >>> generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)

        >>> print(generated_texts)
        ```Nr&   )rŸ   r•   )rU   Úsuper_kwargsrª   s     €r(   r•   z'SmolVLMForConditionalGeneration.forward  s   ø€ ôd 	‰‰Ñ'˜,Ó'r'   )r!   r"   r#   Ú_tied_weights_keysr    r•   Ú__classcell__)rª   s   @r(   rœ   rœ     s   ø„ Ø*Ð,RÐSÐô÷2(ð 2(r'   rœ   )r   r0   r3   r5   rœ   r*   r9   r.   )1rH   Úhuggingface_hub.dataclassesr   r   Úcache_utilsr   r   Ú
generationr   Úmodeling_flash_attention_utilsr	   Úmodeling_outputsr
   Úprocessing_utilsr   Úutilsr   r   r   r   r   Úutils.genericr   Úidefics3.configuration_idefics3r   r   Ú"idefics3.image_processing_idefics3r   Ú&idefics3.image_processing_pil_idefics3r   Úidefics3.modeling_idefics3r   r   r   r   r   Ú
get_loggerr!   Úloggerr   r*   r.   r0   r3   r5   r7   r9   rœ   Ú__all__r&   r'   r(   ú<module>r¾      s	  ðó" Ý .Ý ç .Ý *Ý BÝ :Ý &ß jÕ jÝ 7ß RÝ GÝ N÷õ ð 
ˆ×	Ñ	˜HÓ	%€ñ ÐAÔBØô"Ð.ó "ó ó Cð"ô*	Ð4ô 	ô	Ð8ô 	ñ ÐAÔBØô�Nó ó ó Cðô&	Ð2ô 	ô	Ð8ô 	ô	Ð%Dô 	ôg
�=ô g
ôT<(Ð&Fô <(ò~	�r'   