


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    :





							"""simple docstring"""







							def __init__(      self			,						lowerCAmelCase__			,						lowerCAmelCase__=1_3			,						lowerCAmelCase__=7			,						lowerCAmelCase__=True			,						lowerCAmelCase__=True			,						lowerCAmelCase__=True			,						lowerCAmelCase__=True			,						lowerCAmelCase__=9_9			,						lowerCAmelCase__=1_6			,						lowerCAmelCase__=3_6			,						lowerCAmelCase__=6			,						lowerCAmelCase__=6			,						lowerCAmelCase__=6			,						lowerCAmelCase__=3_7			,						lowerCAmelCase__="gelu"			,						lowerCAmelCase__=0.1			,						lowerCAmelCase__=0.1			,						lowerCAmelCase__=5_1_2			,						lowerCAmelCase__=1_6			,						lowerCAmelCase__=2			,						lowerCAmelCase__=0.02			,						lowerCAmelCase__=3			,						lowerCAmelCase__=4			,						lowerCAmelCase__=None			,						):





										"""simple docstring"""




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






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					ids_tensor([self.batch_size, self.seq_length]			,						self.vocab_size       )

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

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

										SCREAMING_SNAKE_CASE_		:     Tuple					=					None
										SCREAMING_SNAKE_CASE_		:     List[str]					=					None
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					None
										if self.use_labels:
													SCREAMING_SNAKE_CASE_		:     int					=					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_		:     int					=					ids_tensor([self.batch_size]			,						self.num_choices       )

										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					self.get_config()

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






							def 					UpperCamelCase__     (      self       ):





										"""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 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




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





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					AlbertForPreTraining(config=_lowercase       )
										model.to(_lowercase       )
										model.eval()
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					model(
										    _lowercase			,						attention_mask=_lowercase			,						token_type_ids=_lowercase			,						labels=_lowercase			,						sentence_order_label=_lowercase			,						)
										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 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




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






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					AlbertForQuestionAnswering(config=_lowercase       )
										model.to(_lowercase       )
										model.eval()
										SCREAMING_SNAKE_CASE_		:     Any					=					model(
										    _lowercase			,						attention_mask=_lowercase			,						token_type_ids=_lowercase			,						start_positions=_lowercase			,						end_positions=_lowercase			,						)
										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 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     List[Any]					=					self.num_labels
										SCREAMING_SNAKE_CASE_		:     Dict					=					AlbertForSequenceClassification(_lowercase       )
										model.to(_lowercase       )
										model.eval()
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					model(_lowercase			,						attention_mask=_lowercase			,						token_type_ids=_lowercase			,						labels=_lowercase       )
										self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_labels)       )






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					self.num_labels
										SCREAMING_SNAKE_CASE_		:     Any					=					AlbertForTokenClassification(config=_lowercase       )
										model.to(_lowercase       )
										model.eval()
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					model(_lowercase			,						attention_mask=_lowercase			,						token_type_ids=_lowercase			,						labels=_lowercase       )
										self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.seq_length, self.num_labels)       )






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Tuple					=					self.num_choices
										SCREAMING_SNAKE_CASE_		:     Any					=					AlbertForMultipleChoice(config=_lowercase       )
										model.to(_lowercase       )
										model.eval()
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					input_ids.unsqueeze(1       ).expand(-1			,						self.num_choices			,						-1       ).contiguous()
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					token_type_ids.unsqueeze(1       ).expand(-1			,						self.num_choices			,						-1       ).contiguous()
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					input_mask.unsqueeze(1       ).expand(-1			,						self.num_choices			,						-1       ).contiguous()
										SCREAMING_SNAKE_CASE_		:     Tuple					=					model(
										    _lowercase			,						attention_mask=_lowercase			,						token_type_ids=_lowercase			,						labels=_lowercase			,						)
										self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_choices)       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     str					=					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_       
										)		,             
										)		:     Tuple					=					config_and_inputs
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
										return config, inputs_dict







@require_torch
class 	__lowercase    (_SCREAMING_SNAKE_CASE	,       _SCREAMING_SNAKE_CASE	,       unittest.TestCase  ):





							"""simple docstring"""







							_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 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__=False       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     List[Any]					=					super()._prepare_for_class(_lowercase			,						_lowercase			,						return_labels=_lowercase       )

										if return_labels:
													if model_class in get_values(_lowercase       ):
																SCREAMING_SNAKE_CASE_		:     List[Any]					=					torch.zeros(
																    (self.model_tester.batch_size, self.model_tester.seq_length)			,						dtype=torch.long			,						device=_lowercase       )
																SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					torch.zeros(
																    self.model_tester.batch_size			,						dtype=torch.long			,						device=_lowercase       )
										return inputs_dict






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					AlbertModelTester(self       )
										SCREAMING_SNAKE_CASE_		:     List[str]					=					ConfigTester(self			,						config_class=_lowercase			,						hidden_size=3_7       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										self.config_tester.run_common_tests()






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     int					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_lowercase       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Any					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_pretraining(*_lowercase       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Any					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*_lowercase       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*_lowercase       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Dict					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*_lowercase       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     str					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*_lowercase       )






							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Any					=					self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
													SCREAMING_SNAKE_CASE_		:     List[Any]					=					type
													self.model_tester.create_and_check_model(*_lowercase       )






							@slow
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




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







@require_torch
class 	__lowercase    (unittest.TestCase  ):





							"""simple docstring"""







							@slow
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Any					=					AlbertModel.from_pretrained('albert-base-v2'       )
										SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]       )
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
										with torch.no_grad():
													SCREAMING_SNAKE_CASE_		:     Optional[int]					=					model(_lowercase			,						attention_mask=_lowercase       )[0]
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					torch.Size((1, 1_1, 7_6_8)       )
										self.assertEqual(output.shape			,						_lowercase       )
										SCREAMING_SNAKE_CASE_		:     Tuple					=					torch.tensor(
										    [[[-0.6_513, 1.5_035, -0.2_766], [-0.6_515, 1.5_046, -0.2_780], [-0.6_512, 1.5_049, -0.2_784]]]       )

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







'''simple docstring'''



from .testing import (
    are_the_same_tensors,
    execute_subprocess_async,
    require_bnb,
    require_cpu,
    require_cuda,
    require_huggingface_suite,
    require_mps,
    require_multi_gpu,
    require_multi_xpu,
    require_safetensors,
    require_single_gpu,
    require_single_xpu,
    require_torch_min_version,
    require_tpu,
    require_xpu,
    skip,
    slow,
)
from .training import RegressionDataset, RegressionModel, RegressionModelaXPU


from .scripts import test_script, test_sync, test_ops  # isort: skip
