






'''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    A_	:
							"""simple docstring"""




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


										'''simple docstring'''




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

							def 							_A	(		self			:int				)				->  List[str]:


										'''simple docstring'''




										snake_case_				:					Any   =       ids_tensor([self.batch_size, self.seq_length]       ,							self.vocab_size				)

										snake_case_				:					str   =       None
										if self.use_input_mask:
													snake_case_				:					List[Any]   =       random_attention_mask([self.batch_size, self.seq_length]				)

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

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

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

										snake_case_				:					Tuple   =       self.get_config()

										snake_case_				:					List[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			:Any				)				->  Dict:


										'''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			:str       ,							lowerCAmelCase__			:List[str]       ,							lowerCAmelCase__			:int       ,							lowerCAmelCase__			:Any       ,							lowerCAmelCase__			:List[str]       ,							lowerCAmelCase__			:Dict       ,							*lowerCAmelCase__			:List[str]				)				->  int:


										'''simple docstring'''




										snake_case_				:					Dict   =       CTRLModel(config=__snake_case				)
										model.to(__snake_case				)
										model.eval()

										model(__snake_case       ,							token_type_ids=__snake_case       ,							head_mask=__snake_case				)
										model(__snake_case       ,							token_type_ids=__snake_case				)
										snake_case_				:					Dict   =       model(__snake_case				)
										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			:str       ,							lowerCAmelCase__			:Dict       ,							lowerCAmelCase__			:Dict       ,							lowerCAmelCase__			:Optional[int]       ,							lowerCAmelCase__			:Tuple       ,							lowerCAmelCase__			:Dict       ,							*lowerCAmelCase__			:str				)				->  List[str]:


										'''simple docstring'''




										snake_case_				:					List[str]   =       CTRLLMHeadModel(__snake_case				)
										model.to(__snake_case				)
										model.eval()

										snake_case_				:					Optional[Any]   =       model(__snake_case       ,							token_type_ids=__snake_case       ,							labels=__snake_case				)
										self.parent.assertEqual(result.loss.shape       ,							()				)
										self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.seq_length, self.vocab_size)				)

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


										'''simple docstring'''




										snake_case_				:					List[str]   =       self.prepare_config_and_inputs()

										(
										    snake_case_    
										)				:					Optional[int]   =       config_and_inputs

										snake_case_				:					str   =       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask}

										return config, inputs_dict






							def 							_A	(		self			:int       ,							lowerCAmelCase__			:Optional[int]       ,							lowerCAmelCase__			:List[Any]       ,							lowerCAmelCase__			:int       ,							lowerCAmelCase__			:List[Any]       ,							*lowerCAmelCase__			:str				)				->  int:


										'''simple docstring'''




										snake_case_				:					Tuple   =       self.num_labels
										snake_case_				:					Optional[int]   =       CTRLForSequenceClassification(__snake_case				)
										model.to(__snake_case				)
										model.eval()
										snake_case_				:					Any   =       ids_tensor([self.batch_size]       ,							self.type_sequence_label_size				)
										snake_case_				:					List[str]   =       model(__snake_case       ,							token_type_ids=__snake_case       ,							labels=__snake_case				)
										self.parent.assertEqual(result.logits.shape       ,							(self.batch_size, self.num_labels)				)







@require_torch
class    A_	(__SCREAMING_SNAKE_CASE						,					__SCREAMING_SNAKE_CASE						,					__SCREAMING_SNAKE_CASE						,					unittest.TestCase							):
							"""simple docstring"""




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

							def 							_A	(		self			:str       ,							lowerCAmelCase__			:int       ,							lowerCAmelCase__			:int       ,							lowerCAmelCase__			:str       ,							lowerCAmelCase__			:List[str]       ,							lowerCAmelCase__			:Optional[int]				)				->  Tuple:


										'''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			:str				)				->  Any:


										'''simple docstring'''




										snake_case_				:					List[str]   =       CTRLModelTester(self				)
										snake_case_				:					Tuple   =       ConfigTester(self       ,							config_class=__snake_case       ,							n_embd=37				)

							def 							_A	(		self			:str				)				->  str:


										'''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]				)				->  Optional[Any]:


										'''simple docstring'''




										self.config_tester.run_common_tests()

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


										'''simple docstring'''




										snake_case_				:					int   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_ctrl_model(*__snake_case				)

							def 							_A	(		self			:Union[str, Any]				)				->  Dict:


										'''simple docstring'''




										snake_case_				:					str   =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_lm_head_model(*__snake_case				)

							@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."				)
							def 							_A	(		self			:Union[str, Any]				)				->  List[str]:


										'''simple docstring'''




										pass

							@slow
							def 							_A	(		self			:Union[str, Any]				)				->  List[str]:


										'''simple docstring'''




										for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													snake_case_				:					Dict   =       CTRLModel.from_pretrained(__snake_case				)
													self.assertIsNotNone(__snake_case				)






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


										'''simple docstring'''




										pass







