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

     """simple docstring"""

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

       '''simple docstring'''

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

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


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Dict      =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

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

       return config, pixel_values, labels


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


     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

       a_      :	Union[str, Any]      =							ViTModel(config=lowerCAmelCase_ )
       model.to(lowerCAmelCase_ )
       model.eval()
       a_      :	Any      =							model(lowerCAmelCase_ )
       self.parent.assertEqual(result.last_hidden_state.shape							,		(self.batch_size, self.seq_length, self.hidden_size) )


     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

       a_      :	str      =							ViTForMaskedImageModeling(config=lowerCAmelCase_ )
       model.to(lowerCAmelCase_ )
       model.eval()
       a_      :	List[Any]      =							model(lowerCAmelCase_ )
       self.parent.assertEqual(
           result.reconstruction.shape							,		(self.batch_size, self.num_channels, self.image_size, self.image_size) )

       # test greyscale images
       a_      :	Optional[Any]      =							1
       a_      :	Union[str, Any]      =							ViTForMaskedImageModeling(lowerCAmelCase_ )
       model.to(lowerCAmelCase_ )
       model.eval()

       a_      :	Union[str, Any]      =							floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
       a_      :	List[str]      =							model(lowerCAmelCase_ )
       self.parent.assertEqual(result.reconstruction.shape							,		(self.batch_size, 1, self.image_size, self.image_size) )


     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_ ):

       '''simple docstring'''

       a_      :	Optional[Any]      =							self.type_sequence_label_size
       a_      :	Union[str, Any]      =							ViTForImageClassification(lowerCAmelCase_ )
       model.to(lowerCAmelCase_ )
       model.eval()
       a_      :	str      =							model(lowerCAmelCase_							,		labels=lowerCAmelCase_ )
       self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.type_sequence_label_size) )

       # test greyscale images
       a_      :	Optional[int]      =							1
       a_      :	Tuple      =							ViTForImageClassification(lowerCAmelCase_ )
       model.to(lowerCAmelCase_ )
       model.eval()

       a_      :	Tuple      =							floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
       a_      :	Tuple      =							model(lowerCAmelCase_ )
       self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.type_sequence_label_size) )






     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Optional[Any]      =							self.prepare_config_and_inputs()
       (
           (
           a_
       )      , (
           a_
       )      , (
           a_
       )      , 
       )      :	Union[str, Any]      =							config_and_inputs
       a_      :	Tuple      =							{"""pixel_values""": pixel_values}
       return config, inputs_dict







@require_torch
class  _UpperCAmelCase ( lowerCAmelCase__	,lowerCAmelCase__	,unittest.TestCase    ):

     """simple docstring"""

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

     a_    	=					False
     a_    	=					False
     a_    	=					False


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Any      =							ViTModelTester(self )
       a_      :	Dict      =							ConfigTester(self							,		config_class=lowerCAmelCase_							,		has_text_modality=lowerCAmelCase_							,		hidden_size=37 )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       self.config_tester.run_common_tests()


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

       '''simple docstring'''

       pass


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      , a_      :	List[str]      =							self.model_tester.prepare_config_and_inputs_for_common()

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


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      , a_      :	Any      =							self.model_tester.prepare_config_and_inputs_for_common()

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

         a_      :	Dict      =							["""pixel_values"""]
         self.assertListEqual(arg_names[:1]							,		lowerCAmelCase_ )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Union[str, Any]      =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*lowerCAmelCase_ )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	List[Any]      =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase_ )


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	str      =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_ )






     @slow
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         a_      :	List[str]      =							ViTModel.from_pretrained(lowerCAmelCase_ )
         self.assertIsNotNone(lowerCAmelCase_ )







def 	_snake_case						(      ):
  """simple docstring"""







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




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

     """simple docstring"""

     @cached_property
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

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


     @slow
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	List[Any]      =							ViTForImageClassification.from_pretrained("""google/vit-base-patch16-224""" ).to(lowerCAmelCase_ )

       a_      :	Union[str, Any]      =							self.default_image_processor
       a_      :	List[Any]      =							prepare_img()
       a_      :	Union[str, Any]      =							image_processor(images=lowerCAmelCase_							,		return_tensors="""pt""" ).to(lowerCAmelCase_ )

       # forward pass
       with torch.no_grad():
         a_      :	List[str]      =							model(**lowerCAmelCase_ )

       # verify the logits
       a_      :	List[Any]      =							torch.Size((1, 10_00) )
       self.assertEqual(outputs.logits.shape							,		lowerCAmelCase_ )

       a_      :	int      =							torch.tensor([-0.2744, 0.8215, -0.0836] ).to(lowerCAmelCase_ )

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


     @slow
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	List[str]      =							ViTModel.from_pretrained("""facebook/dino-vits8""" ).to(lowerCAmelCase_ )

       a_      :	int      =							ViTImageProcessor.from_pretrained("""facebook/dino-vits8"""							,		size=4_80 )
       a_      :	int      =							prepare_img()
       a_      :	Tuple      =							image_processor(images=lowerCAmelCase_							,		return_tensors="""pt""" )
       a_      :	Union[str, Any]      =							inputs.pixel_values.to(lowerCAmelCase_ )

       # forward pass
       with torch.no_grad():
         a_      :	int      =							model(lowerCAmelCase_							,		interpolate_pos_encoding=lowerCAmelCase_ )

       # verify the logits
       a_      :	Optional[int]      =							torch.Size((1, 36_01, 3_84) )
       self.assertEqual(outputs.last_hidden_state.shape							,		lowerCAmelCase_ )

       a_      :	Any      =							torch.tensor(
           [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]] ).to(lowerCAmelCase_ )

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






     @slow
     @require_accelerate
     @require_torch_gpu
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

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

       a_      :	Union[str, Any]      =							prepare_img()
       a_      :	Optional[int]      =							image_processor(images=lowerCAmelCase_							,		return_tensors="""pt""" )
       a_      :	List[Any]      =							inputs.pixel_values.to(lowerCAmelCase_ )

       # forward pass to make sure inference works in fp16
       with torch.no_grad():
         a_      :	str      =							model(lowerCAmelCase_ )




'''simple docstring'''

from dataclasses import dataclass
from enum import Enum
from typing import List, Optional, Union

import numpy as np
import PIL
from PIL import Image

from ...utils import BaseOutput, is_torch_available, is_transformers_available


@dataclass
class  _UpperCAmelCase ( lowerCAmelCase__    ):

     """simple docstring"""

     a_    	=					42
     a_    	=					42


if is_transformers_available() and is_torch_available():
    from .pipeline_semantic_stable_diffusion import SemanticStableDiffusionPipeline


