



"""simple docstring"""






import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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 transformers import CvtForImageClassification, CvtModel
   from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import AutoImageProcessor






class        __snake_case       (	_a):




  def 						SCREAMING_SNAKE_CASE				( self     :						Any		):






         """simple docstring"""






         _lowerCamelCase						:				int    			= self.config_class(**self.inputs_dict		)
         self.parent.assertTrue(hasattr(UpperCamelCase__						,					'''embed_dim'''		)		)
         self.parent.assertTrue(hasattr(UpperCamelCase__						,					'''num_heads'''		)		)






class        __snake_case       :




  def __init__( self     :						Optional[int]						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						Optional[int]=1_3						,					__lowerCAmelCase     :						Union[str, Any]=6_4						,					__lowerCAmelCase     :						str=3						,					__lowerCAmelCase     :						Dict=[1_6, 4_8, 9_6]						,					__lowerCAmelCase     :						Any=[1, 3, 6]						,					__lowerCAmelCase     :						List[str]=[1, 2, 1_0]						,					__lowerCAmelCase     :						str=[7, 3, 3]						,					__lowerCAmelCase     :						int=[4, 2, 2]						,					__lowerCAmelCase     :						Optional[Any]=[2, 1, 1]						,					__lowerCAmelCase     :						Union[str, Any]=[2, 2, 2]						,					__lowerCAmelCase     :						List[str]=[False, False, True]						,					__lowerCAmelCase     :						Dict=[0.0, 0.0, 0.0]						,					__lowerCAmelCase     :						int=0.02						,					__lowerCAmelCase     :						Dict=1E-12						,					__lowerCAmelCase     :						str=True						,					__lowerCAmelCase     :						Dict=True						,					__lowerCAmelCase     :						int=2						,					):






         """simple docstring"""






         _lowerCamelCase						:				int    			= parent
         _lowerCamelCase						:				Optional[int]    			= batch_size
         _lowerCamelCase						:				List[Any]    			= image_size
         _lowerCamelCase						:				Optional[int]    			= patch_sizes
         _lowerCamelCase						:				Tuple    			= patch_stride
         _lowerCamelCase						:				Any    			= patch_padding
         _lowerCamelCase						:				str    			= is_training
         _lowerCamelCase						:				Dict    			= use_labels
         _lowerCamelCase						:				Dict    			= num_labels
         _lowerCamelCase						:				Optional[int]    			= num_channels
         _lowerCamelCase						:				List[str]    			= embed_dim
         _lowerCamelCase						:				int    			= num_heads
         _lowerCamelCase						:				int    			= stride_kv
         _lowerCamelCase						:				Union[str, Any]    			= depth
         _lowerCamelCase						:				int    			= cls_token
         _lowerCamelCase						:				List[Any]    			= attention_drop_rate
         _lowerCamelCase						:				List[Any]    			= initializer_range
         _lowerCamelCase						:				str    			= layer_norm_eps




  def 						SCREAMING_SNAKE_CASE				( self     :						int		):






         """simple docstring"""






         _lowerCamelCase						:				str    			= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

         _lowerCamelCase						:				Union[str, Any]    			= None
         if self.use_labels:
                _lowerCamelCase						:				str    			= ids_tensor([self.batch_size]						,					self.num_labels		)

         _lowerCamelCase						:				List[str]    			= self.get_config()
         return config, pixel_values, labels




  def 						SCREAMING_SNAKE_CASE				( self     :						str		):






         """simple docstring"""






         return CvtConfig(
             image_size=self.image_size						,					num_labels=self.num_labels						,					num_channels=self.num_channels						,					embed_dim=self.embed_dim						,					num_heads=self.num_heads						,					patch_sizes=self.patch_sizes						,					patch_padding=self.patch_padding						,					patch_stride=self.patch_stride						,					stride_kv=self.stride_kv						,					depth=self.depth						,					cls_token=self.cls_token						,					attention_drop_rate=self.attention_drop_rate						,					initializer_range=self.initializer_range						,					)




  def 						SCREAMING_SNAKE_CASE				( self     :						int						,					__lowerCAmelCase     :						Any						,					__lowerCAmelCase     :						str						,					__lowerCAmelCase     :						int		):






         """simple docstring"""






         _lowerCamelCase						:				List[Any]    			= CvtModel(config=UpperCamelCase__		)
         model.to(UpperCamelCase__		)
         model.eval()
         _lowerCamelCase						:				Any    			= model(UpperCamelCase__		)
         _lowerCamelCase						:				Union[str, Any]    			= (self.image_size, self.image_size)
         _lowerCamelCase			,    _lowerCamelCase						:				List[str]    			= image_size[0], image_size[1]
         for i in range(len(self.depth		)		):
                _lowerCamelCase						:				int    			= floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1		)
                _lowerCamelCase						:				str    			= floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1		)
         self.parent.assertEqual(result.last_hidden_state.shape						,					(self.batch_size, self.embed_dim[-1], height, width)		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[int]						,					__lowerCAmelCase     :						Optional[Any]						,					__lowerCAmelCase     :						Union[str, Any]						,					__lowerCAmelCase     :						Optional[Any]		):






         """simple docstring"""






         _lowerCamelCase						:				List[str]    			= self.num_labels
         _lowerCamelCase						:				Union[str, Any]    			= CvtForImageClassification(UpperCamelCase__		)
         model.to(UpperCamelCase__		)
         model.eval()
         _lowerCamelCase						:				Dict    			= model(UpperCamelCase__						,					labels=UpperCamelCase__		)
         self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.num_labels)		)




  def 						SCREAMING_SNAKE_CASE				( self     :						List[Any]		):






         """simple docstring"""






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






