






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


				def __init__(					self ,						_lowerCAmelCase ,						_lowerCAmelCase=1_3 ,						_lowerCAmelCase=3_0 ,						_lowerCAmelCase=2 ,						_lowerCAmelCase=3 ,						_lowerCAmelCase=True ,						_lowerCAmelCase=True ,						_lowerCAmelCase=3_2 ,						_lowerCAmelCase=2 ,						_lowerCAmelCase=4 ,						_lowerCAmelCase=3_7 ,						_lowerCAmelCase="gelu" ,						_lowerCAmelCase=0.1 ,						_lowerCAmelCase=0.1 ,						_lowerCAmelCase=1_0 ,						_lowerCAmelCase=0.02 ,						_lowerCAmelCase=3 ,						_lowerCAmelCase=None ,						):
											_lowercase							:   Union[str, Any] 							=   parent
											_lowercase							:   Any 							=   batch_size
											_lowercase							:   Optional[int] 							=   image_size
											_lowercase							:   Tuple 							=   patch_size
											_lowercase							:   Tuple 							=   num_channels
											_lowercase							:   List[str] 							=   is_training
											_lowercase							:   Dict 							=   use_labels
											_lowercase							:   Tuple 							=   hidden_size
											_lowercase							:   int 							=   num_hidden_layers
											_lowercase							:   str 							=   num_attention_heads
											_lowercase							:   Optional[Any] 							=   intermediate_size
											_lowercase							:   Optional[int] 							=   hidden_act
											_lowercase							:   List[Any] 							=   hidden_dropout_prob
											_lowercase							:   List[Any] 							=   attention_probs_dropout_prob
											_lowercase							:   Optional[int] 							=   type_sequence_label_size
											_lowercase							:   int 							=   initializer_range
											_lowercase							:   Union[str, Any] 							=   scope

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


				def 						__a					(					self      ):
											_lowercase							:   List[Any] 							=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

											_lowercase							:   Optional[Any] 							=   None
											if self.use_labels:
																		_lowercase							:   Tuple 							=   ids_tensor([self.batch_size] ,						self.type_sequence_label_size      )

											_lowercase							:   Dict 							=   self.get_config()

											return config, pixel_values, labels


				def 						__a					(					self      ):
											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=_lowerCAmelCase ,						initializer_range=self.initializer_range ,						)


				def 						__a					(					self ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase      ):
											_lowercase							:   Union[str, Any] 							=   TFViTModel(config=_lowerCAmelCase      )
											_lowercase							:   Union[str, Any] 							=   model(_lowerCAmelCase ,						training=_lowerCAmelCase      )
											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							:   Tuple 							=   self.image_size // 2
											_lowercase							:   Optional[Any] 							=   pixel_values[:, :, :image_size, :image_size]
											_lowercase							:   List[Any] 							=   model(_lowerCAmelCase ,						interpolate_pos_encoding=_lowerCAmelCase ,						training=_lowerCAmelCase      )
											_lowercase							:   Tuple 							=   (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 ,						_lowerCAmelCase ,						_lowerCAmelCase ,						_lowerCAmelCase      ):
											_lowercase							:   Dict 							=   self.type_sequence_label_size
											_lowercase							:   Optional[int] 							=   TFViTForImageClassification(_lowerCAmelCase      )
											_lowercase							:   Dict 							=   model(_lowerCAmelCase ,						labels=_lowerCAmelCase ,						training=_lowerCAmelCase      )
											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							:   int 							=   self.image_size // 2
											_lowercase							:   int 							=   pixel_values[:, :, :image_size, :image_size]
											_lowercase							:   List[Any] 							=   model(_lowerCAmelCase ,						interpolate_pos_encoding=_lowerCAmelCase ,						training=_lowerCAmelCase      )
											self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.type_sequence_label_size)      )

											# test greyscale images
											_lowercase							:   int 							=   1
											_lowercase							:   Dict 							=   TFViTForImageClassification(_lowerCAmelCase      )
											_lowercase							:   Dict 							=   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
											_lowercase							:   List[str] 							=   model(_lowerCAmelCase      )
											self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.type_sequence_label_size)      )


				def 						__a					(					self      ):
											_lowercase							:   List[Any] 							=   self.prepare_config_and_inputs()
											_lowercase							,							_lowercase							,							_lowercase							:   Union[str, Any] 							=   config_and_inputs
											_lowercase							:   int 							=   {'pixel_values': pixel_values}
											return config, inputs_dict




