






import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     MODEL_FOR_PRETRAINING_MAPPING,
     AlbertForMaskedLM,
     AlbertForMultipleChoice,
     AlbertForPreTraining,
     AlbertForQuestionAnswering,
     AlbertForSequenceClassification,
     AlbertForTokenClassification,
     AlbertModel,
 )
 from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST



class 				_UpperCamelCase      :


      def __init__(						self       ,    __UpperCamelCase       ,    __UpperCamelCase=1_3       ,    __UpperCamelCase=7       ,    __UpperCamelCase=True       ,    __UpperCamelCase=True       ,    __UpperCamelCase=True       ,    __UpperCamelCase=True       ,    __UpperCamelCase=9_9       ,    __UpperCamelCase=1_6       ,    __UpperCamelCase=3_6       ,    __UpperCamelCase=6       ,    __UpperCamelCase=6       ,    __UpperCamelCase=6       ,    __UpperCamelCase=3_7       ,    __UpperCamelCase="gelu"       ,    __UpperCamelCase=0.1       ,    __UpperCamelCase=0.1       ,    __UpperCamelCase=5_1_2       ,    __UpperCamelCase=1_6       ,    __UpperCamelCase=2       ,    __UpperCamelCase=0.0_2       ,    __UpperCamelCase=3       ,    __UpperCamelCase=4       ,    __UpperCamelCase=None       ,    )->						List[Any]:
           __lowerCAmelCase             =       parent
           __lowerCAmelCase             =       batch_size
           __lowerCAmelCase             =       seq_length
           __lowerCAmelCase             =       is_training
           __lowerCAmelCase             =       use_input_mask
           __lowerCAmelCase             =       use_token_type_ids
           __lowerCAmelCase             =       use_labels
           __lowerCAmelCase             =       vocab_size
           __lowerCAmelCase             =       embedding_size
           __lowerCAmelCase             =       hidden_size
           __lowerCAmelCase             =       num_hidden_layers
           __lowerCAmelCase             =       num_hidden_groups
           __lowerCAmelCase             =       num_attention_heads
           __lowerCAmelCase             =       intermediate_size
           __lowerCAmelCase             =       hidden_act
           __lowerCAmelCase             =       hidden_dropout_prob
           __lowerCAmelCase             =       attention_probs_dropout_prob
           __lowerCAmelCase             =       max_position_embeddings
           __lowerCAmelCase             =       type_vocab_size
           __lowerCAmelCase             =       type_sequence_label_size
           __lowerCAmelCase             =       initializer_range
           __lowerCAmelCase             =       num_labels
           __lowerCAmelCase             =       num_choices
           __lowerCAmelCase             =       scope


      def 			__UpperCAmelCase		(						self						)->						Any:
           __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             =       self.get_config()

           return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


      def 			__UpperCAmelCase		(						self						)->						List[str]:
           return AlbertConfig(
               vocab_size=self.vocab_size       ,    hidden_size=self.hidden_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       ,    num_hidden_groups=self.num_hidden_groups       ,    )


      def 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase						)->						Optional[int]:
           __lowerCAmelCase             =       AlbertModel(config=__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.eval()
           __lowerCAmelCase             =       model(__UpperCamelCase       ,    attention_mask=__UpperCamelCase       ,    token_type_ids=__UpperCamelCase						)
           __lowerCAmelCase             =       model(__UpperCamelCase       ,    token_type_ids=__UpperCamelCase						)
           __lowerCAmelCase             =       model(__UpperCamelCase						)
           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 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase						)->						List[Any]:
           __lowerCAmelCase             =       AlbertForPreTraining(config=__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.eval()
           __lowerCAmelCase             =       model(
               __UpperCamelCase       ,    attention_mask=__UpperCamelCase       ,    token_type_ids=__UpperCamelCase       ,    labels=__UpperCamelCase       ,    sentence_order_label=__UpperCamelCase       ,    )
           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, config.num_labels)						)


      def 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase						)->						Tuple:
           __lowerCAmelCase             =       AlbertForMaskedLM(config=__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.eval()
           __lowerCAmelCase             =       model(__UpperCamelCase       ,    attention_mask=__UpperCamelCase       ,    token_type_ids=__UpperCamelCase       ,    labels=__UpperCamelCase						)
           self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.seq_length, self.vocab_size)						)


      def 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase						)->						List[Any]:
           __lowerCAmelCase             =       AlbertForQuestionAnswering(config=__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.eval()
           __lowerCAmelCase             =       model(
               __UpperCamelCase       ,    attention_mask=__UpperCamelCase       ,    token_type_ids=__UpperCamelCase       ,    start_positions=__UpperCamelCase       ,    end_positions=__UpperCamelCase       ,    )
           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 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase						)->						List[Any]:
           __lowerCAmelCase             =       self.num_labels
           __lowerCAmelCase             =       AlbertForSequenceClassification(__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.eval()
           __lowerCAmelCase             =       model(__UpperCamelCase       ,    attention_mask=__UpperCamelCase       ,    token_type_ids=__UpperCamelCase       ,    labels=__UpperCamelCase						)
           self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.num_labels)						)


      def 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase						)->						List[str]:
           __lowerCAmelCase             =       self.num_labels
           __lowerCAmelCase             =       AlbertForTokenClassification(config=__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.eval()
           __lowerCAmelCase             =       model(__UpperCamelCase       ,    attention_mask=__UpperCamelCase       ,    token_type_ids=__UpperCamelCase       ,    labels=__UpperCamelCase						)
           self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.seq_length, self.num_labels)						)


      def 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase						)->						int:
           __lowerCAmelCase             =       self.num_choices
           __lowerCAmelCase             =       AlbertForMultipleChoice(config=__UpperCamelCase						)
           model.to(__UpperCamelCase						)
           model.eval()
           __lowerCAmelCase             =       input_ids.unsqueeze(1						).expand(-1       ,    self.num_choices       ,    -1						).contiguous()
           __lowerCAmelCase             =       token_type_ids.unsqueeze(1						).expand(-1       ,    self.num_choices       ,    -1						).contiguous()
           __lowerCAmelCase             =       input_mask.unsqueeze(1						).expand(-1       ,    self.num_choices       ,    -1						).contiguous()
           __lowerCAmelCase             =       model(
               __UpperCamelCase       ,    attention_mask=__UpperCamelCase       ,    token_type_ids=__UpperCamelCase       ,    labels=__UpperCamelCase       ,    )
           self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.num_choices)						)


      def 			__UpperCAmelCase		(						self						)->						int:
           __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_torch
