



import unittest

from transformers import MPNetConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    MPNetForMaskedLM,
							    MPNetForMultipleChoice,
							    MPNetForQuestionAnswering,
							    MPNetForSequenceClassification,
							    MPNetForTokenClassification,
							    MPNetModel,
							)
class 				_snake_case			:




				def __init__(						self					:						List[str],					__lowercase					:						Union[str, Any],					__lowercase					:						int=13,					__lowercase					:						List[Any]=7,					__lowercase					:						str=True,					__lowercase					:						List[Any]=True,					__lowercase					:						Union[str, Any]=False,					__lowercase					:						Optional[int]=True,					__lowercase					:						Dict=99,					__lowercase					:						List[Any]=64,					__lowercase					:						Optional[int]=5,					__lowercase					:						Union[str, Any]=4,					__lowercase					:						List[Any]=64,					__lowercase					:						Union[str, Any]="gelu",					__lowercase					:						Any=0.1,					__lowercase					:						Optional[Any]=0.1,					__lowercase					:						Dict=512,					__lowercase					:						Tuple=16,					__lowercase					:						int=2,					__lowercase					:						List[str]=0.02,					__lowercase					:						Union[str, Any]=3,					__lowercase					:						str=4,					__lowercase					:						int=None,					):
							lowercase__					  =				parent
							lowercase__					  =				batch_size
							lowercase__					  =				seq_length
							lowercase__					  =				is_training
							lowercase__					  =				use_input_mask
							lowercase__					  =				use_token_type_ids
							lowercase__					  =				use_labels
							lowercase__					  =				vocab_size
							lowercase__					  =				hidden_size
							lowercase__					  =				num_hidden_layers
							lowercase__					  =				num_attention_heads
							lowercase__					  =				intermediate_size
							lowercase__					  =				hidden_act
							lowercase__					  =				hidden_dropout_prob
							lowercase__					  =				attention_probs_dropout_prob
							lowercase__					  =				max_position_embeddings
							lowercase__					  =				type_vocab_size
							lowercase__					  =				type_sequence_label_size
							lowercase__					  =				initializer_range
							lowercase__					  =				num_labels
							lowercase__					  =				num_choices
							lowercase__					  =				scope




				def 							A__      (						self					:						str						):
							return MPNetConfig.from_pretrained("microsoft/mpnet-base"						)




				def 							A__      (						self					:						str						):
							lowercase__					  =				ids_tensor([self.batch_size, self.seq_length],					self.vocab_size						)

							lowercase__					  =				None
							if self.use_input_mask:
										lowercase__					  =				random_attention_mask([self.batch_size, self.seq_length]						)

							lowercase__					  =				None
							lowercase__					  =				None
							lowercase__					  =				None
							if self.use_labels:
										lowercase__					  =				ids_tensor([self.batch_size],					self.type_sequence_label_size						)
										lowercase__					  =				ids_tensor([self.batch_size, self.seq_length],					self.num_labels						)
										lowercase__					  =				ids_tensor([self.batch_size],					self.num_choices						)

							lowercase__					  =				self.get_config()
							return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels




				def 							A__      (						self					:						Any						):
							return MPNetConfig(
							    vocab_size=self.vocab_size,					hidden_size=self.hidden_size,					num_hidden_layers=self.num_hidden_layers,					num_attention_heads=self.num_attention_heads,					intermediate_size=self.intermediate_size,					hidden_act=self.hidden_act,					hidden_dropout_prob=self.hidden_dropout_prob,					attention_probs_dropout_prob=self.attention_probs_dropout_prob,					max_position_embeddings=self.max_position_embeddings,					initializer_range=self.initializer_range,					)




				def 							A__      (						self					:						List[Any],					__lowercase					:						List[Any],					__lowercase					:						Union[str, Any],					__lowercase					:						Optional[int],					__lowercase					:						Optional[int],					__lowercase					:						Tuple,					__lowercase					:						Optional[Any]						):
							lowercase__					  =				MPNetModel(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							lowercase__					  =				model(__lowercase,					__lowercase						)
							lowercase__					  =				model(__lowercase						)
							self.parent.assertEqual(result.last_hidden_state.shape,					(self.batch_size, self.seq_length, self.hidden_size)						)
							self.parent.assertEqual(result.pooler_output.shape,					(self.batch_size, self.hidden_size)						)




				def 							A__      (						self					:						int,					__lowercase					:						List[Any],					__lowercase					:						int,					__lowercase					:						List[str],					__lowercase					:						Optional[int],					__lowercase					:						int,					__lowercase					:						List[Any]						):
							lowercase__					  =				MPNetForQuestionAnswering(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							lowercase__					  =				model(
							    __lowercase,					attention_mask=__lowercase,					start_positions=__lowercase,					end_positions=__lowercase,					)
							self.parent.assertEqual(result.start_logits.shape,					(self.batch_size, self.seq_length)						)
							self.parent.assertEqual(result.end_logits.shape,					(self.batch_size, self.seq_length)						)




				def 							A__      (						self					:						Tuple,					__lowercase					:						Any,					__lowercase					:						Optional[Any],					__lowercase					:						Tuple,					__lowercase					:						Tuple,					__lowercase					:						List[Any],					__lowercase					:						Optional[int]						):
							lowercase__					  =				self.num_labels
							lowercase__					  =				MPNetForSequenceClassification(__lowercase						)
							model.to(__lowercase						)
							model.eval()
							lowercase__					  =				model(__lowercase,					attention_mask=__lowercase,					labels=__lowercase						)
							self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.num_labels)						)




				def 							A__      (						self					:						Union[str, Any],					__lowercase					:						Optional[Any],					__lowercase					:						Optional[int],					__lowercase					:						Dict,					__lowercase					:						Tuple,					__lowercase					:						Any,					__lowercase					:						List[str]						):
							lowercase__					  =				self.num_choices
							lowercase__					  =				MPNetForMultipleChoice(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							lowercase__					  =				input_ids.unsqueeze(1						).expand(-1,					self.num_choices,					-1						).contiguous()
							lowercase__					  =				input_mask.unsqueeze(1						).expand(-1,					self.num_choices,					-1						).contiguous()
							lowercase__					  =				model(
							    __lowercase,					attention_mask=__lowercase,					labels=__lowercase,					)
							self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.num_choices)						)




				def 							A__      (						self					:						Tuple,					__lowercase					:						Optional[int],					__lowercase					:						Any,					__lowercase					:						List[Any],					__lowercase					:						Optional[Any],					__lowercase					:						int,					__lowercase					:						Optional[int]						):
							lowercase__					  =				self.num_labels
							lowercase__					  =				MPNetForTokenClassification(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							lowercase__					  =				model(__lowercase,					attention_mask=__lowercase,					labels=__lowercase						)
							self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.seq_length, self.num_labels)						)




				def 							A__      (						self					:						Tuple						):
							lowercase__					  =				self.prepare_config_and_inputs()
							((lowercase__)  ,       (lowercase__)  ,       (lowercase__)  ,       (lowercase__)  ,       (lowercase__)  ,       (lowercase__))					  =				config_and_inputs
							lowercase__					  =				{"input_ids": input_ids, "attention_mask": input_mask}
							return config, inputs_dict
