




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 		lowerCAmelCase_   (	lowerCamelCase	,     lowerCamelCase	,     lowerCamelCase=None	,     lowerCamelCase=None   ):
				if attention_mask is None:
								__magic_name__	:	Dict								=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 		__A      :
						UpperCamelCase     						=					OPTConfig
						UpperCamelCase     						=					{}
						UpperCamelCase     						=					"""gelu"""

						def __init__(		self   :Dict    , __snake_case   :List[Any]    , __snake_case   :Dict=13    , __snake_case   :Union[str, Any]=7    , __snake_case   :Any=True    , __snake_case   :Optional[int]=False    , __snake_case   :Dict=99    , __snake_case   :Tuple=16    , __snake_case   :Tuple=2    , __snake_case   :Tuple=4    , __snake_case   :Union[str, Any]=4    , __snake_case   :Dict="gelu"    , __snake_case   :Any=0.1    , __snake_case   :Tuple=0.1    , __snake_case   :int=20    , __snake_case   :Optional[Any]=2    , __snake_case   :Dict=1    , __snake_case   :str=0    , __snake_case   :Union[str, Any]=16    , __snake_case   :List[str]=16    , ):
										'''simple docstring'''

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

						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

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

										__magic_name__	:	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    , )
										__magic_name__	:	List[Any]								=prepare_opt_inputs_dict(__snake_case    , __snake_case )
										return config, inputs_dict


						def     A__						(		self   :Optional[Any]    , __snake_case   :Any    , __snake_case   :Any ):
										'''simple docstring'''

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

										__magic_name__	:	List[str]								=input_ids[:1, :]
										__magic_name__	:	List[Any]								=inputs_dict["""attention_mask"""][:1, :]
										__magic_name__	:	str								=1

										# first forward pass
										__magic_name__	:	List[Any]								=model(__snake_case    , attention_mask=__snake_case    , use_cache=__snake_case )

										__magic_name__				, __magic_name__	:	str								=outputs.to_tuple()

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

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

										__magic_name__	:	Any								=model(__snake_case    , attention_mask=__snake_case )[0]
										__magic_name__	:	Dict								=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
										__magic_name__	:	Any								=int(ids_tensor((1,)    , output_from_past.shape[-1] ) )
										__magic_name__	:	str								=output_from_no_past[:, -3:, random_slice_idx]
										__magic_name__	:	Tuple								=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      (			UpperCamelCase__					,						UpperCamelCase__					,						unittest.TestCase			):
						UpperCamelCase     						=					(TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
						UpperCamelCase     						=					(TFOPTForCausalLM,) if is_tf_available() else ()
						UpperCamelCase     						=					(
						    {"""feature-extraction""": TFOPTModel, """text-generation""": TFOPTForCausalLM} if is_tf_available() else {}
						)
						UpperCamelCase     						=					False
						UpperCamelCase     						=					False
						UpperCamelCase     						=					False
						UpperCamelCase     						=					10

						def     A__						(		self   :Optional[int] ):
										'''simple docstring'''

										__magic_name__	:	Optional[Any]								=TFOPTModelTester(self )
										__magic_name__	:	Tuple								=ConfigTester(self    , config_class=__snake_case )

						def     A__						(		self   :str ):
										'''simple docstring'''

										self.config_tester.run_common_tests()

						def     A__						(		self   :int ):
										'''simple docstring'''

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


						def     A__						(		self   :Tuple ):
										'''simple docstring'''

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

										def _get_word_embedding_weight(__snake_case   :Optional[int]    , __snake_case   :str ):
														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 - 10, config.vocab_size + 10]:
																		# build the embeddings
																		__magic_name__	:	List[Any]								=model_class(config=__snake_case )
																		__magic_name__	:	Union[str, Any]								=_get_word_embedding_weight(__snake_case    , model.get_input_embeddings() )
																		__magic_name__	:	Union[str, Any]								=_get_word_embedding_weight(__snake_case    , model.get_output_embeddings() )

																		# reshape the embeddings
																		model.resize_token_embeddings(__snake_case )
																		__magic_name__	:	Tuple								=_get_word_embedding_weight(__snake_case    , model.get_input_embeddings() )
																		__magic_name__	:	List[Any]								=_get_word_embedding_weight(__snake_case    , model.get_output_embeddings() )

																		# check that the resized embeddings size matches the desired size.
																		__magic_name__	:	List[str]								=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
																		__magic_name__	:	Dict								=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:
																										__magic_name__	:	Optional[int]								=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 )

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




