
     `i                     $   d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlZd dlm	Z	 d dl
mZmZmZ ddlmZ ddlmZmZmZmZmZmZmZmZ dd	lmZ dd
lmZmZ ddlmZm Z m!Z! ddl"m#Z#  e!j$        e%          Z&d Z' G d de	j(                  Z)e	j*        e)dZ+ G d de	j(                  Z, G d de	j(                  Z- G d de	j(                  Z. G d de	j(                  Z/ G d de	j(                  Z0 G d de	j(                  Z1 G d de	j(                  Z2 G d  d!e	j(                  Z3 G d" d#e	j(                  Z4 G d$ d%e	j(                  Z5 G d& d'e	j(                  Z6 G d( d)e	j(                  Z7 G d* d+e	j(                  Z8 G d, d-e	j(                  Z9 G d. d/e	j(                  Z: G d0 d1e	j(                  Z; G d2 d3e	j(                  Z< G d4 d5e	j(                  Z=e  G d6 d7e                      Z>e e d89           G d: d;e                                  Z?e  G d< d=e>                      Z@ e d>9           G d? d@e>                      ZAe  G dA dBe>                      ZB G dC dDe	j(                  ZC e dE9           G dF dGe>                      ZD e dH9           G dI dJe>                      ZEe  G dK dLe>                      ZFe  G dM dNe>                      ZGe  G dO dPe>                      ZHg dQZIdS )R    N)	dataclass)OptionalUnion)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )ACT2FN)BaseModelOutputBaseModelOutputWithPoolingMaskedLMOutputMultipleChoiceModelOutputNextSentencePredictorOutputQuestionAnsweringModelOutputSequenceClassifierOutputTokenClassifierOutput)PreTrainedModel) find_pruneable_heads_and_indicesprune_linear_layer)ModelOutputauto_docstringlogging   )MobileBertConfigc           	         	 ddl }ddl}ddl}n)# t          $ r t                              d            w xY wt          j                            |          }t          	                    d|            |j
                            |          }g }g }	|D ]j\  }
}t          	                    d|
 d|            |j
                            ||
          }|                    |
           |	                    |           kt          ||	          D ]\  }
}|
                    dd          }
|
                    d	d
          }
|
                    dd          }
|
                    dd          }
|
                    d          }
t#          d |
D                       r1t          	                    dd                    |
                      | }|
D ]H}|                    d|          r|                    d|          }n|g}|d         dk    s|d         dk    rt)          |d          }n|d         dk    s|d         dk    rt)          |d          }n|d         dk    rt)          |d          }nv|d         dk    rt)          |d          }nY	 t)          ||d                   }nA# t*          $ r4 t          	                    dd                    |
                      Y w xY wt-          |          dk    rt/          |d                   }||         }J|dd         d k    rt)          |d          }n|dk    r|                    |          }	 |j        |j        k    sJ d!|j         d"|j         d#            n/# t4          $ r"}|xj        |j        |j        fz  c_         d}~ww xY wt          	                    d$|
            t9          j        |          |_        | S )%z'Load tf checkpoints in a pytorch model.r   NzLoading a TensorFlow model in PyTorch, requires TensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.z&Converting TensorFlow checkpoint from zLoading TF weight z with shape 	ffn_layerffnFakeLayerNorm	LayerNormextra_output_weightszdense/kernelbert
mobilebert/c              3      K   | ]}|d v V  	dS ))adam_vadam_mAdamWeightDecayOptimizerAdamWeightDecayOptimizer_1global_stepN ).0ns     /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/mobilebert/modeling_mobilebert.py	<genexpr>z0load_tf_weights_in_mobilebert.<locals>.<genexpr>V   s<       
 
 nn
 
 
 
 
 
    z	Skipping z[A-Za-z]+_\d+z_(\d+)kernelgammaweightoutput_biasbetabiasoutput_weightssquad
classifier   r   i_embeddingszPointer shape z and array shape z mismatchedzInitialize PyTorch weight )renumpy
tensorflowImportErrorloggererrorospathabspathinfotrainlist_variablesload_variableappendzipreplacesplitanyjoin	fullmatchgetattrAttributeErrorlenint	transposeshapeAssertionErrorargstorch
from_numpydata)modelconfigtf_checkpoint_pathr<   nptftf_path	init_varsnamesarraysnamerU   arraypointerm_namescope_namesnumes                     r.   load_tf_weights_in_mobilebertrk   5   sc   
			   Q	
 	
 	
 	 goo011G
KKBBBCCC''00IEF   eBBB5BBCCC&&w55Te5&)) 1/ 1/e||K//||O[99||2NCC||FL11zz#  
 

 
 
 
 
 	 KK4CHHTNN44555 	' 	'F||,f55 ' hhy&99%h1~))[^w-F-F!'844Q=00KNf4L4L!'622Q#333!'844Q7**!'<88%g{1~>>GG%   KK <CHHTNN < <===H ;1$$+a.))!#,#$$<=((gx00GGxLL''E	=EK///YYYYYY 0///  	 	 	FFw}ek22FF	 	777888'..Ls2    &5J'':K%$K%+N
N-N((N-c                   D     e Zd Zd fd	Zdej        dej        fdZ xZS )NoNormNc                     t                                                       t          j        t	          j        |                    | _        t          j        t	          j        |                    | _        d S N)	super__init__r   	ParameterrX   zerosr6   onesr3   )self	feat_sizeeps	__class__s      r.   rq   zNoNorm.__init__   sS    LY!7!788	l5:i#8#899r0   input_tensorreturnc                 &    || j         z  | j        z   S ro   )r3   r6   )ru   ry   s     r.   forwardzNoNorm.forward   s    dk)DI55r0   ro   __name__
__module____qualname__rq   rX   Tensorr|   __classcell__rx   s   @r.   rm   rm      sc        : : : : : :
6EL 6U\ 6 6 6 6 6 6 6 6r0   rm   )
layer_normno_normc                        e Zd ZdZ fdZ	 	 	 	 d
deej                 deej                 deej                 deej                 dej	        f
d	Z
 xZS )MobileBertEmbeddingszGConstruct the embeddings from word, position and token_type embeddings.c                    t                                                       |j        | _        |j        | _        |j        | _        t          j        |j        |j        |j                  | _	        t          j        |j
        |j                  | _        t          j        |j        |j                  | _        | j        rdnd}| j        |z  }t          j        ||j                  | _        t!          |j                 |j                  | _        t          j        |j                  | _        |                     dt/          j        |j
                                      d          d           d S )N)padding_idxr
   r   position_ids)r   F)
persistent)rp   rq   trigram_inputembedding_sizehidden_sizer   	Embedding
vocab_sizepad_token_idword_embeddingsmax_position_embeddingsposition_embeddingstype_vocab_sizetoken_type_embeddingsLinearembedding_transformationNORM2FNnormalization_typer    Dropouthidden_dropout_probdropoutregister_bufferrX   arangeexpand)ru   r\   embed_dim_multiplierembedded_input_sizerx   s       r.   rq   zMobileBertEmbeddings.__init__   sF   #1$3!-!|F,=v?Tbhbuvvv#%<0NPVPb#c#c %'\&2H&J\%]%]"$($6=qqA"14HH(*	2EvGY(Z(Z% !:;F<NOOz&"<== 	EL)GHHOOPWXXej 	 	
 	
 	
 	
 	
r0   N	input_idstoken_type_idsr   inputs_embedsrz   c           
      6   ||                                 }n|                                 d d         }|d         }|| j        d d d |f         }|+t          j        |t          j        | j        j                  }||                     |          }| j        rut          j        t          j
                            |d d dd f         g dd          |t          j
                            |d d d df         g dd          gd	          }| j        s| j        | j        k    r|                     |          }|                     |          }|                     |          }||z   |z   }	|                     |	          }	|                     |	          }	|	S )
Nr   r   dtypedevice)r   r   r   r   r   r           )value)r   r   r   r   r   r   r:   dim)sizer   rX   rs   longr   r   r   catr   
functionalpadr   r   r   r   r   r    r   )
ru   r   r   r   r   input_shape
seq_lengthr   r   
embeddingss
             r.   r|   zMobileBertEmbeddings.forward   s     #..**KK',,..ss3K ^
,QQQ^<L!"[EJtO`OghhhN  00;;M 	 "IM%%mAAAqrrE&:<N<N<NVY%ZZ!M%%mAAAssF&;=O=O=OWZ%[[
   M  	I!48H!H!H 99-HHM #66|DD $ : :> J J"%88;PP
