"""simple docstring"""






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

      def __init__(		self	, __lowerCAmelCase	, __lowerCAmelCase=13	, __lowerCAmelCase=7	, __lowerCAmelCase=True	, __lowerCAmelCase=True	, __lowerCAmelCase=True	, __lowerCAmelCase=True	, __lowerCAmelCase=99	, __lowerCAmelCase=16	, __lowerCAmelCase=36	, __lowerCAmelCase=6	, __lowerCAmelCase=6	, __lowerCAmelCase=6	, __lowerCAmelCase=37	, __lowerCAmelCase="gelu"	, __lowerCAmelCase=0.1	, __lowerCAmelCase=0.1	, __lowerCAmelCase=512	, __lowerCAmelCase=16	, __lowerCAmelCase=2	, __lowerCAmelCase=0.0_2	, __lowerCAmelCase=3	, __lowerCAmelCase=4	, __lowerCAmelCase=None	, ):







            """simple docstring"""



            lowercase														=			parent
            lowercase														=			batch_size
            lowercase														=			seq_length
            lowercase														=			is_training
            lowercase														=			use_input_mask
            lowercase														=			use_token_type_ids
            lowercase														=			use_labels
            lowercase														=			vocab_size
            lowercase														=			embedding_size
            lowercase														=			hidden_size
            lowercase														=			num_hidden_layers
            lowercase														=			num_hidden_groups
            lowercase														=			num_attention_heads
            lowercase														=			intermediate_size
            lowercase														=			hidden_act
            lowercase														=			hidden_dropout_prob
            lowercase														=			attention_probs_dropout_prob
            lowercase														=			max_position_embeddings
            lowercase														=			type_vocab_size
            lowercase														=			type_sequence_label_size
            lowercase														=			initializer_range
            lowercase														=			num_labels
            lowercase														=			num_choices
            lowercase														=			scope

      def        A__ (		self		):







            """simple docstring"""



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

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

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

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

            lowercase														=			self.get_config()

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

      def        A__ (		self		):







            """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        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			AlbertModel(config=__A		)
            model.to(__A		)
            model.eval()
            lowercase														=			model(__A	, attention_mask=__A	, token_type_ids=__A		)
            lowercase														=			model(__A	, token_type_ids=__A		)
            lowercase														=			model(__A		)
            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"""



            lowercase														=			AlbertForPreTraining(config=__A		)
            model.to(__A		)
            model.eval()
            lowercase														=			model(
                __A	, attention_mask=__A	, token_type_ids=__A	, labels=__A	, sentence_order_label=__A	, )
            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        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			AlbertForMaskedLM(config=__A		)
            model.to(__A		)
            model.eval()
            lowercase														=			model(__A	, attention_mask=__A	, token_type_ids=__A	, labels=__A		)
            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"""



            lowercase														=			AlbertForQuestionAnswering(config=__A		)
            model.to(__A		)
            model.eval()
            lowercase														=			model(
                __A	, attention_mask=__A	, token_type_ids=__A	, start_positions=__A	, end_positions=__A	, )
            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"""



            lowercase														=			self.num_labels
            lowercase														=			AlbertForSequenceClassification(__A		)
            model.to(__A		)
            model.eval()
            lowercase														=			model(__A	, attention_mask=__A	, token_type_ids=__A	, labels=__A		)
            self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.num_labels)		)

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			self.num_labels
            lowercase														=			AlbertForTokenClassification(config=__A		)
            model.to(__A		)
            model.eval()
            lowercase														=			model(__A	, attention_mask=__A	, token_type_ids=__A	, labels=__A		)
            self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.seq_length, self.num_labels)		)

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			self.num_choices
            lowercase														=			AlbertForMultipleChoice(config=__A		)
            model.to(__A		)
            model.eval()
            lowercase														=			input_ids.unsqueeze(1		).expand(-1	, self.num_choices	, -1		).contiguous()
            lowercase														=			token_type_ids.unsqueeze(1		).expand(-1	, self.num_choices	, -1		).contiguous()
            lowercase														=			input_mask.unsqueeze(1		).expand(-1	, self.num_choices	, -1		).contiguous()
            lowercase														=			model(
                __A	, attention_mask=__A	, token_type_ids=__A	, labels=__A	, )
            self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.num_choices)		)







      def        A__ (		self		):







            """simple docstring"""



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






