




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

    '''simple docstring'''






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





           '''simple docstring'''

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


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





           '''simple docstring'''

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

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

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

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

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

           A__   				=					self.get_config()

           A__   				=					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  SCREAMING_SNAKE_CASE							(    self   :			Optional[Any])		->List[str]:





           '''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  SCREAMING_SNAKE_CASE							(    self   :			Dict      ,				UpperCAmelCase__   :			List[Any]      ,				UpperCAmelCase__   :			Dict      ,				UpperCAmelCase__   :			List[Any]      ,				UpperCAmelCase__   :			Dict      ,				UpperCAmelCase__   :			List[str]      ,				*UpperCAmelCase__   :			int)		->Optional[int]:





           '''simple docstring'''

           A__   				=					CTRLModel(config=UpperCAmelCase__)
           model.to(UpperCAmelCase__)
           model.eval()

           model(UpperCAmelCase__      ,				token_type_ids=UpperCAmelCase__      ,				head_mask=UpperCAmelCase__)
           model(UpperCAmelCase__      ,				token_type_ids=UpperCAmelCase__)
           A__   				=					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  SCREAMING_SNAKE_CASE							(    self   :			Tuple      ,				UpperCAmelCase__   :			str      ,				UpperCAmelCase__   :			Dict      ,				UpperCAmelCase__   :			Dict      ,				UpperCAmelCase__   :			Union[str, Any]      ,				UpperCAmelCase__   :			int      ,				*UpperCAmelCase__   :			int)		->List[Any]:





           '''simple docstring'''

           A__   				=					CTRLLMHeadModel(UpperCAmelCase__)
           model.to(UpperCAmelCase__)
           model.eval()

           A__   				=					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  SCREAMING_SNAKE_CASE							(    self   :			int)		->int:





           '''simple docstring'''

           A__   				=					self.prepare_config_and_inputs()

           (
               (
               A__
           )				,							(
               A__
           )				,							(
               A__
           )				,							(
               A__
           )				,							(
               A__
           )				,							(
               A__
           )				,							(
               A__
           )				,							(
               A__
           )				,							(
               A__
           )				,							
           )   				=					config_and_inputs

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

           return config, inputs_dict





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





           '''simple docstring'''

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


@require_torch
class        UpperCamelCase_					(				UpperCAmelCase__	,      UpperCAmelCase__	,      UpperCAmelCase__	,      unittest.TestCase  ):

    '''simple docstring'''






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


    def  SCREAMING_SNAKE_CASE							(    self   :			Tuple      ,				UpperCAmelCase__   :			Any      ,				UpperCAmelCase__   :			Union[str, Any]      ,				UpperCAmelCase__   :			Union[str, Any]      ,				UpperCAmelCase__   :			List[str]      ,				UpperCAmelCase__   :			List[str])		->Optional[int]:





           '''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  SCREAMING_SNAKE_CASE							(    self   :			Dict)		->Any:





           '''simple docstring'''

           A__   				=					CTRLModelTester(self)
           A__   				=					ConfigTester(self      ,				config_class=UpperCAmelCase__      ,				n_embd=37)


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





           '''simple docstring'''

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


    def  SCREAMING_SNAKE_CASE							(    self   :			Optional[Any])		->Optional[int]:





           '''simple docstring'''

           self.config_tester.run_common_tests()


    def  SCREAMING_SNAKE_CASE							(    self   :			List[str])		->str:





           '''simple docstring'''

           A__   				=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_ctrl_model(*UpperCAmelCase__)


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





           '''simple docstring'''

           A__   				=					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  SCREAMING_SNAKE_CASE							(    self   :			Dict)		->str:





           '''simple docstring'''

           pass


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





           '''simple docstring'''

           for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  A__   				=					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  SCREAMING_SNAKE_CASE							(    self   :			List[Any])		->Optional[int]:





           '''simple docstring'''

           pass


