


import argparse
import json
import os

import fairseq
import torch
from fairseq.data import Dictionary

# Register SEW's fairseq modules
from sew_asapp import tasks  # noqa: F401

from transformers import (
    SEWDConfig,
    SEWDForCTC,
    SEWDModel,
    WavaVecaCTCTokenizer,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
    logging,
)


logging.set_verbosity_info()
A :       Any						=   logging.get_logger(__name__)

A :       Optional[int]						=   {
    "post_extract_proj": "feature_projection",
    "encoder.pos_conv.0": "encoder.pos_conv_embed.conv",
    "attention.self.query_proj": "encoder.encoder.layer.*.attention.self.query_proj",
    "attention.self.key_proj": "encoder.encoder.layer.*.attention.self.key_proj",
    "attention.self.value_proj": "encoder.encoder.layer.*.attention.self.value_proj",
    "attention.output.dense": "encoder.encoder.layer.*.attention.output.dense",
    "attention.output.LayerNorm": "encoder.encoder.layer.*.attention.output.LayerNorm",
    "intermediate.dense": "encoder.encoder.layer.*.intermediate.dense",
    "output.dense": "encoder.encoder.layer.*.output.dense",
    "output.LayerNorm": "encoder.encoder.layer.*.output.LayerNorm",
    "encoder.encoder.rel_embeddings": "encoder.encoder.rel_embeddings",
    "encoder.encoder.LayerNorm": "encoder.encoder.LayerNorm",
    "encoder.upsample.0": "encoder.upsample.projection",
    "encoder.layer_norm": "encoder.layer_norm",
    "w2v_model.layer_norm": "layer_norm",
    "w2v_encoder.proj": "lm_head",
    "mask_emb": "masked_spec_embed",
}

def  lowercase_						(						_A     :       List[str]       ,   _A     :       Union[str, Any]       ,   _A     :       Optional[Any]       ,   _A     :       Union[str, Any]       ,   _A     :       Tuple		):

      """simple docstring"""




      for attribute in key.split("."		):
            lowerCamelCase__						:							int       =    getattr(_A       ,   _A		)

      if weight_type is not None:
            lowerCamelCase__						:							str       =    getattr(_A       ,   _A		).shape
      else:
            lowerCamelCase__						:							Any       =    hf_pointer.shape

      assert hf_shape == value.shape, (
          F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be"
          F" {value.shape} for {full_name}"
      )

      if weight_type == "weight":
            lowerCamelCase__						:							List[Any]       =    value
      elif weight_type == "weight_g":
            lowerCamelCase__						:							Dict       =    value
      elif weight_type == "weight_v":
            lowerCamelCase__						:							List[Any]       =    value
      elif weight_type == "bias":
            lowerCamelCase__						:							Any       =    value
      else:
            lowerCamelCase__						:							Optional[Any]       =    value

      logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}."		)

def  lowercase_						(						_A     :       Optional[int]       ,   _A     :       Any       ,   _A     :       Tuple		):

      """simple docstring"""




      lowerCamelCase__						:							int       =    []
      lowerCamelCase__						:							Optional[Any]       =    fairseq_model.state_dict()

      lowerCamelCase__						:							Any       =    hf_model.sew_d.feature_extractor if is_finetuned else hf_model.feature_extractor

      for name, value in fairseq_dict.items():
            lowerCamelCase__						:							Dict       =    False
            if "conv_layers" in name:
                  load_conv_layer(
                      _A       ,   _A       ,   _A       ,   _A       ,   hf_model.config.feat_extract_norm == "group"       ,   )
                  lowerCamelCase__						:							Optional[Any]       =    True
            else:
                  for key, mapped_key in MAPPING.items():
                        lowerCamelCase__						:							Tuple       =    "sew_d." + mapped_key if (is_finetuned and mapped_key != "lm_head") else mapped_key

                        if key in name or key.split("w2v_model."		)[-1] == name.split("."		)[0]:
                              lowerCamelCase__						:							List[str]       =    True
                              if "*" in mapped_key:
                                    lowerCamelCase__						:							str       =    name.split(_A		)[0].split("."		)[-2]
                                    if not layer_index.isnumeric():
                                          continue
                                    lowerCamelCase__						:							Tuple       =    mapped_key.replace("*"       ,   _A		)
                              if "weight_g" in name:
                                    lowerCamelCase__						:							List[Any]       =    "weight_g"
                              elif "weight_v" in name:
                                    lowerCamelCase__						:							Dict       =    "weight_v"
                              elif "weight" in name:
                                    lowerCamelCase__						:							Optional[int]       =    "weight"
                              elif "bias" in name:
                                    lowerCamelCase__						:							Optional[Any]       =    "bias"
                              else:
                                    lowerCamelCase__						:							int       =    None
                              set_recursively(_A       ,   _A       ,   _A       ,   _A       ,   _A		)
                        continue
            if not is_used:
                  unused_weights.append(_A		)

      logger.warning(F"Unused weights: {unused_weights}"		)

