
    i                     D   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mZmZ ddl m!Z!  ejD                  e#      Z$ G d dejJ                        Z& G d dejJ                        Z' G d dejJ                        Z( G d dejJ                        Z) G d dejJ                        Z* G d dejJ                        Z+ G d dejJ                        Z, G d dejJ                        Z- G d  d!ejJ                        Z.	 d@d"ejJ                  d#ej^                  d$ej^                  d%ej^                  d&ej^                  dz  d'e0d(e0fd)Z1 G d* d+ejJ                        Z2 G d, d-ejJ                        Z3 G d. d/e      Z4 G d0 d1ejJ                        Z5e G d2 d3e             Z6e G d4 d5e6             Z7e G d6 d7e6             Z8 ed89       G d: d;e6             Z9 ed<9       G d= d>e6             Z:g d?Z;y)AzPyTorch MarkupLM model.    )CallableN)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )initialization)ACT2FN)GradientCheckpointingLayer)BaseModelOutputBaseModelOutputWithPoolingMaskedLMOutputQuestionAnsweringModelOutputSequenceClassifierOutputTokenClassifierOutput)ALL_ATTENTION_FUNCTIONSPreTrainedModel)apply_chunking_to_forward)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      x/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/transformers/models/markuplm/modeling_markuplm.pyr   zXPathEmbeddings.__init__0   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    appendr0   r2   torchcatr+   r'   r)   r*   )r3   xpath_tags_seqxpath_subs_seqxpath_tags_embeddingsxpath_subs_embeddingsixpath_embeddingss          r7   forwardzXPathEmbeddings.forwardJ   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   rE   __classcell__r6   s   @r7   r   r   *   s    

4 rF   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   r9   F)
persistent)r   r   r4   r   r.   
vocab_sizer#   pad_token_idword_embeddingsmax_position_embeddingsposition_embeddingsr    r   rD   type_vocab_sizetoken_type_embeddings	LayerNormlayer_norm_epsr%   r&   r'   register_bufferr=   arangeexpandrP   r3   r4   r6   s     r7   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\$
 rF   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
        Nr9   r   dtypedevicer   )sizer=   r`   longrf   	unsqueezera   )inputs_embedsrP   input_shapesequence_lengthrS   s        r7   &create_position_ids_from_inputs_embedsz9MarkupLMEmbeddings.create_position_ids_from_inputs_embedsw   sp     $((*3B/%a.||!O_{:Q>ejjYfYmYm
 %%a(//<<rF   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_asrh   )	input_idsrP   past_key_values_lengthmaskincremental_indicess        r7   "create_position_ids_from_input_idsz5MarkupLMEmbeddings.create_position_ids_from_input_ids   sW     ||K(,,.$||Da8@@FI__cgg"'')K77rF   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 )Nr9   rd   )rg   rf   rw   rP   rm   r=   zerosrh   rX   r4   
tag_pad_idonestuplelistr    subs_pad_idrZ   r\   rD   r]   r'   )r3   rs   r?   r@   token_type_idsrS   rj   rk   rf   words_embeddingsrZ   r\   rD   
embeddingss                 r7   rE   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/
\\*-
rF   )r   )NNNNNN)
rG   rH   rI   rJ   r   staticmethodrm   rw   rE   rK   rL   s   @r7   rN   rN   \   sL    Q
0 = =" 8 8" 1rF   rN   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 NrQ   )r   r   r   r!   r#   denser]   r^   r%   r&   r'   rb   s     r7   r   zMarkupLMSelfOutput.__init__   s`    YYv1163E3EF
f&8&8f>S>STzz&"<"<=rF   hidden_statesinput_tensorreturnc                 r    | j                  |      }| j                  |      }| j                  ||z         }|S Nr   r'   r]   r3   r   r   s      r7   rE   zMarkupLMSelfOutput.forward   7    

