+
    œ°³jv8  ã                   óÂ   € R t ^ RIt^RIHt ^RIHtHt ^RIH	t	H
t
Ht ^RIHtHt ^RIHt  ! R R]	R	R
7      tR R ltR R ltR R lt] ! R R]
4      4       tR.tR# )zProcessor class for Mllama.N)ÚBatchFeature)Ú
ImageInputÚmake_nested_list_of_images)ÚProcessingKwargsÚProcessorMixinÚUnpack)ÚPreTokenizedInputÚ	TextInput)Úauto_docstringc                   ó"   € ] tR t^tRR^//tRtR# )ÚMllamaProcessorKwargsÚimage_kwargsÚmax_image_tiles© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú	_defaultsÚ__static_attributes__r   ó    Úm/home/luca/screen-checking/.venv/lib/python3.14/site-packages/transformers/models/mllama/processing_mllama.pyr   r      s   † àØ˜qð
ð„Ir   r   F)Útotalc                ó~   € V ^8„  d   QhR\         \        ,          R\        R\         \         \        ,          ,          /# )é   Ú	input_idsÚimage_token_idÚreturn)ÚlistÚint)Úformats   "r   Ú__annotate__r!   "   s2   € ÷ -ñ -¬d´3­ið -Ìð -ÔQUÔVZÔ[^ÕV_ÕQ`ñ -r   c                óø  € \        V 4       UUu. uF  w  r#W18X  g   K  VNK  	  ppp\        V4      ^ 8X  d   . # \        V4      ^8X  d   V^ ,          R..# \        VRR VR,          4       UUu. uF  w  rVWV.NK
  	  pppVP                  VR,          \        V 4      .4       VR,          ^,          pVRRR1,           F,  p	V	^ ,          V	^,          ^,
          8X  d   W‰^&   V	^,          pK.  	  V# u uppi u uppi )a·  
Generate a cross-attention token mask for image tokens in the input sequence.

This function identifies the positions of image tokens in the input sequence and creates
a mask that defines which subsequent tokens each image token should attend to.

Args:
    input_ids (list[int]): A list of token ids representing the input sequence.
    image_token_id (int): The id of the token used to represent images in the sequence.

Returns:
    list[list[int]]: A list of [start, end] pairs, where each pair represents the range
    of tokens an image token should attend to.

Notes:
    - If no image tokens are present, an empty list is returned.
    - For a single image token, it attends to all subsequent tokens until the end of the sequence.
    - For multiple image tokens, each attends to tokens up to the next image token or the end of the sequence.
    - Consecutive image tokens are treated as a group and attend to all subsequent tokens together.
N:é   NNéÿÿÿÿ)Ú	enumerateÚlenÚzipÚappend)
r   r   ÚiÚtokenÚimage_token_locationsÚloc1Úloc2Úvision_masksÚlast_mask_endÚvision_masks
   &&        r   Úget_cross_attention_token_maskr1   "   s  € ô, 09¸Ô/CÔ_Ñ/C¡8 1ÀuÑG^ŸQ˜QÑ/CÐÑ_ä
