
    rh
                     n   d Z ddl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mZ ddl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 ddlmZ  ej@                  e!      Z"e G d de             Z# G d dejH                        Z% G d dejH                        Z& G d dejH                        Z' G d dejH                        Z( G d dejH                        Z) G d dejH                        Z* G d dejH                        Z+ G d dejH                        Z,e G d  d!e#             Z- G d" d#e#      Z. G d$ d%ejH                        Z/ ed&'       G d( d)e#             Z0e G d* d+e#             Z1e G d, d-e#             Z2 G d. d/ejH                        Z3e G d0 d1e#             Z4d2 Z5g d3Z6y)4zPyTorch MPNet model.    N)OptionalUnion)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )ACT2FNgelu)BaseModelOutputBaseModelOutputWithPoolingMaskedLMOutputMultipleChoiceModelOutputQuestionAnsweringModelOutputSequenceClassifierOutputTokenClassifierOutput)PreTrainedModel) find_pruneable_heads_and_indicesprune_linear_layer)auto_docstringlogging   )MPNetConfigc                   "    e Zd ZU eed<   dZd Zy)MPNetPreTrainedModelconfigmpnetc                 l   t        |t        j                        rm|j                  j                  j                  d| j                  j                         |j                  %|j                  j                  j                          yyt        |t        j                        rz|j                  j                  j                  d| j                  j                         |j                  2|j                  j                  |j                     j                          yyt        |t        j                        rJ|j                  j                  j                          |j                  j                  j                  d       yt        |t              r%|j                  j                  j                          yy)zInitialize the weightsg        )meanstdNg      ?)
isinstancer   Linearweightdatanormal_r   initializer_rangebiaszero_	Embeddingpadding_idx	LayerNormfill_MPNetLMHead)selfmodules     {/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/transformers/models/mpnet/modeling_mpnet.py_init_weightsz"MPNetPreTrainedModel._init_weights1   s&   fbii( MM&&CT[[5R5R&S{{&  &&( '-MM&&CT[[5R5R&S!!-""6#5#56<<> .-KK""$MM$$S),KK""$ -    N)__name__
__module____qualname__r   __annotations__base_model_prefixr1    r2   r0   r   r   ,   s    %r2   r   c                   ,     e Zd Z fdZddZd Z xZS )MPNetEmbeddingsc                 D   t         |           d| _        t        j                  |j
                  |j                  | j                        | _        t        j                  |j                  |j                  | j                        | _	        t        j                  |j                  |j                        | _
        t        j                  |j                        | _        | j                  dt!        j"                  |j                        j%                  d      d       y )Nr   )r*   epsposition_ids)r   F)
persistent)super__init__r*   r   r)   
vocab_sizehidden_sizeword_embeddingsmax_position_embeddingsposition_embeddingsr+   layer_norm_epsDropouthidden_dropout_probdropoutregister_buffertorcharangeexpandr.   r   	__class__s     r0   rB   zMPNetEmbeddings.__init__E   s    !||F,=,=v?Q?Q_c_o_op#%<<**F,>,>DL\L\$
  f&8&8f>S>STzz&"<"<=ELL)G)GHOOPWXej 	 	
