
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 				UpperCAmelCase (    unittest.TestCase    ):




	'''simple docstring'''





	def __init__(			self   :					str				,A   :					List[str]				,A   :					List[Any]=13				,A   :					Optional[int]=7				,A   :					Tuple=True				,A   :					Tuple=True				,A   :					Tuple=True				,A   :					Optional[Any]=True				,A   :					Optional[int]=99				,A   :					Optional[Any]=32				,A   :					int=5				,A   :					List[Any]=4				,A   :					Union[str, Any]=37				,A   :					int="gelu"				,A   :					int=0.1				,A   :					List[Any]=0.1				,A   :					List[str]=5_12				,A   :					int=16				,A   :					str=2				,A   :					int=0.02				,A   :					Optional[Any]=4				,):
					__A       =					parent
					__A       =					batch_size
					__A       =					seq_length
					__A       =					is_training
					__A       =					use_attention_mask
					__A       =					use_token_type_ids
					__A       =					use_labels
					__A       =					vocab_size
					__A       =					hidden_size
					__A       =					num_hidden_layers
					__A       =					num_attention_heads
					__A       =					intermediate_size
					__A       =					hidden_act
					__A       =					hidden_dropout_prob
					__A       =					attention_probs_dropout_prob
					__A       =					max_position_embeddings
					__A       =					type_vocab_size
					__A       =					type_sequence_label_size
					__A       =					initializer_range
					__A       =					num_choices






	def 							UpperCamelCase_    (			self   :					Dict					):
					__A       =					ids_tensor([self.batch_size, self.seq_length]				,self.vocab_size					)

					__A       =					None
					if self.use_attention_mask:
									__A       =					random_attention_mask([self.batch_size, self.seq_length]					)

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

					__A       =					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=A				,initializer_range=self.initializer_range				,)

					return config, input_ids, token_type_ids, attention_mask







	def 							UpperCamelCase_    (			self   :					Dict					):
					__A       =					self.prepare_config_and_inputs()
					__A     ,				__A     ,				__A     ,				__A       =					config_and_inputs
					__A       =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
					return config, inputs_dict





@require_flax
class 				UpperCAmelCase (    __SCREAMING_SNAKE_CASE     ,     unittest.TestCase    ):




	'''simple docstring'''





	snake_case_          =	(
	    (
	        FlaxAlbertModel,
	        FlaxAlbertForPreTraining,
	        FlaxAlbertForMaskedLM,
	        FlaxAlbertForMultipleChoice,
	        FlaxAlbertForQuestionAnswering,
	        FlaxAlbertForSequenceClassification,
	        FlaxAlbertForTokenClassification,
	        FlaxAlbertForQuestionAnswering,
	    )
	    if is_flax_available()
	    else ()
	)






	def 							UpperCamelCase_    (			self   :					List[Any]					):
					__A       =					FlaxAlbertModelTester(self					)







	@slow
	def 							UpperCamelCase_    (			self   :					List[Any]					):
					for model_class_name in self.all_model_classes:
									__A       =					model_class_name.from_pretrained("albert-base-v2"					)
									__A       =					model(np.ones((1, 1)					)					)
									self.assertIsNotNone(A					)





@require_flax
class 				UpperCAmelCase (    unittest.TestCase    ):




	'''simple docstring'''





	@slow
	def 							UpperCamelCase_    (			self   :					Optional[Any]					):
					__A       =					FlaxAlbertModel.from_pretrained("albert-base-v2"					)
					__A       =					np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]					)
					__A       =					np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
					__A       =					model(A				,attention_mask=A					)[0]
					__A       =					(1, 11, 7_68)
					self.assertEqual(output.shape				,A					)
					__A       =					np.array(
					    [[[-0.65_13, 1.50_35, -0.27_66], [-0.65_15, 1.50_46, -0.27_80], [-0.65_12, 1.50_49, -0.27_84]]]					)

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

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCamelCase__  =					{
    '''configuration_longformer''': [
        '''LONGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''LongformerConfig''',
        '''LongformerOnnxConfig''',
    ],
    '''tokenization_longformer''': ['''LongformerTokenizer'''],
}

try:
   if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowerCamelCase__  =					['''LongformerTokenizerFast''']

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowerCamelCase__  =					[
       '''LONGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''LongformerForMaskedLM''',
       '''LongformerForMultipleChoice''',
       '''LongformerForQuestionAnswering''',
       '''LongformerForSequenceClassification''',
       '''LongformerForTokenClassification''',
       '''LongformerModel''',
       '''LongformerPreTrainedModel''',
       '''LongformerSelfAttention''',
   ]

try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowerCamelCase__  =					[
       '''TF_LONGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''TFLongformerForMaskedLM''',
       '''TFLongformerForMultipleChoice''',
       '''TFLongformerForQuestionAnswering''',
       '''TFLongformerForSequenceClassification''',
       '''TFLongformerForTokenClassification''',
       '''TFLongformerModel''',
       '''TFLongformerPreTrainedModel''',
       '''TFLongformerSelfAttention''',
   ]


if TYPE_CHECKING:
   from .configuration_longformer import (
       LONGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
       LongformerConfig,
       LongformerOnnxConfig,
   )
   from .tokenization_longformer import LongformerTokenizer

   try:
      if not is_tokenizers_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_longformer_fast import LongformerTokenizerFast

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_longformer import (
          LONGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          LongformerForMaskedLM,
          LongformerForMultipleChoice,
          LongformerForQuestionAnswering,
          LongformerForSequenceClassification,
          LongformerForTokenClassification,
          LongformerModel,
          LongformerPreTrainedModel,
          LongformerSelfAttention,
      )

   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_longformer import (
          TF_LONGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFLongformerForMaskedLM,
          TFLongformerForMultipleChoice,
          TFLongformerForQuestionAnswering,
          TFLongformerForSequenceClassification,
          TFLongformerForTokenClassification,
          TFLongformerModel,
          TFLongformerPreTrainedModel,
          TFLongformerSelfAttention,
      )

else:
   import sys

   lowerCamelCase__  =					_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




