
from __future__ import annotations

import unittest

import numpy as np

from transformers import OPTConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import GPTaTokenizer, TFOPTForCausalLM, TFOPTModel




def 		A_       (							snake_case							:      List[Any]       ,					snake_case							:      List[Any]       ,					snake_case							:      Union[str, Any]=None       ,					snake_case							:      Optional[int]=None       )					->			List[Any]:



						'''simple docstring'''

						if attention_mask is None:
												__UpperCamelCase        =	tf.cast(tf.math.not_equal(_SCREAMING_SNAKE_CASE       ,					config.pad_token_id       )       ,					tf.inta       )
						return {"input_ids": input_ids, "attention_mask": attention_mask}






@require_tf
class 				SCREAMING_SNAKE_CASE__			:

							"""simple docstring"""






							_snake_case   						=     OPTConfig
							_snake_case   						=     {}
							_snake_case   						=     """gelu"""



							def __init__(     self   ,		SCREAMING_SNAKE_CASE_   ,		SCREAMING_SNAKE_CASE_=13   ,		SCREAMING_SNAKE_CASE_=7   ,		SCREAMING_SNAKE_CASE_=True   ,		SCREAMING_SNAKE_CASE_=False   ,		SCREAMING_SNAKE_CASE_=99   ,		SCREAMING_SNAKE_CASE_=16   ,		SCREAMING_SNAKE_CASE_=2   ,		SCREAMING_SNAKE_CASE_=4   ,		SCREAMING_SNAKE_CASE_=4   ,		SCREAMING_SNAKE_CASE_="gelu"   ,		SCREAMING_SNAKE_CASE_=0.1   ,		SCREAMING_SNAKE_CASE_=0.1   ,		SCREAMING_SNAKE_CASE_=20   ,		SCREAMING_SNAKE_CASE_=2   ,		SCREAMING_SNAKE_CASE_=1   ,		SCREAMING_SNAKE_CASE_=0   ,		SCREAMING_SNAKE_CASE_=16   ,		SCREAMING_SNAKE_CASE_=16   ,		)->      List[str]:





													'''simple docstring'''






													__UpperCamelCase        =	parent
													__UpperCamelCase        =	batch_size
													__UpperCamelCase        =	seq_length
													__UpperCamelCase        =	is_training
													__UpperCamelCase        =	use_labels
													__UpperCamelCase        =	vocab_size
													__UpperCamelCase        =	hidden_size
													__UpperCamelCase        =	num_hidden_layers
													__UpperCamelCase        =	num_attention_heads
													__UpperCamelCase        =	intermediate_size
													__UpperCamelCase        =	hidden_act
													__UpperCamelCase        =	hidden_dropout_prob
													__UpperCamelCase        =	attention_probs_dropout_prob
													__UpperCamelCase        =	max_position_embeddings
													__UpperCamelCase        =	eos_token_id
													__UpperCamelCase        =	pad_token_id
													__UpperCamelCase        =	bos_token_id
													__UpperCamelCase        =	embed_dim
													__UpperCamelCase        =	word_embed_proj_dim
													__UpperCamelCase        =	False



							def 			A__						(     self	)->      Union[str, Any]:





													'''simple docstring'''






													__UpperCamelCase        =	ids_tensor([self.batch_size, self.seq_length - 1]   ,		self.vocab_size	)
													__UpperCamelCase        =	tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size	)   ,		1	)
													__UpperCamelCase        =	tf.concat([input_ids, eos_tensor]   ,		axis=1	)

													__UpperCamelCase        =	self.config_cls(
													    vocab_size=self.vocab_size   ,		hidden_size=self.hidden_size   ,		num_hidden_layers=self.num_hidden_layers   ,		num_attention_heads=self.num_attention_heads   ,		ffn_dim=self.intermediate_size   ,		dropout=self.hidden_dropout_prob   ,		attention_dropout=self.attention_probs_dropout_prob   ,		max_position_embeddings=self.max_position_embeddings   ,		eos_token_id=self.eos_token_id   ,		bos_token_id=self.bos_token_id   ,		pad_token_id=self.pad_token_id   ,		embed_dim=self.embed_dim   ,		word_embed_proj_dim=self.word_embed_proj_dim   ,		is_encoder_decoder=__UpperCAmelCase   ,		**self.config_updates   ,		)
													__UpperCamelCase        =	prepare_opt_inputs_dict(__UpperCAmelCase   ,		__UpperCAmelCase	)
													return config, inputs_dict



							def 			A__						(     self   ,		SCREAMING_SNAKE_CASE_   ,		SCREAMING_SNAKE_CASE_	)->      str:





													'''simple docstring'''






													__UpperCamelCase        =	TFOPTModel(config=__UpperCAmelCase	)
													__UpperCamelCase        =	inputs_dict['input_ids']

													__UpperCamelCase        =	input_ids[:1, :]
													__UpperCamelCase        =	inputs_dict['attention_mask'][:1, :]
													__UpperCamelCase        =	1

													# first forward pass
													__UpperCamelCase        =	model(__UpperCAmelCase   ,		attention_mask=__UpperCAmelCase   ,		use_cache=__UpperCAmelCase	)

													__UpperCamelCase        =	outputs.to_tuple()

													# create hypothetical next token and extent to next_input_ids
													__UpperCamelCase        =	ids_tensor((self.batch_size, 3)   ,		config.vocab_size	)
													__UpperCamelCase        =	tf.cast(ids_tensor((self.batch_size, 3)   ,		2	)   ,		tf.inta	)

													# append to next input_ids and
													__UpperCamelCase        =	tf.concat([input_ids, next_tokens]   ,		axis=-1	)
													__UpperCamelCase        =	tf.concat([attention_mask, next_attn_mask]   ,		axis=-1	)

													__UpperCamelCase        =	model(__UpperCAmelCase   ,		attention_mask=__UpperCAmelCase	)[0]
													__UpperCamelCase        =	model(__UpperCAmelCase   ,		attention_mask=__UpperCAmelCase   ,		past_key_values=__UpperCAmelCase	)[0]

													self.parent.assertEqual(next_tokens.shape[1]   ,		output_from_past.shape[1]	)

													# select random slice
													__UpperCamelCase        =	int(ids_tensor((1,)   ,		output_from_past.shape[-1]	)	)
													__UpperCamelCase        =	output_from_no_past[:, -3:, random_slice_idx]
													__UpperCamelCase        =	output_from_past[:, :, random_slice_idx]

													# test that outputs are equal for slice
													tf.debugging.assert_near(__UpperCAmelCase   ,		__UpperCAmelCase   ,		rtol=1E-3	)






