
    fTh}1                       S r SSKrSSKrSSKJr  SSKJrJrJrJ	r	  SSK
r
SSKr
SSK
Jr  SSKJrJrJr  SSKJr  SS	KJr  SS
KJrJrJrJrJrJrJrJrJr  SSKJ r   SSK!J"r"J#r#J$r$  SSK%J&r&J'r'J(r(  SSK)J*r*  \(RV                  " \,5      r- " S S\R\                  5      r/ " S S\R\                  5      r0 " S S\R\                  5      r1S\00r2 " S S\R\                  5      r3 " S S\R\                  5      r4 " S S\R\                  5      r5 " S S\R\                  5      r6 " S S \R\                  5      r7 " S! S"\R\                  5      r8 " S# S$\R\                  5      r9 " S% S&\R\                  5      r: " S' S(\R\                  5      r; " S) S*\R\                  5      r< " S+ S,\R\                  5      r=\' " S- S.\ 5      5       r>\ " S/ S0\&5      5       r?\'" S1S29 " S3 S4\>5      5       r@\'" S5S29 " S6 S7\>5      5       rA\'" S8S29 " S9 S:\>\5      5       rB\' " S; S<\>5      5       rC\'" S=S29 " S> S?\>5      5       rD\'" S@S29 " SA SB\>5      5       rE\' " SC SD\>5      5       rF\' " SE SF\>5      5       rG\' " SG SH\>5      5       rH/ SIQrIg)JzPyTorch ERNIE model.    N)	dataclass)ListOptionalTupleUnion)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )ACT2FN)GenerationMixin)	)BaseModelOutputWithPastAndCrossAttentions,BaseModelOutputWithPoolingAndCrossAttentions!CausalLMOutputWithCrossAttentionsMaskedLMOutputMultipleChoiceModelOutputNextSentencePredictorOutputQuestionAnsweringModelOutputSequenceClassifierOutputTokenClassifierOutput)PreTrainedModel)apply_chunking_to_forward find_pruneable_heads_and_indicesprune_linear_layer)ModelOutputauto_docstringlogging   )ErnieConfigc                      ^  \ rS rSrSrU 4S jr      SS\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S	\
S
\R                  4S jjrSrU =r$ )ErnieEmbeddings1   zGConstruct the embeddings from word, position and token_type embeddings.c                   > [         TU ]  5         [        R                  " UR                  UR
                  UR                  S9U l        [        R                  " UR                  UR
                  5      U l	        [        R                  " UR                  UR
                  5      U l        UR                  U l        UR                  (       a0  [        R                  " UR                  UR
                  5      U l        [        R                  " UR
                  UR                   S9U l        [        R"                  " UR$                  5      U l        [)        USS5      U l        U R-                  S[.        R0                  " UR                  5      R3                  S5      SS9  U R-                  S	[.        R4                  " U R6                  R9                  5       [.        R:                  S
9SS9  g )N)padding_idxepsposition_embedding_typeabsoluteposition_ids)r   F)
persistenttoken_type_idsdtype)super__init__r   	Embedding
vocab_sizehidden_sizepad_token_idword_embeddingsmax_position_embeddingsposition_embeddingstype_vocab_sizetoken_type_embeddingsuse_task_idtask_type_vocab_sizetask_type_embeddings	LayerNormlayer_norm_epsDropouthidden_dropout_probdropoutgetattrr(   register_buffertorcharangeexpandzerosr*   sizelongselfconfig	__class__s     `/var/www/auris/envauris/lib/python3.13/site-packages/transformers/models/ernie/modeling_ernie.pyr1   ErnieEmbeddings.__init__4   sf   !||F,=,=v?Q?Q_e_r_rs#%<<0N0NPVPbPb#c %'\\&2H2H&J\J\%]"!--(*V5P5PRXRdRd(eD% f&8&8f>S>STzz&"<"<='.v7PR\']$ELL)G)GHOOPWXej 	 	
 	ekk$*;*;*@*@*B%**Ubg 	 	
    	input_idsr-   task_type_idsr*   inputs_embedspast_key_values_lengthreturnc                 &   Ub  UR                  5       nOUR                  5       S S nUS   nUc  U R                  S S 2XhU-   24   nUcv  [        U S5      (       a-  U R                  S S 2S U24   n	U	R	                  US   U5      n
U
nO8[
        R                  " U[
        R                  U R                  R                  S9nUc  U R                  U5      nU R                  U5      nX[-   nU R                  S:X  a  U R                  U5      nX-  nU R                  (       aP  Uc8  [
        R                  " U[
        R                  U R                  R                  S9nU R                  U5      nX-  nU R                  U5      nU R!                  U5      nU$ )Nr+   r   r-   r   r/   devicer)   )rI   r*   hasattrr-   rG   rE   rH   rJ   rY   r6   r:   r(   r8   r;   r=   r>   rB   )rL   rR   r-   rS   r*   rT   rU   input_shape
seq_lengthbuffered_token_type_ids buffered_token_type_ids_expandedr:   
embeddingsr8   r=   s                  rO   forwardErnieEmbeddings.forwardJ   s     #..*K',,.s3K ^
,,Q0FVlIl0l-lmL
 !t-..*.*=*=a*n*M'3J3Q3QR]^_R`bl3m0!A!&[

SWSdSdSkSk!l  00;M $ : :> J":
'':5"&":":<"H-J $ %KuzzRVRcRcRjRj k#'#<#<]#K .J^^J/
\\*-
rQ   )r>   rB   r(   r8   r=   r:   r;   r6   )NNNNNr   )__name__
__module____qualname____firstlineno____doc__r1   r   rE   
LongTensorFloatTensorintTensorr`   __static_attributes____classcell__rN   s   @rO   r"   r"   1   s    Q
0 1559483759&'0E,,-0 !!1!120   0 01	0
 u//00   1 120 !$0 
0 0rQ   r"   c                   b  ^  \ rS rSrSU 4S jjrS\R                  S\R                  4S jr      SS\R                  S\\R                     S\\R                     S	\\R                     S
\\R                     S\\
\
\R                           S\\   S\
\R                     4S jjrSrU =r$ )ErnieSelfAttention~   c                   > [         TU ]  5         UR                  UR                  -  S:w  a7  [	        US5      (       d&  [        SUR                   SUR                   S35      eUR                  U l        [        UR                  UR                  -  5      U l        U R                  U R                  -  U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  5      U l        U=(       d    [#        USS5      U l        U R$                  S:X  d  U R$                  S	:X  aG  UR&                  U l        [        R(                  " S
UR&                  -  S-
  U R                  5      U l        UR,                  U l        g )Nr   embedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads ()r(   r)   relative_keyrelative_key_query   r   )r0   r1   r4   num_attention_headsrZ   
ValueErrorri   attention_head_sizeall_head_sizer   Linearquerykeyvaluer@   attention_probs_dropout_probrB   rC   r(   r7   r2   distance_embedding
is_decoderrL   rM   r(   rN   s      rO   r1   ErnieSelfAttention.__init__   s    : ::a?PVXhHiHi#F$6$6#7 8 445Q8 
 $*#=#= #&v'9'9F<V<V'V#W !558P8PPYYv1143E3EF
99V//1C1CDYYv1143E3EF
zz&"E"EF'> (
'-zC
$ ''>9T=Y=Y]q=q+1+I+ID(&(ll1v7U7U3UXY3Y[_[s[s&tD# ++rQ   xrV   c                     UR                  5       S S U R                  U R                  4-   nUR                  U5      nUR	                  SSSS5      $ )Nr+   r   rv   r   r   )rI   rw   ry   viewpermute)rL   r   new_x_shapes      rO   transpose_for_scores'ErnieSelfAttention.transpose_for_scores   sL    ffhsmt'?'?AYAY&ZZFF;yyAq!$$rQ   hidden_statesattention_mask	head_maskencoder_hidden_statesencoder_attention_maskpast_key_valueoutput_attentionsc                 V   U R                  U5      nUS Ln	U	(       a  Ub  US   n
US   nUnGOU	(       aC  U R                  U R                  U5      5      n
U R                  U R                  U5      5      nUnOUbu  U R                  U R                  U5      5      n
U R                  U R                  U5      5      n[        R
                  " US   U
/SS9n
[        R
                  " US   U/SS9nO@U R                  U R                  U5      5      n
U R                  U R                  U5      5      nU R                  U5      nUS LnU R                  (       a  X4n[        R                  " XR                  SS5      5      nU R                  S:X  d  U R                  S:X  Ga  UR                  S   U
R                  S   nnU(       aB  [        R                  " US-
  [        R                  UR                  S	9R                  SS5      nO>[        R                  " U[        R                  UR                  S	9R                  SS5      n[        R                  " U[        R                  UR                  S	9R                  SS5      nUU-
  nU R!                  UU R"                  -   S-
  5      nUR%                  UR&                  S
9nU R                  S:X  a  [        R(                  " SUU5      nUU-   nOHU R                  S:X  a8  [        R(                  " SUU5      n[        R(                  " SU
U5      nUU-   U-   nU[*        R,                  " U R.                  5      -  nUb  X-   n[0        R2                  R5                  USS9nU R7                  U5      nUb  UU-  n[        R                  " UU5      nUR9                  SSSS5      R;                  5       nUR=                  5       S S U R>                  4-   nUR                  U5      nU(       a  UU4OU4nU R                  (       a  UU4-   nU$ )Nr   r   rv   dimr+   rt   ru   rX   r.   zbhld,lrd->bhlrzbhrd,lrd->bhlrr   ) r|   r   r}   r~   rE   catr   matmul	transposer(   shapetensorrJ   rY   r   rF   r   r7   tor/   einsummathsqrtry   r   
functionalsoftmaxrB   r   
contiguousrI   rz   )rL   r   r   r   r   r   r   r   mixed_query_layeris_cross_attention	key_layervalue_layerquery_layer	use_cacheattention_scoresquery_length
key_lengthposition_ids_lposition_ids_rdistancepositional_embeddingrelative_position_scoresrelative_position_scores_queryrelative_position_scores_keyattention_probscontext_layernew_context_layer_shapeoutputss                               rO   r`   ErnieSelfAttention.forward   s    !JJ}5
 3$>."<&q)I(+K3N11$((;P2QRI33DJJ?T4UVK3N'11$((=2IJI33DJJ}4MNK		>!#4i"@aHI))^A%6$D!LK11$((=2IJI33DJJ}4MNK//0AB"$.	?? (5N !<<5H5HR5PQ''>9T=Y=Y]q=q'2'8'8';Y__Q=O*L!&j1nEJJWdWkWk!l!q!q" "'l%**UbUiUi!j!o!oprtu!v"\\*EJJ}OcOcdiijkmopN%6H#'#:#:8dFbFb;bef;f#g #7#:#:ARAR#:#S ++~=+0<<8H+Wk+l(#36N#N --1EE16>NP[]q1r./4||<LiYm/n,#36T#TWs#s +dii8P8P.QQ%/@ --//0@b/I ,,7  -	9O_kB%--aAq9DDF"/"4"4"6s";t?Q?Q>S"S%**+BC6G=/2mM]?? 11GrQ   )rz   ry   r   rB   r   r}   r7   rw   r(   r|   r~   NNNNNNF)rb   rc   rd   re   r1   rE   rj   r   r   rh   r   boolr`   rk   rl   rm   s   @rO   ro   ro   ~   s    ,4%ell %u|| % 7;15=A>BDH,1c||c !!2!23c E--.	c
  ((9(9:c !)):): ;c !uU->->'?!@Ac $D>c 
u||	c crQ   ro   c                   z   ^  \ rS rSrU 4S jrS\R                  S\R                  S\R                  4S jrSrU =r	$ )ErnieSelfOutputi  c                 (  > [         TU ]  5         [        R                  " UR                  UR                  5      U l        [        R                  " UR                  UR                  S9U l        [        R                  " UR                  5      U l
        g Nr&   )r0   r1   r   r{   r4   denser>   r?   r@   rA   rB   rK   s     rO   r1   ErnieSelfOutput.__init__  s`    YYv1163E3EF
f&8&8f>S>STzz&"<"<=rQ   r   input_tensorrV   c                 p    U R                  U5      nU R                  U5      nU R                  X-   5      nU$ r   r   rB   r>   rL   r   r   s      rO   r`   ErnieSelfOutput.forward  5    

=1]3}'CDrQ   r>   r   rB   
rb   rc   rd   re   r1   rE   rj   r`   rk   rl   rm   s   @rO   r   r     6    >U\\  RWR^R^  rQ   r   eagerc                   .  ^  \ rS rSrSU 4S jjrS r      SS\R                  S\\R                     S\\R                     S\\R                     S\\R                     S	\\
\
\R                           S
\\   S\
\R                     4S jjrSrU =r$ )ErnieAttentioni  c                    > [         TU ]  5         [        UR                     " XS9U l        [        U5      U l        [        5       U l        g )Nr(   )	r0   r1   ERNIE_SELF_ATTENTION_CLASSES_attn_implementationrL   r   outputsetpruned_headsr   s      rO   r1   ErnieAttention.__init__  s@    01L1LM
	 &f-ErQ   c                 6   [        U5      S:X  a  g [        XR                  R                  U R                  R                  U R
                  5      u  p[        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l	        [        U R                  R                  USS9U R                  l        U R                  R                  [        U5      -
  U R                  l        U R                  R                  U R                  R                  -  U R                  l        U R
                  R                  U5      U l        g )Nr   r   r   )lenr   rL   rw   ry   r   r   r|   r}   r~   r   r   rz   union)rL   headsindexs      rO   prune_headsErnieAttention.prune_heads"  s   u:?79900$))2O2OQUQbQb

 -TYY__eD		*499==%@		,TYY__eD		.t{{/@/@%QO )-		(E(EE
(R		%"&))"?"?$))B_B_"_		 --33E:rQ   r   r   r   r   r   r   r   rV   c           	      p    U R                  UUUUUUU5      nU R                  US   U5      n	U	4USS  -   n
U
$ )Nr   r   )rL   r   )rL   r   r   r   r   r   r   r   self_outputsattention_outputr   s              rO   r`   ErnieAttention.forward4  sW     yy!"
  ;;|AF#%QR(88rQ   )r   r   rL   r   r   )rb   rc   rd   re   r1   r   rE   rj   r   rh   r   r   r`   rk   rl   rm   s   @rO   r   r     s    ";* 7;15=A>BDH,1|| !!2!23 E--.	
  ((9(9: !)):): ; !uU->->'?!@A $D> 
u||	 rQ   r   c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )ErnieIntermediateiM  c                   > [         TU ]  5         [        R                  " UR                  UR
                  5      U l        [        UR                  [        5      (       a  [        UR                     U l        g UR                  U l        g r   )r0   r1   r   r{   r4   intermediate_sizer   
isinstance
hidden_actstrr   intermediate_act_fnrK   s     rO   r1   ErnieIntermediate.__init__N  s`    YYv1163K3KL
f''--'-f.?.?'@D$'-'8'8D$rQ   r   rV   c                 J    U R                  U5      nU R                  U5      nU$ r   r   r   rL   r   s     rO   r`   ErnieIntermediate.forwardV  s&    

=100?rQ   r   r   rm   s   @rO   r   r   M  s(    9U\\ ell  rQ   r   c                   z   ^  \ rS rSrU 4S jrS\R                  S\R                  S\R                  4S jrSrU =r	$ )ErnieOutputi]  c                 (  > [         TU ]  5         [        R                  " UR                  UR
                  5      U l        [        R                  " UR
                  UR                  S9U l        [        R                  " UR                  5      U l        g r   )r0   r1   r   r{   r   r4   r   r>   r?   r@   rA   rB   rK   s     rO   r1   ErnieOutput.__init__^  s`    YYv779K9KL
f&8&8f>S>STzz&"<"<=rQ   r   r   rV   c                 p    U R                  U5      nU R                  U5      nU R                  X-   5      nU$ r   r   r   s      rO   r`   ErnieOutput.forwardd  r   rQ   r   r   rm   s   @rO   r   r   ]  r   rQ   r   c                   *  ^  \ rS rSrU 4S jr      SS\R                  S\\R                     S\\R                     S\\R                     S\\R                     S\\	\	\R                           S	\\
   S
\	\R                     4S jjrS rSrU =r$ )
ErnieLayeril  c                 t  > [         TU ]  5         UR                  U l        SU l        [	        U5      U l        UR                  U l        UR                  U l        U R                  (       a.  U R                  (       d  [        U  S35      e[	        USS9U l	        [        U5      U l        [        U5      U l        g )Nr   z> should be used as a decoder model if cross attention is addedr)   r   )r0   r1   chunk_size_feed_forwardseq_len_dimr   	attentionr   add_cross_attentionrx   crossattentionr   intermediater   r   rK   s     rO   r1   ErnieLayer.__init__m  s    '-'E'E$'/ ++#)#=#= ##?? D6)g!hii"0Q["\D-f5!&)rQ   r   r   r   r   r   r   r   rV   c           	         Ub  US S OS nU R                  UUUUUS9n	U	S   n
U R                  (       a  U	SS nU	S   nOU	SS  nS nU R                  (       aZ  UbW  [        U S5      (       d  [        SU  S35      eUb  US	S  OS nU R	                  U
UUUUUU5      nUS   n
XSS -   nUS   nWU-   n[        U R                  U R                  U R                  U
5      nU4U-   nU R                  (       a  UW4-   nU$ )
Nrv   )r   r   r   r   r+   r   z'If `encoder_hidden_states` are passed, z` has to be instantiated with cross-attention layers by setting `config.add_cross_attention=True`r   )	r   r   rZ   rx   r   r   feed_forward_chunkr   r   )rL   r   r   r   r   r   r   r   self_attn_past_key_valueself_attention_outputsr   r   present_key_valuecross_attn_present_key_valuecross_attn_past_key_valuecross_attention_outputslayer_outputs                    rO   r`   ErnieLayer.forward{  s}    :H9S>"1#5Y] !%/3 "0 "
 2!4 ??,Qr2G 6r :,QR0G'+$??4@4!122 =dV DD D  @N?Yrs(;_c%&*&9&9 %&)!'#  7q9" ==G ,C2+F( 14P P0##T%A%A4CSCSUe
  /G+ ??!2 44GrQ   c                 J    U R                  U5      nU R                  X!5      nU$ r   )r   r   )rL   r   intermediate_outputr  s       rO   r   ErnieLayer.feed_forward_chunk  s)    "//0@A{{#6IrQ   )r   r   r   r   r   r   r   r   r   )rb   rc   rd   re   r1   rE   rj   r   rh   r   r   r`   r   rk   rl   rm   s   @rO   r   r   l  s    *" 7;15=A>BDH,1?||? !!2!23? E--.	?
  ((9(9:? !)):): ;? !uU->->'?!@A? $D>? 
u||	?B rQ   r   c                   R  ^  \ rS rSrU 4S jr         SS\R                  S\\R                     S\\R                     S\\R                     S\\R                     S\\	\	\R                           S	\\
   S
\\
   S\\
   S\\
   S\\	\R                     \4   4S jjrSrU =r$ )ErnieEncoderi  c                    > [         TU ]  5         Xl        [        R                  " [        UR                  5       Vs/ s H  n[        U5      PM     sn5      U l        SU l	        g s  snf )NF)
r0   r1   rM   r   
ModuleListrangenum_hidden_layersr   layergradient_checkpointing)rL   rM   _rN   s      rO   r1   ErnieEncoder.__init__  sR    ]]fF^F^@_#`@_1Jv$6@_#`a
&+# $as   A&r   r   r   r   r   past_key_valuesr   r   output_hidden_statesreturn_dictrV   c                 8   U	(       a  SOS nU(       a  SOS nU(       a  U R                   R                  (       a  SOS nU R                  (       a/  U R                  (       a  U(       a  [        R                  S5        SnU(       a  SOS n[        U R                  5       H  u  nnU	(       a  X4-   nUb  X?   OS nUb  Xo   OS nU R                  (       a4  U R                  (       a#  U R                  UR                  UUUUUUU5      nOU" UUUUUUU5      nUS   nU(       a	  UUS   4-  nU(       d  M  UUS   4-   nU R                   R                  (       d  M  UUS   4-   nM     U	(       a  X4-   nU
(       d  [        S UUUUU4 5       5      $ [        UUUUUS	9$ )
N zZ`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...Fr   r+   r   rv   c              3   0   #    U  H  nUc  M  Uv   M     g 7fr   r  ).0vs     rO   	<genexpr>'ErnieEncoder.forward.<locals>.<genexpr>  s"      
A  s   	)last_hidden_stater  r   
attentionscross_attentions)rM   r   r  trainingloggerwarning_once	enumerater  _gradient_checkpointing_func__call__tupler   )rL   r   r   r   r   r   r  r   r   r  r  all_hidden_statesall_self_attentionsall_cross_attentionsnext_decoder_cacheilayer_modulelayer_head_maskr   layer_outputss                       rO   r`   ErnieEncoder.forward  s    #7BD$5b4%64;;;Z;Zr`d&&4==##p "	#,R$(4OA|#$58H$H!.7.CilO3B3N_/TXN**t}} $ A A ))!"#)*"%	! !-!"#)*"%! *!,M"}R'8&::"  &9]1=M<O&O#;;222+?=QRCSBU+U(G  5J   14D D 
 "&%'(
 
 
 9+.+*1
 	
rQ   )rM   r  r  )	NNNNNNFFT)rb   rc   rd   re   r1   rE   rj   r   rh   r   r   r   r   r`   rk   rl   rm   s   @rO   r
  r
    s   , 7;15=A>BEI$(,1/4&*S
||S
 !!2!23S
 E--.	S

  ((9(9:S
 !)):): ;S
 "%e.?.?(@"ABS
 D>S
 $D>S
 'tnS
 d^S
 
uU\\"$MM	NS
 S
rQ   r
  c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )ErniePooleri!  c                    > [         TU ]  5         [        R                  " UR                  UR                  5      U l        [        R                  " 5       U l        g r   )r0   r1   r   r{   r4   r   Tanh
activationrK   s     rO   r1   ErniePooler.__init__"  s9    YYv1163E3EF
'')rQ   r   rV   c                 \    US S 2S4   nU R                  U5      nU R                  U5      nU$ )Nr   )r   r4  )rL   r   first_token_tensorpooled_outputs       rO   r`   ErniePooler.forward'  s6     +1a40

#566rQ   )r4  r   r   rm   s   @rO   r1  r1  !  s(    $
U\\ ell  rQ   r1  c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )ErniePredictionHeadTransformi1  c                 p  > [         TU ]  5         [        R                  " UR                  UR                  5      U l        [        UR                  [        5      (       a  [        UR                     U l
        OUR                  U l
        [        R                  " UR                  UR                  S9U l        g r   )r0   r1   r   r{   r4   r   r   r   r   r   transform_act_fnr>   r?   rK   s     rO   r1   %ErniePredictionHeadTransform.__init__2  s~    YYv1163E3EF
f''--$*6+<+<$=D!$*$5$5D!f&8&8f>S>STrQ   r   rV   c                 l    U R                  U5      nU R                  U5      nU R                  U5      nU$ r   )r   r=  r>   r   s     rO   r`   $ErniePredictionHeadTransform.forward;  s4    

=1--m<}5rQ   )r>   r   r=  r   rm   s   @rO   r;  r;  1  s)    UU\\ ell  rQ   r;  c                   4   ^  \ rS rSrU 4S jrS rS rSrU =r$ )ErnieLMPredictionHeadiC  c                 H  > [         TU ]  5         [        U5      U l        [        R
                  " UR                  UR                  SS9U l        [        R                  " [        R                  " UR                  5      5      U l        U R                  U R                  l        g )NF)bias)r0   r1   r;  	transformr   r{   r4   r3   decoder	ParameterrE   rH   rD  rK   s     rO   r1   ErnieLMPredictionHead.__init__D  sm    5f= yy!3!3V5F5FUSLLV->->!?@	 !IIrQ   c                 :    U R                   U R                  l         g r   )rD  rF  rL   s    rO   _tie_weights"ErnieLMPredictionHead._tie_weightsQ  s     IIrQ   c                 J    U R                  U5      nU R                  U5      nU$ r   )rE  rF  r   s     rO   r`   ErnieLMPredictionHead.forwardT  s$    }5]3rQ   )rD  rF  rE  )	rb   rc   rd   re   r1   rK  r`   rk   rl   rm   s   @rO   rB  rB  C  s    && rQ   rB  c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )ErnieOnlyMLMHeadi[  c                 B   > [         TU ]  5         [        U5      U l        g r   )r0   r1   rB  predictionsrK   s     rO   r1   ErnieOnlyMLMHead.__init__\  s    08rQ   sequence_outputrV   c                 (    U R                  U5      nU$ r   rR  )rL   rT  prediction_scoress      rO   r`   ErnieOnlyMLMHead.forward`  s     ,,_=  rQ   rV  r   rm   s   @rO   rP  rP  [  s(    9!u|| ! ! !rQ   rP  c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )ErnieOnlyNSPHeadif  c                 n   > [         TU ]  5         [        R                  " UR                  S5      U l        g Nrv   )r0   r1   r   r{   r4   seq_relationshiprK   s     rO   r1   ErnieOnlyNSPHead.__init__g  s'     "		&*<*<a @rQ   c                 (    U R                  U5      nU$ r   r]  )rL   r8  seq_relationship_scores      rO   r`   ErnieOnlyNSPHead.forwardk  s    !%!6!6}!E%%rQ   r`  rb   rc   rd   re   r1   r`   rk   rl   rm   s   @rO   rZ  rZ  f  s    A& &rQ   rZ  c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )ErniePreTrainingHeadsiq  c                    > [         TU ]  5         [        U5      U l        [        R
                  " UR                  S5      U l        g r\  )r0   r1   rB  rR  r   r{   r4   r]  rK   s     rO   r1   ErniePreTrainingHeads.__init__r  s4    08 "		&*<*<a @rQ   c                 L    U R                  U5      nU R                  U5      nX44$ r   rR  r]  )rL   rT  r8  rW  ra  s        rO   r`   ErniePreTrainingHeads.forwardw  s-     ,,_=!%!6!6}!E 88rQ   ri  rc  rm   s   @rO   re  re  q  s    A
9 9rQ   re  c                   &    \ rS rSr\rSrSrS rSr	g)ErniePreTrainedModeli}  ernieTc                    [        U[        R                  5      (       ak  UR                  R                  R                  SU R                  R                  S9  UR                  b%  UR                  R                  R                  5         gg[        U[        R                  5      (       ax  UR                  R                  R                  SU R                  R                  S9  UR                  b2  UR                  R                  UR                     R                  5         gg[        U[        R                  5      (       aJ  UR                  R                  R                  5         UR                  R                  R                  S5        gg)zInitialize the weightsg        )meanstdNg      ?)r   r   r{   weightdatanormal_rM   initializer_rangerD  zero_r2   r%   r>   fill_)rL   modules     rO   _init_weights"ErniePreTrainedModel._init_weights  s   fbii(( MM&&CT[[5R5R&S{{&  &&( '--MM&&CT[[5R5R&S!!-""6#5#56<<> .--KK""$MM$$S) .rQ   r  N)
rb   rc   rd   re   r    config_classbase_model_prefixsupports_gradient_checkpointingrx  rk   r  rQ   rO   rl  rl  }  s    L&*#*rQ   rl  c                       \ rS rSr% SrSr\\R                     \	S'   Sr
\\R                     \	S'   Sr\\R                     \	S'   Sr\\\R                        \	S'   Sr\\\R                        \	S'   S	rg)
ErnieForPreTrainingOutputi  a  
Output type of [`ErnieForPreTraining`].

Args:
    loss (*optional*, returned when `labels` is provided, `torch.FloatTensor` of shape `(1,)`):
        Total loss as the sum of the masked language modeling loss and the next sequence prediction
        (classification) loss.
    prediction_logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
        Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
    seq_relationship_logits (`torch.FloatTensor` of shape `(batch_size, 2)`):
        Prediction scores of the next sequence prediction (classification) head (scores of True/False continuation
        before SoftMax).
    hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) of
        shape `(batch_size, sequence_length, hidden_size)`.

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

        Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
        heads.
Nlossprediction_logitsseq_relationship_logitsr   r  r  )rb   rc   rd   re   rf   r  r   rE   rh   __annotations__r  r  r   r   r  rk   r  rQ   rO   r~  r~    s~    2 )-D(5$$
%,59x 1 129;?Xe&7&78?8<M8E%"3"345<59Ju00129rQ   r~  a  
    The model can behave as an encoder (with only self-attention) as well as a decoder, in which case a layer of
    cross-attention is added between the self-attention layers, following the architecture described in [Attention is
    all you need](https://arxiv.org/abs/1706.03762) by Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit,
    Llion Jones, Aidan N. Gomez, Lukasz Kaiser and Illia Polosukhin.

    To behave as an decoder the model needs to be initialized with the `is_decoder` argument of the configuration set
    to `True`. To be used in a Seq2Seq model, the model needs to initialized with both `is_decoder` argument and
    )custom_introc            "         ^  \ rS rSrSU 4S jjrS rS rS r\              SS\	\
R                     S\	\
R                     S\	\
R                     S	\	\
R                     S
\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\\
R                        S\	\   S\	\   S\	\   S\	\   S\\\
R                     \4   4S jj5       rSrU =r$ )
ErnieModeli  c                    > [         TU ]  U5        Xl        [        U5      U l        [        U5      U l        U(       a  [        U5      OSU l        U R                  5         g)z^
add_pooling_layer (bool, *optional*, defaults to `True`):
    Whether to add a pooling layer
N)
r0   r1   rM   r"   r_   r
  encoderr1  pooler	post_init)rL   rM   add_pooling_layerrN   s      rO   r1   ErnieModel.__init__  sK    
 	 )&1#F+->k&)D 	rQ   c                 .    U R                   R                  $ r   r_   r6   rJ  s    rO   get_input_embeddingsErnieModel.get_input_embeddings  s    ...rQ   c                 $    XR                   l        g r   r  )rL   r~   s     rO   set_input_embeddingsErnieModel.set_input_embeddings  s    */'rQ   c                     UR                  5        H7  u  p#U R                  R                  U   R                  R	                  U5        M9     g)z
Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
class PreTrainedModel
N)itemsr  r  r   r   )rL   heads_to_pruner  r   s       rO   _prune_headsErnieModel._prune_heads  s<    
 +002LELLu%//;;EB 3rQ   rR   r   r-   rS   r*   r   rT   r   r   r  r   r   r  r  rV   c                    Ub  UOU R                   R                  nUb  UOU R                   R                  nUb  UOU R                   R                  nU R                   R                  (       a  Ub  UOU R                   R
                  nOSnUb  Ub  [        S5      eUb"  U R                  X5        UR                  5       nO"Ub  UR                  5       SS nO[        S5      eUu  nnUb  UR                  OUR                  nU
b  U
S   S   R                  S   OSnUc  [        R                  " UUU-   4US9nUcs  [        U R                  S	5      (       a4  U R                  R                  SS2SU24   nUR!                  UU5      nUnO$[        R"                  " U[        R$                  US
9nU R'                  X/5      nU R                   R                  (       aE  UbB  UR                  5       u  nnnUU4nU	c  [        R                  " UUS9n	U R)                  U	5      nOSnU R+                  X`R                   R,                  5      nU R                  UUUUUUS9nU R/                  UUUUUU
UUUUS9
nUS   nU R0                  b  U R1                  U5      OSnU(       d
  UU4USS -   $ [3        UUUR4                  UR6                  UR8                  UR:                  S9$ )  
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]
NFzDYou cannot specify both input_ids and inputs_embeds at the same timer+   z5You have to specify either input_ids or inputs_embedsr   rv   )rY   r-   rX   )rR   r*   r-   rS   rT   rU   )	r   r   r   r   r  r   r   r  r  r   )r  pooler_outputr  r   r  r  )rM   r   r  use_return_dictr   r   rx   %warn_if_padding_and_no_attention_maskrI   rY   r   rE   onesrZ   r_   r-   rG   rH   rJ   get_extended_attention_maskinvert_attention_maskget_head_maskr  r  r  r   r  r   r  r  ) rL   rR   r   r-   rS   r*   r   rT   r   r   r  r   r   r  r  r[   
batch_sizer\   rY   rU   r]   r^   extended_attention_maskencoder_batch_sizeencoder_sequence_lengthr  encoder_hidden_shapeencoder_extended_attention_maskembedding_outputencoder_outputsrT  r8  s                                    rO   r`   ErnieModel.forward  s   2 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B];;!!%.%:	@U@UII ]%>cdd"66yQ#..*K&',,.s3KTUU!,
J%.%:!!@T@T DSC^!3A!6!<!<Q!?de!"ZZ*jCY6Y)ZdjkN!t(899*.//*H*HKZK*X'3J3Q3QR\^h3i0!A!&[

SY!Z 150P0PQ_0m ;;!!&;&G=R=W=W=Y: 7$68O#P %-).4HQW)X&.2.H.HI_.`+.2+ &&y++2O2OP	??%)''#9 + 
 ,,2"7#B+/!5# ' 
 *!,8<8OO4UY#]3oab6III;-'+;;)77&11,==
 	
