






'''simple docstring'''





import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCamelCase		:		Any            = logging.get_logger(__name__)

_UpperCamelCase		:		List[str]            = {
    'asapp/sew-d-tiny-100k': 'https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json',
    # See all SEW-D models at https://huggingface.co/models?filter=sew-d
}


class       snake_case__	( UpperCamelCase):
					a_								= "sew-d"




					def __init__(   self   :						Any							,      _A   :						Dict=32							,      _A   :						Union[str, Any]=7_68							,      _A   :						str=12							,      _A   :						List[Any]=12							,      _A   :						List[str]=30_72							,      _A   :						List[str]=2							,      _A   :						str=5_12							,      _A   :						Optional[Any]=2_56							,      _A   :						int=True							,      _A   :						str=True							,      _A   :						Optional[int]=("p2c", "c2p")							,      _A   :						List[str]="layer_norm"							,      _A   :						Tuple="gelu_python"							,      _A   :						List[Any]=0.1							,      _A   :						Tuple=0.1							,      _A   :						str=0.1							,      _A   :						Tuple=0.0							,      _A   :						Any=0.1							,      _A   :						Optional[Any]=0.02							,      _A   :						Tuple=1e-7							,      _A   :						int=1e-5							,      _A   :						Optional[Any]="group"							,      _A   :						int="gelu"							,      _A   :						List[str]=(64, 1_28, 1_28, 1_28, 1_28, 2_56, 2_56, 2_56, 2_56, 5_12, 5_12, 5_12, 5_12)							,      _A   :						List[str]=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)							,      _A   :						List[Any]=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)							,      _A   :						Dict=False							,      _A   :						int=1_28							,      _A   :						Any=16							,      _A   :						Dict=True							,      _A   :						List[Any]=0.05							,      _A   :						Tuple=10							,      _A   :						Optional[int]=2							,      _A   :						str=0.0							,      _A   :						Dict=10							,      _A   :						str=0							,      _A   :						str="mean"							,      _A   :						str=False							,      _A   :						Any=False							,      _A   :						int=2_56							,      _A   :						Tuple=0							,      _A   :						List[str]=1							,      _A   :						List[Any]=2							,      **_A   :						List[str]							,      )  ->				Optional[Any]:
								super().__init__(**_A							,      pad_token_id=_A							,      bos_token_id=_A							,      eos_token_id=_A	)
								UpperCAmelCase_	:    List[Any]	  =    hidden_size
								UpperCAmelCase_	:    Optional[Any]	  =    feat_extract_norm
								UpperCAmelCase_	:    Any	  =    feat_extract_activation
								UpperCAmelCase_	:    Any	  =    list(_A	)
								UpperCAmelCase_	:    Dict	  =    list(_A	)
								UpperCAmelCase_	:    List[Any]	  =    list(_A	)
								UpperCAmelCase_	:    List[str]	  =    conv_bias
								UpperCAmelCase_	:    int	  =    num_conv_pos_embeddings
								UpperCAmelCase_	:    str	  =    num_conv_pos_embedding_groups
								UpperCAmelCase_	:    Union[str, Any]	  =    len(self.conv_dim	)
								UpperCAmelCase_	:    Union[str, Any]	  =    num_hidden_layers
								UpperCAmelCase_	:    List[str]	  =    intermediate_size
								UpperCAmelCase_	:    Union[str, Any]	  =    squeeze_factor
								UpperCAmelCase_	:    List[Any]	  =    max_position_embeddings
								UpperCAmelCase_	:    List[str]	  =    position_buckets
								UpperCAmelCase_	:    List[Any]	  =    share_att_key
								UpperCAmelCase_	:    int	  =    relative_attention
								UpperCAmelCase_	:    Union[str, Any]	  =    norm_rel_ebd
								UpperCAmelCase_	:    Optional[Any]	  =    list(_A	)
								UpperCAmelCase_	:    Union[str, Any]	  =    hidden_act
								UpperCAmelCase_	:    List[Any]	  =    num_attention_heads
								UpperCAmelCase_	:    List[Any]	  =    hidden_dropout
								UpperCAmelCase_	:    List[str]	  =    attention_dropout
								UpperCAmelCase_	:    List[Any]	  =    activation_dropout
								UpperCAmelCase_	:    Tuple	  =    feat_proj_dropout
								UpperCAmelCase_	:    Optional[Any]	  =    final_dropout
								UpperCAmelCase_	:    Union[str, Any]	  =    layer_norm_eps
								UpperCAmelCase_	:    Any	  =    feature_layer_norm_eps
								UpperCAmelCase_	:    List[Any]	  =    initializer_range
								UpperCAmelCase_	:    Optional[int]	  =    vocab_size

								if (
								    (len(self.conv_stride	) != self.num_feat_extract_layers)
								    or (len(self.conv_kernel	) != self.num_feat_extract_layers)
								    or (len(self.conv_dim	) != self.num_feat_extract_layers)
								):
											raise ValueError(
											    '''Configuration for convolutional layers is incorrect.'''
											    '''It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,'''
											    F"but is `len(config.conv_dim) = {len(self.conv_dim	)}`, `len(config.conv_stride)"
											    F"= {len(self.conv_stride	)}`, `len(config.conv_kernel) = {len(self.conv_kernel	)}`."	)

								# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
								UpperCAmelCase_	:    Dict	  =    apply_spec_augment
								UpperCAmelCase_	:    Any	  =    mask_time_prob
								UpperCAmelCase_	:    int	  =    mask_time_length
								UpperCAmelCase_	:    Union[str, Any]	  =    mask_time_min_masks
								UpperCAmelCase_	:    Optional[Any]	  =    mask_feature_prob
								UpperCAmelCase_	:    List[str]	  =    mask_feature_length
								UpperCAmelCase_	:    List[Any]	  =    mask_feature_min_masks

								# ctc loss
								UpperCAmelCase_	:    int	  =    ctc_loss_reduction
								UpperCAmelCase_	:    str	  =    ctc_zero_infinity

								# sequence classification
								UpperCAmelCase_	:    Dict	  =    use_weighted_layer_sum
								UpperCAmelCase_	:    List[Any]	  =    classifier_proj_size





					@property
					def 						A	(   self   :						str	)  ->				Optional[int]:
								return functools.reduce(operator.mul							,      self.conv_stride							,      1	)





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
lowercase_									=   data_utils.TransfoXLTokenizer
lowercase_									=   data_utils.TransfoXLCorpus
lowercase_									=   data_utils
lowercase_									=   data_utils
def      a      (			A__	:   int      ,   A__	:   Dict      ,   A__	:   Union[str, Any]      ,   A__	:   Union[str, Any]     )    ->      List[str]:





      """simple docstring"""



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

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

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

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

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


if __name__ == "__main__":
  lowercase_									=   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.',
  )
  lowercase_									=   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,
  )


