
    ie                     T   d Z ddlmZ ddlZddlmZ ddlmZmZmZ ddl	m
Z ddlmZ dd	lmZ dd
lmZmZmZmZmZmZ ddlmZmZ ddlmZ ddlmZ ddlmZm Z m!Z!m"Z" ddl#m$Z$  e"jJ                  e&      Z' G d dejP                        Z) G d dejP                        Z* G d dejP                        Z+ G d dejP                        Z, G d dejP                        Z- G d dejP                        Z. G d dejP                        Z/ G d d ejP                        Z0 G d! d"ejP                        Z1	 dAd#ejP                  d$ejd                  d%ejd                  d&ejd                  d'ejd                  dz  d(e3d)e3fd*Z4 G d+ d,ejP                        Z5 G d- d.ejP                        Z6 G d/ d0e      Z7 G d1 d2ejP                        Z8e  G d3 d4e             Z9e  G d5 d6e9             Z:e  G d7 d8e9             Z; e d9:       G d; d<e9             Z< e d=:       G d> d?e9             Z=g d@Z>y)BzPyTorch MarkupLM model.    )CallableN)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )initialization)ACT2FN)GradientCheckpointingLayer)BaseModelOutputBaseModelOutputWithPoolingMaskedLMOutputQuestionAnsweringModelOutputSequenceClassifierOutputTokenClassifierOutput)ALL_ATTENTION_FUNCTIONSPreTrainedModel)Unpack)apply_chunking_to_forward)TransformersKwargsauto_docstringcan_return_tuplelogging   )MarkupLMConfigc                   *     e Zd ZdZ fdZddZ xZS )XPathEmbeddingszConstruct the embeddings from xpath tags and subscripts.

    We drop tree-id in this version, as its info can be covered by xpath.
    c           	         t         |           |j                  | _        t        j                  |j
                  | j                  z  |j                        | _        t        j                  |j                        | _
        t        j                         | _        t        j                  |j
                  | j                  z  d|j                  z        | _        t        j                  d|j                  z  |j                        | _        t        j                  t!        | j                        D cg c],  }t        j"                  |j$                  |j
                        . c}      | _        t        j                  t!        | j                        D cg c],  }t        j"                  |j(                  |j
                        . c}      | _        y c c}w c c}w )N   )super__init__	max_depthr   Linearxpath_unit_hidden_sizehidden_sizexpath_unitseq2_embeddingsDropouthidden_dropout_probdropoutReLU
activationxpath_unitseq2_inner	inner2emb
ModuleListrange	Embeddingmax_xpath_tag_unit_embeddingsxpath_tag_sub_embeddingsmax_xpath_subs_unit_embeddingsxpath_subs_sub_embeddings)selfconfig_	__class__s      j/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/transformers/models/markuplm/modeling_markuplm.pyr!   zXPathEmbeddings.__init__1   sZ   )))+63P3PSWSaSa3acicucu)v&zz&"<"<='')$&IIf.K.Kdnn.\^_bhbtbt^t$u!1v'9'9#96;M;MN(* t~~. VAA6C`C`a)
% *, t~~. VBBFDaDab*
&s   11G1Gc           	         g }g }t        | j                        D ]^  }|j                   | j                  |   |d d d d |f                |j                   | j                  |   |d d d d |f                ` t        j                  |d      }t        j                  |d      }||z   }| j                  | j                  | j                  | j                  |                        }|S )Ndim)r/   r"   appendr2   r4   torchcatr-   r)   r+   r,   )r5   xpath_tags_seqxpath_subs_seqxpath_tags_embeddingsxpath_subs_embeddingsixpath_embeddingss          r9   forwardzXPathEmbeddings.forwardK   s     " "t~~& 	eA!(()I)F)Fq)I.YZ\]_`Y`Ja)bc!(()J)G)G)J>Z[]^`aZaKb)cd	e !&		*?R H %		*?R H03HH>>$,,ttG`G`aqGr7s*tu    )NN)__name__
__module____qualname____doc__r!   rG   __classcell__r8   s   @r9   r   r   +   s    

4 rH   r   c                   X     e Zd ZdZ fdZed        Zedd       Z	 	 	 	 	 	 ddZ xZ	S )MarkupLMEmbeddingszGConstruct the embeddings from word, position and token_type embeddings.c                 d   t         |           || _        t        j                  |j
                  |j                  |j                        | _        t        j                  |j                  |j                        | _
        |j                  | _        t        |      | _        t        j                  |j                  |j                        | _        t        j                   |j                  |j"                        | _        t        j$                  |j&                        | _        | j+                  dt-        j.                  |j                        j1                  d      d       |j                  | _        t        j                  |j                  |j                  | j2                        | _
        y )N)padding_idxepsposition_idsr   r;   F)
