
"""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	,    _a	,    _a=13	,    _a=7	,    _a=True	,    _a=True	,    _a=True	,    _a=True	,    _a=99	,    _a=16	,    _a=36	,    _a=6	,    _a=6	,    _a=6	,    _a=37	,    _a="gelu"	,    _a=0.1	,    _a=0.1	,    _a=512	,    _a=16	,    _a=2	,    _a=0.0_2	,    _a=3	,    _a=4	,    _a=None	,    ):
          lowercase    :				Optional[Any]       		=							parent
          lowercase    :				Any       		=							batch_size
          lowercase    :				List[str]       		=							seq_length
          lowercase    :				Tuple       		=							is_training
          lowercase    :				Any       		=							use_input_mask
          lowercase    :				Dict       		=							use_token_type_ids
          lowercase    :				str       		=							use_labels
          lowercase    :				Optional[Any]       		=							vocab_size
          lowercase    :				Any       		=							embedding_size
          lowercase    :				Union[str, Any]       		=							hidden_size
          lowercase    :				str       		=							num_hidden_layers
          lowercase    :				Tuple       		=							num_hidden_groups
          lowercase    :				Union[str, Any]       		=							num_attention_heads
          lowercase    :				Dict       		=							intermediate_size
          lowercase    :				Optional[Any]       		=							hidden_act
          lowercase    :				Optional[int]       		=							hidden_dropout_prob
          lowercase    :				Optional[Any]       		=							attention_probs_dropout_prob
          lowercase    :				Tuple       		=							max_position_embeddings
          lowercase    :				Optional[Any]       		=							type_vocab_size
          lowercase    :				Optional[Any]       		=							type_sequence_label_size
          lowercase    :				Tuple       		=							initializer_range
          lowercase    :				List[str]       		=							num_labels
          lowercase    :				str       		=							num_choices
          lowercase    :				List[Any]       		=							scope
   def 			__magic_name__			(			self				):
          lowercase    :				str       		=							ids_tensor([self.batch_size, self.seq_length]	,    self.vocab_size				)

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

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

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

          lowercase    :				List[str]       		=							self.get_config()

          return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
   def 			__magic_name__			(			self				):
          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 			__magic_name__			(			self	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a				):
          lowercase    :				str       		=							AlbertModel(config=_a				)
          model.to(_a				)
          model.eval()
          lowercase    :				Union[str, Any]       		=							model(_a	,    attention_mask=_a	,    token_type_ids=_a				)
          lowercase    :				int       		=							model(_a	,    token_type_ids=_a				)
          lowercase    :				Any       		=							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 			__magic_name__			(			self	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a				):
          lowercase    :				Dict       		=							AlbertForPreTraining(config=_a				)
          model.to(_a				)
          model.eval()
          lowercase    :				Optional[Any]       		=							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 			__magic_name__			(			self	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a				):
          lowercase    :				Optional[Any]       		=							AlbertForMaskedLM(config=_a				)
          model.to(_a				)
          model.eval()
          lowercase    :				List[Any]       		=							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 			__magic_name__			(			self	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a				):
          lowercase    :				Optional[Any]       		=							AlbertForQuestionAnswering(config=_a				)
          model.to(_a				)
          model.eval()
          lowercase    :				List[str]       		=							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 			__magic_name__			(			self	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a				):
          lowercase    :				int       		=							self.num_labels
          lowercase    :				Any       		=							AlbertForSequenceClassification(_a				)
          model.to(_a				)
          model.eval()
          lowercase    :				Tuple       		=							model(_a	,    attention_mask=_a	,    token_type_ids=_a	,    labels=_a				)
          self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.num_labels)				)
   def 			__magic_name__			(			self	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a				):
          lowercase    :				List[Any]       		=							self.num_labels
          lowercase    :				Tuple       		=							AlbertForTokenClassification(config=_a				)
          model.to(_a				)
          model.eval()
          lowercase    :				int       		=							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 			__magic_name__			(			self	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a	,    _a				):
          lowercase    :				Any       		=							self.num_choices
          lowercase    :				Dict       		=							AlbertForMultipleChoice(config=_a				)
          model.to(_a				)
          model.eval()
          lowercase    :				Any       		=							input_ids.unsqueeze(1				).expand(-1	,    self.num_choices	,    -1				).contiguous()
          lowercase    :				int       		=							token_type_ids.unsqueeze(1				).expand(-1	,    self.num_choices	,    -1				).contiguous()
          lowercase    :				List[Any]       		=							input_mask.unsqueeze(1				).expand(-1	,    self.num_choices	,    -1				).contiguous()
          lowercase    :				List[str]       		=							model(
              _a	,    attention_mask=_a	,    token_type_ids=_a	,    labels=_a	,    )
          self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.num_choices)				)

   def 			__magic_name__			(			self				):
          lowercase    :				Tuple       		=							self.prepare_config_and_inputs()
          (
              (
              lowercase
          )		,    (
              lowercase
          )		,    (
              lowercase
          )		,    (
              lowercase
          )		,    (
              lowercase
          )		,    (
              lowercase
          )		,    (
              lowercase
          )		,    
          )    :				Dict       		=							config_and_inputs
          lowercase    :				Optional[int]       		=							{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
          return config, inputs_dict


@require_torch
class 		a__						(      a_,			a_,			unittest.TestCase						):
   __lowerCAmelCase            =				(
       (
           AlbertModel,
           AlbertForPreTraining,
           AlbertForMaskedLM,
           AlbertForMultipleChoice,
           AlbertForSequenceClassification,
           AlbertForTokenClassification,
           AlbertForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   __lowerCAmelCase            =				(
       {
           """feature-extraction""": AlbertModel,
           """fill-mask""": AlbertForMaskedLM,
           """question-answering""": AlbertForQuestionAnswering,
           """text-classification""": AlbertForSequenceClassification,
           """token-classification""": AlbertForTokenClassification,
           """zero-shot""": AlbertForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   __lowerCAmelCase            =				True
   def 			__magic_name__			(			self	,    _a	,    _a	,    _a=False				):
          lowercase    :				Union[str, Any]       		=							super()._prepare_for_class(_a	,    _a	,    return_labels=_a				)

          if return_labels:
                 if model_class in get_values(_a				):
                        lowercase    :				Union[str, Any]       		=							torch.zeros(
                            (self.model_tester.batch_size, self.model_tester.seq_length)	,    dtype=torch.long	,    device=_a				)
                        lowercase    :				Tuple       		=							torch.zeros(
                            self.model_tester.batch_size	,    dtype=torch.long	,    device=_a				)
          return inputs_dict
   def 			__magic_name__			(			self				):
          lowercase    :				Dict       		=							AlbertModelTester(self				)
          lowercase    :				Union[str, Any]       		=							ConfigTester(self	,    config_class=_a	,    hidden_size=37				)
   def 			__magic_name__			(			self				):
          self.config_tester.run_common_tests()
   def 			__magic_name__			(			self				):
          lowercase    :				Dict       		=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_a				)
   def 			__magic_name__			(			self				):
          lowercase    :				Dict       		=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_pretraining(*_a				)
   def 			__magic_name__			(			self				):
          lowercase    :				int       		=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*_a				)
   def 			__magic_name__			(			self				):
          lowercase    :				Dict       		=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*_a				)
   def 			__magic_name__			(			self				):
          lowercase    :				Union[str, Any]       		=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*_a				)
   def 			__magic_name__			(			self				):
          lowercase    :				Any       		=							self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*_a				)
   def 			__magic_name__			(			self				):
          lowercase    :				str       		=							self.model_tester.prepare_config_and_inputs()
          for type in ["absolute", "relative_key", "relative_key_query"]:
                 lowercase    :				List[Any]       		=							type
                 self.model_tester.create_and_check_model(*_a				)

   @slow
   def 			__magic_name__			(			self				):
          for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowercase    :				Tuple       		=							AlbertModel.from_pretrained(_a				)
                 self.assertIsNotNone(_a				)


@require_torch
class 		a__						(      unittest.TestCase						):
   @slow
   def 			__magic_name__			(			self				):
          lowercase    :				str       		=							AlbertModel.from_pretrained("albert-base-v2"				)
          lowercase    :				int       		=							torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]				)
          lowercase    :				Any       		=							torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
          with torch.no_grad():
                 lowercase    :				List[Any]       		=							model(_a	,    attention_mask=_a				)[0]
          lowercase    :				List[str]       		=							torch.Size((1, 11, 768)				)
          self.assertEqual(output.shape	,    _a				)
          lowercase    :				List[Any]       		=							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 os
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import AddedToken
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
   from .tokenization_fnet import FNetTokenizer
else:
   _A			:       Union[str, Any]   		=       None

_A			:       str   		=       logging.get_logger(__name__)
_A			:       Tuple   		=       {"""vocab_file""": """spiece.model""", """tokenizer_file""": """tokenizer.json"""}

_A			:       Optional[Any]   		=       {
    """vocab_file""": {
        """google/fnet-base""": """https://huggingface.co/google/fnet-base/resolve/main/spiece.model""",
        """google/fnet-large""": """https://huggingface.co/google/fnet-large/resolve/main/spiece.model""",
    },
    """tokenizer_file""": {
        """google/fnet-base""": """https://huggingface.co/google/fnet-base/resolve/main/tokenizer.json""",
        """google/fnet-large""": """https://huggingface.co/google/fnet-large/resolve/main/tokenizer.json""",
    },
}