@require_tf
class 				lowerCAmelCase_	(		__snake_case   ,		__snake_case   ,		unittest.TestCase			):
				_UpperCamelCase					:       Optional[int]							       = (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
				_UpperCamelCase					:       Optional[Any]							       = (
				    {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
				    if is_tf_available()
				    else {}
				)

				_UpperCamelCase					:       int							       = False
				_UpperCamelCase					:       Optional[int]							       = False
				_UpperCamelCase					:       Optional[int]							       = False


				def 						__a					(					self      ):
											_lowercase							:   Tuple 							=   TFViTModelTester(self      )
											_lowercase							:   Any 							=   ConfigTester(self ,						config_class=_lowerCAmelCase ,						has_text_modality=_lowerCAmelCase ,						hidden_size=3_7      )


				def 						__a					(					self      ):
											self.config_tester.run_common_tests()


				@unittest.skip(reason='ViT does not use inputs_embeds'      )
				def 						__a					(					self      ):
											pass


				@unittest.skip(reason='ViT does not use inputs_embeds'      )
				def 						__a					(					self      ):
											pass


				def 						__a					(					self      ):
											_lowercase							,							_lowercase							:   Dict 							=   self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		_lowercase							:   Optional[Any] 							=   model_class(_lowerCAmelCase      )
																		self.assertIsInstance(model.get_input_embeddings() ,						(tf.keras.layers.Layer)      )
																		_lowercase							:   Union[str, Any] 							=   model.get_output_embeddings()
																		self.assertTrue(x is None or isinstance(_lowerCAmelCase ,						tf.keras.layers.Layer      )      )


				def 						__a					(					self      ):
											_lowercase							,							_lowercase							:   Any 							=   self.model_tester.prepare_config_and_inputs_for_common()

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

																		_lowercase							:   Optional[int] 							=   ['pixel_values']
																		self.assertListEqual(arg_names[:1] ,						_lowerCAmelCase      )


				def 						__a					(					self      ):
											_lowercase							:   Optional[int] 							=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_lowerCAmelCase      )


				def 						__a					(					self      ):
											_lowercase							:   str 							=   self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase      )


				@slow
				def 						__a					(					self      ):
											_lowercase							:   Optional[int] 							=   TFViTModel.from_pretrained('google/vit-base-patch16-224'      )
											self.assertIsNotNone(_lowerCAmelCase      )






def    __magic_name__    (	)     ->    List[Any]:
							_lowercase							:   Optional[Any] 							=   Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
							return image






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


				@cached_property
				def 						__a					(					self      ):
											return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224'      ) if is_vision_available() else None


				@slow
				def 						__a					(					self      ):
											_lowercase							:   int 							=   TFViTForImageClassification.from_pretrained('google/vit-base-patch16-224'      )

											_lowercase							:   Any 							=   self.default_image_processor
											_lowercase							:   Dict 							=   prepare_img()
											_lowercase							:   Union[str, Any] 							=   image_processor(images=_lowerCAmelCase ,						return_tensors='tf'      )

											# forward pass
											_lowercase							:   List[Any] 							=   model(**_lowerCAmelCase      )

											# verify the logits
											_lowercase							:   List[Any] 							=   tf.TensorShape((1, 1_0_0_0)      )
											self.assertEqual(outputs.logits.shape ,						_lowerCAmelCase      )

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

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







import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
    from PIL import Image

    from transformers import CLIPImageProcessor, CLIPProcessor

