

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





    def __init__(      self						,   a						,   a=2						,   a=56						,   a=True						,   a=True						,   a=True						,   a=True						,   a=99						,   a=32						,   a=2						,   a=2						,   a=7						,   a="gelu_new"						,   a=0.1						,   a=0.1						,   a=512						,   a=16						,   a=2						,   a=0.02						,   a=4						,   a="block_sparse"						,   a=True						,   a=False						,   a=2						,   a=3						,   ):
         lowercase__    :   Union[str, Any]      =       parent
         lowercase__    :   Tuple      =       batch_size
         lowercase__    :   str      =       seq_length
         lowercase__    :   Optional[Any]      =       is_training
         lowercase__    :   Tuple      =       use_attention_mask
         lowercase__    :   Optional[Any]      =       use_token_type_ids
         lowercase__    :   List[Any]      =       use_labels
         lowercase__    :   List[Any]      =       vocab_size
         lowercase__    :   Dict      =       hidden_size
         lowercase__    :   Any      =       num_hidden_layers
         lowercase__    :   Tuple      =       num_attention_heads
         lowercase__    :   Any      =       intermediate_size
         lowercase__    :   Optional[Any]      =       hidden_act
         lowercase__    :   List[Any]      =       hidden_dropout_prob
         lowercase__    :   int      =       attention_probs_dropout_prob
         lowercase__    :   Optional[int]      =       max_position_embeddings
         lowercase__    :   Optional[Any]      =       type_vocab_size
         lowercase__    :   Union[str, Any]      =       type_sequence_label_size
         lowercase__    :   List[Any]      =       initializer_range
         lowercase__    :   Optional[int]      =       num_choices

         lowercase__    :   List[Any]      =       rescale_embeddings
         lowercase__    :   List[Any]      =       attention_type
         lowercase__    :   Optional[int]      =       use_bias
         lowercase__    :   Tuple      =       block_size
         lowercase__    :   int      =       num_random_blocks





    def       snake_case_				(      self):
         lowercase__    :   Optional[int]      =       ids_tensor([self.batch_size, self.seq_length]						,   self.vocab_size)

         lowercase__    :   Dict      =       None
         if self.use_attention_mask:
              lowercase__    :   Union[str, Any]      =       random_attention_mask([self.batch_size, self.seq_length])

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

         lowercase__    :   Optional[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=a						,   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       snake_case_				(      self):
         lowercase__    :   Optional[int]      =       self.prepare_config_and_inputs()
         lowercase__      , lowercase__      , lowercase__      , lowercase__    :   Optional[Any]      =       config_and_inputs
         lowercase__    :   Tuple      =       {
             'input_ids': input_ids,
             'token_type_ids': token_type_ids,
             'attention_mask': attention_mask,
         }
         return config, inputs_dict



@require_flax
class       SCREAMING_SNAKE_CASE__					(__snake_case		,       unittest.TestCase     ):
    __lowerCamelCase						:       Dict														=     (
        (
            FlaxBigBirdForCausalLM,
            FlaxBigBirdModel,
            FlaxBigBirdForPreTraining,
            FlaxBigBirdForMaskedLM,
            FlaxBigBirdForMultipleChoice,
            FlaxBigBirdForQuestionAnswering,
            FlaxBigBirdForSequenceClassification,
            FlaxBigBirdForTokenClassification,
        )
        if is_flax_available()
        else ()
    )

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





    def       snake_case_				(      self):
         lowercase__    :   Optional[Any]      =       FlaxBigBirdModelTester(self)





    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def       snake_case_				(      self):
         super().test_from_pretrained_save_pretrained()





    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def       snake_case_				(      self):
         super().test_from_pretrained_with_no_automatic_init()





    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def       snake_case_				(      self):
         super().test_no_automatic_init()





    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def       snake_case_				(      self):
         super().test_hidden_states_output()





    @slow
    def       snake_case_				(      self):
         for model_class_name in self.all_model_classes:
              lowercase__    :   str      =       model_class_name.from_pretrained('google/bigbird-roberta-base')
              self.assertIsNotNone(a)





    def       snake_case_				(      self):
         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       snake_case_				(      self):
         lowercase__      , lowercase__    :   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__):
                   lowercase__    :   List[str]      =       self._prepare_for_class(a						,   a)
                   lowercase__    :   int      =       model_class(a)

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

                   with self.subTest('JIT Enabled'):
                        lowercase__    :   Optional[Any]      =       model_jitted(**a).to_tuple()

                   with self.subTest('JIT Disabled'):
                        with jax.disable_jit():
                             lowercase__    :   Optional[Any]      =       model_jitted(**a).to_tuple()

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





    def       snake_case_				(      self						,   a						,   a						,   a						,   a=1e-5						,   a="outputs"						,   a=None):
         # `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(a						,   a						,   a						,   a						,   a						,   a)




