



'''simple docstring'''






from __future__ import annotations

import unittest

from transformers import CTRLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers.models.ctrl.modeling_tf_ctrl import (
       TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
       TFCTRLForSequenceClassification,
       TFCTRLLMHeadModel,
       TFCTRLModel,
   )



class 				lowercase			(    _lowerCamelCase							):


 """simple docstring"""


 def __init__(				self    ,a_    ,)						->  Tuple:
        _UpperCAmelCase    :		str     	=					parent
        _UpperCAmelCase    :		int     	=					13
        _UpperCAmelCase    :		Union[str, Any]     	=					7
        _UpperCAmelCase    :		Any     	=					True
        _UpperCAmelCase    :		Optional[int]     	=					True
        _UpperCAmelCase    :		Union[str, Any]     	=					True
        _UpperCAmelCase    :		List[Any]     	=					True
        _UpperCAmelCase    :		Any     	=					True
        _UpperCAmelCase    :		str     	=					99
        _UpperCAmelCase    :		List[str]     	=					32
        _UpperCAmelCase    :		str     	=					2
        _UpperCAmelCase    :		Optional[Any]     	=					4
        _UpperCAmelCase    :		List[str]     	=					37
        _UpperCAmelCase    :		Union[str, Any]     	=					"""gelu"""
        _UpperCAmelCase    :		Any     	=					0.1
        _UpperCAmelCase    :		int     	=					0.1
        _UpperCAmelCase    :		Optional[Any]     	=					512
        _UpperCAmelCase    :		List[Any]     	=					16
        _UpperCAmelCase    :		List[str]     	=					2
        _UpperCAmelCase    :		List[str]     	=					0.02
        _UpperCAmelCase    :		List[Any]     	=					3
        _UpperCAmelCase    :		Any     	=					4
        _UpperCAmelCase    :		List[str]     	=					None
        _UpperCAmelCase    :		Optional[int]     	=					self.vocab_size - 1







 def _snake_case					(				self				)						->  List[Any]:
        _UpperCAmelCase    :		str     	=					ids_tensor([self.batch_size, self.seq_length]    ,self.vocab_size				)

        _UpperCAmelCase    :		int     	=					None
        if self.use_input_mask:
               _UpperCAmelCase    :		Dict     	=					random_attention_mask([self.batch_size, self.seq_length]				)

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

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

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

        _UpperCAmelCase    :		Optional[int]     	=					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    ,)

        _UpperCAmelCase    :		str     	=					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 _snake_case					(				self    ,a_    ,a_    ,a_    ,a_    ,a_    ,*a_				)						->  List[str]:
        _UpperCAmelCase    :		int     	=					TFCTRLModel(config=a_				)
        _UpperCAmelCase    :		Tuple     	=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
        _UpperCAmelCase    :		Tuple     	=					model(a_				)

        _UpperCAmelCase    :		str     	=					[input_ids, None, input_mask]  # None is the input for 'past'
        _UpperCAmelCase    :		List[Any]     	=					model(a_				)

        _UpperCAmelCase    :		Union[str, Any]     	=					model(a_				)

        self.parent.assertEqual(result.last_hidden_state.shape    ,(self.batch_size, self.seq_length, self.hidden_size)				)







 def _snake_case					(				self    ,a_    ,a_    ,a_    ,a_    ,a_    ,*a_				)						->  List[str]:
        _UpperCAmelCase    :		str     	=					TFCTRLLMHeadModel(config=a_				)
        _UpperCAmelCase    :		Optional[Any]     	=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
        _UpperCAmelCase    :		Union[str, Any]     	=					model(a_				)
        self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.seq_length, self.vocab_size)				)







 def _snake_case					(				self    ,a_    ,a_    ,a_    ,a_    ,a_    ,*a_				)						->  Union[str, Any]:
        _UpperCAmelCase    :		Any     	=					self.num_labels
        _UpperCAmelCase    :		int     	=					ids_tensor([self.batch_size]    ,self.type_sequence_label_size				)
        _UpperCAmelCase    :		int     	=					{
            """input_ids""": input_ids,
            """token_type_ids""": token_type_ids,
            """labels""": sequence_labels,
        }
        _UpperCAmelCase    :		str     	=					TFCTRLForSequenceClassification(a_				)
        _UpperCAmelCase    :		Tuple     	=					model(a_				)
        self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.num_labels)				)







 def _snake_case					(				self				)						->  List[str]:
        _UpperCAmelCase    :		Dict     	=					self.prepare_config_and_inputs()

        (
            (
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,
        )    :		List[Any]     	=					config_and_inputs

        _UpperCAmelCase    :		Optional[int]     	=					{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
        return config, inputs_dict







@require_tf
class 				lowercase			(    _lowerCamelCase   ,   _lowerCamelCase   ,   unittest.TestCase							):


 """simple docstring"""


 UpperCAmelCase		      =    (TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
 UpperCAmelCase		      =    (TFCTRLLMHeadModel,) if is_tf_available() else ()
 UpperCAmelCase		      =    (
     {
         """feature-extraction""": TFCTRLModel,
         """text-classification""": TFCTRLForSequenceClassification,
         """text-generation""": TFCTRLLMHeadModel,
         """zero-shot""": TFCTRLForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 UpperCAmelCase		      =    False
 UpperCAmelCase		      =    False







 def _snake_case					(				self    ,a_    ,a_    ,a_    ,a_    ,a_				)						->  List[str]:
        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 _snake_case					(				self				)						->  Optional[int]:
        _UpperCAmelCase    :		Union[str, Any]     	=					TFCTRLModelTester(self				)
        _UpperCAmelCase    :		Any     	=					ConfigTester(self    ,config_class=a_    ,n_embd=37				)







 def _snake_case					(				self				)						->  List[str]:
        self.config_tester.run_common_tests()







 def _snake_case					(				self				)						->  List[str]:
        _UpperCAmelCase    :		List[str]     	=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_ctrl_model(*a_				)







 def _snake_case					(				self				)						->  Dict:
        _UpperCAmelCase    :		List[str]     	=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_ctrl_lm_head(*a_				)







 def _snake_case					(				self				)						->  str:
        _UpperCAmelCase    :		List[Any]     	=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_ctrl_for_sequence_classification(*a_				)







 def _snake_case					(				self				)						->  List[Any]:
        _UpperCAmelCase	,_UpperCAmelCase    :		Tuple     	=					self.model_tester.prepare_config_and_inputs_for_common()
        _UpperCAmelCase    :		Union[str, Any]     	=					[TFCTRLLMHeadModel]
        _UpperCAmelCase    :		Optional[Any]     	=					[TFCTRLForSequenceClassification]

        for model_class in self.all_model_classes:
               _UpperCAmelCase    :		List[Any]     	=					model_class(a_				)
               model.build()  # may be needed for the get_bias() call below
               assert isinstance(model.get_input_embeddings()    ,tf.keras.layers.Layer				)

               if model_class in list_lm_models:
                      _UpperCAmelCase    :		List[Any]     	=					model.get_output_embeddings()
                      assert isinstance(a_    ,tf.keras.layers.Layer				)
                      _UpperCAmelCase    :		Tuple     	=					model.get_bias()
                      assert isinstance(a_    ,a_				)
                      for k, v in name.items():
                             assert isinstance(a_    ,tf.Variable				)
               elif model_class in list_other_models_with_output_ebd:
                      _UpperCAmelCase    :		Tuple     	=					model.get_output_embeddings()
                      assert isinstance(a_    ,tf.keras.layers.Layer				)
                      _UpperCAmelCase    :		Union[str, Any]     	=					model.get_bias()
                      assert name is None
               else:
                      _UpperCAmelCase    :		Any     	=					model.get_output_embeddings()
                      assert x is None
                      _UpperCAmelCase    :		Any     	=					model.get_bias()
                      assert name is None







 @slow
 def _snake_case					(				self				)						->  List[str]:
        for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _UpperCAmelCase    :		List[str]     	=					TFCTRLModel.from_pretrained(a_				)
               self.assertIsNotNone(a_				)







@require_tf
class 				lowercase			(    unittest.TestCase							):


 """simple docstring"""


 @slow
 def _snake_case					(				self				)						->  Any:
        _UpperCAmelCase    :		Dict     	=					TFCTRLLMHeadModel.from_pretrained("""ctrl"""				)
        _UpperCAmelCase    :		str     	=					tf.convert_to_tensor([[11_859, 0, 1_611, 8]]    ,dtype=tf.intaa				)  # Legal the president is
        _UpperCAmelCase    :		Tuple     	=					[
            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

        _UpperCAmelCase    :		List[Any]     	=					model.generate(a_    ,do_sample=a_				)
        self.assertListEqual(output_ids[0].numpy().tolist()    ,a_				)








'''simple docstring'''






import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
   import torch

if is_vision_available():
   from PIL import Image

   from transformers import BlipImageProcessor



class 				lowercase			(    unittest.TestCase							):


 """simple docstring"""


 def __init__(				self    ,a_    ,a_=7    ,a_=3    ,a_=18    ,a_=30    ,a_=400    ,a_=True    ,a_=None    ,a_=True    ,a_=False    ,a_=[0.4814_5466, 0.457_8275, 0.4082_1073]    ,a_=[0.2686_2954, 0.2613_0258, 0.2757_7711]    ,a_=True    ,)						->  Optional[int]:
        _UpperCAmelCase    :		Dict     	=					size if size is not None else {"""height""": 20, """width""": 20}
        _UpperCAmelCase    :		Dict     	=					parent
        _UpperCAmelCase    :		Tuple     	=					batch_size
        _UpperCAmelCase    :		Optional[int]     	=					num_channels
        _UpperCAmelCase    :		int     	=					image_size
        _UpperCAmelCase    :		List[Any]     	=					min_resolution
        _UpperCAmelCase    :		str     	=					max_resolution
        _UpperCAmelCase    :		Tuple     	=					do_resize
        _UpperCAmelCase    :		Union[str, Any]     	=					size
        _UpperCAmelCase    :		int     	=					do_normalize
        _UpperCAmelCase    :		List[str]     	=					image_mean
        _UpperCAmelCase    :		Any     	=					image_std
        _UpperCAmelCase    :		Union[str, Any]     	=					do_pad
        _UpperCAmelCase    :		Tuple     	=					do_convert_rgb







 def _snake_case					(				self				)						->  List[str]:
        return {
            "do_resize": self.do_resize,
            "size": self.size,
            "do_normalize": self.do_normalize,
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_convert_rgb": self.do_convert_rgb,
            "do_pad": self.do_pad,
        }







 def _snake_case					(				self    ,a_=False    ,a_=False    ,a_=False				)						->  Dict:
        assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

        if equal_resolution:
               _UpperCAmelCase    :		Union[str, Any]     	=					[]
               for i in range(self.batch_size				):
                      image_inputs.append(
                          np.random.randint(
                              255    ,size=(self.num_channels, self.max_resolution, self.max_resolution)    ,dtype=np.uinta				)				)
        else:
               _UpperCAmelCase    :		Optional[int]     	=					[]
               for i in range(self.batch_size				):
                      _UpperCAmelCase	,_UpperCAmelCase    :		int     	=					np.random.choice(np.arange(self.min_resolution    ,self.max_resolution				)    ,2				)
                      image_inputs.append(np.random.randint(255    ,size=(self.num_channels, width, height)    ,dtype=np.uinta				)				)

        if not numpify and not torchify:
               # PIL expects the channel dimension as last dimension
               _UpperCAmelCase    :		Any     	=					[Image.fromarray(np.moveaxis(a_    ,0    ,-1				)				) for x in image_inputs]

        if torchify:
               _UpperCAmelCase    :		Optional[int]     	=					[torch.from_numpy(a_				) for x in image_inputs]

        return image_inputs







@require_torch
@require_vision
class 				lowercase			(    _lowerCamelCase   ,   unittest.TestCase							):


 """simple docstring"""


 UpperCAmelCase		      =    BlipImageProcessor if is_vision_available() else None







 def _snake_case					(				self				)						->  List[Any]:
        _UpperCAmelCase    :		Dict     	=					BlipImageProcessingTester(self				)







 @property
 def _snake_case					(				self				)						->  str:
        return self.image_processor_tester.prepare_image_processor_dict()







 def _snake_case					(				self				)						->  Tuple:
        _UpperCAmelCase    :		List[str]     	=					self.image_processing_class(**self.image_processor_dict				)
        self.assertTrue(hasattr(a_    ,"""do_resize"""				)				)
        self.assertTrue(hasattr(a_    ,"""size"""				)				)
        self.assertTrue(hasattr(a_    ,"""do_normalize"""				)				)
        self.assertTrue(hasattr(a_    ,"""image_mean"""				)				)
        self.assertTrue(hasattr(a_    ,"""image_std"""				)				)
        self.assertTrue(hasattr(a_    ,"""do_convert_rgb"""				)				)







 def _snake_case					(				self				)						->  Any:
        pass







 def _snake_case					(				self				)						->  Union[str, Any]:
        # Initialize image_processor
        _UpperCAmelCase    :		List[str]     	=					self.image_processing_class(**self.image_processor_dict				)
        # create random PIL images
        _UpperCAmelCase    :		str     	=					self.image_processor_tester.prepare_inputs(equal_resolution=a_				)
        for image in image_inputs:
               self.assertIsInstance(a_    ,Image.Image				)

        # Test not batched input
        _UpperCAmelCase    :		List[str]     	=					image_processor(image_inputs[0]    ,return_tensors="""pt"""				).pixel_values
        self.assertEqual(
            encoded_images.shape    ,(
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )    ,)

        # Test batched
        _UpperCAmelCase    :		List[str]     	=					image_processor(a_    ,return_tensors="""pt"""				).pixel_values
        self.assertEqual(
            encoded_images.shape    ,(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )    ,)







 def _snake_case					(				self				)						->  Optional[Any]:
        # Initialize image_processor
        _UpperCAmelCase    :		str     	=					self.image_processing_class(**self.image_processor_dict				)
        # create random numpy tensors
        _UpperCAmelCase    :		str     	=					self.image_processor_tester.prepare_inputs(equal_resolution=a_    ,numpify=a_				)
        for image in image_inputs:
               self.assertIsInstance(a_    ,np.ndarray				)

        # Test not batched input
        _UpperCAmelCase    :		Dict     	=					image_processor(image_inputs[0]    ,return_tensors="""pt"""				).pixel_values
        self.assertEqual(
            encoded_images.shape    ,(
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )    ,)

        # Test batched
        _UpperCAmelCase    :		int     	=					image_processor(a_    ,return_tensors="""pt"""				).pixel_values
        self.assertEqual(
            encoded_images.shape    ,(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )    ,)







 def _snake_case					(				self				)						->  List[str]:
        # Initialize image_processor
        _UpperCAmelCase    :		Dict     	=					self.image_processing_class(**self.image_processor_dict				)
        # create random PyTorch tensors
        _UpperCAmelCase    :		Union[str, Any]     	=					self.image_processor_tester.prepare_inputs(equal_resolution=a_    ,torchify=a_				)
        for image in image_inputs:
               self.assertIsInstance(a_    ,torch.Tensor				)

        # Test not batched input
        _UpperCAmelCase    :		str     	=					image_processor(image_inputs[0]    ,return_tensors="""pt"""				).pixel_values
        self.assertEqual(
            encoded_images.shape    ,(
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )    ,)

        # Test batched
        _UpperCAmelCase    :		Optional[Any]     	=					image_processor(a_    ,return_tensors="""pt"""				).pixel_values
        self.assertEqual(
            encoded_images.shape    ,(
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )    ,)







@require_torch
@require_vision
class 				lowercase			(    _lowerCamelCase   ,   unittest.TestCase							):


 """simple docstring"""


 UpperCAmelCase		      =    BlipImageProcessor if is_vision_available() else None







 def _snake_case					(				self				)						->  Optional[Any]:
        _UpperCAmelCase    :		Optional[Any]     	=					BlipImageProcessingTester(self    ,num_channels=4				)
        _UpperCAmelCase    :		List[Any]     	=					3







 @property
 def _snake_case					(				self				)						->  int:
        return self.image_processor_tester.prepare_image_processor_dict()







 def _snake_case					(				self				)						->  Optional[Any]:
        _UpperCAmelCase    :		Optional[Any]     	=					self.image_processing_class(**self.image_processor_dict				)
        self.assertTrue(hasattr(a_    ,"""do_resize"""				)				)
        self.assertTrue(hasattr(a_    ,"""size"""				)				)
        self.assertTrue(hasattr(a_    ,"""do_normalize"""				)				)
        self.assertTrue(hasattr(a_    ,"""image_mean"""				)				)
        self.assertTrue(hasattr(a_    ,"""image_std"""				)				)
        self.assertTrue(hasattr(a_    ,"""do_convert_rgb"""				)				)







 def _snake_case					(				self				)						->  Union[str, Any]:
        pass







 def _snake_case					(				self				)						->  List[Any]:
        # Initialize image_processor
        _UpperCAmelCase    :		Any     	=					self.image_processing_class(**self.image_processor_dict				)
        # create random PIL images
        _UpperCAmelCase    :		Optional[Any]     	=					self.image_processor_tester.prepare_inputs(equal_resolution=a_				)
        for image in image_inputs:
               self.assertIsInstance(a_    ,Image.Image				)

        # Test not batched input
        _UpperCAmelCase    :		List[Any]     	=					image_processor(image_inputs[0]    ,return_tensors="""pt"""				).pixel_values
        self.assertEqual(
            encoded_images.shape    ,(
                1,
                self.expected_encoded_image_num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )    ,)

        # Test batched
        _UpperCAmelCase    :		Dict     	=					image_processor(a_    ,return_tensors="""pt"""				).pixel_values
        self.assertEqual(
            encoded_images.shape    ,(
                self.image_processor_tester.batch_size,
                self.expected_encoded_image_num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )    ,)