Ð Ó! QÔ&Øˆ	ô Ð Ó! QÔ&Ø& qÕ)¨2Ð.Ð/Ð/ä36Ð7LÈSÈbÐ7QÐShÐikÕSlÔ3mÔnÑ3m¡Z T�T“LÑ3m€LÑnð ×ÑÐ.¨rÕ2´C¸	³NÐCÔDð
 ! Õ$ QÕ'€MØ#¡D b D×)Ð)ˆØ�q�>˜[¨�^¨aÕ/Ô/Ø*˜‰NØ# A�Šñ *ð
 Ðùó/ `ùó os   �
C0žC0Á.C6c          
      óÖ   € V ^8„  d   QhR\         \         \         \        ,          ,          ,          R\         \         \        ,          ,          R\        R\        R\        P                  /# )r   Úcross_attention_token_maskÚ	num_tilesÚmax_num_tilesÚlengthr   )r   r   ÚnpÚndarray)r    s   "r   r!   r!   R   sS   € ÷ - ñ - Ü $¤T¬$¬s­)¥_Õ 5ð- ä”Dœ•I�ð- ô ð- ô ð	- ô
 ‡Z�Zñ- r   c           	     óz  € \        V 4      p\        R V  4       4      p\        P                  ! WCWR3\        P                  R7      p\        \        W4      4       F]  w  pw  r‰\        \        W‰4      4       F=  w  p
w  r¼\        V4      ^8X  g   K  Vw  rÞ\        Wã4      pVR8X  d   Tp^WgWÞ1V
RV13&   K?  	  K_  	  V# )a½  
Convert the cross attention mask indices to a cross attention mask 4D array.

This function takes a sparse representation of cross attention masks and converts it to a dense 4D numpy array.
The sparse representation is a nested list structure that defines attention ranges for each image in each batch item.

Args:
    cross_attention_token_mask (list[list[list[int]]]): A nested list structure where:
        - The outer list represents the batch dimension.
        - The middle list represents different images within each batch item.
        - The inner list contains pairs of integers [start, end] representing token ranges for each image.
    num_tiles (list[list[int]]): A nested list structure specifying the number of tiles for each image in each batch item.
    max_num_tiles (int): The maximum possible number of tiles.
    length (int): The total sequence length of the input.

Returns:
    np.ndarray: A 4D numpy array of shape (batch_size, length, max_num_images, max_num_tiles)
        The array contains `1` where attention is allowed and `0` where it is not.

Note:
    - Special handling is done for cases where the end token is -1, which is interpreted as attending to the end of the sequence.
c              3   ó8   "  € T F  p\        V4      x € K  	  R # 5i©N©r&   )Ú.0Úmaskss   & r   Ú	<genexpr>Ú?convert_sparse_cross_attention_mask_to_dense.<locals>.<genexpr>p   s   é € ÐLÑ1K¨œ˜UŸ˜Ó1Kùó   ‚)ÚshapeÚdtypeNr$   )r&   Úmaxr7   ÚzerosÚint64r%   r'   Úmin)r3   r4   r5   r6   Ú
batch_sizeÚmax_num_imagesÚcross_attention_maskÚ
sample_idxÚsample_masksÚsample_num_tilesÚmask_idxÚ	locationsÚmask_num_tilesÚstartÚends   &&&&           r   Ú,convert_sparse_cross_attention_mask_to_denserS   R   sÅ   € ô: Ð/Ó0€JÜÑLÑ1KÓLÓL€NäŸ8š8Ø >ÐAÜ�h‰hôÐô
 9BÄ#ÐF`ÓBlÖ8mÑ4ˆ
Ñ4�\Ü5>¼sÀ<Ó?bÖ5cÑ1ˆHÑ1�yÜ�9‹~ Ö"Ø&‘
�Ü˜#Ó&�Ø˜"”9Ø �CØYZÐ$°°¸HÀoÀ~ÀoÐ%UÓVó 6dñ 9nð  Ðr   c                óH   € V ^8„  d   QhR\         R\         R\         R\         /# )r   ÚpromptÚ	bos_tokenÚimage_tokenr   )Ústr)r    s   "r   r!   r!   ‚   s.   € ÷ "Kñ "K¤Cð "K´Cð "KÄcð "KÌcñ "Kr   c                ó’   € W9   d   V # ^ pV P                  V4      '       d   V \        V4      R p V^,          pK0  W#,           V V  2# )a  
Builds a string from the input prompt by adding `bos_token` if not already present.

Args:
    prompt (`str`):
        The input prompt string.
    bos_token (`str`):
        The beginning of sentence token to be added.
    image_token (`str`):
        The image token used to identify the start of an image sequence.

Returns:
    str: The modified prompt string with the `bos_token` added if necessary.

Examples:
    >>> build_string_from_input("Hello world", "<begin_of_text>", "<|image|>")
    '<begin_of_text>Hello world'

    >>> build_string_from_input("<|image|>Hello world", "<begin_of_text>", "<|image|>")
    '<|image|><begin_of_text>Hello world'

    >>> build_string_from_input("<begin_of_text>Hello world", "<begin_of_text>", "<|image|>")
    '<begin_of_text>Hello world'
