'''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 _SCREAMING_SNAKE_CASE:




						def __init__(    self :       Optional[int]							,							UpperCamelCase_ :       str							,							UpperCamelCase_ :       Optional[Any]=14							,							UpperCamelCase_ :       int=7							,							UpperCamelCase_ :       Optional[Any]=True							,							UpperCamelCase_ :       Union[str, Any]=True							,							UpperCamelCase_ :       str=True							,							UpperCamelCase_ :       Tuple=True							,							UpperCamelCase_ :       Any=True							,							UpperCamelCase_ :       int=99							,							UpperCamelCase_ :       List[Any]=32							,							UpperCamelCase_ :       Union[str, Any]=5							,							UpperCamelCase_ :       int=4							,							UpperCamelCase_ :       Optional[int]=37							,							UpperCamelCase_ :       Any="gelu"							,							UpperCamelCase_ :       List[Any]=0.1							,							UpperCamelCase_ :       Any=0.1							,							UpperCamelCase_ :       Union[str, Any]=5_12							,							UpperCamelCase_ :       List[str]=16							,							UpperCamelCase_ :       List[Any]=2							,							UpperCamelCase_ :       Tuple=0.02							,							UpperCamelCase_ :       Dict=3							,							UpperCamelCase_ :       List[Any]=4							,							UpperCamelCase_ :       int=None							,							)     ->				Dict:
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = parent
													SCREAMING_SNAKE_CASE__     :int						    = batch_size
													SCREAMING_SNAKE_CASE__     :Any						    = seq_length
													SCREAMING_SNAKE_CASE__     :Dict						    = is_training
													SCREAMING_SNAKE_CASE__     :List[Any]						    = use_token_type_ids
													SCREAMING_SNAKE_CASE__     :int						    = use_input_mask
													SCREAMING_SNAKE_CASE__     :List[Any]						    = use_labels
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = use_mc_token_ids
													SCREAMING_SNAKE_CASE__     :List[str]						    = vocab_size
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = hidden_size
													SCREAMING_SNAKE_CASE__     :List[str]						    = num_hidden_layers
													SCREAMING_SNAKE_CASE__     :List[Any]						    = num_attention_heads
													SCREAMING_SNAKE_CASE__     :Tuple						    = intermediate_size
													SCREAMING_SNAKE_CASE__     :Any						    = hidden_act
													SCREAMING_SNAKE_CASE__     :Tuple						    = hidden_dropout_prob
													SCREAMING_SNAKE_CASE__     :Dict						    = attention_probs_dropout_prob
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = max_position_embeddings
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = type_vocab_size
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = type_sequence_label_size
													SCREAMING_SNAKE_CASE__     :int						    = initializer_range
													SCREAMING_SNAKE_CASE__     :List[Any]						    = num_labels
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = num_choices
													SCREAMING_SNAKE_CASE__     :Tuple						    = scope
													SCREAMING_SNAKE_CASE__     :Tuple						    = self.vocab_size - 1




						def      __lowerCamelCase       (    self :       List[Any]      )     ->				Dict:
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = ids_tensor([self.batch_size, self.seq_length]							,							self.vocab_size      )

													SCREAMING_SNAKE_CASE__     :Tuple						    = None
													if self.use_input_mask:
																				SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = random_attention_mask([self.batch_size, self.seq_length]      )

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

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

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

													SCREAMING_SNAKE_CASE__     :Dict						    = self.get_config()

													SCREAMING_SNAKE_CASE__     :Dict						    = 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      __lowerCamelCase       (    self :       Dict      )     ->				Optional[Any]:
													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      __lowerCamelCase       (    self :       List[str]							,							UpperCamelCase_ :       int							,							UpperCamelCase_ :       int							,							UpperCamelCase_ :       Any							,							UpperCamelCase_ :       Dict							,							UpperCamelCase_ :       Any							,							*UpperCamelCase_ :       Optional[Any]      )     ->				Optional[Any]:
													SCREAMING_SNAKE_CASE__     :str						    = CTRLModel(config=UpperCamelCase_      )
													model.to(UpperCamelCase_      )
													model.eval()

													model(UpperCamelCase_							,							token_type_ids=UpperCamelCase_							,							head_mask=UpperCamelCase_      )
													model(UpperCamelCase_							,							token_type_ids=UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = 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      __lowerCamelCase       (    self :       Any							,							UpperCamelCase_ :       str							,							UpperCamelCase_ :       str							,							UpperCamelCase_ :       Tuple							,							UpperCamelCase_ :       List[Any]							,							UpperCamelCase_ :       int							,							*UpperCamelCase_ :       List[str]      )     ->				Optional[int]:
													SCREAMING_SNAKE_CASE__     :Tuple						    = CTRLLMHeadModel(UpperCamelCase_      )
													model.to(UpperCamelCase_      )
													model.eval()

													SCREAMING_SNAKE_CASE__     :Tuple						    = 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      __lowerCamelCase       (    self :       Optional[Any]      )     ->				List[Any]:
													SCREAMING_SNAKE_CASE__     :Dict						    = 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__
													)    ,      
													)     :Any						    = config_and_inputs

													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'head_mask': head_mask}

													return config, inputs_dict




						def      __lowerCamelCase       (    self :       int							,							UpperCamelCase_ :       Dict							,							UpperCamelCase_ :       Optional[Any]							,							UpperCamelCase_ :       Any							,							UpperCamelCase_ :       Dict							,							*UpperCamelCase_ :       Dict      )     ->				Optional[Any]:
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = self.num_labels
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = CTRLForSequenceClassification(UpperCamelCase_      )
													model.to(UpperCamelCase_      )
													model.eval()
													SCREAMING_SNAKE_CASE__     :Any						    = ids_tensor([self.batch_size]							,							self.type_sequence_label_size      )
													SCREAMING_SNAKE_CASE__     :Tuple						    = model(UpperCamelCase_							,							token_type_ids=UpperCamelCase_							,							labels=UpperCamelCase_      )
													self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.num_labels)      )




@require_torch
class _SCREAMING_SNAKE_CASE(							_SCREAMING_SNAKE_CASE						,						_SCREAMING_SNAKE_CASE						,						_SCREAMING_SNAKE_CASE						,						unittest.TestCase					):
						A_			:							Optional[int]     		= (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
						A_			:							Dict     		= (CTRLLMHeadModel,) if is_torch_available() else ()
						A_			:							Union[str, Any]     		= (
						    {
						        'feature-extraction': CTRLModel,
						        'text-classification': CTRLForSequenceClassification,
						        'text-generation': CTRLLMHeadModel,
						        'zero-shot': CTRLForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						A_			:							str     		= True
						A_			:							Any     		= False
						A_			:							Any     		= False




						def      __lowerCamelCase       (    self :       Dict							,							UpperCamelCase_ :       Optional[Any]							,							UpperCamelCase_ :       Dict							,							UpperCamelCase_ :       Union[str, Any]							,							UpperCamelCase_ :       Tuple							,							UpperCamelCase_ :       Dict      )     ->				int:
													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      __lowerCamelCase       (    self :       Union[str, Any]      )     ->				Tuple:
													SCREAMING_SNAKE_CASE__     :str						    = CTRLModelTester(self      )
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = ConfigTester(self							,							config_class=UpperCamelCase_							,							n_embd=37      )




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




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




						def      __lowerCamelCase       (    self :       Union[str, Any]      )     ->				Union[str, Any]:
													SCREAMING_SNAKE_CASE__     :List[str]						    = self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_ctrl_model(*UpperCamelCase_      )




						def      __lowerCamelCase       (    self :       List[Any]      )     ->				Union[str, Any]:
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = 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      __lowerCamelCase       (    self :       Any      )     ->				Any:
													pass




						@slow
						def      __lowerCamelCase       (    self :       int      )     ->				Optional[int]:
													for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				SCREAMING_SNAKE_CASE__     :Dict						    = 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      __lowerCamelCase       (    self :       Optional[Any]      )     ->				Tuple:
													pass




@require_torch
class _SCREAMING_SNAKE_CASE(							unittest.TestCase					):




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




						@slow
						def      __lowerCamelCase       (    self :       Union[str, Any]      )     ->				Dict:
													SCREAMING_SNAKE_CASE__     :Dict						    = CTRLLMHeadModel.from_pretrained('ctrl'      )
													model.to(UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :List[str]						    = torch.tensor(
													    [[1_18_59, 0, 16_11, 8]]							,							dtype=torch.long							,							device=UpperCamelCase_      )  # Legal the president is
													SCREAMING_SNAKE_CASE__     :List[Any]						    = [
													    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__     :List[str]						    = model.generate(UpperCamelCase_							,							do_sample=UpperCamelCase_      )
													self.assertListEqual(output_ids[0].tolist()							,							UpperCamelCase_      )







'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


UpperCamelCase_      	=	{
    '''configuration_x_clip''': [
        '''XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''XCLIPConfig''',
        '''XCLIPTextConfig''',
        '''XCLIPVisionConfig''',
    ],
    '''processing_x_clip''': ['''XCLIPProcessor'''],
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		UpperCamelCase_      	=	[
		    '''XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''XCLIPModel''',
		    '''XCLIPPreTrainedModel''',
		    '''XCLIPTextModel''',
		    '''XCLIPVisionModel''',
		]

if TYPE_CHECKING:
		from .configuration_x_clip import (
		    XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    XCLIPConfig,
		    XCLIPTextConfig,
		    XCLIPVisionConfig,
		)
		from .processing_x_clip import XCLIPProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_x_clip import (
				    XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
				    XCLIPModel,
				    XCLIPPreTrainedModel,
				    XCLIPTextModel,
				    XCLIPVisionModel,
				)

else:
		import sys

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