@require_torch
class        __snake_case       (	_a  ,     _a  ,     unittest.TestCase):
  snake_case__				:   Tuple  			=						(CvtModel, CvtForImageClassification) if is_torch_available() else ()
  snake_case__				:   List[str]  			=						(
      {"feature-extraction": CvtModel, "image-classification": CvtForImageClassification}
      if is_torch_available()
      else {}
  )

  snake_case__				:   Tuple  			=						False
  snake_case__				:   Union[str, Any]  			=						False
  snake_case__				:   str  			=						False
  snake_case__				:   str  			=						False
  snake_case__				:   Optional[int]  			=						False




  def 						SCREAMING_SNAKE_CASE				( self     :						str		):






         """simple docstring"""






         _lowerCamelCase						:				Dict    			= CvtModelTester(self		)
         _lowerCamelCase						:				int    			= ConfigTester(self						,					config_class=UpperCamelCase__						,					has_text_modality=UpperCamelCase__						,					hidden_size=3_7		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Union[str, Any]		):






         """simple docstring"""






         self.create_and_test_config_common_properties()
         self.config_tester.create_and_test_config_to_json_string()
         self.config_tester.create_and_test_config_to_json_file()
         self.config_tester.create_and_test_config_from_and_save_pretrained()
         self.config_tester.create_and_test_config_with_num_labels()
         self.config_tester.check_config_can_be_init_without_params()
         self.config_tester.check_config_arguments_init()




  def 						SCREAMING_SNAKE_CASE				( self     :						Any		):






         """simple docstring"""






         return




  @unittest.skip(reason='''Cvt does not output attentions'''		)
  def 						SCREAMING_SNAKE_CASE				( self     :						List[Any]		):






         """simple docstring"""






         pass




  @unittest.skip(reason='''Cvt does not use inputs_embeds'''		)
  def 						SCREAMING_SNAKE_CASE				( self     :						Union[str, Any]		):






         """simple docstring"""






         pass




  @unittest.skip(reason='''Cvt does not support input and output embeddings'''		)
  def 						SCREAMING_SNAKE_CASE				( self     :						List[str]		):






         """simple docstring"""






         pass




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[Any]		):






         """simple docstring"""






         _lowerCamelCase			,    _lowerCamelCase						:				Optional[Any]    			= self.model_tester.prepare_config_and_inputs_for_common()

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

                _lowerCamelCase						:				Optional[int]    			= ['''pixel_values''']
                self.assertListEqual(arg_names[:1]						,					UpperCamelCase__		)




  def 						SCREAMING_SNAKE_CASE				( self     :						str		):






         """simple docstring"""






         _lowerCamelCase						:				int    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*UpperCamelCase__		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Tuple		):






         """simple docstring"""






         def check_hidden_states_output(__lowerCAmelCase     :						str						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						Union[str, Any]		):
                _lowerCamelCase						:				Dict    			= model_class(UpperCamelCase__		)
                model.to(UpperCamelCase__		)
                model.eval()

                with torch.no_grad():
                       _lowerCamelCase						:				Optional[int]    			= model(**self._prepare_for_class(UpperCamelCase__						,					UpperCamelCase__		)		)

                _lowerCamelCase						:				Optional[Any]    			= outputs.hidden_states

                _lowerCamelCase						:				Tuple    			= len(self.model_tester.depth		)
                self.assertEqual(len(UpperCamelCase__		)						,					UpperCamelCase__		)

                # verify the first hidden states (first block)
                self.assertListEqual(
                    list(hidden_states[0].shape[-3:]		)						,					[
                        self.model_tester.embed_dim[0],
                        self.model_tester.image_size // 4,
                        self.model_tester.image_size // 4,
                    ]						,					)

         _lowerCamelCase			,    _lowerCamelCase						:				Tuple    			= self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                _lowerCamelCase						:				Dict    			= True
                check_hidden_states_output(UpperCamelCase__						,					UpperCamelCase__						,					UpperCamelCase__		)

                # check that output_hidden_states also work using config
                del inputs_dict["output_hidden_states"]
                _lowerCamelCase						:				Tuple    			= True

                check_hidden_states_output(UpperCamelCase__						,					UpperCamelCase__						,					UpperCamelCase__		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[Any]		):






         """simple docstring"""






         _lowerCamelCase						:				Optional[Any]    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__		)




  @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''		)
  def 						SCREAMING_SNAKE_CASE				( self     :						int		):






         """simple docstring"""






         pass




  @slow
  def 						SCREAMING_SNAKE_CASE				( self     :						Tuple		):






         """simple docstring"""






         for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _lowerCamelCase						:				Any    			= CvtModel.from_pretrained(UpperCamelCase__		)
                self.assertIsNotNone(UpperCamelCase__		)






