
    qic                        d Z ddlmZ ddlmZ ddlZddlmZ ddlmZm	Z	m
Z
 ddlmZ dd	lmZmZ dd
lmZmZmZ ddlmZ ddlmZmZ ddlmZ ddlmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7  e0jp                  e9      Z: G d dejv                        Z<	 	 dVdejv                  dejz                  dejz                  dejz                  dejz                  dz  de>dz  de>d e)e.   fd!Z? G d" d#ejv                        Z@ G d$ d%ejv                        ZA G d& d'ejv                        ZB G d( d)ejv                        ZC G d* d+ejv                        ZD G d, d-ejv                        ZE G d. d/e      ZF G d0 d1ejv                        ZG G d2 d3ejv                        ZH G d4 d5ejv                        ZIe/ G d6 d7e'             ZJe e/d89       G d: d;e-                    ZKe/ G d< d=eJ             ZL G d> d?ejv                        ZM G d@ dAejv                        ZN e/dB9       G dC dDeJ             ZO e/dE9       G dF dGeJ             ZP e/dH9       G dI dJeJ             ZQ e/dK9       G dL dMeJ             ZRe/ G dN dOeJ             ZSe/ G dP dQeJ             ZT e/dR9       G dS dTeJe             ZUg dUZVy)WzPyTorch ELECTRA model.    )Callable)	dataclassN)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )initialization)ACT2FNget_activation)CacheDynamicCacheEncoderDecoderCache)GenerationMixin)create_bidirectional_maskcreate_causal_mask)GradientCheckpointingLayer)"BaseModelOutputWithCrossAttentions)BaseModelOutputWithPastAndCrossAttentions!CausalLMOutputWithCrossAttentionsMaskedLMOutputMultipleChoiceModelOutput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   )ElectraConfigc                        e Zd ZdZ fdZ	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  ded	ej                  fd
Z
 xZS )ElectraEmbeddingszGConstruct the embeddings from word, position and token_type embeddings.c                    t         |           t        j                  |j                  |j
                  |j                        | _        t        j                  |j                  |j
                        | _	        t        j                  |j                  |j
                        | _        t        j                  |j
                  |j                        | _        t        j                  |j                        | _        | j#                  dt%        j&                  |j                        j)                  d      d       | j#                  dt%        j*                  | j,                  j/                         t$        j0                        d       y )	N)padding_idxepsposition_idsr'   F)
persistenttoken_type_idsdtype)super__init__r   	Embedding
vocab_sizeembedding_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selfconfig	__class__s     ^/opt/pipecat/venv/lib/python3.12/site-packages/transformers/models/electra/modeling_electra.pyr7   zElectraEmbeddings.__init__;   s   !||F,=,=v?T?Tbhbubuv#%<<0N0NPVPePe#f %'\\&2H2H&J_J_%`"f&;&;AVAVWzz&"<"<= 	ELL)G)GHOOPWXej 	 	
 	ekk$*;*;*@*@*B%**Ubg 	 	
    N	input_idsr3   r/   inputs_embedspast_key_values_lengthreturnc                    ||j                         }n|j                         d d }|\  }}|| j                  d d |||z   f   }|t        | d      rT| j                  j	                  |j
                  d   d      }	t        j                  |	d|      }	|	j	                  ||      }n:t        j                  |t        j                  | j                  j                        }|| j                  |      }| j                  |      }
||
z   }| j                  |      }||z   }| j                  |      }| j                  |      }|S )Nr1   r3   r   r'   )dimindex)r5   device)rK   r/   hasattrr3   rI   shaperG   gatherrJ   rL   rZ   r<   r@   r>   rA   rE   )rN   rS   r3   r/   rT   rU   input_shape
batch_size
seq_lengthbuffered_token_type_idsr@   
embeddingsr>   s                rQ   forwardzElectraEmbeddings.forwardM   sP     #..*K',,.s3K!,
J,,Q0FVlIl0l-lmL
 !t-.*.*=*=*D*D\EWEWXYEZ\^*_'*/,,7NTU]i*j'!8!?!?
J!W!&[

SWSdSdSkSk!l  00;M $ : :> J"%::
"66|D"55
^^J/
\\*-
rR   )NNNNr   )__name__
__module____qualname____doc__r7   rG   
LongTensorFloatTensorintTensorrc   __classcell__rP   s   @rQ   r*   r*   8   s    Q
( .2260426&'(##d*( ((4/( &&-	(
 ((4/( !$( 
(rR   r*   modulequerykeyvalueattention_maskscalingrE   kwargsc                    ||j                  d      dz  }t        j                  ||j                  dd            |z  }|||z   }t        j
                  j                  |d      }t        j
                  j                  ||| j                        }t        j                  ||      }	|	j                  dd      j                         }	|	|fS )Nr1            r	   rX   )ptrainingr'   )
rK   rG   matmul	transposer   
functionalsoftmaxrE   rz   
contiguous)
rn   ro   rp   rq   rr   rs   rE   rt   attn_weightsattn_outputs
             rQ   eager_attention_forwardr   y   s     **R.D( <<s}}Q':;gEL!#n4==((2(>L==((6??([L,,|U3K''1-88:K$$rR   c                        e Zd Zd
 fd	Z	 	 	 ddej
                  dej                  dz  dedz  dej
                  dz  dee	   de
ej
                     fd	Z xZS )ElectraSelfAttentionNc                 @   t         |           |j                  |j                  z  dk7  r2t	        |d      s&t        d|j                   d|j                   d      || _        |j                  | _        t        |j                  |j                  z        | _        | j                  | j                  z  | _	        | j                  dz  | _
        t        j                  |j                  | j                        | _        t        j                  |j                  | j                        | _        t        j                  |j                  | j                        | _        t        j                   |j"                        | _        |j&                  | _        || _        || _        y Nr   r:   zThe hidden size (z6) is not a multiple of the number of attention heads ()rv   )r6   r7   hidden_sizenum_attention_headsr[   
