





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


  '''simple docstring'''







  def __init__(      self      :					List[Any]      ,				UpperCamelCase      :					str      ,				UpperCamelCase      :					Any=13      ,				UpperCamelCase      :					int=7      ,				UpperCamelCase      :					List[str]=True      ,				UpperCamelCase      :					str=True      ,				UpperCamelCase      :					int=True      ,				UpperCamelCase      :					Any=True      ,				UpperCamelCase      :					Optional[int]=99      ,				UpperCamelCase      :					List[str]=16      ,				UpperCamelCase      :					Any=36      ,				UpperCamelCase      :					int=6      ,				UpperCamelCase      :					Optional[Any]=6      ,				UpperCamelCase      :					Union[str, Any]=6      ,				UpperCamelCase      :					int=37      ,				UpperCamelCase      :					Any="gelu"      ,				UpperCamelCase      :					str=0.1      ,				UpperCamelCase      :					int=0.1      ,				UpperCamelCase      :					int=5_12      ,				UpperCamelCase      :					str=16      ,				UpperCamelCase      :					Dict=2      ,				UpperCamelCase      :					Union[str, Any]=0.02      ,				UpperCamelCase      :					Any=3      ,				UpperCamelCase      :					Union[str, Any]=4      ,				UpperCamelCase      :					Any=None      ,				):


    '''simple docstring'''





    _snake_case   :				Optional[Any]														=	parent
    _snake_case   :				Dict														=	batch_size
    _snake_case   :				List[Any]														=	seq_length
    _snake_case   :				Optional[int]														=	is_training
    _snake_case   :				Optional[Any]														=	use_input_mask
    _snake_case   :				Any														=	use_token_type_ids
    _snake_case   :				List[str]														=	use_labels
    _snake_case   :				str														=	vocab_size
    _snake_case   :				Dict														=	embedding_size
    _snake_case   :				List[Any]														=	hidden_size
    _snake_case   :				str														=	num_hidden_layers
    _snake_case   :				Any														=	num_hidden_groups
    _snake_case   :				Tuple														=	num_attention_heads
    _snake_case   :				Tuple														=	intermediate_size
    _snake_case   :				Dict														=	hidden_act
    _snake_case   :				str														=	hidden_dropout_prob
    _snake_case   :				Any														=	attention_probs_dropout_prob
    _snake_case   :				List[Any]														=	max_position_embeddings
    _snake_case   :				Optional[Any]														=	type_vocab_size
    _snake_case   :				Tuple														=	type_sequence_label_size
    _snake_case   :				Optional[Any]														=	initializer_range
    _snake_case   :				Any														=	num_labels
    _snake_case   :				str														=	num_choices
    _snake_case   :				Union[str, Any]														=	scope







  def UpperCamelCase_ (      self      :					int							):


    '''simple docstring'''





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

    _snake_case   :				Tuple														=	None
    if self.use_input_mask:
      _snake_case   :				List[Any]														=	random_attention_mask([self.batch_size, self.seq_length]							)

    _snake_case   :				Any														=	None
    if self.use_token_type_ids:
      _snake_case   :				List[str]														=	ids_tensor([self.batch_size, self.seq_length]      ,				self.type_vocab_size							)

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

    _snake_case   :				str														=	self.get_config()

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







  def UpperCamelCase_ (      self      :					Tuple							):


    '''simple docstring'''





    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      :					List[str]      ,				UpperCamelCase      :					Tuple      ,				UpperCamelCase      :					List[Any]      ,				UpperCamelCase      :					Tuple      ,				UpperCamelCase      :					Any      ,				UpperCamelCase      :					Optional[Any]      ,				UpperCamelCase      :					Any      ,				UpperCamelCase      :					Optional[int]							):


    '''simple docstring'''





    _snake_case   :				List[str]														=	AlbertModel(config=UpperCamelCase							)
    model.to(UpperCamelCase							)
    model.eval()
    _snake_case   :				Optional[Any]														=	model(UpperCamelCase      ,				attention_mask=UpperCamelCase      ,				token_type_ids=UpperCamelCase							)
    _snake_case   :				Union[str, Any]														=	model(UpperCamelCase      ,				token_type_ids=UpperCamelCase							)
    _snake_case   :				Union[str, Any]														=	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      :					List[Any]      ,				UpperCamelCase      :					Optional[Any]      ,				UpperCamelCase      :					Tuple      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					List[Any]      ,				UpperCamelCase      :					Tuple      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					Optional[int]							):


    '''simple docstring'''





    _snake_case   :				Union[str, Any]														=	AlbertForPreTraining(config=UpperCamelCase							)
    model.to(UpperCamelCase							)
    model.eval()
    _snake_case   :				int														=	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      :					Optional[Any]      ,				UpperCamelCase      :					Tuple      ,				UpperCamelCase      :					Optional[int]      ,				UpperCamelCase      :					List[Any]      ,				UpperCamelCase      :					Any      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					Any      ,				UpperCamelCase      :					Optional[Any]							):


    '''simple docstring'''





    _snake_case   :				List[str]														=	AlbertForMaskedLM(config=UpperCamelCase							)
    model.to(UpperCamelCase							)
    model.eval()
    _snake_case   :				Union[str, Any]														=	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      :					str      ,				UpperCamelCase      :					Optional[int]      ,				UpperCamelCase      :					Union[str, Any]      ,				UpperCamelCase      :					Any      ,				UpperCamelCase      :					List[Any]      ,				UpperCamelCase      :					Dict      ,				UpperCamelCase      :					Optional[int]      ,				UpperCamelCase      :					str							):


    '''simple docstring'''





    _snake_case   :				List[str]														=	AlbertForQuestionAnswering(config=UpperCamelCase							)
    model.to(UpperCamelCase							)
    model.eval()
    _snake_case   :				Tuple														=	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      :					Optional[Any]      ,				UpperCamelCase      :					Union[str, Any]      ,				UpperCamelCase      :					Tuple      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					str      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					int      ,				UpperCamelCase      :					Optional[Any]							):


    '''simple docstring'''





    _snake_case   :				Any														=	self.num_labels
    _snake_case   :				Optional[Any]														=	AlbertForSequenceClassification(UpperCamelCase							)
    model.to(UpperCamelCase							)
    model.eval()
    _snake_case   :				int														=	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      :					Any      ,				UpperCamelCase      :					Optional[Any]      ,				UpperCamelCase      :					Union[str, Any]      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					Tuple      ,				UpperCamelCase      :					str      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					Any							):


    '''simple docstring'''





    _snake_case   :				Dict														=	self.num_labels
    _snake_case   :				Optional[Any]														=	AlbertForTokenClassification(config=UpperCamelCase							)
    model.to(UpperCamelCase							)
    model.eval()
    _snake_case   :				Tuple														=	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      :					Tuple      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					List[Any]      ,				UpperCamelCase      :					str      ,				UpperCamelCase      :					List[Any]      ,				UpperCamelCase      :					Tuple      ,				UpperCamelCase      :					Tuple      ,				UpperCamelCase      :					Dict							):


    '''simple docstring'''





    _snake_case   :				Optional[Any]														=	self.num_choices
    _snake_case   :				Dict														=	AlbertForMultipleChoice(config=UpperCamelCase							)
    model.to(UpperCamelCase							)
    model.eval()
    _snake_case   :				Optional[Any]														=	input_ids.unsqueeze(1							).expand(-1      ,				self.num_choices      ,				-1							).contiguous()
    _snake_case   :				List[Any]														=	token_type_ids.unsqueeze(1							).expand(-1      ,				self.num_choices      ,				-1							).contiguous()
    _snake_case   :				List[Any]														=	input_mask.unsqueeze(1							).expand(-1      ,				self.num_choices      ,				-1							).contiguous()
    _snake_case   :				Optional[int]														=	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      :					Dict							):


    '''simple docstring'''





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




