




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

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

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

     def       a__			(						self       )		->							Any:
           _A							:  List[Any]    =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

           _A							:  Union[str, Any]    =		self.get_config()

           return config, pixel_values, labels

     def       a__			(						self       )		->							Any:
           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=_a	,  initializer_range=self.initializer_range	,  )

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

           # test greyscale images
           _A							:  Optional[int]    =		1
           _A							:  List[Any]    =		TFViTForImageClassification(_a       )
           _A							:  List[Any]    =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
           _A							:  List[Any]    =		model(_a       )
           self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size)       )



     def       a__			(						self       )		->							Any:
           _A							:  List[Any]    =		self.prepare_config_and_inputs()
           _A    ,     _A    ,     _A							:  str    =		config_and_inputs
           _A							:  Optional[Any]    =		{"""pixel_values""": pixel_values}
           return config, inputs_dict







@require_tf
class 		lowercase							(     UpperCamelCase__,UpperCamelCase__,unittest.TestCase	):
     _a											=		(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
     _a											=		(
         {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
         if is_tf_available()
         else {}
     )

     _a											=		False
     _a											=		False
     _a											=		False

     def       a__			(						self       )		->							Any:
           _A							:  List[Any]    =		TFViTModelTester(self       )
           _A							:  Dict    =		ConfigTester(self	,  config_class=_a	,  has_text_modality=_a	,  hidden_size=37       )

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

     @unittest.skip(reason="""ViT does not use inputs_embeds"""       )
     def       a__			(						self       )		->							Optional[int]:
           pass

     @unittest.skip(reason="""ViT does not use inputs_embeds"""       )
     def       a__			(						self       )		->							Optional[int]:
           pass

     def       a__			(						self       )		->							Any:
           _A    ,     _A							:  Tuple    =		self.model_tester.prepare_config_and_inputs_for_common()

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

     def       a__			(						self       )		->							Union[str, Any]:
           _A    ,     _A							:  Any    =		self.model_tester.prepare_config_and_inputs_for_common()

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

                 _A							:  Any    =		["""pixel_values"""]
                 self.assertListEqual(arg_names[:1]	,  _a       )

     def       a__			(						self       )		->							Any:
           _A							:  Union[str, Any]    =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*_a       )

     def       a__			(						self       )		->							List[Any]:
           _A							:  Optional[int]    =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*_a       )



     @slow
     def       a__			(						self       )		->							Tuple:
           _A							:  str    =		TFViTModel.from_pretrained("""google/vit-base-patch16-224"""       )
           self.assertIsNotNone(_a       )







def        lowerCAmelCase_			(  ):
      _A							:  Optional[Any]    =		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
      return image







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

     @cached_property
     def       a__			(						self       )		->							Optional[Any]:
           return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""       ) if is_vision_available() else None



     @slow
     def       a__			(						self       )		->							Dict:
           _A							:  List[Any]    =		TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""       )

           _A							:  Tuple    =		self.default_image_processor
           _A							:  Any    =		prepare_img()
           _A							:  List[Any]    =		image_processor(images=_a	,  return_tensors="""tf"""       )

           # forward pass
           _A							:  str    =		model(**_a       )

           # verify the logits
           _A							:  List[str]    =		tf.TensorShape((1, 1000)       )
           self.assertEqual(outputs.logits.shape	,  _a       )

           _A							:  Dict    =		tf.constant([-0.2744, 0.8215, -0.0836]       )

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







import json
import os
import unittest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin







