
    sg                       d Z ddlmZ ddlmZmZmZ ddlZddl	Z
ddlmZ ddlmZmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZm Z  dd	l!m"Z"m#Z#m$Z$ dd
l%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+  e)jX                  e-      Z.dZ/dZ0 G d dejb                  jd                        Z3 G d dejb                  jd                        Z4 G d dejb                  jd                        Z5 G d dejb                  jd                        Z6 G d dejb                  jd                        Z7 G d dejb                  jd                        Z8 G d dejb                  jd                        Z9 G d dejb                  jd                        Z: G d d ejb                  jd                        Z; G d! d"ejb                  jd                        Z<e G d# d$ejb                  jd                               Z= G d% d&e      Z>d'Z?d(Z@ e'd)e?       G d* d+e>             ZA G d, d-ejb                  jd                        ZB G d. d/ejb                  jd                        ZC e'd0e?       G d1 d2e>e             ZD G d3 d4ejb                  jd                        ZE e'd5e?       G d6 d7e>e             ZF e'd8e?       G d9 d:e>e             ZG e'd;e?       G d< d=e>e             ZH e'd>e?       G d? d@e>e             ZIy)AzTF 2.0 ConvBERT model.    )annotations)OptionalTupleUnionN   )get_tf_activation)TFBaseModelOutputTFMaskedLMOutputTFMultipleChoiceModelOutputTFQuestionAnsweringModelOutputTFSequenceClassifierOutputTFTokenClassifierOutput)TFMaskedLanguageModelingLossTFModelInputTypeTFMultipleChoiceLossTFPreTrainedModelTFQuestionAnsweringLossTFSequenceClassificationLossTFSequenceSummaryTFTokenClassificationLossget_initializerkeraskeras_serializableunpack_inputs)check_embeddings_within_bounds
shape_liststable_softmax)add_code_sample_docstringsadd_start_docstrings%add_start_docstrings_to_model_forwardlogging   )ConvBertConfigzYituTech/conv-bert-baser#   c                  X     e Zd ZdZd fdZddZ	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ xZS )	TFConvBertEmbeddingszGConstruct the embeddings from word, position and token_type embeddings.c                V   t        |   di | || _        |j                  | _        |j                  | _        |j
                  | _        t        j                  j                  |j                  d      | _
        t        j                  j                  |j                        | _        y )N	LayerNormepsilonname)rate )super__init__configembedding_sizemax_position_embeddingsinitializer_ranger   layersLayerNormalizationlayer_norm_epsr'   Dropouthidden_dropout_probdropoutselfr/   kwargs	__class__s      d/var/www/html/venv/lib/python3.12/site-packages/transformers/models/convbert/modeling_tf_convbert.pyr.   zTFConvBertEmbeddings.__init__C   s    "6"$33'-'E'E$!'!9!988AVAV]h8i||++1K1K+L    c                   t        j                  d      5  | j                  d| j                  j                  | j
                  gt        | j                              | _        d d d        t        j                  d      5  | j                  d| j                  j                  | j
                  gt        | j                              | _
        d d d        t        j                  d      5  | j                  d| j                  | j
                  gt        | j                              | _        d d d        | j                  ry d| _        t        | dd       et        j                  | j                  j                         5  | j                  j#                  d d | j                  j
                  g       d d d        y y # 1 sw Y   [xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)	Nword_embeddingsweight)r*   shapeinitializertoken_type_embeddings
embeddingsposition_embeddingsTr'   )tf
name_scope
add_weightr/   
vocab_sizer0   r   r2   rA   type_vocab_sizerD   r1   rF   builtgetattrr'   r*   buildr:   input_shapes     r=   rN   zTFConvBertEmbeddings.buildM   s   ]],- 	//{{--t/B/BC+D,B,BC * DK	 ]]23 	)-!{{22D4G4GH+D,B,BC *9 *D&	 ]]01 	'+!33T5H5HI+D,B,BC (7 (D$	 ::
4d+7t~~223 O$$dD$++2L2L%MNO O 81	 		 		 	O Os2   AF< AG	*AG?3G!<G	GG!G*c                >   ||t        d      |At        || j                  j                         t	        j
                  | j                  |      }t        |      dd }|t	        j                  |d      }|2t	        j                  t	        j                  ||d   |z         d	      }t	        j
                  | j                  |      }t	        j
                  | j                  |      }	||z   |	z   }
| j                  |

      }
| j                  |
|      }
|
S )z
        Applies embedding based on inputs tensor.

        Returns:
            final_embeddings (`tf.Tensor`): output embedding tensor.
        Nz5Need to provide either `input_ids` or `input_embeds`.)paramsindicesr   )dimsvaluer"   )startlimitaxis)inputs)r[   training)
ValueErrorr   r/   rJ   rG   gatherrA   r   fillexpand_dimsrangerF   rD   r'   r8   )r:   	input_idsposition_idstoken_type_idsinputs_embedspast_key_values_lengthr\   rP   position_embedstoken_type_embedsfinal_embeddingss              r=   callzTFConvBertEmbeddings.callk   s    !6TUU *9dkk6L6LMIIT[[)LM /4!WW+Q?N>>5[^Nd=delmL ))4+C+C\ZIIT-G-GQ_`(?:=NN>>1A>B<</?(<Sr>   )r/   r#   N)NNNNr   F)rb   	tf.Tensorrc   rl   rd   rl   re   rl   r\   boolreturnrl   )__name__
__module____qualname____doc__r.   rN   rj   __classcell__r<   s   @r=   r%   r%   @   sf    QMO@  $"&$(#' & &   &  "	& 
 !&  &  
& r>   r%   c                  4     e Zd Z fdZd ZddZddZ xZS )TFConvBertSelfAttentionc           
     :   t        |   di | |j                  |j                  z  dk7  r&t	        d|j                   d|j                   d      t        |j                  |j                  z        }|dk  r|j                  | _        d}n|}|j                  | _        || _        |j                  | _        |j                  | j                  z  dk7  rt	        d      |j                  |j                  z  | _        | j                  | j                  z  | _	        t        j                  j                  | j                  t        |j                        d      | _        t        j                  j                  | j                  t        |j                        d	      | _        t        j                  j                  | j                  t        |j                        d
      | _        t        j                  j%                  | j                  | j                  dd t        d| j                  z        t        |j                        d      | _        t        j                  j                  | j                  | j                  z  d dt        |j                              | _        t        j                  j                  | j                  d dt        |j                              | _        t        j                  j-                  |j.                        | _        || _        y )Nr   zThe hidden size (z6) is not a multiple of the number of attention heads ()r"   z6hidden_size should be divisible by num_attention_headsquerykernel_initializerr*   keyrV   samekey_conv_attn_layer)padding