@require_torch
class        _lowerCAmelCase    (			UpperCAmelCase_							,    UpperCAmelCase_							,    unittest.TestCase							):


  '''simple docstring'''
  a_							:       Union[str, Any]      					=(
      (
          AlbertModel,
          AlbertForPreTraining,
          AlbertForMaskedLM,
          AlbertForMultipleChoice,
          AlbertForSequenceClassification,
          AlbertForTokenClassification,
          AlbertForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  a_							:       Dict      					=(
      {
          """feature-extraction""": AlbertModel,
          """fill-mask""": AlbertForMaskedLM,
          """question-answering""": AlbertForQuestionAnswering,
          """text-classification""": AlbertForSequenceClassification,
          """token-classification""": AlbertForTokenClassification,
          """zero-shot""": AlbertForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  a_							:       List[str]      					=True







  def UpperCamelCase_ (      self      :					Optional[int]      ,				UpperCamelCase      :					List[Any]      ,				UpperCamelCase      :					int      ,				UpperCamelCase      :					Tuple=False							):


    '''simple docstring'''





    _snake_case   :				int														=	super()._prepare_for_class(UpperCamelCase      ,				UpperCamelCase      ,				return_labels=UpperCamelCase							)

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







  def UpperCamelCase_ (      self      :					List[Any]							):


    '''simple docstring'''





    _snake_case   :				str														=	AlbertModelTester(self							)
    _snake_case   :				Optional[int]														=	ConfigTester(self      ,				config_class=UpperCamelCase      ,				hidden_size=37							)







  def UpperCamelCase_ (      self      :					Any							):


    '''simple docstring'''





    self.config_tester.run_common_tests()







  def UpperCamelCase_ (      self      :					Union[str, Any]							):


    '''simple docstring'''





    _snake_case   :				int														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*UpperCamelCase							)







  def UpperCamelCase_ (      self      :					int							):


    '''simple docstring'''





    _snake_case   :				List[str]														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_pretraining(*UpperCamelCase							)







  def UpperCamelCase_ (      self      :					int							):


    '''simple docstring'''





    _snake_case   :				List[str]														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase							)







  def UpperCamelCase_ (      self      :					Any							):


    '''simple docstring'''





    _snake_case   :				Any														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase							)







  def UpperCamelCase_ (      self      :					List[str]							):


    '''simple docstring'''





    _snake_case   :				Optional[int]														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*UpperCamelCase							)







  def UpperCamelCase_ (      self      :					List[Any]							):


    '''simple docstring'''





    _snake_case   :				Optional[Any]														=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase							)







  def UpperCamelCase_ (      self      :					List[Any]							):


    '''simple docstring'''





    _snake_case   :				Any														=	self.model_tester.prepare_config_and_inputs()
    for type in ["absolute", "relative_key", "relative_key_query"]:
      _snake_case   :				int														=	type
      self.model_tester.create_and_check_model(*UpperCamelCase							)







  @slow
  def UpperCamelCase_ (      self      :					str							):


    '''simple docstring'''





    for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      _snake_case   :				int														=	AlbertModel.from_pretrained(UpperCamelCase							)
      self.assertIsNotNone(UpperCamelCase							)




