



'''simple docstring'''





from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

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


if is_tf_available():
 import tensorflow as tf

 from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
 from PIL import Image

 from transformers import ViTImageProcessor


class 					snake_case			:



       def __init__(					self  ,UpperCAmelCase_  ,UpperCAmelCase_=13  ,UpperCAmelCase_=30  ,UpperCAmelCase_=2  ,UpperCAmelCase_=3  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=32  ,UpperCAmelCase_=2  ,UpperCAmelCase_=4  ,UpperCAmelCase_=37  ,UpperCAmelCase_="gelu"  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=10  ,UpperCAmelCase_=0.02  ,UpperCAmelCase_=3  ,UpperCAmelCase_=None  ,)	->				Optional[Any]:
           lowercase__									=    parent
           lowercase__									=    batch_size
           lowercase__									=    image_size
           lowercase__									=    patch_size
           lowercase__									=    num_channels
           lowercase__									=    is_training
           lowercase__									=    use_labels
           lowercase__									=    hidden_size
           lowercase__									=    num_hidden_layers
           lowercase__									=    num_attention_heads
           lowercase__									=    intermediate_size
           lowercase__									=    hidden_act
           lowercase__									=    hidden_dropout_prob
           lowercase__									=    attention_probs_dropout_prob
           lowercase__									=    type_sequence_label_size
           lowercase__									=    initializer_range
           lowercase__									=    scope

           # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
           lowercase__									=    (image_size // patch_size) ** 2
           lowercase__									=    num_patches + 1



       def 				_a   (					self   )	->				Dict:
           lowercase__									=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

           lowercase__									=    None
           if self.use_labels:
               lowercase__									=    ids_tensor([self.batch_size]  ,self.type_sequence_label_size   )

           lowercase__									=    self.get_config()

           return config, pixel_values, labels



       def 				_a   (					self   )	->				int:
           return ViTConfig(
               image_size=self.image_size  ,patch_size=self.patch_size  ,num_channels=self.num_channels  ,hidden_size=self.hidden_size  ,num_hidden_layers=self.num_hidden_layers  ,num_attention_heads=self.num_attention_heads  ,intermediate_size=self.intermediate_size  ,hidden_act=self.hidden_act  ,hidden_dropout_prob=self.hidden_dropout_prob  ,attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,is_decoder=UpperCAmelCase_  ,initializer_range=self.initializer_range  ,)



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Any:
           lowercase__									=    TFViTModel(config=UpperCAmelCase_   )
           lowercase__									=    model(UpperCAmelCase_  ,training=UpperCAmelCase_   )
           self.parent.assertEqual(result.last_hidden_state.shape  ,(self.batch_size, self.seq_length, self.hidden_size)   )

           # Test with an image with different size than the one specified in config.
           lowercase__									=    self.image_size // 2
           lowercase__									=    pixel_values[:, :, :image_size, :image_size]
           lowercase__									=    model(UpperCAmelCase_  ,interpolate_pos_encoding=UpperCAmelCase_  ,training=UpperCAmelCase_   )
           lowercase__									=    (image_size // self.patch_size) ** 2 + 1
           self.parent.assertEqual(result.last_hidden_state.shape  ,(self.batch_size, seq_length, self.hidden_size)   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				str:
           lowercase__									=    self.type_sequence_label_size
           lowercase__									=    TFViTForImageClassification(UpperCAmelCase_   )
           lowercase__									=    model(UpperCAmelCase_  ,labels=UpperCAmelCase_  ,training=UpperCAmelCase_   )
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.type_sequence_label_size)   )

           # Test with an image with different size than the one specified in config.
           lowercase__									=    self.image_size // 2
           lowercase__									=    pixel_values[:, :, :image_size, :image_size]
           lowercase__									=    model(UpperCAmelCase_  ,interpolate_pos_encoding=UpperCAmelCase_  ,training=UpperCAmelCase_   )
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.type_sequence_label_size)   )

           # test greyscale images
           lowercase__									=    1
           lowercase__									=    TFViTForImageClassification(UpperCAmelCase_   )
           lowercase__									=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
           lowercase__									=    model(UpperCAmelCase_   )
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.type_sequence_label_size)   )







       def 				_a   (					self   )	->				Optional[int]:
           lowercase__									=    self.prepare_config_and_inputs()
           lowercase__							,							lowercase__							,							lowercase__									=    config_and_inputs
           lowercase__									=    {"pixel_values": pixel_values}
           return config, inputs_dict





