o
    eie                     @   sn  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"%e&Z'G dd dej(Z)G dd dej(Z*G dd dej(Z+G dd dej(Z,G dd dej(Z-G dd dej(Z.G dd dej(Z/G dd  d ej(Z0G d!d" d"ej(Z1	#dCd$ej(d%ej2d&ej2d'ej2d(ej2dB d)e3d*e3fd+d,Z4G d-d. d.ej(Z5G d/d0 d0ej(Z6G d1d2 d2eZ7G d3d4 d4ej(Z8e G d5d6 d6eZ9e G d7d8 d8e9Z:e G d9d: d:e9Z;e d;d<G d=d> d>e9Z<e d?d<G d@dA dAe9Z=g dBZ>dS )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   )MarkupLMConfigc                       s*   e Zd ZdZ fddZdddZ  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                    s   t     j| _t j| j  j| _t j	| _
t | _t j| j d j | _td j  j| _t fddt| jD | _t fddt| jD | _d S )N   c                       g | ]
}t  j jqS  )r   	Embeddingmax_xpath_tag_unit_embeddingsxpath_unit_hidden_size.0_configr   l/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/transformers/models/markuplm/modeling_markuplm.py
<listcomp>>       z,XPathEmbeddings.__init__.<locals>.<listcomp>c                    r   r   )r   r   max_xpath_subs_unit_embeddingsr!   r"   r%   r   r'   r(   E   r)   )super__init__	max_depthr   Linearr!   hidden_sizexpath_unitseq2_embeddingsDropouthidden_dropout_probdropoutReLU
activationxpath_unitseq2_inner	inner2emb
ModuleListrangexpath_tag_sub_embeddingsxpath_subs_sub_embeddingsselfr&   	__class__r%   r'   r,   1   s"   




zXPathEmbeddings.__init__Nc              	   C   s   g }g }t | jD ](}|| j| |d d d d |f  || j| |d d d d |f  q	tj|dd}tj|dd}|| }| | | 	| 
|}|S )Ndim)r9   r-   appendr:   r;   torchcatr7   r3   r5   r6   )r=   xpath_tags_seqxpath_subs_seqxpath_tags_embeddingsxpath_subs_embeddingsixpath_embeddingsr   r   r'   forwardK   s   &(zXPathEmbeddings.forward)NN)__name__
__module____qualname____doc__r,   rL   __classcell__r   r   r>   r'   r   +   s    r   c                       sP   e Zd ZdZ fddZedd ZedddZ												dd
dZ  Z	S )MarkupLMEmbeddingszGConstruct the embeddings from word, position and token_type embeddings.c                    s   t    || _tj|j|j|jd| _t|j	|j| _
|j| _t|| _t|j|j| _tj|j|jd| _t|j| _| jdt|j	ddd |j| _tj|j	|j| jd| _
d S )N)padding_idxepsposition_idsr   r@   F)
persistent)r+   r,   r&   r   r   
vocab_sizer/   pad_token_idword_embeddingsmax_position_embeddingsposition_embeddingsr-   r   rK   type_vocab_sizetoken_type_embeddings	LayerNormlayer_norm_epsr1   r2   r3   register_bufferrD   arangeexpandrS   r<   r>   r   r'   r,   `   s    