N)Ú
startswithr&   )rU   rV   rW   Únum_image_tokens_on_starts   &&& r   Úbuild_string_from_inputr\   ‚   sZ   € ð4 ÔØˆà !ÐØ
×
Ñ
˜K×
(Ò
(Øœ˜KÓ(Ð*Ð+ˆØ! QÕ&Ò!àÕ5Ð6°y°kÀ&ÀÐJÐJr   c                   ó¾   a a€ ] tR t^§t o]tRV 3R llt]RV3R lR ll4       tRV3R lV 3R lllt	RV3R lV 3R lllt
V3R lR	 ltRR
 lt]R 4       tRtVtV ;t# )ÚMllamaProcessorc                óR  <€ \        VR 4      '       g)   RV n        VP                  V P                  4      V n        M"VP                  V n        VP                  V n        RV n        VP                  V P                  4      V n        VP                  V n        \        SV `!  WVR7       R# )rW   z	<|image|>z<|python_tag|>)Úchat_templateN)	ÚhasattrrW   Úconvert_tokens_to_idsr   Úpython_tokenÚpython_token_idrV   ÚsuperÚ__init__)ÚselfÚimage_processorÚ	tokenizerr`   Ú	__class__s   &&&&€r   rf   ÚMllamaProcessor.__init__«   s�   ø€ Ü�y -×0Ò0Ø*ˆDÔØ"+×"AÑ"AÀ$×BRÑBRÓ"SˆDÕà(×4Ñ4ˆDÔØ"+×":Ñ":ˆDÔà,ˆÔØ(×>Ñ>¸t×?PÑ?PÓQˆÔØ"×,Ñ,ˆŒÜ‰Ñ˜À=ÐÖQr   c                ó®   <€ V ^8„  d   QhRS[ R,          RS[S[,          S[S[,          ,          S[S[,          ,          R,          RS[S[,          RS[/# )r   ÚimagesNÚtextÚkwargsr   )r   r	   r   r   r   r   r   )r    Ú__classdict__s   "€r   r!   ÚMllamaProcessor.__annotate__¹   sd   ø€ ÷ 3^ñ 3^á˜TÕ!ð3^ñ Ñ+Õ+©d±9­oÕ=ÁÑEVÕ@WÕWÐZ^Õ^ð3^ñ Ñ.Õ/ð	3^ñ
 
ñ3^r   c           
     ó¾  € V P                  WR7      w  rV P                  ! RRVRV/VB  V P                  ! \        3RV P                  P
                  /VB pVR,          P                  RR4      p/ pVe/   V P                  ! V3/ VR,          B pV P                  W&R.R	7       / pVe.   V P                  ! V3/ VR
