§
    9²¸j¤  ã                   óü   — d Z ddlZddlmZmZmZ ddlmZm	Z	m
Z
 ddlmZ  e	¦   «         rddlmZ  e
j        e¦  «        Z G d	„ d
ed¬¦  «        Ze ed¬¦  «         G d„ de¦  «        ¦   «         ¦   «         ZdgZdS )z
Processor class for Pixtral.
é    Né   )ÚMultiModalDataÚProcessingKwargsÚProcessorMixin)Úauto_docstringÚis_vision_availableÚlogging)Úrequiresé   )Úget_resize_output_image_sizec                   ó"   — e Zd ZdddœddidœZdS )ÚPixtralProcessorKwargsF)ÚpaddingÚreturn_mm_token_type_idsÚreturn_tensorsÚpt)Útext_kwargsÚcommon_kwargsN)Ú__name__Ú
__module__Ú__qualname__Ú	_defaults© ó    ú~/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/models/pixtral/processing_pixtral.pyr   r   $   s8   € € € € € ð Ø(-ð
ð 
ð
 ˜dð
ðð €I€I€Ir   r   F)Útotal)ÚtorchvisionÚtorch)Úbackendsc                   ó¢   ‡ — e Zd ZeZ	 	 	 	 	 	 	 	 ddedefˆ fd	„Zed
ee         fd„¦   «         Z	ˆ fd„Z
deded
efd„Zdd„Zed„ ¦   «         Zˆ xZS )ÚPixtralProcessorNé   r   ú[IMG]ú[IMG_BREAK]ú	[IMG_END]Ú
patch_sizeÚspatial_merge_sizec	                 óŽ  •— t          ¦   «                              |||¬¦  «         || _        || _        || _        |                     | j        ¦  «        | _        || _        || _        |                     | j        ¦  «        | _        |                     | j        ¦  «        | _	        |                     | j        ¦  «        | _
        dS )a–  
        patch_size (`int`, *optional*, defaults to 16):
            Patch size from the vision tower.
        spatial_merge_size (`int`, *optional*, defaults to 1):
            The downsampling factor for the spatial merge operation.
        image_token (`str`, *optional*, defaults to `"[IMG]"`):
            Special token used to denote image location.
        image_break_token (`str`, *optional*, defaults to `"[IMG_BREAK]"`):
            Special token used to denote the end of a line of pixels in an image.
        image_end_token (`str`, *optional*, defaults to `"[IMG_END]"`):
            Special token used to denote the end of an image input.
        )Úchat_templateN)ÚsuperÚ__init__r&   r'   Úimage_tokenÚconvert_tokens_to_idsÚimage_token_idÚimage_break_tokenÚimage_end_tokenÚimage_break_token_idÚimage_end_token_id)ÚselfÚimage_processorÚ	tokenizerr&   r'   r)   r,   r/   r0   ÚkwargsÚ	__class__s             €r   r+   zPixtralProcessor.__init__5   sµ   ø€ õ0 	‰Œ×Ò˜¨)À=ÐÑQÔQÐQà$ˆŒØ"4ˆÔØ&ˆÔØ'×=Ò=¸dÔ>NÑOÔOˆÔØ!2ˆÔØ.ˆÔØ'×=Ò=¸dÔ>NÑOÔOˆÔØ$-×$CÒ$CÀDÔDZÑ$[Ô$[ˆÔ!Ø"+×"AÒ"AÀ$ÔBVÑ"WÔ"WˆÔÐÐr   Úreturnc                 ó*   — | j         | j        | j        gS ©N)r.   r1   r2   )r3   s    r   Úimage_token_idsz PixtralProcessor.image_token_idsY   s   € àÔ# TÔ%>ÀÔ@WÐXÐXr   c                 ó\   •— | j         | j        z  |d<    t          ¦   «         j        |fi |¤ŽS )Nr&   )r&   r'   r*   Ú_process_images)r3   ÚimagesÚimages_kwargsr7   s      €r   r=   z PixtralProcessor._process_images]   s6   ø€ Ø&*¤o¸Ô8OÑ&Oˆ�lÑ#Ø&�u‰wŒwÔ& vÐ?Ð?°Ð?Ð?Ð?r   Úimage_inputsÚ	image_idxc                 óÜ   — | j         | j        z  }|d         |         \  }}||z  }||z  }| j        g|z  | j        gz   g|z  }	d„ |	D ¦   «         }	| j        |	d<   d                     |	¦  «        S )NÚimage_sizesc                 ó   — g | ]	}|D ]}|‘ŒŒ
S r   r   )Ú.0ÚsublistÚitems      r   ú
<listcomp>z8PixtralProcessor.replace_image_token.<locals>.<listcomp>g   s%   € ÐQÐQÐQ 7ÈÐQÐQÀ˜$ÐQÐQÐQÐQr   éÿÿÿÿÚ )r&   r'   r,   r/   r0   Újoin)
r3   r@   rA   r6   r&   ÚheightÚwidthÚnum_height_tokensÚnum_width_tokensÚreplace_tokenss
             r   Úreplace_image_tokenz$PixtralProcessor.replace_image_tokena   s‘   € Ø”_ tÔ'>Ñ>ˆ
