


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]   ,     __A						:   Union[str, Any]   ,     __A						:   Optional[Any]=2   ,     __A						:   int=56   ,     __A						:   str=True   ,     __A						:   int=True   ,     __A						:   List[Any]=True   ,     __A						:   Dict=True   ,     __A						:   str=99   ,     __A						:   Optional[int]=32   ,     __A						:   List[str]=2   ,     __A						:   Dict=2   ,     __A						:   Tuple=7   ,     __A						:   List[str]="gelu_new"   ,     __A						:   Optional[Any]=0.1   ,     __A						:   Any=0.1   ,     __A						:   Union[str, Any]=512   ,     __A						:   Any=16   ,     __A						:   Union[str, Any]=2   ,     __A						:   Dict=0.02   ,     __A						:   Tuple=4   ,     __A						:   int="block_sparse"   ,     __A						:   Dict=True   ,     __A						:   Union[str, Any]=False   ,     __A						:   Optional[Any]=2   ,     __A						:   Any=3   ,     )      ->Tuple:






									"""simple docstring"""






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

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




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






									"""simple docstring"""






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

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

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

									a__ :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=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  _snake_case					(	self						:   List[Any]						)      ->Tuple:






									"""simple docstring"""






									a__ :int						  =    self.prepare_config_and_inputs()
									a__ :Dict						  =    config_and_inputs
									a__ :Union[str, Any]						  =    {
									    """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):
						lowerCamelCase_         =     (
						    (
						        FlaxBigBirdForCausalLM,
						        FlaxBigBirdModel,
						        FlaxBigBirdForPreTraining,
						        FlaxBigBirdForMaskedLM,
						        FlaxBigBirdForMultipleChoice,
						        FlaxBigBirdForQuestionAnswering,
						        FlaxBigBirdForSequenceClassification,
						        FlaxBigBirdForTokenClassification,
						    )
						    if is_flax_available()
						    else ()
						)

						lowerCamelCase_         =     False
						lowerCamelCase_         =     False




						def  _snake_case					(	self						:   Dict						)      ->str:






									"""simple docstring"""






									a__ :List[Any]						  =    FlaxBigBirdModelTester(self						)




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






									"""simple docstring"""






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






									"""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  _snake_case					(	self						:   Tuple						)      ->Any:






									"""simple docstring"""






									super().test_no_automatic_init()




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






									"""simple docstring"""






									super().test_hidden_states_output()




						@slow
						def  _snake_case					(	self						:   List[Any]						)      ->Optional[Any]:






									"""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  _snake_case					(	self						:   Any						)      ->List[str]:






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






									"""simple docstring"""






									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__ :Dict						  =    self._prepare_for_class(lowercase_   ,     lowercase_						)
															a__ :Dict						  =    model_class(lowercase_						)

															@jax.jit
															def model_jitted(__A						:   Optional[Any]   ,     __A						:   Optional[int]=None   ,     **__A						:   Optional[int]						):
																		return model(input_ids=lowercase_   ,     attention_mask=lowercase_   ,     **lowercase_						)

															with self.subTest("JIT Enabled"						):
																		a__ :List[Any]						  =    model_jitted(**lowercase_						).to_tuple()

															with self.subTest("JIT Disabled"						):
																		with jax.disable_jit():
																					a__ :Optional[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  _snake_case					(	self						:   int   ,     __A						:   int   ,     __A						:   Union[str, Any]   ,     __A						:   List[Any]   ,     __A						:   Optional[int]=1E-5   ,     __A						:   Optional[Any]="outputs"   ,     __A						:   Any=None						)      ->Optional[Any]:






									"""simple docstring"""






									if name.startswith("outputs.attentions"						):
												return
									else:
												super().check_pt_flax_outputs(lowercase_   ,     lowercase_   ,     lowercase_   ,     lowercase_   ,     lowercase_   ,     lowercase_						)






# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

from ...utils.dataclasses import (
    ComputeEnvironment,
    DistributedType,
    DynamoBackend,
    PrecisionType,
    SageMakerDistributedType,
)
from ..menu import BulletMenu


snake_case__			 =						[
    '''EAGER''',
    '''AOT_EAGER''',
    '''INDUCTOR''',
    '''NVFUSER''',
    '''AOT_NVFUSER''',
    '''AOT_CUDAGRAPHS''',
    '''OFI''',
    '''FX2TRT''',
    '''ONNXRT''',
    '''IPEX''',
]


def 				lowerCamelCase__	(      a	:   Optional[Any]	,  a	:   Tuple=None	,  a	:   int=None	,  a	:   List[str]=None				)    ->	int:





			"""simple docstring"""
			a__ :Tuple						  =    True
			while ask_again:
						a__ :str						  =    input(a				)
						try:
									if default is not None and len(a				) == 0:
												return default
									return convert_value(a				) if convert_value is not None else result
						except Exception:
									if error_message is not None:
												print(a				)


def 				lowerCamelCase__	(      a	:   int	,  a	:   Union[str, Any]=[]	,  a	:   str=None	,  a	:   str=0				)    ->	List[str]:





			"""simple docstring"""
			a__ :str						  =    BulletMenu(a	,  a				)
			a__ :int						  =    menu.run(default_choice=a				)
			return convert_value(a				) if convert_value is not None else result


def 				lowerCamelCase__	(      a	:   Optional[Any]				)    ->	Dict:





			"""simple docstring"""
			a__ :str						  =    int(a				)
			return ComputeEnvironment(["LOCAL_MACHINE", "AMAZON_SAGEMAKER"][value]				)


def 				lowerCamelCase__	(      a	:   Tuple				)    ->	List[str]:





			"""simple docstring"""
			a__ :Union[str, Any]						  =    int(a				)
			return DistributedType(["NO", "MULTI_CPU", "MULTI_XPU", "MULTI_GPU", "MULTI_NPU", "TPU"][value]				)


def 				lowerCamelCase__	(      a	:   List[Any]				)    ->	str:





			"""simple docstring"""
			a__ :int						  =    int(a				)
			return DynamoBackend(DYNAMO_BACKENDS[value]				).value


def 				lowerCamelCase__	(      a	:   Any				)    ->	Union[str, Any]:





			"""simple docstring"""
			a__ :Optional[Any]						  =    int(a				)
			return PrecisionType(["no", "fp16", "bf16", "fp8"][value]				)


def 				lowerCamelCase__	(      a	:   List[Any]				)    ->	Union[str, Any]:





			"""simple docstring"""
			a__ :Optional[Any]						  =    int(a				)
			return SageMakerDistributedType(["NO", "DATA_PARALLEL", "MODEL_PARALLEL"][value]				)


def 				lowerCamelCase__	(      a	:   Union[str, Any]				)    ->	List[str]:





			"""simple docstring"""
			return {"yes": True, "no": False}[value.lower()]







class 					lowerCAmelCase_ (       argparse.RawDescriptionHelpFormatter):




						def  _snake_case					(	self						:   Tuple   ,     __A						:   Tuple   ,     __A						:   int   ,     __A						:   Tuple   ,     __A						:   Any						)      ->Union[str, Any]:






									"""simple docstring"""






									a__ :Optional[int]						  =    super()._format_usage(__A   ,     __A   ,     __A   ,     __A						)
									a__ :Any						  =    usage.replace("<command> [<args>] "   ,     ""						)
									return usage