=1]3}|'CDrF   rG   rH   rI   r   r=   TensorrE   rK   rL   s   @r7   r   r      1    >U\\  RWR^R^ rF   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_fnrb   s     r7   r   zMarkupLMIntermediate.__init__   s]    YYv1163K3KL
f''-'-f.?.?'@D$'-'8'8D$rF   r   r   c                 J    | j                  |      }| j                  |      }|S r   )r   r   r3   r   s     r7   rE   zMarkupLMIntermediate.forward   s&    

=100?rF   r   rL   s   @r7   r   r      s#    9U\\ ell rF   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'   rb   s     r7   r   zMarkupLMOutput.__init__   s`    YYv779K9KL
f&8&8f>S>STzz&"<"<=rF   r   r   r   c                 r    | j                  |      }| j                  |      }| j                  ||z         }|S r   r   r   s      r7   rE   zMarkupLMOutput.forward   r   rF   r   rL   s   @r7   r   r      r   rF   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)   rb   s     r7   r   zMarkupLMPooler.__init__   s9    YYv1163E3EF
'')rF   r   r   c                 \    |d d df   }| j                  |      }| j                  |      }|S )Nr   )r   r)   )r3   r   first_token_tensorpooled_outputs       r7   rE   zMarkupLMPooler.forward  s6     +1a40

#566rF   r   rL   s   @r7   r   r      s#    $
U\\ ell rF   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^   rb   s     r7   r   z(MarkupLMPredictionHeadTransform.__init__  s{    YYv1163E3EF
f''-$*6+<+<$=D!$*$5$5D!f&8&8f>S>STrF   r   r   c                 l    | j                  |      }| j                  |      }| j                  |      }|S r   )r   r   r]   r   s     r7   rE   z'MarkupLMPredictionHeadTransform.forward  s4    

=1--m<}5rF   r   rL   s   @r7   r   r     s$    UU\\ ell rF   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#   rV   decoder	Parameterr=   ry   r   rb   s     r7   r   z!MarkupLMLMPredictionHead.__init__!  s[    8@ yy!3!3V5F5FTRLLV->->!?@	rF   c                 J    | j                  |      }| j                  |      }|S r   )r   r   r   s     r7   rE   z MarkupLMLMPredictionHead.forward*  s$    }5]3rF   )rG   rH   rI   r   rE   rK   rL   s   @r7   r   r      s    ArF   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   predictionsrb   s     r7   r   zMarkupLMOnlyMLMHead.__init__2  s    3F;rF   sequence_outputr   c                 (    | j                  |      }|S r   )r   )r3   r   prediction_scoress      r7   rE   zMarkupLMOnlyMLMHead.forward6  s     ,,_=  rF   r   rL   s   @r7   r   r   1  s#    <!u|| ! !rF   r   modulequerykeyvalueattention_maskscalingr'   c                    t        j                  ||j                  dd            |z  }|#|d d d d d d d |j                  d   f   }	||	z   }t        j
                  j                  |dt         j                        j                  |j                        }t        j
                  j                  ||| j                        }t        j                  ||      }
|
j                  dd      j                         }
|
|fS )N   r   r9   )r;   re   )ptrainingr   )r=   matmul	transposeshaper   
functionalsoftmaxfloat32tore   r'   r   
contiguous)r   r   r   r   r   r   r'   kwargsattn_weightscausal_maskattn_outputs              r7   eager_attention_forwardr   <  s     <<s}}Q':;gEL!$Q1o		"o%=>#k1==((2U]](SVVW\WbWbcL==((6??([L,,|U3K''1-88:K$$rF   c            
            e Zd Z fdZ	 	 ddej
                  dej                  dz  dedz  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
ValueErrorr4   rp   attention_head_sizeall_head_sizer   r!   r   r   r   r%   attention_probs_dropout_probr'   attention_dropoutr   rb   s     r7   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rF   Nr   r   output_attentionsr   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 )Nr9   r   r           )r'   r   )r   r   r   viewr   r   r   r   get_interfacer4   _attn_implementationr   r   r   r   reshaper   )r3   r   r   r   r   rk   hidden_shapequery_states
key_statesvalue_statesattention_interfacer   r   outputss                 r7   rE   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rF   NFrG   rH   rI   r   r=   r   FloatTensorboolr|   rE   rK   rL   s   @r7   r   r   T  sW    60 48).	|| ))D0  $;	 
u||	rF   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j
                     fdZ	 xZ
