



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				(UpperCamelCase		:      Tuple      ,			UpperCamelCase		:      Optional[Any]      ,			UpperCamelCase		:      str      ,			UpperCamelCase		:      Any )  ->Optional[int]:



			'''simple docstring'''







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

						lowerCamelCase__ :      List[str]								=		corpus.__dict__
						corpus_dict_no_vocab.pop("""vocab"""      ,			__snake_case )
						lowerCamelCase__ :      Dict								=		pytorch_dump_folder_path + """/""" + CORPUS_NAME
						print(f"Save dataset to {pytorch_dataset_dump_path}" )
						torch.save(__snake_case      ,			__snake_case )

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

						print(f"Converting Transformer XL checkpoint from {tf_path} with config at {config_path}." )
						# Initialise PyTorch model
						if transfo_xl_config_file == "":
									lowerCamelCase__ :      Optional[Any]								=		TransfoXLConfig()
						else:
									lowerCamelCase__ :      List[Any]								=		TransfoXLConfig.from_json_file(__snake_case )
						print(f"Building PyTorch model from configuration: {config}" )
						lowerCamelCase__ :      Union[str, Any]								=		TransfoXLLMHeadModel(__snake_case )

						lowerCamelCase__ :      Tuple								=		load_tf_weights_in_transfo_xl(__snake_case      ,			__snake_case      ,			__snake_case )
						# Save pytorch-model
						lowerCamelCase__ :      Tuple								=		os.path.join(__snake_case      ,			__snake_case )
						lowerCamelCase__ :      List[str]								=		os.path.join(__snake_case      ,			__snake_case )
						print(f"Save PyTorch model to {os.path.abspath(__snake_case )}" )
						torch.save(model.state_dict()      ,			__snake_case )
						print(f"Save configuration file to {os.path.abspath(__snake_case )}" )
						with open(__snake_case      ,			"""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,
				)








import unittest

from .lib import (
    Matrix,
    Vector,
    axpy,
    square_zero_matrix,
    unit_basis_vector,
    zero_vector,
)
class 				__A					( unittest.TestCase      ):





			def        _snake_case    (self				):
						lowerCamelCase__ :      List[str]								=		Vector([1, 2, 3]				)
						self.assertEqual(x.component(0				)							,    1				)
						self.assertEqual(x.component(2				)							,    3				)
						lowerCamelCase__ :      str								=		Vector()





			def        _snake_case    (self				):
						lowerCamelCase__ :      Dict								=		Vector([0, 0, 0, 0, 0, 1]				)
						self.assertEqual(str(__magic_name__				)							,    """(0,0,0,0,0,1)"""				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Tuple								=		Vector([1, 2, 3, 4]				)
						self.assertEqual(len(__magic_name__				)							,    4				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Dict								=		Vector([1, 2]				)
						lowerCamelCase__ :      str								=		Vector([1, 2, 3, 4, 5]				)
						lowerCamelCase__ :      int								=		Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]				)
						lowerCamelCase__ :      int								=		Vector([1, -1, 1, -1, 2, -3, 4, -5]				)
						self.assertAlmostEqual(x.euclidean_length()							,    2.2_36							,    3				)
						self.assertAlmostEqual(y.euclidean_length()							,    7.4_16							,    3				)
						self.assertEqual(z.euclidean_length()							,    0				)
						self.assertAlmostEqual(w.euclidean_length()							,    7.6_16							,    3				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      str								=		Vector([1, 2, 3]				)
						lowerCamelCase__ :      Union[str, Any]								=		Vector([1, 1, 1]				)
						self.assertEqual((x + y).component(0				)							,    2				)
						self.assertEqual((x + y).component(1				)							,    3				)
						self.assertEqual((x + y).component(2				)							,    4				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[int]								=		Vector([1, 2, 3]				)
						lowerCamelCase__ :      Any								=		Vector([1, 1, 1]				)
						self.assertEqual((x - y).component(0				)							,    0				)
						self.assertEqual((x - y).component(1				)							,    1				)
						self.assertEqual((x - y).component(2				)							,    2				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Dict								=		Vector([1, 2, 3]				)
						lowerCamelCase__ :      int								=		Vector([2, -1, 4]				)  # for test of dot product
						lowerCamelCase__ :      List[Any]								=		Vector([1, -2, -1]				)
						self.assertEqual(str(x * 3.0				)							,    """(3.0,6.0,9.0)"""				)
						self.assertEqual((a * b)							,    0				)





			def        _snake_case    (self				):
						self.assertEqual(str(zero_vector(10				)				).count("""0"""				)							,    10				)





			def        _snake_case    (self				):
						self.assertEqual(str(unit_basis_vector(3							,    1				)				)							,    """(0,1,0)"""				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Any								=		Vector([1, 2, 3]				)
						lowerCamelCase__ :      str								=		Vector([1, 0, 1]				)
						self.assertEqual(str(axpy(2							,    __magic_name__							,    __magic_name__				)				)							,    """(3,4,7)"""				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[Any]								=		Vector([1, 0, 0, 0, 0, 0]				)
						lowerCamelCase__ :      int								=		x.copy()
						self.assertEqual(str(__magic_name__				)							,    str(__magic_name__				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      int								=		Vector([1, 0, 0]				)
						x.change_component(0							,    0				)
						x.change_component(1							,    1				)
						self.assertEqual(str(__magic_name__				)							,    """(0,1,0)"""				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[Any]								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						self.assertEqual("""|1,2,3|\n|2,4,5|\n|6,7,8|\n"""							,    str(__magic_name__				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      int								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						lowerCamelCase__ :      int								=		[[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]]
						for x in range(a.height()				):
									for y in range(a.width()				):
												self.assertEqual(minors[x][y]							,    a.minor(__magic_name__							,    __magic_name__				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Any								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						lowerCamelCase__ :      Optional[Any]								=		[[-3, 14, -10], [5, -10, 5], [-2, 1, 0]]
						for x in range(a.height()				):
									for y in range(a.width()				):
												self.assertEqual(cofactors[x][y]							,    a.cofactor(__magic_name__							,    __magic_name__				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Optional[int]								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						self.assertEqual(-5							,    a.determinant()				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      List[str]								=		Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]							,    3							,    3				)
						lowerCamelCase__ :      Optional[int]								=		Vector([1, 2, 3]				)
						self.assertEqual("""(14,32,50)"""							,    str(a * x				)				)
						self.assertEqual("""|2,4,6|\n|8,10,12|\n|14,16,18|\n"""							,    str(a * 2				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      str								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						a.change_component(0							,    2							,    5				)
						self.assertEqual("""|1,2,5|\n|2,4,5|\n|6,7,8|\n"""							,    str(__magic_name__				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      List[Any]								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						self.assertEqual(7							,    a.component(2							,    1				)							,    0.01				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Tuple								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						lowerCamelCase__ :      int								=		Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]							,    3							,    3				)
						self.assertEqual("""|2,4,10|\n|4,8,10|\n|12,14,18|\n"""							,    str(a + b				)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      str								=		Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]							,    3							,    3				)
						lowerCamelCase__ :      Optional[Any]								=		Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]							,    3							,    3				)
						self.assertEqual("""|0,0,-4|\n|0,0,0|\n|0,0,-2|\n"""							,    str(a - b				)				)





			def        _snake_case    (self				):
						self.assertEqual(
						    """|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n"""							,    str(square_zero_matrix(5				)				)							,    )


if __name__ == "__main__":
				unittest.main()