Ø$ ]Ô3°IÔ>‰ˆ�Ø" jÑ0ÐØ  JÑ.ÐØÔ+Ð,Ð/?Ñ?À4ÔCYÐBZÑZÐ[Ð^oÑoˆØQÐQ¨nÐQÑQÔQˆØ!Ô1ˆ�rÑØ�wŠw�~Ñ&Ô&Ð&r   c                 ó  — i }|�út           j                             di ¦  «        }|                     |¦  «         |                     dd¦  «        p| j        j        }| j        | j        z  }g }|D ]d\  }}	t          t          j
        ||	df¦  «        |d         |d         f||f¬¦  «        \  }
}|
|z  }||z  }|                     |dz   |z  ¦  «         Œedgt          |¦  «        z  }|                     ||dœ¦  «         t          d	i |¤ŽS )
a»  
        Computes the number of placeholder tokens needed for multimodal inputs with the given sizes.

        Args:
            image_sizes (`list[list[int]]`, *optional*):
                The input sizes formatted as (height, width) per each image.

        Returns:
            `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
            input modalities, along with other useful data.
        Nr?   Úsizer   Úlongest_edge)rS   r&   r   )Únum_image_tokensÚnum_image_patchesr   )r   r   ÚgetÚupdater4   rS   r&   r'   r   ÚnpÚzerosÚappendÚlenr   )r3   rC   r6   Úvision_datar?   rS   r&   rU   rL   rM   Úresized_heightÚresized_widthrN   rO   rV   s                  r   Ú_get_num_multimodal_tokensz+PixtralProcessor._get_num_multimodal_tokensk   sO  € ð ˆØÐ"Ý2Ô<×@Ò@ÀÐRTÑUÔUˆMØ× Ò  Ñ(Ô(Ð(à ×$Ò$ V¨TÑ2Ô2ÐO°dÔ6JÔ6OˆDØœ¨4Ô+BÑBˆJà!ÐØ!,ð Tð T‘�˜Ý0LÝ”H˜f e¨QÐ/Ñ0Ô0Ø˜~Ô.°°^Ô0DÐEØ *¨JÐ7ð1ñ 1ô 1Ñ-� ð
 %3°jÑ$@Ð!Ø#0°JÑ#>Ð Ø ×'Ò'Ð)9¸AÑ)=ÐARÑ(RÑSÔSÐSÐSà!" ¥c¨+Ñ&6Ô&6Ñ 6ÐØ×ÒÐ4DÐ[lÐmÐmÑnÔnÐnåÐ,Ð, Ð,Ð,Ð,r   c                 óD   — | j         j        }| j        j        }||z   dgz   S )NrC   )r5   Úmodel_input_namesr4   )r3   Útokenizer_input_namesÚimage_processor_input_namess      r   rb   z"PixtralProcessor.model_input_names�   s,   € à $¤Ô @ÐØ&*Ô&:Ô&LÐ#Ø$Ð'BÑBÀmÀ_ÑTÐTr   )NNr"   r   Nr#   r$   r%   r:   )r   r   r   r   Úvalid_processor_kwargsÚintr+   ÚpropertyÚlistr;   r=   ÚdictÚstrrQ   r`   rb   Ú__classcell__)r7   s   @r   r!   r!   0   s1  ø€ € € € € ð 4Ðð ØØØ"#ØØØ'Ø#ð"Xð "Xð ð	"Xð
  ð"Xð "Xð "Xð "Xð "Xð "XðH ðY  c¤ð Yð Yð Yñ „XðYð@ð @ð @ð @ð @ð'°ð 'Àð 'ÐSVð 'ð 'ð 'ð 'ð"-ð "-ð "-ð "-ðH ðUð Uñ „XðUð Uð Uð Uð Ur   r!   )Ú__doc__ÚnumpyrY   Úprocessing_utilsr   r   r   Úutilsr   r   r	   Úutils.import_utilsr
   Úimage_processing_pixtralr   Ú
get_loggerr   Úloggerr   r!   Ú__all__r   r   r   ú<module>ru      s\  ððð ð Ð Ð Ð ðð ð ð ð ð ð ð ð ð ð
 BÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AØ *Ð *Ð *Ð *Ð *Ð *ð ÐÑÔð GØFÐFÐFÐFÐFÐFð 
ˆÔ	˜HÑ	%Ô	%€ð	ð 	ð 	ð 	ð 	Ð-°Uð 	ñ 	ô 	ð 	ð Ø	€Ð+Ð,Ñ,Ô,ðaUð aUð aUð aUð aU�~ñ aUô aUñ -Ô,ñ „ðaUðH Ð
€€€r   