def  lowercase_						(						_A     :       Optional[int]       ,   _A     :       List[Any]       ,   _A     :       Tuple       ,   _A     :       str       ,   _A     :       List[Any]		):

      """simple docstring"""




      lowerCamelCase__						:							str       =    full_name.split("conv_layers."		)[-1]
      lowerCamelCase__						:							Optional[int]       =    name.split("."		)
      lowerCamelCase__						:							Optional[Any]       =    int(items[0]		)
      lowerCamelCase__						:							Any       =    int(items[1]		)

      if type_id == 0:
            if "bias" in name:
                  assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, (
                      F"{full_name} has size {value.shape}, but"
                      F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found."
                  )
                  lowerCamelCase__						:							str       =    value
                  logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}."		)
            elif "weight" in name:
                  assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, (
                      F"{full_name} has size {value.shape}, but"
                      F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found."
                  )
                  lowerCamelCase__						:							List[str]       =    value
                  logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}."		)
      elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
            if "bias" in name:
                  assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, (
                      F"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was"
                      " found."
                  )
                  lowerCamelCase__						:							Optional[Any]       =    value
                  logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."		)
            elif "weight" in name:
                  assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, (
                      F"{full_name} has size {value.shape}, but"
                      F" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found."
                  )
                  lowerCamelCase__						:							str       =    value
                  logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."		)
      else:
            unused_weights.append(_A		)

