
    i                     l   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$ ddl%m&Z& ddl'm(Z(  e"jR                  e*      Z+ G d dejX                        Z- G d dejX                        Z. G d dejX                        Z/ G d dejX                        Z0 G d dejX                        Z1 G d dejX                        Z2 G d d ejX                        Z3 G d! d"ejX                        Z4 G d# d$ejX                        Z5	 dCd%ejX                  d&ejl                  d'ejl                  d(ejl                  d)ejl                  dz  d*e7d+e7fd,Z8 G d- d.ejX                        Z9 G d/ d0ejX                        Z: G d1 d2e      Z; G d3 d4ejX                        Z<e  G d5 d6e             Z=e  G d7 d8e=             Z>e  G d9 d:e=             Z? e d;<       G d= d>e=             Z@ e d?<       G d@ dAe=             ZAg dBZBy)Dz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)merge_with_config_defaults)capture_outputs   )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      /var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/transformers/models/markuplm/modeling_markuplm.pyr#   zXPathEmbeddings.__init__3   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)r1   r$   appendr4   r6   torchcatr/   r+   r-   r.   )r7   xpath_tags_seqxpath_subs_seqxpath_tags_embeddingsxpath_subs_embeddingsixpath_embeddingss          r;   forwardzXPathEmbeddings.forwardM   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#   rI   __classcell__r:   s   @r;   r   r   -   s    

4 rJ   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#   r8   r   r2   
vocab_sizer'   pad_token_idword_embeddingsmax_position_embeddingsposition_embeddingsr$   r   rH   type_vocab_sizetoken_type_embeddings	LayerNormlayer_norm_epsr)   r*   r+   register_bufferrA   arangeexpandrT   r7   r8   r:   s     r;   r#   zMarkupLMEmbeddings.__init__b   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\$
 rJ   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   )sizerA   rd   longrj   	unsqueezere   )inputs_embedsrT   input_shapesequence_lengthrW   s        r;   &create_position_ids_from_inputs_embedsz9MarkupLMEmbeddings.create_position_ids_from_inputs_embedsz   sp     $((*3B/%a.||!O_{:Q>ejjYfYmYm
 %%a(//<<rJ   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intrA   cumsumtype_asrl   )	input_idsrT   past_key_values_lengthmaskincremental_indicess        r;   "create_position_ids_from_input_idsz5MarkupLMEmbeddings.create_position_ids_from_input_ids   sW     ||K(,,.$||Da8@@FI__cgg"'')K77rJ   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=   rh   )rk   rj   r{   rT   rq   rA   zerosrl   r\   r8   
tag_pad_idonestuplelistr$   subs_pad_idr^   r`   rH   ra   r+   )r7   rw   rC   rD   token_type_idsrW   rn   ro   rj   words_embeddingsr^   r`   rH   
embeddingss                 r;   rI   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/
\\*-
rJ   )r   )NNNNNN)
rK   rL   rM   rN   r#   staticmethodrq   r{   rI   rO   rP   s   @r;   rR   rR   _   sL    Q
0 = =" 8 8" 1rJ   rR   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 NrU   )r"   r#   r   r%   r'   densera   rb   r)   r*   r+   rf   s     r;   r#   zMarkupLMSelfOutput.__init__   s`    YYv1163E3EF
f&8&8f>S>STzz&"<"<=rJ   hidden_statesinput_tensorreturnc                 r    | j                  |      }| j                  |      }| j                  ||z         }|S Nr   r+   ra   r7   r   r   s      r;   rI   zMarkupLMSelfOutput.forward   7    

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

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

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

