import unittest

import numpy as np

from transformers import AlbertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
      import jax.numpy as jnp

      from transformers.models.albert.modeling_flax_albert import (
          FlaxAlbertForMaskedLM,
          FlaxAlbertForMultipleChoice,
          FlaxAlbertForPreTraining,
          FlaxAlbertForQuestionAnswering,
          FlaxAlbertForSequenceClassification,
          FlaxAlbertForTokenClassification,
          FlaxAlbertModel,
      )





class      A							(				unittest.TestCase     ):


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



         UpperCAmelCase__								=  parent
         UpperCAmelCase__								=  batch_size
         UpperCAmelCase__								=  seq_length
         UpperCAmelCase__								=  is_training
         UpperCAmelCase__								=  use_attention_mask
         UpperCAmelCase__								=  use_token_type_ids
         UpperCAmelCase__								=  use_labels
         UpperCAmelCase__								=  vocab_size
         UpperCAmelCase__								=  hidden_size
         UpperCAmelCase__								=  num_hidden_layers
         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_choices


   def 			lowercase_      (self     :						str		)						->				Optional[Any]:
         """simple docstring"""



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

         UpperCAmelCase__								=  None
         if self.use_attention_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__								=  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       ,  is_decoder=__UpperCAmelCase       ,  initializer_range=self.initializer_range       ,  )

         return config, input_ids, token_type_ids, attention_mask





   def 			lowercase_      (self     :						Tuple		)						->				Tuple:
         """simple docstring"""



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





@require_flax
class      A							(				UpperCAmelCase_		,				unittest.TestCase     ):
   __UpperCAmelCase : Tuple      =			(
       (
           FlaxAlbertModel,
           FlaxAlbertForPreTraining,
           FlaxAlbertForMaskedLM,
           FlaxAlbertForMultipleChoice,
           FlaxAlbertForQuestionAnswering,
           FlaxAlbertForSequenceClassification,
           FlaxAlbertForTokenClassification,
           FlaxAlbertForQuestionAnswering,
       )
       if is_flax_available()
       else ()
   )


   def 			lowercase_      (self     :						Tuple		)						->				Any:
         """simple docstring"""



         UpperCAmelCase__								=  FlaxAlbertModelTester(self		)





   @slow
   def 			lowercase_      (self     :						Optional[Any]		)						->				List[str]:
         """simple docstring"""



         for model_class_name in self.all_model_classes:
               UpperCAmelCase__								=  model_class_name.from_pretrained("albert-base-v2"		)
               UpperCAmelCase__								=  model(np.ones((1, 1)		)		)
               self.assertIsNotNone(__UpperCAmelCase		)





@require_flax
class      A							(				unittest.TestCase     ):


   @slow
   def 			lowercase_      (self     :						int		)						->				str:
         """simple docstring"""



         UpperCAmelCase__								=  FlaxAlbertModel.from_pretrained("albert-base-v2"		)
         UpperCAmelCase__								=  np.array([[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__								=  np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
         UpperCAmelCase__								=  model(__UpperCAmelCase       ,  attention_mask=__UpperCAmelCase		)[0]
         UpperCAmelCase__								=  (1, 1_1, 7_6_8)
         self.assertEqual(output.shape       ,  __UpperCAmelCase		)
         UpperCAmelCase__								=  np.array(
             [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]		)

         self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]       ,  __UpperCAmelCase       ,  atol=1E-4		)		)




from __future__ import annotations

from collections import deque





class      A							:


   def __init__(self     :						Dict       ,  __UpperCAmelCase     :						list[str]		)						->				Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  []
         self.adlist.append(
             {"value": "", "next_states": [], "fail_state": 0, "output": []}		)

         for keyword in keywords:
               self.add_keyword(__UpperCAmelCase		)
         self.set_fail_transitions()


   def 			lowercase_      (self     :						Tuple       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						str		)						->				int | None:
         """simple docstring"""



         for state in self.adlist[current_state]["next_states"]:
               if char == self.adlist[state]["value"]:
                     return state
         return None


   def 			lowercase_      (self     :						Dict       ,  __UpperCAmelCase     :						str		)						->				None:
         """simple docstring"""



         UpperCAmelCase__								=  0
         for character in keyword:
               UpperCAmelCase__								=  self.find_next_state(__UpperCAmelCase       ,  __UpperCAmelCase		)
               if next_state is None:
                     self.adlist.append(
                         {
                             "value": character,
                             "next_states": [],
                             "fail_state": 0,
                             "output": [],
                         }		)
                     self.adlist[current_state]["next_states"].append(len(self.adlist		) - 1		)
                     UpperCAmelCase__								=  len(self.adlist		) - 1
               else:
                     UpperCAmelCase__								=  next_state
         self.adlist[current_state]["output"].append(__UpperCAmelCase		)


   def 			lowercase_      (self     :						Optional[int]		)						->				None:
         """simple docstring"""



         UpperCAmelCase__								=  deque()
         for node in self.adlist[0]["next_states"]:
               q.append(__UpperCAmelCase		)
               UpperCAmelCase__								=  0
         while q:
               UpperCAmelCase__								=  q.popleft()
               for child in self.adlist[r]["next_states"]:
                     q.append(__UpperCAmelCase		)
                     UpperCAmelCase__								=  self.adlist[r]["fail_state"]
                     while (
                         self.find_next_state(__UpperCAmelCase       ,  self.adlist[child]["value"]		) is None
                         and state != 0
                     ):
                           UpperCAmelCase__								=  self.adlist[state]["fail_state"]
                     UpperCAmelCase__								=  self.find_next_state(
                         __UpperCAmelCase       ,  self.adlist[child]["value"]		)
                     if self.adlist[child]["fail_state"] is None:
                           UpperCAmelCase__								=  0
                     UpperCAmelCase__								=  (
                         self.adlist[child]["output"]
                         + self.adlist[self.adlist[child]["fail_state"]]["output"]
                     )





   def 			lowercase_      (self     :						Union[str, Any]       ,  __UpperCAmelCase     :						str		)						->				dict[str, list[int]]:
         """simple docstring"""



         UpperCAmelCase__								=  {}  # returns a dict with keywords and list of its occurrences
         UpperCAmelCase__								=  0
         for i in range(len(__UpperCAmelCase		)		):
               while (
                   self.find_next_state(__UpperCAmelCase       ,  string[i]		) is None
                   and current_state != 0
               ):
                     UpperCAmelCase__								=  self.adlist[current_state]["fail_state"]
               UpperCAmelCase__								=  self.find_next_state(__UpperCAmelCase       ,  string[i]		)
               if next_state is None:
                     UpperCAmelCase__								=  0
               else:
                     UpperCAmelCase__								=  next_state
                     for key in self.adlist[current_state]["output"]:
                           if key not in result:
                                 UpperCAmelCase__								=  []
                           result[key].append(i - len(__UpperCAmelCase		) + 1		)
         return result


if __name__ == "__main__":
      import doctest

      doctest.testmod()