rQ   )rM   r_   r  r  )T)NNNNNNNNNNNNNN)rb   rc   rd   re   r1   r  r  r  r   r   rE   rj   r   rh   r   r   r   r   r`   rk   rl   rm   s   @rO   r  r    s   "/0C  -1151504/3,0048<9==A$(,0/3&*u
ELL)u
 !.u
 !.	u

  -u
 u||,u
 ELL)u
  -u
  (5u
 !) 6u
 "$u'8'8"9:u
 D>u
 $D>u
 'tnu
 d^u
  
uU\\"$PP	Q!u
 u
rQ   r  z
    Ernie Model with two heads on top as done during the pretraining: a `masked language modeling` head and a `next
    sentence prediction (classification)` head.
    c                     ^  \ rS rSrSS/rU 4S jrS rS r\            SS\	\
R                     S\	\
R                     S	\	\
R                     S
\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\   S\	\   S\	\   S\\\
R                     \4   4S jj5       rSrU =r$ )ErnieForPreTrainingi^  cls.predictions.decoder.biascls.predictions.decoder.weightc                    > [         TU ]  U5        [        U5      U l        [	        U5      U l        U R                  5         g r   )r0   r1   r  rm  re  clsr  rK   s     rO   r1   ErnieForPreTraining.__init__h  s4     '
(0 	rQ   c                 B    U R                   R                  R                  $ r   r  rR  rF  rJ  s    rO   get_output_embeddings)ErnieForPreTraining.get_output_embeddingsr      xx##+++rQ   c                     XR                   R                  l        UR                  U R                   R                  l        g r   r  rR  rF  rD  rL   new_embeddingss     rO   set_output_embeddings)ErnieForPreTraining.set_output_embeddingsv  *    '5$$2$7$7!rQ   rR   r   r-   rS   r*   r   rT   labelsnext_sentence_labelr   r  r  rV   c                    Ub  UOU R                   R                  nU R                  UUUUUUUU
UUS9
nUSS u  pU R                  X5      u  nnSnUbv  U	bs  [	        5       nU" UR                  SU R                   R                  5      UR                  S5      5      nU" UR                  SS5      U	R                  S5      5      nUU-   nU(       d  UU4USS -   nUb  U4U-   $ U$ [        UUUUR                  UR                  S9$ )aj  
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
```
N	r   r-   rS   r*   r   rT   r   r  r  rv   r+   )r  r  r  r   r  )
rM   r  rm  r  r
   r   r3   r~  r   r  )rL   rR   r   r-   rS   r*   r   rT   r  r  r   r  r  r   rT  r8  rW  ra  
