Ë
    ÛÓ^jèŒ  ã                   óÚ  — 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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,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7  ejp                  e9«      Z: G d„ de&«      Z; G d„ de5«      Z< G d„ de%«      Z= G d„ de0«      Z> G d„ de4«      Z? G d„ de2«      Z@ G d„ de'«      ZAe G d„ d e«      «       ZB G d!„ d"e3«      ZC G d#„ d$e,«      ZD G d%„ d&e+«      ZE G d'„ d(e1«      ZF G d)„ d*e(«      ZG G d+„ d,e*«      ZH G d-„ d.e.«      ZI G d/„ d0e)«      ZJ G d1„ d2e/«      ZK G d3„ d4e-«      ZLg d5¢ZMy)6zPyTorch ERNIE model.é    N)ÚBCEWithLogitsLossÚCrossEntropyLossÚMSELossé   )Úinitialization)ÚCacheÚDynamicCacheÚEncoderDecoderCache)Ú,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     €úr/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/ernie/modular_ernie.pyr3   zErnieEmbeddings.__init__A   sL   ø€ Ü‰Ñ˜Ô à!×-Ñ-ˆÔØ×ÒÜ(*¯©°V×5PÑ5PÐRX×RdÑRdÓ(eˆDÕ%ð ó    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 )Néÿÿÿÿr@   r   r-   )ÚdimÚindex)ÚdtypeÚdevice)ÚsizerB   Úhasattrr@   ÚexpandÚshapeÚtorchÚgatherÚzerosÚlongrK   Úword_embeddingsÚtoken_type_embeddingsÚtoÚposition_embeddingsr4   r9   Ú	LayerNormÚdropout)r:   r?   r@   rA   rB   rC   rD   Úinput_shapeÚ
batch_sizeÚ
seq_lengthÚbuffered_token_type_idsrU   Ú
embeddingsrW   r9   s                  r=   ÚforwardzErnieEmbeddings.forwardH   s¹  € ð Ð Ø#Ÿ.™.Ó*‰Kà'×,Ñ,Ó.¨s°Ð3ˆKà!,Ñˆ
�JàÐØ×,Ñ,ªQÐ0FÈÐVlÑIlÐ0lÐ-lÑmˆLð
 Ð!Ü�tÐ-Ô.à*.×*=Ñ*=×*DÑ*DÀ\×EWÑEWÐXYÑEZÐ\^Ó*_Ð'Ü*/¯,©,Ð7NÐTUÐ]iÔ*jÐ'Ø!8×!?Ñ!?À
