
    qi                        d Z ddl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mZmZ ddlmZmZ ddlmZmZmZmZmZmZ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#m$Z$ ddl%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z:  e!jv                  e<      Z= G d de)      Z> G d de8      Z? G d de(      Z@ G d de3      ZA G d de7      ZB G d de5      ZC G d de*      ZDe  G d  d!e             ZE G d" d#e6      ZF G d$ d%e/      ZG G d& d'e.      ZH G d( d)e4      ZI G d* d+e+      ZJ G d, d-e-      ZK G d. d/e1      ZL G d0 d1e,      ZM G d2 d3e2      ZN G d4 d5e0      ZOg d6ZPy)7zPyTorch ERNIE model.    N)BCEWithLogitsLossCrossEntropyLossMSELoss   )initialization)CacheDynamicCacheEncoderDecoderCache)create_bidirectional_maskcreate_causal_mask),BaseModelOutputWithPoolingAndCrossAttentions!CausalLMOutputWithCrossAttentionsMaskedLMOutputMultipleChoiceModelOutputNextSentencePredictorOutputQuestionAnsweringModelOutputSequenceClassifierOutputTokenClassifierOutput)PreTrainedModel)Unpack)TransformersKwargsauto_docstringlogging)can_return_tuplemerge_with_config_defaults)capture_outputs   )BertCrossAttentionBertEmbeddingsBertEncoderBertForMaskedLMBertForMultipleChoiceBertForNextSentencePredictionBertForPreTrainingBertForPreTrainingOutputBertForQuestionAnsweringBertForSequenceClassificationBertForTokenClassification	BertLayerBertLMHeadModelBertLMPredictionHead	BertModel
BertPoolerBertSelfAttention   )ErnieConfigc                        e Zd ZdZ fdZ	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ed
ej                  fdZ
 xZS )ErnieEmbeddingszGConstruct the embeddings from word, position and token_type embeddings.c                     t         |   |       |j                  | _        |j                  r0t        j                  |j
                  |j                        | _        y y )N)super__init__use_task_idnn	Embeddingtask_type_vocab_sizehidden_sizetask_type_embeddings)selfconfig	__class__s     Y/opt/pipecat/venv/lib/python3.12/site-packages/transformers/models/ernie/modular_ernie.pyr5   zErnieEmbeddings.__init__B   sL     !--(*V5P5PRXRdRd(eD%     N	input_idstoken_type_idstask_type_idsposition_idsinputs_embedspast_key_values_lengthreturnc                 |   ||j                         }n|j                         d d }|\  }}	|| j                  d d ||	|z   f   }|t        | d      rT| j                  j	                  |j
                  d   d      }
t        j                  |
d|      }
|
j	                  ||	      }n:t        j                  |t        j                  | j                  j                        }|| j                  |      }| j                  |      }|j                  |j                        }||z   }| j                  |      }||z   }| j                  rR|:t        j                  |t        j                  | j                  j                        }| j!                  |      }||z  }| j#                  |      }| j%                  |      }|S )NrB   r   r/   )dimindex)dtypedevice)sizerD   hasattrrB   expandshapetorchgatherzeroslongrM   word_embeddingstoken_type_embeddingstoposition_embeddingsr6   r;   	LayerNormdropout)r<   rA   rB   rC   rD   rE   rF   input_shape
batch_size
seq_lengthbuffered_token_type_idsrW   
embeddingsrY   r;   s                  r?   forwardzErnieEmbeddings.forwardI   s     #..*K',,.s3K!,
J,,Q0FVlIl0l-lmL
 !t-.*.*=*=*D*D\EWEWXYEZ\^*_'*/,,7NTU]i*j'!8!?!?
J!W!&[

SWSdSdSkSk!l  00;M $ : :> J &(()>)E)EF"%::
"66|D"55
 $ %KuzzRVRcRcRjRj k#'#<#<]#K ..J^^J/
