§
    ‚Štjú  ã                   ó:  — 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	m
Z
mZ ddlmZ d	d
l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 )é    )Ú	dataclassN)Únné   )ÚPreTrainedModel)ÚUnpack)ÚModelOutputÚTransformersKwargsÚauto_docstring)Úcan_return_tupleé   )Ú	AutoModelé   )ÚColModernVBertConfigc                   ó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 )ÚColModernVBertPreTrainedModelÚconfigÚmodel)ÚimageÚtextTc                 óJ   •— t          ¦   «                              |¦  «         d S ©N)ÚsuperÚ_init_weights)ÚselfÚmoduleÚ	__class__s     €úx/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/colmodernvbert/modeling_colmodernvbert.pyr   z+ColModernVBertPreTrainedModel._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   z:
    Base class for ColModernVBert 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ej                 dz  ed<   dZeej                 dz  ed<   dZeej                 dz  ed<   dS )Ú ColModernVBertForRetrievalOutputa  
    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.
    image_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True` and `pixel_values` are provided):
        Tuple of `torch.FloatTensor` (one for the output of the image modality projection + one for the output of each layer) of shape
        `(batch_size, num_channels, image_size, image_size)`.
        Hidden-states of the image encoder at the output of each layer plus the initial modality projection outputs.
    NÚlossÚ
embeddingsÚhidden_statesÚimage_hidden_statesÚ
attentions)r   r    r!   Ú__doc__r0   r)   ÚFloatTensorr"   r1   ÚTensorr2   Útupler3   r4   © r   r   r/   r/   1   s¥   € € € € € € ð	ð 	ð &*€Dˆ%Ô
˜dÑ
"Ð)Ð)Ñ)Ø&*€J�”˜tÑ#Ð*Ð*Ñ*Ø59€M�5˜Ô*Ô+¨dÑ2Ð9Ð9Ñ9Ø;?Ð˜˜uÔ0Ô1°DÑ8Ð?Ð?Ñ?Ø26€J��eÔ'Ô(¨4Ñ/Ð6Ð6Ñ6Ð6Ð6r   r/   u  
    Following the ColPali approach, ColModernVBert 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 ColModernVBert 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, ...) of a document.

    ColModernVBert is trained on top of ModernVBert, and was introduced in the following paper:
    [*ModernVBERT: Towards Smaller Visual Document Retrievers*](https://arxiv.org/abs/2510.01149).

    ColModernVBert is part of the ColVision model family, which was introduced with ColPali 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 )ÚColModernVBertForRetrievalÚ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#ColModernVBertForRetrieval.__init__^   s”   ø€ Ý‰Œ×Ò˜Ñ Ô Ð ØˆŒØ Ô+Ô7ÔBˆŒÝÔ(¨Ô):Ñ;Ô;ˆŒà!œ[Ô6ˆÔÝ$&¤IØŒKÔ"Ô.Ô:ØÔñ%
ô %
ˆÔ!ð
 	�ŠÑÔÐÐÐr   NÚ	input_idsÚpixel_valuesÚattention_maskÚkwargsÚreturnc                 ó”  —  | j         d|||dœ|¤Ž}|d         }| j        j        j        }|                      |                     |¦  «        ¦  «        }||                     dd¬¦  «        z  }|�9|                     |j        |j        ¬¦  «        }||                     d¦  «        z  }t          ||j	        |j
        |j        ¬¦  «        S )	N)rH   rJ   rI   r   éÿÿÿÿT)ÚdimÚkeepdim)ÚdtypeÚdevice)r1   r2   r4   r3   r9   )r<   rF   ÚweightrQ   ÚtoÚnormrR   Ú	unsqueezer/   r2   r4   r3   )	r   rH   rI   rJ   rK   Ú
vlm_outputÚlast_hidden_statesÚ
proj_dtyper1   s	            r   Úforwardz"ColModernVBertForRetrieval.forwardl   sô   € ð �T”Xð 
ØØ)Ø%ð
ð 
ð ð	
ð 
ˆ
ð (¨œ]ÐØÔ.Ô5Ô;ˆ
Ø×.Ò.Ð/A×/DÒ/DÀZÑ/PÔ/PÑQÔQˆ
ð   *§/¢/°bÀ$ /Ñ"GÔ"GÑGˆ
àÐ%Ø+×.Ò.°ZÔ5EÈjÔN_Ð.Ñ`Ô`ˆNØ# n×&>Ò&>¸rÑ&BÔ&BÑBˆJå/Ø!Ø$Ô2Ø!Ô,Ø *Ô >ð	
ñ 
ô 
ð 	
r   )NNN)r   r    r!   r#   r   r>   r   r
   r)   Ú
LongTensorr6   r7   r   r	   r/   rZ   r+   r,   s   @r   r;   r;   J   sÕ   ø€ € € € € ð$ ÐðÐ3ð ð ð ð ð ð ð Øð .2Ø15Ø.2ð	
ð 
àÔ# dÑ*ð
ð Ô'¨$Ñ.ð
ð œ tÑ+ð	
ð
 Ð+Ô,ð
ð 
*ð
ð 
ð 
ñ „^ñ Ôð
ð 
ð 
ð 
ð 
r   r;   )Údataclassesr   r)   r   Úmodeling_utilsr   Úprocessing_utilsr   Úutilsr   r	   r
   Úutils.genericr   Úauto.modeling_autor   Úconfiguration_colmodernvbertr   r   r/   r;   Ú__all__r9   r   r   ú<module>rd      sŸ  ðð* "Ð !Ð !Ð !Ð !Ð !à €€€Ø Ð Ð Ð Ð Ð à -Ð -Ð -Ð -Ð -Ð -Ø &Ð &Ð &Ð &Ð &Ð &Ø DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DØ -Ð -Ð -Ð -Ð -Ð -Ø *Ð *Ð *Ð *Ð *Ð *Ø >Ð >Ð >Ð >Ð >Ð >ð ð&ð &ð &ð &ð & Oñ &ô &ñ „ð&ð €ððñ ô ð
 ð7ð 7ð 7ð 7ð 7 {ñ 7ô 7ñ „ñô ð7ð& €ððñ ô ð"1
ð 1
ð 1
ð 1
ð 1
Ð!>ñ 1
ô 1
ñ#ô ð"1
ðh (Ð)HÐ
I€€€r   