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






   '''simple docstring'''







   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 ,   ):

      '''simple docstring'''







      UpperCamelCase       =  parent
      UpperCamelCase       =  batch_size
      UpperCamelCase       =  image_size
      UpperCamelCase       =  patch_size
      UpperCamelCase       =  num_channels
      UpperCamelCase       =  is_training
      UpperCamelCase       =  use_labels
      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       =  type_sequence_label_size
      UpperCamelCase       =  initializer_range
      UpperCamelCase       =  scope

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







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

      UpperCamelCase       =  self.get_config()

      return config, pixel_values, labels







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      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       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):

      '''simple docstring'''







      UpperCamelCase       =  TFViTModel(config=lowerCamelCase__   )
      UpperCamelCase       =  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.
      UpperCamelCase       =  self.image_size // 2
      UpperCamelCase       =  pixel_values[:, :, :image_size, :image_size]
      UpperCamelCase       =  model(lowerCamelCase__ ,   interpolate_pos_encoding=lowerCamelCase__ ,   training=lowerCamelCase__   )
      UpperCamelCase       =  (image_size // self.patch_size) ** 2 + 1
      self.parent.assertEqual(result.last_hidden_state.shape ,   (self.batch_size, seq_length, self.hidden_size)   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):

      '''simple docstring'''







      UpperCamelCase       =  self.type_sequence_label_size
      UpperCamelCase       =  TFViTForImageClassification(lowerCamelCase__   )
      UpperCamelCase       =  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.
      UpperCamelCase       =  self.image_size // 2
      UpperCamelCase       =  pixel_values[:, :, :image_size, :image_size]
      UpperCamelCase       =  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
      UpperCamelCase       =  1
      UpperCamelCase       =  TFViTForImageClassification(lowerCamelCase__   )
      UpperCamelCase       =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
      UpperCamelCase       =  model(lowerCamelCase__   )
      self.parent.assertEqual(result.logits.shape ,   (self.batch_size, self.type_sequence_label_size)   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.prepare_config_and_inputs()
      UpperCamelCase     , UpperCamelCase     , UpperCamelCase       =  config_and_inputs
      UpperCamelCase       =  {'''pixel_values''': pixel_values}
      return config, inputs_dict




@require_tf
class 						lowercase__	(  snake_case_,					snake_case_,					unittest.TestCase   ):






   '''simple docstring'''







   _snake_case		   =							(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
   _snake_case		   =							(
       {'''feature-extraction''': TFViTModel, '''image-classification''': TFViTForImageClassification}
       if is_tf_available()
       else {}
   )

   _snake_case		   =							False
   _snake_case		   =							False
   _snake_case		   =							False







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  TFViTModelTester(self   )
      UpperCamelCase       =  ConfigTester(self ,   config_class=lowerCamelCase__ ,   has_text_modality=lowerCamelCase__ ,   hidden_size=3_7   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      self.config_tester.run_common_tests()







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

      '''simple docstring'''







      pass







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

      '''simple docstring'''







      pass







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase     , UpperCamelCase       =  self.model_tester.prepare_config_and_inputs_for_common()

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







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase     , UpperCamelCase       =  self.model_tester.prepare_config_and_inputs_for_common()

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

         UpperCamelCase       =  ['''pixel_values''']
         self.assertListEqual(arg_names[:1] ,   lowerCamelCase__   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCamelCase__   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__   )







   @slow
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  TFViTModel.from_pretrained('''google/vit-base-patch16-224'''   )
      self.assertIsNotNone(lowerCamelCase__   )




def  __snake_case     (    ):
   UpperCamelCase       =  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
   return image


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






   '''simple docstring'''







   @cached_property
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''   ) if is_vision_available() else None







   @slow
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''   )

      UpperCamelCase       =  self.default_image_processor
      UpperCamelCase       =  prepare_img()
      UpperCamelCase       =  image_processor(images=lowerCamelCase__ ,   return_tensors='''tf'''   )

      # forward pass
      UpperCamelCase       =  model(**lowerCamelCase__   )

      # verify the logits
      UpperCamelCase       =  tf.TensorShape((1, 1_0_0_0)   )
      self.assertEqual(outputs.logits.shape ,   lowerCamelCase__   )

      UpperCamelCase       =  tf.constant([-0.2744, 0.8215, -0.0836]   )

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







'''simple docstring'''


import os
from typing import List, Optional, Union

from ...image_processing_utils import BatchFeature
from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType
from ..auto import AutoTokenizer


class 						lowercase__	(  snake_case_   ):






   '''simple docstring'''







   _snake_case		   =							['''image_processor''', '''tokenizer''']
   _snake_case		   =							'''BlipImageProcessor'''
   _snake_case		   =							'''AutoTokenizer'''







   def __init__(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):

      '''simple docstring'''







      super().__init__(lowerCamelCase__ ,   lowerCamelCase__   )

      # add QFormer tokenizer
      UpperCamelCase       =  qformer_tokenizer







   def __call__(			self ,   lowerCamelCase__ = None ,   lowerCamelCase__ = None ,   lowerCamelCase__ = True ,   lowerCamelCase__ = False ,   lowerCamelCase__ = None ,   lowerCamelCase__ = None ,   lowerCamelCase__ = 0 ,   lowerCamelCase__ = None ,   lowerCamelCase__ = None ,   lowerCamelCase__ = False ,   lowerCamelCase__ = False ,   lowerCamelCase__ = False ,   lowerCamelCase__ = False ,   lowerCamelCase__ = False ,   lowerCamelCase__ = True ,   lowerCamelCase__ = None ,   **lowerCamelCase__ ,   ):

      '''simple docstring'''







      if images is None and text is None:
         raise ValueError('''You have to specify at least images or text.'''   )

      UpperCamelCase       =  BatchFeature()

      if text is not None:
         UpperCamelCase       =  self.tokenizer(
             text=lowerCamelCase__ ,   add_special_tokens=lowerCamelCase__ ,   padding=lowerCamelCase__ ,   truncation=lowerCamelCase__ ,   max_length=lowerCamelCase__ ,   stride=lowerCamelCase__ ,   pad_to_multiple_of=lowerCamelCase__ ,   return_attention_mask=lowerCamelCase__ ,   return_overflowing_tokens=lowerCamelCase__ ,   return_special_tokens_mask=lowerCamelCase__ ,   return_offsets_mapping=lowerCamelCase__ ,   return_token_type_ids=lowerCamelCase__ ,   return_length=lowerCamelCase__ ,   verbose=lowerCamelCase__ ,   return_tensors=lowerCamelCase__ ,   **lowerCamelCase__ ,   )
         encoding.update(lowerCamelCase__   )
         UpperCamelCase       =  self.qformer_tokenizer(
             text=lowerCamelCase__ ,   add_special_tokens=lowerCamelCase__ ,   padding=lowerCamelCase__ ,   truncation=lowerCamelCase__ ,   max_length=lowerCamelCase__ ,   stride=lowerCamelCase__ ,   pad_to_multiple_of=lowerCamelCase__ ,   return_attention_mask=lowerCamelCase__ ,   return_overflowing_tokens=lowerCamelCase__ ,   return_special_tokens_mask=lowerCamelCase__ ,   return_offsets_mapping=lowerCamelCase__ ,   return_token_type_ids=lowerCamelCase__ ,   return_length=lowerCamelCase__ ,   verbose=lowerCamelCase__ ,   return_tensors=lowerCamelCase__ ,   **lowerCamelCase__ ,   )
         UpperCamelCase       =  qformer_text_encoding.pop('''input_ids'''   )
         UpperCamelCase       =  qformer_text_encoding.pop('''attention_mask'''   )

      if images is not None:
         UpperCamelCase       =  self.image_processor(lowerCamelCase__ ,   return_tensors=lowerCamelCase__   )
         encoding.update(lowerCamelCase__   )

      return encoding







   def       UpperCAmelCase				(			self ,   *lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      return self.tokenizer.batch_decode(*lowerCamelCase__ ,   **lowerCamelCase__   )







   def       UpperCAmelCase				(			self ,   *lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      return self.tokenizer.decode(*lowerCamelCase__ ,   **lowerCamelCase__   )







   @property
   # Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.tokenizer.model_input_names
      UpperCamelCase       =  self.image_processor.model_input_names
      return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names   )   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      if os.path.isfile(lowerCamelCase__   ):
         raise ValueError(f'Provided path ({save_directory}) should be a directory, not a file'   )
      os.makedirs(lowerCamelCase__ ,   exist_ok=lowerCamelCase__   )
      UpperCamelCase       =  os.path.join(lowerCamelCase__ ,   '''qformer_tokenizer'''   )
      self.qformer_tokenizer.save_pretrained(lowerCamelCase__   )
      return super().save_pretrained(lowerCamelCase__ ,   **lowerCamelCase__   )







   @classmethod
   def       UpperCAmelCase				(			cls ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      UpperCamelCase       =  AutoTokenizer.from_pretrained(lowerCamelCase__ ,   subfolder='''qformer_tokenizer'''   )
      UpperCamelCase       =  cls._get_arguments_from_pretrained(lowerCamelCase__ ,   **lowerCamelCase__   )
      args.append(lowerCamelCase__   )
      return cls(*lowerCamelCase__   )





