





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


    '''simple docstring'''







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





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= parent
        SCREAMING_SNAKE_CASE__       							= 13
        SCREAMING_SNAKE_CASE__       							= 7
        SCREAMING_SNAKE_CASE__       							= True
        SCREAMING_SNAKE_CASE__       							= True
        SCREAMING_SNAKE_CASE__       							= True
        SCREAMING_SNAKE_CASE__       							= True
        SCREAMING_SNAKE_CASE__       							= 99
        SCREAMING_SNAKE_CASE__       							= 16
        SCREAMING_SNAKE_CASE__       							= 32
        SCREAMING_SNAKE_CASE__       							= 2
        SCREAMING_SNAKE_CASE__       							= 4
        SCREAMING_SNAKE_CASE__       							= 37
        SCREAMING_SNAKE_CASE__       							= '''gelu'''
        SCREAMING_SNAKE_CASE__       							= 0.1
        SCREAMING_SNAKE_CASE__       							= 0.1
        SCREAMING_SNAKE_CASE__       							= 5_12
        SCREAMING_SNAKE_CASE__       							= 16
        SCREAMING_SNAKE_CASE__       							= 2
        SCREAMING_SNAKE_CASE__       							= 0.02
        SCREAMING_SNAKE_CASE__       							= 3
        SCREAMING_SNAKE_CASE__       							= 4
        SCREAMING_SNAKE_CASE__       							= None



    def    lowercase_				(					self    ):





        '''simple docstring'''

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

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

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

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

        SCREAMING_SNAKE_CASE__       							= 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    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_    ):





        '''simple docstring'''

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

        SCREAMING_SNAKE_CASE__       							= [input_ids, input_mask]
        SCREAMING_SNAKE_CASE__       							= model(lowerCamelCase_    )

        SCREAMING_SNAKE_CASE__       							= 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    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.num_labels
        SCREAMING_SNAKE_CASE__       							= TFAlbertForPreTraining(config=lowerCamelCase_    )
        SCREAMING_SNAKE_CASE__       							= {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
        SCREAMING_SNAKE_CASE__       							= 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    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_    ):





        '''simple docstring'''

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



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_    ):





        '''simple docstring'''

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



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= TFAlbertForQuestionAnswering(config=lowerCamelCase_    )
        SCREAMING_SNAKE_CASE__       							= {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
        SCREAMING_SNAKE_CASE__       							= 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    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_    ):





        '''simple docstring'''

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



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_    ):





        '''simple docstring'''

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




    def    lowercase_				(					self    ):





        '''simple docstring'''

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





@require_tf
class 				__snake_case  (     lowerCAmelCase_ ,    lowerCAmelCase_ ,    unittest.TestCase						):


    '''simple docstring'''







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



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_=False    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= super()._prepare_for_class(lowerCamelCase_      ,							lowerCamelCase_      ,							return_labels=lowerCamelCase_    )

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

        return inputs_dict



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= TFAlbertModelTester(self    )
        SCREAMING_SNAKE_CASE__       							= ConfigTester(self      ,							config_class=lowerCamelCase_      ,							hidden_size=37    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        self.config_tester.run_common_tests()



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_model(*lowerCamelCase_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_pretraining(*lowerCamelCase_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_masked_lm(*lowerCamelCase_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_multiple_choice(*lowerCamelCase_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_sequence_classification(*lowerCamelCase_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_albert_for_question_answering(*lowerCamelCase_    )




    @slow
    def    lowercase_				(					self    ):





        '''simple docstring'''

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





@require_tf
class 				__snake_case  (     unittest.TestCase						):


    '''simple docstring'''







    @slow
    def    lowercase_				(					self    ):





        '''simple docstring'''

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

        SCREAMING_SNAKE_CASE__       							= [1, 6, 3_00_00]
        self.assertEqual(output.shape      ,							lowerCamelCase_    )

        SCREAMING_SNAKE_CASE__       							= tf.constant(
            [
                [
                    [4.595668, 0.74462754, -1.818147],
                    [4.5954347, 0.7454184, -1.8188258],
                    [4.5954905, 0.7448235, -1.8182316],
                ]
            ]    )
        tf.debugging.assert_near(output[:, :3, :3]      ,							lowerCamelCase_      ,							atol=1E-4    )








import numpy as np

_A				:							List[Any]							= [
    ["""a""", """b""", """c""", """d""", """e"""],
    ["""f""", """g""", """h""", """i""", """k"""],
    ["""l""", """m""", """n""", """o""", """p"""],
    ["""q""", """r""", """s""", """t""", """u"""],
    ["""v""", """w""", """x""", """y""", """z"""],
]



class 				__snake_case  :


    '''simple docstring'''







    def __init__(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= np.array(A_    )



    def    lowercase_				(					self      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__       							= np.where(letter == self.SQUARE    )
        SCREAMING_SNAKE_CASE__       							= np.concatenate([indexa + 1, indexa + 1]    )
        return indexes



    def    lowercase_				(					self      ,							A_      ,							A_    ):





        '''simple docstring'''

        return self.SQUARE[indexa - 1, indexa - 1]



    def    lowercase_				(					self      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= message.lower()
        SCREAMING_SNAKE_CASE__       							= message.replace('''j'''      ,							'''i'''    )

        SCREAMING_SNAKE_CASE__       							= ''''''
        for letter_index in range(len(A_    )    ):
            if message[letter_index] != " ":
                SCREAMING_SNAKE_CASE__       							= self.letter_to_numbers(message[letter_index]    )
                SCREAMING_SNAKE_CASE__       							= encoded_message + str(numbers[0]    ) + str(numbers[1]    )
            elif message[letter_index] == " ":
                SCREAMING_SNAKE_CASE__       							= encoded_message + ''' '''

        return encoded_message




    def    lowercase_				(					self      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= message.replace(''' '''      ,							'''  '''    )
        SCREAMING_SNAKE_CASE__       							= ''''''
        for numbers_index in range(int(len(A_    ) / 2    )    ):
            if message[numbers_index * 2] != " ":
                SCREAMING_SNAKE_CASE__       							= message[numbers_index * 2]
                SCREAMING_SNAKE_CASE__       							= message[numbers_index * 2 + 1]

                SCREAMING_SNAKE_CASE__       							= self.numbers_to_letter(int(A_    )      ,							int(A_    )    )
                SCREAMING_SNAKE_CASE__       							= decoded_message + letter
            elif message[numbers_index * 2] == " ":
                SCREAMING_SNAKE_CASE__       							= decoded_message + ''' '''

        return decoded_message
