



from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers.models.distilbert.modeling_tf_distilbert import (
       TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
       TFDistilBertForMaskedLM,
       TFDistilBertForMultipleChoice,
       TFDistilBertForQuestionAnswering,
       TFDistilBertForSequenceClassification,
       TFDistilBertForTokenClassification,
       TFDistilBertModel,
   )


class 	lowerCamelCase__	:



 """simple docstring"""

 def __init__(    self					:					Any			,						__lowerCAmelCase					:					List[Any]			,						)   ->	Tuple:
    _A      =       parent
    _A      =       13
    _A      =       7
    _A      =       True
    _A      =       True
    _A      =       False
    _A      =       True
    _A      =       99
    _A      =       32
    _A      =       2
    _A      =       4
    _A      =       37
    _A      =       '''gelu'''
    _A      =       0.1
    _A      =       0.1
    _A      =       5_12
    _A      =       16
    _A      =       2
    _A      =       0.02
    _A      =       3
    _A      =       4
    _A      =       None

 def     snake_case_      (    self					:					str      )   ->	Optional[int]:
    _A      =       ids_tensor([self.batch_size, self.seq_length]			,						self.vocab_size      )

    _A      =       None
    if self.use_input_mask:
       _A      =       random_attention_mask([self.batch_size, self.seq_length]      )

    _A      =       None
    _A      =       None
    _A      =       None
    if self.use_labels:
       _A      =       ids_tensor([self.batch_size]			,						self.type_sequence_label_size      )
       _A      =       ids_tensor([self.batch_size, self.seq_length]			,						self.num_labels      )
       _A      =       ids_tensor([self.batch_size]			,						self.num_choices      )

    _A      =       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			,						)

    return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels

 def     snake_case_      (    self					:					str			,						__lowerCAmelCase					:					Union[str, Any]			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					Tuple			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					Union[str, Any]      )   ->	Any:
    _A      =       TFDistilBertModel(config=__lowerCAmelCase      )
    _A      =       {'''input_ids''': input_ids, '''attention_mask''': input_mask}

    _A      =       model(__lowerCAmelCase      )

    _A      =       [input_ids, input_mask]

    _A      =       model(__lowerCAmelCase      )

    self.parent.assertEqual(result.last_hidden_state.shape			,						(self.batch_size, self.seq_length, self.hidden_size)      )

 def     snake_case_      (    self					:					Optional[int]			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					Union[str, Any]			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					Any      )   ->	Union[str, Any]:
    _A      =       TFDistilBertForMaskedLM(config=__lowerCAmelCase      )
    _A      =       {'''input_ids''': input_ids, '''attention_mask''': input_mask}
    _A      =       model(__lowerCAmelCase      )
    self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.seq_length, self.vocab_size)      )

 def     snake_case_      (    self					:					Union[str, Any]			,						__lowerCAmelCase					:					Tuple			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					Dict			,						__lowerCAmelCase					:					str			,						__lowerCAmelCase					:					Any      )   ->	Optional[Any]:
    _A      =       TFDistilBertForQuestionAnswering(config=__lowerCAmelCase      )
    _A      =       {
        '''input_ids''': input_ids,
        '''attention_mask''': input_mask,
    }
    _A      =       model(__lowerCAmelCase      )
    self.parent.assertEqual(result.start_logits.shape			,						(self.batch_size, self.seq_length)      )
    self.parent.assertEqual(result.end_logits.shape			,						(self.batch_size, self.seq_length)      )

 def     snake_case_      (    self					:					str			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					Dict			,						__lowerCAmelCase					:					str			,						__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					Tuple      )   ->	Tuple:
    _A      =       self.num_labels
    _A      =       TFDistilBertForSequenceClassification(__lowerCAmelCase      )
    _A      =       {'''input_ids''': input_ids, '''attention_mask''': input_mask}
    _A      =       model(__lowerCAmelCase      )
    self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_labels)      )

 def     snake_case_      (    self					:					str			,						__lowerCAmelCase					:					Optional[int]			,						__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					Tuple			,						__lowerCAmelCase					:					int      )   ->	str:
    _A      =       self.num_choices
    _A      =       TFDistilBertForMultipleChoice(__lowerCAmelCase      )
    _A      =       tf.tile(tf.expand_dims(__lowerCAmelCase			,						1      )			,						(1, self.num_choices, 1)      )
    _A      =       tf.tile(tf.expand_dims(__lowerCAmelCase			,						1      )			,						(1, self.num_choices, 1)      )
    _A      =       {
        '''input_ids''': multiple_choice_inputs_ids,
        '''attention_mask''': multiple_choice_input_mask,
    }
    _A      =       model(__lowerCAmelCase      )
    self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_choices)      )

 def     snake_case_      (    self					:					Tuple			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					Optional[int]			,						__lowerCAmelCase					:					Dict			,						__lowerCAmelCase					:					List[Any]      )   ->	Dict:
    _A      =       self.num_labels
    _A      =       TFDistilBertForTokenClassification(__lowerCAmelCase      )
    _A      =       {'''input_ids''': input_ids, '''attention_mask''': input_mask}
    _A      =       model(__lowerCAmelCase      )
    self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.seq_length, self.num_labels)      )

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