total_lossloss_fctmasked_lm_lossnext_sentence_lossr   s                          rO   r`   ErnieForPreTraining.forwardz  sF   b &1%<k$++B]B]**))'%'/!5#  
 *1!&48HH_4\11
"5"A')H%&7&<&<RAWAW&XZ`ZeZefhZijN!)*@*E*Eb!*LNaNfNfgiNj!k'*<<J')?@712;NF/9/EZMF*Q6Q(/$:!//))
 	
rQ   r  rm  NNNNNNNNNNNN)rb   rc   rd   re   _tied_weights_keysr1   r  r  r   r   rE   rj   r   r   r   r~  r`   rk   rl   rm   s   @rO   r  r  ^  sZ    9:Z[,8  -1151504/3,004)-6:,0/3&*S
ELL)S
 !.S
 !.	S

  -S
 u||,S
 ELL)S
  -S
 &S
 &ell3S
 $D>S
 'tnS
 d^S
 
uU\\"$==	>S
 S
rQ   r  zQ
    Ernie Model with a `language modeling` head on top for CLM fine-tuning.
    c            $         ^  \ rS rSrSS/rU 4S jrS rS r\               SS\	\
R                     S\	\
R                     S	\	\
R                     S
\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\\
R                        S\	\   S\	\   S\	\   S\	\   S\\\
R                     \4   4 S jj5       rS rSrU =r$ )ErnieForCausalLMi  r  r  c                    > [         TU ]  U5        UR                  (       d  [        R	                  S5        [        USS9U l        [        U5      U l        U R                  5         g )NzMIf you want to use `ErnieForCausalLM` as a standalone, add `is_decoder=True.`Fr  
r0   r1   r   r!  warningr  rm  rP  r  r  rK   s     rO   r1   ErnieForCausalLM.__init__  sL       NNjk%@
#F+ 	rQ   c                 B    U R                   R                  R                  $ r   r  rJ  s    rO   r  &ErnieForCausalLM.get_output_embeddings  r  rQ   c                     XR                   R                  l        UR                  U R                   R                  l        g r   r  r  s     rO   r  &ErnieForCausalLM.set_output_embeddings  r  rQ   rR   r   r-   rS   r*   r   rT   r   r   r  r  r   r   r  r  rV   c                    Ub  UOU R                   R                  nU
b  SnU R                  UUUUUUUUU	UUUUUS9nUS   nU R                  U5      nSnU
b*  U R                  " UU
4SU R                   R
                  0UD6nU(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  UR                  UR                  S9$ )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]`
