





'''simple docstring'''






import collections
import inspect
import unittest
from typing import Dict, List, Tuple

from transformers import MaskFormerSwinConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device
from transformers.utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch
      from torch import nn

      from transformers import MaskFormerSwinBackbone
      from transformers.models.maskformer import MaskFormerSwinModel







class 		UpperCAmelCase   :


  """simple docstring"""







  def __init__(				self    :					Optional[int]							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					Optional[int]=13							,	UpperCamelCase__    :					str=32							,	UpperCamelCase__    :					Any=2							,	UpperCamelCase__    :					Dict=3							,	UpperCamelCase__    :					str=16							,	UpperCamelCase__    :					Union[str, Any]=[1, 2, 1]							,	UpperCamelCase__    :					List[str]=[2, 2, 4]							,	UpperCamelCase__    :					Tuple=2							,	UpperCamelCase__    :					Tuple=2.0							,	UpperCamelCase__    :					Optional[Any]=True							,	UpperCamelCase__    :					Dict=0.0							,	UpperCamelCase__    :					Tuple=0.0							,	UpperCamelCase__    :					List[str]=0.1							,	UpperCamelCase__    :					str="gelu"							,	UpperCamelCase__    :					Dict=False							,	UpperCamelCase__    :					Tuple=True							,	UpperCamelCase__    :					str=0.02							,	UpperCamelCase__    :					List[str]=1E-5							,	UpperCamelCase__    :					Any=True							,	UpperCamelCase__    :					Optional[Any]=None							,	UpperCamelCase__    :					Optional[Any]=True							,	UpperCamelCase__    :					Dict=10							,	UpperCamelCase__    :					Dict=8							,	UpperCamelCase__    :					str=["stage1", "stage2", "stage3"]							,	UpperCamelCase__    :					List[str]=[1, 2, 3]							,	)     ->						Any:
    _UpperCamelCase       			=parent
    _UpperCamelCase       			=batch_size
    _UpperCamelCase       			=image_size
    _UpperCamelCase       			=patch_size
    _UpperCamelCase       			=num_channels
    _UpperCamelCase       			=embed_dim
    _UpperCamelCase       			=depths
    _UpperCamelCase       			=num_heads
    _UpperCamelCase       			=window_size
    _UpperCamelCase       			=mlp_ratio
    _UpperCamelCase       			=qkv_bias
    _UpperCamelCase       			=hidden_dropout_prob
    _UpperCamelCase       			=attention_probs_dropout_prob
    _UpperCamelCase       			=drop_path_rate
    _UpperCamelCase       			=hidden_act
    _UpperCamelCase       			=use_absolute_embeddings
    _UpperCamelCase       			=patch_norm
    _UpperCamelCase       			=layer_norm_eps
    _UpperCamelCase       			=initializer_range
    _UpperCamelCase       			=is_training
    _UpperCamelCase       			=scope
    _UpperCamelCase       			=use_labels
    _UpperCamelCase       			=type_sequence_label_size
    _UpperCamelCase       			=encoder_stride
    _UpperCamelCase       			=out_features
    _UpperCamelCase       			=out_indices


  def   UpperCamelCase__      (				self    :					str  )     ->						Union[str, Any]:
    _UpperCamelCase       			=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

    _UpperCamelCase       			=None
    if self.use_labels:
      _UpperCamelCase       			=ids_tensor([self.batch_size]							,	self.type_sequence_label_size  )

    _UpperCamelCase       			=self.get_config()

    return config, pixel_values, labels


  def   UpperCamelCase__      (				self    :					Any  )     ->						Optional[Any]:
    return MaskFormerSwinConfig(
        image_size=self.image_size							,	patch_size=self.patch_size							,	num_channels=self.num_channels							,	embed_dim=self.embed_dim							,	depths=self.depths							,	num_heads=self.num_heads							,	window_size=self.window_size							,	mlp_ratio=self.mlp_ratio							,	qkv_bias=self.qkv_bias							,	hidden_dropout_prob=self.hidden_dropout_prob							,	attention_probs_dropout_prob=self.attention_probs_dropout_prob							,	drop_path_rate=self.drop_path_rate							,	hidden_act=self.hidden_act							,	use_absolute_embeddings=self.use_absolute_embeddings							,	path_norm=self.patch_norm							,	layer_norm_eps=self.layer_norm_eps							,	initializer_range=self.initializer_range							,	encoder_stride=self.encoder_stride							,	out_features=self.out_features							,	out_indices=self.out_indices							,	)


  def   UpperCamelCase__      (				self    :					int							,	UpperCamelCase__    :					Optional[int]							,	UpperCamelCase__    :					str							,	UpperCamelCase__    :					Any  )     ->						Union[str, Any]:
    _UpperCamelCase       			=MaskFormerSwinModel(config=UpperCamelCase__  )
    model.to(UpperCamelCase__  )
    model.eval()
    _UpperCamelCase       			=model(UpperCamelCase__  )

    _UpperCamelCase       			=((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths  ) - 1))
    _UpperCamelCase       			=int(config.embed_dim * 2 ** (len(config.depths  ) - 1)  )

    self.parent.assertEqual(result.last_hidden_state.shape							,	(self.batch_size, expected_seq_len, expected_dim)  )


  def   UpperCamelCase__      (				self    :					Optional[int]							,	UpperCamelCase__    :					Tuple							,	UpperCamelCase__    :					int							,	UpperCamelCase__    :					Optional[Any]  )     ->						List[str]:
    _UpperCamelCase       			=MaskFormerSwinBackbone(config=UpperCamelCase__  )
    model.to(UpperCamelCase__  )
    model.eval()
    _UpperCamelCase       			=model(UpperCamelCase__  )

    # verify feature maps
    self.parent.assertEqual(len(result.feature_maps  )							,	len(config.out_features  )  )
    self.parent.assertListEqual(list(result.feature_maps[0].shape  )							,	[13, 16, 16, 16]  )

    # verify channels
    self.parent.assertEqual(len(model.channels  )							,	len(config.out_features  )  )
    self.parent.assertListEqual(model.channels							,	[16, 32, 64]  )

    # verify ValueError
    with self.parent.assertRaises(UpperCamelCase__  ):
      _UpperCamelCase       			=['''stem''']
      _UpperCamelCase       			=MaskFormerSwinBackbone(config=UpperCamelCase__  )




  def   UpperCamelCase__      (				self    :					Optional[int]  )     ->						Optional[int]:
    _UpperCamelCase       			=self.prepare_config_and_inputs()
    _UpperCamelCase       			=config_and_inputs
    _UpperCamelCase       			={'''pixel_values''': pixel_values}
    return config, inputs_dict