class 				_UpperCamelCase      (a_				,		a_				,		unittest.TestCase      ):
      snake_case_	=				(
          (
              AlbertModel,
              AlbertForPreTraining,
              AlbertForMaskedLM,
              AlbertForMultipleChoice,
              AlbertForSequenceClassification,
              AlbertForTokenClassification,
              AlbertForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      snake_case_	=				(
          {
              """feature-extraction""": AlbertModel,
              """fill-mask""": AlbertForMaskedLM,
              """question-answering""": AlbertForQuestionAnswering,
              """text-classification""": AlbertForSequenceClassification,
              """token-classification""": AlbertForTokenClassification,
              """zero-shot""": AlbertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      snake_case_	=				True


      def 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase=False						)->						Optional[Any]:
           __lowerCAmelCase             =       super()._prepare_for_class(__UpperCamelCase       ,    __UpperCamelCase       ,    return_labels=__UpperCamelCase						)

           if return_labels:
                if model_class in get_values(__UpperCamelCase						):
                     __lowerCAmelCase             =       torch.zeros(
                         (self.model_tester.batch_size, self.model_tester.seq_length)       ,    dtype=torch.long       ,    device=__UpperCamelCase						)
                     __lowerCAmelCase             =       torch.zeros(
                         self.model_tester.batch_size       ,    dtype=torch.long       ,    device=__UpperCamelCase						)
           return inputs_dict


      def 			__UpperCAmelCase		(						self						)->						Any:
           __lowerCAmelCase             =       AlbertModelTester(self						)
           __lowerCAmelCase             =       ConfigTester(self       ,    config_class=__UpperCamelCase       ,    hidden_size=3_7						)


      def 			__UpperCAmelCase		(						self						)->						str:
           self.config_tester.run_common_tests()


      def 			__UpperCAmelCase		(						self						)->						Optional[int]:
           __lowerCAmelCase             =       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__UpperCamelCase						)


      def 			__UpperCAmelCase		(						self						)->						Dict:
           __lowerCAmelCase             =       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_pretraining(*__UpperCamelCase						)


      def 			__UpperCAmelCase		(						self						)->						int:
           __lowerCAmelCase             =       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*__UpperCamelCase						)


      def 			__UpperCAmelCase		(						self						)->						List[Any]:
           __lowerCAmelCase             =       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*__UpperCamelCase						)


      def 			__UpperCAmelCase		(						self						)->						Dict:
           __lowerCAmelCase             =       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*__UpperCamelCase						)


      def 			__UpperCAmelCase		(						self						)->						Optional[Any]:
           __lowerCAmelCase             =       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*__UpperCamelCase						)


      def 			__UpperCAmelCase		(						self						)->						Tuple:
           __lowerCAmelCase             =       self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                __lowerCAmelCase             =       type
                self.model_tester.create_and_check_model(*__UpperCamelCase						)


      @slow
      def 			__UpperCAmelCase		(						self						)->						int:
           for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                __lowerCAmelCase             =       AlbertModel.from_pretrained(__UpperCamelCase						)
                self.assertIsNotNone(__UpperCamelCase						)



