
'''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						:					List[Any],     SCREAMING_SNAKE_CASE_						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					str=13,     SCREAMING_SNAKE_CASE_						:					List[Any]=7,     SCREAMING_SNAKE_CASE_						:					Dict=True,     SCREAMING_SNAKE_CASE_						:					Optional[Any]=True,     SCREAMING_SNAKE_CASE_						:					Tuple=True,     SCREAMING_SNAKE_CASE_						:					Dict=True,     SCREAMING_SNAKE_CASE_						:					str=99,     SCREAMING_SNAKE_CASE_						:					Any=16,     SCREAMING_SNAKE_CASE_						:					Any=32,     SCREAMING_SNAKE_CASE_						:					List[str]=2,     SCREAMING_SNAKE_CASE_						:					Any=4,     SCREAMING_SNAKE_CASE_						:					str=37,     SCREAMING_SNAKE_CASE_						:					Tuple="gelu",     SCREAMING_SNAKE_CASE_						:					Tuple=0.1,     SCREAMING_SNAKE_CASE_						:					Optional[int]=0.1,     SCREAMING_SNAKE_CASE_						:					str=5_12,     SCREAMING_SNAKE_CASE_						:					Tuple=16,     SCREAMING_SNAKE_CASE_						:					List[Any]=2,     SCREAMING_SNAKE_CASE_						:					Optional[int]=0.02,     SCREAMING_SNAKE_CASE_						:					List[str]=3,     SCREAMING_SNAKE_CASE_						:					int=4,     SCREAMING_SNAKE_CASE_						:					Union[str, Any]=None,     ):
										snake_case							:  Tuple			=     parent
										snake_case							:  Union[str, Any]			=     13
										snake_case							:  List[Any]			=     7
										snake_case							:  Optional[Any]			=     True
										snake_case							:  Optional[int]			=     True
										snake_case							:  str			=     True
										snake_case							:  Any			=     True
										snake_case							:  Optional[Any]			=     99
										snake_case							:  int			=     16
										snake_case							:  Optional[Any]			=     32
										snake_case							:  Tuple			=     2
										snake_case							:  Any			=     4
										snake_case							:  str			=     37
										snake_case							:  int			=     '''gelu'''
										snake_case							:  Dict			=     0.1
										snake_case							:  Union[str, Any]			=     0.1
										snake_case							:  int			=     5_12
										snake_case							:  Optional[Any]			=     16
										snake_case							:  int			=     2
										snake_case							:  int			=     0.02
										snake_case							:  Dict			=     3
										snake_case							:  int			=     4
										snake_case							:  List[str]			=     None




				def 						__snake_case				(				self						:					Optional[Any]   ):
										snake_case							:  int			=     ids_tensor([self.batch_size, self.seq_length],     self.vocab_size   )

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

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

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

										snake_case							:  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						:					List[Any],     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					str,     SCREAMING_SNAKE_CASE_						:					Tuple,     SCREAMING_SNAKE_CASE_						:					Tuple,     SCREAMING_SNAKE_CASE_						:					List[str],     SCREAMING_SNAKE_CASE_						:					str   ):
										snake_case							:  str			=     TFAlbertModel(config=SCREAMING_SNAKE_CASE_   )
										# inputs = {'input_ids': input_ids,
										#           'attention_mask': input_mask,
										#           'token_type_ids': token_type_ids}
										# sequence_output, pooled_output = model(**inputs)
										snake_case							:  str			=     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										snake_case							:  int			=     model(SCREAMING_SNAKE_CASE_   )

										snake_case							:  Tuple			=     [input_ids, input_mask]
										snake_case							:  Optional[int]			=     model(SCREAMING_SNAKE_CASE_   )

										snake_case							:  Optional[Any]			=     model(SCREAMING_SNAKE_CASE_   )

										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						:					List[Any],     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					List[Any],     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					Optional[int],     SCREAMING_SNAKE_CASE_						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					List[str]   ):
										snake_case							:  Any			=     self.num_labels
										snake_case							:  str			=     TFAlbertForPreTraining(config=SCREAMING_SNAKE_CASE_   )
										snake_case							:  Tuple			=     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										snake_case							:  int			=     model(SCREAMING_SNAKE_CASE_   )
										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						:					Tuple,     SCREAMING_SNAKE_CASE_						:					Dict,     SCREAMING_SNAKE_CASE_						:					Any,     SCREAMING_SNAKE_CASE_						:					Optional[int],     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					Tuple,     SCREAMING_SNAKE_CASE_						:					str,     SCREAMING_SNAKE_CASE_						:					Optional[int]   ):
										snake_case							:  Optional[Any]			=     TFAlbertForMaskedLM(config=SCREAMING_SNAKE_CASE_   )
										snake_case							:  Optional[int]			=     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										snake_case							:  int			=     model(SCREAMING_SNAKE_CASE_   )
										self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.seq_length, self.vocab_size)   )




				def 						__snake_case				(				self						:					Any,     SCREAMING_SNAKE_CASE_						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					Optional[int],     SCREAMING_SNAKE_CASE_						:					List[Any],     SCREAMING_SNAKE_CASE_						:					List[Any],     SCREAMING_SNAKE_CASE_						:					Tuple,     SCREAMING_SNAKE_CASE_						:					Optional[Any]   ):
										snake_case							:  List[Any]			=     self.num_labels
										snake_case							:  Union[str, Any]			=     TFAlbertForSequenceClassification(config=SCREAMING_SNAKE_CASE_   )
										snake_case							:  Any			=     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										snake_case							:  Optional[int]			=     model(SCREAMING_SNAKE_CASE_   )
										self.parent.assertEqual(result.logits.shape,     (self.batch_size, self.num_labels)   )




				def 						__snake_case				(				self						:					Any,     SCREAMING_SNAKE_CASE_						:					Dict,     SCREAMING_SNAKE_CASE_						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					Optional[int],     SCREAMING_SNAKE_CASE_						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					Any,     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					Optional[Any]   ):
										snake_case							:  Dict			=     TFAlbertForQuestionAnswering(config=SCREAMING_SNAKE_CASE_   )
										snake_case							:  List[Any]			=     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										snake_case							:  List[str]			=     model(SCREAMING_SNAKE_CASE_   )
										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						:					int,     SCREAMING_SNAKE_CASE_						:					Any,     SCREAMING_SNAKE_CASE_						:					Union[str, Any],     SCREAMING_SNAKE_CASE_						:					List[Any],     SCREAMING_SNAKE_CASE_						:					List[str],     SCREAMING_SNAKE_CASE_						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					int,     SCREAMING_SNAKE_CASE_						:					Optional[int]   ):
										snake_case							:  Union[str, Any]			=     self.num_choices
										snake_case							:  List[str]			=     TFAlbertForMultipleChoice(config=SCREAMING_SNAKE_CASE_   )
										snake_case							:  Dict			=     tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_,     1   ),     (1, self.num_choices, 1)   )
										snake_case							:  Tuple			=     tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_,     1   ),     (1, self.num_choices, 1)   )
										snake_case							:  Optional[int]			=     tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_,     1   ),     (1, self.num_choices, 1)   )
										snake_case							:  Union[str, Any]			=     {
										    '''input_ids''': multiple_choice_inputs_ids,
										    '''attention_mask''': multiple_choice_input_mask,
										    '''token_type_ids''': multiple_choice_token_type_ids,
										}
										snake_case							:  List[str]			=     model(SCREAMING_SNAKE_CASE_   )
										self.parent.assertListEqual(list(result['''logits'''].shape   ),     [self.batch_size, self.num_choices]   )




				def 						__snake_case				(				self						:					Dict,     SCREAMING_SNAKE_CASE_						:					List[Any],     SCREAMING_SNAKE_CASE_						:					Dict,     SCREAMING_SNAKE_CASE_						:					Optional[int],     SCREAMING_SNAKE_CASE_						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					Tuple,     SCREAMING_SNAKE_CASE_						:					Dict,     SCREAMING_SNAKE_CASE_						:					Tuple   ):
										snake_case							:  List[Any]			=     self.num_labels
										snake_case							:  Union[str, Any]			=     TFAlbertForTokenClassification(config=SCREAMING_SNAKE_CASE_   )
										snake_case							:  int			=     {
										    '''input_ids''': input_ids,
										    '''attention_mask''': input_mask,
										    '''token_type_ids''': token_type_ids,
										}
										snake_case							:  Union[str, Any]			=     model(SCREAMING_SNAKE_CASE_   )
										self.parent.assertListEqual(list(result['''logits'''].shape   ),     [self.batch_size, self.seq_length, self.num_labels]   )




				def 						__snake_case				(				self						:					List[str]   ):
										snake_case							:  List[str]			=     self.prepare_config_and_inputs()
										(
										    (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       (
										    snake_case
										),       
										)							:  str			=     config_and_inputs
										snake_case							:  Any			=     {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
										return config, inputs_dict




@require_tf
class     a (					__magic_name__					,__magic_name__					,unittest.TestCase     ):
				_snake_case		     =			(
				    (
				        TFAlbertModel,
				        TFAlbertForPreTraining,
				        TFAlbertForMaskedLM,
				        TFAlbertForSequenceClassification,
				        TFAlbertForQuestionAnswering,
				        TFAlbertForTokenClassification,
				        TFAlbertForMultipleChoice,
				    )
				    if is_tf_available()
				    else ()
				)
				_snake_case		     =			(
				    {
				        '''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		     =			False
				_snake_case		     =			False




				def 						__snake_case				(				self						:					Optional[Any],     SCREAMING_SNAKE_CASE_						:					List[Any],     SCREAMING_SNAKE_CASE_						:					Optional[int],     SCREAMING_SNAKE_CASE_						:					Optional[int]=False   ):
										snake_case							:  Optional[Any]			=     super()._prepare_for_class(SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     return_labels=SCREAMING_SNAKE_CASE_   )

										if return_labels:
																if model_class in get_values(SCREAMING_SNAKE_CASE_   ):
																						snake_case							:  str			=     tf.zeros(self.model_tester.batch_size,     dtype=tf.intaa   )

										return inputs_dict




				def 						__snake_case				(				self						:					List[Any]   ):
										snake_case							:  Any			=     TFAlbertModelTester(self   )
										snake_case							:  int			=     ConfigTester(self,     config_class=SCREAMING_SNAKE_CASE_,     hidden_size=37   )




				def 						__snake_case				(				self						:					Optional[int]   ):
										self.config_tester.run_common_tests()




				def 						__snake_case				(				self						:					Union[str, Any]   ):
										snake_case							:  str			=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_albert_model(*SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					Optional[int]   ):
										snake_case							:  int			=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_albert_for_pretraining(*SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					Optional[int]   ):
										snake_case							:  List[Any]			=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_albert_for_masked_lm(*SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					Union[str, Any]   ):
										snake_case							:  List[Any]			=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_albert_for_multiple_choice(*SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					Optional[Any]   ):
										snake_case							:  Tuple			=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_albert_for_sequence_classification(*SCREAMING_SNAKE_CASE_   )




				def 						__snake_case				(				self						:					Optional[Any]   ):
										snake_case							:  List[Any]			=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_albert_for_question_answering(*SCREAMING_SNAKE_CASE_   )




				@slow
				def 						__snake_case				(				self						:					Tuple   ):
										for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																snake_case							:  Union[str, Any]			=     TFAlbertModel.from_pretrained(SCREAMING_SNAKE_CASE_   )
																self.assertIsNotNone(SCREAMING_SNAKE_CASE_   )




