




import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
       CTRLForSequenceClassification,
       CTRLLMHeadModel,
       CTRLModel,
   )





class 			lowerCamelCase_				:


 '''simple docstring'''







 def __init__(				self    :						Optional[Any]							,							_lowerCAmelCase    :						Union[str, Any]							,							_lowerCAmelCase    :						List[Any]=14							,							_lowerCAmelCase    :						int=7							,							_lowerCAmelCase    :						Union[str, Any]=True							,							_lowerCAmelCase    :						Dict=True							,							_lowerCAmelCase    :						Optional[int]=True							,							_lowerCAmelCase    :						Optional[Any]=True							,							_lowerCAmelCase    :						Any=True							,							_lowerCAmelCase    :						List[Any]=99							,							_lowerCAmelCase    :						Dict=32							,							_lowerCAmelCase    :						Optional[int]=5							,							_lowerCAmelCase    :						int=4							,							_lowerCAmelCase    :						Union[str, Any]=37							,							_lowerCAmelCase    :						List[str]="gelu"							,							_lowerCAmelCase    :						Any=0.1							,							_lowerCAmelCase    :						str=0.1							,							_lowerCAmelCase    :						Tuple=512							,							_lowerCAmelCase    :						Dict=16							,							_lowerCAmelCase    :						Any=2							,							_lowerCAmelCase    :						Tuple=0.02							,							_lowerCAmelCase    :						List[Any]=3							,							_lowerCAmelCase    :						Union[str, Any]=4							,							_lowerCAmelCase    :						List[str]=None							,							):
       SCREAMING_SNAKE_CASE_      		=     parent
       SCREAMING_SNAKE_CASE_      		=     batch_size
       SCREAMING_SNAKE_CASE_      		=     seq_length
       SCREAMING_SNAKE_CASE_      		=     is_training
       SCREAMING_SNAKE_CASE_      		=     use_token_type_ids
       SCREAMING_SNAKE_CASE_      		=     use_input_mask
       SCREAMING_SNAKE_CASE_      		=     use_labels
       SCREAMING_SNAKE_CASE_      		=     use_mc_token_ids
       SCREAMING_SNAKE_CASE_      		=     vocab_size
       SCREAMING_SNAKE_CASE_      		=     hidden_size
       SCREAMING_SNAKE_CASE_      		=     num_hidden_layers
       SCREAMING_SNAKE_CASE_      		=     num_attention_heads
       SCREAMING_SNAKE_CASE_      		=     intermediate_size
       SCREAMING_SNAKE_CASE_      		=     hidden_act
       SCREAMING_SNAKE_CASE_      		=     hidden_dropout_prob
       SCREAMING_SNAKE_CASE_      		=     attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE_      		=     max_position_embeddings
       SCREAMING_SNAKE_CASE_      		=     type_vocab_size
       SCREAMING_SNAKE_CASE_      		=     type_sequence_label_size
       SCREAMING_SNAKE_CASE_      		=     initializer_range
       SCREAMING_SNAKE_CASE_      		=     num_labels
       SCREAMING_SNAKE_CASE_      		=     num_choices
       SCREAMING_SNAKE_CASE_      		=     scope
       SCREAMING_SNAKE_CASE_      		=     self.vocab_size - 1



 def  lowerCAmelCase_    (				self    :						Any		):
       SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size, self.seq_length]							,							self.vocab_size		)

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

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

       SCREAMING_SNAKE_CASE_      		=     None
       if self.use_mc_token_ids:
             SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size, self.num_choices]							,							self.seq_length		)

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

       SCREAMING_SNAKE_CASE_      		=     self.get_config()

       SCREAMING_SNAKE_CASE_      		=     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  lowerCAmelCase_    (				self    :						Optional[int]		):
       return 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							,							)



 def  lowerCAmelCase_    (				self    :						str							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						Optional[int]							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						Optional[int]							,							*_lowerCAmelCase    :						int		):
       SCREAMING_SNAKE_CASE_      		=     CTRLModel(config=_lowerCAmelCase		)
       model.to(_lowerCAmelCase		)
       model.eval()

       model(_lowerCAmelCase							,							token_type_ids=_lowerCAmelCase							,							head_mask=_lowerCAmelCase		)
       model(_lowerCAmelCase							,							token_type_ids=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		)
       self.parent.assertEqual(result.last_hidden_state.shape							,							(self.batch_size, self.seq_length, self.hidden_size)		)
       self.parent.assertEqual(len(result.past_key_values		)							,							config.n_layer		)



 def  lowerCAmelCase_    (				self    :						str							,							_lowerCAmelCase    :						Any							,							_lowerCAmelCase    :						Any							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						Union[str, Any]							,							_lowerCAmelCase    :						Dict							,							*_lowerCAmelCase    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     CTRLLMHeadModel(_lowerCAmelCase		)
       model.to(_lowerCAmelCase		)
       model.eval()

       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase							,							token_type_ids=_lowerCAmelCase							,							labels=_lowerCAmelCase		)
       self.parent.assertEqual(result.loss.shape							,							()		)
       self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length, self.vocab_size)		)



 def  lowerCAmelCase_    (				self    :						List[str]		):
       SCREAMING_SNAKE_CASE_      		=     self.prepare_config_and_inputs()

       (
           (
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					(
           SCREAMING_SNAKE_CASE_
       )		,					
       )      		=     config_and_inputs

       SCREAMING_SNAKE_CASE_      		=     {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'head_mask': head_mask}

       return config, inputs_dict




 def  lowerCAmelCase_    (				self    :						Union[str, Any]							,							_lowerCAmelCase    :						Any							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						Tuple							,							_lowerCAmelCase    :						str							,							*_lowerCAmelCase    :						List[str]		):
       SCREAMING_SNAKE_CASE_      		=     self.num_labels
       SCREAMING_SNAKE_CASE_      		=     CTRLForSequenceClassification(_lowerCAmelCase		)
       model.to(_lowerCAmelCase		)
       model.eval()
       SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size]							,							self.type_sequence_label_size		)
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase							,							token_type_ids=_lowerCAmelCase							,							labels=_lowerCAmelCase		)
       self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.num_labels)		)





