


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






       def __init__(  self			,       __SCREAMING_SNAKE_CASE			,       __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=512			,       __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			,       ):




              """simple docstring"""




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






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						Optional[int] 						=		ids_tensor([self.batch_size, self.seq_length]			,       self.vocab_size    )

              UpperCamelCase     :						Optional[Any] 						=		None
              if self.use_input_mask:
                     UpperCamelCase     :						Union[str, Any] 						=		random_attention_mask([self.batch_size, self.seq_length]    )

              UpperCamelCase     :						Tuple 						=		None
              if self.use_token_type_ids:
                     UpperCamelCase     :						Union[str, Any] 						=		ids_tensor([self.batch_size, self.seq_length]			,       self.type_vocab_size    )

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

              UpperCamelCase     :						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 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




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

              UpperCamelCase     :						int 						=		[input_ids, input_mask]
              UpperCamelCase     :						int 						=		model(__SCREAMING_SNAKE_CASE    )

              UpperCamelCase     :						Any 						=		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 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




              UpperCamelCase     :						List[str] 						=		self.num_labels
              UpperCamelCase     :						Optional[int] 						=		TFAlbertForPreTraining(config=__SCREAMING_SNAKE_CASE    )
              UpperCamelCase     :						Optional[Any] 						=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
              UpperCamelCase     :						Tuple 						=		model(__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, self.num_labels)    )






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




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






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




              UpperCamelCase     :						Optional[Any] 						=		self.num_labels
              UpperCamelCase     :						Union[str, Any] 						=		TFAlbertForSequenceClassification(config=__SCREAMING_SNAKE_CASE    )
              UpperCamelCase     :						Optional[Any] 						=		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
              UpperCamelCase     :						Optional[int] 						=		model(__SCREAMING_SNAKE_CASE    )
              self.parent.assertEqual(result.logits.shape			,       (self.batch_size, self.num_labels)    )






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




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




              """simple docstring"""




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






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE    ):




              """simple docstring"""




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






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						Dict 						=		self.prepare_config_and_inputs()
              (
                  (
                  UpperCamelCase
              )						,			(
                  UpperCamelCase
              )						,			(
                  UpperCamelCase
              )						,			(
                  UpperCamelCase
              )						,			(
                  UpperCamelCase
              )						,			(
                  UpperCamelCase
              )						,			(
                  UpperCamelCase
              )						,			
              )     :						str 						=		config_and_inputs
              UpperCamelCase     :						List[str] 						=		{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
              return config, inputs_dict




@require_tf
class  UpperCAmelCase_						(				_a,  _a,  unittest.TestCase):
       '''simple docstring'''
       __UpperCamelCase   :  Tuple				=       (
           (
               TFAlbertModel,
               TFAlbertForPreTraining,
               TFAlbertForMaskedLM,
               TFAlbertForSequenceClassification,
               TFAlbertForQuestionAnswering,
               TFAlbertForTokenClassification,
               TFAlbertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       __UpperCamelCase   :  Union[str, Any]				=       (
           {
               "feature-extraction": TFAlbertModel,
               "fill-mask": TFAlbertForMaskedLM,
               "question-answering": TFAlbertForQuestionAnswering,
               "text-classification": TFAlbertForSequenceClassification,
               "token-classification": TFAlbertForTokenClassification,
               "zero-shot": TFAlbertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       __UpperCamelCase   :  str				=       False
       __UpperCamelCase   :  List[Any]				=       False






       def 			_lowercase (  self			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE			,       __SCREAMING_SNAKE_CASE=False    ):




              """simple docstring"""




              UpperCamelCase     :						Optional[Any] 						=		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    ):
                            UpperCamelCase     :						List[Any] 						=		tf.zeros(self.model_tester.batch_size			,       dtype=tf.intaa    )

              return inputs_dict






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						int 						=		TFAlbertModelTester(self    )
              UpperCamelCase     :						Dict 						=		ConfigTester(self			,       config_class=__SCREAMING_SNAKE_CASE			,       hidden_size=37    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              self.config_tester.run_common_tests()






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						List[Any] 						=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_albert_model(*__SCREAMING_SNAKE_CASE    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						str 						=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_albert_for_pretraining(*__SCREAMING_SNAKE_CASE    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						Optional[int] 						=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_albert_for_masked_lm(*__SCREAMING_SNAKE_CASE    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						List[Any] 						=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_albert_for_multiple_choice(*__SCREAMING_SNAKE_CASE    )






       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						Union[str, Any] 						=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_albert_for_sequence_classification(*__SCREAMING_SNAKE_CASE    )






       def 			_lowercase (  self    ):




              """simple docstring"""




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






       @slow
       def 			_lowercase (  self    ):




              """simple docstring"""




              for model_name in TF_ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     UpperCamelCase     :						Dict 						=		TFAlbertModel.from_pretrained(__SCREAMING_SNAKE_CASE    )
                     self.assertIsNotNone(__SCREAMING_SNAKE_CASE    )




@require_tf
class  UpperCAmelCase_						(				unittest.TestCase):
       '''simple docstring'''






       @slow
       def 			_lowercase (  self    ):




              """simple docstring"""




              UpperCamelCase     :						Tuple 						=		TFAlbertForPreTraining.from_pretrained('''albert-base-v2'''    )
              UpperCamelCase     :						Optional[Any] 						=		tf.constant([[0, 1, 2, 3, 4, 5]]    )
              UpperCamelCase     :						int 						=		model(__SCREAMING_SNAKE_CASE    )[0]

              UpperCamelCase     :						List[str] 						=		[1, 6, 30_000]
              self.assertEqual(output.shape			,       __SCREAMING_SNAKE_CASE    )

              UpperCamelCase     :						Optional[Any] 						=		tf.constant(
                  [
                      [
                          [4.595_668, 0.74_462_754, -1.818_147],
                          [4.5_954_347, 0.7_454_184, -1.8_188_258],
                          [4.5_954_905, 0.7_448_235, -1.8_182_316],
                      ]
                  ]    )
              tf.debugging.assert_near(output[:, :3, :3]			,       __SCREAMING_SNAKE_CASE			,       atol=1e-4    )





import sys


def   a						(				SCREAMING_SNAKE_CASE_   :		int				,      SCREAMING_SNAKE_CASE_   :		list[int]							):






       """simple docstring"""





       if not numbers:
              return 0
       if target == 0 and target in numbers:
              return 0
       if not isinstance(SCREAMING_SNAKE_CASE_				,      (list, tuple)							) or not all(
           isinstance(SCREAMING_SNAKE_CASE_				,      SCREAMING_SNAKE_CASE_							) for number in numbers							):
              raise ValueError('''numbers must be an iterable of integers'''							)

       UpperCamelCase     :						Dict 						=		0
       UpperCamelCase     :						Tuple 						=		sys.maxsize

       while right < len(SCREAMING_SNAKE_CASE_							):
              curr_sum += numbers[right]
              while curr_sum >= target and left <= right:
                     UpperCamelCase     :						int 						=		min(SCREAMING_SNAKE_CASE_				,      right - left + 1							)
                     curr_sum -= numbers[left]
                     left += 1
              right += 1

       return 0 if min_len == sys.maxsize else min_len

