
    Z j                        S SK Jr  S SKJ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  SS	KJr  SS
KJr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%J&r&  SSK'J(r(  SSK)J*r*  SSK+J,r,J-r-J.r.J/r/  SSK0J1r1J2r2  SSK3J4r4  SSK5J6r6  \/Rn                  " \85      r9 " S S\Rt                  5      r;  SZS\Rt                  S\Rx                  S\Rx                  S\Rx                  S\Rx                  S-  S\=S-  S\=S\(\-   4S jjr> " S  S!\Rt                  5      r? " S" S#\Rt                  5      r@ " S$ S%\Rt                  5      rA " S& S'\Rt                  5      rB " S( S)\Rt                  5      rC " S* S+\Rt                  5      rD " S, S-\5      rE " S. S/\Rt                  5      rF " S0 S1\Rt                  5      rG " S2 S3\Rt                  5      rH " S4 S5\Rt                  5      rI\. " S6 S7\&5      5       rJ\." S8S99 " S: S;\J5      5       rK\." S<S99\ " S= S>\,5      5       5       rL " S? S@\Rt                  5      rM\." SAS99 " SB SC\J5      5       rN " SD SE\Rt                  5      rO\." SFS99 " SG SH\J\5      5       rP\. " SI SJ\J5      5       rQ " SK SL\Rt                  5      rR\." SMS99 " SN SO\J5      5       rS\." SPS99 " SQ SR\J5      5       rT\. " SS ST\J5      5       rU\. " SU SV\J5      5       rV\. " SW SX\J5      5       rW/ SYQrXg)[    )Callable)	dataclassN)BCEWithLogitsLossCrossEntropyLossMSELoss   )initialization)ACT2FN)CacheDynamicCacheEncoderDecoderCache)GenerationMixin)create_bidirectional_maskcreate_causal_mask)GradientCheckpointingLayer)	)BaseModelOutputWithPastAndCrossAttentions,BaseModelOutputWithPoolingAndCrossAttentions!CausalLMOutputWithCrossAttentionsMaskedLMOutputMultipleChoiceModelOutputNextSentencePredictorOutputQuestionAnsweringModelOutputSequenceClassifierOutputTokenClassifierOutput)ALL_ATTENTION_FUNCTIONSPreTrainedModel)Unpack)apply_chunking_to_forward)ModelOutputTransformersKwargsauto_docstringlogging)can_return_tuplemerge_with_config_defaults)capture_outputs   )ErnieConfigc                      ^  \ rS rSrSrU 4S jr      SS\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S	\R                  S-  S
\	S\R                  4S jjrSrU =r$ )ErnieEmbeddings9   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        [        R                  " UR
                  UR                  S9U l        [        R                  " UR                  5      U l        U R#                  S[$        R&                  " UR                  5      R)                  S5      SS9  U R#                  S[$        R*                  " U R,                  R/                  5       [$        R0                  S9SS9  UR2                  U l        UR2                  (       a1  [        R                  " UR4                  UR
                  5      U l        g g )	N)padding_idxepsposition_idsr&   F)
persistenttoken_type_ids)dtype)super__init__nn	Embedding
vocab_sizehidden_sizepad_token_idword_embeddingsmax_position_embeddingsposition_embeddingstype_vocab_sizetoken_type_embeddings	LayerNormlayer_norm_epsDropouthidden_dropout_probdropoutregister_buffertorcharangeexpandzerosr/   sizelonguse_task_idtask_type_vocab_sizetask_type_embeddingsselfconfig	__class__s     y/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/ernie/modeling_ernie.pyr6   ErnieEmbeddings.__init__<   sX   !||F,=,=v?Q?Q_e_r_rs#%<<0N0NPVPbPb#c %'\\&2H2H&J\J\%]"f&8&8f>S>STzz&"<"<=ELL)G)GHOOPWXej 	 	
 	ekk$*;*;*@*@*B%**Ubg 	 	
 "--(*V5P5PRXRdRd(eD%     N	input_idsr3   task_type_idsr/   inputs_embedspast_key_values_lengthreturnc                    Ub  UR                  5       nOUR                  5       S S nUu  pUc  U R                  S S 2XiU-   24   nUc  [        U S5      (       aQ  U R                  R	                  UR
                  S   S5      n