ÈJÓ!W‘ä!&§¡¨[ÄÇ
Á
ÐSW×SdÑSd×SkÑSkÔ!l�àÐ Ø ×0Ñ0°Ó;ˆMØ $× :Ñ :¸>Ó JÐð &×(Ñ(Ð)>×)EÑ)EÓFˆØ"Ð%:Ñ:ˆ
à"×6Ñ6°|ÓDÐØÐ"5Ñ5ˆ
ð ×ÒØÐ$Ü %§¡¨K¼u¿z¹zÐRV×RcÑRc×RjÑRjÔ k�Ø#'×#<Ñ#<¸]Ó#KÐ ØÐ.Ñ.ˆJà—^‘^ JÓ/ˆ
Ø—\‘\ *Ó-ˆ
ØÐr>   )NNNNNr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   rP   Ú
LongTensorÚFloatTensorÚintÚTensorr_   Ú__classcell__©r<   s   @r=   r0   r0   >   s¤   ø„ ÙQôfð .2Ø26Ø15Ø04Ø26Ø&'ñ3à×#Ñ# dÑ*ð3ð ×(Ñ(¨4Ñ/ð3ð ×'Ñ'¨$Ñ.ð	3ð
 ×&Ñ&¨Ñ-ð3ð ×(Ñ(¨4Ñ/ð3ð !$ð3ð 
�‰÷3r>   r0   c                   ó   — e Zd Zy)ÚErnieSelfAttentionN©r`   ra   rb   © r>   r=   rk   rk   ~   ó   „ Ør>   rk   c                   ó   — e Zd Zy)ÚErnieCrossAttentionNrl   rm   r>   r=   rp   rp   ‚   rn   r>   rp   c                   ó   — e Zd Zy)Ú
ErnieLayerNrl   rm   r>   r=   rr   rr   †   rn   r>   rr   c                   ó   — e Zd Zy)ÚErniePoolerNrl   rm   r>   r=   rt   rt   Š   rn   r>   rt   c                   ó   — e Zd Zy)ÚErnieLMPredictionHeadNrl   rm   r>   r=   rv   rv   Ž   rn   r>   rv   c                   ó   — e Zd Zy)ÚErnieEncoderNrl   rm   r>   r=   rx   rx   ’   rn   r>   rx   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 weightsrG   )r-   rG   N)r2   Ú_init_weightsÚ
isinstancerv   ÚinitÚzeros_Úbiasr0   Úcopy_rB   rP   ÚarangerO   rN   r@   )r:   Úmoduler<   s     €r=   r€   z"ErniePreTrainedModel._init_weights¥   s‰   ø€ ô 	‰Ñ˜fÔ%Ü�fÔ3Ô4Ü�K‰K˜Ÿ™Õ$Ü˜¤Ô0Ü�J‰J�v×*Ñ*¬E¯L©L¸×9LÑ9L×9RÑ9RÐSUÑ9VÓ,W×,^Ñ,^Ð_fÓ,gÔhÜ�K‰K˜×-Ñ-Õ.ð 1r>   )r`   ra   rb   r.   Úconfig_classÚbase_model_prefixÚsupports_gradient_checkpointingÚ_supports_flash_attnÚ_supports_sdpaÚ_supports_flex_attnÚ_supports_attention_backendrr   rk   rp   Ú_can_record_outputsrP   Úno_gradr€   rh   ri   s   @r=   rz   rz   –   sX   ø„ à€LØÐØ&*Ð#ØÐØ€NØÐØ"&Ðà#Ø(Ø/ñÐð €U‡]�]ƒ_ó/ó ô/r>   rz   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e   deej                     ez  fd„«       «       «       Zˆ xZS )Ú
ErnieModelrr   c                 óÊ   •— t         ‰| �  | |«       || _        d| _        t	        |«      | _        t        |«      | _        |rt        |«      nd | _	        | j                  «        y )NF)r2   r3   r;   Úgradient_checkpointingr0   r^   rx   Úencoderrt   ÚpoolerÚ	post_init)r:   r;   Úadd_pooling_layerr<   s      €r=   r3   zErnieModel.__init__³   sU   ø€ Ü‰Ñ˜˜vÔ&ØˆŒØ&+ˆÔ#ä)¨&Ó1ˆŒÜ# FÓ+ˆŒá->”k &Ô)ÀDˆŒð 	�‰Õr>   Nr?   Úattention_maskr@   rA   rB   rC   Úencoder_hidden_statesÚencoder_attention_maskÚpast_key_valuesÚ	use_cacheÚkwargsrE   c           
      ó�  — |du |duz  rt        d«      ‚| j                  j                  r|
�|
n| j                  j                  }
nd}
|
rd|	€b|€| j                  j                  r4t        t        | j                  ¬«      t        | j                  ¬«      «      nt        | j                  ¬«      }	|	�|	j                  «       nd}| 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]
        Nz:You must specify exactly one of input_ids or inputs_embedsF)r;   r   )r?   rB   r@   rA   rC   rD   )r™   r›   Úembedding_outputrš   rœ   )r™   rš   r›   rœ   r�   rB   )Úlast_hidden_stateÚpooler_outputrœ   )Ú