def 		lowerCAmelCase_   (	lowerCamelCase   ):
				return tf.constant(lowerCamelCase	,     dtype=tf.intaa   )




@require_tf
class 		__A      (			unittest.TestCase			):
						UpperCamelCase     						=					99

						def     A__						(		self   :Any ):
										'''simple docstring'''

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

						@slow
						def     A__						(		self   :Dict ):
										'''simple docstring'''

										__magic_name__	:	Optional[int]								=TFOPTModel.from_pretrained("""facebook/opt-350m""" )
										__magic_name__	:	str								=_long_tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]] )
										__magic_name__	:	str								=tf.not_equal(__snake_case    , model.config.pad_token_id )
										with tf.GradientTape():
														__magic_name__	:	Tuple								=model(input_ids=__snake_case    , attention_mask=__snake_case ).last_hidden_state
										__magic_name__	:	Optional[Any]								=(1, 11, 5_12)
										self.assertEqual(output.shape    , __snake_case )
										__magic_name__	:	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]    , __snake_case    , atol=4E-3 ) )

										__magic_name__	:	List[Any]								=tf.function(__snake_case    , jit_compile=__snake_case )
										__magic_name__	:	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     A__						(		self   :int ):
										'''simple docstring'''

										super().setUp()
										__magic_name__	:	int								="""facebook/opt-350m"""


						def     A__						(		self   :str ):
										'''simple docstring'''

										__magic_name__	:	Optional[Any]								=TFOPTForCausalLM.from_pretrained(self.path_model )
										__magic_name__	:	str								=GPTaTokenizer.from_pretrained(self.path_model )

										__magic_name__	:	Dict								=[
										    """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
										__magic_name__	:	Optional[int]								=tokenizer(__snake_case    , return_tensors="""tf"""    , padding=__snake_case    , add_special_tokens=__snake_case )
										__magic_name__	:	Dict								=tf.math.reduce_mean(model(inputs.input_ids    , attention_mask=inputs.attention_mask )[0]    , axis=-1 )
										__magic_name__	:	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(__snake_case    , __snake_case    , atol=1E-4 ) )

										__magic_name__	:	Tuple								=tf.function(__snake_case    , jit_compile=__snake_case )
										__magic_name__	:	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     A__						(		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     A__						(		self   :List[str] ):
										'''simple docstring'''

										__magic_name__	:	Optional[Any]								="""facebook/opt-125m"""

										__magic_name__	:	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""",
										]

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

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

														__magic_name__	:	List[str]								=model.generate(__snake_case    , max_length=10 )

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

										self.assertListEqual(__snake_case    , __snake_case )

						def     A__						(		self   :List[str] ):
										'''simple docstring'''

										__magic_name__	:	List[Any]								="""facebook/opt-350m"""

										__magic_name__	:	List[Any]								=GPTaTokenizer.from_pretrained(__snake_case )
										__magic_name__	:	int								=TFOPTForCausalLM.from_pretrained(__snake_case )

										__magic_name__	:	Union[str, Any]								="""left"""

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

										__magic_name__	:	Any								=tokenizer(__snake_case    , return_tensors="""tf"""    , padding=__snake_case )
										__magic_name__	:	Optional[Any]								=inputs["""input_ids"""]

										__magic_name__	:	str								=model.generate(input_ids=__snake_case    , attention_mask=inputs["""attention_mask"""] )

										__magic_name__	:	str								=tokenizer(sentences[0]    , return_tensors="""tf""" ).input_ids
										__magic_name__	:	Tuple								=model.generate(input_ids=__snake_case )

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

										__magic_name__	:	Dict								=tokenizer.batch_decode(__snake_case    , skip_special_tokens=__snake_case )
										__magic_name__	:	Optional[Any]								=tokenizer.decode(output_non_padded[0]    , skip_special_tokens=__snake_case )
										__magic_name__	:	int								=tokenizer.decode(output_padded[0]    , skip_special_tokens=__snake_case )

										__magic_name__	:	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     A__						(		self   :Tuple ):
										'''simple docstring'''

										__magic_name__	:	List[Any]								="""facebook/opt-350m"""

										__magic_name__	:	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""",
										]

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

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

														__magic_name__	:	Optional[Any]								=model.generate(__snake_case    , max_length=10 )

														__magic_name__	:	int								=tokenizer.batch_decode(__snake_case    , skip_special_tokens=__snake_case )
														predicted_outputs += generated_string

										self.assertListEqual(__snake_case    , __snake_case )










import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin




class 		__A      (			UpperCamelCase__			):
						UpperCamelCase     						=					"""Speech2TextFeatureExtractor"""
						UpperCamelCase     						=					"""Speech2TextTokenizer"""

						def __init__(		self   :Any    , __snake_case   :int    , __snake_case   :Any ):
										'''simple docstring'''

										super().__init__(__snake_case    , __snake_case )
										__magic_name__	:	Optional[int]								=self.feature_extractor
										__magic_name__	:	Any								=False

						def __call__(		self   :Tuple    , *__snake_case   :List[str]    , **__snake_case   :List[str] ):
										'''simple docstring'''

										if self._in_target_context_manager:
														return self.current_processor(*__snake_case    , **__snake_case )

										if "raw_speech" in kwargs:
														warnings.warn("""Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.""" )
														__magic_name__	:	Tuple								=kwargs.pop("""raw_speech""" )
										else:
														__magic_name__	:	List[Any]								=kwargs.pop("""audio"""    , __snake_case )
										__magic_name__	:	Union[str, Any]								=kwargs.pop("""sampling_rate"""    , __snake_case )
										__magic_name__	:	Dict								=kwargs.pop("""text"""    , __snake_case )
										if len(__snake_case ) > 0:
														__magic_name__	:	Union[str, Any]								=args[0]
														__magic_name__	:	int								=args[1:]

										if audio is None and text is None:
														raise ValueError("""You need to specify either an `audio` or `text` input to process.""" )

										if audio is not None:
														__magic_name__	:	List[str]								=self.feature_extractor(__snake_case    , *__snake_case    , sampling_rate=__snake_case    , **__snake_case )
										if text is not None:
														__magic_name__	:	Any								=self.tokenizer(__snake_case    , **__snake_case )

										if text is None:
														return inputs
										elif audio is None:
														return encodings
										else:
														__magic_name__	:	List[str]								=encodings["""input_ids"""]
														return inputs

						def     A__						(		self   :List[Any]    , *__snake_case   :str    , **__snake_case   :List[str] ):
										'''simple docstring'''

										return self.tokenizer.batch_decode(*__snake_case    , **__snake_case )

						def     A__						(		self   :int    , *__snake_case   :int    , **__snake_case   :Optional[int] ):
										'''simple docstring'''

										return self.tokenizer.decode(*__snake_case    , **__snake_case )


						@contextmanager
						def     A__						(		self   :int ):
										'''simple docstring'''

										warnings.warn(
										    """`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your """
										    """labels by using the argument `text` of the regular `__call__` method (either in the same call as """
										    """your audio inputs, or in a separate call.""" )
										__magic_name__	:	int								=True
										__magic_name__	:	List[Any]								=self.tokenizer
										yield
										__magic_name__	:	List[Any]								=self.feature_extractor
										__magic_name__	:	str								=False