[        R                  " U
SUS9n
U
R	                  X5      nO8[        R                  " U[        R                  U R                  R                  S9nUc  U R                  U5      nU R                  U5      nUR                  UR                  5      nX[-   n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$ )Nr1   r3   r   r&   )dimindex)r4   device)rK   r/   hasattrr3   rI   shaperG   gatherrJ   rL   r_   r<   r@   tor>   rM   rO   rA   rE   )rQ   rW   r3   rX   r/   rY   rZ   input_shape
batch_size
seq_lengthbuffered_token_type_idsr@   
embeddingsr>   rO   s                  rT   forwardErnieEmbeddings.forwardP   s     #..*K',,.s3K!,
,,Q0FVlIl0l-lmL
 !t-..*.*=*=*D*D\EWEWXYEZ\^*_'*/,,7NTU]i*j'!8!?!?
!W!&[

SWSdSdSkSk!l  00;M $ : :> J &(()>)E)EF":
"66|D5
 $ %KuzzRVRcRcRjRj k#'#<#<]#K .J^^J/
\\*-
rV   )rA   rE   r>   rO   r@   rM   r<   )NNNNNr   )__name__
__module____qualname____firstlineno____doc__r6   rG   
LongTensorFloatTensorintTensorri   __static_attributes____classcell__rS   s   @rT   r)   r)   9   s    Qf, .226150426&'3##d*3 ((4/3 ''$.	3
 &&-3 ((4/3 !$3 
3 3rV   r)   modulequerykeyvalueattention_maskscalingrE   kwargsc                    Uc  UR                  S5      S-  n[        R                  " XR                  SS5      5      U-  nUb  X-   n[        R
                  R                  USS9n[        R
                  R                  XU R                  S9n[        R                  " X5      n	U	R                  SS5      R                  5       n	X4$ )Nr1            r   r]   )ptrainingr&   )
rK   rG   matmul	transposer7   
functionalsoftmaxrE   r   
contiguous)
rw   rx   ry   rz   r{   r|   rE   r}   attn_weightsattn_outputs
             rT   eager_attention_forwardr      s     **R.D( <<}}Q':;gEL!#4==((2(>L==((6??([L,,|3K''1-88:K$$rV   c                      ^  \ rS rSrSU 4S jjr  SS\R                  S\R                  S-  S\S-  S\	\
   S\\R                     4
S	 jjrS
rU =r$ )ErnieSelfAttention   Nc                 N  > [         TU ]  5         UR                  UR                  -  S:w  a7  [	        US5      (       d&  [        SUR                   SUR                   S35      eXl        UR                  U l        [        UR                  UR                  -  5      U l        U R                  U R                  -  U l	        U R                  S-  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R&                  U l        X l        X0l        g Nr   embedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads ()r   )r5   r6   r:   num_attention_headsr`   
ValueErrorrR   rr   attention_head_sizeall_head_sizer|   r7   Linearrx   ry   rz   rC   attention_probs_dropout_probrE   
is_decoder	is_causal	layer_idxrQ   rR   r   r   rS   s       rT   r6   ErnieSelfAttention.__init__   sM    : ::a?PVXhHiHi#F$6$6#7 8 445Q8  #)#=#= #&v'9'9F<V<V'V#W !558P8PP//5YYv1143E3EF
99V//1C1CDYYv1143E3EF
zz&"E"EF ++""rV   hidden_statesr{   past_key_valuesr}   r[   c                    UR                   S S n/ UQSPU R                  P7nU R                  U5      R                  " U6 R	                  SS5      nU R                  U5      R                  " U6 R	                  SS5      nU R                  U5      R                  " U6 R	                  SS5      n	UbA  Un
[        U[        5      (       a  UR                  n
U
R                  XU R                  5      u  p[        R                  " U R                  R                  [         5      nU" U UUU	U4U R"                  (       d  SOU R$                  R&                  U R(                  S.UD6u  pUR*                  " / UQSP76 R-                  5       nX4$ )Nr1   r&   r           rE   r|   )ra   r   rx   viewr   ry   rz   
isinstancer   self_attention_cacheupdater   r   get_interfacerR   _attn_implementationr   r   rE   r   r|   reshaper   )rQ   r   r{   r   r}   rd   hidden_shapequery_layer	key_layervalue_layercurrent_past_key_valuesattention_interfacer   r   s                 rT   ri   ErnieSelfAttention.forward   s    $))#2.CCbC$*B*BC jj/44lCMMaQRSHH]+00,?II!QO	jj/44lCMMaQRS&&5#/+>??*9*N*N' &=%C%CI\`\j\j%k"I(?(M(MKK,,.E)
 %8	%
  $}}C$,,..LL	%
 	%
! "));;;;FFH((rV   )r   r   rR   rE   r   r   ry   r   r   rx   r|   rz   FN)NNrk   rl   rm   rn   r6   rG   rs   rq   r   r   r    tupleri   rt   ru   rv   s   @rT   r   r      sl    #6 48(,	')||') ))D0') 	')
 +,') 
u||	') ')rV   r   c                      ^  \ rS rSrSU 4S jjr   SS\R                  S\R                  S-  S\R                  S-  S\S-  S\	\
   S	\\R                     4S
 jjrSrU =r$ )ErnieCrossAttention   Nc                 ,  > [         TU ]  5         UR                  UR                  -  S:w  a7  [	        US5      (       d&  [        SUR                   SUR                   S35      eXl        UR                  U l        [        UR                  UR                  -  5      U l        U R                  U R                  -  U l	        U R                  S-  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        X l        X0l        g r   )r5   r6   r:   r   r`   r   rR   rr   r   r   r|   r7   r   rx   ry   rz   rC   r   rE   r   r   r   s       rT   r6   ErnieCrossAttention.__init__   s@    : ::a?PVXhHiHi#F$6$6#7 8 445Q8  #)#=#= #&v'9'9F<V<V'V#W !558P8PP//5YYv1143E3EF
99V//1C1CDYYv1143E3EF
zz&"E"EF""rV   r   encoder_hidden_statesr{   r   r}   r[   c                 z   UR                   S S n/ UQSPU R                  P7nU R                  U5      R                  U5      R	                  SS5      nUb%  UR
                  R                  U R                  5      OSn	Ubb  U	(       a[  UR                  R                  U R                     R                  n
UR                  R                  U R                     R                  nO/ UR                   S S QSPU R                  P7nU R                  U5      R                  U5      R	                  SS5      n
U R                  U5      R                  U5      R	                  SS5      nUbA  UR                  R                  XU R                  5      u  pSUR
                  U R                  '   [        R                   " U R"                  R$                  [&        5      nU" U UU
UU4U R(                  (       d  SOU R*                  R,                  U R.                  S.UD6u  pUR0                  " / UQSP76 R3                  5       nX4$ )Nr1   r&   r   FTr   r   )ra   r   rx   r   r   
is_updatedgetr   cross_attention_cachelayerskeysvaluesry   rz   r   r   r   rR   r   r   r   rE   r   r|   r   r   )rQ   r   r   r{   r   r}   rd   r   r   r   r   r   kv_shaper   r   r   s                   rT   ri   ErnieCrossAttention.forward   s    $))#2.CCbC$*B*BC jj/44\BLLQPQRGVGb_//33DNNChm
&:'==DDT^^TYYI)??FFt~~V]]KX.44Sb9X2Xt?W?WXH!67<<XFPPQRTUVI**%:;@@JTTUVXYZK*)8)N)N)U)UDNN*&	 >B**4>>:(?(M(MKK,,.E)
 %8	%
  $}}C$,,..LL	%
 	%
! "));;;;FFH((rV   )r   r   rR   rE   r   ry   r   r   rx   r|   rz   r   )NNN)rk   rl   rm   rn   r6   rG   rs   rq   r   r   r    r   ri   rt   ru   rv   s   @rT   r   r      s    #4 ;?376:1)||1)  %00471) ))D0	1)
 -t31) +,1) 
u||	1) 1)rV   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	$ )ErnieSelfOutputi1  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-   )r5   r6   r7   r   r:   denserA   rB   rC   rD   rE   rP   s     rT   r6   ErnieSelfOutput.__init__2  s`    YYv1163E3EF
f&8&8f>S>STzz&"<"<=rV   r   input_tensorr[   c                 p    U R                  U5      nU R                  U5      nU R                  X-   5      nU$ Nr   rE   rA   rQ   r   r   s      rT   ri   ErnieSelfOutput.forward8  5    

=1]3}'CDrV   rA   r   rE   
rk   rl   rm   rn   r6   rG   rs   ri   rt   ru   rv   s   @rT   r   r   1  6    >U\\  RWR^R^  rV   r   c                      ^  \ rS rSrSU 4S jjr    SS\R                  S\R                  S-  S\R                  S-  S\R                  S-  S\S-  S	\	\
   S
\\R                     4S jjrSrU =r$ )ErnieAttentioni?  Nc                    > [         TU ]  5         X@l        U(       a  [        O[        nU" XUS9U l        [        U5      U l        g )Nr   r   )r5   r6   is_cross_attentionr   r   rQ   r   output)rQ   rR   r   r   r   attention_classrS   s         rT   r6   ErnieAttention.__init__@  s9    "41C-I[#F9U	%f-rV   r   r{   r   encoder_attention_maskr   r}   r[   c                     U R                   (       d  UOUnU R                  " U4UUUS.UD6u  pxU R                  Xq5      nXx4$ )N)r   r{   r   )r   rQ   r   )	rQ   r   r{   r   r   r   r}   attention_outputr   s	            rT   ri   ErnieAttention.forwardG  s\     04/F/FLb)-*
"7)+	*

 *
&  ;;'7G--rV   )r   r   rQ   )FNFNNNNr   rv   s   @rT   r   r   ?  s    . 48:>;?(,.||. ))D0.  %0047	.
 !& 1 1D 8. . +,. 
u||	. .rV   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	$ )ErnieIntermediatei\  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   )r5   r6   r7   r   r:   intermediate_sizer   r   
hidden_actstrr
   intermediate_act_fnrP   s     rT   r6   ErnieIntermediate.__init__]  s`    YYv1163K3KL
