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






       def __init__(  self :							Optional[int]       ,      UpperCAmelCase_ :							Optional[int]       ,      UpperCAmelCase_ :							int=13       ,      UpperCAmelCase_ :							Dict=30       ,      UpperCAmelCase_ :							Optional[int]=2       ,      UpperCAmelCase_ :							Optional[int]=3       ,      UpperCAmelCase_ :							str=True       ,      UpperCAmelCase_ :							Optional[int]=True       ,      UpperCAmelCase_ :							Tuple=32       ,      UpperCAmelCase_ :							int=5       ,      UpperCAmelCase_ :							Optional[Any]=4       ,      UpperCAmelCase_ :							Optional[Any]=37       ,      UpperCAmelCase_ :							List[str]="gelu"       ,      UpperCAmelCase_ :							str=0.1       ,      UpperCAmelCase_ :							Optional[int]=0.1       ,      UpperCAmelCase_ :							Optional[int]=10       ,      UpperCAmelCase_ :							str=0.02       ,      UpperCAmelCase_ :							List[Any]=None       ,      UpperCAmelCase_ :							Optional[int]=2       ,      )->  str:







              """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
              UpperCamelCase					 =		encoder_stride

              # 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 	_SCREAMING_SNAKE_CASE       (  self :							str			)->  Optional[Any]:







              """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 	_SCREAMING_SNAKE_CASE       (  self :							List[Any]			)->  Dict:







              """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 	_SCREAMING_SNAKE_CASE       (  self :							str       ,      UpperCAmelCase_ :							str       ,      UpperCAmelCase_ :							Dict       ,      UpperCAmelCase_ :							Optional[int]			)->  Optional[int]:







              """simple docstring"""
              UpperCamelCase					 =		ViTModel(config=UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()
              UpperCamelCase					 =		model(UpperCAmelCase_			)
              self.parent.assertEqual(result.last_hidden_state.shape       ,      (self.batch_size, self.seq_length, self.hidden_size)			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[Any]       ,      UpperCAmelCase_ :							Tuple       ,      UpperCAmelCase_ :							Optional[Any]       ,      UpperCAmelCase_ :							Optional[Any]			)->  int:







              """simple docstring"""
              UpperCamelCase					 =		ViTForMaskedImageModeling(config=UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()
              UpperCamelCase					 =		model(UpperCAmelCase_			)
              self.parent.assertEqual(
                  result.reconstruction.shape       ,      (self.batch_size, self.num_channels, self.image_size, self.image_size)			)

              # test greyscale images
              UpperCamelCase					 =		1
              UpperCamelCase					 =		ViTForMaskedImageModeling(UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()

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






       def 	_SCREAMING_SNAKE_CASE       (  self :							Tuple       ,      UpperCAmelCase_ :							Optional[Any]       ,      UpperCAmelCase_ :							List[Any]       ,      UpperCAmelCase_ :							Optional[int]			)->  Tuple:







              """simple docstring"""
              UpperCamelCase					 =		self.type_sequence_label_size
              UpperCamelCase					 =		ViTForImageClassification(UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()
              UpperCamelCase					 =		model(UpperCAmelCase_       ,      labels=UpperCAmelCase_			)
              self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.type_sequence_label_size)			)

              # test greyscale images
              UpperCamelCase					 =		1
              UpperCamelCase					 =		ViTForImageClassification(UpperCAmelCase_			)
              model.to(UpperCAmelCase_			)
              model.eval()

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






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[int]			)->  int:







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





@require_torch
class   __a  (      _lowerCAmelCase    ,     _lowerCAmelCase    ,     unittest.TestCase	):
       UpperCamelCase_		:					str   =			(
           (
               ViTModel,
               ViTForImageClassification,
               ViTForMaskedImageModeling,
           )
           if is_torch_available()
           else ()
       )
       UpperCamelCase_		:					Union[str, Any]   =			(
           {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
           if is_torch_available()
           else {}
       )
       UpperCamelCase_		:					Tuple   =			True

       UpperCamelCase_		:					Tuple   =			False
       UpperCamelCase_		:					Tuple   =			False
       UpperCamelCase_		:					Tuple   =			False






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[int]			)->  int:







              """simple docstring"""
              UpperCamelCase					 =		ViTModelTester(self			)
              UpperCamelCase					 =		ConfigTester(self       ,      config_class=UpperCAmelCase_       ,      has_text_modality=UpperCAmelCase_       ,      hidden_size=37			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							str			)->  Optional[Any]:







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






       @unittest.skip(reason="ViT does not use inputs_embeds"			)
       def 	_SCREAMING_SNAKE_CASE       (  self :							List[str]			)->  Optional[Any]:







              """simple docstring"""
              pass






       def 	_SCREAMING_SNAKE_CASE       (  self :							Any			)->  Any:







              """simple docstring"""
              UpperCamelCase ,       UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs_for_common()

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






       def 	_SCREAMING_SNAKE_CASE       (  self :							Dict			)->  Dict:







              """simple docstring"""
              UpperCamelCase ,       UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs_for_common()

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

                     UpperCamelCase					 =		["pixel_values"]
                     self.assertListEqual(arg_names[:1]       ,      UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							List[str]			)->  int:







              """simple docstring"""
              UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							int			)->  Any:







              """simple docstring"""
              UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_masked_image_modeling(*UpperCAmelCase_			)






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







              """simple docstring"""
              UpperCamelCase					 =		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_			)






       @slow
       def 	_SCREAMING_SNAKE_CASE       (  self :							List[str]			)->  Optional[Any]:







              """simple docstring"""
              for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     UpperCamelCase					 =		ViTModel.from_pretrained(UpperCAmelCase_			)
                     self.assertIsNotNone(UpperCAmelCase_			)





def 	lowerCamelCase__			(							)->							int:







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



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






       @cached_property
       def 	_SCREAMING_SNAKE_CASE       (  self :							Union[str, Any]			)->  List[Any]:







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






       @slow
       def 	_SCREAMING_SNAKE_CASE       (  self :							int			)->  Any:







              """simple docstring"""
              UpperCamelCase					 =		ViTForImageClassification.from_pretrained("google/vit-base-patch16-224"			).to(UpperCAmelCase_			)

              UpperCamelCase					 =		self.default_image_processor
              UpperCamelCase					 =		prepare_img()
              UpperCamelCase					 =		image_processor(images=UpperCAmelCase_       ,      return_tensors="pt"			).to(UpperCAmelCase_			)

              # forward pass
              with torch.no_grad():
                     UpperCamelCase					 =		model(**UpperCAmelCase_			)

              # verify the logits
              UpperCamelCase					 =		torch.Size((1, 1_000)			)
              self.assertEqual(outputs.logits.shape       ,      UpperCAmelCase_			)

              UpperCamelCase					 =		torch.tensor([-0.2744, 0.8215, -0.0836]			).to(UpperCAmelCase_			)

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






       @slow
       def 	_SCREAMING_SNAKE_CASE       (  self :							Any			)->  int:







              """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					 =		ViTModel.from_pretrained("facebook/dino-vits8"			).to(UpperCAmelCase_			)

              UpperCamelCase					 =		ViTImageProcessor.from_pretrained("facebook/dino-vits8"       ,      size=480			)
              UpperCamelCase					 =		prepare_img()
              UpperCamelCase					 =		image_processor(images=UpperCAmelCase_       ,      return_tensors="pt"			)
              UpperCamelCase					 =		inputs.pixel_values.to(UpperCAmelCase_			)

              # forward pass
              with torch.no_grad():
                     UpperCamelCase					 =		model(UpperCAmelCase_       ,      interpolate_pos_encoding=UpperCAmelCase_			)

              # verify the logits
              UpperCamelCase					 =		torch.Size((1, 3_601, 384)			)
              self.assertEqual(outputs.last_hidden_state.shape       ,      UpperCAmelCase_			)

              UpperCamelCase					 =		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 	_SCREAMING_SNAKE_CASE       (  self :							int			)->  Tuple:







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

              UpperCamelCase					 =		prepare_img()
              UpperCamelCase					 =		image_processor(images=UpperCAmelCase_       ,      return_tensors="pt"			)
              UpperCamelCase					 =		inputs.pixel_values.to(UpperCAmelCase_			)

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







