
"""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  _SCREAMING_SNAKE_CASE:




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





             """simple docstring"""






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

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




       def    _UpperCamelCase					(						self							)      ->				str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

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




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





             """simple docstring"""






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




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				List[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  ViTForMaskedImageModeling(config=SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :Any		    =  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
             __SCREAMING_SNAKE_CASE       :Any		    =  1
             __SCREAMING_SNAKE_CASE       :str		    =  ViTForMaskedImageModeling(SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()

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




       def    _UpperCamelCase					(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__							)      ->				int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[str]		    =  self.type_sequence_label_size
             __SCREAMING_SNAKE_CASE       :str		    =  ViTForImageClassification(SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  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
             __SCREAMING_SNAKE_CASE       :int		    =  1
             __SCREAMING_SNAKE_CASE       :Any		    =  ViTForImageClassification(SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()

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




       def    _UpperCamelCase					(						self							)      ->				int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Any		    =  self.prepare_config_and_inputs()
             (
                 __SCREAMING_SNAKE_CASE
             )       :Dict		    =  config_and_inputs
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  {'''pixel_values''': pixel_values}
             return config, inputs_dict






@require_torch
class  _SCREAMING_SNAKE_CASE(       A   ,     A   ,     unittest.TestCase  ):
       SCREAMING_SNAKE_CASE_       :				int     						=  (
           (
               ViTModel,
               ViTForImageClassification,
               ViTForMaskedImageModeling,
           )
           if is_torch_available()
           else ()
       )
       SCREAMING_SNAKE_CASE_       :				Union[str, Any]     						=  (
           {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
           if is_torch_available()
           else {}
       )
       SCREAMING_SNAKE_CASE_       :				List[str]     						=  True

       SCREAMING_SNAKE_CASE_       :				Dict     						=  False
       SCREAMING_SNAKE_CASE_       :				str     						=  False
       SCREAMING_SNAKE_CASE_       :				Dict     						=  False




       def    _UpperCamelCase					(						self							)      ->				Any:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  ViTModelTester(self							)
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  ConfigTester(self		,config_class=SCREAMING_SNAKE_CASE__		,has_text_modality=SCREAMING_SNAKE_CASE__		,hidden_size=37							)




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





             """simple docstring"""






             self.config_tester.run_common_tests()




       @unittest.skip(reason='''ViT does not use inputs_embeds'''							)
       def    _UpperCamelCase					(						self							)      ->				str:





             """simple docstring"""






             pass




       def    _UpperCamelCase					(						self							)      ->				Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :int		    =  self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                   __SCREAMING_SNAKE_CASE       :List[str]		    =  model_class(SCREAMING_SNAKE_CASE__							)
                   self.assertIsInstance(model.get_input_embeddings()		,(nn.Module)							)
                   __SCREAMING_SNAKE_CASE       :Dict		    =  model.get_output_embeddings()
                   self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE__		,nn.Linear							)							)




       def    _UpperCamelCase					(						self							)      ->				int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :str		    =  self.model_tester.prepare_config_and_inputs_for_common()

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

                   __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  ['''pixel_values''']
                   self.assertListEqual(arg_names[:1]		,SCREAMING_SNAKE_CASE__							)




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_image_modeling(*SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[str]		    =  self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE__							)




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





             """simple docstring"""






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




def 					__lowerCamelCase       (							)      ->				Dict:
      __SCREAMING_SNAKE_CASE       :Any		    =  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
      return image







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




       @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							)      ->				List[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[str]		    =  ViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''							).to(SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE       :List[Any]		    =  self.default_image_processor
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  prepare_img()
             __SCREAMING_SNAKE_CASE       :str		    =  image_processor(images=SCREAMING_SNAKE_CASE__		,return_tensors='''pt'''							).to(SCREAMING_SNAKE_CASE__							)

             # forward pass
             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  model(**SCREAMING_SNAKE_CASE__							)

             # verify the logits
             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  torch.Size((1, 10_00)							)
             self.assertEqual(outputs.logits.shape		,SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE       :List[str]		    =  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    _UpperCamelCase					(						self							)      ->				Union[str, Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Tuple		    =  ViTModel.from_pretrained('''facebook/dino-vits8'''							).to(SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  ViTImageProcessor.from_pretrained('''facebook/dino-vits8'''		,size=4_80							)
             __SCREAMING_SNAKE_CASE       :Dict		    =  prepare_img()
             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  image_processor(images=SCREAMING_SNAKE_CASE__		,return_tensors='''pt'''							)
             __SCREAMING_SNAKE_CASE       :int		    =  inputs.pixel_values.to(SCREAMING_SNAKE_CASE__							)

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

             # verify the logits
             __SCREAMING_SNAKE_CASE       :Tuple		    =  torch.Size((1, 36_01, 3_84)							)
             self.assertEqual(outputs.last_hidden_state.shape		,SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE       :int		    =  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    _UpperCamelCase					(						self							)      ->				str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :int		    =  ViTModel.from_pretrained('''facebook/dino-vits8'''		,torch_dtype=torch.floataa		,device_map='''auto'''							)
             __SCREAMING_SNAKE_CASE       :Tuple		    =  self.default_image_processor

             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  prepare_img()
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  image_processor(images=SCREAMING_SNAKE_CASE__		,return_tensors='''pt'''							)
             __SCREAMING_SNAKE_CASE       :Any		    =  inputs.pixel_values.to(SCREAMING_SNAKE_CASE__							)

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






"""simple docstring"""
def 					__lowerCamelCase       (							a_					:		int			,       a_					:		int							)      ->				int:
      return int((input_a, input_a).count(0							) == 0							)



def 					__lowerCamelCase       (							)      ->				None:
      assert and_gate(0			,       0							) == 0
      assert and_gate(0			,       1							) == 0
      assert and_gate(1			,       0							) == 0
      assert and_gate(1			,       1							) == 1


if __name__ == "__main__":
  test_and_gate()
  print(and_gate(1, 0))
  print(and_gate(0, 0))
  print(and_gate(0, 1))
  print(and_gate(1, 1))