
    qiXU                     \   d Z ddl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j$                  e      Ze ed       G d de                    Ze ed       G d de                    Ze ed       G d de                    Ze G d de	             Z G d de      Z G d de      Z G d de      Z G d de      Z G d d e      Z ed!       G d" d#e             Z ed$       G d% d&e             Z ed'       G d( d)e             Z g d*Z!y)+z5PyTorch DPR model for Open Domain Question Answering.    )	dataclassN)Tensornn   )BaseModelOutputWithPooling)PreTrainedModel)ModelOutputauto_docstringlogging   )	BertModel   )	DPRConfigz6
    Class for outputs of [`DPRQuestionEncoder`].
    )custom_introc                       e Zd ZU dZej
                  ed<   dZeej
                  df   dz  ed<   dZ	eej
                  df   dz  ed<   y)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   tupler        V/opt/pipecat/venv/lib/python3.12/site-packages/transformers/models/dpr/modeling_dpr.pyr   r   (   S     $$$:>M5**C/047>7;Je'',-4;r    r   c                       e Zd ZU dZej
                  ed<   dZeej
                  df   dz  ed<   dZ	eej
                  df   dz  ed<   y)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    r$   c                       e Zd ZU dZej
                  ed<   dZej
                  dz  ed<   dZej
                  dz  ed<   dZ	e
ej
                  df   dz  ed<   dZe
ej
                  df   dz  ed<   y)	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&   N   s~     ###+/J!!D(/15e''$.5:>M5**C/047>7;Je'',-4;r    r&   c                       e Zd ZdZy)DPRPreTrainedModelTN)r   r   r   _supports_sdpar   r    r!   r+   r+   f   s    Nr    r+   c                        e Zd ZdZdef fdZ	 	 	 	 	 	 ddededz  dedz  dedz  d	ed
ededee	edf   z  fdZ
edefd       Z xZS )
DPREncoder
bert_modelconfigc                    t         |   |       t        |d      | _        | j                  j                  j
                  dk  rt        d      |j                  | _        | j                  dkD  rCt        j                  | j                  j                  j
                  |j                        | _
        | j                          y )NF)add_pooling_layerr   z!Encoder hidden_size can't be zero)super__init__r   r/   r0   hidden_size
ValueErrorprojection_dimr   Linearencode_proj	post_initselfr0   	__class__s     r!   r4   zDPREncoder.__init__n   s     #FeD??!!--2@AA$33"!yy)?)?)K)KVMbMbcDr    N	input_idsattention_masktoken_type_idsinputs_embedsoutput_attentionsoutput_hidden_statesreturn_dictreturn.c           	          | j                  |||||||      }	|	d   }
|
d d dd d f   }| j                  dkD  r| j                  |      }|s
|
|f|	dd  z   S t        |
||	j                  |	j
                        S )Nr>   r?   r@   rA   rB   rC   rD   r   r   )last_hidden_stater   r   r   )r/   r7   r9   r   r   r   )r<   r>   r?   r@   rA   rB   rC   rD   kwargsoutputssequence_outputpooled_outputs               r!   forwardzDPREncoder.forwardy   s     //))'/!5# " 
 "!*'1a0" ,,];M#]3gabkAA)-'!//))	
 	
r    c                     | j                   dkD  r| j                  j                  S | j                  j                  j
                  S )Nr   )r7   r9   out_featuresr/   r0   r5   )r<   s    r!   embeddings_sizezDPREncoder.embeddings_size   s8    "##000%%111r    )NNNFFF)r   r   r   base_model_prefixr   r4   r   boolr   r   rM   propertyintrP   __classcell__r=   s   @r!   r.   r.   k   s    $	y 	 )-(,'+"'%*!"
"
 "
 	"

 }"
  "
 #"
 "
 
$eFCK&8	8"
H 2 2 2r    r.   c                   l     e Zd ZdZdef fdZ	 	 	 	 ddedededz  ded	ed
edee	edf   z  fdZ
 xZS )DPRSpanPredictorencoderr0   c                     t         |   |       t        |      | _        t	        j
                  | j                  j                  d      | _        t	        j
                  | j                  j                  d      | _        | j                          y )Nr   r   )
