






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



  def __init__(      self ,		snake_case__ ,		snake_case__=14 ,		snake_case__=7 ,		snake_case__=True ,		snake_case__=True ,		snake_case__=True ,		snake_case__=True ,		snake_case__=True ,		snake_case__=99 ,		snake_case__=32 ,		snake_case__=5 ,		snake_case__=4 ,		snake_case__=37 ,		snake_case__="gelu" ,		snake_case__=0.1 ,		snake_case__=0.1 ,		snake_case__=5_12 ,		snake_case__=16 ,		snake_case__=2 ,		snake_case__=0.02 ,		snake_case__=3 ,		snake_case__=4 ,		snake_case__=None ,		)			->			List[Any]:



         """simple docstring"""





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



  def 		UpperCamelCase_				(      self				)			->			Tuple:



         """simple docstring"""





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

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

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

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

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

         UpperCAmelCase	    =			self.get_config()

         UpperCAmelCase	    =			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 		UpperCamelCase_				(      self				)			->			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 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		*snake_case__				)			->			List[str]:



         """simple docstring"""





         UpperCAmelCase	    =			CTRLModel(config=snake_case__				)
         model.to(snake_case__				)
         model.eval()

         model(snake_case__ ,		token_type_ids=snake_case__ ,		head_mask=snake_case__				)
         model(snake_case__ ,		token_type_ids=snake_case__				)
         UpperCAmelCase	    =			model(snake_case__				)
         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 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		*snake_case__				)			->			Tuple:



         """simple docstring"""





         UpperCAmelCase	    =			CTRLLMHeadModel(snake_case__				)
         model.to(snake_case__				)
         model.eval()

         UpperCAmelCase	    =			model(snake_case__ ,		token_type_ids=snake_case__ ,		labels=snake_case__				)
         self.parent.assertEqual(result.loss.shape ,		()				)
         self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.seq_length, self.vocab_size)				)



  def 		UpperCamelCase_				(      self				)			->			Optional[int]:



         """simple docstring"""





         UpperCAmelCase	    =			self.prepare_config_and_inputs()

         (
             (
             UpperCAmelCase       
         )    ,       (
             UpperCAmelCase       
         )    ,       (
             UpperCAmelCase       
         )    ,       (
             UpperCAmelCase       
         )    ,       (
             UpperCAmelCase       
         )    ,       (
             UpperCAmelCase       
         )    ,       (
             UpperCAmelCase       
         )    ,       (
             UpperCAmelCase       
         )    ,       (
             UpperCAmelCase       
         )    ,              
         )	    =			config_and_inputs

         UpperCAmelCase	    =			{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask}

         return config, inputs_dict



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		*snake_case__				)			->			str:



         """simple docstring"""





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