@require_torch
class 		UpperCAmelCase   (				__A   ,     __A   ,     unittest.TestCase):


  """simple docstring"""







  lowerCAmelCase_					=				(
      (
          MaskFormerSwinModel,
          MaskFormerSwinBackbone,
      )
      if is_torch_available()
      else ()
  )
  lowerCAmelCase_					=				{"""feature-extraction""": MaskFormerSwinModel} if is_torch_available() else {}
  lowerCAmelCase_					=				False
  lowerCAmelCase_					=				False
  lowerCAmelCase_					=				False
  lowerCAmelCase_					=				False
  lowerCAmelCase_					=				False


  def   UpperCamelCase__      (				self    :					Any  )     ->						str:
    _UpperCamelCase       			=MaskFormerSwinModelTester(self  )
    _UpperCamelCase       			=ConfigTester(self							,	config_class=UpperCamelCase__							,	embed_dim=37  )


  @require_torch_multi_gpu
  @unittest.skip(
      reason=(
          '''`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn\'t work well with'''
          ''' `nn.DataParallel`'''
      )  )
  def   UpperCamelCase__      (				self    :					Any  )     ->						str:
    pass


  def   UpperCamelCase__      (				self    :					str  )     ->						Optional[int]:
    self.create_and_test_config_common_properties()
    self.config_tester.create_and_test_config_to_json_string()
    self.config_tester.create_and_test_config_to_json_file()
    self.config_tester.create_and_test_config_from_and_save_pretrained()
    self.config_tester.create_and_test_config_with_num_labels()
    self.config_tester.check_config_can_be_init_without_params()
    self.config_tester.check_config_arguments_init()


  def   UpperCamelCase__      (				self    :					List[Any]  )     ->						Optional[Any]:
    return


  def   UpperCamelCase__      (				self    :					Optional[int]  )     ->						Tuple:
    _UpperCamelCase       			=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*UpperCamelCase__  )


  def   UpperCamelCase__      (				self    :					Optional[int]  )     ->						Dict:
    _UpperCamelCase       			=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_backbone(*UpperCamelCase__  )


  @unittest.skip('''Swin does not use inputs_embeds'''  )
  def   UpperCamelCase__      (				self    :					Optional[int]  )     ->						Dict:
    pass


  @unittest.skip('''Swin does not support feedforward chunking'''  )
  def   UpperCamelCase__      (				self    :					Tuple  )     ->						Dict:
    pass


  def   UpperCamelCase__      (				self    :					str  )     ->						str:
    _UpperCamelCase       			=self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      _UpperCamelCase       			=model_class(UpperCamelCase__  )
      self.assertIsInstance(model.get_input_embeddings()							,	(nn.Module)  )
      _UpperCamelCase       			=model.get_output_embeddings()
      self.assertTrue(x is None or isinstance(UpperCamelCase__							,	nn.Linear  )  )


  def   UpperCamelCase__      (				self    :					Any  )     ->						str:
    _UpperCamelCase       			=self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      _UpperCamelCase       			=model_class(UpperCamelCase__  )
      _UpperCamelCase       			=inspect.signature(model.forward  )
      # signature.parameters is an OrderedDict => so arg_names order is deterministic
      _UpperCamelCase       			=[*signature.parameters.keys()]

      _UpperCamelCase       			=['''pixel_values''']
      self.assertListEqual(arg_names[:1]							,	UpperCamelCase__  )


  @unittest.skip(reason='''MaskFormerSwin is only used as backbone and doesn\'t support output_attentions'''  )
  def   UpperCamelCase__      (				self    :					str  )     ->						Any:
    pass


  @unittest.skip(reason='''MaskFormerSwin is only used as an internal backbone'''  )
  def   UpperCamelCase__      (				self    :					List[str]  )     ->						Optional[Any]:
    pass


  def   UpperCamelCase__      (				self    :					Optional[Any]							,	UpperCamelCase__    :					str							,	UpperCamelCase__    :					Tuple							,	UpperCamelCase__    :					List[str]							,	UpperCamelCase__    :					Dict  )     ->						Any:
    _UpperCamelCase       			=model_class(UpperCamelCase__  )
    model.to(UpperCamelCase__  )
    model.eval()

    with torch.no_grad():
      _UpperCamelCase       			=model(**self._prepare_for_class(UpperCamelCase__							,	UpperCamelCase__  )  )

    _UpperCamelCase       			=outputs.hidden_states

    _UpperCamelCase       			=getattr(
        self.model_tester							,	'''expected_num_hidden_layers'''							,	len(self.model_tester.depths  ) + 1  )
    self.assertEqual(len(UpperCamelCase__  )							,	UpperCamelCase__  )

    # Swin has a different seq_length
    _UpperCamelCase       			=(
        config.patch_size
        if isinstance(config.patch_size							,	collections.abc.Iterable  )
        else (config.patch_size, config.patch_size)
    )

    _UpperCamelCase       			=(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

    self.assertListEqual(
        list(hidden_states[0].shape[-2:]  )							,	[num_patches, self.model_tester.embed_dim]							,	)


  def   UpperCamelCase__      (				self    :					List[Any]  )     ->						Optional[int]:
    _UpperCamelCase       			=self.model_tester.prepare_config_and_inputs_for_common()

    _UpperCamelCase       			=(
        self.model_tester.image_size
        if isinstance(self.model_tester.image_size							,	collections.abc.Iterable  )
        else (self.model_tester.image_size, self.model_tester.image_size)
    )

    for model_class in self.all_model_classes:
      _UpperCamelCase       			=True
      self.check_hidden_states_output(UpperCamelCase__							,	UpperCamelCase__							,	UpperCamelCase__							,	UpperCamelCase__  )

      # check that output_hidden_states also work using config
      del inputs_dict["output_hidden_states"]
      _UpperCamelCase       			=True

      self.check_hidden_states_output(UpperCamelCase__							,	UpperCamelCase__							,	UpperCamelCase__							,	UpperCamelCase__  )


  def   UpperCamelCase__      (				self    :					Union[str, Any]  )     ->						Optional[int]:
    _UpperCamelCase       			=self.model_tester.prepare_config_and_inputs_for_common()
    _UpperCamelCase       			=3

    _UpperCamelCase       			=(
        self.model_tester.image_size
        if isinstance(self.model_tester.image_size							,	collections.abc.Iterable  )
        else (self.model_tester.image_size, self.model_tester.image_size)
    )
    _UpperCamelCase       			=(
        config.patch_size
        if isinstance(config.patch_size							,	collections.abc.Iterable  )
        else (config.patch_size, config.patch_size)
    )

    _UpperCamelCase       			=image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
    _UpperCamelCase       			=image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

    for model_class in self.all_model_classes:
      _UpperCamelCase       			=True
      self.check_hidden_states_output(UpperCamelCase__							,	UpperCamelCase__							,	UpperCamelCase__							,	(padded_height, padded_width)  )

      # check that output_hidden_states also work using config
      del inputs_dict["output_hidden_states"]
      _UpperCamelCase       			=True
      self.check_hidden_states_output(UpperCamelCase__							,	UpperCamelCase__							,	UpperCamelCase__							,	(padded_height, padded_width)  )


  @unittest.skip(reason='''MaskFormerSwin doesn\'t have pretrained checkpoints'''  )
  def   UpperCamelCase__      (				self    :					str  )     ->						List[str]:
    pass


  @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin'''  )
  def   UpperCamelCase__      (				self    :					int  )     ->						List[Any]:
    pass


  @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin'''  )
  def   UpperCamelCase__      (				self    :					Union[str, Any]  )     ->						str:
    pass




  def   UpperCamelCase__      (				self    :					Optional[Any]  )     ->						Any:
    _UpperCamelCase       			=self.model_tester.prepare_config_and_inputs_for_common()

    def set_nan_tensor_to_zero(UpperCamelCase__    :					List[str]  ):
      _UpperCamelCase       			=0
      return t

    def check_equivalence(UpperCamelCase__    :					Optional[int]							,	UpperCamelCase__    :					str							,	UpperCamelCase__    :					Dict							,	UpperCamelCase__    :					Optional[int]={}  ):
      with torch.no_grad():
        _UpperCamelCase       			=model(**UpperCamelCase__							,	return_dict=UpperCamelCase__							,	**UpperCamelCase__  )
        _UpperCamelCase       			=model(**UpperCamelCase__							,	return_dict=UpperCamelCase__							,	**UpperCamelCase__  ).to_tuple()

        def recursive_check(UpperCamelCase__    :					int							,	UpperCamelCase__    :					Tuple  ):
          if isinstance(UpperCamelCase__							,	(List, Tuple)  ):
            for tuple_iterable_value, dict_iterable_value in zip(UpperCamelCase__							,	UpperCamelCase__  ):
              recursive_check(UpperCamelCase__							,	UpperCamelCase__  )
          elif isinstance(UpperCamelCase__							,	UpperCamelCase__  ):
            for tuple_iterable_value, dict_iterable_value in zip(
                tuple_object.values()							,	dict_object.values()  ):
              recursive_check(UpperCamelCase__							,	UpperCamelCase__  )
          elif tuple_object is None:
            return
          else:
            self.assertTrue(
                torch.allclose(
                    set_nan_tensor_to_zero(UpperCamelCase__  )							,	set_nan_tensor_to_zero(UpperCamelCase__  )							,	atol=1E-5  )							,	msg=(
                    '''Tuple and dict output are not equal. Difference:'''
                    F''' {torch.max(torch.abs(tuple_object - dict_object  )  )}. Tuple has `nan`:'''
                    F''' {torch.isnan(UpperCamelCase__  ).any()} and `inf`: {torch.isinf(UpperCamelCase__  )}. Dict has'''
                    F''' `nan`: {torch.isnan(UpperCamelCase__  ).any()} and `inf`: {torch.isinf(UpperCamelCase__  )}.'''
                )							,	)

        recursive_check(UpperCamelCase__							,	UpperCamelCase__  )

    for model_class in self.all_model_classes:
      _UpperCamelCase       			=model_class(UpperCamelCase__  )
      model.to(UpperCamelCase__  )
      model.eval()

      _UpperCamelCase       			=self._prepare_for_class(UpperCamelCase__							,	UpperCamelCase__  )
      _UpperCamelCase       			=self._prepare_for_class(UpperCamelCase__							,	UpperCamelCase__  )
      check_equivalence(UpperCamelCase__							,	UpperCamelCase__							,	UpperCamelCase__  )

      _UpperCamelCase       			=self._prepare_for_class(UpperCamelCase__							,	UpperCamelCase__							,	return_labels=UpperCamelCase__  )
      _UpperCamelCase       			=self._prepare_for_class(UpperCamelCase__							,	UpperCamelCase__							,	return_labels=UpperCamelCase__  )
      check_equivalence(UpperCamelCase__							,	UpperCamelCase__							,	UpperCamelCase__  )

      _UpperCamelCase       			=self._prepare_for_class(UpperCamelCase__							,	UpperCamelCase__  )
      _UpperCamelCase       			=self._prepare_for_class(UpperCamelCase__							,	UpperCamelCase__  )
      check_equivalence(UpperCamelCase__							,	UpperCamelCase__							,	UpperCamelCase__							,	{'''output_hidden_states''': True}  )

      _UpperCamelCase       			=self._prepare_for_class(UpperCamelCase__							,	UpperCamelCase__							,	return_labels=UpperCamelCase__  )
      _UpperCamelCase       			=self._prepare_for_class(UpperCamelCase__							,	UpperCamelCase__							,	return_labels=UpperCamelCase__  )
      check_equivalence(UpperCamelCase__							,	UpperCamelCase__							,	UpperCamelCase__							,	{'''output_hidden_states''': True}  )







