
    <h                    .   S r SSKrSSKJrJr  SSKrSSK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Jr  SS	K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#  SSK$J%r%  SSK&J'r'J(r(J)r)  SSK*J+r+J,r,J-r-  SSK.J/r/  \-R`                  " \15      r2 " S S\	Rf                  5      r4 " S S\	Rf                  5      r5 " S S\55      r6 " S S\	Rf                  5      r7\5\6S.r8 " S S\	Rf                  5      r9 " S S\	Rf                  5      r: " S  S!\	Rf                  5      r; " S" S#\5      r< " S$ S%\	Rf                  5      r= " S& S'\	Rf                  5      r>\+ " S( S)\%5      5       r? " S* S+\	Rf                  5      r@ " S, S-\	Rf                  5      rA\+ " S. S/\?5      5       rB\+ " S0 S1\?5      5       rC\+" S2S39 " S4 S5\?5      5       rD\+ " S6 S7\?5      5       rE\+ " S8 S9\?5      5       rF\+ " S: S;\?5      5       rG\+" S<S39 " S= S>\?\5      5       rHSAS? jrI/ S@QrJg)BzPyTorch CamemBERT model.    N)OptionalUnion)version)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )ACT2FNgelu)CacheEncoderDecoderCache)GenerationMixin)#_prepare_4d_attention_mask_for_sdpa*_prepare_4d_causal_attention_mask_for_sdpa)GradientCheckpointingLayer))BaseModelOutputWithPastAndCrossAttentions,BaseModelOutputWithPoolingAndCrossAttentions!CausalLMOutputWithCrossAttentionsMaskedLMOutputMultipleChoiceModelOutputQuestionAnsweringModelOutputSequenceClassifierOutputTokenClassifierOutput)PreTrainedModel)apply_chunking_to_forward find_pruneable_heads_and_indicesprune_linear_layer)auto_docstringget_torch_versionlogging   )CamembertConfigc                   >   ^  \ rS rSrSrU 4S jr SS jrS rSrU =r	$ )CamembertEmbeddings4   zN
Same as BertEmbeddings with a tiny tweak for positional embeddings indexing.
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SS5      U l        U R'                  S[(        R*                  " UR                  5      R-                  S5      SS9  U R'                  S	[(        R.                  " U R0                  R3                  5       [(        R4                  S
9SS9  UR                  U l        [        R                  " UR                  UR
                  U R6                  S9U l	        g )N)padding_idxepsposition_embedding_typeabsoluteposition_ids)r"   F)