=1--m<}5rJ   r   rP   s   @r;   r   r     s$    UU\\ ell rJ   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'   rZ   decoder	ParameterrA   r}   r   rf   s     r;   r#   z!MarkupLMLMPredictionHead.__init__$  s[    8@ yy!3!3V5F5FTRLLV->->!?@	rJ   c                 J    | j                  |      }| j                  |      }|S r   )r   r   r   s     r;   rI   z MarkupLMLMPredictionHead.forward-  s$    }5]3rJ   )rK   rL   rM   r#   rI   rO   rP   s   @r;   r   r   #  s    ArJ   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   predictionsrf   s     r;   r#   zMarkupLMOnlyMLMHead.__init__5  s    3F;rJ   sequence_outputr   c                 (    | j                  |      }|S r   )r   )r7   r   prediction_scoress      r;   rI   zMarkupLMOnlyMLMHead.forward9  s     ,,_=  rJ   r   rP   s   @r;   r   r   4  s#    <!u|| ! !rJ   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?   ri   )ptrainingr   )rA   matmul	transposer   
functionalsoftmaxfloat32tori   r+   r   
contiguous)
r   r   r   r   r   r   r+   kwargsattn_weightsattn_outputs
             r;   eager_attention_forwardr   ?  s     <<s}}Q':;gEL!#n4==((2U]](SVVW\WbWbcL==((6??([L,,|U3K''1-88:K$$rJ   c                        e Zd Z fdZ	 ddej
                  dej                  dz  dee   de	ej
                  ej
                  dz  f   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
ValueErrorr8   rt   attention_head_sizeall_head_sizer   r%   r   r   r   r)   attention_probs_dropout_probr+   attention_dropoutr   rf   s     r;   r#   zMarkupLMSelfAttention.__init__W  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rJ   Nr   r   r   r   c                 x   |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!                         }
|
|fS )Nr=   r   r           )r+   r   )shaper   r   viewr   r   r   r   get_interfacer8   _attn_implementationr   r   r   r   reshaper   )r7   r   r   r   ro   hidden_shapequery_states
key_statesvalue_statesattention_interfacer   r   s               r;   rI   zMarkupLMSelfAttention.forwardl  s>    $))#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L((rJ   r   )rK   rL   rM   r#   rA   r   FloatTensorr   r   r   rI   rO   rP   s   @r;   r   r   V  sd    60 48)||) ))D0) +,	)
 
u||U\\D00	1)rJ   r   c            	            e Zd Z fdZ	 ddej
                  dej                  dz  dee   dej
                  fdZ	 xZ
S )	MarkupLMAttentionc                 b    t         |           t        |      | _        t	        |      | _        y r   )r"   r#   r   r7   r   outputrf   s     r;   r#   zMarkupLMAttention.__init__  s&    )&1	(0rJ   Nr   r   r   r   c                 ^    |} | j                   |fd|i|\  }}| j                  ||      }|S Nr   )r7   r   )r7   r   r   r   residualr9   s         r;   rI   zMarkupLMAttention.forward  sK     !$499
)
 
q
 M8<rJ   r   )rK   rL   rM   r#   rA   r   r   r   r   rI   rO   rP   s   @r;   r   r     sQ    1 48|| ))D0 +,	
 
rJ   r   c            	            e Zd Z fdZ	 d	dej
                  dej                  dz  dee   d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   rf   s     r;   r#   zMarkupLMLayer.__init__  sI    '-'E'E$*6208$V,rJ   Nr   r   r   r   c                      | j                   |fd|i|}t        | j                  | j                  | j                  |      }|S r   )r   r   feed_forward_chunkr   r   )r7   r   r   r   s       r;   rI   zMarkupLMLayer.forward  sY     '
)
 
 2##T%A%A4CSCSUb
 rJ   c                 L    | j                  |      }| j                  ||      }|S r   )r   r   )r7   attention_outputintermediate_outputlayer_outputs       r;   r   z MarkupLMLayer.feed_forward_chunk  s,    "//0@A{{#68HIrJ   r   )rK   rL   rM   r#   rA   r   r   r   r   rI   r   rO   rP   s   @r;   r   r     sV    - 48|| ))D0 +,	
 
$rJ   r   c            	       n     e Zd Z fdZ	 ddej
                  dej                  dz  dee   de	fdZ
 xZS )	MarkupLMEncoderc                     t         |           || _        t        j                  t        |j                        D cg c]  }t        |       c}      | _        d| _	        y c c}w )NF)