ValueErrorrO   rj   attention_head_sizeall_head_sizers   r   Linearro   rp   rq   rC   attention_probs_dropout_probrE   
is_decoder	is_causal	layer_idxrN   rO   r   r   rP   s       rQ   r7   zElectraSelfAttention.__init__   sP    : ::a?PVXhHi#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 ++""rR   hidden_statesrr   past_key_valuescache_positionrt   rV   c                    |j                   d d }g |d| j                  } | j                  |      j                  | j	                  dd      } | j                  |      j                  | j	                  dd      }	 | j                  |      j                  | j	                  dd      }
|A|}t        |t              r|j                  }|j                  |	|
| j                  d|i      \  }	}
t        j                  | j                  j                  t               } || ||	|
|f| j"                  sdn| j$                  j&                  | j(                  d|\  }} |j*                  g |d j-                         }||fS )Nr1   r'   rw   r           rE   rs   )r\   r   ro   viewr|   rp   rq   
isinstancer   self_attention_cacheupdater   r   get_interfacerO   _attn_implementationr   rz   rE   ry   rs   reshaper   )rN   r   rr   r   r   rt   r^   hidden_shapequery_layer	key_layervalue_layercurrent_past_key_valuesattention_interfacer   r   s                  rQ   rc   zElectraSelfAttention.forward   s    $))#2.CCbC$*B*BC 5djj/44lCMMaQRS0DHH]+00,?II!QO	4djj/44lCMMaQRS&&5#/+>?*9*N*N' &=%C%C!>2	&"I{ )@(M(MKK,,.E)
 %8	%
  $}}C$,,..LL	%
 	%
!\ *k));;;;FFHL((rR   FNNNNrd   re   rf   r7   rG   rk   ri   r   r   r!   tuplerc   rl   rm   s   @rQ   r   r      s}    #6 48(,.2-)||-) ))D0-) 	-)
 t+-) +,-) 
u||	-)rR   r   c                        e Zd Zd
 fd	Z	 	 	 ddej
                  dej                  dz  dej                  dz  dedz  dee	   de
ej
                     fd	Z xZS )ElectraCrossAttentionNc                    t         |           |j                  |j                  z  dk7  r2t	        |d      s&t        d|j                   d|j                   d      || _        |j                  | _        t        |j                  |j                  z        | _        | j                  | j                  z  | _	        | j                  dz  | _
        t        j                  |j                  | j                        | _        t        j                  |j                  | j                        | _        t        j                  |j                  | j                        | _        t        j                   |j"                        | _        || _        || _        y r   )r6   r7   r   r   r[   r   rO   rj   r   r   rs   r   r   ro   rp   rq   rC   r   rE   r   r   r   s       rQ   r7   zElectraCrossAttention.__init__   sC    : ::a?PVXhHi#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""rR   r   encoder_hidden_statesrr   r   rt   rV   c                 V   |j                   d d \  }}|j                   d   }||d| j                  f}	||d| j                  f}
 | j                  |      j                  |	 j	                  dd      }|%|j
                  j                  | j                        nd}|]|r[|j                  j                  | j                     j                  }|j                  j                  | j                     j                  }n | j                  |      j                  |
 j	                  dd      } | j                  |      j                  |
 j	                  dd      }|C|j                  j                  ||| j                        \  }}d|j
                  | j                  <   t        j                   | j"                  j$                  t&              } || ||||f| j(                  sdn| j*                  j,                  | j.                  d|\  }}|j1                  ||d      j3                         }||fS )Nr1   r'   rw   FTr   r   )r\   r   ro   r   r|   
is_updatedgetr   cross_attention_cachelayerskeysvaluesrp   rq   r   r   r   rO   r   r   rz   rE   ry   rs   r   r   )rN   r   r   rr   r   rt   bsztgt_lensrc_lenq_input_shapekv_input_shaper   r   r   r   r   r   r   s                     rQ   rc   zElectraCrossAttention.forward   s    %**3B/W'--a0gr4+C+CDwD,D,DE 5djj/44mDNNqRSTGVGb_//33DNNChm
&:'==DDT^^TYYI)??FFt~~V]]K<!67<<nMWWXY[\]I@$**%:;@@.Q[[\]_`aK*)8)N)N)U)U{DNN*&	; >B**4>>:(?(M(MKK,,.E)
 %8	%
  $}}C$,,..LL	%
 	%
!\ "))#w;FFHL((rR   r   r   )rd   re   rf   r7   rG   rk   ri   r   r   r!   r   rc   rl   rm   s   @rQ   r   r      s    #4 ;?376:2)||2)  %00472) ))D0	2)
 -t32) +,2) 
