




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 						_A (  _lowerCamelCase				):






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






       """simple docstring"""







       lowercase :       List[str]       =			self.config_class(**self.inputs_dict				)
       self.parent.assertTrue(hasattr(_A				,						'''embed_dim'''				)				)
       self.parent.assertTrue(hasattr(_A				,						'''num_heads'''				)				)


class 						_A :






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






       """simple docstring"""







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






  def  __a      (  self		:     Any				)		->   Optional[Any]:






       """simple docstring"""







       lowercase :       int       =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

       lowercase :       Union[str, Any]       =			self.get_config()
       return config, pixel_values, labels






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






       """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  __a      (  self		:     int				,						_A		:     Optional[Any]				,						_A		:     List[Any]				,						_A		:     Union[str, Any]				)		->   int:






       """simple docstring"""







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






       """simple docstring"""







       lowercase :       Tuple       =			self.num_labels
       lowercase :       Optional[int]       =			CvtForImageClassification(_A				)
       model.to(_A				)
       model.eval()
       lowercase :       str       =			model(_A				,						labels=_A				)
       self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.num_labels)				)






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






       """simple docstring"""







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


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

  _UpperCamelCase       :      Dict			=      False
  _UpperCamelCase       :      Dict			=      False
  _UpperCamelCase       :      Dict			=      False
  _UpperCamelCase       :      Any			=      False
  _UpperCamelCase       :      str			=      False






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






       """simple docstring"""







       lowercase :       List[str]       =			CvtModelTester(self				)
       lowercase :       Optional[int]       =			ConfigTester(self				,						config_class=_A				,						has_text_modality=_A				,						hidden_size=37				)






  def  __a      (  self		:     Union[str, 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  __a      (  self		:     List[str]				)		->   str:






       """simple docstring"""







       return






  @unittest.skip(reason='''Cvt does not output attentions'''				)
  def  __a      (  self		:     Dict				)		->   Dict:






       """simple docstring"""







       pass






  @unittest.skip(reason='''Cvt does not use inputs_embeds'''				)
  def  __a      (  self		:     str				)		->   Optional[int]:






       """simple docstring"""







       pass






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






       """simple docstring"""







       pass






  def  __a      (  self		:     str				)		->   Dict:






       """simple docstring"""







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

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

            lowercase :       Tuple       =			['''pixel_values''']
            self.assertListEqual(arg_names[:1]				,						_A				)






  def  __a      (  self		:     str				)		->   Dict:






       """simple docstring"""







       lowercase :       Tuple       =			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_A				)






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






       """simple docstring"""







       def check_hidden_states_output(_A		:     Union[str, Any]				,						_A		:     int				,						_A		:     Union[str, Any]				):
            lowercase :       Any       =			model_class(_A				)
            model.to(_A				)
            model.eval()

            with torch.no_grad():
                 lowercase :       Dict       =			model(**self._prepare_for_class(_A				,						_A				)				)

            lowercase :       Union[str, Any]       =			outputs.hidden_states

            lowercase :       Optional[int]       =			len(self.model_tester.depth				)
            self.assertEqual(len(_A				)				,						_A				)

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

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

       for model_class in self.all_model_classes:
            lowercase :       int       =			True
            check_hidden_states_output(_A				,						_A				,						_A				)

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            lowercase :       Union[str, Any]       =			True

            check_hidden_states_output(_A				,						_A				,						_A				)






  def  __a      (  self		:     List[Any]				)		->   Dict:






       """simple docstring"""







       lowercase :       str       =			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_A				)






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






       """simple docstring"""







       pass






  @slow
  def  __a      (  self		:     Tuple				)		->   Tuple:






       """simple docstring"""







       for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            lowercase :       List[str]       =			CvtModel.from_pretrained(_A				)
            self.assertIsNotNone(_A				)






def  snake_case(    )			->							List[str]:





     '''simple docstring'''


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



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






  @cached_property
  def  __a      (  self		:     Any				)		->   str:






       """simple docstring"""







       return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]				)






  @slow
  def  __a      (  self		:     Any				)		->   str:






       """simple docstring"""







       lowercase :       str       =			CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]				).to(_A				)

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

       # forward pass
       with torch.no_grad():
            lowercase :       str       =			model(**_A				)

       # verify the logits
       lowercase :       Any       =			torch.Size((1, 1_000)				)
       self.assertEqual(outputs.logits.shape				,						_A				)

       lowercase :       str       =			torch.tensor([0.9_285, 0.9_015, -0.3_150]				).to(_A				)

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





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCAmelCase_						=			logging.get_logger(__name__)

lowerCAmelCase_						=			{
    'facebook/xmod-base': 'https://huggingface.co/facebook/xmod-base/resolve/main/config.json',
    'facebook/xmod-large-prenorm': 'https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json',
    'facebook/xmod-base-13-125k': 'https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json',
    'facebook/xmod-base-30-125k': 'https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json',
    'facebook/xmod-base-30-195k': 'https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json',
    'facebook/xmod-base-60-125k': 'https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json',
    'facebook/xmod-base-60-265k': 'https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json',
    'facebook/xmod-base-75-125k': 'https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json',
    'facebook/xmod-base-75-269k': 'https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json',
}
class 						_A (  _lowerCamelCase				):
  _UpperCamelCase       :      Tuple			=      '''xmod'''






  def __init__(  self		:     Optional[Any]				,						_A		:     Union[str, Any]=30_522				,						_A		:     List[Any]=768				,						_A		:     Optional[Any]=12				,						_A		:     Any=12				,						_A		:     Tuple=3_072				,						_A		:     Optional[int]="gelu"				,						_A		:     List[Any]=0.1				,						_A		:     str=0.1				,						_A		:     List[Any]=512				,						_A		:     List[str]=2				,						_A		:     str=0.02				,						_A		:     Any=1E-12				,						_A		:     Union[str, Any]=1				,						_A		:     List[Any]=0				,						_A		:     Dict=2				,						_A		:     int="absolute"				,						_A		:     Dict=True				,						_A		:     int=None				,						_A		:     List[str]=False				,						_A		:     Dict=2				,						_A		:     int=False				,						_A		:     Optional[int]=True				,						_A		:     Any=True				,						_A		:     Optional[int]=("en_XX",)				,						_A		:     Any=None				,						**_A		:     Optional[int]				,						)		->   Union[str, Any]:






       """simple docstring"""







       super().__init__(pad_token_id=_A				,						bos_token_id=_A				,						eos_token_id=_A				,						**_A				)

       lowercase :       Optional[Any]       =			vocab_size
       lowercase :       Union[str, Any]       =			hidden_size
       lowercase :       Optional[Any]       =			num_hidden_layers
       lowercase :       Dict       =			num_attention_heads
       lowercase :       Union[str, Any]       =			hidden_act
       lowercase :       Tuple       =			intermediate_size
       lowercase :       List[str]       =			hidden_dropout_prob
       lowercase :       Union[str, Any]       =			attention_probs_dropout_prob
       lowercase :       Dict       =			max_position_embeddings
       lowercase :       Any       =			type_vocab_size
       lowercase :       Optional[Any]       =			initializer_range
       lowercase :       str       =			layer_norm_eps
       lowercase :       Tuple       =			position_embedding_type
       lowercase :       Optional[Any]       =			use_cache
       lowercase :       int       =			classifier_dropout
       lowercase :       Optional[int]       =			pre_norm
       lowercase :       Any       =			adapter_reduction_factor
       lowercase :       Union[str, Any]       =			adapter_layer_norm
       lowercase :       Optional[int]       =			adapter_reuse_layer_norm
       lowercase :       Optional[Any]       =			ln_before_adapter
       lowercase :       Union[str, Any]       =			list(_A				)
       lowercase :       List[Any]       =			default_language


class 						_A (  _lowerCamelCase				):






  @property
  def  __a      (  self		:     Union[str, Any]				)		->   Mapping[str, Mapping[int, str]]:






       """simple docstring"""







       if self.task == "multiple-choice":
            lowercase :       Any       =			{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
       else:
            lowercase :       Union[str, Any]       =			{0: '''batch''', 1: '''sequence'''}
       return OrderedDict(
           [
               ('''input_ids''', dynamic_axis),
               ('''attention_mask''', dynamic_axis),
           ]				)