\\*-
r@   )NNNNNr   )__name__
__module____qualname____doc__r5   rR   
LongTensorFloatTensorintTensorra   __classcell__r>   s   @r?   r2   r2   ?   s    Qf .226150426&'3##d*3 ((4/3 ''$.	3
 &&-3 ((4/3 !$3 
3r@   r2   c                       e Zd Zy)ErnieSelfAttentionNrb   rc   rd    r@   r?   rm   rm          r@   rm   c                       e Zd Zy)ErnieCrossAttentionNrn   ro   r@   r?   rr   rr      rp   r@   rr   c                       e Zd Zy)
ErnieLayerNrn   ro   r@   r?   rt   rt      rp   r@   rt   c                       e Zd Zy)ErniePoolerNrn   ro   r@   r?   rv   rv      rp   r@   rv   c                       e Zd Zy)ErnieLMPredictionHeadNrn   ro   r@   r?   rx   rx      rp   r@   rx   c                       e Zd Zy)ErnieEncoderNrn   ro   r@   r?   rz   rz      rp   r@   rz   c                   n     e Zd ZeZdZdZdZdZdZ	dZ
eeedZ ej                           fd       Z xZS )ErniePreTrainedModelernieT)hidden_states
attentionscross_attentionsc                    t         |   |       t        |t              r t	        j
                  |j                         yt        |t              ryt	        j                  |j                  t        j                  |j                  j                  d         j                  d             t	        j
                  |j                         yy)zInitialize the weightsrI   )r/   rI   N)r4   _init_weights