activationdepthwise_initializerpointwise_initializerr*   conv_kernel_layer)r   r*   r{   conv_out_layerr,   )r-   r.   hidden_sizenum_attention_headsr]   int
head_ratioconv_kernel_sizeattention_head_sizeall_head_sizer   r3   Denser   r2   ry   r|   rV   SeparableConv1Dr~   r   r   r6   attention_probs_dropout_probr8   r/   )r:   r/   r;   new_num_attention_headsr   r<   s        r=   r.   z TFConvBertSelfAttention.__init__   s   "6" : ::a?#F$6$6#7 8 445Q8 
 #&f&@&@6CTCT&T"U"Q&$88DO"#"9$//DO#6  & 7 7 8 88A=UVV#)#5#59S9S#S !558P8PP\\''?6C[C[3\cj ( 

 <<%%?6C[C[3\ch & 
 \\''?6C[C[3\cj ( 

 $)<<#?#?!!"1!d6K6K2K"L"1&2J2J"K& $@ $
  "'!3!3$$t'<'<<$.v/G/GH	 "4 "
 $ll00!.v/G/GH	 1 
 ||++F,O,OPr>   c                    t        j                  ||d| j                  | j                  f      }t        j                  |g d      S )NrT   r      r"   r   perm)rG   reshaper   r   	transpose)r:   x
batch_sizes      r=   transpose_for_scoresz,TFConvBertSelfAttention.transpose_for_scores   s8    JJq:r4+C+CTE]E]^_||AL11r>   c                   t        |      d   }| j                  |      }| j                  |      }| j                  |      }	| j	                  |      }
| j                  ||      }| j                  ||      }t        j                  |
|      }| j                  |      }t        j                  |d| j                  dg      }t        |d      }t        j                  ddgt        | j                  dz
  dz        t        | j                  dz
  dz        gddgg      }| j                  |      }t        j                  ||d| j                  g      }t        j                   ||d      }t        j"                  t%        | j                        D cg c]5  }t        j&                  |d|dg|t        |      d   | j                  g      7 c}d      }t        j                  |d| j(                  | j                  g      }t        j*                  ||      }t        j                  |d| j                  g      }t        j*                  ||d      }t        j,                  t        |      d   |j.                        }|t        j0                  j3                  |      z  }|||z   }t        |d      }| j5                  ||	      }|||z  }t        j                  |	|d| j6                  | j(                  g      }t        j8                  |g d
      }t        j*                  ||      }t        j8                  |g d
      }t        j                  ||d| j6                  | j(                  g      }t        j:                  ||gd      }t        j                  ||d| j<                  | j                  z  f      }|r||f}|S |f}|S c c}w )Nr   rT   r"   rY   r   CONSTANTT)transpose_br\   r   r   )r   ry   r|   rV   r~   r   rG   multiplyr   r   r   r   constantr   r   r   padstackra   slicer   matmulcastdtypemathsqrtr8   r   r   concatr   )r:   hidden_statesattention_mask	head_maskoutput_attentionsr\   r   mixed_query_layermixed_key_layermixed_value_layermixed_key_conv_attn_layerquery_layer	key_layerconv_attn_layerr   paddingsr   iunfold_conv_out_layerattention_scoresdkattention_probsvalue_layercontext_layerconv_outoutputss                             r=   rj   zTFConvBertSelfAttention.call   s   .q1
 JJ}5((=1 JJ}5$($<$<]$K!//0A:N--ozJ	++&?ARS 22?CJJ'82t?T?TVW:XY*+<1E;;  d++a/145sD<Q<QTU<UYZ;Z7[\A	
 ,,];NZTEWEW4XY*E " t445 !QZL]A^_`Aacgcucu4vw !
 $9B@X@XZ^ZoZo;pq>3DENR9K9K4LM 99
 WWZ	*2.0@0F0FG+bggll2.>>%/.@ ))9C ,,,J  -	9Ojj
B0H0H$JbJbc
 ll;=		/;?]F::nz2t?W?WY]YqYq.rs		=(";Q?

JDOOd>P>P,PQ
 7H=/2 O\M]cs   :Oc                   | j                   ry d| _         t        | dd       dt        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        t        | dd       dt        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        t        | dd       dt        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        t        | dd       dt        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        t        | dd       Zt        j                  | j                  j
                        5  | j                  j                  d d | j                  g       d d d        t        | dd       et        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        y y # 1 sw Y   7xY w# 1 sw Y   xY w# 1 sw Y   oxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)NTry   r|   rV   r~   r   r   )rL   rM   rG   rH   ry   r*   rN   r/   r   r|   rV   r~   r   r   r   rO   s     r=   rN   zTFConvBertSelfAttention.build+  sg   ::
4$'3tzz/ H

  $dkk.E.E!FGH4%1txx}}- FdDKK,C,CDEF4$'3tzz/ H

  $dkk.E.E!FGH4.5At77<<= V((..dDKK<S<S/TUV4,d3?t55::; O&&,,dD$:L:L-MNO4)40<t22778 Q##))4t{{7N7N*OPQ Q =H HF FH HV VO OQ QsH   3J3<3K -3K3K)K'63K33J= K
KK$'K03K<Frk   )ro   rp   rq   r.   r   rj   rN   rs   rt   s   @r=   rv   rv      s    <|2
QfQr>   rv   c                  .     e Zd Z fdZddZddZ xZS )TFConvBertSelfOutputc                v   t        |   di | t        j                  j	                  |j
                  t        |j                        d      | _        t        j                  j                  |j                  d      | _        t        j                  j                  |j                        | _        || _        y Ndenserz   r'   r(   r,   )r-   r.   r   r3   r   r   r   r2   r   r4   r5   r'   r6   r7   r8   r/   r9   s      r=   r.   zTFConvBertSelfOutput.__init__D  s    "6"\\''?6C[C[3\cj ( 

 88AVAV]h8i||++F,F,FGr>   c                v    | j                  |      }| j                  ||      }| j                  ||z         }|S Nr   r   r8   r'   r:   r   input_tensorr\   s       r=   rj   zTFConvBertSelfOutput.callN  ;    