r2   c                 x   |*|t        || j                        }n| j                  |      }||j                         }n|j                         d d }|d   }|| j                  d d d |f   }|| j                  |      }| j                  |      }||z   }| j                  |      }| j                  |      }|S )Nr?   r   )	"create_position_ids_from_input_idsr*   &create_position_ids_from_inputs_embedssizer>   rE   rG   r+   rK   )	r.   	input_idsr>   inputs_embedskwargsinput_shape
seq_lengthrG   
embeddingss	            r0   forwardzMPNetEmbeddings.forwardS   s    $A)TM]M]^#JJ=Y #..*K',,.s3K ^
,,Q^<L  00;M"66|D"%88
^^J/
\\*-
r2   c                    |j                         dd }|d   }t        j                  | j                  dz   || j                  z   dz   t        j                  |j
                        }|j                  d      j                  |      S )z
        We are provided embeddings directly. We cannot infer which are padded so just generate sequential position ids.

        Args:
            inputs_embeds: torch.Tensor

        Returns: torch.Tensor
        Nr?   r   )dtypedevicer   )rU   rM   rN   r*   longr_   	unsqueezerO   )r.   rW   rY   sequence_lengthr>   s        r0   rT   z6MPNetEmbeddings.create_position_ids_from_inputs_embedsm   s     $((*3B/%a.||q /D4D4D"Dq"HPUPZPZcpcwcw
 %%a(//<<r2   )NNN)r3   r4   r5   rB   r\   rT   __classcell__rQ   s   @r0   r:   r:   D   s    
4=r2   r:   c                   .     e Zd Z fdZ	 	 	 	 ddZ xZS )MPNetSelfAttentionc                 *   t         |           |j                  |j                  z  dk7  r2t	        |d      s&t        d|j                   d|j                   d      |j                  | _        t        |j                  |j                  z        | _        | j                  | j                  z  | _        t        j                  |j                  | j                        | _        t        j                  |j                  | j                        | _        t        j                  |j                  | j                        | _        t        j                  |j                  |j                        | _        t        j                  |j                         | _        y )Nr   embedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads ())rA   rB   rD   num_attention_headshasattr
ValueErrorintattention_head_sizeall_head_sizer   r"   qkvorI   attention_probs_dropout_probrK   rP   s     r0   rB   zMPNetSelfAttention.__init__   s:    : ::a?PVXhHi#F$6$6#7 8 445Q8 
 $*#=#= #&v'9'9F<V<V'V#W !558P8PP6--t/A/AB6--t/A/AB6--t/A/AB6--v/A/ABzz&"E"EFr2   c                    |j                   \  }}}	| j                  |      j                  |d| j                  | j                        j                  dd      }
| j                  |      j                  |d| j                  | j                        j                  dd      }| j                  |      j                  |d| j                  | j                        j                  dd      }t        j                  |
|j                  dd            }|t        j                  | j                        z  }|||z  }|||z   }t        j                  j                  |d      }| j                  |      }|||z  }t        j                  ||      }|j!                  dddd      j#                         }|j%                         d d | j&                  fz   } |j                  | }| j)                  |      }|r||f}|S |f}|S )Nr?   r      dimr   r	   )shaperp   viewrj   rn   	transposerq   rr   rM   matmulmathsqrtr   
functionalsoftmaxrK   permute
contiguousrU   ro   rs   )r.   hidden_statesattention_mask	head_maskposition_biasoutput_attentionsrX   
batch_sizerZ   _rp   rq   rr   attention_scoresattention_probscnew_c_shapers   outputss                      r0   r\   zMPNetSelfAttention.forward   s    %2$7$7!
JFF=!T*b$":":D<T<TUYq!_ 	
 FF=!T*b$":":D<T<TUYq!_ 	
 FF=!T*b$":":D<T<TUYq!_ 	
 !<<1;;r2+>?+dii8P8P.QQ $-%/.@ --//0@b/I,,7 -	9OLL!,IIaAq!,,.ffhsmt'9'9&;;AFFK FF1I*;1o& CDr2   NNNFr3   r4   r5   rB   r\   rc   rd   s   @r0   rf   rf      s    G, 6r2   rf   c                   4     e Zd Z fdZd Z	 	 	 	 ddZ xZS )MPNetAttentionc                    t         |           t        |      | _        t	        j
                  |j                  |j                        | _        t	        j                  |j                        | _
        t               | _        y Nr<   )rA   rB   rf   attnr   r+   rD   rH   rI   rJ   rK   setpruned_headsrP   s     r0   rB   zMPNetAttention.__init__   sX    &v.	f&8&8f>S>STzz&"<"<=Er2   c                 >   t        |      dk(  ry 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
                  j                  |      | _        y )Nr   r   rx   )lenr   r   rj   rn   r   r   rp   rq   rr   rs   ro   union)r.   headsindexs      r0   prune_headszMPNetAttention.prune_heads   s   u:?749900$))2O2OQUQbQb