^^J//
\\*--
r0   )NNNN)r~   r   r   __doc__rq   r   rX   
LongTensorFloatTensorr   r|   r   r   s   @r.   r   r      s        QQ
 
 
 
 
0 155937590 0E,-0 !!120 u/0	0
   120 
0 0 0 0 0 0 0 0r0   r   c                        e Zd Z fdZ	 	 	 ddej        dej        dej        deej                 deej                 dee         d	e	ej                 fd
Z
 xZS )MobileBertSelfAttentionc                    t                                                       |j        | _        t          |j        |j        z            | _        | j        | j        z  | _        t          j        |j        | j                  | _	        t          j        |j        | j                  | _
        t          j        |j        r|j        n|j        | j                  | _        t          j        |j                  | _        d S ro   )rp   rq   num_attention_headsrS   true_hidden_sizeattention_head_sizeall_head_sizer   r   querykeyuse_bottleneck_attentionr   r   r   attention_probs_dropout_probr   ru   r\   rx   s     r.   rq   z MobileBertSelfAttention.__init__   s    #)#= #&v'>A['[#\#\ !58PPYv68JKK
9V4d6HIIY'-'F^F##FL^`d`r
 

 z&"EFFr0   Nquery_tensor
key_tensorvalue_tensorattention_mask	head_maskoutput_attentionsrz   c                    |j         \  }}}	|                     |                              |d| j        | j                                      dd          }
|                     |                              |d| j        | j                                      dd          }|                     |                              |d| j        | j                                      dd          }t          j	        |
|                    dd                    }|t          j        | j                  z  }|||z   }t          j                            |d          }|                     |          }|||z  }t          j	        ||          }|                    dddd                                          }|                                d d         | j        fz   }|                    |          }|r||fn|f}|S )Nr   r   r:   r   r   r
   )rU   r   viewr   r   rT   r   r   rX   matmulmathsqrtr   r   softmaxr   permute
contiguousr   r   )ru   r   r   r   r   r   r   
batch_sizer   _query_layer	key_layervalue_layerattention_scoresattention_probscontext_layernew_context_layer_shapeoutputss                     r.   r|   zMobileBertSelfAttention.forward   s    %1$6!
JJJ|$$T*b$":D<TUUYq!__ 	 HHZ  T*b$":D<TUUYq!__ 	 JJ|$$T*b$":D<TUUYq!__ 	 !<Y5H5HR5P5PQQ+di8P.Q.QQ%/.@-//0@b/II ,,77 -	9O_kBB%--aAq99DDFF"/"4"4"6"6ss";t?Q>S"S%**+BCC6G]=/22mM]r0   NNNr~   r   r   rq   rX   r   r   r   booltupler|   r   r   s   @r.   r   r      s        G G G G G$ 7;15,0- -l- L- l	-
 !!23- E-.- $D>- 
u|	- - - - - - - -r0   r   c                   P     e Zd Z fdZdej        dej        dej        fdZ xZS )MobileBertSelfOutputc                 L   t                                                       |j        | _        t          j        |j        |j                  | _        t          |j                 |j        |j	                  | _
        | j        s t          j        |j                  | _        d S d S Nrw   )rp   rq   use_bottleneckr   r   r   denser   r   layer_norm_epsr    r   r   r   r   s     r.   rq   zMobileBertSelfOutput.__init__  s    $3Yv68OPP
 !:;F<SY_Ynooo" 	B:f&@AADLLL	B 	Br0   hidden_statesresidual_tensorrz   c                     |                      |          }| j        s|                     |          }|                     ||z             }|S ro   )r   r   r   r    ru   r   r   layer_outputss       r.   r|   zMobileBertSelfOutput.forward#  sK    

=11" 	8 LL77M}'FGGr0   r}   r   s   @r.   r   r     sn        B B B B BU\ EL UZUa        r0   r   c                        e Zd Z fdZd Z	 	 	 ddej        dej        dej        dej        deej                 d	eej                 d
ee	         de
ej                 fdZ xZS )MobileBertAttentionc                     t                                                       t          |          | _        t	          |          | _        t                      | _        d S ro   )rp   rq   r   ru   r   outputsetpruned_headsr   s     r.   rq   zMobileBertAttention.__init__,  sI    +F33	*622EEr0   c                    t          |          dk    rd S t          || j        j        | j        j        | j                  \  }}t          | j        j        |          | j        _        t          | j        j        |          | j        _        t          | j        j	        |          | j        _	        t          | j
        j        |d          | j
        _        | j        j        t          |          z
  | j        _        | j        j        | j        j        z  | j        _        | j                            |          | _        d S )Nr   r   r   )rR   r   ru   r   r   r   r   r   r   r   r   r   r   union)ru   headsindexs      r.   prune_headszMobileBertAttention.prune_heads2  s    u::??F7490$)2OQUQb
 
u
 -TY_eDD	*49=%@@	,TY_eDD	.t{/@%QOOO )-	(EE

(R	%"&)"?$)B_"_	 -33E::r0   Nr   r   r   layer_inputr   r   r   rz   c                     |                      ||||||          }|                     |d         |          }	|	f|dd          z   }
|
S )Nr   r   )ru   r   )ru   r   r   r   r   r   r   r   self_outputsattention_outputr   s              r.   r|   zMobileBertAttention.forwardD  s^     yy
 
  ;;|ADD#%QRR(88r0   r   )r~   r   r   rq   r   rX   r   r   r   r   r   r|   r   r   s   @r.   r   r   +  s        " " " " "; ; ;0 7;15,0 l L l	
 \ !!23 E-. $D> 
