




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      ,snake_case__      ,snake_case__=2      ,snake_case__=56      ,snake_case__=True      ,snake_case__=True      ,snake_case__=True      ,snake_case__=True      ,snake_case__=99      ,snake_case__=32      ,snake_case__=2      ,snake_case__=2      ,snake_case__=7      ,snake_case__="gelu_new"      ,snake_case__=0.1      ,snake_case__=0.1      ,snake_case__=512      ,snake_case__=16      ,snake_case__=2      ,snake_case__=0.02      ,snake_case__=4      ,snake_case__="block_sparse"      ,snake_case__=True      ,snake_case__=False      ,snake_case__=2      ,snake_case__=3      ,):
							SCREAMING_SNAKE_CASE_   : Tuple					  =      parent
							SCREAMING_SNAKE_CASE_   : int					  =      batch_size
							SCREAMING_SNAKE_CASE_   : str					  =      seq_length
							SCREAMING_SNAKE_CASE_   : Tuple					  =      is_training
							SCREAMING_SNAKE_CASE_   : int					  =      use_attention_mask
							SCREAMING_SNAKE_CASE_   : int					  =      use_token_type_ids
							SCREAMING_SNAKE_CASE_   : int					  =      use_labels
							SCREAMING_SNAKE_CASE_   : str					  =      vocab_size
							SCREAMING_SNAKE_CASE_   : List[str]					  =      hidden_size
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      num_hidden_layers
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      num_attention_heads
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      intermediate_size
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      hidden_act
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      hidden_dropout_prob
							SCREAMING_SNAKE_CASE_   : str					  =      attention_probs_dropout_prob
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      max_position_embeddings
							SCREAMING_SNAKE_CASE_   : int					  =      type_vocab_size
							SCREAMING_SNAKE_CASE_   : Tuple					  =      type_sequence_label_size
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      initializer_range
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      num_choices

							SCREAMING_SNAKE_CASE_   : Any					  =      rescale_embeddings
							SCREAMING_SNAKE_CASE_   : int					  =      attention_type
							SCREAMING_SNAKE_CASE_   : Any					  =      use_bias
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      block_size
							SCREAMING_SNAKE_CASE_   : Dict					  =      num_random_blocks






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      ids_tensor([self.batch_size, self.seq_length]      ,self.vocab_size )

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

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

							SCREAMING_SNAKE_CASE_   : 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=snake_case__      ,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 ):
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      self.prepare_config_and_inputs()
							SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : List[str]					  =      config_and_inputs
							SCREAMING_SNAKE_CASE_   : int					  =      {
							    'input_ids': input_ids,
							    'token_type_ids': token_type_ids,
							    'attention_mask': attention_mask,
							}
							return config, inputs_dict







@require_flax
class 				lowerCAmelCase_       (					lowerCamelCase_						,     unittest.TestCase							):
				__a     :       int						=     (
				    (
				        FlaxBigBirdForCausalLM,
				        FlaxBigBirdModel,
				        FlaxBigBirdForPreTraining,
				        FlaxBigBirdForMaskedLM,
				        FlaxBigBirdForMultipleChoice,
				        FlaxBigBirdForQuestionAnswering,
				        FlaxBigBirdForSequenceClassification,
				        FlaxBigBirdForTokenClassification,
				    )
				    if is_flax_available()
				    else ()
				)

				__a     :       List[Any]						=     False
				__a     :       Any						=     False






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Dict					  =      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:
										SCREAMING_SNAKE_CASE_   : Dict					  =      model_class_name.from_pretrained('google/bigbird-roberta-base' )
										self.assertIsNotNone(snake_case__ )






				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 ):
							SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : 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__ ):
													SCREAMING_SNAKE_CASE_   : str					  =      self._prepare_for_class(snake_case__      ,snake_case__ )
													SCREAMING_SNAKE_CASE_   : Dict					  =      model_class(snake_case__ )

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

													with self.subTest('JIT Enabled' ):
																SCREAMING_SNAKE_CASE_   : Optional[int]					  =      model_jitted(**snake_case__ ).to_tuple()

													with self.subTest('JIT Disabled' ):
																with jax.disable_jit():
																			SCREAMING_SNAKE_CASE_   : Any					  =      model_jitted(**snake_case__ ).to_tuple()

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






				def 		snake_case	(			self      ,snake_case__      ,snake_case__      ,snake_case__      ,snake_case__=1E-5      ,snake_case__="outputs"      ,snake_case__=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(snake_case__      ,snake_case__      ,snake_case__      ,snake_case__      ,snake_case__      ,snake_case__ )









import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import PoolFormerImageProcessor

class     __a(	unittest.TestCase  ):






    """simple docstring"""





    def __init__( self  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE=7  ,_SCREAMING_SNAKE_CASE=3  ,_SCREAMING_SNAKE_CASE=30  ,_SCREAMING_SNAKE_CASE=400  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=None  ,_SCREAMING_SNAKE_CASE=0.9  ,_SCREAMING_SNAKE_CASE=None  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]  ,_SCREAMING_SNAKE_CASE=[0.5, 0.5, 0.5]  ,)					->						Optional[int]:
         UpperCAmelCase_     :				int  =     size if size is not None else {'''shortest_edge''': 30}
         UpperCAmelCase_     :				List[str]  =     crop_size if crop_size is not None else {'''height''': 30, '''width''': 30}
         UpperCAmelCase_     :				Dict  =     parent
         UpperCAmelCase_     :				int  =     batch_size
         UpperCAmelCase_     :				int  =     num_channels
         UpperCAmelCase_     :				Any  =     min_resolution
         UpperCAmelCase_     :				Tuple  =     max_resolution
         UpperCAmelCase_     :				Optional[int]  =     do_resize_and_center_crop
         UpperCAmelCase_     :				Tuple  =     size
         UpperCAmelCase_     :				List[str]  =     crop_pct
         UpperCAmelCase_     :				List[str]  =     crop_size
         UpperCAmelCase_     :				Any  =     do_normalize
         UpperCAmelCase_     :				str  =     image_mean
         UpperCAmelCase_     :				List[Any]  =     image_std





    def 				a__ ( self    )					->						str:
         return {
             "size": self.size,
             "do_resize_and_center_crop": self.do_resize_and_center_crop,
             "crop_pct": self.crop_pct,
             "crop_size": self.crop_size,
             "do_normalize": self.do_normalize,
             "image_mean": self.image_mean,
             "image_std": self.image_std,
         }