NF)r   r-   rS   r*   r   rT   r   r   r  r   r   r  r  r   r3   rv   )r  logitsr  r   r  r  )rM   r  rm  r  loss_functionr3   r   r  r   r  r  )rL   rR   r   r-   rS   r*   r   rT   r   r   r  r  r   r   r  r  kwargsr   rT  rW  lm_lossr   s                         rO   r`   ErnieForCausalLM.forward  s!   > &1%<k$++B]B]I**))'%'"7#9+/!5#  
" "!* HH_5((!  ;;11 	G ')GABK7F,3,?WJ'KVK0$#33!//))$55
 	
rQ   c                 P   ^ SnU H  nU[        U4S jU 5       5      4-  nM     U$ )Nr  c              3   x   >#    U  H/  oR                  S TR                  UR                  5      5      v   M1     g7f)r   N)index_selectr   rY   )r  
past_statebeam_idxs     rO   r  2ErnieForCausalLM._reorder_cache.<locals>.<genexpr>A  s1     ncmU_--aZ=N=N1OPPcms   7:)r&  )rL   r  r  reordered_past
layer_pasts     `  rO   _reorder_cacheErnieForCausalLM._reorder_cache=  s8    )Jncmnn N * rQ   r  )NNNNNNNNNNNNNNN)rb   rc   rd   re   r  r1   r  r  r   r   rE   rj   r   r   r   r   r   r`   r  rk   rl   rm   s   @rO   r  r    s    9:Z[
,8  -1151504/3,0048<9=)-8<$(,0/3&*!J
ELL)J
 !.J
 !.	J

  -J
 u||,J
 ELL)J
  -J
  (5J
 !) 6J
 &J
 "$u||"45J
 D>J
 $D>J
 'tnJ
  d^!J
$ 
uU\\"$EE	F%J
 J
Z rQ   r  c                      ^  \ rS rSrSS/rU 4S jrS rS r\             SS\	\
R                     S\	\
R                     S	\	\
R                     S
\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\   S\	\   S\	\   S\\\
R                     \4   4S jj5       rSS jr\S\4S j5       rSrU =r$ )ErnieForMaskedLMiF  r  r  c                    > [         TU ]  U5        UR                  (       a  [        R	                  S5        [        USS9U l        [        U5      U l        U R                  5         g )NzlIf you want to use `ErnieForMaskedLM` make sure `config.is_decoder=False` for bi-directional self-attention.Fr  r  rK   s     rO   r1   ErnieForMaskedLM.__init__K  sR     NN1
  %@
#F+ 	rQ   c                 B    U R                   R                  R                  $ r   r  rJ  s    rO   r  &ErnieForMaskedLM.get_output_embeddings[  r  rQ   c                     XR                   R                  l        UR                  U R                   R                  l        g r   r  r  s     rO   r  &ErnieForMaskedLM.set_output_embeddings_  r  rQ   rR   r   r-   rS   r*   r   rT   r   r   r  r   r  r  rV   c                    Ub  UOU R                   R                  nU R                  UUUUUUUUU	UUUS9nUS   nU R                  U5      nSnU
bF  [	        5       nU" UR                  SU R                   R                  5      U
R                  S5      5      nU(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  S9$ )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]`
N)r   r-   rS   r*   r   rT   r   r   r   r  r  r   r+   rv   r  r  r   r  )
rM   r  rm  r  r
   r   r3   r   r   r  )rL   rR   r   r-   rS   r*   r   rT   r   r   r  r   r  r  r   rT  rW  r  r  r   s                       rO   r`   ErnieForMaskedLM.forwardc  s   : &1%<k$++B]B]**))'%'"7#9/!5#  
 "!* HH_5')H%&7&<&<RAWAW&XZ`ZeZefhZijN')GABK7F3A3M^%.YSYY$!//))	
 	