=1]XF}|'CDr>   c                "   | j                   ry d| _         t        | dd       dt        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        t        | dd       et        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        y y # 1 sw Y   |xY w# 1 sw Y   y xY wNTr   r'   
rL   rM   rG   rH   r   r*   rN   r/   r   r'   rO   s     r=   rN   zTFConvBertSelfOutput.buildU      ::
4$'3tzz/ H

  $dkk.E.E!FGH4d+7t~~223 L$$dD$++2I2I%JKL L 8H HL L   3C9<3D9DDr   rk   ro   rp   rq   r.   rj   rN   rs   rt   s   @r=   r   r   C  s    	Lr>   r   c                  4     e Zd Z fdZd ZddZddZ xZS )TFConvBertAttentionc                l    t        |   di | t        |d      | _        t	        |d      | _        y )Nr:   r*   outputr,   )r-   r.   rv   self_attentionr   dense_outputr9   s      r=   r.   zTFConvBertAttention.__init__b  s1    "6"5f6J0hGr>   c                    t         rk   NotImplementedError)r:   headss     r=   prune_headszTFConvBertAttention.prune_headsh  s    !!r>   c                r    | j                  |||||      }| j                  |d   ||      }|f|dd  z   }|S Nr   r   r"   )r   r   )	r:   r   r   r   r   r\   self_outputsattention_outputr   s	            r=   rj   zTFConvBertAttention.callk  s\    **.)5FQY + 
  ,,\!_lU],^#%QR(88r>   c                   | j                   ry d| _         t        | dd       Mt        j                  | j                  j
                        5  | j                  j                  d        d d d        t        | dd       Nt        j                  | j                  j
                        5  | j                  j                  d        d d d        y y # 1 sw Y   exY w# 1 sw Y   y xY w)NTr   r   )rL   rM   rG   rH   r   r*   rN   r   rO   s     r=   rN   zTFConvBertAttention.buildt  s    ::
4)40<t22778 0##))$/04.:t00556 .!!''-. . ;0 0. .   C%CCC r   rk   )ro   rp   rq   r.   r   rj   rN   rs   rt   s   @r=   r   r   a  s    H"	.r>   r   c                  0     e Zd Z fdZd fd	Zd Z xZS )GroupedLinearLayerc                    t        |   di | || _        || _        || _        || _        | j                  | j                  z  | _        | j                  | j                  z  | _        y Nr,   )r-   r.   
input_sizeoutput_size
num_groupsr{   group_in_dimgroup_out_dim)r:   r   r   r   r{   r;   r<   s         r=   r.   zGroupedLinearLayer.__init__  s]    "6"$&$"4 OOt>!--@r>   c                "   | j                  d| j                  | j                  | j                  g| j                  d      | _        | j                  d| j                  g| j                  | j                  d      | _        t        | )  |       y )NkernelT)rB   rC   	trainablebias)rB   rC   r   r   )rI   r   r   r   r{   r   r   r   r   r-   rN   r:   rP   r<   s     r=   rN   zGroupedLinearLayer.build  s    oo%%t'8'8$//J//	 & 
 OO4++,$:Q:QY]YcYcos $ 
	 	k"r>   c                   t        |      d   }t        j                  t        j                  |d| j                  | j
                  g      g d      }t        j                  |t        j                  | j                  g d            }t        j                  |g d      }t        j                  ||d| j                  g      }t        j                  j                  || j                        }|S )Nr   rT   )r"   r   r   )r   r"   r   rV   r   )r   rG   r   r   r   r   r   r   r   nnbias_addr   )r:   r   r   r   s       r=   rj   zGroupedLinearLayer.call  s    .q1
LLMBIZIZ3[\^ghIIadkk9=>LLI&JJq:r4+;+;<=EENNN3r>   rk   )ro   rp   rq   r.   rN   rj   rs   rt   s   @r=   r   r     s    A#r>   r   c                  ,     e Zd Z fdZd ZddZ xZS )TFConvBertIntermediatec                    t        |   di | |j                  dk(  rEt        j                  j                  |j                  t        |j                        d      | _	        nFt        |j                  |j                  |j                  t        |j                        d      | _	        t        |j                  t              r"t        |j                        | _        || _        y |j                  | _        || _        y )Nr"   r   rz   r   r{   r*   r,   )r-   r.   r   r   r3   r   intermediate_sizer   r2   r   r   r   
isinstance
hidden_actstrr   intermediate_act_fnr/   r9   s      r=   r.   zTFConvBertIntermediate.__init__  s    "6"!++((_VMeMe=fmt , DJ ,""((!,,#263K3K#LDJ f''-'89J9J'KD$  (.'8'8D$r>   c                J    | j                  |      }| j                  |      }|S rk   )r   r   r:   r   s     r=   rj   zTFConvBertIntermediate.call  s&    

=100?r>   c                (   | j                   ry d| _         t        | dd       et        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        y y # 1 sw Y   y xY w)NTr   )	rL   rM   rG   rH   r   r*   rN   r/   r   rO   s     r=   rN   zTFConvBertIntermediate.build  s}    ::
4$'3tzz/ H

  $dkk.E.E!FGH H 4H Hs   3BBrk   r   rt   s   @r=   r   r     s    *Hr>   r   c                  .     e Zd Z fdZddZddZ xZS )TFConvBertOutputc                "   t        |   di | |j                  dk(  rEt        j                  j                  |j                  t        |j                        d      | _	        nFt        |j                  |j                  |j                  t        |j                        d      | _	        t        j                  j                  |j                  d      | _        t        j                  j                  |j                         | _        || _        y )Nr"   r   rz   r   r'   r(   r,   )r-   r.   r   r   r3   r   r   r   r2   r   r   r   r4   r5   r'   r6   r7   r8   r/   r9   s      r=   r.   zTFConvBertOutput.__init__  s    "6"!++""vG_G_7`gn , DJ ,((""!,,#263K3K#LDJ 88AVAV]h8i||++F,F,FGr>   c                v    | j                  |      }| j                  ||      }| j                  ||z         }|S r   r   r   s       r=   rj   zTFConvBertOutput.call  r   r>   c                "   | j                   ry d| _         t        | dd       dt        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        t        | dd       et        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        y y # 1 sw Y   |xY w# 1 sw Y   y xY wNTr'   r   )rL   rM   rG   rH   r'   r*   rN   r/   r   r   r   rO   s     r=   rN   zTFConvBertOutput.build  s    ::
4d+7t~~223 L$$dD$++2I2I%JKL4$'3tzz/ N

  $dkk.K.K!LMN N 4L LN Nr   r   rk   r   rt   s   @r=   r  r    s    &	Nr>   r  c                  .     e Zd Z fdZddZddZ xZS )TFConvBertLayerc                    t        |   di | t        |d      | _        t	        |d      | _        t        |d      | _        y )N	attentionr   intermediater   r,   )r-   r.   r   r
  r   r  r  bert_outputr9   s      r=   r.   zTFConvBertLayer.__init__  s?    "6",V+F26O+FBr>   c                    | j                  |||||      }|d   }| j                  |      }| j                  |||      }	|	f|dd  z   }
|
S r   )r
  r  r  )r:   r   r   r   r   r\   attention_outputsr   intermediate_outputlayer_outputr   s              r=   rj   zTFConvBertLayer.call  su     NN>96GRZ + 
 -Q/"//0@A''(;=MX`'a/$5ab$99r>   c                   | j                   ry d| _         t        | dd       Mt        j                  | j                  j
                        5  | j                  j                  d        d d d        t        | dd       Mt        j                  | j                  j
                        5  | j                  j                  d        d d d        t        | dd       Nt        j                  | j                  j
                        5  | j                  j                  d        d d d        y y # 1 sw Y   xY w# 1 sw Y   qxY w# 1 sw Y   y xY w)NTr
  r  r  )	rL   rM   rG   rH   r
  r*   rN   r  r  rO   s     r=   rN   zTFConvBertLayer.build   s	   ::
4d+7t~~223 +$$T*+4.:t00556 .!!''-.4-9t//445 -  &&t,- - :+ +. .- -$   D%%D1?D=%D.1D:=Er   rk   r   rt   s   @r=   r  r    s    C	-r>   r  c                  0     e Zd Z fdZ	 ddZddZ xZS )TFConvBertEncoderc                    t        |   di | t        |j                        D cg c]  }t	        |d|        c}| _        y c c}w )Nzlayer_._r   r,   )r-   r.   ra   num_hidden_layersr  layer)r:   r/   r;   r   r<   s       r=   r.   zTFConvBertEncoder.__init__  sA    "6"LQRXRjRjLklqofXaS>Bl
ls   Ac                    |rdnd }|rdnd }	t        | j                        D ].  \  }
}|r||fz   } |||||
   ||      }|d   }|s&|	|d   fz   }	0 |r||fz   }|st        d |||	fD              S t        |||	      S )Nr,   r   r   r"   c              3  &   K   | ]	  }||  y wrk   r,   ).0vs     r=   	<genexpr>z)TFConvBertEncoder.call.<locals>.<genexpr>3  s     hqZ[Zghs   )last_hidden_stater   