f''--'-f.?.?'@D$'-'8'8D$rV   r   r[   c                 J    U R                  U5      nU R                  U5      nU$ r   r   r   rQ   r   s     rT   ri   ErnieIntermediate.forwarde  s&    

=100?rV   r   r   rv   s   @rT   r   r   \  s(    9U\\ ell  rV   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	$ )ErnieOutputik  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   )r5   r6   r7   r   r   r:   r   rA   rB   rC   rD   rE   rP   s     rT   r6   ErnieOutput.__init__l  s`    YYv779K9KL
f&8&8f>S>STzz&"<"<=rV   r   r   r[   c                 p    U R                  U5      nU R                  U5      nU R                  X-   5      nU$ r   r   r   s      rT   ri   ErnieOutput.forwardr  r   rV   r   r   rv   s   @rT   r   r   k  r   rV   r   c                      ^  \ rS rSrSU 4S jjr    SS\R                  S\R                  S-  S\R                  S-  S\R                  S-  S\S-  S	\	\
   S
\R                  4S jjrS rSrU =r$ )
ErnieLayeriy  Nc                   > [         TU ]  5         UR                  U l        SU l        [	        XR
                  US9U l        UR
                  U l        UR                  U l        U R                  (       a0  U R
                  (       d  [        U  S35      e[	        USUSS9U l	        [        U5      U l        [        U5      U l        g )Nr&   r   z> should be used as a decoder model if cross attention is addedFT)r   r   r   )r5   r6   chunk_size_feed_forwardseq_len_dimr   r   	attentionadd_cross_attentionr   crossattentionr   intermediater   r   )rQ   rR   r   rS   s      rT   r6   ErnieLayer.__init__z  s    '-'E'E$':K:KW`a ++#)#=#= ##?? D6)g!hii"0##'	#D .f5!&)rV   r   r{   r   r   r   r}   r[   c                 2   U R                   " UU4SU0UD6u  pxUn	U R                  (       a?  Ub<  [        U S5      (       d  [        SU  S35      eU R                  " US UU4SU0UD6u  pU