u|	       r0   r   c                   B     e Zd Z fdZdej        dej        fdZ xZS )MobileBertIntermediatec                    t                                                       t          j        |j        |j                  | _        t          |j        t                    rt          |j                 | _        d S |j        | _        d S ro   )rp   rq   r   r   r   intermediate_sizer   
isinstance
hidden_actstrr   intermediate_act_fnr   s     r.   rq   zMobileBertIntermediate.__init__^  sn    Yv68PQQ
f'-- 	9'-f.?'@D$$$'-'8D$$$r0   r   rz   c                 Z    |                      |          }|                     |          }|S ro   )r   r  ru   r   s     r.   r|   zMobileBertIntermediate.forwardf  s,    

=1100??r0   r}   r   s   @r.   r   r   ]  s^        9 9 9 9 9U\ el        r0   r   c                   P     e Zd Z fdZdej        dej        dej        fdZ xZS )OutputBottleneckc                 "   t                                                       t          j        |j        |j                  | _        t          |j                 |j        |j	                  | _
        t          j        |j                  | _        d S r   )rp   rq   r   r   r   r   r   r   r   r   r    r   r   r   r   s     r.   rq   zOutputBottleneck.__init__m  sm    Yv68JKK
 !:;F<NTZTijjjz&"<==r0   r   r   rz   c                     |                      |          }|                     |          }|                     ||z             }|S ro   )r   r   r    r   s       r.   r|   zOutputBottleneck.forwards  s@    

=11]33}'FGGr0   r}   r   s   @r.   r
  r
  l  si        > > > > >U\ EL UZUa        r0   r
  c                   ^     e Zd Z fdZdej        dej        dej        dej        fdZ xZS )MobileBertOutputc                 f   t                                                       |j        | _        t          j        |j        |j                  | _        t          |j	                 |j                  | _
        | j        s t          j        |j                  | _        d S t          |          | _        d S ro   )rp   rq   r   r   r   r  r   r   r   r   r    r   r   r   r
  
bottleneckr   s     r.   rq   zMobileBertOutput.__init__{  s    $3Yv79PQQ
 !:;F<STT" 	7:f&@AADLLL.v66DOOOr0   intermediate_statesresidual_tensor_1residual_tensor_2rz   c                     |                      |          }| j        s.|                     |          }|                     ||z             }n.|                     ||z             }|                     ||          }|S ro   )r   r   r   r    r  )ru   r  r  r  layer_outputs        r.   r|   zMobileBertOutput.forward  s}     zz"566" 	L<<55L>>,9J*JKKLL>>,9J*JKKL??<9JKKLr0   r}   r   s   @r.   r  r  z  su        7 7 7 7 7
#(<
DIL
ejeq
	
 
 
 
 
 
 
 
r0   r  c                   B     e Zd Z fdZdej        dej        fdZ xZS )BottleneckLayerc                     t                                                       t          j        |j        |j                  | _        t          |j                 |j        |j	                  | _
        d S r   )rp   rq   r   r   r   intra_bottleneck_sizer   r   r   r   r    r   s     r.   rq   zBottleneckLayer.__init__  sY    Yv163OPP
 !:;F<X^d^stttr0   r   rz   c                 Z    |                      |          }|                     |          }|S ro   r   r    )ru   r   r   s      r.   r|   zBottleneckLayer.forward  s*    jj//nn[11r0   r}   r   s   @r.   r  r    sc        u u u u u
U\ el        r0   r  c                   N     e Zd Z fdZdej        deej                 fdZ xZS )
Bottleneckc                     t                                                       |j        | _        |j        | _        t	          |          | _        | j        rt	          |          | _        d S d S ro   )rp   rq   key_query_shared_bottleneckr   r  input	attentionr   s     r.   rq   zBottleneck.__init__  sf    +1+M((.(G%$V,,
+ 	5,V44DNNN	5 	5r0   r   rz   c                     |                      |          }| j        r|fdz  S | j        r|                     |          }||||fS ||||fS )N   )r   r   r  r!  )ru   r   bottlenecked_hidden_statesshared_attention_inputs       r.   r|   zBottleneck.forward  so    " &*ZZ%>%>"( 	].0144- 	]%)^^M%B%B"*,BMSmnn!=-A[\\r0   	r~   r   r   rq   rX   r   r   r|   r   r   s   @r.   r  r    sm        5 5 5 5 5]U\ ]eEL6I ] ] ] ] ] ] ] ]r0   r  c                   P     e Zd Z fdZdej        dej        dej        fdZ xZS )	FFNOutputc                     t                                                       t          j        |j        |j                  | _        t          |j                 |j        |j	                  | _
        d S r   )rp   rq   r   r   r  r   r   r   r   r   r    r   s     r.   rq   zFFNOutput.__init__  sY    Yv79PQQ
 !:;F<SY_Ynooor0   r   r   rz   c                 `    |                      |          }|                     ||z             }|S ro   r  r   s       r.   r|   zFFNOutput.forward  s/    

=11}'FGGr0   r}   r   s   @r.   r(  r(    sn        p p p p p
U\ EL UZUa        r0   r(  c                   B     e Zd Z fdZdej        dej        fdZ xZS )FFNLayerc                     t                                                       t          |          | _        t	          |          | _        d S ro   )rp   rq   r   intermediater(  r   r   s     r.   rq   zFFNLayer.__init__  s<    26::''r0   r   rz   c                 \    |                      |          }|                     ||          }|S ro   )r.  r   )ru   r   intermediate_outputr   s       r.   r|   zFFNLayer.forward  s0    "//>>$7GGr0   r}   r   s   @r.   r,  r,    s^        ( ( ( ( (
U\ el        r0   r,  c                        e Zd Z fdZ	 	 	 d	dej        deej                 deej                 dee         de	ej                 f
dZ
 xZS )
MobileBertLayerc                    t                                                       j        | _        j        | _        t	                    | _        t                    | _        t                    | _	        | j        rt                    | _        j        dk    r<t          j        fdt          j        dz
            D                       | _        d S d S )Nr   c                 .    g | ]}t                    S r+   )r,  r,   r   r\   s     r.   
<listcomp>z,MobileBertLayer.__init__.<locals>.<listcomp>  s!    %k%k%k1hv&6&6%k%k%kr0   )rp   rq   r   num_feedforward_networksr   r!  r   r.  r  r   r  r  r   
ModuleListranger   r   s    `r.   rq   zMobileBertLayer.__init__  s    $3(.(G%,V4426::&v.. 	1(00DO*Q..}%k%k%k%kfFehiFi@j@j%k%k%kllDHHH /.r0   Nr   r   r   r   rz   c           	         | j         r|                     |          \  }}}}n|gdz  \  }}}}|                     |||||||          }	|	d         }
|
f}|	dd          }| j        dk    r+t	          | j                  D ]\  }} ||
          }
||
fz  }|                     |
          }|                     ||
