'''simple docstring'''

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     ,       __lowerCAmelCase     ,       __lowerCAmelCase=13     ,       __lowerCAmelCase=7     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=True     ,       __lowerCAmelCase=99     ,       __lowerCAmelCase=32     ,       __lowerCAmelCase=5     ,       __lowerCAmelCase=4     ,       __lowerCAmelCase=37     ,       __lowerCAmelCase="gelu"     ,       __lowerCAmelCase=0.1     ,       __lowerCAmelCase=0.1     ,       __lowerCAmelCase=512     ,       __lowerCAmelCase=16     ,       __lowerCAmelCase=2     ,       __lowerCAmelCase=0.0_2     ,       __lowerCAmelCase=4     ,       )  ->  List[str]:
        lowercase__    :   List[str]						= parent
        lowercase__    :   int						= batch_size
        lowercase__    :   List[str]						= seq_length
        lowercase__    :   Tuple						= is_training
        lowercase__    :   List[Any]						= use_attention_mask
        lowercase__    :   List[Any]						= use_token_type_ids
        lowercase__    :   List[Any]						= use_labels
        lowercase__    :   Tuple						= vocab_size
        lowercase__    :   Optional[int]						= hidden_size
        lowercase__    :   List[Any]						= num_hidden_layers
        lowercase__    :   Any						= num_attention_heads
        lowercase__    :   Dict						= intermediate_size
        lowercase__    :   Union[str, Any]						= hidden_act
        lowercase__    :   List[Any]						= hidden_dropout_prob
        lowercase__    :   Dict						= attention_probs_dropout_prob
        lowercase__    :   Union[str, Any]						= max_position_embeddings
        lowercase__    :   List[str]						= type_vocab_size
        lowercase__    :   Dict						= type_sequence_label_size
        lowercase__    :   Optional[int]						= initializer_range
        lowercase__    :   List[Any]						= num_choices

       def _lowerCAmelCase(						self			)  ->  Any:
        lowercase__    :   Optional[Any]						= ids_tensor([self.batch_size, self.seq_length]     ,       self.vocab_size			)

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

        lowercase__    :   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_=__lowerCAmelCase     ,       )

        return config, input_ids, attention_mask


       def _lowerCAmelCase(						self			)  ->  Union[str, Any]:
        lowercase__    :   Dict						= self.prepare_config_and_inputs()
        lowercase__			,							lowercase__			,							lowercase__    :   Any						= config_and_inputs
        lowercase__    :   str						= {'''input_ids''': input_ids, '''attention_mask''': attention_mask}
        return config, inputs_dict



@require_flax
class       UpperCAmelCase  (						a__   ,						unittest.TestCase ):





       '''simple docstring'''



       SCREAMING_SNAKE_CASE 		=   (
           (
               FlaxDistilBertModel,
               FlaxDistilBertForMaskedLM,
               FlaxDistilBertForMultipleChoice,
               FlaxDistilBertForQuestionAnswering,
               FlaxDistilBertForSequenceClassification,
               FlaxDistilBertForTokenClassification,
               FlaxDistilBertForQuestionAnswering,
           )
           if is_flax_available()
           else ()
       )

       def _lowerCAmelCase(						self			)  ->  int:
        lowercase__    :   List[str]						= FlaxDistilBertModelTester(self			)


       @slow
       def _lowerCAmelCase(						self			)  ->  List[str]:
        for model_class_name in self.all_model_classes:
         lowercase__    :   Dict						= model_class_name.from_pretrained('''distilbert-base-uncased'''			)
         lowercase__    :   Dict						= model(np.ones((1, 1)			)			)
         self.assertIsNotNone(__lowerCAmelCase			)



@require_flax
class       UpperCAmelCase  (						unittest.TestCase ):





       '''simple docstring'''



       @slow
       def _lowerCAmelCase(						self			)  ->  Any:
        lowercase__    :   List[Any]						= FlaxDistilBertModel.from_pretrained('''distilbert-base-uncased'''			)
        lowercase__    :   Optional[Any]						= np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]			)
        lowercase__    :   Dict						= np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
        lowercase__    :   Any						= model(__lowerCAmelCase     ,       attention_mask=__lowerCAmelCase			)[0]
        lowercase__    :   Optional[Any]						= (1, 11, 768)
        self.assertEqual(output.shape     ,       __lowerCAmelCase			)
        lowercase__    :   Dict						= 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]     ,       __lowerCAmelCase     ,       atol=1E-4			)			)


'''simple docstring'''

import numpy as np
def       __UpperCamelCase	(       UpperCAmelCase					):
 return 1 / (1 + np.exp(-vector					))
def       __UpperCamelCase	(       UpperCAmelCase					):
 return vector * sigmoid(UpperCAmelCase					)


if __name__ == "__main__":
  import doctest

  doctest.testmod()

