





import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel






def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							BertModel					,   UpperCamelCase__:							str					,   UpperCamelCase__:							str    ):
		SCREAMING_SNAKE_CASE__			=      ("""dense.weight""", """attention.self.query""", """attention.self.key""", """attention.self.value""")

		SCREAMING_SNAKE_CASE__			=      (
		    ("""layer.""", """layer_"""),
		    ("""word_embeddings.weight""", """word_embeddings"""),
		    ("""position_embeddings.weight""", """position_embeddings"""),
		    ("""token_type_embeddings.weight""", """token_type_embeddings"""),
		    (""".""", """/"""),
		    ("""LayerNorm/weight""", """LayerNorm/gamma"""),
		    ("""LayerNorm/bias""", """LayerNorm/beta"""),
		    ("""weight""", """kernel"""),
		)

		if not os.path.isdir(UpperCamelCase__    ):
				os.makedirs(UpperCamelCase__    )

		SCREAMING_SNAKE_CASE__			=      model.state_dict()

		def to_tf_var_name(UpperCamelCase__:							str    ):
				for patt, repl in iter(UpperCamelCase__    ):
						SCREAMING_SNAKE_CASE__			=      name.replace(UpperCamelCase__					,   UpperCamelCase__    )
				return f'''bert/{name}'''

		def create_tf_var(UpperCamelCase__:							np.ndarray					,   UpperCamelCase__:							str					,   UpperCamelCase__:							tf.Session    ):
				SCREAMING_SNAKE_CASE__			=      tf.dtypes.as_dtype(tensor.dtype    )
				SCREAMING_SNAKE_CASE__			=      tf.get_variable(dtype=UpperCamelCase__					,   shape=tensor.shape					,   name=UpperCamelCase__					,   initializer=tf.zeros_initializer()    )
				session.run(tf.variables_initializer([tf_var]    )    )
				session.run(UpperCamelCase__    )
				return tf_var

		tf.reset_default_graph()
		with tf.Session() as session:
				for var_name in state_dict:
						SCREAMING_SNAKE_CASE__			=      to_tf_var_name(UpperCamelCase__    )
						SCREAMING_SNAKE_CASE__			=      state_dict[var_name].numpy()
						if any(x in var_name for x in tensors_to_transpose    ):
								SCREAMING_SNAKE_CASE__			=      torch_tensor.T
						SCREAMING_SNAKE_CASE__			=      create_tf_var(tensor=UpperCamelCase__					,   name=UpperCamelCase__					,   session=UpperCamelCase__    )
						tf.keras.backend.set_value(UpperCamelCase__					,   UpperCamelCase__    )
						SCREAMING_SNAKE_CASE__			=      session.run(UpperCamelCase__    )
						print(f'''Successfully created {tf_name}: {np.allclose(UpperCamelCase__					,   UpperCamelCase__    )}'''    )

				SCREAMING_SNAKE_CASE__			=      tf.train.Saver(tf.trainable_variables()    )
				saver.save(UpperCamelCase__					,   os.path.join(UpperCamelCase__					,   model_name.replace("""-"""					,   """_"""    ) + """.ckpt"""    )    )






def 				SCREAMING_SNAKE_CASE__							(       UpperCamelCase__:							Optional[int]=None    ):
		SCREAMING_SNAKE_CASE__			=      argparse.ArgumentParser()
		parser.add_argument("""--model_name"""					,   type=UpperCamelCase__					,   required=UpperCamelCase__					,   help="""model name e.g. bert-base-uncased"""    )
		parser.add_argument(
		    """--cache_dir"""					,   type=UpperCamelCase__					,   default=UpperCamelCase__					,   required=UpperCamelCase__					,   help="""Directory containing pytorch model"""    )
		parser.add_argument("""--pytorch_model_path"""					,   type=UpperCamelCase__					,   required=UpperCamelCase__					,   help="""/path/to/<pytorch-model-name>.bin"""    )
		parser.add_argument("""--tf_cache_dir"""					,   type=UpperCamelCase__					,   required=UpperCamelCase__					,   help="""Directory in which to save tensorflow model"""    )
		SCREAMING_SNAKE_CASE__			=      parser.parse_args(UpperCamelCase__    )

		SCREAMING_SNAKE_CASE__			=      BertModel.from_pretrained(
		    pretrained_model_name_or_path=args.model_name					,   state_dict=torch.load(args.pytorch_model_path    )					,   cache_dir=args.cache_dir					,   )

		convert_pytorch_checkpoint_to_tf(model=UpperCamelCase__					,   ckpt_dir=args.tf_cache_dir					,   model_name=args.model_name    )


if __name__ == "__main__":
	main()






import json
import os
import unittest

from transformers import OpenAIGPTTokenizer, OpenAIGPTTokenizerFast
from transformers.models.openai.tokenization_openai import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_spacy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin




