
    sgQ$                         d Z ddlZddlmZ ddlZddlmc mZ ddlmZ ddl	m
Z
 ddlmZmZ dd	lmZ d
dlmZ  ej"                  e      Z G d de
      ZdZ ede       G d de             Zy)z
RetriBERT model
    N)Optional)nn   )PreTrainedModel)add_start_docstringslogging   )	BertModel   )RetriBertConfigc                   "    e Zd ZdZeZdZdZd Zy)RetriBertPreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    N	retribertc                    t        |t        j                        rm|j                  j                  j                  d| j                  j                         |j                  %|j                  j                  j                          yyt        |t        j                        rz|j                  j                  j                  d| j                  j                         |j                  2|j                  j                  |j                     j                          yyt        |t        j                        rJ|j                  j                  j                          |j                  j                  j                  d       yy)zInitialize the weightsg        )meanstdNg      ?)
isinstancer   Linearweightdatanormal_configinitializer_rangebiaszero_	Embeddingpadding_idx	LayerNormfill_)selfmodules     n/var/www/html/venv/lib/python3.12/site-packages/transformers/models/deprecated/retribert/modeling_retribert.py_init_weightsz&RetriBertPreTrainedModel._init_weights.   s   fbii(MM&&CT[[5R5R&S{{&  &&( '-MM&&CT[[5R5R&S!!-""6#5#56<<> .-KK""$MM$$S) .    )	__name__
__module____qualname____doc__r   config_classload_tf_weightsbase_model_prefixr#    r$   r"   r   r   $   s    
 #LO#*r$   r   aC  

    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

    This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
    Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
    and behavior.

    Parameters:
        config ([`RetriBertConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
zEBert Based model to embed queries or document for document retrieval.c                        e Zd Zdeddf fdZ	 ddZ	 	 ddZ	 	 ddZ	 ddej                  d	e
ej                     d
ej                  de
ej                     dedej                  fdZ xZS )RetriBertModelr   returnNc                    t         |   |       |j                  | _        t        |      | _        |j
                  rd n
t        |      | _        t        j                  |j                        | _
        t        j                  |j                  |j                  d      | _        t        j                  |j                  |j                  d      | _        t        j                  d      | _        | j#                          y )NF)r   r   )	reduction)super__init__projection_dimr
   
bert_queryshare_encodersbert_docr   Dropouthidden_dropout_probdropoutr   hidden_sizeproject_queryproject_docCrossEntropyLossce_loss	post_init)r    r   	__class__s     r"   r3   zRetriBertModel.__init__S   s     $33#F+ & 5 59V;Lzz&"<"<=YYv'9'96;P;PW\]99V%7%79N9NUZ[**V< 	r$   c                    |dk  s|j                   d   |k  r ||      d   S |j                  }|j                         }t        j                  |t        j
                  |      }d gj                  j                  z  j                  ||      }fd}	j                  |d |d       }
g }t        t        j                  |j                   d   |z              D ]F  }|
||z  |dz   |z   }|||z  |dz   |z   }t        j                  |	||      }|j                  |       H t        j                  |d      S )Nr   )attention_maskr   )dtypedevicec                  h    j                  | d   | d         }|d   }j                  |      }|S )Nr   r   )rC   	head_mask)encoderpooler)inputsencoder_outputssequence_outputpooled_outputrG   sent_encoders       r"   partial_encodezCRetriBertModel.embed_sentences_checkpointed.<locals>.partial_encodew   sK    "."6"61I#)!9' #7 #
 #2!"4 , 3 3O D$$r$   )	input_idsposition_idstoken_type_idsinputs_embeds)dim)shaperE   sizetorchzeroslongr   num_hidden_layersget_extended_attention_mask
embeddingsrangemathceil
checkpointappendcat)r    rP   rC   rN   checkpoint_batch_sizerE   input_shaperR   extended_attention_maskrO   embedding_outputpooled_output_listbb_embedding_outputb_attention_maskrM   rG   s      `            @r"   embed_sentences_checkpointedz+RetriBertModel.embed_sentences_checkpointedb   s`    !1$	(:=R(R	.I!LL %%F#..*K"[[EJJvVN!4!4!F!FFI4@4\4\5#
%  ,66#$~ei  7   "$499Y__Q%7:O%OPQ 9%5a:O6OSTWXSX\qRq%r"#:1?T;TXY\]X]avWv#w  * 5 5nFXZj k"))-8	9
 99/Q77r$   c                 `    | j                  ||| j                  |      }| j                  |      S N)rk   r5   r<   )r    rP   rC   rc   q_repss        r"   embed_questionszRetriBertModel.embed_questions   s7     22OO!	
 !!&))r$   c                     | j                  ||| j                  | j                  n| j                  |      }| j                  |      S rm   )rk   r7   r5   r=   )r    rP   rC   rc   a_repss        r"   embed_answerszRetriBertModel.embed_answers   sF     22#}}4DOO$--!	
 ''r$   input_ids_queryattention_mask_queryinput_ids_docattention_mask_docrc   c                    |j                   }| j                  |||      }| j                  |||      }t        j                  ||j                               }	| j                  |	t        j                  |	j                  d         j                  |            }
| j                  |	j                         t        j                  |	j                  d         j                  |            }|
|z   dz  }|S )a  
        Args:
            input_ids_query (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
                Indices of input sequence tokens in the vocabulary for the queries in a batch.

                Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
                [`PreTrainedTokenizer.__call__`] for details.

                [What are input IDs?](../glossary#input-ids)
            attention_mask_query (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*):
                Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

                - 1 for tokens that are **not masked**,
                - 0 for tokens that are **masked**.

                [What are attention masks?](../glossary#attention-mask)
            input_ids_doc (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
                Indices of input sequence tokens in the vocabulary for the documents in a batch.
            attention_mask_doc (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*):
                Mask to avoid performing attention on documents padding token indices.
            checkpoint_batch_size (`int`, *optional*, defaults to `-1`):
                If greater than 0, uses gradient checkpointing to only compute sequence representation on
                `checkpoint_batch_size` examples at a time on the GPU. All query representations are still compared to
                all document representations in the batch.

        Return:
            `torch.FloatTensor``: The bidirectional cross-entropy loss obtained while trying to match each query to its
            corresponding document and each document to its corresponding query in the batch
        r   r      )
rE   ro   rr   rW   mmtr?   arangerU   to)r    rs   rt   ru   rv   rc   rE   rn   rq   compare_scoresloss_qaloss_aqlosss                r"   forwardzRetriBertModel.forward   s    J !''%%o7KMbc##M3EG\]&&((*5,,~u||N<P<PQR<S/T/W/WX^/_`,,~//15<<@T@TUV@W3X3[3[\b3cd'!Q&r$   ))Nr   )r%   r&   r'   r   r3   rk   ro   rr   rW   
LongTensorr   FloatTensorintr   __classcell__)rA   s   @r"   r.   r.   N   s    
 4 ( !*8^  	*"  	(( &(,)), 'u'8'89, ''	,
 %U%6%67,  #, 
		,r$   r.   )r(   r^   typingr   rW   torch.utils.checkpointutilsr`   r   modeling_utilsr   r   r   bert.modeling_bertr
   configuration_retribertr   
get_loggerr%   loggerr   RETRIBERT_START_DOCSTRINGr.   r,   r$   r"   <module>r      s|       + +  . 3 + 4 
		H	%* *2 " OD- D	Dr$   