


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 					__magic_name__		(   lowercase__):


     def    UpperCAmelCase__		(					self							:   Tuple	)     ->					Any:





      '''simple docstring'''






      UpperCamelCase__    :			Optional[Any]      				=      self.config_class(**self.inputs_dict	)
      self.parent.assertTrue(hasattr(__lowercase							,		'''embed_dim'''	)	)
      self.parent.assertTrue(hasattr(__lowercase							,		'''num_heads'''	)	)



class 					__magic_name__		:


     def __init__(					self							:   Tuple							,		lowerCamelCase__							:   Optional[int]							,		lowerCamelCase__							:   Dict=13							,		lowerCamelCase__							:   str=64							,		lowerCamelCase__							:   str=3							,		lowerCamelCase__							:   Tuple=[16, 48, 96]							,		lowerCamelCase__							:   Dict=[1, 3, 6]							,		lowerCamelCase__							:   Union[str, Any]=[1, 2, 10]							,		lowerCamelCase__							:   Optional[int]=[7, 3, 3]							,		lowerCamelCase__							:   Tuple=[4, 2, 2]							,		lowerCamelCase__							:   List[str]=[2, 1, 1]							,		lowerCamelCase__							:   List[str]=[2, 2, 2]							,		lowerCamelCase__							:   Optional[Any]=[False, False, True]							,		lowerCamelCase__							:   Optional[Any]=[0.0, 0.0, 0.0]							,		lowerCamelCase__							:   Tuple=0.02							,		lowerCamelCase__							:   Tuple=1E-1_2							,		lowerCamelCase__							:   List[str]=True							,		lowerCamelCase__							:   Dict=True							,		lowerCamelCase__							:   Optional[int]=2							,		)     ->					Optional[Any]:





      '''simple docstring'''






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


     def    UpperCAmelCase__		(					self							:   List[Any]	)     ->					Union[str, Any]:





      '''simple docstring'''






      UpperCamelCase__    :			Union[str, Any]      				=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

      UpperCamelCase__    :			int      				=      None
      if self.use_labels:
       UpperCamelCase__    :			Dict      				=      ids_tensor([self.batch_size]							,		self.num_labels	)

      UpperCamelCase__    :			Optional[int]      				=      self.get_config()
      return config, pixel_values, labels


     def    UpperCAmelCase__		(					self							:   str	)     ->					Optional[int]:





      '''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    UpperCAmelCase__		(					self							:   Optional[int]							,		lowerCamelCase__							:   Any							,		lowerCamelCase__							:   Dict							,		lowerCamelCase__							:   Dict	)     ->					List[Any]:





      '''simple docstring'''






      UpperCamelCase__    :			Optional[int]      				=      CvtModel(config=__lowercase	)
      model.to(__lowercase	)
      model.eval()
      UpperCamelCase__    :			int      				=      model(__lowercase	)
      UpperCamelCase__    :			Tuple      				=      (self.image_size, self.image_size)
      UpperCamelCase__    :			Union[str, Any]      				=      image_size[0], image_size[1]
      for i in range(len(self.depth	)	):
       UpperCamelCase__    :			Tuple      				=      floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1	)
       UpperCamelCase__    :			Union[str, Any]      				=      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    UpperCAmelCase__		(					self							:   str							,		lowerCamelCase__							:   Any							,		lowerCamelCase__							:   Optional[int]							,		lowerCamelCase__							:   Optional[Any]	)     ->					int:





      '''simple docstring'''






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


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





      '''simple docstring'''






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



@require_torch
class 					__magic_name__		(   lowercase__    , lowercase__    , unittest.TestCase):
     A:						Union[str, Any]								=					(CvtModel, CvtForImageClassification) if is_torch_available() else ()
     A:						List[str]								=					(
         {'feature-extraction': CvtModel, 'image-classification': CvtForImageClassification}
         if is_torch_available()
         else {}
     )

     A:						int								=					False
     A:						str								=					False
     A:						Tuple								=					False
     A:						List[Any]								=					False
     A:						Optional[Any]								=					False


     def    UpperCAmelCase__		(					self							:   List[str]	)     ->					Any:





      '''simple docstring'''






      UpperCamelCase__    :			Union[str, Any]      				=      CvtModelTester(self	)
      UpperCamelCase__    :			Any      				=      ConfigTester(self							,		config_class=__lowercase							,		has_text_modality=__lowercase							,		hidden_size=37	)


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





      '''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    UpperCAmelCase__		(					self							:   Union[str, Any]	)     ->					Union[str, Any]:





      '''simple docstring'''






      return


     @unittest.skip(reason='''Cvt does not output attentions'''	)
     def    UpperCAmelCase__		(					self							:   Optional[Any]	)     ->					str:





      '''simple docstring'''






      pass


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





      '''simple docstring'''






      pass


     @unittest.skip(reason='''Cvt does not support input and output embeddings'''	)
     def    UpperCAmelCase__		(					self							:   Tuple	)     ->					Union[str, Any]:





      '''simple docstring'''






      pass


     def    UpperCAmelCase__		(					self							:   int	)     ->					str:





      '''simple docstring'''






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

      for model_class in self.all_model_classes:
       UpperCamelCase__    :			str      				=      model_class(__lowercase	)
       UpperCamelCase__    :			Dict      				=      inspect.signature(model.forward	)
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       UpperCamelCase__    :			Optional[int]      				=      [*signature.parameters.keys()]

       UpperCamelCase__    :			List[str]      				=      ["""pixel_values"""]
       self.assertListEqual(arg_names[:1]							,		__lowercase	)


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





      '''simple docstring'''






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


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





      '''simple docstring'''






      def check_hidden_states_output(lowerCamelCase__							:   str							,		lowerCamelCase__							:   Any							,		lowerCamelCase__							:   Tuple	):
       UpperCamelCase__    :			int      				=      model_class(__lowercase	)
       model.to(__lowercase	)
       model.eval()

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

       UpperCamelCase__    :			Optional[int]      				=      outputs.hidden_states

       UpperCamelCase__    :			Union[str, Any]      				=      len(self.model_tester.depth	)
       self.assertEqual(len(__lowercase	)							,		__lowercase	)

       # 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,
           ]							,		)

      UpperCamelCase__    :			str      				=      self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
       UpperCamelCase__    :			Union[str, Any]      				=      True
       check_hidden_states_output(__lowercase							,		__lowercase							,		__lowercase	)

       # check that output_hidden_states also work using config
       del inputs_dict["output_hidden_states"]
       UpperCamelCase__    :			List[Any]      				=      True

       check_hidden_states_output(__lowercase							,		__lowercase							,		__lowercase	)


     def    UpperCAmelCase__		(					self							:   int	)     ->					Any:





      '''simple docstring'''






      UpperCamelCase__    :			Tuple      				=      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__lowercase	)


     @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''	)
     def    UpperCAmelCase__		(					self							:   str	)     ->					Optional[Any]:





      '''simple docstring'''






      pass


     @slow
     def    UpperCAmelCase__		(					self							:   List[str]	)     ->					Union[str, Any]:





      '''simple docstring'''






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