|          }|f|z   t          j	        d          |||||
|fz   |z   }|S )Nr#  )r   r   r   i  )
r   r  r!  r7  	enumerater   r.  r   rX   tensor)ru   r   r   r   r   r   r   r   r   self_attention_outputsr   sr   i
ffn_moduler0  r  s                    r.   r|   zMobileBertLayer.forward  sZ     	VBF//R_B`B`?L*lKKCP/TUBU?L*lK!%/ "0 "
 "
 2!4(,(A--!*48!4!4 ) ):#-:.>#?#? &(("//0@AA{{#68H-XXO T"" #
  	 r0   r   r   r   s   @r.   r2  r2    s        m m m m m  7;15,0. .|. !!23. E-.	.
 $D>. 
u|	. . . . . . . .r0   r2  c                        e Zd Z fdZ	 	 	 	 	 ddej        deej                 deej                 dee         d	ee         d
ee         de	e
ef         fdZ xZS )MobileBertEncoderc                     t                                                       t          j        fdt	          j                  D                       | _        d S )Nc                 .    g | ]}t                    S r+   )r2  r5  s     r.   r6  z.MobileBertEncoder.__init__.<locals>.<listcomp>  s!    #e#e#eOF$;$;#e#e#er0   )rp   rq   r   r8  r9  num_hidden_layerslayerr   s    `r.   rq   zMobileBertEncoder.__init__  sO    ]#e#e#e#eU6KcEdEd#e#e#eff


r0   NFTr   r   r   r   output_hidden_statesreturn_dictrz   c                 "   |rdnd }|rdnd }t          | j                  D ]7\  }	}
|r||fz   } |
||||	         |          }|d         }|r||d         fz   }8|r||fz   }|st          d |||fD                       S t          |||          S )Nr+   r   r   c              3      K   | ]}||V  	d S ro   r+   )r,   vs     r.   r/   z,MobileBertEncoder.forward.<locals>.<genexpr>=  s(      hhqZ[ZgZgZgZgZghhr0   )last_hidden_stater   
attentions)r;  rF  r   r   )ru   r   r   r   r   rG  rH  all_hidden_statesall_attentionsr?  layer_moduler   s               r.   r|   zMobileBertEncoder.forward  s    #7@BBD0:d(44 	F 	FOA|# I$58H$H!(L!!	 M *!,M  F!/=3C2E!E   	E 1]4D D 	ihh]4E~$Vhhhhhh+;LYg
 
 
 	
r0   )NNFFT)r~   r   r   rq   rX   r   r   r   r   r   r   r   r|   r   r   s   @r.   rB  rB    s        g g g g g 7;15,1/4&*"
 "
|"
 !!23"
 E-.	"

 $D>"
 'tn"
 d^"
 
uo%	&"
 "
 "
 "
 "
 "
 "
 "
r0   rB  c                   B     e Zd Z fdZdej        dej        fdZ xZS )MobileBertPoolerc                     t                                                       |j        | _        | j        r&t	          j        |j        |j                  | _        d S d S ro   )rp   rq   classifier_activationdo_activater   r   r   r   r   s     r.   rq   zMobileBertPooler.__init__D  sY    !7 	K6#5v7IJJDJJJ	K 	Kr0   r   rz   c                     |d d df         }| j         s|S |                     |          }t          j        |          }|S )Nr   )rU  r   rX   tanh)ru   r   first_token_tensorpooled_outputs       r.   r|   zMobileBertPooler.forwardJ  sO     +111a40 	!%% JJ'9::M!J}55M  r0   r}   r   s   @r.   rR  rR  C  sc        K K K K K	!U\ 	!el 	! 	! 	! 	! 	! 	! 	! 	!r0   rR  c                   B     e Zd Z fdZdej        dej        fdZ xZS )!MobileBertPredictionHeadTransformc                 X   t                                                       t          j        |j        |j                  | _        t          |j        t                    rt          |j                 | _
        n|j        | _
        t          d         |j        |j                  | _        d S )Nr   r   )rp   rq   r   r   r   r   r  r  r  r   transform_act_fnr   r   r    r   s     r.   rq   z*MobileBertPredictionHeadTransform.__init__W  s    Yv163EFF
f'-- 	6$*6+<$=D!!$*$5D! .v/AvG\]]]r0   r   rz   c                     |                      |          }|                     |          }|                     |          }|S ro   )r   r]  r    r  s     r.   r|   z)MobileBertPredictionHeadTransform.forward`  s=    

=11--m<<}55r0   r}   r   s   @r.   r[  r[  V  sc        ^ ^ ^ ^ ^U\ el        r0   r[  c                   J     e Zd Z fdZddZdej        dej        fdZ xZS )MobileBertLMPredictionHeadc                    t                                                       t          |          | _        t	          j        |j        |j        |j        z
  d          | _	        t	          j        |j        |j        d          | _
        t	          j        t          j        |j                            | _        | j        | j
        _        d S )NF)r6   )rp   rq   r[  	transformr   r   r   r   r   r   decoderrr   rX   rs   r6   r   s     r.   rq   z#MobileBertLMPredictionHead.__init__h  s    :6BB Yv0&2DvG\2\chiii
y!68IPUVVVLV->!?!?@@	 Ir0   rz   Nc                 (    | j         | j        _         d S ro   )r6   rc  ru   s    r.   _tie_weightsz'MobileBertLMPredictionHead._tie_weightss  s     Ir0   r   c                     |                      |          }|                    t          j        | j        j                                        | j        j        gd                    }|| j        j        z  }|S )Nr   r   )	rb  r   rX   r   rc  r3   tr   r6   r  s     r.   r|   z"MobileBertLMPredictionHead.forwardv  sh    }55%,,UY8K8M8M8O8OQUQ[Qb7cij-k-k-kll**r0   )rz   N)	r~   r   r   rq   rf  rX   r   r|   r   r   s   @r.   r`  r`  g  sr        	& 	& 	& 	& 	&& & & &U\ el        r0   r`  c                   B     e Zd Z fdZdej        dej        fdZ xZS )MobileBertOnlyMLMHeadc                 p    t                                                       t          |          | _        d S ro   )rp   rq   r`  predictionsr   s     r.   rq   zMobileBertOnlyMLMHead.__init__~  s/    5f==r0   sequence_outputrz   c                 0    |                      |          }|S ro   )rl  )ru   rm  prediction_scoress      r.   r|   zMobileBertOnlyMLMHead.forward  s     ,,_==  r0   r}   r   s   @r.   rj  rj  }  s^        > > > > >!u| ! ! ! ! ! ! ! ! !r0   rj  c                   \     e Zd Z fdZdej        dej        deej                 fdZ xZS )MobileBertPreTrainingHeadsc                     t                                                       t          |          | _        t	          j        |j        d          | _        d S Nr:   )rp   rq   r`  rl  r   r   r   seq_relationshipr   s     r.   rq   z#MobileBertPreTrainingHeads.__init__  sF    5f== "	&*<a @ @r0   rm  rY  rz   c                 ^    |                      |          }|                     |          }||fS ro   )rl  rt  )ru   rm  rY  ro  seq_relationship_scores        r.   r|   z"MobileBertPreTrainingHeads.forward  s6     ,,_==!%!6!6}!E!E "888r0   r&  r   s   @r.   rq  rq    st        A A A A A
9u| 9EL 9UZ[`[gUh 9 9 9 9 9 9 9 9r0   rq  c                   (    e Zd ZU eed<   eZdZd ZdS )MobileBertPreTrainedModelr\   r#   c                    t          |t          j                  rT|j        j                            d| j        j                   |j         |j        j        	                                 dS dS t          |t          j
                  r_|j        j                            d| j        j                   |j        +|j        j        |j                 	                                 dS dS t          |t          j        t          f          r?|j        j        	                                 |j        j                            d           dS t          |t                    r |j        j        	                                 dS dS )zInitialize the weightsr   )meanstdNg      ?)r  r   r   r3   rZ   normal_r\   initializer_ranger6   zero_r   r   r    rm   fill_r`  )ru   modules     r.   _init_weightsz'MobileBertPreTrainedModel._init_weights  s^   fbi(( 	% M&&CT[5R&SSS{& &&((((( '&-- 	%M&&CT[5R&SSS!-"6#56<<>>>>> .-v 677 	%K""$$$M$$S))))) :;; 	%K""$$$$$	% 	%r0   N)	r~   r   r   r   __annotations__rk   load_tf_weightsbase_model_prefixr  r+   r0   r.   rx  rx    s<         3O$% % % % %r0   rx  z6
    Output type of [`MobileBertForPreTraining`].
    )custom_introc                       e Zd ZU dZdZeej                 ed<   dZ	eej                 ed<   dZ
eej                 ed<   dZeeej                          ed<   dZeeej                          ed<   dS )MobileBertForPreTrainingOutputa  
    loss (*optional*, returned when `labels` is provided, `torch.FloatTensor` of shape `(1,)`):
        Total loss as the sum of the masked language modeling loss and the next sequence prediction
        (classification) loss.
    prediction_logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
        Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
    seq_relationship_logits (`torch.FloatTensor` of shape `(batch_size, 2)`):
        Prediction scores of the next sequence prediction (classification) head (scores of True/False continuation
        before SoftMax).
    Nlossprediction_logitsseq_relationship_logitsr   rM  )r~   r   r   r   r  r   rX   r   r  r  r  r   r   rM  r+   r0   r.   r  r    s         	 	 )-D(5$
%,,,59x 12999;?Xe&78???8<M8E%"345<<<59Ju01299999r0   r  c                   B    e Zd ZdZd fd	Zd Zd Zd Ze	 	 	 	 	 	 	 	 	 dde	e
j                 d	e	e
j                 d
e	e
j                 de	e
j                 de	e
j                 de	e
j                 de	e         de	e         de	e         deeef         fd            Z xZS )MobileBertModelz2
    https://huggingface.co/papers/2004.02984
    Tc                     t                                          |           || _        t          |          | _        t          |          | _        |rt          |          nd| _        | 	                                 dS )zv
        add_pooling_layer (bool, *optional*, defaults to `True`):
            Whether to add a pooling layer
        N)