persistent)r    r!   r6   r   r0   
vocab_sizer%   pad_token_idword_embeddingsmax_position_embeddingsposition_embeddingsr"   r   rF   type_vocab_sizetoken_type_embeddings	LayerNormlayer_norm_epsr'   r(   r)   register_bufferr?   arangeexpandrR   r5   r6   r8   s     r9   r!   zMarkupLMEmbeddings.__init__`   s:   !||F,=,=v?Q?Q_e_r_rs#%<<0N0NPVPbPb#c )) / 7%'\\&2H2H&J\J\%]"f&8&8f>S>STzz&"<"<=ELL)G)GHOOPWXej 	 	
 "..#%<<**F,>,>DL\L\$
 rH   c                     | j                         dd }|d   }t        j                  |dz   ||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   )sizer?   rb   longrh   	unsqueezerc   )inputs_embedsrR   input_shapesequence_lengthrU   s        r9   &create_position_ids_from_inputs_embedsz9MarkupLMEmbeddings.create_position_ids_from_inputs_embedsx   sp     $((*3B/%a.||!O_{:Q>ejjYfYmYm
 %%a(//<<rH   c                     | j                  |      j                         }t        j                  |d      j	                  |      |z   |z  }|j                         |z   S )a  
        Replace non-padding symbols with their position numbers. Position numbers begin at padding_idx+1. Padding symbols
        are ignored. This is modified from fairseq's `utils.make_positions`.

        Args:
            x: torch.Tensor x:

        Returns: torch.Tensor
        r   r<   )neintr?   cumsumtype_asrj   )	input_idsrR   past_key_values_lengthmaskincremental_indicess        r9   "create_position_ids_from_input_idsz5MarkupLMEmbeddings.create_position_ids_from_input_ids   sW     ||K(,,.$||Da8@@FI__cgg"'')K77rH   c                    ||j                         }n|j                         d d }||j                  n|j                  }|;|| j                  || j                        }n| j	                  || j                        }|&t        j                  |t
        j                  |      }|| j                  |      }|]| j                  j                  t        j                  t        t        |      | j                  gz         t
        j                  |      z  }|]| j                  j                  t        j                  t        t        |      | j                  gz         t
        j                  |      z  }|}	| j!                  |      }
| j#                  |      }| j%                  ||      }|	|
z   |z   |z   }| j'                  |      }| j)                  |      }|S )Nr;   rf   )ri   rh   ry   rR   ro   r?   zerosrj   rZ   r6   
tag_pad_idonestuplelistr"   subs_pad_idr\   r^   rF   r_   r)   )r5   ru   rA   rB   token_type_idsrU   rl   rm   rh   words_embeddingsr\   r^   rF   
embeddingss                 r9   rG   zMarkupLMEmbeddings.forward   s     #..*K',,.s3K%.%:!!@T@T$#FFyRVRbRbc#JJ=Z^ZjZjk!"[[EJJvVN  00;M !![[33ejjd;'4>>*::;5::V\7 N !![[44uzzd;'4>>*::;5::V\8 N )"66|D $ : :> J00P%(;;>SSVff
^^J/
\\*-
rH   )r   )NNNNNN)
rI   rJ   rK   rL   r!   staticmethodro   ry   rG   rM   rN   s   @r9   rP   rP   ]   sL    Q
0 = =" 8 8" 1rH   rP   c                   n     e Zd Z fdZdej
                  dej
                  dej
                  fdZ xZS )MarkupLMSelfOutputc                 (   t         |           t        j                  |j                  |j                        | _        t        j                  |j                  |j                        | _        t        j                  |j                        | _
        y NrS   )r    r!   r   r#   r%   denser_   r`   r'   r(   r)   rd   s     r9   r!   zMarkupLMSelfOutput.__init__   s`    YYv1163E3EF
f&8&8f>S>STzz&"<"<=rH   hidden_statesinput_tensorreturnc                 r    | j                  |      }| j                  |      }| j                  ||z         }|S Nr   r)   r_   r5   r   r   s      r9   rG   zMarkupLMSelfOutput.forward   7    