n	[        U R                  U R                  U R                  U	5      nU$ )Nr   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`   r   r   r   feed_forward_chunkr   r   )rQ   r   r{   r   r   r   r}   self_attention_output_r   cross_attention_outputlayer_outputs               rT   ri   ErnieLayer.forward  s     $(>>$
 ,$
 	$
  1??4@4!122 =dV DD D 
 )-(;(;%%&	)
 !0) )%"  60##T%A%A4CSCSUe
 rV   c                 J    U R                  U5      nU R                  X!5      nU$ r   )r   r   )rQ   r   intermediate_outputr  s       rT   r   ErnieLayer.feed_forward_chunk  s)    "//0@A{{#6IrV   )r   r   r   r   r   r   r   r   r   r   )rk   rl   rm   rn   r6   rG   rs   rq   r   r   r    ri   r   rt   ru   rv   s   @rT   r   r   y  s    *, 48:>;?(,%||% ))D0%  %0047	%
 !& 1 1D 8% % +,% 
%N rV   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   )r5   r6   r7   r   r:   r   Tanh
activationrP   s     rT   r6   ErniePooler.__init__  s9    YYv1163E3EF
'')rV   r   r[   c                 \    US S 2S4   nU R                  U5      nU R                  U5      nU$ )Nr   )r   r
  )rQ   r   first_token_tensorpooled_outputs       rT   ri   ErniePooler.forward  s6     +1a40

#566rV   )r
  r   r   rv   s   @rT   r  r    s(    $
U\\ ell  rV   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	$ )ErniePredictionHeadTransformi  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   )r5   r6   r7   r   r:   r   r   r   r   r
   transform_act_fnrA   rB   rP   s     rT   r6   %ErniePredictionHeadTransform.__init__  s~    YYv1163E3EF
f''--$*6+<+<$=D!$*$5$5D!f&8&8f>S>STrV   r   r[   c                 l    U R                  U5      nU R                  U5      nU R                  U5      nU$ r   )r   r  rA   r   s     rT   ri   $ErniePredictionHeadTransform.forward  s4    

=1--m<}5rV   )rA   r   r  r   rv   s   @rT   r  r    s)    UU\\ ell  rV   r  c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )ErnieLMPredictionHeadi  c                   > [         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        g )NT)bias)r5   r6   r  	transformr7   r   r:   r9   decoder	ParameterrG   rJ   r  rP   s     rT   r6   ErnieLMPredictionHead.__init__  s[    5f= yy!3!3V5F5FTRLLV->->!?@	rV   c                 J    U R                  U5      nU R                  U5      nU$ r   )r  r  r   s     rT   ri   ErnieLMPredictionHead.forward  s$    }5]3rV   )r  r  r  rk   rl   rm   rn   r6   ri   rt   ru   rv   s   @rT   r  r    s    A rV   r  c                      ^  \ rS rSrU 4S jr     SS\R                  S\R                  S-  S\R                  S-  S\R                  S-  S\S-  S	\	S-  S
\
\   S\\R                     \-  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[        XS9PM     sn5      U l        g s  snf )N)r   )	r5   r6   rR   r7   
ModuleListrangenum_hidden_layersr   layer)rQ   rR   irS   s      rT   r6   ErnieEncoder.__init__  sH    ]]USYSkSkMl#mMlJv$CMl#mn
#ms   ANr   r{   r   r   r   	use_cacher}   r[   c                     [        U R                  5       H  u  pU	" UUU4UUS.UD6nM     [        UU(       a  US9$ S S9$ )N)r   r   )last_hidden_stater   )	enumerater(  r   )
rQ   r   r{   r   r   r   r+  r}   r)  layer_modules
             rT   ri   ErnieEncoder.forward  sg      )4OA(% (> / M  5 9+/8O
 	
>B
 	
rV   )rR   r(  )NNNNN)rk   rl   rm   rn   r6   rG   rs   rq   r   boolr   r    r   r   ri   rt   ru   rv   s   @rT   r#  r#    s    o 48:>;?(,!%
||
 ))D0
  %0047	

 !& 1 1D 8
 
 $;
 +,
 
u||	H	H
 
rV   r#  c                   x   ^  \ rS rSr\rSrSrSrSr	Sr
Sr\\\S.r\R"                  " 5       U 4S j5       rSrU =r$ )ErniePreTrainedModeli
  ernieT)r   
attentionscross_attentionsc                   > [         TU ]  U5        [        U[        5      (       a!  [        R
                  " UR                  5        g[        U[        5      (       a|  [        R                  " UR                  [        R                  " UR                  R                  S   5      R                  S5      5        [        R
                  " UR                  5        gg)zInitialize the weightsr1   r0   N)r5   _init_weightsr   r  initzeros_r  r)   copy_r/   rG   rH   ra   rI   r3   )rQ   rw   rS   s     rT   r8  "ErniePreTrainedModel._init_weights  s     	f%f344KK$00JJv**ELL9L9L9R9RSU9V,W,^,^_f,ghKK--. 1rV    )rk   rl   rm   rn   r'   config_classbase_model_prefixsupports_gradient_checkpointing_supports_flash_attn_supports_sdpa_supports_flex_attn_supports_attention_backendr   r   r   _can_record_outputsrG   no_gradr8  rt   ru   rv   s   @rT   r3  r3  
  sV    L&*#N"&#(/ ]]_/ /rV   r3  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://huggingface.co/papers/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
    `add_cross_attention` set to `True`; an `encoder_hidden_states` is then expected as an input to the forward pass.
    )custom_introc                     ^  \ rS rSrS/rSU 4S jjrS rS r\\	\
          SS\R                  S-  S\R                  S-  S	\R                  S-  S
\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S\S-  S\S-  S\\   S\\R                     \-  4S jj5       5       5       rS rSrU =r$ )
ErnieModeli$  r   c                    > [         TU ]  U5        Xl        SU 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
FN)r5   r6   rR   gradient_checkpointingr)   rh   r#  encoderr  pooler	post_init)rQ   rR   add_pooling_layerrS   s      rT   r6   ErnieModel.__init__3  sS    
 	 &+#)&1#F+->k&)D 	rV   c                 .    U R                   R                  $ r   rh   r<   rQ   s    rT   get_input_embeddingsErnieModel.get_input_embeddingsD  s    ...rV   c                 $    XR                   l        g r   rR  )rQ   rz   s     rT   set_input_embeddingsErnieModel.set_input_embeddingsG  s    */'rV   NrW   r{   r3   rX   r/   rY   r   r   r   r+  r}   r[   c           
         USL USL-  (       a  [        S5      eU R                  R                  (       a  U
b  U
OU R                  R                  n
OSn
U
(       ab  U	c_  Uc  U R                  R                  (       a.  [        [        U R                  S9[        U R                  S95      O[        U R                  S9n	U	b  U	R                  5       OSnU R                  UUUUUUS9nU R                  UUUUU	S9u  p(U R                  " U4UUUU	U
US.UD6nUS   nU R                  b  U R                  U5      OSn[        UU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]
Nz:You must specify exactly one of input_ids or inputs_embedsF)rR   r   )rW   r/   r3   rX   rY   rZ   )r{   r   embedding_outputr   r   )r{   r   r   r   r+  r/   )r-  pooler_outputr   )r   rR   r   r+  is_encoder_decoderr   r   get_seq_lengthrh   _create_attention_masksrL  rM  r   r   )rQ   rW   r{   r3   rX   r/   rY   r   r   r   r+  r}   rZ   r[  encoder_outputssequence_outputr  s                    rT   ri   ErnieModel.forwardJ  sy   0 -t";<YZZ;;!!%.%:	@U@UII0 )48V8V $L$DlZ^ZeZeFfg!5  FUE`!?!?!Afg??%)''#9 + 
 261M1M)#9-"7+ 2N 2