u||	2)rR   r   c                   n     e Zd Z fdZdej
                  dej
                  dej
                  fdZ xZS )ElectraSelfOutputc                 (   t         |           t        j                  |j                  |j                        | _        t        j                  |j                  |j                        | _        t        j                  |j                        | _
        y Nr-   )r6   r7   r   r   r   denserA   rB   rC   rD   rE   rM   s     rQ   r7   zElectraSelfOutput.__init__/  s`    YYv1163E3EF
f&8&8f>S>STzz&"<"<=rR   r   input_tensorrV   c                 r    | j                  |      }| j                  |      }| j                  ||z         }|S Nr   rE   rA   rN   r   r   s      rQ   rc   zElectraSelfOutput.forward5  7    

=1]3}|'CDrR   rd   re   rf   r7   rG   rk   rc   rl   rm   s   @rQ   r   r   .  1    >U\\  RWR^R^ rR   r   c                        e Zd Zd fd	Z	 	 	 	 	 ddej
                  dej                  dz  dej                  dz  dej                  dz  dedz  dej
                  dz  d	ee	   d
e
ej
                     fdZ xZS )ElectraAttentionNc                     t         |           || _        |rt        nt        } ||||      | _        t        |      | _        y )Nr   r   )r6   r7   is_cross_attentionr   r   rN   r   output)rN   rO   r   r   r   attention_classrP   s         rQ   r7   zElectraAttention.__init__>  s=    "43E/K_#Fi9U	'/rR   r   rr   r   encoder_attention_maskr   r   rt   rV   c                     | j                   s|n|} | j                  |f||||d|\  }}	| j                  ||      }||	fS )N)r   rr   r   r   )r   rN   r   )
rN   r   rr   r   r   r   r   rt   attention_outputr   s
             rQ   rc   zElectraAttention.forwardE  sg     04/F/FLb)2*
"7)+)*
 *
&,  ;;'7G--rR   )FNFNNNNNr   rm   s   @rQ   r   r   =  s    0 48:>;?(,.2.||. ))D0.  %0047	.
 !& 1 1D 8. . t+. +,. 
u||	.rR   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )ElectraIntermediatec                    t         |           t        j                  |j                  |j
                        | _        t        |j                  t              rt        |j                     | _        y |j                  | _        y r   )r6   r7   r   r   r   intermediate_sizer   r   
hidden_actstrr   intermediate_act_fnrM   s     rQ   r7   zElectraIntermediate.__init__^  s]    YYv1163K3KL
f''-'-f.?.?'@D$'-'8'8D$rR   r   rV   c                 J    | j                  |      }| j                  |      }|S r   )r   r   )rN   r   s     rQ   rc   zElectraIntermediate.forwardf  s&    

=100?rR   r   rm   s   @rQ   r   r   ]  s#    9U\\ ell rR   r   c                   n     e Zd Z fdZdej
                  dej
                  dej
                  fdZ xZS )ElectraOutputc                 (   t         |           t        j                  |j                  |j
                        | _        t        j                  |j
                  |j                        | _        t        j                  |j                        | _        y r   )r6   r7   r   r   r   r   r   rA   rB   rC   rD   rE   rM   s     rQ   r7   zElectraOutput.__init__n  s`    YYv779K9KL
f&8&8f>S>STzz&"<"<=rR   r   r   rV   c                 r    | j                  |      }| j                  |      }| j                  ||z         }|S r   r   r   s      rQ   rc   zElectraOutput.forwardt  r   rR   r   rm   s   @rQ   r   r   m  r   rR   r   c                        e Zd Zd fd	Z	 	 	 	 	 ddej
                  dej                  dz  dej                  dz  dej                  dz  dedz  dej
                  dz  d	ee	   d
e
ej
                     fdZd Z xZS )ElectraLayerNc                    t         |           |j                  | _        d| _        t	        ||j
                  |      | _        |j
                  | _        |j                  | _        | j                  r.| j
                  st        |  d      t	        |d|d      | _	        t        |      | _        t        |      | _        y )Nr'   r   z> should be used as a decoder model if cross attention is addedFT)r   r   r   )r6   r7   chunk_size_feed_forwardseq_len_dimr   r   	attentionadd_cross_attentionr   crossattentionr   intermediater   r   )rN   rO   r   rP   s      rQ   r7   zElectraLayer.__init__}  s    '-'E'E$)&F<M<MYbc ++#)#=#= ##?? D6)g!hii"2##'	#D 07#F+rR   r   rr   r   r   r   r   rt   rV   c                 "    | j                   ||f||d|\  }}	|}
| j                  r:|8t        | d      st        d|  d       | j                  |d ||fd|i|\  }}	|}
t        | j                  | j                  | j                  |
      }|S )N)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   r[   r   r   r   feed_forward_chunkr   r   )rN   r   rr   r   r   r   r   rt   self_attention_output_r   cross_attention_outputlayer_outputs                rQ   rc   zElectraLayer.forward  s     $24>>$
 ,)	$

 $
 q 1??4@4!12 =dV DD D 
 )<(;(;%%&	)
 !0) )%"A  60##T%A%A4CSCSUe
 rR   c                 L    | j                  |      }| j                  ||      }|S r   )r   r   )rN   r   intermediate_outputr   s       rQ   r   zElectraLayer.feed_forward_chunk  s,    "//0@A{{#68HIrR   r   r   )rd   re   rf   r7   rG   rk   ri   r   r   r!   r   rc   r   rl   rm   s   @rQ   r   r   |  s    ,, 48:>;?(,.2'||' ))D0'  %0047	'
 !& 1 1D 8' ' t+' +,' 
u||	'RrR   r   c                       e Zd Z fdZ	 	 	 	 	 	 ddej
                  dej                  dz  dej                  dz  dej                  dz  dedz  dedz  d	ej
                  dz  d
e	e
   deej
                     ez  fdZ xZS )ElectraEncoderc           	          t         |           || _        t        j                  t        |j                        D cg c]  }t        ||       c}      | _        y c c}w )N)r   )	r6   r7   rO   r   
ModuleListrangenum_hidden_layersr   layer)rN   rO   irP   s      rQ   r7   zElectraEncoder.__init__  sF    ]]uU[UmUmOn#o!L1$E#op
#os   ANr   rr   r   r   r   	use_cacher   rt   rV   c                     t        | j                        D ]  \  }	}
 |
|||f|||d|} t        ||r|      S d       S )N)r   r   r   last_hidden_stater   )	enumerater   r   )rN   r   rr   r   r   r   r   r   rt   r   layer_modules              rQ   rc   zElectraEncoder.forward  sq      )4 		OA|(% (> /- M		 9+/8O
 	
>B
 	
rR   NNNNNN)rd   re   rf   r7   rG   rk   ri   r   boolr   r!   r   r   rc   rl   rm   s   @rQ   r   r     s    q 48:>;?(,!%.2
||
 ))D0
  %0047	

 !& 1 1D 8
 
 $;
 t+
 +,
 
u||	H	H
rR   r   c                   (     e Zd ZdZ fdZd Z xZS )ElectraDiscriminatorPredictionszEPrediction module for the discriminator, made up of two dense layers.c                    t         |           t        j                  |j                  |j                        | _        t        |j                        | _        t        j                  |j                  d      | _	        || _
        y Nr'   )r6   r7   r   r   r   r   r   r   
