



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





					'''simple docstring'''



					def __init__(			self ,				lowerCamelCase__ ,				lowerCamelCase__=1_3 ,				lowerCamelCase__=7 ,				lowerCamelCase__=True ,				lowerCamelCase__=True ,				lowerCamelCase__=True ,				lowerCamelCase__=True ,				lowerCamelCase__=9_9 ,				lowerCamelCase__=1_6 ,				lowerCamelCase__=3_2 ,				lowerCamelCase__=2 ,				lowerCamelCase__=4 ,				lowerCamelCase__=3_7 ,				lowerCamelCase__="gelu" ,				lowerCamelCase__=0.1 ,				lowerCamelCase__=0.1 ,				lowerCamelCase__=5_1_2 ,				lowerCamelCase__=1_6 ,				lowerCamelCase__=2 ,				lowerCamelCase__=0.0_2 ,				lowerCamelCase__=3 ,				lowerCamelCase__=4 ,				lowerCamelCase__=None ,				):
									_lowerCamelCase					     = parent
									_lowerCamelCase					     = 1_3
									_lowerCamelCase					     = 7
									_lowerCamelCase					     = True
									_lowerCamelCase					     = True
									_lowerCamelCase					     = True
									_lowerCamelCase					     = True
									_lowerCamelCase					     = 9_9
									_lowerCamelCase					     = 1_6
									_lowerCamelCase					     = 3_2
									_lowerCamelCase					     = 2
									_lowerCamelCase					     = 4
									_lowerCamelCase					     = 3_7
									_lowerCamelCase					     = '''gelu'''
									_lowerCamelCase					     = 0.1
									_lowerCamelCase					     = 0.1
									_lowerCamelCase					     = 5_1_2
									_lowerCamelCase					     = 1_6
									_lowerCamelCase					     = 2
									_lowerCamelCase					     = 0.0_2
									_lowerCamelCase					     = 3
									_lowerCamelCase					     = 4
									_lowerCamelCase					     = None

					def 	snake_case__					(			self      ):
									_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 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_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 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_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 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_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 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_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 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_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 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_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 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_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 	snake_case__					(			self      ):
									_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 							lowerCamelCase_(      A__, A__, unittest.TestCase			):





					'''simple docstring'''



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

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__=False      ):
									_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 	snake_case__					(			self      ):
									_lowerCamelCase					     = TFAlbertModelTester(self      )
									_lowerCamelCase					     = ConfigTester(self ,				config_class=lowerCamelCase__ ,				hidden_size=3_7      )

					def 	snake_case__					(			self      ):
									self.config_tester.run_common_tests()

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_albert_model(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_albert_for_pretraining(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_albert_for_masked_lm(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_albert_for_multiple_choice(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_albert_for_sequence_classification(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_albert_for_question_answering(*lowerCamelCase__      )





					@slow
					def 	snake_case__					(			self      ):
									for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_lowerCamelCase					     = TFAlbertModel.from_pretrained(lowerCamelCase__      )
													self.assertIsNotNone(lowerCamelCase__      )


@require_tf
class 							lowerCamelCase_(      unittest.TestCase			):





					'''simple docstring'''



					@slow
					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = TFAlbertForPreTraining.from_pretrained('''albert-base-v2'''      )
									_lowerCamelCase					     = tf.constant([[0, 1, 2, 3, 4, 5]]      )
									_lowerCamelCase					     = model(lowerCamelCase__      )[0]

									_lowerCamelCase					     = [1, 6, 3_0_0_0_0]
									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 copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


_UpperCamelCase				:    Optional[int]     =						logging.get_logger(__name__)






class 						snake_case  (  UpperCAmelCase       ):
    __magic_name__         =     '''speech-encoder-decoder'''
    __magic_name__         =     True





    def __init__(							self	:					Optional[Any]				,		**A	:					List[Any]			):


      '''simple docstring'''




      super().__init__(**A			)
      if "encoder" not in kwargs or "decoder" not in kwargs:
        raise ValueError(
            F'''A configuraton of type {self.model_type} cannot be instantiated because not both `encoder` and'''
            F''' `decoder` sub-configurations are passed, but only {kwargs}'''			)

      a     :			Optional[int]				=   kwargs.pop('encoder'			)
      a     :			Any				=   encoder_config.pop('model_type'			)
      a     :			Optional[Any]				=   kwargs.pop('decoder'			)
      a     :			str				=   decoder_config.pop('model_type'			)

      a     :			Tuple				=   AutoConfig.for_model(A				,		**A			)
      a     :			Dict				=   AutoConfig.for_model(A				,		**A			)
      a     :			Optional[Any]				=   True





    @classmethod
    def 				lowerCamelCase__      (							cls	:					List[Any]				,		A	:					PretrainedConfig				,		A	:					PretrainedConfig				,		**A	:					str			):


      '''simple docstring'''




      logger.info('Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'			)
      a     :			Any				=   True
      a     :			List[str]				=   True

      return cls(encoder=encoder_config.to_dict()				,		decoder=decoder_config.to_dict()				,		**A			)





    def 				lowerCamelCase__      (							self	:					Optional[int]			):


      '''simple docstring'''




      a     :			List[Any]				=   copy.deepcopy(self.__dict__			)
      a     :			int				=   self.encoder.to_dict()
      a     :			Optional[int]				=   self.decoder.to_dict()
      a     :			Any				=   self.__class__.model_type
      return output




