



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	,__lowerCamelCase	,__lowerCamelCase=14	,__lowerCamelCase=7	,__lowerCamelCase=True	,__lowerCamelCase=True	,__lowerCamelCase=True	,__lowerCamelCase=True	,__lowerCamelCase=True	,__lowerCamelCase=99	,__lowerCamelCase=32	,__lowerCamelCase=5	,__lowerCamelCase=4	,__lowerCamelCase=37	,__lowerCamelCase="gelu"	,__lowerCamelCase=0.1	,__lowerCamelCase=0.1	,__lowerCamelCase=5_12	,__lowerCamelCase=16	,__lowerCamelCase=2	,__lowerCamelCase=0.02	,__lowerCamelCase=3	,__lowerCamelCase=4	,__lowerCamelCase=None	,)  ->    int:

						"""simple docstring"""






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



		def 	lowerCAmelCase__   (self				)  ->    str:

						"""simple docstring"""






						lowerCAmelCase__  :       str				=  ids_tensor([self.batch_size, self.seq_length]	,self.vocab_size				)

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

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

						lowerCAmelCase__  :       Optional[int]				=  None
						if self.use_mc_token_ids:
										lowerCAmelCase__  :       Dict				=  ids_tensor([self.batch_size, self.num_choices]	,self.seq_length				)

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

						lowerCAmelCase__  :       Optional[Any]				=  self.get_config()

						lowerCAmelCase__  :       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				)  ->    int:

						"""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	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,*__lowerCamelCase				)  ->    Any:

						"""simple docstring"""






						lowerCAmelCase__  :       Dict				=  CTRLModel(config=__lowerCamelCase				)
						model.to(__lowerCamelCase				)
						model.eval()

						model(__lowerCamelCase	,token_type_ids=__lowerCamelCase	,head_mask=__lowerCamelCase				)
						model(__lowerCamelCase	,token_type_ids=__lowerCamelCase				)
						lowerCAmelCase__  :       Optional[Any]				=  model(__lowerCamelCase				)
						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	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,*__lowerCamelCase				)  ->    Dict:

						"""simple docstring"""






						lowerCAmelCase__  :       Tuple				=  CTRLLMHeadModel(__lowerCamelCase				)
						model.to(__lowerCamelCase				)
						model.eval()

						lowerCAmelCase__  :       List[Any]				=  model(__lowerCamelCase	,token_type_ids=__lowerCamelCase	,labels=__lowerCamelCase				)
						self.parent.assertEqual(result.loss.shape	,()				)
						self.parent.assertEqual(result.logits.shape	,(self.batch_size, self.seq_length, self.vocab_size)				)



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

						"""simple docstring"""






						lowerCAmelCase__  :       str				=  self.prepare_config_and_inputs()

						(
						    (
						    lowerCAmelCase__
						)		,	(
						    lowerCAmelCase__
						)		,	(
						    lowerCAmelCase__
						)		,	(
						    lowerCAmelCase__
						)		,	(
						    lowerCAmelCase__
						)		,	(
						    lowerCAmelCase__
						)		,	(
						    lowerCAmelCase__
						)		,	(
						    lowerCAmelCase__
						)		,	(
						    lowerCAmelCase__
						)		,	
						)  :       Union[str, Any]				=  config_and_inputs

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

						return config, inputs_dict



		def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,*__lowerCamelCase				)  ->    str:

						"""simple docstring"""






						lowerCAmelCase__  :       Union[str, Any]				=  self.num_labels
						lowerCAmelCase__  :       Any				=  CTRLForSequenceClassification(__lowerCamelCase				)
						model.to(__lowerCamelCase				)
						model.eval()
						lowerCAmelCase__  :       Dict				=  ids_tensor([self.batch_size]	,self.type_sequence_label_size				)
						lowerCAmelCase__  :       List[str]				=  model(__lowerCamelCase	,token_type_ids=__lowerCamelCase	,labels=__lowerCamelCase				)
						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_			=(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
		snake_case_			=(CTRLLMHeadModel,) if is_torch_available() else ()
		snake_case_			=(
		    {
		        """feature-extraction""": CTRLModel,
		        """text-classification""": CTRLForSequenceClassification,
		        """text-generation""": CTRLLMHeadModel,
		        """zero-shot""": CTRLForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		snake_case_			=True
		snake_case_			=False
		snake_case_			=False



		def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase				)  ->    Optional[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 	lowerCAmelCase__   (self				)  ->    Any:

						"""simple docstring"""






						lowerCAmelCase__  :       str				=  CTRLModelTester(self				)
						lowerCAmelCase__  :       Union[str, Any]				=  ConfigTester(self	,config_class=__lowerCamelCase	,n_embd=37				)



		def 	lowerCAmelCase__   (self				)  ->    str:

						"""simple docstring"""






						super().tearDown()
						# clean-up as much as possible GPU memory occupied by PyTorch
						gc.collect()
						torch.cuda.empty_cache()



		def 	lowerCAmelCase__   (self				)  ->    Dict:

						"""simple docstring"""






						self.config_tester.run_common_tests()



		def 	lowerCAmelCase__   (self				)  ->    Tuple:

						"""simple docstring"""






						lowerCAmelCase__  :       Optional[Any]				=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_ctrl_model(*__lowerCamelCase				)



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

						"""simple docstring"""






						lowerCAmelCase__  :       List[Any]				=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_lm_head_model(*__lowerCamelCase				)



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

						"""simple docstring"""






						pass



		@slow
		def 	lowerCAmelCase__   (self				)  ->    str:

						"""simple docstring"""






						for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase__  :       Union[str, Any]				=  CTRLModel.from_pretrained(__lowerCamelCase				)
										self.assertIsNotNone(__lowerCamelCase				)



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

						"""simple docstring"""






						pass



@require_torch
class  lowerCamelCase__    (							unittest.TestCase):




		'''simple docstring'''



		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()



		@slow
		def 	lowerCAmelCase__   (self				)  ->    List[Any]:

						"""simple docstring"""






						lowerCAmelCase__  :       Tuple				=  CTRLLMHeadModel.from_pretrained('''ctrl'''				)
						model.to(__lowerCamelCase				)
						lowerCAmelCase__  :       Optional[Any]				=  torch.tensor(
						    [[1_18_59, 0, 16_11, 8]]	,dtype=torch.long	,device=__lowerCamelCase				)  # Legal the president is
						lowerCAmelCase__  :       List[str]				=  [
						    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

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






from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


__snake_case       :	str            =logging.get_logger(__name__)


if is_vision_available():
							import PIL



class  lowerCamelCase__    (							lowerCamelCase__):




		'''simple docstring'''



		snake_case_			=["""pixel_values"""]



		def __init__(self	,__lowerCamelCase = True	,__lowerCamelCase = None	,__lowerCamelCase = PILImageResampling.BICUBIC	,__lowerCamelCase = True	,__lowerCamelCase = None	,__lowerCamelCase = True	,__lowerCamelCase = 1 / 2_55	,__lowerCamelCase = True	,__lowerCamelCase = None	,__lowerCamelCase = None	,__lowerCamelCase = True	,**__lowerCamelCase	,)  ->    None:

						"""simple docstring"""






						super().__init__(**__lowerCamelCase				)
						lowerCAmelCase__  :       Union[str, Any]				=  size if size is not None else {'''shortest_edge''': 2_24}
						lowerCAmelCase__  :       Union[str, Any]				=  get_size_dict(__lowerCamelCase	,default_to_square=__lowerCamelCase				)
						lowerCAmelCase__  :       List[Any]				=  crop_size if crop_size is not None else {'''height''': 2_24, '''width''': 2_24}
						lowerCAmelCase__  :       Optional[int]				=  get_size_dict(__lowerCamelCase	,default_to_square=__lowerCamelCase	,param_name='''crop_size'''				)

						lowerCAmelCase__  :       Optional[int]				=  do_resize
						lowerCAmelCase__  :       Any				=  size
						lowerCAmelCase__  :       int				=  resample
						lowerCAmelCase__  :       Dict				=  do_center_crop
						lowerCAmelCase__  :       str				=  crop_size
						lowerCAmelCase__  :       Dict				=  do_rescale
						lowerCAmelCase__  :       Optional[Any]				=  rescale_factor
						lowerCAmelCase__  :       Dict				=  do_normalize
						lowerCAmelCase__  :       Tuple				=  image_mean if image_mean is not None else OPENAI_CLIP_MEAN
						lowerCAmelCase__  :       Union[str, Any]				=  image_std if image_std is not None else OPENAI_CLIP_STD
						lowerCAmelCase__  :       int				=  do_convert_rgb



		def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase = PILImageResampling.BICUBIC	,__lowerCamelCase = None	,**__lowerCamelCase	,)  ->    np.ndarray:

						"""simple docstring"""






						lowerCAmelCase__  :       Optional[int]				=  get_size_dict(__lowerCamelCase	,default_to_square=__lowerCamelCase				)
						if "shortest_edge" not in size:
										raise ValueError(f"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"""				)
						lowerCAmelCase__  :       Optional[int]				=  get_resize_output_image_size(__lowerCamelCase	,size=size['''shortest_edge''']	,default_to_square=__lowerCamelCase				)
						return resize(__lowerCamelCase	,size=__lowerCamelCase	,resample=__lowerCamelCase	,data_format=__lowerCamelCase	,**__lowerCamelCase				)



		def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase = None	,**__lowerCamelCase	,)  ->    np.ndarray:

						"""simple docstring"""






						lowerCAmelCase__  :       List[Any]				=  get_size_dict(__lowerCamelCase				)
						if "height" not in size or "width" not in size:
										raise ValueError(f"""The `size` parameter must contain the keys (height, width). Got {size.keys()}"""				)
						return center_crop(__lowerCamelCase	,size=(size['''height'''], size['''width'''])	,data_format=__lowerCamelCase	,**__lowerCamelCase				)



		def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase = None	,**__lowerCamelCase	,)  ->    int:

						"""simple docstring"""






						return rescale(__lowerCamelCase	,scale=__lowerCamelCase	,data_format=__lowerCamelCase	,**__lowerCamelCase				)



		def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase	,__lowerCamelCase = None	,**__lowerCamelCase	,)  ->    np.ndarray:

						"""simple docstring"""






						return normalize(__lowerCamelCase	,mean=__lowerCamelCase	,std=__lowerCamelCase	,data_format=__lowerCamelCase	,**__lowerCamelCase				)



		def 	lowerCAmelCase__   (self	,__lowerCamelCase	,__lowerCamelCase = None	,__lowerCamelCase = None	,__lowerCamelCase = None	,__lowerCamelCase = None	,__lowerCamelCase = None	,__lowerCamelCase = None	,__lowerCamelCase = None	,__lowerCamelCase = None	,__lowerCamelCase = None	,__lowerCamelCase = None	,__lowerCamelCase = None	,__lowerCamelCase = None	,__lowerCamelCase = ChannelDimension.FIRST	,**__lowerCamelCase	,)  ->    PIL.Image.Image:

						"""simple docstring"""






						lowerCAmelCase__  :       Tuple				=  do_resize if do_resize is not None else self.do_resize
						lowerCAmelCase__  :       Tuple				=  size if size is not None else self.size
						lowerCAmelCase__  :       Any				=  get_size_dict(__lowerCamelCase	,param_name='''size'''	,default_to_square=__lowerCamelCase				)
						lowerCAmelCase__  :       Tuple				=  resample if resample is not None else self.resample
						lowerCAmelCase__  :       Dict				=  do_center_crop if do_center_crop is not None else self.do_center_crop
						lowerCAmelCase__  :       List[Any]				=  crop_size if crop_size is not None else self.crop_size
						lowerCAmelCase__  :       str				=  get_size_dict(__lowerCamelCase	,param_name='''crop_size'''	,default_to_square=__lowerCamelCase				)
						lowerCAmelCase__  :       Dict				=  do_rescale if do_rescale is not None else self.do_rescale
						lowerCAmelCase__  :       int				=  rescale_factor if rescale_factor is not None else self.rescale_factor
						lowerCAmelCase__  :       List[Any]				=  do_normalize if do_normalize is not None else self.do_normalize
						lowerCAmelCase__  :       Dict				=  image_mean if image_mean is not None else self.image_mean
						lowerCAmelCase__  :       int				=  image_std if image_std is not None else self.image_std
						lowerCAmelCase__  :       Dict				=  do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

						lowerCAmelCase__  :       Optional[int]				=  make_list_of_images(__lowerCamelCase				)

						if not valid_images(__lowerCamelCase				):
										raise ValueError(
										    '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
										    '''torch.Tensor, tf.Tensor or jax.ndarray.'''				)

						if do_resize and size is None:
										raise ValueError('''Size must be specified if do_resize is True.'''				)

						if do_center_crop and crop_size is None:
										raise ValueError('''Crop size must be specified if do_center_crop is True.'''				)

						if do_rescale and rescale_factor is None:
										raise ValueError('''Rescale factor must be specified if do_rescale is True.'''				)

						if do_normalize and (image_mean is None or image_std is None):
										raise ValueError('''Image mean and std must be specified if do_normalize is True.'''				)

						# PIL RGBA images are converted to RGB
						if do_convert_rgb:
										lowerCAmelCase__  :       Union[str, Any]				=  [convert_to_rgb(__lowerCamelCase				) for image in images]

						# All transformations expect numpy arrays.
						lowerCAmelCase__  :       Any				=  [to_numpy_array(__lowerCamelCase				) for image in images]

						if do_resize:
										lowerCAmelCase__  :       str				=  [self.resize(image=__lowerCamelCase	,size=__lowerCamelCase	,resample=__lowerCamelCase				) for image in images]

						if do_center_crop:
										lowerCAmelCase__  :       Dict				=  [self.center_crop(image=__lowerCamelCase	,size=__lowerCamelCase				) for image in images]

						if do_rescale:
										lowerCAmelCase__  :       Union[str, Any]				=  [self.rescale(image=__lowerCamelCase	,scale=__lowerCamelCase				) for image in images]

						if do_normalize:
										lowerCAmelCase__  :       Optional[Any]				=  [self.normalize(image=__lowerCamelCase	,mean=__lowerCamelCase	,std=__lowerCamelCase				) for image in images]

						lowerCAmelCase__  :       List[Any]				=  [to_channel_dimension_format(__lowerCamelCase	,__lowerCamelCase				) for image in images]

						lowerCAmelCase__  :       Optional[int]				=  {'''pixel_values''': images}
						return BatchFeature(data=__lowerCamelCase	,tensor_type=__lowerCamelCase				)