isinstancerx   initzeros_biasr2   copy_rD   rR   arangerQ   rP   rB   )r<   moduler>   s     r?   r   z"ErniePreTrainedModel._init_weights   s     	f%f34KK$0JJv**ELL9L9L9R9RSU9V,W,^,^_f,ghKK--. 1r@   )rb   rc   rd   r0   config_classbase_model_prefixsupports_gradient_checkpointing_supports_flash_attn_supports_sdpa_supports_flex_attn_supports_attention_backendrt   rm   rr   _can_record_outputsrR   no_gradr   rj   rk   s   @r?   r|   r|      sX    L&*#N"&#(/ U]]_/ /r@   r|   c                       e Zd ZdgZd fd	Zeee	 	 	 	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  dej                  dz  de
dz  dedz  dej                  dz  dee   deej                     ez  fd                     Zd Z xZS )
ErnieModelrt   c                     t         |   | |       || _        d| _        t	        |      | _        t        |      | _        |rt        |      nd | _	        | j                          y )NF)r4   r5   r=   gradient_checkpointingr2   r`   rz   encoderrv   pooler	post_init)r<   r=   add_pooling_layerr>   s      r?   r5   zErnieModel.__init__   sU    v&&+#)&1#F+->k&)D 	r@   NrA   attention_maskrB   rC   rD   rE   encoder_hidden_statesencoder_attention_maskpast_key_values	use_cachecache_positionkwargsrG   c                    | j                   j                  r|
|
n| j                   j                  }
nd}
| j                  r%| j                  r|
rt
        j                  d       d}
|
rd|	b|| j                   j                  r4t        t        | j                         t        | j                               nt        | j                         }	||t        d      |#| j                  ||       |j                         }n!||j                         dd }nt        d      |\  }}||j                  n|j                  }|	|	j                         nd}|t        j                   |||z   |	      }| j#                  ||||||
      }| j%                  ||||||	      \  }} | j&                  |f||||	|
||d|}|d   }| j(                  | j)                  |      nd}t+        |||j,                        S )  
        task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Task type embedding is a special embedding to represent the characteristic of different tasks, such as
            word-aware pre-training task, structure-aware pre-training task and semantic-aware pre-training task. We
            assign a `task_type_id` to each task and the `task_type_id` is in the range `[0,
            config.task_type_vocab_size-1]
        NFzZ`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...)r=   zDYou cannot specify both input_ids and inputs_embeds at the same timerI   z5You have to specify either input_ids or inputs_embedsr   )rM   )rA   rD   rB   rC   rE   rF   )r   r   embedding_outputr   r   r   )r   r   r   r   r   r   rD   )last_hidden_statepooler_outputr   )r=   
is_decoderr   r   trainingloggerwarning_onceis_encoder_decoderr
   r	   
ValueError%warn_if_padding_and_no_attention_maskrN   rM   get_seq_lengthrR   r   r`   _create_attention_masksr   r   r   r   )r<   rA   r   rB   rC   rD   rE   r   r   r   r   r   r   r\   r]   r^   rM   rF   r   encoder_outputssequence_outputpooled_outputs                         r?   ra   zErnieModel.forward   s1   2 ;;!!%.%:	@U@UII&&4==##p "	0 )48V8V $L$DlZ^ZeZeFfg!5   ]%>cdd"66y.Q#..*K&',,.s3KTUU!,
J%.%:!!@T@TETE`!?!?!Afg!"\\*@BX[eBentuN??%)''#9 + 
 261M1M)#9-"7)+ 2N 2
.. '$,,

)"7#9+)%

 

 *!,8<8OO4UY;-'+;;
 	
r@   c                     | j                   j                  rt        | j                   ||||      }nt        | j                   ||      }|t        | j                   |||      }||fS )N)r=   rE   r   r   r   )r=   rE   r   )r=   rE   r   r   )r=   r   r   r   )r<   r   r   r   r   r   r   s          r?   r   z"ErnieModel._create_attention_masks&  sx     ;;!!/{{.-- /N 7{{.-N "-%>{{.5&;	&" 555r@   )T)NNNNNNNNNNN)rb   rc   rd   _no_split_modulesr5   r   r   r   rR   ri   r   boolr   r   tupler   ra   r   rj   rk   s   @r?   r   r      sZ   %   *..2.2-1,0-1596:(,!%.2_
<<$&_
 t+_
 t+	_

 ||d*_
 llT)_
 ||d*_
  %||d2_
 !&t 3_
 _
 $;_
 t+_
 +,_
 
u||	K	K_
    _
D 6r@   r   c                       e Zd Zy)ErnieForPreTrainingOutputNrn   ro   r@   r?   r   r   I  rp   r@   r   c                   b   e Zd ZdddZee	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  dej                  dz  dej                  dz  dee	   de
ej                     ez  fd              Zy)ErnieForPreTrainingcls.predictions.bias'ernie.embeddings.word_embeddings.weightzcls.predictions.decoder.biaszcls.predictions.decoder.weightNrA   r   rB   rC   rD   rE   labelsnext_sentence_labelr   rG   c	           
          | j                   |f|||||dd|	}
|
dd \  }}| j                  ||      \  }}d}|u|st               } ||j                  d| j                  j
                        |j                  d            } ||j                  dd      |j                  d            }||z   }t        ||||
j                  |
j                        S )a:  
        task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Task type embedding is a special embedding to represent the characteristic of different tasks, such as
            word-aware pre-training task, structure-aware pre-training task and semantic-aware pre-training task. We
            assign a `task_type_id` to each task and the `task_type_id` is in the range `[0,
            config.task_type_vocab_size-1]
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ...,
            config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked),
            the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`
        next_sentence_label (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the next sequence prediction (classification) loss. Input should be a sequence
            pair (see `input_ids` docstring) Indices should be in `[0, 1]`:

            - 0 indicates sequence B is a continuation of sequence A,
            - 1 indicates sequence B is a random sequence.

        Example:

        ```python
        >>> from transformers import AutoTokenizer, ErnieForPreTraining
        >>> import torch

        >>> tokenizer = AutoTokenizer.from_pretrained("nghuyong/ernie-1.0-base-zh")
        >>> model = ErnieForPreTraining.from_pretrained("nghuyong/ernie-1.0-base-zh")

        >>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
        >>> outputs = model(**inputs)

        >>> prediction_logits = outputs.prediction_logits
        >>> seq_relationship_logits = outputs.seq_relationship_logits
        ```
        Tr   rB   rC   rD   rE   return_dictNr   rI   )lossprediction_logitsseq_relationship_logitsr~   r   )	r}   clsr   viewr=   
vocab_sizer   r~   r   )r<   rA   r   rB   rC   rD   rE   r   r   r   outputsr   r   prediction_scoresseq_relationship_score
total_lossloss_fctmasked_lm_lossnext_sentence_losss                      r?   ra   zErnieForPreTraining.forwardS  s   ^ $**	
))'%'	
 	
 *1!&48HH_m4\11
"5"A')H%&7&<&<RAWAW&XZ`ZeZefhZijN!)*@*E*Eb!*LNaNfNfgiNj!k'*<<J(/$:!//))
 	