ValueErrorr;   Ú
is_decoderr�   Úis_encoder_decoderr
   r	   Úget_seq_lengthr^   Ú_create_attention_masksr•   r–   r   rœ   )r:   r?   r™   r@   rA   rB   rC   rš   r›   rœ   r�   rž   rD   r¡   Úencoder_outputsÚsequence_outputÚpooled_outputs                    r=   r_   zErnieModel.forwardÀ   s~  € ð0 ˜Ð -°tÐ";Ò<ÜÐYÓZÐZà�;‰;×!Ò!Ø%.Ð%:™	ÀÇÁ×@UÑ@U‰IàˆIá˜Ð0ð )Ð4¸¿¹×8VÒ8Vô $¤L¸¿¹Ô$DÄlÐZ^×ZeÑZeÔFfÔgä!¨¯©Ô5ð ð FUÐE` ×!?Ñ!?Ô!AÐfgÐØŸ?™?ØØ%Ø)à'Ø'Ø#9ð +ó 
Ðð 26×1MÑ1MØ)Ø#9Ø-Ø"7Ø+ð 2Nó 2
Ñ.ˆÐ.ð '˜$Ÿ,™,Øð	
à)Ø"7Ø#9Ø+ØØ%ñ	
ð ñ	
ˆð *¨!Ñ,ˆØ8<¿¹Ð8O˜Ÿ™ OÔ4ÐUYˆä;Ø-Ø'Ø+×;Ñ;ô
ð 	
r>   )T)
NNNNNNNNNN)r`   ra   rb   Ú_no_split_modulesr3   r   r   r   rP   rg   r   Úboolr   r   Útupler   r_   rh   ri   s   @r=   r’   r’   °   s=  ø„ Ø%˜Ðõð  ØØð *.Ø.2Ø.2Ø-1Ø,0Ø-1Ø59Ø6:Ø(,Ø!%ñH
à—<‘< $Ñ&ðH
ð Ÿ™ tÑ+ðH
ð Ÿ™ tÑ+ð	H
ð
 —|‘| dÑ*ðH
ð —l‘l TÑ)ðH
ð —|‘| dÑ*ðH
ð  %Ÿ|™|¨dÑ2ðH
ð !&§¡¨tÑ 3ðH
ð  ™ðH
ð ˜$‘;ðH
ð Ð+Ñ,ðH
ð 
ˆu�|‰|Ñ	ÐKÑ	KòH
ó ó ó  ôH
r>   r’   c                   ó   — e Zd Zy)ÚErnieForPreTrainingOutputNrl   rm   r>   r=   r°   r°     rn   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.weightNr?   r™   r@   rA   rB   rC   ÚlabelsÚnext_sentence_labelrž   rE   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™   r@   rA   rB   rC   Úreturn_dictNr   rG   )ÚlossÚprediction_logitsÚseq_relationship_logitsr|   r}   )	r{   Úclsr   Úviewr;   Ú
vocab_sizer°   r|   r}   )r:   r?   r™   r@   rA   rB   rC   r¶   r·   rž   Úoutputsrª   r«   Úprediction_scoresÚseq_relationship_scoreÚ
total_lossÚloss_fctÚmasked_lm_lossÚnext_sentence_losss                      r=   r_   zErnieForPreTraining.forward  s  € ð^ �$—*‘*Øð	
à)Ø)Ø'Ø%Ø'Øñ	
ð ñ	
ˆð *1°°!¨Ñ&ˆ˜Ø48·H±H¸_ÈmÓ4\Ñ1ÐÐ1àˆ
ØÐÐ"5Ð"AÜ'Ó)ˆHÙ%Ð&7×&<Ñ&<¸RÀÇÁ×AWÑAWÓ&XÐZ`×ZeÑZeÐfhÓZiÓjˆNÙ!)Ð*@×*EÑ*EÀbÈ!Ó*LÐNa×NfÑNfÐgiÓNjÓ!kÐØ'Ð*<Ñ<ˆJä(ØØ/Ø$:Ø!×/Ñ/Ø×)Ñ)ô
ð 	
r>   ©NNNNNNNN)r`   ra   rb   Ú_tied_weights_keysr   r   rP   rg   r   r   r®   r°   r_   rm   r>   r=   r²   r²     s  „ à(>Ø*SñÐð
 Øð *.Ø.2Ø.2Ø-1Ø,0Ø-1Ø&*Ø37ñH
à—<‘< $Ñ&ðH
ð Ÿ™ tÑ+ðH
ð Ÿ™ tÑ+ð	H
ð
 —|‘| dÑ*ðH
ð —l‘l TÑ)ð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	ej                  z  de
e   deej                     ez  fd„«       «       Zy)ÚErnieForCausalLMNr?   r™   r@   rA   rB   rC   rš   r›   r¶   rœ   r�   Úlogits_to_keeprž   rE   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™   r@   rA   rB   rC   rš   r›   rœ   r�   rº   )Úlogitsr¶   rÀ   )r»   rÎ   rœ   r|   r}   r~   rm   )r{   r¢   r�   rf   Úslicer¾   Úloss_functionr;   rÀ   r   rœ   r|   r}   r~   )r:   r?   r™   r@   rA   rB   rC   rš   r›   r¶   rœ   r�   rÌ   rž   rÁ   r|   Úslice_indicesrÎ   r»   s                      r=   r_   zErnieForCausalLM.forwardf  sÿ   € ð: ÐØˆIà@JÀÇ
Á
ØðA
à)Ø)Ø'Ø%Ø'Ø"7Ø#9Ø+ØØñA
ð ñA
ˆð  ×1Ñ1ˆä8BÀ>ÔSVÔ8Wœ˜~˜o¨tÔ4Ð]kˆØ—‘˜-ª¨=º!Ð(;Ñ<Ó=ˆàˆØÐØ%�4×%Ñ%Ðp¨V¸FÈtÏ{É{×OeÑOeÑpÐioÑpˆDä0ØØØ#×3Ñ3Ø!×/Ñ/Ø×)Ñ)Ø$×5Ñ5ô
ð 	
r>   )NNNNNNNNNNNr   )r`   ra   rb   r   r   rP   rg   Úlistr­   rf   r   r   r®   r   r_   rm   r>   r=   rË   rË   e  sM  „ ØØð *.Ø.2Ø.2Ø-1Ø,0Ø-1Ø59Ø6:Ø&*Ø59Ø!%Ø-.ñ=
à—<‘< $Ñ&ð=
ð Ÿ™ tÑ+ð=
ð Ÿ™ tÑ+ð	=
ð
 —|‘| dÑ*ð=