def        snake_case_  (      ):


       '''simple docstring'''





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







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




  @cached_property
  def 						SCREAMING_SNAKE_CASE				( self     :						List[str]		):






         """simple docstring"""






         return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)




  @slow
  def 						SCREAMING_SNAKE_CASE				( self     :						Any		):






         """simple docstring"""






         _lowerCamelCase						:				Dict    			= CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]		).to(UpperCamelCase__		)

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

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

         # verify the logits
         _lowerCamelCase						:				Union[str, Any]    			= torch.Size((1, 1_0_0_0)		)
         self.assertEqual(outputs.logits.shape						,					UpperCamelCase__		)

         _lowerCamelCase						:				List[str]    			= torch.tensor([0.92_85, 0.90_15, -0.31_50]		).to(UpperCamelCase__		)

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







'''simple docstring'''


from dataclasses import dataclass
from typing import Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, randn_tensor
from .scheduling_utils import SchedulerMixin

@dataclass
class  SCREAMING_SNAKE_CASE (  _a		):






    """simple docstring"""





    _SCREAMING_SNAKE_CASE	  =  42
    _SCREAMING_SNAKE_CASE	  =  42
    _SCREAMING_SNAKE_CASE	  =  None

class  SCREAMING_SNAKE_CASE (  _a   ,						_a		):






    """simple docstring"""





    _SCREAMING_SNAKE_CASE	  =  2






    @register_to_config
    def __init__(  self      :					Union[str, Any]  ,						UpperCamelCase__      :					float = 0.0_2  ,						UpperCamelCase__      :					float = 1_0_0  ,						UpperCamelCase__      :					float = 1.0_0_7  ,						UpperCamelCase__      :					float = 8_0  ,						UpperCamelCase__      :					float = 0.0_5  ,						UpperCamelCase__      :					float = 5_0  ,						):


       """simple docstring"""







       UpperCamelCase					=  sigma_max

       # setable values
       UpperCamelCase					=  None
       UpperCamelCase					=  None
       UpperCamelCase					=  None  # sigma(t_i)






    def  A       (  self      :					str  ,						UpperCamelCase__      :					torch.FloatTensor  ,						UpperCamelCase__      :					Optional[int] = None ):


       """simple docstring"""







       return sample






    def  A       (  self      :					Union[str, Any]  ,						UpperCamelCase__      :					int  ,						UpperCamelCase__      :					Union[str, torch.device] = None ):


       """simple docstring"""







       UpperCamelCase					=  num_inference_steps
       UpperCamelCase					=  np.arange(0  ,						self.num_inference_steps )[::-1].copy()
       UpperCamelCase					=  torch.from_numpy(UpperCamelCase__ ).to(UpperCamelCase__ )
       UpperCamelCase					=  [
           (
               self.config.sigma_max**2
               * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1))
           )
           for i in self.timesteps
       ]
       UpperCamelCase					=  torch.tensor(UpperCamelCase__  ,						dtype=torch.floataa  ,						device=UpperCamelCase__ )






    def  A       (  self      :					Dict  ,						UpperCamelCase__      :					torch.FloatTensor  ,						UpperCamelCase__      :					float  ,						UpperCamelCase__      :					Optional[torch.Generator] = None ):


       """simple docstring"""







       if self.config.s_min <= sigma <= self.config.s_max:
          UpperCamelCase					=  min(self.config.s_churn / self.num_inference_steps  ,						2**0.5 - 1 )
       else:
          UpperCamelCase					=  0

       # sample eps ~ N(0, S_noise^2 * I)
       UpperCamelCase					=  self.config.s_noise * randn_tensor(sample.shape  ,						generator=UpperCamelCase__ ).to(sample.device )
       UpperCamelCase					=  sigma + gamma * sigma
       UpperCamelCase					=  sample + ((sigma_hat**2 - sigma**2) ** 0.5 * eps)

       return sample_hat, sigma_hat






    def  A       (  self      :					str  ,						UpperCamelCase__      :					torch.FloatTensor  ,						UpperCamelCase__      :					float  ,						UpperCamelCase__      :					float  ,						UpperCamelCase__      :					torch.FloatTensor  ,						UpperCamelCase__      :					bool = True  ,						):


       """simple docstring"""







       UpperCamelCase					=  sample_hat + sigma_hat * model_output
       UpperCamelCase					=  (sample_hat - pred_original_sample) / sigma_hat
       UpperCamelCase					=  sample_hat + (sigma_prev - sigma_hat) * derivative

       if not return_dict:
          return (sample_prev, derivative)

       return KarrasVeOutput(
           prev_sample=UpperCamelCase__  ,						derivative=UpperCamelCase__  ,						pred_original_sample=UpperCamelCase__ )






    def  A       (  self      :					List[Any]  ,						UpperCamelCase__      :					torch.FloatTensor  ,						UpperCamelCase__      :					float  ,						UpperCamelCase__      :					float  ,						UpperCamelCase__      :					torch.FloatTensor  ,						UpperCamelCase__      :					torch.FloatTensor  ,						UpperCamelCase__      :					torch.FloatTensor  ,						UpperCamelCase__      :					bool = True  ,						):


       """simple docstring"""







       UpperCamelCase					=  sample_prev + sigma_prev * model_output
       UpperCamelCase					=  (sample_prev - pred_original_sample) / sigma_prev
       UpperCamelCase					=  sample_hat + (sigma_prev - sigma_hat) * (0.5 * derivative + 0.5 * derivative_corr)

       if not return_dict:
          return (sample_prev, derivative)

       return KarrasVeOutput(
           prev_sample=UpperCamelCase__  ,						derivative=UpperCamelCase__  ,						pred_original_sample=UpperCamelCase__ )






    def  A       (  self      :					Optional[Any]  ,						UpperCamelCase__      :					str  ,						UpperCamelCase__      :					int  ,						UpperCamelCase__      :					str ):


       """simple docstring"""







       raise NotImplementedError()