r@   NNNNNNNN)rb   rc   rd   _tied_weights_keysr   r   rR   ri   r   r   r   r   ra   ro   r@   r?   r   r   M  s   (>*S
  *..2.2-1,0-1&*37H
<<$&H
 t+H
 t+	H

 ||d*H
 llT)H
 ||d*H
 t#H
 #\\D0H
 +,H
 
u||	8	8H
  H
r@   r   c            "          e Zd Zee	 	 	 	 	 	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  deej                     dz  dedz  dej                  dz  de	ej                  z  de
e   deej                     ez  fd              Zy)ErnieForCausalLMNrA   r   rB   rC   rD   rE   r   r   r   r   r   r   logits_to_keepr   rG   c                    |	d} | j                   |f||||||||
||dd|}|j                  }t        |t              rt	        | d      n|}| j                  |dd|ddf         }d}|	* | j                  d||	| j                  j                  d|}t        |||j                  |j                  |j                  |j                        S )a  
        task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Task type embedding is a special embedding to represent the characteristic of different tasks, such as
            word-aware pre-training task, structure-aware pre-training task and semantic-aware pre-training task. We
            assign a `task_type_id` to each task and the `task_type_id` is in the range `[0,
            config.task_type_vocab_size-1]
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in
            `[-100, 0, ..., config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are
            ignored (masked), the loss is only computed for the tokens with labels n `[0, ..., config.vocab_size]`
        NFT)r   rB   rC   rD   rE   r   r   r   r   r   r   )logitsr   r   )r   r   r   r~   r   r   ro   )r}   r   r   rh   slicer   loss_functionr=   r   r   r   r~   r   r   )r<   rA   r   rB   rC   rD   rE   r   r   r   r   r   r   r   r   r   r~   slice_indicesr   r   s                       r?   ra   zErnieForCausalLM.forward  s   < I@J

A
))'%'"7#9+)A
 A
   118B>SV8W~ot4]k-=!(;<=%4%%pVFt{{OeOepiopD0#33!//))$55
 	
r@   )NNNNNNNNNNNNr   )rb   rc   rd   r   r   rR   ri   listr   rh   r   r   r   r   ra   ro   r@   r?   r   r     sc    *..2.2-1,0-1596:&*59!%.2-.?
<<$&?
 t+?
 t+	?

 ||d*?
 llT)?
 ||d*?
  %||d2?
 !&t 3?
 t#?
 ell+d2?
 $;?
 t+?
 ell*?
 +,?
  
u||	@	@!?
  ?
r@   r   c                      e Zd ZdddZee	 	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  dej                  dz  dej                  dz  dej                  dz  dee	   de
ej                     ez  fd              Zy)ErnieForMaskedLMr   r   r   NrA   r   rB   rC   rD   rE   r   r   r   r   rG   c
                 @    | j                   |f|||||||dd|
}|d   }| j                  |      }d}|	Ft               } ||j                  d| j                  j
                        |	j                  d            }t        |||j                  |j                        S )as  
        task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Task type embedding is a special embedding to represent the characteristic of different tasks, such as
            word-aware pre-training task, structure-aware pre-training task and semantic-aware pre-training task. We
            assign a `task_type_id` to each task and the `task_type_id` is in the range `[0,
            config.task_type_vocab_size-1]
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ...,
            config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the
            loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`
        T)r   rB   rC   rD   rE   r   r   r   r   NrI   r   r   r~   r   )	r}   r   r   r   r=   r   r   r~   r   )r<   rA   r   rB   rC   rD   rE   r   r   r   r   r   r   r   r   r   s                   r?   ra   zErnieForMaskedLM.forward  s    4 $**
))'%'"7#9
 
 "!* HH_5')H%&7&<&<RAWAW&XZ`ZeZefhZijN$!//))	
 	