def 	_a     ( ):







 """simple docstring"""



 UpperCamelCase__    :			Union[str, Any]      				=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
 return image






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


     @cached_property
     def    UpperCAmelCase__		(					self							:   Optional[Any]	)     ->					List[str]:





      '''simple docstring'''






      return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)


     @slow
     def    UpperCAmelCase__		(					self							:   Dict	)     ->					Any:





      '''simple docstring'''






      UpperCamelCase__    :			Union[str, Any]      				=      CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]	).to(__lowercase	)

      UpperCamelCase__    :			Optional[int]      				=      self.default_image_processor
      UpperCamelCase__    :			Dict      				=      prepare_img()
      UpperCamelCase__    :			Any      				=      image_processor(images=__lowercase							,		return_tensors='''pt'''	).to(__lowercase	)

      # forward pass
      with torch.no_grad():
       UpperCamelCase__    :			str      				=      model(**__lowercase	)

      # verify the logits
      UpperCamelCase__    :			str      				=      torch.Size((1, 1000)	)
      self.assertEqual(outputs.logits.shape							,		__lowercase	)

      UpperCamelCase__    :			Optional[Any]      				=      torch.tensor([0.9285, 0.9015, -0.3150]	).to(__lowercase	)

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










import warnings

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a		:       Tuple			     = logging.get_logger(__name__)

