


'''simple docstring'''







import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin


class  lowerCAmelCase_						(       snake_case__      ):





  """simple docstring"""



  a_       :List[str]												=["""image_processor""", """tokenizer"""]
  a_       :List[str]												="""AutoImageProcessor"""
  a_       :int												="""AutoTokenizer"""

  def __init__(					self     :							List[Any]				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]=None				,							SCREAMING_SNAKE_CASE__     :							List[Any]=None				,							**SCREAMING_SNAKE_CASE__     :							int						):





       '''simple docstring'''

       __a       =   None
       if "feature_extractor" in kwargs:
            warnings.warn(
                """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
                """ instead."""				,							SCREAMING_SNAKE_CASE__				,							)
            __a       =   kwargs.pop("""feature_extractor"""						)

       __a       =   image_processor if image_processor is not None else feature_extractor
       if image_processor is None:
            raise ValueError("""You need to specify an `image_processor`."""						)
       if tokenizer is None:
            raise ValueError("""You need to specify a `tokenizer`."""						)

       super().__init__(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__						)
       __a       =   self.image_processor
       __a       =   False

  def __call__(					self     :							Union[str, Any]				,							*SCREAMING_SNAKE_CASE__     :							Optional[int]				,							**SCREAMING_SNAKE_CASE__     :							str						):





       '''simple docstring'''

       if self._in_target_context_manager:
            return self.current_processor(*SCREAMING_SNAKE_CASE__				,							**SCREAMING_SNAKE_CASE__						)

       __a       =   kwargs.pop("""images"""				,							SCREAMING_SNAKE_CASE__						)
       __a       =   kwargs.pop("""text"""				,							SCREAMING_SNAKE_CASE__						)
       if len(SCREAMING_SNAKE_CASE__						) > 0:
            __a       =   args[0]
            __a       =   args[1:]

       if images is None and text is None:
            raise ValueError("""You need to specify either an `images` or `text` input to process."""						)

       if images is not None:
            __a       =   self.image_processor(SCREAMING_SNAKE_CASE__				,							*SCREAMING_SNAKE_CASE__				,							**SCREAMING_SNAKE_CASE__						)
       if text is not None:
            __a       =   self.tokenizer(SCREAMING_SNAKE_CASE__				,							**SCREAMING_SNAKE_CASE__						)

       if text is None:
            return inputs
       elif images is None:
            return encodings
       else:
            __a       =   encodings["""input_ids"""]
            return inputs

  def __a						(					self     :							Union[str, Any]				,							*SCREAMING_SNAKE_CASE__     :							List[str]				,							**SCREAMING_SNAKE_CASE__     :							Tuple						):





       '''simple docstring'''

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

  def __a						(					self     :							List[Any]				,							*SCREAMING_SNAKE_CASE__     :							Optional[Any]				,							**SCREAMING_SNAKE_CASE__     :							List[str]						):





       '''simple docstring'''

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

  @contextmanager
  def __a						(					self     :							Dict						):





       '''simple docstring'''

       warnings.warn(
           """`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your """
           """labels by using the argument `text` of the regular `__call__` method (either in the same call as """
           """your images inputs, or in a separate call."""						)
       __a       =   True
       __a       =   self.tokenizer
       yield
       __a       =   self.image_processor
       __a       =   False

  def __a						(					self     :							List[Any]				,							SCREAMING_SNAKE_CASE__     :							List[str]				,							SCREAMING_SNAKE_CASE__     :							Dict=False				,							SCREAMING_SNAKE_CASE__     :							int=None						):





       '''simple docstring'''

       if added_vocab is None:
            __a       =   self.tokenizer.get_added_vocab()

       __a       =   {}

       while tokens:
            __a       =   re.search(r"""<s_(.*?)>"""				,							SCREAMING_SNAKE_CASE__				,							re.IGNORECASE						)
            if start_token is None:
                 break
            __a       =   start_token.group(1						)
            __a       =   re.search(rf'''</s_{key}>'''				,							SCREAMING_SNAKE_CASE__				,							re.IGNORECASE						)
            __a       =   start_token.group()
            if end_token is None:
                 __a       =   tokens.replace(SCREAMING_SNAKE_CASE__				,							""""""						)
            else:
                 __a       =   end_token.group()
                 __a       =   re.escape(SCREAMING_SNAKE_CASE__						)
                 __a       =   re.escape(SCREAMING_SNAKE_CASE__						)
                 __a       =   re.search(f'''{start_token_escaped}(.*?){end_token_escaped}'''				,							SCREAMING_SNAKE_CASE__				,							re.IGNORECASE						)
                 if content is not None:
                      __a       =   content.group(1						).strip()
                      if r"<s_" in content and r"</s_" in content:  # non-leaf node
                           __a       =   self.tokenajson(SCREAMING_SNAKE_CASE__				,							is_inner_value=SCREAMING_SNAKE_CASE__				,							added_vocab=SCREAMING_SNAKE_CASE__						)
                           if value:
                                if len(SCREAMING_SNAKE_CASE__						) == 1:
                                     __a       =   value[0]
                                __a       =   value
                      else:  # leaf nodes
                           __a       =   []
                           for leaf in content.split(r"""<sep/>"""						):
                                __a       =   leaf.strip()
                                if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>":
                                     __a       =   leaf[1:-2]  # for categorical special tokens
                                output[key].append(SCREAMING_SNAKE_CASE__						)
                           if len(output[key]						) == 1:
                                __a       =   output[key][0]

                 __a       =   tokens[tokens.find(SCREAMING_SNAKE_CASE__						) + len(SCREAMING_SNAKE_CASE__						) :].strip()
                 if tokens[:6] == r"<sep/>":  # non-leaf nodes
                      return [output] + self.tokenajson(tokens[6:]				,							is_inner_value=SCREAMING_SNAKE_CASE__				,							added_vocab=SCREAMING_SNAKE_CASE__						)

       if len(SCREAMING_SNAKE_CASE__						):
            return [output] if is_inner_value else output
       else:
            return [] if is_inner_value else {"text_sequence": tokens}

  @property
  def __a						(					self     :							List[str]						):





       '''simple docstring'''

       warnings.warn(
           """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."""				,							SCREAMING_SNAKE_CASE__				,							)
       return self.image_processor_class


  @property
  def __a						(					self     :							Optional[Any]						):





       '''simple docstring'''

       warnings.warn(
           """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."""				,							SCREAMING_SNAKE_CASE__				,							)
       return self.image_processor










'''simple docstring'''







import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch
 from torch import nn

 from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
 from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import ViTImageProcessor


class  lowerCAmelCase_						:





  """simple docstring"""



  def __init__(					self     :							Any				,							SCREAMING_SNAKE_CASE__     :							int				,							SCREAMING_SNAKE_CASE__     :							List[str]=1_3				,							SCREAMING_SNAKE_CASE__     :							int=3_0				,							SCREAMING_SNAKE_CASE__     :							List[str]=2				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]=3				,							SCREAMING_SNAKE_CASE__     :							List[str]=True				,							SCREAMING_SNAKE_CASE__     :							Dict=True				,							SCREAMING_SNAKE_CASE__     :							str=3_2				,							SCREAMING_SNAKE_CASE__     :							Tuple=5				,							SCREAMING_SNAKE_CASE__     :							List[Any]=4				,							SCREAMING_SNAKE_CASE__     :							Dict=3_7				,							SCREAMING_SNAKE_CASE__     :							Any="gelu"				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]=0.1				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]=0.1				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]=1_0				,							SCREAMING_SNAKE_CASE__     :							str=0.0_2				,							SCREAMING_SNAKE_CASE__     :							Union[str, Any]=None				,							SCREAMING_SNAKE_CASE__     :							Any=2				,							):





       '''simple docstring'''

       __a       =   parent
       __a       =   batch_size
       __a       =   image_size
       __a       =   patch_size
       __a       =   num_channels
       __a       =   is_training
       __a       =   use_labels
       __a       =   hidden_size
       __a       =   num_hidden_layers
       __a       =   num_attention_heads
       __a       =   intermediate_size
       __a       =   hidden_act
       __a       =   hidden_dropout_prob
       __a       =   attention_probs_dropout_prob
       __a       =   type_sequence_label_size
       __a       =   initializer_range
       __a       =   scope
       __a       =   encoder_stride

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

  def __a						(					self     :							Any						):





       '''simple docstring'''

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

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

       __a       =   self.get_config()

       return config, pixel_values, labels

  def __a						(					self     :							Optional[int]						):





       '''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=SCREAMING_SNAKE_CASE__				,							initializer_range=self.initializer_range				,							encoder_stride=self.encoder_stride				,							)

  def __a						(					self     :							Dict				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]				,							SCREAMING_SNAKE_CASE__     :							Any				,							SCREAMING_SNAKE_CASE__     :							Optional[Any]						):





       '''simple docstring'''

       __a       =   ViTModel(config=SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()
       __a       =   model(SCREAMING_SNAKE_CASE__						)
       self.parent.assertEqual(result.last_hidden_state.shape				,							(self.batch_size, self.seq_length, self.hidden_size)						)

  def __a						(					self     :							str				,							SCREAMING_SNAKE_CASE__     :							List[str]				,							SCREAMING_SNAKE_CASE__     :							Tuple				,							SCREAMING_SNAKE_CASE__     :							str						):





       '''simple docstring'''

       __a       =   ViTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()
       __a       =   model(SCREAMING_SNAKE_CASE__						)
       self.parent.assertEqual(
           result.reconstruction.shape				,							(self.batch_size, self.num_channels, self.image_size, self.image_size)						)

       # test greyscale images
       __a       =   1
       __a       =   ViTForMaskedImageModeling(SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()

       __a       =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
       __a       =   model(SCREAMING_SNAKE_CASE__						)
       self.parent.assertEqual(result.reconstruction.shape				,							(self.batch_size, 1, self.image_size, self.image_size)						)

  def __a						(					self     :							str				,							SCREAMING_SNAKE_CASE__     :							Tuple				,							SCREAMING_SNAKE_CASE__     :							List[Any]				,							SCREAMING_SNAKE_CASE__     :							Dict						):





       '''simple docstring'''

       __a       =   self.type_sequence_label_size
       __a       =   ViTForImageClassification(SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()
       __a       =   model(SCREAMING_SNAKE_CASE__				,							labels=SCREAMING_SNAKE_CASE__						)
       self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.type_sequence_label_size)						)

       # test greyscale images
       __a       =   1
       __a       =   ViTForImageClassification(SCREAMING_SNAKE_CASE__						)
       model.to(SCREAMING_SNAKE_CASE__						)
       model.eval()

       __a       =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
       __a       =   model(SCREAMING_SNAKE_CASE__						)
       self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.type_sequence_label_size)						)


  def __a						(					self     :							str						):





       '''simple docstring'''

       __a       =   self.prepare_config_and_inputs()
       (
           (
           __a
       ) ,						(
           __a
       ) ,						(
           __a
       ) ,						
       )       =   config_and_inputs
       __a       =   {"""pixel_values""": pixel_values}
       return config, inputs_dict


@require_torch
class  lowerCAmelCase_						(       snake_case__		,    snake_case__		,    unittest.TestCase      ):





  """simple docstring"""



  a_       :str												=(
      (
          ViTModel,
          ViTForImageClassification,
          ViTForMaskedImageModeling,
      )
      if is_torch_available()
      else ()
  )
  a_       :Tuple												=(
      {"""feature-extraction""": ViTModel, """image-classification""": ViTForImageClassification}
      if is_torch_available()
      else {}
  )
  a_       :List[str]												=True

  a_       :str												=False
  a_       :Optional[int]												=False
  a_       :Tuple												=False

  def __a						(					self     :							str						):





       '''simple docstring'''

       __a       =   ViTModelTester(self						)
       __a       =   ConfigTester(self				,							config_class=SCREAMING_SNAKE_CASE__				,							has_text_modality=SCREAMING_SNAKE_CASE__				,							hidden_size=3_7						)

  def __a						(					self     :							Dict						):





       '''simple docstring'''

       self.config_tester.run_common_tests()

  @unittest.skip(reason="""ViT does not use inputs_embeds"""						)
  def __a						(					self     :							List[Any]						):





       '''simple docstring'''

       pass

  def __a						(					self     :							int						):





       '''simple docstring'''

       __a ,						__a       =   self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
            __a       =   model_class(SCREAMING_SNAKE_CASE__						)
            self.assertIsInstance(model.get_input_embeddings()				,							(nn.Module)						)
            __a       =   model.get_output_embeddings()
            self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE__				,							nn.Linear						)						)

  def __a						(					self     :							int						):





       '''simple docstring'''

       __a ,						__a       =   self.model_tester.prepare_config_and_inputs_for_common()

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

            __a       =   ["""pixel_values"""]
            self.assertListEqual(arg_names[:1]				,							SCREAMING_SNAKE_CASE__						)

  def __a						(					self     :							List[str]						):





       '''simple docstring'''

       __a       =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__						)

  def __a						(					self     :							Union[str, Any]						):





       '''simple docstring'''

       __a       =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE__						)

  def __a						(					self     :							Optional[Any]						):





       '''simple docstring'''

       __a       =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__						)


  @slow
  def __a						(					self     :							List[str]						):





       '''simple docstring'''

       for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            __a       =   ViTModel.from_pretrained(SCREAMING_SNAKE_CASE__						)
            self.assertIsNotNone(SCREAMING_SNAKE_CASE__						)




def        __lowercase						(      )				->					int:
     """simple docstring"""




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



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





  """simple docstring"""



  @cached_property
  def __a						(					self     :							str						):





       '''simple docstring'''

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

  @slow
  def __a						(					self     :							Dict						):





       '''simple docstring'''

       __a       =   ViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""						).to(SCREAMING_SNAKE_CASE__						)

       __a       =   self.default_image_processor
       __a       =   prepare_img()
       __a       =   image_processor(images=SCREAMING_SNAKE_CASE__				,							return_tensors="""pt"""						).to(SCREAMING_SNAKE_CASE__						)

       # forward pass
       with torch.no_grad():
            __a       =   model(**SCREAMING_SNAKE_CASE__						)

       # verify the logits
       __a       =   torch.Size((1, 1_0_0_0)						)
       self.assertEqual(outputs.logits.shape				,							SCREAMING_SNAKE_CASE__						)

       __a       =   torch.tensor([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]						).to(SCREAMING_SNAKE_CASE__						)

       self.assertTrue(torch.allclose(outputs.logits[0, :3]				,							SCREAMING_SNAKE_CASE__				,							atol=1E-4						)						)

  @slow
  def __a						(					self     :							int						):





       '''simple docstring'''

       __a       =   ViTModel.from_pretrained("""facebook/dino-vits8"""						).to(SCREAMING_SNAKE_CASE__						)

       __a       =   ViTImageProcessor.from_pretrained("""facebook/dino-vits8"""				,							size=4_8_0						)
       __a       =   prepare_img()
       __a       =   image_processor(images=SCREAMING_SNAKE_CASE__				,							return_tensors="""pt"""						)
       __a       =   inputs.pixel_values.to(SCREAMING_SNAKE_CASE__						)

       # forward pass
       with torch.no_grad():
            __a       =   model(SCREAMING_SNAKE_CASE__				,							interpolate_pos_encoding=SCREAMING_SNAKE_CASE__						)

       # verify the logits
       __a       =   torch.Size((1, 3_6_0_1, 3_8_4)						)
       self.assertEqual(outputs.last_hidden_state.shape				,							SCREAMING_SNAKE_CASE__						)

       __a       =   torch.tensor(
           [[4.2_3_4_0, 4.3_9_0_6, -6.6_6_9_2], [4.5_4_6_3, 1.8_9_2_8, -6.7_2_5_7], [4.4_4_2_9, 0.8_4_9_6, -5.8_5_8_5]]						).to(SCREAMING_SNAKE_CASE__						)

       self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]				,							SCREAMING_SNAKE_CASE__				,							atol=1E-4						)						)


  @slow
  @require_accelerate
  @require_torch_gpu
  def __a						(					self     :							Optional[Any]						):





       '''simple docstring'''

       __a       =   ViTModel.from_pretrained("""facebook/dino-vits8"""				,							torch_dtype=torch.floataa				,							device_map="""auto"""						)
       __a       =   self.default_image_processor

       __a       =   prepare_img()
       __a       =   image_processor(images=SCREAMING_SNAKE_CASE__				,							return_tensors="""pt"""						)
       __a       =   inputs.pixel_values.to(SCREAMING_SNAKE_CASE__						)

       # forward pass to make sure inference works in fp16
       with torch.no_grad():
            __a       =   model(SCREAMING_SNAKE_CASE__						)






