

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     __snake_case    :
       """simple docstring"""







       def __init__(		self      , _UpperCamelCase      , )      ->							Union[str, Any]:






              """simple docstring"""


              __snake_case       	=							parent
              __snake_case       	=							13
              __snake_case       	=							7
              __snake_case       	=							True
              __snake_case       	=							True
              __snake_case       	=							False
              __snake_case       	=							True
              __snake_case       	=							99
              __snake_case       	=							32
              __snake_case       	=							2
              __snake_case       	=							4
              __snake_case       	=							37
              __snake_case       	=							"""gelu"""
              __snake_case       	=							0.1
              __snake_case       	=							0.1
              __snake_case       	=							5_12
              __snake_case       	=							16
              __snake_case       	=							2
              __snake_case       	=							0.02
              __snake_case       	=							3
              __snake_case       	=							4
              __snake_case       	=							None


       def 			a		(		self   )      ->							int:






              """simple docstring"""


              __snake_case       	=							ids_tensor([self.batch_size, self.seq_length]      , self.vocab_size   )

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

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

              __snake_case       	=							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 			a		(		self      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase   )      ->							Tuple:






              """simple docstring"""


              __snake_case       	=							TFDistilBertModel(config=_UpperCamelCase   )
              __snake_case       	=							{"""input_ids""": input_ids, """attention_mask""": input_mask}

              __snake_case       	=							model(_UpperCamelCase   )

              __snake_case       	=							[input_ids, input_mask]

              __snake_case       	=							model(_UpperCamelCase   )

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


       def 			a		(		self      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase   )      ->							List[str]:






              """simple docstring"""


              __snake_case       	=							TFDistilBertForMaskedLM(config=_UpperCamelCase   )
              __snake_case       	=							{"""input_ids""": input_ids, """attention_mask""": input_mask}
              __snake_case       	=							model(_UpperCamelCase   )
              self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.seq_length, self.vocab_size)   )


       def 			a		(		self      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase   )      ->							List[Any]:






              """simple docstring"""


              __snake_case       	=							TFDistilBertForQuestionAnswering(config=_UpperCamelCase   )
              __snake_case       	=							{
                  """input_ids""": input_ids,
                  """attention_mask""": input_mask,
              }
              __snake_case       	=							model(_UpperCamelCase   )
              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 			a		(		self      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase   )      ->							Tuple:






              """simple docstring"""


              __snake_case       	=							self.num_labels
              __snake_case       	=							TFDistilBertForSequenceClassification(_UpperCamelCase   )
              __snake_case       	=							{"""input_ids""": input_ids, """attention_mask""": input_mask}
              __snake_case       	=							model(_UpperCamelCase   )
              self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.num_labels)   )


       def 			a		(		self      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase   )      ->							Tuple:






              """simple docstring"""


              __snake_case       	=							self.num_choices
              __snake_case       	=							TFDistilBertForMultipleChoice(_UpperCamelCase   )
              __snake_case       	=							tf.tile(tf.expand_dims(_UpperCamelCase      , 1   )      , (1, self.num_choices, 1)   )
              __snake_case       	=							tf.tile(tf.expand_dims(_UpperCamelCase      , 1   )      , (1, self.num_choices, 1)   )
              __snake_case       	=							{
                  """input_ids""": multiple_choice_inputs_ids,
                  """attention_mask""": multiple_choice_input_mask,
              }
              __snake_case       	=							model(_UpperCamelCase   )
              self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.num_choices)   )


       def 			a		(		self      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase      , _UpperCamelCase   )      ->							str:






              """simple docstring"""


              __snake_case       	=							self.num_labels
              __snake_case       	=							TFDistilBertForTokenClassification(_UpperCamelCase   )
              __snake_case       	=							{"""input_ids""": input_ids, """attention_mask""": input_mask}
              __snake_case       	=							model(_UpperCamelCase   )
              self.parent.assertEqual(result.logits.shape      , (self.batch_size, self.seq_length, self.num_labels)   )






       def 			a		(		self   )      ->							Optional[Any]:






              """simple docstring"""


              __snake_case       	=							self.prepare_config_and_inputs()
              ((__snake_case)	,    (__snake_case)	,    (__snake_case)	,    (__snake_case)	,    (__snake_case)	,    (__snake_case))       	=							config_and_inputs
              __snake_case       	=							{"""input_ids""": input_ids, """attention_mask""": input_mask}
              return config, inputs_dict






