o
    Zh`c                     @   s  d Z ddlmZ ddlmZ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G dd deZeG dd deZe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TupleUnionN)Tensornn   )BaseModelOutputWithPooling)PreTrainedModel)ModelOutputauto_docstringlogging   )	BertModel   )	DPRConfigc                   @   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 )DPRContextEncoderOutputac  
    Class for outputs of [`DPRQuestionEncoder`].

    Args:
        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.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) of
            shape `(batch_size, sequence_length, hidden_size)`.

            Hidden-states of the model at the output of each layer plus the initial embedding outputs.
        attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`.

            Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
            heads.
    pooler_outputN.hidden_states
attentions__name__
__module____qualname____doc__torchFloatTensor__annotations__r   r   r   r    r   r   S/var/www/auris/lib/python3.10/site-packages/transformers/models/dpr/modeling_dpr.pyr   *   
   
 
r   c                   @   r   )DPRQuestionEncoderOutputac  
    Class for outputs of [`DPRQuestionEncoder`].

    Args:
        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.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) of
            shape `(batch_size, sequence_length, hidden_size)`.

            Hidden-states of the model at the output of each layer plus the initial embedding outputs.
        attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`.

            Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
            heads.
    r   N.r   r   r   r   r   r   r    r"   F   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  
    Class for outputs of [`DPRQuestionEncoder`].

    Args:
        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.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) of
            shape `(batch_size, sequence_length, hidden_size)`.

            Hidden-states of the model at the output of each layer plus the initial embedding outputs.
        attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`.

            Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
            heads.
    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#   b   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        )meanZstdNg      ?)
isinstancer   LinearweightdataZnormal_configZinitializer_rangeZbiasZzero_Z	EmbeddingZpadding_idxZ	LayerNormZfill_)selfmoduler   r   r    _init_weights   s   

z DPRPreTrainedModel._init_weightsN)r   r   r   Z_supports_sdpar0   r   r   r   r    r'      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_modelr-   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)Zadd_pooling_layerr   z!Encoder hidden_size can't be zero)super__init__r   r2   r-   hidden_size
ValueErrorprojection_dimr   r*   encode_proj	post_initr.   r-   	__class__r   r    r4      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r=   r>   r?   r@   rA   rB   rC   r   r   )Zlast_hidden_stater   r   r   )r2   r7   r8   r	   r   r   )r.   r=   r>   r?   r@   rA   rB   rC   outputssequence_outputZpooled_outputr   r   r    forward   s*   
	

zDPREncoder.forwardc                 C   s   | j dkr	| jjS | jjjS )Nr   )r7   r8   Zout_featuresr2   r-   r5   )r.   r   r   r    embeddings_size   s   

zDPREncoder.embeddings_size)NNNFFF)r   r   r   base_model_prefixr   r4   r   r   boolr   r	   r   rH   propertyintrI   __classcell__r   r   r;   r    r1      s8    	
#r1   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encoderr-   c                    sF   t  | t|| _t| jjd| _t| jjd| _| 	  d S )Nr   r   )
r3   r4   r1   rP   r   r*   rI   
qa_outputsqa_classifierr9   r:   r;   r   r    r4      s
   
zDPRSpanPredictor.__init__NFr=   r>   r@   rA   rB   rC   rD   .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   )r>   r@   rA   rB   rC   r   r   )dim)r$   r%   r&   r   r   )sizerP   rQ   splitZsqueeze
contiguousrR   viewr#   r   r   )r.   r=   r>   r@   rA   rB   rC   Z
n_passagesZsequence_lengthrF   rG   Zlogitsr$   r%   r&   r   r   r    rH      s6   $


zDPRSpanPredictor.forward)NFFF)r   r   r   rJ   r   r4   r   r   rK   r   r#   r   rH   rN   r   r   r;   r    rO      s,    rO   c                   @      e Zd ZdZeZdZdZdS )DPRPretrainedContextEncoder
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    Nctx_encoderr   r   r   r   r   Zconfig_classZload_tf_weightsrJ   r   r   r   r    rZ     
    rZ   c                   @   rY   )DPRPretrainedQuestionEncoderr[   Nquestion_encoderr]   r   r   r   r    r_     r^   r_   c                   @   rY   )DPRPretrainedReaderr[   Nspan_predictorr]   r   r   r   r    ra   $  r^   ra   zf
    The bare DPRContextEncoder transformer outputting pooler outputs as context representations.
    )Zcustom_introc                          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 )DPRContextEncoderr-   c                    (   t  | || _t|| _|   d S N)r3   r4   r-   r1   r\   r9   r:   r;   r   r    r4   :     
zDPRContextEncoder.__init__Nr=   r>   r?   r@   rA   rB   rC   rD   .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 timerS   5You have to specify either input_ids or inputs_embedsdeviceZdtyperk   rE   r   r   r   r   )r-   rA   rB   use_return_dictr6   rU   rk   r   onespad_token_idzeroslongr\   r   r   r   r   r.   r=   r>   r?   r@   rA   rB   rC   input_shaperk   rF   r   r   r    rH   A  sB   1


zDPRContextEncoder.forwardNNNNNNN)r   r   r   r   r4   r   r   r   rK   r   r   r   rH   rN   r   r   r;   r    rd   4  6    	rd   zh
    The bare DPRQuestionEncoder transformer outputting pooler outputs as question representations.
    c                       rc   )DPRQuestionEncoderr-   c                    re   rf   )r3   r4   r-   r1   r`   r9   r:   r;   r   r    r4     rg   zDPRQuestionEncoder.__init__Nr=   r>   r?   r@   rA   rB   rC   rD   .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
        ```
        Nrh   rS   ri   rj   rl   rE   r   rm   )r-   rA   rB   rn   r6   %warn_if_padding_and_no_attention_maskrU   rk   r   ro   rp   rq   rr   r`   r"   r   r   r   rs   r   r   r    rH     sD   1


zDPRQuestionEncoder.forwardru   )r   r   r   r   r4   r   r   r   rK   r   r"   r   rH   rN   r   r   r;   r    rw     rv   rw   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 )	DPRReaderr-   c                    re   rf   )r3   r4   r-   rO   rb   r9   r:   r;   r   r    r4     rg   zDPRReader.__init__Nr=   r>   r@   rA   rB   rC   rD   .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
        ```
        Nrh   rS   ri   rj   )r@   rA   rB   rC   )r-   rA   rB   rn   r6   rx   rU   rk   r   ro   rb   )	r.   r=   r>   r@   rA   rB   rC   rt   rk   r   r   r    rH     s.   0
zDPRReader.forward)NNNNNN)r   r   r   r   r4   r   r   r   rK   r   r#   r   rH   rN   r   r   r;   r    ry     s0    ry   )rd   rZ   r'   r_   ra   rw   ry   )&r   dataclassesr   typingr   r   r   r   r   r   Zmodeling_outputsr	   Zmodeling_utilsr
   utilsr   r   r   Zbert.modeling_bertr   Zconfiguration_dprr   Z
get_loggerr   loggerr   r"   r#   r'   r1   rO   rZ   r_   ra   rd   rw   ry   __all__r   r   r   r    <module>   sJ   
 8>deW