# flake8: noqa
# Lint as: python3

from typing import Dict, List, Optional, Type

from .. import config
from ..utils import logging
from .formatting import (
    ArrowFormatter,
    CustomFormatter,
    Formatter,
    PandasFormatter,
    PythonFormatter,
    TensorFormatter,
    format_table,
    query_table,
)
from .np_formatter import NumpyFormatter


snake_case_			  = logging.get_logger(__name__)

snake_case_			  = {}
snake_case_			  = {}
snake_case_			  = {}

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    type	,     SCREAMING_SNAKE_CASE_		:    Optional[str]	,     SCREAMING_SNAKE_CASE_		:    Optional[List[str]] = None	,     ):



     '''simple docstring'''





     lowercase__    :   Union[str, Any]      =       aliases if aliases is not None else []
     if format_type in _FORMAT_TYPES:
          logger.warning(
              f"""Overwriting format type '{format_type}' ({_FORMAT_TYPES[format_type].__name__} -> {formatter_cls.__name__})"""			)
     lowercase__    :   Any      =       formatter_cls
     for alias in set(aliases + [format_type]			):
          if alias in _FORMAT_TYPES_ALIASES:
               logger.warning(
                   f"""Overwriting format type alias '{alias}' ({_FORMAT_TYPES_ALIASES[alias]} -> {format_type})"""			)
          lowercase__    :   int      =       format_type

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    Exception	,     SCREAMING_SNAKE_CASE_		:    Optional[str]	,     SCREAMING_SNAKE_CASE_		:    Optional[List[str]] = None			):



     '''simple docstring'''





     lowercase__    :   Optional[Any]      =       aliases if aliases is not None else []
     for alias in set(aliases + [format_type]			):
          lowercase__    :   Any      =       unavailable_error


# Here we define all the available formatting functions that can be used by `Dataset.set_format`
_register_formatter(PythonFormatter, None, aliases=['''python'''])
_register_formatter(ArrowFormatter, '''arrow''', aliases=['''pa''', '''pyarrow'''])
_register_formatter(NumpyFormatter, '''numpy''', aliases=['''np'''])
_register_formatter(PandasFormatter, '''pandas''', aliases=['''pd'''])
_register_formatter(CustomFormatter, '''custom''')

if config.TORCH_AVAILABLE:
       from .torch_formatter import TorchFormatter

       _register_formatter(TorchFormatter, '''torch''', aliases=['''pt''', '''pytorch'''])
else:
       snake_case_			  = ValueError('''PyTorch needs to be installed to be able to return PyTorch tensors.''')
       _register_unavailable_formatter(_torch_error, '''torch''', aliases=['''pt''', '''pytorch'''])

if config.TF_AVAILABLE:
       from .tf_formatter import TFFormatter

       _register_formatter(TFFormatter, '''tensorflow''', aliases=['''tf'''])
else:
       snake_case_			  = ValueError('''Tensorflow needs to be installed to be able to return Tensorflow tensors.''')
       _register_unavailable_formatter(_tf_error, '''tensorflow''', aliases=['''tf'''])

if config.JAX_AVAILABLE:
       from .jax_formatter import JaxFormatter

       _register_formatter(JaxFormatter, '''jax''', aliases=[])
else:
       snake_case_			  = ValueError('''JAX needs to be installed to be able to return JAX arrays.''')
       _register_unavailable_formatter(_jax_error, '''jax''', aliases=[])

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    Optional[str]			):



     '''simple docstring'''





     if format_type in _FORMAT_TYPES_ALIASES:
          return _FORMAT_TYPES_ALIASES[format_type]
     else:
          return format_type

def 	snake_case__			(    SCREAMING_SNAKE_CASE_		:    Optional[str]	,     **SCREAMING_SNAKE_CASE_		:    Any			):



     '''simple docstring'''





     lowercase__    :   Tuple      =       get_format_type_from_alias(SCREAMING_SNAKE_CASE_			)
     if format_type in _FORMAT_TYPES:
          return _FORMAT_TYPES[format_type](**SCREAMING_SNAKE_CASE_			)
     if format_type in _FORMAT_TYPES_ALIASES_UNAVAILABLE:
          raise _FORMAT_TYPES_ALIASES_UNAVAILABLE[format_type]
     else:
          raise ValueError(
              f"""Return type should be None or selected in {list(type for type in _FORMAT_TYPES.keys() if type != None			)}, but got '{format_type}'"""			)

