§
    ‚Štjÿ  ã                   óB  — d Z ddl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 dd
lmZmZmZmZ ddlmZ e G d„ de
¦  «        ¦   «         Z ed¬¦  «        e G d„ de¦  «        ¦   «         ¦   «         Z ed¬¦  «         G d„ de¦  «        ¦   «         ZddgZdS )zPyTorch ColPali modelé    )Ú	dataclassN)Únn)Ú	AutoModelé   )ÚCache)ÚPreTrainedModel)ÚUnpack)ÚModelOutputÚTransformersKwargsÚauto_docstringÚcan_return_tupleé   )ÚColPaliConfigc                   ól   ‡ — e Zd ZU eed<   dZdZg ZdZdZ	dZ
 ej        ¦   «         ˆ fd„¦   «         Zˆ xZS )ÚColPaliPreTrainedModelÚconfigÚmodel)ÚimageÚtextTc                 óJ   •— t          ¦   «                              |¦  «         d S ©N)ÚsuperÚ_init_weights)ÚselfÚmoduleÚ	__class__s     €új/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/colpali/modeling_colpali.pyr   z$ColPaliPreTrainedModel._init_weights(   s!   ø€ å‰Œ×Ò˜fÑ%Ô%Ð%Ð%Ð%ó    )Ú__name__Ú
__module__Ú__qualname__r   Ú__annotations__Úbase_model_prefixÚinput_modalitiesÚ_no_split_modulesÚ_supports_sdpaÚ_supports_flash_attnÚ_supports_flex_attnÚtorchÚno_gradr   Ú__classcell__©r   s   @r   r   r      s{   ø€ € € € € € àÐÐÑØÐØ(ÐØÐØ€NØÐØÐà€U„]�_„_ð&ð &ð &ð &ñ „_ð&ð &ð &ð &ð &r   r   z3
    Base class for ColPali embeddings output.
    )Úcustom_introc                   óÖ   — e Zd ZU dZdZej        dz  ed<   dZej	        dz  ed<   dZ
edz  ed<   dZeej                 dz  ed<   dZeej                 dz  ed<   dZej        dz  ed<   dS )	ÚColPaliForRetrievalOutputaä  
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
        Language modeling loss (for next-token prediction).
    embeddings (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
        The embeddings of the model.
    past_key_values (`Cache`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
        It is a [`~cache_utils.Cache`] instance. For more details, see our [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache).

        Contains pre-computed hidden-states (key and values in the self-attention blocks) that can be used (see
        `past_key_values` input) to speed up sequential decoding.
    image_hidden_states (`torch.FloatTensor`, *optional*):
        A `torch.FloatTensor` of size `(batch_size, num_images, sequence_length, hidden_size)`.
        image_hidden_states of the model produced by the vision encoder after projecting last hidden state.
    NÚlossÚ
embeddingsÚpast_key_valuesÚhidden_statesÚ
attentionsÚimage_hidden_states)r   r    r!   Ú__doc__r0   r)   ÚFloatTensorr"   r1   ÚTensorr2   r   r3   Útupler4   r5   © r   r   r/   r/   -   s´   € € € € € € ðð ð &*€Dˆ%Ô
˜dÑ
"Ð)Ð)Ñ)Ø&*€J�”˜tÑ#Ð*Ð*Ñ*Ø$(€O�U˜T‘\Ð(Ð(Ñ(Ø59€M�5˜Ô*Ô+¨dÑ2Ð9Ð9Ñ9Ø26€J��eÔ'Ô(¨4Ñ/Ð6Ð6Ñ6Ø48Ð˜Ô*¨TÑ1Ð8Ð8Ñ8Ð8Ð8r   r/   u/  
    The ColPali architecture leverages VLMs to construct efficient multi-vector embeddings directly
    from document images (â€œscreenshotsâ€�) for document retrieval. The model is trained to maximize the similarity
    between these document embeddings and the corresponding query embeddings, using the late interaction method
    introduced in ColBERT.

    Using ColPali removes the need for potentially complex and brittle layout recognition and OCR pipelines with a
    single model that can take into account both the textual and visual content (layout, charts, etc.) of a document.

    ColPali is part of the ColVision model family, which was first introduced in the following paper:
    [*ColPali: Efficient Document Retrieval with Vision Language Models*](https://huggingface.co/papers/2407.01449).
    c                   ó¨   ‡ — e Zd ZdZdefˆ fd„Zee	 	 	 ddej	        dz  dej
        dz  dej        dz  dee         d	ef
d
„¦   «         ¦   «         Zˆ xZS )ÚColPaliForRetrievalÚvlmr   c                 óp  •— t          ¦   «                              |¦  «         || _        |j        j        j        | _        t          j        |j        ¦  «        | _        | j        j	        | _	        t          j        | j        j        j        j        | j	        ¦  «        | _        |                      ¦   «          d S r   )r   Ú__init__r   Ú
vlm_configÚtext_configÚ
vocab_sizer   Úfrom_configr=   Úembedding_dimr   ÚLinearÚhidden_sizeÚembedding_proj_layerÚ	post_init)r   r   r   s     €r   r?   zColPaliForRetrieval.__init__\   s”   ø€ Ý‰Œ×Ò˜Ñ Ô Ð ØˆŒØ Ô+Ô7ÔBˆŒåÔ(¨Ô):Ñ;Ô;ˆŒà!œ[Ô6ˆÔÝ$&¤IØŒKÔ"Ô.Ô:ØÔñ%
ô %
ˆÔ!ð
 	�ŠÑÔÐÐÐr   NÚ	input_idsÚpixel_valuesÚattention_maskÚkwargsÚreturnc                 óú  — |�|                      | j        ¬¦  «        }|                     dd ¦  «        }|€| j        j        } | j        d	|||ddœ|¤Ž}|r|j        nd }|�|j        nd }|d         }	| j        j	        j        }