zMarkupLMEmbeddings.__init__c                 C   sJ   |   dd }|d }tj|d || d tj| jd}|d|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   )sizerD   rc   longrg   	unsqueezerd   )inputs_embedsrS   input_shapesequence_lengthrV   r   r   r'   &create_position_ids_from_inputs_embedsx   s   z9MarkupLMEmbeddings.create_position_ids_from_inputs_embedsr   c                 C   s6   |  | }tj|dd|| | }| | 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   rA   )neintrD   cumsumtype_asri   )	input_idsrS   past_key_values_lengthmaskincremental_indicesr   r   r'   "create_position_ids_from_input_ids   s   z5MarkupLMEmbeddings.create_position_ids_from_input_idsNc                 C   s@  |d ur	|  }n|  d d }|d ur|jn|j}|d u r2|d ur+| || j}n| || j}|d u r?tj|tj|d}|d u rH| |}|d u ra| j	j
tjtt|| jg tj|d }|d u rz| j	jtjtt|| jg tj|d }|}	| |}
| |}| ||}|	|
 | | }| |}| |}|S )Nr@   re   )rh   rg   rw   rS   rn   rD   zerosri   r[   r&   
tag_pad_idonestuplelistr-   subs_pad_idr]   r_   rK   r`   r3   )r=   rs   rF   rG   token_type_idsrV   rk   rl   rg   words_embeddingsr]   r_   rK   
embeddingsr   r   r'   rL      s8   	







zMarkupLMEmbeddings.forward)r   )NNNNNN)
rM   rN   rO   rP   r,   staticmethodrn   rw   rL   rQ   r   r   r>   r'   rR   ]   s    
rR   c                       8   e Zd Z fddZdejdejdejfddZ  ZS )MarkupLMSelfOutputc                    sB   t    t|j|j| _tj|j|jd| _t|j	| _
d S NrT   )r+   r,   r   r.   r/   denser`   ra   r1   r2   r3   r<   r>   r   r'   r,         
zMarkupLMSelfOutput.__init__hidden_statesinput_tensorreturnc                 C   &   |  |}| |}| || }|S Nr   r3   r`   r=   r   r   r   r   r'   rL         

zMarkupLMSelfOutput.forwardrM   rN   rO   r,   rD   TensorrL   rQ   r   r   r>   r'   r          $r   c                       2   e Zd Z fddZdejdejfddZ  ZS )MarkupLMIntermediatec                    sD   t    t|j|j| _t|jt	rt
|j | _d S |j| _d S r   )r+   r,   r   r.   r/   intermediate_sizer   
isinstance
hidden_actstrr	   intermediate_act_fnr<   r>   r   r'   r,      s
   
zMarkupLMIntermediate.__init__r   r   c                 C      |  |}| |}|S r   )r   r   r=   r   r   r   r'   rL         

zMarkupLMIntermediate.forwardr   r   r   r>   r'   r      s    r   c                       r   )MarkupLMOutputc                    sB   t    t|j|j| _tj|j|jd| _t	|j
| _d S r   )r+   r,   r   r.   r   r/   r   r`   ra   r1   r2   r3   r<   r>   r   r'   r,      r   zMarkupLMOutput.__init__r   r   r   c                 C   r   r   r   r   r   r   r'   rL      r   zMarkupLMOutput.forwardr   r   r   r>   r'   r      r   r   c                       r   )MarkupLMPoolerc                    s*   t    t|j|j| _t | _d S r   )r+   r,   r   r.   r/   r   Tanhr5   r<   r>   r   r'   r,      s   
zMarkupLMPooler.__init__r   r   c                 C   s(   |d d df }|  |}| |}|S )Nr   )r   r5   )r=   r   first_token_tensorpooled_outputr   r   r'   rL     s   

zMarkupLMPooler.forwardr   r   r   r>   r'   r      s    r   c                       r   )MarkupLMPredictionHeadTransformc                    sV   t    t|j|j| _t|jtrt	|j | _
n|j| _
tj|j|jd| _d S r   )r+   r,   r   r.   r/   r   r   r   r   r	   transform_act_fnr`   ra   r<   r>   r   r'   r,     s   
z(MarkupLMPredictionHeadTransform.__init__r   r   c                 C   s"   |  |}| |}| |}|S r   )r   r   r`   r   r   r   r'   rL     s   


z'MarkupLMPredictionHeadTransform.forwardr   r   r   r>   r'   r     s    	r   c                       s$   e Zd Z fddZdd Z  ZS )MarkupLMLMPredictionHeadc                    sB   t    t|| _tj|j|jdd| _t	t
|j| _d S )NT)bias)r+   r,   r   	transformr   r.   r/   rY   decoder	ParameterrD   rx   r   r<   r>   r   r'   r,   "  s   