activationdense_predictionrO   rM   s     rQ   r7   z(ElectraDiscriminatorPredictions.__init__  s^    YYv1163E3EF
():):; "		&*<*<a @rR   c                     | j                  |      }| j                  |      }| j                  |      j                  d      }|S )Nr1   )r   r  r  squeeze)rN   discriminator_hidden_statesr   logitss       rQ   rc   z'ElectraDiscriminatorPredictions.forward  s?    

#>?6&&}5==bArR   rd   re   rf   rg   r7   rc   rl   rm   s   @rQ   r   r     s    OrR   r   c                   (     e Zd ZdZ fdZd Z xZS )ElectraGeneratorPredictionszAPrediction module for the generator, made up of two dense layers.c                     t         |           t        d      | _        t	        j
                  |j                  |j                        | _        t	        j                  |j                  |j                        | _
        y )Ngelur-   )r6   r7   r   r  r   rA   r:   rB   r   r   r   rM   s     rQ   r7   z$ElectraGeneratorPredictions.__init__  sV    (0f&;&;AVAVWYYv1163H3HI
rR   c                 l    | j                  |      }| j                  |      }| j                  |      }|S r   )r   r  rA   )rN   generator_hidden_statesr   s      rQ   rc   z#ElectraGeneratorPredictions.forward  s3    

#:;6}5rR   r	  rm   s   @rQ   r  r    s    KJrR   r  c                   F     e Zd ZeZdZdZdZdZdZ	dZ
eeedZ fdZ xZS )ElectraPreTrainedModelelectraT)r   
attentionscross_attentionsc                 6   t         |   |       t        |t              ryt	        j
                  |j                  t        j                  |j                  j                  d         j                  d             t	        j                  |j                         y y )Nr1   r0   )r6   _init_weightsr   r*   initcopy_r/   rG   rH   r\   rI   zeros_r3   )rN   rn   rP   s     rQ   r  z$ElectraPreTrainedModel._init_weights  sm    f%f/0JJv**ELL9L9L9R9RSU9V,W,^,^_f,ghKK--. 1rR   )rd   re   rf   r(   config_classbase_model_prefixsupports_gradient_checkpointing_supports_flash_attn_supports_sdpa_supports_flex_attn_supports_attention_backendr   r   r   _can_record_outputsr  rl   rm   s   @rQ   r  r    sF     L!&*#N"&%*1/ /rR   r  z3
    Output type of [`ElectraForPreTraining`].
    )custom_introc                       e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	e
ej                     dz  ed<   dZe
ej                     dz  ed<   y)ElectraForPreTrainingOutputa+  
    loss (*optional*, returned when `labels` is provided, `torch.FloatTensor` of shape `(1,)`):
        Total loss of the ELECTRA objective.
    logits (`torch.FloatTensor` of shape `(batch_size, sequence_length)`):
        Prediction scores of the head (scores for each token before SoftMax).
    Nlossr  r   r  )rd   re   rf   rg   r%  rG   ri   __annotations__r  r   r   r   rR   rQ   r$  r$    sg     &*D%

d
")'+FE$+59M5**+d2926Je''(4/6rR   r$  c                       e Zd Z fdZd Zd Zeee	 	 	 	 	 	 	 	 	 	 dde	j                  dz  de	j                  dz  de	j                  dz  de	j                  dz  d	e	j                  dz  d
e	j                  dz  de	j                  dz  dee	j                     dz  dedz  de	j                  dz  dee   dee	j                     ez  fd                     Zd Z xZS )ElectraModelc                 0   t         |   |       t        |      | _        |j                  |j
                  k7  r/t        j                  |j                  |j
                        | _        t        |      | _
        || _        d| _        | j                          y )NF)r6   r7   r*   rb   r:   r   r   r   embeddings_projectr   encoderrO   gradient_checkpointing	post_initrM   s     rQ   r7   zElectraModel.__init__3  st     +F3  F$6$66&(ii0E0EvGYGY&ZD#%f-&+#rR   c                 .    | j                   j                  S r   rb   r<   rN   s    rQ   get_input_embeddingsz!ElectraModel.get_input_embeddings@  s    ...rR   c                 &    || j                   _        y r   r0  )rN   rq   s     rQ   set_input_embeddingsz!ElectraModel.set_input_embeddingsC  s    */'rR   NrS   rr   r3   r/   rT   r   r   r   r   r   rt   rV   c           
      D   | j                   j                  r|	|	n| j                   j                  }	nd}	|	rd|b|| j                   j                  r4t	        t        | j                         t        | j                               nt        | j                         }|d u |d uz  rt        d      ||j                  }|j                  }n|j                  }|j                  d d }|d   }||j                         nd}|
t        j                  |||z   |      }
| j                  |||||      }t        | d	      r| j                  |      }| j                  |||||
|
      \  }} | j                   |f|||||	|d|}t#        |j$                  |j&                        S )NF)rO   z:You must specify exactly one of input_ids or inputs_embedsr1   r'   r   )rZ   )rS   r/   r3   rT   rU   r+  )rr   r   embedding_outputr   r   r   )rr   r   r   r   r   r/   r   )rO   r   r   is_encoder_decoderr   r   r   rZ   r\   get_seq_lengthrG   rH   rb   r[   r+  _create_attention_masksr,  r   r   r   )rN   rS   rr   r3   r/   rT   r   r   r   r   r   rt   rZ   r^   r`   rU   r6  encoder_outputss                     rQ   rc   zElectraModel.forwardF  s   " ;;!!%.%:	@U@UII0 )48V8V $L$DlZ^ZeZeFfg!5  -t";<YZZ %%F#//K"))F'--cr2K ^
