"""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 	__UpperCAmelCase						:







			'''simple docstring'''





			def __init__( self      ,			snake_case_      ,			snake_case_=13      ,			snake_case_=7      ,			snake_case_=True      ,			snake_case_=True      ,			snake_case_=True      ,			snake_case_=True      ,			snake_case_=99      ,			snake_case_=16      ,			snake_case_=32      ,			snake_case_=2      ,			snake_case_=4      ,			snake_case_=37      ,			snake_case_="gelu"      ,			snake_case_=0.1      ,			snake_case_=0.1      ,			snake_case_=512      ,			snake_case_=16      ,			snake_case_=2      ,			snake_case_=0.02      ,			snake_case_=3      ,			snake_case_=4      ,			snake_case_=None      ,			):







										'''simple docstring'''


										A__					:    int														= parent
										A__					:    Dict														= 13
										A__					:    Optional[int]														= 7
										A__					:    Tuple														= True
										A__					:    Dict														= True
										A__					:    Optional[Any]														= True
										A__					:    str														= True
										A__					:    str														= 99
										A__					:    int														= 16
										A__					:    Tuple														= 32
										A__					:    int														= 2
										A__					:    Tuple														= 4
										A__					:    str														= 37
										A__					:    Union[str, Any]														= """gelu"""
										A__					:    Any														= 0.1
										A__					:    int														= 0.1
										A__					:    int														= 512
										A__					:    List[Any]														= 16
										A__					:    List[str]														= 2
										A__					:    Optional[Any]														= 0.02
										A__					:    int														= 3
										A__					:    Optional[Any]														= 4
										A__					:    Any														= None





			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										A__					:    Dict														= ids_tensor([self.batch_size, self.seq_length]      ,			self.vocab_size       )

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

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

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

										A__					:    Optional[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 		lowerCamelCase				( self      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_       ):







										'''simple docstring'''


										A__					:    List[str]														= TFAlbertModel(config=snake_case_       )
										# inputs = {'input_ids': input_ids,
										#           'attention_mask': input_mask,
										#           'token_type_ids': token_type_ids}
										# sequence_output, pooled_output = model(**inputs)
										A__					:    Union[str, Any]														= {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
										A__					:    List[Any]														= model(snake_case_       )

										A__					:    Dict														= [input_ids, input_mask]
										A__					:    Optional[Any]														= model(snake_case_       )

										A__					:    Tuple														= model(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 		lowerCamelCase				( self      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_       ):







										'''simple docstring'''


										A__					:    List[str]														= self.num_labels
										A__					:    Dict														= TFAlbertForPreTraining(config=snake_case_       )
										A__					:    Tuple														= {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
										A__					:    List[Any]														= model(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 		lowerCamelCase				( self      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_       ):







										'''simple docstring'''


										A__					:    Any														= TFAlbertForMaskedLM(config=snake_case_       )
										A__					:    Dict														= {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
										A__					:    Union[str, Any]														= model(snake_case_       )
										self.parent.assertEqual(result.logits.shape      ,			(self.batch_size, self.seq_length, self.vocab_size)       )





			def 		lowerCamelCase				( self      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_       ):







										'''simple docstring'''


										A__					:    str														= self.num_labels
										A__					:    Optional[Any]														= TFAlbertForSequenceClassification(config=snake_case_       )
										A__					:    Union[str, Any]														= {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
										A__					:    Union[str, Any]														= model(snake_case_       )
										self.parent.assertEqual(result.logits.shape      ,			(self.batch_size, self.num_labels)       )





			def 		lowerCamelCase				( self      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_       ):







										'''simple docstring'''


										A__					:    int														= TFAlbertForQuestionAnswering(config=snake_case_       )
										A__					:    Dict														= {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
										A__					:    List[str]														= model(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 		lowerCamelCase				( self      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_       ):







										'''simple docstring'''


										A__					:    Union[str, Any]														= self.num_choices
										A__					:    Optional[int]														= TFAlbertForMultipleChoice(config=snake_case_       )
										A__					:    Tuple														= tf.tile(tf.expand_dims(snake_case_      ,			1       )      ,			(1, self.num_choices, 1)       )
										A__					:    Optional[Any]														= tf.tile(tf.expand_dims(snake_case_      ,			1       )      ,			(1, self.num_choices, 1)       )
										A__					:    str														= tf.tile(tf.expand_dims(snake_case_      ,			1       )      ,			(1, self.num_choices, 1)       )
										A__					:    Any														= {
										    """input_ids""": multiple_choice_inputs_ids,
										    """attention_mask""": multiple_choice_input_mask,
										    """token_type_ids""": multiple_choice_token_type_ids,
										}
										A__					:    List[Any]														= model(snake_case_       )
										self.parent.assertListEqual(list(result["""logits"""].shape       )      ,			[self.batch_size, self.num_choices]       )





			def 		lowerCamelCase				( self      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_      ,			snake_case_       ):







										'''simple docstring'''


										A__					:    int														= self.num_labels
										A__					:    Dict														= TFAlbertForTokenClassification(config=snake_case_       )
										A__					:    List[Any]														= {
										    """input_ids""": input_ids,
										    """attention_mask""": input_mask,
										    """token_type_ids""": token_type_ids,
										}
										A__					:    int														= model(snake_case_       )
										self.parent.assertListEqual(list(result["""logits"""].shape       )      ,			[self.batch_size, self.seq_length, self.num_labels]       )





			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										A__					:    Dict														= self.prepare_config_and_inputs()
										(
										    A__       
										)					:    Dict														= config_and_inputs
										A__					:    Any														= {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
										return config, inputs_dict





@require_tf
class 	__UpperCAmelCase						(__A       ,      __A       ,      unittest.TestCase    ):







			'''simple docstring'''
			_UpperCamelCase     :	List[Any]				    =							(
			    (
			        TFAlbertModel,
			        TFAlbertForPreTraining,
			        TFAlbertForMaskedLM,
			        TFAlbertForSequenceClassification,
			        TFAlbertForQuestionAnswering,
			        TFAlbertForTokenClassification,
			        TFAlbertForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			_UpperCamelCase     :	List[str]				    =							(
			    {
			        'feature-extraction': TFAlbertModel,
			        'fill-mask': TFAlbertForMaskedLM,
			        'question-answering': TFAlbertForQuestionAnswering,
			        'text-classification': TFAlbertForSequenceClassification,
			        'token-classification': TFAlbertForTokenClassification,
			        'zero-shot': TFAlbertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			_UpperCamelCase     :	List[Any]				    =							False
			_UpperCamelCase     :	List[Any]				    =							False





			def 		lowerCamelCase				( self      ,			snake_case_      ,			snake_case_      ,			snake_case_=False       ):







										'''simple docstring'''


										A__					:    str														= super()._prepare_for_class(snake_case_      ,			snake_case_      ,			return_labels=snake_case_       )

										if return_labels:
																	if model_class in get_values(snake_case_       ):
																								A__					:    Dict														= tf.zeros(self.model_tester.batch_size      ,			dtype=tf.intaa       )

										return inputs_dict





			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										A__					:    Dict														= TFAlbertModelTester(self       )
										A__					:    Dict														= ConfigTester(self      ,			config_class=snake_case_      ,			hidden_size=37       )





			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										self.config_tester.run_common_tests()





			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										A__					:    int														= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_albert_model(*snake_case_       )





			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										A__					:    Dict														= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_albert_for_pretraining(*snake_case_       )





			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										A__					:    List[Any]														= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_albert_for_masked_lm(*snake_case_       )





			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										A__					:    Any														= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_albert_for_multiple_choice(*snake_case_       )





			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										A__					:    Union[str, Any]														= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_albert_for_sequence_classification(*snake_case_       )





			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										A__					:    Tuple														= self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_albert_for_question_answering(*snake_case_       )





			@slow
			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	A__					:    List[str]														= TFAlbertModel.from_pretrained(snake_case_       )
																	self.assertIsNotNone(snake_case_       )





@require_tf
class 	__UpperCAmelCase						(unittest.TestCase    ):







			'''simple docstring'''





			@slow
			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										A__					:    Union[str, Any]														= TFAlbertForPreTraining.from_pretrained("""albert-base-v2"""       )
										A__					:    List[str]														= tf.constant([[0, 1, 2, 3, 4, 5]]       )
										A__					:    Optional[Any]														= model(snake_case_       )[0]

										A__					:    str														= [1, 6, 30_000]
										self.assertEqual(output.shape      ,			snake_case_       )

										A__					:    str														= tf.constant(
										    [
										        [
										            [4.59_56_68, 0.74_46_27_54, -1.81_81_47],
										            [4.5_95_43_47, 0.7_45_41_84, -1.8_18_82_58],
										            [4.5_95_49_05, 0.7_44_82_35, -1.8_18_23_16],
										        ]
										    ]       )
										tf.debugging.assert_near(output[:, :3, :3]      ,			snake_case_      ,			atol=1E-4       )






"""simple docstring"""



import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_multi_gpu,
    require_single_gpu,
    test_sync,
)
from accelerate.utils import patch_environment
class 	__UpperCAmelCase						(unittest.TestCase    ):







			'''simple docstring'''





			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										A__					:    Optional[int]														= inspect.getfile(accelerate.test_utils       )
										A__					:    Tuple														= os.path.sep.join(mod_file.split(os.path.sep       )[:-1] + ["""scripts""", """test_sync.py"""]       )





			@require_cpu
			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										debug_launcher(test_sync.main      ,			num_processes=1       )





			@require_cpu
			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										debug_launcher(test_sync.main       )





			@require_single_gpu
			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										test_sync.main()





			@require_multi_gpu
			def 		lowerCamelCase				( self       ):







										'''simple docstring'''


										print(F'''Found {torch.cuda.device_count()} devices.'''       )
										A__					:    List[str]														= ["""torchrun""", F'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path]
										with patch_environment(omp_num_threads=1       ):
																	execute_subprocess_async(snake_case_      ,			env=os.environ.copy()       )





