§
    9²¸jØ/  ã                   ó6  — d dl mZ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 ddlmZmZmZ ddlmZmZ  e¦   «         rd dlZ G d	„ d
e
d¬¦  «        ZdZd„  ed¦  «        D ¦   «         d„  ed¦  «        D ¦   «         z   Ze G d„ de¦  «        ¦   «         ZdgZdS )é    )ÚOptionalÚUnioné   )ÚBatchFeature)Ú
ImageInputÚmake_flat_list_of_images)ÚMultiModalDataÚProcessingKwargsÚProcessorMixinÚUnpack)Ú
AddedTokenÚPreTokenizedInputÚ	TextInput)Úauto_docstringÚis_torch_availableNc                   ó,   — e Zd Zddddœdddœddid	œZd
S )ÚColPaliProcessorKwargsÚlongestF)ÚpaddingÚreturn_mm_token_type_idsÚreturn_text_replacement_offsetsÚchannels_firstT)Údata_formatÚdo_convert_rgbÚreturn_tensorsÚpt)Útext_kwargsÚimages_kwargsÚcommon_kwargsN)Ú__name__Ú
__module__Ú__qualname__Ú	_defaults© ó    ú~/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/models/colpali/processing_colpali.pyr   r   "   sK   € € € € € ð !Ø(-Ø/4ð
ð 
ð ,Ø"ð
ð 
ð +¨DÐ1ðð €I€I€Ir%   r   F)Útotalz<image>c                 ó   — g | ]	}d |d›d�‘Œ
S )z<locz0>4ú>r$   ©Ú.0Úis     r&   ú
<listcomp>r-   2   s"   € Ð5Ð5Ð5 A��q����Ð5Ð5Ð5r%   i   c                 ó   — g | ]	}d |d›d�‘Œ
S )z<segz0>3r)   r$   r*   s     r&   r-   r-   2   s"   € Ð8]Ð8]Ð8]ÈQ¸À¸¸¸¸Ð8]Ð8]Ð8]r%   é€   c                   ód  ‡ — e Zd ZeZ	 	 	 	 	 d$dedefˆ fd„Ze	 	 d%dedz  de	e
z  ee	         z  ee
         z  d	ee         d
efˆ fd„¦   «         Zd%ˆ fd„	Z	 	 d%dedz  de	e
z  ee	         z  ee
         z  dz  d	ee         fˆ fd„Zdeded
efd„Zd&d„Zeˆ fd„¦   «         Zed
efd„¦   «         Z ed¬¦  «        	 d&dedz  d
efd„¦   «         Z ed¬¦  «        de	ee	         z  d
efd„¦   «         Z	 	 	 d'deded         f         deded         f         deded          d!ed"ef         d
dfd#„Zˆ xZS )(ÚColPaliProcessorNúDescribe the image.ú
Question: Úvisual_prompt_prefixÚquery_prefixc                 ó  •— || _         || _        t          |d¦  «        st          d¦  «        ‚|j        | _        t          |d¦  «        s]t          t          dd¬¦  «        }d|gi}|                     |¦  «         |                     t          ¦  «        | _	        t          | _
        n|j	        | _	        |j
        | _
        |                     t          ¦  «         d|_        d|_        t          ¦   «                              |||¬¦  «         d	S )
a!  
        visual_prompt_prefix (`str`, *optional*, defaults to `"Describe the image."`):
            A string that gets tokenized and prepended to the image tokens.
        query_prefix (`str`, *optional*, defaults to `"Question: "`):
            A prefix to be used for the query.
        Úimage_seq_lengthz;Image processor is missing an `image_seq_length` attribute.Úimage_tokenFT)Ú