ETE`!?!?!Afg!"\\*@BX[eBentuN??%)'#9 + 
 4-.#667GH151M1M)#9-"7)+ 2N 2
.. '$,,	
)"7#9+%	
 	
 9-??+;;
 	
rR   c                     | j                   j                  rt        | j                   ||||      }nt        | j                   ||      }|t        | j                   |||      }||fS )N)rO   rT   rr   r   r   )rO   rT   rr   )rO   rT   rr   r   )rO   r   r   r   )rN   rr   r   r6  r   r   r   s          rQ   r9  z$ElectraModel._create_attention_masks  sx     ;;!!/{{.-- /N 7{{.-N "-%>{{.5&;	&" 555rR   )
NNNNNNNNNN)rd   re   rf   r7   r2  r4  r%   r&   r"   rG   rk   listri   r   r   r!   r   r   rc   r9  rl   rm   s   @rQ   r)  r)  1  sQ   /0   *..2.2,0-1596::>!%.2J
<<$&J
 t+J
 t+	J

 llT)J
 ||d*J
  %||d2J
 !&t 3J
 e//047J
 $;J
 t+J
 +,J
 
u||	A	AJ
    J
Z 6rR   r)  c                   (     e Zd ZdZ fdZd Z xZS )ElectraClassificationHeadz-Head for sentence-level classification tasks.c                 z   t         |           t        j                  |j                  |j                        | _        |j                  |j                  n|j                  }t        d      | _	        t        j                  |      | _        t        j                  |j                  |j                        | _        y )Nr  )r6   r7   r   r   r   r   classifier_dropoutrD   r   r  rC   rE   
num_labelsout_projrN   rO   r@  rP   s      rQ   r7   z"ElectraClassificationHead.__init__  s    YYv1163E3EF
)/)B)B)NF%%TZTnTn 	 )0zz"45		&"4"4f6G6GHrR   c                     |d d dd d f   }| j                  |      }| j                  |      }| j                  |      }| j                  |      }| j                  |      }|S )Nr   )rE   r   r  rB  )rN   featuresrt   xs       rQ   rc   z!ElectraClassificationHead.forward  sZ    Q1WLLOJJqMOOALLOMM!rR   r	  rm   s   @rQ   r>  r>    s    7IrR   r>  c                        e Zd ZdZdef fdZ	 d	dej                  dej                  dz  dej                  fdZ	 xZ
S )
ElectraSequenceSummarya  
    Compute a single vector summary of a sequence hidden states.

    Args:
        config ([`ElectraConfig`]):
            The config used by the model. Relevant arguments in the config class of the model are (refer to the actual
            config class of your model for the default values it uses):

            - **summary_type** (`str`) -- The method to use to make this summary. Accepted values are:

                - `"last"` -- Take the last token hidden state (like XLNet)
                - `"first"` -- Take the first token hidden state (like Bert)
                - `"mean"` -- Take the mean of all tokens hidden states
                - `"cls_index"` -- Supply a Tensor of classification token position (GPT/GPT-2)
                - `"attn"` -- Not implemented now, use multi-head attention

            - **summary_use_proj** (`bool`) -- Add a projection after the vector extraction.
            - **summary_proj_to_labels** (`bool`) -- If `True`, the projection outputs to `config.num_labels` classes
              (otherwise to `config.hidden_size`).
            - **summary_activation** (`Optional[str]`) -- Set to `"tanh"` to add a tanh activation to the output,
              another string or `None` will add no activation.
            - **summary_first_dropout** (`float`) -- Optional dropout probability before the projection and activation.
            - **summary_last_dropout** (`float`)-- Optional dropout probability after the projection and activation.
    rO   c                 f   t         |           t        |dd      | _        | j                  dk(  rt        t        j                         | _        t        |d      rq|j                  ret        |d      r(|j                  r|j                  dkD  r|j                  }n|j                  }t        j                  |j                  |      | _        t        |dd       }|rt        |      nt        j                         | _        t        j                         | _        t        |d      r3|j"                  dkD  r$t        j$                  |j"                        | _        t        j                         | _        t        |d	      r5|j(                  dkD  r%t        j$                  |j(                        | _        y y y )
Nsummary_typelastattnsummary_use_projsummary_proj_to_labelsr   summary_activationsummary_first_dropoutsummary_last_dropout)r6   r7   getattrrJ  NotImplementedErrorr   Identitysummaryr[   rM  rN  rA  r   r   r   r  first_dropoutrP  rC   last_dropoutrQ  )rN   rO   num_classesactivation_stringrP   s       rQ   r7   zElectraSequenceSummary.__init__  sU   #FNFC& &%{{}6-.63J3Jv78V=Z=Z_e_p_pst_t$//$0099V%7%7EDL#F,@$GIZN3D$E`b`k`k`m[[]6238T8TWX8X!#F,H,H!IDKKM612v7R7RUV7V "

6+F+F GD 8W2rR   Nr   	cls_indexrV   c                    | j                   dk(  r|dddf   }n| j                   dk(  r|dddf   }n| j                   dk(  r|j                  d      }n| j                   d	k(  r|At        j                  |d
ddddf   |j                  d   dz
  t        j
                        }nX|j                  d      j                  d      }|j                  d|j                         dz
  z  |j                  d      fz         }|j                  d|      j                  d      }n| j                   dk(  rt        | j                        }| j                  |      }| j                  |      }| j!                  |      }|S )ak  
        Compute a single vector summary of a sequence hidden states.

        Args:
            hidden_states (`torch.FloatTensor` of shape `[batch_size, seq_len, hidden_size]`):
                The hidden states of the last layer.
            cls_index (`torch.LongTensor` of shape `[batch_size]` or `[batch_size, ...]` where ... are optional leading dimensions of `hidden_states`, *optional*):
                Used if `summary_type == "cls_index"` and takes the last token of the sequence as classification token.

        Returns:
            `torch.FloatTensor`: The summary of the sequence hidden states.
        rK  Nr1   firstr   meanr'   rx   rZ  .r4   )r1   rL  )rJ  r]  rG   	full_liker\   rL   	unsqueezerI   rX   rK   r]   r  rS  rV  rU  r  rW  )rN   r   rZ  r   s       rQ   rc   zElectraSequenceSummary.forward  sn    &"1b5)F')"1a4(F&("''A'.F+- !OO!#rr1*-!''+a/**	 &//3==bA	%,,Uimmo6I-JmN`N`acNdMf-fg	"))"i8@@DF&(%%##F+f%(""6*rR   r   )rd   re   rf   rg   r(   r7   rG   ri   rh   rc   rl   rm   s   @rQ   rH  rH    sQ    2H} H< VZ)"..);@;K;Kd;R)			)rR   rH  z
    ELECTRA 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                   *    e Zd Z fdZee	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ee	   d
e
ej                     ez  fd              Z xZS ) ElectraForSequenceClassificationc                     t         |   |       |j                  | _        || _        t	        |      | _        t        |      | _        | j                          y r   )	r6   r7   rA  rO   r)  r  r>  
classifierr.  rM   s     rQ   r7   z)ElectraForSequenceClassification.__init__;  sH      ++#F+3F; 	rR   NrS   rr   r3   r/   rT   labelsrt   rV   c           	          | j                   |f||||dd|}|d   }	| j                  |	      }
d}|| j                  j                  | j                  dk(  rd| j                  _        nl| j                  dkD  rL|j
                  t        j                  k(  s|j
                  t        j                  k(  rd| j                  _        nd| j                  _        | j                  j                  dk(  rIt               }| j                  dk(  r& ||
j                         |j                               }n ||
|      }n| j                  j                  dk(  r=t               } ||
j                  d	| j                        |j                  d	            }n,| j                  j                  dk(  rt               } ||
|      }t        ||
|j                  |j                   
      S )a  
        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).
        Trr   r3   r/   rT   return_dictr   Nr'   
regressionsingle_label_classificationmulti_label_classificationr1   r%  r  r   r  )r  rd  rO   problem_typerA  r5   rG   rL   rj   r   r  r   r   r   r   r   r  )rN   rS   rr   r3   r/   rT   re  rt   r  sequence_outputr  r%  loss_fcts                rQ   rc   z(ElectraForSequenceClassification.forwardE  s   $ '3dll'
))%''
 '
# 6a81{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#FNN$4fnn6FGD#FF3D))-JJ+-B @&++b/R))-II,./'5CC2==	
 	
rR   r   )rd   re   rf   r7   r$   r"   rG   rk   r   r!   r   r   rc   rl   rm   s   @rQ   rb  rb  4  s      *..2.2,0-1&*9
<<$&9
 t+9
 t+	9

 llT)9
 ||d*9
 t#9
 +,9
 
u||	7	79
  9
rR   rb  z
    Electra model with a binary classification head on top as used during pretraining for identifying generated tokens.

    It is recommended to load the discriminator checkpoint into that model.
    c                   *    e Zd Z fdZee	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ee	   d
e
ej                     ez  fd              Z xZS )ElectraForPreTrainingc                     t         |   |       t        |      | _        t	        |      | _        | j                          y r   )r6   r7   r)  r  r   discriminator_predictionsr.  rM   s     rQ   r7   zElectraForPreTraining.__init__  s3     #F+)H)P&rR   NrS   rr   r3   r/   rT   re  rt   rV   c           	          | j                   |f||||dd|}|d   }	| j                  |	      }
d}|t        j                         }|a|j	                  d|	j
                  d         dk(  }|
j	                  d|	j
                  d         |   }||   } |||j                               }n4 ||
j	                  d|	j
                  d         |j                               }t        ||
|j                  |j                        S )am  
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the ELECTRA loss. Input should be a sequence of tokens (see `input_ids` docstring)
            Indices should be in `[0, 1]`:

            - 0 indicates the token is an original token,
            - 1 indicates the token was replaced.

        Examples:

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

        >>> discriminator = ElectraForPreTraining.from_pretrained("google/electra-base-discriminator")
        >>> tokenizer = AutoTokenizer.from_pretrained("google/electra-base-discriminator")

        >>> sentence = "The quick brown fox jumps over the lazy dog"
        >>> fake_sentence = "The quick brown fox fake over the lazy dog"

        >>> fake_tokens = tokenizer.tokenize(fake_sentence, add_special_tokens=True)
        >>> fake_inputs = tokenizer.encode(fake_sentence, return_tensors="pt")
        >>> discriminator_outputs = discriminator(fake_inputs)
        >>> predictions = torch.round((torch.sign(discriminator_outputs[0]) + 1) / 2)

        >>> fake_tokens
        ['[CLS]', 'the', 'quick', 'brown', 'fox', 'fake', 'over', 'the', 'lazy', 'dog', '[SEP]']

        >>> predictions.squeeze().tolist()
        [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0]
        ```Trg  r   Nr1   r'   rl  )