rQ   c                    UR                   nUS   nU R                  R                  c  [        S5      e[        R
                  " X"R                  UR                   S   S45      /SS9n[        R                  " US4U R                  R                  [        R                  UR                  S9n[        R
                  " X/SS9nXS.$ )Nr   z.The PAD token should be defined for generationr   r+   r   rX   )rR   r   )
r   rM   r5   rx   rE   r   	new_zerosfullrJ   rY   )rL   rR   r   model_kwargsr[   effective_batch_sizedummy_tokens          rO   prepare_inputs_for_generation.ErnieForMaskedLM.prepare_inputs_for_generation  s    oo*1~ ;;##+MNNN4L4LnNbNbcdNeghMi4j#kqstjj!1%t{{'?'?uzzZcZjZj
 IIy6A>	&IIrQ   c                     g)z
Legacy correction: ErnieForMaskedLM can't call `generate()` from `GenerationMixin`, even though it has a
`prepare_inputs_for_generation` method.
Fr  )r  s    rO   can_generateErnieForMaskedLM.can_generate  s     rQ   r  )NNNNNNNNNNNNNr   )rb   rc   rd   re   r  r1   r  r  r   r   rE   rj   r   r   r   r   r`   r  classmethodr  rk   rl   rm   s   @rO   r  r  F  s   8:Z[ ,8  -1151504/3,0048<9=)-,0/3&*>
ELL)>
 !.>
 !.	>

  ->
 u||,>
 ELL)>
  ->
  (5>
 !) 6>
 &>
 $D>>
 'tn>
 d^>
 
uU\\"N2	3>
 >
BJ  T  rQ   r  zU
    Ernie Model with a `next sentence prediction (classification)` head on top.
    c                     ^  \ rS rSrU 4S jr\           SS\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S	\\R                     S
\\R                     S\\	   S\\	   S\\	   S\
\\R                     \4   4S jj5       rSrU =r$ )ErnieForNextSentencePredictioni  c                    > [         TU ]  U5        [        U5      U l        [	        U5      U l        U R                  5         g r   )r0   r1   r  rm  rZ  r  r  rK   s     rO   r1   'ErnieForNextSentencePrediction.__init__  s4     '
#F+ 	rQ   rR   r   r-   rS   r*   r   rT   r  r   r  r  rV   c                    SU;   a,  [         R                  " S[        5        UR                  S5      nUb  UOU R                  R
                  nU R                  UUUUUUUU	U
US9
nUS   nU R                  U5      nSnUb2  [        5       nU" UR                  SS5      UR                  S5      5      nU(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  S9$ )	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
```
r  zoThe `next_sentence_label` argument is deprecated and will be removed in a future version, use `labels` instead.Nr  r   r+   rv   r  )warningswarnFutureWarningpoprM   r  rm  r  r
   r   r   r   r  )rL   rR   r   r-   rS   r*   r   rT   r  r   r  r  r  r   r8  seq_relationship_scoresr  r  r   s                      rO   r`   &ErnieForNextSentencePrediction.forward  s    ` !F*MM%
 ZZ 56F%0%<k$++B]B]**))'%'/!5#  
  