u )e<		(e<		(e<		(eC		(,		(E(EE
(R		%"&))"?"?$))B_B_"_		 --33E:r2   c                     | j                  |||||      }| j                  | j                  |d         |z         }|f|dd  z   }	|	S )N)r   r   r   )r   r+   rK   )
r.   r   r   r   r   r   rX   self_outputsattention_outputr   s
             r0   r\   zMPNetAttention.forward   sb     yy/ ! 
  >>$,,|A*G-*WX#%QR(88r2   r   )r3   r4   r5   rB   r   r\   rc   rd   s   @r0   r   r      s     ";& r2   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )MPNetIntermediatec                    t         |           t        j                  |j                  |j
                        | _        t        |j                  t              rt        |j                     | _        y |j                  | _        y N)rA   rB   r   r"   rD   intermediate_sizedenser!   
hidden_actstrr
   intermediate_act_fnrP   s     r0   rB   zMPNetIntermediate.__init__   s]    YYv1163K3KL
f''-'-f.?.?'@D$'-'8'8D$r2   r   returnc                 J    | j                  |      }| j                  |      }|S r   )r   r   )r.   r   s     r0   r\   zMPNetIntermediate.forward  s&    

=100?r2   r3   r4   r5   rB   rM   Tensorr\   rc   rd   s   @r0   r   r      s#    9U\\ ell r2   r   c                   n     e Zd Z fdZdej
                  dej
                  dej
                  fdZ xZS )MPNetOutputc                 (   t         |           t        j                  |j                  |j
                        | _        t        j                  |j
                  |j                        | _        t        j                  |j                        | _        y r   )rA   rB   r   r"   r   rD   r   r+   rH   rI   rJ   rK   rP   s     r0   rB   zMPNetOutput.__init__  s`    YYv779K9KL
f&8&8f>S>STzz&"<"<=r2   r   input_tensorr   c                 r    | j                  |      }| j                  |      }| j                  ||z         }|S r   )r   rK   r+   )r.   r   r   s      r0   r\   zMPNetOutput.forward  s7    

=1]3}|'CDr2   r   rd   s   @r0   r   r     s1    >U\\  RWR^R^ r2   r   c                   .     e Zd Z fdZ	 	 	 	 ddZ xZS )
MPNetLayerc                     t         |           t        |      | _        t	        |      | _        t        |      | _        y r   )rA   rB   r   	attentionr   intermediater   outputrP   s     r0   rB   zMPNetLayer.__init__  s3    '/-f5!&)r2   c                     | j                  |||||      }|d   }|dd  }	| j                  |      }
| j                  |
|      }|f|	z   }	|	S )N)r   r   r   r   )r   r   r   )r.   r   r   r   r   r   rX   self_attention_outputsr   r   intermediate_outputlayer_outputs               r0   r\   zMPNetLayer.forward   su     "&'/ "0 "
 2!4(,"//0@A{{#68HI/G+r2   r   r   rd   s   @r0   r   r     s    * r2   r   c                        e Zd Z fdZ	 	 	 	 	 ddej
                  deej
                     deej
                     dededefdZdd	Z	e
dd
       Z xZS )MPNetEncoderc                 B   t         |           || _        |j                  | _        t        j                  t        |j                        D cg c]  }t        |       c}      | _
        t        j                  |j                  | j                        | _        y c c}w r   )rA   rB   r   rj   n_headsr   
