


'''simple docstring'''





from __future__ import annotations

import unittest

from transformers import CTRLConfig, 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.ctrl.modeling_tf_ctrl import (
        TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFCTRLForSequenceClassification,
        TFCTRLLMHeadModel,
        TFCTRLModel,
    )




class 							UpperCAmelCase ( _SCREAMING_SNAKE_CASE	):


 def __init__(			self       :  int ,					__lowerCamelCase       :  str ,					):
    UpperCAmelCase__  :List[Any]            =			parent
    UpperCAmelCase__  :List[str]            =			1_3
    UpperCAmelCase__  :List[str]            =			7
    UpperCAmelCase__  :Dict            =			True
    UpperCAmelCase__  :Tuple            =			True
    UpperCAmelCase__  :Optional[Any]            =			True
    UpperCAmelCase__  :Optional[int]            =			True
    UpperCAmelCase__  :Dict            =			True
    UpperCAmelCase__  :str            =			9_9
    UpperCAmelCase__  :Dict            =			3_2
    UpperCAmelCase__  :Optional[int]            =			2
    UpperCAmelCase__  :List[Any]            =			4
    UpperCAmelCase__  :List[Any]            =			3_7
    UpperCAmelCase__  :int            =			'''gelu'''
    UpperCAmelCase__  :Any            =			0.1
    UpperCAmelCase__  :Optional[int]            =			0.1
    UpperCAmelCase__  :List[Any]            =			5_1_2
    UpperCAmelCase__  :int            =			1_6
    UpperCAmelCase__  :Tuple            =			2
    UpperCAmelCase__  :List[str]            =			0.02
    UpperCAmelCase__  :Tuple            =			3
    UpperCAmelCase__  :Dict            =			4
    UpperCAmelCase__  :Union[str, Any]            =			None
    UpperCAmelCase__  :int            =			self.vocab_size - 1


 def 					__SCREAMING_SNAKE_CASE				(			self       :  str   ):
    UpperCAmelCase__  :Optional[int]            =			ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size   )

    UpperCAmelCase__  :Optional[int]            =			None
    if self.use_input_mask:
       UpperCAmelCase__  :List[Any]            =			random_attention_mask([self.batch_size, self.seq_length]   )

    UpperCAmelCase__  :str            =			None
    if self.use_token_type_ids:
       UpperCAmelCase__  :Any            =			ids_tensor([self.batch_size, self.seq_length] ,					self.type_vocab_size   )

    UpperCAmelCase__  :List[str]            =			None
    if self.use_mc_token_ids:
       UpperCAmelCase__  :Union[str, Any]            =			ids_tensor([self.batch_size, self.num_choices] ,					self.seq_length   )

    UpperCAmelCase__  :Tuple            =			None
    UpperCAmelCase__  :List[Any]            =			None
    UpperCAmelCase__  :Optional[Any]            =			None
    if self.use_labels:
       UpperCAmelCase__  :List[Any]            =			ids_tensor([self.batch_size] ,					self.type_sequence_label_size   )
       UpperCAmelCase__  :Tuple            =			ids_tensor([self.batch_size, self.seq_length] ,					self.num_labels   )
       UpperCAmelCase__  :Tuple            =			ids_tensor([self.batch_size] ,					self.num_choices   )

    UpperCAmelCase__  :Union[str, Any]            =			CTRLConfig(
        vocab_size=self.vocab_size ,					n_embd=self.hidden_size ,					n_layer=self.num_hidden_layers ,					n_head=self.num_attention_heads ,					n_positions=self.max_position_embeddings ,					pad_token_id=self.pad_token_id ,					)

    UpperCAmelCase__  :Optional[Any]            =			ids_tensor([self.num_hidden_layers, self.num_attention_heads] ,					2   )

    return (
        config,
        input_ids,
        input_mask,
        head_mask,
        token_type_ids,
        mc_token_ids,
        sequence_labels,
        token_labels,
        choice_labels,
    )


 def 					__SCREAMING_SNAKE_CASE				(			self       :  List[Any] ,					__lowerCamelCase       :  Union[str, Any] ,					__lowerCamelCase       :  str ,					__lowerCamelCase       :  Optional[Any] ,					__lowerCamelCase       :  Dict ,					__lowerCamelCase       :  str ,					*__lowerCamelCase       :  Optional[int]   ):
    UpperCAmelCase__  :Optional[Any]            =			TFCTRLModel(config=UpperCamelCase__   )
    UpperCAmelCase__  :Optional[Any]            =			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
    UpperCAmelCase__  :Any            =			model(UpperCamelCase__   )

    UpperCAmelCase__  :int            =			[input_ids, None, input_mask]  # None is the input for 'past'
    UpperCAmelCase__  :Optional[int]            =			model(UpperCamelCase__   )

    UpperCAmelCase__  :Any            =			model(UpperCamelCase__   )

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


 def 					__SCREAMING_SNAKE_CASE				(			self       :  Dict ,					__lowerCamelCase       :  Tuple ,					__lowerCamelCase       :  Optional[Any] ,					__lowerCamelCase       :  Tuple ,					__lowerCamelCase       :  Optional[int] ,					__lowerCamelCase       :  Any ,					*__lowerCamelCase       :  Dict   ):
    UpperCAmelCase__  :Union[str, Any]            =			TFCTRLLMHeadModel(config=UpperCamelCase__   )
    UpperCAmelCase__  :List[str]            =			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
    UpperCAmelCase__  :str            =			model(UpperCamelCase__   )
    self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.vocab_size)   )


 def 					__SCREAMING_SNAKE_CASE				(			self       :  List[Any] ,					__lowerCamelCase       :  List[Any] ,					__lowerCamelCase       :  Any ,					__lowerCamelCase       :  Any ,					__lowerCamelCase       :  Tuple ,					__lowerCamelCase       :  Any ,					*__lowerCamelCase       :  Optional[Any]   ):
    UpperCAmelCase__  :Optional[int]            =			self.num_labels
    UpperCAmelCase__  :Union[str, Any]            =			ids_tensor([self.batch_size] ,					self.type_sequence_label_size   )
    UpperCAmelCase__  :Any            =			{
        '''input_ids''': input_ids,
        '''token_type_ids''': token_type_ids,
        '''labels''': sequence_labels,
    }
    UpperCAmelCase__  :Union[str, Any]            =			TFCTRLForSequenceClassification(UpperCamelCase__   )
    UpperCAmelCase__  :List[Any]            =			model(UpperCamelCase__   )
    self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_labels)   )





 def 					__SCREAMING_SNAKE_CASE				(			self       :  Optional[Any]   ):
    UpperCAmelCase__  :List[Any]            =			self.prepare_config_and_inputs()

    (
        (
        UpperCAmelCase__ 
    )      ,	(
        UpperCAmelCase__ 
    )      ,	(
        UpperCAmelCase__ 
    )      ,	(
        UpperCAmelCase__ 
    )      ,	(
        UpperCAmelCase__ 
    )      ,	(
        UpperCAmelCase__ 
    )      ,	(
        UpperCAmelCase__ 
    )      ,	(
        UpperCAmelCase__ 
    )      ,	(
        UpperCAmelCase__ 
    )      ,	 
    )  :List[str]            =			config_and_inputs

    UpperCAmelCase__  :List[str]            =			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
    return config, inputs_dict