r3   r4   r.   rY   r   r8   rP   
qa_outputsqa_classifierr:   r;   s     r!   r4   zDPRSpanPredictor.__init__   s_     !&)))DLL$@$@!DYYt||'C'CQGr    Nr>   r?   rA   rB   rC   rD   rE   .c                 V   ||j                         n|j                         d d \  }}	| j                  ||||||      }
|
d   }| j                  |      }|j                  dd      \  }}|j	                  d      j                         }|j	                  d      j                         }| j                  |d d dd d f         }|j                  ||	      }|j                  ||	      }|j                  |      }|s|||f|
dd  z   S t        ||||
j                  |
j                        S )Nr   )r?   rA   rB   rC   rD   r   r   )dim)r'   r(   r)   r   r   )sizerY   r[   splitsqueeze
contiguousr\   viewr&   r   r   )r<   r>   r?   rA   rB   rC   rD   rI   
n_passagessequence_lengthrJ   rK   logitsr'   r(   r)   s                   r!   rM   zDPRSpanPredictor.forward   sN    ;D:Oinn&6UbUgUgUijlklUm#
O,,)'/!5#  
 "!* 1#)<<r<#: j#++B/::<''+668
--oaAg.FG $((_E__ZA
+00< *.>?'!"+MM%!-!//))
 	
r    )NFFF)r   r   r   rQ   r   r4   r   rR   r&   r   rM   rU   rV   s   @r!   rX   rX      s~    !y  (,"'%*!,
,
 ,
 }	,

  ,
 #,
 ,
 
5-	-,
r    rX   c                        e Zd ZU dZeed<   dZy)DPRPretrainedContextEncoder
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    r0   ctx_encoderNr   r   r   r   r   r   rQ   r   r    r!   ri   ri      s    
 %r    ri   c                        e Zd ZU dZeed<   dZy)DPRPretrainedQuestionEncoderrj   r0   question_encoderNrl   r   r    r!   rn   rn      s    
 *r    rn   c                        e Zd ZU dZeed<   dZy)DPRPretrainedReaderrj   r0   span_predictorNrl   r   r    r!   rq   rq      s    
 (r    rq   zf
    The bare DPRContextEncoder transformer outputting pooler outputs as context representations.
    c                        e Zd Zdef fdZe	 	 	 	 	 	 	 ddedz  dedz  dedz  dedz  dedz  d	edz  d
edz  dee	edf   z  fd       Z
 xZS )DPRContextEncoderr0   c                 r    t         |   |       || _        t        |      | _        | j                          y N)r3   r4   r0   r.   rk   r:   r;   s     r!   r4   zDPRContextEncoder.__init__  s.     %f-r    Nr>   r?   r@   rA   rB   rC   rD   rE   .c           	         ||n| j                   j                  }||n| j                   j                  }||n| j                   j                  }||t	        d      ||j                         }	n!||j                         dd }	nt	        d      ||j                  n|j                  }
