






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



		def  __UpperCAmelCase      (							self       )     ->							List[Any]:
							UpperCAmelCase_   :				Optional[Any]						=						ids_tensor([self.batch_size, self.seq_length]    ,		self.vocab_size       )

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

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

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

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

							UpperCAmelCase_   :				List[Any]						=						self.get_config()

							UpperCAmelCase_   :				Optional[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       )     ->							Dict:
							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    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		*_UpperCamelCase       )     ->							Dict:
							UpperCAmelCase_   :				Optional[int]						=						CTRLModel(config=__A       )
							model.to(__A       )
							model.eval()

							model(__A    ,		token_type_ids=__A    ,		head_mask=__A       )
							model(__A    ,		token_type_ids=__A       )
							UpperCAmelCase_   :				Tuple						=						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  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		*_UpperCamelCase       )     ->							List[str]:
							UpperCAmelCase_   :				List[str]						=						CTRLLMHeadModel(__A       )
							model.to(__A       )
							model.eval()

							UpperCAmelCase_   :				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  __UpperCAmelCase      (							self       )     ->							Any:
							UpperCAmelCase_   :				List[str]						=						self.prepare_config_and_inputs()

							(
							    UpperCAmelCase_						
							)   :				Dict						=						config_and_inputs

							UpperCAmelCase_   :				Any						=						{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask}

							return config, inputs_dict



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		*_UpperCamelCase       )     ->							int:
							UpperCAmelCase_   :				List[str]						=						self.num_labels
							UpperCAmelCase_   :				int						=						CTRLForSequenceClassification(__A       )
							model.to(__A       )
							model.eval()
							UpperCAmelCase_   :				List[Any]						=						ids_tensor([self.batch_size]    ,		self.type_sequence_label_size       )
							UpperCAmelCase_   :				List[str]						=						model(__A    ,		token_type_ids=__A    ,		labels=__A       )
							self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.num_labels)       )





@require_torch
class   lowerCamelCase				(lowerCamelCase__			,  lowerCamelCase__			,  lowerCamelCase__			,  unittest.TestCase      ):







		'''simple docstring'''



		_snake_case   :   Any							=  (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
		_snake_case   :   Dict							=  (CTRLLMHeadModel,) if is_torch_available() else ()
		_snake_case   :   Tuple							=  (
		    {
		        '''feature-extraction''': CTRLModel,
		        '''text-classification''': CTRLForSequenceClassification,
		        '''text-generation''': CTRLLMHeadModel,
		        '''zero-shot''': CTRLForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		_snake_case   :   List[str]							=  True
		_snake_case   :   Union[str, Any]							=  False
		_snake_case   :   Optional[Any]							=  False



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase       )     ->							Union[str, 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  __UpperCAmelCase      (							self       )     ->							Tuple:
							UpperCAmelCase_   :				Union[str, Any]						=						CTRLModelTester(self       )
							UpperCAmelCase_   :				Union[str, Any]						=						ConfigTester(self    ,		config_class=__A    ,		n_embd=3_7       )



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



		def  __UpperCAmelCase      (							self       )     ->							Union[str, Any]:
							self.config_tester.run_common_tests()



		def  __UpperCAmelCase      (							self       )     ->							Optional[Any]:
							UpperCAmelCase_   :				str						=						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_ctrl_model(*__A       )



		def  __UpperCAmelCase      (							self       )     ->							Optional[int]:
							UpperCAmelCase_   :				Tuple						=						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  __UpperCAmelCase      (							self       )     ->							Dict:
							pass



		@slow
		def  __UpperCAmelCase      (							self       )     ->							Tuple:
							for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												UpperCAmelCase_   :				Optional[int]						=						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  __UpperCAmelCase      (							self       )     ->							str:
							pass





@require_torch
class   lowerCamelCase				(unittest.TestCase      ):







		'''simple docstring'''



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



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














from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


__UpperCAmelCase     							=						{
    'configuration_roc_bert': ['ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RoCBertConfig'],
    'tokenization_roc_bert': ['RoCBertTokenizer'],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		pass

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__UpperCAmelCase     							=						[
		    'ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'RoCBertForCausalLM',
		    'RoCBertForMaskedLM',
		    'RoCBertForMultipleChoice',
		    'RoCBertForPreTraining',
		    'RoCBertForQuestionAnswering',
		    'RoCBertForSequenceClassification',
		    'RoCBertForTokenClassification',
		    'RoCBertLayer',
		    'RoCBertModel',
		    'RoCBertPreTrainedModel',
		    'load_tf_weights_in_roc_bert',
		]

if TYPE_CHECKING:
		from .configuration_roc_bert import ROC_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RoCBertConfig
		from .tokenization_roc_bert import RoCBertTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				raise OptionalDependencyNotAvailable()

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_roc_bert import (
				    ROC_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    RoCBertForCausalLM,
				    RoCBertForMaskedLM,
				    RoCBertForMultipleChoice,
				    RoCBertForPreTraining,
				    RoCBertForQuestionAnswering,
				    RoCBertForSequenceClassification,
				    RoCBertForTokenClassification,
				    RoCBertLayer,
				    RoCBertModel,
				    RoCBertPreTrainedModel,
				    load_tf_weights_in_roc_bert,
				)


else:
		import sys

		__UpperCAmelCase     							=						_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)