r"   r#   r8   r   r0   r1   num_hidden_layersr   layergradient_checkpointing)r7   r8   rG   r:   s      r;   r#   zMarkupLMEncoder.__init__  sN    ]]5IaIaCb#caM&$9#cd
&+# $ds   A#Nr   r   r   r   c                 P    | j                   D ]  } |||fi |} t        |      S )N)last_hidden_state)r  r   )r7   r   r   r   layer_modules        r;   rI   zMarkupLMEncoder.forward  sC     !JJ 	L( M	 +
 	
rJ   r   )rK   rL   rM   r#   rA   r   r   r   r   r   rI   rO   rP   s   @r;   r  r    sM    , 48
||
 ))D0
 +,	

 

rJ   r  c                   `     e Zd ZU eed<   dZeedZ e	j                          fd       Z xZS )MarkupLMPreTrainedModelr8   markuplm)r   
attentionsc                 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=   rX   N)r"   _init_weightsr   r   initzeros_r   rR   copy_rW   rA   rd   r   re   )r7   r   r:   s     r;   r  z%MarkupLMPreTrainedModel._init_weights  sx     	f%f67KK$ 23JJv**ELL9L9L9R9RSU9V,W,^,^_f,gh 4rJ   )rK   rL   rM   r   __annotations__base_model_prefixr   r   _can_record_outputsrA   no_gradr  rO   rP   s   @r;   r  r    s;    "&+
 U]]_i irJ   r  c                   H    e Zd Zd fd	Zd Zd Ze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e   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#   r8   rR   r   r  encoderr   pooler	post_init)r7   r8   add_pooling_layerr:   s      r;   r#   zMarkupLMModel.__init__  sM    
 	 ,V4&v.0AnV,t 	rJ   c                 .    | j                   j                  S r   r   r\   )r7   s    r;   get_input_embeddingsz"MarkupLMModel.get_input_embeddings  s    ...rJ   c                 &    || j                   _        y r   r   )r7   r   s     r;   set_input_embeddingsz"MarkupLMModel.set_input_embeddings  s    */'rJ   Nrw   rC   rD   r   r   rW   rn   r   r   c                    ||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                  ||fi |}|d   }| j                  | j                  |      nd}t        ||      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)rj   rh   r   r   )ri   g      ?g     )rw   rC   rD   rW   r   rn   r   )r	  pooler_output)r   %warn_if_padding_and_no_attention_maskrk   rj   rA   r   r}   rl   rm   r   ri   r   r  r  r   )r7   rw   rC   rD   r   r   rW   rn   r   ro   rj   extended_attention_maskembedding_outputencoder_outputsr   r   s                   r;   rI   zMarkupLMModel.forward  su   J  ]%>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??))%)' + 
 '$,,#
 

 *!,8<8OO4UY)-'
 	
rJ   )T)NNNNNNN)rK   rL   rM   r#   r!  r#  r   r   r   rA   
LongTensorr   r   r   r   r   rI   rO   rP   s   @r;   r  r    s	    /0   .2262637260426K
##d*K
 ((4/K
 ((4/	K

 ))D0K
 ((4/K
 &&-K
 ((4/K
 +,K
 
+	+K
    K
rJ   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e	   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  rf   s     r;   r#   z%MarkupLMForQuestionAnswering.__init___  sU      ++%fF))F$6$68I8IJ 	rJ   Nrw   rC   rD   r   r   rW   rn   start_positionsend_positionsr   r   c
           
          | j                   |f||||||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'
        ```rC   rD   r   r   rW   rn   r   r   r=   r>   N)ignore_indexr   )lossstart_logits
end_logitsr   r  )r  r0  splitsqueezer   lenrk   clamp_r   r   r   r  )r7   rw   rC   rD   r   r   rW   rn   r1  r2  r   outputsr   logitsr7  r8  
total_lossignored_indexloss_fct
start_lossend_losss                        r;   rI   z$MarkupLMForQuestionAnswering.forwardi  s   Z  $--	
))))%'	
 	
 "!*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+%!!//))
 	
