




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



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

								"""simple docstring"""

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



						def 	__a					(			self	:   str  ):

								"""simple docstring"""

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

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

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

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

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

								SCREAMING_SNAKE_CASE_					      =							self.get_config()

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

								"""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	:   int					,							snake_case__	:   Optional[Any]					,							snake_case__	:   Optional[int]					,							snake_case__	:   Dict					,							snake_case__	:   str					,							snake_case__	:   List[Any]					,							*snake_case__	:   Any  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							CTRLModel(config=a_  )
								model.to(a_  )
								model.eval()

								model(a_					,							token_type_ids=a_					,							head_mask=a_  )
								model(a_					,							token_type_ids=a_  )
								SCREAMING_SNAKE_CASE_					      =							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 	__a					(			self	:   Any					,							snake_case__	:   List[Any]					,							snake_case__	:   List[Any]					,							snake_case__	:   List[Any]					,							snake_case__	:   Any					,							snake_case__	:   List[Any]					,							*snake_case__	:   Optional[Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							CTRLLMHeadModel(a_  )
								model.to(a_  )
								model.eval()

								SCREAMING_SNAKE_CASE_					      =							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 	__a					(			self	:   Any  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							self.prepare_config_and_inputs()

								(
								    (
								    SCREAMING_SNAKE_CASE_   
								)      ,				(
								    SCREAMING_SNAKE_CASE_   
								)      ,				(
								    SCREAMING_SNAKE_CASE_   
								)      ,				(
								    SCREAMING_SNAKE_CASE_   
								)      ,				(
								    SCREAMING_SNAKE_CASE_   
								)      ,				(
								    SCREAMING_SNAKE_CASE_   
								)      ,				(
								    SCREAMING_SNAKE_CASE_   
								)      ,				(
								    SCREAMING_SNAKE_CASE_   
								)      ,				(
								    SCREAMING_SNAKE_CASE_   
								)      ,				   
								)					      =							config_and_inputs

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

								return config, inputs_dict






						def 	__a					(			self	:   List[Any]					,							snake_case__	:   Union[str, Any]					,							snake_case__	:   List[str]					,							snake_case__	:   List[str]					,							snake_case__	:   Any					,							*snake_case__	:   Union[str, Any]  ):

								"""simple docstring"""

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



@require_torch
class    lowercase_		(SCREAMING_SNAKE_CASE__							,	SCREAMING_SNAKE_CASE__							,	SCREAMING_SNAKE_CASE__							,	unittest.TestCase ):
						lowerCAmelCase__			      =(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
						lowerCAmelCase__			      =(CTRLLMHeadModel,) if is_torch_available() else ()
						lowerCAmelCase__			      =(
						    {
						        "feature-extraction": CTRLModel,
						        "text-classification": CTRLForSequenceClassification,
						        "text-generation": CTRLLMHeadModel,
						        "zero-shot": CTRLForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						lowerCAmelCase__			      =True
						lowerCAmelCase__			      =False
						lowerCAmelCase__			      =False



						def 	__a					(			self	:   Optional[int]					,							snake_case__	:   Optional[Any]					,							snake_case__	:   List[str]					,							snake_case__	:   Tuple					,							snake_case__	:   List[Any]					,							snake_case__	:   Union[str, 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 	__a					(			self	:   List[str]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							CTRLModelTester(self  )
								SCREAMING_SNAKE_CASE_					      =							ConfigTester(self					,							config_class=a_					,							n_embd=37  )



						def 	__a					(			self	:   List[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	:   Any  ):

								"""simple docstring"""

								self.config_tester.run_common_tests()



						def 	__a					(			self	:   List[str]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_model(*a_  )



						def 	__a					(			self	:   Tuple  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							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 	__a					(			self	:   List[str]  ):

								"""simple docstring"""

								pass



						@slow
						def 	__a					(			self	:   List[Any]  ):

								"""simple docstring"""

								for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										SCREAMING_SNAKE_CASE_					      =							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 	__a					(			self	:   Tuple  ):

								"""simple docstring"""

								pass



@require_torch
class    lowercase_		(unittest.TestCase ):



						def 	__a					(			self	:   str  ):

								"""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"""

								SCREAMING_SNAKE_CASE_					      =							CTRLLMHeadModel.from_pretrained('ctrl'  )
								model.to(a_  )
								SCREAMING_SNAKE_CASE_					      =							torch.tensor(
								    [[1_18_59, 0, 16_11, 8]]					,							dtype=torch.long					,							device=a_  )  # Legal the president is
								SCREAMING_SNAKE_CASE_					      =							[
								    1_18_59,
								    0,
								    16_11,
								    8,
								    5,
								    1_50,
								    2_64_49,
								    2,
								    19,
								    3_48,
								    4_69,
								    3,
								    25_95,
								    48,
								    2_07_40,
								    24_65_33,
								    24_65_33,
								    19,
								    30,
								    5,
								]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

								SCREAMING_SNAKE_CASE_					      =							model.generate(a_					,							do_sample=a_  )
								self.assertListEqual(output_ids[0].tolist()					,							a_  )


'''simple docstring'''




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


_a					:			str							=    logging.get_logger(__name__)

_a					:			Dict							=    {
    '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 							lowercase_						(		a			):

  '''simple docstring'''





  __lowerCAmelCase       :			List[Any]     	= "cvt"
  def __init__(  self ,			a_=3 ,			a_=[7, 3, 3] ,			a_=[4, 2, 2] ,			a_=[2, 1, 1] ,			a_=[6_4, 1_9_2, 3_8_4] ,			a_=[1, 3, 6] ,			a_=[1, 2, 1_0] ,			a_=[4.0, 4.0, 4.0] ,			a_=[0.0, 0.0, 0.0] ,			a_=[0.0, 0.0, 0.0] ,			a_=[0.0, 0.0, 0.1] ,			a_=[True, True, True] ,			a_=[False, False, True] ,			a_=["dw_bn", "dw_bn", "dw_bn"] ,			a_=[3, 3, 3] ,			a_=[1, 1, 1] ,			a_=[2, 2, 2] ,			a_=[1, 1, 1] ,			a_=[1, 1, 1] ,			a_=0.02 ,			a_=1E-12 ,			**a_ ,			)				->					Union[str, Any]:


    """simple docstring"""
    super().__init__(**a_  )
    UpperCAmelCase    				=						num_channels
    UpperCAmelCase    				=						patch_sizes
    UpperCAmelCase    				=						patch_stride
    UpperCAmelCase    				=						patch_padding
    UpperCAmelCase    				=						embed_dim
    UpperCAmelCase    				=						num_heads
    UpperCAmelCase    				=						depth
    UpperCAmelCase    				=						mlp_ratio
    UpperCAmelCase    				=						attention_drop_rate
    UpperCAmelCase    				=						drop_rate
    UpperCAmelCase    				=						drop_path_rate
    UpperCAmelCase    				=						qkv_bias
    UpperCAmelCase    				=						cls_token
    UpperCAmelCase    				=						qkv_projection_method
    UpperCAmelCase    				=						kernel_qkv
    UpperCAmelCase    				=						padding_kv
    UpperCAmelCase    				=						stride_kv
    UpperCAmelCase    				=						padding_q
    UpperCAmelCase    				=						stride_q
    UpperCAmelCase    				=						initializer_range
    UpperCAmelCase    				=						layer_norm_eps