@require_torch
class    A_	(unittest.TestCase							):
							"""simple docstring"""




							def 							_A	(		self			:Dict				)				->  List[Any]:


										'''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			:Union[str, Any]				)				->  List[Any]:


										'''simple docstring'''




										snake_case_				:					Tuple   =       CTRLLMHeadModel.from_pretrained("ctrl"				)
										model.to(__snake_case				)
										snake_case_				:					Any   =       torch.tensor(
										    [[11_859, 0, 1_611, 8]]       ,							dtype=torch.long       ,							device=__snake_case				)  # Legal the president is
										snake_case_				:					List[str]   =       [
										    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

										snake_case_				:					int   =       model.generate(__snake_case       ,							do_sample=__snake_case				)
										self.assertListEqual(output_ids[0].tolist()       ,							__snake_case				)












'''simple docstring'''




from ...configuration_utils import PretrainedConfig
from ...utils import logging


__lowerCamelCase   :					List[str]   							=					logging.get_logger(__name__)

__lowerCamelCase   :					int   							=					{
    '''microsoft/cvt-13''': '''https://huggingface.co/microsoft/cvt-13/resolve/main/config.json''',
    # See all Cvt models at https://huggingface.co/models?filter=cvt
}







class    A_	(a_							):
							"""simple docstring"""




							a__   		=	'''cvt'''

							def __init__(		self			:List[Any]       ,							lowerCAmelCase__			:Optional[int]=3       ,							lowerCAmelCase__			:Any=[7, 3, 3]       ,							lowerCAmelCase__			:Dict=[4, 2, 2]       ,							lowerCAmelCase__			:Union[str, Any]=[2, 1, 1]       ,							lowerCAmelCase__			:Any=[64, 192, 384]       ,							lowerCAmelCase__			:List[str]=[1, 3, 6]       ,							lowerCAmelCase__			:str=[1, 2, 10]       ,							lowerCAmelCase__			:Any=[4.0, 4.0, 4.0]       ,							lowerCAmelCase__			:int=[0.0, 0.0, 0.0]       ,							lowerCAmelCase__			:Optional[Any]=[0.0, 0.0, 0.0]       ,							lowerCAmelCase__			:Dict=[0.0, 0.0, 0.1]       ,							lowerCAmelCase__			:List[Any]=[True, True, True]       ,							lowerCAmelCase__			:List[Any]=[False, False, True]       ,							lowerCAmelCase__			:Dict=["dw_bn", "dw_bn", "dw_bn"]       ,							lowerCAmelCase__			:Any=[3, 3, 3]       ,							lowerCAmelCase__			:Tuple=[1, 1, 1]       ,							lowerCAmelCase__			:Optional[int]=[2, 2, 2]       ,							lowerCAmelCase__			:Union[str, Any]=[1, 1, 1]       ,							lowerCAmelCase__			:Any=[1, 1, 1]       ,							lowerCAmelCase__			:List[str]=0.0_2       ,							lowerCAmelCase__			:Dict=1E-1_2       ,							**lowerCAmelCase__			:Optional[Any]       ,							)				->  str:


										'''simple docstring'''




										super().__init__(**lowerCAmelCase__				)
										snake_case_				:					int   =       num_channels
										snake_case_				:					int   =       patch_sizes
										snake_case_				:					Optional[Any]   =       patch_stride
										snake_case_				:					Dict   =       patch_padding
										snake_case_				:					Tuple   =       embed_dim
										snake_case_				:					Optional[int]   =       num_heads
										snake_case_				:					Union[str, Any]   =       depth
										snake_case_				:					Optional[int]   =       mlp_ratio
										snake_case_				:					Tuple   =       attention_drop_rate
										snake_case_				:					str   =       drop_rate
										snake_case_				:					Tuple   =       drop_path_rate
										snake_case_				:					Any   =       qkv_bias
										snake_case_				:					Union[str, Any]   =       cls_token
										snake_case_				:					int   =       qkv_projection_method
										snake_case_				:					Any   =       kernel_qkv
										snake_case_				:					Union[str, Any]   =       padding_kv
										snake_case_				:					str   =       stride_kv
										snake_case_				:					Dict   =       padding_q
										snake_case_				:					Tuple   =       stride_q
										snake_case_				:					Any   =       initializer_range
										snake_case_				:					Any   =       layer_norm_eps