@require_torch
class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE			,  _SCREAMING_SNAKE_CASE			,  _SCREAMING_SNAKE_CASE			,  unittest.TestCase   ):


 '''simple docstring'''







 lowercase_							       =       (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
 lowercase_							       =       (CTRLLMHeadModel,) if is_torch_available() else ()
 lowercase_							       =       (
     {
         "feature-extraction": CTRLModel,
         "text-classification": CTRLForSequenceClassification,
         "text-generation": CTRLLMHeadModel,
         "zero-shot": CTRLForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 lowercase_							       =       True
 lowercase_							       =       False
 lowercase_							       =       False



 def  lowerCAmelCase_    (				self    :						Dict							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						Union[str, Any]							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						Optional[Any]							,							_lowerCAmelCase    :						Optional[Any]		):
       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  lowerCAmelCase_    (				self    :						Tuple		):
       SCREAMING_SNAKE_CASE_      		=     CTRLModelTester(self		)
       SCREAMING_SNAKE_CASE_      		=     ConfigTester(self							,							config_class=_lowerCAmelCase							,							n_embd=37		)



 def  lowerCAmelCase_    (				self    :						int		):
       super().tearDown()
       # clean-up as much as possible GPU memory occupied by PyTorch
       gc.collect()
       torch.cuda.empty_cache()



 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       self.config_tester.run_common_tests()



 def  lowerCAmelCase_    (				self    :						List[str]		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_ctrl_model(*_lowerCAmelCase		)



 def  lowerCAmelCase_    (				self    :						List[str]		):
       SCREAMING_SNAKE_CASE_      		=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_lm_head_model(*_lowerCAmelCase		)



 @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'		)
 def  lowerCAmelCase_    (				self    :						int		):
       pass



 @slow
 def  lowerCAmelCase_    (				self    :						str		):
       for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE_      		=     CTRLModel.from_pretrained(_lowerCAmelCase		)
             self.assertIsNotNone(_lowerCAmelCase		)




 @unittest.skip('The model doesn\'t support left padding'		)  # and it's not used enough to be worth fixing :)
 def  lowerCAmelCase_    (				self    :						int		):
       pass





@require_torch
class 			lowerCamelCase_				( unittest.TestCase   ):


 '''simple docstring'''







 def  lowerCAmelCase_    (				self    :						str		):
       super().tearDown()
       # clean-up as much as possible GPU memory occupied by PyTorch
       gc.collect()
       torch.cuda.empty_cache()




 @slow
 def  lowerCAmelCase_    (				self    :						int		):
       SCREAMING_SNAKE_CASE_      		=     CTRLLMHeadModel.from_pretrained('ctrl'		)
       model.to(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     torch.tensor(
           [[11_859, 0, 1_611, 8]]							,							dtype=torch.long							,							device=_lowerCAmelCase		)  # Legal the president is
       SCREAMING_SNAKE_CASE_      		=     [
           11_859,
           0,
           1_611,
           8,
           5,
           150,
           26_449,
           2,
           19,
           348,
           469,
           3,
           2_595,
           48,
           20_740,
           246_533,
           246_533,
           19,
           30,
           5,
       ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

       SCREAMING_SNAKE_CASE_      		=     model.generate(_lowerCAmelCase							,							do_sample=_lowerCAmelCase		)
       self.assertListEqual(output_ids[0].tolist()							,							_lowerCAmelCase		)





class 			lowerCamelCase_				:


 '''simple docstring'''







 def __init__(				self    :						str		):
       SCREAMING_SNAKE_CASE_      		=     {}



 def  lowerCAmelCase_    (				self    :						List[str]		):
       print(self.vertex		)
       for i in self.vertex:
             print(_lowerCAmelCase							,							' -> '							,							' -> '.join([str(_lowerCAmelCase		) for j in self.vertex[i]]		)		)



 def  lowerCAmelCase_    (				self    :						Union[str, Any]							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						int		):
       # check if vertex is already present,
       if from_vertex in self.vertex:
             self.vertex[from_vertex].append(_lowerCAmelCase		)
       else:
             # else make a new vertex
             SCREAMING_SNAKE_CASE_      		=     [to_vertex]



 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       # visited array for storing already visited nodes
       SCREAMING_SNAKE_CASE_      		=     [False] * len(self.vertex		)

       # call the recursive helper function
       for i in range(len(self.vertex		)		):
             if not visited[i]:
                   self.dfs_recursive(_lowerCAmelCase							,							_lowerCAmelCase		)




 def  lowerCAmelCase_    (				self    :						Tuple							,							_lowerCAmelCase    :						int							,							_lowerCAmelCase    :						list		):
       # mark start vertex as visited
       SCREAMING_SNAKE_CASE_      		=     True

       print(_lowerCAmelCase							,							end=' '		)

       # Recur for all the vertices that are adjacent to this node
       for i in self.vertex:
             if not visited[i]:
                   self.dfs_recursive(_lowerCAmelCase							,							_lowerCAmelCase		)


if __name__ == "__main__":
   lowerCamelCase__					:  List[Any]      							=				Graph()
   g.add_edge(0, 1)
   g.add_edge(0, 2)
   g.add_edge(1, 2)
   g.add_edge(2, 0)
   g.add_edge(2, 3)
   g.add_edge(3, 3)

   g.print_graph()
   print('DFS:')
   g.dfs()

   # OUTPUT:
   # 0  ->  1 -> 2
   # 1  ->  2
   # 2  ->  0 -> 3
   # 3  ->  3
   # DFS:
   #  0 1 2 3