ModuleListrangenum_hidden_layersr   layerr)   relative_attention_num_bucketsrelative_attention_bias)r.   r   r   rQ   s      r0   rB   zMPNetEncoder.__init__:  ss    11]]fF^F^@_#`1Jv$6#`a
')||F4Y4Y[_[g[g'h$ $as   Br   r   r   r   output_hidden_statesreturn_dictc                 $   | j                  |      }|rdnd }	|rdnd }
t        | j                        D ]/  \  }}|r|	|fz   }	 |||||   |fd|i|}|d   }|s'|
|d   fz   }
1 |r|	|fz   }	|st        d ||	|
fD              S t	        ||	|
      S )Nr8   r   r   r   c              3   &   K   | ]	  }||  y wr   r8   ).0rr   s     r0   	<genexpr>z'MPNetEncoder.forward.<locals>.<genexpr>d  s     hqZ[Zghs   )last_hidden_stater   
attentions)compute_position_bias	enumerater   tupler   )r.   r   r   r   r   r   r   rX   r   all_hidden_statesall_attentionsilayer_modulelayer_outputss                 r0   r\   zMPNetEncoder.forwardA  s     22=A"6BD0d(4 	FOA|#$58H$H!(!	
 #4 M *!,M !/=3C2E!E	F$   1]4D Dh]4E~$Vhhh++%
 	
r2   c                 d   |j                  d      |j                  d      |j                  d      }}}||d d d d d f   }|d d d d d f   }nXt        j                  |t        j                        d d d f   }t        j                  |t        j                        d d d f   }||z
  }	| j	                  |	|      }