attentions)	enumerater  tupler	   )r:   r   r   r   r   output_hidden_statesreturn_dictr\   all_hidden_statesall_attentionsr   layer_modulelayer_outputss                r=   rj   zTFConvBertEncoder.call  s     #7BD0d(4 
	FOA|#$58H$H!(~y|=NYaM *!,M !/=3C2E!E
	F   1]4D Dh]4E~$Vhhh +;LYg
 	
r>   c                    | j                   ry d| _         t        | dd       K| j                  D ];  }t        j                  |j
                        5  |j                  d        d d d        = y y # 1 sw Y   IxY w)NTr  )rL   rM   r  rG   rH   r*   rN   )r:   rP   r  s      r=   rN   zTFConvBertEncoder.build9  sp    ::
4$'3 &]]5::. &KK%& && 4& &s   A..A7	r   rk   r   rt   s   @r=   r  r    s    m "
H&r>   r  c                  ,     e Zd Z fdZd ZddZ xZS )!TFConvBertPredictionHeadTransformc                   t        |   di | t        j                  j	                  |j
                  t        |j                        d      | _        t        |j                  t              rt        |j                        | _        n|j                  | _        t        j                  j                  |j                  d      | _        || _        y r   )r-   r.   r   r3   r   r0   r   r2   r   r   r   r   r   transform_act_fnr4   r5   r'   r/   r9   s      r=   r.   z*TFConvBertPredictionHeadTransform.__init__D  s    "6"\\''!!ofF^F^6_fm ( 

 f''-$5f6G6G$HD!$*$5$5D!88AVAV]h8ir>   c                l    | j                  |      }| j                  |      }| j                  |      }|S rk   )r   r+  r'   r   s     r=   rj   z&TFConvBertPredictionHeadTransform.callS  s4    