,          B w  rxVP                  R4      p	Ven   Vej   VR,           U
u. uF  p
\        W P                  4      NK  	  pp
\        VX	V P                  P                  \        R VR,           4       4      R7      pWÆR&   \        / VCVCVR7      # u up
i )aO  
Returns:
    [`BatchFeature`]: A [`BatchFeature`] with the following fields:

    - **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`.
    - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
      `return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not
      `None`).
    - **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`.
    TODO: add aspect_ratio_ids and aspect_ratio_mask and cross_attention_mask
)rm   rn   rm   rn   Útokenizer_init_kwargsÚtext_kwargsÚreturn_tensorsNÚimage)Ú
modalitiesÚimages_kwargsr4   r   c              3   ó8   "  € T F  p\        V4      x € K  	  R # 5ir;   r<   )r=   r   s   & r   r?   Ú+MllamaProcessor.__call__.<locals>.<genexpr>è   s   é € ÐTÑ;S¨iœ3˜yŸ>˜>Ó;SùrA   )r4   r5   r6   rJ   )ÚdataÚtensor_typer   )Úprepare_inputs_layoutÚvalidate_inputsÚ_merge_kwargsr   ri   Úinit_kwargsÚpopÚ_check_special_mm_tokensÚ_process_imagesr1   r   rS   rh   r   rD   r   )rg   rm   rn   ro   Úoutput_kwargsru   Útext_inputsÚimage_inputsÚ_r4   Ú	token_idsr3   rJ   s   &&&,         r   Ú__call__ÚMllamaProcessor.__call__¸   sŠ  € ð$ ×1Ñ1¸Ð1ÓK‰ˆØ×ÒÑ@ FÐ@°Ð@¸Ò@à×*Ò*Ü!ñ
à"&§.¡.×"<Ñ"<ð
ð ñ
ˆð
 ' }Õ5×9Ñ9Ð:JÈDÓQˆàˆØÒØŸ.š.¨ÑN°¸}Õ1MÑNˆKØ×)Ñ)¨$ÈÈ	Ð)ÔRàˆØÒØ"×2Ò2°6Ñ\¸]È?Õ=[Ñ\‰OˆLØ$×(Ñ(¨Ó5ˆIð Ò $Ò"2ð "-¨[Ö!9ó*á!9�Iô /¨y×:MÑ:MÖNÙ!9ð 'ð *ô $PØ*Ø#Ø"×2Ñ2×BÑBÜÑT¸;À{Ö;SÓTÓTô	$Ð ð 3GÐ.Ñ/äÐ!@ KÐ!@°<Ð!@ÈnÔ]Ð]ùò*s   Ã+Ec                ó’   <€ V ^8„  d   QhRS[ R,          RS[S[,          S[S[,          ,          S[S[,          ,          R,          /# )r   rm   Nrn   )r   r	   r   r   )r    rp   s   "€r   r!   rq   î   sD   ø€ ÷ ñ á˜TÕ!ðñ Ñ+Õ+©d±9­oÕ=ÁÑEVÕ@WÕWÐZ^Õ^ñr   c                óÄ   <€ \         SV `  ! RR VRV/VB vrpVe   \        V4      pVe0   V Uu. uF#  p\        WPP                  V P
                  4      NK%  	  ppW3# u upi )rm   rn   r   )re   r}   r   r\   rV   rW   )rg   rm   rn   ro   r‡   Ú	text_itemrj   s   &&&,  €r   r}   Ú%MllamaProcessor.prepare_inputs_layoutî   sv   ø€ ô !™7Ò8Ñ\ÀÐ\ÈTÐ\ÐU[Ñ\Ðˆ�qð ÒÜ/°Ó7ˆFàÒÙjnÓoÑjnÐ]fÔ+¨I·~±~Àt×GWÑGWÖXÑjnˆDÐoàˆ|Ðùò ps   ¯)Ac                ó¨   <€ V ^8„  d   QhRS[ R,          RS[S[,          S[S[,          ,          S[S[,          ,          R,          RS[S[,          /# )r   rm   Nrn   ro   )r   r	   r   r   r   r   )r    rp   s   "€r   r!   rq   ÿ   sT   ø€ ÷  ñ  á˜TÕ!ð ñ Ñ+Õ+©d±9­oÕ=ÁÑEVÕ@WÕWÐZ^Õ^ð ñ Ñ)Õ*ñ	 r   c                óª  <€ \         S	V `  ! W3/ VB  VEe2   V Uu. uF  qDP                  V P                  4      NK  	  pp\	        V4      ^ 8”  d   Vf   \        R4      hVeä   \        V4      pV Uu. uF  p\        V4      NK  	  pp\        ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      '       dF   \        ;QJ d    R V 4       F  '       d   K   RM	  RM! R V 4       4      '       g   \        R4      hWu8w  d8   Rp\	        V4      \	        V4      8X  d	   Wu8w  d   Rp\        R	V R