@require_tokenizers
class   UpperCamelCase_       (     UpperCamelCase__				,  unittest.TestCase    ):
							lowerCamelCase_           =					OpenAIGPTTokenizer
							lowerCamelCase_           =					OpenAIGPTTokenizerFast
							lowerCamelCase_           =					True
							lowerCamelCase_           =					False


							def      _snake_case  (							self    :Optional[Any]		)   ->   Dict:



									"""simple docstring"""



									super().setUp()

									# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
									SCREAMING_SNAKE_CASE__			=      [
									    """l""",
									    """o""",
									    """w""",
									    """e""",
									    """r""",
									    """s""",
									    """t""",
									    """i""",
									    """d""",
									    """n""",
									    """w</w>""",
									    """r</w>""",
									    """t</w>""",
									    """lo""",
									    """low""",
									    """er</w>""",
									    """low</w>""",
									    """lowest</w>""",
									    """newer</w>""",
									    """wider</w>""",
									    """<unk>""",
									]
									SCREAMING_SNAKE_CASE__			=      dict(zip(__A							,				range(len(__A		)		)		)		)
									SCREAMING_SNAKE_CASE__			=      ["""#version: 0.2""", """l o""", """lo w""", """e r</w>""", """"""]

									SCREAMING_SNAKE_CASE__			=      os.path.join(self.tmpdirname							,				VOCAB_FILES_NAMES["""vocab_file"""]		)
									SCREAMING_SNAKE_CASE__			=      os.path.join(self.tmpdirname							,				VOCAB_FILES_NAMES["""merges_file"""]		)
									with open(self.vocab_file							,				"""w"""		) as fp:
											fp.write(json.dumps(__A		)		)
									with open(self.merges_file							,				"""w"""		) as fp:
											fp.write("""\n""".join(__A		)		)


							def      _snake_case  (							self    :Union[str, Any]							,				__A    :str		)   ->   List[Any]:



									"""simple docstring"""



									return "lower newer", "lower newer"


							def      _snake_case  (							self    :Optional[Any]		)   ->   Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      OpenAIGPTTokenizer(self.vocab_file							,				self.merges_file		)

									SCREAMING_SNAKE_CASE__			=      """lower"""
									SCREAMING_SNAKE_CASE__			=      ["""low""", """er</w>"""]
									SCREAMING_SNAKE_CASE__			=      tokenizer.tokenize(__A		)
									self.assertListEqual(__A							,				__A		)

									SCREAMING_SNAKE_CASE__			=      tokens + ["""<unk>"""]
									SCREAMING_SNAKE_CASE__			=      [14, 15, 20]
									self.assertListEqual(tokenizer.convert_tokens_to_ids(__A		)							,				__A		)


							def      _snake_case  (							self    :Optional[Any]							,				__A    :Optional[Any]=15		)   ->   Any:



									"""simple docstring"""



									for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
											with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})'''		):
													SCREAMING_SNAKE_CASE__			=      self.rust_tokenizer_class.from_pretrained(__A							,				**__A		)

													# Simple input
													SCREAMING_SNAKE_CASE__			=      """This is a simple input"""
													SCREAMING_SNAKE_CASE__			=      ["""This is a simple input 1""", """This is a simple input 2"""]
													SCREAMING_SNAKE_CASE__			=      ("""This is a simple input""", """This is a pair""")
													SCREAMING_SNAKE_CASE__			=      [
													    ("""This is a simple input 1""", """This is a simple input 2"""),
													    ("""This is a simple pair 1""", """This is a simple pair 2"""),
													]

													# Simple input tests
													self.assertRaises(__A							,				tokenizer_r.encode							,				__A							,				max_length=__A							,				padding="""max_length"""		)

													# Simple input
													self.assertRaises(__A							,				tokenizer_r.encode_plus							,				__A							,				max_length=__A							,				padding="""max_length"""		)

													# Simple input
													self.assertRaises(
													    __A							,				tokenizer_r.batch_encode_plus							,				__A							,				max_length=__A							,				padding="""max_length"""							,				)

													# Pair input
													self.assertRaises(__A							,				tokenizer_r.encode							,				__A							,				max_length=__A							,				padding="""max_length"""		)

													# Pair input
													self.assertRaises(__A							,				tokenizer_r.encode_plus							,				__A							,				max_length=__A							,				padding="""max_length"""		)

													# Pair input
													self.assertRaises(
													    __A							,				tokenizer_r.batch_encode_plus							,				__A							,				max_length=__A							,				padding="""max_length"""							,				)


							def      _snake_case  (							self    :Dict		)   ->   List[Any]:



									"""simple docstring"""



									pass




@require_ftfy
@require_spacy
@require_tokenizers
class   UpperCamelCase_       (     UpperCamelCase__    ):
							pass