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







  '''simple docstring'''






  def __init__(			self				,		snake_case_				,		snake_case_=1_3				,		snake_case_=3_0				,		snake_case_=2				,		snake_case_=3				,		snake_case_=True				,		snake_case_=True				,		snake_case_=3_2				,		snake_case_=5				,		snake_case_=4				,		snake_case_=3_7				,		snake_case_="gelu"				,		snake_case_=0.1				,		snake_case_=0.1				,		snake_case_=1_0				,		snake_case_=0.02				,		snake_case_=None				,		snake_case_=2				,		):




       '''simple docstring'''

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

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


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Optional[Any]       =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

       UpperCAmelCase_							:       Optional[int]       =				self.get_config()

       return config, pixel_values, labels


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


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Any       =				ViTModel(config=snake_case_						)
       model.to(snake_case_						)
       model.eval()
       UpperCAmelCase_							:       str       =				model(snake_case_						)
       self.parent.assertEqual(result.last_hidden_state.shape				,		(self.batch_size, self.seq_length, self.hidden_size)						)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[str]       =				ViTForMaskedImageModeling(config=snake_case_						)
       model.to(snake_case_						)
       model.eval()
       UpperCAmelCase_							:       Optional[int]       =				model(snake_case_						)
       self.parent.assertEqual(
           result.reconstruction.shape				,		(self.batch_size, self.num_channels, self.image_size, self.image_size)						)

       # test greyscale images
       UpperCAmelCase_							:       Any       =				1
       UpperCAmelCase_							:       Any       =				ViTForMaskedImageModeling(snake_case_						)
       model.to(snake_case_						)
       model.eval()

       UpperCAmelCase_							:       Tuple       =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
       UpperCAmelCase_							:       str       =				model(snake_case_						)
       self.parent.assertEqual(result.reconstruction.shape				,		(self.batch_size, 1, self.image_size, self.image_size)						)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[Any]       =				self.type_sequence_label_size
       UpperCAmelCase_							:       Any       =				ViTForImageClassification(snake_case_						)
       model.to(snake_case_						)
       model.eval()
       UpperCAmelCase_							:       Any       =				model(snake_case_				,		labels=snake_case_						)
       self.parent.assertEqual(result.logits.shape				,		(self.batch_size, self.type_sequence_label_size)						)

       # test greyscale images
       UpperCAmelCase_							:       Union[str, Any]       =				1
       UpperCAmelCase_							:       Tuple       =				ViTForImageClassification(snake_case_						)
       model.to(snake_case_						)
       model.eval()

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






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

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