@require_tokenizers
class 		lowercase							(     UpperCamelCase__,unittest.TestCase	):
     _a											=		CLIPTokenizer
     _a											=		CLIPTokenizerFast
     _a											=		True
     _a											=		{}
     _a											=		False

     def       a__			(						self       )		->							Optional[Any]:
           super().setUp()

           # fmt: off
           _A							:  int    =		["""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
           _A							:  str    =		dict(zip(_a	,  range(len(_a       )       )       )       )
           _A							:  Optional[int]    =		["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>"""]
           _A							:  str    =		{"""unk_token""": """<unk>"""}

           _A							:  Union[str, Any]    =		os.path.join(self.tmpdirname	,  VOCAB_FILES_NAMES["""vocab_file"""]       )
           _A							:  Union[str, Any]    =		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(_a       ) + """\n"""       )
           with open(self.merges_file	,  """w"""	,  encoding="""utf-8"""       ) as fp:
                 fp.write("""\n""".join(_a       )       )

     def       a__			(						self	,  **_a       )		->							List[Any]:
           kwargs.update(self.special_tokens_map       )
           return CLIPTokenizer.from_pretrained(self.tmpdirname	,  **_a       )

     def       a__			(						self	,  **_a       )		->							Union[str, Any]:
           kwargs.update(self.special_tokens_map       )
           return CLIPTokenizerFast.from_pretrained(self.tmpdirname	,  **_a       )

     def       a__			(						self	,  _a       )		->							str:
           _A							:  Tuple    =		"""lower newer"""
           _A							:  Optional[Any]    =		"""lower newer"""
           return input_text, output_text

     def       a__			(						self       )		->							List[Any]:
           _A							:  Optional[int]    =		CLIPTokenizer(self.vocab_file	,  self.merges_file	,  **self.special_tokens_map       )
           _A							:  str    =		"""lower newer"""
           _A							:  List[str]    =		["""lo""", """w""", """er</w>""", """n""", """e""", """w""", """er</w>"""]
           _A							:  int    =		tokenizer.tokenize(_a       )
           self.assertListEqual(_a	,  _a       )

           _A							:  str    =		tokens + [tokenizer.unk_token]
           _A							:  Dict    =		[10, 2, 16, 9, 3, 2, 16, 20]
           self.assertListEqual(tokenizer.convert_tokens_to_ids(_a       )	,  _a       )

     @require_ftfy
     def       a__			(						self       )		->							Tuple:
           for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
                 with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})'''       ):
                       _A							:  Dict    =		self.tokenizer_class.from_pretrained(_a	,  **_a       )
                       _A							:  str    =		self.rust_tokenizer_class.from_pretrained(_a	,  **_a       )

                       _A							:  List[str]    =		"""A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d."""
                       _A							:  Dict    =		tokenizer_s.tokenize(_a       )
                       _A							:  Dict    =		tokenizer_r.tokenize(_a       )

                       self.assertListEqual(_a	,  _a       )

                       # Test that the tokenization is identical on an example containing a character (Latin Small Letter A
                       # with Tilde) encoded in 2 different ways
                       _A							:  Tuple    =		"""xa\u0303y""" + """ """ + """x\xe3y"""
                       _A							:  Dict    =		tokenizer_s.tokenize(_a       )
                       _A							:  Dict    =		tokenizer_r.tokenize(_a       )

                       self.assertListEqual(_a	,  _a       )

                       # Test that the tokenization is identical on unicode of space type
                       _A							:  Any    =		[
                           """\u0009""",  # (horizontal tab, '\t')
                           """\u000B""",  # (vertical tab)
                           """\u000C""",  # (form feed)
                           """\u0020""",  # (space, ' ')
                           """\u200E""",  # (left-to-right mark):w
                           """\u200F""",  # (right-to-left mark)
                       ]
                       for unicode_seq in spaces_unicodes:
                             _A							:  Optional[int]    =		tokenizer_s.tokenize(_a       )
                             _A							:  List[Any]    =		tokenizer_r.tokenize(_a       )

                             self.assertListEqual(_a	,  _a       )

                       # Test that the tokenization is identical on unicode of line break type
                       _A							:  int    =		[
                           """\u000A""",  # (line feed, '\n')
                           """\r\n""",  # (carriage return and line feed, '\r\n')
                           """\u000D""",  # (carriage return, '\r')
                           """\r""",  # (carriage return, '\r')
                           """\u000D""",  # (carriage return, '\r')
                           """\u2028""",  # (line separator)
                           """\u2029""",  # (paragraph separator)
                           # "\u0085", # (next line)
                       ]

                       # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms
                       # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a
                       # space (and thus into an empty list).

                       for unicode_seq in line_break_unicodes:
                             _A							:  Optional[Any]    =		tokenizer_s.tokenize(_a       )
                             _A							:  Tuple    =		tokenizer_r.tokenize(_a       )

                             self.assertListEqual(_a	,  _a       )

     def       a__			(						self       )		->							Any:
           # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space`
           for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
                 with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})'''       ):
                       _A							:  List[Any]    =		"""hello"""  # `hello` is a token in the vocabulary of `pretrained_name`
                       _A							:  str    =		F'''{text_of_1_token} {text_of_1_token}'''

                       _A							:  str    =		self.rust_tokenizer_class.from_pretrained(
                           _a	,  use_fast=_a	,  )
                       _A							:  Dict    =		tokenizer_r(_a	,  return_offsets_mapping=_a	,  add_special_tokens=_a       )
                       self.assertEqual(encoding.offset_mapping[0]	,  (0, len(_a       ))       )
                       self.assertEqual(
                           encoding.offset_mapping[1]	,  (len(_a       ) + 1, len(_a       ) + 1 + len(_a       ))	,  )

                       _A							:  Any    =		F''' {text}'''

                       _A							:  List[Any]    =		self.rust_tokenizer_class.from_pretrained(
                           _a	,  use_fast=_a	,  )
                       _A							:  Optional[int]    =		tokenizer_r(_a	,  return_offsets_mapping=_a	,  add_special_tokens=_a       )
                       self.assertEqual(encoding.offset_mapping[0]	,  (1, 1 + len(_a       ))       )
                       self.assertEqual(
                           encoding.offset_mapping[1]	,  (1 + len(_a       ) + 1, 1 + len(_a       ) + 1 + len(_a       ))	,  )

     def       a__			(						self       )		->							List[Any]:
           # Test related to the breaking change introduced in transformers v4.17.0
           # We need to check that an error in raised when the user try to load a previous version of the tokenizer.
           with self.assertRaises(_a       ) as context:
                 self.rust_tokenizer_class.from_pretrained("""robot-test/old-clip-tokenizer"""       )

           self.assertTrue(
               context.exception.args[0].startswith(
                   """The `backend_tokenizer` provided does not match the expected format."""       )       )

     @require_ftfy
     def       a__			(						self       )		->							str:
           super().test_tokenization_python_rust_equals()



     def       a__			(						self       )		->							Union[str, Any]:
           # CLIP always lower cases letters
           pass