r@   )	NNNNNNNNN)rb   rc   rd   r   r   r   rR   ri   r   r   r   r   ra   ro   r@   r?   r   r     s   (>*S
  *..2.2-1,0-1596:&*2
<<$&2
 t+2
 t+	2

 ||d*2
 llT)2
 ||d*2
  %||d22
 !&t 32
 t#2
 +,2
 
u||	~	-2
  2
r@   r   c                   8   e Zd Zee	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ee   d
e	ej                     e
z  fd              Zy)ErnieForNextSentencePredictionNrA   r   rB   rC   rD   rE   r   r   rG   c           
          | j                   |f|||||dd|}	|	d   }
| j                  |
      }d}|2t               } ||j                  dd      |j                  d            }t	        |||	j
                  |	j                        S )a  
        task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Task type embedding is a special embedding to represent the characteristic of different tasks, such as
            word-aware pre-training task, structure-aware pre-training task and semantic-aware pre-training task. We
            assign a `task_type_id` to each task and the `task_type_id` is in the range `[0,
            config.task_type_vocab_size-1]
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair
            (see `input_ids` docstring). Indices should be in `[0, 1]`:

            - 0 indicates sequence B is a continuation of sequence A,
            - 1 indicates sequence B is a random sequence.

        Example:

        ```python
        >>> from transformers import AutoTokenizer, ErnieForNextSentencePrediction
        >>> import torch

        >>> tokenizer = AutoTokenizer.from_pretrained("nghuyong/ernie-1.0-base-zh")
        >>> model = ErnieForNextSentencePrediction.from_pretrained("nghuyong/ernie-1.0-base-zh")

        >>> prompt = "In Italy, pizza served in formal settings, such as at a restaurant, is presented unsliced."
        >>> next_sentence = "The sky is blue due to the shorter wavelength of blue light."
        >>> encoding = tokenizer(prompt, next_sentence, return_tensors="pt")

        >>> outputs = model(**encoding, labels=torch.LongTensor([1]))
        >>> logits = outputs.logits
        >>> assert logits[0, 0] < logits[0, 1]  # next sentence was random
        ```
        Tr   r/   NrI   r   r   )r}   r   r   r   r   r~   r   )r<   rA   r   rB   rC   rD   rE   r   r   r   r   seq_relationship_scoresr   r   s                 r?   ra   z&ErnieForNextSentencePrediction.forward#  s    Z $**	
))'%'	
 	
  
"&((="9!')H!)*A*F*Fr1*Mv{{[]!_*#*!//))	
 	
r@   NNNNNNN)rb   rc   rd   r   r   rR   ri   r   r   r   r   ra   ro   r@   r?   r   r   "  s     *..2.2-1,0-1&*D
<<$&D
 t+D
 t+	D

 ||d*D
 llT)D
 ||d*D
 t#D
 +,D
 
u||	:	:D
  D
r@   r   c                   8   e Zd Zee	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ee   d
e	ej                     e
z  fd              Zy)ErnieForSequenceClassificationNrA   r   rB   rC   rD   rE   r   r   rG   c           
          | j                   |f|||||dd|}	|	d   }
| j                  |
      }
| j                  |
      }d}|| j                  j                  | j
                  dk(  rd| j                  _        nl| j
                  dkD  rL|j                  t        j                  k(  s|j                  t        j                  k(  rd| j                  _        nd| j                  _        | j                  j                  dk(  rIt               }| j
                  dk(  r& ||j                         |j                               }n |||      }n| j                  j                  dk(  r=t               } ||j                  d| j
                        |j                  d            }n,| j                  j                  dk(  rt               } |||      }t        |||	j                   |	j"                  	      S )
a^  
        task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Task type embedding is a special embedding to represent the characteristic of different tasks, such as
            word-aware pre-training task, structure-aware pre-training task and semantic-aware pre-training task. We
            assign a `task_type_id` to each task and the `task_type_id` is in the range `[0,
            config.task_type_vocab_size-1]
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        Tr   r/   N
regressionsingle_label_classificationmulti_label_classificationrI   r   )r}   r[   
classifierr=   problem_type
num_labelsrL   rR   rU   rh   r   squeezer   r   r   r   r~   r   )r<   rA   r   rB   rC   rD   rE   r   r   r   r   r   r   r   s                 r?   ra   z&ErnieForSequenceClassification.forwardm  s   0 $**	
))'%'	
 	
  
]3/{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#FNN$4fnn6FGD#FF3D))-JJ+-B @&++b/R))-II,./'!//))	
 	
r@   r   )rb   rc   rd   r   r   rR   ri   r   r   r   r   ra   ro   r@   r?   r   r   l  s     *..2.2-1,0-1&*B
<<$&B
 t+B
 t+	B

 ||d*B
 llT)B
 ||d*B
 t#B
 +,B
 