"&((="9!')H!)*A*F*Fr1*Mv{{[]!_-/'!"+=F7I7U')F2a[aa*#*!//))	
 	
rQ   r  NNNNNNNNNNN)rb   rc   rd   re   r1   r   r   rE   rj   r   r   r   r   r`   rk   rl   rm   s   @rO   r  r    s+     -1151504/3,004)-,0/3&*X
ELL)X
 !.X
 !.	X

  -X
 u||,X
 ELL)X
  -X
 &X
 $D>X
 'tnX
 d^X
 
uU\\"$??	@X
 X
rQ   r  z
    Ernie Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled
    output) e.g. for GLUE tasks.
    c                     ^  \ rS rSrU 4S jr\           SS\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S	\\R                     S
\\R                     S\\	   S\\	   S\\	   S\
\\R                     \4   4S jj5       rSrU =r$ )ErnieForSequenceClassificationi*  c                 r  > [         TU ]  U5        UR                  U l        Xl        [	        U5      U l        UR                  b  UR                  OUR                  n[        R                  " U5      U l
        [        R                  " UR                  UR                  5      U l        U R                  5         g r   )r0   r1   
num_labelsrM   r  rm  classifier_dropoutrA   r   r@   rB   r{   r4   
classifierr  rL   rM   r  rN   s      rO   r1   'ErnieForSequenceClassification.__init__2  s      ++'
)/)B)B)NF%%TZTnTn 	 zz"45))F$6$68I8IJ 	rQ   rR   r   r-   rS   r*   r   rT   r  r   r  r  rV   c                 T   Ub  UOU R                   R                  nU R                  UUUUUUUU	U
US9
nUS   nU R                  U5      nU R	                  U5      nSnUGb  U R                   R
                  c  U R                  S:X  a  SU R                   l        OoU R                  S:  aN  UR                  [        R                  :X  d  UR                  [        R                  :X  a  SU R                   l        OSU R                   l        U R                   R
                  S:X  aI  [        5       nU R                  S:X  a&  U" UR                  5       UR                  5       5      nOU" X5      nOU R                   R
                  S:X  a=  [        5       nU" UR                  SU R                  5      UR                  S5      5      nO,U R                   R
                  S:X  a  [        5       nU" X5      nU(       d  U4USS -   nUb  U4U-   $ U$ [!        UUUR"                  UR$                  S	9$ )
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).
Nr  r   
regressionsingle_label_classificationmulti_label_classificationr+   rv   r  )rM   r  rm  rB   r  problem_typer
  r/   rE   rJ   ri   r   squeezer
   r   r	   r   r   r  )rL   rR   r   r-   rS   r*   r   rT   r  r   r  r  r   r8  r  r  r  r   s                     rO   r`   &ErnieForSequenceClassification.forwardA  s   4 &1%<k$++B]B]**))'%'/!5#  
  
]3/{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#FNN$4fnn6FGD#F3D))-JJ+-B @&++b/R))-II,./Y,F)-)9TGf$EvE'!//))	
 	
rQ   )r  rM   rB   rm  r
  r  )rb   rc   rd   re   r1   r   r   rE   rj   r   r   r   r   r`   rk   rl   rm   s   @rO   r  r  *  s+     -1151504/3,004)-,0/3&*L
ELL)L
 !.L
 !.	L

  -L
 u||,L
 ELL)L
  -L
 &L
 $D>L
 'tnL
 d^L
 
uU\\"$<<	=L
 L
rQ   r  c                     ^  \ rS rSrU 4S jr\           SS\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S	\\R                     S
\\R                     S\\	   S\\	   S\\	   S\
\\R                     \4   4S jj5       rSrU =r$ )ErnieForMultipleChoicei  c                 0  > [         TU ]  U5        [        U5      U l        UR                  b  UR                  OUR
                  n[        R                  " U5      U l        [        R                  " UR                  S5      U l        U R                  5         g )Nr   )r0   r1   r  rm  r  rA   r   r@   rB   r{   r4   r  r  r  s      rO   r1   ErnieForMultipleChoice.__init__  su     '
)/)B)B)NF%%TZTnTn 	 zz"45))F$6$6: 	rQ   rR   r   r-   rS   r*   r   rT   r  r   r  r  rV   c                 ^   Ub  UOU R                   R                  nUb  UR                  S   OUR                  S   nUb!  UR                  SUR	                  S5      5      OSnUb!  UR                  SUR	                  S5      5      OSnUb!  UR                  SUR	                  S5      5      OSnUb!  UR                  SUR	                  S5      5      OSnUb1  UR                  SUR	                  S5      UR	                  S5      5      OSnU R                  UUUUUUUU	U
US9
nUS   nU R                  U5      nU R                  U5      nUR                  SU5      nSnUb  [        5       nU" UU5      nU(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  S9$ )aQ  
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   r+   r   r  rv   r  )rM   r  r   r   rI   rm  rB   r  r
   r   r   r  )rL   rR   r   r-   rS   r*   r   rT   r  r   r  r  num_choicesr   r8  r  reshaped_logitsr  r  r   s                       rO   r`   ErnieForMultipleChoice.forward  s   d &1%<k$++B]B],5,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 	 **))'%'/!5#  
  
]3/ ++b+6')HOV4D%''!"+5F)-)9TGf$EvE("!//))	
 	