@require_tf
class 	lowerCamelCase__	(       _A							,  _A							,  unittest.TestCase):



 """simple docstring"""

 a__					: Optional[int] =  (
     (
         TFDistilBertModel,
         TFDistilBertForMaskedLM,
         TFDistilBertForQuestionAnswering,
         TFDistilBertForSequenceClassification,
         TFDistilBertForTokenClassification,
         TFDistilBertForMultipleChoice,
     )
     if is_tf_available()
     else None
 )
 a__					: Dict =  (
     {
         "feature-extraction": TFDistilBertModel,
         "fill-mask": TFDistilBertForMaskedLM,
         "question-answering": TFDistilBertForQuestionAnswering,
         "text-classification": TFDistilBertForSequenceClassification,
         "token-classification": TFDistilBertForTokenClassification,
         "zero-shot": TFDistilBertForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 a__					: str =  False
 a__					: Optional[Any] =  False

 def     snake_case_      (    self					:					int      )   ->	Optional[int]:
    _A      =       TFDistilBertModelTester(self      )
    _A      =       ConfigTester(self			,						config_class=__lowerCAmelCase			,						dim=37      )

 def     snake_case_      (    self					:					Tuple      )   ->	List[str]:
    self.config_tester.run_common_tests()

 def     snake_case_      (    self					:					Optional[Any]      )   ->	Tuple:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_model(*__lowerCAmelCase      )

 def     snake_case_      (    self					:					Union[str, Any]      )   ->	List[Any]:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_masked_lm(*__lowerCAmelCase      )

 def     snake_case_      (    self					:					Optional[Any]      )   ->	Optional[int]:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_question_answering(*__lowerCAmelCase      )

 def     snake_case_      (    self					:					Optional[Any]      )   ->	Tuple:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_sequence_classification(*__lowerCAmelCase      )

 def     snake_case_      (    self					:					Union[str, Any]      )   ->	Optional[int]:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_multiple_choice(*__lowerCAmelCase      )

 def     snake_case_      (    self					:					Union[str, Any]      )   ->	Optional[int]:
    _A      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_distilbert_for_token_classification(*__lowerCAmelCase      )

 @slow
 def     snake_case_      (    self					:					List[str]      )   ->	List[str]:
    for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]      ):
       _A      =       TFDistilBertModel.from_pretrained(__lowerCAmelCase      )
       self.assertIsNotNone(__lowerCAmelCase      )


@require_tf
class 	lowerCamelCase__	(       unittest.TestCase):



 """simple docstring"""

 @slow
 def     snake_case_      (    self					:					Dict      )   ->	Union[str, Any]:
    _A      =       TFDistilBertModel.from_pretrained('''distilbert-base-uncased'''      )
    _A      =       tf.constant([[0, 1, 2, 3, 4, 5]]      )
    _A      =       model(__lowerCAmelCase      )[0]

    _A      =       [1, 6, 7_68]
    self.assertEqual(output.shape			,						__lowerCAmelCase      )

    _A      =       tf.constant(
        [
            [
                [0.1926_1885, -0.1373_2955, 0.411_9799],
                [0.2215_0156, -0.0742_2661, 0.3903_7204],
                [0.2275_6018, -0.089_6414, 0.370_1467],
            ]
        ]      )
    tf.debugging.assert_near(output[:, :3, :3]			,						__lowerCAmelCase			,						atol=1E-4      )








from graphs.minimum_spanning_tree_kruskal import kruskal

def 	SCREAMING_SNAKE_CASE_							(	)  ->     Tuple:
   _A      =       9
   _A      =       [
       [0, 1, 4],
       [0, 7, 8],
       [1, 2, 8],
       [7, 8, 7],
       [7, 6, 1],
       [2, 8, 2],
       [8, 6, 6],
       [2, 3, 7],
       [2, 5, 4],
       [6, 5, 2],
       [3, 5, 14],
       [3, 4, 9],
       [5, 4, 10],
       [1, 7, 11],
   ]

   _A      =       kruskal(_snake_case	,   _snake_case				)

   _A      =       [
       [7, 6, 1],
       [2, 8, 2],
       [6, 5, 2],
       [0, 1, 4],
       [2, 5, 4],
       [2, 3, 7],
       [0, 7, 8],
       [3, 4, 9],
   ]

   assert sorted(_snake_case				) == sorted(_snake_case				)