def  lowercase_						(						_A     :       Any       ,   _A     :       Optional[Any]		):

      """simple docstring"""




      lowerCamelCase__						:							str       =    SEWDConfig()
      if is_finetuned:
            lowerCamelCase__						:							Any       =    model.wav_encoder.wav_model.cfg
      else:
            lowerCamelCase__						:							Dict       =    model.cfg

      lowerCamelCase__						:							int       =    fs_config.conv_bias
      lowerCamelCase__						:							List[str]       =    eval(fs_config.conv_feature_layers		)
      lowerCamelCase__						:							Dict       =    [x[0] for x in conv_layers]
      lowerCamelCase__						:							Any       =    [x[1] for x in conv_layers]
      lowerCamelCase__						:							List[Any]       =    [x[2] for x in conv_layers]
      lowerCamelCase__						:							List[str]       =    "gelu"
      lowerCamelCase__						:							Dict       =    "layer" if fs_config.extractor_mode == "layer_norm" else "group"
      lowerCamelCase__						:							str       =    0.0
      lowerCamelCase__						:							int       =    fs_config.activation_fn.name
      lowerCamelCase__						:							int       =    fs_config.encoder_embed_dim
      lowerCamelCase__						:							List[Any]       =    0.02
      lowerCamelCase__						:							Dict       =    fs_config.encoder_ffn_embed_dim
      lowerCamelCase__						:							Union[str, Any]       =    1E-5
      lowerCamelCase__						:							Optional[Any]       =    fs_config.encoder_layerdrop
      lowerCamelCase__						:							Any       =    fs_config.encoder_attention_heads
      lowerCamelCase__						:							int       =    fs_config.conv_pos_groups
      lowerCamelCase__						:							Dict       =    fs_config.conv_pos
      lowerCamelCase__						:							Dict       =    len(_A		)
      lowerCamelCase__						:							Optional[int]       =    fs_config.encoder_layers
      lowerCamelCase__						:							str       =    fs_config.squeeze_factor
      # DeBERTa-specific parameters:
      lowerCamelCase__						:							Dict       =    fs_config.max_position_embeddings
      lowerCamelCase__						:							Any       =    fs_config.position_buckets
      lowerCamelCase__						:							Union[str, Any]       =    fs_config.share_att_key
      lowerCamelCase__						:							Optional[int]       =    fs_config.relative_attention
      lowerCamelCase__						:							List[str]       =    fs_config.position_biased_input
      lowerCamelCase__						:							Dict       =    tuple(fs_config.pos_att_type.split("|"		)		)
      lowerCamelCase__						:							Any       =    fs_config.norm_rel_ebd

      # take care of any params that are overridden by the Wav2VecCtc model
      if is_finetuned:
            lowerCamelCase__						:							Optional[Any]       =    model.cfg
            lowerCamelCase__						:							Optional[int]       =    fs_config.final_dropout
            lowerCamelCase__						:							Any       =    fs_config.layerdrop
      lowerCamelCase__						:							Optional[int]       =    fs_config.activation_dropout
      lowerCamelCase__						:							Optional[int]       =    fs_config.mask_prob > 0 or fs_config.mask_channel_prob > 0
      lowerCamelCase__						:							int       =    fs_config.attention_dropout
      lowerCamelCase__						:							str       =    fs_config.dropout_input
      lowerCamelCase__						:							Dict       =    fs_config.dropout
      lowerCamelCase__						:							Optional[int]       =    fs_config.mask_channel_length
      lowerCamelCase__						:							List[Any]       =    fs_config.mask_channel_prob
      lowerCamelCase__						:							Union[str, Any]       =    fs_config.mask_length
      lowerCamelCase__						:							Optional[Any]       =    fs_config.mask_prob

      lowerCamelCase__						:							int       =    "Wav2Vec2FeatureExtractor"
      lowerCamelCase__						:							Any       =    "Wav2Vec2CTCTokenizer"

      return config

