§
    ‚ŠtjÇ!  ã                   óF  — 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¦   «         rd d
l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 )é    )Ú	dataclass)Únn)Ú	AutoModelé   )ÚCache)ÚPreTrainedModel)ÚModelOutputÚauto_docstringÚcan_return_tupleÚis_torch_availableé   )ÚColQwen2ConfigNc                   ó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 )ÚColQwen2PreTrainedModelÚconfigÚmodel)ÚimageÚtextTc                 óJ   •— t          ¦   «                              |¦  «         d S ©N)ÚsuperÚ_init_weights)ÚselfÚmoduleÚ	__class__s     €úl/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/colqwen2/modeling_colqwen2.pyr   z%ColQwen2PreTrainedModel._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   z4
    Base class for ColQwen2 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S )ÚColQwen2ForRetrievalOutputaÝ  
    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.
    NÚlossÚ
embeddingsÚpast_key_valuesÚhidden_statesÚ
attentions)r   r   r    Ú__doc__r/   r(   ÚFloatTensorr!   r0   ÚTensorr1   r   r2   Útupler3   © r   r   r.   r.   4   s›   € € € € € € ð
ð 
ð &*€Dˆ%Ô
˜dÑ
"Ð)Ð)Ñ)Ø&*€J�”˜tÑ#Ð*Ð*Ñ*Ø$(€O�U˜T‘\Ð(Ð(Ñ(Ø59€M�5˜Ô*Ô+¨dÑ2Ð9Ð9Ñ9Ø26€J��eÔ'Ô(¨4Ñ/Ð6Ð6Ñ6Ð6Ð6r   r.   uG  
    Following the ColPali approach, ColQwen2 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 ColQwen2 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.

    ColQwen2 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dz  d	ej	        dz  d
ej        dz  dedz  dedz  dedz  dedz  dej
        dz  dej	        dz  defd„¦   «         ¦   «         Zˆ xZS )ÚColQwen2ForRetrievalÚ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ColQwen2ForRetrieval.__init___   s”   ø€ Ý‰Œ×Ò˜Ñ Ô Ð ØˆŒØ Ô+Ô7ÔBˆŒåÔ(¨Ô):Ñ;Ô;ˆŒà!œ[Ô6ˆÔÝ$&¤IØŒKÔ"Ô.Ô:ØÔñ%
ô %
ˆÔ!ð
 	�ŠÑÔÐÐÐr   NÚ	input_idsÚattention_maskÚposition_idsr1   ÚlabelsÚinputs_embedsÚ	use_cacheÚoutput_attentionsÚoutput_hidden_statesÚreturn_dictÚpixel_valuesÚimage_grid_thwÚreturnc                 ó  — |�u|�s|dd…df         |dd…df         z  }t          j        |j        d         |j        ¬¦  «        }|                     d¦  «        |                     d¦  «        k     }||         }|�|n| j        j        }|	�|	n| j        j        }	|
�|
n| j        j        }
|€¤ | j	         
                    ¦   «         |¦  «        }|�€| j	                             ||d¬¦  «        j        }|| j        j        j        k                         d¦  «        }|                     |j        |j        ¦  «        }|                     ||¦  «        }|  	                    d|||||||	|
¬	¦	  «	        }|	r|j        nd}|d         }| j        j        j        }|                      |                     |¦  «        ¦  «        }||                     dd¬
¦  «        z  }|�||                     d¦  «        z  }t-          ||j        ||j        ¬¦  «        S )z¯
        image_grid_thw (`torch.LongTensor` of shape `(num_images, 3)`, *optional*):
            The temporal, height and width of feature shape of each image in LLM.
        Nr   é   )Údevicer   T)Úgrid_thwrO   éÿÿÿÿ)	rG   rI   rH   r1   rK   rL   rM   rN   rO   )ÚdimÚkeepdim)r0   r1   r2   r3   )r(   ÚarangeÚshaperU   Ú	unsqueezer   rM   rN   rO   r;   Úget_input_embeddingsÚvisualÚpooler_outputr>   Úimage_token_idÚtoÚdtypeÚmasked_scatterr2   rE   ÚweightÚnormr.   r1   r3   )r   rG   rH   rI   r1   rJ   rK   rL   rM   rN   rO   rP   rQ   ÚkwargsÚoffsetsrZ   ÚmaskÚimage_embedsÚ
