



import unittest

import numpy as np
import torch

from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad
class 				_snake_case			(					unittest.TestCase):




				def 							A__      (						self					:						Tuple						):
							lowercase__					  =				10




				def 							A__      (						self					:						int						):
							lowercase__					  =				[1, 2, 3, 4]
							lowercase__					  =				[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
							self.assertEqual(truncate_or_pad(__A,					self.block_size,					0						),					__A						)




				def 							A__      (						self					:						Dict						):
							lowercase__					  =				[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
							lowercase__					  =				[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
							self.assertEqual(truncate_or_pad(__A,					self.block_size,					0						),					__A						)




				def 							A__      (						self					:						Optional[Any]						):
							lowercase__					  =				[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
							lowercase__					  =				[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
							self.assertEqual(truncate_or_pad(__A,					self.block_size,					0						),					__A						)




				def 							A__      (						self					:						Union[str, Any]						):
							lowercase__					  =				"It was the year of Our Lord one thousand seven hundred and\n        seventy-five.\n\nSpiritual revelations were conceded to England at that\n        favoured period, as at this."
							lowercase__					  =				process_story(__A						)
							self.assertEqual(__A,					[]						)




				def 							A__      (						self					:						List[str]						):
							lowercase__					  =				""
							lowercase__					  =				process_story(__A						)
							self.assertEqual(__A,					[]						)
							self.assertEqual(__A,					[]						)




				def 							A__      (						self					:						Dict						):
							lowercase__					  =				(
							    "It was the year of Our Lord one thousand seven hundred and "
							    "seventy-five\n\nSpiritual revelations were conceded to England "
							    "at that favoured period, as at this.\n@highlight\n\nIt was the best of times"
							)
							lowercase__					  =				process_story(__A						)

							lowercase__					  =				[
							    "It was the year of Our Lord one thousand seven hundred and seventy-five.",
							    "Spiritual revelations were conceded to England at that favoured period, as at this.",
							]
							self.assertEqual(__A,					__A						)

							lowercase__					  =				["It was the best of times."]
							self.assertEqual(__A,					__A						)




				def 							A__      (						self					:						Optional[Any]						):
							lowercase__					  =				torch.tensor([1, 2, 3, 4]						)
							lowercase__					  =				torch.tensor([1, 1, 1, 1]						)
							np.testing.assert_array_equal(build_mask(__A,					0						).numpy(),					expected.numpy()						)




				def 							A__      (						self					:						List[str]						):
							lowercase__					  =				torch.tensor([1, 2, 3, 4, 23, 23, 23]						)
							lowercase__					  =				torch.tensor([1, 1, 1, 1, 0, 0, 0]						)
							np.testing.assert_array_equal(build_mask(__A,					23						).numpy(),					expected.numpy()						)




				def 							A__      (						self					:						List[Any]						):
							lowercase__					  =				torch.tensor([8, 2, 3, 4, 1, 1, 1]						)
							lowercase__					  =				torch.tensor([1, 1, 1, 1, 0, 0, 0]						)
							np.testing.assert_array_equal(build_mask(__A,					1						).numpy(),					expected.numpy()						)




				def 							A__      (						self					:						Optional[Any]						):
							lowercase__					  =				101
							lowercase__					  =				torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 101, 5, 6], [1, 101, 3, 4, 101, 6]]						)
							lowercase__					  =				torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]]						)

							lowercase__					  =				compute_token_type_ids(__A,					__A						)
							np.testing.assert_array_equal(__A,					__A						)









import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel



def __lowerCAmelCase (     SCREAMING_SNAKE_CASE_						,  SCREAMING_SNAKE_CASE_						,  SCREAMING_SNAKE_CASE_  ):
			lowercase__					  =				("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value")

			lowercase__					  =				(
			    ("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(SCREAMING_SNAKE_CASE_  ):
						os.makedirs(SCREAMING_SNAKE_CASE_  )

			lowercase__					  =				model.state_dict()

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

			def create_tf_var(SCREAMING_SNAKE_CASE_						,  SCREAMING_SNAKE_CASE_						,  SCREAMING_SNAKE_CASE_  ):
						lowercase__					  =				tf.dtypes.as_dtype(tensor.dtype  )
						lowercase__					  =				tf.get_variable(dtype=SCREAMING_SNAKE_CASE_						,  shape=tensor.shape						,  name=SCREAMING_SNAKE_CASE_						,  initializer=tf.zeros_initializer()  )
						session.run(tf.variables_initializer([tf_var]  )  )
						session.run(SCREAMING_SNAKE_CASE_  )
						return tf_var

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

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



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

			lowercase__					  =				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=SCREAMING_SNAKE_CASE_						,  ckpt_dir=args.tf_cache_dir						,  model_name=args.model_name  )


if __name__ == "__main__":
							main()