|
j                  |j                        }
| j                  |
      }|j                  g d      j                  d      }|j                  |d||f      j                         }|S )Nr   r   )r^   )num_buckets)rv   r   r   r?   )rU   rM   rN   r`   relative_position_buckettor_   r   r   ra   rO   r   )r.   xr>   r   bszqlenklencontext_positionmemory_positionrelative_position	rp_bucketvaluess               r0   r   z"MPNetEncoder.compute_position_biask  s   &&)QVVAYq	4T#+Aq$J7*1dA:6O$||D

CAtGL#ll4uzzB47KO+.>>112CQ\1]	LL*	--i8	*44Q7Rt45@@Br2   c                     d}|  }|dz  }||dk  j                  t        j                        |z  z  }t        j                  |      }|dz  }||k  }|t        j                  |j                         |z        t        j                  ||z        z  ||z
  z  j                  t        j                        z   }t        j                  |t        j                  ||dz
              }|t        j                  |||      z  }|S )Nr   rv   r   )
r   rM   r`   abslogfloatr~   min	full_likewhere)r   r   max_distanceretn	max_exactis_smallval_if_larges           r0   r   z%MPNetEncoder.relative_position_bucket}  s    Azz%**%33IIaL1$	y= IIaggi)+,txxy8P/QQU`clUlm
"UZZ. yyu|[[\_/]^u{{8Q55
r2   )NNFFF)N    )r      )r3   r4   r5   rB   rM   r   r   boolr\   r   staticmethodr   rc   rd   s   @r0   r   r   9  s    i 26,0"'%*!(
||(
 !.(
 ELL)	(

  (
 #(
 (
T$  r2   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )MPNetPoolerc                     t         |           t        j                  |j                  |j                        | _        t        j                         | _        y r   )rA   rB   r   r"   rD   r   Tanh
activationrP   s     r0   rB   zMPNetPooler.__init__  s9    YYv1163E3EF
'')r2   r   r   c                 \    |d d df   }| j                  |      }| j                  |      }|S Nr   )r   r   )r.   r   first_token_tensorpooled_outputs       r0   r\   zMPNetPooler.forward  s6     +1a40

#566r2   r   rd   s   @r0   r   r     s#    $
U\\ ell r2   r   c                   2    e 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   dee   dee   deee	j                     ef   fd       Z xZS )
MPNetModelc                     t         |   |       || _        t        |      | _        t        |      | _        |rt        |      nd| _        | j                          y)zv
        add_pooling_layer (bool, *optional*, defaults to `True`):
            Whether to add a pooling layer
        N)
rA   rB   r   r:   r[   r   encoderr   pooler	post_init)r.   r   add_pooling_layerrQ   s      r0   rB   zMPNetModel.__init__  sM    
 	 )&1#F+->k&)D 	r2   c                 .    | j                   j                  S r   r[   rE   r.   s    r0   get_input_embeddingszMPNetModel.get_input_embeddings  s    ...r2   c                 &    || j                   _        y r   r  )r.   values     r0   set_input_embeddingszMPNetModel.set_input_embeddings  s    */'r2   c                     |j                         D ]7  \  }}| j                  j                  |   j                  j	                  |       9 y)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  r   r   r   )r.   heads_to_pruner   r   s       r0   _prune_headszMPNetModel._prune_heads  sE    
 +002 	CLE5LLu%//;;EB	Cr2   rV   r   r>   r   rW   r   r   r   r   c	                    ||n| j                   j                  }||n| j                   j                  }||n| j                   j                  }||t	        d      |#| j                  ||       |j                         }
n!||j                         d d }
nt	        d      ||j                  n|j                  }|t        j                  |
|      }| j                  ||
      }| j                  || j                   j                        }| j                  |||      }| j                  ||||||      }|d   }| j                  | 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_   )rV   r>   rW   )r   r   r   r   r   r   r   )r   pooler_outputr   r   )r   r   r   use_return_dictrl   %warn_if_padding_and_no_attention_maskrU   r_   rM   onesget_extended_attention_maskget_head_maskr   r[   r  r  r   r   r   )r.   rV   r   r>   r   rW   r   r   r   rX   rY   r_   extended_attention_maskembedding_outputencoder_outputssequence_outputr   s                    r0   r\   zMPNetModel.forward  s    2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B] ]%>cdd"66y.Q#..*K&',,.s3KTUU%.%:!!@T@T!"ZZFCN040P0PQ_al0m&&y$++2O2OP	??Y\iv?w,,2/!5# ' 
 *!,8<8OO4UY#]3oab6III)-')77&11	
 	
r2   )T)NNNNNNNN)r3   r4   r5   rB   r	  r  r  r   r   rM   
LongTensorFloatTensorr   r   r   r   r   r\   rc   rd   s   @r0   r   r     s    /0C  156:371559,0/3&*7
E,,-7
 !!2!237
 u//0	7

 E--.7
   1 127
 $D>7
 'tn7
 d^7
 
uU\\"$>>	?7
 7
r2   r   c                   P    e Zd ZdgZ f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	j                     ef   fd       Z xZS )MPNetForMaskedLMzlm_head.decoderc                     t         |   |       t        |d      | _        t	        |      | _        | j                          y NF)r  )rA   rB   r   r   r-   lm_headr  rP   s     r0   rB   zMPNetForMaskedLM.__init__  s6     %@
"6* 	r2   c                 .    | j                   j                  S r   )r"  decoderr  s    r0   get_output_embeddingsz&MPNetForMaskedLM.get_output_embeddings  s    ||###r2   c                 \    || j                   _        |j                  | j                   _        y r   )r"  r$  r'   )r.   new_embeddingss     r0   set_output_embeddingsz&MPNetForMaskedLM.set_output_embeddings  s     -*//r2   rV   r   r>   r   rW   labelsr   r   r   r   c
           
         |	|	n| j                   j                  }	| j                  ||||||||	      }
|
d   }| j                  |      }d}|Ft	               } ||j                  d| j                   j                        |j                  d            }|	s|f|
dd z   }||f|z   S |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]`
        Nr   r>   r   rW   r   r   r   r   r?   rv   losslogitsr   r   )
r   r  r   r"  r   r{   rC   r   r   r   )r.   rV   r   r>   r   rW   r)  r   r   r   r   r  prediction_scoresmasked_lm_lossloss_fctr   s                   r0   r\   zMPNetForMaskedLM.forward  s    & &1%<k$++B]B]**)%'/!5#  	
 "!* LL9')H%&7&<&<RAWAW&XZ`ZeZefhZijN')GABK7F3A3M^%.YSYY$!//))	
 	