normalizedÚspecialÚadditional_special_tokens)Úchat_templateN)r4   r5   ÚhasattrÚ
ValueErrorr7   r   ÚIMAGE_TOKENÚadd_special_tokensÚconvert_tokens_to_idsÚimage_token_idr8   Ú
add_tokensÚEXTRA_TOKENSÚadd_bos_tokenÚadd_eos_tokenÚsuperÚ__init__)	ÚselfÚimage_processorÚ	tokenizerr<   r4   r5   r8   Útokens_to_addÚ	__class__s	           €r&   rH   zColPaliProcessor.__init__9   s	  ø€ ð %9ˆÔ!Ø(ˆÔÝ�Ð(:Ñ;Ô;ð 	\ÝÐZÑ[Ô[Ð[à /Ô @ˆÔå�y -Ñ0Ô0ð 	5Ý$¥[¸UÈDÐQÑQÔQˆKØ8¸;¸-ÐHˆMØ×(Ò(¨Ñ7Ô7Ð7Ø"+×"AÒ"AÅ+Ñ"NÔ"NˆDÔÝ*ˆDÔÐà"+Ô":ˆDÔØ(Ô4ˆDÔà×Ò�\Ñ*Ô*Ð*Ø"'ˆ	ÔØ"'ˆ	Ôå‰Œ×Ò˜¨)À=ÐÑQÔQÐQÐQÐQr%   ÚimagesÚtextÚkwargsÚreturnc                 óà  •‡ ‡— |�|�t          d¦  «        ‚d|d<    ‰ j        ‰ j        fd‰ j        j        i|¤Ž}|d                              d‰ j        dz  ¦  «        Š|�Fˆ ˆfd	„|D ¦   «         }|d                              d
d¦  «        �|d         d
xx         ‰ j        z  cc<    t          ¦   «         j
        d||dœ|¤Ž}|�)|d                              |d         dk    d¦  «        |d<   |S )aô  
        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`. If `suffix`
              is provided, the `input_ids` will also contain the suffix input ids.
            - **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`.
            - **labels** -- Labels compatible with training if `suffix` is not None
        Nz5Only one of text or images can be processed at a timeTÚreturn_token_type_idsÚtokenizer_init_kwargsr   Úsuffixé
   c                 óD   •— g | ]}‰j         j        › ‰j        › |› ‰› d �‘ŒS ©ú
)rK   Ú	bos_tokenr5   )r+   ÚsamplerI   rU   s     €€r&   r-   z-ColPaliProcessor.__call__.<locals>.<listcomp>}   s:   ø€ ÐjÐjÐjÐ[a�t”~Ô/ÐV°Ô1BÐVÀFÐVÈFÐVÐVÐVÐjÐjÐjr%   Ú
max_length©rN   rO   Ú	input_idsÚtoken_type_idsr   iœÿÿÿÚlabelsr$   )r>   Ú_merge_kwargsÚvalid_processor_kwargsrK   Úinit_kwargsÚpopÚquery_augmentation_tokenÚgetr7   rG   Ú__call__Úmasked_fill)rI   rN   rO   rP   Úoutput_kwargsÚmodel_inputsrU   rM   s   `     @€r&   rg   zColPaliProcessor.__call__^   sD  øøø€ ð& Ð Ð 2ÝÐTÑUÔUÐUà*.ˆÐ&Ñ'Ø*˜Ô*ØÔ'ð
ð 
à"&¤.Ô"<ð
ð ð
ð 
ˆð
 ˜}Ô-×1Ò1°(¸DÔ<YÐ\^Ñ<^Ñ_Ô_ˆàÐØjÐjÐjÐjÐjÐeiÐjÑjÔjˆDØ˜]Ô+×/Ò/°¸dÑCÔCÐOØ˜mÔ,¨\Ð:Ð:Ô:¸dÔ>SÑSÐ:Ð:Ñ:à'•u‘w”wÔ'ÐR¨v¸DÐRÐRÀMÐRÐRˆØÐØ%1°+Ô%>×%JÒ%JÈ<ÐXhÔKiÐmnÒKnÐptÑ%uÔ%uˆL˜Ñ"ØÐr%   c                 óÂ   •‡ —  t          ¦   «         j        d||dœ|¤Ž^}}}|�7t          |¦  «        }ˆ fd„t          t	          |¦  «        ¦  «        D ¦   «         }||d d fS )Nr]   c                 óJ   •— g | ]}‰j         › ‰j        j        › ‰j        › d �‘Œ S rX   )r8   rK   rZ   r4   )r+   Ú_rI   s     €r&   r-   z:ColPaliProcessor.prepare_inputs_layout.<locals>.<listcomp>Š   sG   ø€ ð ð ð àð Ô#Ð\ T¤^Ô%=Ð\¸tÔ?XÐ\Ð\Ð\ðð ð r%   r$   )rG   Úprepare_inputs_layoutr   ÚrangeÚlen)rI   rN   rO   rP   rm   rM   s   `    €r&   rn   z&ColPaliProcessor.prepare_inputs_layout†   s‰   øø€ Ø8�5™7œ7Ô8Ð\ÀÈTÐ\Ð\ÐU[Ð\Ð\Ðˆ��qØÐÝ-¨fÑ5Ô5ˆFðð ð ð å�s 6™{œ{Ñ+Ô+ðñ ô ˆDð �t˜T 4Ð'Ð'r%   c                 óx   •— t          ¦   «                              ||¬¦  «         |€|€t          d¦  «        ‚d S d S )Nr]   z&Either text or images must be provided)rG   Úvalidate_inputsr>   )rI   rN   rO   rP   rM   s       €r&   rr   z ColPaliProcessor.validate_inputs�   sE   ø€ õ 	‰Œ×Ò v°DÐÑ9Ô9Ð9Øˆ<˜F˜NÝÐEÑFÔFÐFð ˆ<˜N˜Nr%   Úimage_inputsÚ	image_idxc                 ó    — | j         | j        z  S ©N)r8   r7   )rI   rs   rt   rP   s       r&   Úreplace_image_tokenz$ColPaliProcessor.replace_image_tokenš   s   € ØÔ $Ô"7Ñ7Ð7r%   c                 ó¨   — i }|�C| j         gt          |¦  «        z  }d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[str]], *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.
        Né   )Únum_image_tokensÚnum_image_patchesr$   )r7   rp   Úupdater	   )rI   Úimage_sizesrP   Úvision_datarz   r{   s         r&   Ú_get_num_multimodal_tokensz+ColPaliProcessor._get_num_multimodal_tokens�   so   € ð ˆØÐ"Ø $Ô 5Ð6½¸[Ñ9IÔ9IÑIÐØ!" ¥c¨+Ñ&6Ô&6Ñ 6ÐØ×ÒÐ4DÐ[lÐmÐmÑnÔnÐnÝÐ,Ð, Ð,Ð,Ð,r%   c                 ó4   •— t          ¦   «         j        ddgz   S )Nr_   r`   )rG   Úmodel_input_names)rI   rM   s    €r&   r�   z"ColPaliProcessor.model_input_names¯   s   ø€ å‰wŒwÔ(Ð,<¸hÐ+GÑGÐGr%   c                 ó   — | j         j        S )zŠ
        Return the query augmentation token.

        Query augmentation buffers are used as reasoning buffers during inference.
        )rK   Ú	pad_token)rI   s    r&   re   z)ColPaliProcessor.query_augmentation_token³   s   € ð Œ~Ô'Ð'r%   znThis method forwards the `images` and `kwargs` arguments to ColPaliProcessor's [`~ColPaliProcessor.__call__`].)Úcustom_introc                 ó    —  | j         dd|i|¤ŽS )NrN   r$   ©rg   )rI   rN   rP   s      r&   Úprocess_imageszColPaliProcessor.process_images¼   s!   € ð ˆtŒ}Ð5Ð5 FÐ5¨fÐ5Ð5Ð5r%   zlThis method forwards the `text` and `kwargs` arguments to ColPaliProcessor's [`~ColPaliProcessor.__call__`].c                 ó    —  | j         dd|i|¤ŽS )NrO   r$   r†   )rI   rO   rP   s      r&   Úprocess_queriesz ColPaliProcessor.process_queriesÆ   s!   € ð ˆtŒ}Ð1Ð1 $Ð1¨&Ð1Ð1Ð1r%   r/   ÚcpuÚquery_embeddingsztorch.TensorÚpassage_embeddingsÚ
batch_sizeÚoutput_dtypeztorch.dtypeÚoutput_deviceztorch.devicec           	      ó8  — t          |¦  «        dk    rt          d¦  «        ‚t          |¦  «        dk    rt          d¦  «        ‚|d         j        |d         j        k    rt          d¦  «        ‚|d         j        |d         j        k    rt          d¦  «        ‚|€|d         j        }g }t	          dt          |¦  «        |¦  «        D �]:}g }t
          j        j        j         	                    ||||z   …         dd¬¦  «        }	t	          dt          |¦  «        |¦  «        D ]�}
t
          j        j        j         	                    ||
|
|z   …         dd¬¦  «        }| 
                    t          j        d	|	|¦  «                             d
¬¦  «        d                              d¬¦  «        ¦  «         Œ‘| 
                    t          j        |d¬¦  «                             |¦  «                             |¦  «        ¦  «         �Œ<t          j        |d¬¦  «        S )aZ  
        Compute the late-interaction/MaxSim score (ColBERT-like) for the given multi-vector
        query embeddings (`qs`) and passage embeddings (`ps`). For ColPali, a passage is the
        image of a document page.

        Because the embedding tensors are multi-vector and can thus have different shapes, they
        should be fed as:
        (1) a list of tensors, where the i-th tensor is of shape (sequence_length_i, embedding_dim)
        (2) a single tensor of shape (n_passages, max_sequence_length, embedding_dim) -> usually
            obtained by padding the list of tensors.

        Args:
            query_embeddings (`Union[torch.Tensor, list[torch.Tensor]`): Query embeddings.
            passage_embeddings (`Union[torch.Tensor, list[torch.Tensor]`): Passage embeddings.
            batch_size (`int`, *optional*, defaults to 128): Batch size for computing scores.
            output_dtype (`torch.dtype`, *optional*, defaults to `torch.float32`): The dtype of the output tensor.
                If `None`, the dtype of the input embeddings is used.
            output_device (`torch.device` or `str`, *optional*, defaults to "cpu"): The device of the output tensor.

        Returns:
            `torch.Tensor`: A tensor of shape `(n_queries, n_passages)` containing the scores. The score
            tensor is saved on the "cpu" device.
        r   zNo queries providedzNo passages providedz/Queries and passages must be on the same devicez-Queries and passages must have the same dtypeNT)Úbatch_firstÚpadding_valuezbnd,csd->bcnsr   )Údimé   ry   )rp   r>   ÚdeviceÚdtypero   ÚtorchÚnnÚutilsÚrnnÚpad_sequenceÚappendÚeinsumÚmaxÚsumÚcatÚto)rI   r‹   rŒ   r�   rŽ   r�   Úscoresr,   Úbatch_scoresÚbatch_queriesÚjÚbatch_passagess               r&   Úscore_retrievalz ColPaliProcessor.score_retrievalÐ   s*  € õ@ ÐÑ Ô  AÒ%Ð%ÝÐ2Ñ3Ô3Ð3ÝÐ!Ñ"Ô" aÒ'Ð'ÝÐ3Ñ4Ô4Ð4à˜AÔÔ%Ð);¸AÔ)>Ô)EÒEÐEÝÐNÑOÔOÐOà˜AÔÔ$Ð(:¸1Ô(=Ô(CÒCÐCÝÐLÑMÔMÐMàÐØ+¨AÔ.Ô4ˆLà%'ˆå�q�#Ð.Ñ/Ô/°Ñ<Ô<ð 	]ñ 	]ˆAØ/1ˆLÝ!œHœNÔ.×;Ò;Ø   Q¨¡^Ð!3Ô4À$ÐVWð <ñ ô ˆMõ ˜1�cÐ"4Ñ5Ô5°zÑBÔBð ð �Ý!&¤¤Ô!3×!@Ò!@Ø& q¨1¨z©>Ð'9Ô:ÈÐ\]ð "Añ "ô "�ð ×#Ò#Ý”L °-ÀÑPÔP×TÒTÐYZÐTÑ[Ô[Ð\]Ô^×bÒbÐghÐbÑiÔiñô ð ð ð �MŠM�%œ) L°aÐ8Ñ8Ô8×;Ò;¸LÑIÔI×LÒLÈ]Ñ[Ô[Ñ\Ô\Ð\Ñ\åŒy˜ QÐ'Ñ'Ô'Ð'r%   )NNNr2   r3   )NNrv   )r/   NrŠ   )r    r!   r"   r   rb   ÚstrrH   r   r   r   r   Úlistr   r   rg   rn   r
   rr   ÚdictÚintrw   r   Úpropertyr�   re   r‡   r‰   r   r   r§   Ú__classcell__)rM   s   @r&   r1   r1   5   s^  ø€ € € € € à3Ðð ØØØ$9Ø(ð#Rð #Rð
 "ð#Rð ð#Rð #Rð #Rð #Rð #Rð #RðJ ð %)ØZ^ð%ð %à˜TÑ!ð%ð Ð+Ñ+¨d°9¬oÑ=ÀÐEVÔ@WÑWð%ð Ð/Ô0ð	%ð
 
ð%ð %ð %ð %ð %ñ „^ð%ðN(ð (ð (ð (ð (ð (ð %)ØaeðGð Gà˜TÑ!ðGð Ð+Ñ+¨d°9¬oÑ=ÀÐEVÔ@WÑWÐZ^Ñ^ðGð Ð)Ô*ð	Gð Gð Gð Gð Gð Gð8°ð 8Àð 8ÐSVð 8ð 8ð 8ð 8ð-ð -ð -ð -ð$ ðHð Hð Hð Hñ „XðHð ð(¨#ð (ð (ð (ñ „Xð(ð €^ð Fðñ ô ð
 %)ð6ð 6à˜TÑ!ð6ð 
ð	6ð 6ð 6ñô ð6ð €^ð Dðñ ô ð2à˜$˜yœ/Ñ)ð2ð 
ð	2ð 2ð 2ñô ð2ð Ø04Ø49ð>(ð >(à °°^Ô0DÐ DÔEð>(ð " .°$°~Ô2FÐ"FÔGð>(ð ð	>(ð
 ˜}Ô-ð>(ð ˜^¨SÐ0Ô1ð>(ð 
ð>(ð >(ð >(ð >(ð >(ð >(ð >(ð >(r%   r1   )Útypingr   r   Úfeature_extraction_utilsr   Úimage_utilsr   r   Úprocessing_utilsr	   r
   r   r   Útokenization_utils_baser   r   r   r™   r   r   r—   r   r?   ro   rD   r1   Ú__all__r$   r%   r&   ú<module>r´      s�  ðð* #Ð "Ð "Ð "Ð "Ð "Ð "Ð "à 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XØ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OÐ OØ 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7ð ÐÑÔð Ø€L€L€Lðð ð ð ð Ð-°Uð ñ ô ð ð €Ø5Ð5¨¨¨t©¬Ð5Ñ5Ô5Ð8]Ð8]ÐRWÐRWÐX[ÑR\ÔR\Ð8]Ñ8]Ô8]Ñ]€ð ðX(ð X(ð X(ð X(ð X(�~ñ X(ô X(ñ „ðX(ðv Ð
€€€r%   