


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      ,     _UpperCAmelCase      ,     _UpperCAmelCase=13      ,     _UpperCAmelCase=7      ,     _UpperCAmelCase=True      ,     _UpperCAmelCase=True      ,     _UpperCAmelCase=True      ,     _UpperCAmelCase=True      ,     _UpperCAmelCase=99      ,     _UpperCAmelCase=16      ,     _UpperCAmelCase=36      ,     _UpperCAmelCase=6      ,     _UpperCAmelCase=6      ,     _UpperCAmelCase=6      ,     _UpperCAmelCase=37      ,     _UpperCAmelCase="gelu"      ,     _UpperCAmelCase=0.1      ,     _UpperCAmelCase=0.1      ,     _UpperCAmelCase=512      ,     _UpperCAmelCase=16      ,     _UpperCAmelCase=2      ,     _UpperCAmelCase=0.0_2      ,     _UpperCAmelCase=3      ,     _UpperCAmelCase=4      ,     _UpperCAmelCase=None      ,     ):







													"""simple docstring"""


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


							def     lowercase__  ( self		):







													"""simple docstring"""


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

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

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

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

													a_		      =		self.get_config()

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


							def     lowercase__  ( 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     lowercase__  ( self      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase		):







													"""simple docstring"""


													a_		      =		AlbertModel(config=__SCREAMING_SNAKE_CASE		)
													model.to(__SCREAMING_SNAKE_CASE		)
													model.eval()
													a_		      =		model(__SCREAMING_SNAKE_CASE      ,     attention_mask=__SCREAMING_SNAKE_CASE      ,     token_type_ids=__SCREAMING_SNAKE_CASE		)
													a_		      =		model(__SCREAMING_SNAKE_CASE      ,     token_type_ids=__SCREAMING_SNAKE_CASE		)
													a_		      =		model(__SCREAMING_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     lowercase__  ( self      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase		):







													"""simple docstring"""


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


							def     lowercase__  ( self      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase		):







													"""simple docstring"""


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


							def     lowercase__  ( self      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase		):







													"""simple docstring"""


													a_		      =		AlbertForQuestionAnswering(config=__SCREAMING_SNAKE_CASE		)
													model.to(__SCREAMING_SNAKE_CASE		)
													model.eval()
													a_		      =		model(
													    __SCREAMING_SNAKE_CASE      ,     attention_mask=__SCREAMING_SNAKE_CASE      ,     token_type_ids=__SCREAMING_SNAKE_CASE      ,     start_positions=__SCREAMING_SNAKE_CASE      ,     end_positions=__SCREAMING_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     lowercase__  ( self      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase		):







													"""simple docstring"""


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


							def     lowercase__  ( self      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase		):







													"""simple docstring"""


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


							def     lowercase__  ( self      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase		):







													"""simple docstring"""


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






							def     lowercase__  ( self		):







													"""simple docstring"""


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





@require_torch
class 							lowercase_					(			__snake_case      ,__snake_case      ,unittest.TestCase):






							"""simple docstring"""







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


							def     lowercase__  ( self      ,     _UpperCAmelCase      ,     _UpperCAmelCase      ,     _UpperCAmelCase=False		):







													"""simple docstring"""


													a_		      =		super()._prepare_for_class(__SCREAMING_SNAKE_CASE      ,     __SCREAMING_SNAKE_CASE      ,     return_labels=__SCREAMING_SNAKE_CASE		)

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


							def     lowercase__  ( self		):







													"""simple docstring"""


													a_		      =		AlbertModelTester(self		)
													a_		      =		ConfigTester(self      ,     config_class=__SCREAMING_SNAKE_CASE      ,     hidden_size=37		)


							def     lowercase__  ( self		):







													"""simple docstring"""


													self.config_tester.run_common_tests()


							def     lowercase__  ( self		):







													"""simple docstring"""


													a_		      =		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE		)


							def     lowercase__  ( self		):







													"""simple docstring"""


													a_		      =		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_pretraining(*__SCREAMING_SNAKE_CASE		)


							def     lowercase__  ( self		):







													"""simple docstring"""


													a_		      =		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE		)


							def     lowercase__  ( self		):







													"""simple docstring"""


													a_		      =		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*__SCREAMING_SNAKE_CASE		)


							def     lowercase__  ( self		):







													"""simple docstring"""


													a_		      =		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE		)


							def     lowercase__  ( self		):







													"""simple docstring"""


													a_		      =		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE		)


							def     lowercase__  ( self		):







													"""simple docstring"""


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






							@slow
							def     lowercase__  ( self		):







													"""simple docstring"""


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