persistenttoken_type_idsdtype)super__init__r   	Embedding
vocab_sizehidden_sizepad_token_idword_embeddingsmax_position_embeddingsposition_embeddingstype_vocab_sizetoken_type_embeddings	LayerNormlayer_norm_epsDropouthidden_dropout_probdropoutgetattrr+   register_buffertorcharangeexpandzerosr-   sizelongr(   selfconfig	__class__s     h/var/www/html/shao/venv/lib/python3.13/site-packages/transformers/models/camembert/modeling_camembert.pyr4   CamembertEmbeddings.__init__:   si   !||F,=,=v?Q?Q_e_r_rs#%<<0N0NPVPbPb#c %'\\&2H2H&J\J\%]" f&8&8f>S>STzz&"<"<='.v7PR\']$ELL)G)GHOOPWXej 	 	
 	ekk$*;*;*@*@*B%**Ubg 	 	

 "..#%<<**F,>,>DL\L\$
     c                    Uc+  Ub  [        XR                  U5      nOU R                  U5      nUb  UR                  5       nOUR                  5       S S nUS   nUcv  [	        U S5      (       a-  U R
                  S S 2S U24   nUR                  US   U5      n	U	nO8[        R                  " U[        R                  U R                  R                  S9nUc  U R                  U5      nU R                  U5      n
XJ-   nU R                  S:X  a  U R                  U5      nX-  nU R!                  U5      nU R#                  U5      nU$ )Nr.   r"   r0   r   r2   devicer,   )"create_position_ids_from_input_idsr(   &create_position_ids_from_inputs_embedsrI   hasattrr0   rG   rE   rH   rJ   r-   rT   r9   r=   r+   r;   r>   rB   )rL   	input_idsr0   r-   inputs_embedspast_key_values_lengthinput_shape
seq_lengthbuffered_token_type_ids buffered_token_type_ids_expandedr=   
embeddingsr;   s                rO   forwardCamembertEmbeddings.forwardS   sM    $A)M]M]_uv#JJ=Y #..*K',,.s3K ^

 !t-..*.*=*=a*n*M'3J3Q3QR]^_R`bl3m0!A!&[

SWSdSdSkSk!l  00;M $ : :> J":
'':5"&":":<"H-J^^J/
\\*-
rQ   c                    UR                  5       SS nUS   n[        R                  " U R                  S-   X0R                  -   S-   [        R                  UR
                  S9nUR                  S5      R                  U5      $ )z
We are provided embeddings directly. We cannot infer which are padded so just generate sequential position ids.

Args:
    inputs_embeds: torch.Tensor

Returns: torch.Tensor
Nr.   r"   rS   r   )rI   rE   rF   r(   rJ   rT   	unsqueezerG   )rL   rY   r[   sequence_lengthr-   s        rO   rV   :CamembertEmbeddings.create_position_ids_from_inputs_embeds{   s~     $((*3B/%a.||q /4D4D"Dq"HPUPZPZcpcwcw
 %%a(//<<rQ   )r>   rB   r(   r+   r;   r=   r9   )NNNNr   )
__name__
__module____qualname____firstlineno____doc__r4   r`   rV   __static_attributes____classcell__rN   s   @rO   r%   r%   4   s$    

4 rs&P= =rQ   r%   c                     ^  \ rS rSrSU 4S jjr      SS\R                  S\\R                     S\\R                     S\\R                     S\\	   S\\
   S	\\R                     S
\\R                     4S jjrSrU =r$ )CamembertSelfAttention   c                   > [         TU ]  5         UR                  UR                  -  S:w  a7  [	        US5      (       d&  [        SUR                   SUR                   S35      eUR                  U l        [        UR                  UR                  -  5      U l        U R                  U R                  -  U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  5      U l        U=(       d    [#        USS5      U l        U R$                  S:X  d  U R$                  S	:X  aG  UR&                  U l        [        R(                  " S
UR&                  -  S-
  U R                  5      U l        UR,                  U l        X0l        g )Nr   embedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads ()r+   r,   relative_keyrelative_key_query   r"   )r3   r4   r7   num_attention_headsrW   
ValueErrorintattention_head_sizeall_head_sizer   Linearquerykeyvaluer@   attention_probs_dropout_probrB   rC   r+   r:   r5   distance_embedding
is_decoder	layer_idxrL   rM   r+   r   rN   s       rO   r4   CamembertSelfAttention.__init__   s    : ::a?PVXhHiHi#F$6$6#7 8 445Q8 
 $*#=#= #&v'9'9F<V<V'V#W !558P8PPYYv1143E3EF
99V//1C1CDYYv1143E3EF
zz&"E"EF'> (
'-zC
$ ''>9T=Y=Y]q=q+1+I+ID(&(ll1v7U7U3UXY3Y[_[s[s&tD# ++"rQ   hidden_statesattention_mask	head_maskencoder_hidden_statespast_key_valueoutput_attentionscache_positionreturnc                 `	   UR                   u  pn
U R                  U5      nUR                  USU R                  U R                  5      R                  SS5      nUS LnUb]  [        U[        5      (       aF  UR                  R                  U R                  5      nU(       a  UR                  nOUR                  nOUnU(       a  UOUnU(       aQ  UbN  W(       aG  WR                  U R                     R                  nUR                  U R                     R                  nOU R!                  U5      nUR                  USU R                  U R                  5      R                  SS5      nU R#                  U5      nUR                  USU R                  U R                  5      R                  SS5      nUbN  U(       d  UOS nWR%                  UUU R                  SU05      u  nnU(       a  SUR                  U R                  '   [&        R(                  " UUR                  SS5      5      nU R*                  S:X  d  U R*                  S:X  Ga  UR                   S   UR                   S   nnUbB  [&        R,                  " US-
  [&        R.                  UR0                  S	9R                  SS5      nO>[&        R2                  " U[&        R.                  UR0                  S	9R                  SS5      n[&        R2                  " U[&        R.                  UR0                  S	9R                  SS5      nUU-
  nU R5                  UU R6                  -   S-
  5      nUR9                  UR:                  S
9nU R*                  S:X  a  [&        R<                  " SUU5      nUU-   nOHU R*                  S:X  a8  [&        R<                  " SUU5      n[&        R<                  " SUU5      nUU-   U-   nU[>        R@                  " U R                  5      -  nUb  UU-   n[B        RD                  RG                  USS9nU RI                  U5      nUb  UU-  n[&        R(                  " UU5      nURK                  SSSS5      RM                  5       nURO                  5       S S U RP                  4-   nUR                  U5      nUU4$ )Nr.   r"   rv   r   Trt   ru   rS   r1   zbhld,lrd->bhlrzbhrd,lrd->bhlrdimr   r
   ))shaper}   viewrw   rz   	transpose
isinstancer   
is_updatedgetr   cross_attention_cacheself_attention_cachelayerskeysvaluesr~   r   updaterE   matmulr+   tensorrJ   rT   rF   r   r:   tor2   einsummathsqrtr   
functionalsoftmaxrB   permute
contiguousrI   r{   )rL   r   r   r   r   r   r   r   
batch_sizer\   _query_layeris_cross_attentionr   curr_past_key_valuecurrent_states	key_layervalue_layerattention_scoresquery_length
key_lengthposition_ids_lposition_ids_rdistancepositional_embeddingrelative_position_scoresrelative_position_scores_queryrelative_position_scores_keyattention_probscontext_layernew_context_layer_shapes                                  rO   r`   CamembertSelfAttention.forward   sO    %2$7$7!
jj/!&&z2t7O7OQUQiQijttq
 3$>%.*=>>+66::4>>J
%*8*N*N'*8*M*M'&4#2D.-."<+224>>BGGI-44T^^DKKK0I!z2t7O7OQUQiQijtt1I **^4K%**B 8 8$:R:Ri1o  )7It)<)C)C{DNN=M~<^*&	; &@DN--dnn= !<<Y5H5HR5PQ''>9T=Y=Y]q=q'2'8'8';Y__Q=O*L)!&j1nEJJWdWkWk!l!q!q" "'l%**UbUiUi!j!o!oprtu!v"\\*EJJ}OcOcdiijkmopN%6H#'#:#:8dFbFb;bef;f#g #7#:#:ARAR#:#S ++~=+0<<8H+Wk+l(#36N#N --1EE16>NP[]q1r./4||<LiYm/n,#36T#TWs#s +dii8P8P.QQ%/.@ --//0@b/I ,,7  -	9O_kB%--aAq9DDF"/"4"4"6s";t?Q?Q>S"S%**+BCo--rQ   )r{   rz   r   rB   r   r~   r   r:   rw   r+   r}   r   NNNNNNFNrf   rg   rh   ri   r4   rE   Tensorr   FloatTensorr   booltupler`   rk   rl   rm   s   @rO   ro   ro      s    #< 7;15=A*.,115d.||d. !!2!23d. E--.	d.
  ((9(9:d. !d. $D>d. !.d. 
u||	d. d.rQ   ro   c                     ^  \ rS rSrSU 4S jjr      SS\R                  S\\R                     S\\R                     S\\R                     S\\	   S\\
   S	\\R                     S
\\R                     4U 4S jjjrSrU =r$ )CamembertSdpaSelfAttentioni  c                    > [         TU ]  XUS9  UR                  U l        [        R
                  " [        5       5      [        R
                  " S5      :  U l        g )Nr+   r   z2.2.0)r3   r4   r   dropout_probr   parser    require_contiguous_qkvr   s       rO   r4   #CamembertSdpaSelfAttention.__init__  sH    \ef"??&-mm4E4G&H7==Y`Ka&a#rQ   r   r   r   r   r   r   r   r   c           	        > U R                   S:w  d
  U(       d  Ub*  [        R                  S5        [        TU ]  UUUUUUU5      $ UR                  5       u  pn
U R                  U5      R                  USU R                  U R                  5      R                  SS5      nUS LnU(       a  UOUnUb]  [        U[        5      (       aF  UR                  R                  U R                  5      nU(       a  UR                   nOUR"                  nOUnU(       a  UOUnU(       aQ  UbN  W(       aG  WR$                  U R                     R&                  nUR$                  U R                     R(                  nOU R+                  U5      R                  USU R                  U R                  5      R                  SS5      nU R-                  U5      R                  USU R                  U R                  5      R                  SS5      nUbN  U(       d  UOS nWR/                  UUU R                  SU05      u  nnU(       a  SUR                  U R                  '   U R0                  (       aM  UR2                  R4                  S:X  a3  Ub0  UR7                  5       nUR7                  5       nUR7                  5       nU R8                  =(       a    U(       + =(       a    US L =(       a    U	S:  n[:        R<                  R>                  RA                  UUUUU RB                  (       a  U RD                  OS	US
9nUR                  SS5      nURG                  XU RH                  5      nUS 4$ )Nr,   a  CamembertSdpaSelfAttention is used but `torch.nn.functional.scaled_dot_product_attention` does not support non-absolute `position_embedding_type` or `output_attentions=True` or `head_mask`. Falling back to the manual attention implementation, but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.r.   r"   rv   r   Tcuda        )	attn_mask	dropout_p	is_causal)%r+   loggerwarning_oncer3   r`   rI   r}   r   rw   rz   r   r   r   r   r   r   r   r   r   r   r   r~   r   r   r   rT   typer   r   rE   r   r   scaled_dot_product_attentiontrainingr   reshaper{   )rL   r   r   r   r   r   r   r   bsztgt_lenr   r   r   r   r   r   r   r   r   attn_outputrN   s                       rO   r`   "CamembertSdpaSelfAttention.forward  s    '':59JiNcH 7?%!  (,,.a JJ}%**3D4L4LdNfNfgqqrsuvw 	 3$>2D.-%.*=>>+66::4>>J
%*8*N*N'*8*M*M'&4#2D.-."<+224>>BGGI-44T^^DKKK (c2t779Q9QR1a  

>*c2t779Q9QR1a  )7It)<)C)C{DNN=M~<^*&	; &@DN--dnn=
 &&;+=+=+B+Bf+LQ_Qk%002K!,,.I%002K OOi,>(>i>UYCYi^ehi^i	hh))FF$+/==d''c G 
 "++Aq1!))#8J8JKD  rQ   )r   r   r   r   r   rm   s   @rO   r   r     s    b 2615=A*.,115e!||e! !.e! E--.	e!
  ((9(9:e! !e! $D>e! !.e! 
u||	e! e!rQ   r   c                   z   ^  \ rS rSrU 4S jrS\R                  S\R                  S\R                  4S jrSrU =r	$ )CamembertSelfOutputi  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)   )r3   r4   r   r|   r7   denser>   r?   r@   rA   rB   rK   s     rO   r4   CamembertSelfOutput.__init__  s`    YYv1163E3EF
f&8&8f>S>STzz&"<"<=rQ   r   input_tensorr   c                 p    U R                  U5      nU R                  U5      nU R                  X-   5      nU$ Nr   rB   r>   rL   r   r   s      rO   r`   CamembertSelfOutput.forward  5    

=1]3}'CDrQ   r>   r   rB   
rf   rg   rh   ri   r4   rE   r   r`   rk   rl   rm   s   @rO   r   r     6    >U\\  RWR^R^  rQ   r   )eagersdpac                     ^  \ rS rSrSU 4S jjrS r      SS\R                  S\\R                     S\\R                     S\\R                     S\\
   S	\\   S
\\R                     S\\R                     4S jjrSrU =r$ )CamembertAttentioni  c                    > [         TU ]  5         [        UR                     " UUUS9U l        [        U5      U l        [        5       U l        g )Nr   )	r3   r4    CAMEMBERT_SELF_ATTENTION_CLASSES_attn_implementationrL   r   outputsetpruned_headsr   s       rO   r4   CamembertAttention.__init__  sF    4V5P5PQ$;
	
 *&1ErQ   c                 6   [        U5      S:X  a  g [        XR                  R                  U R                  R                  U R
                  5      u  p[        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l	        [        U R                  R                  USS9U R                  l        U R                  R                  [        U5      -
  U R                  l        U R                  R                  U R                  R                  -  U R                  l        U R
                  R                  U5      U l        g )Nr   r"   r   )lenr   rL   rw   rz   r   r   r}   r~   r   r   r   r{   union)rL   headsindexs      rO   prune_headsCamembertAttention.prune_heads  s   u:?79900$))2O2OQUQbQb

 -TYY__eD		*499==%@		,TYY__eD		.t{{/@/@%QO )-		(E(EE
(R		%"&))"?"?$))B_B_"_		 --33E:rQ   r   r   r   r   r   r   r   r   c           
      l    U R                  UUUUUUUS9nU R                  US   U5      n	U	4USS  -   n
U
$ )Nr   r   r   r   r   r   r   r"   )rL   r   )rL   r   r   r   r   r   r   r   self_outputsattention_outputoutputss              rO   r`   CamembertAttention.forward  s\     yy)"7)/) ! 
  ;;|AF#%QR(88rQ   )r   r   rL   r   r   )rf   rg   rh   ri   r4   r   rE   r   r   r   r   r   r   r`   rk   rl   rm   s   @rO   r   r     s    ";* 7;15=A*.,115|| !!2!23 E--.	
  ((9(9: ! $D> !. 
u||	 rQ   r   c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )CamembertIntermediatei  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   )r3   r4   r   r|   r7   intermediate_sizer   r   
hidden_actstrr   intermediate_act_fnrK   s     rO   r4   CamembertIntermediate.__init__  s`    YYv1163K3KL
f''--'-f.?.?'@D$'-'8'8D$rQ   r   r   c                 J    U R                  U5      nU R                  U5      nU$ r   r   r  )rL   r   s     rO   r`   CamembertIntermediate.forward  s&    

=100?rQ   r  r   rm   s   @rO   r  r    s(    9U\\ ell  rQ   r  c                   z   ^  \ rS rSrU 4S jrS\R                  S\R                  S\R                  4S jrSrU =r	$ )CamembertOutputi  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   )r3   r4   r   r|   r  r7   r   r>   r?   r@   rA   rB   rK   s     rO   r4   CamembertOutput.__init__  s`    YYv779K9KL
f&8&8f>S>STzz&"<"<=rQ   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      rO   r`   CamembertOutput.forward  r   rQ   r   r   rm   s   @rO   r  r    r   rQ   r  c                   .  ^  \ rS rSrSU 4S jjr       SS\R                  S\\R                     S\\R                     S\\R                     S\\R                     S\\	   S	\\
   S
\\R                     S\\R                     4S jjrS rSrU =r$ )CamembertLayeri  c                 r  > [         TU ]  5         UR                  U l        SU l        [	        XS9U l        UR                  U l        UR                  U l        U R                  (       a/  U R                  (       d  [        U  S35      e[	        USU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 addedr,   r   )r3   r4   chunk_size_feed_forwardseq_len_dimr   	attentionr   add_cross_attentionrx   crossattentionr  intermediater  r   )rL   rM   r   rN   s      rO   r4   CamembertLayer.__init__  s    '-'E'E$+FH ++#)#=#= ##?? D6)g!hii"4VU_kt"uD1&9%f-rQ   r   r   r   r   encoder_attention_maskr   r   r   r   c	           
      P   U R                  UUUUUUS9n	U	S   n
U	SS  nU R                  (       aD  UbA  [        U S5      (       d  [        SU  S35      eU R	                  U
UUUUUUS9nUS   n
XSS  -   n[        U R                  U R                  U R                  U
5      nU4U-   nU$ )N)r   r   r   r   r   r   r"   r  z'If `encoder_hidden_states` are passed, z` has to be instantiated with cross-attention layers by setting `config.add_cross_attention=True`r   )	r  r   rW   rx   r  r   feed_forward_chunkr  r  )rL   r   r   r   r   r!  r   r   r   self_attention_outputsr  r  cross_attention_outputslayer_outputs                 rO   r`   CamembertLayer.forward  s    "&)/)) "0 "
 2!4(,??4@4!122 =dV DD D 
 '+&9&9 5#&;-"3- ': '#  7q9 ;;G0##T%A%A4CSCSUe
  /G+rQ   c                 J    U R                  U5      nU R                  X!5      nU$ r   )r  r   )rL   r  intermediate_outputr&  s       rO   r#  !CamembertLayer.feed_forward_chunk+  s)    "//0@A{{#6IrQ   )r  r  r  r  r  r   r   r  r   )NNNNNFN)rf   rg   rh   ri   r4   rE   r   r   r   r   r   r   r`   r#  rk   rl   rm   s   @rO   r  r    s    ." 7;15=A>B*.,115.||. !!2!23. E--.	.
  ((9(9:. !)):): ;. !. $D>. !.. 
u||	.` rQ   r  c                   v  ^  \ rS rSrSU 4S jjr          SS\R                  S\\R                     S\\R                     S\\R                     S\\R                     S\\	\	\R                           S	\\
   S
\\
   S\\
   S\\
   S\\R                     S\\	\R                     \4   4S jjrSrU =r$ )CamembertEncoderi2  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        SU l	        g s  snf )Nr  F)
r3   r4   rM   r   
ModuleListrangenum_hidden_layersr  layergradient_checkpointing)rL   rM   r   irN   s       rO   r4   CamembertEncoder.__init__3  sT    ]]QVW]WoWoQp#qQpAN6$GQp#qr
&+# $rs   A#r   r   r   r   r!  past_key_values	use_cacher   output_hidden_statesreturn_dictr   r   c                 `   U	(       a  SOS nU(       a  SOS nU(       a  U R                   R                  (       a  SOS nU R                  (       a/  U R                  (       a  U(       a  [        R                  S5        SnSnU(       a]  U R                   R                  (       aB  [        U[        5      (       d-  [        R                  S5        Sn[        R                  " U5      n[        U R                  5       Hf  u  nnU	(       a  X4-   nUb  UU   OS nU" UUUUUUUUS9nUS   nU(       d  M7  UUS   4-   nU R                   R                  (       d  M]  UUS	   4-   nMh     U	(       a  X4-   nU(       a  UR                  5       nU
(       d  [        S
 UUUUU4 5       5      $ [        UUUUUS9$ )N zZ`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...FzPassing a tuple of `past_key_values` is deprecated and will be removed in Transformers v4.58.0. You should pass an instance of `EncoderDecoderCache` instead, e.g. `past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`.T)r!  r   r   r   r   r"   rv   c              3   .   #    U H  nUc  M  Uv   M     g 7fr   r:  ).0vs     rO   	<genexpr>+CamembertEncoder.forward.<locals>.<genexpr>z  s"      
A  s   	)last_hidden_stater5  r   
attentionscross_attentions)rM   r  r2  r   r   r   r   r   r   r   from_legacy_cache	enumerater1  to_legacy_cacher   r   )rL   r   r   r   r   r!  r5  r6  r   r7  r8  r   all_hidden_statesall_self_attentionsall_cross_attentionsreturn_legacy_cacher3  layer_modulelayer_head_masklayer_outputss                       rO   r`   CamembertEncoder.forward9  s    #7BD$5b4%64;;;Z;Zr`d&&4==##p "	#//
?TY8Z8Z\
 #'1CCOTO(4OA|#$58H$H!.7.CilO(%'=."3-	M *!,M  &9]1=M<O&O#;;222+?=QRCSBU+U(+  5.   14D D-==?O 
 "#%'(
 
 
 9+++*1
 	
rQ   )rM   r2  r1  r   )
NNNNNNFFTN)rf   rg   rh   ri   r4   rE   r   r   r   r   r   r   r   r`   rk   rl   rm   s   @rO   r,  r,  2  s(   , 7;15=A>BEI$(,1/4&*15R
||R
 !!2!23R
 E--.	R

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

#566rQ   )rR  r   r   rm   s   @rO   rO  rO    s(    $
U\\ ell  rQ   rO  c                   2    \ rS rSr% \\S'   SrSrSrS r	Sr
g)CamembertPreTrainedModeli  rM   robertaTc                    [        U[        R                  5      (       ak  UR                  R                  R                  SU R                  R                  S9  UR                  b%  UR                  R                  R                  5         gg[        U[        R                  5      (       ax  UR                  R                  R                  SU R                  R                  S9  UR                  b2  UR                  R                  UR                     R                  5         gg[        U[        R                  5      (       aJ  UR                  R                  R                  5         UR                  R                  R                  S5        g[        U[        5      (       a%  UR                  R                  R                  5         gg)zInitialize the weightsr   )meanstdNg      ?)r   r   r|   weightdatanormal_rM   initializer_rangebiaszero_r5   r(   r>   fill_CamembertLMHead)rL   modules     rO   _init_weights&CamembertPreTrainedModel._init_weights  s2   fbii(( MM&&CT[[5R5R&S{{&  &&( '--MM&&CT[[5R5R&S!!-""6#5#56<<> .--KK""$MM$$S)00KK""$ 1rQ   r:  N)rf   rg   rh   ri   r#   __annotations__base_model_prefixsupports_gradient_checkpointing_supports_sdparh  rk   r:  rQ   rO   rZ  rZ    s    !&*#N%rQ   rZ  c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )CamembertClassificationHeadi  z-Head for sentence-level classification tasks.c                 b  > [         TU ]  5         [        R                  " UR                  UR                  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        g r   )r3   r4   r   r|   r7   r   classifier_dropoutrA   r@   rB   
num_labelsout_projrL   rM   rq  rN   s      rO   r4   $CamembertClassificationHead.__init__  s    YYv1163E3EF
)/)B)B)NF%%TZTnTn 	 zz"45		&"4"4f6G6GHrQ   c                     US S 2SS S 24   nU R                  U5      nU R                  U5      n[        R                  " U5      nU R                  U5      nU R	                  U5      nU$ rU  )rB   r   rE   tanhrs  rL   featureskwargsxs       rO   r`   #CamembertClassificationHead.forward  sY    Q1WLLOJJqMJJqMLLOMM!rQ   )r   rB   rs  )	rf   rg   rh   ri   rj   r4   r`   rk   rl   rm   s   @rO   ro  ro    s    7I rQ   ro  c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )rf  i  z,Camembert Head for masked language modeling.c                   > [         TU ]  5         [        R                  " UR                  UR                  5      U l        [        R                  " UR                  UR                  S9U l        [        R                  " UR                  UR                  5      U l
        [        R                  " [        R                  " UR                  5      5      U l        U R                  U R                  l        g r   )r3   r4   r   r|   r7   r   r>   r?   
layer_normr6   decoder	ParameterrE   rH   rc  rK   s     rO   r4   CamembertLMHead.__init__  s    YYv1163E3EF
,,v'9'9v?T?TUyy!3!3V5F5FGLLV->->!?@	 IIrQ   c                     U R                  U5      n[        U5      nU R                  U5      nU R                  U5      nU$ r   )r   r   r  r  rx  s       rO   r`   CamembertLMHead.forward  s;    JJx GOOA LLOrQ   c                     U R                   R                  R                  R                  S:X  a  U R                  U R                   l        g U R                   R                  U l        g )Nmeta)r  rc  rT   r   rL   s    rO   _tie_weightsCamembertLMHead._tie_weights  sC     <<##((F2 $		DLL))DIrQ   )rc  r  r   r  )
rf   rg   rh   ri   rj   r4   r`   r  rk   rl   rm   s   @rO   rf  rf    s    6&* *rQ   rf  c            "         ^  \ rS rSrSr/ rSU 4S jjrS rS rS r	\
              SS\\R                     S\\R                     S	\\R                     S
\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S\\\R                        S\\   S\\   S\\   S\\   S\\R                     S\\\R                     \4   4S jj5       rSrU =r$ )CamembertModeli  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*_ by Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz
Kaiser and Illia Polosukhin.

To behave as a 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.

.. _*Attention is all you need*: https://huggingface.co/papers/1706.03762

c                   > [         TU ]  U5        Xl        [        U5      U l        [        U5      U l        U(       a  [        U5      OSU l        UR                  U l
        UR                  U l        U R                  5         g)z^
add_pooling_layer (bool, *optional*, defaults to `True`):
    Whether to add a pooling layer
N)r3   r4   rM   r%   r_   r,  encoderrO  poolerr   attn_implementationr+   	post_init)rL   rM   add_pooling_layerrN   s      rO   r4   CamembertModel.__init__  sg    
 	 -f5'/1Bof-#)#>#> '-'E'E$ 	rQ   c                 .    U R                   R                  $ r   r_   r9   r  s    rO   get_input_embeddings#CamembertModel.get_input_embeddings  s    ...rQ   c                 $    XR                   l        g r   r  )rL   r   s     rO   set_input_embeddings#CamembertModel.set_input_embeddings  s    */'rQ   c                     UR                  5        H7  u  p#U R                  R                  U   R                  R	                  U5        M9     g)z
Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
class PreTrainedModel
N)itemsr  r1  r  r   )rL   heads_to_pruner1  r   s       rO   _prune_headsCamembertModel._prune_heads  s<    
 +002LELLu%//;;EB 3rQ   rX   r   r0   r-   r   rY   r   r!  r5  r6  r   r7  r8  r   r   c                    Ub  UOU R                   R                  nUb  UOU R                   R                  nUb  UOU R                   R                  nU R                   R                  (       a  U
b  U
OU R                   R
                  n
OSn
Ub  Ub  [        S5      eUb"  U R                  X5        UR                  5       nO"Ub  UR                  5       S S nO[        S5      eUu  nnUb  UR                  OUR                  nSnU	b:  [        U	[        5      (       d  U	S   S   R                  S   OU	R                  5       nUcs  [        U R                  S5      (       a4  U R                  R                   S S 2S U24   nUR#                  UU5      nUnO$[$        R&                  " U[$        R(                  US9nU R                  UUUUUS	9nUc  [$        R*                  " UUU-   4US
9nU R,                  S:H  =(       a(    U R.                  S:H  =(       a    US L =(       a    U(       + nU(       aT  UR1                  5       S:X  a@  U R                   R                  (       a  [3        UUUU5      nO'[5        UUR6                  US9nOU R9                  X/5      nU R                   R                  (       av  Ubs  UR                  5       u  nnnUU4nUc  [$        R*                  " UUS
9nU(       a*  UR1                  5       S:X  a  [5        UUR6                  US9nOU R;                  U5      nOS nU R=                  XPR                   R>                  5      nU RA                  UUUUUU	U
UUUUS9nUS   nU RB                  b  U RC                  U5      OS n U(       d
  UU 4USS  -   $ [E        UU URF                  URH                  URJ                  URL                  S9$ )NFzDYou cannot specify both input_ids and inputs_embeds at the same timer.   z5You have to specify either input_ids or inputs_embedsr   r   r0   rS   )rX   r-   r0   rY   rZ   )rT   r   r,   rv   )r   )
r   r   r   r!  r5  r6  r   r7  r8  r   r"   )r@  pooler_outputr5  r   rA  rB  )'rM   r   r7  use_return_dictr   r6  rx   %warn_if_padding_and_no_attention_maskrI   rT   r   r   r   get_seq_lengthrW   r_   r0   rG   rE   rH   rJ   onesr  r+   r   r   r   r2   get_extended_attention_maskinvert_attention_maskget_head_maskr0  r  r  r   r5  r   rA  rB  )!rL   rX   r   r0   r-   r   rY   r   r!  r5  r6  r   r7  r8  r   r[   r   r\   rT   rZ   r]   r^   embedding_outputuse_sdpa_attention_masksextended_attention_maskencoder_batch_sizeencoder_sequence_lengthr   encoder_hidden_shapeencoder_extended_attention_maskencoder_outputssequence_outputrW  s!                                    rO   r`   CamembertModel.forward%  s   & 2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B];;!!%.%:	@U@UII ]%>cdd"66yQ#..*K&',,.s3KTUU!,
J%.%:!!@T@T!"& "/599  "1%++B/$335 # !t(899*.//*H*HKZK*X'3J3Q3QR\^h3i0!A!&[

SY!Z??%)'#9 + 
 !"ZZZBX5X(YbhiN $$. &,,
:&T!& &%	 	! $(:(:(<(A {{%%*T"$*	+' +N"$4$:$:J+' '+&F&F~&c# ;;!!&;&G=R=W=W=Y: 7$68O#P %-).4HQW)X&',B,F,F,HA,M 3V*,<,B,BJ3/ 372L2LMc2d/.2+ &&y++2O2OP	,,2"7#B+/!5#) ' 
 *!,8<8OO4UY#]3oab6III;-'+;;)77&11,==
 	
rQ   )r  rM   r_   r  r  r+   )TNNNNNNNNNNNNNN)rf   rg   rh   ri   rj   _no_split_modulesr4   r  r  r  r   r   rE   r   listr   r   r   r   r   r`   rk   rl   rm   s   @rO   r  r    s    &/0C  -11515/3,0048<9==A$(,0/3&*15S
ELL)S
 !.S
 !.	S

 u||,S
 ELL)S
  -S
  (5S
 !) 6S
 "$u'8'8"9:S
 D>S
 $D>S
 'tnS
 d^S
 !.S
  
uU\\"$PP	Q!S
 S
rQ   r  c                     ^  \ rS rSrSS/rU 4S jrS rS r\            SS\	\
R                     S\	\
R                     S	\	\
R                     S
\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\   S\	\   S\	\   S\\\
R                      \4   4S jj5       rSrU =r$ )CamembertForMaskedLMi  lm_head.decoder.weightlm_head.decoder.biasc                    > [         TU ]  U5        UR                  (       a  [        R	                  S5        [        USS9U l        [        U5      U l        U R                  5         g )NzpIf you want to use `CamembertForMaskedLM` make sure `config.is_decoder=False` for bi-directional self-attention.Fr  
r3   r4   r   r   warningr  r[  rf  lm_headr  rK   s     rO   r4   CamembertForMaskedLM.__init__  sR     NN1
 &fF&v. 	rQ   c                 .    U R                   R                  $ r   r  r  r  s    rO   get_output_embeddings*CamembertForMaskedLM.get_output_embeddings      ||###rQ   c                 $    XR                   l        g r   r  rL   new_embeddingss     rO   set_output_embeddings*CamembertForMaskedLM.set_output_embeddings      -rQ   rX   r   r0   r-   r   rY   r   r!  labelsr   r7  r8  r   c                    Ub  UOU R                   R                  nU R                  UUUUUUUUU
UUS9nUS   nU R                  U5      nSnU	ba  U	R	                  UR
                  5      n	[        5       nU" UR                  SU R                   R                  5      U	R                  S5      5      nU(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  S9$ )a  
token_type_ids (`torch.LongTensor` of shape `(batch_size, 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.
    This parameter can only be used when the model is initialized with `type_vocab_size` parameter with value
    >= 2. All the value in this tensor should be always < type_vocab_size.

    [What are token type IDs?](../glossary#token-type-ids)
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
    Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ...,
    config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the
    loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`
N)
r   r0   r-   r   rY   r   r!  r   r7  r8  r   r.   rv   losslogitsr   rA  )rM   r  r[  r  r   rT   r   r   r6   r   r   rA  )rL   rX   r   r0   r-   r   rY   r   r!  r  r   r7  r8  r  r  prediction_scoresmasked_lm_lossloss_fctr   s                      rO   r`   CamembertForMaskedLM.forward  s   > &1%<k$++B]B],,))%'"7#9/!5#  
 "!* LL9YY0778F')H%&7&<&<RAWAW&XZ`ZeZefhZijN')GABK7F3A3M^%.YSYY$!//))	
 	
rQ   r  r[  )NNNNNNNNNNNN)rf   rg   rh   ri   _tied_weights_keysr4   r  r  r   r   rE   
LongTensorr   r   r   r   r   r   r`   rk   rl   rm   s   @rO   r  r    sk    34JK$.  156:59371559=A>B-1,0/3&*@
E,,-@
 !!2!23@
 !!1!12	@

 u//0@
 E--.@
   1 12@
  ((9(9:@
 !)):): ;@
 ))*@
 $D>@
 'tn@
 d^@
 
uU\\"N2	3@
 @
rQ   r  z
    CamemBERT Model transformer with a sequence classification/regression head on top (a linear layer on top of the
    pooled output) e.g. for GLUE tasks.
    )custom_introc                   l  ^  \ rS rSrU 4S jr\          SS\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S	\\R                     S
\\
   S\\
   S\\
   S\\\R                     \4   4S jj5       rSrU =r$ )"CamembertForSequenceClassificationi  c                    > [         TU ]  U5        UR                  U l        Xl        [	        USS9U l        [        U5      U l        U R                  5         g NFr  )	r3   r4   rr  rM   r  r[  ro  
classifierr  rK   s     rO   r4   +CamembertForSequenceClassification.__init__#  sH      ++%fF5f= 	rQ   rX   r   r0   r-   r   rY   r  r   r7  r8  r   c                 f   U
b  U
OU R                   R                  n
U R                  UUUUUUUU	U
S9	nUS   nU R                  U5      nSnUGb  UR	                  UR
                  5      nU R                   R                  c  U R                  S:X  a  SU R                   l        OoU R                  S:  aN  UR                  [        R                  :X  d  UR                  [        R                  :X  a  SU R                   l        OSU R                   l        U R                   R                  S:X  aI  [        5       nU R                  S:X  a&  U" UR                  5       UR                  5       5      nOU" X5      nOU R                   R                  S:X  a=  [        5       nU" UR                  SU R                  5      UR                  S5      5      nO,U R                   R                  S:X  a  [!        5       nU" X5      nU
(       d  U4US	S -   nUb  U4U-   $ U$ [#        UUUR$                  UR&                  S
9$ )a  
token_type_ids (`torch.LongTensor` of shape `(batch_size, 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.
    This parameter can only be used when the model is initialized with `type_vocab_size` parameter with value
    >= 2. All the value in this tensor should be always < type_vocab_size.

    [What are token type IDs?](../glossary#token-type-ids)
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).
Nr   r0   r-   r   rY   r   r7  r8  r   r"   
regressionsingle_label_classificationmulti_label_classificationr.   rv   r  )rM   r  r[  r  r   rT   problem_typerr  r2   rE   rJ   ry   r	   squeezer   r   r   r   r   rA  rL   rX   r   r0   r-   r   rY   r  r   r7  r8  r  r  r  r  r  r   s                    rO   r`   *CamembertForSequenceClassification.forward.  s   : &1%<k$++B]B],,))%'/!5#  

 "!*1YYv}}-F{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#FNN$4fnn6FGD#F3D))-JJ+-B @&++b/R))-II,./Y,F)-)9TGf$EvE'!//))	
 	
rQ   )r  rM   rr  r[  
NNNNNNNNNN)rf   rg   rh   ri   r4   r   r   rE   r  r   r   r   r   r   r   r`   rk   rl   rm   s   @rO   r  r    s"   	  156:59371559-1,0/3&*N
E,,-N
 !!2!23N
 !!1!12	N

 u//0N
 E--.N
   1 12N
 ))*N
 $D>N
 'tnN
 d^N
 
uU\\"$<<	=N
 N
rQ   r  c                   l  ^  \ rS rSrU 4S jr\          SS\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S	\\R                     S
\\
   S\\
   S\\
   S\\\R                     \4   4S jj5       rSrU =r$ )CamembertForMultipleChoicei  c                    > [         TU ]  U5        [        U5      U l        [        R
                  " UR                  5      U l        [        R                  " UR                  S5      U l
        U R                  5         g )Nr"   )r3   r4   r  r[  r   r@   rA   rB   r|   r7   r  r  rK   s     rO   r4   #CamembertForMultipleChoice.__init__  sV     %f-zz&"<"<=))F$6$6: 	rQ   rX   r0   r   r  r-   r   rY   r   r7  r8  r   c                    U
b  U
OU R                   R                  n
Ub  UR                  S   OUR                  S   nUb!  UR                  SUR	                  S5      5      OSnUb!  UR                  SUR	                  S5      5      OSnUb!  UR                  SUR	                  S5      5      OSnUb!  UR                  SUR	                  S5      5      OSnUb1  UR                  SUR	                  S5      UR	                  S5      5      OSnU R                  UUUUUUUU	U
S9	nUS   nU R                  U5      nU R                  U5      nUR                  SU5      nSnUb.  UR                  UR                  5      n[        5       nU" UU5      nU
(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  S9$ )aO  
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.
    This parameter can only be used when the model is initialized with `type_vocab_size` parameter with value
    >= 2. All the value in this tensor should be always < type_vocab_size.

    [What are token type IDs?](../glossary#token-type-ids)
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)
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.
Nr"   r.   r   )r-   r0   r   r   rY   r   r7  r8  rv   r  )rM   r  r   r   rI   r[  rB   r  r   rT   r   r   r   rA  )rL   rX   r0   r   r  r-   r   rY   r   r7  r8  num_choicesflat_input_idsflat_position_idsflat_token_type_idsflat_attention_maskflat_inputs_embedsr  rW  r  reshaped_logitsr  r  r   s                           rO   r`   "CamembertForMultipleChoice.forward  s   Z &1%<k$++B]B],5,Aiooa(}GZGZ[\G]CLCXINN2,>?^bLXLdL--b,2C2CB2GHjnR`Rln11"n6I6I"6MNrvR`Rln11"n6I6I"6MNrv ( r=#5#5b#9=;M;Mb;QR 	 ,,*..,/!5#  

  
]3/ ++b+6YY556F')HOV4D%''!"+5F)-)9TGf$EvE("!//))	
 	
rQ   )r  rB   r[  r  )rf   rg   rh   ri   r4   r   r   rE   r  r   r   r   r   r   r   r`   rk   rl   rm   s   @rO   r  r    s"     15596:-1371559,0/3&*Z
E,,-Z
 !!1!12Z
 !!2!23	Z

 ))*Z
 u//0Z
 E--.Z
   1 12Z
 $D>Z
 'tnZ
 d^Z
 
uU\\"$==	>Z
 Z
rQ   r  c                   l  ^  \ rS rSrU 4S jr\          SS\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S	\\R                     S
\\
   S\\
   S\\
   S\\\R                     \4   4S jj5       rSrU =r$ )CamembertForTokenClassificationi  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 r  )r3   r4   rr  r  r[  rq  rA   r   r@   rB   r|   r7   r  r  rt  s      rO   r4   (CamembertForTokenClassification.__init__  s      ++%fF)/)B)B)NF%%TZTnTn 	 zz"45))F$6$68I8IJ 	rQ   rX   r   r0   r-   r   rY   r  r   r7  r8  r   c                    U
b  U
OU R                   R                  n
U R                  UUUUUUUU	U
S9	nUS   nU R                  U5      nU R	                  U5      nSnUbW  UR                  UR                  5      n[        5       nU" UR                  SU R                  5      UR                  S5      5      nU
(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  S9$ )a  
token_type_ids (`torch.LongTensor` of shape `(batch_size, 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.
    This parameter can only be used when the model is initialized with `type_vocab_size` parameter with value
    >= 2. All the value in this tensor should be always < type_vocab_size.

    [What are token type IDs?](../glossary#token-type-ids)
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
    Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`.
Nr  r   r.   rv   r  )rM   r  r[  rB   r  r   rT   r   r   rr  r   r   rA  r  s                    rO   r`   'CamembertForTokenClassification.forward  s   6 &1%<k$++B]B],,))%'/!5#  

 "!*,,71YYv}}-F')HFKKDOO<fkk"oNDY,F)-)9TGf$EvE$!//))	
 	
rQ   )r  rB   rr  r[  r  )rf   rg   rh   ri   r4   r   r   rE   r  r   r   r   r   r   r   r`   rk   rl   rm   s   @rO   r  r    s     156:59371559-1,0/3&*=
E,,-=
 !!2!23=
 !!1!12	=

 u//0=
 E--.=
   1 12=
 ))*=
 $D>=
 'tn=
 d^=
 
uU\\"$99	:=
 =
rQ   r  c                     ^  \ rS rSrU 4S jr\           SS\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S\\R                     S	\\R                     S
\\R                     S\\
   S\\
   S\\
   S\\\R                     \4   4S jj5       rSrU =r$ )CamembertForQuestionAnsweringi=  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  )
r3   r4   rr  r  r[  r   r|   r7   
qa_outputsr  rK   s     rO   r4   &CamembertForQuestionAnswering.__init__@  sU      ++%fF))F$6$68I8IJ 	rQ   rX   r   r0   r-   r   rY   start_positionsend_positionsr   r7  r8  r   c                 $   Ub  UOU R                   R                  nU R                  UUUUUUU	U
US9	nUS   nU R                  U5      nUR	                  SSS9u  nnUR                  S5      R                  5       nUR                  S5      R                  5       nSnUb  Ub  [        UR                  5       5      S:  a  UR                  S5      n[        UR                  5       5      S:  a  UR                  S5      nUR                  S5      nUR                  SU5      nUR                  SU5      n[        US9nU" X5      nU" UU5      nUU-   S-  nU(       d  UU4USS -   nUb  U4U-   $ U$ [        UUUUR                  UR                  S	9$ )
a  
token_type_ids (`torch.LongTensor` of shape `(batch_size, 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.
    This parameter can only be used when the model is initialized with `type_vocab_size` parameter with value
    >= 2. All the value in this tensor should be always < type_vocab_size.

    [What are token type IDs?](../glossary#token-type-ids)
Nr  r   r"   r.   r   )ignore_indexrv   )r  start_logits
end_logitsr   rA  )rM   r  r[  r  splitr  r   r   rI   clampr   r   r   rA  )rL   rX   r   r0   r-   r   rY   r  r   r   r7  r8  r  r  r  r  r  
total_lossignored_indexr  
start_lossend_lossr   s                          rO   r`   %CamembertForQuestionAnswering.forwardJ  s   4 &1%<k$++B]B],,))%'/!5#  

 "!*1#)<<r<#: j#++B/::<''+668

&=+D?'')*Q."1"9"9""==%%'(1, - 5 5b 9(--a0M-33A}EO)//=AM']CH!,@J
M:H$x/14J"J/'!"+=F/9/EZMF*Q6Q+%!!//))
 	
rQ   )rr  r  r[  )NNNNNNNNNNN)rf   rg   rh   ri   r4   r   r   rE   r  r   r   r   r   r   r   r`   rk   rl   rm   s   @rO   r  r  =  s;     156:593715596:48,0/3&*I
E,,-I
 !!2!23I
 !!1!12	I

 u//0I
 E--.I
   1 12I
 "%"2"23I
   0 01I
 $D>I
 'tnI
 d^I
 
uU\\"$@@	AI
 I
rQ   r  zU
    CamemBERT Model with a `language modeling` head on top for CLM fine-tuning.
    c            "         ^  \ rS rSrSS/rU 4S jrS rS r\              SS\	\
R                     S\	\
R                     S	\	\
R                     S
\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\
R                     S\	\\\
R                           S\	\   S\	\   S\	\   S\	\   S\\\
R                      \4   4S jj5       rSrU =r$ )CamembertForCausalLMi  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 )NzQIf you want to use `CamembertLMHeadModel` as a standalone, add `is_decoder=True.`Fr  r  rK   s     rO   r4   CamembertForCausalLM.__init__  sL       NNno%fF&v. 	rQ   c                 .    U R                   R                  $ r   r  r  s    rO   r  *CamembertForCausalLM.get_output_embeddings  r  rQ   c                 $    XR                   l        g r   r  r  s     rO   r  *CamembertForCausalLM.set_output_embeddings  r  rQ   rX   r   r0   r-   r   rY   r   r!  r  r5  r6  r   r7  r8  r   c                    Ub  UOU R                   R                  nU	b  SnU R                  UUUUUUUUU
UUUUS9nUS   nU R                  U5      nSnU	bE  U	R	                  UR
                  5      n	U R                  " UU	4SU R                   R                  0UD6nU(       d  U4USS -   nUb  U4U-   $ U$ [        UUUR                  UR                  UR                  UR                  S9$ )a  
token_type_ids (`torch.LongTensor` of shape `(batch_size, 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.
    This parameter can only be used when the model is initialized with `type_vocab_size` parameter with value
    >= 2. All the value in this tensor should be always < type_vocab_size.

    [What are token type IDs?](../glossary#token-type-ids)
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, CamembertForCausalLM, AutoConfig
>>> import torch

>>> tokenizer = AutoTokenizer.from_pretrained("almanach/camembert-base")
>>> config = AutoConfig.from_pretrained("almanach/camembert-base")
>>> config.is_decoder = True
>>> model = CamembertForCausalLM.from_pretrained("almanach/camembert-base", config=config)

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

>>> prediction_logits = outputs.logits
```NF)r   r0   r-   r   rY   r   r!  r5  r6  r   r7  r8  r   r6   rv   )r  r  r5  r   rA  rB  )rM   r  r[  r  r   rT   loss_functionr6   r   r5  r   rA  rB  )rL   rX   r   r0   r-   r   rY   r   r!  r  r5  r6  r   r7  r8  rz  r  r  r  lm_lossr   s                        rO   r`   CamembertForCausalLM.forward  s4   d &1%<k$++B]B]I,,))%'"7#9+/!5#  
  "!* LL9YY0778F((!  ;;11 	G ')GABK7F,3,?WJ'KVK0$#33!//))$55
 	
rQ   r  r  )rf   rg   rh   ri   r  r4   r  r  r   r   rE   r  r   r   r   r   r   r   r`   rk   rl   rm   s   @rO   r  r    s    34JK
$.  156:59371559=A>B-1EI$(,0/3&*^
E,,-^
 !!2!23^
 !!1!12	^

 u//0^
 E--.^
   1 12^
  ((9(9:^
 !)):): ;^
 ))*^
 "%e.?.?(@"AB^
 D>^
 $D>^
 'tn^
 d^^
" 
uU\\"$EE	F#^
 ^
rQ   r  c                     U R                  U5      R                  5       n[        R                  " USS9R	                  U5      U-   U-  nUR                  5       U-   $ )z
Replace non-padding symbols with their position numbers. Position numbers begin at padding_idx+1. Padding symbols
are ignored. This is modified from fairseq's `utils.make_positions`.

Args:
    x: torch.Tensor x:

Returns: torch.Tensor
r"   r   )nery   rE   cumsumtype_asrJ   )rX   r(   rZ   maskincremental_indicess        rO   rU   rU     sW     <<$((*D <<!4<<TBE[[_cc##%33rQ   )r  r  r  r  r  r  r  rZ  )r   )Krj   r   typingr   r   rE   torch.utils.checkpoint	packagingr   r   torch.nnr   r   r	   activationsr   r   cache_utilsr   r   
generationr   modeling_attn_mask_utilsr   r   modeling_layersr   modeling_outputsr   r   r   r   r   r   r   r   modeling_utilsr   pytorch_utilsr   r   r   utilsr   r    r!   configuration_camembertr#   
get_loggerrf   r   Moduler%   ro   r   r   r   r   r  r  r  r,  rO  rZ  ro  rf  r  r  r  r  r  r  r  rU   __all__r:  rQ   rO   <module>r/     s]      "     A A ' 5 ) w 9	 	 	 . l l ? ? 4 
		H	%V=")) V=t@.RYY @.Hl!!7 l!`"))  $&$  2 2lBII  bii B/ BLY
ryy Y
zbii  % % %6")) .*bii *> I
- I
 I
X Y
3 Y
 Y
x [
)A [
[
| f
!9 f
 f
R M
&> M
 M
` U
$< U
 U
p t
3_ t
t
p4 	rQ   