o
    i*Y                     @   s  d Z ddlmZ ddlmZmZ ddlZddlmZ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 eeZeeddG dd deZeeddG dd deZeeddG dd deZeG dd deZG dd deZG dd deZG dd deZG dd deZG d d! d!eZ ed"dG d#d$ d$eZ!ed%dG d&d' d'eZ"ed(dG d)d* d*e Z#g d+Z$dS ),z5PyTorch DPR model for Open Domain Question Answering.    )	dataclass)OptionalUnionN)Tensornn   )BaseModelOutputWithPooling)PreTrainedModel)ModelOutputauto_docstringlogging   )	BertModel   )	DPRConfigz6
    Class for outputs of [`DPRQuestionEncoder`].
    )custom_introc                   @   P   e Zd ZU dZejed< dZee	ejdf  ed< dZ
ee	ejdf  ed< dS )DPRContextEncoderOutputa  
    pooler_output (`torch.FloatTensor` of shape `(batch_size, embeddings_size)`):
        The DPR encoder outputs the *pooler_output* that corresponds to the context representation. Last layer
        hidden-state of the first token of the sequence (classification token) further processed by a Linear layer.
        This output is to be used to embed contexts for nearest neighbors queries with questions embeddings.
    pooler_outputN.hidden_states
attentions__name__
__module____qualname____doc__torchFloatTensor__annotations__r   r   tupler    r    r    a/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/transformers/models/dpr/modeling_dpr.pyr   *   
   
 
r   c                   @   r   )DPRQuestionEncoderOutputa  
    pooler_output (`torch.FloatTensor` of shape `(batch_size, embeddings_size)`):
        The DPR encoder outputs the *pooler_output* that corresponds to the question representation. Last layer
        hidden-state of the first token of the sequence (classification token) further processed by a Linear layer.
        This output is to be used to embed questions for nearest neighbors queries with context embeddings.
    r   N.r   r   r   r    r    r    r!   r#   =   r"   r#   c                   @   st   e Zd ZU dZejed< dZeej ed< dZ	eej ed< dZ
eeejdf  ed< dZeeejdf  ed< dS )	DPRReaderOutputa  
    start_logits (`torch.FloatTensor` of shape `(n_passages, sequence_length)`):
        Logits of the start index of the span for each passage.
    end_logits (`torch.FloatTensor` of shape `(n_passages, sequence_length)`):
        Logits of the end index of the span for each passage.
    relevance_logits (`torch.FloatTensor` of shape `(n_passages, )`):
        Outputs of the QA classifier of the DPRReader that corresponds to the scores of each passage to answer the
        question, compared to all the other passages.
    start_logitsN
end_logitsrelevance_logits.r   r   )r   r   r   r   r   r   r   r&   r   r'   r   r   r   r    r    r    r!   r$   P   s   
 

r$   c                   @   s   e Zd ZdZdd ZdS )DPRPreTrainedModelTc                 C   s   t |tjr |jjjd| jjd |jdur|jj	  dS dS t |tj
rC|jjjd| jjd |jdurA|jj|j 	  dS dS t |tjrX|jj	  |jjd dS dS )zInitialize the weightsg        )meanstdNg      ?)
isinstancer   Linearweightdatanormal_configinitializer_rangebiaszero_	Embeddingpadding_idx	LayerNormfill_)selfmoduler    r    r!   _init_weightsl   s   

z DPRPreTrainedModel._init_weightsN)r   r   r   _supports_sdpar:   r    r    r    r!   r(   h   s    r(   c                       s   e Zd ZdZdef fddZ						ddedee d	ee d
ee dededede	e
eedf f fddZedefddZ  ZS )
DPREncoder
bert_modelr0   c                    sd   t  | t|dd| _| jjjdkrtd|j| _| jdkr,t	| jjj|j| _
|   d S )NF)add_pooling_layerr   z!Encoder hidden_size can't be zero)super__init__r   r=   r0   hidden_size
ValueErrorprojection_dimr   r,   encode_proj	post_initr8   r0   	__class__r    r!   r@      s   
zDPREncoder.__init__NF	input_idsattention_masktoken_type_idsinputs_embedsoutput_attentionsoutput_hidden_statesreturn_dictreturn.c              	   C   sv   | j |||||||d}|d }	|	d d dd d f }
| jdkr%| |
}
|s1|	|
f|dd   S t|	|
|j|jdS )NrI   rJ   rK   rL   rM   rN   rO   r   r   )last_hidden_stater   r   r   )r=   rC   rD   r   r   r   )r8   rI   rJ   rK   rL   rM   rN   rO   outputssequence_outputpooled_outputr    r    r!   forward   s*   
	

zDPREncoder.forwardc                 C   s   | j dkr	| jjS | jjjS )Nr   )rC   rD   out_featuresr=   r0   rA   )r8   r    r    r!   embeddings_size   s   