=1--m<}5r>   c                "   | j                   ry d| _         t        | dd       dt        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        t        | dd       et        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        y y # 1 sw Y   |xY w# 1 sw Y   y xY wr   r   rO   s     r=   rN   z'TFConvBertPredictionHeadTransform.buildZ  r   r   rk   r   rt   s   @r=   r)  r)  C  s    	Lr>   r)  c                  n     e Zd ZeZ fdZd Zd Zd Zd Z	d Z
e	 	 	 	 	 	 	 	 	 	 d	d       Zd
dZ xZS )TFConvBertMainLayerc                   t        |   di | t        |d      | _        |j                  |j
                  k7  r0t        j                  j                  |j
                  d      | _	        t        |d      | _        || _        y )NrE   r   embeddings_projectencoderr,   )r-   r.   r%   rE   r0   r   r   r3   r   r1  r  r2  r/   r9   s      r=   r.   zTFConvBertMainLayer.__init__j  sm    "6".vLI  F$6$66&+ll&8&89K9KRf&8&gD#(i@r>   c                    | j                   S rk   )rE   r:   s    r=   get_input_embeddingsz(TFConvBertMainLayer.get_input_embeddingsu  s    r>   c                b    || j                   _        |j                  d   | j                   _        y Nr   )rE   rA   rB   rJ   r:   rV   s     r=   set_input_embeddingsz(TFConvBertMainLayer.set_input_embeddingsx  s"    !&%*[[^"r>   c                    t         )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
        r   )r:   heads_to_prunes     r=   _prune_headsz TFConvBertMainLayer._prune_heads|  s
    
 "!r>   c                    |t        j                  |d      }t        j                  ||d   dd|d   f      }t        j                  ||      }d|z
  dz  }|S )Nr"   r   g      ?g     )rG   r_   r   r   )r:   r   rP   r   extended_attention_masks        r=   get_extended_attention_maskz/TFConvBertMainLayer.get_extended_attention_mask  sk    !WW[!4N #%**^k!naQRT_`aTb=c"d #%''*A5"I#&)@#@H"L&&r>   c                J    |t         d g| j                  j                  z  }|S rk   )r   r/   r  )r:   r   s     r=   get_head_maskz!TFConvBertMainLayer.get_head_mask  s*     %%!>!>>Ir>   c           	        ||t        d      |t        |      }n|t        |      d d }nt        d      |t        j                  |d      }|t        j                  |d      }| j	                  |||||
      }| j                  |||j                        }| j                  |      }t        | d      r| j                  ||
      }| j                  ||||||	|
      }|S )NzDYou cannot specify both input_ids and inputs_embeds at the same timerT   z5You have to specify either input_ids or inputs_embedsr"   r   r   r1  )r]   r   rG   r_   rE   r?  r   rA  hasattrr1  r2  )r:   rb   r   rd   rc   r   re   r   r!  r"  r\   rP   r   r>  s                 r=   rj   zTFConvBertMainLayer.call  s     ]%>cdd"$Y/K&$]3CR8KTUU!WW[!4N!WW[!4N	<Q^iqr"&"B"B>S^`m`s`s"t&&y1	4-. 33MH3UM#  % 
 r>   c                   | j                   ry d| _         t        | dd       Mt        j                  | j                  j
                        5  | j                  j                  d        d d d        t        | dd       Mt        j                  | j                  j
                        5  | j                  j                  d        d d d        t        | dd       et        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        y y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)NTrE   r2  r1  )rL   rM   rG   rH   rE   r*   rN   r2  r1  r/   r0   rO   s     r=   rN   zTFConvBertMainLayer.build  s   ::
4t,8t334 ,%%d+,4D)5t||001 )""4()4-t4@t66;;< X''--tT4;;;U;U.VWX X A, ,) )X X$   D<%E?3E<EEE
NNNNNNNNNFrk   )ro   rp   rq   r#   config_classr.   r5  r9  r<  r?  rA  r   rj   rN   rs   rt   s   @r=   r/  r/  f  sa    !L	4"'*  !- -^Xr>   r/  c                      e Zd ZdZeZdZy)TFConvBertPreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    convbertN)ro   rp   rq   rr   r#   rG  base_model_prefixr,   r>   r=   rI  rI    s    
 "L"r>   rI  ax	  

    This model inherits from [`TFPreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

    This model is also a [keras.Model](https://www.tensorflow.org/api_docs/python/tf/keras/Model) subclass. Use it
    as a regular TF 2.0 Keras Model and refer to the TF 2.0 documentation for all matter related to general usage and
    behavior.

    <Tip>

    TensorFlow models and layers in `transformers` accept two formats as input:

    - having all inputs as keyword arguments (like PyTorch models), or
    - having all inputs as a list, tuple or dict in the first positional argument.

    The reason the second format is supported is that Keras methods prefer this format when passing inputs to models
    and layers. Because of this support, when using methods like `model.fit()` things should "just work" for you - just
    pass your inputs and labels in any format that `model.fit()` supports! If, however, you want to use the second
    format outside of Keras methods like `fit()` and `predict()`, such as when creating your own layers or models with
    the Keras `Functional` API, there are three possibilities you can use to gather all the input Tensors in the first
    positional argument:

    - a single Tensor with `input_ids` only and nothing else: `model(input_ids)`
    - a list of varying length with one or several input Tensors IN THE ORDER given in the docstring:
    `model([input_ids, attention_mask])` or `model([input_ids, attention_mask, token_type_ids])`
    - a dictionary with one or several input Tensors associated to the input names given in the docstring:
    `model({"input_ids": input_ids, "token_type_ids": token_type_ids})`

    Note that when creating models and layers with
    [subclassing](https://keras.io/guides/making_new_layers_and_models_via_subclassing/) then you don't need to worry
    about any of this, as you can just pass inputs like you would to any other Python function!

    </Tip>

    Args:
        config ([`ConvBertConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
a  
    Args:
        input_ids (`Numpy array` or `tf.Tensor` of shape `({0})`):
            Indices of input sequence tokens in the vocabulary.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.__call__`] and
            [`PreTrainedTokenizer.encode`] for details.

            [What are input IDs?](../glossary#input-ids)
        attention_mask (`Numpy array` or `tf.Tensor` of shape `({0})`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

            [What are attention masks?](../glossary#attention-mask)
        token_type_ids (`Numpy array` or `tf.Tensor` of shape `({0})`, *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 (`Numpy array` or `tf.Tensor` of shape `({0})`, *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)
        head_mask (`Numpy array` or `tf.Tensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules. Mask values selected in `[0, 1]`:

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.

        inputs_embeds (`tf.Tensor` of shape `({0}, 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.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail. This argument can be used only in eager mode, in graph mode the value in the
            config will be used instead.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail. This argument can be used only in eager mode, in graph mode the value in the config will be
            used instead.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. This argument can be used in
            eager mode, in graph mode the value will always be set to True.
        training (`bool`, *optional*, defaults to `False`):
            Whether or not to use the model in training mode (some modules like dropout modules have different
            behaviors between training and evaluation).
zbThe bare ConvBERT Model transformer outputting raw hidden-states without any specific head on top.c                       e Zd Z fdZe eej                  d             ee	e
e      	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd                     ZddZ xZS )	TFConvBertModelc                P    t        |   |g|i | t        |d      | _        y )NrJ  r   )r-   r.   r/  rJ  r:   r/   r[   r;   r<   s       r=   r.   zTFConvBertModel.__init__P  s(    3&3F3+FDr>   batch_size, sequence_length
checkpointoutput_typerG  c                <    | j                  |||||||||	|

      }|S )N
rb   r   rd   rc   r   re   r   r!  r"  r\   )rJ  )r:   rb   r   rd   rc   r   re   r   r!  r"  r\   r   s               r=   rj   zTFConvBertModel.callU  s<    ( --))%'/!5#   
 r>   c                    | j                   ry d| _         t        | dd       Nt        j                  | j                  j
                        5  | j                  j                  d        d d d        y y # 1 sw Y   y xY w)NTrJ  )rL   rM   rG   rH   rJ  r*   rN   rO   s     r=   rN   zTFConvBertModel.buildx  si    ::
4T*6t}}112 *##D)* * 7* *s   A11A:rF  )rb   TFModelInputType | Noner   $Optional[Union[np.array, tf.Tensor]]rd   rX  rc   rX  r   rX  re   tf.Tensor | Noner   Optional[bool]r!  rZ  r"  rZ  r\   rm   rn   z*Union[TFBaseModelOutput, Tuple[tf.Tensor]]rk   )ro   rp   rq   r.   r   r    CONVBERT_INPUTS_DOCSTRINGformatr   _CHECKPOINT_FOR_DOCr	   _CONFIG_FOR_DOCrj   rN   rs   rt   s   @r=   rM  rM  K  s    
E
 *+D+K+KLi+jk&%$ .2?C?C=A:>*.,0/3&** = =	
 ; 8 ( * - $  
4 l 8*r>   rM  c                  F     e Zd Z fdZ fdZd Zd Zd Zd Zd Z	 xZ
S )TFConvBertMaskedLMHeadc                b    t        |   di | || _        |j                  | _        || _        y r   )r-   r.   r/   r0   input_embeddings)r:   r/   rb  r;   r<   s       r=   r.   zTFConvBertMaskedLMHead.__init__  s0    "6"$33 0r>   c                    | j                  | j                  j                  fddd      | _        t        |   |       y )NzerosTr   )rB   rC   r   r*   )rI   r/   rJ   r   r-   rN   r   s     r=   rN   zTFConvBertMaskedLMHead.build  s7    OO4;;+A+A*CQXdhouOv	k"r>   c                    | j                   S rk   )rb  r4  s    r=   get_output_embeddingsz,TFConvBertMaskedLMHead.get_output_embeddings  s    $$$r>   c                `    || j                   _        t        |      d   | j                   _        y r7  )rb  rA   r   rJ   r8  s     r=   set_output_embeddingsz,TFConvBertMaskedLMHead.set_output_embeddings  s(    ',$+5e+<Q+?(r>   c                    d| j                   iS )Nr   )r   r4  s    r=   get_biaszTFConvBertMaskedLMHead.get_bias  s    		""r>   c                X    |d   | _         t        |d         d   | j                  _        y )Nr   r   )r   r   r/   rJ   r8  s     r=   set_biaszTFConvBertMaskedLMHead.set_bias  s'    &M	!+E&M!:1!=r>   c                t   t        |      d   }t        j                  |d| j                  g      }t        j                  || j
                  j                  d      }t        j                  |d|| j                  j                  g      }t        j                  j                  || j                        }|S )N)tensorr"   rT   )rn  rB   T)abr   r   )r   rG   r   r0   r   rb  rA   r/   rJ   r   r   r   )r:   r   
