import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    MODEL_FOR_PRETRAINING_MAPPING,
					    AlbertForMaskedLM,
					    AlbertForMultipleChoice,
					    AlbertForPreTraining,
					    AlbertForQuestionAnswering,
					    AlbertForSequenceClassification,
					    AlbertForTokenClassification,
					    AlbertModel,
					)
					from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST





class 					lowercase__						:


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





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Any							=							parent
												SCREAMING_SNAKE_CASE							:    int							=							batch_size
												SCREAMING_SNAKE_CASE							:    List[Any]							=							seq_length
												SCREAMING_SNAKE_CASE							:    str							=							is_training
												SCREAMING_SNAKE_CASE							:    int							=							use_input_mask
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							use_token_type_ids
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							use_labels
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							vocab_size
												SCREAMING_SNAKE_CASE							:    List[str]							=							embedding_size
												SCREAMING_SNAKE_CASE							:    List[str]							=							hidden_size
												SCREAMING_SNAKE_CASE							:    Dict							=							num_hidden_layers
												SCREAMING_SNAKE_CASE							:    Dict							=							num_hidden_groups
												SCREAMING_SNAKE_CASE							:    str							=							num_attention_heads
												SCREAMING_SNAKE_CASE							:    Dict							=							intermediate_size
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							hidden_act
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							hidden_dropout_prob
												SCREAMING_SNAKE_CASE							:    Tuple							=							attention_probs_dropout_prob
												SCREAMING_SNAKE_CASE							:    int							=							max_position_embeddings
												SCREAMING_SNAKE_CASE							:    str							=							type_vocab_size
												SCREAMING_SNAKE_CASE							:    Dict							=							type_sequence_label_size
												SCREAMING_SNAKE_CASE							:    Dict							=							initializer_range
												SCREAMING_SNAKE_CASE							:    Any							=							num_labels
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							num_choices
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							scope


						def        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





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

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

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							None
												if self.use_token_type_ids:
																		SCREAMING_SNAKE_CASE							:    Tuple							=							ids_tensor([self.batch_size, self.seq_length]    ,		self.type_vocab_size						)

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

												SCREAMING_SNAKE_CASE							:    List[str]							=							self.get_config()

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


						def        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





												return AlbertConfig(
												    vocab_size=self.vocab_size    ,		hidden_size=self.hidden_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    ,		num_hidden_groups=self.num_hidden_groups    ,		)


						def        __A       (       self	:				Optional[Any]    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Dict    ,		UpperCamelCase__	:				Dict    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Dict							=							AlbertModel(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    List[str]							=							model(UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		token_type_ids=UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Dict							=							model(UpperCamelCase__    ,		token_type_ids=UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							model(UpperCamelCase__						)
												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        __A       (       self	:				int    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				Dict    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				List[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Tuple							=							AlbertForPreTraining(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							model(
												    UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		token_type_ids=UpperCamelCase__    ,		labels=UpperCamelCase__    ,		sentence_order_label=UpperCamelCase__    ,		)
												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, config.num_labels)						)


						def        __A       (       self	:				Optional[int]    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				Any    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				List[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							AlbertForMaskedLM(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    int							=							model(UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		token_type_ids=UpperCamelCase__    ,		labels=UpperCamelCase__						)
												self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.vocab_size)						)


						def        __A       (       self	:				Union[str, Any]    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				Optional[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Any							=							AlbertForQuestionAnswering(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    Any							=							model(
												    UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		token_type_ids=UpperCamelCase__    ,		start_positions=UpperCamelCase__    ,		end_positions=UpperCamelCase__    ,		)
												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        __A       (       self	:				List[Any]    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				Any    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Tuple							=							self.num_labels
												SCREAMING_SNAKE_CASE							:    Tuple							=							AlbertForSequenceClassification(UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    int							=							model(UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		token_type_ids=UpperCamelCase__    ,		labels=UpperCamelCase__						)
												self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.num_labels)						)


						def        __A       (       self	:				List[Any]    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				Any    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[Any]							=							self.num_labels
												SCREAMING_SNAKE_CASE							:    Dict							=							AlbertForTokenClassification(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							model(UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		token_type_ids=UpperCamelCase__    ,		labels=UpperCamelCase__						)
												self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.num_labels)						)


						def        __A       (       self	:				Union[str, Any]    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    str							=							self.num_choices
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							AlbertForMultipleChoice(config=UpperCamelCase__						)
												model.to(UpperCamelCase__						)
												model.eval()
												SCREAMING_SNAKE_CASE							:    Tuple							=							input_ids.unsqueeze(1						).expand(-1    ,		self.num_choices    ,		-1						).contiguous()
												SCREAMING_SNAKE_CASE							:    Dict							=							token_type_ids.unsqueeze(1						).expand(-1    ,		self.num_choices    ,		-1						).contiguous()
												SCREAMING_SNAKE_CASE							:    int							=							input_mask.unsqueeze(1						).expand(-1    ,		self.num_choices    ,		-1						).contiguous()
												SCREAMING_SNAKE_CASE							:    List[str]							=							model(
												    UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		token_type_ids=UpperCamelCase__    ,		labels=UpperCamelCase__    ,		)
												self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.num_choices)						)



						def        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





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






@require_torch
class 					lowercase__						(   UpperCamelCase_	,							UpperCamelCase_	,							unittest.TestCase):
						UpperCamelCase_     =		(
						    (
						        AlbertModel,
						        AlbertForPreTraining,
						        AlbertForMaskedLM,
						        AlbertForMultipleChoice,
						        AlbertForSequenceClassification,
						        AlbertForTokenClassification,
						        AlbertForQuestionAnswering,
						    )
						    if is_torch_available()
						    else ()
						)
						UpperCamelCase_     =		(
						    {
						        """feature-extraction""": AlbertModel,
						        """fill-mask""": AlbertForMaskedLM,
						        """question-answering""": AlbertForQuestionAnswering,
						        """text-classification""": AlbertForSequenceClassification,
						        """token-classification""": AlbertForTokenClassification,
						        """zero-shot""": AlbertForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						UpperCamelCase_     =		True


						def        __A       (       self	:				Optional[Any]    ,		UpperCamelCase__	:				Any    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				Union[str, Any]=False						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Tuple							=							super()._prepare_for_class(UpperCamelCase__    ,		UpperCamelCase__    ,		return_labels=UpperCamelCase__						)

												if return_labels:
																		if model_class in get_values(UpperCamelCase__						):
																								SCREAMING_SNAKE_CASE							:    Optional[int]							=							torch.zeros(
																								    (self.model_tester.batch_size, self.model_tester.seq_length)    ,		dtype=torch.long    ,		device=UpperCamelCase__						)
																								SCREAMING_SNAKE_CASE							:    List[Any]							=							torch.zeros(
																								    self.model_tester.batch_size    ,		dtype=torch.long    ,		device=UpperCamelCase__						)
												return inputs_dict


						def        __A       (       self	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    int							=							AlbertModelTester(self						)
												SCREAMING_SNAKE_CASE							:    str							=							ConfigTester(self    ,		config_class=UpperCamelCase__    ,		hidden_size=37						)


						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												self.config_tester.run_common_tests()


						def        __A       (       self	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Tuple							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*UpperCamelCase__						)


						def        __A       (       self	:				str						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_pretraining(*UpperCamelCase__						)


						def        __A       (       self	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase__						)


						def        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase__						)


						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__						)


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[Any]							=							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__						)


						def        __A       (       self	:				str						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							self.model_tester.prepare_config_and_inputs()
												for type in ["absolute", "relative_key", "relative_key_query"]:
																		SCREAMING_SNAKE_CASE							:    Dict							=							type
																		self.model_tester.create_and_check_model(*UpperCamelCase__						)



						@slow
						def        __A       (       self	:				Optional[int]						):





												'''simple docstring'''





												for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE							:    Dict							=							AlbertModel.from_pretrained(UpperCamelCase__						)
																		self.assertIsNotNone(UpperCamelCase__						)






@require_torch
class 					lowercase__						(   unittest.TestCase):


						@slow
						def        __A       (       self	:				str						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							AlbertModel.from_pretrained('''albert-base-v2'''						)
												SCREAMING_SNAKE_CASE							:    Any							=							torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]						)
												SCREAMING_SNAKE_CASE							:    List[str]							=							torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
												with torch.no_grad():
																		SCREAMING_SNAKE_CASE							:    Dict							=							model(UpperCamelCase__    ,		attention_mask=UpperCamelCase__						)[0]
												SCREAMING_SNAKE_CASE							:    Dict							=							torch.Size((1, 11, 768)						)
												self.assertEqual(output.shape    ,		UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    int							=							torch.tensor(
												    [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]						)

												self.assertTrue(torch.allclose(output[:, 1:4, 1:4]    ,		UpperCamelCase__    ,		atol=1E-4						)						)






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__UpperCamelCase		:       int									=     {'configuration_yolos': ['YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP', 'YolosConfig', 'YolosOnnxConfig']}

try:
					if not is_vision_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__UpperCamelCase		:       int									=     ['YolosFeatureExtractor']
					__UpperCamelCase		:       Union[str, Any]									=     ['YolosImageProcessor']

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__UpperCamelCase		:       str									=     [
					    'YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST',
					    'YolosForObjectDetection',
					    'YolosModel',
					    'YolosPreTrainedModel',
					]


if TYPE_CHECKING:
					from .configuration_yolos import YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP, YolosConfig, YolosOnnxConfig

					try:
										if not is_vision_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .feature_extraction_yolos import YolosFeatureExtractor
										from .image_processing_yolos import YolosImageProcessor

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_yolos import (
										    YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST,
										    YolosForObjectDetection,
										    YolosModel,
										    YolosPreTrainedModel,
										)


else:
					import sys

					__UpperCamelCase		:       int									=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





