





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

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__=13,		__magic_name__=30,		__magic_name__=2,		__magic_name__=3,		__magic_name__=True,		__magic_name__=True,		__magic_name__=32,		__magic_name__=5,		__magic_name__=4,		__magic_name__=37,		__magic_name__="gelu",		__magic_name__=0.1,		__magic_name__=0.1,		__magic_name__=10,		__magic_name__=0.02,		__magic_name__=None,		__magic_name__=2,		) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				List[Any]          =      parent
       UpperCamelCase__							:				Tuple          =      batch_size
       UpperCamelCase__							:				Any          =      image_size
       UpperCamelCase__							:				List[str]          =      patch_size
       UpperCamelCase__							:				int          =      num_channels
       UpperCamelCase__							:				str          =      is_training
       UpperCamelCase__							:				Optional[int]          =      use_labels
       UpperCamelCase__							:				int          =      hidden_size
       UpperCamelCase__							:				Optional[Any]          =      num_hidden_layers
       UpperCamelCase__							:				Optional[Any]          =      num_attention_heads
       UpperCamelCase__							:				Dict          =      intermediate_size
       UpperCamelCase__							:				Optional[int]          =      hidden_act
       UpperCamelCase__							:				List[str]          =      hidden_dropout_prob
       UpperCamelCase__							:				Union[str, Any]          =      attention_probs_dropout_prob
       UpperCamelCase__							:				Any          =      type_sequence_label_size
       UpperCamelCase__							:				Optional[Any]          =      initializer_range
       UpperCamelCase__							:				List[str]          =      scope
       UpperCamelCase__							:				Dict          =      encoder_stride

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




   def       UpperCamelCase__  (    self							) ->      Union[str, Any]:






       """simple docstring"""
       UpperCamelCase__							:				Optional[Any]          =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

       UpperCamelCase__							:				Dict          =      self.get_config()

       return config, pixel_values, labels




   def       UpperCamelCase__  (    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=UpperCAmelCase__,		initializer_range=self.initializer_range,		encoder_stride=self.encoder_stride,		)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:				int          =      ViTModel(config=UpperCAmelCase__							)
       model.to(UpperCAmelCase__							)
       model.eval()
       UpperCamelCase__							:				Tuple          =      model(UpperCAmelCase__							)
       self.parent.assertEqual(result.last_hidden_state.shape,		(self.batch_size, self.seq_length, self.hidden_size)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Union[str, Any]:






       """simple docstring"""
       UpperCamelCase__							:				List[str]          =      ViTForMaskedImageModeling(config=UpperCAmelCase__							)
       model.to(UpperCAmelCase__							)
       model.eval()
       UpperCamelCase__							:				Any          =      model(UpperCAmelCase__							)
       self.parent.assertEqual(
           result.reconstruction.shape,		(self.batch_size, self.num_channels, self.image_size, self.image_size)							)

       # test greyscale images
       UpperCamelCase__							:				List[str]          =      1
       UpperCamelCase__							:				int          =      ViTForMaskedImageModeling(UpperCAmelCase__							)
       model.to(UpperCAmelCase__							)
       model.eval()

       UpperCamelCase__							:				str          =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
       UpperCamelCase__							:				Dict          =      model(UpperCAmelCase__							)
       self.parent.assertEqual(result.reconstruction.shape,		(self.batch_size, 1, self.image_size, self.image_size)							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				Any          =      self.type_sequence_label_size
       UpperCamelCase__							:				Union[str, Any]          =      ViTForImageClassification(UpperCAmelCase__							)
       model.to(UpperCAmelCase__							)
       model.eval()
       UpperCamelCase__							:				Optional[int]          =      model(UpperCAmelCase__,		labels=UpperCAmelCase__							)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.type_sequence_label_size)							)

       # test greyscale images
       UpperCamelCase__							:				Optional[Any]          =      1
       UpperCamelCase__							:				Optional[Any]          =      ViTForImageClassification(UpperCAmelCase__							)
       model.to(UpperCAmelCase__							)
       model.eval()

       UpperCamelCase__							:				List[str]          =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
       UpperCamelCase__							:				Tuple          =      model(UpperCAmelCase__							)
       self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.type_sequence_label_size)							)




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






       """simple docstring"""
       UpperCamelCase__							:				List[str]          =      self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase__  
       )   ,(
           UpperCamelCase__  
       )   ,(
           UpperCamelCase__  
       )   ,  
       )							:				List[str]          =      config_and_inputs
       UpperCamelCase__							:				Any          =      {'''pixel_values''': pixel_values}
       return config, inputs_dict