@require_vision
class     __snake_case    (				unittest.TestCase  ):
       """simple docstring"""







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






              """simple docstring"""


              __snake_case       	=							tempfile.mkdtemp()

              # fmt: off
              __snake_case       	=							["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""]
              # fmt: on
              __snake_case       	=							dict(zip(_UpperCamelCase      , range(len(_UpperCamelCase   )   )   )   )
              __snake_case       	=							["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
              __snake_case       	=							{"""unk_token""": """<unk>"""}

              __snake_case       	=							os.path.join(self.tmpdirname      , VOCAB_FILES_NAMES["""vocab_file"""]   )
              __snake_case       	=							os.path.join(self.tmpdirname      , VOCAB_FILES_NAMES["""merges_file"""]   )
              with open(self.vocab_file      , """w"""      , encoding="""utf-8"""   ) as fp:
                     fp.write(json.dumps(_UpperCamelCase   ) + """\n"""   )
              with open(self.merges_file      , """w"""      , encoding="""utf-8"""   ) as fp:
                     fp.write("""\n""".join(_UpperCamelCase   )   )

              __snake_case       	=							{
                  """do_resize""": True,
                  """size""": 20,
                  """do_center_crop""": True,
                  """crop_size""": 18,
                  """do_normalize""": True,
                  """image_mean""": [0.4814_5466, 0.457_8275, 0.4082_1073],
                  """image_std""": [0.2686_2954, 0.2613_0258, 0.2757_7711],
              }
              __snake_case       	=							os.path.join(self.tmpdirname      , _UpperCamelCase   )
              with open(self.image_processor_file      , """w"""      , encoding="""utf-8"""   ) as fp:
                     json.dump(_UpperCamelCase      , _UpperCamelCase   )


       def 			a		(		self      , **_UpperCamelCase   )      ->							Optional[int]:






              """simple docstring"""


              return CLIPTokenizer.from_pretrained(self.tmpdirname      , **_UpperCamelCase   )


       def 			a		(		self      , **_UpperCamelCase   )      ->							Union[str, Any]:






              """simple docstring"""


              return CLIPTokenizerFast.from_pretrained(self.tmpdirname      , **_UpperCamelCase   )


       def 			a		(		self      , **_UpperCamelCase   )      ->							List[str]:






              """simple docstring"""


              return CLIPImageProcessor.from_pretrained(self.tmpdirname      , **_UpperCamelCase   )


       def 			a		(		self   )      ->							int:






              """simple docstring"""


              shutil.rmtree(self.tmpdirname   )


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






              """simple docstring"""


              __snake_case       	=							[np.random.randint(2_55      , size=(3, 30, 4_00)      , dtype=np.uinta   )]

              __snake_case       	=							[Image.fromarray(np.moveaxis(_UpperCamelCase      , 0      , -1   )   ) for x in image_inputs]

              return image_inputs


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






              """simple docstring"""


              __snake_case       	=							self.get_tokenizer()
              __snake_case       	=							self.get_rust_tokenizer()
              __snake_case       	=							self.get_image_processor()

              __snake_case       	=							CLIPProcessor(tokenizer=_UpperCamelCase      , image_processor=_UpperCamelCase   )
              processor_slow.save_pretrained(self.tmpdirname   )
              __snake_case       	=							CLIPProcessor.from_pretrained(self.tmpdirname      , use_fast=_UpperCamelCase   )

              __snake_case       	=							CLIPProcessor(tokenizer=_UpperCamelCase      , image_processor=_UpperCamelCase   )
              processor_fast.save_pretrained(self.tmpdirname   )
              __snake_case       	=							CLIPProcessor.from_pretrained(self.tmpdirname   )

              self.assertEqual(processor_slow.tokenizer.get_vocab()      , tokenizer_slow.get_vocab()   )
              self.assertEqual(processor_fast.tokenizer.get_vocab()      , tokenizer_fast.get_vocab()   )
              self.assertEqual(tokenizer_slow.get_vocab()      , tokenizer_fast.get_vocab()   )
              self.assertIsInstance(processor_slow.tokenizer      , _UpperCamelCase   )
              self.assertIsInstance(processor_fast.tokenizer      , _UpperCamelCase   )

              self.assertEqual(processor_slow.image_processor.to_json_string()      , image_processor.to_json_string()   )
              self.assertEqual(processor_fast.image_processor.to_json_string()      , image_processor.to_json_string()   )
              self.assertIsInstance(processor_slow.image_processor      , _UpperCamelCase   )
              self.assertIsInstance(processor_fast.image_processor      , _UpperCamelCase   )


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






              """simple docstring"""


              __snake_case       	=							CLIPProcessor(tokenizer=self.get_tokenizer()      , image_processor=self.get_image_processor()   )
              processor.save_pretrained(self.tmpdirname   )

              __snake_case       	=							self.get_tokenizer(bos_token="""(BOS)"""      , eos_token="""(EOS)"""   )
              __snake_case       	=							self.get_image_processor(do_normalize=_UpperCamelCase      , padding_value=1.0   )

              __snake_case       	=							CLIPProcessor.from_pretrained(
                  self.tmpdirname      , bos_token="""(BOS)"""      , eos_token="""(EOS)"""      , do_normalize=_UpperCamelCase      , padding_value=1.0   )

              self.assertEqual(processor.tokenizer.get_vocab()      , tokenizer_add_kwargs.get_vocab()   )
              self.assertIsInstance(processor.tokenizer      , _UpperCamelCase   )

              self.assertEqual(processor.image_processor.to_json_string()      , image_processor_add_kwargs.to_json_string()   )
              self.assertIsInstance(processor.image_processor      , _UpperCamelCase   )


       def 			a		(		self   )      ->							Any:






              """simple docstring"""


              __snake_case       	=							self.get_image_processor()
              __snake_case       	=							self.get_tokenizer()

              __snake_case       	=							CLIPProcessor(tokenizer=_UpperCamelCase      , image_processor=_UpperCamelCase   )

              __snake_case       	=							self.prepare_image_inputs()

              __snake_case       	=							image_processor(_UpperCamelCase      , return_tensors="""np"""   )
              __snake_case       	=							processor(images=_UpperCamelCase      , return_tensors="""np"""   )

              for key in input_image_proc.keys():
                     self.assertAlmostEqual(input_image_proc[key].sum()      , input_processor[key].sum()      , delta=1E-2   )


       def 			a		(		self   )      ->							str:






              """simple docstring"""


              __snake_case       	=							self.get_image_processor()
              __snake_case       	=							self.get_tokenizer()

              __snake_case       	=							CLIPProcessor(tokenizer=_UpperCamelCase      , image_processor=_UpperCamelCase   )

              __snake_case       	=							"""lower newer"""

              __snake_case       	=							processor(text=_UpperCamelCase   )

              __snake_case       	=							tokenizer(_UpperCamelCase   )

              for key in encoded_tok.keys():
                     self.assertListEqual(encoded_tok[key]      , encoded_processor[key]   )


       def 			a		(		self   )      ->							str:






              """simple docstring"""


              __snake_case       	=							self.get_image_processor()
              __snake_case       	=							self.get_tokenizer()

              __snake_case       	=							CLIPProcessor(tokenizer=_UpperCamelCase      , image_processor=_UpperCamelCase   )

              __snake_case       	=							"""lower newer"""
              __snake_case       	=							self.prepare_image_inputs()

              __snake_case       	=							processor(text=_UpperCamelCase      , images=_UpperCamelCase   )

              self.assertListEqual(list(inputs.keys()   )      , ["""input_ids""", """attention_mask""", """pixel_values"""]   )

              # test if it raises when no input is passed
              with pytest.raises(_UpperCamelCase   ):
                     processor()


       def 			a		(		self   )      ->							Any:






              """simple docstring"""


              __snake_case       	=							self.get_image_processor()
              __snake_case       	=							self.get_tokenizer()

              __snake_case       	=							CLIPProcessor(tokenizer=_UpperCamelCase      , image_processor=_UpperCamelCase   )

              __snake_case       	=							[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

              __snake_case       	=							processor.batch_decode(_UpperCamelCase   )
              __snake_case       	=							tokenizer.batch_decode(_UpperCamelCase   )

              self.assertListEqual(_UpperCamelCase      , _UpperCamelCase   )






       def 			a		(		self   )      ->							int:






              """simple docstring"""


              __snake_case       	=							self.get_image_processor()
              __snake_case       	=							self.get_tokenizer()

              __snake_case       	=							CLIPProcessor(tokenizer=_UpperCamelCase      , image_processor=_UpperCamelCase   )

              __snake_case       	=							"""lower newer"""
              __snake_case       	=							self.prepare_image_inputs()

              __snake_case       	=							processor(text=_UpperCamelCase      , images=_UpperCamelCase   )

              self.assertListEqual(list(inputs.keys()   )      , processor.model_input_names   )