@require_tf
class     a (					unittest.TestCase     ):




				@slow
				def 						__snake_case				(				self						:					Union[str, Any]   ):
										snake_case							:  int			=     TFAlbertForPreTraining.from_pretrained('''albert-base-v2'''   )
										snake_case							:  Any			=     tf.constant([[0, 1, 2, 3, 4, 5]]   )
										snake_case							:  List[str]			=     model(SCREAMING_SNAKE_CASE_   )[0]

										snake_case							:  Tuple			=     [1, 6, 3_00_00]
										self.assertEqual(output.shape,     SCREAMING_SNAKE_CASE_   )

										snake_case							:  Dict			=     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],     SCREAMING_SNAKE_CASE_,     atol=1e-4   )









'''simple docstring'''







import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
			from PIL import Image

class _lowercase      (							unittest.TestCase							, _lowercase						):




							def 						lowerCamelCase_							(				self:							Tuple	):
														lowerCamelCase__   :      Optional[Any]			    =			load_tool("""image-captioning"""	)
														self.tool.setup()
														lowerCamelCase__   :      Optional[Any]			    =			load_tool("""image-captioning"""     ,				remote=UpperCamelCase__	)




							def 						lowerCamelCase_							(				self:							int	):
														lowerCamelCase__   :      List[Any]			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      Optional[Any]			    =			self.tool(UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)




							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														lowerCamelCase__   :      Tuple			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      Dict			    =			self.remote_tool(UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)




							def 						lowerCamelCase_							(				self:							str	):
														lowerCamelCase__   :      Optional[int]			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      str			    =			self.tool(image=UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)




							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														lowerCamelCase__   :      Optional[int]			    =			Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""	)	) / """000000039769.png"""	)
														lowerCamelCase__   :      Optional[int]			    =			self.remote_tool(image=UpperCamelCase__	)
														self.assertEqual(UpperCamelCase__     ,				"""two cats sleeping on a couch"""	)