z!MarkupLMLMPredictionHead.__init__c                 C   r   r   )r   r   r   r   r   r'   rL   +  r   z MarkupLMLMPredictionHead.forward)rM   rN   rO   r,   rL   rQ   r   r   r>   r'   r   !  s    	r   c                       r   )MarkupLMOnlyMLMHeadc                    s   t    t|| _d S r   )r+   r,   r   predictionsr<   r>   r   r'   r,   3  s   
zMarkupLMOnlyMLMHead.__init__sequence_outputr   c                 C   s   |  |}|S r   )r   )r=   r   prediction_scoresr   r   r'   rL   7  s   
zMarkupLMOnlyMLMHead.forwardr   r   r   r>   r'   r   2  s    r           modulequerykeyvalueattention_maskscalingr3   c           
      K   s|   t ||dd| }|d ur|| }tjj|dt jd|j}tjj	||| j
d}t ||}	|	dd }	|	|fS )N   r   r@   )rB   rf   )ptrainingr   )rD   matmul	transposer   
functionalsoftmaxfloat32torf   r3   r   
contiguous)
r   r   r   r   r   r   r3   kwargsattn_weightsattn_outputr   r   r'   eager_attention_forward=  s   
r   c                       V   e Zd Z fddZ		ddejdejdB dedB dee	 d	e
ej f
d
dZ  ZS )MarkupLMSelfAttentionc                    s   t    |j|j dkrt|dstd|j d|j d|| _|j| _t|j|j | _| j| j | _	t
|j| j	| _t
|j| j	| _t
|j| j	| _t
|j| _|j| _| jd | _d S )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
ValueErrorr&   rp   attention_head_sizeall_head_sizer   r.   r   r   r   r1   attention_probs_dropout_probr3   attention_dropoutr   r<   r>   r   r'   r,   U  s"   

zMarkupLMSelfAttention.__init__NFr   r   output_attentionsr   r   c                 K   s   |j d d }g |d| jR }| ||dd}| ||dd}| ||dd}	t| j	j
t}
|
| |||	|f| jsIdn| j| jd|\}}|jg |dR   }|ri||f}|S |f}|S )Nr@   r   r   r   )r3   r   )shaper   r   viewr   r   r   r   get_interfacer&   _attn_implementationr   r   r   r   reshaper   )r=   r   r   r   r   rl   hidden_shapequery_states
key_statesvalue_statesattention_interfacer   r   outputsr   r   r'   rL   j  s2   
zMarkupLMSelfAttention.forwardNFrM   rN   rO   r,   rD   r   FloatTensorboolr   r   r{   rL   rQ   r   r   r>   r'   r   T  s    r   c                       r   )MarkupLMAttentionc                    s"   t    t|| _t|| _d S r   )r+   r,   r   r=   r   outputr<   r>   r   r'   r,     s   

zMarkupLMAttention.__init__NFr   r   r   r   r   c                 K   s>   | j |f||d|}| |d |}|f|dd   }|S N)r   r   r   r   )r=   r   )r=   r   r   r   r   self_outputsattention_outputr   r   r   r'   rL     s   zMarkupLMAttention.forwardr   r   r   r   r>   r'   r     s    r   c                       s^   e Zd Z fddZ		ddejdejdB dedB dee	 d	e
ej f
d
dZdd Z  ZS )MarkupLMLayerc                    s:   t    |j| _d| _t|| _t|| _t|| _	d S )Nr   )
r+   r,   chunk_size_feed_forwardseq_len_dimr   	attentionr   intermediater   r   r<   r>   r   r'   r,     s   


zMarkupLMLayer.__init__NFr   r   r   r   r   c           	      K   sN   | j |f||d|}|d }|dd  }t| j| j| j|}|f| }|S r   )r   r   feed_forward_chunkr   r   )	r=   r   r   r   r   self_attention_outputsr   r   layer_outputr   r   r'   rL     s   
zMarkupLMLayer.forwardc                 C   s   |  |}| ||}|S r   )r   r   )r=   r   intermediate_outputr   r   r   r'   r     s   
z MarkupLMLayer.feed_forward_chunkr   )rM   rN   rO   r,   rD   r   r   r   r   r   r{   rL   r   rQ   r   r   r>   r'   r     s     
r   c                       sr   e Zd Z fddZe				ddejdejdB dedB d	edB d
edB de	e
 deej eB fddZ  ZS )MarkupLMEncoderc                    s:   t     | _t fddt jD | _d| _d S )Nc                    s   g | ]}t  qS r   )r   )r#   rJ   r%   r   r'   r(     s    z,MarkupLMEncoder.__init__.<locals>.<listcomp>F)	r+   r,   r&   r   r8   r9   num_hidden_layerslayergradient_checkpointingr<   r>   r%   r'   r,     s   
 
