


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


def 				lowerCamelCase__	(      a	:   Dict	,  a	:   Optional[int]	,  a	:   str	,  a	:   Union[str, Any]				)    ->	Optional[int]:





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

						a__ :Optional[int]						  =    corpus.__dict__
						corpus_dict_no_vocab.pop("vocab"	,  _lowerCamelCase				)
						a__ :Optional[int]						  =    pytorch_dump_folder_path + "/" + CORPUS_NAME
						print(F'''Save dataset to {pytorch_dataset_dump_path}'''				)
						torch.save(_lowerCamelCase	,  _lowerCamelCase				)

			if tf_checkpoint_path:
						# Convert a pre-trained TensorFlow model
						a__ :str						  =    os.path.abspath(_lowerCamelCase				)
						a__ :Optional[Any]						  =    os.path.abspath(_lowerCamelCase				)

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

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


if __name__ == "__main__":
		snake_case__      =						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.''',
		)
		snake_case__      =						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 os
import time

import numpy as np
import onnxruntime as ort


snake_case__			 =						'''1'''
snake_case__			 =						'''0'''
snake_case__			 =						'''1'''

snake_case__			 =						ort.SessionOptions()
snake_case__			 =						ort.GraphOptimizationLevel.ORT_DISABLE_ALL
print('''Create inference session...''')
snake_case__			 =						['''TensorrtExecutionProvider''', '''CUDAExecutionProvider''']
snake_case__			 =						ort.InferenceSession('''model.onnx''', sess_options=sess_opt, providers=execution_provider)
snake_case__			 =						ort.RunOptions()

snake_case__			 =						128
snake_case__			 =						1
snake_case__			 =						np.ones((batch, sequence), dtype=np.intaa)
snake_case__			 =						np.ones((batch, sequence), dtype=np.intaa)
snake_case__			 =						np.ones((batch, sequence), dtype=np.intaa)

print('''Warm up phase...''')
sess.run(
    None,
    {
        sess.get_inputs()[0].name: input_ids,
        sess.get_inputs()[1].name: attention_mask,
        sess.get_inputs()[2].name: token_type_ids,
    },
    run_options=run_opt,
)

print('''Start inference...''')
snake_case__			 =						time.time()
snake_case__			 =						2000
snake_case__			 =						{}
for iter in range(max_iters):
		snake_case__			 =						sess.run(
		    None,
		    {
		        sess.get_inputs()[0].name: input_ids,
		        sess.get_inputs()[1].name: attention_mask,
		        sess.get_inputs()[2].name: token_type_ids,
		    },
		    run_options=run_opt,
		)
print('''Average Inference Time = {:.3f} ms'''.format((time.time() - start_time) * 1000 / max_iters))


