

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






					'''simple docstring'''



					def __init__(				self				,      lowerCamelCase__				,      lowerCamelCase__=1_4				,      lowerCamelCase__=7				,      lowerCamelCase__=True				,      lowerCamelCase__=True				,      lowerCamelCase__=True				,      lowerCamelCase__=True				,      lowerCamelCase__=True				,      lowerCamelCase__=9_9				,      lowerCamelCase__=3_2				,      lowerCamelCase__=5				,      lowerCamelCase__=4				,      lowerCamelCase__=3_7				,      lowerCamelCase__="gelu"				,      lowerCamelCase__=0.1				,      lowerCamelCase__=0.1				,      lowerCamelCase__=5_1_2				,      lowerCamelCase__=1_6				,      lowerCamelCase__=2				,      lowerCamelCase__=0.02				,      lowerCamelCase__=3				,      lowerCamelCase__=4				,      lowerCamelCase__=None				,      ):
						UpperCAmelCase__:	Any									= parent
						UpperCAmelCase__:	List[Any]									= batch_size
						UpperCAmelCase__:	List[Any]									= seq_length
						UpperCAmelCase__:	int									= is_training
						UpperCAmelCase__:	List[Any]									= use_token_type_ids
						UpperCAmelCase__:	Any									= use_input_mask
						UpperCAmelCase__:	Any									= use_labels
						UpperCAmelCase__:	str									= use_mc_token_ids
						UpperCAmelCase__:	Optional[Any]									= vocab_size
						UpperCAmelCase__:	Optional[int]									= hidden_size
						UpperCAmelCase__:	Optional[int]									= num_hidden_layers
						UpperCAmelCase__:	List[str]									= num_attention_heads
						UpperCAmelCase__:	int									= intermediate_size
						UpperCAmelCase__:	Dict									= hidden_act
						UpperCAmelCase__:	Dict									= hidden_dropout_prob
						UpperCAmelCase__:	Dict									= attention_probs_dropout_prob
						UpperCAmelCase__:	Optional[Any]									= max_position_embeddings
						UpperCAmelCase__:	Any									= type_vocab_size
						UpperCAmelCase__:	int									= type_sequence_label_size
						UpperCAmelCase__:	str									= initializer_range
						UpperCAmelCase__:	Union[str, Any]									= num_labels
						UpperCAmelCase__:	Optional[int]									= num_choices
						UpperCAmelCase__:	Optional[Any]									= scope
						UpperCAmelCase__:	Any									= self.vocab_size - 1





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

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

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

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

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

						UpperCAmelCase__:	int									= self.get_config()

						UpperCAmelCase__:	List[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    _UpperCAmelCase    (				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    _UpperCAmelCase    (				self				,      lowerCamelCase__				,      lowerCamelCase__				,      lowerCamelCase__				,      lowerCamelCase__				,      lowerCamelCase__				,      *lowerCamelCase__						):
						UpperCAmelCase__:	str									= CTRLModel(config=lowerCamelCase__						)
						model.to(lowerCamelCase__						)
						model.eval()

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

						UpperCAmelCase__:	Tuple									= 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						):
						UpperCAmelCase__:	List[Any]									= self.prepare_config_and_inputs()

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

						UpperCAmelCase__:	int									= {"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

						return config, inputs_dict





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

@require_torch
class __UpperCamelCase     (				_a					,_a					,_a					,unittest.TestCase		):






					'''simple docstring'''



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





					def    _UpperCAmelCase    (				self				,      lowerCamelCase__				,      lowerCamelCase__				,      lowerCamelCase__				,      lowerCamelCase__				,      lowerCamelCase__						):
						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						):
						UpperCAmelCase__:	Optional[int]									= CTRLModelTester(self						)
						UpperCAmelCase__:	Union[str, Any]									= ConfigTester(self				,      config_class=lowerCamelCase__				,      n_embd=3_7						)





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





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





					def    _UpperCAmelCase    (				self						):
						UpperCAmelCase__:	Tuple									= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_ctrl_model(*lowerCamelCase__						)





					def    _UpperCAmelCase    (				self						):
						UpperCAmelCase__:	Union[str, Any]									= 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						):
						pass





					@slow
					def    _UpperCAmelCase    (				self						):
						for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase__:	str									= 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						):
						pass

@require_torch
class __UpperCamelCase     (				unittest.TestCase		):






					'''simple docstring'''



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





					@slow
					def    _UpperCAmelCase    (				self						):
						UpperCAmelCase__:	List[str]									= CTRLLMHeadModel.from_pretrained("ctrl"						)
						model.to(lowerCamelCase__						)
						UpperCAmelCase__:	Tuple									= torch.tensor(
						    [[1_1_8_5_9, 0, 1_6_1_1, 8]]				,      dtype=torch.long				,      device=lowerCamelCase__						)  # Legal the president is
						UpperCAmelCase__:	str									= [
						    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__:	Optional[Any]									= model.generate(lowerCamelCase__				,      do_sample=lowerCamelCase__						)
						self.assertListEqual(output_ids[0].tolist()				,      lowerCamelCase__						)


_lowerCAmelCase  :      int			 ="""
# Installazione di Transformers
! pip install transformers datasets
# Per installare dalla fonte invece dell'ultima versione rilasciata, commenta il comando sopra e
# rimuovi la modalità commento al comando seguente.
# ! pip install git+https://github.com/huggingface/transformers.git
"""

_lowerCAmelCase  :      List[str]			 =[{"""type""": """code""", """content""": INSTALL_CONTENT}]
_lowerCAmelCase  :      int			 ={
    """{processor_class}""": """FakeProcessorClass""",
    """{model_class}""": """FakeModelClass""",
    """{object_class}""": """FakeObjectClass""",
}