zDPREncoder.embeddings_size)NNNFFF)r   r   r   base_model_prefixr   r@   r   r   boolr   r   r   rV   propertyintrX   __classcell__r    r    rG   r!   r<   }   s8    	
#r<   c                       sj   e Zd ZdZdef fddZ				ddeded	ee d
ededede	e
eedf f fddZ  ZS )DPRSpanPredictorencoderr0   c                    sF   t  | t|| _t| jjd| _t| jjd| _| 	  d S )Nr   r   )
r?   r@   r<   r_   r   r,   rX   
qa_outputsqa_classifierrE   rF   rG   r    r!   r@      s
   
zDPRSpanPredictor.__init__NFrI   rJ   rL   rM   rN   rO   rP   .c                 C   s   |d ur|  n|  d d \}}| j||||||d}	|	d }
| |
}|jddd\}}|d }|d }| |
d d dd d f }|||}|||}||}|si|||f|	dd   S t||||	j	|	j
dS )Nr   )rJ   rL   rM   rN   rO   r   r   )dim)r%   r&   r'   r   r   )sizer_   r`   splitsqueeze
contiguousra   viewr$   r   r   )r8   rI   rJ   rL   rM   rN   rO   
n_passagessequence_lengthrS   rT   logitsr%   r&   r'   r    r    r!   rV      s6   $


zDPRSpanPredictor.forward)NFFF)r   r   r   rY   r   r@   r   r   rZ   r   r$   r   rV   r]   r    r    rG   r!   r^      s,    r^   c                   @   "   e Zd ZU dZeed< dZdZdS )DPRPretrainedContextEncoder
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    r0   Nctx_encoderr   r   r   r   r   r   load_tf_weightsrY   r    r    r    r!   rm      
   
 rm   c                   @   rl   )DPRPretrainedQuestionEncoderrn   r0   Nquestion_encoderrp   r    r    r    r!   rs      rr   rs   c                   @   rl   )DPRPretrainedReaderrn   r0   Nspan_predictorrp   r    r    r    r!   ru   	  rr   ru   zf
    The bare DPRContextEncoder transformer outputting pooler outputs as context representations.
    c                          e Zd Zdef fddZe							ddee dee dee dee d	ee d
ee dee de	e
eedf f fddZ  ZS )DPRContextEncoderr0   c                    (   t  | || _t|| _|   d S N)r?   r@   r0   r<   ro   rE   rF   rG   r    r!   r@        
zDPRContextEncoder.__init__NrI   rJ   rK   rL   rM   rN   rO   rP   .c              	   C   s  |dur|n| j j}|dur|n| j j}|dur|n| j j}|dur*|dur*td|dur3| }n|dur@| dd }ntd|durK|jn|j}	|du rc|du r]tj||	dn|| j j	k}|du rptj
|tj|	d}| j|||||||d}
|s|
dd S t|
j|
j|
jd	S )
aS  
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. To match pretraining, DPR input sequence should be
            formatted with [CLS] and [SEP] tokens as follows:

            (a) For sequence pairs (for a pair title+text for example):

            ```
            tokens:         [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
            token_type_ids:   0   0  0    0    0     0       0   0   1  1  1  1   1   1
            ```

            (b) For single sequences (for a question for example):

            ```
            tokens:         [CLS] the dog is hairy . [SEP]
            token_type_ids:   0   0   0   0  0     0   0
            ```

            DPR is a model with absolute position embeddings so it's usually advised to pad the inputs on the right
            rather than the left.

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

            [What are input IDs?](../glossary#input-ids)

        Examples:

        ```python
        >>> from transformers import DPRContextEncoder, DPRContextEncoderTokenizer

        >>> tokenizer = DPRContextEncoderTokenizer.from_pretrained("facebook/dpr-ctx_encoder-single-nq-base")
        >>> model = DPRContextEncoder.from_pretrained("facebook/dpr-ctx_encoder-single-nq-base")
        >>> input_ids = tokenizer("Hello, is my dog cute ?", return_tensors="pt")["input_ids"]
        >>> embeddings = model(input_ids).pooler_output
        ```NDYou cannot specify both input_ids and inputs_embeds at the same timerb   5You have to specify either input_ids or inputs_embedsdevicedtyper   rQ   r   r   r   r   )r0   rM   rN   use_return_dictrB   rd   r   r   onespad_token_idzeroslongro   r   r   r   r   r8   rI   rJ   rK   rL   rM   rN   rO   input_shaper   rS   r    r    r!   rV   &  sB   1


zDPRContextEncoder.forwardNNNNNNN)r   r   r   r   r@   r   r   r   rZ   r   r   r   rV   r]   r    r    rG   r!   rx     6    	rx   zh
    The bare DPRQuestionEncoder transformer outputting pooler outputs as question representations.
    c                       rw   )DPRQuestionEncoderr0   c                    ry   rz   )r?   r@   r0   r<   rt   rE   rF   rG   r    r!   r@     r{   zDPRQuestionEncoder.__init__NrI   rJ   rK   rL   rM   rN   rO   rP   .c              	   C   s(  |dur|n| j j}|dur|n| j j}|dur|n| j j}|dur*|dur*td|dur9| || | }n|durF| dd }ntd|durQ|jn|j}	|du ri|du rctj	||	dn|| j j