S )	MarkupLMAttentionc                 b    t         |           t        |      | _        t	        |      | _        y r   )r   r   r   r3   r   outputrb   s     r7   r   zMarkupLMAttention.__init__  s&    )&1	(0rF   Nr   r   r   r   c                 n     | j                   |f||d|}| j                  |d   |      }|f|dd  z   }|S N)r   r   r   r   )r3   r   )r3   r   r   r   r   self_outputsattention_outputr   s           r7   rE   zMarkupLMAttention.forward  s\     !tyy
)/
 	
  ;;|AF#%QR(88rF   r   r   rL   s   @r7   r   r     sW    1 48).	|| ))D0  $;	 
u||	rF   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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   rb   s     r7   r   zMarkupLMLayer.__init__  sI    '-'E'E$*6208$V,rF   Nr   r   r   r   c                      | j                   |f||d|}|d   }|dd  }t        | j                  | j                  | j                  |      }|f|z   }|S r   )r   r   feed_forward_chunkr   r   )	r3   r   r   r   r   self_attention_outputsr   r   layer_outputs	            r7   rE   zMarkupLMLayer.forward  s     "0"
)/"
 	"
 2!4(,0##T%A%A4CSCSUe
  /G+rF   c                 L    | j                  |      }| j                  ||      }|S r   )r   r   )r3   r   intermediate_outputr  s       r7   r  z MarkupLMLayer.feed_forward_chunk  s,    "//0@A{{#68HIrF   r   )rG   rH   rI   r   r=   r   r   r   r|   rE   r  rK   rL   s   @r7   r   r     s\    - 48).	|| ))D0  $;	 
u||	.rF   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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   r4   r   r,   r-   num_hidden_layersr   layergradient_checkpointing)r3   r4   rC   r6   s      r7   r   zMarkupLMEncoder.__init__  sN    ]]5IaIaCb#caM&$9#cd
&+# $ds   A#Nr   r   r   output_hidden_statesreturn_dictr   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   )r3   r   r   r   r  r  r   all_hidden_statesall_self_attentionsrC   layer_modulelayer_outputss               r7   rE   zMarkupLMEncoder.forward  s     #7BD$5b4(4 	POA|#$58H$H!(! 	M *!,M &9]1=M<O&O#	P   1]4D D++*
 	
