



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     __lowerCAmelCase					:



			def __init__(		self     :						Union[str, Any]				,	A     :						Union[str, Any]				,	A     :						List[str]=13				,	A     :						List[str]=7				,	A     :						List[Any]=True				,	A     :						List[Any]=True				,	A     :						int=True				,	A     :						Union[str, Any]=True				,	A     :						Dict=99				,	A     :						int=16				,	A     :						Optional[int]=36				,	A     :						List[Any]=6				,	A     :						int=6				,	A     :						Optional[int]=6				,	A     :						str=37				,	A     :						int="gelu"				,	A     :						int=0.1				,	A     :						List[Any]=0.1				,	A     :						str=5_12				,	A     :						str=16				,	A     :						List[Any]=2				,	A     :						Optional[int]=0.0_2				,	A     :						Any=3				,	A     :						List[Any]=4				,	A     :						Optional[int]=None				,	)     ->					str:



						"""simple docstring"""






						_UpperCAmelCase	 =     parent
						_UpperCAmelCase	 =     batch_size
						_UpperCAmelCase	 =     seq_length
						_UpperCAmelCase	 =     is_training
						_UpperCAmelCase	 =     use_input_mask
						_UpperCAmelCase	 =     use_token_type_ids
						_UpperCAmelCase	 =     use_labels
						_UpperCAmelCase	 =     vocab_size
						_UpperCAmelCase	 =     embedding_size
						_UpperCAmelCase	 =     hidden_size
						_UpperCAmelCase	 =     num_hidden_layers
						_UpperCAmelCase	 =     num_hidden_groups
						_UpperCAmelCase	 =     num_attention_heads
						_UpperCAmelCase	 =     intermediate_size
						_UpperCAmelCase	 =     hidden_act
						_UpperCAmelCase	 =     hidden_dropout_prob
						_UpperCAmelCase	 =     attention_probs_dropout_prob
						_UpperCAmelCase	 =     max_position_embeddings
						_UpperCAmelCase	 =     type_vocab_size
						_UpperCAmelCase	 =     type_sequence_label_size
						_UpperCAmelCase	 =     initializer_range
						_UpperCAmelCase	 =     num_labels
						_UpperCAmelCase	 =     num_choices
						_UpperCAmelCase	 =     scope



			def 					_lowerCamelCase (		self     :						Dict)     ->					Dict:



						"""simple docstring"""






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

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

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

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

						_UpperCAmelCase	 =     self.get_config()

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



			def 					_lowerCamelCase (		self     :						Union[str, Any])     ->					int:



						"""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 					_lowerCamelCase (		self     :						Optional[int]				,	A     :						Optional[int]				,	A     :						str				,	A     :						int				,	A     :						Tuple				,	A     :						Union[str, Any]				,	A     :						List[Any]				,	A     :						Optional[int])     ->					Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     AlbertModel(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase	 =     model(A				,	attention_mask=A				,	token_type_ids=A)
						_UpperCAmelCase	 =     model(A				,	token_type_ids=A)
						_UpperCAmelCase	 =     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 					_lowerCamelCase (		self     :						List[Any]				,	A     :						List[str]				,	A     :						Any				,	A     :						Tuple				,	A     :						List[Any]				,	A     :						int				,	A     :						List[str]				,	A     :						Optional[Any])     ->					List[Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     AlbertForPreTraining(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase	 =     model(
						    A				,	attention_mask=A				,	token_type_ids=A				,	labels=A				,	sentence_order_label=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, config.num_labels))



			def 					_lowerCamelCase (		self     :						Optional[Any]				,	A     :						Any				,	A     :						Any				,	A     :						Dict				,	A     :						Optional[Any]				,	A     :						int				,	A     :						int				,	A     :						Dict)     ->					Any:



						"""simple docstring"""






						_UpperCAmelCase	 =     AlbertForMaskedLM(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase	 =     model(A				,	attention_mask=A				,	token_type_ids=A				,	labels=A)
						self.parent.assertEqual(result.logits.shape				,	(self.batch_size, self.seq_length, self.vocab_size))



			def 					_lowerCamelCase (		self     :						Tuple				,	A     :						List[str]				,	A     :						List[Any]				,	A     :						Tuple				,	A     :						Any				,	A     :						str				,	A     :						List[Any]				,	A     :						List[str])     ->					Tuple:



						"""simple docstring"""






						_UpperCAmelCase	 =     AlbertForQuestionAnswering(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase	 =     model(
						    A				,	attention_mask=A				,	token_type_ids=A				,	start_positions=A				,	end_positions=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 					_lowerCamelCase (		self     :						List[Any]				,	A     :						List[Any]				,	A     :						List[str]				,	A     :						Union[str, Any]				,	A     :						Union[str, Any]				,	A     :						Optional[int]				,	A     :						Any				,	A     :						List[str])     ->					Optional[int]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.num_labels
						_UpperCAmelCase	 =     AlbertForSequenceClassification(A)
						model.to(A)
						model.eval()
						_UpperCAmelCase	 =     model(A				,	attention_mask=A				,	token_type_ids=A				,	labels=A)
						self.parent.assertEqual(result.logits.shape				,	(self.batch_size, self.num_labels))



			def 					_lowerCamelCase (		self     :						Optional[Any]				,	A     :						Optional[Any]				,	A     :						Optional[Any]				,	A     :						int				,	A     :						Tuple				,	A     :						Any				,	A     :						Union[str, Any]				,	A     :						List[str])     ->					List[str]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.num_labels
						_UpperCAmelCase	 =     AlbertForTokenClassification(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase	 =     model(A				,	attention_mask=A				,	token_type_ids=A				,	labels=A)
						self.parent.assertEqual(result.logits.shape				,	(self.batch_size, self.seq_length, self.num_labels))



			def 					_lowerCamelCase (		self     :						Union[str, Any]				,	A     :						List[Any]				,	A     :						Optional[int]				,	A     :						Union[str, Any]				,	A     :						Optional[int]				,	A     :						Tuple				,	A     :						List[str]				,	A     :						int)     ->					Tuple:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.num_choices
						_UpperCAmelCase	 =     AlbertForMultipleChoice(config=A)
						model.to(A)
						model.eval()
						_UpperCAmelCase	 =     input_ids.unsqueeze(1).expand(-1				,	self.num_choices				,	-1).contiguous()
						_UpperCAmelCase	 =     token_type_ids.unsqueeze(1).expand(-1				,	self.num_choices				,	-1).contiguous()
						_UpperCAmelCase	 =     input_mask.unsqueeze(1).expand(-1				,	self.num_choices				,	-1).contiguous()
						_UpperCAmelCase	 =     model(
						    A				,	attention_mask=A				,	token_type_ids=A				,	labels=A				,	)
						self.parent.assertEqual(result.logits.shape				,	(self.batch_size, self.num_choices))



			def 					_lowerCamelCase (		self     :						str)     ->					Dict:



						"""simple docstring"""






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







@require_torch
class     __lowerCAmelCase					(    A       ,		A       ,		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 					_lowerCamelCase (		self     :						Optional[Any]				,	A     :						Optional[Any]				,	A     :						Optional[int]				,	A     :						Tuple=False)     ->					Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     super()._prepare_for_class(A				,	A				,	return_labels=A)

						if return_labels:
									if model_class in get_values(A):
												_UpperCAmelCase	 =     torch.zeros(
												    (self.model_tester.batch_size, self.model_tester.seq_length)				,	dtype=torch.long				,	device=A)
												_UpperCAmelCase	 =     torch.zeros(
												    self.model_tester.batch_size				,	dtype=torch.long				,	device=A)
						return inputs_dict



			def 					_lowerCamelCase (		self     :						Optional[int])     ->					Optional[int]:



						"""simple docstring"""






						_UpperCAmelCase	 =     AlbertModelTester(self)
						_UpperCAmelCase	 =     ConfigTester(self				,	config_class=A				,	hidden_size=37)



			def 					_lowerCamelCase (		self     :						int)     ->					Tuple:



						"""simple docstring"""






						self.config_tester.run_common_tests()



			def 					_lowerCamelCase (		self     :						List[str])     ->					str:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*A)



			def 					_lowerCamelCase (		self     :						str)     ->					List[str]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_pretraining(*A)



			def 					_lowerCamelCase (		self     :						Union[str, Any])     ->					int:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*A)



			def 					_lowerCamelCase (		self     :						Tuple)     ->					str:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*A)



			def 					_lowerCamelCase (		self     :						str)     ->					Tuple:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*A)



			def 					_lowerCamelCase (		self     :						str)     ->					int:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*A)



			def 					_lowerCamelCase (		self     :						int)     ->					Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
									_UpperCAmelCase	 =     type
									self.model_tester.create_and_check_model(*A)



			@slow
			def 					_lowerCamelCase (		self     :						Union[str, Any])     ->					Optional[Any]:



						"""simple docstring"""






						for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_UpperCAmelCase	 =     AlbertModel.from_pretrained(A)
									self.assertIsNotNone(A)







@require_torch
class     __lowerCAmelCase					(    unittest.TestCase		):



			@slow
			def 					_lowerCamelCase (		self     :						Any)     ->					List[Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     AlbertModel.from_pretrained('albert-base-v2')
						_UpperCAmelCase	 =     torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]])
						_UpperCAmelCase	 =     torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
						with torch.no_grad():
									_UpperCAmelCase	 =     model(A				,	attention_mask=A)[0]
						_UpperCAmelCase	 =     torch.Size((1, 11, 7_68))
						self.assertEqual(output.shape				,	A)
						_UpperCAmelCase	 =     torch.tensor(
						    [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]])

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





from typing import TYPE_CHECKING

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


UpperCAmelCase__     = {"configuration_yolos": ["YOLOS_PRETRAINED_CONFIG_ARCHIVE_MAP", "YolosConfig", "YolosOnnxConfig"]}

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase__     = ["YolosFeatureExtractor"]
							UpperCAmelCase__     = ["YolosImageProcessor"]

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase__     = [
							    "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__     = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