r  rs  r   r   r   r\   floatr$  r   r  )rN   rS   rr   r3   r/   rT   re  rt   r  discriminator_sequence_outputr  r%  ro  active_lossactive_logitsactive_labelss                   rQ   rc   zElectraForPreTraining.forward  s/   V '3dll'
))%''
 '
# )DA(F%//0MN++-H),11"6S6Y6YZ[6\]abb &B0M0S0STU0V WXc d &{ 3}/B/B/DEB0M0S0STU0V WY_YeYeYgh*5CC2==	
 	
rR   r   )rd   re   rf   r7   r$   r"   rG   rk   r   r!   r   r$  rc   rl   rm   s   @rQ   rq  rq    s      *..2.2,0-1&*F
<<$&F
 t+F
 t+	F

 llT)F
 ||d*F
 t#F
 +,F
 
u||	:	:F
  F
rR   rq  z
    Electra model with a language modeling head on top.

    Even though both the discriminator and generator may be loaded into this model, the generator is the only model of
    the two to have been trained for the masked language modeling task.
    c                   >    e Zd ZddiZ fdZd Zd Zee	 	 	 	 	 	 dde	j                  dz  de	j                  dz  d	e	j                  dz  d
e	j                  dz  de	j                  dz  de	j                  dz  dee   dee	j                     ez  fd              Z xZS )ElectraForMaskedLMgenerator_lm_head.weight)electra.embeddings.word_embeddings.weightc                     t         |   |       t        |      | _        t	        |      | _        t        j                  |j                  |j                        | _
        | j                          y r   )r6   r7   r)  r  r  generator_predictionsr   r   r:   r9   generator_lm_headr.  rM   s     rQ   r7   zElectraForMaskedLM.__init__  sR     #F+%@%H"!#6+@+@&BSBS!TrR   c                     | j                   S r   r  r1  s    rQ   get_output_embeddingsz(ElectraForMaskedLM.get_output_embeddings      %%%rR   c                     || _         y r   r  )rN   r<   s     rQ   set_output_embeddingsz(ElectraForMaskedLM.set_output_embeddings  s
    !0rR   NrS   rr   r3   r/   rT   re  rt   rV   c           	      p    | j                   |f||||dd|}|d   }	| j                  |	      }