@require_tf
class 					snake_case			(UpperCamelCase		,   UpperCamelCase		,   unittest.TestCase ):
       lowerCAmelCase__	:Optional[int]     =       (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
       lowerCAmelCase__	:Tuple     =       (
           {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
           if is_tf_available()
           else {}
       )

       lowerCAmelCase__	:Dict     =       False
       lowerCAmelCase__	:Union[str, Any]     =       False
       lowerCAmelCase__	:List[Any]     =       False



       def 				_a   (					self   )	->				str:
           lowercase__									=    TFViTModelTester(self   )
           lowercase__									=    ConfigTester(self  ,config_class=UpperCAmelCase_  ,has_text_modality=UpperCAmelCase_  ,hidden_size=37   )



       def 				_a   (					self   )	->				str:
           self.config_tester.run_common_tests()



       @unittest.skip(reason="ViT does not use inputs_embeds"   )
       def 				_a   (					self   )	->				Any:
           pass



       @unittest.skip(reason="ViT does not use inputs_embeds"   )
       def 				_a   (					self   )	->				str:
           pass



       def 				_a   (					self   )	->				Optional[int]:
           lowercase__							,							lowercase__									=    self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               lowercase__									=    model_class(UpperCAmelCase_   )
               self.assertIsInstance(model.get_input_embeddings()  ,(tf.keras.layers.Layer)   )
               lowercase__									=    model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(UpperCAmelCase_  ,tf.keras.layers.Layer   )   )



       def 				_a   (					self   )	->				str:
           lowercase__							,							lowercase__									=    self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               lowercase__									=    model_class(UpperCAmelCase_   )
               lowercase__									=    inspect.signature(model.call   )
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               lowercase__									=    [*signature.parameters.keys()]

               lowercase__									=    ["pixel_values"]
               self.assertListEqual(arg_names[:1]  ,UpperCAmelCase_   )



       def 				_a   (					self   )	->				Tuple:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				Any:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_   )







       @slow
       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    TFViTModel.from_pretrained("google/vit-base-patch16-224"   )
           self.assertIsNotNone(UpperCAmelCase_   )





def 							lowerCamelCase	(					):





    '''simple docstring'''


    lowercase__									=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
    return image



@require_tf
@require_vision
class 					snake_case			(unittest.TestCase ):



       @cached_property
       def 				_a   (					self   )	->				int:
           return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"   ) if is_vision_available() else None







       @slow
       def 				_a   (					self   )	->				str:
           lowercase__									=    TFViTForImageClassification.from_pretrained("google/vit-base-patch16-224"   )

           lowercase__									=    self.default_image_processor
           lowercase__									=    prepare_img()
           lowercase__									=    image_processor(images=UpperCAmelCase_  ,return_tensors="tf"   )

           # forward pass
           lowercase__									=    model(**UpperCAmelCase_   )

           # verify the logits
           lowercase__									=    tf.TensorShape((1, 1_000)   )
           self.assertEqual(outputs.logits.shape  ,UpperCAmelCase_   )

           lowercase__									=    tf.constant([-0.27_44, 0.82_15, -0.08_36]   )

           tf.debugging.assert_near(outputs.logits[0, :3]  ,UpperCAmelCase_  ,atol=1E-4   )