k}|du rvtj|tj|	d}| j|||||||d}
|s|
dd S t|
j|
j|
jd	S )
aj  
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. To match pretraining, DPR input sequence should be
            formatted with [CLS] and [SEP] tokens as follows:

            (a) For sequence pairs (for a pair title+text for example):

            ```
            tokens:         [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
            token_type_ids:   0   0  0    0    0     0       0   0   1  1  1  1   1   1
            ```

            (b) For single sequences (for a question for example):

            ```
            tokens:         [CLS] the dog is hairy . [SEP]
            token_type_ids:   0   0   0   0  0     0   0
            ```

            DPR is a model with absolute position embeddings so it's usually advised to pad the inputs on the right
            rather than the left.

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

            [What are input IDs?](../glossary#input-ids)

        Examples:

        ```python
        >>> from transformers import DPRQuestionEncoder, DPRQuestionEncoderTokenizer

        >>> tokenizer = DPRQuestionEncoderTokenizer.from_pretrained("facebook/dpr-question_encoder-single-nq-base")
        >>> model = DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base")
        >>> input_ids = tokenizer("Hello, is my dog cute ?", return_tensors="pt")["input_ids"]
        >>> embeddings = model(input_ids).pooler_output
        ```
        Nr|   rb   r}   r~   r   rQ   r   r   )r0   rM   rN   r   rB   %warn_if_padding_and_no_attention_maskrd   r   r   r   r   r   r   rt   r#   r   r   r   r   r    r    r!   rV     sD   1


zDPRQuestionEncoder.forwardr   )r   r   r   r   r@   r   r   r   rZ   r   r#   r   rV   r]   r    r    rG   r!   r     r   r   zE
    The bare DPRReader transformer outputting span predictions.
    c                       s   e Zd Zdef fddZe						ddee dee dee dee d	ee d
ee de	e
eedf f fddZ  ZS )	DPRReaderr0   c                    ry   rz   )r?   r@   r0   r^   rv   rE   rF   rG   r    r!   r@     r{   zDPRReader.__init__NrI   rJ   rL   rM   rN   rO   rP   .c           	      C   s   |dur|n| j j}|dur|n| j j}|dur|n| j j}|dur*|dur*td|dur9| || | }n|durF| dd }ntd|durQ|jn|j}|du r_tj	||d}| j
||||||dS )a  
        input_ids (`tuple[torch.LongTensor]` of shapes `(n_passages, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. It has to be a sequence triplet with 1) the question
            and 2) the passages titles and 3) the passages texts To match pretraining, DPR `input_ids` sequence should
            be formatted with [CLS] and [SEP] with the format:

            `[CLS] <question token ids> [SEP] <titles ids> [SEP] <texts ids>`

            DPR is a model with absolute position embeddings so it's usually advised to pad the inputs on the right
            rather than the left.

            Indices can be obtained using [`DPRReaderTokenizer`]. See this class documentation for more details.

            [What are input IDs?](../glossary#input-ids)
        inputs_embeds (`torch.FloatTensor` of shape `(n_passages, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.

        Examples:

        ```python
        >>> from transformers import DPRReader, DPRReaderTokenizer

        >>> tokenizer = DPRReaderTokenizer.from_pretrained("facebook/dpr-reader-single-nq-base")
        >>> model = DPRReader.from_pretrained("facebook/dpr-reader-single-nq-base")
        >>> encoded_inputs = tokenizer(
        ...     questions=["What is love ?"],
        ...     titles=["Haddaway"],
        ...     texts=["'What Is Love' is a song recorded by the artist Haddaway"],
        ...     return_tensors="pt",
        ... )
        >>> outputs = model(**encoded_inputs)
        >>> start_logits = outputs.start_logits
        >>> end_logits = outputs.end_logits
        >>> relevance_logits = outputs.relevance_logits
        ```
        Nr|   rb   r}   r~   )rL   rM   rN   rO   )r0   rM   rN   r   rB   r   rd   r   r   r   rv   )	r8   rI   rJ   rL   rM   rN   rO   r   r   r    r    r!   rV     s.   0
zDPRReader.forward)NNNNNN)r   r   r   r   r@   r   r   r   rZ   r   r$   r   rV   r]   r    r    rG   r!   r     s0    r   )rx   rm   r(   rs   ru   r   r   )%r   dataclassesr   typingr   r   r   r   r   modeling_outputsr   modeling_utilsr	   utilsr
   r   r   bert.modeling_bertr   configuration_dprr   
get_loggerr   loggerr   r#   r$   r(   r<   r^   rm   rs   ru   rx   r   r   __all__r    r    r    r!   <module>   s\   
8>deW