rQ   )r  rB   rm  r  )rb   rc   rd   re   r1   r   r   rE   rj   r   r   r   r   r`   rk   rl   rm   s   @rO   r  r    s+     -1151504/3,004)-,0/3&*_
ELL)_
 !._
 !.	_

  -_
 u||,_
 ELL)_
  -_
 &_
 $D>_
 'tn_
 d^_
 
uU\\"$==	>_
 _
rQ   r  c                     ^  \ rS rSrU 4S jr\           SS\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S	\\R                     S
\\R                     S\\	   S\\	   S\\	   S\
\\R                     \4   4S jj5       rSrU =r$ )ErnieForTokenClassificationi  c                 d  > [         TU ]  U5        UR                  U l        [        USS9U l        UR
                  b  UR
                  OUR                  n[        R                  " U5      U l	        [        R                  " UR                  UR                  5      U l        U R                  5         g NFr  )r0   r1   r
  r  rm  r  rA   r   r@   rB   r{   r4   r  r  r  s      rO   r1   $ErnieForTokenClassification.__init__  s      ++%@
)/)B)B)NF%%TZTnTn 	 zz"45))F$6$68I8IJ 	rQ   rR   r   r-   rS   r*   r   rT   r  r   r  r  rV   c                    Ub  UOU R                   R                  nU R                  UUUUUUUU	U
US9
nUS   nU R                  U5      nU R	                  U5      nSnUb<  [        5       nU" UR                  SU R                  5      UR                  S5      5      nU(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  S9$ )al  
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]`.
Nr  r   r+   rv   r  )rM   r  rm  rB   r  r
   r   r
  r   r   r  )rL   rR   r   r-   rS   r*   r   rT   r  r   r  r  r   rT  r  r  r  r   s                     rO   r`   #ErnieForTokenClassification.forward  s    0 &1%<k$++B]B]**))'%'/!5#  
 "!*,,71')HFKKDOO<fkk"oNDY,F)-)9TGf$EvE$!//))	
 	
rQ   )r  rB   rm  r
  r  )rb   rc   rd   re   r1   r   r   rE   rj   r   r   r   r   r`   rk   rl   rm   s   @rO   r  r    s     -1151504/3,004)-,0/3&*9
