

'''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     :		List[Any]		,					UpperCAmelCase__     :		List[str]		,					UpperCAmelCase__     :		str=14		,					UpperCAmelCase__     :		Tuple=7		,					UpperCAmelCase__     :		Any=True		,					UpperCAmelCase__     :		Tuple=True		,					UpperCAmelCase__     :		Optional[Any]=True		,					UpperCAmelCase__     :		Optional[int]=True		,					UpperCAmelCase__     :		List[Any]=True		,					UpperCAmelCase__     :		Any=99		,					UpperCAmelCase__     :		Union[str, Any]=32		,					UpperCAmelCase__     :		Dict=5		,					UpperCAmelCase__     :		Union[str, Any]=4		,					UpperCAmelCase__     :		List[Any]=37		,					UpperCAmelCase__     :		Optional[int]="gelu"		,					UpperCAmelCase__     :		str=0.1		,					UpperCAmelCase__     :		int=0.1		,					UpperCAmelCase__     :		Dict=512		,					UpperCAmelCase__     :		Tuple=16		,					UpperCAmelCase__     :		List[str]=2		,					UpperCAmelCase__     :		int=0.02		,					UpperCAmelCase__     :		Dict=3		,					UpperCAmelCase__     :		Tuple=4		,					UpperCAmelCase__     :		Dict=None		,					):
										'''simple docstring'''
										lowercase :							List[str]    		=parent
										lowercase :							Union[str, Any]    		=batch_size
										lowercase :							Optional[int]    		=seq_length
										lowercase :							List[Any]    		=is_training
										lowercase :							List[Any]    		=use_token_type_ids
										lowercase :							str    		=use_input_mask
										lowercase :							Dict    		=use_labels
										lowercase :							Optional[int]    		=use_mc_token_ids
										lowercase :							Union[str, Any]    		=vocab_size
										lowercase :							int    		=hidden_size
										lowercase :							Optional[int]    		=num_hidden_layers
										lowercase :							Optional[Any]    		=num_attention_heads
										lowercase :							Optional[Any]    		=intermediate_size
										lowercase :							int    		=hidden_act
										lowercase :							str    		=hidden_dropout_prob
										lowercase :							int    		=attention_probs_dropout_prob
										lowercase :							str    		=max_position_embeddings
										lowercase :							Tuple    		=type_vocab_size
										lowercase :							Union[str, Any]    		=type_sequence_label_size
										lowercase :							List[Any]    		=initializer_range
										lowercase :							Any    		=num_labels
										lowercase :							Optional[int]    		=num_choices
										lowercase :							List[Any]    		=scope
										lowercase :							Union[str, Any]    		=self.vocab_size - 1







				def  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										lowercase :							Any    		=ids_tensor([self.batch_size, self.seq_length]		,					self.vocab_size   )

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

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

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

										lowercase :							Dict    		=None
										lowercase :							Any    		=None
										lowercase :							Optional[Any]    		=None
										if self.use_labels:
																lowercase :							Tuple    		=ids_tensor([self.batch_size]		,					self.type_sequence_label_size   )
																lowercase :							Any    		=ids_tensor([self.batch_size, self.seq_length]		,					self.num_labels   )
																lowercase :							Optional[Any]    		=ids_tensor([self.batch_size]		,					self.num_choices   )

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

										lowercase :							Tuple    		=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     :		List[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  lowerCamelCase_	(       self     :		Any		,					UpperCAmelCase__     :		Optional[Any]		,					UpperCAmelCase__     :		Tuple		,					UpperCAmelCase__     :		List[str]		,					UpperCAmelCase__     :		Optional[int]		,					UpperCAmelCase__     :		Tuple		,					*UpperCAmelCase__     :		Any   ):
										'''simple docstring'''
										lowercase :							int    		=CTRLModel(config=UpperCAmelCase__   )
										model.to(UpperCAmelCase__   )
										model.eval()

										model(UpperCAmelCase__		,					token_type_ids=UpperCAmelCase__		,					head_mask=UpperCAmelCase__   )
										model(UpperCAmelCase__		,					token_type_ids=UpperCAmelCase__   )
										lowercase :							int    		=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     :		Tuple		,					UpperCAmelCase__     :		Tuple		,					UpperCAmelCase__     :		Dict		,					UpperCAmelCase__     :		Dict		,					UpperCAmelCase__     :		List[Any]		,					UpperCAmelCase__     :		Tuple		,					*UpperCAmelCase__     :		Tuple   ):
										'''simple docstring'''
										lowercase :							int    		=CTRLLMHeadModel(UpperCAmelCase__   )
										model.to(UpperCAmelCase__   )
										model.eval()

										lowercase :							List[str]    		=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     :		Dict   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=self.prepare_config_and_inputs()

										(
										    (
										    lowercase
										)		,	(
										    lowercase
										)		,	(
										    lowercase
										)		,	(
										    lowercase
										)		,	(
										    lowercase
										)		,	(
										    lowercase
										)		,	(
										    lowercase
										)		,	(
										    lowercase
										)		,	(
										    lowercase
										)		,	
										) :							Optional[int]    		=config_and_inputs

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

										return config, inputs_dict







				def  lowerCamelCase_	(       self     :		Tuple		,					UpperCAmelCase__     :		Optional[int]		,					UpperCAmelCase__     :		List[str]		,					UpperCAmelCase__     :		Any		,					UpperCAmelCase__     :		List[str]		,					*UpperCAmelCase__     :		str   ):
										'''simple docstring'''
										lowercase :							Optional[int]    		=self.num_labels
										lowercase :							Dict    		=CTRLForSequenceClassification(UpperCAmelCase__   )
										model.to(UpperCAmelCase__   )
										model.eval()
										lowercase :							Tuple    		=ids_tensor([self.batch_size]		,					self.type_sequence_label_size   )
										lowercase :							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							(			lowercase__						,							lowercase__						,							lowercase__						,							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  lowerCamelCase_	(       self     :		int		,					UpperCAmelCase__     :		Optional[Any]		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		Optional[Any]		,					UpperCAmelCase__     :		Any		,					UpperCAmelCase__     :		List[str]   ):
										'''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  lowerCamelCase_	(       self     :		List[Any]   ):
										'''simple docstring'''
										lowercase :							str    		=CTRLModelTester(self   )
										lowercase :							Any    		=ConfigTester(self		,					config_class=UpperCAmelCase__		,					n_embd=37   )







				def  lowerCamelCase_	(       self     :		Optional[Any]   ):
										'''simple docstring'''
										super().tearDown()
										# clean-up as much as possible GPU memory occupied by PyTorch
										gc.collect()
										torch.cuda.empty_cache()







				def  lowerCamelCase_	(       self     :		str   ):
										'''simple docstring'''
										self.config_tester.run_common_tests()







				def  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										lowercase :							Union[str, Any]    		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_ctrl_model(*UpperCAmelCase__   )







				def  lowerCamelCase_	(       self     :		List[Any]   ):
										'''simple docstring'''
										lowercase :							str    		=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     :		int   ):
										'''simple docstring'''
										pass







				@slow
				def  lowerCamelCase_	(       self     :		Union[str, Any]   ):
										'''simple docstring'''
										for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																lowercase :							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     :		Any   ):
										'''simple docstring'''
										pass





