


'''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    __snake_case       (   unittest.TestCase):







    """simple docstring"""



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

    def __lowercase   (   self    :  Optional[Any]		)   ->  List[str]:
      lowerCAmelCase_			:      List[Any]			=   ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size		)

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

      lowerCAmelCase_			:      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_=lowerCamelCase    ,						)

      return config, input_ids, attention_mask





    def __lowercase   (   self    :  Optional[int]		)   ->  Optional[int]:
      lowerCAmelCase_			:      Union[str, Any]			=   self.prepare_config_and_inputs()
      lowerCAmelCase_,							lowerCAmelCase_,							lowerCAmelCase_			:      Union[str, Any]			=   config_and_inputs
      lowerCAmelCase_			:      List[Any]			=   {"""input_ids""": input_ids, """attention_mask""": attention_mask}
      return config, inputs_dict







@require_flax
class    __snake_case       (   _SCREAMING_SNAKE_CASE	,unittest.TestCase):







    """simple docstring"""



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

    def __lowercase   (   self    :  List[Any]		)   ->  Optional[Any]:
      lowerCAmelCase_			:      Optional[Any]			=   FlaxDistilBertModelTester(self		)





    @slow
    def __lowercase   (   self    :  List[Any]		)   ->  Union[str, Any]:
      for model_class_name in self.all_model_classes:
        lowerCAmelCase_			:      Tuple			=   model_class_name.from_pretrained("""distilbert-base-uncased"""		)
        lowerCAmelCase_			:      int			=   model(np.ones((1, 1)		)		)
        self.assertIsNotNone(lowerCamelCase		)







@require_flax
class    __snake_case       (   unittest.TestCase):







    """simple docstring"""



    @slow
    def __lowercase   (   self    :  Any		)   ->  Optional[int]:
      lowerCAmelCase_			:      int			=   FlaxDistilBertModel.from_pretrained("""distilbert-base-uncased"""		)
      lowerCAmelCase_			:      Optional[Any]			=   np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]		)
      lowerCAmelCase_			:      Dict			=   np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
      lowerCAmelCase_			:      Any			=   model(lowerCamelCase    ,						attention_mask=lowerCamelCase		)[0]
      lowerCAmelCase_			:      List[Any]			=   (1, 11, 7_68)
      self.assertEqual(output.shape    ,						lowerCamelCase		)
      lowerCAmelCase_			:      Tuple			=   np.array([[[-0.1_639, 0.3_299, 0.1_648], [-0.1_746, 0.3_289, 0.1_710], [-0.1_884, 0.3_357, 0.1_810]]]		)

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







'''simple docstring'''






from __future__ import annotations

from typing import Any






class    __snake_case       (   _SCREAMING_SNAKE_CASE):







    """simple docstring"""



    pass







class    __snake_case       :







    """simple docstring"""



    def __init__(   self    :  Union[str, Any]    ,						lowerCamelCase    :  Any		)   ->  None:
      lowerCAmelCase_			:      Any			=   data
      lowerCAmelCase_			:      Node | None			=   None

    def __iter__(   self    :  Union[str, Any]		)   ->  Optional[Any]:
      lowerCAmelCase_			:      Union[str, Any]			=   self
      lowerCAmelCase_			:      Any			=   []
      while node:
        if node in visited:
          raise ContainsLoopError
        visited.append(lowerCamelCase		)
        yield node.data
        lowerCAmelCase_			:      int			=   node.next_node





    @property
    def __lowercase   (   self    :  str		)   ->  bool:
      try:
        list(self		)
        return False
      except ContainsLoopError:
        return True


if __name__ == "__main__":
  __A					:  Dict											=      Node(1)
  __A					:  Optional[Any]											=      Node(2)
  __A					:  int											=      Node(3)
  __A					:  Optional[Any]											=      Node(4)
  print(root_node.has_loop)  # False
  __A					:  Any											=      root_node.next_node
  print(root_node.has_loop)  # True

  __A					:  List[Any]											=      Node(5)
  __A					:  Dict											=      Node(6)
  __A					:  str											=      Node(5)
  __A					:  Dict											=      Node(6)
  print(root_node.has_loop)  # False

  __A					:  Optional[int]											=      Node(1)
  print(root_node.has_loop)  # False



