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






     """simple docstring"""







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







       """simple docstring"""




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



     def  UpperCAmelCase__							(				self							:							List[str] ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Optional[int]					=		ids_tensor([self.batch_size, self.seq_length]     ,						self.vocab_size )

       __SCREAMING_SNAKE_CASE      :	Tuple					=		None
       if self.use_input_mask:
         __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		random_attention_mask([self.batch_size, self.seq_length] )

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

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

       __SCREAMING_SNAKE_CASE      :	Optional[int]					=		self.get_config()

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



     def  UpperCAmelCase__							(				self							:							Optional[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  UpperCAmelCase__							(				self							:							Dict     ,						_A							:							Optional[int]     ,						_A							:							Dict     ,						_A							:							str     ,						_A							:							Union[str, Any]     ,						_A							:							Optional[Any]     ,						_A							:							Optional[Any]     ,						_A							:							Union[str, Any] ):







       """simple docstring"""




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







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Optional[int]					=		AlbertForPreTraining(config=_A )
       model.to(_A )
       model.eval()
       __SCREAMING_SNAKE_CASE      :	Tuple					=		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  UpperCAmelCase__							(				self							:							Dict     ,						_A							:							Optional[int]     ,						_A							:							List[str]     ,						_A							:							Union[str, Any]     ,						_A							:							Optional[Any]     ,						_A							:							Any     ,						_A							:							List[str]     ,						_A							:							Dict ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	List[str]					=		AlbertForMaskedLM(config=_A )
       model.to(_A )
       model.eval()
       __SCREAMING_SNAKE_CASE      :	int					=		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  UpperCAmelCase__							(				self							:							Tuple     ,						_A							:							Any     ,						_A							:							Tuple     ,						_A							:							Union[str, Any]     ,						_A							:							Tuple     ,						_A							:							int     ,						_A							:							Optional[int]     ,						_A							:							str ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Any					=		AlbertForQuestionAnswering(config=_A )
       model.to(_A )
       model.eval()
       __SCREAMING_SNAKE_CASE      :	List[str]					=		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  UpperCAmelCase__							(				self							:							List[str]     ,						_A							:							List[str]     ,						_A							:							str     ,						_A							:							int     ,						_A							:							Any     ,						_A							:							Optional[Any]     ,						_A							:							Optional[int]     ,						_A							:							List[str] ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Dict					=		self.num_labels
       __SCREAMING_SNAKE_CASE      :	Optional[int]					=		AlbertForSequenceClassification(_A )
       model.to(_A )
       model.eval()
       __SCREAMING_SNAKE_CASE      :	Tuple					=		model(_A     ,						attention_mask=_A     ,						token_type_ids=_A     ,						labels=_A )
       self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.num_labels) )



     def  UpperCAmelCase__							(				self							:							List[str]     ,						_A							:							Optional[int]     ,						_A							:							Optional[Any]     ,						_A							:							Union[str, Any]     ,						_A							:							List[str]     ,						_A							:							Dict     ,						_A							:							Optional[int]     ,						_A							:							str ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Tuple					=		self.num_labels
       __SCREAMING_SNAKE_CASE      :	str					=		AlbertForTokenClassification(config=_A )
       model.to(_A )
       model.eval()
       __SCREAMING_SNAKE_CASE      :	int					=		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  UpperCAmelCase__							(				self							:							Optional[int]     ,						_A							:							List[str]     ,						_A							:							List[str]     ,						_A							:							Tuple     ,						_A							:							Optional[Any]     ,						_A							:							Optional[int]     ,						_A							:							Any     ,						_A							:							int ):







       """simple docstring"""




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




     def  UpperCAmelCase__							(				self							:							Optional[int] ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Dict					=		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[str]					=		config_and_inputs
       __SCREAMING_SNAKE_CASE      :	Any					=		{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
       return config, inputs_dict






@require_torch
class     __UpperCamelCase (							lowerCAmelCase__ ,						lowerCAmelCase__ ,						unittest.TestCase       ):






     """simple docstring"""







     lowerCAmelCase_							   =			(
         (
             AlbertModel,
             AlbertForPreTraining,
             AlbertForMaskedLM,
             AlbertForMultipleChoice,
             AlbertForSequenceClassification,
             AlbertForTokenClassification,
             AlbertForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     lowerCAmelCase_							   =			(
         {
             '''feature-extraction''': AlbertModel,
             '''fill-mask''': AlbertForMaskedLM,
             '''question-answering''': AlbertForQuestionAnswering,
             '''text-classification''': AlbertForSequenceClassification,
             '''token-classification''': AlbertForTokenClassification,
             '''zero-shot''': AlbertForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     lowerCAmelCase_							   =			True



     def  UpperCAmelCase__							(				self							:							int     ,						_A							:							Optional[int]     ,						_A							:							List[str]     ,						_A							:							int=False ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	List[str]					=		super()._prepare_for_class(_A     ,						_A     ,						return_labels=_A )

       if return_labels:
         if model_class in get_values(_A ):
           __SCREAMING_SNAKE_CASE      :	Dict					=		torch.zeros(
               (self.model_tester.batch_size, self.model_tester.seq_length)     ,						dtype=torch.long     ,						device=_A )
           __SCREAMING_SNAKE_CASE      :	Tuple					=		torch.zeros(
               self.model_tester.batch_size     ,						dtype=torch.long     ,						device=_A )
       return inputs_dict



     def  UpperCAmelCase__							(				self							:							Dict ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	str					=		AlbertModelTester(self )
       __SCREAMING_SNAKE_CASE      :	Optional[int]					=		ConfigTester(self     ,						config_class=_A     ,						hidden_size=37 )



     def  UpperCAmelCase__							(				self							:							Optional[Any] ):







       """simple docstring"""




       self.config_tester.run_common_tests()



     def  UpperCAmelCase__							(				self							:							Optional[int] ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Dict					=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_A )



     def  UpperCAmelCase__							(				self							:							Optional[int] ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Tuple					=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_pretraining(*_A )



     def  UpperCAmelCase__							(				self							:							List[str] ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Optional[int]					=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*_A )



     def  UpperCAmelCase__							(				self							:							Tuple ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	str					=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*_A )



     def  UpperCAmelCase__							(				self							:							Tuple ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Tuple					=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*_A )



     def  UpperCAmelCase__							(				self							:							Dict ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	int					=		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*_A )



     def  UpperCAmelCase__							(				self							:							str ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Dict					=		self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
         __SCREAMING_SNAKE_CASE      :	str					=		type
         self.model_tester.create_and_check_model(*_A )




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







       """simple docstring"""




       for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __SCREAMING_SNAKE_CASE      :	Tuple					=		AlbertModel.from_pretrained(_A )
         self.assertIsNotNone(_A )






@require_torch
class     __UpperCamelCase (							unittest.TestCase       ):






     """simple docstring"""







     @slow
     def  UpperCAmelCase__							(				self							:							Tuple ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Tuple					=		AlbertModel.from_pretrained('''albert-base-v2''' )
       __SCREAMING_SNAKE_CASE      :	Tuple					=		torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]] )
       __SCREAMING_SNAKE_CASE      :	List[Any]					=		torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
       with torch.no_grad():
         __SCREAMING_SNAKE_CASE      :	int					=		model(_A     ,						attention_mask=_A )[0]
       __SCREAMING_SNAKE_CASE      :	List[Any]					=		torch.Size((1, 11, 768) )
       self.assertEqual(output.shape     ,						_A )
       __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		torch.tensor(
           [[[-0.65_13, 1.50_35, -0.27_66], [-0.65_15, 1.50_46, -0.27_80], [-0.65_12, 1.50_49, -0.27_84]]] )

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




import os
import zipfile

import requests
from get_ci_error_statistics import download_artifact, get_artifacts_links






def 					UpperCamelCase   (      __magic_name__	:				Dict       ,       __magic_name__	:				List[str]=7	)  ->    Dict:
		"""simple docstring"""




		lowercase__	     =						None
		if token is not None:
				lowercase__	     =						{"""Accept""": """application/vnd.github+json""", """Authorization""": f'''Bearer {token}'''}

		# The id of a workflow (not of a workflow run)
		lowercase__	     =						"""636036"""

		lowercase__	     =						f'''https://api.github.com/repos/huggingface/transformers/actions/workflows/{workflow_id}/runs'''
		# On `main` branch + event being `schedule` + not returning PRs + only `num_runs` results
		url += f'''?branch=main&event=schedule&exclude_pull_requests=true&per_page={num_runs}'''

		lowercase__	     =						requests.get(__magic_name__       ,       headers=__magic_name__	).json()

		return result["workflow_runs"]






def 					UpperCamelCase   (      __magic_name__	:				str	)  ->    Dict:
		"""simple docstring"""




		lowercase__	     =						get_daily_ci_runs(__magic_name__	)
		lowercase__	     =						None
		for workflow_run in workflow_runs:
				if workflow_run["status"] == "completed":
						lowercase__	     =						workflow_run["""id"""]
						break

		return workflow_run_id






def 					UpperCamelCase   (      __magic_name__	:				str       ,       __magic_name__	:				List[Any]       ,       __magic_name__	:				Dict	)  ->    str:
		"""simple docstring"""




		lowercase__	     =						get_last_daily_ci_runs(__magic_name__	)
		if workflow_run_id is not None:
				lowercase__	     =						get_artifacts_links(worflow_run_id=__magic_name__       ,       token=__magic_name__	)
				for artifact_name in artifact_names:
						if artifact_name in artifacts_links:
								lowercase__	     =						artifacts_links[artifact_name]
								download_artifact(
								    artifact_name=__magic_name__       ,       artifact_url=__magic_name__       ,       output_dir=__magic_name__       ,       token=__magic_name__	)







def 					UpperCamelCase   (      __magic_name__	:				List[str]       ,       __magic_name__	:				str       ,       __magic_name__	:				Union[str, Any]	)  ->    Tuple:
		"""simple docstring"""




		get_last_daily_ci_artifacts(__magic_name__       ,       __magic_name__       ,       __magic_name__	)

		lowercase__	     =						{}
		for artifact_name in artifact_names:
				lowercase__	     =						os.path.join(__magic_name__       ,       f'''{artifact_name}.zip'''	)
				if os.path.isfile(__magic_name__	):
						lowercase__	     =						{}
						with zipfile.ZipFile(__magic_name__	) as z:
								for filename in z.namelist():
										if not os.path.isdir(__magic_name__	):
												# read the file
												with z.open(__magic_name__	) as f:
														lowercase__	     =						f.read().decode("""UTF-8"""	)

		return results

