





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 ):

     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,       ):





            """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

            _UpperCamelCase              =						rescale_embeddings
            _UpperCamelCase              =						attention_type
            _UpperCamelCase              =						use_bias
            _UpperCamelCase              =						block_size
            _UpperCamelCase              =						num_random_blocks

     def 	_UpperCamelCase     (      self      ):





            """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              =						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 	_UpperCamelCase     (      self      ):





            """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 ):
     __A	  =       (
         (
             FlaxBigBirdForCausalLM,
             FlaxBigBirdModel,
             FlaxBigBirdForPreTraining,
             FlaxBigBirdForMaskedLM,
             FlaxBigBirdForMultipleChoice,
             FlaxBigBirdForQuestionAnswering,
             FlaxBigBirdForSequenceClassification,
             FlaxBigBirdForTokenClassification,
         )
         if is_flax_available()
         else ()
     )

     __A	  =       False
     __A	  =       False

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						FlaxBigBirdModelTester(self      )

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





            """simple docstring"""

            super().test_from_pretrained_save_pretrained()

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





            """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 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            super().test_no_automatic_init()

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





            """simple docstring"""

            super().test_hidden_states_output()

     @slow
     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            for model_class_name in self.all_model_classes:
                   _UpperCamelCase              =						model_class_name.from_pretrained('''google/bigbird-roberta-base'''      )
                   self.assertIsNotNone(A      )

     def 	_UpperCamelCase     (      self      ):





            """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 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase       ,		_UpperCamelCase              =						self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                   with self.subTest(model_class.__name__      ):
                          _UpperCamelCase              =						self._prepare_for_class(A,       A      )
                          _UpperCamelCase              =						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'''      ):
                                 _UpperCamelCase              =						model_jitted(**A      ).to_tuple()

                          with self.subTest('''JIT Disabled'''      ):
                                 with jax.disable_jit():
                                        _UpperCamelCase              =						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 	_UpperCamelCase     (      self,       A,       A,       A,       A=1E-5,       A="outputs",       A=None      ):





            """simple docstring"""

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













import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
       import torch

@is_pipeline_test
class  __A(						unittest.TestCase ):
     __A	  =       MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
     __A	  =       TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

     def 	_UpperCamelCase     (      self,       A,       A,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						TextaTextGenerationPipeline(model=A,       tokenizer=A      )
            return generator, ["Something to write", "Something else"]

     def 	_UpperCamelCase     (      self,       A,       A      ):





            """simple docstring"""

            _UpperCamelCase              =						generator('''Something there'''      )
            self.assertEqual(A,       [{'''generated_text''': ANY(A      )}]      )
            # These are encoder decoder, they don't just append to incoming string
            self.assertFalse(outputs[0]['''generated_text'''].startswith('''Something there'''      )      )

            _UpperCamelCase              =						generator(['''This is great !''', '''Something else'''],       num_return_sequences=2,       do_sample=A      )
            self.assertEqual(
                A,       [
                    [{'''generated_text''': ANY(A      )}, {'''generated_text''': ANY(A      )}],
                    [{'''generated_text''': ANY(A      )}, {'''generated_text''': ANY(A      )}],
                ],       )

            _UpperCamelCase              =						generator(
                ['''This is great !''', '''Something else'''],       num_return_sequences=2,       batch_size=2,       do_sample=A      )
            self.assertEqual(
                A,       [
                    [{'''generated_text''': ANY(A      )}, {'''generated_text''': ANY(A      )}],
                    [{'''generated_text''': ANY(A      )}, {'''generated_text''': ANY(A      )}],
                ],       )

            with self.assertRaises(A      ):
                   generator(4      )

     @require_torch
     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						pipeline('''text2text-generation''',       model='''patrickvonplaten/t5-tiny-random''',       framework='''pt'''      )
            # do_sample=False necessary for reproducibility
            _UpperCamelCase              =						generator('''Something there''',       do_sample=A      )
            self.assertEqual(A,       [{'''generated_text''': ''''''}]      )

            _UpperCamelCase              =						3
            _UpperCamelCase              =						generator(
                '''Something there''',       num_return_sequences=A,       num_beams=A,       )
            _UpperCamelCase              =						[
                {'''generated_text''': '''Beide Beide Beide Beide Beide Beide Beide Beide Beide'''},
                {'''generated_text''': '''Beide Beide Beide Beide Beide Beide Beide Beide'''},
                {'''generated_text''': ''''''},
            ]
            self.assertEqual(A,       A      )

            _UpperCamelCase              =						generator('''This is a test''',       do_sample=A,       num_return_sequences=2,       return_tensors=A      )
            self.assertEqual(
                A,       [
                    {'''generated_token_ids''': ANY(torch.Tensor      )},
                    {'''generated_token_ids''': ANY(torch.Tensor      )},
                ],       )
            _UpperCamelCase              =						generator.model.config.eos_token_id
            _UpperCamelCase              =						'''<pad>'''
            _UpperCamelCase              =						generator(
                ['''This is a test''', '''This is a second test'''],       do_sample=A,       num_return_sequences=2,       batch_size=2,       return_tensors=A,       )
            self.assertEqual(
                A,       [
                    [
                        {'''generated_token_ids''': ANY(torch.Tensor      )},
                        {'''generated_token_ids''': ANY(torch.Tensor      )},
                    ],
                    [
                        {'''generated_token_ids''': ANY(torch.Tensor      )},
                        {'''generated_token_ids''': ANY(torch.Tensor      )},
                    ],
                ],       )



     @require_tf
     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						pipeline('''text2text-generation''',       model='''patrickvonplaten/t5-tiny-random''',       framework='''tf'''      )
            # do_sample=False necessary for reproducibility
            _UpperCamelCase              =						generator('''Something there''',       do_sample=A      )
            self.assertEqual(A,       [{'''generated_text''': ''''''}]      )