@require_tf
class 				SCREAMING_SNAKE_CASE__			(							SCREAMING_SNAKE_CASE_      ,						SCREAMING_SNAKE_CASE_      ,						unittest.TestCase			):

							"""simple docstring"""






							_snake_case   						=     (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
							_snake_case   						=     (TFOPTForCausalLM,) if is_tf_available() else ()
							_snake_case   						=     (
							    {"""feature-extraction""": TFOPTModel, """text-generation""": TFOPTForCausalLM} if is_tf_available() else {}
							)
							_snake_case   						=     False
							_snake_case   						=     False
							_snake_case   						=     False
							_snake_case   						=     10



							def 			A__						(     self	)->      Optional[Any]:





													'''simple docstring'''






													__UpperCamelCase        =	TFOPTModelTester(self	)
													__UpperCamelCase        =	ConfigTester(self   ,		config_class=__UpperCAmelCase	)



							def 			A__						(     self	)->      int:





													'''simple docstring'''






													self.config_tester.run_common_tests()



							def 			A__						(     self	)->      List[str]:





													'''simple docstring'''






													__UpperCamelCase        =	self.model_tester.prepare_config_and_inputs_for_common()
													self.model_tester.check_decoder_model_past_large_inputs(*__UpperCAmelCase	)



							def 			A__						(     self	)->      Any:





													'''simple docstring'''






													__UpperCamelCase        =	self.model_tester.prepare_config_and_inputs_for_common()

													def _get_word_embedding_weight(SCREAMING_SNAKE_CASE_   ,		SCREAMING_SNAKE_CASE_	):
																			if hasattr(__UpperCAmelCase   ,		'''weight'''	):
																									return embedding_layer.weight
																			else:
																									# Here we build the word embeddings weights if not exists.
																									# And then we retry to get the attribute once built.
																									model.build()
																									if hasattr(__UpperCAmelCase   ,		'''weight'''	):
																															return embedding_layer.weight
																									else:
																															return None

													for model_class in self.all_model_classes:
																			for size in [config.vocab_size - 10, config.vocab_size + 10]:
																									# build the embeddings
																									__UpperCamelCase        =	model_class(config=__UpperCAmelCase	)
																									__UpperCamelCase        =	_get_word_embedding_weight(__UpperCAmelCase   ,		model.get_input_embeddings()	)
																									__UpperCamelCase        =	_get_word_embedding_weight(__UpperCAmelCase   ,		model.get_output_embeddings()	)

																									# reshape the embeddings
																									model.resize_token_embeddings(__UpperCAmelCase	)
																									__UpperCamelCase        =	_get_word_embedding_weight(__UpperCAmelCase   ,		model.get_input_embeddings()	)
																									__UpperCamelCase        =	_get_word_embedding_weight(__UpperCAmelCase   ,		model.get_output_embeddings()	)

																									# check that the resized embeddings size matches the desired size.
																									__UpperCamelCase        =	size if size is not None else config.vocab_size

																									self.assertEqual(new_input_embeddings.shape[0]   ,		__UpperCAmelCase	)

																									# check that weights remain the same after resizing
																									__UpperCamelCase        =	True
																									for pa, pa in zip(old_input_embeddings.value()   ,		new_input_embeddings.value()	):
																															if tf.math.reduce_sum(tf.math.abs(pa - pa	)	) > 0:
																																					__UpperCamelCase        =	False
																									self.assertTrue(__UpperCAmelCase	)

																									if old_output_embeddings is not None and new_output_embeddings is not None:
																															self.assertEqual(new_output_embeddings.shape[0]   ,		__UpperCAmelCase	)

																															__UpperCamelCase        =	True
																															for pa, pa in zip(old_output_embeddings.value()   ,		new_output_embeddings.value()	):
																																					if tf.math.reduce_sum(tf.math.abs(pa - pa	)	) > 0:
																																											__UpperCamelCase        =	False
																															self.assertTrue(__UpperCAmelCase	)





def 		A_       (							snake_case							:      Dict       )					->			Optional[Any]:



						'''simple docstring'''

						return tf.constant(_SCREAMING_SNAKE_CASE       ,					dtype=tf.intaa       )






@require_tf
class 				SCREAMING_SNAKE_CASE__			(							unittest.TestCase			):

							"""simple docstring"""






							_snake_case   						=     99



							def 			A__						(     self	)->      Optional[int]:





													'''simple docstring'''






													__UpperCamelCase        =	tf.ones((4, 1)   ,		dtype=tf.intaa	) * 2
													__UpperCamelCase        =	tf.concat([ids_tensor((4, 6)   ,		self.vocab_size - 3	) + 3, eos_column_vector]   ,		axis=1	)
													__UpperCamelCase        =	input_ids.shape[0]
													__UpperCamelCase        =	OPTConfig(
													    vocab_size=self.vocab_size   ,		hidden_size=24   ,		num_hidden_layers=2   ,		num_attention_heads=2   ,		ffn_dim=32   ,		max_position_embeddings=48   ,		eos_token_id=2   ,		pad_token_id=1   ,		bos_token_id=0   ,		)
													return config, input_ids, batch_size






@require_sentencepiece
@require_tf
class 				SCREAMING_SNAKE_CASE__			(							unittest.TestCase			):

							"""simple docstring"""






							@slow
							def 			A__						(     self	)->      Optional[Any]:





													'''simple docstring'''






													__UpperCamelCase        =	TFOPTModel.from_pretrained('''facebook/opt-350m'''	)
													__UpperCamelCase        =	_long_tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]	)
													__UpperCamelCase        =	tf.not_equal(__UpperCAmelCase   ,		model.config.pad_token_id	)
													with tf.GradientTape():
																			__UpperCamelCase        =	model(input_ids=__UpperCAmelCase   ,		attention_mask=__UpperCAmelCase	).last_hidden_state
													__UpperCamelCase        =	(1, 11, 512)
													self.assertEqual(output.shape   ,		__UpperCAmelCase	)
													__UpperCamelCase        =	tf.constant(
													    [[-0.2_8_7_3, -1.9_2_1_8, -0.3_0_3_3], [-1.2_7_1_0, -0.1_3_3_8, -0.1_9_0_2], [0.4_0_9_5, 0.1_2_1_4, -1.3_1_2_1]]	)
													self.assertTrue(np.allclose(output[:, :3, :3]   ,		__UpperCAmelCase   ,		atol=4E-3	)	)

													__UpperCamelCase        =	tf.function(__UpperCAmelCase   ,		jit_compile=__UpperCAmelCase	)
													__UpperCamelCase        =	xla_generate(__UpperCAmelCase   ,		__UpperCAmelCase	)[0]
													self.assertTrue(np.allclose(output[:, :3, :3]   ,		__UpperCAmelCase   ,		atol=4E-2	)	)






