





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




   """simple docstring"""



   def __init__(					self					,  lowercase					,  lowercase=2					,  lowercase=56					,  lowercase=True					,  lowercase=True					,  lowercase=True					,  lowercase=True					,  lowercase=99					,  lowercase=32					,  lowercase=2					,  lowercase=2					,  lowercase=7					,  lowercase="gelu_new"					,  lowercase=0.1					,  lowercase=0.1					,  lowercase=512					,  lowercase=16					,  lowercase=2					,  lowercase=0.02					,  lowercase=4					,  lowercase="block_sparse"					,  lowercase=True					,  lowercase=False					,  lowercase=2					,  lowercase=3					,  )			->						Any:







          '''simple docstring'''

          a__						:	List[str] =				parent
          a__						:	Optional[Any] =				batch_size
          a__						:	Tuple =				seq_length
          a__						:	Optional[Any] =				is_training
          a__						:	Optional[Any] =				use_attention_mask
          a__						:	Any =				use_token_type_ids
          a__						:	Union[str, Any] =				use_labels
          a__						:	Union[str, Any] =				vocab_size
          a__						:	Dict =				hidden_size
          a__						:	Union[str, Any] =				num_hidden_layers
          a__						:	Optional[int] =				num_attention_heads
          a__						:	Optional[int] =				intermediate_size
          a__						:	Dict =				hidden_act
          a__						:	Optional[Any] =				hidden_dropout_prob
          a__						:	int =				attention_probs_dropout_prob
          a__						:	Tuple =				max_position_embeddings
          a__						:	Tuple =				type_vocab_size
          a__						:	Tuple =				type_sequence_label_size
          a__						:	Union[str, Any] =				initializer_range
          a__						:	List[Any] =				num_choices

          a__						:	List[str] =				rescale_embeddings
          a__						:	str =				attention_type
          a__						:	Dict =				use_bias
          a__						:	Union[str, Any] =				block_size
          a__						:	Dict =				num_random_blocks

   def 							__lowercase		(					self)			->						Any:







          '''simple docstring'''

          a__						:	int =				ids_tensor([self.batch_size, self.seq_length]					,  self.vocab_size)

          a__						:	int =				None
          if self.use_attention_mask:
                 a__						:	Any =				random_attention_mask([self.batch_size, self.seq_length])

          a__						:	Any =				None
          if self.use_token_type_ids:
                 a__						:	Optional[int] =				ids_tensor([self.batch_size, self.seq_length]					,  self.type_vocab_size)

          a__						:	int =				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=lowercase					,  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 							__lowercase		(					self)			->						int:







          '''simple docstring'''

          a__						:	Optional[int] =				self.prepare_config_and_inputs()
          a__ ,							a__ ,							a__ ,							a__						:	Optional[Any] =				config_and_inputs
          a__						:	Tuple =				{
              '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	):




   """simple docstring"""



   __A   :				Optional[Any]         =  (
       (
           FlaxBigBirdForCausalLM,
           FlaxBigBirdModel,
           FlaxBigBirdForPreTraining,
           FlaxBigBirdForMaskedLM,
           FlaxBigBirdForMultipleChoice,
           FlaxBigBirdForQuestionAnswering,
           FlaxBigBirdForSequenceClassification,
           FlaxBigBirdForTokenClassification,
       )
       if is_flax_available()
       else ()
   )

   __A   :				List[Any]         =  False
   __A   :				int         =  False

   def 							__lowercase		(					self)			->						int:







          '''simple docstring'''

          a__						:	Any =				FlaxBigBirdModelTester(self)

   @slow
   # copied from `test_modeling_flax_common` because it takes much longer than other models
   def 							__lowercase		(					self)			->						List[Any]:







          '''simple docstring'''

          super().test_from_pretrained_save_pretrained()

   @slow
   # copied from `test_modeling_flax_common` because it takes much longer than other models
   def 							__lowercase		(					self)			->						Dict:







          '''simple docstring'''

          super().test_from_pretrained_with_no_automatic_init()

   @slow
   # copied from `test_modeling_flax_common` because it takes much longer than other models
   def 							__lowercase		(					self)			->						Dict:







          '''simple docstring'''

          super().test_no_automatic_init()

   @slow
   # copied from `test_modeling_flax_common` because it takes much longer than other models
   def 							__lowercase		(					self)			->						Dict:







          '''simple docstring'''

          super().test_hidden_states_output()

   @slow
   def 							__lowercase		(					self)			->						List[str]:







          '''simple docstring'''

          for model_class_name in self.all_model_classes:
                 a__						:	Optional[int] =				model_class_name.from_pretrained('google/bigbird-roberta-base')
                 self.assertIsNotNone(lowercase)

   def 							__lowercase		(					self)			->						Union[str, Any]:







          '''simple docstring'''

          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 							__lowercase		(					self)			->						Optional[Any]:







          '''simple docstring'''

          a__ ,							a__						:	List[str] =				self.model_tester.prepare_config_and_inputs_for_common()

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

                        @jax.jit
                        def model_jitted(lowercase					,  lowercase=None					,  **lowercase):
                               return model(input_ids=lowercase					,  attention_mask=lowercase					,  **lowercase)

                        with self.subTest('JIT Enabled'):
                               a__						:	str =				model_jitted(**lowercase).to_tuple()

                        with self.subTest('JIT Disabled'):
                               with jax.disable_jit():
                                      a__						:	int =				model_jitted(**lowercase).to_tuple()

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

   def 							__lowercase		(					self					,  lowercase					,  lowercase					,  lowercase					,  lowercase=1e-5					,  lowercase="outputs"					,  lowercase=None)			->						Optional[int]:







          '''simple docstring'''

          if name.startswith('outputs.attentions'):
                 return
          else:
                 super().check_pt_flax_outputs(lowercase					,  lowercase					,  lowercase					,  lowercase					,  lowercase					,  lowercase)









from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_torch_available():
    import torch

if is_torch_tpu_available(check_device=False):
    import torch_xla.core.xla_model as xm


lowercase      :       List[str]									=  logging.get_logger(__name__)





@dataclass
class  A__    (							__UpperCAmelCase	):




   """simple docstring"""



   __A   :				Optional[Any]         =  [
       '''no_inference''',
       '''no_cuda''',
       '''no_tpu''',
       '''no_speed''',
       '''no_memory''',
       '''no_env_print''',
       '''no_multi_process''',
   ]

   def __init__(					self					,  **lowercase)			->						Tuple:







          '''simple docstring'''

          for deprecated_arg in self.deprecated_args:
                 if deprecated_arg in kwargs:
                        a__						:	Union[str, Any] =				deprecated_arg[3:]
                        setattr(self					,  lowercase					,  not kwargs.pop(lowercase))
                        logger.warning(
                            F'{deprecated_arg} is depreciated. Please use --no_{positive_arg} or'
                            F' {positive_arg}={kwargs[positive_arg]}')

          a__						:	Union[str, Any] =				kwargs.pop('torchscript'					,  self.torchscript)
          a__						:	Tuple =				kwargs.pop('torch_xla_tpu_print_metrics'					,  self.torch_xla_tpu_print_metrics)
          a__						:	Tuple =				kwargs.pop('fp16_opt_level'					,  self.fpaa_opt_level)
          super().__init__(**lowercase)

   __A   :				bool         =  field(default=__UpperCAmelCase ,  metadata={'''help''': '''Trace the models using torchscript'''}	)
   __A   :				bool         =  field(default=__UpperCAmelCase ,  metadata={'''help''': '''Print Xla/PyTorch tpu metrics'''}	)
   __A   :				str         =  field(
       default='''O1''' ,  metadata={
           '''help''': (
               '''For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\']. '''
               '''See details at https://nvidia.github.io/apex/amp.html'''
           )
       } ,  )

   @cached_property
   def 							__lowercase		(					self)			->						Tuple["torch.device", int]:







          '''simple docstring'''

          requires_backends(self					,  ['torch'])
          logger.info('PyTorch: setting up devices')
          if not self.cuda:
                 a__						:	List[str] =				torch.device('cpu')
                 a__						:	Optional[Any] =				0
          elif is_torch_tpu_available():
                 a__						:	List[str] =				xm.xla_device()
                 a__						:	Union[str, Any] =				0
          else:
                 a__						:	List[Any] =				torch.device('cuda' if torch.cuda.is_available() else 'cpu')
                 a__						:	Optional[int] =				torch.cuda.device_count()
          return device, n_gpu

   @property
   def 							__lowercase		(					self)			->						List[str]:







          '''simple docstring'''

          return is_torch_tpu_available() and self.tpu

   @property
   def 							__lowercase		(					self)			->						int:







          '''simple docstring'''

          requires_backends(self					,  ['torch'])
          # TODO(PVP): currently only single GPU is supported
          return torch.cuda.current_device()

   @property
   def 							__lowercase		(					self)			->						"torch.device":







          '''simple docstring'''

          requires_backends(self					,  ['torch'])
          return self._setup_devices[0]

   @property
   def 							__lowercase		(					self)			->						Dict:







          '''simple docstring'''

          requires_backends(self					,  ['torch'])
          return self._setup_devices[1]

   @property
   def 							__lowercase		(					self)			->						Optional[int]:







          '''simple docstring'''

          return self.n_gpu > 0