@require_torch
class 				_A		( __lowercase						,					__lowercase						,					unittest.TestCase			):
      snake_case__    :     Dict													=			(
          (
              AlbertModel,
              AlbertForPreTraining,
              AlbertForMaskedLM,
              AlbertForMultipleChoice,
              AlbertForSequenceClassification,
              AlbertForTokenClassification,
              AlbertForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      snake_case__    :     Union[str, Any]													=			(
          {
              '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__    :     int													=			True

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase	, __lowerCAmelCase=False		):







            """simple docstring"""



            lowercase														=			super()._prepare_for_class(__A	, __A	, return_labels=__A		)

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

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			AlbertModelTester(self		)
            lowercase														=			ConfigTester(self	, config_class=__A	, hidden_size=37		)

      def        A__ (		self		):







            """simple docstring"""



            self.config_tester.run_common_tests()

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__A		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_pretraining(*__A		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_lm(*__A		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_multiple_choice(*__A		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*__A		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*__A		)

      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			self.model_tester.prepare_config_and_inputs()
            for type in ["absolute", "relative_key", "relative_key_query"]:
                  lowercase														=			type
                  self.model_tester.create_and_check_model(*__A		)







      @slow
      def        A__ (		self		):







            """simple docstring"""



            for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  lowercase														=			AlbertModel.from_pretrained(__A		)
                  self.assertIsNotNone(__A		)






@require_torch
class 				_A		( unittest.TestCase			):

      @slow
      def        A__ (		self		):







            """simple docstring"""



            lowercase														=			AlbertModel.from_pretrained("""albert-base-v2"""		)
            lowercase														=			torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]		)
            lowercase														=			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
            with torch.no_grad():
                  lowercase														=			model(__A	, attention_mask=__A		)[0]
            lowercase														=			torch.Size((1, 11, 768)		)
            self.assertEqual(output.shape	, __A		)
            lowercase														=			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]	, __A	, atol=1E-4		)		)




"""simple docstring"""






import json
from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_bart import BartTokenizer


__lowerCAmelCase					: Tuple        =logging.get_logger(__name__)