| j                  |
      }
d}|Pt        j                         } ||
j                  d| j                  j                        |j                  d            }t        ||
|j                  |j                        S )a  
        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]`
        Trg  r   Nr1   rl  )r  r  r  r   r   r   rO   r9   r   r   r  )rN   rS   rr   r3   r/   rT   re  rt   r  generator_sequence_outputprediction_scoresr%  ro  s                rQ   rc   zElectraForMaskedLM.forward  s    $ #/$,,#
))%'#
 #
 %<A$>! 667PQ 223DE**,H-222t{{7M7MNPVP[P[\^P_`D$1??.99	
 	
rR   r   )rd   re   rf   _tied_weights_keysr7   r  r  r$   r"   rG   rk   r   r!   r   r   rc   rl   rm   s   @rQ   r{  r{    s     56ab&1  *..2.2,0-1&*)
<<$&)
 t+)
 t+	)

 llT))
 ||d*)
 t#)
 +,)
 
u||	~	-)
  )
rR   r{  z
    Electra model with a token classification head on top.

    Both the discriminator and generator may be loaded into this model.
    c                   *    e Zd Z fdZee	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ee	   d
e
ej                     ez  fd              Z xZS )ElectraForTokenClassificationc                 `   t         |   |       |j                  | _        t        |      | _        |j
                  |j
                  n|j                  }t        j                  |      | _	        t        j                  |j                  |j                        | _        | j                          y r   )r6   r7   rA  r)  r  r@  rD   r   rC   rE   r   r   rd  r.  rC  s      rQ   r7   z&ElectraForTokenClassification.__init__/  s      ++#F+)/)B)B)NF%%TZTnTn 	 zz"45))F$6$68I8IJrR   NrS   rr   r3   r/   rT   re  rt   rV   c           	      H    | j                   |f||||dd|}|d   }	| j                  |	      }	| j                  |	      }
d}|<t               } ||
j	                  d| j
                        |j	                  d            }t        ||
|j                  |j                        S )z
        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]`.
        Trg  r   Nr1   rl  )	r  rE   rd  r   r   rA  r   r   r  )rN   rS   rr   r3   r/   rT   re  rt   r  rv  r  r%  ro  s                rQ   rc   z%ElectraForTokenClassification.forward<  s      '3dll'
))%''
 '
# )DA(F%(,5R(S%!>?')HFKKDOO<fkk"oND$5CC2==	
 	
rR   r   )rd   re   rf   r7   r$   r"   rG   rk   r   r!   r   r   rc   rl   rm   s   @rQ   r  r  '  s      *..2.2,0-1&*&
<<$&&
 t+&
 t+	&

 llT)&
 ||d*&
 t#&
 +,&
 
u||	4	4&
  &
rR   r  c                   R    e Zd ZeZdZ fdZee	 	 	 	 	 	 	 dde	j                  dz  de	j                  dz  de	j                  dz  de	j                  dz  de	j                  dz  d	e	j                  dz  d
e	j                  dz  dee   dee	j                     ez  fd              Z xZS )ElectraForQuestionAnsweringr  c                     t         |   |       |j                  | _        t        |      | _        t        j                  |j                  |j                        | _        | j                          y r   )
r6   r7   rA  r)  r  r   r   r   
qa_outputsr.  rM   s     rQ   r7   z$ElectraForQuestionAnswering.__init__l  sS      ++#F+))F$6$68I8IJ 	rR   NrS   rr   r3   r/   rT   start_positionsend_positionsrt   rV   c           	          | j                   |f||||dd|}	|	d   }
| j                  |
      }|j                  dd      \  }}|j                  d      j	                         }|j                  d      j	                         }d }||t        |j                               dkD  r|j                  d      }t        |j                               dkD  r|j                  d      }|j                  d      }|j                  d|      }|j                  d|      }t        |      } |||      } |||      }||z   dz  }t        ||||	j                  |	j                  	      S )
NTrg  r   r'   r1   rx   )ignore_indexrw   )r%  start_logits
end_logitsr   r  )r  r  splitr  r   lenrK   clampr   r   r   r  )rN   rS   rr   r3   r/   rT   r  r  rt   r  rn  r  r  r  
total_lossignored_indexro  
start_lossend_losss                      rQ   rc   z#ElectraForQuestionAnswering.forwardv  s    '3dll'
))%''
 '
# 6a8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+%!5CC2==
 	
rR   )NNNNNNN)rd   re   rf   r(   r  r  r7   r$   r"   rG   rk   r   r!   r   r   rc   rl   rm   s   @rQ   r  r  g  s     L!  *..2.2,0-1/3-13
<<$&3
 t+3
 t+	3

 llT)3
 ||d*3
 ,3
 ||d*3
 +,3
 
u||	;	;3
  3