zMarkupLMEncoder.__init__NFTr   r   r   output_hidden_statesreturn_dictr   r   c                 K   s   |rdnd }|r
dnd }t | jD ]"\}	}
|r||f }|
|||fi |}|d }|r3||d f }q|r;||f }t|||dS )Nr   r   r   )last_hidden_stater   
attentions)	enumerater   r   )r=   r   r   r   r   r   r   all_hidden_statesall_self_attentionsrJ   layer_modulelayer_outputsr   r   r'   rL     s.   


zMarkupLMEncoder.forward)NFFT)rM   rN   rO   r,   r   rD   r   r   r   r   r   r{   r   rL   rQ   r   r   r>   r'   r     s,    r   c                       s2   e Zd ZU eed< dZe  fddZ  Z	S )MarkupLMPreTrainedModelr&   markuplmc                    sZ   t  | t|trt|j dS t|tr+t|j	t
|j	jd d dS dS )zInitialize the weightsr@   rW   N)r+   _init_weightsr   r   initzeros_r   rR   copy_rV   rD   rc   r   rd   )r=   r   r>   r   r'   r     s   

&z%MarkupLMPreTrainedModel._init_weights)
rM   rN   rO   r   __annotations__base_model_prefixrD   no_gradr   rQ   r   r   r>   r'   r     s
   
 r   c                       s   e Zd Zd fdd	Zdd Zdd Zee										dd	ej	dB d
ej	dB dej	dB dej
dB dej	dB dej	dB dej
dB dedB dedB dedB deeB fddZ  ZS )MarkupLMModelTc                    sD   t  | || _t|| _t|| _|rt|nd| _| 	  dS )zv
        add_pooling_layer (bool, *optional*, defaults to `True`):
            Whether to add a pooling layer
        N)
r+   r,   r&   rR   r   r   encoderr   pooler	post_init)r=   r&   add_pooling_layerr>   r   r'   r,     s   

zMarkupLMModel.__init__c                 C   s   | j jS r   r   r[   )r=   r   r   r'   get_input_embeddings  s   z"MarkupLMModel.get_input_embeddingsc                 C   s   || j _d S r   r  )r=   r   r   r   r'   set_input_embeddings  s   z"MarkupLMModel.set_input_embeddingsNrs   rF   rG   r   r~   rV   rk   r   r   r   r   c                 K   s`  |dur|n| j j}|	dur|	n| j j}	|
dur|
n| j j}
|dur*|dur*td|dur9| || | }n|durF| dd }ntd|durQ|jn|j}|du r_tj	||d}|du rltj
|tj|d}|dd}|j| jd	}d
| d }| j||||||d}| j||||	dd}|d }| jdur| |nd}t|||j|jd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)rg   re   r   r   )rf   g      ?g     )rs   rF   rG   rV   r~   rk   T)r   r   r   r   )r   pooler_outputr   r   )r&   r   r   use_return_dictr   %warn_if_padding_and_no_attention_maskrh   rg   rD   rz   rx   ri   rj   r   rf   r   r  r  r   r   r   )r=   rs   rF   rG   r   r~   rV   rk   r   r   r   r   rl   rg   extended_attention_maskembedding_outputencoder_outputsr   r   r   r   r'   rL   "  sV   '
zMarkupLMModel.forward)T)
NNNNNNNNNN)rM   rN   rO   r,   r  r  r   r   rD   
LongTensorr   r   r{   r   rL   rQ   r   r   r>   r'   r   	  sN    	
r   c                       s   e Zd Z fddZee												ddejdB dejdB dejdB dejdB dejdB d	ejdB d
ejdB dejdB dejdB dedB dedB dedB de	ej e
B fddZ  ZS )MarkupLMForQuestionAnsweringc                    s@   t  | |j| _t|dd| _t|j|j| _| 	  d S NF)r  )
r+   r,   
num_labelsr   r   r   r.   r/   
qa_outputsr  r<   r>   r   r'   r,     s
   z%MarkupLMForQuestionAnswering.__init__Nrs   rF   rG   r   r~   rV   rk   start_positionsend_positionsr   r   r   r   c                 K   s  |dur|n| j j}| j||||||||