rp   rq   r\   r   r   rB  encoderrR  pooler	post_init)ru   r\   add_pooling_layerrx   s      r.   rq   zMobileBertModel.__init__  st    
 	   .v66(002CM&v... 	r0   c                     | j         j        S ro   r   r   re  s    r.   get_input_embeddingsz$MobileBertModel.get_input_embeddings  s    ..r0   c                     || j         _        d S ro   r  )ru   r   s     r.   set_input_embeddingsz$MobileBertModel.set_input_embeddings  s    */'''r0   c                     |                                 D ]/\  }}| j        j        |         j                            |           0dS )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  rF  r!  r   )ru   heads_to_prunerF  r   s       r.   _prune_headszMobileBertModel._prune_heads  sU    
 +0022 	C 	CLE5Lu%/;;EBBBB	C 	Cr0   Nr   r   r   r   r   r   rG  r   rH  rz   c
                 P   ||n| j         j        }||n| j         j        }|	|	n| j         j        }	||t	          d          |+|                     ||           |                                }
n.||                                d d         }
nt	          d          ||j        n|j        }|t          j	        |
|          }|!t          j
        |
t          j        |          }|                     ||
          }|                     || j         j                  }|                     ||||          }|                     ||||||	          }|d         }| j        |                     |          nd }|	s||f|d	d          z   S t%          |||j        |j        
          S )NzDYou cannot specify both input_ids and inputs_embeds at the same timer   z5You have to specify either input_ids or inputs_embeds)r   r   )r   r   r   r   )r   r   r   rG  rH  r   r   )rL  pooler_outputr   rM  )r\   r   rG  use_return_dict
ValueError%warn_if_padding_and_no_attention_maskr   r   rX   rt   rs   r   get_extended_attention_maskget_head_maskrE  r   r  r  r   r   rM  )ru   r   r   r   r   r   r   rG  r   rH  r   r   extended_attention_maskembedding_outputencoder_outputsrm  rY  s                    r.   r|   zMobileBertModel.forward  s	    2C1N--TXT_Tq$8$D  $+Jj 	 &1%<kk$+B] ]%>cddd"66y.QQQ#..**KK&',,..ss3KKTUUU%.%:!!@T!"ZFCCCN!"[EJvVVVN 150P0PQ_al0m0m &&y$+2OPP	??l>iv + 
 
 ,,2/!5# ' 
 
 *!,8<8OO444UY 	J#]3oabb6III)-')7&1	
 
 
 	
r0   )T)	NNNNNNNNN)r~   r   r   r   rq   r  r  r  r   r   rX   r   r   r   r   r   r   r|   r   r   s   @r.   r  r    su             / / /0 0 0C C C  156:59371559/3,0&*D
 D
E,-D
 !!23D
 !!12	D

 u/0D
 E-.D
   12D
 'tnD
 $D>D
 d^D
 
u00	1D
 D
 D
 ^D
 D
 D
 D
 D
r0   r  z
    MobileBert Model with two heads on top as done during the pretraining: a `masked language modeling` head and a
    `next sentence prediction (classification)` head.
    c                       e Zd ZddgZ fdZd Zd Zddee         de	j
        f fd	Ze	 	 	 	 	 	 	 	 	 	 	 dd
eej                 deej                 deej                 deej                 deej                 deej                 deej                 deej                 deej                 deej                 deej                 deeef         fd            Z xZS )MobileBertForPreTrainingcls.predictions.decoder.weightcls.predictions.decoder.biasc                     t                                          |           t          |          | _        t	          |          | _        |                                  d S ro   )rp   rq   r  r#   rq  clsr  r   s     r.   rq   z!MobileBertForPreTraining.__init__9  sQ       )&11-f55 	r0   c                 $    | j         j        j        S ro   r  rl  rc  re  s    r.   get_output_embeddingsz.MobileBertForPreTraining.get_output_embeddingsA      x#++r0   c                 T    || j         j        _        |j        | j         j        _        d S ro   r  rl  rc  r6   ru   new_embeddingss     r.   set_output_embeddingsz.MobileBertForPreTraining.set_output_embeddingsD  %    '5$$2$7!!!r0   Nnew_num_tokensrz   c                     |                      | j        j        j        |d          | j        j        _        t	                                          |          S NT)r  
transposed)r  _get_resized_lm_headr  rl  r   rp   resize_token_embeddingsru   r  rx   s     r.   r  z0MobileBertForPreTraining.resize_token_embeddingsH  sR    %)%>%>H &~RV &? &
 &
" ww..n.MMMr0   r   r   r   r   r   r   labelsnext_sentence_labelr   rG  rH  c                 .   ||n| j         j        }|                     |||||||	|
|	  	        }|dd         \  }}|                     ||          \  }}d}||t	                      } ||                    d| j         j                  |                    d                    } ||                    dd          |                    d                    }||z   }|s||f|dd         z   }||f|z   n|S t          ||||j        |j	                  S )a  
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ...,
            config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the
            loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`
        next_sentence_label (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair
            (see `input_ids` docstring) Indices should be in `[0, 1]`:

            - 0 indicates sequence B is a continuation of sequence A,
            - 1 indicates sequence B is a random sequence.

        Examples:

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

        >>> tokenizer = AutoTokenizer.from_pretrained("google/mobilebert-uncased")
        >>> model = MobileBertForPreTraining.from_pretrained("google/mobilebert-uncased")

        >>> input_ids = torch.tensor(tokenizer.encode("Hello, my dog is cute", add_special_tokens=True)).unsqueeze(0)
        >>> # Batch size 1
        >>> outputs = model(input_ids)

        >>> prediction_logits = outputs.prediction_logits
        >>> seq_relationship_logits = outputs.seq_relationship_logits
        ```Nr   r   r   r   r   r   rG  rH  r:   r   )r  r  r  r   rM  )
r\   r  r#   r  r   r   r   r  r   rM  )ru   r   r   r   r   r   r   r  r  r   rG  rH  r   rm  rY  ro  rv  
total_lossloss_fctmasked_lm_lossnext_sentence_lossr   s                         r.   r|   z MobileBertForPreTraining.forwardP  sn   V &1%<kk$+B]//))%'/!5# " 

 

 *1!&48HH_m4\4\11
"5"A'))H%X&7&<&<RAW&X&XZ`ZeZefhZiZijjN!)*@*E*Eb!*L*LNaNfNfgiNjNj!k!k'*<<J 	R')?@7122;NF/9/EZMF**6Q-/$:!/)
 
 
 	
r0   ro   NNNNNNNNNNN)r~   r   r   _tied_weights_keysrq   r  r  r   rS   r   r   r  r   rX   r   r   r   r   r  r|   r   r   s   @r.   r  r  0  s        ;<Z[    , , ,8 8 8N Nhsm Nr| N N N N N N  156:59371559-1:>9=<@37K
 K
E,-K
 !!23K
 !!12	K

 u/0K
 E-.K
   12K
 )*K
 &e&67K
 $E$56K
 'u'89K
 e/0K
 
u44	5K
 K
 K
 ^K
 K
 K
 K
 K
r0   r  c                       e Zd ZddgZ fdZd Zd Zddee         de	j
        f fd	Ze	 	 	 	 	 	 	 	 	 	 dd
eej                 deej                 deej                 deej                 deej                 deej                 deej                 dee         dee         dee         deeef         fd            Z xZS )MobileBertForMaskedLMr  r  c                     t                                          |           t          |d          | _        t	          |          | _        || _        |                                  d S NF)r  )rp   rq   r  r#   rj  r  r\   r  r   s     r.   rq   zMobileBertForMaskedLM.__init__  s]       )&EJJJ(00 	r0   c                 $    | j         j        j        S ro   r  re  s    r.   r  z+MobileBertForMaskedLM.get_output_embeddings  r  r0   c                 T    || j         j        _        |j        | j         j        _        d S ro   r  r  s     r.   r  z+MobileBertForMaskedLM.set_output_embeddings  r  r0   Nr  rz   c                     |                      | j        j        j        |d          | j        j        _        t	                                          |          S r  r  r  s     r.   r  z-MobileBertForMaskedLM.resize_token_embeddings  sR    %)%>%>H &~RV &? &
 &
" ww..n.MMMr0   r   r   r   r   r   r   r  r   rG  rH  c                    |
|
n| j         j        }
|                     ||||||||	|
	  	        }|d         }|                     |          }d}|Kt	                      } ||                    d| j         j                  |                    d                    }|
s|f|dd         z   }||f|z   n|S t          |||j        |j	                  S )a  
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ...,
            config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the
            loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`
        Nr  r   r   r:   r  logitsr   rM  )
r\   r  r#   r  r   r   r   r   r   rM  )ru   r   r   r   r   r   r   r  r   rG  rH  r   rm  ro  r  r  r   s                    r.   r|   zMobileBertForMaskedLM.forward  s   ( &1%<kk$+B]//))%'/!5# " 

 

 "!* HH_55'))H%X&7&<&<RAW&X&XZ`ZeZefhZiZijjN 	Z')GABBK7F3A3M^%..SYY$!/)	
 
 
 	
r0   ro   
NNNNNNNNNN)r~   r   r   r  rq   r  r  r   rS   r   r   r  r   rX   r   r   r   r   r   r   r|   r   r   s   @r.   r  r    s       :<Z[    , , ,8 8 8N Nhsm Nr| N N N N N N  156:59371559-1,0/3&*2
 2
E,-2
 !!232
 !!12	2

 u/02
 E-.2
   122
 )*2
 $D>2
 'tn2
 d^2
 
un$	%2
 2
 2
 ^2
 2
 2
 2
 2
r0   r  c                   B     e Zd Z fdZdej        dej        fdZ xZS )MobileBertOnlyNSPHeadc                     t                                                       t          j        |j        d          | _        d S rs  )rp   rq   r   r   r   rt  r   s     r.   rq   zMobileBertOnlyNSPHead.__init__  s6     "	&*<a @ @r0   rY  rz   c                 0    |                      |          }|S ro   )rt  )ru   rY  rv  s      r.   r|   zMobileBertOnlyNSPHead.forward  s    !%!6!6}!E!E%%r0   r}   r   s   @r.   r  r    sc        A A A A A&U\ &el & & & & & & & &r0   r  zZ
    MobileBert Model with a `next sentence prediction (classification)` head on top.
    c                   F    e Zd Z fdZe	 	 	 	 	 	 	 	 	 	 ddeej                 deej                 deej                 deej                 deej                 deej                 d	eej                 d
ee	         dee	         dee	         de
eef         fd            Z xZS )#MobileBertForNextSentencePredictionc                     t                                          |           t          |          | _        t	          |          | _        |                                  d S ro   )rp   rq   r  r#   r  r  r  r   s     r.   rq   z,MobileBertForNextSentencePrediction.__init__   sQ       )&11(00 	r0   Nr   r   r   r   r   r   r  r   rG  rH  rz   c                    d|v r/t          j        dt                     |                    d          }|
|
n| j        j        }
|                     ||||||||	|
	  	        }|d         }|                     |          }d}|At                      } ||	                    dd          |	                    d                    }|
s|f|dd         z   }||f|z   n|S t          |||j        |j                  S )	a  
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair
            (see `input_ids` docstring) Indices should be in `[0, 1]`.

            - 0 indicates sequence B is a continuation of sequence A,
            - 1 indicates sequence B is a random sequence.

        Examples:

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

        >>> tokenizer = AutoTokenizer.from_pretrained("google/mobilebert-uncased")
        >>> model = MobileBertForNextSentencePrediction.from_pretrained("google/mobilebert-uncased")

        >>> prompt = "In Italy, pizza served in formal settings, such as at a restaurant, is presented unsliced."
        >>> next_sentence = "The sky is blue due to the shorter wavelength of blue light."
        >>> encoding = tokenizer(prompt, next_sentence, return_tensors="pt")

        >>> outputs = model(**encoding, labels=torch.LongTensor([1]))
        >>> loss = outputs.loss
        >>> logits = outputs.logits
        ```r  zoThe `next_sentence_label` argument is deprecated and will be removed in a future version, use `labels` instead.Nr  r   r   r:   r  )warningswarnFutureWarningpopr\   r  r#   r  r   r   r   r   rM  )ru   r   r   r   r   r   r   r  r   rG  rH  kwargsr   rY  rv  r  r  r   s                     r.   r|   z+MobileBertForNextSentencePrediction.forward	  sI   R !F**M%  
 ZZ 566F%0%<kk$+B]//))%'/!5# " 

 

  
!%-!8!8!'))H!)*@*E*Eb!*L*LfkkZ\oo!^!^ 	b,.<F7I7U')F22[aa*#)!/)	
 
 
 	
r0   r  )r~   r   r   rq   r   r   rX   r   r   r   r   r   r   r|   r   r   s   @r.   r  r    sL             156:59371559-1,0/3&*O
 O
E,-O
 !!23O
 !!12	O

 u/0O
 E-.O
   12O
 )*O
 $D>O
 'tnO
 d^O
 
u11	2O
 O
 O
 ^O
 O
 O
 O
 O
r0   r  z
    MobileBert Model transformer with a sequence classification/regression head on top (a linear layer on top of the
    pooled output) e.g. for GLUE tasks.
    c                   \    e Zd Z fdZe	 	 	 	 	 	 	 	 	 	 ddeej                 deej                 deej                 deej                 deej                 deej                 d	eej                 d
ee         dee         dee         de	e
ej                 ef         fd            Z xZS )#MobileBertForSequenceClassificationc                 d   t                                          |           |j        | _        || _        t	          |          | _        |j        |j        n|j        }t          j	        |          | _
        t          j        |j        |j                  | _        |                                  d S ro   )rp   rq   
num_labelsr\   r  r#   classifier_dropoutr   r   r   r   r   r   r9   r  ru   r\   r  rx   s      r.   rq   z,MobileBertForSequenceClassification.__init__d  s        +)&11)/)B)NF%%TZTn 	 z"455)F$68IJJ 	r0   Nr   r   r   r   r   r   r  r   rG  rH  rz   c                    |
|
n| j         j        }
|                     ||||||||	|
	  	        }|d         }|                     |          }|                     |          }d}|Z| j         j        f| j        dk    rd| j         _        nN| j        dk    r7|j        t          j	        k    s|j        t          j
        k    rd| j         _        nd| j         _        | j         j        dk    rWt                      }| j        dk    r1 ||                                |                                          }n |||          }n| j         j        dk    rGt                      } ||                    d| j                  |                    d                    }n*| j         j        dk    rt                      } |||          }|
s|f|dd         z   }||f|z   n|S t!          |||j        |j        	          S )
a  
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        Nr  r   
regressionsingle_label_classificationmulti_label_classificationr   r:   r  )r\   r  r#   r   r9   problem_typer  r   rX   r   rS   r	   squeezer   r   r   r   r   rM  )ru   r   r   r   r   r   r   r  r   rG  rH  r   rY  r  r  r  r   s                    r.   r|   z+MobileBertForSequenceClassification.forwards  s   ( &1%<kk$+B]//))%'/!5# " 

 

  
]33//{'/?a''/;DK,,_q((flej.H.HFL\a\eLeLe/LDK,,/KDK,{'<77"99?a''#8FNN$4$4fnn6F6FGGDD#8FF33DD)-JJJ+--xB @ @&++b//RR)-III,..x// 	FY,F)-)9TGf$$vE'!/)	
 
 
 	
r0   r  )r~   r   r   rq   r   r   rX   r   r   r   r   r   r|   r   r   s   @r.   r  r  \  sL             -11515/3,004)-,0/3&*E
 E
EL)E
 !.E
 !.	E

 u|,E
 EL)E
  -E
 &E
 $D>E
 'tnE
 d^E
 
uU\"$<<	=E
 E
 E
 ^E
 E
 E
 E
 E
r0   r  c                   x    e Zd Z fdZe	 	 	 	 	 	 	 	 	 	 	 ddeej                 deej                 deej                 deej                 deej                 deej                 d	eej                 d
eej                 dee         dee         dee         de	e
ej                 ef         fd            Z xZS )MobileBertForQuestionAnsweringc                     t                                          |           |j        | _        t          |d          | _        t          j        |j        |j                  | _        | 	                                 d S r  )
rp   rq   r  r  r#   r   r   r   
qa_outputsr  r   s     r.   rq   z'MobileBertForQuestionAnswering.__init__  sj        +)&EJJJ)F$68IJJ 	r0   Nr   r   r   r   r   r   start_positionsend_positionsr   rG  rH  rz   c                    ||n| j         j        }|                     |||||||	|
|	  	        }|d         }|                     |          }|                    dd          \  }}|                    d                                          }|                    d                                          }d }||t          |                                          dk    r|                    d          }t          |                                          dk    r|                    d          }|                    d          }|	                    d|          }|	                    d|          }t          |          } |||          } |||          }||z   dz  }|s||f|dd          z   }||f|z   n|S t          ||||j        |j                  S )	Nr  r   r   r   r   )ignore_indexr:   )r  start_logits
end_logitsr   rM  )r\   r  r#   r  rL   r  r   rR   r   clampr   r   r   rM  )ru   r   r   r   r   r   r   r  r  r   rG  rH  r   rm  r  r  r  r  ignored_indexr  
start_lossend_lossr   s                          r.   r|   z&MobileBertForQuestionAnswering.forward  s    &1%<kk$+B]//))%'/!5# " 

 

 "!*11#)<<r<#:#: j#++B//::<<''++6688

&=+D?''))**Q.."1"9"9""="==%%''((1,, - 5 5b 9 9(--a00M-33A}EEO)//=AAM']CCCH!,@@Jx
M::H$x/14J 	R"J/'!""+=F/9/EZMF**6Q+%!!/)
 
 
 	
r0   r  )r~   r   r   rq   r   r   rX   r   r   r   r   r   r|   r   r   s   @r.   r  r    sL             -11515/3,0042604,0/3&*>
 >
EL)>
 !.>
 !.	>

 u|,>
 EL)>
  ->
 "%,/>
  ->
 $D>>
 'tn>
 d^>
 
uU\"$@@	A>
 >
 >
 ^>
 >
 >
 >
 >
r0   r  c                   \    e Zd Z fdZe	 	 	 	 	 	 	 	 	 	 ddeej                 deej                 deej                 deej                 deej                 deej                 d	eej                 d
ee         dee         dee         de	e
ej                 ef         fd            Z xZS )MobileBertForMultipleChoicec                 4   t                                          |           t          |          | _        |j        |j        n|j        }t          j        |          | _        t          j	        |j
        d          | _        |                                  d S )Nr   )rp   rq   r  r#   r  r   r   r   r   r   r   r9   r  r  s      r.   rq   z$MobileBertForMultipleChoice.__init__  s       )&11)/)B)NF%%TZTn 	 z"455)F$6:: 	r0   Nr   r   r   r   r   r   r  r   rG  rH  rz   c                    |
|
n| j         j        }
||j        d         n|j        d         }|)|                    d|                    d                    nd}|)|                    d|                    d                    nd}|)|                    d|                    d                    nd}|)|                    d|                    d                    nd}|=|                    d|                    d          |                    d                    nd}|                     ||||||||	|
	  	        }|d         }|                     |          }|                     |          }|                    d|          }d}|t                      } |||          }|
s|f|dd         z   }||f|z   n|S t          |||j
        |j                  S )a[  
        input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`):
            Indices of input sequence tokens in the vocabulary.

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

            [What are input IDs?](../glossary#input-ids)
        token_type_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`, *optional*):
            Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,
            1]`:

            - 0 corresponds to a *sentence A* token,
            - 1 corresponds to a *sentence B* token.

            [What are token type IDs?](../glossary#token-type-ids)
        position_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.max_position_embeddings - 1]`.

            [What are position IDs?](../glossary#position-ids)
        inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_choices, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the multiple choice classification loss. Indices should be in `[0, ...,
            num_choices-1]` where `num_choices` is the size of the second dimension of the input tensors. (See
            `input_ids` above)
        Nr   r   r   r  r:   r  )r\   r  rU   r   r   r#   r   r9   r   r   r   rM  )ru   r   r   r   r   r   r   r  r   rG  rH  num_choicesr   rY  r  reshaped_logitsr  r  r   s                      r.   r|   z#MobileBertForMultipleChoice.forward  s+   X &1%<kk$+B],5,Aioa((}GZ[\G]>G>SINN2y~~b'9'9:::Y]	M[Mg,,R1D1DR1H1HIIImqM[Mg,,R1D1DR1H1HIIImqGSG_|((\->->r-B-BCCCei ( r=#5#5b#9#9=;M;Mb;Q;QRRR 	 //))%'/!5# " 

 

  
]33// ++b+66'))H8OV44D 	F%''!""+5F)-)9TGf$$vE("!/)	
 
 
 	
r0   r  )r~   r   r   rq   r   r   rX   r   r   r   r   r   r|   r   r   s   @r.   r  r    sL             -11515/3,004)-,0/3&*X
 X
EL)X
 !.X
 !.	X

 u|,X
 EL)X
  -X
 &X
 $D>X
 'tnX
 d^X
 
uU\"$==	>X
 X
 X
 ^X
 X
 X
 X
 X
r0   r  c                   \    e Zd Z fdZe	 	 	 	 	 	 	 	 	 	 ddeej                 deej                 deej                 deej                 deej                 deej                 d	eej                 d
ee         dee         dee         de	e
ej                 ef         fd            Z xZS ) MobileBertForTokenClassificationc                 Z   t                                          |           |j        | _        t          |d          | _        |j        |j        n|j        }t          j        |          | _	        t          j
        |j        |j                  | _        |                                  d S r  )rp   rq   r  r  r#   r  r   r   r   r   r   r   r9   r  r  s      r.   rq   z)MobileBertForTokenClassification.__init__z  s        +)&EJJJ)/)B)NF%%TZTn 	 z"455)F$68IJJ 	r0   Nr   r   r   r   r   r   r  r   rG  rH  rz   c                    |
|
n| j         j        }
|                     ||||||||	|
	  	        }|d         }|                     |          }|                     |          }d}|Ft                      } ||                    d| j                  |                    d                    }|
s|f|dd         z   }||f|z   n|S t          |||j	        |j
                  S )z
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`.
        Nr  r   r   r:   r  )r\   r  r#   r   r9   r   r   r  r   r   rM  )ru   r   r   r   r   r   r   r  r   rG  rH  r   rm  r  r  r  r   s                    r.   r|   z(MobileBertForTokenClassification.forward  s   $ &1%<kk$+B]//))%'/!5# " 

 

 "!*,,7711'))H8FKKDO<<fkk"ooNND 	FY,F)-)9TGf$$vE$!/)	
 
 
 	
r0   r  )r~   r   r   rq   r   r   rX   r   r   r   r   r   r|   r   r   s   @r.   r   r   w  s8             -11515/3,004)-,0/3&*2
 2
EL)2
 !.2
 !.	2

 u|,2
 EL)2
  -2
 &2
 $D>2
 'tn2
 d^2
 
uU\"$99	:2
 2
 2
 ^2
 2
 2
 2
 2
r0   r   )r  r  r  r  r  r  r   r2  r  rx  rk   )Jr   rB   r  dataclassesr   typingr   r   rX   r   torch.nnr   r   r	   activationsr   modeling_outputsr   r   r   r   r   r   r   r   modeling_utilsr   pytorch_utilsr   r   utilsr   r   r   configuration_mobilebertr   
get_loggerr~   r@   rk   Modulerm   r    r   r   r   r   r   r   r
  r  r  r  r(  r,  r2  rB  rR  r[  r`  rj  rq  rx  r  r  r  r  r  r  r  r  r  r   __all__r+   r0   r.   <module>r     s  .  				  ! ! ! ! ! ! " " " " " " " "        A A A A A A A A A A ! ! ! ! ! !	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 . - - - - - Q Q Q Q Q Q Q Q 9 9 9 9 9 9 9 9 9 9 6 6 6 6 6 6 
	H	%	%K K K\6 6 6 6 6RY 6 6 6 &
9
9I I I I I29 I I IX; ; ; ; ;bi ; ; ;|    29   "/ / / / /") / / /d    RY       ry       ry   0	 	 	 	 	bi 	 	 	!] !] !] !] !] !] !] !]H	 	 	 	 		 	 	 		 	 	 	 	ry 	 	 	< < < < <bi < < <~'
 '
 '
 '
 '
	 '
 '
 '
T! ! ! ! !ry ! ! !&    	   "       ,! ! ! ! !BI ! ! !	9 	9 	9 	9 	9 	9 	9 	9 % % % % % % % %0   
: : : : :[ : :  :& g
 g
 g
 g
 g
/ g
 g
 g
T   f
 f
 f
 f
 f
8 f
 f
 f
R M
 M
 M
 M
 M
5 M
 M
 M
`& & & & &BI & & &   
Z
 Z
 Z
 Z
 Z
*C Z
 Z
 
Z
z   V
 V
 V
 V
 V
*C V
 V
 V
r J
 J
 J
 J
 J
%> J
 J
 J
Z g
 g
 g
 g
 g
"; g
 g
 g
T B
 B
 B
 B
 B
'@ B
 B
 B
J  r0   