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   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_=13   ,				SCREAMING_SNAKE_CASE_=7   ,				SCREAMING_SNAKE_CASE_=True   ,				SCREAMING_SNAKE_CASE_=True   ,				SCREAMING_SNAKE_CASE_=True   ,				SCREAMING_SNAKE_CASE_=True   ,				SCREAMING_SNAKE_CASE_=99   ,				SCREAMING_SNAKE_CASE_=32   ,				SCREAMING_SNAKE_CASE_=5   ,				SCREAMING_SNAKE_CASE_=4   ,				SCREAMING_SNAKE_CASE_=37   ,				SCREAMING_SNAKE_CASE_="gelu"   ,				SCREAMING_SNAKE_CASE_=0.1   ,				SCREAMING_SNAKE_CASE_=0.1   ,				SCREAMING_SNAKE_CASE_=512   ,				SCREAMING_SNAKE_CASE_=16   ,				SCREAMING_SNAKE_CASE_=2   ,				SCREAMING_SNAKE_CASE_=0.02   ,				SCREAMING_SNAKE_CASE_=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 		_a					(self	)		->      Dict:





       '''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=SCREAMING_SNAKE_CASE_   ,				initializer_range=self.initializer_range   ,				)

       return config, input_ids, token_type_ids, attention_mask






  def 		_a					(self	)		->      List[Any]:





       '''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							):
  SCREAMING_SNAKE_CASE_	:	Tuple						=(
      (
          FlaxAlbertModel,
          FlaxAlbertForPreTraining,
          FlaxAlbertForMaskedLM,
          FlaxAlbertForMultipleChoice,
          FlaxAlbertForQuestionAnswering,
          FlaxAlbertForSequenceClassification,
          FlaxAlbertForTokenClassification,
          FlaxAlbertForQuestionAnswering,
      )
      if is_flax_available()
      else ()
  )






  def 		_a					(self	)		->      List[str]:





       '''simple docstring'''





       UpperCamelCase__										=      FlaxAlbertModelTester(self	)






  @slow
  def 		_a					(self	)		->      Dict:





       '''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(SCREAMING_SNAKE_CASE_	)



@require_flax
class     _A   (    unittest.TestCase							):






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





       '''simple docstring'''





       UpperCamelCase__										=      FlaxAlbertModel.from_pretrained('''albert-base-v2'''	)
       UpperCamelCase__										=      np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]	)
       UpperCamelCase__										=      np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
       UpperCamelCase__										=      model(SCREAMING_SNAKE_CASE_   ,				attention_mask=SCREAMING_SNAKE_CASE_	)[0]
       UpperCamelCase__										=      (1, 11, 768)
       self.assertEqual(output.shape   ,				SCREAMING_SNAKE_CASE_	)
       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]   ,				SCREAMING_SNAKE_CASE_   ,				atol=1E-4	)	)




from datetime import datetime

import matplotlib.pyplot as plt
import torch







def  __UpperCamelCase    (						A							):
     for param in module.parameters():
          UpperCamelCase__										=      False







def  __UpperCamelCase    (						):
     UpperCamelCase__										=      '''cuda''' if torch.cuda.is_available() else '''cpu'''
     if torch.backends.mps.is_available() and torch.backends.mps.is_built():
          UpperCamelCase__										=      '''mps'''
     if device == "mps":
          print(
              '''WARNING: MPS currently doesn\'t seem to work, and messes up backpropagation without any visible torch'''
              ''' errors. I recommend using CUDA on a colab notebook or CPU instead if you\'re facing inexplicable issues'''
              ''' with generations.'''							)
     return device







def  __UpperCamelCase    (						A							):
     UpperCamelCase__										=      plt.imshow(A							)
     fig.axes.get_xaxis().set_visible(A							)
     fig.axes.get_yaxis().set_visible(A							)
     plt.show()







def  __UpperCamelCase    (						):
     UpperCamelCase__										=      datetime.now()
     UpperCamelCase__										=      current_time.strftime('''%H:%M:%S'''							)
     return timestamp