u||	7	7B
  B
r@   r   c                   8   e Zd Zee	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ee   d
e	ej                     e
z  fd              Zy)ErnieForMultipleChoiceNrA   r   rB   rC   rD   rE   r   r   rG   c           
         ||j                   d   n|j                   d   }	|!|j                  d|j                  d            nd}|!|j                  d|j                  d            nd}|!|j                  d|j                  d            nd}|!|j                  d|j                  d            nd}|1|j                  d|j                  d      |j                  d            nd} | j                  |f|||||dd|}
|
d   }| j	                  |      }| j                  |      }|j                  d|	      }d}|t               } |||      }t        |||
j                  |
j                        S )a9	  
        input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

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

            [What are input IDs?](../glossary#input-ids)
        token_type_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`, *optional*):
            Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,
            1]`:

            - 0 corresponds to a *sentence A* token,
            - 1 corresponds to a *sentence B* token.

            [What are token type IDs?](../glossary#token-type-ids)
        task_type_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`, *optional*):
            Task type embedding is a special embedding to represent the characteristic of different tasks, such as
            word-aware pre-training task, structure-aware pre-training task and semantic-aware pre-training task. We
            assign a `task_type_id` to each task and the `task_type_id` is in the range `[0,
            config.task_type_vocab_size-1]
        position_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.max_position_embeddings - 1]`.

            [What are position IDs?](../glossary#position-ids)
        inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_choices, 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.
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the multiple choice classification loss. Indices should be in `[0, ...,
            num_choices-1]` where `num_choices` is the size of the second dimension of the input tensors. (See
            `input_ids` above)
        Nr/   rI   Tr   r   )
rQ   r   rN   r}   r[   r   r   r   r~   r   )r<   rA   r   rB   rC   rD   rE   r   r   num_choicesr   r   r   reshaped_logitsr   r   s                   r?   ra   zErnieForMultipleChoice.forward  s   ` -6,Aiooa(}GZGZ[\G]>G>SINN2y~~b'9:Y]	M[Mg,,R1D1DR1HImqM[Mg,,R1D1DR1HImqGSG_|((\->->r-BCei ( r=#5#5b#9=;M;Mb;QR 	 $**	
))'%'	
 	
  
]3/ ++b+6')HOV4D("!//))	
 	
r@   r   )rb   rc   rd   r   r   rR   ri   r   r   r   r   ra   ro   r@   r?   r   r     s     *..2.2-1,0-1&*U
<<$&U
 t+U
 t+	U

 ||d*U
 llT)U
 ||d*U
 t#U
 +,U
 
u||	8	8U
  U
