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  SCREAMING_SNAKE_CASE_ (					unittest.TestCase			):
  '''simple docstring'''


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







  def 		SCREAMING_SNAKE_CASE_		(				self    :					Dict     )    ->				Any:
         A						:     Optional[int]         =ids_tensor([self.batch_size, self.seq_length]      ,	self.vocab_size     )

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

         A						:     Union[str, Any]         =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_=SCREAMING_SNAKE_CASE__      ,	)

         return config, input_ids, attention_mask







  def 		SCREAMING_SNAKE_CASE_		(				self    :					Any     )    ->				int:
         A						:     int         =self.prepare_config_and_inputs()
         A  ,  A  ,  A						:     Optional[Any]         =config_and_inputs
         A						:     List[Any]         ={'input_ids': input_ids, 'attention_mask': attention_mask}
         return config, inputs_dict







@require_flax
class  SCREAMING_SNAKE_CASE_ (					lowerCAmelCase_   ,						unittest.TestCase			):
  '''simple docstring'''


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







  def 		SCREAMING_SNAKE_CASE_		(				self    :					Dict     )    ->				Optional[int]:
         A						:     Optional[Any]         =FlaxDistilBertModelTester(self     )







  @slow
  def 		SCREAMING_SNAKE_CASE_		(				self    :					Any     )    ->				Optional[Any]:
         for model_class_name in self.all_model_classes:
                A						:     int         =model_class_name.from_pretrained('distilbert-base-uncased'     )
                A						:     Optional[int]         =model(np.ones((1, 1)     )     )
                self.assertIsNotNone(SCREAMING_SNAKE_CASE__     )







@require_flax
class  SCREAMING_SNAKE_CASE_ (					unittest.TestCase			):
  '''simple docstring'''


  @slow
  def 		SCREAMING_SNAKE_CASE_		(				self    :					Any     )    ->				List[str]:
         A						:     Any         =FlaxDistilBertModel.from_pretrained('distilbert-base-uncased'     )
         A						:     List[str]         =np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]     )
         A						:     Dict         =np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
         A						:     Union[str, Any]         =model(SCREAMING_SNAKE_CASE__      ,	attention_mask=SCREAMING_SNAKE_CASE__     )[0]
         A						:     Optional[int]         =(1, 11, 7_68)
         self.assertEqual(output.shape      ,	SCREAMING_SNAKE_CASE__     )
         A						:     str         =np.array([[[-0.1_6_3_9, 0.3_2_9_9, 0.1_6_4_8], [-0.1_7_4_6, 0.3_2_8_9, 0.1_7_1_0], [-0.1_8_8_4, 0.3_3_5_7, 0.1_8_1_0]]]     )

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







_lowercase							:      Dict      							='''0.21.0'''

from .accelerator import Accelerator
from .big_modeling import (
    cpu_offload,
    cpu_offload_with_hook,
    disk_offload,
    dispatch_model,
    init_empty_weights,
    init_on_device,
    load_checkpoint_and_dispatch,
)
from .data_loader import skip_first_batches
from .launchers import debug_launcher, notebook_launcher
from .state import PartialState
from .utils import (
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    FullyShardedDataParallelPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    find_executable_batch_size,
    infer_auto_device_map,
    is_rich_available,
    load_checkpoint_in_model,
    synchronize_rng_states,
)


if is_rich_available():
    from .utils import rich