@require_tf
@slow
class 				SCREAMING_SNAKE_CASE__			(							unittest.TestCase			):

							"""simple docstring"""






							def 			A__						(     self	)->      List[Any]:





													'''simple docstring'''






													super().setUp()
													__UpperCamelCase        =	'facebook/opt-350m'



							def 			A__						(     self	)->      Tuple:





													'''simple docstring'''






													__UpperCamelCase        =	TFOPTForCausalLM.from_pretrained(self.path_model	)
													__UpperCamelCase        =	GPTaTokenizer.from_pretrained(self.path_model	)

													__UpperCamelCase        =	[
													    'Today is a beautiful day and I want to',
													    'In the city of',
													    'Paris is the capital of France and',
													    'Computers and mobile phones have taken',
													]
													# verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
													__UpperCamelCase        =	tokenizer(__UpperCAmelCase   ,		return_tensors='''tf'''   ,		padding=__UpperCAmelCase   ,		add_special_tokens=__UpperCAmelCase	)
													__UpperCamelCase        =	tf.math.reduce_mean(model(inputs.input_ids   ,		attention_mask=inputs.attention_mask	)[0]   ,		axis=-1	)
													__UpperCamelCase        =	tf.constant(
													    [
													        [1.3_8_5_1, -1_3.8_9_2_3, -1_0.5_2_2_9, -1_0.7_5_3_3, -0.2_3_0_9, -1_0.2_3_8_4, -0.5_3_6_5, -9.0_9_4_7, -5.1_6_7_0],
													        [-4.7_0_7_3, -1_0.6_2_7_6, -3.9_4_1_5, -2_1.5_2_4_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2],
													        [0.6_2_4_7, -3.4_2_2_9, -8.9_1_7_9, -1.4_2_9_7, -1_4.1_6_5_0, 1.4_1_4_6, -9.0_2_1_8, -0.2_7_0_3, -0.2_7_0_3],
													        [6.4_7_8_3, -1.9_9_1_3, -1_0.7_9_2_6, -2.3_3_3_6, 1.5_0_9_2, -0.9_9_7_4, -6.8_2_1_3, 1.3_4_7_7, 1.3_4_7_7],
													    ]	)
													self.assertTrue(np.allclose(__UpperCAmelCase   ,		__UpperCAmelCase   ,		atol=1E-4	)	)

													__UpperCamelCase        =	tf.function(__UpperCAmelCase   ,		jit_compile=__UpperCAmelCase	)
													__UpperCamelCase        =	tf.math.reduce_mean(xla_generate(inputs.input_ids   ,		attention_mask=inputs.attention_mask	)[0]   ,		axis=-1	)
													self.assertTrue(np.allclose(__UpperCAmelCase   ,		__UpperCAmelCase   ,		atol=1E-4	)	)