@torch.no_grad()
def  lowercase_						(						_A     :       Optional[Any]       ,   _A     :       Optional[Any]       ,   _A     :       Dict=None       ,   _A     :       Dict=None       ,   _A     :       Tuple=True		):

      """simple docstring"""




      if is_finetuned:
            lowerCamelCase__       ,  lowerCamelCase__       ,  lowerCamelCase__						:							Union[str, Any]       =    fairseq.checkpoint_utils.load_model_ensemble_and_task(
                [checkpoint_path]       ,   arg_overrides={"data": "/".join(dict_path.split("/"		)[:-1]		)}		)
      else:
            lowerCamelCase__       ,  lowerCamelCase__       ,  lowerCamelCase__						:							Union[str, Any]       =    fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path]		)

      if config_path is not None:
            lowerCamelCase__						:							Tuple       =    SEWDConfig.from_pretrained(_A		)
      else:
            lowerCamelCase__						:							int       =    convert_config(model[0]       ,   _A		)
      lowerCamelCase__						:							Dict       =    model[0].eval()

      lowerCamelCase__						:							int       =    True if config.feat_extract_norm == "layer" else False
      lowerCamelCase__						:							int       =    WavaVecaFeatureExtractor(
          feature_size=1       ,   sampling_rate=16000       ,   padding_value=0       ,   do_normalize=_A       ,   return_attention_mask=_A       ,   )

      if is_finetuned:
            if dict_path:
                  lowerCamelCase__						:							str       =    Dictionary.load(_A		)

                  # important change bos & pad token id since CTC symbol is <pad> and
                  # not <s> as in fairseq
                  lowerCamelCase__						:							Optional[Any]       =    target_dict.pad_index
                  lowerCamelCase__						:							Union[str, Any]       =    target_dict.bos_index
                  lowerCamelCase__						:							Any       =    target_dict.pad_index
                  lowerCamelCase__						:							Optional[Any]       =    target_dict.bos_index
                  lowerCamelCase__						:							List[Any]       =    target_dict.eos_index
                  lowerCamelCase__						:							Optional[Any]       =    len(target_dict.symbols		)
                  lowerCamelCase__						:							str       =    os.path.join(_A       ,   "vocab.json"		)
                  if not os.path.isdir(_A		):
                        logger.error("--pytorch_dump_folder_path ({}) should be a directory".format(_A		)		)
                        return
                  os.makedirs(_A       ,   exist_ok=_A		)
                  with open(_A       ,   "w"       ,   encoding="utf-8"		) as vocab_handle:
                        json.dump(target_dict.indices       ,   _A		)
                  lowerCamelCase__						:							int       =    WavaVecaCTCTokenizer(
                      _A       ,   unk_token=target_dict.unk_word       ,   pad_token=target_dict.pad_word       ,   bos_token=target_dict.bos_word       ,   eos_token=target_dict.eos_word       ,   word_delimiter_token="|"       ,   do_lower_case=_A       ,   )
                  lowerCamelCase__						:							int       =    WavaVecaProcessor(feature_extractor=_A       ,   tokenizer=_A		)
                  processor.save_pretrained(_A		)

            lowerCamelCase__						:							Optional[int]       =    SEWDForCTC(_A		)
      else:
            lowerCamelCase__						:							List[str]       =    SEWDModel(_A		)
            feature_extractor.save_pretrained(_A		)

      recursively_load_weights(_A       ,   _A       ,   _A		)

      hf_model.save_pretrained(_A		)


if __name__ == "__main__":
    A :       int						=   argparse.ArgumentParser()
    parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
    parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint")
    parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model")
    parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert")
    parser.add_argument(
        "--is_finetuned", action="store_true", help="Whether the model to convert is a fine-tuned model or not"
    )
    A :       List[Any]						=   parser.parse_args()
    convert_sew_checkpoint(
        args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, args.is_finetuned
    )






import unittest

from transformers import MarkupLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        MarkupLMForQuestionAnswering,
        MarkupLMForSequenceClassification,
        MarkupLMForTokenClassification,
        MarkupLMModel,
    )

# TODO check dependencies
from transformers import MarkupLMFeatureExtractor, MarkupLMProcessor, MarkupLMTokenizer