@require_torch
class 				_UpperCamelCase      (unittest.TestCase      ):


      @slow
      def 			__UpperCAmelCase		(						self						)->						List[Any]:
           __lowerCAmelCase             =       AlbertModel.from_pretrained("albert-base-v2"						)
           __lowerCAmelCase             =       torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]						)
           __lowerCAmelCase             =       torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
           with torch.no_grad():
                __lowerCAmelCase             =       model(__UpperCamelCase       ,    attention_mask=__UpperCamelCase						)[0]
           __lowerCAmelCase             =       torch.Size((1, 1_1, 7_6_8)						)
           self.assertEqual(output.shape       ,    __UpperCamelCase						)
           __lowerCAmelCase             =       torch.tensor(
               [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]]						)

           self.assertTrue(torch.allclose(output[:, 1:4, 1:4]       ,    __UpperCamelCase       ,    atol=1e-4						)						)














import warnings
from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType



class 				_UpperCamelCase      (a_      ):
      snake_case_	=				["""image_processor""", """tokenizer"""]
      snake_case_	=				"""FlavaImageProcessor"""
      snake_case_	=				("""BertTokenizer""", """BertTokenizerFast""")


      def __init__(						self       ,    __UpperCamelCase=None       ,    __UpperCamelCase=None       ,    **__UpperCamelCase						)->						Tuple:
           __lowerCAmelCase             =       None
           if "feature_extractor" in kwargs:
                warnings.warn(
                    "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
                    " instead."       ,    __UpperCamelCase       ,    )
                __lowerCAmelCase             =       kwargs.pop("feature_extractor"						)

           __lowerCAmelCase             =       image_processor if image_processor is not None else feature_extractor
           if image_processor is None:
                raise ValueError("You need to specify an `image_processor`."						)
           if tokenizer is None:
                raise ValueError("You need to specify a `tokenizer`."						)

           super().__init__(__UpperCamelCase       ,    __UpperCamelCase						)
           __lowerCAmelCase             =       self.image_processor


      def __call__(						self       ,    __UpperCamelCase = None       ,    __UpperCamelCase = None       ,    __UpperCamelCase = True       ,    __UpperCamelCase = False       ,    __UpperCamelCase = False       ,    __UpperCamelCase = None       ,    __UpperCamelCase = 0       ,    __UpperCamelCase = None       ,    __UpperCamelCase = None       ,    __UpperCamelCase = None       ,    __UpperCamelCase = None       ,    __UpperCamelCase = None       ,    __UpperCamelCase = False       ,    __UpperCamelCase = False       ,    __UpperCamelCase = False       ,    __UpperCamelCase = False       ,    __UpperCamelCase = True       ,    __UpperCamelCase = None       ,    **__UpperCamelCase       ,    )->						int:

           if text is None and images is None:
                raise ValueError("You have to specify either text or images. Both cannot be none."						)

           if text is not None:
                __lowerCAmelCase             =       self.tokenizer(
                    text=__UpperCamelCase       ,    add_special_tokens=__UpperCamelCase       ,    padding=__UpperCamelCase       ,    truncation=__UpperCamelCase       ,    max_length=__UpperCamelCase       ,    stride=__UpperCamelCase       ,    pad_to_multiple_of=__UpperCamelCase       ,    return_token_type_ids=__UpperCamelCase       ,    return_attention_mask=__UpperCamelCase       ,    return_overflowing_tokens=__UpperCamelCase       ,    return_special_tokens_mask=__UpperCamelCase       ,    return_offsets_mapping=__UpperCamelCase       ,    return_length=__UpperCamelCase       ,    verbose=__UpperCamelCase       ,    return_tensors=__UpperCamelCase       ,    **__UpperCamelCase       ,    )
           if images is not None:
                __lowerCAmelCase             =       self.image_processor(
                    __UpperCamelCase       ,    return_image_mask=__UpperCamelCase       ,    return_codebook_pixels=__UpperCamelCase       ,    return_tensors=__UpperCamelCase       ,    **__UpperCamelCase       ,    )

           if text is not None and images is not None:
                encoding.update(__UpperCamelCase						)
                return encoding
           elif text is not None:
                return encoding
           else:
                return BatchEncoding(data=dict(**__UpperCamelCase						)       ,    tensor_type=__UpperCamelCase						)


      def 			__UpperCAmelCase		(						self       ,    *__UpperCamelCase       ,    **__UpperCamelCase						)->						int:
           return self.tokenizer.batch_decode(*__UpperCamelCase       ,    **__UpperCamelCase						)


      def 			__UpperCAmelCase		(						self       ,    *__UpperCamelCase       ,    **__UpperCamelCase						)->						Optional[int]:
           return self.tokenizer.decode(*__UpperCamelCase       ,    **__UpperCamelCase						)


      @property
      def 			__UpperCAmelCase		(						self						)->						Any:
           __lowerCAmelCase             =       self.tokenizer.model_input_names
           __lowerCAmelCase             =       self.image_processor.model_input_names
           return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names						)						)


      @property
      def 			__UpperCAmelCase		(						self						)->						Dict:
           warnings.warn(
               "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."       ,    __UpperCamelCase       ,    )
           return self.image_processor_class


      @property
      def 			__UpperCAmelCase		(						self						)->						Union[str, Any]:
           warnings.warn(
               "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."       ,    __UpperCamelCase       ,    )
           return self.image_processor