ð —l‘l TÑ)ð=
ð —|‘| dÑ*ð=
ð  %Ÿ|™|¨dÑ2ð=
ð !&§¡¨tÑ 3ð=
ð —‘˜tÑ#ð=
ð ˜eŸl™lÑ+¨dÑ2ð=
ð ˜$‘;ð=
ð ˜eŸl™lÑ*ð=
ð Ð+Ñ,ð=
ð 
ˆ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µ   Nr?   r™   r@   rA   rB   rC   rš   r›   r¶   rž   rE   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™   r@   rA   rB   rC   rš   r›   rº   r   NrG   ©r»   rÎ   r|   r}   )	r{   r¾   r   r¿   r;   rÀ   r   r|   r}   )r:   r?   r™   r@   rA   rB   rC   rš   r›   r¶   rž   rÁ   rª   rÂ   rÆ   rÅ   s                   r=   r_   zErnieForMaskedLM.forward®  sÁ   € ð4 �$—*‘*Øð
à)Ø)Ø'Ø%Ø'Ø"7Ø#9Øñ
ð ñ
ˆð " !™*ˆØ ŸH™H _Ó5ÐàˆØÐÜ'Ó)ˆHÙ%Ð&7×&<Ñ&<¸RÀÇÁ×AWÑAWÓ&XÐZ`×ZeÑZeÐfhÓZiÓjˆNäØØ$Ø!×/Ñ/Ø×)Ñ)ô	
ð 	
r>   )	NNNNNNNNN)r`   ra   rb   rÉ   r   r   rP   rg   r   r   r®   r   r_   rm   r>   r=   rÔ   rÔ   ¨  s  „ à(>Ø*SñÐð
 Øð *.Ø.2Ø.2Ø-1Ø,0Ø-1Ø59Ø6:Ø&*ñ2
à—<‘< $Ñ&ð2
ð Ÿ™ tÑ+ð2
ð Ÿ™ tÑ+ð	2
ð
 —|‘| dÑ*ð2
ð —l‘l TÑ)ð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)ÚErnieForNextSentencePredictionNr?   r™   r@   rA   rB   rC   r¶   rž   rE   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-   NrG   r   rÖ   )r{   r¾   r   r¿   r   r|   r}   )r:   r?   r™   r@   rA   rB   rC   r¶   rž   rÁ   r«   Úseq_relationship_scoresrÇ   rÅ   s                 r=   r_   z&ErnieForNextSentencePrediction.forwardæ  s°   € ðZ �$—*‘*Øð	
à)Ø)Ø'Ø%Ø'Øñ	
ð ñ	
ˆð   ™
ˆà"&§(¡(¨=Ó"9Ðà!ÐØÐÜ'Ó)ˆHÙ!)Ð*A×*FÑ*FÀrÈ1Ó*MÈvÏ{É{Ð[]ËÓ!_Ðä*Ø#Ø*Ø!×/Ñ/Ø×)Ñ)ô	
ð 	
r>   ©NNNNNNN)r`   ra   rb   r   r   rP   rg   r   r   r®   r   r_   rm   r>   r=   rØ   rØ   å  sê   „ ØØð *.Ø.2Ø.2Ø-1Ø,0Ø-1Ø&*ñD
à—<‘< $Ñ&ðD
ð Ÿ™ tÑ+ðD
ð Ÿ™ tÑ+ð	D
ð
 —|‘| dÑ*ðD
