






import unittest

import numpy as np

from transformers import DistilBertConfig, 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.numpy as jnp

							from transformers.models.distilbert.modeling_flax_distilbert import (
							    FlaxDistilBertForMaskedLM,
							    FlaxDistilBertForMultipleChoice,
							    FlaxDistilBertForQuestionAnswering,
							    FlaxDistilBertForSequenceClassification,
							    FlaxDistilBertForTokenClassification,
							    FlaxDistilBertModel,
							)

class        _UpperCamelCase				(					unittest.TestCase  ):






			'''simple docstring'''

			def __init__(       self :       Union[str, Any]						,	snake_case_ :       Optional[Any]						,	snake_case_ :       Tuple=13						,	snake_case_ :       Optional[Any]=7						,	snake_case_ :       Optional[Any]=True						,	snake_case_ :       Tuple=True						,	snake_case_ :       List[Any]=True						,	snake_case_ :       Any=True						,	snake_case_ :       Optional[int]=99						,	snake_case_ :       int=32						,	snake_case_ :       List[str]=5						,	snake_case_ :       Optional[Any]=4						,	snake_case_ :       List[Any]=37						,	snake_case_ :       Union[str, Any]="gelu"						,	snake_case_ :       List[Any]=0.1						,	snake_case_ :       Optional[Any]=0.1						,	snake_case_ :       Tuple=512						,	snake_case_ :       Optional[Any]=16						,	snake_case_ :       Optional[int]=2						,	snake_case_ :       int=0.02						,	snake_case_ :       Dict=4						,	):
							UpperCamelCase_:							Optional[Any]	=						parent
							UpperCamelCase_:							List[Any]	=						batch_size
							UpperCamelCase_:							str	=						seq_length
							UpperCamelCase_:							Any	=						is_training
							UpperCamelCase_:							Optional[Any]	=						use_attention_mask
							UpperCamelCase_:							Any	=						use_token_type_ids
							UpperCamelCase_:							Optional[Any]	=						use_labels
							UpperCamelCase_:							Any	=						vocab_size
							UpperCamelCase_:							Optional[Any]	=						hidden_size
							UpperCamelCase_:							Union[str, Any]	=						num_hidden_layers
							UpperCamelCase_:							int	=						num_attention_heads
							UpperCamelCase_:							Dict	=						intermediate_size
							UpperCamelCase_:							Union[str, Any]	=						hidden_act
							UpperCamelCase_:							Optional[int]	=						hidden_dropout_prob
							UpperCamelCase_:							int	=						attention_probs_dropout_prob
							UpperCamelCase_:							Dict	=						max_position_embeddings
							UpperCamelCase_:							Optional[int]	=						type_vocab_size
							UpperCamelCase_:							Union[str, Any]	=						type_sequence_label_size
							UpperCamelCase_:							Union[str, Any]	=						initializer_range
							UpperCamelCase_:							List[str]	=						num_choices

			def       lowerCAmelCase__	(       self :       Dict      ):
							UpperCamelCase_:							Any	=						ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size      )

							UpperCamelCase_:							str	=						None
							if self.use_attention_mask:
											UpperCamelCase_:							List[str]	=						random_attention_mask([self.batch_size, self.seq_length]      )

							UpperCamelCase_:							List[str]	=						DistilBertConfig(
							    vocab_size=self.vocab_size						,	dim=self.hidden_size						,	n_layers=self.num_hidden_layers						,	n_heads=self.num_attention_heads						,	hidden_dim=self.intermediate_size						,	hidden_act=self.hidden_act						,	dropout=self.hidden_dropout_prob						,	attention_dropout=self.attention_probs_dropout_prob						,	max_position_embeddings=self.max_position_embeddings						,	initializer_range=self.initializer_range						,	tie_weights_=__UpperCamelCase						,	)

							return config, input_ids, attention_mask




			def       lowerCAmelCase__	(       self :       Tuple      ):
							UpperCamelCase_:							List[Any]	=						self.prepare_config_and_inputs()
							UpperCamelCase_,   UpperCamelCase_,   UpperCamelCase_:							Union[str, Any]	=						config_and_inputs
							UpperCamelCase_:							Any	=						{"""input_ids""": input_ids, """attention_mask""": attention_mask}
							return config, inputs_dict




@require_flax
class        _UpperCamelCase				(					_A     ,       unittest.TestCase  ):






			'''simple docstring'''

			__UpperCamelCase      :						Tuple					      = (
			    (
			        FlaxDistilBertModel,
			        FlaxDistilBertForMaskedLM,
			        FlaxDistilBertForMultipleChoice,
			        FlaxDistilBertForQuestionAnswering,
			        FlaxDistilBertForSequenceClassification,
			        FlaxDistilBertForTokenClassification,
			        FlaxDistilBertForQuestionAnswering,
			    )
			    if is_flax_available()
			    else ()
			)

			def       lowerCAmelCase__	(       self :       Optional[int]      ):
							UpperCamelCase_:							Optional[int]	=						FlaxDistilBertModelTester(self      )




			@slow
			def       lowerCAmelCase__	(       self :       Any      ):
							for model_class_name in self.all_model_classes:
											UpperCamelCase_:							int	=						model_class_name.from_pretrained("""distilbert-base-uncased"""      )
											UpperCamelCase_:							Dict	=						model(np.ones((1, 1)      )      )
											self.assertIsNotNone(__UpperCamelCase      )




@require_flax
class        _UpperCamelCase				(					unittest.TestCase  ):






			'''simple docstring'''

			@slow
			def       lowerCAmelCase__	(       self :       Dict      ):
							UpperCamelCase_:							Optional[int]	=						FlaxDistilBertModel.from_pretrained("""distilbert-base-uncased"""      )
							UpperCamelCase_:							Any	=						np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]      )
							UpperCamelCase_:							Tuple	=						np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
							UpperCamelCase_:							List[Any]	=						model(__UpperCamelCase						,	attention_mask=__UpperCamelCase      )[0]
							UpperCamelCase_:							Union[str, Any]	=						(1, 11, 768)
							self.assertEqual(output.shape						,	__UpperCamelCase      )
							UpperCamelCase_:							Tuple	=						np.array([[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]      )

							self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]						,	__UpperCamelCase						,	atol=1e-4      )      )








import inspect
import os
import sys
import unittest

import accelerate
from accelerate.test_utils import execute_subprocess_async, require_tpu

class        _UpperCamelCase				(					unittest.TestCase  ):






			'''simple docstring'''

			def       lowerCAmelCase__	(       self :       Optional[int]      ):
							UpperCamelCase_:							List[Any]	=						inspect.getfile(accelerate.test_utils      )
							UpperCamelCase_:							List[str]	=						os.path.sep.join(mod_file.split(os.path.sep      )[:-1] + ["""scripts""", """test_script.py"""]      )
							UpperCamelCase_:							str	=						os.path.sep.join(inspect.getfile(self.__class__      ).split(os.path.sep      )[:-1]      )




			@require_tpu
			def       lowerCAmelCase__	(       self :       Optional[int]      ):
							UpperCamelCase_:							Any	=						f'''
            {self.test_dir}/xla_spawn.py
            --num_cores 8
            {self.test_file_path}
        '''.split()
							UpperCamelCase_:							Dict	=						[sys.executable] + distributed_args
							execute_subprocess_async(snake_case_						,	env=os.environ.copy()      )