@require_torch
class 							lowercase_					(			unittest.TestCase):






							"""simple docstring"""







							@slow
							def     lowercase__  ( self		):







													"""simple docstring"""


													a_		      =		AlbertModel.from_pretrained("""albert-base-v2"""		)
													a_		      =		torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]		)
													a_		      =		torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
													with torch.no_grad():
																			a_		      =		model(__SCREAMING_SNAKE_CASE      ,     attention_mask=__SCREAMING_SNAKE_CASE		)[0]
													a_		      =		torch.Size((1, 11, 768)		)
													self.assertEqual(output.shape      ,     __SCREAMING_SNAKE_CASE		)
													a_		      =		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]      ,     __SCREAMING_SNAKE_CASE      ,     atol=1e-4		)		)







"""simple docstring"""





import unittest

from datasets import load_dataset

from transformers.pipelines import pipeline
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_torch, slow






@is_pipeline_test
@require_torch
class 	a		(   unittest.TestCase						):






    @require_torch
    def   UpperCamelCase			(			self	:				str   )	->						str:
      lowerCamelCase_       	=		pipeline(
          task='zero-shot-audio-classification'			,		model='hf-internal-testing/tiny-clap-htsat-unfused'   )
      lowerCamelCase_       	=		load_dataset('ashraq/esc50'   )
      lowerCamelCase_       	=		dataset['train']['audio'][-1]['array']
      lowerCamelCase_       	=		audio_classifier(__SCREAMING_SNAKE_CASE			,		candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']   )
      self.assertEqual(
          nested_simplify(__SCREAMING_SNAKE_CASE   )			,		[{'score': 0.501, 'label': 'Sound of a dog'}, {'score': 0.499, 'label': 'Sound of vaccum cleaner'}]			,		)






    @unittest.skip('No models are available in TF'   )
    def   UpperCamelCase			(			self	:				Tuple   )	->						Optional[Any]:
      pass






    @slow
    @require_torch
    def   UpperCamelCase			(			self	:				Optional[Any]   )	->						Optional[Any]:
      lowerCamelCase_       	=		pipeline(
          task='zero-shot-audio-classification'			,		model='laion/clap-htsat-unfused'			,		)
      # This is an audio of a dog
      lowerCamelCase_       	=		load_dataset('ashraq/esc50'   )
      lowerCamelCase_       	=		dataset['train']['audio'][-1]['array']
      lowerCamelCase_       	=		audio_classifier(__SCREAMING_SNAKE_CASE			,		candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']   )

      self.assertEqual(
          nested_simplify(__SCREAMING_SNAKE_CASE   )			,		[
              {'score': 0.999, 'label': 'Sound of a dog'},
              {'score': 0.001, 'label': 'Sound of vaccum cleaner'},
          ]			,		)

      lowerCamelCase_       	=		audio_classifier([audio] * 5			,		candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']   )
      self.assertEqual(
          nested_simplify(__SCREAMING_SNAKE_CASE   )			,		[
              [
                  {'score': 0.999, 'label': 'Sound of a dog'},
                  {'score': 0.001, 'label': 'Sound of vaccum cleaner'},
              ],
          ]
          * 5			,		)
      lowerCamelCase_       	=		audio_classifier(
          [audio] * 5			,		candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']			,		batch_size=5   )
      self.assertEqual(
          nested_simplify(__SCREAMING_SNAKE_CASE   )			,		[
              [
                  {'score': 0.999, 'label': 'Sound of a dog'},
                  {'score': 0.001, 'label': 'Sound of vaccum cleaner'},
              ],
          ]
          * 5			,		)






    @unittest.skip('No models are available in TF'   )
    def   UpperCamelCase			(			self	:				Optional[int]   )	->						int:
      pass




