


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





      def __init__(		self    :       Tuple       ,							__lowerCamelCase    :       Optional[int]       ,							__lowerCamelCase    :       List[str]=1_3       ,							__lowerCamelCase    :       Any=7       ,							__lowerCamelCase    :       Tuple=True       ,							__lowerCamelCase    :       List[str]=True       ,							__lowerCamelCase    :       int=True       ,							__lowerCamelCase    :       List[str]=True       ,							__lowerCamelCase    :       List[Any]=9_9       ,							__lowerCamelCase    :       List[str]=1_6       ,							__lowerCamelCase    :       Optional[Any]=3_6       ,							__lowerCamelCase    :       Optional[Any]=6       ,							__lowerCamelCase    :       Optional[int]=6       ,							__lowerCamelCase    :       int=6       ,							__lowerCamelCase    :       List[str]=3_7       ,							__lowerCamelCase    :       Optional[Any]="gelu"       ,							__lowerCamelCase    :       List[Any]=0.1       ,							__lowerCamelCase    :       Union[str, Any]=0.1       ,							__lowerCamelCase    :       Optional[int]=5_1_2       ,							__lowerCamelCase    :       Optional[Any]=1_6       ,							__lowerCamelCase    :       List[Any]=2       ,							__lowerCamelCase    :       List[str]=0.02       ,							__lowerCamelCase    :       Optional[int]=3       ,							__lowerCamelCase    :       Dict=4       ,							__lowerCamelCase    :       List[Any]=None       ,							)     -> List[str]:






         """simple docstring"""







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





      def 							_lowercase					(		self    :       List[Any]							)     -> List[str]:






         """simple docstring"""







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

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

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

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

         UpperCAmelCase      					=  self.get_config()

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





      def 							_lowercase					(		self    :       Optional[int]							)     -> List[str]:






         """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 							_lowercase					(		self    :       Dict       ,							__lowerCamelCase    :       Tuple       ,							__lowerCamelCase    :       str       ,							__lowerCamelCase    :       Union[str, Any]       ,							__lowerCamelCase    :       Optional[int]       ,							__lowerCamelCase    :       int       ,							__lowerCamelCase    :       int       ,							__lowerCamelCase    :       Optional[int]							)     -> str:






         """simple docstring"""







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






         """simple docstring"""







         UpperCAmelCase      					=  AlbertForPreTraining(config=__lowerCamelCase							)
         model.to(__lowerCamelCase							)
         model.eval()
         UpperCAmelCase      					=  model(
             __lowerCamelCase       ,							attention_mask=__lowerCamelCase       ,							token_type_ids=__lowerCamelCase       ,							labels=__lowerCamelCase       ,							sentence_order_label=__lowerCamelCase       ,							)
         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 							_lowercase					(		self    :       List[str]       ,							__lowerCamelCase    :       Tuple       ,							__lowerCamelCase    :       Union[str, Any]       ,							__lowerCamelCase    :       Tuple       ,							__lowerCamelCase    :       Optional[int]       ,							__lowerCamelCase    :       List[Any]       ,							__lowerCamelCase    :       Tuple       ,							__lowerCamelCase    :       Tuple							)     -> str:






         """simple docstring"""







         UpperCAmelCase      					=  AlbertForMaskedLM(config=__lowerCamelCase							)
         model.to(__lowerCamelCase							)
         model.eval()
         UpperCAmelCase      					=  model(__lowerCamelCase       ,							attention_mask=__lowerCamelCase       ,							token_type_ids=__lowerCamelCase       ,							labels=__lowerCamelCase							)
         self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.seq_length, self.vocab_size)							)





      def 							_lowercase					(		self    :       List[Any]       ,							__lowerCamelCase    :       Optional[int]       ,							__lowerCamelCase    :       str       ,							__lowerCamelCase    :       Union[str, Any]       ,							__lowerCamelCase    :       List[Any]       ,							__lowerCamelCase    :       Any       ,							__lowerCamelCase    :       List[Any]       ,							__lowerCamelCase    :       Tuple							)     -> Optional[int]:






         """simple docstring"""







         UpperCAmelCase      					=  AlbertForQuestionAnswering(config=__lowerCamelCase							)
         model.to(__lowerCamelCase							)
         model.eval()
         UpperCAmelCase      					=  model(
             __lowerCamelCase       ,							attention_mask=__lowerCamelCase       ,							token_type_ids=__lowerCamelCase       ,							start_positions=__lowerCamelCase       ,							end_positions=__lowerCamelCase       ,							)
         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    :       List[Any]       ,							__lowerCamelCase    :       Optional[Any]       ,							__lowerCamelCase    :       Tuple       ,							__lowerCamelCase    :       Any       ,							__lowerCamelCase    :       str       ,							__lowerCamelCase    :       List[Any]       ,							__lowerCamelCase    :       Any       ,							__lowerCamelCase    :       List[str]							)     -> List[str]:






         """simple docstring"""







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





      def 							_lowercase					(		self    :       Any       ,							__lowerCamelCase    :       Union[str, Any]       ,							__lowerCamelCase    :       Any       ,							__lowerCamelCase    :       Optional[int]       ,							__lowerCamelCase    :       Union[str, Any]       ,							__lowerCamelCase    :       str       ,							__lowerCamelCase    :       Optional[Any]       ,							__lowerCamelCase    :       Dict							)     -> Optional[int]:






         """simple docstring"""







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





      def 							_lowercase					(		self    :       Any       ,							__lowerCamelCase    :       Tuple       ,							__lowerCamelCase    :       Tuple       ,							__lowerCamelCase    :       str       ,							__lowerCamelCase    :       str       ,							__lowerCamelCase    :       Tuple       ,							__lowerCamelCase    :       Tuple       ,							__lowerCamelCase    :       str							)     -> List[Any]:






         """simple docstring"""







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





      def 							_lowercase					(		self    :       Optional[Any]							)     -> int:






         """simple docstring"""







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






@require_torch
class 	__lowercase					(	__snake_case			,       __snake_case			,       unittest.TestCase	):
      UpperCamelCase										=							(
          (
              AlbertModel,
              AlbertForPreTraining,
              AlbertForMaskedLM,
              AlbertForMultipleChoice,
              AlbertForSequenceClassification,
              AlbertForTokenClassification,
              AlbertForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      UpperCamelCase										=							(
          {
              '''feature-extraction''': AlbertModel,
              '''fill-mask''': AlbertForMaskedLM,
              '''question-answering''': AlbertForQuestionAnswering,
              '''text-classification''': AlbertForSequenceClassification,
              '''token-classification''': AlbertForTokenClassification,
              '''zero-shot''': AlbertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      UpperCamelCase										=							True





      def 							_lowercase					(		self    :       Tuple       ,							__lowerCamelCase    :       str       ,							__lowerCamelCase    :       Tuple       ,							__lowerCamelCase    :       Optional[Any]=False							)     -> List[Any]:






         """simple docstring"""







         UpperCAmelCase      					=  super()._prepare_for_class(__lowerCamelCase       ,							__lowerCamelCase       ,							return_labels=__lowerCamelCase							)

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





      def 							_lowercase					(		self    :       Tuple							)     -> Tuple:






         """simple docstring"""







         UpperCAmelCase      					=  AlbertModelTester(self							)
         UpperCAmelCase      					=  ConfigTester(self       ,							config_class=__lowerCamelCase       ,							hidden_size=3_7							)





      def 							_lowercase					(		self    :       Dict							)     -> Optional[Any]:






         """simple docstring"""







         self.config_tester.run_common_tests()





      def 							_lowercase					(		self    :       Tuple							)     -> str:






         """simple docstring"""







         UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__lowerCamelCase							)





      def 							_lowercase					(		self    :       Dict							)     -> str:






         """simple docstring"""







         UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_pretraining(*__lowerCamelCase							)





      def 							_lowercase					(		self    :       Any							)     -> Optional[Any]:






         """simple docstring"""







         UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*__lowerCamelCase							)





      def 							_lowercase					(		self    :       Any							)     -> Tuple:






         """simple docstring"""







         UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*__lowerCamelCase							)





      def 							_lowercase					(		self    :       Tuple							)     -> List[str]:






         """simple docstring"""







         UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase							)





      def 							_lowercase					(		self    :       Optional[int]							)     -> Optional[Any]:






         """simple docstring"""







         UpperCAmelCase      					=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase							)





      def 							_lowercase					(		self    :       List[Any]							)     -> Tuple:






         """simple docstring"""







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





      @slow
      def 							_lowercase					(		self    :       Union[str, Any]							)     -> Any:






         """simple docstring"""







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






@require_torch
class 	__lowercase					(	unittest.TestCase	):





      @slow
      def 							_lowercase					(		self    :       Union[str, Any]							)     -> Tuple:






         """simple docstring"""







         UpperCAmelCase      					=  AlbertModel.from_pretrained("""albert-base-v2"""							)
         UpperCAmelCase      					=  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]]							)
         UpperCAmelCase      					=  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
         with torch.no_grad():
            UpperCAmelCase      					=  model(__lowerCamelCase       ,							attention_mask=__lowerCamelCase							)[0]
         UpperCAmelCase      					=  torch.Size((1, 1_1, 7_6_8)							)
         self.assertEqual(output.shape       ,							__lowerCamelCase							)
         UpperCAmelCase      					=  torch.tensor(
             [[[-0.6_513, 1.5_035, -0.2_766], [-0.6_515, 1.5_046, -0.2_780], [-0.6_512, 1.5_049, -0.2_784]]]							)

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







__a	 =      256
# Modulus to hash a string
__a	 =      100_0003
def 	_UpperCamelCase					(    lowerCAmelCase_	,    lowerCAmelCase_ )    ->bool:
   UpperCAmelCase      					=  len(lowerCAmelCase_ )
   UpperCAmelCase      					=  len(lowerCAmelCase_ )
   if p_len > t_len:
      return False

   UpperCAmelCase      					=  0
   UpperCAmelCase      					=  0
   UpperCAmelCase      					=  1

   # Calculating the hash of pattern and substring of text
   for i in range(lowerCAmelCase_ ):
      UpperCAmelCase      					=  (ord(pattern[i] ) + p_hash * alphabet_size) % modulus
      UpperCAmelCase      					=  (ord(text[i] ) + text_hash * alphabet_size) % modulus
      if i == p_len - 1:
         continue
      UpperCAmelCase      					=  (modulus_power * alphabet_size) % modulus

   for i in range(0	,    t_len - p_len + 1 ):
      if text_hash == p_hash and text[i : i + p_len] == pattern:
         return True
      if i == t_len - p_len:
         continue
      # Calculate the https://en.wikipedia.org/wiki/Rolling_hash
      UpperCAmelCase      					=  (
          (text_hash - ord(text[i] ) * modulus_power) * alphabet_size
          + ord(text[i + p_len] )
      ) % modulus
   return False
def 	_UpperCamelCase					(    )    ->None:
   UpperCAmelCase      					=  """abc1abc12"""
   UpperCAmelCase      					=  """alskfjaldsabc1abc1abc12k23adsfabcabc"""
   UpperCAmelCase      					=  """alskfjaldsk23adsfabcabc"""
   assert rabin_karp(lowerCAmelCase_	,    lowerCAmelCase_ ) and not rabin_karp(lowerCAmelCase_	,    lowerCAmelCase_ )

   # Test 2)
   UpperCAmelCase      					=  """ABABX"""
   UpperCAmelCase      					=  """ABABZABABYABABX"""
   assert rabin_karp(lowerCAmelCase_	,    lowerCAmelCase_ )

   # Test 3)
   UpperCAmelCase      					=  """AAAB"""
   UpperCAmelCase      					=  """ABAAAAAB"""
   assert rabin_karp(lowerCAmelCase_	,    lowerCAmelCase_ )

   # Test 4)
   UpperCAmelCase      					=  """abcdabcy"""
   UpperCAmelCase      					=  """abcxabcdabxabcdabcdabcy"""
   assert rabin_karp(lowerCAmelCase_	,    lowerCAmelCase_ )

   # Test 5)
   UpperCAmelCase      					=  """Lü"""
   UpperCAmelCase      					=  """Lüsai"""
   assert rabin_karp(lowerCAmelCase_	,    lowerCAmelCase_ )
   UpperCAmelCase      					=  """Lue"""
   assert not rabin_karp(lowerCAmelCase_	,    lowerCAmelCase_ )
   print("""Success.""" )


if __name__ == "__main__":
 test_rabin_karp()