=1]3}|'CDrH   rI   rJ   rK   r!   r?   TensorrG   rM   rN   s   @r9   r   r      1    >U\\  RWR^R^ rH   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )MarkupLMIntermediatec                    t         |           t        j                  |j                  |j
                        | _        t        |j                  t              rt        |j                     | _        y |j                  | _        y r   )r    r!   r   r#   r%   intermediate_sizer   
isinstance
hidden_actstrr
   intermediate_act_fnrd   s     r9   r!   zMarkupLMIntermediate.__init__   s]    YYv1163K3KL
f''-'-f.?.?'@D$'-'8'8D$rH   r   r   c                 J    | j                  |      }| j                  |      }|S r   )r   r   r5   r   s     r9   rG   zMarkupLMIntermediate.forward   s&    

=100?rH   r   rN   s   @r9   r   r      s#    9U\\ ell rH   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 )MarkupLMOutputc                 (   t         |           t        j                  |j                  |j
                        | _        t        j                  |j
                  |j                        | _        t        j                  |j                        | _        y r   )r    r!   r   r#   r   r%   r   r_   r`   r'   r(   r)   rd   s     r9   r!   zMarkupLMOutput.__init__   s`    YYv779K9KL
f&8&8f>S>STzz&"<"<=rH   r   r   r   c                 r    | j                  |      }| j                  |      }| j                  ||z         }|S r   r   r   s      r9   rG   zMarkupLMOutput.forward   r   rH   r   rN   s   @r9   r   r      r   rH   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )MarkupLMPoolerc                     t         |           t        j                  |j                  |j                        | _        t        j                         | _        y r   )r    r!   r   r#   r%   r   Tanhr+   rd   s     r9   r!   zMarkupLMPooler.__init__   s9    YYv1163E3EF
'')rH   r   r   c                 \    |d d df   }| j                  |      }| j                  |      }|S )Nr   )r   r+   )r5   r   first_token_tensorpooled_outputs       r9   rG   zMarkupLMPooler.forward  s6     +1a40