"""simple docstring"""


import torch

from diffusers import EulerDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest



class   __a  (      _lowerCAmelCase	):
       UpperCamelCase_		:					Any   =			(EulerDiscreteScheduler,)
       UpperCamelCase_		:					Dict   =			10






       def 	_SCREAMING_SNAKE_CASE       (  self :							Any       ,      **UpperCAmelCase_ :							str			)->  Union[str, Any]:







              """simple docstring"""
              UpperCamelCase					 =		{
                  "num_train_timesteps": 1_100,
                  "beta_start": 0.0001,
                  "beta_end": 0.02,
                  "beta_schedule": "linear",
              }

              config.update(**UpperCAmelCase_			)
              return config






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







              """simple docstring"""
              for timesteps in [10, 50, 100, 1_000]:
                     self.check_over_configs(num_train_timesteps=UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							int			)->  Any:







              """simple docstring"""
              for beta_start, beta_end in zip([0.00001, 0.0001, 0.001]       ,      [0.0002, 0.002, 0.02]			):
                     self.check_over_configs(beta_start=UpperCAmelCase_       ,      beta_end=UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							str			)->  Dict:







              """simple docstring"""
              for schedule in ["linear", "scaled_linear"]:
                     self.check_over_configs(beta_schedule=UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							List[Any]			)->  int:







              """simple docstring"""
              for prediction_type in ["epsilon", "v_prediction"]:
                     self.check_over_configs(prediction_type=UpperCAmelCase_			)






       def 	_SCREAMING_SNAKE_CASE       (  self :							str			)->  Any:







              """simple docstring"""
              UpperCamelCase					 =		self.scheduler_classes[0]
              UpperCamelCase					 =		self.get_scheduler_config()
              UpperCamelCase					 =		scheduler_class(**UpperCAmelCase_			)

              scheduler.set_timesteps(self.num_inference_steps			)

              UpperCamelCase					 =		torch.manual_seed(0			)

              UpperCamelCase					 =		self.dummy_model()
              UpperCamelCase					 =		self.dummy_sample_deter * scheduler.init_noise_sigma
              UpperCamelCase					 =		sample.to(UpperCAmelCase_			)

              for i, t in enumerate(scheduler.timesteps			):
                     UpperCamelCase					 =		scheduler.scale_model_input(UpperCAmelCase_       ,      UpperCAmelCase_			)

                     UpperCamelCase					 =		model(UpperCAmelCase_       ,      UpperCAmelCase_			)

                     UpperCamelCase					 =		scheduler.step(UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      generator=UpperCAmelCase_			)
                     UpperCamelCase					 =		output.prev_sample

              UpperCamelCase					 =		torch.sum(torch.abs(UpperCAmelCase_			)			)
              UpperCamelCase					 =		torch.mean(torch.abs(UpperCAmelCase_			)			)

              assert abs(result_sum.item() - 10.0807			) < 1e-2
              assert abs(result_mean.item() - 0.0131			) < 1e-3






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







              """simple docstring"""
              UpperCamelCase					 =		self.scheduler_classes[0]
              UpperCamelCase					 =		self.get_scheduler_config(prediction_type="v_prediction"			)
              UpperCamelCase					 =		scheduler_class(**UpperCAmelCase_			)

              scheduler.set_timesteps(self.num_inference_steps			)

              UpperCamelCase					 =		torch.manual_seed(0			)

              UpperCamelCase					 =		self.dummy_model()
              UpperCamelCase					 =		self.dummy_sample_deter * scheduler.init_noise_sigma
              UpperCamelCase					 =		sample.to(UpperCAmelCase_			)

              for i, t in enumerate(scheduler.timesteps			):
                     UpperCamelCase					 =		scheduler.scale_model_input(UpperCAmelCase_       ,      UpperCAmelCase_			)

                     UpperCamelCase					 =		model(UpperCAmelCase_       ,      UpperCAmelCase_			)

                     UpperCamelCase					 =		scheduler.step(UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      generator=UpperCAmelCase_			)
                     UpperCamelCase					 =		output.prev_sample

              UpperCamelCase					 =		torch.sum(torch.abs(UpperCAmelCase_			)			)
              UpperCamelCase					 =		torch.mean(torch.abs(UpperCAmelCase_			)			)

              assert abs(result_sum.item() - 0.0002			) < 1e-2
              assert abs(result_mean.item() - 2.2_676e-06			) < 1e-3






       def 	_SCREAMING_SNAKE_CASE       (  self :							List[str]			)->  Tuple:







              """simple docstring"""
              UpperCamelCase					 =		self.scheduler_classes[0]
              UpperCamelCase					 =		self.get_scheduler_config()
              UpperCamelCase					 =		scheduler_class(**UpperCAmelCase_			)

              scheduler.set_timesteps(self.num_inference_steps       ,      device=UpperCAmelCase_			)

              UpperCamelCase					 =		torch.manual_seed(0			)

              UpperCamelCase					 =		self.dummy_model()
              UpperCamelCase					 =		self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
              UpperCamelCase					 =		sample.to(UpperCAmelCase_			)

              for t in scheduler.timesteps:
                     UpperCamelCase					 =		scheduler.scale_model_input(UpperCAmelCase_       ,      UpperCAmelCase_			)

                     UpperCamelCase					 =		model(UpperCAmelCase_       ,      UpperCAmelCase_			)

                     UpperCamelCase					 =		scheduler.step(UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      generator=UpperCAmelCase_			)
                     UpperCamelCase					 =		output.prev_sample

              UpperCamelCase					 =		torch.sum(torch.abs(UpperCAmelCase_			)			)
              UpperCamelCase					 =		torch.mean(torch.abs(UpperCAmelCase_			)			)

              assert abs(result_sum.item() - 10.0807			) < 1e-2
              assert abs(result_mean.item() - 0.0131			) < 1e-3






       def 	_SCREAMING_SNAKE_CASE       (  self :							Optional[int]			)->  Any:







              """simple docstring"""
              UpperCamelCase					 =		self.scheduler_classes[0]
              UpperCamelCase					 =		self.get_scheduler_config()
              UpperCamelCase					 =		scheduler_class(**UpperCAmelCase_       ,      use_karras_sigmas=UpperCAmelCase_			)

              scheduler.set_timesteps(self.num_inference_steps       ,      device=UpperCAmelCase_			)

              UpperCamelCase					 =		torch.manual_seed(0			)

              UpperCamelCase					 =		self.dummy_model()
              UpperCamelCase					 =		self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
              UpperCamelCase					 =		sample.to(UpperCAmelCase_			)

              for t in scheduler.timesteps:
                     UpperCamelCase					 =		scheduler.scale_model_input(UpperCAmelCase_       ,      UpperCAmelCase_			)

                     UpperCamelCase					 =		model(UpperCAmelCase_       ,      UpperCAmelCase_			)

                     UpperCamelCase					 =		scheduler.step(UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      generator=UpperCAmelCase_			)
                     UpperCamelCase					 =		output.prev_sample

              UpperCamelCase					 =		torch.sum(torch.abs(UpperCAmelCase_			)			)
              UpperCamelCase					 =		torch.mean(torch.abs(UpperCAmelCase_			)			)

              assert abs(result_sum.item() - 124.52299499511719			) < 1e-2
              assert abs(result_mean.item() - 0.16213932633399963			) < 1e-3