class      _lowercase					:


       """simple docstring"""




       def __init__(  self				:     Dict   , __lowerCamelCase				:     Optional[Any]   , __lowerCamelCase				:     Union[str, Any]=13   , __lowerCamelCase				:     Optional[Any]=7   , __lowerCamelCase				:     List[str]=True   , __lowerCamelCase				:     str=True   , __lowerCamelCase				:     List[str]=True   , __lowerCamelCase				:     List[str]=True   , __lowerCamelCase				:     Optional[int]=99   , __lowerCamelCase				:     Dict=32   , __lowerCamelCase				:     List[str]=5   , __lowerCamelCase				:     str=4   , __lowerCamelCase				:     str=37   , __lowerCamelCase				:     Any="gelu"   , __lowerCamelCase				:     Dict=0.1   , __lowerCamelCase				:     Optional[Any]=0.1   , __lowerCamelCase				:     List[str]=512   , __lowerCamelCase				:     str=16   , __lowerCamelCase				:     List[Any]=2   , __lowerCamelCase				:     int=0.0_2   , __lowerCamelCase				:     Any=3   , __lowerCamelCase				:     Optional[int]=None   , __lowerCamelCase				:     str=20   , __lowerCamelCase				:     Union[str, Any]=30   , __lowerCamelCase				:     str=2   , __lowerCamelCase				:     Dict=2   , __lowerCamelCase				:     str=10   , ):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[int]       =    parent
             lowerCamelCase__						:							Optional[Any]       =    batch_size
             lowerCamelCase__						:							Union[str, Any]       =    seq_length
             lowerCamelCase__						:							Union[str, Any]       =    is_training
             lowerCamelCase__						:							Tuple       =    use_input_mask
             lowerCamelCase__						:							Tuple       =    use_token_type_ids
             lowerCamelCase__						:							Union[str, Any]       =    use_labels
             lowerCamelCase__						:							str       =    vocab_size
             lowerCamelCase__						:							int       =    hidden_size
             lowerCamelCase__						:							List[Any]       =    num_hidden_layers
             lowerCamelCase__						:							List[Any]       =    num_attention_heads
             lowerCamelCase__						:							Optional[Any]       =    intermediate_size
             lowerCamelCase__						:							int       =    hidden_act
             lowerCamelCase__						:							Optional[Any]       =    hidden_dropout_prob
             lowerCamelCase__						:							Optional[Any]       =    attention_probs_dropout_prob
             lowerCamelCase__						:							Optional[int]       =    max_position_embeddings
             lowerCamelCase__						:							Optional[Any]       =    type_vocab_size
             lowerCamelCase__						:							Tuple       =    type_sequence_label_size
             lowerCamelCase__						:							List[Any]       =    initializer_range
             lowerCamelCase__						:							str       =    num_labels
             lowerCamelCase__						:							Dict       =    scope
             lowerCamelCase__						:							List[Any]       =    max_xpath_tag_unit_embeddings
             lowerCamelCase__						:							str       =    max_xpath_subs_unit_embeddings
             lowerCamelCase__						:							Optional[Any]       =    tag_pad_id
             lowerCamelCase__						:							Optional[Any]       =    subs_pad_id
             lowerCamelCase__						:							Optional[int]       =    max_depth





       def 		lowerCAmelCase	(  self				:     int			):


             '''simple docstring'''



             lowerCamelCase__						:							Dict       =    ids_tensor([self.batch_size, self.seq_length]   , self.vocab_size			)

             lowerCamelCase__						:							List[Any]       =    ids_tensor(
                 [self.batch_size, self.seq_length, self.max_depth]   , self.max_xpath_tag_unit_embeddings			)

             lowerCamelCase__						:							int       =    ids_tensor(
                 [self.batch_size, self.seq_length, self.max_depth]   , self.max_xpath_subs_unit_embeddings			)

             lowerCamelCase__						:							List[Any]       =    None
             if self.use_input_mask:
                   lowerCamelCase__						:							Tuple       =    ids_tensor([self.batch_size, self.seq_length]   , vocab_size=2			)

             lowerCamelCase__						:							int       =    None
             if self.use_token_type_ids:
                   lowerCamelCase__						:							Union[str, Any]       =    ids_tensor([self.batch_size, self.seq_length]   , self.type_vocab_size			)

             lowerCamelCase__						:							Any       =    None
             lowerCamelCase__						:							str       =    None
             if self.use_labels:
                   lowerCamelCase__						:							Dict       =    ids_tensor([self.batch_size]   , self.type_sequence_label_size			)
                   lowerCamelCase__						:							Optional[Any]       =    ids_tensor([self.batch_size, self.seq_length]   , self.num_labels			)

             lowerCamelCase__						:							int       =    self.get_config()

             return (
                 config,
                 input_ids,
                 xpath_tags_seq,
                 xpath_subs_seq,
                 token_type_ids,
                 input_mask,
                 sequence_labels,
                 token_labels,
             )





       def 		lowerCAmelCase	(  self				:     int			):


             '''simple docstring'''



             return MarkupLMConfig(
                 vocab_size=self.vocab_size   , hidden_size=self.hidden_size   , num_hidden_layers=self.num_hidden_layers   , num_attention_heads=self.num_attention_heads   , intermediate_size=self.intermediate_size   , hidden_act=self.hidden_act   , hidden_dropout_prob=self.hidden_dropout_prob   , attention_probs_dropout_prob=self.attention_probs_dropout_prob   , max_position_embeddings=self.max_position_embeddings   , type_vocab_size=self.type_vocab_size   , initializer_range=self.initializer_range   , max_xpath_tag_unit_embeddings=self.max_xpath_tag_unit_embeddings   , max_xpath_subs_unit_embeddings=self.max_xpath_subs_unit_embeddings   , tag_pad_id=self.tag_pad_id   , subs_pad_id=self.subs_pad_id   , max_depth=self.max_depth   , )





       def 		lowerCAmelCase	(  self				:     Optional[Any]   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     Optional[int]   , __lowerCamelCase				:     str   , __lowerCamelCase				:     Tuple   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     Any   , __lowerCamelCase				:     str   , ):


             '''simple docstring'''



             lowerCamelCase__						:							List[Any]       =    MarkupLMModel(config=__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             print("Configs:"   , model.config.tag_pad_id   , model.config.subs_pad_id			)
             lowerCamelCase__						:							Optional[int]       =    model(__lowerCamelCase   , attention_mask=__lowerCamelCase   , token_type_ids=__lowerCamelCase			)
             lowerCamelCase__						:							List[str]       =    model(__lowerCamelCase   , token_type_ids=__lowerCamelCase			)
             lowerCamelCase__						:							str       =    model(__lowerCamelCase			)
             self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, self.seq_length, self.hidden_size)			)
             self.parent.assertEqual(result.pooler_output.shape   , (self.batch_size, self.hidden_size)			)





       def 		lowerCAmelCase	(  self				:     Dict   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     Tuple   , __lowerCamelCase				:     int   , __lowerCamelCase				:     Dict   , __lowerCamelCase				:     Optional[Any]   , __lowerCamelCase				:     Tuple   , __lowerCamelCase				:     Optional[Any]   , __lowerCamelCase				:     List[Any]   , ):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[Any]       =    self.num_labels
             lowerCamelCase__						:							str       =    MarkupLMForSequenceClassification(__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							Optional[int]       =    model(
                 __lowerCamelCase   , xpath_tags_seq=__lowerCamelCase   , xpath_subs_seq=__lowerCamelCase   , attention_mask=__lowerCamelCase   , token_type_ids=__lowerCamelCase   , labels=__lowerCamelCase   , )
             self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.num_labels)			)





       def 		lowerCAmelCase	(  self				:     Any   , __lowerCamelCase				:     Optional[int]   , __lowerCamelCase				:     Any   , __lowerCamelCase				:     Any   , __lowerCamelCase				:     Any   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     int   , __lowerCamelCase				:     str   , ):


             '''simple docstring'''



             lowerCamelCase__						:							List[str]       =    self.num_labels
             lowerCamelCase__						:							Tuple       =    MarkupLMForTokenClassification(config=__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							List[Any]       =    model(
                 __lowerCamelCase   , xpath_tags_seq=__lowerCamelCase   , xpath_subs_seq=__lowerCamelCase   , attention_mask=__lowerCamelCase   , token_type_ids=__lowerCamelCase   , labels=__lowerCamelCase   , )
             self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.seq_length, self.num_labels)			)





       def 		lowerCAmelCase	(  self				:     Tuple   , __lowerCamelCase				:     int   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     Optional[int]   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     Dict   , __lowerCamelCase				:     Optional[int]   , __lowerCamelCase				:     Any   , ):


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    MarkupLMForQuestionAnswering(config=__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							Union[str, Any]       =    model(
                 __lowerCamelCase   , xpath_tags_seq=__lowerCamelCase   , xpath_subs_seq=__lowerCamelCase   , attention_mask=__lowerCamelCase   , token_type_ids=__lowerCamelCase   , start_positions=__lowerCamelCase   , end_positions=__lowerCamelCase   , )
             self.parent.assertEqual(result.start_logits.shape   , (self.batch_size, self.seq_length)			)
             self.parent.assertEqual(result.end_logits.shape   , (self.batch_size, self.seq_length)			)





       def 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    self.prepare_config_and_inputs()
             (
                 (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  
             )						:							Optional[int]       =    config_and_inputs
             lowerCamelCase__						:							List[str]       =    {
                 "input_ids": input_ids,
                 "xpath_tags_seq": xpath_tags_seq,
                 "xpath_subs_seq": xpath_subs_seq,
                 "token_type_ids": token_type_ids,
                 "attention_mask": input_mask,
             }
             return config, inputs_dict







@require_torch
class      _lowercase					(				lowercase__				,    lowercase__				,    unittest.TestCase):


       """simple docstring"""




       A__   		=     (
           (
               MarkupLMModel,
               MarkupLMForSequenceClassification,
               MarkupLMForTokenClassification,
               MarkupLMForQuestionAnswering,
           )
           if is_torch_available()
           else None
       )
       A__   		=     (
           {
               "feature-extraction": MarkupLMModel,
               "question-answering": MarkupLMForQuestionAnswering,
               "text-classification": MarkupLMForSequenceClassification,
               "token-classification": MarkupLMForTokenClassification,
               "zero-shot": MarkupLMForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )





       def 		lowerCAmelCase	(  self				:     int   , __lowerCamelCase				:     Optional[Any]   , __lowerCamelCase				:     int   , __lowerCamelCase				:     Any   , __lowerCamelCase				:     Any   , __lowerCamelCase				:     List[str]			):


             '''simple docstring'''



             return True





       def 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             lowerCamelCase__						:							Any       =    MarkupLMModelTester(self			)
             lowerCamelCase__						:							Any       =    ConfigTester(self   , config_class=__lowerCamelCase   , hidden_size=37			)





       def 		lowerCAmelCase	(  self				:     Dict			):


             '''simple docstring'''



             self.config_tester.run_common_tests()





       def 		lowerCAmelCase	(  self				:     List[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     int			):


             '''simple docstring'''



             lowerCamelCase__						:							Dict       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[Any]       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     List[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							List[Any]       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase			)



def  lowercase_						(						):

      """simple docstring"""




      lowerCamelCase__						:							int       =    "\n    <!DOCTYPE html>\n    <html>\n    <head>\n    <title>Page Title</title>\n    </head>\n    <body>\n\n    <h1>This is a Heading</h1>\n    <p>This is a paragraph.</p>\n\n    </body>\n    </html>\n    "

      return html_string







@require_torch
class      _lowercase					(				unittest.TestCase):


       """simple docstring"""




       @cached_property
       def 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    MarkupLMFeatureExtractor()
             lowerCamelCase__						:							Union[str, Any]       =    MarkupLMTokenizer.from_pretrained("microsoft/markuplm-base"			)

             return MarkupLMProcessor(__lowerCamelCase   , __lowerCamelCase			)





       @slow
       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    MarkupLMModel.from_pretrained("microsoft/markuplm-base"			).to(__lowerCamelCase			)

             lowerCamelCase__						:							Dict       =    self.default_processor

             lowerCamelCase__						:							Dict       =    processor(prepare_html_string()   , return_tensors="pt"			)
             lowerCamelCase__						:							Optional[Any]       =    inputs.to(__lowerCamelCase			)

             # forward pass
             with torch.no_grad():
                   lowerCamelCase__						:							Union[str, Any]       =    model(**__lowerCamelCase			)

             # verify the last hidden states
             lowerCamelCase__						:							Dict       =    torch.Size([1, 14, 768]			)
             self.assertEqual(outputs.last_hidden_state.shape   , __lowerCamelCase			)

             lowerCamelCase__						:							Dict       =    torch.tensor(
                 [[0.0_6_7_5, -0.0_0_5_2, 0.5_0_0_1], [-0.2_2_8_1, 0.0_8_0_2, 0.2_1_9_2], [-0.0_5_8_3, -0.3_3_1_1, 0.1_1_8_5]]			).to(__lowerCamelCase			)

             self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]   , __lowerCamelCase   , atol=1E-4			)			)