r@   r   c                   8   e Zd Zee	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ee   d
e	ej                     e
z  fd              Zy)ErnieForTokenClassificationNrA   r   rB   rC   rD   rE   r   r   rG   c           
      J    | j                   |f|||||dd|}	|	d   }
| j                  |
      }
| j                  |
      }d}|<t               } ||j	                  d| j
                        |j	                  d            }t        |||	j                  |	j                        S )a  
        task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Task type embedding is a special embedding to represent the characteristic of different tasks, such as
            word-aware pre-training task, structure-aware pre-training task and semantic-aware pre-training task. We
            assign a `task_type_id` to each task and the `task_type_id` is in the range `[0,
            config.task_type_vocab_size-1]
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`.
        Tr   r   NrI   r   )	r}   r[   r   r   r   r   r   r~   r   )r<   rA   r   rB   rC   rD   rE   r   r   r   r   r   r   r   s                 r?   ra   z#ErnieForTokenClassification.forward  s    , $**	
))'%'	
 	
 "!*,,71')HFKKDOO<fkk"oND$!//))	
 	
r@   r   )rb   rc   rd   r   r   rR   ri   r   r   r   r   ra   ro   r@   r?   r   r     s     *..2.2-1,0-1&*.
<<$&.
 t+.
 t+	.

 ||d*.
 llT).
 ||d*.
 t#.
 +,.
 
u||	4	4.
  .
r@   r   c                   X   e Zd Zee	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ee   de	ej                     e
z  fd              Zy)ErnieForQuestionAnsweringNrA   r   rB   rC   rD   rE   start_positionsend_positionsr   rG   c	           
          | j                   |f|||||dd|	}
|
d   }| j                  |      }|j                  dd      \  }}|j                  d      j	                         }|j                  d      j	                         }d}||t        |j                               dkD  r|j                  d      }t        |j                               dkD  r|j                  d      }|j                  d      }|j                  d|      }|j                  d|      }t        |      } |||      } |||      }||z   d	z  }t        ||||
j                  |
j                  
      S )r   Tr   r   r/   rI   )rJ   N)ignore_indexr   )r   start_logits
end_logitsr~   r   )r}   
qa_outputssplitr   
contiguouslenrN   clampr   r   r~   r   )r<   rA   r   rB   rC   rD   rE   r   r   r   r   r   r   r   r   r   ignored_indexr   
start_lossend_losss                       r?   ra   z!ErnieForQuestionAnswering.forwardD  s   * $**	
))'%'	
 	
 "!*1#)<<r<#: j#++B/::<''+668

&=+D?'')*Q."1"9"9""==%%'(1, - 5 5b 9(--a0M-33A}EO)//=AM']CH!,@J
M:H$x/14J+%!!//))
 	
r@   r   )rb   rc   rd   r   r   rR   ri   r   r   r   r   ra   ro   r@   r?   r   r   C  s     *..2.2-1,0-1/3-1<
<<$&<
 t+<
 t+	<

 ||d*<
 llT)<
 ||d*<
 ,<
 ||d*<
 +,<
 
u||	;	;<
  <
r@   r   )
r   r   r   r   r   r   r   r   r   r|   )Qre   rR   torch.nnr7   r   r   r    r   r   cache_utilsr   r	   r
   masking_utilsr   r   modeling_outputsr   r   r   r   r   r   r   r   modeling_utilsr   processing_utilsr   utilsr   r   r   utils.genericr   r   utils.output_capturingr   bert.modeling_bertr   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   configuration_ernier0   
get_loggerrb   r   r2   rm   rr   rt   rv   rx   rz   r|   r   r   r   r   r   r   r   r   r   r   __all__ro   r@   r?   <module>r     s      A A & C C J	 	 	 . & @ @ I 5    & - 
		H	%=n =@	* 		, 		 		* 		0 		; 	 /? / /2U6 U6p	 8 	P
, P
fB
 B
J:
 :
zG
%B G
TE
%B E
PX
2 X
v1
"< 1
h?
 8 ?
Dr@   