@require_tf
@slow
class 				SCREAMING_SNAKE_CASE__			(							unittest.TestCase			):

							"""simple docstring"""






							@property
							def 			A__						(     self	)->      List[Any]:





													'''simple docstring'''






													return [
													    "Today is a beautiful day and I want",
													    "In the city of",
													    "Paris is the capital of France and",
													    "Computers and mobile phones have taken",
													]



							def 			A__						(     self	)->      Any:





													'''simple docstring'''






													__UpperCamelCase        =	'facebook/opt-125m'

													__UpperCamelCase        =	[
													    'Today is a beautiful day and I want to',
													    'In the city of New York, the city',
													    'Paris is the capital of France and the capital',
													    'Computers and mobile phones have taken over the',
													]

													__UpperCamelCase        =	[]
													__UpperCamelCase        =	GPTaTokenizer.from_pretrained(__UpperCAmelCase	)
													__UpperCamelCase        =	TFOPTForCausalLM.from_pretrained(__UpperCAmelCase	)

													for prompt in self.prompts:
																			__UpperCamelCase        =	tokenizer(__UpperCAmelCase   ,		return_tensors='''tf'''	).input_ids

																			__UpperCamelCase        =	model.generate(__UpperCAmelCase   ,		max_length=10	)

																			__UpperCamelCase        =	tokenizer.batch_decode(__UpperCAmelCase   ,		skip_special_tokens=__UpperCAmelCase	)
																			predicted_outputs += generated_string

													self.assertListEqual(__UpperCAmelCase   ,		__UpperCAmelCase	)



							def 			A__						(     self	)->      str:





													'''simple docstring'''






													__UpperCamelCase        =	'facebook/opt-350m'

													__UpperCamelCase        =	GPTaTokenizer.from_pretrained(__UpperCAmelCase	)
													__UpperCamelCase        =	TFOPTForCausalLM.from_pretrained(__UpperCAmelCase	)

													__UpperCamelCase        =	'left'

													# use different length sentences to test batching
													__UpperCamelCase        =	[
													    'Hello, my dog is a little',
													    'Today, I',
													]

													__UpperCamelCase        =	tokenizer(__UpperCAmelCase   ,		return_tensors='''tf'''   ,		padding=__UpperCAmelCase	)
													__UpperCamelCase        =	inputs['input_ids']

													__UpperCamelCase        =	model.generate(input_ids=__UpperCAmelCase   ,		attention_mask=inputs['''attention_mask''']	)

													__UpperCamelCase        =	tokenizer(sentences[0]   ,		return_tensors='''tf'''	).input_ids
													__UpperCamelCase        =	model.generate(input_ids=__UpperCAmelCase	)

													__UpperCamelCase        =	inputs_non_padded.shape[-1] - tf.math.reduce_sum(
													    tf.cast(inputs['''attention_mask'''][-1]   ,		tf.intaa	)	)
													__UpperCamelCase        =	tokenizer(sentences[1]   ,		return_tensors='''tf'''	).input_ids
													__UpperCamelCase        =	model.generate(input_ids=__UpperCAmelCase   ,		max_length=model.config.max_length - num_paddings	)

													__UpperCamelCase        =	tokenizer.batch_decode(__UpperCAmelCase   ,		skip_special_tokens=__UpperCAmelCase	)
													__UpperCamelCase        =	tokenizer.decode(output_non_padded[0]   ,		skip_special_tokens=__UpperCAmelCase	)
													__UpperCamelCase        =	tokenizer.decode(output_padded[0]   ,		skip_special_tokens=__UpperCAmelCase	)

													__UpperCamelCase        =	[
													    'Hello, my dog is a little bit of a dork.\nI\'m a little bit',
													    'Today, I was in the middle of a conversation with a friend about the',
													]
													self.assertListEqual(__UpperCAmelCase   ,		__UpperCAmelCase	)
													self.assertListEqual(__UpperCAmelCase   ,		[non_padded_sentence, padded_sentence]	)



							def 			A__						(     self	)->      Any:





													'''simple docstring'''






													__UpperCamelCase        =	'facebook/opt-350m'

													__UpperCamelCase        =	[
													    'Today is a beautiful day and I want to',
													    'In the city of San Francisco, the city',
													    'Paris is the capital of France and the capital',
													    'Computers and mobile phones have taken over the',
													]

													__UpperCamelCase        =	[]
													__UpperCamelCase        =	GPTaTokenizer.from_pretrained(__UpperCAmelCase	)
													__UpperCamelCase        =	TFOPTForCausalLM.from_pretrained(__UpperCAmelCase	)

													for prompt in self.prompts:
																			__UpperCamelCase        =	tokenizer(__UpperCAmelCase   ,		return_tensors='''tf'''	).input_ids

																			__UpperCamelCase        =	model.generate(__UpperCAmelCase   ,		max_length=10	)

																			__UpperCamelCase        =	tokenizer.batch_decode(__UpperCAmelCase   ,		skip_special_tokens=__UpperCAmelCase	)
																			predicted_outputs += generated_string

													self.assertListEqual(__UpperCAmelCase   ,		__UpperCAmelCase	)