r2   	NNNNNNNNN)r3   r4   r5   _tied_weights_keysrB   r%  r(  r   r   rM   r  r  r   r   r   r   r   r\   rc   rd   s   @r0   r  r    s   +,$0  156:371559-1,0/3&*0
E,,-0
 !!2!230
 u//0	0

 E--.0
   1 120
 ))*0
 $D>0
 'tn0
 d^0
 
uU\\"N2	30
 0
r2   r  c                   .     e Zd ZdZ fdZd Zd Z xZS )r-   z5MPNet Head for masked and permuted language modeling.c                    t         |           t        j                  |j                  |j                        | _        t        j                  |j                  |j                        | _        t        j                  |j                  |j                  d      | _
        t        j                  t        j                  |j                              | _        | j                  | j                  _        y )Nr<   F)r'   )rA   rB   r   r"   rD   r   r+   rH   
layer_normrC   r$  	ParameterrM   zerosr'   rP   s     r0   rB   zMPNetLMHead.__init__F  s    YYv1163E3EF
,,v'9'9v?T?TUyy!3!3V5F5FUSLLV->->!?@	 !IIr2   c                 :    | j                   | j                  _         y r   )r'   r$  r  s    r0   _tie_weightszMPNetLMHead._tie_weightsQ  s     IIr2   c                     | j                  |      }t        |      }| j                  |      }| j                  |      }|S r   )r   r   r6  r$  r.   featuresrX   r   s       r0   r\   zMPNetLMHead.forwardT  s;    JJx GOOA LLOr2   )r3   r4   r5   __doc__rB   r:  r\   rc   rd   s   @r0   r-   r-   C  s    ?	&&r2   r-   z
    MPNet Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled
    output) e.g. for GLUE tasks.
    )custom_introc                   >    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	   d	ee	   d
ee	   de
eej                     ef   fd       Z xZS )MPNetForSequenceClassificationc                     t         |   |       |j                  | _        t        |d      | _        t        |      | _        | j                          y r!  )rA   rB   
num_labelsr   r   MPNetClassificationHead
classifierr  rP   s     r0   rB   z'MPNetForSequenceClassification.__init__f  sC      ++%@
1&9 	r2   rV   r   r>   r   rW   r)  r   r   r   r   c
           
         |	|	n| j                   j                  }	| j                  ||||||||	      }
|
d   }| j                  |      }d}|| j                   j                  | j
                  dk(  rd| j                   _        nl| j
                  dkD  rL|j                  t        j                  k(  s|j                  t        j                  k(  rd| j                   _        nd| j                   _        | j                   j                  dk(  rIt               }| j
                  dk(  r& ||j                         |j                               }n |||      }n| j                   j                  dk(  r=t               } ||j                  d| j
                        |j                  d            }n,| j                   j                  dk(  rt               } |||      }|	s|f|