. ,,	
)"7#9+%	
 	
 *!,8<8OO4UY;-'+;;
 	
rV   c                     U R                   R                  (       a  [        U R                   UUUS9nO[        U R                   UUS9nUb  [        U R                   UUUS9nX4$ )N)rR   rY   r{   r   )rR   rY   r{   )rR   rY   r{   r   )rR   r   r   r   )rQ   r{   r   r[  r   r   s         rT   r_  "ErnieModel._create_attention_masks  sr     ;;!!/{{.- /	N 7{{.-N "-%>{{.5&;	&" 55rV   )rR   rh   rL  rK  rM  )T)
NNNNNNNNNN)rk   rl   rm   rn   _no_split_modulesr6   rT  rW  r$   r%   r!   rG   rs   r   r1  r   r    r   r   ri   r_  rt   ru   rv   s   @rT   rI  rI  $  sT    &"/0   *..2.2-1,0-1596:(,!%H
<<$&H
 t+H
 t+	H

 ||d*H
 llT)H
 ||d*H
  %||d2H
 !&t 3H
 H
 $;H
 +,H
 
u||	K	KH
    H
T6 6rV   rI  z1
    Output type of [`ErnieForPreTraining`].
    c                       \ rS rSr% SrSr\R                  S-  \S'   Sr	\R                  S-  \S'   Sr
\R                  S-  \S'   Sr\\R                     S-  \S'   Sr\\R                     S-  \S'   S	rg)
ErnieForPreTrainingOutputi  ar  
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).
Nlossprediction_logitsseq_relationship_logitsr   r5  r=  )rk   rl   rm   rn   ro   rh  rG   rq   __annotations__ri  rj  r   r   r5  rt   r=  rV   rT   rg  rg    s~    	 &*D%

d
")26u((4/68<U..5<59M5**+d2926Je''(4/6rV   rg  c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )ErniePreTrainingHeadsi  c                    > [         TU ]  5         [        U5      U l        [        R
                  " UR                  S5      U l        g Nr   )r5   r6   r  predictionsr7   r   r:   seq_relationshiprP   s     rT   r6   ErniePreTrainingHeads.__init__  s4    08 "		&*<*<a @rV   c                 L    U R                  U5      nU R                  U5      nX44$ r   rp  rq  )rQ   ra  r  prediction_scoresseq_relationship_scores        rT   ri   ErniePreTrainingHeads.forward  s-     ,,_=!%!6!6}!E 88rV   rt  r!  rv   s   @rT   rm  rm    s    A