"""simple docstring"""





import argparse

import torch

from transformers import BertConfig, BertForPreTraining, load_tf_weights_in_bert
from transformers.utils import logging


logging.set_verbosity_info()
def  __A   (_SCREAMING_SNAKE_CASE     ,   _SCREAMING_SNAKE_CASE     ,   _SCREAMING_SNAKE_CASE			)					->Dict:





  """simple docstring"""


  lowerCAmelCase__			:str											=							BertConfig.from_json_file(_SCREAMING_SNAKE_CASE			)
  print(F"Building PyTorch model from configuration: {config}"			)
  lowerCAmelCase__			:int											=							BertForPreTraining(_SCREAMING_SNAKE_CASE			)

  # Load weights from tf checkpoint
  load_tf_weights_in_bert(_SCREAMING_SNAKE_CASE     ,   _SCREAMING_SNAKE_CASE     ,   _SCREAMING_SNAKE_CASE			)

  # Save pytorch-model
  print(F"Save PyTorch model to {pytorch_dump_path}"			)
  torch.save(model.state_dict()     ,   _SCREAMING_SNAKE_CASE			)


if __name__ == "__main__":
  __A											=		argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      """--tf_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
  )
  parser.add_argument(
      """--bert_config_file""",
      default=None,
      type=str,
      required=True,
      help=(
          """The config json file corresponding to the pre-trained BERT model. \n"""
          """This specifies the model architecture."""
      ),
  )
  parser.add_argument(
      """--pytorch_dump_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
  )
  __A											=		parser.parse_args()
  convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)