@require_torch
class     lowercase__       (	__lowercase			,				__lowercase			,				unittest.TestCase				):

   '''simple docstring'''


   a						:							Optional[Any]   		=		(
       (
           ViTModel,
           ViTForImageClassification,
           ViTForMaskedImageModeling,
       )
       if is_torch_available()
       else ()
   )
   a						:							Union[str, Any]   		=		(
       {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
       if is_torch_available()
       else {}
   )
   a						:							int   		=		True

   a						:							str   		=		False
   a						:							List[str]   		=		False
   a						:							Optional[int]   		=		False




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






       """simple docstring"""
       UpperCamelCase__							:				List[Any]          =      ViTModelTester(self							)
       UpperCamelCase__							:				Tuple          =      ConfigTester(self,		config_class=UpperCAmelCase__,		has_text_modality=UpperCAmelCase__,		hidden_size=37							)




   def       UpperCamelCase__  (    self							) ->      str:






       """simple docstring"""
       self.config_tester.run_common_tests()




   @unittest.skip(reason='''ViT does not use inputs_embeds'''							)
   def       UpperCamelCase__  (    self							) ->      List[Any]:






       """simple docstring"""
       pass




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






       """simple docstring"""
       UpperCamelCase__   ,UpperCamelCase__							:				Tuple          =      self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           UpperCamelCase__							:				Any          =      model_class(UpperCAmelCase__							)
           self.assertIsInstance(model.get_input_embeddings(),		(nn.Module)							)
           UpperCamelCase__							:				Union[str, Any]          =      model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(UpperCAmelCase__,		nn.Linear							)							)




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






       """simple docstring"""
       UpperCamelCase__   ,UpperCamelCase__							:				Tuple          =      self.model_tester.prepare_config_and_inputs_for_common()

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

           UpperCamelCase__							:				Tuple          =      ['''pixel_values''']
           self.assertListEqual(arg_names[:1],		UpperCAmelCase__							)




   def       UpperCamelCase__  (    self							) ->      Any:






       """simple docstring"""
       UpperCamelCase__							:				List[Any]          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*UpperCAmelCase__							)




   def       UpperCamelCase__  (    self							) ->      Any:






       """simple docstring"""
       UpperCamelCase__							:				Any          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*UpperCAmelCase__							)




   def       UpperCamelCase__  (    self							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				Optional[Any]          =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase__							)




   @slow
   def       UpperCamelCase__  (    self							) ->      Optional[int]:






       """simple docstring"""
       for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__							:				Dict          =      ViTModel.from_pretrained(UpperCAmelCase__							)
           self.assertIsNotNone(UpperCAmelCase__							)




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






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

   '''simple docstring'''


   @cached_property
   def       UpperCamelCase__  (    self							) ->      List[Any]:






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




   @slow
   def       UpperCamelCase__  (    self							) ->      Union[str, Any]:






       """simple docstring"""
       UpperCamelCase__							:				int          =      ViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''							).to(UpperCAmelCase__							)

       UpperCamelCase__							:				str          =      self.default_image_processor
       UpperCamelCase__							:				int          =      prepare_img()
       UpperCamelCase__							:				Optional[int]          =      image_processor(images=UpperCAmelCase__,		return_tensors='''pt'''							).to(UpperCAmelCase__							)

       # forward pass
       with torch.no_grad():
           UpperCamelCase__							:				Union[str, Any]          =      model(**UpperCAmelCase__							)

       # verify the logits
       UpperCamelCase__							:				Dict          =      torch.Size((1, 1000)							)
       self.assertEqual(outputs.logits.shape,		UpperCAmelCase__							)

       UpperCamelCase__							:				List[str]          =      torch.tensor([-0.2744, 0.8215, -0.0836]							).to(UpperCAmelCase__							)

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




   @slow
   def       UpperCamelCase__  (    self							) ->      List[Any]:






       """simple docstring"""
       # ViT models have an `interpolate_pos_encoding` argument in their forward method,
       # allowing to interpolate the pre-trained position embeddings in order to use
       # the model on higher resolutions. The DINO model by Facebook AI leverages this
       # to visualize self-attention on higher resolution images.
       UpperCamelCase__							:				Dict          =      ViTModel.from_pretrained('''facebook/dino-vits8'''							).to(UpperCAmelCase__							)

       UpperCamelCase__							:				Optional[Any]          =      ViTImageProcessor.from_pretrained('''facebook/dino-vits8''',		size=480							)
       UpperCamelCase__							:				Tuple          =      prepare_img()
       UpperCamelCase__							:				Union[str, Any]          =      image_processor(images=UpperCAmelCase__,		return_tensors='''pt'''							)
       UpperCamelCase__							:				int          =      inputs.pixel_values.to(UpperCAmelCase__							)

       # forward pass
       with torch.no_grad():
           UpperCamelCase__							:				Dict          =      model(UpperCAmelCase__,		interpolate_pos_encoding=UpperCAmelCase__							)

       # verify the logits
       UpperCamelCase__							:				Dict          =      torch.Size((1, 3601, 384)							)
       self.assertEqual(outputs.last_hidden_state.shape,		UpperCAmelCase__							)

       UpperCamelCase__							:				str          =      torch.tensor(
           [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]							).to(UpperCAmelCase__							)

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




   @slow
   @require_accelerate
   @require_torch_gpu
   def       UpperCamelCase__  (    self							) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:				Any          =      ViTModel.from_pretrained('''facebook/dino-vits8''',		torch_dtype=torch.floataa,		device_map='''auto'''							)
       UpperCamelCase__							:				Optional[Any]          =      self.default_image_processor

       UpperCamelCase__							:				List[str]          =      prepare_img()
       UpperCamelCase__							:				Optional[int]          =      image_processor(images=UpperCAmelCase__,		return_tensors='''pt'''							)
       UpperCamelCase__							:				Optional[int]          =      inputs.pixel_values.to(UpperCAmelCase__							)

       # forward pass to make sure inference works in fp16
       with torch.no_grad():
           UpperCamelCase__							:				Any          =      model(UpperCAmelCase__							)





'''simple docstring'''




class 		UpperCAmelCase_      :





     def __init__( self       :      List[str]    ,      UpperCAmelCase__       :      list[int]    )				->  None:
        lowerCAmelCase     =			len(UpperCAmelCase__    )
        lowerCAmelCase     =			[0] * len_array

        if len_array > 0:
           lowerCAmelCase     =			array[0]

        for i in range(1    ,      UpperCAmelCase__    ):
           lowerCAmelCase     =			self.prefix_sum[i - 1] + array[i]





     def 							__UpperCAmelCase      ( self       :      Optional[Any]    ,      UpperCAmelCase__       :      int    ,      UpperCAmelCase__       :      int    )				->  int:
        if start == 0:
           return self.prefix_sum[end]

        return self.prefix_sum[end] - self.prefix_sum[start - 1]





     def 							__UpperCAmelCase      ( self       :      int    ,      UpperCAmelCase__       :      int    )				->  bool:
        lowerCAmelCase     =			{0}
        for sum_item in self.prefix_sum:
           if sum_item - target_sum in sums:
              return True

           sums.add(UpperCAmelCase__    )

        return False


if __name__ == "__main__":
       import doctest

       doctest.testmod()