image_maskÚ
vlm_outputÚvlm_hidden_statesÚlast_hidden_statesÚ
proj_dtyper0   s                           r   ÚforwardzColQwen2ForRetrieval.forwardn   sF  € ð. Ð#¨Ð(Bà$ Q Q Q¨ TÔ*¨^¸A¸A¸A¸q¸DÔ-AÑAˆGÝ”\ ,Ô"4°QÔ"7ÀÄÐOÑOÔOˆFØ×#Ò# AÑ&Ô&¨×):Ò):¸1Ñ)=Ô)=Ò=ˆDØ'¨Ô-ˆLà1BÐ1NÐ-Ð-ÐTXÔT_ÔTqÐð %9Ð$DÐ Ð È$Ì+ÔJjð 	ð &1Ð%<�k�kÀ$Ä+ÔBYˆð Ð Ø;˜DœH×9Ò9Ñ;Ô;¸IÑFÔFˆMàÐ'Ø#œxŸš¨|ÀnÐbf˜ÑgÔgÔu�Ø'¨4¬;Ô+AÔ+PÒP×[Ò[Ð\^Ñ_Ô_�
Ø+Ÿš¨}Ô/CÀ]ÔEXÑYÔY�Ø -× <Ò <¸ZÈÑ VÔ V�à—X’XØØ%Ø)Ø+Ø'ØØ/Ø!5Ø#ð ñ 

ô 

ˆ
ð 9MÐV˜JÔ4Ð4ÐRVÐà'¨œ]ÐØÔ.Ô5Ô;ˆ
Ø×.Ò.Ð/A×/DÒ/DÀZÑ/PÔ/PÑQÔQˆ
ð   *§/¢/°bÀ$ /Ñ"GÔ"GÑGˆ
ØÐ%Ø# n×&>Ò&>¸rÑ&BÔ&BÑBˆJå)Ø!Ø&Ô6Ø+Ø!Ô,ð	
ñ 
ô 
ð 	
r   )NNNNNNNNNNNN)r   r   r    r"   r   r=   r   r
   r(   Ú
LongTensorr6   r   r5   Úboolr.   ro   r*   r+   s   @r   r:   r:   N   s‚  ø€ € € € € ð Ðð˜~ð ð ð ð ð ð ð Øð .2Ø.2Ø04Ø(,Ø*.Ø26Ø!%Ø)-Ø,0Ø#'Ø,0Ø26ðI
ð I
àÔ# dÑ*ðI
ð œ tÑ+ðI
ð Ô&¨Ñ-ð	I
ð
  ™ðI
ð Ô  4Ñ'ðI
ð Ô(¨4Ñ/ðI
ð ˜$‘;ðI
ð   $™;ðI
ð # T™kðI
ð ˜D‘[ðI
ð ”l TÑ)ðI
ð Ô(¨4Ñ/ðI
ð 
$ðI
ð I
ð I
ñ „^ñ ÔðI
ð I
ð I
ð I
ð I
r   r:   )Údataclassesr   r(   r   Útransformersr   Úcache_utilsr   Úmodeling_utilsr   Úutilsr	   r
   r   r   Úconfiguration_colqwen2r   r   r.   r:   Ú__all__r8   r   r   ú<module>ry      s®  ðð* "Ð !Ð !Ð !Ð !Ð !à Ð Ð Ð Ð Ð à "Ð "Ð "Ð "Ð "Ð "à  Ð  Ð  Ð  Ð  Ð  Ø -Ð -Ð -Ð -Ð -Ð -Ø VÐ VÐ VÐ VÐ VÐ VÐ VÐ VÐ VÐ VÐ VÐ VØ 2Ð 2Ð 2Ð 2Ð 2Ð 2ð ÐÑÔð Ø€L€L€Lð ð&ð &ð &ð &ð &˜oñ &ô &ñ „ð&ð €ððñ ô ð
 ð7ð 7ð 7ð 7ð 7 ñ 7ô 7ñ „ñô ð7ð( €ððñ ô ð]
ð ]
ð ]
ð ]
ð ]
Ð2ñ ]
ô ]
ñô ð]
ð@ "Ð#<Ð
=€€€r   