@require_torch
class   UpperCamelCase_ (       a_   ,					a_   ,					a_   ,					unittest.TestCase  ):
  _A       :					Any				=	(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
  _A       :					Tuple				=	(CTRLLMHeadModel,) if is_torch_available() else ()
  _A       :					int				=	(
      {
          'feature-extraction': CTRLModel,
          'text-classification': CTRLForSequenceClassification,
          'text-generation': CTRLLMHeadModel,
          'zero-shot': CTRLForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  _A       :					List[Any]				=	True
  _A       :					List[Any]				=	False
  _A       :					str				=	False



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__				)			->			List[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 		UpperCamelCase_				(      self				)			->			Dict:



         """simple docstring"""





         UpperCAmelCase	    =			CTRLModelTester(self				)
         UpperCAmelCase	    =			ConfigTester(self ,		config_class=snake_case__ ,		n_embd=37				)



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



         """simple docstring"""





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



  def 		UpperCamelCase_				(      self				)			->			Optional[Any]:



         """simple docstring"""





         self.config_tester.run_common_tests()



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



         """simple docstring"""





         UpperCAmelCase	    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_ctrl_model(*snake_case__				)



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



         """simple docstring"""





         UpperCAmelCase	    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_lm_head_model(*snake_case__				)



  @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""				)
  def 		UpperCamelCase_				(      self				)			->			Optional[int]:



         """simple docstring"""





         pass



  @slow
  def 		UpperCamelCase_				(      self				)			->			Union[str, Any]:



         """simple docstring"""





         for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase	    =			CTRLModel.from_pretrained(snake_case__				)
                self.assertIsNotNone(snake_case__				)



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



         """simple docstring"""





         pass





@require_torch
class   UpperCamelCase_ (       unittest.TestCase  ):



  def 		UpperCamelCase_				(      self				)			->			Tuple:



         """simple docstring"""





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



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



         """simple docstring"""





         UpperCAmelCase	    =			CTRLLMHeadModel.from_pretrained("""ctrl"""				)
         model.to(snake_case__				)
         UpperCAmelCase	    =			torch.tensor(
             [[1_18_59, 0, 16_11, 8]] ,		dtype=torch.long ,		device=snake_case__				)  # Legal the president is
         UpperCAmelCase	    =			[
             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

         UpperCAmelCase	    =			model.generate(snake_case__ ,		do_sample=snake_case__				)
         self.assertListEqual(output_ids[0].tolist() ,		snake_case__				)








"""simple docstring"""







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,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
 import PIL


lowerCAmelCase_	:    Union[str, Any]            =      logging.get_logger(__name__)





class   UpperCamelCase_ (       a_  ):
  _A       :					List[str]				=	['pixel_values']



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



         """simple docstring"""





         super().__init__(**snake_case__				)
         UpperCAmelCase	    =			size if size is not None else {"""shortest_edge""": 2_24}
         UpperCAmelCase	    =			get_size_dict(snake_case__ ,		default_to_square=snake_case__				)
         UpperCAmelCase	    =			crop_size if crop_size is not None else {"""height""": 2_24, """width""": 2_24}
         UpperCAmelCase	    =			get_size_dict(snake_case__ ,		param_name="""crop_size"""				)

         UpperCAmelCase	    =			do_resize
         UpperCAmelCase	    =			size
         UpperCAmelCase	    =			crop_pct
         UpperCAmelCase	    =			resample
         UpperCAmelCase	    =			do_center_crop
         UpperCAmelCase	    =			crop_size
         UpperCAmelCase	    =			do_rescale
         UpperCAmelCase	    =			rescale_factor
         UpperCAmelCase	    =			do_normalize
         UpperCAmelCase	    =			image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
         UpperCAmelCase	    =			image_std if image_std is not None else IMAGENET_DEFAULT_STD



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



         """simple docstring"""





         UpperCAmelCase	    =			get_size_dict(snake_case__ ,		default_to_square=snake_case__				)
         if "shortest_edge" not in size and ("height" not in size or "width" not in size):
                raise ValueError(f'''size must contain \'height\' and \'width\' or \'shortest_edge\' as keys. Got {size.keys()}'''				)
         if crop_pct is not None:
                if "shortest_edge" in size:
                       UpperCAmelCase	    =			int(size["""shortest_edge"""] / crop_pct				)
                elif "height" in size and "width" in size:
                       if size["height"] == size["width"]:
                              UpperCAmelCase	    =			int(size["""height"""] / crop_pct				)
                       else:
                              UpperCAmelCase	    =			(int(size["""height"""] / crop_pct				), int(size["""width"""] / crop_pct				))
                else:
                       raise ValueError("""Invalid size for resize: {}""".format(snake_case__				)				)

                UpperCAmelCase	    =			get_resize_output_image_size(snake_case__ ,		size=snake_case__ ,		default_to_square=snake_case__				)
         else:
                if "shortest_edge" in size:
                       UpperCAmelCase	    =			get_resize_output_image_size(snake_case__ ,		size=size["""shortest_edge"""] ,		default_to_square=snake_case__				)
                elif "height" in size and "width" in size:
                       UpperCAmelCase	    =			(size["""height"""], size["""width"""])
                else:
                       raise ValueError("""Invalid size for resize: {}""".format(snake_case__				)				)

         return resize(snake_case__ ,		size=snake_case__ ,		resample=snake_case__ ,		data_format=snake_case__ ,		**snake_case__				)



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ = None ,		**snake_case__ ,		)			->			np.ndarray:



         """simple docstring"""





         UpperCAmelCase	    =			get_size_dict(snake_case__				)
         if "height" not in size or "width" not in size:
                raise ValueError(f'''size must contain \'height\' and \'width\' as keys. Got {size.keys()}'''				)
         return center_crop(snake_case__ ,		size=(size["""height"""], size["""width"""]) ,		data_format=snake_case__ ,		**snake_case__				)



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ = None ,		**snake_case__ ,		)			->			Tuple:



         """simple docstring"""





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



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ = None ,		**snake_case__ ,		)			->			np.ndarray:



         """simple docstring"""





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



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



         """simple docstring"""





         UpperCAmelCase	    =			do_resize if do_resize is not None else self.do_resize
         UpperCAmelCase	    =			crop_pct if crop_pct is not None else self.crop_pct
         UpperCAmelCase	    =			resample if resample is not None else self.resample
         UpperCAmelCase	    =			do_center_crop if do_center_crop is not None else self.do_center_crop
         UpperCAmelCase	    =			do_rescale if do_rescale is not None else self.do_rescale
         UpperCAmelCase	    =			rescale_factor if rescale_factor is not None else self.rescale_factor
         UpperCAmelCase	    =			do_normalize if do_normalize is not None else self.do_normalize
         UpperCAmelCase	    =			image_mean if image_mean is not None else self.image_mean
         UpperCAmelCase	    =			image_std if image_std is not None else self.image_std

         UpperCAmelCase	    =			size if size is not None else self.size
         UpperCAmelCase	    =			get_size_dict(snake_case__ ,		default_to_square=snake_case__				)
         UpperCAmelCase	    =			crop_size if crop_size is not None else self.crop_size
         UpperCAmelCase	    =			get_size_dict(snake_case__ ,		param_name="""crop_size"""				)

         UpperCAmelCase	    =			make_list_of_images(snake_case__				)

         if not valid_images(snake_case__				):
                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 or resample is None:
                raise ValueError("""Size and resample must be specified if do_resize is True."""				)

         if do_center_crop and crop_pct is None:
                raise ValueError("""Crop_pct 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."""				)

         # All transformations expect numpy arrays.
         UpperCAmelCase	    =			[to_numpy_array(snake_case__				) for image in images]

         if do_resize:
                UpperCAmelCase	    =			[self.resize(image=snake_case__ ,		size=snake_case__ ,		crop_pct=snake_case__ ,		resample=snake_case__				) for image in images]

         if do_center_crop:
                UpperCAmelCase	    =			[self.center_crop(image=snake_case__ ,		size=snake_case__				) for image in images]

         if do_rescale:
                UpperCAmelCase	    =			[self.rescale(image=snake_case__ ,		scale=snake_case__				) for image in images]

         if do_normalize:
                UpperCAmelCase	    =			[self.normalize(image=snake_case__ ,		mean=snake_case__ ,		std=snake_case__				) for image in images]

         UpperCAmelCase	    =			[to_channel_dimension_format(snake_case__ ,		snake_case__				) for image in images]

         UpperCAmelCase	    =			{"""pixel_values""": images}
         return BatchFeature(data=snake_case__ ,		tensor_type=snake_case__				)