ð —l‘l TÑ)ð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)ÚErnieForSequenceClassificationNr?   r™   r@   rA   rB   rC   r¶   rž   rE   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_classificationrG   rÖ   )r{   rY   Ú
classifierr;   Úproblem_typeÚ
num_labelsrJ   rP   rS   rf   r   Úsqueezer   r¿   r   r   r|   r}   )r:   r?   r™   r@   rA   rB   rC   r¶   rž   rÁ   r«   rÎ   r»   rÅ   s                 r=   r_   z&ErnieForSequenceClassification.forward0  s¬  € ð0 �$—*‘*Øð	
à)Ø)Ø'Ø%Ø'Øñ	
ð ñ	
ˆð   ™
ˆàŸ™ ]Ó3ˆØ—‘ Ó/ˆàˆØÑØ�{‰{×'Ñ'Ð/Ø—?‘? aÒ'Ø/;�D—K‘KÕ,Ø—_‘_ qÒ(¨f¯l©l¼e¿j¹jÒ.HÈFÏLÉLÔ\a×\eÑ\eÒLeØ/L�D—K‘KÕ,à/K�D—K‘KÔ,à�{‰{×'Ñ'¨<Ò7Ü"›9�Ø—?‘? aÒ'Ù# F§N¡NÓ$4°f·n±nÓ6FÓG‘Dá# F¨FÓ3‘DØ—‘×)Ñ)Ð-JÒJÜ+Ó-�Ù §¡¨B°·±Ó @À&Ç+Á+ÈbÃ/ÓR‘Ø—‘×)Ñ)Ð-IÒIÜ,Ó.�Ù ¨Ó/�ä'ØØØ!×/Ñ/Ø×)Ñ)ô	
ð 	
r>   rÛ   )r`   ra   rb   r   r   rP   rg   r   r   r®   r   r_   rm   r>   r=   rÝ   rÝ   /  sê   „ ØØð *.Ø.2Ø.2Ø-1Ø,0Ø-1Ø&*ñB
à—<‘< $Ñ&ðB
ð Ÿ™ tÑ+ðB
ð Ÿ™ tÑ+ð	B
ð
 —|‘| dÑ*ðB
ð —l‘l TÑ)ð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)ÚErnieForMultipleChoiceNr?   r™   r@   rA   rB   rC   r¶   rž   rE   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-   rG   éþÿÿÿTr¹   rÖ   )
rO   r¿   rL   r{   rY   râ   r   r   r|   r}   )r:   r?   r™   r@   rA   rB   rC   r¶   rž   Únum_choicesrÁ   r«   rÎ   Úreshaped_logitsr»   rÅ   s                   r=   r_   zErnieForMultipleChoice.forwardx  s¡  € ð` -6Ð,A�i—o‘o aÒ(À}×GZÑGZÐ[\ÑG]ˆà>GÐ>S�I—N‘N 2 y§~¡~°bÓ'9Ô:ÐY]ˆ	ØM[ÐMg˜×,Ñ,¨R°×1DÑ1DÀRÓ1HÔIÐmqˆØM[ÐMg˜×,Ñ,¨R°×1DÑ1DÀRÓ1HÔIÐmqˆØGSÐG_�|×(Ñ(¨¨\×->Ñ->¸rÓ-BÔCÐeiˆð Ð(ð ×Ñ˜r =×#5Ñ#5°bÓ#9¸=×;MÑ;MÈbÓ;QÔRàð 	ð �$—*‘*Øð	
à)Ø)Ø'Ø%Ø'Øñ	
ð ñ	
ˆð   ™
ˆàŸ™ ]Ó3ˆØ—‘ Ó/ˆØ Ÿ+™+ b¨+Ó6ˆàˆØÐÜ'Ó)ˆHÙ˜O¨VÓ4ˆDä(ØØ"Ø!×/Ñ/Ø×)Ñ)ô	
ð 	
r>   rÛ   )r`   ra   rb   r   r   rP   rg   r   r   r®   r   r_   rm   r>   r=   rç   rç   w  sê   „ ØØð *.Ø.2Ø.2Ø-1Ø,0Ø-1Ø&*ñU
à—<‘< $Ñ&ðU
ð Ÿ™ tÑ+ðU
ð Ÿ™ tÑ+ð	U
ð
 —|‘| dÑ*ðU
ð —l‘l TÑ)ð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)ÚErnieForTokenClassificationNr?   r™   r@   rA   rB   rC   r¶   rž   rE   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   NrG   rÖ   )	r{   rY   râ   r   r¿   rä   r   r|   r}   )r:   r?   r™   r@   rA   rB   rC   r¶   rž   rÁ   rª   rÎ   r»   rÅ   s                 r=   r_   z#ErnieForTokenClassification.forwardÓ  sº   € ð, �$—*‘*Øð	
à)Ø)Ø'Ø%Ø'Øñ	
ð ñ	
ˆð " !™*ˆàŸ,™, Ó7ˆØ—‘ Ó1ˆàˆØÐÜ'Ó)ˆHÙ˜FŸK™K¨¨D¯O©OÓ<¸f¿k¹kÈ"»oÓNˆDä$ØØØ!×/Ñ/Ø×)Ñ)ô	
ð 	
r>   rÛ   )r`   ra   rb   r   r   rP   rg   r   r   r®   r   r_   rm   r>   r=   rí   rí   Ò  sß   „ ØØð *.Ø.2Ø.2Ø-1Ø,0Ø-1Ø&*ñ.
à—<‘< $Ñ&ð.
ð Ÿ™ tÑ+ð.
ð Ÿ™ tÑ+ð	.
ð
 —|‘| dÑ*ð.