d	d z   }||f|z   S |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   r   
regressionsingle_label_classificationmulti_label_classificationr?   rv   r,  )r   r  r   rE  problem_typerC  r^   rM   r`   rm   r   squeezer   r{   r   r   r   r   r.   rV   r   r>   r   rW   r)  r   r   r   r   r  r.  r-  r1  r   s                   r0   r\   z&MPNetForSequenceClassification.forwardp  s   ( &1%<k$++B]B]**)%'/!5#  	
 "!*1{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#FNN$4fnn6FGD#FF3D))-JJ+-B @&++b/R))-II,./Y,F)-)9TGf$EvE'!//))	
 	
r2   r2  )r3   r4   r5   rB   r   r   rM   r  r  r   r   r   r   r   r\   rc   rd   s   @r0   rA  rA  _  s	     156:371559-1,0/3&*A
E,,-A
 !!2!23A
 u//0	A

 E--.A
   1 12A
 ))*A
 $D>A
 'tnA
 d^A
 
uU\\"$<<	=A
 A
r2   rA  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	   d	ee	   d
ee	   de
eej                     ef   fd       Z xZS )MPNetForMultipleChoicec                     t         |   |       t        |      | _        t	        j
                  |j                        | _        t	        j                  |j                  d      | _
        | j                          y )Nr   )rA   rB   r   r   r   rI   rJ   rK   r"   rD   rE  r  rP   s     r0   rB   zMPNetForMultipleChoice.__init__  sV     '
zz&"<"<=))F$6$6: 	r2   rV   r   r>   r   rW   r)  r   r   r   r   c
           
          |	|	n| j                   j                  }	||j                  d   n|j                  d   }
|!|j                  d|j	                  d            nd}|!|j                  d|j	                  d            nd}|!|j                  d|j	                  d            nd}|1|j                  d|j	                  d      |j	                  d            nd}| j                  ||||||||	      }|d   }| j                  |      }| j                  |      }|j                  d|
      }d}|t               } |||      }|	s|f|dd z   }||f|z   S |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)
        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?   rw   )r>   r   r   rW   r   r   r   rv   r,  )r   r  rz   r{   rU   r   rK   rE  r   r   r   r   )r.   rV   r   r>   r   rW   r)  r   r   r   num_choicesflat_input_idsflat_position_idsflat_attention_maskflat_inputs_embedsr   r   r.  reshaped_logitsr-  r1  r   s                         r0   r\   zMPNetForMultipleChoice.forward  s   H &1%<k$++B]B],5,Aiooa(}GZGZ[\G]CLCXINN2,>?^bLXLdL--b,2C2CB2GHjnR`Rln11"n6I6I"6MNrv ( r=#5#5b#9=;M;Mb;QR 	 ***.,/!5#  	
  
]3/ ++b+6')HOV4D%''!"+5F)-)9TGf$EvE("!//))	
 	
r2   r2  )r3   r4   r5   rB   r   r   rM   r  r  r   r   r   r   r   r\   rc   rd   s   @r0   rN  rN    s	     156:371559-1,0/3&*M
E,,-M
 !!2!23M
 u//0	M

 E--.M
   1 12M
 ))*M
 $D>M
 'tnM
 d^M
 
uU\\"$==	>M
 M
r2   rN  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	   d	ee	   d
ee	   de
eej                     ef   fd       Z xZS )MPNetForTokenClassificationc                 0   t         |   |       |j                  | _        t        |d      | _        t        j                  |j                        | _        t        j                  |j                  |j                        | _        | j                          y r!  )rA   rB   rC  r   r   r   rI   rJ   rK   r"   rD   rE  r  rP   s     r0   rB   z$MPNetForTokenClassification.__init__  sk      ++%@
zz&"<"<=))F$6$68I8IJ 	r2   rV   r   r>   r   rW   r)  r   r   r   r   c
           
         |	|	n| j                   j                  }	| j                  ||||||||	      }
|
d   }| j                  |      }| j	                  |      }d}|<t               } ||j                  d| j                        |j                  d            }|	s|f|
dd z   }||f|z   S |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?   rv   r,  )r   r  r   rK   rE  r   r{   rC  r   r   r   rL  s                   r0   r\   z#MPNetForTokenClassification.forward  s    $ &1%<k$++B]B]**)%'/!5#  	
 "!*,,71')HFKKDOO<fkk"oNDY,F)-)9TGf$EvE$!//))	
 	
r2   r2  )r3   r4   r5   rB   r   r   rM   r  r  r   r   r   r   r   r\   rc   rd   s   @r0   rX  rX    s    	  156:371559-1,0/3&*1
E,,-1
 !!2!231
 u//0	1

 E--.1
   1 121
 ))*1
 $D>1
 'tn1
 d^1
 
uU\\"$99	:1
 1
r2   rX  c                   (     e Zd ZdZ fdZd Z xZS )rD  z-Head for sentence-level classification tasks.c                 &   t         |           t        j                  |j                  |j                        | _        t        j                  |j                        | _        t        j                  |j                  |j                        | _
        y r   )rA   rB   r   r"   rD   r   rI   rJ   rK   rC  out_projrP   s     r0   rB   z MPNetClassificationHead.__init__W  s`    YYv1163E3EF