seq_lengths      r=   rj   zTFConvBertMaskedLMHead.call  s    }5a8


-DDWDW?XY		MT5J5J5Q5Q_cd

-JPTP[P[PfPf?gh]Kr>   )ro   rp   rq   r.   rN   rf  rh  rj  rl  rj   rs   rt   s   @r=   r`  r`    s'    1#
%@#>r>   r`  c                  .     e Zd Z fdZddZddZ xZS )TFConvBertGeneratorPredictionsc                    t        |   di | t        j                  j	                  |j
                  d      | _        t        j                  j                  |j                  d      | _	        || _
        y )Nr'   r(   r   r   r,   )r-   r.   r   r3   r4   r5   r'   r   r0   r   r/   r9   s      r=   r.   z'TFConvBertGeneratorPredictions.__init__  s]    "6"88AVAV]h8i\\''(=(=G'L
r>   c                l    | j                  |      } t        d      |      }| j                  |      }|S )Ngelu)r   r   r'   )r:   generator_hidden_statesr\   r   s       r=   rj   z#TFConvBertGeneratorPredictions.call  s7    

#:;1)&1-@}5r>   c                "   | j                   ry d| _         t        | dd       dt        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        t        | dd       et        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        y y # 1 sw Y   |xY w# 1 sw Y   y xY wr  )rL   rM   rG   rH   r'   r*   rN   r/   r0   r   r   rO   s     r=   rN   z$TFConvBertGeneratorPredictions.build  s    ::
4d+7t~~223 O$$dD$++2L2L%MNO4$'3tzz/ H

  $dkk.E.E!FGH H 4O OH Hr   r   rk   r   rt   s   @r=   rs  rs    s    	Hr>   rs  z6ConvBERT Model with a `language modeling` head on top.c                       e Zd Z fdZd Zd Ze eej                  d             e
eee      	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	d                     Zd
dZ xZS )TFConvBertForMaskedLMc                V   t        |   |fi | || _        t        |d      | _        t        |d      | _        t        |j                  t              rt        |j                        | _        n|j                  | _        t        || j                  j                  d      | _        y )NrJ  r   generator_predictionsgenerator_lm_head)r-   r.   r/   r/  rJ  rs  r|  r   r   r   r   r   r`  rE   r}  rO  s       r=   r.   zTFConvBertForMaskedLM.__init__  s    *6*+FD%CFQh%i"f''-/0A0ABDO$//DO!7@X@X_r!sr>   c                    | j                   S rk   )r}  r4  s    r=   get_lm_headz!TFConvBertForMaskedLM.get_lm_head  s    %%%r>   c                N    | j                   dz   | j                  j                   z   S )N/)r*   r}  r4  s    r=   get_prefix_bias_namez*TFConvBertForMaskedLM.get_prefix_bias_name  s!    yy3!7!7!<!<<<r>   rP  rQ  c                *   | j                  |||||||||	|
      }|d   }| j                  ||      }| j                  ||      }|
dn| j                  |
|      }|	s|f|dd z   }||f|z   S |S t	        |||j
                  |j                        S )a  
        labels (`tf.Tensor` 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]`
        rU  r   r   Nr"   losslogitsr   r  )rJ  r|  r}  hf_compute_lossr
   r   r  )r:   rb   r   rd   rc   r   re   r   r!  r"  labelsr\   rw  generator_sequence_outputprediction_scoresr  r   s                    r=   rj   zTFConvBertForMaskedLM.call  s    6 #'--))%'/!5# #0 #
 %<A$>! 667P[c6d 223Dx2X~t4+?+?HY+Z'),CAB,GGF)-)9TGf$EvE$1??.99	
 	
r>   c                   | j                   ry d| _         t        | dd       Mt        j                  | j                  j
                        5  | j                  j                  d        d d d        t        | dd       Mt        j                  | j                  j
                        5  | j                  j                  d        d d d        t        | dd       Nt        j                  | j                  j
                        5  | j                  j                  d        d d d        y y # 1 sw Y   xY w# 1 sw Y   qxY w# 1 sw Y   y xY w)NTrJ  r|  r}  )	rL   rM   rG   rH   rJ  r*   rN   r|  r}  rO   s     r=   rN   zTFConvBertForMaskedLM.build  s   ::
4T*6t}}112 *##D)*40$7Ct99>>? 7**00674,d3?t55::; 3&&,,T23 3 @* *7 73 3r  NNNNNNNNNNF)rb   rW  r   np.ndarray | tf.Tensor | Nonerd   r  rc   r  r   r  re   rY  r   rZ  r!  rZ  r"  rZ  r  rY  r\   rZ  rn   zUnion[Tuple, TFMaskedLMOutput]rk   )ro   rp   rq   r.   r  r  r   r    r[  r\  r   r]  r
   r^  rj   rN   rs   rt   s   @r=   rz  rz    s    t&= *+D+K+KLi+jk&$$ .28<8<6:37*.,0/3&*#'#(/
*/
 6/
 6	/

 4/
 1/
 (/
 */
 -/
 $/
 !/
 !/
 
(/
 l /
b3r>   rz  c                  0     e Zd ZdZ fdZd ZddZ xZS )TFConvBertClassificationHeadz-Head for sentence-level classification tasks.c                   t        |   di | t        j                  j	                  |j
                  t        |j                        d      | _        |j                  |j                  n|j                  }t        j                  j                  |      | _        t        j                  j	                  |j                  t        |j                        d      | _        || _        y )Nr   rz   out_projr,   )r-   r.   r   r3   r   r   r   r2   r   classifier_dropoutr7   r6   r8   
num_labelsr  r/   )r:   r/   r;   r  r<   s       r=   r.   z%TFConvBertClassificationHead.__init__!  s    "6"\\''?6C[C[3\cj ( 

 *0)B)B)NF%%TZTnTn 	 ||++,>?**/&BZBZ2[bl + 
 r>   c                    |d d dd d f   }| j                  |      }| j                  |      } t        | j                  j                        |      }| j                  |      }| j                  |      }|S r7  )r8   r   r   r/   r   r  )r:   r   r;   r   s       r=   rj   z!TFConvBertClassificationHead.call1  sh    !Q'"LLOJJqM5dkk445a8LLOMM!r>   c                "   | j                   ry d| _         t        | dd       dt        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        t        | dd       et        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        y y # 1 sw Y   |xY w# 1 sw Y   y xY w)NTr   r  )