@require_tf
class 							UpperCAmelCase ( _SCREAMING_SNAKE_CASE	,   _SCREAMING_SNAKE_CASE	,   unittest.TestCase	):
 UpperCAmelCase				=				(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
 UpperCAmelCase				=				(TFCTRLLMHeadModel,) if is_tf_available() else ()
 UpperCAmelCase				=				(
     {
         'feature-extraction': TFCTRLModel,
         'text-classification': TFCTRLForSequenceClassification,
         'text-generation': TFCTRLLMHeadModel,
         'zero-shot': TFCTRLForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 UpperCAmelCase				=				False
 UpperCAmelCase				=				False


 def 					__SCREAMING_SNAKE_CASE				(			self       :  Tuple ,					__lowerCamelCase       :  Dict ,					__lowerCamelCase       :  Optional[int] ,					__lowerCamelCase       :  str ,					__lowerCamelCase       :  List[Any] ,					__lowerCamelCase       :  int   ):
    if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
       # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
       # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
       # config could not be created.
       return True

    return False


 def 					__SCREAMING_SNAKE_CASE				(			self       :  Optional[Any]   ):
    UpperCAmelCase__  :Optional[Any]            =			TFCTRLModelTester(self   )
    UpperCAmelCase__  :Any            =			ConfigTester(self ,					config_class=UpperCamelCase__ ,					n_embd=3_7   )


 def 					__SCREAMING_SNAKE_CASE				(			self       :  List[str]   ):
    self.config_tester.run_common_tests()


 def 					__SCREAMING_SNAKE_CASE				(			self       :  List[Any]   ):
    UpperCAmelCase__  :Optional[int]            =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_model(*UpperCamelCase__   )


 def 					__SCREAMING_SNAKE_CASE				(			self       :  str   ):
    UpperCAmelCase__  :int            =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_lm_head(*UpperCamelCase__   )


 def 					__SCREAMING_SNAKE_CASE				(			self       :  List[Any]   ):
    UpperCAmelCase__  :int            =			self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_for_sequence_classification(*UpperCamelCase__   )


 def 					__SCREAMING_SNAKE_CASE				(			self       :  Union[str, Any]   ):
    UpperCAmelCase__      ,	UpperCAmelCase__  :Optional[Any]            =			self.model_tester.prepare_config_and_inputs_for_common()
    UpperCAmelCase__  :List[Any]            =			[TFCTRLLMHeadModel]
    UpperCAmelCase__  :Optional[Any]            =			[TFCTRLForSequenceClassification]

    for model_class in self.all_model_classes:
       UpperCAmelCase__  :Any            =			model_class(UpperCamelCase__   )
       model.build()  # may be needed for the get_bias() call below
       assert isinstance(model.get_input_embeddings() ,					tf.keras.layers.Layer   )

       if model_class in list_lm_models:
          UpperCAmelCase__  :Optional[Any]            =			model.get_output_embeddings()
          assert isinstance(UpperCamelCase__ ,					tf.keras.layers.Layer   )
          UpperCAmelCase__  :Tuple            =			model.get_bias()
          assert isinstance(UpperCamelCase__ ,					UpperCamelCase__   )
          for k, v in name.items():
             assert isinstance(UpperCamelCase__ ,					tf.Variable   )
       elif model_class in list_other_models_with_output_ebd:
          UpperCAmelCase__  :str            =			model.get_output_embeddings()
          assert isinstance(UpperCamelCase__ ,					tf.keras.layers.Layer   )
          UpperCAmelCase__  :int            =			model.get_bias()
          assert name is None
       else:
          UpperCAmelCase__  :Tuple            =			model.get_output_embeddings()
          assert x is None
          UpperCAmelCase__  :Union[str, Any]            =			model.get_bias()
          assert name is None





 @slow
 def 					__SCREAMING_SNAKE_CASE				(			self       :  Tuple   ):
    for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase__  :List[str]            =			TFCTRLModel.from_pretrained(UpperCamelCase__   )
       self.assertIsNotNone(UpperCamelCase__   )




@require_tf
class 							UpperCAmelCase ( unittest.TestCase	):


 @slow
 def 					__SCREAMING_SNAKE_CASE				(			self       :  Dict   ):
    UpperCAmelCase__  :Dict            =			TFCTRLLMHeadModel.from_pretrained('''ctrl'''   )
    UpperCAmelCase__  :Tuple            =			tf.convert_to_tensor([[1_1_8_5_9, 0, 1_6_1_1, 8]] ,					dtype=tf.intaa   )  # Legal the president is
    UpperCAmelCase__  :Any            =			[
        1_1_8_5_9,
        0,
        1_6_1_1,
        8,
        5,
        1_5_0,
        2_6_4_4_9,
        2,
        1_9,
        3_4_8,
        4_6_9,
        3,
        2_5_9_5,
        4_8,
        2_0_7_4_0,
        2_4_6_5_3_3,
        2_4_6_5_3_3,
        1_9,
        3_0,
        5,
    ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

    UpperCAmelCase__  :str            =			model.generate(UpperCamelCase__ ,					do_sample=UpperCamelCase__   )
    self.assertListEqual(output_ids[0].numpy().tolist() ,					UpperCamelCase__   )







'''simple docstring'''





import heapq


def 					a__    (				UpperCamelCase_	:			Dict,      UpperCamelCase_	:			Tuple,      UpperCamelCase_	:			Dict	):
   UpperCAmelCase__  :List[Any]            =			[(0, start)]  # cost from start node,end node
   UpperCAmelCase__  :Optional[int]            =			set()
   while heap:
      ((UpperCAmelCase__)      ,	(UpperCAmelCase__))  :Optional[Any]            =			heapq.heappop(UpperCamelCase_	)
      if u in visited:
         continue
      visited.add(UpperCamelCase_	)
      if u == end:
         return cost
      for v, c in graph[u]:
         if v in visited:
            continue
         UpperCAmelCase__  :int            =			cost + c
         heapq.heappush(UpperCamelCase_,      (next_item, v)	)
   return -1


__lowerCamelCase       				=	{
    '''A''': [['''B''', 2], ['''C''', 5]],
    '''B''': [['''A''', 2], ['''D''', 3], ['''E''', 1], ['''F''', 1]],
    '''C''': [['''A''', 5], ['''F''', 3]],
    '''D''': [['''B''', 3]],
    '''E''': [['''B''', 4], ['''F''', 3]],
    '''F''': [['''C''', 3], ['''E''', 3]],
}
__lowerCamelCase       				=	{
    '''B''': [['''C''', 1]],
    '''C''': [['''D''', 1]],
    '''D''': [['''F''', 1]],
    '''E''': [['''B''', 1], ['''F''', 3]],
    '''F''': [],
}
__lowerCamelCase       				=	{
    '''B''': [['''C''', 1]],
    '''C''': [['''D''', 1]],
    '''D''': [['''F''', 1]],
    '''E''': [['''B''', 1], ['''G''', 2]],
    '''F''': [],
    '''G''': [['''F''', 1]],
}

__lowerCamelCase       				=	dijkstra(G, '''E''', '''C''')
print(short_distance)  # E -- 3 --> F -- 3 --> C == 6

__lowerCamelCase       				=	dijkstra(Ga, '''E''', '''F''')
print(short_distance)  # E -- 3 --> F == 3

__lowerCamelCase       				=	dijkstra(Ga, '''E''', '''F''')
print(short_distance)  # E -- 2 --> G -- 1 --> F == 3

if __name__ == "__main__":
    import doctest

    doctest.testmod()