zz&"<"<=		&"4"4f6G6GHr2   c                     |d d dd d f   }| j                  |      }| j                  |      }t        j                  |      }| j                  |      }| j	                  |      }|S r   )rK   r   rM   tanhr]  r<  s       r0   r\   zMPNetClassificationHead.forward]  sY    Q1WLLOJJqMJJqMLLOMM!r2   )r3   r4   r5   r>  rB   r\   rc   rd   s   @r0   rD  rD  T  s    7Ir2   rD  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 )MPNetForQuestionAnsweringc                     t         |   |       |j                  | _        t        |d      | _        t        j                  |j                  |j                        | _        | j                          y r!  )
rA   rB   rC  r   r   r   r"   rD   
qa_outputsr  rP   s     r0   rB   z"MPNetForQuestionAnswering.__init__i  sU      ++%@
))F$6$68I8IJ 	r2   rV   r   r>   r   rW   start_positionsend_positionsr   r   r   r   c           
      &   |
|
n| j                   j                  }
| j                  |||||||	|
      }|d   }| j                  |      }|j	                  dd      \  }}|j                  d      j                         }|j                  d      j                         }d }||t        |j                               dkD  r|j                  d      }t        |j                               dkD  r|j                  d      }|j                  d      }|j                  d|      }|j                  d|      }t        |      } |||      } |||      }||z   dz  }|
s||f|dd  z   }||f|z   S |S t        ||||j                  |j                        S )	Nr+  r   r   r?   rx   )ignore_indexrv   )r-  start_logits
end_logitsr   r   )r   r  r   rc  splitrK  r   r   rU   clampr   r   r   r   )r.   rV   r   r>   r   rW   rd  re  r   r   r   r   r  r.  rh  ri  
total_lossignored_indexr1  
start_lossend_lossr   s                         r0   r\   z!MPNetForQuestionAnswering.forwards  s    &1%<k$++B]B]**)%'/!5#  	
 "!*1#)<<r<#: j#++B/::<''+668

&=+D?'')*Q."1"9"9""==%%'(1, - 5 5b 9(--a0M-33A}EO)//=AM']CH!,@J
M:H$x/14J"J/'!"+=F/9/EZMF*Q6Q+%!!//))
 	
r2   )
NNNNNNNNNN)r3   r4   r5   rB   r   r   rM   r  r  r   r   r   r   r   r\   rc   rd   s   @r0   ra  ra  g  s     156:3715596:48,0/3&*<
E,,-<
 !!2!23<
 u//0	<

 E--.<
   1 12<
 "%"2"23<
   0 01<
 $D><
 'tn<
 d^<
 
uU\\"$@@	A<
 <
r2   ra  c                     | j                  |      j                         }t        j                  |d      j	                  |      |z  }|j                         |z   S )z
    Replace non-padding symbols with their position numbers. Position numbers begin at padding_idx+1. Padding symbols
    are ignored. This is modified from fairseq's `utils.make_positions`. :param torch.Tensor x: :return torch.Tensor:
    r   rx   )nerm   rM   cumsumtype_asr`   )rV   r*   maskincremental_indicess       r0   rS   rS     sP     <<$((*D,,t3;;DADH##%33r2   )r  rN  ra  rA  rX  r   r   r   )7r>  r~   typingr   r   rM   r   torch.nnr   r   r   activationsr
   r   modeling_outputsr   r   r   r   r   r   r   modeling_utilsr   pytorch_utilsr   r   utilsr   r   configuration_mpnetr   
get_loggerr3   loggerr   Moduler:   rf   r   r   r   r   r   r   r   r  r-   rA  rN  rX  rD  ra  rS   __all__r8   r2   r0   <module>r     s      "   A A '   . Q , , 
		H	% %? % %.8=bii 8=vJ JZ+RYY +^		  "))  @V299 Vt"))  V
% V
 V
rD
+ D
N")) 8 M
%9 M
M
` Y
1 Y
 Y
x >
"6 >
 >
Bbii & H
 4 H
 H
V4	r2   