9 9rV   rm  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S.rU 4S jrS rS r\\	        SS	\
R                  S-  S
\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\\   S\\
R                     \-  4S jj5       5       rSrU =r$ )ErnieForPreTrainingi  cls.predictions.bias'ernie.embeddings.word_embeddings.weight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   )r5   r6   rI  r4  rm  clsrN  rP   s     rT   r6   ErnieForPreTraining.__init__  s4     '
(0 	rV   c                 B    U R                   R                  R                  $ r   r  rp  r  rS  s    rT   get_output_embeddings)ErnieForPreTraining.get_output_embeddings      xx##+++rV   c                     XR                   R                  l        UR                  U R                   R                  l        g r   r  rp  r  r  rQ   new_embeddingss     rT   set_output_embeddings)ErnieForPreTraining.set_output_embeddings  *    '5$$2$7$7!rV   NrW   r{   r3   rX   r/   rY   labelsnext_sentence_labelr}   r[   c	           
         U R                   " U4UUUUUSS.U	D6n
U
SS u  pU R                  X5      u  pS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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
```
Tr{   r3   rX   r/   rY   return_dictNr   r1   )rh  ri  rj  r   r5  )	r4  r  r   r   rR   r9   rg  r   r5  )rQ   rW   r{   r3   rX   r/   rY   r  r  r}   outputsra  r  ru  rv  
total_lossloss_fctmasked_lm_lossnext_sentence_losss                      rT   ri   ErnieForPreTraining.forward  s    ^ **	
))'%'	
 	
 *1!&48HH_4\1
"5"A')H%&7&<&<RAWAW&XZ`ZeZefhZijN!)*@*E*Eb!*LNaNfNfgiNj!k'*<<J(/$:!//))
 	
rV   r  r4  NNNNNNNN)rk   rl   rm   rn   _tied_weights_keysr6   r  r  r#   r!   rG   rs   r   r    r   rg  ri   rt   ru   rv   s   @rT   ry  ry    s#    )?*S
,8  *..2.2-1,0-1&*37H
<<$&H
 t+H
 t+	H

 ||d*H
 llT)H
 ||d*H
 t#H
 #\\D0H
 +,H
 
u||	8	8H
  H
rV   ry  c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )ErnieOnlyMLMHeadiF  c                 B   > [         TU ]  5         [        U5      U l        g r   )r5   r6   r  rp  rP   s     rT   r6   ErnieOnlyMLMHead.__init__G  s    08rV   ra  r[   c                 (    U R                  U5      nU$ r   rp  )rQ   ra  ru  s      rT   ri   ErnieOnlyMLMHead.forwardK  s     ,,_=  rV   r  r   rv   s   @rT   r  r  F  s(    9!u|| ! ! !rV   r  zQ
    Ernie Model with a `language modeling` head on top for CLM fine-tuning.
    c                       ^  \ rS rSrSSS.rU 4S jrS rS r\\	            SS	\
R                  S-  S
\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\\
R                     S-  S\S-  S\\
R                  -  S\\   S\\
R                     \-  4S jj5       5       rSrU =r$ )ErnieForCausalLMiP  r{  rz  )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rO  
r5   r6   r   loggerwarningrI  r4  r  r  rN  rP   s     rT   r6   ErnieForCausalLM.__init__[  sL       NNjk%@
#F+ 	rV   c                 B    U R                   R                  R                  $ r   r  rS  s    rT   r  &ErnieForCausalLM.get_output_embeddingsg  r  rV   c                     XR                   R                  l        UR                  U R                   R                  l        g r   r  r  s     rT   r  &ErnieForCausalLM.set_output_embeddingsj  r  rV   NrW   r{   r3   rX   r/   rY   r   r   r  r   r+  logits_to_keepr}   r[   c                    U	b  SnU R                   " U4UUUUUUUU
USS.
UD6nUR                  n[        U[        5      (       a  [	        U* S5      OUnU R                  USS2USS24   5      nSnU	b)  U R                  " SUXR                  R                  S.UD6n[        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]`
NFT)
r{   r3   rX   r/   rY   r   r   r   r+  r  )logitsr  r9   )rh  r  r   r   r5  r6  r=  )r4  r-  r   rr   slicer  loss_functionrR   r9   r   r   r   r5  r6  )rQ   rW   r{   r3   rX   r/   rY   r   r   r  r   r+  r  r}   r  r   slice_indicesr  rh  s                      rT   ri   ErnieForCausalLM.forwardn  s    : I@D

A
))'%'"7#9+A
 A
  118B>SV8W8W~ot4]k-=!(;<=%%pVF{{OeOepiopD0#33!//))$55
 	
rV   r  )NNNNNNNNNNNr   )rk   rl   rm   rn   r  r6   r  r  r#   r!   rG   rs   listr1  rr   r   r    r   r   ri   rt   ru   rv   s   @rT   r  r  P  sp    +T(>

,8  *..2.2-1,0-1596:&*59!%-.=
<<$&=
 t+=
 t+	=

 ||d*=
 llT)=
 ||d*=
  %||d2=
 !&t 3=
 t#=
 ell+d2=
 $;=
 ell*=
 +,=
 
u||	@	@=
  =
rV   r  c                     ^  \ rS rSrSSS.rU 4S jrS rS r\\	         SS	\
R                  S-  S
\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\
R                  S-  S\\   S\\
R                     \-  4S jj5       5       rSrU =r$ )ErnieForMaskedLMi  rz  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  rP   s     rT   r6   ErnieForMaskedLM.__init__  sR     NN1
  %@
#F+ 	rV   c                 B    U R                   R                  R                  $ r   r  rS  s    rT   r  &ErnieForMaskedLM.get_output_embeddings  r  rV   c                     XR                   R                  l        UR                  U R                   R                  l        g r   r  r  s     rT   r  &ErnieForMaskedLM.set_output_embeddings  r  rV   NrW   r{   r3   rX   r/   rY   r   r   r  r}   r[   c
                 <   U R                   " U4UUUUUUUSS.U