rJ   )	NNNNNNNNN)rK   rL   rM   r#   r   r   rA   r   r   r   r   r   rI   rO   rP   s   @r;   r,  r,  \  s      *..2.2.2.2,0-1/3-1T
<<$&T
 t+T
 t+	T

 t+T
 t+T
 llT)T
 ||d*T
 ,T
 ||d*T
 +,T
 
u||	;	;T
  T
rJ   r,  zC
    MarkupLM Model with a `token_classification` head on top.
    )custom_introc                   j    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e	   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 r.  )r"   r#   r/  r  r  classifier_dropoutr*   r   r)   r+   r%   r'   
classifierr  r7   r8   rH  r:   s      r;   r#   z'MarkupLMForTokenClassification.__init__  s      ++%fF)/)B)B)NF%%TZTnTn 	 zz"45))F$6$68I8IJ 	rJ   Nrw   rC   rD   r   r   rW   rn   labelsr   r   c	           
      <    | j                   |f||||||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
        ```r4  r   Nr=   r6  r>  r   r  )	r  rI  r   r   r8   r/  r   r   r  )r7   rw   rC   rD   r   r   rW   rn   rK  r   r=  r   r   r6  rA  s                  r;   rI   z&MarkupLMForTokenClassification.forward  s    V  $--	
))))%'	
 	
 "!* OOO<')H!&&r4;;+A+ABBD
 %$!//))	
 	
rJ   NNNNNNNN)rK   rL   rM   r#   r   r   rA   r   r   r   r   r   rI   rO   rP   s   @r;   rF  rF    s     *..2.2.2.2,0-1&*D
<<$&D
 t+D
 t+	D

 t+D
 t+D
 llT)D
 ||d*D
 t#D
 +,D
 
u||	~	-D
  D
rJ   rF  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                   j    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e	   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/  r8   r  r  rH  r*   r   r)   r+   r%   r'   rI  r  rJ  s      r;   r#   z*MarkupLMForSequenceClassification.__init__(  s      ++%f-)/)B)B)NF%%TZTnTn 	 zz"45))F$6$68I8IJ 	rJ   Nrw   rC   rD   r   r   rW   rn   rK  r   r   c	           
          | j                   |f||||||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
        ```r4  r   N
regressionsingle_label_classificationmulti_label_classificationr=   rM  )r  r+   rI  r8   problem_typer/  ri   rA   rl   rt   r   r:  r   r   r   r   r   r  )r7   rw   rC   rD   r   r   rW   rn   rK  r   r=  r   r>  r6  rA  s                  r;   rI   z)MarkupLMForSequenceClassification.forward7  s   T  $--	
))))%'	
 	
  
]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,./'!//))	
 	
rJ   rN  )rK   rL   rM   r#   r   r   rA   r   r   r   r   r   rI   rO   rP   s   @r;   rP  rP     s	     *..2.2.2.2,0-1&*T
<<$&T
 t+T
 t+	T

 t+T
 t+T
 llT)T
 ||d*T
 t#T
 +,T
 
u||	7	7T
  T
rJ   rP  )r,  rP  rF  r  r  )r   )CrN   collections.abcr   rA   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   utils.genericr   utils.output_capturingr   configuration_markuplmr   
get_loggerrK   loggerModuler   rR   r   r   r   r   r   r   r   r   floatr   r   r   r   r  r  r  r,  rF  rP  __all__ rJ   r;   <module>rj     sL    $   A A & ! 9  G & 6 R R 7 5 2 
		H	%/ bii / dp ph 299  RYY RYY  bii $ryy "!")) !$ %II%<<% 
% <<	%
 LL4'% % %.3)BII 3)n		 .. D
bii 
2 io i i$ f
+ f
 f
R b
#: b
 b
J 
V
%< V

V
r g
(? g
g
TrJ   