#566rH   r   rN   s   @r9   r   r      s#    $
U\\ ell rH   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )MarkupLMPredictionHeadTransformc                 h   t         |           t        j                  |j                  |j                        | _        t        |j                  t              rt        |j                     | _
        n|j                  | _
        t        j                  |j                  |j                        | _        y r   )r    r!   r   r#   r%   r   r   r   r   r
   transform_act_fnr_   r`   rd   s     r9   r!   z(MarkupLMPredictionHeadTransform.__init__  s{    YYv1163E3EF
f''-$*6+<+<$=D!$*$5$5D!f&8&8f>S>STrH   r   r   c                 l    | j                  |      }| j                  |      }| j                  |      }|S r   )r   r   r_   r   s     r9   rG   z'MarkupLMPredictionHeadTransform.forward  s4    

=1--m<}5rH   r   rN   s   @r9   r   r     s$    UU\\ ell rH   r   c                   $     e Zd Z fdZd Z xZS )MarkupLMLMPredictionHeadc                    t         |           t        |      | _        t	        j
                  |j                  |j                  d      | _        t	        j                  t        j                  |j                              | _        y )NT)bias)r    r!   r   	transformr   r#   r%   rX   decoder	Parameterr?   r{   r   rd   s     r9   r!   z!MarkupLMLMPredictionHead.__init__"  s[    8@ yy!3!3V5F5FTRLLV->->!?@	rH   c                 J    | j                  |      }| j                  |      }|S r   )r   r   r   s     r9   rG   z MarkupLMLMPredictionHead.forward+  s$    }5]3rH   )rI   rJ   rK   r!   rG   rM   rN   s   @r9   r   r   !  s    ArH   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )MarkupLMOnlyMLMHeadc                 B    t         |           t        |      | _        y r   )r    r!   r   predictionsrd   s     r9   r!   zMarkupLMOnlyMLMHead.__init__3  s    3F;rH   sequence_outputr   c                 (    | j                  |      }|S r   )r   )r5   r   prediction_scoress      r9   rG   zMarkupLMOnlyMLMHead.forward7  s     ,,_=  rH   r   rN   s   @r9   r   r   2  s#    <!u|| ! !rH   r   modulequerykeyvalueattention_maskscalingr)   c                    t        j                  ||j                  dd            |z  }|||z   }t        j                  j                  |dt         j                        j                  |j                        }t        j                  j                  ||| j                        }t        j                  ||      }	|	j                  dd      j                         }	|	|fS )N   r   r;   )r=   rg   )ptrainingr   )r?   matmul	transposer   
functionalsoftmaxfloat32torg   r)   r   
contiguous)
r   r   r   r   r   r   r)   kwargsattn_weightsattn_outputs
             r9   eager_attention_forwardr   =  s     <<s}}Q':;gEL!#n4==((2U]](SVVW\WbWbcL==((6??([L,,|U3K''1-88:K$$rH   c                        e Zd Z fdZ	 	 d	dej
                  dej                  dz  dedz  dee	   de
ej
                     f
dZ xZS )
MarkupLMSelfAttentionc                 $   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                   | _        | j                  dz  | _        y )Nr   embedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads ()g      )r    r!   r%   num_attention_headshasattr
ValueErrorr6   rr   attention_head_sizeall_head_sizer   r#   r   r   r   r'   attention_probs_dropout_probr)   attention_dropoutr   rd   s     r9   r!   zMarkupLMSelfAttention.__init__U  sC    : ::a?PVXhHi#F$6$6#7 8 445Q8 
 #)#=#= #&v'9'9F<V<V'V#W !558P8PPYYv1143E3EF
99V//1C1CDYYv1143E3EF
zz&"E"EF!'!D!D//5rH   Nr   r   output_attentionsr   r   c                    |j                   d d }g |d| j                  }| j                  |      j                  |      j	                  dd      }| j                  |      j                  |      j	                  dd      }| j                  |      j                  |      j	                  dd      }	t        j                  | j                  j                  t              }
 |
| |||	|f| j                  sdn| j                  | j                  d|\  }} |j                  g |d j!                         }|r||f}|S |f}|S )Nr;   r   r           )r)   r   )shaper   r   viewr   r   r   r   get_interfacer6   _attn_implementationr   r   r   r   reshaper   )r5   r   r   r   r   rm   hidden_shapequery_states
key_statesvalue_statesattention_interfacer   r   outputss                 r9   rG   zMarkupLMSelfAttention.forwardj  sT    $))#2.CCbC$*B*BCzz-055lCMMaQRSXXm,11,?II!QO
zz-055lCMMaQRS(?(M(MKK,,.E)
 %8	%
  $}}C$2H2HLL	%
 	%
!\ *k));;;;FFH1B;- JUrH   NFrI   rJ   rK   r!   r?   r   FloatTensorboolr   r   r~   rG   rM   rN   s   @r9   r   r   T  sg    60 48).	|| ))D0  $;	
 +, 
u||	rH   r   c                        e Zd Z fdZ	 	 d	dej
                  dej                  dz  dedz  dee	   de
ej
                     f
dZ xZS )
MarkupLMAttentionc                 b    t         |           t        |      | _        t	        |      | _        y r   )r    r!   r   r5   r   outputrd   s     r9   r!   zMarkupLMAttention.__init__  s&    )&1	(0rH   Nr   r   r   r   r   c                 n     | j                   |f||d|}| j                  |d   |      }|f|dd  z   }|S N)r   r   r   r   )r5   r   )r5   r   r   r   r   self_outputsattention_outputr   s           r9   rG   zMarkupLMAttention.forward  s\     !tyy
)/
 	
  ;;|AF#%QR(88rH   r   r   rN   s   @r9   r   r     sg    1 48).	|| ))D0  $;	
 +, 
u||	rH   r   c                        e Zd Z fdZ	 	 d
dej
                  dej                  dz  dedz  dee	   de
ej
                     f
dZd	 Z xZS )MarkupLMLayerc                     t         |           |j                  | _        d| _        t	        |      | _        t        |      | _        t        |      | _	        y )Nr   )
r    r!   chunk_size_feed_forwardseq_len_dimr   	attentionr   intermediater   r   rd   s     r9   r!   zMarkupLMLayer.__init__  sI    '-'E'E$*6208$V,rH   Nr   r   r   r   r   c                      | j                   |f||d|}|d   }|dd  }t        | j                  | j                  | j                  |      }|f|z   }|S r   )r   r   feed_forward_chunkr   r   )	r5   r   r   r   r   self_attention_outputsr   r   layer_outputs	            r9   rG   zMarkupLMLayer.forward  s     "0"
)/"
 	"
 2!4(,0##T%A%A4CSCSUe
  /G+rH   c                 L    | j                  |      }| j                  ||      }|S r   )r   r   )r5   r   intermediate_outputr  s       r9   r  z MarkupLMLayer.feed_forward_chunk  s,    "//0@A{{#68HIrH   r   )rI   rJ   rK   r!   r?   r   r   r   r   r   r~   rG   r  rM   rN   s   @r9   r   r     sl    - 48).	|| ))D0  $;	
 +, 
u||	.rH   r   c                        e Zd Z fdZe	 	 	 	 ddej                  dej                  dz  dedz  dedz  dedz  de	e
   d	eej                     ez  fd
       Z xZS )MarkupLMEncoderc                     t         |           || _        t        j                  t        |j                        D cg c]  }t        |       c}      | _        d| _	        y c c}w r   )
r    r!   r6   r   r.   r/   num_hidden_layersr   layergradient_checkpointing)r5   r6   rE   r8   s      r9   r!   zMarkupLMEncoder.__init__  sN    ]]5IaIaCb#caM&$9#cd
&+# $ds   A#Nr   r   r   output_hidden_statesreturn_dictr   r   c                     |rdnd }|rdnd }t        | j                        D ])  \  }	}
|r||fz   } |
|||fi |}|d   }|s!||d   fz   }+ |r||fz   }t        |||      S )N r   r   )last_hidden_stater   
attentions)	enumerater
  r   )r5   r   r   r   r  r  r   all_hidden_statesall_self_attentionsrE   layer_modulelayer_outputss               r9   rG   zMarkupLMEncoder.forward  s     #7BD$5b4(4 	POA|#$58H$H!(! 	M *!,M &9]1=M<O&O#	P   1]4D D++*
 	
rH   )NFFT)rI   rJ   rK   r!   r   r?   r   r   r   r   r   r~   r   rG   rM   rN   s   @r9   r  r    s    ,  48).,1#'"
||"
 ))D0"
  $;	"

 #Tk"
 D["
 +,"
 
u||		."
 "
rH   r  c                   V     e Zd ZU eed<   dZ ej                          fd       Z xZ	S )MarkupLMPreTrainedModelr6   markuplmc                 X   t         |   |       t        |t              r t	        j
                  |j                         yt        |t              rZt	        j                  |j                  t        j                  |j                  j                  d         j                  d             yy)zInitialize the weightsr;   rV   N)r    _init_weightsr   r   initzeros_r   rP   copy_rU   r?   rb   r   rc   )r5   r   r8   s     r9   r  z%MarkupLMPreTrainedModel._init_weights  sx     	f%f67KK$ 23JJv**ELL9L9L9R9RSU9V,W,^,^_f,gh 4rH   )
rI   rJ   rK   r   __annotations__base_model_prefixr?   no_gradr  rM   rN   s   @r9   r  r    s+    "U]]_i irH   r  c                   X    e Zd Zd fd	Zd Zd Zee	 	 	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  dej                  dz  dedz  dedz  dedz  deez  fd              Z xZS )MarkupLMModelc                     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)
r    r!   r6   rP   r   r  encoderr   pooler	post_init)r5   r6   add_pooling_layerr8   s      r9   r!   zMarkupLMModel.__init__  sM    
 	 ,V4&v.0AnV,t 	rH   c                 .    | j                   j                  S r   r   rZ   )r5   s    r9   get_input_embeddingsz"MarkupLMModel.get_input_embeddings  s    ...rH   c                 &    || j                   _        y r   r*  )r5   r   s     r9   set_input_embeddingsz"MarkupLMModel.set_input_embeddings  s    */'rH   Nru   rA   rB   r   r   rU   rl   r   r  r  r   c                 n   ||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                  ||      }|&t        j                  |t        j                  |      }|j                  d      j                  d      }|j                  | j                  	      }d
|z
  dz  }| j                  ||||||      }| j!                  ||||	d      }|d   }| j"                  | j#                  |      nd}t%        |||j&                  |j(                        S )a  
        xpath_tags_seq (`torch.LongTensor` of shape `(batch_size, sequence_length, config.max_depth)`, *optional*):
            Tag IDs for each token in the input sequence, padded up to config.max_depth.
        xpath_subs_seq (`torch.LongTensor` of shape `(batch_size, sequence_length, config.max_depth)`, *optional*):
            Subscript IDs for each token in the input sequence, padded up to config.max_depth.

        Examples:

        ```python
        >>> from transformers import AutoProcessor, MarkupLMModel

        >>> processor = AutoProcessor.from_pretrained("microsoft/markuplm-base")
        >>> model = MarkupLMModel.from_pretrained("microsoft/markuplm-base")

        >>> html_string = "<html> <head> <title>Page Title</title> </head> </html>"

        >>> encoding = processor(html_string, return_tensors="pt")

        >>> outputs = model(**encoding)
        >>> last_hidden_states = outputs.last_hidden_state
        >>> list(last_hidden_states.shape)
        [1, 4, 768]
        ```NzDYou cannot specify both input_ids and inputs_embeds at the same timer;   z5You have to specify either input_ids or inputs_embeds)rh   rf   r   r   )rg   g      ?g     )ru   rA   rB   rU   r   rl   T)r   r  r  r   )r  pooler_outputr   r  )r6   r   r  use_return_dictr   %warn_if_padding_and_no_attention_maskri   rh   r?   r}   r{   rj   rk   r   rg   r   r%  r&  r   r   r  )r5   ru   rA   rB   r   r   rU   rl   r   r  r  r   rm   rh   extended_attention_maskembedding_outputencoder_outputsr   r   s                      r9   rG   zMarkupLMModel.forward"  s   N 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CN!"[[EJJvVN"0":":1"="G"G"J"9"<"<4::"<"N#&)@#@H"L??))%)' + 
 ,,#/!5 ' 
 *!,8<8OO4UY)-')77&11	
 	
rH   )T)
NNNNNNNNNN)rI   rJ   rK   r!   r+  r-  r   r   r?   
LongTensorr   r   r~   r   rG   rM   rN   s   @r9   r#  r#  	  s&    /0  .2262637260426)-,0#'X
##d*X
 ((4/X
 ((4/	X

 ))D0X
 ((4/X
 &&-X
 ((4/X
  $;X
 #TkX
 D[X
 
+	+X
  X
rH   r#  c                       e Zd Z fdZee	 	 	 	 	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  dej                  dz  dedz  dedz  dedz  de	ej                     e
z  fd              Z xZS )MarkupLMForQuestionAnsweringc                     t         |   |       |j                  | _        t        |d      | _        t        j                  |j                  |j                        | _        | j                          y NF)r(  )
r    r!   
num_labelsr#  r  r   r#   r%   
qa_outputsr'  rd   s     r9   r!   z%MarkupLMForQuestionAnswering.__init__  sU      ++%fF))F$6$68I8IJ 	rH   Nru   rA   rB   r   r   rU   rl   start_positionsend_positionsr   r  r  r   c                    ||n| j                   j                  }| j                  ||||||||
|d
      }|d   }| j                  |      }|j	                  dd      \  }}|j                  d      j                         }|j                  d      j                         }d}||	t        |j                               dkD  r|j                  d      }t        |	j                               dkD  r|	j                  d      }	|j                  d      }|j                  d|       |	j                  d|       t        |      } |||      } |||	      }||z   d	z  }t        ||||j                  |j                  
      S )ae  
        xpath_tags_seq (`torch.LongTensor` of shape `(batch_size, sequence_length, config.max_depth)`, *optional*):
            Tag IDs for each token in the input sequence, padded up to config.max_depth.
        xpath_subs_seq (`torch.LongTensor` of shape `(batch_size, sequence_length, config.max_depth)`, *optional*):
            Subscript IDs for each token in the input sequence, padded up to config.max_depth.

        Examples:

        ```python
        >>> from transformers import AutoProcessor, MarkupLMForQuestionAnswering
        >>> import torch

        >>> processor = AutoProcessor.from_pretrained("microsoft/markuplm-base-finetuned-websrc")
        >>> model = MarkupLMForQuestionAnswering.from_pretrained("microsoft/markuplm-base-finetuned-websrc")

        >>> html_string = "<html> <head> <title>My name is Niels</title> </head> </html>"
        >>> question = "What's his name?"

        >>> encoding = processor(html_string, questions=question, return_tensors="pt")

        >>> with torch.no_grad():
        ...     outputs = model(**encoding)

        >>> answer_start_index = outputs.start_logits.argmax()
        >>> answer_end_index = outputs.end_logits.argmax()

        >>> predict_answer_tokens = encoding.input_ids[0, answer_start_index : answer_end_index + 1]
        >>> processor.decode(predict_answer_tokens).strip()
        'Niels'
        ```NT	rA   rB   r   r   rU   rl   r   r  r  r   r   r;   r<   )ignore_indexr   )lossstart_logits
end_logitsr   r  )r6   r0  r  r;  splitsqueezer   lenri   clamp_r   r   r   r  )r5   ru   rA   rB   r   r   rU   rl   r<  r=  r   r  r  r   r   r   logitsrB  rC  
total_lossignored_indexloss_fct
start_lossend_losss                           r9   rG   z$MarkupLMForQuestionAnswering.forward  s   ` &1%<k$++B]B]--))))%'/!5   
 "!*1#)<<r<#: j#++B/::<''+668