rR   r  c                   *    e Zd Z fdZee	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ee	   d
e
ej                     ez  fd              Z xZS )ElectraForMultipleChoicec                     t         |   |       t        |      | _        t	        |      | _        t        j                  |j                  d      | _	        | j                          y r  )r6   r7   r)  r  rH  sequence_summaryr   r   r   rd  r.  rM   s     rQ   r7   z!ElectraForMultipleChoice.__init__  sM     #F+ 6v >))F$6$6: 	rR   NrS   rr   r3   r/   rT   re  rt   rV   c           	         ||j                   d   n|j                   d   }|!|j                  d|j                  d            nd}|!|j                  d|j                  d            nd}|!|j                  d|j                  d            nd}|!|j                  d|j                  d            nd}|1|j                  d|j                  d      |j                  d            nd} | j                  |f||||dd|}	|	d   }
| j	                  |
      }| j                  |      }|j                  d|      }d}|t               } |||      }t        |||	j                  |	j                        S )	a[  
        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)
        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   r^  Trg  r   rl  )
r\   r   rK   r  r  rd  r   r   r   r  )rN   rS   rr   r3   r/   rT   re  rt   num_choicesr  rn  pooled_outputr  reshaped_logitsr%  ro  s                   rQ   rc   z ElectraForMultipleChoice.forward  s   T -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 	 '3dll'
))%''
 '
# 6a8--o>/ ++b+6')HOV4D("5CC2==	
 	
rR   r   )rd   re   rf   r7   r$   r"   rG   rk   r   r!   r   r   rc   rl   rm   s   @rQ   r  r    s      *..2.2,0-1&*N
<<$&N
 t+N
 t+	N

 llT)N
 ||d*N
 t#N
 +,N
 
u||	8	8N
  N
rR   r  zS
    ELECTRA Model with a `language modeling` head on top for CLM fine-tuning.
    c                        e Zd ZddiZ fdZd Zd Zee	 	 	 	 	 	 	 	 	 	 	 	 dde	j                  dz  de	j                  dz  d	e	j                  dz  d
e	j                  dz  de	j                  dz  de	j                  dz  de	j                  dz  de	j                  dz  dedz  dedz  de	j                  dz  dee	j                  z  dee   dee	j                     ez  fd              Z xZS )ElectraForCausalLMr|  r}  c                 $   t         |   |       |j                  st        j	                  d       t        |      | _        t        |      | _        t        j                  |j                  |j                        | _        | j                          y )NzOIf you want to use `ElectraForCausalLM` as a standalone, add `is_decoder=True.`)r6   r7   r   loggerwarningr)  r  r  r  r   r   r:   r9   r  r.  rM   s     rQ   r7   zElectraForCausalLM.__init__  sh       NNlm#F+%@%H"!#6+@+@&BSBS!TrR   c                     | j                   S r   r  r1  s    rQ   r  z(ElectraForCausalLM.get_output_embeddings!  r  rR   c                     || _         y r   r  )rN   new_embeddingss     rQ   r  z(ElectraForCausalLM.set_output_embeddings$  s
    !/rR   NrS   rr   r3   r/   rT   r   r   re  r   r   r   logits_to_keeprt   rV   c                    |d}
 | j                   |f|||||||	|
|dd
|}|j                  }t        |t              rt	        | d      n|}| j                  | j                  |dd|ddf               }d}|* | j                  d||| j                  j                  d|}t        |||j                  |j                  |j                  |j                        S )a3  
        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 in `[0, ..., config.vocab_size]`

        Example:

        ```python
        >>> from transformers import AutoTokenizer, ElectraForCausalLM, ElectraConfig
        >>> import torch

        >>> tokenizer = AutoTokenizer.from_pretrained("google/electra-base-generator")
        >>> config = ElectraConfig.from_pretrained("google/electra-base-generator")
        >>> config.is_decoder = True
        >>> model = ElectraForCausalLM.from_pretrained("google/electra-base-generator", config=config)

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

        >>> prediction_logits = outputs.logits
        ```NFT)
rr   r3   r/   rT   r   r   r   r   r   rh  )r  re  r9   )r%  r  r   r   r  r  r'  )r  r   r   rj   slicer  r  loss_functionrO   r9   r   r   r   r  r  )rN   rS   rr   r3   r/   rT   r   r   re  r   r   r   r  rt   outputsr   slice_indicesr  r%  s                      rQ   rc   zElectraForCausalLM.forward'  s   P I=IT\\>
))%'"7#9+)>
 >
  118B>SV8W~ot4]k''(B(B=QRTacdQdCe(fg%4%%pVFt{{OeOepiopD0#33!//))$55
 	
rR   )NNNNNNNNNNNr   )rd   re   rf   r  r7   r  r  r$   r"   rG   rk   r   r   rj   r   r!   r   r   rc   rl   rm   s   @rQ   r  r    ss    56ab
&0  *..2.2,0-1596:&*(,!%.2-.H
<<$&H
 t+H
 t+	H

 llT)H
 ||d*H
  %||d2H
 !&t 3H
 t#H
 H
 $;H
 t+H
 ell*H
 +,H
 
u||	@	@H
  H
rR   r  )	r  r{  r  rq  r  rb  r  r)  r  )Nr   )Wrg   collections.abcr   dataclassesr   rG   r   torch.nnr   r   r    r
   r  activationsr   r   cache_utilsr   r   r   
generationr   masking_utilsr   r   modeling_layersr   modeling_outputsr   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_electrar(   
get_loggerrd   r  Moduler*   rk   ru  r   r   r   r   r   r   r   r   r   r   r  r  r$  r)  r>  rH  rb  rq  r{  r  r  r  r  __all__r'  rR   rQ   <module>r     sK    $ !   A A & 1 C C ) J 9	 	 	 G & 6  J 5 0 
		H	%=		 =N !%II%<<% 
% <<	%
 LL4'% T\% % '(%:F)299 F)TJ)BII J)\		 .ryy .@"))  BII @- @H
RYY 
Dbii &")) $ /_ / /* 
7+ 7 7 D6) D6 D6N		 0`RYY `F F
'= F
F
R Q
2 Q
Q
h >
/ >
>
B 6
$: 6
6
r C
"8 C
 C
L [
5 [
 [
| 
_
/ _

_
D
rR   