





'''simple docstring'''






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	:



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



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     List[Any]       						= ids_tensor([self.batch_size, self.seq_length]		,   self.vocab_size						)

			lowerCAmelCase  :     int       						= None
			if self.use_input_mask:
					lowerCAmelCase  :     Tuple       						= random_attention_mask([self.batch_size, self.seq_length]						)

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

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

			lowerCAmelCase  :     int       						= None
			lowerCAmelCase  :     str       						= None
			lowerCAmelCase  :     List[str]       						= None
			if self.use_labels:
					lowerCAmelCase  :     Union[str, Any]       						= ids_tensor([self.batch_size]		,   self.type_sequence_label_size						)
					lowerCAmelCase  :     Any       						= ids_tensor([self.batch_size, self.seq_length]		,   self.num_labels						)
					lowerCAmelCase  :     Tuple       						= ids_tensor([self.batch_size]		,   self.num_choices						)

			lowerCAmelCase  :     str       						= self.get_config()

			lowerCAmelCase  :     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 	_lowerCamelCase     ( self						):
			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		,   a_		,   a_		,   a_		,   a_		,   a_		,   *a_						):
			lowerCAmelCase  :     List[Any]       						= CTRLModel(config=a_						)
			model.to(a_						)
			model.eval()

			model(a_		,   token_type_ids=a_		,   head_mask=a_						)
			model(a_		,   token_type_ids=a_						)
			lowerCAmelCase  :     List[str]       						= model(a_						)
			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		,   a_		,   a_		,   a_		,   a_		,   a_		,   *a_						):
			lowerCAmelCase  :     Union[str, Any]       						= CTRLLMHeadModel(a_						)
			model.to(a_						)
			model.eval()

			lowerCAmelCase  :     List[str]       						= model(a_		,   token_type_ids=a_		,   labels=a_						)
			self.parent.assertEqual(result.loss.shape		,   ()						)
			self.parent.assertEqual(result.logits.shape		,   (self.batch_size, self.seq_length, self.vocab_size)						)



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Any       						= self.prepare_config_and_inputs()

			(
			    (
			    lowerCAmelCase
			)						,		(
			    lowerCAmelCase
			)						,		(
			    lowerCAmelCase
			)						,		(
			    lowerCAmelCase
			)						,		(
			    lowerCAmelCase
			)						,		(
			    lowerCAmelCase
			)						,		(
			    lowerCAmelCase
			)						,		(
			    lowerCAmelCase
			)						,		(
			    lowerCAmelCase
			)						,		
			)  :     Dict       						= config_and_inputs

			lowerCAmelCase  :     List[str]       						= {"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

			return config, inputs_dict





	def 	_lowerCamelCase     ( self		,   a_		,   a_		,   a_		,   a_		,   *a_						):
			lowerCAmelCase  :     str       						= self.num_labels
			lowerCAmelCase  :     List[str]       						= CTRLForSequenceClassification(a_						)
			model.to(a_						)
			model.eval()
			lowerCAmelCase  :     Union[str, Any]       						= ids_tensor([self.batch_size]		,   self.type_sequence_label_size						)
			lowerCAmelCase  :     Dict       						= model(a_		,   token_type_ids=a_		,   labels=a_						)
			self.parent.assertEqual(result.logits.shape		,   (self.batch_size, self.num_labels)						)






@require_torch
class 		lowerCamelCase	(   _A   , _A   , _A   , unittest.TestCase							):
	snake_case_   = (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
	snake_case_   = (CTRLLMHeadModel,) if is_torch_available() else ()
	snake_case_   = (
	    {
	        "feature-extraction": CTRLModel,
	        "text-classification": CTRLForSequenceClassification,
	        "text-generation": CTRLLMHeadModel,
	        "zero-shot": CTRLForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	snake_case_   = True
	snake_case_   = False
	snake_case_   = False



	def 	_lowerCamelCase     ( self		,   a_		,   a_		,   a_		,   a_		,   a_						):
			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						):
			lowerCAmelCase  :     Tuple       						= CTRLModelTester(self						)
			lowerCAmelCase  :     List[Any]       						= ConfigTester(self		,   config_class=a_		,   n_embd=37						)



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



	def 	_lowerCamelCase     ( self						):
			self.config_tester.run_common_tests()



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     str       						= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_ctrl_model(*a_						)



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     List[str]       						= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_lm_head_model(*a_						)



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



	@slow
	def 	_lowerCamelCase     ( self						):
			for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCAmelCase  :     List[str]       						= CTRLModel.from_pretrained(a_						)
					self.assertIsNotNone(a_						)





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






@require_torch
class 		lowerCamelCase	(   unittest.TestCase							):



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





	@slow
	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Optional[Any]       						= CTRLLMHeadModel.from_pretrained("ctrl"						)
			model.to(a_						)
			lowerCAmelCase  :     List[Any]       						= torch.tensor(
			    [[11_859, 0, 1_611, 8]]		,   dtype=torch.long		,   device=a_						)  # Legal the president is
			lowerCAmelCase  :     Tuple       						= [
			    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

			lowerCAmelCase  :     Tuple       						= model.generate(a_		,   do_sample=a_						)
			self.assertListEqual(output_ids[0].tolist()		,   a_						)










'''simple docstring'''






from collections import defaultdict

from graphs.minimum_spanning_tree_prims import prisms_algorithm as mst






def   __A    (				):
		lowerCAmelCase						,		lowerCAmelCase  :     List[Any]       						= 9, 1_4  # noqa: F841
		lowerCAmelCase  :     int       						= [
		    [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, 1_4],
		    [3, 4, 9],
		    [5, 4, 1_0],
		    [1, 7, 1_1],
		]

		lowerCAmelCase  :     Optional[Any]       						= defaultdict(a_						)
		for nodea, nodea, cost in edges:
				adjancency[nodea].append([nodea, cost]						)
				adjancency[nodea].append([nodea, cost]						)

		lowerCAmelCase  :     int       						= mst(a_						)

		lowerCAmelCase  :     Tuple       						= [
		    [7, 6, 1],
		    [2, 8, 2],
		    [6, 5, 2],
		    [0, 1, 4],
		    [2, 5, 4],
		    [2, 3, 7],
		    [0, 7, 8],
		    [3, 4, 9],
		]

		for answer in expected:
				lowerCAmelCase  :     List[str]       						= tuple(answer[:2]						)
				lowerCAmelCase  :     int       						= tuple(edge[::-1]						)
				assert edge in result or reverse in result