@require_torch
class 		UpperCAmelCase   (				unittest.TestCase   ,     __A):


  """simple docstring"""







  lowerCAmelCase_					=				(MaskFormerSwinBackbone,) if is_torch_available() else ()
  lowerCAmelCase_					=				MaskFormerSwinConfig


  def   UpperCamelCase__      (				self    :					Optional[int]  )     ->						Dict:
    _UpperCamelCase       			=MaskFormerSwinModelTester(self  )




  def   UpperCamelCase__      (				self    :					List[str]  )     ->						Tuple:
    _UpperCamelCase       			=self.model_tester.prepare_config_and_inputs_for_common()
    _UpperCamelCase       			=inputs_dict['''pixel_values'''].shape[0]

    for backbone_class in self.all_model_classes:
      _UpperCamelCase       			=backbone_class(UpperCamelCase__  )
      backbone.to(UpperCamelCase__  )
      backbone.eval()

      _UpperCamelCase       			=backbone(**UpperCamelCase__  )

      # Test default outputs and verify feature maps
      self.assertIsInstance(outputs.feature_maps							,	UpperCamelCase__  )
      self.assertTrue(len(outputs.feature_maps  ) == len(backbone.channels  )  )
      for feature_map, n_channels in zip(outputs.feature_maps							,	backbone.channels  ):
        self.assertTrue(feature_map.shape[:2]							,	(batch_size, n_channels)  )
      self.assertIsNone(outputs.hidden_states  )
      self.assertIsNone(outputs.attentions  )

      # Test output_hidden_states=True
      _UpperCamelCase       			=backbone(**UpperCamelCase__							,	output_hidden_states=UpperCamelCase__  )
      self.assertIsNotNone(outputs.hidden_states  )
      self.assertTrue(len(outputs.hidden_states  )							,	len(backbone.stage_names  )  )
      # We skip the stem layer
      for hidden_states, n_channels in zip(outputs.hidden_states[1:]							,	backbone.channels  ):
        for hidden_state in hidden_states:
          # Hidden states are in the format (batch_size, (height * width), n_channels)
          _UpperCamelCase       			=hidden_state.shape
          self.assertTrue((h_batch_size, h_n_channels)							,	(batch_size, n_channels)  )

            # Test output_attentions=True
      if self.has_attentions:
        _UpperCamelCase       			=backbone(**UpperCamelCase__							,	output_attentions=UpperCamelCase__  )
        self.assertIsNotNone(outputs.attentions  )








