





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				(							UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase=None  ,  UpperCAmelCase=None				):
					if attention_mask is None:
										_snake_case       :					List[str]				       =   tf.cast(tf.math.not_equal(UpperCAmelCase  ,  config.pad_token_id				)  ,  tf.inta				)
					return {"input_ids": input_ids, "attention_mask": attention_mask}






@require_tf
class        _a:
		lowerCamelCase__     :List[str]   				=		OPTConfig
		lowerCamelCase__     :Any   				=		{}
		lowerCamelCase__     :Tuple   				=		'gelu'







		def __init__(    self ,						__snake_case ,						__snake_case=1_3 ,						__snake_case=7 ,						__snake_case=True ,						__snake_case=False ,						__snake_case=9_9 ,						__snake_case=1_6 ,						__snake_case=2 ,						__snake_case=4 ,						__snake_case=4 ,						__snake_case="gelu" ,						__snake_case=0.1 ,						__snake_case=0.1 ,						__snake_case=2_0 ,						__snake_case=2 ,						__snake_case=1 ,						__snake_case=0 ,						__snake_case=1_6 ,						__snake_case=1_6 ,						)      ->      str:


							'''simple docstring'''

							_snake_case       :					List[Any]				       =   parent
							_snake_case       :					List[Any]				       =   batch_size
							_snake_case       :					Dict				       =   seq_length
							_snake_case       :					Any				       =   is_training
							_snake_case       :					List[Any]				       =   use_labels
							_snake_case       :					Dict				       =   vocab_size
							_snake_case       :					Dict				       =   hidden_size
							_snake_case       :					Any				       =   num_hidden_layers
							_snake_case       :					int				       =   num_attention_heads
							_snake_case       :					Union[str, Any]				       =   intermediate_size
							_snake_case       :					Union[str, Any]				       =   hidden_act
							_snake_case       :					Optional[Any]				       =   hidden_dropout_prob
							_snake_case       :					Any				       =   attention_probs_dropout_prob
							_snake_case       :					Dict				       =   max_position_embeddings
							_snake_case       :					Optional[Any]				       =   eos_token_id
							_snake_case       :					Optional[Any]				       =   pad_token_id
							_snake_case       :					int				       =   bos_token_id
							_snake_case       :					Optional[int]				       =   embed_dim
							_snake_case       :					str				       =   word_embed_proj_dim
							_snake_case       :					Dict				       =   False







		def        lowercase       (    self				)      ->      Tuple:


							'''simple docstring'''

							_snake_case       :					Optional[int]				       =   ids_tensor([self.batch_size, self.seq_length - 1] ,						self.vocab_size				)
							_snake_case       :					Optional[int]				       =   tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size				) ,						1				)
							_snake_case       :					Optional[Any]				       =   tf.concat([input_ids, eos_tensor] ,						axis=1				)

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







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


							'''simple docstring'''

							_snake_case       :					Union[str, Any]				       =   TFOPTModel(config=__snake_case				)
							_snake_case       :					List[str]				       =   inputs_dict["input_ids"]

							_snake_case       :					Optional[Any]				       =   input_ids[:1, :]
							_snake_case       :					Union[str, Any]				       =   inputs_dict["attention_mask"][:1, :]
							_snake_case       :					List[Any]				       =   1

							# first forward pass
							_snake_case       :					Dict				       =   model(__snake_case ,						attention_mask=__snake_case ,						use_cache=__snake_case				)

							_snake_case       , _snake_case       :					List[Any]				       =   outputs.to_tuple()

							# create hypothetical next token and extent to next_input_ids
							_snake_case       :					List[str]				       =   ids_tensor((self.batch_size, 3) ,						config.vocab_size				)
							_snake_case       :					int				       =   tf.cast(ids_tensor((self.batch_size, 3) ,						2				) ,						tf.inta				)

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

							_snake_case       :					List[str]				       =   model(__snake_case ,						attention_mask=__snake_case				)[0]
							_snake_case       :					Tuple				       =   model(__snake_case ,						attention_mask=__snake_case ,						past_key_values=__snake_case				)[0]

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

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

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