D6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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]`
T)r{   r3   rX   r/   rY   r   r   r  r   Nr1   rh  r  r   r5  )	r4  r  r   r   rR   r9   r   r   r5  )rQ   rW   r{   r3   rX   r/   rY   r   r   r  r}   r  ra  ru  r  r  s                   rT   ri   ErnieForMaskedLM.forward  s    4 **
))'%'"7#9
 
 "!* HH_5')H%&7&<&<RAWAW&XZ`ZeZefhZijN$!//))	
 	
rV   r  )	NNNNNNNNN)rk   rl   rm   rn   r  r6   r  r  r#   r!   rG   rs   r   r    r   r   ri   rt   ru   rv   s   @rT   r  r    s,    )?*S
,8  *..2.2-1,0-1596:&*2
<<$&2
 t+2
 t+	2

 ||d*2
 llT)2
 ||d*2
  %||d22
 !&t 32
 t#2
 +,2
 
u||	~	-2
  2
rV   r  c                   .   ^  \ rS rSrU 4S jrS rSrU =r$ )ErnieOnlyNSPHeadi  c                 n   > [         TU ]  5         [        R                  " UR                  S5      U l        g ro  )r5   r6   r7   r   r:   rq  rP   s     rT   r6   ErnieOnlyNSPHead.__init__  s'     "		&*<*<a @rV   c                 (    U R                  U5      nU$ r   rq  )rQ   r  rv  s      rT   ri   ErnieOnlyNSPHead.forward	  s    !%!6!6}!E%%rV   r  r!  rv   s   @rT   r  r    s    A& &rV   r  zU
    Ernie Model with a `next sentence prediction (classification)` head on top.
    c                   X  ^  \ rS rSrU 4S jr\\       SS\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S	\R                  S-  S
\R                  S-  S\	\
   S\\R                     \-  4S jj5       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   )r5   r6   rI  r4  r  r  rN  rP   s     rT   r6   'ErnieForNextSentencePrediction.__init__  s4     '
#F+ 	rV   NrW   r{   r3   rX   r/   rY   r  r}   r[   c           
         U R                   " U4UUUUUSS.UD6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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
```
Tr  r&   Nr1   r   r  )r4  r  r   r   r   r   r5  )rQ   rW   r{   r3   rX   r/   rY   r  r}   r  r  seq_relationship_scoresr  r  s                 rT   ri   &ErnieForNextSentencePrediction.forward  s    Z **	
))'%'	
 	
  
"&((="9!')H!)*A*F*Fr1*Mv{{[]!_*#*!//))	
 	
rV   r  NNNNNNN)rk   rl   rm   rn   r6   r#   r!   rG   rs   r   r    r   r   ri   rt   ru   rv   s   @rT   r  r    s      *..2.2-1,0-1&*D
<<$&D
 t+D
 t+	D

 ||d*D
 llT)D
 ||d*D
 t#D
 +,D
 
u||	:	:D
  D
rV   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                   X  ^  \ rS rSrU 4S jr\\       SS\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S	\R                  S-  S
\R                  S-  S\	\
   S\\R                     \-  4S jj5       5       rSrU =r$ )ErnieForSequenceClassificationif  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   )r5   r6   
num_labelsrR   rI  r4  classifier_dropoutrD   r7   rC   rE   r   r:   
classifierrN  rQ   rR   r  rS   s      rT   r6   'ErnieForSequenceClassification.__init__m  s      ++'
)/)B)B)NF%%TZTnTn 	 zz"45))F$6$68I8IJ 	rV   NrW   r{   r3   rX   r/   rY   r  r}   r[   c           
         U R                   " U4UUUUUSS.UD6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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).
Tr  r&   N
regressionsingle_label_classificationmulti_label_classificationr1   r  )r4  rE   r  rR   problem_typer  r4   rG   rL   rr   r   squeezer   r   r   r   r   r5  )rQ   rW   r{   r3   rX   r/   rY   r  r}   r  r  r  rh  r  s                 rT   ri   &ErnieForSequenceClassification.forward|  s   0 **	
))'%'	
 	
  
]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,./'!//))	
 	
rV   )r  rR   rE   r4  r  r  )rk   rl   rm   rn   r6   r#   r!   rG   rs   r   r    r   r   ri   rt   ru   rv   s   @rT   r  r  f  s      *..2.2-1,0-1&*B
<<$&B
 t+B
 t+	B

 ||d*B
 llT)B
 ||d*B
 t#B
 +,B
 
u||	7	7B
  B
rV   r  c                   X  ^  \ rS rSrU 4S jr\\       SS\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S	\R                  S-  S
\R                  S-  S\	\
   S\\R                     \-  4S jj5       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&   )r5   r6   rI  r4  r  rD   r7   rC   rE   r   r:   r  rN  r  s      rT   r6   ErnieForMultipleChoice.__init__  su     '
)/)B)B)NF%%TZTnTn 	 zz"45))F$6$6: 	rV   NrW   r{   r3   rX   r/   rY   r  r}   r[   c           
         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4UUUUUSS.UD6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" X5      n[        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&   r1   Tr  r  )
ra   r   rK   r4  rE   r  r   r   r   r5  )rQ   rW   r{   r3   rX   r/   rY   r  r}   num_choicesr  r  r  reshaped_logitsrh  r  s                   rT   ri   ErnieForMultipleChoice.forward  s   ` -6,Aiooa(}GZGZ[\G]>G>SINN2y~~b'9:Y]	M[Mg,,R1D1DR1HImqM[Mg,,R1D1DR1HImqGSG_|((\->->r-BCei ( r=#5#5b#9=;M;Mb;QR 	 **	
))'%'	
 	
  
]3/ ++b+6')HO4D("!//))	
 	