@require_torch
class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_						,     lowerCamelCase_						,     unittest.TestCase	):







  '''simple docstring'''






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

  lowerCamelCase_		:List[str]						   =		False
  lowerCamelCase_		:Optional[int]						   =		False
  lowerCamelCase_		:Tuple						   =		False


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Any       =				ViTModelTester(self						)
       UpperCAmelCase_							:       Any       =				ConfigTester(self				,		config_class=snake_case_				,		has_text_modality=snake_case_				,		hidden_size=3_7						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       self.config_tester.run_common_tests()


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




       '''simple docstring'''

       pass


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_       ,      UpperCAmelCase_							:       List[Any]       =				self.model_tester.prepare_config_and_inputs_for_common()

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


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

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

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

            UpperCAmelCase_							:       List[Any]       =				['pixel_values']
            self.assertListEqual(arg_names[:1]				,		snake_case_						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[str]       =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*snake_case_						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*snake_case_						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Optional[Any]       =				self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*snake_case_						)






  @slow
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

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







def  _lowerCamelCase   (				):



     """simple docstring"""





     UpperCAmelCase_							:       Any       =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
     return image






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







  '''simple docstring'''






  @cached_property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

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


  @slow
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       str       =				ViTForImageClassification.from_pretrained('google/vit-base-patch16-224'						).to(snake_case_						)

       UpperCAmelCase_							:       List[Any]       =				self.default_image_processor
       UpperCAmelCase_							:       Tuple       =				prepare_img()
       UpperCAmelCase_							:       Optional[Any]       =				image_processor(images=snake_case_				,		return_tensors='pt'						).to(snake_case_						)

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

       # verify the logits
       UpperCAmelCase_							:       Any       =				torch.Size((1, 1_0_0_0)						)
       self.assertEqual(outputs.logits.shape				,		snake_case_						)

       UpperCAmelCase_							:       Optional[int]       =				torch.tensor([-0.27_44, 0.82_15, -0.08_36]						).to(snake_case_						)

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


  @slow
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Any       =				ViTModel.from_pretrained('facebook/dino-vits8'						).to(snake_case_						)

       UpperCAmelCase_							:       Tuple       =				ViTImageProcessor.from_pretrained('facebook/dino-vits8'				,		size=4_8_0						)
       UpperCAmelCase_							:       Any       =				prepare_img()
       UpperCAmelCase_							:       int       =				image_processor(images=snake_case_				,		return_tensors='pt'						)
       UpperCAmelCase_							:       List[Any]       =				inputs.pixel_values.to(snake_case_						)

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

       # verify the logits
       UpperCAmelCase_							:       int       =				torch.Size((1, 3_6_0_1, 3_8_4)						)
       self.assertEqual(outputs.last_hidden_state.shape				,		snake_case_						)

       UpperCAmelCase_							:       Any       =				torch.tensor(
           [[4.23_40, 4.39_06, -6.66_92], [4.54_63, 1.89_28, -6.72_57], [4.44_29, 0.84_96, -5.85_85]]						).to(snake_case_						)

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






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




       '''simple docstring'''

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

       UpperCAmelCase_							:       Tuple       =				prepare_img()
       UpperCAmelCase_							:       Optional[Any]       =				image_processor(images=snake_case_				,		return_tensors='pt'						)
       UpperCAmelCase_							:       Tuple       =				inputs.pixel_values.to(snake_case_						)

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







'''simple docstring'''


import math
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from .scheduling_utils import SchedulerMixin, SchedulerOutput






class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_						,     lowerCamelCase_	):







  '''simple docstring'''






  lowerCamelCase_		:Union[str, Any]						   =		1


  @register_to_config
  def __init__(			self				,		snake_case_ = 1_0_0_0				,		snake_case_ = None						):




       '''simple docstring'''

       self.set_timesteps(snake_case_						)

       # standard deviation of the initial noise distribution
       UpperCAmelCase_							:       Union[str, Any]       =				1.0

       # For now we only support F-PNDM, i.e. the runge-kutta method
       # For more information on the algorithm please take a look at the paper: https://arxiv.org/pdf/2202.09778.pdf
       # mainly at formula (9), (12), (13) and the Algorithm 2.
       UpperCAmelCase_							:       int       =				4

       # running values
       UpperCAmelCase_							:       str       =				[]


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_ = None						):




       '''simple docstring'''

       UpperCAmelCase_							:       Union[str, Any]       =				num_inference_steps
       UpperCAmelCase_							:       int       =				torch.linspace(1				,		0				,		num_inference_steps + 1						)[:-1]
       UpperCAmelCase_							:       Tuple       =				torch.cat([steps, torch.tensor([0.0]						)]						)

       if self.config.trained_betas is not None:
            UpperCAmelCase_							:       Optional[int]       =				torch.tensor(self.config.trained_betas				,		dtype=torch.floataa						)
       else:
            UpperCAmelCase_							:       Tuple       =				torch.sin(steps * math.pi / 2						) ** 2

       UpperCAmelCase_							:       Dict       =				(1.0 - self.betas**2) ** 0.5

       UpperCAmelCase_							:       str       =				(torch.atana(self.betas				,		self.alphas						) / math.pi * 2)[:-1]
       UpperCAmelCase_							:       str       =				timesteps.to(snake_case_						)

       UpperCAmelCase_							:       Any       =				[]


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_ = True				,		):




       '''simple docstring'''

       if self.num_inference_steps is None:
            raise ValueError(
                'Number of inference steps is \'None\', you need to run \'set_timesteps\' after creating the scheduler'						)

       UpperCAmelCase_							:       Any       =				(self.timesteps == timestep).nonzero().item()
       UpperCAmelCase_							:       Optional[Any]       =				timestep_index + 1

       UpperCAmelCase_							:       Dict       =				sample * self.betas[timestep_index] + model_output * self.alphas[timestep_index]
       self.ets.append(snake_case_						)

       if len(self.ets						) == 1:
            UpperCAmelCase_							:       Tuple       =				self.ets[-1]
       elif len(self.ets						) == 2:
            UpperCAmelCase_							:       Any       =				(3 * self.ets[-1] - self.ets[-2]) / 2
       elif len(self.ets						) == 3:
            UpperCAmelCase_							:       List[str]       =				(2_3 * self.ets[-1] - 1_6 * self.ets[-2] + 5 * self.ets[-3]) / 1_2
       else:
            UpperCAmelCase_							:       Union[str, Any]       =				(1 / 2_4) * (5_5 * self.ets[-1] - 5_9 * self.ets[-2] + 3_7 * self.ets[-3] - 9 * self.ets[-4])

       UpperCAmelCase_							:       Union[str, Any]       =				self._get_prev_sample(snake_case_				,		snake_case_				,		snake_case_				,		snake_case_						)

       if not return_dict:
            return (prev_sample,)

       return SchedulerOutput(prev_sample=snake_case_						)


  def _UpperCamelCase					(			self				,		snake_case_				,		*snake_case_				,		**snake_case_						):




       '''simple docstring'''

       return sample


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				self.alphas[timestep_index]
       UpperCAmelCase_							:       Union[str, Any]       =				self.betas[timestep_index]

       UpperCAmelCase_							:       Any       =				self.alphas[prev_timestep_index]
       UpperCAmelCase_							:       Dict       =				self.betas[prev_timestep_index]

       UpperCAmelCase_							:       List[Any]       =				(sample - sigma * ets) / max(snake_case_				,		1E-8						)
       UpperCAmelCase_							:       Tuple       =				next_alpha * pred + ets * next_sigma

       return prev_sample






  def __len__(			self						):




       '''simple docstring'''

       return self.config.num_train_timesteps






