




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
__a          =			data_utils.TransfoXLTokenizer
__a          =			data_utils.TransfoXLCorpus
__a          =			data_utils
__a          =			data_utils







def lowerCamelCase__       (      _lowercase			,						_lowercase			,						_lowercase			,						_lowercase				):

     '''simple docstring'''


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

          UpperCAmelCase_      :							List[str]       =      corpus.__dict__
          corpus_dict_no_vocab.pop('''vocab'''			,						_lowercase				)
          UpperCAmelCase_      :							Optional[int]       =      pytorch_dump_folder_path + '''/''' + CORPUS_NAME
          print(f'''Save dataset to {pytorch_dataset_dump_path}'''				)
          torch.save(_lowercase			,						_lowercase				)

     if tf_checkpoint_path:
          # Convert a pre-trained TensorFlow model
          UpperCAmelCase_      :							List[Any]       =      os.path.abspath(_lowercase				)
          UpperCAmelCase_      :							Union[str, Any]       =      os.path.abspath(_lowercase				)

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

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


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





def lowerCamelCase__       (      _lowercase			,						_lowercase				):

     '''simple docstring'''


     return (pointa[0] - pointa[0]) ** 2 + (pointa[1] - pointa[1]) ** 2







def lowerCamelCase__       (      _lowercase			,						_lowercase=0				):

     '''simple docstring'''


     return sorted(_lowercase			,						key=lambda _lowercase				: x[column]				)







def lowerCamelCase__       (      _lowercase			,						_lowercase			,						_lowercase=float('''inf'''				)				):

     '''simple docstring'''


     for i in range(points_counts - 1				):
          for j in range(i + 1			,						_lowercase				):
               UpperCAmelCase_      :							Optional[int]       =      euclidean_distance_sqr(points[i]			,						points[j]				)
               if current_dis < min_dis:
                    UpperCAmelCase_      :							Optional[Any]       =      current_dis
     return min_dis







def lowerCamelCase__       (      _lowercase			,						_lowercase			,						_lowercase=float('''inf'''				)				):

     '''simple docstring'''


     for i in range(min(6			,						points_counts - 1				)			,						_lowercase				):
          for j in range(max(0			,						i - 6				)			,						_lowercase				):
               UpperCAmelCase_      :							List[str]       =      euclidean_distance_sqr(points[i]			,						points[j]				)
               if current_dis < min_dis:
                    UpperCAmelCase_      :							Optional[int]       =      current_dis
     return min_dis







def lowerCamelCase__       (      _lowercase			,						_lowercase			,						_lowercase				):

     '''simple docstring'''


     if points_counts <= 3:
          return dis_between_closest_pair(_lowercase			,						_lowercase				)

     # recursion
     UpperCAmelCase_      :							Optional[int]       =      points_counts // 2
     UpperCAmelCase_      :							List[Any]       =      closest_pair_of_points_sqr(
         _lowercase			,						points_sorted_on_y[:mid]			,						_lowercase				)
     UpperCAmelCase_      :							Dict       =      closest_pair_of_points_sqr(
         _lowercase			,						points_sorted_on_y[mid:]			,						points_counts - mid				)
     UpperCAmelCase_      :							Union[str, Any]       =      min(_lowercase			,						_lowercase				)
     UpperCAmelCase_      :							str       =      []
     for point in points_sorted_on_x:
          if abs(point[0] - points_sorted_on_x[mid][0]				) < closest_pair_dis:
               cross_strip.append(_lowercase				)

     UpperCAmelCase_      :							Optional[Any]       =      dis_between_closest_in_strip(
         _lowercase			,						len(_lowercase				)			,						_lowercase				)
     return min(_lowercase			,						_lowercase				)







def lowerCamelCase__       (      _lowercase			,						_lowercase				):

     '''simple docstring'''


     UpperCAmelCase_      :							Tuple       =      column_based_sort(_lowercase			,						column=0				)
     UpperCAmelCase_      :							List[Any]       =      column_based_sort(_lowercase			,						column=1				)
     return (
         closest_pair_of_points_sqr(
             _lowercase			,						_lowercase			,						_lowercase				)
     ) ** 0.5


if __name__ == "__main__":
     __a          =			[(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)]
     print('Distance:', closest_pair_of_points(points, len(points)))