a		:       Optional[Any]			     = {
    "RUCAIBox/mvp": "https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json",
}
class     a    ( lowercase__   ):



      """simple docstring"""


      a  : str							       =	'mvp'
      a  : Optional[Any]							       =	['past_key_values']
      a  : int							       =	{'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}
      def __init__(      self					:			Dict		,      __lowercase					:			Tuple=50267		,      __lowercase					:			Optional[int]=1024		,      __lowercase					:			Any=12		,      __lowercase					:			List[Any]=4096		,      __lowercase					:			Optional[Any]=16		,      __lowercase					:			List[str]=12		,      __lowercase					:			Optional[Any]=4096		,      __lowercase					:			Tuple=16		,      __lowercase					:			Union[str, Any]=0.0		,      __lowercase					:			str=0.0		,      __lowercase					:			Any="gelu"		,      __lowercase					:			Any=1024		,      __lowercase					:			List[str]=0.1		,      __lowercase					:			List[Any]=0.0		,      __lowercase					:			int=0.0		,      __lowercase					:			str=0.02		,      __lowercase					:			Tuple=0.0		,      __lowercase					:			Union[str, Any]=False		,      __lowercase					:			Dict=True		,      __lowercase					:			List[Any]=1		,      __lowercase					:			Optional[Any]=0		,      __lowercase					:			Union[str, Any]=2		,      __lowercase					:			Optional[int]=True		,      __lowercase					:			Dict=2		,      __lowercase					:			int=2		,      __lowercase					:			Union[str, Any]=False		,      __lowercase					:			Union[str, Any]=100		,      __lowercase					:			str=800		,      **__lowercase					:			Union[str, Any]		,      )							->      Any:
        __UpperCAmelCase							:	List[str]           =  vocab_size
        __UpperCAmelCase							:	Union[str, Any]           =  max_position_embeddings
        __UpperCAmelCase							:	Optional[Any]           =  d_model
        __UpperCAmelCase							:	int           =  encoder_ffn_dim
        __UpperCAmelCase							:	Tuple           =  encoder_layers
        __UpperCAmelCase							:	List[str]           =  encoder_attention_heads
        __UpperCAmelCase							:	Any           =  decoder_ffn_dim
        __UpperCAmelCase							:	List[Any]           =  decoder_layers
        __UpperCAmelCase							:	str           =  decoder_attention_heads
        __UpperCAmelCase							:	Optional[int]           =  dropout
        __UpperCAmelCase							:	Tuple           =  attention_dropout
        __UpperCAmelCase							:	int           =  activation_dropout
        __UpperCAmelCase							:	List[Any]           =  activation_function
        __UpperCAmelCase							:	Tuple           =  init_std
        __UpperCAmelCase							:	Any           =  encoder_layerdrop
        __UpperCAmelCase							:	Union[str, Any]           =  decoder_layerdrop
        __UpperCAmelCase							:	List[Any]           =  classifier_dropout
        __UpperCAmelCase							:	Optional[int]           =  use_cache
        __UpperCAmelCase							:	int           =  encoder_layers
        __UpperCAmelCase							:	Union[str, Any]           =  scale_embedding  # scale factor will be sqrt(d_model) if True
        __UpperCAmelCase							:	Tuple           =  use_prompt
        __UpperCAmelCase							:	str           =  prompt_length
        __UpperCAmelCase							:	Union[str, Any]           =  prompt_mid_dim

        super().__init__(
            pad_token_id=__lowercase		,      bos_token_id=__lowercase		,      eos_token_id=__lowercase		,      is_encoder_decoder=__lowercase		,      decoder_start_token_id=__lowercase		,      forced_eos_token_id=__lowercase		,      **__lowercase		,      )

        if self.forced_bos_token_id is None and kwargs.get("""force_bos_token_to_be_generated"""		,      __lowercase					):
          __UpperCAmelCase							:	Optional[Any]           =  self.bos_token_id
          warnings.warn(
              f"""Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. """
              """The config can simply be saved and uploaded again to be fixed."""					)