&=+D?'')*Q."1"9"9""==%%'(1, - 5 5b 9(--a0M""1m4  M2']CH!,@J
M:H$x/14J+%!!//))
 	
rH   )NNNNNNNNNNNN)rI   rJ   rK   r!   r   r   r?   r   r   r~   r   rG   rM   rN   s   @r9   r7  r7    sE     *..2.2.2.2,0-1/3-1)-,0#'[
<<$&[
 t+[
 t+	[

 t+[
 t+[
 llT)[
 ||d*[
 ,[
 ||d*[
  $;[
 #Tk[
 D[[
 
u||	;	;[
  [
rH   r7  zC
    MarkupLM Model with a `token_classification` head on top.
    )custom_introc                       e Zd Z fdZee	 	 	 	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  dedz  dedz  dedz  de	ej                     e
z  fd              Z xZS )MarkupLMForTokenClassificationc                 d   t         |   |       |j                  | _        t        |d      | _        |j
                  |j
                  n|j                  }t        j                  |      | _	        t        j                  |j                  |j                        | _        | j                          y r9  )r    r!   r:  r#  r  classifier_dropoutr(   r   r'   r)   r#   r%   
classifierr'  r5   r6   rR  r8   s      r9   r!   z'MarkupLMForTokenClassification.__init__  s      ++%fF)/)B)B)NF%%TZTnTn 	 zz"45))F$6$68I8IJ 	rH   Nru   rA   rB   r   r   rU   rl   labelsr   r  r  r   c                 r   ||n| j                   j                  }| j                  ||||||||	|
d
      }|d   }| j                  |      }d}|Ft	               } ||j                  d| j                   j                        |j                  d            }t        |||j                  |j                        S )a  
        xpath_tags_seq (`torch.LongTensor` of shape `(batch_size, sequence_length, config.max_depth)`, *optional*):
            Tag IDs for each token in the input sequence, padded up to config.max_depth.
        xpath_subs_seq (`torch.LongTensor` of shape `(batch_size, sequence_length, config.max_depth)`, *optional*):
            Subscript IDs for each token in the input sequence, padded up to config.max_depth.
        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]`.

        Examples:

        ```python
        >>> from transformers import AutoProcessor, AutoModelForTokenClassification
        >>> import torch

        >>> processor = AutoProcessor.from_pretrained("microsoft/markuplm-base")
        >>> processor.parse_html = False
        >>> model = AutoModelForTokenClassification.from_pretrained("microsoft/markuplm-base", num_labels=7)

        >>> nodes = ["hello", "world"]
        >>> xpaths = ["/html/body/div/li[1]/div/span", "/html/body/div/li[1]/div/span"]
        >>> node_labels = [1, 2]
        >>> encoding = processor(nodes=nodes, xpaths=xpaths, node_labels=node_labels, return_tensors="pt")

        >>> with torch.no_grad():
        ...     outputs = model(**encoding)

        >>> loss = outputs.loss
        >>> logits = outputs.logits
        ```NTr?  r   r;   rA  rH  r   r  )
r6   r0  r  rS  r   r   r:  r   r   r  )r5   ru   rA   rB   r   r   rU   rl   rU  r   r  r  r   r   r   r   rA  rK  s                     r9   rG   z&MarkupLMForTokenClassification.forward  s    \ &1%<k$++B]B]--))))%'/!5   
 "!* OOO<')H!&&r4;;+A+ABBD
 %$!//))	
 	
rH   NNNNNNNNNNN)rI   rJ   rK   r!   r   r   r?   r   r   r~   r   rG   rM   rN   s   @r9   rP  rP    s-     *..2.2.2.2,0-1&*)-,0#'K
<<$&K
 t+K
 t+	K

 t+K
 t+K
 llT)K
 ||d*K
 t#K
  $;K
 #TkK
 D[K
 
u||	~	-K
  K
rH   rP  z
    MarkupLM Model transformer with a sequence classification/regression head on top (a linear layer on top of the
    pooled output) e.g. for GLUE tasks.
    c                       e Zd Z fdZee	 	 	 	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  dej                  dz  d	ej                  dz  d
ej                  dz  dedz  dedz  dedz  de	ej                     e
z  fd              Z xZS )!MarkupLMForSequenceClassificationc                 n   t         |   |       |j                  | _        || _        t	        |      | _        |j                  |j                  n|j                  }t        j                  |      | _
        t        j                  |j                  |j                        | _        | j                          y r   )r    r!   r:  r6   r#  r  rR  r(   r   r'   r)   r#   r%   rS  r'  rT  s      r9   r!   z*MarkupLMForSequenceClassification.__init__Y  s      ++%f-)/)B)B)NF%%TZTnTn 	 zz"45))F$6$68I8IJ 	rH   Nru   rA   rB   r   r   rU   rl   rU  r   r  r  r   c                    ||n| j                   j                  }| j                  ||||||||	|
d
      }|d   }| j                  |      }| j	                  |      }d}|| j                   j
                  | j                  dk(  rd| j                   _        nl| j                  dkD  rL|j                  t        j                  k(  s|j                  t        j                  k(  rd| j                   _        nd| j                   _        | j                   j
                  dk(  rIt               }| j                  dk(  r& ||j                         |j                               }n |||      }n| j                   j
                  dk(  r=t               } ||j                  d| j                        |j                  d            }n,| j                   j
                  dk(  rt               } |||      }t!        |||j"                  |j$                  	      S )
a  
        xpath_tags_seq (`torch.LongTensor` of shape `(batch_size, sequence_length, config.max_depth)`, *optional*):
            Tag IDs for each token in the input sequence, padded up to config.max_depth.
        xpath_subs_seq (`torch.LongTensor` of shape `(batch_size, sequence_length, config.max_depth)`, *optional*):
            Subscript IDs for each token in the input sequence, padded up to config.max_depth.
        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).

        Examples:

        ```python
        >>> from transformers import AutoProcessor, AutoModelForSequenceClassification
        >>> import torch

        >>> processor = AutoProcessor.from_pretrained("microsoft/markuplm-base")
        >>> model = AutoModelForSequenceClassification.from_pretrained("microsoft/markuplm-base", num_labels=7)

        >>> html_string = "<html> <head> <title>Page Title</title> </head> </html>"
        >>> encoding = processor(html_string, return_tensors="pt")

        >>> with torch.no_grad():
        ...     outputs = model(**encoding)

        >>> loss = outputs.loss
        >>> logits = outputs.logits
        ```NTr?  r   
regressionsingle_label_classificationmulti_label_classificationr;   rW  )r6   r0  r  r)   rS  problem_typer:  rg   r?   rj   rr   r   rE  r   r   r   r   r   r  )r5   ru   rA   rB   r   r   rU   rl   rU  r   r  r  r   r   r   rH  rA  rK  s                     r9   rG   z)MarkupLMForSequenceClassification.forwardh  s   Z &1%<k$++B]B]--))))%'/!5   
  
]3/{{''/??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,./'!//))	
 	
rH   rX  )rI   rJ   rK   r!   r   r   r?   r   r   r~   r   rG   rM   rN   s   @r9   rZ  rZ  Q  s.     *..2.2.2.2,0-1&*)-,0#'[
<<$&[
 t+[
 t+	[

 t+[
 t+[
 llT)[
 ||d*[
 t#[
  $;[
 #Tk[
 D[[
 
u||	7	7[
  [
rH   rZ  )r7  rZ  rP  r#  r  )r   )?rL   collections.abcr   r?   r   torch.nnr   r   r    r	   r  activationsr
   modeling_layersr   modeling_outputsr   r   r   r   r   r   modeling_utilsr   r   processing_utilsr   pytorch_utilsr   utilsr   r   r   r   configuration_markuplmr   
get_loggerrI   loggerModuler   rP   r   r   r   r   r   r   r   r   floatr   r   r   r   r  r  r#  r7  rP  rZ  __all__r  rH   r9   <module>rq     sG    $   A A & ! 9  G & 6 R R 2 
		H	%/ bii / dp ph 299  RYY RYY  bii $ryy "!")) !$ %II%<<% 
% <<	%
 LL4'% % %.5BII 5r		 2#. #N*
bii *
Z io i i r
+ r
 r
j i
#: i
 i
X 
]
%< ]

]
@ n
(? n
n
brH   