
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       _SCREAMING_SNAKE_CASE   (						_lowerCamelCase					:     str		,    _lowerCamelCase					:     str		,    _lowerCamelCase					:     List[Any]=None		,    _lowerCamelCase					:     Any=None)   ->       Any:






							'''simple docstring'''





							if attention_mask is None:
														__UpperCamelCase      :  Union[str, Any]       =   tf.cast(tf.math.not_equal(_lowerCamelCase		,    config.pad_token_id)		,    tf.inta)
							return {"input_ids": input_ids, "attention_mask": attention_mask}




@require_tf
class 					lowerCamelCase__				:




	'''simple docstring'''




	_A								=	OPTConfig
	_A								=	{}
	_A								=	'gelu'







	def __init__(   self						:Dict	,  a						:Union[str, Any]	,  a						:Any=1_3	,  a						:Tuple=7	,  a						:Optional[int]=True	,  a						:str=False	,  a						:Dict=9_9	,  a						:Tuple=1_6	,  a						:Optional[Any]=2	,  a						:Any=4	,  a						:List[str]=4	,  a						:Optional[Any]="gelu"	,  a						:str=0.1	,  a						:Optional[Any]=0.1	,  a						:Dict=2_0	,  a						:Optional[int]=2	,  a						:Optional[int]=1	,  a						:Union[str, Any]=0	,  a						:Optional[int]=1_6	,  a						:List[Any]=1_6	,  )						->					Dict:
								__UpperCamelCase      :  Dict       =   parent
								__UpperCamelCase      :  str       =   batch_size
								__UpperCamelCase      :  Tuple       =   seq_length
								__UpperCamelCase      :  Dict       =   is_training
								__UpperCamelCase      :  Tuple       =   use_labels
								__UpperCamelCase      :  List[Any]       =   vocab_size
								__UpperCamelCase      :  str       =   hidden_size
								__UpperCamelCase      :  List[Any]       =   num_hidden_layers
								__UpperCamelCase      :  List[str]       =   num_attention_heads
								__UpperCamelCase      :  str       =   intermediate_size
								__UpperCamelCase      :  Any       =   hidden_act
								__UpperCamelCase      :  Tuple       =   hidden_dropout_prob
								__UpperCamelCase      :  int       =   attention_probs_dropout_prob
								__UpperCamelCase      :  List[str]       =   max_position_embeddings
								__UpperCamelCase      :  List[str]       =   eos_token_id
								__UpperCamelCase      :  Union[str, Any]       =   pad_token_id
								__UpperCamelCase      :  Optional[int]       =   bos_token_id
								__UpperCamelCase      :  List[Any]       =   embed_dim
								__UpperCamelCase      :  List[Any]       =   word_embed_proj_dim
								__UpperCamelCase      :  Optional[Any]       =   False







	def   _lowerCamelCase							(   self						:Any		)						->					str:
								__UpperCamelCase      :  Tuple       =   ids_tensor([self.batch_size, self.seq_length - 1]	,  self.vocab_size		)
								__UpperCamelCase      :  Optional[int]       =   tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size		)	,  1		)
								__UpperCamelCase      :  Union[str, Any]       =   tf.concat([input_ids, eos_tensor]	,  axis=1		)

								__UpperCamelCase      :  Optional[int]       =   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=a	,  **self.config_updates	,  )
								__UpperCamelCase      :  Optional[int]       =   prepare_opt_inputs_dict(a	,  a		)
								return config, inputs_dict







	def   _lowerCamelCase							(   self						:Optional[Any]	,  a						:Optional[Any]	,  a						:Optional[int]		)						->					Optional[int]:
								__UpperCamelCase      :  List[Any]       =   TFOPTModel(config=a		)
								__UpperCamelCase      :  str       =   inputs_dict["input_ids"]

								__UpperCamelCase      :  Dict       =   input_ids[:1, :]
								__UpperCamelCase      :  List[str]       =   inputs_dict["attention_mask"][:1, :]
								__UpperCamelCase      :  Optional[Any]       =   1

								# first forward pass
								__UpperCamelCase      :  Optional[Any]       =   model(a	,  attention_mask=a	,  use_cache=a		)

								__UpperCamelCase  ,		__UpperCamelCase      :  int       =   outputs.to_tuple()

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

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

								__UpperCamelCase      :  Optional[Any]       =   model(a	,  attention_mask=a		)[0]
								__UpperCamelCase      :  str       =   model(a	,  attention_mask=a	,  past_key_values=a		)[0]

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

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

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




