





import unittest

from transformers import BigBirdConfig, 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

    from transformers.models.big_bird.modeling_flax_big_bird import (
        FlaxBigBirdForCausalLM,
        FlaxBigBirdForMaskedLM,
        FlaxBigBirdForMultipleChoice,
        FlaxBigBirdForPreTraining,
        FlaxBigBirdForQuestionAnswering,
        FlaxBigBirdForSequenceClassification,
        FlaxBigBirdForTokenClassification,
        FlaxBigBirdModel,
    )

class    lowerCAmelCase							(					unittest.TestCase			):
 def __init__(      self  :      Optional[Any]			,     UpperCAmelCase  :      Optional[Any]			,     UpperCAmelCase  :      Any=2			,     UpperCAmelCase  :      str=56			,     UpperCAmelCase  :      int=True			,     UpperCAmelCase  :      int=True			,     UpperCAmelCase  :      int=True			,     UpperCAmelCase  :      List[str]=True			,     UpperCAmelCase  :      str=99			,     UpperCAmelCase  :      Union[str, Any]=32			,     UpperCAmelCase  :      Tuple=2			,     UpperCAmelCase  :      Optional[int]=2			,     UpperCAmelCase  :      List[Any]=7			,     UpperCAmelCase  :      Tuple="gelu_new"			,     UpperCAmelCase  :      List[str]=0.1			,     UpperCAmelCase  :      List[str]=0.1			,     UpperCAmelCase  :      List[str]=512			,     UpperCAmelCase  :      Any=16			,     UpperCAmelCase  :      Optional[int]=2			,     UpperCAmelCase  :      List[Any]=0.0_2			,     UpperCAmelCase  :      Any=4			,     UpperCAmelCase  :      List[Any]="block_sparse"			,     UpperCAmelCase  :      Optional[Any]=True			,     UpperCAmelCase  :      int=False			,     UpperCAmelCase  :      Any=2			,     UpperCAmelCase  :      Any=3			,     )			->	str:
     lowerCamelCase__    :      Dict					 =		parent
     lowerCamelCase__    :      List[Any]					 =		batch_size
     lowerCamelCase__    :      Optional[int]					 =		seq_length
     lowerCamelCase__    :      int					 =		is_training
     lowerCamelCase__    :      Tuple					 =		use_attention_mask
     lowerCamelCase__    :      Optional[Any]					 =		use_token_type_ids
     lowerCamelCase__    :      Union[str, Any]					 =		use_labels
     lowerCamelCase__    :      Optional[Any]					 =		vocab_size
     lowerCamelCase__    :      Union[str, Any]					 =		hidden_size
     lowerCamelCase__    :      List[str]					 =		num_hidden_layers
     lowerCamelCase__    :      List[str]					 =		num_attention_heads
     lowerCamelCase__    :      str					 =		intermediate_size
     lowerCamelCase__    :      str					 =		hidden_act
     lowerCamelCase__    :      Any					 =		hidden_dropout_prob
     lowerCamelCase__    :      List[str]					 =		attention_probs_dropout_prob
     lowerCamelCase__    :      Dict					 =		max_position_embeddings
     lowerCamelCase__    :      Tuple					 =		type_vocab_size
     lowerCamelCase__    :      List[str]					 =		type_sequence_label_size
     lowerCamelCase__    :      Any					 =		initializer_range
     lowerCamelCase__    :      Dict					 =		num_choices

     lowerCamelCase__    :      int					 =		rescale_embeddings
     lowerCamelCase__    :      Union[str, Any]					 =		attention_type
     lowerCamelCase__    :      int					 =		use_bias
     lowerCamelCase__    :      List[Any]					 =		block_size
     lowerCamelCase__    :      str					 =		num_random_blocks
 def      A_	(      self  :      str	)			->	Dict:
     lowerCamelCase__    :      Any					 =		ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size	)

     lowerCamelCase__    :      List[str]					 =		None
     if self.use_attention_mask:
         lowerCamelCase__    :      Tuple					 =		random_attention_mask([self.batch_size, self.seq_length]	)

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

     lowerCamelCase__    :      Any					 =		BigBirdConfig(
         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			,     attention_type=self.attention_type			,     block_size=self.block_size			,     num_random_blocks=self.num_random_blocks			,     use_bias=self.use_bias			,     rescale_embeddings=self.rescale_embeddings			,     )

     return config, input_ids, token_type_ids, attention_mask







 def      A_	(      self  :      List[str]	)			->	int:
     lowerCamelCase__    :      Tuple					 =		self.prepare_config_and_inputs()
     lowerCamelCase__ ,							lowerCamelCase__ ,							lowerCamelCase__ ,							lowerCamelCase__    :      List[str]					 =		config_and_inputs
     lowerCamelCase__    :      Optional[int]					 =		{
         'input_ids': input_ids,
         'token_type_ids': token_type_ids,
         'attention_mask': attention_mask,
     }
     return config, inputs_dict


