


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





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






								a_							: Optional[Any]								=						parent
								a_							: Union[str, Any]								=						batch_size
								a_							: Tuple								=						seq_length
								a_							: Dict								=						is_training
								a_							: Union[str, Any]								=						use_token_type_ids
								a_							: Optional[int]								=						use_input_mask
								a_							: Tuple								=						use_labels
								a_							: Any								=						use_mc_token_ids
								a_							: Any								=						vocab_size
								a_							: Tuple								=						hidden_size
								a_							: Dict								=						num_hidden_layers
								a_							: Dict								=						num_attention_heads
								a_							: Dict								=						intermediate_size
								a_							: List[str]								=						hidden_act
								a_							: List[Any]								=						hidden_dropout_prob
								a_							: Optional[int]								=						attention_probs_dropout_prob
								a_							: Optional[int]								=						max_position_embeddings
								a_							: Dict								=						type_vocab_size
								a_							: Union[str, Any]								=						type_sequence_label_size
								a_							: Tuple								=						initializer_range
								a_							: str								=						num_labels
								a_							: int								=						num_choices
								a_							: int								=						scope
								a_							: Tuple								=						self.vocab_size - 1





	def      A					(	self				)		->      Optional[int]:
								"""simple docstring"""






								a_							: List[str]								=						ids_tensor([self.batch_size, self.seq_length]					,     self.vocab_size				)

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

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

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

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

								a_							: Any								=						self.get_config()

								a_							: str								=						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      A					(	self				)		->      str:
								"""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      A					(	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     *UpperCamelCase_				)		->      int:
								"""simple docstring"""






								a_							: int								=						CTRLModel(config=UpperCamelCase_				)
								model.to(UpperCamelCase_				)
								model.eval()

								model(UpperCamelCase_					,     token_type_ids=UpperCamelCase_					,     head_mask=UpperCamelCase_				)
								model(UpperCamelCase_					,     token_type_ids=UpperCamelCase_				)
								a_							: Optional[int]								=						model(UpperCamelCase_				)
								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      A					(	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     *UpperCamelCase_				)		->      Dict:
								"""simple docstring"""






								a_							: List[str]								=						CTRLLMHeadModel(UpperCamelCase_				)
								model.to(UpperCamelCase_				)
								model.eval()

								a_							: Dict								=						model(UpperCamelCase_					,     token_type_ids=UpperCamelCase_					,     labels=UpperCamelCase_				)
								self.parent.assertEqual(result.loss.shape					,     ()				)
								self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.seq_length, self.vocab_size)				)





	def      A					(	self				)		->      Tuple:
								"""simple docstring"""






								a_							: Tuple								=						self.prepare_config_and_inputs()

								(
								    a_							
								)							: Dict								=						config_and_inputs

								a_							: Optional[int]								=						{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask}

								return config, inputs_dict





	def      A					(	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     *UpperCamelCase_				)		->      List[Any]:
								"""simple docstring"""






								a_							: Optional[int]								=						self.num_labels
								a_							: Optional[int]								=						CTRLForSequenceClassification(UpperCamelCase_				)
								model.to(UpperCamelCase_				)
								model.eval()
								a_							: int								=						ids_tensor([self.batch_size]					,     self.type_sequence_label_size				)
								a_							: Optional[int]								=						model(UpperCamelCase_					,     token_type_ids=UpperCamelCase_					,     labels=UpperCamelCase_				)
								self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.num_labels)				)





@require_torch
class        snake_case__	(			A__   ,     A__   ,     A__   ,     unittest.TestCase							):
	UpperCAmelCase :				str       =			(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
	UpperCAmelCase :				Optional[Any]       =			(CTRLLMHeadModel,) if is_torch_available() else ()
	UpperCAmelCase :				Optional[int]       =			(
	    {
	        """feature-extraction""": CTRLModel,
	        """text-classification""": CTRLForSequenceClassification,
	        """text-generation""": CTRLLMHeadModel,
	        """zero-shot""": CTRLForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	UpperCAmelCase :				str       =			True
	UpperCAmelCase :				str       =			False
	UpperCAmelCase :				List[Any]       =			False





	def      A					(	self					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_					,     UpperCamelCase_				)		->      Dict:
								"""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      A					(	self				)		->      Union[str, Any]:
								"""simple docstring"""






								a_							: List[str]								=						CTRLModelTester(self				)
								a_							: Optional[int]								=						ConfigTester(self					,     config_class=UpperCamelCase_					,     n_embd=37				)





	def      A					(	self				)		->      Optional[int]:
								"""simple docstring"""






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





	def      A					(	self				)		->      List[Any]:
								"""simple docstring"""






								self.config_tester.run_common_tests()





	def      A					(	self				)		->      List[str]:
								"""simple docstring"""






								a_							: Optional[int]								=						self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_model(*UpperCamelCase_				)





	def      A					(	self				)		->      Tuple:
								"""simple docstring"""






								a_							: Tuple								=						self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_lm_head_model(*UpperCamelCase_				)





	@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""				)
	def      A					(	self				)		->      str:
								"""simple docstring"""






								pass





	@slow
	def      A					(	self				)		->      Any:
								"""simple docstring"""






								for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															a_							: Tuple								=						CTRLModel.from_pretrained(UpperCamelCase_				)
															self.assertIsNotNone(UpperCamelCase_				)





	@unittest.skip("""The model doesn\'t support left padding"""				)  # and it's not used enough to be worth fixing :)
	def      A					(	self				)		->      Optional[Any]:
								"""simple docstring"""






								pass