__lowerCAmelCase					: str        ={"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

# See all BART models at https://huggingface.co/models?filter=bart
__lowerCAmelCase					: Optional[int]        ={
    """vocab_file""": {
        """facebook/bart-base""": """https://huggingface.co/facebook/bart-base/resolve/main/vocab.json""",
        """facebook/bart-large""": """https://huggingface.co/facebook/bart-large/resolve/main/vocab.json""",
        """facebook/bart-large-mnli""": """https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json""",
        """facebook/bart-large-cnn""": """https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json""",
        """facebook/bart-large-xsum""": """https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json""",
        """yjernite/bart_eli5""": """https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """facebook/bart-base""": """https://huggingface.co/facebook/bart-base/resolve/main/merges.txt""",
        """facebook/bart-large""": """https://huggingface.co/facebook/bart-large/resolve/main/merges.txt""",
        """facebook/bart-large-mnli""": """https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt""",
        """facebook/bart-large-cnn""": """https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt""",
        """facebook/bart-large-xsum""": """https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt""",
        """yjernite/bart_eli5""": """https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt""",
    },
    """tokenizer_file""": {
        """facebook/bart-base""": """https://huggingface.co/facebook/bart-base/resolve/main/tokenizer.json""",
        """facebook/bart-large""": """https://huggingface.co/facebook/bart-large/resolve/main/tokenizer.json""",
        """facebook/bart-large-mnli""": """https://huggingface.co/facebook/bart-large-mnli/resolve/main/tokenizer.json""",
        """facebook/bart-large-cnn""": """https://huggingface.co/facebook/bart-large-cnn/resolve/main/tokenizer.json""",
        """facebook/bart-large-xsum""": """https://huggingface.co/facebook/bart-large-xsum/resolve/main/tokenizer.json""",
        """yjernite/bart_eli5""": """https://huggingface.co/yjernite/bart_eli5/resolve/main/tokenizer.json""",
    },
}

__lowerCAmelCase					: Optional[int]        ={
    """facebook/bart-base""": 1_0_2_4,
    """facebook/bart-large""": 1_0_2_4,
    """facebook/bart-large-mnli""": 1_0_2_4,
    """facebook/bart-large-cnn""": 1_0_2_4,
    """facebook/bart-large-xsum""": 1_0_2_4,
    """yjernite/bart_eli5""": 1_0_2_4,
}





class 				_A		( lowerCAmelCase			):
      snake_case__    :     Optional[int]													=			VOCAB_FILES_NAMES
      snake_case__    :     Tuple													=			PRETRAINED_VOCAB_FILES_MAP
      snake_case__    :     Tuple													=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      snake_case__    :     Optional[Any]													=			['input_ids', 'attention_mask']
      snake_case__    :     Dict													=			BartTokenizer

      def __init__(		self	, __lowerCAmelCase=None	, __lowerCAmelCase=None	, __lowerCAmelCase=None	, __lowerCAmelCase="replace"	, __lowerCAmelCase="<s>"	, __lowerCAmelCase="</s>"	, __lowerCAmelCase="</s>"	, __lowerCAmelCase="<s>"	, __lowerCAmelCase="<unk>"	, __lowerCAmelCase="<pad>"	, __lowerCAmelCase="<mask>"	, __lowerCAmelCase=False	, __lowerCAmelCase=True	, **__lowerCAmelCase	, ):







            """simple docstring"""



            super().__init__(
                __lowerCAmelCase	, __lowerCAmelCase	, tokenizer_file=__lowerCAmelCase	, errors=__lowerCAmelCase	, bos_token=__lowerCAmelCase	, eos_token=__lowerCAmelCase	, sep_token=__lowerCAmelCase	, cls_token=__lowerCAmelCase	, unk_token=__lowerCAmelCase	, pad_token=__lowerCAmelCase	, mask_token=__lowerCAmelCase	, add_prefix_space=__lowerCAmelCase	, trim_offsets=__lowerCAmelCase	, **__lowerCAmelCase	, )

            lowercase														=			json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()		)
            if pre_tok_state.get("""add_prefix_space"""	, __lowerCAmelCase		) != add_prefix_space:
                  lowercase														=			getattr(__lowerCAmelCase	, pre_tok_state.pop("""type"""		)		)
                  lowercase														=			add_prefix_space
                  lowercase														=			pre_tok_class(**__lowerCAmelCase		)

            lowercase														=			add_prefix_space

            # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__`
            lowercase														=			"""post_processor"""
            lowercase														=			getattr(self.backend_tokenizer	, __lowerCAmelCase	, __lowerCAmelCase		)
            if tokenizer_component_instance:
                  lowercase														=			json.loads(tokenizer_component_instance.__getstate__()		)

                  # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
                  if "sep" in state:
                        lowercase														=			tuple(state["""sep"""]		)
                  if "cls" in state:
                        lowercase														=			tuple(state["""cls"""]		)

                  lowercase														=			False

                  if state.get("""add_prefix_space"""	, __lowerCAmelCase		) != add_prefix_space:
                        lowercase														=			add_prefix_space
                        lowercase														=			True

                  if state.get("""trim_offsets"""	, __lowerCAmelCase		) != trim_offsets:
                        lowercase														=			trim_offsets
                        lowercase														=			True

                  if changes_to_apply:
                        lowercase														=			getattr(__lowerCAmelCase	, state.pop("""type"""		)		)
                        lowercase														=			component_class(**__lowerCAmelCase		)
                        setattr(self.backend_tokenizer	, __lowerCAmelCase	, __lowerCAmelCase		)

      @property
      def        A__ (		self		):







            """simple docstring"""



            if self._mask_token is None:
                  if self.verbose:
                        logger.error("""Using mask_token, but it is not set yet."""		)
                  return None
            return str(self._mask_token		)

      @mask_token.setter
      def        A__ (		self	, __lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			AddedToken(__lowerCAmelCase	, lstrip=__lowerCAmelCase	, rstrip=__lowerCAmelCase		) if isinstance(__lowerCAmelCase	, __lowerCAmelCase		) else value
            lowercase														=			value

      def        A__ (		self	, *__lowerCAmelCase	, **__lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			kwargs.get("""is_split_into_words"""	, __lowerCAmelCase		)

            if is_split_into_words and not self.add_prefix_space:
                  raise ValueError(
                      f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
                      """to use it with pretokenized inputs."""		)

            return super()._batch_encode_plus(*__lowerCAmelCase	, **__lowerCAmelCase		)

      def        A__ (		self	, *__lowerCAmelCase	, **__lowerCAmelCase		):







            """simple docstring"""



            lowercase														=			kwargs.get("""is_split_into_words"""	, __lowerCAmelCase		)

            if is_split_into_words and not self.add_prefix_space:
                  raise ValueError(
                      f'You need to instantiate {self.__class__.__name__} with add_prefix_space=True '
                      """to use it with pretokenized inputs."""		)

            return super()._encode_plus(*__lowerCAmelCase	, **__lowerCAmelCase		)

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase = None		):







            """simple docstring"""



            lowercase														=			self._tokenizer.model.save(__lowerCAmelCase	, name=__lowerCAmelCase		)
            return tuple(__lowerCAmelCase		)

      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase=None		):







            """simple docstring"""



            lowercase														=			[self.bos_token_id] + token_ids_a + [self.eos_token_id]
            if token_ids_a is None:
                  return output

            return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]







      def        A__ (		self	, __lowerCAmelCase	, __lowerCAmelCase = None		):







            """simple docstring"""



            lowercase														=			[self.sep_token_id]
            lowercase														=			[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 + sep + token_ids_a + sep		) * [0]



