






"""simple docstring"""





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






    def __init__(			self	:				Dict			,		__SCREAMING_SNAKE_CASE	:				Tuple			,		__SCREAMING_SNAKE_CASE	:				Any=13			,		__SCREAMING_SNAKE_CASE	:				Union[str, Any]=7			,		__SCREAMING_SNAKE_CASE	:				Tuple=True			,		__SCREAMING_SNAKE_CASE	:				int=True			,		__SCREAMING_SNAKE_CASE	:				str=True			,		__SCREAMING_SNAKE_CASE	:				Dict=True			,		__SCREAMING_SNAKE_CASE	:				Any=99			,		__SCREAMING_SNAKE_CASE	:				Tuple=16			,		__SCREAMING_SNAKE_CASE	:				Optional[int]=36			,		__SCREAMING_SNAKE_CASE	:				List[str]=6			,		__SCREAMING_SNAKE_CASE	:				List[str]=6			,		__SCREAMING_SNAKE_CASE	:				Union[str, Any]=6			,		__SCREAMING_SNAKE_CASE	:				List[Any]=37			,		__SCREAMING_SNAKE_CASE	:				Any="gelu"			,		__SCREAMING_SNAKE_CASE	:				Optional[int]=0.1			,		__SCREAMING_SNAKE_CASE	:				List[str]=0.1			,		__SCREAMING_SNAKE_CASE	:				List[str]=512			,		__SCREAMING_SNAKE_CASE	:				Union[str, Any]=16			,		__SCREAMING_SNAKE_CASE	:				str=2			,		__SCREAMING_SNAKE_CASE	:				List[str]=0.02			,		__SCREAMING_SNAKE_CASE	:				Optional[int]=3			,		__SCREAMING_SNAKE_CASE	:				Optional[int]=4			,		__SCREAMING_SNAKE_CASE	:				Dict=None			,		)	->						Any:
      lowerCamelCase_       	=		parent
      lowerCamelCase_       	=		batch_size
      lowerCamelCase_       	=		seq_length
      lowerCamelCase_       	=		is_training
      lowerCamelCase_       	=		use_input_mask
      lowerCamelCase_       	=		use_token_type_ids
      lowerCamelCase_       	=		use_labels
      lowerCamelCase_       	=		vocab_size
      lowerCamelCase_       	=		embedding_size
      lowerCamelCase_       	=		hidden_size
      lowerCamelCase_       	=		num_hidden_layers
      lowerCamelCase_       	=		num_hidden_groups
      lowerCamelCase_       	=		num_attention_heads
      lowerCamelCase_       	=		intermediate_size
      lowerCamelCase_       	=		hidden_act
      lowerCamelCase_       	=		hidden_dropout_prob
      lowerCamelCase_       	=		attention_probs_dropout_prob
      lowerCamelCase_       	=		max_position_embeddings
      lowerCamelCase_       	=		type_vocab_size
      lowerCamelCase_       	=		type_sequence_label_size
      lowerCamelCase_       	=		initializer_range
      lowerCamelCase_       	=		num_labels
      lowerCamelCase_       	=		num_choices
      lowerCamelCase_       	=		scope






    def   UpperCamelCase			(			self	:				List[str]   )	->						Dict:
      lowerCamelCase_       	=		ids_tensor([self.batch_size, self.seq_length]			,		self.vocab_size   )

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

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

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

      lowerCamelCase_       	=		self.get_config()

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






    def   UpperCamelCase			(			self	:				Tuple   )	->						Tuple:
      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	:				Optional[int]			,		__SCREAMING_SNAKE_CASE	:				List[Any]			,		__SCREAMING_SNAKE_CASE	:				Dict			,		__SCREAMING_SNAKE_CASE	:				int			,		__SCREAMING_SNAKE_CASE	:				Optional[int]			,		__SCREAMING_SNAKE_CASE	:				Dict			,		__SCREAMING_SNAKE_CASE	:				Optional[Any]			,		__SCREAMING_SNAKE_CASE	:				Dict   )	->						str:
      lowerCamelCase_       	=		AlbertModel(config=__SCREAMING_SNAKE_CASE   )
      model.to(__SCREAMING_SNAKE_CASE   )
      model.eval()
      lowerCamelCase_       	=		model(__SCREAMING_SNAKE_CASE			,		attention_mask=__SCREAMING_SNAKE_CASE			,		token_type_ids=__SCREAMING_SNAKE_CASE   )
      lowerCamelCase_       	=		model(__SCREAMING_SNAKE_CASE			,		token_type_ids=__SCREAMING_SNAKE_CASE   )
      lowerCamelCase_       	=		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   UpperCamelCase			(			self	:				Union[str, Any]			,		__SCREAMING_SNAKE_CASE	:				Optional[int]			,		__SCREAMING_SNAKE_CASE	:				str			,		__SCREAMING_SNAKE_CASE	:				str			,		__SCREAMING_SNAKE_CASE	:				Union[str, Any]			,		__SCREAMING_SNAKE_CASE	:				str			,		__SCREAMING_SNAKE_CASE	:				Union[str, Any]			,		__SCREAMING_SNAKE_CASE	:				List[str]   )	->						Dict:
      lowerCamelCase_       	=		AlbertForPreTraining(config=__SCREAMING_SNAKE_CASE   )
      model.to(__SCREAMING_SNAKE_CASE   )
      model.eval()
      lowerCamelCase_       	=		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   UpperCamelCase			(			self	:				Dict			,		__SCREAMING_SNAKE_CASE	:				str			,		__SCREAMING_SNAKE_CASE	:				Dict			,		__SCREAMING_SNAKE_CASE	:				int			,		__SCREAMING_SNAKE_CASE	:				List[str]			,		__SCREAMING_SNAKE_CASE	:				Union[str, Any]			,		__SCREAMING_SNAKE_CASE	:				str			,		__SCREAMING_SNAKE_CASE	:				Dict   )	->						List[Any]:
      lowerCamelCase_       	=		AlbertForMaskedLM(config=__SCREAMING_SNAKE_CASE   )
      model.to(__SCREAMING_SNAKE_CASE   )
      model.eval()
      lowerCamelCase_       	=		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   UpperCamelCase			(			self	:				Dict			,		__SCREAMING_SNAKE_CASE	:				Optional[int]			,		__SCREAMING_SNAKE_CASE	:				Tuple			,		__SCREAMING_SNAKE_CASE	:				Union[str, Any]			,		__SCREAMING_SNAKE_CASE	:				str			,		__SCREAMING_SNAKE_CASE	:				List[str]			,		__SCREAMING_SNAKE_CASE	:				str			,		__SCREAMING_SNAKE_CASE	:				Optional[int]   )	->						List[str]:
      lowerCamelCase_       	=		AlbertForQuestionAnswering(config=__SCREAMING_SNAKE_CASE   )
      model.to(__SCREAMING_SNAKE_CASE   )
      model.eval()
      lowerCamelCase_       	=		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   UpperCamelCase			(			self	:				List[Any]			,		__SCREAMING_SNAKE_CASE	:				int			,		__SCREAMING_SNAKE_CASE	:				Dict			,		__SCREAMING_SNAKE_CASE	:				Dict			,		__SCREAMING_SNAKE_CASE	:				Optional[int]			,		__SCREAMING_SNAKE_CASE	:				List[Any]			,		__SCREAMING_SNAKE_CASE	:				Tuple			,		__SCREAMING_SNAKE_CASE	:				Optional[Any]   )	->						Tuple:
      lowerCamelCase_       	=		self.num_labels
      lowerCamelCase_       	=		AlbertForSequenceClassification(__SCREAMING_SNAKE_CASE   )
      model.to(__SCREAMING_SNAKE_CASE   )
      model.eval()
      lowerCamelCase_       	=		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   UpperCamelCase			(			self	:				Tuple			,		__SCREAMING_SNAKE_CASE	:				Dict			,		__SCREAMING_SNAKE_CASE	:				Tuple			,		__SCREAMING_SNAKE_CASE	:				int			,		__SCREAMING_SNAKE_CASE	:				Any			,		__SCREAMING_SNAKE_CASE	:				int			,		__SCREAMING_SNAKE_CASE	:				Tuple			,		__SCREAMING_SNAKE_CASE	:				str   )	->						List[str]:
      lowerCamelCase_       	=		self.num_labels
      lowerCamelCase_       	=		AlbertForTokenClassification(config=__SCREAMING_SNAKE_CASE   )
      model.to(__SCREAMING_SNAKE_CASE   )
      model.eval()
      lowerCamelCase_       	=		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   UpperCamelCase			(			self	:				Optional[int]			,		__SCREAMING_SNAKE_CASE	:				str			,		__SCREAMING_SNAKE_CASE	:				int			,		__SCREAMING_SNAKE_CASE	:				Optional[int]			,		__SCREAMING_SNAKE_CASE	:				List[str]			,		__SCREAMING_SNAKE_CASE	:				int			,		__SCREAMING_SNAKE_CASE	:				int			,		__SCREAMING_SNAKE_CASE	:				Optional[Any]   )	->						str:
      lowerCamelCase_       	=		self.num_choices
      lowerCamelCase_       	=		AlbertForMultipleChoice(config=__SCREAMING_SNAKE_CASE   )
      model.to(__SCREAMING_SNAKE_CASE   )
      model.eval()
      lowerCamelCase_       	=		input_ids.unsqueeze(1   ).expand(-1			,		self.num_choices			,		-1   ).contiguous()
      lowerCamelCase_       	=		token_type_ids.unsqueeze(1   ).expand(-1			,		self.num_choices			,		-1   ).contiguous()
      lowerCamelCase_       	=		input_mask.unsqueeze(1   ).expand(-1			,		self.num_choices			,		-1   ).contiguous()
      lowerCamelCase_       	=		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   UpperCamelCase			(			self	:				Optional[Any]   )	->						List[Any]:
      lowerCamelCase_       	=		self.prepare_config_and_inputs()
      (
          (
          lowerCamelCase_
      )	,      (
          lowerCamelCase_
      )	,      (
          lowerCamelCase_
      )	,      (
          lowerCamelCase_
      )	,      (
          lowerCamelCase_
      )	,      (
          lowerCamelCase_
      )	,      (
          lowerCamelCase_
      )	,      
      )       	=		config_and_inputs
      lowerCamelCase_       	=		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict






@require_torch
class 	a		(   __snake_case    , __snake_case    , unittest.TestCase						):
    SCREAMING_SNAKE_CASE		:	int		=					(
        (
            AlbertModel,
            AlbertForPreTraining,
            AlbertForMaskedLM,
            AlbertForMultipleChoice,
            AlbertForSequenceClassification,
            AlbertForTokenClassification,
            AlbertForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    SCREAMING_SNAKE_CASE		:	str		=					(
        {
            """feature-extraction""": AlbertModel,
            """fill-mask""": AlbertForMaskedLM,
            """question-answering""": AlbertForQuestionAnswering,
            """text-classification""": AlbertForSequenceClassification,
            """token-classification""": AlbertForTokenClassification,
            """zero-shot""": AlbertForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    SCREAMING_SNAKE_CASE		:	Optional[Any]		=					True






    def   UpperCamelCase			(			self	:				List[Any]			,		__SCREAMING_SNAKE_CASE	:				Tuple			,		__SCREAMING_SNAKE_CASE	:				Union[str, Any]			,		__SCREAMING_SNAKE_CASE	:				List[str]=False   )	->						Union[str, Any]:
      lowerCamelCase_       	=		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   ):
          lowerCamelCase_       	=		torch.zeros(
              (self.model_tester.batch_size, self.model_tester.seq_length)			,		dtype=torch.long			,		device=__SCREAMING_SNAKE_CASE   )
          lowerCamelCase_       	=		torch.zeros(
              self.model_tester.batch_size			,		dtype=torch.long			,		device=__SCREAMING_SNAKE_CASE   )
      return inputs_dict






    def   UpperCamelCase			(			self	:				Optional[int]   )	->						Any:
      lowerCamelCase_       	=		AlbertModelTester(self   )
      lowerCamelCase_       	=		ConfigTester(self			,		config_class=__SCREAMING_SNAKE_CASE			,		hidden_size=37   )






    def   UpperCamelCase			(			self	:				int   )	->						Dict:
      self.config_tester.run_common_tests()






    def   UpperCamelCase			(			self	:				Optional[Any]   )	->						Tuple:
      lowerCamelCase_       	=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE   )






    def   UpperCamelCase			(			self	:				List[Any]   )	->						int:
      lowerCamelCase_       	=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_pretraining(*__SCREAMING_SNAKE_CASE   )






    def   UpperCamelCase			(			self	:				List[str]   )	->						Union[str, Any]:
      lowerCamelCase_       	=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE   )






    def   UpperCamelCase			(			self	:				List[str]   )	->						List[Any]:
      lowerCamelCase_       	=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_multiple_choice(*__SCREAMING_SNAKE_CASE   )






    def   UpperCamelCase			(			self	:				Dict   )	->						str:
      lowerCamelCase_       	=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE   )






    def   UpperCamelCase			(			self	:				List[Any]   )	->						Tuple:
      lowerCamelCase_       	=		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE   )






    def   UpperCamelCase			(			self	:				Dict   )	->						Any:
      lowerCamelCase_       	=		self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
        lowerCamelCase_       	=		type
        self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE   )






    @slow
    def   UpperCamelCase			(			self	:				List[Any]   )	->						List[Any]:
      for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowerCamelCase_       	=		AlbertModel.from_pretrained(__SCREAMING_SNAKE_CASE   )
        self.assertIsNotNone(__SCREAMING_SNAKE_CASE   )






@require_torch
class 	a		(   unittest.TestCase						):






    @slow
    def   UpperCamelCase			(			self	:				int   )	->						Tuple:
      lowerCamelCase_       	=		AlbertModel.from_pretrained('albert-base-v2'   )
      lowerCamelCase_       	=		torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]   )
      lowerCamelCase_       	=		torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
      with torch.no_grad():
        lowerCamelCase_       	=		model(__SCREAMING_SNAKE_CASE			,		attention_mask=__SCREAMING_SNAKE_CASE   )[0]
      lowerCamelCase_       	=		torch.Size((1, 11, 768)   )
      self.assertEqual(output.shape			,		__SCREAMING_SNAKE_CASE   )
      lowerCamelCase_       	=		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]			,		__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