ð —l‘l TÑ)ð.
ð —|‘| 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)ÚErnieForQuestionAnsweringNr?   r™   r@   rA   rB   rC   Ústart_positionsÚend_positionsrž   rE   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-   rG   )rH   N)Úignore_indexr   )r»   Ústart_logitsÚ
end_logitsr|   r}   )r{   Ú
qa_outputsÚsplitrå   Ú
contiguousÚlenrL   Úclampr   r   r|   r}   )r:   r?   r™   r@   rA   rB   rC   rñ   rò   rž   rÁ   rª   rÎ   rõ   rö   rÄ   Úignored_indexrÅ   Ú
start_lossÚend_losss                       r=   r_   z!ErnieForQuestionAnswering.forward  sƒ  € ð* �$—*‘*Øð	
à)Ø)Ø'Ø%Ø'Øñ	
ð ñ	
ˆð " !™*ˆà—‘ Ó1ˆØ#)§<¡<°°r <Ó#:Ñ ˆ�jØ#×+Ñ+¨BÓ/×:Ñ:Ó<ˆØ×'Ñ'¨Ó+×6Ñ6Ó8ˆ
àˆ
ØÐ&¨=Ð+Dä�?×'Ñ'Ó)Ó*¨QÒ.Ø"1×"9Ñ"9¸"Ó"=�Ü�=×%Ñ%Ó'Ó(¨1Ò,Ø -× 5Ñ 5°bÓ 9�à(×-Ñ-¨aÓ0ˆMØ-×3Ñ3°A°}ÓEˆOØ)×/Ñ/°°=ÓAˆMä'°]ÔCˆHÙ! ,°Ó@ˆJÙ 
¨MÓ:ˆHØ$ xÑ/°1Ñ4ˆJä+ØØ%Ø!Ø!×/Ñ/Ø×)Ñ)ô
ð 	
r>   rÈ   )r`   ra   rb   r   r   rP   rg   r   r   r®   r   r_   rm   r>   r=   rð   rð     sõ   „ ØØð *.Ø.2Ø.2Ø-1Ø,0Ø-1Ø/3Ø-1ñ<
à—<‘< $Ñ&ð<
ð Ÿ™ tÑ+ð<
ð Ÿ™ tÑ+ð	<
ð
 —|‘| dÑ*ð<
ð —l‘l TÑ)ð<
ð —|‘| dÑ*ð<
ð Ÿ™¨Ñ,ð<
ð —|‘| dÑ*ð<
ð Ð+Ñ,ð<
ð 
ˆu�|‰|Ñ	Ð;Ñ	;ò<
ó ó ñ<
r>   rð   )
rË   rÔ   rç   rØ   r²   rð   rÝ   rí   r’   rz   )Nrc   rP   Útorch.nnr5   r   r   r   Ú r   r‚   Úcache_utilsr   r	   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_ernier.   Ú
get_loggerr`   Úloggerr0   rk   rp   rr   rt   rv   rx   rz   r’   r°   r²   rË   rÔ   rØ   rÝ   rç   rí   rð   Ú__all__rm   r>   r=   ú<module>r     s‚  ðñ ã Ý ß AÑ Aå &ß CÑ C÷	÷ 	ó 	õ .Ý &ß @Ñ @ß IÝ 5÷÷ ÷ ÷ õ õ& -ð 
ˆ×	Ñ	˜HÓ	%€ô=�nô =ô@	Ð*ô 	ô	Ð,ô 	ô	�ô 	ô	�*ô 	ô	Ð0ô 	ô	�;ô 	ð ô/˜?ó /ó ð/ô2[
�ô [
ô|	Ð 8ô 	ôP
Ð,ô P
ôf@
�ô @
ôF:
�ô :
ôzG
Ð%Bô G
ôTE
Ð%Bô E
ôPX
Ð2ô X
ôv1
Ð"<ô 1
ôh?
Ð 8ô ?
òD�r>   