
'''simple docstring'''

from __future__ import annotations

import unittest

from transformers import AlbertConfig, is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
		import tensorflow as tf

		from transformers import TF_MODEL_FOR_PRETRAINING_MAPPING
		from transformers.models.albert.modeling_tf_albert import (
		    TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFAlbertForMaskedLM,
		    TFAlbertForMultipleChoice,
		    TFAlbertForPreTraining,
		    TFAlbertForQuestionAnswering,
		    TFAlbertForSequenceClassification,
		    TFAlbertForTokenClassification,
		    TFAlbertModel,
		)






class 				A       :





	def __init__(   self     :    Dict ,							__magic_name__     :    str ,							__magic_name__     :    int=13 ,							__magic_name__     :    Dict=7 ,							__magic_name__     :    Tuple=True ,							__magic_name__     :    List[Any]=True ,							__magic_name__     :    Optional[Any]=True ,							__magic_name__     :    Dict=True ,							__magic_name__     :    Optional[int]=99 ,							__magic_name__     :    Optional[int]=16 ,							__magic_name__     :    Union[str, Any]=32 ,							__magic_name__     :    Tuple=2 ,							__magic_name__     :    Optional[Any]=4 ,							__magic_name__     :    Union[str, Any]=37 ,							__magic_name__     :    Optional[Any]="gelu" ,							__magic_name__     :    List[Any]=0.1 ,							__magic_name__     :    Optional[int]=0.1 ,							__magic_name__     :    Tuple=512 ,							__magic_name__     :    Optional[Any]=16 ,							__magic_name__     :    Union[str, Any]=2 ,							__magic_name__     :    Tuple=0.02 ,							__magic_name__     :    Dict=3 ,							__magic_name__     :    Optional[Any]=4 ,							__magic_name__     :    List[str]=None ,							):


						"""simple docstring"""

						lowerCAmelCase__									=							parent
						lowerCAmelCase__									=							13
						lowerCAmelCase__									=							7
						lowerCAmelCase__									=							True
						lowerCAmelCase__									=							True
						lowerCAmelCase__									=							True
						lowerCAmelCase__									=							True
						lowerCAmelCase__									=							99
						lowerCAmelCase__									=							16
						lowerCAmelCase__									=							32
						lowerCAmelCase__									=							2
						lowerCAmelCase__									=							4
						lowerCAmelCase__									=							37
						lowerCAmelCase__									=							"gelu"
						lowerCAmelCase__									=							0.1
						lowerCAmelCase__									=							0.1
						lowerCAmelCase__									=							512
						lowerCAmelCase__									=							16
						lowerCAmelCase__									=							2
						lowerCAmelCase__									=							0.02
						lowerCAmelCase__									=							3
						lowerCAmelCase__									=							4
						lowerCAmelCase__									=							None





	def 							__SCREAMING_SNAKE_CASE					(   self     :    List[str]							):


						"""simple docstring"""

						lowerCAmelCase__									=							ids_tensor([self.batch_size, self.seq_length] ,							self.vocab_size							)

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

						lowerCAmelCase__									=							None
						if self.use_token_type_ids:
											lowerCAmelCase__									=							ids_tensor([self.batch_size, self.seq_length] ,							self.type_vocab_size							)

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

						lowerCAmelCase__									=							AlbertConfig(
						    vocab_size=self.vocab_size ,							hidden_size=self.hidden_size ,							embedding_size=self.embedding_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 ,							type_vocab_size=self.type_vocab_size ,							initializer_range=self.initializer_range ,							)

						return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels





	def 							__SCREAMING_SNAKE_CASE					(   self     :    Dict ,							__magic_name__     :    List[Any] ,							__magic_name__     :    Optional[int] ,							__magic_name__     :    Union[str, Any] ,							__magic_name__     :    Dict ,							__magic_name__     :    Optional[Any] ,							__magic_name__     :    Optional[Any] ,							__magic_name__     :    Optional[int]							):


						"""simple docstring"""

						lowerCAmelCase__									=							TFAlbertModel(config=__lowerCAmelCase							)
						# inputs = {'input_ids': input_ids,
						#           'attention_mask': input_mask,
						#           'token_type_ids': token_type_ids}
						# sequence_output, pooled_output = model(**inputs)
						lowerCAmelCase__									=							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
						lowerCAmelCase__									=							model(__lowerCAmelCase							)

						lowerCAmelCase__									=							[input_ids, input_mask]
						lowerCAmelCase__									=							model(__lowerCAmelCase							)

						lowerCAmelCase__									=							model(__lowerCAmelCase							)

						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 							__SCREAMING_SNAKE_CASE					(   self     :    int ,							__magic_name__     :    Union[str, Any] ,							__magic_name__     :    Dict ,							__magic_name__     :    str ,							__magic_name__     :    Optional[Any] ,							__magic_name__     :    Dict ,							__magic_name__     :    List[Any] ,							__magic_name__     :    Tuple							):


						"""simple docstring"""

						lowerCAmelCase__									=							self.num_labels
						lowerCAmelCase__									=							TFAlbertForPreTraining(config=__lowerCAmelCase							)
						lowerCAmelCase__									=							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
						lowerCAmelCase__									=							model(__lowerCAmelCase							)
						self.parent.assertEqual(result.prediction_logits.shape ,							(self.batch_size, self.seq_length, self.vocab_size)							)
						self.parent.assertEqual(result.sop_logits.shape ,							(self.batch_size, self.num_labels)							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    Dict ,							__magic_name__     :    Optional[int] ,							__magic_name__     :    Optional[Any] ,							__magic_name__     :    str ,							__magic_name__     :    List[Any] ,							__magic_name__     :    Any ,							__magic_name__     :    List[Any] ,							__magic_name__     :    List[Any]							):


						"""simple docstring"""

						lowerCAmelCase__									=							TFAlbertForMaskedLM(config=__lowerCAmelCase							)
						lowerCAmelCase__									=							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
						lowerCAmelCase__									=							model(__lowerCAmelCase							)
						self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.seq_length, self.vocab_size)							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    Union[str, Any] ,							__magic_name__     :    Any ,							__magic_name__     :    Tuple ,							__magic_name__     :    Union[str, Any] ,							__magic_name__     :    Dict ,							__magic_name__     :    List[Any] ,							__magic_name__     :    Any ,							__magic_name__     :    int							):


						"""simple docstring"""

						lowerCAmelCase__									=							self.num_labels
						lowerCAmelCase__									=							TFAlbertForSequenceClassification(config=__lowerCAmelCase							)
						lowerCAmelCase__									=							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
						lowerCAmelCase__									=							model(__lowerCAmelCase							)
						self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.num_labels)							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    str ,							__magic_name__     :    str ,							__magic_name__     :    Dict ,							__magic_name__     :    List[Any] ,							__magic_name__     :    int ,							__magic_name__     :    Union[str, Any] ,							__magic_name__     :    Union[str, Any] ,							__magic_name__     :    Tuple							):


						"""simple docstring"""

						lowerCAmelCase__									=							TFAlbertForQuestionAnswering(config=__lowerCAmelCase							)
						lowerCAmelCase__									=							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
						lowerCAmelCase__									=							model(__lowerCAmelCase							)
						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 							__SCREAMING_SNAKE_CASE					(   self     :    List[Any] ,							__magic_name__     :    Optional[Any] ,							__magic_name__     :    str ,							__magic_name__     :    Optional[int] ,							__magic_name__     :    Union[str, Any] ,							__magic_name__     :    Tuple ,							__magic_name__     :    Union[str, Any] ,							__magic_name__     :    Union[str, Any]							):


						"""simple docstring"""

						lowerCAmelCase__									=							self.num_choices
						lowerCAmelCase__									=							TFAlbertForMultipleChoice(config=__lowerCAmelCase							)
						lowerCAmelCase__									=							tf.tile(tf.expand_dims(__lowerCAmelCase ,							1							) ,							(1, self.num_choices, 1)							)
						lowerCAmelCase__									=							tf.tile(tf.expand_dims(__lowerCAmelCase ,							1							) ,							(1, self.num_choices, 1)							)
						lowerCAmelCase__									=							tf.tile(tf.expand_dims(__lowerCAmelCase ,							1							) ,							(1, self.num_choices, 1)							)
						lowerCAmelCase__									=							{
						    "input_ids": multiple_choice_inputs_ids,
						    "attention_mask": multiple_choice_input_mask,
						    "token_type_ids": multiple_choice_token_type_ids,
						}
						lowerCAmelCase__									=							model(__lowerCAmelCase							)
						self.parent.assertListEqual(list(result["logits"].shape							) ,							[self.batch_size, self.num_choices]							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    List[Any] ,							__magic_name__     :    str ,							__magic_name__     :    Optional[int] ,							__magic_name__     :    Union[str, Any] ,							__magic_name__     :    Union[str, Any] ,							__magic_name__     :    Dict ,							__magic_name__     :    Union[str, Any] ,							__magic_name__     :    Dict							):


						"""simple docstring"""

						lowerCAmelCase__									=							self.num_labels
						lowerCAmelCase__									=							TFAlbertForTokenClassification(config=__lowerCAmelCase							)
						lowerCAmelCase__									=							{
						    "input_ids": input_ids,
						    "attention_mask": input_mask,
						    "token_type_ids": token_type_ids,
						}
						lowerCAmelCase__									=							model(__lowerCAmelCase							)
						self.parent.assertListEqual(list(result["logits"].shape							) ,							[self.batch_size, self.seq_length, self.num_labels]							)







	def 							__SCREAMING_SNAKE_CASE					(   self     :    Optional[int]							):


						"""simple docstring"""

						lowerCAmelCase__									=							self.prepare_config_and_inputs()
						(
						    (
						    lowerCAmelCase__					
						)		,(
						    lowerCAmelCase__					
						)		,(
						    lowerCAmelCase__					
						)		,(
						    lowerCAmelCase__					
						)		,(
						    lowerCAmelCase__					
						)		,(
						    lowerCAmelCase__					
						)		,(
						    lowerCAmelCase__					
						)		,					
						)									=							config_and_inputs
						lowerCAmelCase__									=							{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
						return config, inputs_dict






@require_tf
class 				A       (		lowerCAmelCase__					, lowerCAmelCase__					, unittest.TestCase      ):
	snake_case__ :str				       =       (
	    (
	        TFAlbertModel,
	        TFAlbertForPreTraining,
	        TFAlbertForMaskedLM,
	        TFAlbertForSequenceClassification,
	        TFAlbertForQuestionAnswering,
	        TFAlbertForTokenClassification,
	        TFAlbertForMultipleChoice,
	    )
	    if is_tf_available()
	    else ()
	)
	snake_case__ :Optional[Any]				       =       (
	    {
	        '''feature-extraction''': TFAlbertModel,
	        '''fill-mask''': TFAlbertForMaskedLM,
	        '''question-answering''': TFAlbertForQuestionAnswering,
	        '''text-classification''': TFAlbertForSequenceClassification,
	        '''token-classification''': TFAlbertForTokenClassification,
	        '''zero-shot''': TFAlbertForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	snake_case__ :str				       =       False
	snake_case__ :Dict				       =       False





	def 							__SCREAMING_SNAKE_CASE					(   self     :    Optional[int] ,							__magic_name__     :    Tuple ,							__magic_name__     :    Optional[int] ,							__magic_name__     :    List[str]=False							):


						"""simple docstring"""

						lowerCAmelCase__									=							super()._prepare_for_class(__lowerCAmelCase ,							__lowerCAmelCase ,							return_labels=__lowerCAmelCase							)

						if return_labels:
											if model_class in get_values(__lowerCAmelCase							):
																lowerCAmelCase__									=							tf.zeros(self.model_tester.batch_size ,							dtype=tf.intaa							)

						return inputs_dict





	def 							__SCREAMING_SNAKE_CASE					(   self     :    List[Any]							):


						"""simple docstring"""

						lowerCAmelCase__									=							TFAlbertModelTester(self							)
						lowerCAmelCase__									=							ConfigTester(self ,							config_class=__lowerCAmelCase ,							hidden_size=37							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    Tuple							):


						"""simple docstring"""

						self.config_tester.run_common_tests()





	def 							__SCREAMING_SNAKE_CASE					(   self     :    Any							):


						"""simple docstring"""

						lowerCAmelCase__									=							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_albert_model(*__lowerCAmelCase							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    Union[str, Any]							):


						"""simple docstring"""

						lowerCAmelCase__									=							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_albert_for_pretraining(*__lowerCAmelCase							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    str							):


						"""simple docstring"""

						lowerCAmelCase__									=							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_albert_for_masked_lm(*__lowerCAmelCase							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    Tuple							):


						"""simple docstring"""

						lowerCAmelCase__									=							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_albert_for_multiple_choice(*__lowerCAmelCase							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    List[Any]							):


						"""simple docstring"""

						lowerCAmelCase__									=							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_albert_for_sequence_classification(*__lowerCAmelCase							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    Dict							):


						"""simple docstring"""

						lowerCAmelCase__									=							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_albert_for_question_answering(*__lowerCAmelCase							)







	@slow
	def 							__SCREAMING_SNAKE_CASE					(   self     :    Dict							):


						"""simple docstring"""

						for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowerCAmelCase__									=							TFAlbertModel.from_pretrained(__lowerCAmelCase							)
											self.assertIsNotNone(__lowerCAmelCase							)






@require_tf
class 				A       (		unittest.TestCase      ):





	@slow
	def 							__SCREAMING_SNAKE_CASE					(   self     :    str							):


						"""simple docstring"""

						lowerCAmelCase__									=							TFAlbertForPreTraining.from_pretrained("albert-base-v2"							)
						lowerCAmelCase__									=							tf.constant([[0, 1, 2, 3, 4, 5]]							)
						lowerCAmelCase__									=							model(__lowerCAmelCase							)[0]

						lowerCAmelCase__									=							[1, 6, 30000]
						self.assertEqual(output.shape ,							__lowerCAmelCase							)

						lowerCAmelCase__									=							tf.constant(
						    [
						        [
						            [4.59_5668, 0.7446_2754, -1.81_8147],
						            [4.595_4347, 0.745_4184, -1.818_8258],
						            [4.595_4905, 0.744_8235, -1.818_2316],
						        ]
						    ]							)
						tf.debugging.assert_near(output[:, :3, :3] ,							__lowerCAmelCase ,							atol=1E-4							)







'''simple docstring'''

import unittest

from transformers import load_tool
from transformers.tools.agent_types import AGENT_TYPE_MAPPING

from .test_tools_common import ToolTesterMixin, output_types






class 				A       (		unittest.TestCase					, SCREAMING_SNAKE_CASE__      ):





	def 							__SCREAMING_SNAKE_CASE					(   self     :    List[Any]							):


				"""simple docstring"""

				lowerCAmelCase__									=							load_tool("translation"							)
				self.tool.setup()
				lowerCAmelCase__									=							load_tool("translation" ,							remote=__magic_name__							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    Tuple							):


				"""simple docstring"""

				lowerCAmelCase__									=							self.tool("Hey, what's up?" ,							src_lang="English" ,							tgt_lang="French"							)
				self.assertEqual(__magic_name__ ,							"- Hé, comment ça va?"							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    Optional[int]							):


				"""simple docstring"""

				lowerCAmelCase__									=							self.remote_tool("Hey, what's up?" ,							src_lang="English" ,							tgt_lang="French"							)
				self.assertEqual(__magic_name__ ,							"- Hé, comment ça va?"							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    List[Any]							):


				"""simple docstring"""

				lowerCAmelCase__									=							self.tool(text="Hey, what's up?" ,							src_lang="English" ,							tgt_lang="French"							)
				self.assertEqual(__magic_name__ ,							"- Hé, comment ça va?"							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    int							):


				"""simple docstring"""

				lowerCAmelCase__									=							self.remote_tool(text="Hey, what's up?" ,							src_lang="English" ,							tgt_lang="French"							)
				self.assertEqual(__magic_name__ ,							"- Hé, comment ça va?"							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    int							):


				"""simple docstring"""

				lowerCAmelCase__									=							["Hey, what's up?", "English", "Spanish"]
				lowerCAmelCase__									=							self.tool(*__magic_name__							)

				# There is a single output
				if len(self.tool.outputs							) == 1:
							lowerCAmelCase__									=							[outputs]

				self.assertListEqual(output_types(__magic_name__							) ,							self.tool.outputs							)





	def 							__SCREAMING_SNAKE_CASE					(   self     :    str							):


				"""simple docstring"""

				lowerCAmelCase__									=							["Hey, what's up?", "English", "Spanish"]
				lowerCAmelCase__									=							self.tool(*__magic_name__							)

				if not isinstance(__magic_name__ ,							__magic_name__							):
							lowerCAmelCase__									=							[outputs]

				self.assertEqual(len(__magic_name__							) ,							len(self.tool.outputs							)							)

				for output, output_type in zip(__magic_name__ ,							self.tool.outputs							):
							lowerCAmelCase__									=							AGENT_TYPE_MAPPING[output_type]
							self.assertTrue(isinstance(__magic_name__ ,							__magic_name__							)							)







	def 							__SCREAMING_SNAKE_CASE					(   self     :    int							):


				"""simple docstring"""

				lowerCAmelCase__									=							["Hey, what's up?", "English", "Spanish"]

				lowerCAmelCase__									=							[]

				for _input, input_type in zip(__magic_name__ ,							self.tool.inputs							):
							if isinstance(__magic_name__ ,							__magic_name__							):
										_inputs.append([AGENT_TYPE_MAPPING[_input_type](_input							) for _input_type in input_type]							)
							else:
										_inputs.append(AGENT_TYPE_MAPPING[input_type](_input							)							)

        # Should not raise an error
				lowerCAmelCase__									=							self.tool(*__magic_name__							)

				if not isinstance(__magic_name__ ,							__magic_name__							):
							lowerCAmelCase__									=							[outputs]

				self.assertEqual(len(__magic_name__							) ,							len(self.tool.outputs							)							)





