





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 		snake_case_   :







	'''simple docstring'''




	def __init__(						self							:     str					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     Optional[int]=14					,      __lowerCamelCase							:     Optional[int]=7					,      __lowerCamelCase							:     List[Any]=True					,      __lowerCamelCase							:     Any=True					,      __lowerCamelCase							:     str=True					,      __lowerCamelCase							:     int=True					,      __lowerCamelCase							:     Any=True					,      __lowerCamelCase							:     Optional[int]=99					,      __lowerCamelCase							:     Optional[Any]=32					,      __lowerCamelCase							:     Any=5					,      __lowerCamelCase							:     Optional[int]=4					,      __lowerCamelCase							:     Optional[Any]=37					,      __lowerCamelCase							:     Tuple="gelu"					,      __lowerCamelCase							:     Tuple=0.1					,      __lowerCamelCase							:     Union[str, Any]=0.1					,      __lowerCamelCase							:     List[str]=512					,      __lowerCamelCase							:     str=16					,      __lowerCamelCase							:     Tuple=2					,      __lowerCamelCase							:     List[Any]=0.02					,      __lowerCamelCase							:     int=3					,      __lowerCamelCase							:     Any=4					,      __lowerCamelCase							:     Optional[int]=None					,      )       -> Optional[int]:

			'''simple docstring'''




			__lowercase											=     parent
			__lowercase											=     batch_size
			__lowercase											=     seq_length
			__lowercase											=     is_training
			__lowercase											=     use_token_type_ids
			__lowercase											=     use_input_mask
			__lowercase											=     use_labels
			__lowercase											=     use_mc_token_ids
			__lowercase											=     vocab_size
			__lowercase											=     hidden_size
			__lowercase											=     num_hidden_layers
			__lowercase											=     num_attention_heads
			__lowercase											=     intermediate_size
			__lowercase											=     hidden_act
			__lowercase											=     hidden_dropout_prob
			__lowercase											=     attention_probs_dropout_prob
			__lowercase											=     max_position_embeddings
			__lowercase											=     type_vocab_size
			__lowercase											=     type_sequence_label_size
			__lowercase											=     initializer_range
			__lowercase											=     num_labels
			__lowercase											=     num_choices
			__lowercase											=     scope
			__lowercase											=     self.vocab_size - 1




	def    UpperCAmelCase			(						self							:     List[Any]   )       -> Optional[int]:

			'''simple docstring'''




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

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

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

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

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

			__lowercase											=     self.get_config()

			__lowercase											=     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    UpperCAmelCase			(						self							:     Dict   )       -> int:

			'''simple docstring'''




			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    UpperCAmelCase			(						self							:     List[Any]					,      __lowerCamelCase							:     List[str]					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Dict					,      *__lowerCamelCase							:     Optional[int]   )       -> List[Any]:

			'''simple docstring'''




			__lowercase											=     CTRLModel(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()

			model(__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      head_mask=__lowerCamelCase   )
			model(__lowerCamelCase					,      token_type_ids=__lowerCamelCase   )
			__lowercase											=     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    UpperCAmelCase			(						self							:     Optional[int]					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     List[Any]					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     int					,      *__lowerCamelCase							:     Optional[int]   )       -> List[Any]:

			'''simple docstring'''




			__lowercase											=     CTRLLMHeadModel(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()

			__lowercase											=     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    UpperCAmelCase			(						self							:     Tuple   )       -> Optional[int]:

			'''simple docstring'''




			__lowercase											=     self.prepare_config_and_inputs()

			(
			    (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      
			)											=     config_and_inputs

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

			return config, inputs_dict






	def    UpperCAmelCase			(						self							:     str					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     List[str]					,      __lowerCamelCase							:     Dict					,      *__lowerCamelCase							:     Optional[Any]   )       -> Optional[int]:

			'''simple docstring'''




			__lowercase											=     self.num_labels
			__lowercase											=     CTRLForSequenceClassification(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     ids_tensor([self.batch_size]					,      self.type_sequence_label_size   )
			__lowercase											=     model(__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      labels=__lowerCamelCase   )
			self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)   )





@require_torch
class 		snake_case_   (							UpperCAmelCase_     ,     UpperCAmelCase_     ,     UpperCAmelCase_     ,     unittest.TestCase						):







	'''simple docstring'''
	__UpperCamelCase   =		(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
	__UpperCamelCase   =		(CTRLLMHeadModel,) if is_torch_available() else ()
	__UpperCamelCase   =		(
	    {
	        '''feature-extraction''': CTRLModel,
	        '''text-classification''': CTRLForSequenceClassification,
	        '''text-generation''': CTRLLMHeadModel,
	        '''zero-shot''': CTRLForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	__UpperCamelCase   =		True
	__UpperCamelCase   =		False
	__UpperCamelCase   =		False




	def    UpperCAmelCase			(						self							:     List[Any]					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     Optional[Any]					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     List[Any]   )       -> List[Any]:

			'''simple docstring'''




			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    UpperCAmelCase			(						self							:     Dict   )       -> Any:

			'''simple docstring'''




			__lowercase											=     CTRLModelTester(self   )
			__lowercase											=     ConfigTester(self					,      config_class=__lowerCamelCase					,      n_embd=37   )




	def    UpperCAmelCase			(						self							:     str   )       -> int:

			'''simple docstring'''




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




	def    UpperCAmelCase			(						self							:     List[Any]   )       -> str:

			'''simple docstring'''




			self.config_tester.run_common_tests()




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

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_ctrl_model(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     int   )       -> Tuple:

			'''simple docstring'''




			__lowercase											=     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    UpperCAmelCase			(						self							:     List[Any]   )       -> Tuple:

			'''simple docstring'''




			pass




	@slow
	def    UpperCAmelCase			(						self							:     Tuple   )       -> Tuple:

			'''simple docstring'''




			for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					__lowercase											=     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    UpperCAmelCase			(						self							:     Union[str, Any]   )       -> List[Any]:

			'''simple docstring'''




			pass





@require_torch
class 		snake_case_   (							unittest.TestCase						):







	'''simple docstring'''




	def    UpperCAmelCase			(						self							:     Union[str, Any]   )       -> int:

			'''simple docstring'''




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






	@slow
	def    UpperCAmelCase			(						self							:     Any   )       -> List[str]:

			'''simple docstring'''




			__lowercase											=     CTRLLMHeadModel.from_pretrained('ctrl'   )
			model.to(__lowerCamelCase   )
			__lowercase											=     torch.tensor(
			    [[11_859, 0, 1_611, 8]]					,      dtype=torch.long					,      device=__lowerCamelCase   )  # Legal the president is
			__lowercase											=     [
			    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

			__lowercase											=     model.generate(__lowerCamelCase					,      do_sample=__lowerCamelCase   )
			self.assertListEqual(output_ids[0].tolist()					,      __lowerCamelCase   )











def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case						,   snake_case							)		->    bool:
		return not any(
		    neighbour == 1 and colored_vertices[i] == color
		    for i, neighbour in enumerate(snake_case							)							)



def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case						,   snake_case						,   snake_case							)		->    bool:

		# Base Case
		if index == len(snake_case							):
				return True

		# Recursive Step
		for i in range(snake_case							):
				if valid_coloring(graph[index]						,   snake_case						,   snake_case							):
						# Color current vertex
						__lowercase											=     i
						# Validate coloring
						if util_color(snake_case						,   snake_case						,   snake_case						,   index + 1							):
								return True
						# Backtrack
						__lowercase											=     -1
		return False



def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case							)		->    list[int]:
		__lowercase											=     [-1] * len(snake_case							)

		if util_color(snake_case						,   snake_case						,   snake_case						,   0							):
				return colored_vertices

		return []