ELL)9
 !.9
 !.	9

  -9
 u||,9
 ELL)9
  -9
 &9
 $D>9
 'tn9
 d^9
 
uU\\"$99	:9
 9
rQ   r  c                     ^  \ rS rSrU 4S jr\            SS\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S	\\R                     S
\\R                     S\\R                     S\\	   S\\	   S\\	   S\
\\R                     \4   4S jj5       rSrU =r$ )ErnieForQuestionAnsweringiR  c                    > [         TU ]  U5        UR                  U l        [        USS9U l        [
        R                  " UR                  UR                  5      U l        U R                  5         g r!  )
r0   r1   r
  r  rm  r   r{   r4   
qa_outputsr  rK   s     rO   r1   "ErnieForQuestionAnswering.__init__U  sU      ++%@
))F$6$68I8IJ 	rQ   rR   r   r-   rS   r*   r   rT   start_positionsend_positionsr   r  r  rV   c                 (   Ub  UOU R                   R                  nU R                  UUUUUUUU
UUS9
nUS   nU R                  U5      nUR	                  SSS9u  nnUR                  S5      R                  5       nUR                  S5      R                  5       nSnUb  U	b  [        UR                  5       5      S:  a  UR                  S5      n[        U	R                  5       5      S:  a  U	R                  S5      n	UR                  S5      nUR                  SU5      nU	R                  SU5      n	[        US9nU" UU5      nU" UU	5      nUU-   S-  nU(       d  UU4USS -   nUb  U4U-   $ U$ [        UUUUR                  UR                  S	9$ )
r  Nr  r   r   r+   r   )ignore_indexrv   )r  start_logits
end_logitsr   r  )rM   r  rm  r(  splitr  r   r   rI   clampr
   r   r   r  )rL   rR   r   r-   rS   r*   r   rT   r*  r+  r   r  r  r   rT  r  r.  r/  r  ignored_indexr  
start_lossend_lossr   s                           rO   r`   !ErnieForQuestionAnswering.forward_  s   . &1%<k$++B]B]**))'%'/!5#  
 "!*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"J/'!"+=F/9/EZMF*Q6Q+%!!//))
 	
rQ   )rm  r
  r(  r  )rb   rc   rd   re   r1   r   r   rE   rj   r   r   r   r   r`   rk   rl   rm   s   @rO   r&  r&  R  sB     -1151504/3,0042604,0/3&*G
ELL)G
 !.G
 !.	G

  -G
 u||,G
 ELL)G
  -G
 "%,,/G
  -G
 $D>G
 'tnG
 d^G
 
uU\\"$@@	AG
 G
rQ   r&  )
r  r  r  r  r  r&  r  r  r  rl  )Jrf   r   r   dataclassesr   typingr   r   r   r   rE   torch.utils.checkpointr   torch.nnr	   r
   r   activationsr   
generationr   modeling_outputsr   r   r   r   r   r   r   r   r   modeling_utilsr   pytorch_utilsr   r   r   utilsr   r   r   configuration_ernier    
get_loggerrb   r!  Moduler"   ro   r   r   r   r   r   r   r
  r1  r;  rB  rP  rZ  re  rl  r~  r  r  r  r  r  r  r  r  r&  __all__r  rQ   rO   <module>rD     s      ! / /    A A ! )
 
 
 . l l 9 9 , 
		H	%Ibii IZC CNbii    0RYY 0h		  ")) S SnZ
299 Z
|"))  299 $BII 0!ryy !&ryy &	9BII 	9 *? * *, : : :B 
Y
% Y

Y
x j
. j
j
Z 
m+_ m
m` t+ t tn 
d
%9 d

d
N ^
%9 ^
^
B o
1 o
 o
d J
"6 J
 J
Z T
 4 T
 T
nrQ   