@require_torch
class        snake_case__	(			unittest.TestCase							):





	def      A					(	self				)		->      Tuple:
								"""simple docstring"""






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





	@slow
	def      A					(	self				)		->      str:
								"""simple docstring"""






								a_							: Optional[int]								=						CTRLLMHeadModel.from_pretrained("""ctrl"""				)
								model.to(UpperCamelCase_				)
								a_							: int								=						torch.tensor(
								    [[11859, 0, 1611, 8]]					,     dtype=torch.long					,     device=UpperCamelCase_				)  # Legal the president is
								a_							: Optional[int]								=						[
								    11859,
								    0,
								    1611,
								    8,
								    5,
								    150,
								    26449,
								    2,
								    19,
								    348,
								    469,
								    3,
								    2595,
								    48,
								    20740,
								    246533,
								    246533,
								    19,
								    30,
								    5,
								]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

								a_							: Union[str, Any]								=						model.generate(UpperCamelCase_					,     do_sample=UpperCamelCase_				)
								self.assertListEqual(output_ids[0].tolist()					,     UpperCamelCase_				)






'''simple docstring'''
import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_huggingface_suite,
    require_multi_gpu,
    require_single_gpu,
)
from accelerate.utils import patch_environment
@require_huggingface_suite
class   lowercase      (				unittest.TestCase   ):

		"""simple docstring"""

		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Any				   =					inspect.getfile(accelerate.test_utils				)
							UpperCamelCase__  :Any				   =					os.path.sep.join(
							    mod_file.split(os.path.sep				)[:-1] + ['''scripts''', '''external_deps''', '''test_metrics.py''']				)

							from accelerate.test_utils.scripts.external_deps import test_metrics  # noqa: F401

							UpperCamelCase__  :List[str]				   =					test_metrics




		@require_cpu
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							debug_launcher(self.test_metrics.main       ,  num_processes=1				)




		@require_cpu
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							debug_launcher(self.test_metrics.main				)




		@require_single_gpu
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							self.test_metrics.main()




		@require_multi_gpu
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							print(F'''Found {torch.cuda.device_count()} devices.'''				)
							UpperCamelCase__  :Optional[Any]				   =					['''torchrun''', F'''--nproc_per_node={torch.cuda.device_count()}''', self.test_file_path]
							with patch_environment(omp_num_threads=1				):
												execute_subprocess_async(UpperCamelCase_       ,  env=os.environ.copy()				)