_A			:       Optional[Any]   		=       {
    """google/fnet-base""": 5_12,
    """google/fnet-large""": 5_12,
}

_A			:       List[str]   		=       """▁"""

class 		a__						(      a_						):
   __lowerCAmelCase            =				VOCAB_FILES_NAMES
   __lowerCAmelCase            =				PRETRAINED_VOCAB_FILES_MAP
   __lowerCAmelCase            =				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   __lowerCAmelCase            =				["""input_ids""", """token_type_ids"""]
   __lowerCAmelCase            =				FNetTokenizer
   def __init__(			self	,    _a=None	,    _a=None	,    _a=False	,    _a=True	,    _a=True	,    _a="<unk>"	,    _a="[SEP]"	,    _a="<pad>"	,    _a="[CLS]"	,    _a="[MASK]"	,    **_a	,    ):
          # Mask token behave like a normal word, i.e. include the space before it and
          # is included in the raw text, there should be a match in a non-normalized sentence.
          lowercase    :				int       		=							(
              AddedToken(_a	,    lstrip=_a	,    rstrip=_a	,    normalized=_a				)
              if isinstance(_a	,    _a				)
              else mask_token
          )

          super().__init__(
              _a	,    tokenizer_file=_a	,    do_lower_case=_a	,    remove_space=_a	,    keep_accents=_a	,    unk_token=_a	,    sep_token=_a	,    pad_token=_a	,    cls_token=_a	,    mask_token=_a	,    **_a	,    )

          lowercase    :				Dict       		=							do_lower_case
          lowercase    :				Union[str, Any]       		=							remove_space
          lowercase    :				Any       		=							keep_accents
          lowercase    :				List[Any]       		=							vocab_file
          lowercase    :				Union[str, Any]       		=							False if not self.vocab_file else True
   def 			__magic_name__			(			self	,    _a	,    _a = None				):
          lowercase    :				Optional[Any]       		=							[self.sep_token_id]
          lowercase    :				str       		=							[self.cls_token_id]
          if token_ids_a is None:
                 return cls + token_ids_a + sep
          return cls + token_ids_a + sep + token_ids_a + sep
   def 			__magic_name__			(			self	,    _a	,    _a = None				):
          lowercase    :				Any       		=							[self.sep_token_id]
          lowercase    :				Dict       		=							[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 			__magic_name__			(			self	,    _a	,    _a = None				):
          if not os.path.isdir(_a				):
                 logger.error(f"""Vocabulary path ({save_directory}) should be a directory"""				)
                 return
          lowercase    :				Optional[Any]       		=							os.path.join(
              _a	,    (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]				)

          if os.path.abspath(self.vocab_file				) != os.path.abspath(_a				):
                 copyfile(self.vocab_file	,    _a				)

          return (out_vocab_file,)