@require_torch
class        UpperCamelCase_					(				unittest.TestCase  ):

    '''simple docstring'''






    def  SCREAMING_SNAKE_CASE							(    self   :			Optional[int])		->Tuple:





           '''simple docstring'''

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





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





           '''simple docstring'''

           A__   				=					CTRLLMHeadModel.from_pretrained('''ctrl''')
           model.to(UpperCAmelCase__)
           A__   				=					torch.tensor(
               [[11_859, 0, 1_611, 8]]      ,				dtype=torch.long      ,				device=UpperCAmelCase__)  # Legal the president is
           A__   				=					[
               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

           A__   				=					model.generate(UpperCAmelCase__      ,				do_sample=UpperCAmelCase__)
           self.assertListEqual(output_ids[0].tolist()      ,				UpperCAmelCase__)








from typing import List, Optional, Union

import numpy as np
import PIL.Image

from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import rescale, resize, to_channel_dimension_format
from ...image_utils import (
    ChannelDimension,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


_lowerCamelCase			:	str							 =     logging.get_logger(__name__)


class        UpperCamelCase_					(				UpperCAmelCase__  ):

    '''simple docstring'''






    UpperCAmelCase__													=   ['''pixel_values''']


    def __init__(    self   :			List[Any]      ,				UpperCAmelCase__   :			bool = True      ,				UpperCAmelCase__   :			int = 32      ,				UpperCAmelCase__   :			Dict=PILImageResampling.BILINEAR      ,				UpperCAmelCase__   :			bool = True      ,				**UpperCAmelCase__   :			Optional[int]      ,				)		->None:





           '''simple docstring'''

           A__   				=					do_resize
           A__   				=					do_rescale
           A__   				=					size_divisor
           A__   				=					resample
           super().__init__(**UpperCAmelCase__)


    def  SCREAMING_SNAKE_CASE							(    self   :			int      ,				UpperCAmelCase__   :			np.ndarray      ,				UpperCAmelCase__   :			int      ,				UpperCAmelCase__   :			List[str]      ,				UpperCAmelCase__   :			Optional[ChannelDimension] = None      ,				**UpperCAmelCase__   :			Dict)		->np.ndarray:





           '''simple docstring'''

           A__				,							A__   				=					get_image_size(UpperCAmelCase__)
           # Rounds the height and width down to the closest multiple of size_divisor
           A__   				=					height // size_divisor * size_divisor
           A__   				=					width // size_divisor * size_divisor
           A__   				=					resize(UpperCAmelCase__      ,				(new_h, new_w)      ,				resample=UpperCAmelCase__      ,				data_format=UpperCAmelCase__      ,				**UpperCAmelCase__)
           return image


    def  SCREAMING_SNAKE_CASE							(    self   :			List[Any]      ,				UpperCAmelCase__   :			np.ndarray      ,				UpperCAmelCase__   :			float      ,				UpperCAmelCase__   :			Optional[ChannelDimension] = None      ,				**UpperCAmelCase__   :			List[str])		->np.ndarray:





           '''simple docstring'''

           return rescale(image=UpperCAmelCase__      ,				scale=UpperCAmelCase__      ,				data_format=UpperCAmelCase__      ,				**UpperCAmelCase__)





    def  SCREAMING_SNAKE_CASE							(    self   :			Optional[int]      ,				UpperCAmelCase__   :			Union["PIL.Image.Image", TensorType, List["PIL.Image.Image"], List[TensorType]]      ,				UpperCAmelCase__   :			Optional[bool] = None      ,				UpperCAmelCase__   :			Optional[int] = None      ,				UpperCAmelCase__   :			Dict=None      ,				UpperCAmelCase__   :			Optional[bool] = None      ,				UpperCAmelCase__   :			Optional[Union[TensorType, str]] = None      ,				UpperCAmelCase__   :			ChannelDimension = ChannelDimension.FIRST      ,				**UpperCAmelCase__   :			List[str]      ,				)		->BatchFeature:





           '''simple docstring'''

           A__   				=					do_resize if do_resize is not None else self.do_resize
           A__   				=					do_rescale if do_rescale is not None else self.do_rescale
           A__   				=					size_divisor if size_divisor is not None else self.size_divisor
           A__   				=					resample if resample is not None else self.resample

           if do_resize and size_divisor is None:
                  raise ValueError('''size_divisor is required for resizing''')

           A__   				=					make_list_of_images(UpperCAmelCase__)

           if not valid_images(UpperCAmelCase__):
                  raise ValueError('''Invalid image(s)''')

           # All transformations expect numpy arrays.
           A__   				=					[to_numpy_array(UpperCAmelCase__) for img in images]

           if do_resize:
                  A__   				=					[self.resize(UpperCAmelCase__      ,				size_divisor=UpperCAmelCase__      ,				resample=UpperCAmelCase__) for image in images]

           if do_rescale:
                  A__   				=					[self.rescale(UpperCAmelCase__      ,				scale=1 / 255) for image in images]

           A__   				=					[to_channel_dimension_format(UpperCAmelCase__      ,				UpperCAmelCase__) for image in images]

           A__   				=					{'''pixel_values''': images}
           return BatchFeature(data=UpperCAmelCase__      ,				tensor_type=UpperCAmelCase__)