rL   rM   rG   rH   r   r*   rN   r/   r   r  rO   s     r=   rN   z"TFConvBertClassificationHead.build;  s    ::
4$'3tzz/ H

  $dkk.E.E!FGH4T*6t}}112 K##T41H1H$IJK K 7H HK Kr   rk   )ro   rp   rq   rr   r.   rj   rN   rs   rt   s   @r=   r  r    s    7 	Kr>   r  zp
    ConvBERT Model transformer with a sequence classification/regression head on top e.g., for GLUE tasks.
    c                       e Zd Z fdZe eej                  d             ee	e
e      	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd                     ZddZ xZS )	#TFConvBertForSequenceClassificationc                    t        |   |g|i | |j                  | _        t        |d      | _        t        |d      | _        y )NrJ  r   
classifier)r-   r.   r  r/  rJ  r  r  rO  s       r=   r.   z,TFConvBertForSequenceClassification.__init__N  sC    3&3F3 +++FD6vLQr>   rP  rQ  c                    | j                  |||||||||	|
      }| j                  |d   |      }|
dn| j                  |
|      }|	s|f|dd z   }||f|z   S |S t        |||j                  |j
                        S )a  
        labels (`tf.Tensor` 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).
        	r   rd   rc   r   re   r   r!  r"  r\   r   r   Nr"   r  )rJ  r  r  r   r   r  )r:   rb   r   rd   rc   r   re   r   r!  r"  r  r\   r   r  r  r   s                   r=   rj   z(TFConvBertForSequenceClassification.callT  s    6 --))%'/!5#   
 h?~t4+?+?+OY,F)-)9TGf$EvE)!//))	
 	
r>   c                   | j                   ry d| _         t        | dd       Mt        j                  | j                  j
                        5  | j                  j                  d        d d d        t        | dd       Nt        j                  | j                  j
                        5  | j                  j                  d        d d d        y y # 1 sw Y   exY w# 1 sw Y   y xY wNTrJ  r  )rL   rM   rG   rH   rJ  r*   rN   r  rO   s     r=   rN   z)TFConvBertForSequenceClassification.build  s    ::
4T*6t}}112 *##D)*4t,8t334 ,%%d+, , 9* *, ,r   r  )rb   rW  r   r  rd   r  rc   r  r   r  re   rY  r   rZ  r!  rZ  r"  rZ  r  rY  r\   rZ  rn   z(Union[Tuple, TFSequenceClassifierOutput]rk   )ro   rp   rq   r.   r   r    r[  r\  r   r]  r   r^  rj   rN   rs   rt   s   @r=   r  r  G  s    R *+D+K+KLi+jk&.$ .28<8<6:37*.,0/3&*#'#(-
*-
 6-
 6	-

 4-
 1-
 (-
 *-
 --
 $-
 !-
 !-
 
2-
 l -
^	,r>   r  z
    ConvBERT Model with a multiple choice classification head on top (a linear layer on top of the pooled output and a
    softmax) e.g. for RocStories/SWAG tasks.
    c                       e Zd Z fdZe eej                  d             ee	e
e      	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd                     ZddZ xZS )	TFConvBertForMultipleChoicec                   t        |   |g|i | t        |d      | _        t	        ||j
                  d      | _        t        j                  j                  dt        |j
                        d      | _        || _        y )NrJ  r   sequence_summary)r2   r*   r"   r  rz   )r-   r.   r/  rJ  r   r2   r  r   r3   r   r   r  r/   rO  s       r=   r.   z$TFConvBertForMultipleChoice.__init__  s|    3&3F3+FD 1f&>&>EW!
  ,,,,/&2J2J"KR^ - 
 r>   z(batch_size, num_choices, sequence_lengthrQ  c                   |t        |      d   }t        |      d   }nt        |      d   }t        |      d   }|t        j                  |d|f      nd}|t        j                  |d|f      nd}|t        j                  |d|f      nd}|t        j                  |d|f      nd}|%t        j                  |d|t        |      d   f      nd}| j                  |||||||||	|
      }| j	                  |d   |      }| j                  |      }t        j                  |d|f      }|
dn| j                  |
|      }|	s|f|dd z   }||f|z   S |S t        |||j                  |j                  	      S )
a5  
        labels (`tf.Tensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]`
            where `num_choices` is the size of the second dimension of the input tensors. (See `input_ids` above)
        Nr"   r   rT   r   )r"  r\   r   r   r  )
r   rG   r   rJ  r  r  r  r   r   r  )r:   rb   r   rd   rc   r   re   r   r!  r"  r  r\   num_choicesrq  flat_input_idsflat_attention_maskflat_token_type_idsflat_position_idsflat_inputs_embedsr   r  reshaped_logitsr  r   s                           r=   rj   z TFConvBertForMultipleChoice.call  s   8  $Y/2K#I.q1J$]3A6K#M215JDMDYIJ/?@_cN\Nhbjj"j9IJnrN\Nhbjj"j9IJnrJVJbBJJ|b*5EFhl ( JJ}r:z-7PQR7S&TU 	
 -- #   
 &&wqzH&E(**Vb+->?~t4+?+?+X%''!"+5F)-)9TGf$EvE*"!//))	
 	
r>   c                   | j                   ry d| _         t        | dd       Mt        j                  | j                  j
                        5  | j                  j                  d        d d d        t        | dd       Mt        j                  | j                  j
                        5  | j                  j                  d        d d d        t        | dd       et        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        y y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)NTrJ  r  r  )rL   rM   rG   rH   rJ  r*   rN   r  r  r/   r   rO   s     r=   rN   z!TFConvBertForMultipleChoice.build  s   ::
4T*6t}}112 *##D)*4+T2>t4499: 2%%++D124t,8t334 M%%tT4;;3J3J&KLM M 9* *2 2M MrE  r  )rb   rW  r   r  rd   r  rc   r  r   r  re   rY  r   rZ  r!  rZ  r"  rZ  r  rY  r\   rZ  rn   z)Union[Tuple, TFMultipleChoiceModelOutput]rk   )ro   rp   rq   r.   r   r    r[  r\  r   r]  r   r^  rj   rN   rs   rt   s   @r=   r  r    s    
 *!(()ST  &/$ .28<8<6:37*.,0/3&*#'#(>
*>
 6>
 6	>

 4>
 1>
 (>
 *>
 ->
 $>
 !>
 !>
 
3>
 >
@Mr>   r  z
    ConvBERT Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for
    Named-Entity-Recognition (NER) tasks.
    c                       e Zd Z fdZe eej                  d             ee	e
e      	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd                     ZddZ xZS )	 TFConvBertForTokenClassificationc                   t        |   |g|i | |j                  | _        t        |d      | _        |j
                  |j
                  n|j                  }t        j                  j                  |      | _
        t        j                  j                  |j                  t        |j                        d      | _        || _        y )NrJ  r   r  rz   )r-   r.   r  r/  rJ  r  r7   r   r3   r6   r8   r   r   r2   r  r/   )r:   r/   r[   r;   r  r<   s        r=   r.   z)TFConvBertForTokenClassification.__init__
  s    3&3F3 +++FD)/)B)B)NF%%TZTnTn 	 ||++,>?,,,,/&BZBZ2[bn - 
 r>   rP  rQ  c                &   | j                  |||||||||	|
      }|d   }| j                  ||      }| j                  |      }|