rF   )NFFT)rG   rH   rI   r   r   r=   r   r   r   r|   r   rE   rK   rL   s   @r7   r  r    s    ,  48).,1#'"
||"
 ))D0"
  $;	"

 #Tk"
 D["
 
u||		."
 "
rF   r  c                   V     e Zd ZU eed<   dZ ej                          fd       Z xZ	S )MarkupLMPreTrainedModelr4   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 weightsr9   rT   N)r   _init_weightsr   r   initzeros_r   rN   copy_rS   r=   r`   r   ra   )r3   r   r6   s     r7   r  z%MarkupLMPreTrainedModel._init_weights  sx     	f%f67KK$ 23JJv**ELL9L9L9R9RSU9V,W,^,^_f,gh 4rF   )
rG   rH   rI   r   __annotations__base_model_prefixr=   no_gradr  rK   rL   s   @r7   r  r    s+    "U]]_i irF   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   r4   rN   r   r  encoderr   pooler	post_init)r3   r4   add_pooling_layerr6   s      r7   r   zMarkupLMModel.__init__  sM    
 	 ,V4&v.0AnV,t 	rF   c                 .    | j                   j                  S r   r   rX   )r3   s    r7   get_input_embeddingsz"MarkupLMModel.get_input_embeddings  s    ...rF   c                 &    || j                   _        y r   r*  )r3   r   s     r7   set_input_embeddingsz"MarkupLMModel.set_input_embeddings  s    */'rF   Nrs   r?   r@   r   r   rS   rj   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 timer9   z5You have to specify either input_ids or inputs_embeds)rf   rd   r   r   )re   g      ?g     )rs   r?   r@   rS   r   rj   T)r   r  r  r   )r  pooler_outputr   r  )r4   r   r  use_return_dictr   %warn_if_padding_and_no_attention_maskrg   rf   r=   r{   ry   rh   ri   r   re   r   r%  r&  r   r   r  )r3   rs   r?   r@   r   r   rS   rj   r   r  r  r   rk   rf   extended_attention_maskembedding_outputencoder_outputsr   r   s                      r7   rE   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	
 	
rF   )T)
NNNNNNNNNN)rG   rH   rI   r   r+  r-  r   r   r=   
LongTensorr   r   r|   r   rE   rK   rL   s   @r7   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
rF   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'  rb   s     r7   r   z%MarkupLMForQuestionAnswering.__init__  sU      ++%fF))F$6$68I8IJ 	rF   Nrs   r?   r@   r   r   rS   rj   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	r?   r@   r   r   rS   rj   r   r  r  r   r   r9   r:   )ignore_indexr   )lossstart_logits
end_logitsr   r  )r4   r0  r  r;  splitsqueezer   lenrg   clamp_r   r   r   r  )r3   rs   r?   r@   r   r   rS   rj   r<  r=  r   r  r  r   r   r   logitsrB  rC  
total_lossignored_indexloss_fct
start_lossend_losss                           r7   rE   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+%!!//))
 	
rF   )NNNNNNNNNNNN)rG   rH   rI   r   r   r   r=   r   r   r|   r   rE   rK   rL   s   @r7   r7  r7    sE     *..2.2.2.2,0-1/3-1)-,0#'[
<<$&[
 t+[
 t+	[

 t+[
 t+[
 llT)[
 ||d*[
 ,[
 ||d*[
  $;[
 #Tk[
 D[[
 
u||	;	;[
  [
rF   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'  r3   r4   rR  r6   s      r7   r   z'MarkupLMForTokenClassification.__init__  s      ++%fF)/)B)B)NF%%TZTnTn 	 zz"45))F$6$68I8IJ 	rF   Nrs   r?   r@   r   r   rS   rj   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   r9   rA  rH  r   r  )
r4   r0  r  rS  r   r   r:  r   r   r  )r3   rs   r?   r@   r   r   rS   rj   rU  r   r  r  r   r   r   r   rA  rK  s                     r7   rE   z&MarkupLMForTokenClassification.forward  s    \ &1%<k$++B]B]--))))%'/!5   
 "!* OOO<')H!&&r4;;+A+ABBD
 %$!//))	
 	
rF   NNNNNNNNNNN)rG   rH   rI   r   r   r   r=   r   r   r|   r   rE   rK   rL   s   @r7   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
rF   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:  r4   r#  r  rR  r&   r   r%   r'   r!   r#   rS  r'  rT  s      r7   r   z*MarkupLMForSequenceClassification.__init__Y  s      ++%f-)/)B)B)NF%%TZTnTn 	 zz"45))F$6$68I8IJ 	rF   Nrs   r?   r@   r   r   rS   rj   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_classificationr9   rW  )r4   r0  r  r'   rS  problem_typer:  re   r=   rh   rp   r   rE  r   r   r   r   r   r  )r3   rs   r?   r@   r   r   rS   rj   rU  r   r  r  r   r   r   rH  rA  rK  s                     r7   rE   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,./'!//))	
 	
rF   rX  )rG   rH   rI   r   r   r   r=   r   r   r|   r   rE   rK   rL   s   @r7   rZ  rZ  Q  s.     *..2.2.2.2,0-1&*)-,0#'[
<<$&[
 t+[
 t+	[

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