@require_tf
class        _a(    __A	,   __A	,   unittest.TestCase    ):
		lowerCamelCase__     :List[str]   				=		(TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
		lowerCamelCase__     :Any   				=		(TFOPTForCausalLM,) if is_tf_available() else ()
		lowerCamelCase__     :Union[str, Any]   				=		(
		    {'feature-extraction': TFOPTModel, 'text-generation': TFOPTForCausalLM} if is_tf_available() else {}
		)
		lowerCamelCase__     :Tuple   				=		False
		lowerCamelCase__     :List[str]   				=		False
		lowerCamelCase__     :Any   				=		False
		lowerCamelCase__     :Union[str, Any]   				=		10







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


							'''simple docstring'''

							_snake_case       :					List[Any]				       =   TFOPTModelTester(self				)
							_snake_case       :					List[Any]				       =   ConfigTester(self ,						config_class=__snake_case				)







		def        lowercase       (    self				)      ->      str:


							'''simple docstring'''

							self.config_tester.run_common_tests()







		def        lowercase       (    self				)      ->      str:


							'''simple docstring'''

							_snake_case       :					Tuple				       =   self.model_tester.prepare_config_and_inputs_for_common()
							self.model_tester.check_decoder_model_past_large_inputs(*__snake_case				)







		def        lowercase       (    self				)      ->      Any:


							'''simple docstring'''

							_snake_case       , _snake_case       :					Optional[Any]				       =   self.model_tester.prepare_config_and_inputs_for_common()

							def _get_word_embedding_weight(__snake_case ,						__snake_case				):
												if hasattr(__snake_case ,						"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(__snake_case ,						"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
																	_snake_case       :					Dict				       =   model_class(config=__snake_case				)
																	_snake_case       :					List[Any]				       =   _get_word_embedding_weight(__snake_case ,						model.get_input_embeddings()				)
																	_snake_case       :					Union[str, Any]				       =   _get_word_embedding_weight(__snake_case ,						model.get_output_embeddings()				)

																	# reshape the embeddings
																	model.resize_token_embeddings(__snake_case				)
																	_snake_case       :					Any				       =   _get_word_embedding_weight(__snake_case ,						model.get_input_embeddings()				)
																	_snake_case       :					str				       =   _get_word_embedding_weight(__snake_case ,						model.get_output_embeddings()				)

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

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

																	# check that weights remain the same after resizing
																	_snake_case       :					Tuple				       =   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:
																											_snake_case       :					str				       =   False
																	self.assertTrue(__snake_case				)

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

																						_snake_case       :					Optional[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:
																																_snake_case       :					Optional[Any]				       =   False
																						self.assertTrue(__snake_case				)







def    A				(							UpperCAmelCase				):
					return tf.constant(UpperCAmelCase  ,  dtype=tf.intaa				)






@require_tf
class        _a(    unittest.TestCase    ):
		lowerCamelCase__     :Any   				=		99







		def        lowercase       (    self				)      ->      int:


							'''simple docstring'''

							_snake_case       :					Tuple				       =   tf.ones((4, 1) ,						dtype=tf.intaa				) * 2
							_snake_case       :					Optional[Any]				       =   tf.concat([ids_tensor((4, 6) ,						self.vocab_size - 3				) + 3, eos_column_vector] ,						axis=1				)
							_snake_case       :					str				       =   input_ids.shape[0]
							_snake_case       :					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        _a(    unittest.TestCase    ):







		@slow
		def        lowercase       (    self				)      ->      Tuple:


							'''simple docstring'''

							_snake_case       :					Union[str, Any]				       =   TFOPTModel.from_pretrained("facebook/opt-350m"				)
							_snake_case       :					List[str]				       =   _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]]				)
							_snake_case       :					List[Any]				       =   tf.not_equal(__snake_case ,						model.config.pad_token_id				)
							with tf.GradientTape():
												_snake_case       :					Optional[Any]				       =   model(input_ids=__snake_case ,						attention_mask=__snake_case				).last_hidden_state
							_snake_case       :					List[Any]				       =   (1, 1_1, 5_1_2)
							self.assertEqual(output.shape ,						__snake_case				)
							_snake_case       :					Dict				       =   tf.constant(
							    [[-0.28_73, -1.92_18, -0.30_33], [-1.27_10, -0.13_38, -0.19_02], [0.40_95, 0.12_14, -1.31_21]]				)
							self.assertTrue(np.allclose(output[:, :3, :3] ,						__snake_case ,						atol=4E-3				)				)

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






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







		def        lowercase       (    self				)      ->      int:


							'''simple docstring'''

							super().setUp()
							_snake_case       :					Tuple				       =   "facebook/opt-350m"







		def        lowercase       (    self				)      ->      str:


							'''simple docstring'''

							_snake_case       :					List[Any]				       =   TFOPTForCausalLM.from_pretrained(self.path_model				)
							_snake_case       :					Optional[Any]				       =   GPTaTokenizer.from_pretrained(self.path_model				)

							_snake_case       :					List[Any]				       =   [
							    "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
							_snake_case       :					List[Any]				       =   tokenizer(__snake_case ,						return_tensors="tf" ,						padding=__snake_case ,						add_special_tokens=__snake_case				)
							_snake_case       :					Union[str, Any]				       =   tf.math.reduce_mean(model(inputs.input_ids ,						attention_mask=inputs.attention_mask				)[0] ,						axis=-1				)
							_snake_case       :					str				       =   tf.constant(
							    [
							        [1.38_51, -13.89_23, -10.52_29, -10.75_33, -0.23_09, -10.23_84, -0.53_65, -9.09_47, -5.16_70],
							        [-4.70_73, -10.62_76, -3.94_15, -21.52_42, -0.28_22, -0.28_22, -0.28_22, -0.28_22, -0.28_22],
							        [0.62_47, -3.42_29, -8.91_79, -1.42_97, -14.16_50, 1.41_46, -9.02_18, -0.27_03, -0.27_03],
							        [6.47_83, -1.99_13, -10.79_26, -2.33_36, 1.50_92, -0.99_74, -6.82_13, 1.34_77, 1.34_77],
							    ]				)
							self.assertTrue(np.allclose(__snake_case ,						__snake_case ,						atol=1E-4				)				)

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






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







		@property
		def        lowercase       (    self				)      ->      Optional[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        lowercase       (    self				)      ->      int:


							'''simple docstring'''

							_snake_case       :					List[str]				       =   "facebook/opt-125m"

							_snake_case       :					List[Any]				       =   [
							    "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",
							]

							_snake_case       :					int				       =   []
							_snake_case       :					Union[str, Any]				       =   GPTaTokenizer.from_pretrained(__snake_case				)
							_snake_case       :					Tuple				       =   TFOPTForCausalLM.from_pretrained(__snake_case				)

							for prompt in self.prompts:
												_snake_case       :					Optional[Any]				       =   tokenizer(__snake_case ,						return_tensors="tf"				).input_ids

												_snake_case       :					List[str]				       =   model.generate(__snake_case ,						max_length=1_0				)

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

							self.assertListEqual(__snake_case ,						__snake_case				)







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


							'''simple docstring'''

							_snake_case       :					str				       =   "facebook/opt-350m"

							_snake_case       :					Optional[Any]				       =   GPTaTokenizer.from_pretrained(__snake_case				)
							_snake_case       :					Optional[Any]				       =   TFOPTForCausalLM.from_pretrained(__snake_case				)

							_snake_case       :					str				       =   "left"

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

							_snake_case       :					Any				       =   tokenizer(__snake_case ,						return_tensors="tf" ,						padding=__snake_case				)
							_snake_case       :					List[str]				       =   inputs["input_ids"]

							_snake_case       :					Dict				       =   model.generate(input_ids=__snake_case ,						attention_mask=inputs["attention_mask"]				)

							_snake_case       :					str				       =   tokenizer(sentences[0] ,						return_tensors="tf"				).input_ids
							_snake_case       :					List[Any]				       =   model.generate(input_ids=__snake_case				)

							_snake_case       :					int				       =   inputs_non_padded.shape[-1] - tf.math.reduce_sum(
							    tf.cast(inputs["attention_mask"][-1] ,						tf.intaa				)				)
							_snake_case       :					Dict				       =   tokenizer(sentences[1] ,						return_tensors="tf"				).input_ids
							_snake_case       :					List[str]				       =   model.generate(input_ids=__snake_case ,						max_length=model.config.max_length - num_paddings				)

							_snake_case       :					List[Any]				       =   tokenizer.batch_decode(__snake_case ,						skip_special_tokens=__snake_case				)
							_snake_case       :					List[Any]				       =   tokenizer.decode(output_non_padded[0] ,						skip_special_tokens=__snake_case				)
							_snake_case       :					Union[str, Any]				       =   tokenizer.decode(output_padded[0] ,						skip_special_tokens=__snake_case				)

							_snake_case       :					str				       =   [
							    "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(__snake_case ,						__snake_case				)
							self.assertListEqual(__snake_case ,						[non_padded_sentence, padded_sentence]				)







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


							'''simple docstring'''

							_snake_case       :					List[str]				       =   "facebook/opt-350m"

							_snake_case       :					Tuple				       =   [
							    "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",
							]

							_snake_case       :					str				       =   []
							_snake_case       :					Optional[Any]				       =   GPTaTokenizer.from_pretrained(__snake_case				)
							_snake_case       :					Union[str, Any]				       =   TFOPTForCausalLM.from_pretrained(__snake_case				)

							for prompt in self.prompts:
												_snake_case       :					str				       =   tokenizer(__snake_case ,						return_tensors="tf"				).input_ids

												_snake_case       :					Optional[Any]				       =   model.generate(__snake_case ,						max_length=1_0				)

												_snake_case       :					str				       =   tokenizer.batch_decode(__snake_case ,						skip_special_tokens=__snake_case				)
												predicted_outputs += generated_string

							self.assertListEqual(__snake_case ,						__snake_case				)






import warnings

from ...utils import logging
from .image_processing_videomae import VideoMAEImageProcessor


__lowerCAmelCase :str     =    logging.get_logger(__name__)






class        _a(    __A    ):







		def __init__(    self ,						*__snake_case ,						**__snake_case				)      ->      None:


							'''simple docstring'''

							warnings.warn(
							    "The class VideoMAEFeatureExtractor is deprecated and will be removed in version 5 of Transformers."
							    " Please use VideoMAEImageProcessor instead." ,						__snake_case ,						)
							super().__init__(*__snake_case ,						**__snake_case				)