V RV 24      hR # R # R # u upi u upi )Nz@No image were provided, but there are image tokens in the promptc              3   ó*   "  € T F	  q^ 8H  x € K  	  R# 5i©é    Nr   ©r=   Ú	batch_imgs   & r   r?   Ú2MllamaProcessor.validate_inputs.<locals>.<genexpr>  s   é € ÐHÑ7G¨) A–~Ó7Gùó   ‚TFc              3   ó*   "  € T F	  q^ 8H  x € K  	  R# 5ir’   r   r”   s   & r   r?   r–     s   é € ð UÙ4D y –NÓ4Dùr—   zaIf a batch of text is provided, there should be either no images or at least one image per sampleÚ zZMake sure to pass your images as a nested list, where each sub-list holds images per batchz)The number of image tokens in each text (zA) should be the same as the number of provided images per batch (z). )
re   r~   ÚcountrW   ÚsumÚ
ValueErrorr   r&   ÚanyÚall)
rg   rm   rn   ro   ÚtÚn_images_in_textÚsampleÚn_images_in_imagesÚadd_messagerj   s
   &&&,     €r   r~   ÚMllamaProcessor.validate_inputsÿ   sm  ø€ ô 	‰Ò Ñ7°Ò7àÓÙCGÓHÁ4¸a§¡¨×(8Ñ(8Ö 9Á4ÐÐHäÐ#Ó$ qÔ(¨Vª^Ü Ð!cÓdÐdØÒ#Ü3°FÓ;�Ù@FÓ%GÁ°f¤c¨&¦kÁÐ"Ð%Gç“3ÑHÑ7GÓH—3—3’3ÑHÑ7GÓH×HÒH×QTÓQTñ UÙ4DóU×QT×QTÒQTñ UÙ4DóU÷ Rò Rô %Ø{óð ð &Ô9Ø"$�KÜÐ-Ó.´#Ð6FÓ2GÔGÐL^ÔLrð 'C˜ä$ØCÐDTÐCUð V@Ø@RÐ?SÐSVÐWbÐVcðeóð ñ :ñ $ñ ùÚHùò &Hs   œ#EÁ3Ec                ó,   <€ V ^8„  d   QhRS[ RS[RS[/# )r   r†   Ú	image_idxr   )Údictr   rX   )r    rp   s   "€r   r!   rq   !  s#   ø€ ÷  ñ  ±ð  Áð  ÑSVñ  r   c                ó   € V P                   # r;   )rW   )rg   r†   r¦   ro   s   &&&,r   Úreplace_image_tokenÚ#MllamaProcessor.replace_image_token!  s   € ð ×ÑÐr   c                óD   € V P                   P                  ! V3RVRV/VB # )a*  
Post-process the output of the model to decode the text.

Args:
    generated_outputs (`torch.Tensor` or `np.ndarray`):
        The output of the model `generate` function. The output is expected to be a tensor of shape `(batch_size, sequence_length)`
        or `(sequence_length,)`.
    skip_special_tokens (`bool`, *optional*, defaults to `True`):
        Whether or not to remove special tokens in the output. Argument passed to the tokenizer's `batch_decode` method.
    clean_up_tokenization_spaces (`bool`, *optional*, defaults to `False`):
        Whether or not to clean up the tokenization spaces. Argument passed to the tokenizer's `batch_decode` method.
    **kwargs:
        Additional arguments to be passed to the tokenizer's `batch_decode method`.

Returns:
    `list[str]`: The decoded text.
Úskip_special_tokensÚclean_up_tokenization_spaces)ri   Úbatch_decode)rg   Úgenerated_outputsr¬   r­   ro   s   &&&&,r   Úpost_process_image_text_to_textÚ/MllamaProcessor.post_process_image_text_to_text&  s;   € ð( �~‰~×*Ò*Øñ
à 3ð
ð *Fð
ð ñ	
ð 	
r   c                óÈ   € V P                   P                  pV P                  P                  pV Uu. uF  q3R 8w  g   K  VNK  	  pp\        W,           R.,           4      # u upi )r4   rJ   )ri   Úmodel_input_namesrh   r   )rg   Útokenizer_input_namesÚimage_processor_input_namesÚnames   &   r   r³   Ú!MllamaProcessor.model_input_namesA  sd   € à $§¡× @Ñ @ÐØ&*×&:Ñ&:×&LÑ&LÐ#ñ 9TÓ&kÑ8S°Ð_jÑWj§t tÑ8SÐ#Ð&kÜÐ)ÕGÐKaÐJbÕbÓcÐcùò 'ls
   ±A¾A)rV   rW   r   rc   rd   r;   )NN)TF)r   r   r   r   r   Úvalid_processor_kwargsrf   r
   r‰   r}   r~   r©   r°   Úpropertyr³   r   Ú__classdictcell__Ú__classcell__)rj   rp   s   @@r   r^   r^   §   sn   ù‡ € à2Ð÷Rð ÷3^ñ 3^ó ð3^÷jõ ÷" õ  ÷D ð  ô

ð6 ñdó ÷dð dr   r^   )Ú__doc__Únumpyr7   Úfeature_extraction_utilsr   Úimage_utilsr   r   Úprocessing_utilsr   r   r   Útokenization_utils_baser   r	   Úutilsr
   r   r1   rS   r\   r^   Ú__all__r   r   r   Ú<module>rÄ      sm   ðñ "ã å 4ß Aß HÑ Hß CÝ #ôÐ,°Eõ õ-õ`- õ`"KðJ ôad�nó adó ðadðH Ð
‚r   