rV   )r  rE   r4  r  )rk   rl   rm   rn   r6   r#   r!   rG   rs   r   r    r   r   ri   rt   ru   rv   s   @rT   r  r    s      *..2.2-1,0-1&*U
<<$&U
 t+U
 t+	U

 ||d*U
 llT)U
 ||d*U
 t#U
 +,U
 
u||	8	8U
  U
rV   r  c                   X  ^  \ rS rSrU 4S jr\\       SS\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S	\R                  S-  S
\R                  S-  S\	\
   S\\R                     \-  4S jj5       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  )r5   r6   r  rI  r4  r  rD   r7   rC   rE   r   r:   r  rN  r  s      rT   r6   $ErnieForTokenClassification.__init__.  s      ++%@
)/)B)B)NF%%TZTnTn 	 zz"45))F$6$68I8IJ 	rV   NrW   r{   r3   rX   r/   rY   r  r}   r[   c           
      F   U R                   " U4UUUUUSS.UD6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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]`.
Tr  r   Nr1   r  )	r4  rE   r  r   r   r  r   r   r5  )rQ   rW   r{   r3   rX   r/   rY   r  r}   r  ra  r  rh  r  s                 rT   ri   #ErnieForTokenClassification.forward<  s    , **	
))'%'	
 	
 "!*,,71')HFKKDOO<fkk"oND$!//))	
 	
rV   )r  rE   r4  r  r  )rk   rl   rm   rn   r6   r#   r!   rG   rs   r   r    r   r   ri   rt   ru   rv   s   @rT   r  r  ,  s      *..2.2-1,0-1&*.
<<$&.
 t+.
 t+	.

 ||d*.
 llT).
 ||d*.
 t#.
 +,.
 
u||	4	4.
  .
rV   r  c                   x  ^  \ rS rSrU 4S jr\\        SS\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S\R                  S-  S	\R                  S-  S
\R                  S-  S\R                  S-  S\	\
   S\\R                     \-  4S jj5       5       rSrU =r$ )ErnieForQuestionAnsweringio  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  )
r5   r6   r  rI  r4  r7   r   r:   
qa_outputsrN  rP   s     rT   r6   "ErnieForQuestionAnswering.__init__q  sU      ++%@
))F$6$68I8IJ 	rV   NrW   r{   r3   rX   r/   rY   start_positionsend_positionsr}   r[   c	           
         U R                   " U4UUUUUSS.U	D6n
U
S   nU R                  U5      nUR                  SSS9u  pU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" X5      nU" X5      nUU-   S	-  n[        UUUU
R                  U
R                  S
9$ )rZ  Tr  r   r&   r1   r   N)ignore_indexr   )rh  start_logits
end_logitsr   r5  )r4  r  splitr  r   lenrK   clampr   r   r   r5  )rQ   rW   r{   r3   rX   r/   rY   r  r  r}   r  ra  r  r  r  r  ignored_indexr  
start_lossend_losss                       rT   ri   !ErnieForQuestionAnswering.forward{  s{   * **	
))'%'	
 	
 "!*1#)<<r<#: #++B/::<''+668

&=+D?'')*Q."1"9"9""==%%'(1, - 5 5b 9(--a0M-33A}EO)//=AM']CH!,@J
:H$x/14J+%!!//))
 	
rV   )r4  r  r  r  )rk   rl   rm   rn   r6   r#   r!   rG   rs   r   r    r   r   ri   rt   ru   rv   s   @rT   r  r  o  s      *..2.2-1,0-1/3-1<
<<$&<
 t+<
 t+	<

 ||d*<
 llT)<
 ||d*<
 ,<
 ||d*<
 +,<
 
u||	;	;<
  <
rV   r  )
r  r  r  r  ry  r  r  r  rI  r3  )Nr   )Ycollections.abcr   dataclassesr   rG   torch.nnr7   r   r   r    r	   r9  activationsr
   cache_utilsr   r   r   
generationr   masking_utilsr   r   modeling_layersr   modeling_outputsr   r   r   r   r   r   r   r   r   modeling_utilsr   r   processing_utilsr   pytorch_utilsr   utilsr   r    r!   r"   utils.genericr#   r$   utils.output_capturingr%   configuration_ernier'   
get_loggerrk   r  Moduler)   rs   floatr   r   r   r   r   r   r   r   r  r  r  r#  r3  rI  rg  rm  ry  r  r  r  r  r  r  r  r  r  __all__r=  rV   rT   <module>r     s  * % !   A A & ! C C ) J 9
 
 
 G & 6 M M I 5 , 
		H	%Jbii Jf !%II%<<% 
% <<	%
 LL4'% T\% % '(%8@) @)FI)")) I)Xbii .RYY .:		 ")) >+ >B")) 299 "BII  
299 
@ /? / /2 	E6% E6E6P 
 7 7 7&	9BII 	9 `
. `
`
F!ryy ! 
X
+_ X

X
v P
+ P
 P
f&ryy & 
P
%9 P

P
f T
%9 T
T
n e
1 e
 e
P ?
"6 ?
 ?
D I
 4 I
 I
XrV   