|2|t        j                  |	|
      n|| j                   j                  k7  }|&t        j                  |	t        j                  |
      }| j                  |||||||      }|s|dd S t        |j                  |j                  |j                   	      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 timer^   5You have to specify either input_ids or inputs_embedsdevicedtyper{   rG   r   r   r   r   )r0   rB   rC   use_return_dictr6   r`   r{   r   onespad_token_idzeroslongrk   r   r   r   r   r<   r>   r?   r@   rA   rB   rC   rD   rI   input_shaper{   rJ   s               r!   rM   zDPRContextEncoder.forward  sp   d 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B] ]%>cdd"#..*K&',,.s3KTUU%.%:!!@T@T! $ 

;v64;;#;#;; 
 !"[[EJJvVN""))'/!5# # 
 12;&!//w?T?Tahasas
 	
r    NNNNNNN)r   r   r   r   r4   r
   r   rR   r   r   rM   rU   rV   s   @r!   rt   rt     s    y   $((,(,'+)-,0#'Y
D=Y
 Y
 	Y

 }Y
  $;Y
 #TkY
 D[Y
 
!5#5	5Y
 Y
r    rt   zh
    The bare DPRQuestionEncoder transformer outputting pooler outputs as question representations.
    c                        e Zd Zdef fdZe	 	 	 	 	 	 	 ddedz  dedz  dedz  dedz  dedz  d	edz  d
edz  dee	edf   z  fd       Z
 xZS )DPRQuestionEncoderr0   c                 r    t         |   |       || _        t        |      | _        | j                          y rv   )r3   r4   r0   r.   ro   r:   r;   s     r!   r4   zDPRQuestionEncoder.__init__v  s.      *6 2r    Nr>   r?   r@   rA   rB   rC   rD   rE   .c           	         ||n| j                   j                  }||n| j                   j                  }||n| j                   j                  }||t	        d      |#| j                  ||       |j                         }	n!||j                         dd }	nt	        d      ||j                  n|j                  }
|2|t        j                  |	|
      n|| j                   j                  k7  }|&t        j                  |	t        j                  |
      }| j                  |||||||      }|s|dd S t        |j                  |j                   |j"                  	      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
        ```
        Nrx   r^   ry   rz   r|   rG   r   r~   )r0   rB   rC   r   r6   %warn_if_padding_and_no_attention_maskr`   r{   r   r   r   r   r   ro   r$   r   r   r   r   s               r!   rM   zDPRQuestionEncoder.forward}  s   d 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B] ]%>cdd"66y.Q#..*K&',,.s3KTUU%.%:!!@T@T! $ 

;v64;;#;#;; 
 !"[[EJJvVN''))'/!5# ( 
 12;'!//w?T?Tahasas
 	
r    r   )r   r   r   r   r4   r
   r   rR   r$   r   rM   rU   rV   s   @r!   r   r   p  s    y   $((,(,'+)-,0#'Z
D=Z
 Z
 	Z

 }Z
  $;Z
 #TkZ
 D[Z
 
"E&#+$6	6Z
 Z
r    r   zE
    The bare DPRReader transformer outputting span predictions.
    c                        e Zd Zdef fdZe	 	 	 	 	 	 ddedz  dedz  dedz  dedz  dedz  d	edz  d
ee	edf   z  fd       Z
 xZS )	DPRReaderr0   c                 r    t         |   |       || _        t        |      | _        | j                          y rv   )r3   r4   r0   rX   rr   r:   r;   s     r!   r4   zDPRReader.__init__  s.     .v6r    Nr>   r?   rA   rB   rC   rD   rE   .c                    ||n| j                   j                  }||n| j                   j                  }||n| j                   j                  }||t	        d      |#| j                  ||       |j                         }n!||j                         dd }nt	        d      ||j                  n|j                  }	|t        j                  ||	      }| j                  ||||||      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
        ```
        Nrx   r^   ry   rz   )rA   rB   rC   rD   )r0   rB   rC   r   r6   r   r`   r{   r   r   rr   )
r<   r>   r?   rA   rB   rC   rD   rI   r   r{   s
             r!   rM   zDPRReader.forward  s   b 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B] ]%>cdd"66y.Q#..*K&',,.s3KTUU%.%:!!@T@T!"ZZFCN""'/!5# # 
 	
r    )NNNNNN)r   r   r   r   r4   r
   r   rR   r&   r   rM   rU   rV   s   @r!   r   r     s    y   $((,'+)-,0#'L
D=L
 L
 }	L

  $;L
 #TkL
 D[L
 
5-	-L
 L
r    r   )rt   ri   r+   rn   rq   r   r   )"r   dataclassesr   r   r   r   modeling_outputsr   modeling_utilsr   utilsr	   r
   r   bert.modeling_bertr   configuration_dprr   
get_loggerr   loggerr   r$   r&   r+   r.   rX   ri   rn   rq   rt   r   r   __all__r   r    r!   <module>r      s   < !   : - 
 + ( 
		H	% 

<k 
< 
< 

<{ 
< 
< 
<k < <$   62# 62r7
) 7
~&"4 &+#5 +), ) 
b
3 b

b
J 
c
5 c

c
L 
U
# U

U
pr    