@require_tf
class     __snake_case    (				snake_case__			,	snake_case__			,	unittest.TestCase  ):
       """simple docstring"""







       __SCREAMING_SNAKE_CASE				    =							(
           (
               TFDistilBertModel,
               TFDistilBertForMaskedLM,
               TFDistilBertForQuestionAnswering,
               TFDistilBertForSequenceClassification,
               TFDistilBertForTokenClassification,
               TFDistilBertForMultipleChoice,
           )
           if is_tf_available()
           else None
       )
       __SCREAMING_SNAKE_CASE				    =							(
           {
               "feature-extraction": TFDistilBertModel,
               "fill-mask": TFDistilBertForMaskedLM,
               "question-answering": TFDistilBertForQuestionAnswering,
               "text-classification": TFDistilBertForSequenceClassification,
               "token-classification": TFDistilBertForTokenClassification,
               "zero-shot": TFDistilBertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       __SCREAMING_SNAKE_CASE				    =							False
       __SCREAMING_SNAKE_CASE				    =							False


       def 			a		(		self   )      ->							Dict:






              """simple docstring"""


              __snake_case       	=							TFDistilBertModelTester(self   )
              __snake_case       	=							ConfigTester(self      , config_class=_UpperCamelCase      , dim=37   )


       def 			a		(		self   )      ->							List[str]:






              """simple docstring"""


              self.config_tester.run_common_tests()


       def 			a		(		self   )      ->							List[Any]:






              """simple docstring"""


              __snake_case       	=							self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_distilbert_model(*_UpperCamelCase   )


       def 			a		(		self   )      ->							List[Any]:






              """simple docstring"""


              __snake_case       	=							self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_distilbert_for_masked_lm(*_UpperCamelCase   )


       def 			a		(		self   )      ->							Optional[int]:






              """simple docstring"""


              __snake_case       	=							self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_distilbert_for_question_answering(*_UpperCamelCase   )


       def 			a		(		self   )      ->							Tuple:






              """simple docstring"""


              __snake_case       	=							self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_distilbert_for_sequence_classification(*_UpperCamelCase   )


       def 			a		(		self   )      ->							Optional[Any]:






              """simple docstring"""


              __snake_case       	=							self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_distilbert_for_multiple_choice(*_UpperCamelCase   )


       def 			a		(		self   )      ->							Dict:






              """simple docstring"""


              __snake_case       	=							self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_distilbert_for_token_classification(*_UpperCamelCase   )






       @slow
       def 			a		(		self   )      ->							Optional[int]:






              """simple docstring"""


              for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]   ):
                     __snake_case       	=							TFDistilBertModel.from_pretrained(_UpperCamelCase   )
                     self.assertIsNotNone(_UpperCamelCase   )






@require_tf
class     __snake_case    (				unittest.TestCase  ):
       """simple docstring"""







       @slow
       def 			a		(		self   )      ->							Dict:






              """simple docstring"""


              __snake_case       	=							TFDistilBertModel.from_pretrained("""distilbert-base-uncased"""   )
              __snake_case       	=							tf.constant([[0, 1, 2, 3, 4, 5]]   )
              __snake_case       	=							model(_UpperCamelCase   )[0]

              __snake_case       	=							[1, 6, 7_68]
              self.assertEqual(output.shape      , _UpperCamelCase   )

              __snake_case       	=							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]      , _UpperCamelCase      , atol=1E-4   )




from __future__ import annotations







def 						lowerCamelCase__      ( __A    :list[float]	,__A    :Union[str, Any]		):


       """simple docstring"""




       print(F'Vertex\tShortest Distance from vertex {src}'		)
       for i, d in enumerate(__A		):
              print(F'{i}\t\t{d}'		)







def 						lowerCamelCase__      ( __A    :list[dict[str, int]]	,__A    :list[float]	,__A    :int		):


       """simple docstring"""




       for j in range(__A		):
              __snake_case	,    __snake_case	,    __snake_case       	=							(graph[j][k] for k in ["""src""", """dst""", """weight"""])
              if distance[u] != float("""inf"""		) and distance[u] + w < distance[v]:
                     return True
       return False







def 						lowerCamelCase__      ( __A    :list[dict[str, int]]	,__A    :int	,__A    :int	,__A    :int		):


       """simple docstring"""




       __snake_case       	=							[float("""inf"""		)] * vertex_count
       __snake_case       	=							0.0

       for _ in range(vertex_count - 1		):
              for j in range(__A		):
                     __snake_case	,    __snake_case	,    __snake_case       	=							(graph[j][k] for k in ["""src""", """dst""", """weight"""])

                     if distance[u] != float("""inf"""		) and distance[u] + w < distance[v]:
                            __snake_case       	=							distance[u] + w

       __snake_case       	=							check_negative_cycle(__A	,__A	,__A		)
       if negative_cycle_exists:
              raise Exception("""Negative cycle found"""		)

       return distance


if __name__ == "__main__":
    import doctest

    doctest.testmod()

    UpperCamelCase__		 =     int(input('''Enter number of vertices: ''').strip())
    UpperCamelCase__		 =     int(input('''Enter number of edges: ''').strip())

    UpperCamelCase__		 =     [{} for _ in range(E)]

    for i in range(E):
        print('''Edge ''', i + 1)
        UpperCamelCase__ ,UpperCamelCase__ ,UpperCamelCase__		 =     (
            int(x)
            for x in input('''Enter source, destination, weight: ''').strip().split(''' ''')
        )
        UpperCamelCase__		 =     {'''src''': src, '''dst''': dest, '''weight''': weight}

    UpperCamelCase__		 =     int(input('''\nEnter shortest path source:''').strip())
    UpperCamelCase__		 =     bellman_ford(graph, V, E, source)
    print_distance(shortest_distance, 0)