@require_torch
class 				_snake_case			(					lowercase__    ,	lowercase__    ,	unittest.TestCase):
				UpperCamelCase__     		=(
				    (
				        MPNetForMaskedLM,
				        MPNetForMultipleChoice,
				        MPNetForQuestionAnswering,
				        MPNetForSequenceClassification,
				        MPNetForTokenClassification,
				        MPNetModel,
				    )
				    if is_torch_available()
				    else ()
				)
				UpperCamelCase__     		=(
				    {
				        """feature-extraction""": MPNetModel,
				        """fill-mask""": MPNetForMaskedLM,
				        """question-answering""": MPNetForQuestionAnswering,
				        """text-classification""": MPNetForSequenceClassification,
				        """token-classification""": MPNetForTokenClassification,
				        """zero-shot""": MPNetForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				UpperCamelCase__     		=False
				UpperCamelCase__     		=True




				def 							A__      (						self					:						List[Any]						):
							lowercase__					  =				MPNetModelTester(self						)
							lowercase__					  =				ConfigTester(self,					config_class=__lowercase,					hidden_size=37						)




				def 							A__      (						self					:						List[str]						):
							self.config_tester.run_common_tests()




				def 							A__      (						self					:						Tuple						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_model(*__lowercase						)




				def 							A__      (						self					:						List[Any]						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_sequence_classification(*__lowercase						)




				def 							A__      (						self					:						List[Any]						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_multiple_choice(*__lowercase						)




				def 							A__      (						self					:						Any						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_token_classification(*__lowercase						)




				def 							A__      (						self					:						int						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_question_answering(*__lowercase						)
@require_torch
class 				_snake_case			(					unittest.TestCase):




				@slow
				def 							A__      (						self					:						Dict						):
							lowercase__					  =				MPNetModel.from_pretrained("microsoft/mpnet-base"						)
							lowercase__					  =				torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]						)
							lowercase__					  =				model(__lowercase						)[0]
							lowercase__					  =				torch.Size((1, 11, 768)						)
							self.assertEqual(output.shape,					__lowercase						)
							lowercase__					  =				torch.tensor(
							    [[[-0.0550, 0.1943, -0.0740], [-0.0562, 0.2211, -0.0579], [-0.0437, 0.3337, -0.0641]]]						)
							# compare the actual values for a slice.
							self.assertTrue(torch.allclose(output[:, :3, :3],					__lowercase,					atol=1e-4						)						)









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()




