



'''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        SCREAMING_SNAKE_CASE   :
  """simple docstring"""







  def __init__(  self   :					List[Any]					,					__lowerCAmelCase   :					Any					,					__lowerCAmelCase   :					str=13					,					__lowerCAmelCase   :					List[Any]=7					,					__lowerCAmelCase   :					Optional[int]=True					,					__lowerCAmelCase   :					Dict=True					,					__lowerCAmelCase   :					List[Any]=True					,					__lowerCAmelCase   :					Dict=True					,					__lowerCAmelCase   :					Optional[int]=99					,					__lowerCAmelCase   :					List[str]=16					,					__lowerCAmelCase   :					int=32					,					__lowerCAmelCase   :					Tuple=2					,					__lowerCAmelCase   :					List[str]=4					,					__lowerCAmelCase   :					Tuple=37					,					__lowerCAmelCase   :					Optional[Any]="gelu"					,					__lowerCAmelCase   :					Optional[Any]=0.1					,					__lowerCAmelCase   :					Optional[Any]=0.1					,					__lowerCAmelCase   :					Optional[Any]=512					,					__lowerCAmelCase   :					Tuple=16					,					__lowerCAmelCase   :					Tuple=2					,					__lowerCAmelCase   :					Union[str, Any]=0.0_2					,					__lowerCAmelCase   :					Optional[Any]=3					,					__lowerCAmelCase   :					Any=4					,					__lowerCAmelCase   :					List[str]=None					,					):


        """simple docstring"""



        _lowerCAmelCase		   =   parent
        _lowerCAmelCase		   =   13
        _lowerCAmelCase		   =   7
        _lowerCAmelCase		   =   True
        _lowerCAmelCase		   =   True
        _lowerCAmelCase		   =   True
        _lowerCAmelCase		   =   True
        _lowerCAmelCase		   =   99
        _lowerCAmelCase		   =   16
        _lowerCAmelCase		   =   32
        _lowerCAmelCase		   =   2
        _lowerCAmelCase		   =   4
        _lowerCAmelCase		   =   37
        _lowerCAmelCase		   =   'gelu'
        _lowerCAmelCase		   =   0.1
        _lowerCAmelCase		   =   0.1
        _lowerCAmelCase		   =   512
        _lowerCAmelCase		   =   16
        _lowerCAmelCase		   =   2
        _lowerCAmelCase		   =   0.0_2
        _lowerCAmelCase		   =   3
        _lowerCAmelCase		   =   4
        _lowerCAmelCase		   =   None



  def   a   (  self   :					Optional[int]					):


        """simple docstring"""



        _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		   =   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   a   (  self   :					str					,					__lowerCAmelCase   :					List[str]					,					__lowerCAmelCase   :					Optional[int]					,					__lowerCAmelCase   :					List[str]					,					__lowerCAmelCase   :					int					,					__lowerCAmelCase   :					Optional[int]					,					__lowerCAmelCase   :					int					,					__lowerCAmelCase   :					Tuple					):


        """simple docstring"""



        _lowerCAmelCase		   =   TFAlbertModel(config=__lowerCAmelCase					)
        # inputs = {'input_ids': input_ids,
        #           'attention_mask': input_mask,
        #           'token_type_ids': token_type_ids}
        # sequence_output, pooled_output = model(**inputs)
        _lowerCAmelCase		   =   {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        _lowerCAmelCase		   =   model(__lowerCAmelCase					)

        _lowerCAmelCase		   =   [input_ids, input_mask]
        _lowerCAmelCase		   =   model(__lowerCAmelCase					)

        _lowerCAmelCase		   =   model(__lowerCAmelCase					)

        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   a   (  self   :					Dict					,					__lowerCAmelCase   :					Tuple					,					__lowerCAmelCase   :					Optional[int]					,					__lowerCAmelCase   :					int					,					__lowerCAmelCase   :					int					,					__lowerCAmelCase   :					Optional[Any]					,					__lowerCAmelCase   :					Tuple					,					__lowerCAmelCase   :					Union[str, Any]					):


        """simple docstring"""



        _lowerCAmelCase		   =   self.num_labels
        _lowerCAmelCase		   =   TFAlbertForPreTraining(config=__lowerCAmelCase					)
        _lowerCAmelCase		   =   {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        _lowerCAmelCase		   =   model(__lowerCAmelCase					)
        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   a   (  self   :					Dict					,					__lowerCAmelCase   :					str					,					__lowerCAmelCase   :					Dict					,					__lowerCAmelCase   :					Optional[Any]					,					__lowerCAmelCase   :					List[str]					,					__lowerCAmelCase   :					Any					,					__lowerCAmelCase   :					List[str]					,					__lowerCAmelCase   :					Dict					):


        """simple docstring"""



        _lowerCAmelCase		   =   TFAlbertForMaskedLM(config=__lowerCAmelCase					)
        _lowerCAmelCase		   =   {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        _lowerCAmelCase		   =   model(__lowerCAmelCase					)
        self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.seq_length, self.vocab_size)					)



  def   a   (  self   :					List[Any]					,					__lowerCAmelCase   :					List[str]					,					__lowerCAmelCase   :					str					,					__lowerCAmelCase   :					Any					,					__lowerCAmelCase   :					int					,					__lowerCAmelCase   :					Any					,					__lowerCAmelCase   :					int					,					__lowerCAmelCase   :					Union[str, Any]					):


        """simple docstring"""



        _lowerCAmelCase		   =   self.num_labels
        _lowerCAmelCase		   =   TFAlbertForSequenceClassification(config=__lowerCAmelCase					)
        _lowerCAmelCase		   =   {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        _lowerCAmelCase		   =   model(__lowerCAmelCase					)
        self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.num_labels)					)



  def   a   (  self   :					Dict					,					__lowerCAmelCase   :					List[Any]					,					__lowerCAmelCase   :					Tuple					,					__lowerCAmelCase   :					Any					,					__lowerCAmelCase   :					List[str]					,					__lowerCAmelCase   :					List[Any]					,					__lowerCAmelCase   :					Tuple					,					__lowerCAmelCase   :					Union[str, Any]					):


        """simple docstring"""



        _lowerCAmelCase		   =   TFAlbertForQuestionAnswering(config=__lowerCAmelCase					)
        _lowerCAmelCase		   =   {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
        _lowerCAmelCase		   =   model(__lowerCAmelCase					)
        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   a   (  self   :					Union[str, Any]					,					__lowerCAmelCase   :					List[Any]					,					__lowerCAmelCase   :					List[str]					,					__lowerCAmelCase   :					List[str]					,					__lowerCAmelCase   :					Union[str, Any]					,					__lowerCAmelCase   :					Any					,					__lowerCAmelCase   :					int					,					__lowerCAmelCase   :					List[Any]					):


        """simple docstring"""



        _lowerCAmelCase		   =   self.num_choices
        _lowerCAmelCase		   =   TFAlbertForMultipleChoice(config=__lowerCAmelCase					)
        _lowerCAmelCase		   =   tf.tile(tf.expand_dims(__lowerCAmelCase					,					1					)					,					(1, self.num_choices, 1)					)
        _lowerCAmelCase		   =   tf.tile(tf.expand_dims(__lowerCAmelCase					,					1					)					,					(1, self.num_choices, 1)					)
        _lowerCAmelCase		   =   tf.tile(tf.expand_dims(__lowerCAmelCase					,					1					)					,					(1, self.num_choices, 1)					)
        _lowerCAmelCase		   =   {
            'input_ids': multiple_choice_inputs_ids,
            'attention_mask': multiple_choice_input_mask,
            'token_type_ids': multiple_choice_token_type_ids,
        }
        _lowerCAmelCase		   =   model(__lowerCAmelCase					)
        self.parent.assertListEqual(list(result['logits'].shape					)					,					[self.batch_size, self.num_choices]					)



  def   a   (  self   :					List[Any]					,					__lowerCAmelCase   :					List[Any]					,					__lowerCAmelCase   :					Any					,					__lowerCAmelCase   :					str					,					__lowerCAmelCase   :					Union[str, Any]					,					__lowerCAmelCase   :					Any					,					__lowerCAmelCase   :					List[str]					,					__lowerCAmelCase   :					Tuple					):


        """simple docstring"""



        _lowerCAmelCase		   =   self.num_labels
        _lowerCAmelCase		   =   TFAlbertForTokenClassification(config=__lowerCAmelCase					)
        _lowerCAmelCase		   =   {
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'token_type_ids': token_type_ids,
        }
        _lowerCAmelCase		   =   model(__lowerCAmelCase					)
        self.parent.assertListEqual(list(result['logits'].shape					)					,					[self.batch_size, self.seq_length, self.num_labels]					)



  def   a   (  self   :					int					):


        """simple docstring"""



        _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_tf
class        SCREAMING_SNAKE_CASE   (				lowercase_	,						lowercase_	,						unittest.TestCase ):
  """simple docstring"""







  __A     =		(
      (
          TFAlbertModel,
          TFAlbertForPreTraining,
          TFAlbertForMaskedLM,
          TFAlbertForSequenceClassification,
          TFAlbertForQuestionAnswering,
          TFAlbertForTokenClassification,
          TFAlbertForMultipleChoice,
      )
      if is_tf_available()
      else ()
  )
  __A     =		(
      {
          """feature-extraction""": TFAlbertModel,
          """fill-mask""": TFAlbertForMaskedLM,
          """question-answering""": TFAlbertForQuestionAnswering,
          """text-classification""": TFAlbertForSequenceClassification,
          """token-classification""": TFAlbertForTokenClassification,
          """zero-shot""": TFAlbertForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  __A     =		False
  __A     =		False



  def   a   (  self   :					Any					,					__lowerCAmelCase   :					Optional[int]					,					__lowerCAmelCase   :					int					,					__lowerCAmelCase   :					Union[str, Any]=False					):


        """simple docstring"""



        _lowerCAmelCase		   =   super()._prepare_for_class(__lowerCAmelCase					,					__lowerCAmelCase					,					return_labels=__lowerCAmelCase					)

        if return_labels:
              if model_class in get_values(__lowerCAmelCase					):
                    _lowerCAmelCase		   =   tf.zeros(self.model_tester.batch_size					,					dtype=tf.intaa					)

        return inputs_dict



  def   a   (  self   :					List[Any]					):


        """simple docstring"""



        _lowerCAmelCase		   =   TFAlbertModelTester(self					)
        _lowerCAmelCase		   =   ConfigTester(self					,					config_class=__lowerCAmelCase					,					hidden_size=37					)



  def   a   (  self   :					Tuple					):


        """simple docstring"""



        self.config_tester.run_common_tests()



  def   a   (  self   :					Optional[int]					):


        """simple docstring"""



        _lowerCAmelCase		   =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_model(*__lowerCAmelCase					)



  def   a   (  self   :					int					):


        """simple docstring"""



        _lowerCAmelCase		   =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_pretraining(*__lowerCAmelCase					)



  def   a   (  self   :					List[str]					):


        """simple docstring"""



        _lowerCAmelCase		   =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_masked_lm(*__lowerCAmelCase					)



  def   a   (  self   :					List[str]					):


        """simple docstring"""



        _lowerCAmelCase		   =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_multiple_choice(*__lowerCAmelCase					)



  def   a   (  self   :					Optional[int]					):


        """simple docstring"""



        _lowerCAmelCase		   =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_sequence_classification(*__lowerCAmelCase					)



  def   a   (  self   :					int					):


        """simple docstring"""



        _lowerCAmelCase		   =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_question_answering(*__lowerCAmelCase					)



  @slow
  def   a   (  self   :					List[str]					):


        """simple docstring"""



        for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _lowerCAmelCase		   =   TFAlbertModel.from_pretrained(__lowerCAmelCase					)
              self.assertIsNotNone(__lowerCAmelCase					)





@require_tf
class        SCREAMING_SNAKE_CASE   (				unittest.TestCase ):
  """simple docstring"""







  @slow
  def   a   (  self   :					List[str]					):


        """simple docstring"""



        _lowerCAmelCase		   =   TFAlbertForPreTraining.from_pretrained('albert-base-v2'					)
        _lowerCAmelCase		   =   tf.constant([[0, 1, 2, 3, 4, 5]]					)
        _lowerCAmelCase		   =   model(__lowerCAmelCase					)[0]

        _lowerCAmelCase		   =   [1, 6, 3_0000]
        self.assertEqual(output.shape					,					__lowerCAmelCase					)

        _lowerCAmelCase		   =   tf.constant(
            [
                [
                    [4.5_9_5_6_6_8, 0.7_4_4_6_2_7_5_4, -1.8_1_8_1_4_7],
                    [4.5_9_5_4_3_4_7, 0.7_4_5_4_1_8_4, -1.8_1_8_8_2_5_8],
                    [4.5_9_5_4_9_0_5, 0.7_4_4_8_2_3_5, -1.8_1_8_2_3_1_6],
                ]
            ]					)
        tf.debugging.assert_near(output[:, :3, :3]					,					__lowerCAmelCase					,					atol=1e-4					)






'''simple docstring'''




import unittest

from transformers import is_torch_available, load_tool

from .test_tools_common import ToolTesterMixin


if is_torch_available():
 import torch





class        SCREAMING_SNAKE_CASE   (				unittest.TestCase	,						__a ):
  """simple docstring"""







  def   a   (  self   :					List[str]					):


        """simple docstring"""



        _lowerCAmelCase		   =   load_tool('speech-to-text'					)
        self.tool.setup()



  def   a   (  self   :					Any					):


        """simple docstring"""



        _lowerCAmelCase		   =   self.tool(torch.ones(3000					)					)
        self.assertEqual(__lowerCAmelCase					,					' you'					)



  def   a   (  self   :					Optional[int]					):


        """simple docstring"""



        _lowerCAmelCase		   =   self.tool(audio=torch.ones(3000					)					)
        self.assertEqual(__lowerCAmelCase					,					' you'					)