@require_torch
@require_vision
class     __a(	_a							,		unittest.TestCase  ):






    """simple docstring"""





    lowerCAmelCase         = PoolFormerImageProcessor if is_vision_available() else None





    def 				a__ ( self    )					->						Dict:
         UpperCAmelCase_     :				str  =     PoolFormerImageProcessingTester(self    )





    @property
    def 				a__ ( self    )					->						Optional[int]:
         return self.image_processor_tester.prepare_image_processor_dict()





    def 				a__ ( self    )					->						Optional[int]:
         UpperCAmelCase_     :				List[Any]  =     self.image_processing_class(**self.image_processor_dict    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''do_resize_and_center_crop'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''size'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''crop_pct'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''do_normalize'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''image_mean'''    )    )
         self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE  ,'''image_std'''    )    )





    def 				a__ ( self    )					->						Union[str, Any]:
         UpperCAmelCase_     :				Any  =     self.image_processing_class.from_dict(self.image_processor_dict    )
         self.assertEqual(image_processor.size  ,{'''shortest_edge''': 30}    )
         self.assertEqual(image_processor.crop_size  ,{'''height''': 30, '''width''': 30}    )

         UpperCAmelCase_     :				Optional[Any]  =     self.image_processing_class.from_dict(self.image_processor_dict  ,size=42  ,crop_size=84    )
         self.assertEqual(image_processor.size  ,{'''shortest_edge''': 42}    )
         self.assertEqual(image_processor.crop_size  ,{'''height''': 84, '''width''': 84}    )





    def 				a__ ( self    )					->						Optional[int]:
         pass





    def 				a__ ( self    )					->						Dict:
         # Initialize image_processing
         UpperCAmelCase_     :				str  =     self.image_processing_class(**self.image_processor_dict    )
         # create random PIL images
         UpperCAmelCase_     :				int  =     prepare_image_inputs(self.image_processor_tester  ,equal_resolution=_SCREAMING_SNAKE_CASE    )
         for image in image_inputs:
              self.assertIsInstance(_SCREAMING_SNAKE_CASE  ,Image.Image    )

         # Test not batched input
         UpperCAmelCase_     :				Optional[int]  =     image_processing(image_inputs[0]  ,return_tensors='''pt'''    ).pixel_values

         self.assertEqual(
             encoded_images.shape  ,(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )  ,)

         # Test batched
         UpperCAmelCase_     :				Union[str, Any]  =     image_processing(_SCREAMING_SNAKE_CASE  ,return_tensors='''pt'''    ).pixel_values
         self.assertEqual(
             encoded_images.shape  ,(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )  ,)





    def 				a__ ( self    )					->						List[Any]:
         # Initialize image_processing
         UpperCAmelCase_     :				Any  =     self.image_processing_class(**self.image_processor_dict    )
         # create random numpy tensors
         UpperCAmelCase_     :				List[str]  =     prepare_image_inputs(self.image_processor_tester  ,equal_resolution=_SCREAMING_SNAKE_CASE  ,numpify=_SCREAMING_SNAKE_CASE    )
         for image in image_inputs:
              self.assertIsInstance(_SCREAMING_SNAKE_CASE  ,np.ndarray    )

         # Test not batched input
         UpperCAmelCase_     :				Tuple  =     image_processing(image_inputs[0]  ,return_tensors='''pt'''    ).pixel_values
         self.assertEqual(
             encoded_images.shape  ,(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )  ,)

         # Test batched
         UpperCAmelCase_     :				List[Any]  =     image_processing(_SCREAMING_SNAKE_CASE  ,return_tensors='''pt'''    ).pixel_values
         self.assertEqual(
             encoded_images.shape  ,(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )  ,)





    def 				a__ ( self    )					->						Union[str, Any]:
         # Initialize image_processing
         UpperCAmelCase_     :				List[Any]  =     self.image_processing_class(**self.image_processor_dict    )
         # create random PyTorch tensors
         UpperCAmelCase_     :				Optional[Any]  =     prepare_image_inputs(self.image_processor_tester  ,equal_resolution=_SCREAMING_SNAKE_CASE  ,torchify=_SCREAMING_SNAKE_CASE    )
         for image in image_inputs:
              self.assertIsInstance(_SCREAMING_SNAKE_CASE  ,torch.Tensor    )

         # Test not batched input
         UpperCAmelCase_     :				Tuple  =     image_processing(image_inputs[0]  ,return_tensors='''pt'''    ).pixel_values
         self.assertEqual(
             encoded_images.shape  ,(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )  ,)

         # Test batched
         UpperCAmelCase_     :				List[Any]  =     image_processing(_SCREAMING_SNAKE_CASE  ,return_tensors='''pt'''    ).pixel_values
         self.assertEqual(
             encoded_images.shape  ,(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )  ,)