@require_flax
class    lowerCAmelCase							(					__UpperCamelCase,  unittest.TestCase			):
 UpperCAmelCase__													=		(
     (
         FlaxBigBirdForCausalLM,
         FlaxBigBirdModel,
         FlaxBigBirdForPreTraining,
         FlaxBigBirdForMaskedLM,
         FlaxBigBirdForMultipleChoice,
         FlaxBigBirdForQuestionAnswering,
         FlaxBigBirdForSequenceClassification,
         FlaxBigBirdForTokenClassification,
     )
     if is_flax_available()
     else ()
 )

 UpperCAmelCase__													=		False
 UpperCAmelCase__													=		False
 def      A_	(      self  :      int	)			->	Any:
     lowerCamelCase__    :      Optional[Any]					 =		FlaxBigBirdModelTester(self	)
 @slow
 # copied from `test_modeling_flax_common` because it takes much longer than other models
 def      A_	(      self  :      List[str]	)			->	int:
     super().test_from_pretrained_save_pretrained()
 @slow
 # copied from `test_modeling_flax_common` because it takes much longer than other models
 def      A_	(      self  :      Union[str, Any]	)			->	Dict:
     super().test_from_pretrained_with_no_automatic_init()
 @slow
 # copied from `test_modeling_flax_common` because it takes much longer than other models
 def      A_	(      self  :      str	)			->	List[Any]:
     super().test_no_automatic_init()
 @slow
 # copied from `test_modeling_flax_common` because it takes much longer than other models
 def      A_	(      self  :      str	)			->	int:
     super().test_hidden_states_output()
 @slow
 def      A_	(      self  :      List[Any]	)			->	Optional[Any]:
     for model_class_name in self.all_model_classes:
         lowerCamelCase__    :      str					 =		model_class_name.from_pretrained('google/bigbird-roberta-base'	)
         self.assertIsNotNone(UpperCAmelCase	)
 def      A_	(      self  :      Tuple	)			->	Dict:
     if self.test_attn_probs:
         super().test_attention_outputs()
 @slow
 # copied from `test_modeling_flax_common` because it takes much longer than other models
 def      A_	(      self  :      Union[str, Any]	)			->	Dict:
     lowerCamelCase__ ,							lowerCamelCase__    :      Dict					 =		self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         with self.subTest(model_class.__name__	):
             lowerCamelCase__    :      str					 =		self._prepare_for_class(UpperCAmelCase			,     UpperCAmelCase	)
             lowerCamelCase__    :      List[Any]					 =		model_class(UpperCAmelCase	)

             @jax.jit
             def model_jitted(UpperCAmelCase  :      Dict			,     UpperCAmelCase  :      int=None			,     **UpperCAmelCase  :      str	):
                 return model(input_ids=UpperCAmelCase			,     attention_mask=UpperCAmelCase			,     **UpperCAmelCase	)

             with self.subTest('JIT Enabled'	):
                 lowerCamelCase__    :      Dict					 =		model_jitted(**UpperCAmelCase	).to_tuple()

             with self.subTest('JIT Disabled'	):
                 with jax.disable_jit():
                     lowerCamelCase__    :      Dict					 =		model_jitted(**UpperCAmelCase	).to_tuple()

             self.assertEqual(len(UpperCAmelCase	)			,     len(UpperCAmelCase	)	)
             for jitted_output, output in zip(UpperCAmelCase			,     UpperCAmelCase	):
                 self.assertEqual(jitted_output.shape			,     output.shape	)







 def      A_	(      self  :      str			,     UpperCAmelCase  :      Tuple			,     UpperCAmelCase  :      List[str]			,     UpperCAmelCase  :      Optional[Any]			,     UpperCAmelCase  :      Tuple=1e-5			,     UpperCAmelCase  :      List[Any]="outputs"			,     UpperCAmelCase  :      Any=None	)			->	Optional[int]:
     # `bigbird_block_sparse_attention` in `FlaxBigBird` returns `attention_probs = None`, while in PyTorch version,
     # an effort was done to return `attention_probs` (yet to be verified).
     if name.startswith('outputs.attentions'	):
         return
     else:
         super().check_pt_flax_outputs(UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase			,     UpperCAmelCase	)












from typing import TYPE_CHECKING

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


UpperCAmelCase						:   Any			  =     {
    '''configuration_falcon''': ['''FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''FalconConfig'''],
}

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCAmelCase						:   Any			  =     [
				    '''FALCON_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''FalconForCausalLM''',
				    '''FalconModel''',
				    '''FalconPreTrainedModel''',
				    '''FalconForSequenceClassification''',
				    '''FalconForTokenClassification''',
				    '''FalconForQuestionAnswering''',
				]


if TYPE_CHECKING:
				from .configuration_falcon import FALCON_PRETRAINED_CONFIG_ARCHIVE_MAP, FalconConfig

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_falcon import (
								    FALCON_PRETRAINED_MODEL_ARCHIVE_LIST,
								    FalconForCausalLM,
								    FalconForQuestionAnswering,
								    FalconForSequenceClassification,
								    FalconForTokenClassification,
								    FalconModel,
								    FalconPreTrainedModel,
								)


else:
				import sys

				UpperCAmelCase						:   List[str]			  =     _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)