|                      |	                      |
¦  «        ¦  «        }|| 
                    dd¬¦  «        z  }|�||                     d¦  «        z  }t          ||j        ||j        |¬¦  «        S )
N)ÚdtypeÚoutput_hidden_statesT)rI   rK   rJ   rP   r   éÿÿÿÿ)ÚdimÚkeepdim)r1   r2   r3   r4   r5   r:   )ÚtorO   Úpopr   rP   r=   r3   r5   rG   ÚweightÚnormÚ	unsqueezer/   r2   r4   )r   rI   rJ   rK   rL   rP   Ú
vlm_outputÚvlm_hidden_statesÚvlm_image_hidden_statesÚlast_hidden_statesÚ
proj_dtyper1   s               r   ÚforwardzColPaliForRetrieval.forwardk   sC  € ð Ð#Ø'Ÿ?š?°´˜?Ñ<Ô<ˆLØ%ŸzšzÐ*@À$ÑGÔGÐØÐ'Ø#'¤;Ô#CÐ à�T”Xð 
ØØ)Ø%Ø!%ð	
ð 
ð
 ð
ð 
ˆ
ð 9MÐV˜JÔ4Ð4ÐRVÐØDPÐD\ *Ô"@Ð"@ÐbfÐà'¨œ]ÐØÔ.Ô5Ô;ˆ
Ø×.Ò.Ð/A×/DÒ/DÀZÑ/PÔ/PÑQÔQˆ
ð   *§/¢/°bÀ$ /Ñ"GÔ"GÑGˆ
àÐ%Ø# n×&>Ò&>¸rÑ&BÔ&BÑBˆJå(Ø!Ø&Ô6Ø+Ø!Ô,Ø 7ð
ñ 
ô 
ð 	
r   )NNN)r   r    r!   r#   r   r?   r   r   r)   Ú
LongTensorr7   r8   r	   r   r/   r^   r+   r,   s   @r   r<   r<   K   sÔ   ø€ € € € € ð Ðð˜}ð ð ð ð ð ð ð Øð .2Ø15Ø.2ð	'
ð '
àÔ# dÑ*ð'
ð Ô'¨$Ñ.ð'
ð œ tÑ+ð	'
ð
 Ð+Ô,ð'
ð 
#ð'
ð '
ð '
ñ „^ñ Ôð'
ð '
ð '
ð '
ð '
r   r<   )r6   Údataclassesr   r)   r   Útransformersr   Úcache_utilsr   Úmodeling_utilsr   Úprocessing_utilsr	   Úutilsr
   r   r   r   Úconfiguration_colpalir   r   r/   r<   Ú__all__r:   r   r   ú<module>rh      s­  ðð Ð à !Ð !Ð !Ð !Ð !Ð !à €€€Ø Ð Ð Ð Ð Ð à "Ð "Ð "Ð "Ð "Ð "à  Ð  Ð  Ð  Ð  Ð  Ø -Ð -Ð -Ð -Ð -Ð -Ø &Ð &Ð &Ð &Ð &Ð &Ø VÐ VÐ VÐ VÐ VÐ VÐ VÐ VÐ VÐ VÐ VÐ VØ 0Ð 0Ð 0Ð 0Ð 0Ð 0ð ð&ð &ð &ð &ð &˜_ñ &ô &ñ „ð&ð €ððñ ô ð
 ð9ð 9ð 9ð 9ð 9 ñ 9ô 9ñ „ñô ð9ð0 €ððñ ô ð;
ð ;
ð ;
ð ;
ð ;
Ð0ñ ;
ô ;
ñô ð;
ð~ Øð€€€r   