dn| j                  |
|      }|	s|f|dd z   }||f|z   S |S t	        |||j
                  |j                        S )z
        labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`.
        r  r   r   Nr"   r  )rJ  r8   r  r  r   r   r  )r:   rb   r   rd   rc   r   re   r   r!  r"  r  r\   r   sequence_outputr  r  r   s                    r=   rj   z%TFConvBertForTokenClassification.call  s    2 --))%'/!5#   
 "!*,,,J1~t4+?+?+OY,F)-)9TGf$EvE&!//))	
 	
r>   c                   | j                   ry d| _         t        | dd       Mt        j                  | j                  j
                        5  | j                  j                  d        d d d        t        | dd       et        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        y y # 1 sw Y   |xY w# 1 sw Y   y xY wr  )
rL   rM   rG   rH   rJ  r*   rN   r  r/   r   rO   s     r=   rN   z&TFConvBertForTokenClassification.buildM      ::
4T*6t}}112 *##D)*4t,8t334 M%%tT4;;3J3J&KLM M 9* *M M   C"%3C."C+.C7r  )rb   rW  r   r  rd   r  rc   r  r   r  re   rY  r   rZ  r!  rZ  r"  rZ  r  rY  r\   rZ  rn   z%Union[Tuple, TFTokenClassifierOutput]rk   )ro   rp   rq   r.   r   r    r[  r\  r   r]  r   r^  rj   rN   rs   rt   s   @r=   r  r    s     *+D+K+KLi+jk&+$ .28<8<6:37*.,0/3&*#'#(,
*,
 6,
 6	,

 4,
 1,
 (,
 *,
 -,
 $,
 !,
 !,
 
/,
 l ,
\	Mr>   r  z
    ConvBERT Model with a span classification head on top for extractive question-answering tasks like SQuAD (a linear
    layer on top of the hidden-states output to compute `span start logits` and `span end logits`).
    c                       e Zd Z fdZe eej                  d             ee	e
e      	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd                     ZddZ xZS )	TFConvBertForQuestionAnsweringc                   t        |   |g|i | |j                  | _        t        |d      | _        t
        j                  j                  |j                  t        |j                        d      | _
        || _        y )NrJ  r   
qa_outputsrz   )r-   r.   r  r/  rJ  r   r3   r   r   r2   r  r/   rO  s       r=   r.   z'TFConvBertForQuestionAnswering.__init__a  sr    3&3F3 +++FD,,,,/&BZBZ2[bn - 
 r>   rP  rQ  c                   | j                  |||||||||	|
      }|d   }| j                  |      }t        j                  |dd      \  }}t        j                  |d      }t        j                  |d      }d}|
|d|
i}||d<   | j                  |||f      }|	s||f|d	d z   }||f|z   S |S t        ||||j                  |j                  
      S )a  
        start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*):
            Labels for position (index) of the start of the labelled span for computing the token classification loss.
            Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
            are not taken into account for computing the loss.
        end_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*):
            Labels for position (index) of the end of the labelled span for computing the token classification loss.
            Positions are clamped to the length of the sequence (`sequence_length`). Position outside of the sequence
            are not taken into account for computing the loss.
        r  r   r   rT   rY   Nstart_positionend_positionr"   )r  start_logits
end_logitsr   r  )	rJ  r  rG   splitsqueezer  r   r   r  )r:   rb   r   rd   rc   r   re   r   r!  r"  start_positionsend_positionsr\   r   r  r  r  r  r  r  r   s                        r=   rj   z#TFConvBertForQuestionAnswering.callk  s   @ --))%'/!5#   
 "!*1#%88FAB#? jzz,R8ZZ
4
&=+D&8F%2F>"''z0JKD"J/'!"+=F)-)9TGf$EvE-%!!//))
 	
r>   c                   | j                   ry d| _         t        | dd       Mt        j                  | j                  j
                        5  | j                  j                  d        d d d        t        | dd       et        j                  | j                  j
                        5  | j                  j                  d d | j                  j                  g       d d d        y y # 1 sw Y   |xY w# 1 sw Y   y xY w)NTrJ  r  )
rL   rM   rG   rH   rJ  r*   rN   r  r/   r   rO   s     r=   rN   z$TFConvBertForQuestionAnswering.build  r  r  )NNNNNNNNNNNF)rb   rW  r   r  rd   r  rc   r  r   r  re   rY  r   rZ  r!  rZ  r"  rZ  r  rY  r  rY  r\   rZ  rn   z,Union[Tuple, TFQuestionAnsweringModelOutput]rk   )ro   rp   rq   r.   r   r    r[  r\  r   r]  r   r^  rj   rN   rs   rt   s   @r=   r  r  Y  s     *+D+K+KLi+jk&2$ .28<8<6:37*.,0/3&*,0*.#(;
*;
 6;
 6	;

 4;
 1;
 (;
 *;
 -;
 $;
 *;
 (;
 !;
 
6;
 l ;
z	Mr>   r  )Jrr   
__future__r   typingr   r   r   numpynp
tensorflowrG   activations_tfr   modeling_tf_outputsr	   r
   r   r   r   r   modeling_tf_utilsr   r   r   r   r   r   r   r   r   r   r   r   tf_utilsr   r   r   utilsr   r   r    r!   configuration_convbertr#   
get_loggerro   loggerr]  r^  r3   Layerr%   rv   r   r   r   r   r  r  r  r)  r/  rI  CONVBERT_START_DOCSTRINGr[  rM  r`  rs  rz  r  r  r  r  r  r,   r>   r=   <module>r     s    " ) )   /     S R  3 
		H	%/ "Q 5<<-- Q hlQell00 lQ^L5<<-- L<.%,,,, .>++ B"HU\\// "HJ$Nu||)) $NN-ell(( -D1&** 1&h L(:(:  LF uX%,,,, uX uXp# 1 #( T5 p h/*/ /*	/*d"U\\// "JHU\\%7%7 H6 RTlmY357S Y3 nY3x&K5<<#5#5 &KR  	F,*CEa F,F,R  bM";=Q bMbMJ  MM'@B[ MMMM`  XM%>@W XMXMr>   