@require_torch
class 		__SCREAMING_SNAKE_CASE							(			unittest.TestCase      ):







				def  lowerCamelCase_	(       self     :		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  lowerCamelCase_	(       self     :		int   ):
										'''simple docstring'''
										lowercase :							str    		=CTRLLMHeadModel.from_pretrained('''ctrl'''   )
										model.to(UpperCAmelCase__   )
										lowercase :							str    		=torch.tensor(
										    [[11859, 0, 1611, 8]]		,					dtype=torch.long		,					device=UpperCAmelCase__   )  # Legal the president is
										lowercase :							Optional[Any]    		=[
										    11859,
										    0,
										    1611,
										    8,
										    5,
										    150,
										    26449,
										    2,
										    19,
										    348,
										    469,
										    3,
										    2595,
										    48,
										    20740,
										    246533,
										    246533,
										    19,
										    30,
										    5,
										]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

										lowercase :							List[Any]    		=model.generate(UpperCAmelCase__		,					do_sample=UpperCAmelCase__   )
										self.assertListEqual(output_ids[0].tolist()		,					UpperCAmelCase__   )







import warnings

from ...utils import logging
from .image_processing_flava import FlavaImageProcessor


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




class   __lowercase						(						__snake_case       ):






	def __init__(self				: int      ,	*snake_case				: Optional[Any]      ,	**snake_case				: Optional[Any]    ) ->			None:
							warnings.warn(
							    "The class FlavaFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please"
							    " use FlavaImageProcessor instead."      ,	snake_case      ,	)
							super().__init__(*snake_case      ,	**snake_case    )