'''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 					snake_case			:



       def __init__(					self  ,UpperCAmelCase_  ,UpperCAmelCase_=14  ,UpperCAmelCase_=7  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=99  ,UpperCAmelCase_=32  ,UpperCAmelCase_=5  ,UpperCAmelCase_=4  ,UpperCAmelCase_=37  ,UpperCAmelCase_="gelu"  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=512  ,UpperCAmelCase_=16  ,UpperCAmelCase_=2  ,UpperCAmelCase_=0.02  ,UpperCAmelCase_=3  ,UpperCAmelCase_=4  ,UpperCAmelCase_=None  ,)	->				Dict:
           lowercase__									=    parent
           lowercase__									=    batch_size
           lowercase__									=    seq_length
           lowercase__									=    is_training
           lowercase__									=    use_token_type_ids
           lowercase__									=    use_input_mask
           lowercase__									=    use_labels
           lowercase__									=    use_mc_token_ids
           lowercase__									=    vocab_size
           lowercase__									=    hidden_size
           lowercase__									=    num_hidden_layers
           lowercase__									=    num_attention_heads
           lowercase__									=    intermediate_size
           lowercase__									=    hidden_act
           lowercase__									=    hidden_dropout_prob
           lowercase__									=    attention_probs_dropout_prob
           lowercase__									=    max_position_embeddings
           lowercase__									=    type_vocab_size
           lowercase__									=    type_sequence_label_size
           lowercase__									=    initializer_range
           lowercase__									=    num_labels
           lowercase__									=    num_choices
           lowercase__									=    scope
           lowercase__									=    self.vocab_size - 1



       def 				_a   (					self   )	->				Any:
           lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size   )

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

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

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

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

           lowercase__									=    self.get_config()

           lowercase__									=    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 				_a   (					self   )	->				Optional[int]:
           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 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,*UpperCAmelCase_   )	->				List[Any]:
           lowercase__									=    CTRLModel(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()

           model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,head_mask=UpperCAmelCase_   )
           model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_   )
           lowercase__									=    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 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,*UpperCAmelCase_   )	->				Optional[int]:
           lowercase__									=    CTRLLMHeadModel(UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()

           lowercase__									=    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 				_a   (					self   )	->				Union[str, Any]:
           lowercase__									=    self.prepare_config_and_inputs()

           (
               (
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							
           )									=    config_and_inputs

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

           return config, inputs_dict







       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,*UpperCAmelCase_   )	->				Optional[int]:
           lowercase__									=    self.num_labels
           lowercase__									=    CTRLForSequenceClassification(UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    ids_tensor([self.batch_size]  ,self.type_sequence_label_size   )
           lowercase__									=    model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,labels=UpperCAmelCase_   )
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.num_labels)   )





@require_torch
class 					snake_case			(UpperCamelCase		,   UpperCamelCase		,   UpperCamelCase		,   unittest.TestCase ):
       lowerCAmelCase__	:Union[str, Any]     =       (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
       lowerCAmelCase__	:Dict     =       (CTRLLMHeadModel,) if is_torch_available() else ()
       lowerCAmelCase__	:int     =       (
           {
               "feature-extraction": CTRLModel,
               "text-classification": CTRLForSequenceClassification,
               "text-generation": CTRLLMHeadModel,
               "zero-shot": CTRLForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       lowerCAmelCase__	:str     =       True
       lowerCAmelCase__	:Tuple     =       False
       lowerCAmelCase__	:List[Any]     =       False



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Tuple:
           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 				_a   (					self   )	->				str:
           lowercase__									=    CTRLModelTester(self   )
           lowercase__									=    ConfigTester(self  ,config_class=UpperCAmelCase_  ,n_embd=37   )



       def 				_a   (					self   )	->				List[Any]:
           super().tearDown()
           # clean-up as much as possible GPU memory occupied by PyTorch
           gc.collect()
           torch.cuda.empty_cache()



       def 				_a   (					self   )	->				Tuple:
           self.config_tester.run_common_tests()



       def 				_a   (					self   )	->				Any:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_ctrl_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				Optional[Any]:
           lowercase__									=    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 				_a   (					self   )	->				Tuple:
           pass



       @slow
       def 				_a   (					self   )	->				List[str]:
           for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__									=    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 				_a   (					self   )	->				Tuple:
           pass





@require_torch
class 					snake_case			(unittest.TestCase ):



       def 				_a   (					self   )	->				int:
           super().tearDown()
           # clean-up as much as possible GPU memory occupied by PyTorch
           gc.collect()
           torch.cuda.empty_cache()







       @slow
       def 				_a   (					self   )	->				int:
           lowercase__									=    CTRLLMHeadModel.from_pretrained("ctrl"   )
           model.to(UpperCAmelCase_   )
           lowercase__									=    torch.tensor(
               [[11_859, 0, 1_611, 8]]  ,dtype=torch.long  ,device=UpperCAmelCase_   )  # Legal the president is
           lowercase__									=    [
               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

           lowercase__									=    model.generate(UpperCAmelCase_  ,do_sample=UpperCAmelCase_   )
           self.assertListEqual(output_ids[0].tolist()  ,UpperCAmelCase_   )