'''simple docstring'''






import argparse
import os
import pickle
import sys

import torch

from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl
from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils
from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()

# We do this to be able to load python 2 datasets pickles
# See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918
__lowerCamelCase						: List[Any]		=   data_utils.TransfoXLTokenizer
__lowerCamelCase						: str		=   data_utils.TransfoXLCorpus
__lowerCamelCase						: Optional[int]		=   data_utils
__lowerCamelCase						: int		=   data_utils


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  if transfo_xl_dataset_file:
    # Convert a pre-processed corpus (see original TensorFlow repo)
    with open(__SCREAMING_SNAKE_CASE  ,      '''rb'''   ) as fp:
      _UpperCamelCase       			=pickle.load(__SCREAMING_SNAKE_CASE  ,      encoding='''latin1'''   )
    # Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term)
    _UpperCamelCase       			=pytorch_dump_folder_path + '''/''' + VOCAB_FILES_NAMES['''pretrained_vocab_file''']
    print(f'''Save vocabulary to {pytorch_vocab_dump_path}'''   )
    _UpperCamelCase       			=corpus.vocab.__dict__
    torch.save(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )

    _UpperCamelCase       			=corpus.__dict__
    corpus_dict_no_vocab.pop('''vocab'''  ,      __SCREAMING_SNAKE_CASE   )
    _UpperCamelCase       			=pytorch_dump_folder_path + '''/''' + CORPUS_NAME
    print(f'''Save dataset to {pytorch_dataset_dump_path}'''   )
    torch.save(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )

  if tf_checkpoint_path:
    # Convert a pre-trained TensorFlow model
    _UpperCamelCase       			=os.path.abspath(__SCREAMING_SNAKE_CASE   )
    _UpperCamelCase       			=os.path.abspath(__SCREAMING_SNAKE_CASE   )

    print(f'''Converting Transformer XL checkpoint from {tf_path} with config at {config_path}.'''   )
    # Initialise PyTorch model
    if transfo_xl_config_file == "":
      _UpperCamelCase       			=TransfoXLConfig()
    else:
      _UpperCamelCase       			=TransfoXLConfig.from_json_file(__SCREAMING_SNAKE_CASE   )
    print(f'''Building PyTorch model from configuration: {config}'''   )
    _UpperCamelCase       			=TransfoXLLMHeadModel(__SCREAMING_SNAKE_CASE   )

    _UpperCamelCase       			=load_tf_weights_in_transfo_xl(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
    # Save pytorch-model
    _UpperCamelCase       			=os.path.join(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
    _UpperCamelCase       			=os.path.join(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
    print(f'''Save PyTorch model to {os.path.abspath(__SCREAMING_SNAKE_CASE   )}'''   )
    torch.save(model.state_dict()  ,      __SCREAMING_SNAKE_CASE   )
    print(f'''Save configuration file to {os.path.abspath(__SCREAMING_SNAKE_CASE   )}'''   )
    with open(__SCREAMING_SNAKE_CASE  ,      '''w'''  ,      encoding='''utf-8'''   ) as f:
      f.write(config.to_json_string()   )


if __name__ == "__main__":
      __lowerCamelCase						: Any		=   argparse.ArgumentParser()
      parser.add_argument(
          '--pytorch_dump_folder_path',
          default=None,
          type=str,
          required=True,
          help='Path to the folder to store the PyTorch model or dataset/vocab.',
      )
      parser.add_argument(
          '--tf_checkpoint_path',
          default='',
          type=str,
          help='An optional path to a TensorFlow checkpoint path to be converted.',
      )
      parser.add_argument(
          '--transfo_xl_config_file',
          default='',
          type=str,
          help=(
              'An optional config json file corresponding to the pre-trained BERT model. \n'
              'This specifies the model architecture.'
          ),
      )
      parser.add_argument(
          '--transfo_xl_dataset_file',
          default='',
          type=str,
          help='An optional dataset file to be converted in a vocabulary.',
      )
      __lowerCamelCase						: Optional[int]		=   parser.parse_args()
      convert_transfo_xl_checkpoint_to_pytorch(
          args.tf_checkpoint_path,
          args.transfo_xl_config_file,
          args.pytorch_dump_folder_path,
          args.transfo_xl_dataset_file,
      )