@require_torch
class        _lowerCAmelCase    (			unittest.TestCase							):


  '''simple docstring'''







  @slow
  def UpperCamelCase_ (      self      :					Optional[int]							):


    '''simple docstring'''





    _snake_case   :				Optional[Any]														=	AlbertModel.from_pretrained('albert-base-v2'							)
    _snake_case   :				Optional[int]														=	torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]							)
    _snake_case   :				Dict														=	torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
    with torch.no_grad():
      _snake_case   :				Optional[Any]														=	model(UpperCamelCase      ,				attention_mask=UpperCamelCase							)[0]
    _snake_case   :				Union[str, Any]														=	torch.Size((1, 11, 7_68)							)
    self.assertEqual(output.shape      ,				UpperCamelCase							)
    _snake_case   :				List[str]														=	torch.tensor(
        [[[-0.65_13, 1.50_35, -0.27_66], [-0.65_15, 1.50_46, -0.27_80], [-0.65_12, 1.50_49, -0.27_84]]]							)

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











import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_lxmert import LxmertTokenizer


lowerCAmelCase_						   =    {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

lowerCAmelCase_						   =    {
    """vocab_file""": {
        """unc-nlp/lxmert-base-uncased""": """https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/vocab.txt""",
    },
    """tokenizer_file""": {
        """unc-nlp/lxmert-base-uncased""": (
            """https://huggingface.co/unc-nlp/lxmert-base-uncased/resolve/main/tokenizer.json"""
        ),
    },
}

lowerCAmelCase_						   =    {
    """unc-nlp/lxmert-base-uncased""": 512,
}

lowerCAmelCase_						   =    {
    """unc-nlp/lxmert-base-uncased""": {"""do_lower_case""": True},
}



class        _lowerCAmelCase    (			UpperCAmelCase_							):


  '''simple docstring'''
  a_							:       List[Any]      					=VOCAB_FILES_NAMES
  a_							:       Tuple      					=PRETRAINED_VOCAB_FILES_MAP
  a_							:       Optional[Any]      					=PRETRAINED_INIT_CONFIGURATION
  a_							:       Any      					=PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  a_							:       Any      					=LxmertTokenizer







  def __init__(      self      :					Any      ,				UpperCamelCase      :					Union[str, Any]=None      ,				UpperCamelCase      :					Dict=None      ,				UpperCamelCase      :					List[str]=True      ,				UpperCamelCase      :					List[str]="[UNK]"      ,				UpperCamelCase      :					List[Any]="[SEP]"      ,				UpperCamelCase      :					List[Any]="[PAD]"      ,				UpperCamelCase      :					Optional[Any]="[CLS]"      ,				UpperCamelCase      :					Optional[int]="[MASK]"      ,				UpperCamelCase      :					Optional[int]=True      ,				UpperCamelCase      :					str=None      ,				**UpperCamelCase      :					List[str]      ,				):


    '''simple docstring'''





    super().__init__(
        UpperCamelCase      ,				tokenizer_file=UpperCamelCase      ,				do_lower_case=UpperCamelCase      ,				unk_token=UpperCamelCase      ,				sep_token=UpperCamelCase      ,				pad_token=UpperCamelCase      ,				cls_token=UpperCamelCase      ,				mask_token=UpperCamelCase      ,				tokenize_chinese_chars=UpperCamelCase      ,				strip_accents=UpperCamelCase      ,				**UpperCamelCase      ,				)

    _snake_case   :				Tuple														=	json.loads(self.backend_tokenizer.normalizer.__getstate__()							)
    if (
        normalizer_state.get('lowercase'      ,				UpperCamelCase							) != do_lower_case
        or normalizer_state.get('strip_accents'      ,				UpperCamelCase							) != strip_accents
        or normalizer_state.get('handle_chinese_chars'      ,				UpperCamelCase							) != tokenize_chinese_chars
    ):
      _snake_case   :				List[Any]														=	getattr(UpperCamelCase      ,				normalizer_state.pop('type'							)							)
      _snake_case   :				Optional[int]														=	do_lower_case
      _snake_case   :				Dict														=	strip_accents
      _snake_case   :				Optional[int]														=	tokenize_chinese_chars
      _snake_case   :				Optional[Any]														=	normalizer_class(**UpperCamelCase							)

    _snake_case   :				int														=	do_lower_case







  def UpperCamelCase_ (      self      :					int      ,				UpperCamelCase      :					List[str]      ,				UpperCamelCase      :					str=None							):


    '''simple docstring'''





    _snake_case   :				List[Any]														=	[self.cls_token_id] + token_ids_a + [self.sep_token_id]

    if token_ids_a:
      output += token_ids_a + [self.sep_token_id]

    return output







  def UpperCamelCase_ (      self      :					List[str]      ,				UpperCamelCase      :					List[int]      ,				UpperCamelCase      :					Optional[List[int]] = None							):


    '''simple docstring'''





    _snake_case   :				Tuple														=	[self.sep_token_id]
    _snake_case   :				List[str]														=	[self.cls_token_id]
    if token_ids_a is None:
      return len(cls + token_ids_a + sep							) * [0]
    return len(cls + token_ids_a + sep							) * [0] + len(token_ids_a + sep							) * [1]







  def UpperCamelCase_ (      self      :					int      ,				UpperCamelCase      :					str      ,				UpperCamelCase      :					Optional[str] = None							):


    '''simple docstring'''





    _snake_case   :				int														=	self._tokenizer.model.save(UpperCamelCase      ,				name=UpperCamelCase							)
    return tuple(UpperCamelCase							)




