

"""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     _UpperCAmelCase			:
			def __init__(		self      ,		_A      ,		_A=13      ,		_A=7      ,		_A=True      ,		_A=True      ,		_A=True      ,		_A=True      ,		_A=99      ,		_A=16      ,		_A=32      ,		_A=2      ,		_A=4      ,		_A=37      ,		_A="gelu"      ,		_A=0.1      ,		_A=0.1      ,		_A=5_12      ,		_A=16      ,		_A=2      ,		_A=0.02      ,		_A=3      ,		_A=4      ,		_A=None      ,		)    ->    Tuple:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					parent
				_UpperCAmelCase    :							str									=					13
				_UpperCAmelCase    :							str									=					7
				_UpperCAmelCase    :							List[str]									=					True
				_UpperCAmelCase    :							Optional[Any]									=					True
				_UpperCAmelCase    :							Dict									=					True
				_UpperCAmelCase    :							Dict									=					True
				_UpperCAmelCase    :							Optional[Any]									=					99
				_UpperCAmelCase    :							Optional[int]									=					16
				_UpperCAmelCase    :							Dict									=					32
				_UpperCAmelCase    :							Optional[int]									=					2
				_UpperCAmelCase    :							Dict									=					4
				_UpperCAmelCase    :							List[str]									=					37
				_UpperCAmelCase    :							Optional[Any]									=					"""gelu"""
				_UpperCAmelCase    :							List[Any]									=					0.1
				_UpperCAmelCase    :							Optional[int]									=					0.1
				_UpperCAmelCase    :							Any									=					5_12
				_UpperCAmelCase    :							List[str]									=					16
				_UpperCAmelCase    :							Dict									=					2
				_UpperCAmelCase    :							Any									=					0.02
				_UpperCAmelCase    :							List[Any]									=					3
				_UpperCAmelCase    :							Optional[Any]									=					4
				_UpperCAmelCase    :							Optional[int]									=					None
			def 							__snake_case (		self     )    ->    Dict:






				'''simple docstring'''







				_UpperCAmelCase    :							List[Any]									=					ids_tensor([self.batch_size, self.seq_length]      ,		self.vocab_size     )

				_UpperCAmelCase    :							Union[str, Any]									=					None
				if self.use_input_mask:
					_UpperCAmelCase    :							Union[str, Any]									=					random_attention_mask([self.batch_size, self.seq_length]     )

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

				_UpperCAmelCase    :							Union[str, Any]									=					None
				_UpperCAmelCase    :							str									=					None
				_UpperCAmelCase    :							Any									=					None
				if self.use_labels:
					_UpperCAmelCase    :							Optional[Any]									=					ids_tensor([self.batch_size]      ,		self.type_sequence_label_size     )
					_UpperCAmelCase    :							Optional[int]									=					ids_tensor([self.batch_size, self.seq_length]      ,		self.num_labels     )
					_UpperCAmelCase    :							Union[str, Any]									=					ids_tensor([self.batch_size]      ,		self.num_choices     )

				_UpperCAmelCase    :							Union[str, Any]									=					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 							__snake_case (		self      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A     )    ->    Any:






				'''simple docstring'''







				_UpperCAmelCase    :							str									=					TFAlbertModel(config=_A     )
				# inputs = {'input_ids': input_ids,
				#           'attention_mask': input_mask,
				#           'token_type_ids': token_type_ids}
				# sequence_output, pooled_output = model(**inputs)
				_UpperCAmelCase    :							List[Any]									=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
				_UpperCAmelCase    :							Optional[Any]									=					model(_A     )

				_UpperCAmelCase    :							str									=					[input_ids, input_mask]
				_UpperCAmelCase    :							List[Any]									=					model(_A     )

				_UpperCAmelCase    :							str									=					model(_A     )

				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 							__snake_case (		self      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A     )    ->    Union[str, Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							Tuple									=					self.num_labels
				_UpperCAmelCase    :							int									=					TFAlbertForPreTraining(config=_A     )
				_UpperCAmelCase    :							int									=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
				_UpperCAmelCase    :							Any									=					model(_A     )
				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 							__snake_case (		self      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A     )    ->    List[str]:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					TFAlbertForMaskedLM(config=_A     )
				_UpperCAmelCase    :							List[str]									=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
				_UpperCAmelCase    :							List[str]									=					model(_A     )
				self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.seq_length, self.vocab_size)     )
			def 							__snake_case (		self      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A     )    ->    str:






				'''simple docstring'''







				_UpperCAmelCase    :							Tuple									=					self.num_labels
				_UpperCAmelCase    :							Any									=					TFAlbertForSequenceClassification(config=_A     )
				_UpperCAmelCase    :							Any									=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
				_UpperCAmelCase    :							Union[str, Any]									=					model(_A     )
				self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.num_labels)     )
			def 							__snake_case (		self      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A     )    ->    List[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							Any									=					TFAlbertForQuestionAnswering(config=_A     )
				_UpperCAmelCase    :							Optional[Any]									=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
				_UpperCAmelCase    :							Tuple									=					model(_A     )
				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 							__snake_case (		self      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A     )    ->    Dict:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					self.num_choices
				_UpperCAmelCase    :							Any									=					TFAlbertForMultipleChoice(config=_A     )
				_UpperCAmelCase    :							Optional[Any]									=					tf.tile(tf.expand_dims(_A      ,		1     )      ,		(1, self.num_choices, 1)     )
				_UpperCAmelCase    :							str									=					tf.tile(tf.expand_dims(_A      ,		1     )      ,		(1, self.num_choices, 1)     )
				_UpperCAmelCase    :							Optional[int]									=					tf.tile(tf.expand_dims(_A      ,		1     )      ,		(1, self.num_choices, 1)     )
				_UpperCAmelCase    :							Dict									=					{
				    """input_ids""": multiple_choice_inputs_ids,
				    """attention_mask""": multiple_choice_input_mask,
				    """token_type_ids""": multiple_choice_token_type_ids,
				}
				_UpperCAmelCase    :							Optional[Any]									=					model(_A     )
				self.parent.assertListEqual(list(result["""logits"""].shape     )      ,		[self.batch_size, self.num_choices]     )
			def 							__snake_case (		self      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A      ,		_A     )    ->    str:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					self.num_labels
				_UpperCAmelCase    :							List[str]									=					TFAlbertForTokenClassification(config=_A     )
				_UpperCAmelCase    :							Union[str, Any]									=					{
				    """input_ids""": input_ids,
				    """attention_mask""": input_mask,
				    """token_type_ids""": token_type_ids,
				}
				_UpperCAmelCase    :							str									=					model(_A     )
				self.parent.assertListEqual(list(result["""logits"""].shape     )      ,		[self.batch_size, self.seq_length, self.num_labels]     )
			def 							__snake_case (		self     )    ->    Optional[int]:






				'''simple docstring'''







				_UpperCAmelCase    :							int									=					self.prepare_config_and_inputs()
				(
				    (
				    _UpperCAmelCase
				)						,   (
				    _UpperCAmelCase
				)						,   (
				    _UpperCAmelCase
				)						,   (
				    _UpperCAmelCase
				)						,   (
				    _UpperCAmelCase
				)						,   (
				    _UpperCAmelCase
				)						,   (
				    _UpperCAmelCase
				)						,   
				)    :							str									=					config_and_inputs
				_UpperCAmelCase    :							List[str]									=					{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
				return config, inputs_dict






@require_tf
class     _UpperCAmelCase			(		__a    ,     __a    ,     unittest.TestCase):
			__a :       int									=							(
			    (
			        TFAlbertModel,
			        TFAlbertForPreTraining,
			        TFAlbertForMaskedLM,
			        TFAlbertForSequenceClassification,
			        TFAlbertForQuestionAnswering,
			        TFAlbertForTokenClassification,
			        TFAlbertForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			__a :       Dict									=							(
			    {
			        """feature-extraction""": TFAlbertModel,
			        """fill-mask""": TFAlbertForMaskedLM,
			        """question-answering""": TFAlbertForQuestionAnswering,
			        """text-classification""": TFAlbertForSequenceClassification,
			        """token-classification""": TFAlbertForTokenClassification,
			        """zero-shot""": TFAlbertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			__a :       str									=							False
			__a :       Optional[Any]									=							False
			def 							__snake_case (		self      ,		_A      ,		_A      ,		_A=False     )    ->    List[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							str									=					super()._prepare_for_class(_A      ,		_A      ,		return_labels=_A     )

				if return_labels:
					if model_class in get_values(_A     ):
						_UpperCAmelCase    :							List[Any]									=					tf.zeros(self.model_tester.batch_size      ,		dtype=tf.intaa     )

				return inputs_dict
			def 							__snake_case (		self     )    ->    Optional[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							str									=					TFAlbertModelTester(self     )
				_UpperCAmelCase    :							Optional[int]									=					ConfigTester(self      ,		config_class=_A      ,		hidden_size=37     )
			def 							__snake_case (		self     )    ->    Any:






				'''simple docstring'''







				self.config_tester.run_common_tests()
			def 							__snake_case (		self     )    ->    Tuple:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_albert_model(*_A     )
			def 							__snake_case (		self     )    ->    Dict:






				'''simple docstring'''







				_UpperCAmelCase    :							str									=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_albert_for_pretraining(*_A     )
			def 							__snake_case (		self     )    ->    List[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							str									=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_albert_for_masked_lm(*_A     )
			def 							__snake_case (		self     )    ->    Optional[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							int									=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_albert_for_multiple_choice(*_A     )
			def 							__snake_case (		self     )    ->    int:






				'''simple docstring'''







				_UpperCAmelCase    :							Optional[int]									=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_albert_for_sequence_classification(*_A     )
			def 							__snake_case (		self     )    ->    Optional[int]:






				'''simple docstring'''







				_UpperCAmelCase    :							List[Any]									=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_albert_for_question_answering(*_A     )
			@slow
			def 							__snake_case (		self     )    ->    Optional[Any]:






				'''simple docstring'''







				for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					_UpperCAmelCase    :							Dict									=					TFAlbertModel.from_pretrained(_A     )
					self.assertIsNotNone(_A     )






@require_tf
class     _UpperCAmelCase			(		unittest.TestCase):
			@slow
			def 							__snake_case (		self     )    ->    List[str]:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					TFAlbertForPreTraining.from_pretrained("""albert-base-v2"""     )
				_UpperCAmelCase    :							Tuple									=					tf.constant([[0, 1, 2, 3, 4, 5]]     )
				_UpperCAmelCase    :							int									=					model(_A     )[0]

				_UpperCAmelCase    :							str									=					[1, 6, 3_00_00]
				self.assertEqual(output.shape      ,		_A     )

				_UpperCAmelCase    :							Union[str, Any]									=					tf.constant(
				    [
				        [
				            [4.595668, 0.74462754, -1.818147],
				            [4.5954347, 0.7454184, -1.8188258],
				            [4.5954905, 0.7448235, -1.8182316],
				        ]
				    ]     )
				tf.debugging.assert_near(output[:, :3, :3]      ,		_A      ,		atol=1e-4     )








"""simple docstring"""






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCamelCase__				:  Optional[int]													=  {'''configuration_t5''': ['''T5_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''T5Config''', '''T5OnnxConfig''']}

try:
						if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__				:  Dict													=  ['''T5Tokenizer''']

try:
						if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__				:  List[str]													=  ['''T5TokenizerFast''']

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__				:  Optional[Any]													=  [
						    '''T5_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''T5EncoderModel''',
						    '''T5ForConditionalGeneration''',
						    '''T5Model''',
						    '''T5PreTrainedModel''',
						    '''load_tf_weights_in_t5''',
						    '''T5ForQuestionAnswering''',
						]

try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__				:  int													=  [
						    '''TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''TFT5EncoderModel''',
						    '''TFT5ForConditionalGeneration''',
						    '''TFT5Model''',
						    '''TFT5PreTrainedModel''',
						]

try:
						if not is_flax_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__				:  Any													=  [
						    '''FlaxT5EncoderModel''',
						    '''FlaxT5ForConditionalGeneration''',
						    '''FlaxT5Model''',
						    '''FlaxT5PreTrainedModel''',
						]


if TYPE_CHECKING:
						from .configuration_ta import T5_PRETRAINED_CONFIG_ARCHIVE_MAP, TaConfig, TaOnnxConfig

						try:
												if not is_sentencepiece_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_ta import TaTokenizer

						try:
												if not is_tokenizers_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_ta_fast import TaTokenizerFast

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_ta import (
												    T5_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TaEncoderModel,
												    TaForConditionalGeneration,
												    TaForQuestionAnswering,
												    TaModel,
												    TaPreTrainedModel,
												    load_tf_weights_in_ta,
												)

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_ta import (
												    TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TFTaEncoderModel,
												    TFTaForConditionalGeneration,
												    TFTaModel,
												    TFTaPreTrainedModel,
												)

						try:
												if not is_flax_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_flax_ta import (
												    FlaxTaEncoderModel,
												    FlaxTaForConditionalGeneration,
												    FlaxTaModel,
												    FlaxTaPreTrainedModel,
												)


else:
						import sys

						lowerCamelCase__				:  Dict													=  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