@require_tf
class 					lowerCamelCase__				(							__lowercase	,		__lowercase	,		unittest.TestCase):




	'''simple docstring'''




	_A								=	(TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
	_A								=	(TFOPTForCausalLM,) if is_tf_available() else ()
	_A								=	(
	    {'feature-extraction': TFOPTModel, 'text-generation': TFOPTForCausalLM} if is_tf_available() else {}
	)
	_A								=	False
	_A								=	False
	_A								=	False
	_A								=	1_0







	def   _lowerCamelCase							(   self						:Any		)						->					List[Any]:
								__UpperCamelCase      :  List[str]       =   TFOPTModelTester(self		)
								__UpperCamelCase      :  Tuple       =   ConfigTester(self	,  config_class=a		)







	def   _lowerCamelCase							(   self						:List[str]		)						->					Union[str, Any]:
								self.config_tester.run_common_tests()







	def   _lowerCamelCase							(   self						:List[Any]		)						->					Any:
								__UpperCamelCase      :  Optional[Any]       =   self.model_tester.prepare_config_and_inputs_for_common()
								self.model_tester.check_decoder_model_past_large_inputs(*a		)







	def   _lowerCamelCase							(   self						:Tuple		)						->					int:
								__UpperCamelCase  ,		__UpperCamelCase      :  Dict       =   self.model_tester.prepare_config_and_inputs_for_common()

								def _get_word_embedding_weight(a						:Optional[int]	,  a						:Optional[Any]		):
															if hasattr(a	,  "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(a	,  "weight"		):
																													return embedding_layer.weight
																						else:
																													return None

								for model_class in self.all_model_classes:
															for size in [config.vocab_size - 1_0, config.vocab_size + 1_0]:
																						# build the embeddings
																						__UpperCamelCase      :  Tuple       =   model_class(config=a		)
																						__UpperCamelCase      :  str       =   _get_word_embedding_weight(a	,  model.get_input_embeddings()		)
																						__UpperCamelCase      :  str       =   _get_word_embedding_weight(a	,  model.get_output_embeddings()		)

																						# reshape the embeddings
																						model.resize_token_embeddings(a		)
																						__UpperCamelCase      :  int       =   _get_word_embedding_weight(a	,  model.get_input_embeddings()		)
																						__UpperCamelCase      :  Union[str, Any]       =   _get_word_embedding_weight(a	,  model.get_output_embeddings()		)

																						# check that the resized embeddings size matches the desired size.
																						__UpperCamelCase      :  List[str]       =   size if size is not None else config.vocab_size

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

																						# check that weights remain the same after resizing
																						__UpperCamelCase      :  Optional[int]       =   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      :  Union[str, Any]       =   False
																						self.assertTrue(a		)

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

																													__UpperCamelCase      :  Union[str, Any]       =   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      :  Tuple       =   False
																													self.assertTrue(a		)




def       _SCREAMING_SNAKE_CASE   (						_lowerCamelCase					:     List[Any])   ->       Optional[Any]:






							'''simple docstring'''





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




@require_tf
class 					lowerCamelCase__				(							unittest.TestCase):




	'''simple docstring'''




	_A								=	9_9







	def   _lowerCamelCase							(   self						:str		)						->					Union[str, Any]:
								__UpperCamelCase      :  str       =   tf.ones((4, 1)	,  dtype=tf.intaa		) * 2
								__UpperCamelCase      :  List[Any]       =   tf.concat([ids_tensor((4, 6)	,  self.vocab_size - 3		) + 3, eos_column_vector]	,  axis=1		)
								__UpperCamelCase      :  Dict       =   input_ids.shape[0]
								__UpperCamelCase      :  Any       =   OPTConfig(
								    vocab_size=self.vocab_size	,  hidden_size=2_4	,  num_hidden_layers=2	,  num_attention_heads=2	,  ffn_dim=3_2	,  max_position_embeddings=4_8	,  eos_token_id=2	,  pad_token_id=1	,  bos_token_id=0	,  )
								return config, input_ids, batch_size




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




	'''simple docstring'''




	@slow
	def   _lowerCamelCase							(   self						:Tuple		)						->					Dict:
								__UpperCamelCase      :  int       =   TFOPTModel.from_pretrained("facebook/opt-350m"		)
								__UpperCamelCase      :  int       =   _long_tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]		)
								__UpperCamelCase      :  Optional[Any]       =   tf.not_equal(a	,  model.config.pad_token_id		)
								with tf.GradientTape():
															__UpperCamelCase      :  str       =   model(input_ids=a	,  attention_mask=a		).last_hidden_state
								__UpperCamelCase      :  Dict       =   (1, 1_1, 5_1_2)
								self.assertEqual(output.shape	,  a		)
								__UpperCamelCase      :  Dict       =   tf.constant(
								    [[-0.2873, -1.9218, -0.3033], [-1.2710, -0.1338, -0.1902], [0.4095, 0.1214, -1.3121]]		)
								self.assertTrue(np.allclose(output[:, :3, :3]	,  a	,  atol=4E-3		)		)

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




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




	'''simple docstring'''




	def   _lowerCamelCase							(   self						:List[Any]		)						->					Optional[Any]:
								super().setUp()
								__UpperCamelCase      :  Dict       =   "facebook/opt-350m"







	def   _lowerCamelCase							(   self						:Any		)						->					Optional[int]:
								__UpperCamelCase      :  Dict       =   TFOPTForCausalLM.from_pretrained(self.path_model		)
								__UpperCamelCase      :  Tuple       =   GPTaTokenizer.from_pretrained(self.path_model		)

								__UpperCamelCase      :  int       =   [
								    "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      :  Optional[Any]       =   tokenizer(a	,  return_tensors="tf"	,  padding=a	,  add_special_tokens=a		)
								__UpperCamelCase      :  Any       =   tf.math.reduce_mean(model(inputs.input_ids	,  attention_mask=inputs.attention_mask		)[0]	,  axis=-1		)
								__UpperCamelCase      :  List[str]       =   tf.constant(
								    [
								        [1.3851, -13.8923, -10.5229, -10.7533, -0.2309, -10.2384, -0.5365, -9.0947, -5.1670],
								        [-4.7073, -10.6276, -3.9415, -21.5242, -0.2822, -0.2822, -0.2822, -0.2822, -0.2822],
								        [0.6247, -3.4229, -8.9179, -1.4297, -14.1650, 1.4146, -9.0218, -0.2703, -0.2703],
								        [6.4783, -1.9913, -10.7926, -2.3336, 1.5092, -0.9974, -6.8213, 1.3477, 1.3477],
								    ]		)
								self.assertTrue(np.allclose(a	,  a	,  atol=1E-4		)		)

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




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




	'''simple docstring'''




	@property
	def   _lowerCamelCase							(   self						:Optional[int]		)						->					Optional[Any]:
								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   _lowerCamelCase							(   self						:Any		)						->					List[str]:
								__UpperCamelCase      :  Union[str, Any]       =   "facebook/opt-125m"

								__UpperCamelCase      :  Optional[int]       =   [
								    "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      :  Any       =   []
								__UpperCamelCase      :  Dict       =   GPTaTokenizer.from_pretrained(a		)
								__UpperCamelCase      :  Any       =   TFOPTForCausalLM.from_pretrained(a		)

								for prompt in self.prompts:
															__UpperCamelCase      :  Optional[int]       =   tokenizer(a	,  return_tensors="tf"		).input_ids

															__UpperCamelCase      :  List[Any]       =   model.generate(a	,  max_length=1_0		)

															__UpperCamelCase      :  Optional[int]       =   tokenizer.batch_decode(a	,  skip_special_tokens=a		)
															predicted_outputs += generated_string

								self.assertListEqual(a	,  a		)







	def   _lowerCamelCase							(   self						:Optional[int]		)						->					Dict:
								__UpperCamelCase      :  Union[str, Any]       =   "facebook/opt-350m"

								__UpperCamelCase      :  Optional[Any]       =   GPTaTokenizer.from_pretrained(a		)
								__UpperCamelCase      :  List[Any]       =   TFOPTForCausalLM.from_pretrained(a		)

								__UpperCamelCase      :  Dict       =   "left"

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

								__UpperCamelCase      :  Optional[int]       =   tokenizer(a	,  return_tensors="tf"	,  padding=a		)
								__UpperCamelCase      :  Dict       =   inputs["input_ids"]

								__UpperCamelCase      :  Tuple       =   model.generate(input_ids=a	,  attention_mask=inputs["attention_mask"]		)

								__UpperCamelCase      :  int       =   tokenizer(sentences[0]	,  return_tensors="tf"		).input_ids
								__UpperCamelCase      :  int       =   model.generate(input_ids=a		)

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

								__UpperCamelCase      :  List[str]       =   tokenizer.batch_decode(a	,  skip_special_tokens=a		)
								__UpperCamelCase      :  List[Any]       =   tokenizer.decode(output_non_padded[0]	,  skip_special_tokens=a		)
								__UpperCamelCase      :  int       =   tokenizer.decode(output_padded[0]	,  skip_special_tokens=a		)

								__UpperCamelCase      :  Optional[int]       =   [
								    "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(a	,  a		)
								self.assertListEqual(a	,  [non_padded_sentence, padded_sentence]		)







	def   _lowerCamelCase							(   self						:str		)						->					Dict:
								__UpperCamelCase      :  Dict       =   "facebook/opt-350m"

								__UpperCamelCase      :  Any       =   [
								    "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      :  int       =   []
								__UpperCamelCase      :  str       =   GPTaTokenizer.from_pretrained(a		)
								__UpperCamelCase      :  Any       =   TFOPTForCausalLM.from_pretrained(a		)

								for prompt in self.prompts:
															__UpperCamelCase      :  Dict       =   tokenizer(a	,  return_tensors="tf"		).input_ids

															__UpperCamelCase      :  Optional[int]       =   model.generate(a	,  max_length=1_0		)

															__UpperCamelCase      :  List[Any]       =   tokenizer.batch_decode(a	,  skip_special_tokens=a		)
															predicted_outputs += generated_string

								self.assertListEqual(a	,  a		)

from .testing import (
    are_the_same_tensors,
    execute_subprocess_async,
    require_bnb,
    require_cpu,
    require_cuda,
    require_huggingface_suite,
    require_mps,
    require_multi_gpu,
    require_multi_xpu,
    require_safetensors,
    require_single_gpu,
    require_single_xpu,
    require_torch_min_version,
    require_tpu,
    require_xpu,
    skip,
    slow,
)
from .training import RegressionDataset, RegressionModel, RegressionModelaXPU


from .scripts import test_script, test_sync, test_ops  # isort: skip