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


    '''simple docstring'''





    def __init__(  self					,       __lowerCamelCase					,       __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.02					,       __lowerCamelCase=3					,       __lowerCamelCase=4					,       __lowerCamelCase=None					,       ):




         '''simple docstring'''



         __A					:					Tuple								=		parent
         __A					:					Optional[Any]								=		13
         __A					:					Union[str, Any]								=		7
         __A					:					Tuple								=		True
         __A					:					Any								=		True
         __A					:					Union[str, Any]								=		True
         __A					:					Optional[Any]								=		True
         __A					:					List[str]								=		99
         __A					:					int								=		16
         __A					:					List[str]								=		32
         __A					:					Union[str, Any]								=		2
         __A					:					Dict								=		4
         __A					:					Dict								=		37
         __A					:					Optional[int]								=		'gelu'
         __A					:					str								=		0.1
         __A					:					str								=		0.1
         __A					:					Optional[Any]								=		512
         __A					:					List[Any]								=		16
         __A					:					List[Any]								=		2
         __A					:					Dict								=		0.02
         __A					:					int								=		3
         __A					:					Any								=		4
         __A					:					List[Any]								=		None
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					List[str]								=		ids_tensor([self.batch_size, self.seq_length]					,       self.vocab_size	)

         __A					:					str								=		None
         if self.use_input_mask:
              __A					:					Optional[int]								=		random_attention_mask([self.batch_size, self.seq_length]	)

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

         __A					:					Dict								=		None
         __A					:					str								=		None
         __A					:					Dict								=		None
         if self.use_labels:
              __A					:					Any								=		ids_tensor([self.batch_size]					,       self.type_sequence_label_size	)
              __A					:					List[Any]								=		ids_tensor([self.batch_size, self.seq_length]					,       self.num_labels	)
              __A					:					Any								=		ids_tensor([self.batch_size]					,       self.num_choices	)

         __A					:					List[str]								=		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					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase	):




         '''simple docstring'''



         __A					:					List[Any]								=		TFAlbertModel(config=__lowerCamelCase	)
         # inputs = {'input_ids': input_ids,
         #           'attention_mask': input_mask,
         #           'token_type_ids': token_type_ids}
         # sequence_output, pooled_output = model(**inputs)
         __A					:					str								=		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
         __A					:					List[str]								=		model(__lowerCamelCase	)

         __A					:					List[Any]								=		[input_ids, input_mask]
         __A					:					Optional[Any]								=		model(__lowerCamelCase	)

         __A					:					Optional[int]								=		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					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase	):




         '''simple docstring'''



         __A					:					Tuple								=		self.num_labels
         __A					:					int								=		TFAlbertForPreTraining(config=__lowerCamelCase	)
         __A					:					Tuple								=		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
         __A					:					Dict								=		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					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase	):




         '''simple docstring'''



         __A					:					Any								=		TFAlbertForMaskedLM(config=__lowerCamelCase	)
         __A					:					Any								=		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
         __A					:					str								=		model(__lowerCamelCase	)
         self.parent.assertEqual(result.logits.shape					,       (self.batch_size, self.seq_length, self.vocab_size)	)
    def 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase	):




         '''simple docstring'''



         __A					:					Optional[int]								=		self.num_labels
         __A					:					Dict								=		TFAlbertForSequenceClassification(config=__lowerCamelCase	)
         __A					:					int								=		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
         __A					:					Optional[Any]								=		model(__lowerCamelCase	)
         self.parent.assertEqual(result.logits.shape					,       (self.batch_size, self.num_labels)	)
    def 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase	):




         '''simple docstring'''



         __A					:					Optional[Any]								=		TFAlbertForQuestionAnswering(config=__lowerCamelCase	)
         __A					:					Union[str, Any]								=		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
         __A					:					Optional[int]								=		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					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase	):




         '''simple docstring'''



         __A					:					List[str]								=		self.num_choices
         __A					:					Any								=		TFAlbertForMultipleChoice(config=__lowerCamelCase	)
         __A					:					Any								=		tf.tile(tf.expand_dims(__lowerCamelCase					,       1	)					,       (1, self.num_choices, 1)	)
         __A					:					str								=		tf.tile(tf.expand_dims(__lowerCamelCase					,       1	)					,       (1, self.num_choices, 1)	)
         __A					:					Any								=		tf.tile(tf.expand_dims(__lowerCamelCase					,       1	)					,       (1, self.num_choices, 1)	)
         __A					:					Optional[Any]								=		{
             'input_ids': multiple_choice_inputs_ids,
             'attention_mask': multiple_choice_input_mask,
             'token_type_ids': multiple_choice_token_type_ids,
         }
         __A					:					List[str]								=		model(__lowerCamelCase	)
         self.parent.assertListEqual(list(result['logits'].shape	)					,       [self.batch_size, self.num_choices]	)
    def 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase	):




         '''simple docstring'''



         __A					:					str								=		self.num_labels
         __A					:					Dict								=		TFAlbertForTokenClassification(config=__lowerCamelCase	)
         __A					:					List[Any]								=		{
             'input_ids': input_ids,
             'attention_mask': input_mask,
             'token_type_ids': token_type_ids,
         }
         __A					:					int								=		model(__lowerCamelCase	)
         self.parent.assertListEqual(list(result['logits'].shape	)					,       [self.batch_size, self.seq_length, self.num_labels]	)

    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Any								=		self.prepare_config_and_inputs()
         (
             (
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		
         )					:					Union[str, Any]								=		config_and_inputs
         __A					:					Any								=		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
         return config, inputs_dict







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


    '''simple docstring'''





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




         '''simple docstring'''



         __A					:					Optional[int]								=		super()._prepare_for_class(__lowerCamelCase					,       __lowerCamelCase					,       return_labels=__lowerCamelCase	)

         if return_labels:
              if model_class in get_values(__lowerCamelCase	):
                   __A					:					Dict								=		tf.zeros(self.model_tester.batch_size					,       dtype=tf.intaa	)

         return inputs_dict
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					List[Any]								=		TFAlbertModelTester(self	)
         __A					:					List[Any]								=		ConfigTester(self					,       config_class=__lowerCamelCase					,       hidden_size=37	)
    def 						_a							(  self	):




         '''simple docstring'''



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




         '''simple docstring'''



         __A					:					Union[str, Any]								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_albert_model(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Optional[Any]								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_albert_for_pretraining(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					int								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_albert_for_masked_lm(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Optional[Any]								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_albert_for_multiple_choice(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Optional[int]								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_albert_for_sequence_classification(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Union[str, Any]								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_albert_for_question_answering(*__lowerCamelCase	)

    @slow
    def 						_a							(  self	):




         '''simple docstring'''



         for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              __A					:					Union[str, Any]								=		TFAlbertModel.from_pretrained(__lowerCamelCase	)
              self.assertIsNotNone(__lowerCamelCase	)







@require_tf
class 							__snake_case(					unittest.TestCase	):


    '''simple docstring'''





    @slow
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					List[Any]								=		TFAlbertForPreTraining.from_pretrained('albert-base-v2'	)
         __A					:					Union[str, Any]								=		tf.constant([[0, 1, 2, 3, 4, 5]]	)
         __A					:					Optional[int]								=		model(__lowerCamelCase	)[0]

         __A					:					List[str]								=		[1, 6, 30000]
         self.assertEqual(output.shape					,       __lowerCamelCase	)

         __A					:					Optional[int]								=		tf.constant(
             [
                 [
                     [4.59_56_68, 0.74_46_27_54, -1.81_81_47],
                     [4.5_95_43_47, 0.7_45_41_84, -1.8_18_82_58],
                     [4.5_95_49_05, 0.7_44_82_35, -1.8_18_23_16],
                 ]
             ]	)
         tf.debugging.assert_near(output[:, :3, :3]					,       __lowerCamelCase					,       atol=1e-4	)




"""simple docstring"""







from typing import Dict, Optional, Tuple

import torch




def 						_lowercase				(  _SCREAMING_SNAKE_CASE				:				torch.Tensor						)    ->     torch.Tensor:







     '''simple docstring'''


     __A					:					List[str]								=		boundaries[1] - boundaries[0]
     __A					:					int								=		boundaries + step / 2
     __A					:					Union[str, Any]								=		torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1						)]  ,       dim=0						)
     return bin_centers




def 						_lowercase				(  _SCREAMING_SNAKE_CASE				:				torch.Tensor  ,       _SCREAMING_SNAKE_CASE				:				torch.Tensor  ,       )    ->     Tuple[torch.Tensor, torch.Tensor]:







     '''simple docstring'''


     __A					:					str								=		_calculate_bin_centers(_SCREAMING_SNAKE_CASE						)
     return (
         torch.sum(aligned_distance_error_probs * bin_centers  ,       dim=-1						),
         bin_centers[-1],
     )




def 						_lowercase				(  _SCREAMING_SNAKE_CASE				:				torch.Tensor  ,       _SCREAMING_SNAKE_CASE				:				int = 31  ,       _SCREAMING_SNAKE_CASE				:				int = 64  ,       **_SCREAMING_SNAKE_CASE				:				Optional[Any]  ,       )    ->     Dict[str, torch.Tensor]:







     '''simple docstring'''


     __A					:					int								=		torch.linspace(0  ,       _SCREAMING_SNAKE_CASE  ,       steps=(no_bins - 1)  ,       device=logits.device						)

     __A					:					Any								=		torch.nn.functional.softmax(_SCREAMING_SNAKE_CASE  ,       dim=-1						)
     __A						,		__A					:					Tuple								=		_calculate_expected_aligned_error(
         alignment_confidence_breaks=_SCREAMING_SNAKE_CASE  ,       aligned_distance_error_probs=_SCREAMING_SNAKE_CASE  ,       )

     return {
         "aligned_confidence_probs": aligned_confidence_probs,
         "predicted_aligned_error": predicted_aligned_error,
         "max_predicted_aligned_error": max_predicted_aligned_error,
     }




def 						_lowercase				(  _SCREAMING_SNAKE_CASE				:				torch.Tensor  ,       _SCREAMING_SNAKE_CASE				:				Optional[torch.Tensor] = None  ,       _SCREAMING_SNAKE_CASE				:				int = 31  ,       _SCREAMING_SNAKE_CASE				:				int = 64  ,       _SCREAMING_SNAKE_CASE				:				float = 1E-8  ,       **_SCREAMING_SNAKE_CASE				:				Union[str, Any]  ,       )    ->     torch.Tensor:







     '''simple docstring'''


     if residue_weights is None:
          __A					:					Optional[int]								=		logits.new_ones(logits.shape[-2]						)

     __A					:					List[str]								=		torch.linspace(0  ,       _SCREAMING_SNAKE_CASE  ,       steps=(no_bins - 1)  ,       device=logits.device						)

     __A					:					Tuple								=		_calculate_bin_centers(_SCREAMING_SNAKE_CASE						)
     torch.sum(_SCREAMING_SNAKE_CASE						)
     __A					:					int								=		logits.shape[-2]
     __A					:					str								=		max(_SCREAMING_SNAKE_CASE  ,       19						)

     __A					:					Tuple								=		1.24 * (clipped_n - 15) ** (1.0 / 3) - 1.8

     __A					:					Tuple								=		torch.nn.functional.softmax(_SCREAMING_SNAKE_CASE  ,       dim=-1						)

     __A					:					List[str]								=		1.0 / (1 + (bin_centers**2) / (da**2))
     __A					:					int								=		torch.sum(probs * tm_per_bin  ,       dim=-1						)

     __A					:					Any								=		residue_weights / (eps + residue_weights.sum())
     __A					:					List[Any]								=		torch.sum(predicted_tm_term * normed_residue_mask  ,       dim=-1						)

     __A					:					int								=		per_alignment * residue_weights

     __A					:					Optional[int]								=		(weighted == torch.max(_SCREAMING_SNAKE_CASE						)).nonzero()[0]
     return per_alignment[tuple(_SCREAMING_SNAKE_CASE						)]