|dd
}|d }| |}|jddd\}}|d }|d }d}|dur|	durt| dkrP|d}t|	 dkr]|	d}	|d}|	d| |		d| t
|d}|||}|||	}|| d	 }t||||j|jd
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	rF   rG   r   r~   rV   rk   r   r   r   r   r   r@   rA   )ignore_indexr   )lossstart_logits
end_logitsr   r   )r&   r	  r   r  splitsqueezer   lenrh   clamp_r   r   r   r   )r=   rs   rF   rG   r   r~   rV   rk   r  r  r   r   r   r   r   r   logitsr  r  
total_lossignored_indexloss_fct
start_lossend_lossr   r   r'   rL     sL   0






z$MarkupLMForQuestionAnswering.forward)NNNNNNNNNNNN)rM   rN   rO   r,   r   r   rD   r   r   r{   r   rL   rQ   r   r   r>   r'   r    sV    
	
r  zC
    MarkupLM Model with a `token_classification` head on top.
    )custom_introc                          e Zd Z fddZee											ddejdB dejdB dejdB dejdB dejdB d	ejdB d
ejdB dejdB dedB dedB dedB de	ej e
B fddZ  ZS )MarkupLMForTokenClassificationc                    sb   t  | |j| _t|dd| _|jd ur|jn|j}t|| _	t
|j|j| _|   d S r  )r+   r,   r  r   r   classifier_dropoutr2   r   r1   r3   r.   r/   
classifierr  r=   r&   r'  r>   r   r'   r,     s   z'MarkupLMForTokenClassification.__init__Nrs   rF   rG   r   r~   rV   rk   labelsr   r   r   r   c                 K   s   |dur|n| j j}| j||||||||	|
dd
}|d }| |}d}|dur9t }||d| j j|d}t|||j|j	d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@   r  r  r   r   )
r&   r	  r   r(  r   r   r  r   r   r   )r=   rs   rF   rG   r   r~   rV   rk   r*  r   r   r   r   r   r   r   r  r!  r   r   r'   rL     s8   .
z&MarkupLMForTokenClassification.forwardNNNNNNNNNNN)rM   rN   rO   r,   r   r   rD   r   r   r{   r   rL   rQ   r   r   r>   r'   r&    sP    	
r&  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                       r%  )!MarkupLMForSequenceClassificationc                    sd   t  | |j| _|| _t|| _|jd ur|jn|j}t	|| _
t|j|j| _|   d S r   )r+   r,   r  r&   r   r   r'  r2   r   r1   r3   r.   r/   r(  r  r)  r>   r   r'   r,   Y  s   
z*MarkupLMForSequenceClassification.__init__Nrs   rF   rG   r   r~   rV   rk   r*  r   r   r   r   c                 K   sH  |dur|n| j j}| j||||||||	|
dd
}|d }| |}| |}d}|dur| j jdu rW| jdkr=d| j _n| jdkrS|jtj	ksN|jtj
krSd| j _nd| j _| j jdkrut }| jdkro|| | }n+|||}n%| j jdkrt }||d| j|d}n| j jdkrt }|||}t|||j|jd	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@   r+  )r&   r	  r   r3   r(  problem_typer  rf   rD   ri   rp   r   r  r   r   r   r   r   r   )r=   rs   rF   rG   r   r~   rV   rk   r*  r   r   r   r   r   r   r  r  r!  r   r   r'   rL   h  sR   -



"


z)MarkupLMForSequenceClassification.forwardr,  )rM   rN   rO   r,   r   r   rD   r   r   r{   r   rL   rQ   r   r   r>   r'   r-  Q  sP    	
r-  )r  r-  r&  r   r   )r   )?rP   collections.abcr   rD   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_loggerrM   loggerModuler   rR   r   r   r   r   r   r   r   r   floatr   r   r   r   r   r   r   r  r&  r-  __all__r   r   r   r'   <module>   sv    
2t
9'-ul`q